| 68 | * @throws {ChatProviderError} (and subtypes) on provider failures. |
| 69 | */ |
| 70 | export async function step( |
| 71 | provider: ChatProvider, |
| 72 | systemPrompt: string, |
| 73 | toolset: Toolset, |
| 74 | history: Message[], |
| 75 | callbacks?: StepCallbacks, |
| 76 | options?: GenerateOptions, |
| 77 | ): Promise<StepResult> { |
| 78 | const toolCalls: ToolCall[] = []; |
| 79 | const toolResultPromises = new Map<string, Promise<ToolResult>>(); |
| 80 | |
| 81 | async function onToolCall(toolCall: ToolCall): Promise<void> { |
| 82 | toolCalls.push(toolCall); |
| 83 | |
| 84 | const handleResult = toolset.handle(toolCall); |
| 85 | |
| 86 | // Normalise to a Promise regardless of whether handle() returned sync. |
| 87 | const promise: Promise<ToolResult> = |
| 88 | handleResult instanceof Promise ? handleResult : Promise.resolve(handleResult); |
| 89 | |
| 90 | // When the promise resolves, fire the onToolResult callback. |
| 91 | const tracked = promise.then((result) => { |
| 92 | if (callbacks?.onToolResult !== undefined) { |
| 93 | callbacks.onToolResult(result); |
| 94 | } |
| 95 | return result; |
| 96 | }); |
| 97 | |
| 98 | toolResultPromises.set(toolCall.id, tracked); |
| 99 | void tracked.catch(() => {}); |
| 100 | } |
| 101 | |
| 102 | let result: GenerateResult; |
| 103 | try { |
| 104 | const generateCallbacks: GenerateCallbacks = { onToolCall }; |
| 105 | if (callbacks?.onMessagePart !== undefined) { |
| 106 | generateCallbacks.onMessagePart = callbacks.onMessagePart; |
| 107 | } |
| 108 | result = await generate( |
| 109 | provider, |
| 110 | systemPrompt, |
| 111 | toolset.tools, |
| 112 | history, |
| 113 | generateCallbacks, |
| 114 | options, |
| 115 | ); |
| 116 | } catch (error: unknown) { |
| 117 | // On provider or cancellation errors, cancel/await all pending tool |
| 118 | // result promises to avoid dangling work. |
| 119 | await cleanupPromises(toolResultPromises); |
| 120 | throw error; |
| 121 | } |
| 122 | |
| 123 | return { |
| 124 | id: result.id, |
| 125 | message: result.message, |
| 126 | usage: result.usage, |
| 127 | finishReason: result.finishReason, |