auto_forest_demo/
auto_forest_demo.rs

1//! # AutoForest Demo
2//!
3//! This example demonstrates the AutoForest feature - automatic orchestration of agent forests.
4//! AutoForest intelligently spawns specialized agents to tackle complex tasks.
5//!
6//! The example shows:
7//! 1. Creating an AutoForest orchestrator
8//! 2. Submitting a complex task
9//! 3. Inspecting the generated orchestration plan
10//! 4. Getting the synthesized results
11
12use helios_engine::{AutoForest, CalculatorTool, Config};
13
14#[tokio::main]
15async fn main() -> helios_engine::Result<()> {
16    println!("🌲 AutoForest - Intelligent Agent Orchestration Demo\n");
17    println!("====================================================\n");
18
19    // Step 1: Load or create configuration
20    println!("šŸ“‹ Creating configuration...");
21    let config = Config::builder().temperature(0.7).max_tokens(2048).build();
22    println!("āœ“ Configuration ready\n");
23
24    // Step 2: Create AutoForest with available tools
25    println!("šŸ”§ Initializing AutoForest with tools...");
26    let mut auto_forest = AutoForest::new(config)
27        .with_tools(vec![
28            Box::new(CalculatorTool),
29            // Additional tools could be added here
30        ])
31        .build()
32        .await?;
33    println!("āœ“ AutoForest initialized\n");
34
35    // Step 3: Define a complex task
36    let task = r#"
37    I need to analyze a business problem:
38    1. A company has Q3 revenue data showing mixed results across 5 product categories
39    2. I need to understand which categories are underperforming
40    3. Calculate the performance metrics for each category
41    4. Identify trends and predict Q4 performance
42    5. Recommend which categories need attention
43    "#;
44
45    println!("šŸ“ Submitting task to AutoForest...");
46    println!("Task: {}\n", task.trim());
47
48    // Step 4: Execute the task
49    println!("šŸš€ AutoForest is orchestrating agent deployment...\n");
50    let result = auto_forest.execute_task(task).await?;
51
52    // Step 5: Display the orchestration plan
53    println!("šŸ“Š Orchestration Plan Generated:");
54    println!("================================\n");
55
56    if let Some(plan) = auto_forest.orchestration_plan() {
57        println!("Task: {}", plan.task.trim());
58        println!("Number of Agents Spawned: {}", plan.num_agents);
59        println!("Planning Reasoning: {}\n", plan.reasoning);
60
61        println!("Agent Configurations:");
62        println!("-----------------");
63        for (i, agent_config) in plan.agents.iter().enumerate() {
64            println!(
65                "Agent {}: {} ({})",
66                i + 1,
67                agent_config.name,
68                agent_config.role
69            );
70            println!("  Prompt: {}", agent_config.system_prompt);
71            if !agent_config.tool_indices.is_empty() {
72                println!("  Tools: {:?}", agent_config.tool_indices);
73            }
74            println!();
75        }
76
77        println!("Task Breakdown:");
78        println!("--------------");
79        for (agent_name, subtask) in &plan.task_breakdown {
80            println!("• {} → {}", agent_name, subtask);
81        }
82        println!();
83    }
84
85    // Step 6: Display spawned agents
86    println!("šŸ¤– Spawned Agents:");
87    println!("-----------------");
88    let spawned = auto_forest.spawned_agents();
89    println!("Total agents created: {}\n", spawned.len());
90
91    for (i, spawned_agent) in spawned.iter().enumerate() {
92        println!("Agent {}: {}", i + 1, spawned_agent.config.name);
93        println!("  Role: {}", spawned_agent.config.role);
94        if let Some(result) = &spawned_agent.result {
95            println!("  Result available: Yes ({} chars)", result.len());
96        } else {
97            println!("  Result: Pending");
98        }
99        println!();
100    }
101
102    // Step 7: Display the final synthesized result
103    println!("šŸ“ˆ Final Synthesized Result:");
104    println!("==========================\n");
105    println!("{}\n", result);
106
107    // Step 8: Demonstrate a second task with different complexity
108    println!("\nšŸ”„ Executing a second task with different complexity...\n");
109
110    let simple_task = "Calculate the average of these numbers: 100, 200, 300, 400, 500. Then tell me what percentage each is of the total.";
111
112    println!("Task: {}\n", simple_task);
113    println!("šŸš€ Processing...\n");
114
115    let result2 = auto_forest.execute_task(simple_task).await?;
116
117    println!("Result:\n{}\n", result2);
118
119    if let Some(plan) = auto_forest.orchestration_plan() {
120        println!(
121            "This task resulted in {} agent(s) - a simpler orchestration plan.",
122            plan.num_agents
123        );
124        println!("Reasoning: {}\n", plan.reasoning);
125    }
126
127    println!("āœ… AutoForest demo completed!");
128    println!("=============================");
129    println!("\nšŸ’” Key Features Demonstrated:");
130    println!("  • Automatic agent spawning based on task complexity");
131    println!("  • Specialized agent prompt generation");
132    println!("  • Task breakdown across agents");
133    println!("  • Result aggregation and synthesis");
134    println!("  • Flexible orchestration for varying task complexity");
135
136    Ok(())
137}