(input: RunTurnInput)
| 52 | } |
| 53 | |
| 54 | export async function runTurn(input: RunTurnInput): Promise<TurnResult> { |
| 55 | const { |
| 56 | turnId, |
| 57 | signal, |
| 58 | llm, |
| 59 | buildMessages, |
| 60 | buildMessagesStrict, |
| 61 | dispatchEvent, |
| 62 | tools, |
| 63 | hooks, |
| 64 | log, |
| 65 | maxSteps, |
| 66 | maxRetryAttempts, |
| 67 | recordStepUsage: hostRecordStepUsage, |
| 68 | } = input; |
| 69 | let usage: TokenUsage = emptyUsage(); |
| 70 | let steps = 0; |
| 71 | // Normal exits overwrite this with the completed step's stop reason. |
| 72 | let stopReason: LoopTurnStopReason = 'end_turn'; |
| 73 | let activeStep: number | undefined; |
| 74 | const recordStepUsage = async ( |
| 75 | stepUsage: TokenUsage, |
| 76 | ): Promise<RecordStepUsageResult | void> => { |
| 77 | usage = addUsage(usage, stepUsage); |
| 78 | return hostRecordStepUsage?.(stepUsage); |
| 79 | }; |
| 80 | |
| 81 | try { |
| 82 | while (true) { |
| 83 | signal.throwIfAborted(); |
| 84 | |
| 85 | if (maxSteps !== undefined && maxSteps > 0 && steps >= maxSteps) { |
| 86 | throw createMaxStepsExceededError(maxSteps); |
| 87 | } |
| 88 | |
| 89 | steps += 1; |
| 90 | activeStep = steps; |
| 91 | const stepResult = await executeLoopStep({ |
| 92 | turnId, |
| 93 | signal, |
| 94 | buildMessages, |
| 95 | buildMessagesStrict, |
| 96 | dispatchEvent, |
| 97 | llm, |
| 98 | tools, |
| 99 | hooks, |
| 100 | log, |
| 101 | currentStep: steps, |
| 102 | maxRetryAttempts, |
| 103 | recordUsage: recordStepUsage, |
| 104 | }); |
| 105 | activeStep = undefined; |
| 106 | |
| 107 | if (stepResult.stopReason === 'tool_use') { |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | const terminalStopReason: LoopTerminalStepStopReason = stepResult.stopReason; |
no test coverage detected