( question: string, callbacks?: StreamCallbacks, modelId?: ModelId, )
| 33 | - Users who opened most PRs: data.users.sort((a,b) => b.prsOpened - a.prsOpened).slice(0,10)`; |
| 34 | |
| 35 | export async function runCodemodeAgent( |
| 36 | question: string, |
| 37 | callbacks?: StreamCallbacks, |
| 38 | modelId?: ModelId, |
| 39 | ): Promise<AgentResult> { |
| 40 | const startTime = Date.now(); |
| 41 | let fullText = ''; |
| 42 | let totalTokens = 0; |
| 43 | let toolCallCount = 0; |
| 44 | |
| 45 | const agent = new ToolLoopAgent({ |
| 46 | model: createModel(modelId ?? getModelFromEnv()), |
| 47 | instructions: SYSTEM_PROMPT, |
| 48 | tools: codemodeTools, |
| 49 | stopWhen: stepCountIs(MAX_STEPS), |
| 50 | }); |
| 51 | |
| 52 | const stream = await agent.stream({ |
| 53 | prompt: question, |
| 54 | }); |
| 55 | |
| 56 | for await (const event of stream.fullStream) { |
| 57 | switch (event.type) { |
| 58 | case 'text-delta': |
| 59 | fullText += event.text; |
| 60 | callbacks?.onText?.(event.text); |
| 61 | break; |
| 62 | |
| 63 | case 'tool-call': |
| 64 | toolCallCount++; |
| 65 | callbacks?.onToolCall?.(event.toolName, event.input as Record<string, unknown>); |
| 66 | callbacks?.onProgress?.({ toolCalls: toolCallCount, tokens: totalTokens }); |
| 67 | break; |
| 68 | |
| 69 | case 'tool-result': |
| 70 | const resultStr = |
| 71 | typeof event.output === 'string' ? event.output : JSON.stringify(event.output); |
| 72 | callbacks?.onToolResult?.(event.toolName, resultStr.slice(0, 500)); |
| 73 | break; |
| 74 | |
| 75 | case 'finish-step': |
| 76 | totalTokens += event.usage?.totalTokens || 0; |
| 77 | callbacks?.onProgress?.({ toolCalls: toolCallCount, tokens: totalTokens }); |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Check if agent ran out of steps without completing |
| 83 | const steps = await stream.steps; |
| 84 | const lastStep = steps[steps.length - 1]; |
| 85 | if (steps.length >= MAX_STEPS && lastStep?.finishReason === 'tool-calls') { |
| 86 | throw new Error(`Agent reached maximum ${MAX_STEPS} steps without producing a final answer`); |
| 87 | } |
| 88 | |
| 89 | return { |
| 90 | answer: fullText, |
| 91 | latencyMs: Date.now() - startTime, |
| 92 | tokens: totalTokens, |
nothing calls this directly
no test coverage detected