diff --git a/packages/ai/src/tools/communication-tools/done-tool/done-tool-execute.ts b/packages/ai/src/tools/communication-tools/done-tool/done-tool-execute.ts index fbd7062b7..573f11f97 100644 --- a/packages/ai/src/tools/communication-tools/done-tool/done-tool-execute.ts +++ b/packages/ai/src/tools/communication-tools/done-tool/done-tool-execute.ts @@ -20,14 +20,21 @@ async function processDone( state: DoneToolState, toolCallId: string, messageId: string, - _context: DoneToolContext + _context: DoneToolContext, + input: DoneToolInput ): Promise { const output: DoneToolOutput = { success: true, }; + // Update state with the full finalResponse from input to ensure completeness + const updatedState: DoneToolState = { + ...state, + finalResponse: input.finalResponse, + }; + // Create both the tool call and result messages to maintain proper ordering - const rawLlmMessage = createDoneToolRawLlmMessageEntry(state, toolCallId); + const rawLlmMessage = createDoneToolRawLlmMessageEntry(updatedState, toolCallId); const rawToolResultEntry = createRawToolResultEntry(toolCallId, DONE_TOOL_NAME, output); try { @@ -55,12 +62,12 @@ async function processDone( // Factory function that creates the execute function with proper context typing export function createDoneToolExecute(context: DoneToolContext, state: DoneToolState) { return wrapTraced( - async (_input: DoneToolInput): Promise => { + async (input: DoneToolInput): Promise => { if (!state.toolCallId) { throw new Error('Tool call ID is required'); } - const result = await processDone(state, state.toolCallId, context.messageId, context); + const result = await processDone(state, state.toolCallId, context.messageId, context, input); // Wait for all pending updates from delta/finish to complete before returning await waitForPendingUpdates(context.messageId);