Skip to content

Commit 8f85e3f

Browse files
committed
feat: add real-time SubAgent task list and activity monitoring
- Add SubAgentInfo struct with metadata (id, type, description, state, timestamps) - Add SubAgentActivity enum (Idle, CallingTool, RequestingLlm, WaitingForControl) - Add orchestrator.list_subagents() - get all SubAgent info with current activity - Add orchestrator.get_subagent_info(id) - get specific SubAgent details - Add orchestrator.get_active_activities() - get all active SubAgent activities - Add orchestrator.get_handle(id) - get SubAgent handle for direct control - Update SubAgentHandle to track config, created_at, and activity - Update SubAgentWrapper to track and update current activity - Add orchestrator_monitoring.rs example demonstrating real-time monitoring - Update architecture docs with monitoring API and use cases
1 parent 8c80f76 commit 8f85e3f

7 files changed

Lines changed: 336 additions & 10 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//! 演示主智能体实时监控子智能体任务列表和活动
2+
//!
3+
//! 运行: cargo run --example orchestrator_monitoring
4+
5+
use a3s_code_core::orchestrator::{AgentOrchestrator, SubAgentConfig};
6+
use std::time::Duration;
7+
8+
#[tokio::main]
9+
async fn main() -> anyhow::Result<()> {
10+
println!("=== AgentTeam 实时监控演示 ===\n");
11+
12+
// 1. 创建编排器
13+
let orchestrator = AgentOrchestrator::new_memory();
14+
println!("✓ 创建 AgentOrchestrator\n");
15+
16+
// 2. 启动多个子智能体
17+
println!("启动 3 个子智能体...");
18+
19+
let config1 = SubAgentConfig::new("explore", "Search for Python files")
20+
.with_description("探索代码库,查找所有 Python 文件")
21+
.with_max_steps(10);
22+
23+
let config2 = SubAgentConfig::new("analyze", "Analyze code quality")
24+
.with_description("分析代码质量和潜在问题")
25+
.with_max_steps(15);
26+
27+
let config3 = SubAgentConfig::new("refactor", "Refactor legacy code")
28+
.with_description("重构遗留代码,提高可维护性")
29+
.with_max_steps(20);
30+
31+
let handle1 = orchestrator.spawn_subagent(config1).await?;
32+
let handle2 = orchestrator.spawn_subagent(config2).await?;
33+
let handle3 = orchestrator.spawn_subagent(config3).await?;
34+
35+
println!("✓ 启动了 3 个子智能体\n");
36+
37+
// 3. 实时监控任务列表
38+
println!("=== 实时监控任务列表 ===\n");
39+
40+
for i in 1..=5 {
41+
println!("--- 监控快照 #{} ---", i);
42+
43+
// 获取所有子智能体信息
44+
let subagents = orchestrator.list_subagents().await;
45+
println!("活跃子智能体数量: {}", orchestrator.active_count().await);
46+
println!();
47+
48+
for info in subagents {
49+
println!("SubAgent: {}", info.id);
50+
println!(" 类型: {}", info.agent_type);
51+
println!(" 描述: {}", info.description);
52+
println!(" 状态: {}", info.state);
53+
if let Some(activity) = info.current_activity {
54+
println!(" 当前活动: {:?}", activity);
55+
}
56+
println!();
57+
}
58+
59+
// 获取所有活跃子智能体的当前活动
60+
let activities = orchestrator.get_active_activities().await;
61+
if !activities.is_empty() {
62+
println!("活跃子智能体的实时活动:");
63+
for (id, activity) in activities {
64+
println!(" {}: {:?}", id, activity);
65+
}
66+
println!();
67+
}
68+
69+
tokio::time::sleep(Duration::from_millis(500)).await;
70+
}
71+
72+
// 4. 查询特定子智能体的详细信息
73+
println!("=== 查询特定子智能体详情 ===\n");
74+
75+
if let Some(info) = orchestrator.get_subagent_info(&handle1.id).await {
76+
println!("SubAgent 详情:");
77+
println!(" ID: {}", info.id);
78+
println!(" 类型: {}", info.agent_type);
79+
println!(" 描述: {}", info.description);
80+
println!(" 状态: {}", info.state);
81+
println!(" 创建时间: {}", info.created_at);
82+
println!(" 更新时间: {}", info.updated_at);
83+
if let Some(activity) = info.current_activity {
84+
println!(" 当前活动: {:?}", activity);
85+
}
86+
println!();
87+
}
88+
89+
// 5. 动态控制子智能体
90+
println!("=== 动态控制演示 ===\n");
91+
92+
println!("暂停 subagent-1...");
93+
orchestrator.pause_subagent(&handle1.id).await?;
94+
tokio::time::sleep(Duration::from_millis(200)).await;
95+
96+
// 查看状态变化
97+
if let Some(info) = orchestrator.get_subagent_info(&handle1.id).await {
98+
println!(" 状态: {} (应该是 Paused)", info.state);
99+
if let Some(activity) = info.current_activity {
100+
println!(" 活动: {:?}", activity);
101+
}
102+
}
103+
println!();
104+
105+
println!("恢复 subagent-1...");
106+
orchestrator.resume_subagent(&handle1.id).await?;
107+
tokio::time::sleep(Duration::from_millis(200)).await;
108+
109+
if let Some(info) = orchestrator.get_subagent_info(&handle1.id).await {
110+
println!(" 状态: {} (应该是 Running)", info.state);
111+
}
112+
println!();
113+
114+
// 6. 等待所有完成
115+
println!("等待所有子智能体完成...");
116+
orchestrator.wait_all().await?;
117+
118+
println!("\n=== 最终状态 ===\n");
119+
let final_states = orchestrator.get_all_states().await;
120+
for (id, state) in final_states {
121+
println!("{}: {:?}", id, state);
122+
}
123+
124+
println!("\n✓ 演示完成");
125+
126+
Ok(())
127+
}

core/src/orchestrator/config.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,58 @@ pub struct SubAgentConfig {
6060
pub metadata: serde_json::Value,
6161
}
6262

63+
/// SubAgent 信息(元数据)
64+
#[derive(Debug, Clone, Serialize, Deserialize)]
65+
pub struct SubAgentInfo {
66+
/// SubAgent ID
67+
pub id: String,
68+
69+
/// Agent 类型
70+
pub agent_type: String,
71+
72+
/// 任务描述
73+
pub description: String,
74+
75+
/// 当前状态
76+
pub state: String,
77+
78+
/// 父 SubAgent ID
79+
pub parent_id: Option<String>,
80+
81+
/// 创建时间(Unix 时间戳,毫秒)
82+
pub created_at: u64,
83+
84+
/// 最后更新时间(Unix 时间戳,毫秒)
85+
pub updated_at: u64,
86+
87+
/// 当前活动
88+
pub current_activity: Option<SubAgentActivity>,
89+
}
90+
91+
/// SubAgent 当前活动
92+
#[derive(Debug, Clone, Serialize, Deserialize)]
93+
#[serde(tag = "type", rename_all = "snake_case")]
94+
pub enum SubAgentActivity {
95+
/// 空闲
96+
Idle,
97+
98+
/// 正在调用工具
99+
CallingTool {
100+
tool_name: String,
101+
args: serde_json::Value,
102+
},
103+
104+
/// 正在请求 LLM
105+
RequestingLlm {
106+
message_count: usize,
107+
},
108+
109+
/// 正在等待控制信号
110+
WaitingForControl {
111+
reason: String,
112+
},
113+
}
114+
63115
impl SubAgentConfig {
64116
/// 创建新的 SubAgent 配置
65117
pub fn new(agent_type: impl Into<String>, prompt: impl Into<String>) -> Self {

core/src/orchestrator/handle.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! SubAgent 句柄
22
33
use crate::error::Result;
4-
use crate::orchestrator::{ControlSignal, SubAgentState};
4+
use crate::orchestrator::{ControlSignal, SubAgentActivity, SubAgentConfig, SubAgentState};
55
use std::sync::Arc;
66
use tokio::sync::RwLock;
77

@@ -13,12 +13,21 @@ pub struct SubAgentHandle {
1313
/// SubAgent ID
1414
pub id: String,
1515

16+
/// SubAgent 配置
17+
pub(crate) config: SubAgentConfig,
18+
19+
/// 创建时间(Unix 时间戳,毫秒)
20+
pub(crate) created_at: u64,
21+
1622
/// 控制信号发送器
1723
control_tx: tokio::sync::mpsc::Sender<ControlSignal>,
1824

1925
/// 状态
2026
state: Arc<RwLock<SubAgentState>>,
2127

28+
/// 当前活动
29+
pub(crate) activity: Arc<RwLock<SubAgentActivity>>,
30+
2231
/// 任务句柄
2332
task_handle: Arc<tokio::task::JoinHandle<Result<String>>>,
2433
}
@@ -27,14 +36,22 @@ impl SubAgentHandle {
2736
/// 创建新的句柄
2837
pub(crate) fn new(
2938
id: String,
39+
config: SubAgentConfig,
3040
control_tx: tokio::sync::mpsc::Sender<ControlSignal>,
3141
state: Arc<RwLock<SubAgentState>>,
42+
activity: Arc<RwLock<SubAgentActivity>>,
3243
task_handle: tokio::task::JoinHandle<Result<String>>,
3344
) -> Self {
3445
Self {
3546
id,
47+
config,
48+
created_at: std::time::SystemTime::now()
49+
.duration_since(std::time::UNIX_EPOCH)
50+
.unwrap()
51+
.as_millis() as u64,
3652
control_tx,
3753
state,
54+
activity,
3855
task_handle: Arc::new(task_handle),
3956
}
4057
}
@@ -55,6 +72,21 @@ impl SubAgentHandle {
5572
self.state.read().await.clone()
5673
}
5774

75+
/// 获取当前活动
76+
pub async fn activity(&self) -> SubAgentActivity {
77+
self.activity.read().await.clone()
78+
}
79+
80+
/// 获取配置
81+
pub fn config(&self) -> &SubAgentConfig {
82+
&self.config
83+
}
84+
85+
/// 获取创建时间
86+
pub fn created_at(&self) -> u64 {
87+
self.created_at
88+
}
89+
5890
/// 发送控制信号
5991
pub async fn send_control(&self, signal: ControlSignal) -> Result<()> {
6092
self.control_tx

core/src/orchestrator/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ mod wrapper;
6565
#[cfg(test)]
6666
mod tests;
6767

68-
pub use config::{OrchestratorConfig, SubAgentConfig};
68+
pub use config::{OrchestratorConfig, SubAgentActivity, SubAgentConfig, SubAgentInfo};
6969
pub use control::ControlSignal;
7070
pub use events::{OrchestratorEvent, SubAgentEventPayload};
7171
pub use handle::SubAgentHandle;

0 commit comments

Comments
 (0)