(deps: ExecuteLoopStepDeps)
| 46 | } |
| 47 | |
| 48 | export async function executeLoopStep(deps: ExecuteLoopStepDeps): Promise<{ |
| 49 | readonly usage: TokenUsage; |
| 50 | readonly stopReason: LoopStepStopReason; |
| 51 | }> { |
| 52 | const { |
| 53 | turnId, |
| 54 | signal, |
| 55 | buildMessages, |
| 56 | buildMessagesStrict, |
| 57 | dispatchEvent, |
| 58 | llm, |
| 59 | tools, |
| 60 | hooks, |
| 61 | log, |
| 62 | currentStep, |
| 63 | maxRetryAttempts, |
| 64 | recordUsage, |
| 65 | } = deps; |
| 66 | |
| 67 | if (hooks?.beforeStep !== undefined) { |
| 68 | const beforeStep = await hooks.beforeStep({ |
| 69 | turnId, |
| 70 | stepNumber: currentStep, |
| 71 | signal, |
| 72 | llm, |
| 73 | }); |
| 74 | if (beforeStep?.block === true) { |
| 75 | throw new Error(beforeStep.reason ?? `Step ${String(currentStep)} was blocked`); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | signal.throwIfAborted(); |
| 80 | |
| 81 | const messages = await buildMessages(); |
| 82 | signal.throwIfAborted(); |
| 83 | |
| 84 | const stepUuid = randomUUID(); |
| 85 | |
| 86 | const step: ToolCallStepContext = { |
| 87 | tools, |
| 88 | hooks, |
| 89 | log, |
| 90 | dispatchEvent, |
| 91 | llm, |
| 92 | signal, |
| 93 | turnId, |
| 94 | currentStep, |
| 95 | stepUuid, |
| 96 | }; |
| 97 | |
| 98 | await dispatchEvent({ |
| 99 | type: 'step.begin', |
| 100 | uuid: stepUuid, |
| 101 | turnId, |
| 102 | step: currentStep, |
| 103 | }); |
| 104 | |
| 105 | const chatParams: LLMChatParams = { |
no test coverage detected