diff options
Diffstat (limited to 'packages/server/src')
| -rw-r--r-- | packages/server/src/core/turn.ts | 127 |
1 files changed, 61 insertions, 66 deletions
diff --git a/packages/server/src/core/turn.ts b/packages/server/src/core/turn.ts index 0a1c594c..31656466 100644 --- a/packages/server/src/core/turn.ts +++ b/packages/server/src/core/turn.ts @@ -130,83 +130,78 @@ export class Turn { yield event; } } + } - // Execute pending tool calls - const toolPromises = this.pendingToolCalls.map( - async (pendingToolCall): Promise<ServerToolExecutionOutcome> => { - const tool = this.availableTools.get(pendingToolCall.name); - if (!tool) { - return { - ...pendingToolCall, - error: new Error( - `Tool "${pendingToolCall.name}" not found or not provided to Turn.`, - ), - confirmationDetails: undefined, - }; - } + // Execute pending tool calls + const toolPromises = this.pendingToolCalls.map( + async (pendingToolCall): Promise<ServerToolExecutionOutcome> => { + const tool = this.availableTools.get(pendingToolCall.name); + if (!tool) { + return { + ...pendingToolCall, + error: new Error( + `Tool "${pendingToolCall.name}" not found or not provided to Turn.`, + ), + confirmationDetails: undefined, + }; + } - try { - const confirmationDetails = await tool.shouldConfirmExecute( - pendingToolCall.args, - ); - if (confirmationDetails) { - return { ...pendingToolCall, confirmationDetails }; - } else { - const result = await tool.execute(pendingToolCall.args); - return { - ...pendingToolCall, - result, - confirmationDetails: undefined, - }; - } - } catch (execError: unknown) { + try { + const confirmationDetails = await tool.shouldConfirmExecute( + pendingToolCall.args, + ); + if (confirmationDetails) { + return { ...pendingToolCall, confirmationDetails }; + } else { + const result = await tool.execute(pendingToolCall.args); return { ...pendingToolCall, - error: new Error( - `Tool execution failed: ${execError instanceof Error ? execError.message : String(execError)}`, - ), + result, confirmationDetails: undefined, }; } - }, - ); - const outcomes = await Promise.all(toolPromises); + } catch (execError: unknown) { + return { + ...pendingToolCall, + error: new Error( + `Tool execution failed: ${execError instanceof Error ? execError.message : String(execError)}`, + ), + confirmationDetails: undefined, + }; + } + }, + ); + const outcomes = await Promise.all(toolPromises); - // Process outcomes and prepare function responses - this.pendingToolCalls = []; // Clear pending calls for this turn + // Process outcomes and prepare function responses + this.pendingToolCalls = []; // Clear pending calls for this turn - for (let i = 0; i < outcomes.length; i++) { - const outcome = outcomes[i]; - if (outcome.confirmationDetails) { - this.confirmationDetails.push(outcome.confirmationDetails); - const serverConfirmationetails: ServerToolCallConfirmationDetails = { - request: { - callId: outcome.callId, - name: outcome.name, - args: outcome.args, - }, - details: outcome.confirmationDetails, - }; - yield { - type: GeminiEventType.ToolCallConfirmation, - value: serverConfirmationetails, - }; - } else { - const responsePart = this.buildFunctionResponse(outcome); - this.fnResponses.push(responsePart); - const responseInfo: ToolCallResponseInfo = { + for (const outcome of outcomes) { + if (outcome.confirmationDetails) { + this.confirmationDetails.push(outcome.confirmationDetails); + const serverConfirmationetails: ServerToolCallConfirmationDetails = { + request: { callId: outcome.callId, - responsePart, - resultDisplay: outcome.result?.returnDisplay, - error: outcome.error, - }; - yield { type: GeminiEventType.ToolCallResponse, value: responseInfo }; - } + name: outcome.name, + args: outcome.args, + }, + details: outcome.confirmationDetails, + }; + yield { + type: GeminiEventType.ToolCallConfirmation, + value: serverConfirmationetails, + }; + } else { + const responsePart = this.buildFunctionResponse(outcome); + this.fnResponses.push(responsePart); + const responseInfo: ToolCallResponseInfo = { + callId: outcome.callId, + responsePart, + resultDisplay: outcome.result?.returnDisplay, + error: outcome.error, + }; + yield { type: GeminiEventType.ToolCallResponse, value: responseInfo }; } - - // If there were function responses, the caller (GeminiService) will loop - // and call run() again with these responses. - // If no function responses, the turn ends here. } } |
