cloudllm 0.15.9

A batteries-included Rust toolkit for building intelligent agents with LLM integration, multi-protocol tool support, multi-agent orchestration, and MentisDB-backed durable memory.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
use async_trait::async_trait;
use cloudllm::client_wrapper::{ClientWrapper, Message, Role, TokenUsage, ToolDefinition};
use cloudllm::orchestration::{Orchestration, OrchestrationMode, RalphTask};
use cloudllm::Agent;
use std::sync::Arc;

struct MockClient {
    name: String,
    response: String,
}

#[async_trait]
impl ClientWrapper for MockClient {
    async fn send_message(
        &self,
        _messages: &[Message],
        _tools: Option<Vec<ToolDefinition>>,
    ) -> Result<Message, Box<dyn std::error::Error>> {
        Ok(Message {
            role: Role::Assistant,
            content: Arc::from(self.response.as_str()),
            tool_calls: vec![],
        })
    }

    fn model_name(&self) -> &str {
        &self.name
    }

    fn provider_name(&self) -> &str {
        "mock"
    }

    async fn get_last_usage(&self) -> Option<TokenUsage> {
        None
    }
}

#[tokio::test]
async fn test_agent_creation() {
    let client = Arc::new(MockClient {
        name: "mock".to_string(),
        response: "test response".to_string(),
    });

    let agent = Agent::new("agent1", "Test Agent", client)
        .with_expertise("Testing")
        .with_personality("Thorough and detail-oriented");

    assert_eq!(agent.id, "agent1");
    assert_eq!(agent.name, "Test Agent");
    assert_eq!(agent.expertise, Some("Testing".to_string()));
}

#[tokio::test]
async fn test_orchestration_parallel_mode() {
    let agent1 = Agent::new(
        "agent1",
        "Agent 1",
        Arc::new(MockClient {
            name: "mock1".to_string(),
            response: "Response from agent 1".to_string(),
        }),
    );

    let agent2 = Agent::new(
        "agent2",
        "Agent 2",
        Arc::new(MockClient {
            name: "mock2".to_string(),
            response: "Response from agent 2".to_string(),
        }),
    );

    let mut orchestration = Orchestration::new("test-orchestration", "Test Orchestration")
        .with_mode(OrchestrationMode::Parallel);

    orchestration.add_agent(agent1).unwrap();
    orchestration.add_agent(agent2).unwrap();

    let response = orchestration.run("Test question", 1).await.unwrap();

    assert_eq!(response.messages.len(), 2);
    assert!(response.is_complete);
}

#[tokio::test]
async fn test_orchestration_round_robin_mode() {
    let agent1 = Agent::new(
        "agent1",
        "Agent 1",
        Arc::new(MockClient {
            name: "mock1".to_string(),
            response: "First agent response".to_string(),
        }),
    );

    let agent2 = Agent::new(
        "agent2",
        "Agent 2",
        Arc::new(MockClient {
            name: "mock2".to_string(),
            response: "Second agent response".to_string(),
        }),
    );

    let mut orchestration = Orchestration::new("test-orchestration", "Test Orchestration")
        .with_mode(OrchestrationMode::RoundRobin);

    orchestration.add_agent(agent1).unwrap();
    orchestration.add_agent(agent2).unwrap();

    let response = orchestration.run("Test question", 2).await.unwrap();

    assert_eq!(response.messages.len(), 4); // 2 agents * 2 rounds
    assert!(response.is_complete);
}

#[tokio::test]
async fn test_agent_with_tool_execution() {
    use cloudllm::tool_protocol::{
        ToolMetadata, ToolParameter, ToolParameterType, ToolRegistry, ToolResult,
    };
    use cloudllm::tool_protocols::CustomToolProtocol;
    use tokio::sync::Mutex as TokioMutex;

    // Create a custom tool adapter
    let adapter = CustomToolProtocol::new();

    // Register a simple calculator tool
    adapter
        .register_tool(
            ToolMetadata::new("add", "Adds two numbers")
                .with_parameter(ToolParameter::new("a", ToolParameterType::Number).required())
                .with_parameter(ToolParameter::new("b", ToolParameterType::Number).required()),
            Arc::new(|params| {
                let a = params["a"].as_f64().unwrap_or(0.0);
                let b = params["b"].as_f64().unwrap_or(0.0);
                Ok(ToolResult::success(serde_json::json!({"sum": a + b})))
            }),
        )
        .await;

    let mut registry = ToolRegistry::new(Arc::new(adapter));
    // Discover tools from the adapter
    registry.discover_tools_from_primary().await.unwrap();

    // Create a mock client that will respond with a tool call
    struct ToolCallingMockClient {
        call_count: Arc<TokioMutex<usize>>,
    }

    #[async_trait]
    impl ClientWrapper for ToolCallingMockClient {
        async fn send_message(
            &self,
            messages: &[Message],
            tools: Option<Vec<ToolDefinition>>,
        ) -> Result<Message, Box<dyn std::error::Error>> {
            let mut count = self.call_count.lock().await;
            *count += 1;

            // First call: return a tool call
            // Second call: return final response
            let response = if *count == 1 {
                let tools = tools.expect("native tool definitions should be provided");
                let add_tool = tools
                    .iter()
                    .find(|tool| tool.name == "add")
                    .expect("expected add tool definition");
                if add_tool.description != "Adds two numbers" {
                    panic!("Unexpected add tool description: {}", add_tool.description);
                }

                // Return tool call
                r#"{"tool_call": {"name": "add", "parameters": {"a": 5, "b": 3}}}"#
            } else {
                // Verify tool result was provided
                let last_msg = messages.last().unwrap();
                let last_content = last_msg.content.as_ref();
                if !last_content.contains("Tool 'add' executed successfully") {
                    panic!(
                        "Last message doesn't contain tool result. Content:\n{}",
                        last_content
                    );
                }

                "The sum is 8"
            };

            Ok(Message {
                role: Role::Assistant,
                content: Arc::from(response),
                tool_calls: vec![],
            })
        }

        fn model_name(&self) -> &str {
            "tool-mock"
        }

        fn provider_name(&self) -> &str {
            "mock"
        }

        async fn get_last_usage(&self) -> Option<TokenUsage> {
            None
        }
    }

    let agent = Agent::new(
        "calculator",
        "Calculator Agent",
        Arc::new(ToolCallingMockClient {
            call_count: Arc::new(TokioMutex::new(0)),
        }),
    )
    .with_tools(registry);

    let response = agent
        .generate("You are a helpful assistant", "What is 5 + 3?", &[])
        .await
        .unwrap();

    assert_eq!(response, "The sum is 8");
}

#[tokio::test]
async fn test_debate_mode_convergence() {
    use tokio::sync::Mutex as TokioMutex;

    // Mock client that returns increasingly similar responses
    struct ConvergingMockClient {
        call_count: Arc<TokioMutex<usize>>,
        agent_id: String,
    }

    #[async_trait]
    impl ClientWrapper for ConvergingMockClient {
        async fn send_message(
            &self,
            _messages: &[Message],
            _tools: Option<Vec<ToolDefinition>>,
        ) -> Result<Message, Box<dyn std::error::Error>> {
            let mut count = self.call_count.lock().await;
            *count += 1;

            // Simulate agents converging on a solution over multiple rounds
            let response = match *count {
                1 => format!("Agent {}: I think we should use approach A", self.agent_id),
                2 => format!(
                    "Agent {}: Approach A seems reasonable but needs refinement",
                    self.agent_id
                ),
                3 => format!(
                    "Agent {}: After consideration approach A with refinement is best solution",
                    self.agent_id
                ),
                _ => format!(
                    "Agent {}: I agree approach A with refinement is the best solution",
                    self.agent_id
                ),
            };

            Ok(Message {
                role: Role::Assistant,
                content: Arc::from(response.as_str()),
                tool_calls: vec![],
            })
        }

        fn model_name(&self) -> &str {
            "converging-mock"
        }

        fn provider_name(&self) -> &str {
            "mock"
        }

        async fn get_last_usage(&self) -> Option<TokenUsage> {
            None
        }
    }

    let agent1 = Agent::new(
        "agent1",
        "Agent 1",
        Arc::new(ConvergingMockClient {
            call_count: Arc::new(TokioMutex::new(0)),
            agent_id: "1".to_string(),
        }),
    );

    let agent2 = Agent::new(
        "agent2",
        "Agent 2",
        Arc::new(ConvergingMockClient {
            call_count: Arc::new(TokioMutex::new(0)),
            agent_id: "2".to_string(),
        }),
    );

    let mut orchestration = Orchestration::new("debate-orchestration", "Debate Orchestration")
        .with_mode(OrchestrationMode::Debate {
            max_rounds: 5,
            convergence_threshold: Some(0.6), // 60% similarity threshold
        });

    orchestration.add_agent(agent1).unwrap();
    orchestration.add_agent(agent2).unwrap();

    let response = orchestration
        .run("What approach should we use?", 5)
        .await
        .unwrap();

    // Should converge before max rounds (5)
    assert!(response.round < 5);
    assert!(response.is_complete);

    // Should have a convergence score
    assert!(response.convergence_score.is_some());
    let score = response.convergence_score.unwrap();
    assert!(score >= 0.6, "Convergence score {} should be >= 0.6", score);
}

#[tokio::test]
async fn test_ralph_mode_completion() {
    use tokio::sync::Mutex as TokioMutex;

    // Mock client that completes task1 on first call, task2 on second call
    struct RalphCompletingClient {
        call_count: Arc<TokioMutex<usize>>,
    }

    #[async_trait]
    impl ClientWrapper for RalphCompletingClient {
        async fn send_message(
            &self,
            _messages: &[Message],
            _tools: Option<Vec<ToolDefinition>>,
        ) -> Result<Message, Box<dyn std::error::Error>> {
            let mut count = self.call_count.lock().await;
            *count += 1;

            let response = match *count {
                1 => "I've implemented the HTML structure. [TASK_COMPLETE:task1]".to_string(),
                _ => "Game loop is done. [TASK_COMPLETE:task2]".to_string(),
            };

            Ok(Message {
                role: Role::Assistant,
                content: Arc::from(response.as_str()),
                tool_calls: vec![],
            })
        }

        fn model_name(&self) -> &str {
            "ralph-mock"
        }

        fn provider_name(&self) -> &str {
            "mock"
        }

        async fn get_last_usage(&self) -> Option<TokenUsage> {
            None
        }
    }

    let agent = Agent::new(
        "builder",
        "Builder Agent",
        Arc::new(RalphCompletingClient {
            call_count: Arc::new(TokioMutex::new(0)),
        }),
    );

    let tasks = vec![
        RalphTask::new("task1", "HTML Structure", "Create the HTML boilerplate"),
        RalphTask::new("task2", "Game Loop", "Implement the game loop"),
    ];

    let mut orchestration =
        Orchestration::new("ralph-test", "Ralph Test").with_mode(OrchestrationMode::Ralph {
            tasks,
            max_iterations: 5,
        });

    orchestration.add_agent(agent).unwrap();

    let response = orchestration.run("Build a breakout game", 1).await.unwrap();

    assert!(response.is_complete);
    assert_eq!(response.convergence_score, Some(1.0));
    assert!(response.round <= 5);
}

#[tokio::test]
async fn test_ralph_mode_max_iterations() {
    // Mock client that never emits completion markers
    let agent = Agent::new(
        "lazy",
        "Lazy Agent",
        Arc::new(MockClient {
            name: "lazy-mock".to_string(),
            response: "I'm working on it but not done yet.".to_string(),
        }),
    );

    let tasks = vec![
        RalphTask::new("task1", "Task One", "Do something"),
        RalphTask::new("task2", "Task Two", "Do something else"),
    ];

    let max_iterations = 3;

    let mut orchestration = Orchestration::new("ralph-max-test", "Ralph Max Test").with_mode(
        OrchestrationMode::Ralph {
            tasks,
            max_iterations,
        },
    );

    orchestration.add_agent(agent).unwrap();

    let response = orchestration.run("Do the tasks", 1).await.unwrap();

    assert!(!response.is_complete);
    assert_eq!(response.round, max_iterations);
    assert_eq!(response.convergence_score.unwrap(), 0.0);
}

#[tokio::test]
async fn test_ralph_mode_empty_tasks() {
    let agent = Agent::new(
        "agent1",
        "Agent 1",
        Arc::new(MockClient {
            name: "mock".to_string(),
            response: "test".to_string(),
        }),
    );

    let mut orchestration =
        Orchestration::new("ralph-empty", "Ralph Empty").with_mode(OrchestrationMode::Ralph {
            tasks: vec![],
            max_iterations: 5,
        });

    orchestration.add_agent(agent).unwrap();

    let response = orchestration.run("Do nothing", 1).await.unwrap();

    assert!(response.is_complete);
    assert_eq!(response.convergence_score, Some(1.0));
    assert_eq!(response.round, 0);
    assert!(response.messages.is_empty());
}

#[tokio::test]
async fn test_agent_send_uses_session() {
    let client = Arc::new(MockClient {
        name: "mock".to_string(),
        response: "session response".to_string(),
    });

    let mut agent = Agent::new("agent1", "Test Agent", client);
    agent.set_system_prompt("You are a helpful assistant.");

    let result = agent.send("Hello").await.unwrap();
    assert_eq!(result.content, "session response");

    // After send, session should have history (user message + assistant response)
    assert!(agent.session_history_len() >= 2);
}

#[tokio::test]
async fn test_agent_receive_message() {
    let client = Arc::new(MockClient {
        name: "mock".to_string(),
        response: "test".to_string(),
    });

    let mut agent = Agent::new("agent1", "Test Agent", client);
    assert_eq!(agent.session_history_len(), 0);

    agent.receive_message(Role::Assistant, "[Agent 2]: Some context".to_string());
    assert_eq!(agent.session_history_len(), 1);

    agent.receive_message(Role::Assistant, "[Agent 3]: More context".to_string());
    assert_eq!(agent.session_history_len(), 2);
}

#[tokio::test]
async fn test_hub_routing_no_duplication() {
    use tokio::sync::Mutex as TokioMutex;

    /// Mock client that counts messages received per call
    struct CountingMockClient {
        message_counts: Arc<TokioMutex<Vec<usize>>>,
    }

    #[async_trait]
    impl ClientWrapper for CountingMockClient {
        async fn send_message(
            &self,
            messages: &[Message],
            _tools: Option<Vec<ToolDefinition>>,
        ) -> Result<Message, Box<dyn std::error::Error>> {
            let mut counts = self.message_counts.lock().await;
            counts.push(messages.len());
            Ok(Message {
                role: Role::Assistant,
                content: Arc::from("response"),
                tool_calls: vec![],
            })
        }

        fn model_name(&self) -> &str {
            "counting-mock"
        }

        fn provider_name(&self) -> &str {
            "mock"
        }

        async fn get_last_usage(&self) -> Option<TokenUsage> {
            None
        }
    }

    let counts1 = Arc::new(TokioMutex::new(Vec::new()));
    let counts2 = Arc::new(TokioMutex::new(Vec::new()));

    let agent1 = Agent::new(
        "agent1",
        "Agent 1",
        Arc::new(CountingMockClient {
            message_counts: counts1.clone(),
        }),
    );

    let agent2 = Agent::new(
        "agent2",
        "Agent 2",
        Arc::new(CountingMockClient {
            message_counts: counts2.clone(),
        }),
    );

    let mut orchestration =
        Orchestration::new("test", "Test").with_mode(OrchestrationMode::RoundRobin);

    orchestration.add_agent(agent1).unwrap();
    orchestration.add_agent(agent2).unwrap();

    let response = orchestration.run("Test question", 2).await.unwrap();
    assert_eq!(response.messages.len(), 4); // 2 agents * 2 rounds

    // Verify agent1's message counts grow incrementally (not full broadcast)
    let c1 = counts1.lock().await;
    let c2 = counts2.lock().await;

    // Round 1: agent1 sees system + user = 2 messages
    // Round 1: agent2 sees system + agent1's response + user = 3 messages
    // Round 2: agent1 sees prior + new messages + user (grows incrementally)
    // Round 2: agent2 sees prior + new messages + user (grows incrementally)
    //
    // Key assertion: agent2's first call should NOT receive the entire
    // conversation_history duplicated (which would be much larger)
    assert!(
        c1.len() >= 2,
        "Agent 1 should have been called at least 2 times"
    );
    assert!(
        c2.len() >= 2,
        "Agent 2 should have been called at least 2 times"
    );

    // In the old broadcast mode, agent2's second call would receive ALL messages
    // from ALL prior rounds plus the full conversation_history. With hub-routing,
    // each call receives only the session's accumulated messages.
    // Verify the message count is reasonable (not exploding)
    for &count in c1.iter() {
        assert!(
            count < 20,
            "Agent 1 message count {} should be reasonable",
            count
        );
    }
    for &count in c2.iter() {
        assert!(
            count < 20,
            "Agent 2 message count {} should be reasonable",
            count
        );
    }
}

#[tokio::test]
async fn test_agent_fork_with_context() {
    let client = Arc::new(MockClient {
        name: "mock".to_string(),
        response: "forked response".to_string(),
    });

    let mut agent = Agent::new("agent1", "Test Agent", client);
    agent.set_system_prompt("You are a helpful assistant.");
    agent.receive_message(Role::User, "Hello".to_string());
    agent.receive_message(Role::Assistant, "Hi there!".to_string());
    assert_eq!(agent.session_history_len(), 2);

    let forked = agent.fork_with_context();
    assert_eq!(forked.session_history_len(), 2);
    assert_eq!(forked.id, "agent1");
    assert_eq!(forked.name, "Test Agent");

    // Regular fork should have empty session
    let regular_fork = agent.fork();
    assert_eq!(regular_fork.session_history_len(), 0);
}