|
| 1 | +# Orchestrator Monitoring API 快速参考 |
| 2 | + |
| 3 | +## 设置 |
| 4 | + |
| 5 | +```bash |
| 6 | +# 设置 Kimi API Key |
| 7 | +export MOONSHOT_API_KEY=your_api_key_here |
| 8 | +``` |
| 9 | + |
| 10 | +## Python API |
| 11 | + |
| 12 | +### 创建和启动 |
| 13 | + |
| 14 | +```python |
| 15 | +from a3s_code import Orchestrator, SubAgentConfig |
| 16 | + |
| 17 | +# 创建 Orchestrator |
| 18 | +orch = Orchestrator.create() |
| 19 | + |
| 20 | +# 配置 SubAgent |
| 21 | +config = SubAgentConfig( |
| 22 | + agent_type="explore", |
| 23 | + description="Find Python files", |
| 24 | + prompt="Use glob to find all Python files", |
| 25 | + permissive=True, |
| 26 | + max_steps=5 |
| 27 | +) |
| 28 | + |
| 29 | +# 启动 SubAgent |
| 30 | +handle = orch.spawn_subagent(config) |
| 31 | +``` |
| 32 | + |
| 33 | +### 监控 API |
| 34 | + |
| 35 | +```python |
| 36 | +# 获取所有 SubAgent 信息(包含当前活动) |
| 37 | +subagents = orch.list_subagents() |
| 38 | +for info in subagents: |
| 39 | + print(f"{info.id}: {info.state}") |
| 40 | + if info.current_activity: |
| 41 | + print(f" Activity: {info.current_activity.activity_type}") |
| 42 | + |
| 43 | +# 获取特定 SubAgent 信息 |
| 44 | +info = orch.get_subagent_info(subagent_id) |
| 45 | + |
| 46 | +# 获取所有活跃 SubAgent 的当前活动 |
| 47 | +activities = orch.get_active_activities() |
| 48 | +for subagent_id, activity in activities: |
| 49 | + print(f"{subagent_id}: {activity.activity_type}") |
| 50 | + |
| 51 | +# 获取所有 SubAgent 状态 |
| 52 | +states = orch.get_all_states() |
| 53 | + |
| 54 | +# 获取活跃数量 |
| 55 | +count = orch.active_count() |
| 56 | +``` |
| 57 | + |
| 58 | +### 控制 API |
| 59 | + |
| 60 | +```python |
| 61 | +# 暂停 |
| 62 | +orch.pause_subagent(subagent_id) |
| 63 | + |
| 64 | +# 恢复 |
| 65 | +orch.resume_subagent(subagent_id) |
| 66 | + |
| 67 | +# 取消 |
| 68 | +orch.cancel_subagent(subagent_id) |
| 69 | + |
| 70 | +# 等待所有完成 |
| 71 | +orch.wait_all() |
| 72 | +``` |
| 73 | + |
| 74 | +## TypeScript/Node.js API |
| 75 | + |
| 76 | +### 创建和启动 |
| 77 | + |
| 78 | +```typescript |
| 79 | +import { Orchestrator, SubAgentConfig } from '@a3s-lab/code'; |
| 80 | + |
| 81 | +// 创建 Orchestrator |
| 82 | +const orch = Orchestrator.create(); |
| 83 | + |
| 84 | +// 配置 SubAgent |
| 85 | +const config: SubAgentConfig = { |
| 86 | + agentType: 'explore', |
| 87 | + description: 'Find TypeScript files', |
| 88 | + prompt: 'Use glob to find all TypeScript files', |
| 89 | + permissive: true, |
| 90 | + maxSteps: 5 |
| 91 | +}; |
| 92 | + |
| 93 | +// 启动 SubAgent |
| 94 | +const handle = orch.spawnSubagent(config); |
| 95 | +``` |
| 96 | + |
| 97 | +### 监控 API |
| 98 | + |
| 99 | +```typescript |
| 100 | +// 获取所有 SubAgent 信息(包含当前活动) |
| 101 | +const subagents = orch.listSubagents(); |
| 102 | +for (const info of subagents) { |
| 103 | + console.log(`${info.id}: ${info.state}`); |
| 104 | + if (info.currentActivity) { |
| 105 | + console.log(` Activity: ${info.currentActivity.activityType}`); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// 获取特定 SubAgent 信息 |
| 110 | +const info = orch.getSubagentInfo(subagentId); |
| 111 | + |
| 112 | +// 获取所有活跃 SubAgent 的当前活动 |
| 113 | +const activities = orch.getActiveActivities(); |
| 114 | +for (const entry of activities) { |
| 115 | + console.log(`${entry.id}: ${entry.activity.activityType}`); |
| 116 | +} |
| 117 | + |
| 118 | +// 获取所有 SubAgent 状态 |
| 119 | +const states = orch.getAllStates(); |
| 120 | + |
| 121 | +// 获取活跃数量 |
| 122 | +const count = orch.activeCount(); |
| 123 | +``` |
| 124 | + |
| 125 | +### 控制 API |
| 126 | + |
| 127 | +```typescript |
| 128 | +// 暂停 |
| 129 | +orch.pauseSubagent(subagentId); |
| 130 | + |
| 131 | +// 恢复 |
| 132 | +orch.resumeSubagent(subagentId); |
| 133 | + |
| 134 | +// 取消 |
| 135 | +orch.cancelSubagent(subagentId); |
| 136 | + |
| 137 | +// 等待所有完成 |
| 138 | +orch.waitAll(); |
| 139 | +``` |
| 140 | + |
| 141 | +## SubAgentInfo 结构 |
| 142 | + |
| 143 | +```typescript |
| 144 | +interface SubAgentInfo { |
| 145 | + id: string; // SubAgent ID |
| 146 | + agentType: string; // 类型 (explore, analyze, etc.) |
| 147 | + description: string; // 任务描述 |
| 148 | + state: string; // 当前状态 |
| 149 | + parentId?: string; // 父 SubAgent ID |
| 150 | + createdAt: number; // 创建时间戳 |
| 151 | + updatedAt: number; // 更新时间戳 |
| 152 | + currentActivity?: { // 当前活动 |
| 153 | + activityType: string; // idle | calling_tool | requesting_llm | waiting_for_control |
| 154 | + data?: string; // JSON 数据 |
| 155 | + }; |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +## 活动类型 |
| 160 | + |
| 161 | +| 类型 | 说明 | 数据示例 | |
| 162 | +|------|------|---------| |
| 163 | +| `idle` | 空闲 | `null` | |
| 164 | +| `calling_tool` | 调用工具 | `{"tool_name": "glob", "args": {...}}` | |
| 165 | +| `requesting_llm` | 请求 LLM | `{"message_count": 3}` | |
| 166 | +| `waiting_for_control` | 等待控制 | `{"reason": "Paused by orchestrator"}` | |
| 167 | + |
| 168 | +## 运行示例 |
| 169 | + |
| 170 | +### Python |
| 171 | + |
| 172 | +```bash |
| 173 | +# 快速开始 |
| 174 | +cd sdk/python/examples |
| 175 | +pip install -r requirements.txt |
| 176 | +python quickstart_monitoring.py |
| 177 | + |
| 178 | +# 完整示例 |
| 179 | +python orchestrator_monitoring_kimi.py |
| 180 | +``` |
| 181 | + |
| 182 | +### TypeScript |
| 183 | + |
| 184 | +```bash |
| 185 | +# 快速开始 |
| 186 | +cd sdk/node/examples |
| 187 | +npm install |
| 188 | +npm run quickstart |
| 189 | + |
| 190 | +# 完整示例 |
| 191 | +npm run full |
| 192 | +``` |
| 193 | + |
| 194 | +## 常见模式 |
| 195 | + |
| 196 | +### 实时监控循环 |
| 197 | + |
| 198 | +```python |
| 199 | +# Python |
| 200 | +import asyncio |
| 201 | + |
| 202 | +for i in range(10): |
| 203 | + subagents = orch.list_subagents() |
| 204 | + print(f"[{i}] Active: {orch.active_count()}") |
| 205 | + for info in subagents: |
| 206 | + activity = info.current_activity.activity_type if info.current_activity else "idle" |
| 207 | + print(f" {info.id}: {info.state} | {activity}") |
| 208 | + await asyncio.sleep(1) |
| 209 | +``` |
| 210 | + |
| 211 | +```typescript |
| 212 | +// TypeScript |
| 213 | +for (let i = 0; i < 10; i++) { |
| 214 | + const subagents = orch.listSubagents(); |
| 215 | + console.log(`[${i}] Active: ${orch.activeCount()}`); |
| 216 | + for (const info of subagents) { |
| 217 | + const activity = info.currentActivity?.activityType || 'idle'; |
| 218 | + console.log(` ${info.id}: ${info.state} | ${activity}`); |
| 219 | + } |
| 220 | + await sleep(1000); |
| 221 | +} |
| 222 | +``` |
| 223 | + |
| 224 | +### 条件控制 |
| 225 | + |
| 226 | +```python |
| 227 | +# Python - 自动取消空闲太久的 SubAgent |
| 228 | +for info in orch.list_subagents(): |
| 229 | + if info.current_activity and info.current_activity.activity_type == "idle": |
| 230 | + # 空闲超过阈值,取消 |
| 231 | + orch.cancel_subagent(info.id) |
| 232 | +``` |
| 233 | + |
| 234 | +```typescript |
| 235 | +// TypeScript - 自动取消空闲太久的 SubAgent |
| 236 | +for (const info of orch.listSubagents()) { |
| 237 | + if (info.currentActivity?.activityType === 'idle') { |
| 238 | + // 空闲超过阈值,取消 |
| 239 | + orch.cancelSubagent(info.id); |
| 240 | + } |
| 241 | +} |
| 242 | +``` |
| 243 | + |
| 244 | +## 相关文档 |
| 245 | + |
| 246 | +- [完整示例文档](./ORCHESTRATOR_MONITORING.md) |
| 247 | +- [架构设计](../../../docs/architecture/agent-team-architecture.md) |
| 248 | +- [Python SDK](../python/README.md) |
| 249 | +- [Node.js SDK](../node/README.md) |
0 commit comments