| 10 | const parentSpanContext = process.env.PARENT_SPAN_CONTEXT; |
| 11 | |
| 12 | async function run() { |
| 13 | // Import the appropriate agent - but we'll call the underlying logic, not the traced wrapper |
| 14 | let runAgent: (question: string, callbacks?: any) => Promise<any>; |
| 15 | |
| 16 | switch (agentType) { |
| 17 | case 'bash': |
| 18 | const { runBashAgent } = await import('./bash-agent.js'); |
| 19 | runAgent = runBashAgent; |
| 20 | break; |
| 21 | case 'bash-sqlite': |
| 22 | const { runBashSqliteAgent } = await import('./bash-sqlite-agent.js'); |
| 23 | runAgent = runBashSqliteAgent; |
| 24 | break; |
| 25 | case 'fs': |
| 26 | const { runFsAgent } = await import('./fs-agent.js'); |
| 27 | runAgent = runFsAgent; |
| 28 | break; |
| 29 | case 'sql': |
| 30 | const { runSqlAgent } = await import('./sql-agent.js'); |
| 31 | runAgent = runSqlAgent; |
| 32 | break; |
| 33 | case 'embedding': |
| 34 | const { runEmbeddingAgent } = await import('./embedding-agent.js'); |
| 35 | runAgent = runEmbeddingAgent; |
| 36 | break; |
| 37 | case 'codemode': |
| 38 | const { runCodemodeAgent } = await import('./codemode-agent.js'); |
| 39 | runAgent = runCodemodeAgent; |
| 40 | break; |
| 41 | default: |
| 42 | throw new Error(`Unknown agent type: ${agentType}`); |
| 43 | } |
| 44 | |
| 45 | const callbacks = { |
| 46 | onText: (chunk: string) => { |
| 47 | process.send?.({ type: 'text', chunk }); |
| 48 | }, |
| 49 | onToolCall: (toolName: string, args: Record<string, unknown>) => { |
| 50 | process.send?.({ type: 'tool_call', toolName, args }); |
| 51 | }, |
| 52 | onToolResult: (toolName: string, result: string) => { |
| 53 | process.send?.({ type: 'tool_result', toolName, result }); |
| 54 | }, |
| 55 | onProgress: (update: { toolCalls: number; tokens: number }) => { |
| 56 | process.send?.({ type: 'progress', ...update }); |
| 57 | }, |
| 58 | }; |
| 59 | |
| 60 | // If we have a parent span context, use it to create a child span |
| 61 | // Otherwise just run the agent directly |
| 62 | // Note: parentSpanContext from span.export() is an opaque string, not JSON |
| 63 | if (parentSpanContext) { |
| 64 | // Run within traced context, passing the parent span slug directly |
| 65 | const result = await traced( |
| 66 | async (_span) => { |
| 67 | return runAgent(question, callbacks); |
| 68 | }, |
| 69 | { name: agentType, parent: parentSpanContext }, |