| 53 | } |
| 54 | |
| 55 | async function runTask( |
| 56 | client: CodebuffClient, |
| 57 | task: TaskDefinition, |
| 58 | agentDefinitions: AgentDefinition[], |
| 59 | taskIndex: number, |
| 60 | ): Promise<{ |
| 61 | success: boolean |
| 62 | traceFile: string |
| 63 | output: unknown |
| 64 | validationErrors: string[] |
| 65 | }> { |
| 66 | const events: TraceEvent[] = [] |
| 67 | const validationErrors: string[] = [] |
| 68 | const startTime = Date.now() |
| 69 | |
| 70 | console.log(`\n${'='.repeat(60)}`) |
| 71 | console.log(`Task ${taskIndex}: ${task.name}`) |
| 72 | console.log(`Repo: ${task.repoUrl}`) |
| 73 | console.log(`Prompt: ${task.prompt}`) |
| 74 | console.log(`${'='.repeat(60)}\n`) |
| 75 | |
| 76 | const runState = await client.run({ |
| 77 | agent: 'librarian', |
| 78 | prompt: task.prompt, |
| 79 | params: { repoUrl: task.repoUrl }, |
| 80 | agentDefinitions, |
| 81 | maxAgentSteps: 40, |
| 82 | handleEvent: (event) => { |
| 83 | events.push({ |
| 84 | timestamp: new Date().toISOString(), |
| 85 | type: event.type, |
| 86 | data: event as Record<string, unknown>, |
| 87 | }) |
| 88 | |
| 89 | if (event.type === 'text') { |
| 90 | process.stdout.write(event.text ?? '') |
| 91 | } else if (event.type === 'tool_call') { |
| 92 | console.log(`\n[Tool Call] ${event.toolName}`) |
| 93 | } else if (event.type === 'tool_result') { |
| 94 | const preview = JSON.stringify(event.output)?.slice(0, 200) |
| 95 | console.log(`[Tool Result] ${preview}...`) |
| 96 | } else if (event.type === 'error') { |
| 97 | console.error(`[Error] ${event.message}`) |
| 98 | } else if (event.type === 'subagent_start') { |
| 99 | console.log(`[Subagent Start] ${event.agentType}`) |
| 100 | } else if (event.type === 'subagent_finish') { |
| 101 | console.log(`[Subagent Finish] ${event.agentType}`) |
| 102 | } |
| 103 | }, |
| 104 | }) |
| 105 | |
| 106 | const duration = ((Date.now() - startTime) / 1000).toFixed(1) |
| 107 | const output = runState.output |
| 108 | |
| 109 | // Validate structured output |
| 110 | if (output?.type === 'structuredOutput' && output.value !== null) { |
| 111 | const data = output.value as Record<string, unknown> |
| 112 | |