Skip to content

Commit 9f5e61a

Browse files
committed
docs: add Orchestrator monitoring examples with Kimi model
Python Examples: - orchestrator_monitoring_kimi.py - Full-featured monitoring demo - quickstart_monitoring.py - Minimal quick start example - requirements.txt - Python dependencies Node.js/TypeScript Examples: - orchestrator_monitoring_kimi.ts - Full-featured monitoring demo - quickstart_monitoring.ts - Minimal quick start example - package.json - Node.js dependencies and scripts Documentation: - ORCHESTRATOR_MONITORING.md - Complete guide with: - Setup instructions (API key, dependencies) - Feature demonstrations (monitoring, control, queries) - Expected output examples - Activity types reference - API usage examples for both Python and TypeScript - Troubleshooting guide - Extension examples Features Demonstrated: 1. Creating Orchestrator with memory-based events 2. Spawning multiple SubAgents with different tasks 3. Real-time monitoring (5 snapshots showing state changes) 4. Dynamic control (pause, resume, cancel) 5. Querying specific SubAgent information 6. Getting all active activities 7. Getting all SubAgent states 8. Waiting for all SubAgents to complete All examples use real Kimi API (MOONSHOT_API_KEY) and demonstrate the complete monitoring API surface.
1 parent a2b1d6b commit 9f5e61a

7 files changed

Lines changed: 920 additions & 0 deletions

File tree

Lines changed: 311 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,311 @@
1+
# Orchestrator Monitoring Examples with Kimi
2+
3+
这些示例展示了如何使用真实的 Kimi 模型测试 Orchestrator 的实时监控功能。
4+
5+
## 功能演示
6+
7+
1. **创建 Orchestrator** - 使用内存事件通信
8+
2. **启动多个 SubAgent** - 不同类型的任务(explore, analyze, document)
9+
3. **实时监控** - 查看所有 SubAgent 的状态和当前活动
10+
4. **动态控制** - 暂停、恢复、取消 SubAgent
11+
5. **查询信息** - 获取特定 SubAgent 的详细信息
12+
6. **活动跟踪** - 监控所有活跃 SubAgent 的当前活动
13+
14+
## 前置要求
15+
16+
### 1. 获取 Kimi API Key
17+
18+
访问 [Moonshot AI](https://platform.moonshot.cn/) 注册并获取 API Key。
19+
20+
### 2. 设置环境变量
21+
22+
```bash
23+
export MOONSHOT_API_KEY=your_api_key_here
24+
```
25+
26+
## Python 示例
27+
28+
### 安装依赖
29+
30+
```bash
31+
pip install a3s-code
32+
```
33+
34+
### 运行示例
35+
36+
```bash
37+
cd sdk/python/examples
38+
python orchestrator_monitoring_kimi.py
39+
```
40+
41+
### 示例输出
42+
43+
```
44+
=== Orchestrator Real-time Monitoring with Kimi ===
45+
46+
✓ Creating Orchestrator with memory-based event communication
47+
48+
Spawning 3 SubAgents...
49+
✓ Spawned: subagent-1 (explore)
50+
✓ Spawned: subagent-2 (analyze)
51+
✓ Spawned: subagent-3 (document)
52+
53+
✓ Total active SubAgents: 3
54+
55+
=== Real-time Monitoring (5 snapshots) ===
56+
57+
--- Snapshot #1 ---
58+
Time: 14:30:15
59+
Active SubAgents: 3/3
60+
61+
SubAgent: subagent-1
62+
Type: explore
63+
Description: 探索 Python 代码库
64+
State: Running
65+
Created: 1709625015123
66+
Current Activity: calling_tool
67+
Data: {"tool_name": "glob", "args": {"pattern": "**/*.py"}}
68+
69+
SubAgent: subagent-2
70+
Type: analyze
71+
Description: 分析代码质量
72+
State: Running
73+
Created: 1709625015234
74+
Current Activity: requesting_llm
75+
Data: {"message_count": 3}
76+
77+
SubAgent: subagent-3
78+
Type: document
79+
Description: 生成文档
80+
State: Running
81+
Created: 1709625015345
82+
Current Activity: idle
83+
84+
...
85+
```
86+
87+
## TypeScript/Node.js 示例
88+
89+
### 安装依赖
90+
91+
```bash
92+
npm install @a3s-lab/code tsx
93+
```
94+
95+
### 运行示例
96+
97+
```bash
98+
cd sdk/node/examples
99+
npx tsx orchestrator_monitoring_kimi.ts
100+
```
101+
102+
### 示例输出
103+
104+
```
105+
=== Orchestrator Real-time Monitoring with Kimi ===
106+
107+
✓ Creating Orchestrator with memory-based event communication
108+
109+
Spawning 3 SubAgents...
110+
✓ Spawned: subagent-1 (explore)
111+
✓ Spawned: subagent-2 (analyze)
112+
✓ Spawned: subagent-3 (document)
113+
114+
✓ Total active SubAgents: 3
115+
116+
=== Real-time Monitoring (5 snapshots) ===
117+
118+
--- Snapshot #1 ---
119+
Time: 2:30:15 PM
120+
Active SubAgents: 3/3
121+
122+
SubAgent: subagent-1
123+
Type: explore
124+
Description: 探索 TypeScript 代码库
125+
State: Running
126+
Created: 1709625015123
127+
Current Activity: calling_tool
128+
Data: {"tool_name": "glob", "args": {"pattern": "**/*.ts"}}
129+
130+
...
131+
```
132+
133+
## 监控的活动类型
134+
135+
示例会展示 4 种 SubAgent 活动类型:
136+
137+
1. **idle** - 空闲状态
138+
```json
139+
{
140+
"activity_type": "idle",
141+
"data": null
142+
}
143+
```
144+
145+
2. **calling_tool** - 正在调用工具
146+
```json
147+
{
148+
"activity_type": "calling_tool",
149+
"data": "{\"tool_name\": \"glob\", \"args\": {\"pattern\": \"**/*.py\"}}"
150+
}
151+
```
152+
153+
3. **requesting_llm** - 正在请求 LLM
154+
```json
155+
{
156+
"activity_type": "requesting_llm",
157+
"data": "{\"message_count\": 3}"
158+
}
159+
```
160+
161+
4. **waiting_for_control** - 等待控制信号(如暂停状态)
162+
```json
163+
{
164+
"activity_type": "waiting_for_control",
165+
"data": "{\"reason\": \"Paused by orchestrator\"}"
166+
}
167+
```
168+
169+
## 控制操作演示
170+
171+
示例会演示以下控制操作:
172+
173+
```python
174+
# Python
175+
orchestrator.pause_subagent(subagent_id) # 暂停
176+
orchestrator.resume_subagent(subagent_id) # 恢复
177+
orchestrator.cancel_subagent(subagent_id) # 取消
178+
orchestrator.wait_all() # 等待所有完成
179+
```
180+
181+
```typescript
182+
// TypeScript
183+
orchestrator.pauseSubagent(subagentId); // 暂停
184+
orchestrator.resumeSubagent(subagentId); // 恢复
185+
orchestrator.cancelSubagent(subagentId); // 取消
186+
orchestrator.waitAll(); // 等待所有完成
187+
```
188+
189+
## 查询 API 演示
190+
191+
示例会演示以下查询 API:
192+
193+
```python
194+
# Python
195+
# 获取所有 SubAgent 信息
196+
subagents = orchestrator.list_subagents()
197+
198+
# 获取特定 SubAgent 信息
199+
info = orchestrator.get_subagent_info(subagent_id)
200+
201+
# 获取所有活跃 SubAgent 的当前活动
202+
activities = orchestrator.get_active_activities()
203+
204+
# 获取所有 SubAgent 状态
205+
states = orchestrator.get_all_states()
206+
207+
# 获取活跃数量
208+
count = orchestrator.active_count()
209+
```
210+
211+
```typescript
212+
// TypeScript
213+
// 获取所有 SubAgent 信息
214+
const subagents = orchestrator.listSubagents();
215+
216+
// 获取特定 SubAgent 信息
217+
const info = orchestrator.getSubagentInfo(subagentId);
218+
219+
// 获取所有活跃 SubAgent 的当前活动
220+
const activities = orchestrator.getActiveActivities();
221+
222+
// 获取所有 SubAgent 状态
223+
const states = orchestrator.getAllStates();
224+
225+
// 获取活跃数量
226+
const count = orchestrator.activeCount();
227+
```
228+
229+
## 注意事项
230+
231+
1. **API Key 安全**: 不要将 API Key 硬编码在代码中,使用环境变量
232+
2. **速率限制**: Kimi API 有速率限制,注意控制并发数量
233+
3. **错误处理**: 生产环境中应添加完善的错误处理
234+
4. **资源清理**: 确保所有 SubAgent 正确完成或取消
235+
236+
## 故障排查
237+
238+
### 问题:MOONSHOT_API_KEY not set
239+
240+
**解决方案**:
241+
```bash
242+
export MOONSHOT_API_KEY=your_api_key_here
243+
```
244+
245+
### 问题:API 调用失败
246+
247+
**可能原因**:
248+
- API Key 无效或过期
249+
- 网络连接问题
250+
- 超过速率限制
251+
252+
**解决方案**:
253+
- 检查 API Key 是否正确
254+
- 检查网络连接
255+
- 减少并发 SubAgent 数量
256+
257+
### 问题:SubAgent 长时间卡在某个状态
258+
259+
**解决方案**:
260+
```python
261+
# 使用 cancel 取消卡住的 SubAgent
262+
orchestrator.cancel_subagent(subagent_id)
263+
```
264+
265+
## 扩展示例
266+
267+
### 自定义监控间隔
268+
269+
```python
270+
# Python - 每 500ms 监控一次
271+
for snapshot in range(1, 11):
272+
subagents = orchestrator.list_subagents()
273+
# ... 处理监控数据
274+
await asyncio.sleep(0.5)
275+
```
276+
277+
```typescript
278+
// TypeScript - 每 500ms 监控一次
279+
for (let snapshot = 1; snapshot <= 10; snapshot++) {
280+
const subagents = orchestrator.listSubagents();
281+
// ... 处理监控数据
282+
await sleep(500);
283+
}
284+
```
285+
286+
### 条件控制
287+
288+
```python
289+
# Python - 当进度慢时暂停并调整
290+
for info in orchestrator.list_subagents():
291+
if info.current_activity and info.current_activity.activity_type == "idle":
292+
# 空闲太久,可能有问题
293+
orchestrator.cancel_subagent(info.id)
294+
```
295+
296+
```typescript
297+
// TypeScript - 当进度慢时暂停并调整
298+
for (const info of orchestrator.listSubagents()) {
299+
if (info.currentActivity?.activityType === 'idle') {
300+
// 空闲太久,可能有问题
301+
orchestrator.cancelSubagent(info.id);
302+
}
303+
}
304+
```
305+
306+
## 相关文档
307+
308+
- [Orchestrator 架构设计](../../../docs/architecture/agent-team-architecture.md)
309+
- [Python SDK 文档](../README.md)
310+
- [Node.js SDK 文档](../../node/README.md)
311+
- [Kimi API 文档](https://platform.moonshot.cn/docs)

0 commit comments

Comments
 (0)