Skip to main content

mojentic/agents/
iterative_problem_solver.rs

1//! Iterative problem solver agent that uses tools to break down and solve complex problems.
2//!
3//! This agent uses a chat-based approach to iteratively work on solving a problem,
4//! continuing until it succeeds, fails explicitly, or reaches the maximum number of iterations.
5
6use crate::error::Result;
7use crate::llm::chat_session::ChatSession;
8use crate::llm::tools::LlmTool;
9use crate::llm::LlmBroker;
10use tracing::{info, warn};
11
12/// An agent that iteratively attempts to solve a problem using available tools.
13///
14/// The solver uses a chat-based approach to break down and solve complex problems.
15/// It will continue attempting to solve the problem until it either succeeds,
16/// fails explicitly, or reaches the maximum number of iterations.
17///
18/// # Examples
19///
20/// ```ignore
21/// use mojentic::agents::IterativeProblemSolver;
22/// use mojentic::llm::{LlmBroker, LlmGateway};
23/// use mojentic::llm::gateways::OllamaGateway;
24/// use mojentic::llm::tools::simple_date_tool::SimpleDateTool;
25/// use std::sync::Arc;
26///
27/// #[tokio::main]
28/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
29///     let gateway = Arc::new(OllamaGateway::default());
30///     let broker = LlmBroker::new("qwen3:32b", gateway, None);
31///
32///     let tools: Vec<Box<dyn LlmTool>> = vec![Box::new(SimpleDateTool)];
33///
34///     let solver = IterativeProblemSolver::builder(broker)
35///         .tools(tools)
36///         .max_iterations(5)
37///         .build();
38///
39///     let result = solver.solve("What's the date next Friday?").await?;
40///     println!("Result: {}", result);
41///
42///     Ok(())
43/// }
44/// ```
45pub struct IterativeProblemSolver {
46    chat: ChatSession,
47    max_iterations: usize,
48}
49
50impl IterativeProblemSolver {
51    /// Create a new problem solver with default settings.
52    ///
53    /// # Arguments
54    ///
55    /// * `broker` - The LLM broker to use for generating responses
56    ///
57    /// # Examples
58    ///
59    /// ```ignore
60    /// use mojentic::agents::IterativeProblemSolver;
61    /// use mojentic::llm::LlmBroker;
62    ///
63    /// let solver = IterativeProblemSolver::new(broker);
64    /// ```
65    pub fn new(broker: LlmBroker) -> Self {
66        Self::builder(broker).build()
67    }
68
69    /// Create a problem solver builder for custom configuration.
70    ///
71    /// # Arguments
72    ///
73    /// * `broker` - The LLM broker to use for generating responses
74    ///
75    /// # Examples
76    ///
77    /// ```ignore
78    /// use mojentic::agents::IterativeProblemSolver;
79    ///
80    /// let solver = IterativeProblemSolver::builder(broker)
81    ///     .max_iterations(10)
82    ///     .system_prompt("You are a specialized problem solver.")
83    ///     .tools(vec![Box::new(SimpleDateTool)])
84    ///     .build();
85    /// ```
86    pub fn builder(broker: LlmBroker) -> IterativeProblemSolverBuilder {
87        IterativeProblemSolverBuilder::new(broker)
88    }
89
90    /// Execute the problem-solving process.
91    ///
92    /// This method runs the iterative problem-solving process, continuing until one of
93    /// these conditions is met:
94    /// - The task is completed successfully (response contains "DONE")
95    /// - The task fails explicitly (response contains "FAIL")
96    /// - The maximum number of iterations is reached
97    ///
98    /// After completion, the agent requests a summary of the final result.
99    ///
100    /// # Arguments
101    ///
102    /// * `problem` - The problem or request to be solved
103    ///
104    /// # Returns
105    ///
106    /// A summary of the final result, excluding the process details
107    ///
108    /// # Examples
109    ///
110    /// ```ignore
111    /// let result = solver.solve("Calculate the date 7 days from now").await?;
112    /// println!("Solution: {}", result);
113    /// ```
114    pub async fn solve(&mut self, problem: &str) -> Result<String> {
115        let mut iterations_remaining = self.max_iterations;
116
117        loop {
118            let result = self.step(problem).await?;
119
120            // Check for explicit failure
121            if result.to_lowercase().contains("fail") {
122                info!(user_request = problem, result = result.as_str(), "Task failed");
123                break;
124            }
125
126            // Check for successful completion
127            if result.to_lowercase().contains("done") {
128                info!(user_request = problem, result = result.as_str(), "Task completed");
129                break;
130            }
131
132            iterations_remaining -= 1;
133            if iterations_remaining == 0 {
134                warn!(
135                    max_iterations = self.max_iterations,
136                    user_request = problem,
137                    result = result.as_str(),
138                    "Max iterations reached"
139                );
140                break;
141            }
142        }
143
144        // Request final summary
145        let summary = self
146            .chat
147            .send(
148                "Summarize the final result, and only the final result, \
149                 without commenting on the process by which you achieved it.",
150            )
151            .await?;
152
153        Ok(summary)
154    }
155
156    /// Execute a single problem-solving step.
157    ///
158    /// This method sends a prompt to the chat session asking it to work on the user's request
159    /// using available tools. The response should indicate success ("DONE") or failure ("FAIL").
160    ///
161    /// # Arguments
162    ///
163    /// * `problem` - The problem or request to be solved
164    ///
165    /// # Returns
166    ///
167    /// The response from the chat session, indicating the step's outcome
168    async fn step(&mut self, problem: &str) -> Result<String> {
169        let prompt = format!(
170            "Given the user request:\n\
171             {}\n\
172             \n\
173             Use the tools at your disposal to act on their request. \
174             You may wish to create a step-by-step plan for more complicated requests.\n\
175             \n\
176             If you cannot provide an answer, say only \"FAIL\".\n\
177             If you have the answer, say only \"DONE\".",
178            problem
179        );
180
181        self.chat.send(&prompt).await
182    }
183}
184
185/// Builder for constructing an `IterativeProblemSolver` with custom configuration.
186pub struct IterativeProblemSolverBuilder {
187    broker: LlmBroker,
188    tools: Option<Vec<Box<dyn LlmTool>>>,
189    max_iterations: usize,
190    system_prompt: Option<String>,
191}
192
193impl IterativeProblemSolverBuilder {
194    /// Create a new builder
195    fn new(broker: LlmBroker) -> Self {
196        Self {
197            broker,
198            tools: None,
199            max_iterations: 3,
200            system_prompt: None,
201        }
202    }
203
204    /// Set the tools available to the problem solver
205    pub fn tools(mut self, tools: Vec<Box<dyn LlmTool>>) -> Self {
206        self.tools = Some(tools);
207        self
208    }
209
210    /// Set the maximum number of iterations (default: 3)
211    pub fn max_iterations(mut self, max_iterations: usize) -> Self {
212        self.max_iterations = max_iterations;
213        self
214    }
215
216    /// Set a custom system prompt
217    pub fn system_prompt(mut self, prompt: impl Into<String>) -> Self {
218        self.system_prompt = Some(prompt.into());
219        self
220    }
221
222    /// Build the problem solver
223    pub fn build(self) -> IterativeProblemSolver {
224        let system_prompt = self.system_prompt.unwrap_or_else(|| {
225            "You are a problem-solving assistant that can solve complex problems step by step. \
226             You analyze problems, break them down into smaller parts, and solve them systematically. \
227             If you cannot solve a problem completely in one step, you make progress and identify what to do next."
228                .to_string()
229        });
230
231        let mut chat_builder = ChatSession::builder(self.broker).system_prompt(system_prompt);
232
233        if let Some(tools) = self.tools {
234            chat_builder = chat_builder.tools(tools);
235        }
236
237        IterativeProblemSolver {
238            chat: chat_builder.build(),
239            max_iterations: self.max_iterations,
240        }
241    }
242}
243
244#[cfg(test)]
245mod tests {
246    use super::*;
247    use crate::llm::gateway::{CompletionConfig, LlmGateway, StreamChunk};
248    use crate::llm::models::{LlmGatewayResponse, LlmMessage};
249    use crate::llm::tools::{FunctionDescriptor, ToolDescriptor};
250    use async_trait::async_trait;
251    use futures::stream::{self, Stream};
252    use serde_json::{json, Value};
253    use std::collections::HashMap;
254    use std::pin::Pin;
255    use std::sync::{Arc, Mutex};
256
257    // Mock gateway for testing
258    struct MockGateway {
259        responses: Vec<String>,
260        call_count: Arc<Mutex<usize>>,
261    }
262
263    impl MockGateway {
264        fn new(responses: Vec<String>) -> Self {
265            Self {
266                responses,
267                call_count: Arc::new(Mutex::new(0)),
268            }
269        }
270    }
271
272    #[async_trait::async_trait]
273    impl LlmGateway for MockGateway {
274        async fn complete(
275            &self,
276            _model: &str,
277            _messages: &[LlmMessage],
278            _tools: Option<&[Box<dyn LlmTool>]>,
279            _config: &CompletionConfig,
280        ) -> Result<LlmGatewayResponse> {
281            let mut count = self.call_count.lock().unwrap();
282            let idx = *count;
283            *count += 1;
284
285            let content = if idx < self.responses.len() {
286                self.responses[idx].clone()
287            } else {
288                "default response".to_string()
289            };
290
291            Ok(LlmGatewayResponse {
292                content: Some(content),
293                object: None,
294                tool_calls: vec![],
295                thinking: None,
296                ..Default::default()
297            })
298        }
299
300        async fn complete_json(
301            &self,
302            _model: &str,
303            _messages: &[LlmMessage],
304            _schema: Value,
305            _config: &CompletionConfig,
306        ) -> Result<Value> {
307            Ok(json!({}))
308        }
309
310        async fn get_available_models(&self) -> Result<Vec<String>> {
311            Ok(vec!["test-model".to_string()])
312        }
313
314        async fn calculate_embeddings(
315            &self,
316            _text: &str,
317            _model: Option<&str>,
318        ) -> Result<Vec<f32>> {
319            Ok(vec![0.1, 0.2, 0.3])
320        }
321
322        fn complete_stream<'a>(
323            &'a self,
324            _model: &'a str,
325            _messages: &'a [LlmMessage],
326            _tools: Option<&'a [Box<dyn LlmTool>]>,
327            _config: &'a CompletionConfig,
328        ) -> Pin<Box<dyn Stream<Item = Result<StreamChunk>> + Send + 'a>> {
329            Box::pin(stream::iter(vec![Ok(StreamChunk::Content("test".to_string()))]))
330        }
331    }
332
333    // Mock tool for testing
334    #[derive(Clone)]
335    struct MockTool {
336        name: String,
337    }
338
339    #[async_trait]
340
341    impl LlmTool for MockTool {
342        async fn run(
343            &self,
344            _args: &HashMap<String, Value>,
345            _ctx: &crate::llm::tools::ToolRunCtx,
346        ) -> Result<Value> {
347            Ok(json!({"result": "success"}))
348        }
349
350        fn descriptor(&self) -> ToolDescriptor {
351            ToolDescriptor {
352                r#type: "function".to_string(),
353                function: FunctionDescriptor {
354                    name: self.name.clone(),
355                    description: "A mock tool".to_string(),
356                    parameters: json!({}),
357                },
358            }
359        }
360
361        fn clone_box(&self) -> Box<dyn LlmTool> {
362            Box::new(self.clone())
363        }
364    }
365
366    #[tokio::test]
367    async fn test_builder_default_settings() {
368        let gateway = Arc::new(MockGateway::new(vec![]));
369        let broker = LlmBroker::new("test-model", gateway, None);
370        let solver = IterativeProblemSolver::new(broker);
371
372        assert_eq!(solver.max_iterations, 3);
373    }
374
375    #[tokio::test]
376    async fn test_builder_custom_max_iterations() {
377        let gateway = Arc::new(MockGateway::new(vec![]));
378        let broker = LlmBroker::new("test-model", gateway, None);
379        let solver = IterativeProblemSolver::builder(broker).max_iterations(5).build();
380
381        assert_eq!(solver.max_iterations, 5);
382    }
383
384    #[tokio::test]
385    async fn test_builder_with_tools() {
386        let gateway = Arc::new(MockGateway::new(vec![]));
387        let broker = LlmBroker::new("test-model", gateway, None);
388
389        let tools: Vec<Box<dyn LlmTool>> = vec![Box::new(MockTool {
390            name: "test_tool".to_string(),
391        })];
392
393        let _solver = IterativeProblemSolver::builder(broker).tools(tools).build();
394
395        // If this compiles and runs, the builder pattern works
396    }
397
398    #[tokio::test]
399    async fn test_solve_completes_with_done() {
400        let gateway = Arc::new(MockGateway::new(vec![
401            "Working on it...".to_string(),
402            "DONE".to_string(),
403            "The answer is 42".to_string(),
404        ]));
405        let broker = LlmBroker::new("test-model", gateway, None);
406        let mut solver = IterativeProblemSolver::new(broker);
407
408        let result = solver.solve("Test problem").await.unwrap();
409
410        assert_eq!(result, "The answer is 42");
411    }
412
413    #[tokio::test]
414    async fn test_solve_fails_with_fail() {
415        let gateway = Arc::new(MockGateway::new(vec![
416            "Trying...".to_string(),
417            "FAIL".to_string(),
418            "Could not solve the problem".to_string(),
419        ]));
420        let broker = LlmBroker::new("test-model", gateway, None);
421        let mut solver = IterativeProblemSolver::new(broker);
422
423        let result = solver.solve("Impossible problem").await.unwrap();
424
425        assert_eq!(result, "Could not solve the problem");
426    }
427
428    #[tokio::test]
429    async fn test_solve_stops_at_max_iterations() {
430        let gateway = Arc::new(MockGateway::new(vec![
431            "Step 1".to_string(),
432            "Step 2".to_string(),
433            "Step 3".to_string(),
434            "Final summary".to_string(),
435        ]));
436        let broker = LlmBroker::new("test-model", gateway, None);
437        let mut solver = IterativeProblemSolver::builder(broker).max_iterations(3).build();
438
439        let result = solver.solve("Long problem").await.unwrap();
440
441        // Should have called the gateway 4 times: 3 iterations + 1 summary
442        assert_eq!(result, "Final summary");
443    }
444
445    #[tokio::test]
446    async fn test_solve_case_insensitive_done() {
447        let gateway = Arc::new(MockGateway::new(vec![
448            "done".to_string(),                 // lowercase "done"
449            "The task is complete".to_string(), // summary
450        ]));
451        let broker = LlmBroker::new("test-model", gateway, None);
452        let mut solver = IterativeProblemSolver::new(broker);
453
454        let result = solver.solve("Test problem").await.unwrap();
455
456        assert_eq!(result, "The task is complete");
457    }
458
459    #[tokio::test]
460    async fn test_solve_case_insensitive_fail() {
461        let gateway = Arc::new(MockGateway::new(vec![
462            "fail".to_string(),                    // lowercase "fail"
463            "Unable to complete task".to_string(), // summary
464        ]));
465        let broker = LlmBroker::new("test-model", gateway, None);
466        let mut solver = IterativeProblemSolver::new(broker);
467
468        let result = solver.solve("Test problem").await.unwrap();
469
470        assert_eq!(result, "Unable to complete task");
471    }
472
473    #[tokio::test]
474    async fn test_custom_system_prompt() {
475        let gateway =
476            Arc::new(MockGateway::new(vec!["DONE".to_string(), "Custom response".to_string()]));
477        let broker = LlmBroker::new("test-model", gateway, None);
478        let mut solver = IterativeProblemSolver::builder(broker)
479            .system_prompt("Custom system prompt for testing")
480            .build();
481
482        let result = solver.solve("Test problem").await.unwrap();
483
484        assert_eq!(result, "Custom response");
485    }
486
487    #[tokio::test]
488    async fn test_step_method() {
489        let gateway = Arc::new(MockGateway::new(vec!["Step response".to_string()]));
490        let broker = LlmBroker::new("test-model", gateway, None);
491        let mut solver = IterativeProblemSolver::new(broker);
492
493        let result = solver.step("Test problem").await.unwrap();
494
495        assert_eq!(result, "Step response");
496    }
497
498    #[tokio::test]
499    async fn test_multiple_iterations_before_done() {
500        let gateway = Arc::new(MockGateway::new(vec![
501            "Working...".to_string(),
502            "Still working...".to_string(),
503            "Almost there...".to_string(),
504            "DONE".to_string(),
505            "Completed successfully".to_string(),
506        ]));
507        let broker = LlmBroker::new("test-model", gateway, None);
508        let mut solver = IterativeProblemSolver::builder(broker).max_iterations(5).build();
509
510        let result = solver.solve("Complex problem").await.unwrap();
511
512        assert_eq!(result, "Completed successfully");
513    }
514
515    #[tokio::test]
516    async fn test_done_substring_detection() {
517        let gateway = Arc::new(MockGateway::new(vec![
518            "I'm DONE with this task".to_string(), // Contains "DONE"
519            "Task completed".to_string(),
520        ]));
521        let broker = LlmBroker::new("test-model", gateway, None);
522        let mut solver = IterativeProblemSolver::new(broker);
523
524        let result = solver.solve("Test problem").await.unwrap();
525
526        assert_eq!(result, "Task completed");
527    }
528
529    #[tokio::test]
530    async fn test_fail_substring_detection() {
531        let gateway = Arc::new(MockGateway::new(vec![
532            "This will FAIL".to_string(), // Contains "FAIL"
533            "Failed to complete".to_string(),
534        ]));
535        let broker = LlmBroker::new("test-model", gateway, None);
536        let mut solver = IterativeProblemSolver::new(broker);
537
538        let result = solver.solve("Test problem").await.unwrap();
539
540        assert_eq!(result, "Failed to complete");
541    }
542}