1use crate::error::Result;
7use crate::llm::chat_session::ChatSession;
8use crate::llm::tools::LlmTool;
9use crate::llm::LlmBroker;
10use tracing::{info, warn};
11
12pub struct IterativeProblemSolver {
46 chat: ChatSession,
47 max_iterations: usize,
48}
49
50impl IterativeProblemSolver {
51 pub fn new(broker: LlmBroker) -> Self {
66 Self::builder(broker).build()
67 }
68
69 pub fn builder(broker: LlmBroker) -> IterativeProblemSolverBuilder {
87 IterativeProblemSolverBuilder::new(broker)
88 }
89
90 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 if result.to_lowercase().contains("fail") {
122 info!(user_request = problem, result = result.as_str(), "Task failed");
123 break;
124 }
125
126 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 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 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
185pub 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 fn new(broker: LlmBroker) -> Self {
196 Self {
197 broker,
198 tools: None,
199 max_iterations: 3,
200 system_prompt: None,
201 }
202 }
203
204 pub fn tools(mut self, tools: Vec<Box<dyn LlmTool>>) -> Self {
206 self.tools = Some(tools);
207 self
208 }
209
210 pub fn max_iterations(mut self, max_iterations: usize) -> Self {
212 self.max_iterations = max_iterations;
213 self
214 }
215
216 pub fn system_prompt(mut self, prompt: impl Into<String>) -> Self {
218 self.system_prompt = Some(prompt.into());
219 self
220 }
221
222 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 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 #[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 }
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 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(), "The task is complete".to_string(), ]));
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(), "Unable to complete task".to_string(), ]));
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(), "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(), "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}