react_debugging/
react_debugging.rs

1//! # Example: Using ReAct for Debugging
2//!
3//! This example shows how ReAct mode helps with debugging and understanding
4//! how agents approach problems.
5
6use helios_engine::{Agent, CalculatorTool, Config, FileReadTool, JsonParserTool};
7
8#[tokio::main]
9async fn main() -> helios_engine::Result<()> {
10    println!("🔍 Helios Engine - ReAct for Debugging");
11    println!("=======================================\n");
12
13    let config = Config::from_file("config.toml")?;
14
15    // Create a ReAct agent with verbose reasoning
16    let debug_prompt = r#"Debug this task step by step. For each step, explain:
17
181. CURRENT STATE: What information do I have?
192. NEXT ACTION: What should I do next?
203. REASONING: Why is this the right action?
214. EXPECTED RESULT: What should happen?
225. VALIDATION: How will I know if it worked?
23
24Be extremely detailed in your thinking."#;
25
26    let mut debug_agent = Agent::builder("DebugAgent")
27        .config(config)
28        .system_prompt("You are a debugging assistant who explains every decision.")
29        .tools(vec![
30            Box::new(CalculatorTool),
31            Box::new(JsonParserTool),
32            Box::new(FileReadTool),
33        ])
34        .react_with_prompt(debug_prompt)
35        .max_iterations(15) // Allow more iterations for complex debugging
36        .build()
37        .await?;
38
39    println!(
40        "Available tools: {:?}\n",
41        debug_agent.tool_registry().list_tools()
42    );
43
44    // Scenario 1: Tracing calculation steps
45    println!("═══════════════════════════════════════════════════════════");
46    println!("Scenario 1: Trace Complex Calculation");
47    println!("═══════════════════════════════════════════════════════════\n");
48
49    println!("Problem: Calculate the compound interest formula result");
50    println!("Formula: A = P(1 + r)^n where P=1000, r=0.05, n=3\n");
51
52    let response = debug_agent
53        .chat("Calculate compound interest: Principal=1000, rate=0.05, time=3 years. Use A = P * (1 + r)^n")
54        .await?;
55    println!("\nAgent: {}\n", response);
56
57    // Scenario 2: Understanding tool selection
58    println!("═══════════════════════════════════════════════════════════");
59    println!("Scenario 2: Tool Selection Reasoning");
60    println!("═══════════════════════════════════════════════════════════\n");
61
62    println!("Task: Parse JSON and extract a value, then perform calculation\n");
63
64    let response = debug_agent
65        .chat(r#"Parse this JSON: {"price": 25.50, "quantity": 4} and calculate the total cost"#)
66        .await?;
67    println!("\nAgent: {}\n", response);
68
69    // Scenario 3: Error recovery reasoning
70    println!("═══════════════════════════════════════════════════════════");
71    println!("Scenario 3: Multi-Step Problem Solving");
72    println!("═══════════════════════════════════════════════════════════\n");
73
74    println!("Task: Calculate average of a series of operations\n");
75
76    let response = debug_agent
77        .chat("Calculate: (10 * 5) + (20 * 3) + (15 * 2), then divide by 3 to get the average")
78        .await?;
79    println!("\nAgent: {}\n", response);
80
81    // Explanation
82    println!("═══════════════════════════════════════════════════════════");
83    println!("💡 Debugging Benefits");
84    println!("═══════════════════════════════════════════════════════════\n");
85
86    println!("ReAct mode helps you:");
87    println!("  1. 🔍 See exactly what the agent is thinking");
88    println!("  2. 🎯 Understand why it chose specific tools");
89    println!("  3. 📋 Follow the step-by-step execution plan");
90    println!("  4. 🐛 Identify where reasoning might go wrong");
91    println!("  5. 🔧 Optimize prompts based on visible thinking\n");
92
93    println!("Tips for debugging with ReAct:");
94    println!("  • Use detailed custom prompts for more verbose reasoning");
95    println!("  • Increase max_iterations for complex tasks");
96    println!("  • Watch the '💭 ReAct Reasoning' output carefully");
97    println!("  • Compare reasoning across different queries");
98    println!("  • Adjust system prompts based on reasoning patterns\n");
99
100    Ok(())
101}