|
| 1 | +/** |
| 2 | + * Real-world test of Orchestrator monitoring with actual Kimi API. |
| 3 | + * Uses the config from a3s/.a3s/config.hcl |
| 4 | + * |
| 5 | + * This test will: |
| 6 | + * 1. Create an Orchestrator |
| 7 | + * 2. Spawn 3 SubAgents with real tasks |
| 8 | + * 3. Monitor their execution in real-time |
| 9 | + * 4. Demonstrate control operations |
| 10 | + * 5. Show all monitoring APIs in action |
| 11 | + */ |
| 12 | + |
| 13 | +import { Agent, Orchestrator, SubAgentConfig } from '@a3s-lab/code'; |
| 14 | +import * as path from 'path'; |
| 15 | +import * as fs from 'fs'; |
| 16 | + |
| 17 | +function sleep(ms: number): Promise<void> { |
| 18 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 19 | +} |
| 20 | + |
| 21 | +async function main() { |
| 22 | + console.log('='.repeat(70)); |
| 23 | + console.log('Orchestrator Real-World Test with Kimi API'); |
| 24 | + console.log('='.repeat(70)); |
| 25 | + console.log(); |
| 26 | + |
| 27 | + // Get config path |
| 28 | + const configPath = path.join(__dirname, '..', '..', '..', '..', '..', '.a3s', 'config.hcl'); |
| 29 | + if (!fs.existsSync(configPath)) { |
| 30 | + console.error(`✗ Config file not found: ${configPath}`); |
| 31 | + process.exit(1); |
| 32 | + } |
| 33 | + |
| 34 | + console.log(`✓ Using config: ${configPath}`); |
| 35 | + console.log(); |
| 36 | + |
| 37 | + // Create Agent with config |
| 38 | + console.log('Creating Agent with Kimi configuration...'); |
| 39 | + let agent: Agent; |
| 40 | + try { |
| 41 | + agent = await Agent.create(configPath); |
| 42 | + console.log('✓ Agent created successfully'); |
| 43 | + } catch (e) { |
| 44 | + console.error(`✗ Failed to create agent: ${e}`); |
| 45 | + process.exit(1); |
| 46 | + } |
| 47 | + |
| 48 | + console.log(); |
| 49 | + |
| 50 | + // Create Orchestrator |
| 51 | + console.log('Creating Orchestrator...'); |
| 52 | + let orch: Orchestrator; |
| 53 | + try { |
| 54 | + orch = Orchestrator.create(); |
| 55 | + console.log('✓ Orchestrator created'); |
| 56 | + } catch (e) { |
| 57 | + console.error(`✗ Failed to create orchestrator: ${e}`); |
| 58 | + process.exit(1); |
| 59 | + } |
| 60 | + |
| 61 | + console.log(); |
| 62 | + |
| 63 | + // Configure SubAgents with real tasks |
| 64 | + console.log('Configuring SubAgents with real tasks...'); |
| 65 | + const configs: SubAgentConfig[] = [ |
| 66 | + { |
| 67 | + agentType: 'explore', |
| 68 | + description: '探索代码库结构', |
| 69 | + prompt: '使用 glob 工具查找当前目录下的所有 .ts 文件,并统计数量', |
| 70 | + permissive: true, |
| 71 | + maxSteps: 3, |
| 72 | + }, |
| 73 | + { |
| 74 | + agentType: 'analyze', |
| 75 | + description: '分析 TODO 注释', |
| 76 | + prompt: '使用 grep 工具搜索所有 TODO 注释,并列出前 5 个', |
| 77 | + permissive: true, |
| 78 | + maxSteps: 3, |
| 79 | + }, |
| 80 | + { |
| 81 | + agentType: 'document', |
| 82 | + description: '读取 README', |
| 83 | + prompt: '使用 read 工具读取 README.md 文件的前 10 行', |
| 84 | + permissive: true, |
| 85 | + maxSteps: 2, |
| 86 | + }, |
| 87 | + ]; |
| 88 | + |
| 89 | + // Spawn SubAgents |
| 90 | + console.log('\nSpawning SubAgents...'); |
| 91 | + const handles = []; |
| 92 | + for (let i = 0; i < configs.length; i++) { |
| 93 | + const config = configs[i]; |
| 94 | + try { |
| 95 | + const handle = orch.spawnSubagent(config); |
| 96 | + handles.push(handle); |
| 97 | + console.log(` ${i + 1}. ✓ Spawned: ${handle.id} (${config.agentType})`); |
| 98 | + console.log(` Task: ${config.description}`); |
| 99 | + } catch (e) { |
| 100 | + console.log(` ${i + 1}. ✗ Failed to spawn: ${e}`); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + console.log(`\n✓ Total SubAgents spawned: ${handles.length}`); |
| 105 | + console.log(`✓ Active count: ${orch.activeCount()}`); |
| 106 | + console.log(); |
| 107 | + |
| 108 | + // Real-time monitoring |
| 109 | + console.log('='.repeat(70)); |
| 110 | + console.log('Real-time Monitoring (10 snapshots, 1 second interval)'); |
| 111 | + console.log('='.repeat(70)); |
| 112 | + console.log(); |
| 113 | + |
| 114 | + for (let snapshot = 1; snapshot <= 10; snapshot++) { |
| 115 | + console.log(`--- Snapshot #${snapshot} (${new Date().toLocaleTimeString()}) ---`); |
| 116 | + |
| 117 | + // Get all SubAgent information |
| 118 | + try { |
| 119 | + const subagents = orch.listSubagents(); |
| 120 | + const activeCount = orch.activeCount(); |
| 121 | + console.log(`Active: ${activeCount}/${subagents.length}`); |
| 122 | + console.log(); |
| 123 | + |
| 124 | + for (const info of subagents) { |
| 125 | + // Basic info |
| 126 | + console.log(`📋 ${info.id}`); |
| 127 | + console.log(` Type: ${info.agentType}`); |
| 128 | + console.log(` State: ${info.state}`); |
| 129 | + console.log(` Description: ${info.description}`); |
| 130 | + |
| 131 | + // Current activity |
| 132 | + if (info.currentActivity) { |
| 133 | + const activity = info.currentActivity; |
| 134 | + console.log(` 🔄 Activity: ${activity.activityType}`); |
| 135 | + if (activity.data) { |
| 136 | + // Parse and display activity data |
| 137 | + try { |
| 138 | + const data = JSON.parse(activity.data); |
| 139 | + if (activity.activityType === 'calling_tool') { |
| 140 | + console.log(` Tool: ${data.tool_name || 'unknown'}`); |
| 141 | + } else if (activity.activityType === 'requesting_llm') { |
| 142 | + console.log(` Messages: ${data.message_count || 0}`); |
| 143 | + } else if (activity.activityType === 'waiting_for_control') { |
| 144 | + console.log(` Reason: ${data.reason || 'unknown'}`); |
| 145 | + } |
| 146 | + } catch (e) { |
| 147 | + // Ignore parse errors |
| 148 | + } |
| 149 | + } |
| 150 | + } else { |
| 151 | + console.log(` 🔄 Activity: None`); |
| 152 | + } |
| 153 | + |
| 154 | + console.log(); |
| 155 | + } |
| 156 | + |
| 157 | + // Show active activities summary |
| 158 | + const activities = orch.getActiveActivities(); |
| 159 | + if (activities.length > 0) { |
| 160 | + console.log('Active Activities Summary:'); |
| 161 | + for (const entry of activities) { |
| 162 | + console.log(` • ${entry.id}: ${entry.activity.activityType}`); |
| 163 | + } |
| 164 | + console.log(); |
| 165 | + } |
| 166 | + } catch (e) { |
| 167 | + console.log(`✗ Monitoring error: ${e}`); |
| 168 | + } |
| 169 | + |
| 170 | + // Wait before next snapshot |
| 171 | + await sleep(1000); |
| 172 | + } |
| 173 | + |
| 174 | + // Demonstrate control operations |
| 175 | + console.log(); |
| 176 | + console.log('='.repeat(70)); |
| 177 | + console.log('Control Operations Demo'); |
| 178 | + console.log('='.repeat(70)); |
| 179 | + console.log(); |
| 180 | + |
| 181 | + if (handles.length > 0) { |
| 182 | + const targetId = handles[0].id; |
| 183 | + |
| 184 | + // Pause |
| 185 | + console.log(`1. Pausing ${targetId}...`); |
| 186 | + try { |
| 187 | + orch.pauseSubagent(targetId); |
| 188 | + await sleep(500); |
| 189 | + |
| 190 | + const info = orch.getSubagentInfo(targetId); |
| 191 | + if (info) { |
| 192 | + console.log(` ✓ State: ${info.state}`); |
| 193 | + if (info.currentActivity) { |
| 194 | + console.log(` ✓ Activity: ${info.currentActivity.activityType}`); |
| 195 | + } |
| 196 | + } |
| 197 | + } catch (e) { |
| 198 | + console.log(` ✗ Failed: ${e}`); |
| 199 | + } |
| 200 | + |
| 201 | + console.log(); |
| 202 | + |
| 203 | + // Resume |
| 204 | + console.log(`2. Resuming ${targetId}...`); |
| 205 | + try { |
| 206 | + orch.resumeSubagent(targetId); |
| 207 | + await sleep(500); |
| 208 | + |
| 209 | + const info = orch.getSubagentInfo(targetId); |
| 210 | + if (info) { |
| 211 | + console.log(` ✓ State: ${info.state}`); |
| 212 | + } |
| 213 | + } catch (e) { |
| 214 | + console.log(` ✗ Failed: ${e}`); |
| 215 | + } |
| 216 | + |
| 217 | + console.log(); |
| 218 | + } |
| 219 | + |
| 220 | + // Query specific SubAgent |
| 221 | + console.log('='.repeat(70)); |
| 222 | + console.log('Query Specific SubAgent'); |
| 223 | + console.log('='.repeat(70)); |
| 224 | + console.log(); |
| 225 | + |
| 226 | + if (handles.length > 0) { |
| 227 | + const targetId = handles[0].id; |
| 228 | + console.log(`Querying ${targetId}...`); |
| 229 | + |
| 230 | + try { |
| 231 | + const info = orch.getSubagentInfo(targetId); |
| 232 | + if (info) { |
| 233 | + console.log(` ID: ${info.id}`); |
| 234 | + console.log(` Type: ${info.agentType}`); |
| 235 | + console.log(` Description: ${info.description}`); |
| 236 | + console.log(` State: ${info.state}`); |
| 237 | + console.log(` Parent ID: ${info.parentId || 'None'}`); |
| 238 | + console.log(` Created: ${info.createdAt}`); |
| 239 | + console.log(` Updated: ${info.updatedAt}`); |
| 240 | + |
| 241 | + if (info.currentActivity) { |
| 242 | + console.log(` Current Activity:`); |
| 243 | + console.log(` Type: ${info.currentActivity.activityType}`); |
| 244 | + if (info.currentActivity.data) { |
| 245 | + console.log(` Data: ${info.currentActivity.data}`); |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | + } catch (e) { |
| 250 | + console.log(` ✗ Failed: ${e}`); |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + console.log(); |
| 255 | + |
| 256 | + // Get all states |
| 257 | + console.log('='.repeat(70)); |
| 258 | + console.log('All SubAgent States'); |
| 259 | + console.log('='.repeat(70)); |
| 260 | + console.log(); |
| 261 | + |
| 262 | + try { |
| 263 | + const states = orch.getAllStates(); |
| 264 | + for (const entry of states) { |
| 265 | + console.log(` ${entry.id}: ${entry.state}`); |
| 266 | + } |
| 267 | + } catch (e) { |
| 268 | + console.log(`✗ Failed: ${e}`); |
| 269 | + } |
| 270 | + |
| 271 | + console.log(); |
| 272 | + |
| 273 | + // Wait for all to complete |
| 274 | + console.log('='.repeat(70)); |
| 275 | + console.log('Waiting for all SubAgents to complete...'); |
| 276 | + console.log('='.repeat(70)); |
| 277 | + console.log(); |
| 278 | + |
| 279 | + try { |
| 280 | + orch.waitAll(); |
| 281 | + console.log('✓ All SubAgents completed'); |
| 282 | + } catch (e) { |
| 283 | + console.log(`✗ Wait failed: ${e}`); |
| 284 | + } |
| 285 | + |
| 286 | + console.log(); |
| 287 | + |
| 288 | + // Final status |
| 289 | + console.log('='.repeat(70)); |
| 290 | + console.log('Final Status'); |
| 291 | + console.log('='.repeat(70)); |
| 292 | + console.log(); |
| 293 | + |
| 294 | + try { |
| 295 | + const finalStates = orch.getAllStates(); |
| 296 | + for (const entry of finalStates) { |
| 297 | + console.log(` ${entry.id}: ${entry.state}`); |
| 298 | + } |
| 299 | + } catch (e) { |
| 300 | + console.log(`✗ Failed: ${e}`); |
| 301 | + } |
| 302 | + |
| 303 | + console.log(); |
| 304 | + console.log('='.repeat(70)); |
| 305 | + console.log('✓ Test completed successfully!'); |
| 306 | + console.log('='.repeat(70)); |
| 307 | +} |
| 308 | + |
| 309 | +main().catch((error) => { |
| 310 | + console.error(`\n\n✗ Test failed with error: ${error}`); |
| 311 | + console.error(error.stack); |
| 312 | + process.exit(1); |
| 313 | +}); |
0 commit comments