|
| 1 | +//! Orchestrator 使用示例 |
| 2 | +
|
| 3 | +use a3s_code_core::orchestrator::{AgentOrchestrator, SubAgentConfig}; |
| 4 | + |
| 5 | +#[tokio::main] |
| 6 | +async fn main() -> anyhow::Result<()> { |
| 7 | + println!("=== AgentOrchestrator 示例 ===\n"); |
| 8 | + |
| 9 | + // 1. 创建 orchestrator (默认使用内存事件通讯) |
| 10 | + let orchestrator = AgentOrchestrator::new_memory(); |
| 11 | + println!("✓ Orchestrator 已创建(使用内存事件通讯)\n"); |
| 12 | + |
| 13 | + // 2. 订阅所有事件 |
| 14 | + let mut events = orchestrator.subscribe_all(); |
| 15 | + println!("✓ 已订阅所有事件\n"); |
| 16 | + |
| 17 | + // 3. 在后台监控事件 |
| 18 | + tokio::spawn(async move { |
| 19 | + println!("--- 事件监控开始 ---"); |
| 20 | + while let Ok(event) = events.recv().await { |
| 21 | + println!("[Event] {} - {:?}", event.event_name(), event.subagent_id()); |
| 22 | + } |
| 23 | + }); |
| 24 | + |
| 25 | + // 4. 启动第一个 SubAgent |
| 26 | + println!("启动 SubAgent 1..."); |
| 27 | + let handle1 = orchestrator |
| 28 | + .spawn_subagent( |
| 29 | + SubAgentConfig::new("general", "Analyze Python files") |
| 30 | + .with_description("Count Python files") |
| 31 | + .with_permissive(true) |
| 32 | + .with_max_steps(5), |
| 33 | + ) |
| 34 | + .await?; |
| 35 | + println!("✓ SubAgent 1 已启动: {}\n", handle1.id); |
| 36 | + |
| 37 | + // 5. 启动第二个 SubAgent |
| 38 | + println!("启动 SubAgent 2..."); |
| 39 | + let handle2 = orchestrator |
| 40 | + .spawn_subagent( |
| 41 | + SubAgentConfig::new("explore", "Find Rust files") |
| 42 | + .with_description("Count Rust files") |
| 43 | + .with_permissive(true) |
| 44 | + .with_max_steps(3), |
| 45 | + ) |
| 46 | + .await?; |
| 47 | + println!("✓ SubAgent 2 已启动: {}\n", handle2.id); |
| 48 | + |
| 49 | + // 6. 查询状态 |
| 50 | + tokio::time::sleep(tokio::time::Duration::from_millis(200)).await; |
| 51 | + println!("--- 当前状态 ---"); |
| 52 | + let states = orchestrator.get_all_states().await; |
| 53 | + for (id, state) in &states { |
| 54 | + println!(" {}: {}", id, state); |
| 55 | + } |
| 56 | + println!(); |
| 57 | + |
| 58 | + // 7. 控制 SubAgent |
| 59 | + println!("暂停 SubAgent 1..."); |
| 60 | + orchestrator.pause_subagent(&handle1.id).await?; |
| 61 | + println!("✓ SubAgent 1 已暂停\n"); |
| 62 | + |
| 63 | + tokio::time::sleep(tokio::time::Duration::from_millis(100)).await; |
| 64 | + |
| 65 | + println!("恢复 SubAgent 1..."); |
| 66 | + orchestrator.resume_subagent(&handle1.id).await?; |
| 67 | + println!("✓ SubAgent 1 已恢复\n"); |
| 68 | + |
| 69 | + // 8. 等待完成 |
| 70 | + println!("等待所有 SubAgent 完成..."); |
| 71 | + let result1 = handle1.wait().await?; |
| 72 | + let result2 = handle2.wait().await?; |
| 73 | + |
| 74 | + println!("\n--- 执行结果 ---"); |
| 75 | + println!("SubAgent 1: {}", result1); |
| 76 | + println!("SubAgent 2: {}", result2); |
| 77 | + |
| 78 | + // 9. 最终状态 |
| 79 | + println!("\n--- 最终状态 ---"); |
| 80 | + let final_states = orchestrator.get_all_states().await; |
| 81 | + for (id, state) in &final_states { |
| 82 | + println!(" {}: {}", id, state); |
| 83 | + } |
| 84 | + |
| 85 | + println!("\n✓ 所有 SubAgent 已完成"); |
| 86 | + println!("活跃数量: {}", orchestrator.active_count().await); |
| 87 | + |
| 88 | + Ok(()) |
| 89 | +} |
0 commit comments