diff --git a/extensions/cli/src/stream/handleToolCalls.ts b/extensions/cli/src/stream/handleToolCalls.ts index 5150a436b..bc5d99176 100644 --- a/extensions/cli/src/stream/handleToolCalls.ts +++ b/extensions/cli/src/stream/handleToolCalls.ts @@ -109,51 +109,15 @@ export async function handleToolCalls( const { preprocessedCalls, errorChatEntries } = await preprocessStreamedToolCalls(isHeadless, toolCalls, callbacks); - // Add any preprocessing errors to chat history - // Convert error entries from OpenAI format to ChatHistoryItem format + // Add any preprocessing errors to the toolCallStates on the assistant message + // (NOT as separate history items, which would cause duplicate tool_result messages) errorChatEntries.forEach((errorEntry) => { - const item = createHistoryItem({ - role: "tool", - content: stripImages(errorEntry.content) || "", - toolCallId: errorEntry.tool_call_id, - }); - if (useService) { - chatHistorySvc.addHistoryItem(item); - } else { - // Fallback only when service is unavailable - chatHistory.push(item); - } - }); - - // Execute the valid preprocessed tool calls - const { chatHistoryEntries: toolResults, hasRejection } = - await executeStreamedToolCalls(preprocessedCalls, callbacks, isHeadless); - - if (isHeadless && hasRejection) { - logger.debug( - "Tool call rejected in headless mode - returning current content", - ); - return true; // Signal early return needed - } - - // Convert tool results and add them to the chat history with status from execution - toolResults.forEach((toolResult) => { - const resultContent = - typeof toolResult.content === "string" ? toolResult.content : ""; - - // Use the status from the tool execution result instead of text matching - const status = toolResult.status; - - logger.debug("Tool result status", { - status, - toolCallId: toolResult.tool_call_id, - }); - + const errorContent = stripImages(errorEntry.content) || ""; if (useService) { chatHistorySvc.addToolResult( - toolResult.tool_call_id, - resultContent, - status, + errorEntry.tool_call_id, + errorContent, + "errored", ); } else { // Fallback only when service is unavailable: update local tool state @@ -165,13 +129,13 @@ export async function handleToolCalls( chatHistory[lastAssistantIndex].toolCallStates ) { const toolState = chatHistory[lastAssistantIndex].toolCallStates.find( - (ts) => ts.toolCallId === toolResult.tool_call_id, + (ts) => ts.toolCallId === errorEntry.tool_call_id, ); if (toolState) { - toolState.status = status; + toolState.status = "errored"; toolState.output = [ { - content: resultContent, + content: errorContent, name: `Tool Result`, description: "Tool execution result", }, @@ -180,6 +144,27 @@ export async function handleToolCalls( } } }); + + // Execute the valid preprocessed tool calls + // Note: executeStreamedToolCalls adds tool results to toolCallStates via + // services.chatHistory.addToolResult() internally + const { hasRejection } = await executeStreamedToolCalls( + preprocessedCalls, + callbacks, + isHeadless, + ); + + if (isHeadless && hasRejection) { + logger.debug( + "Tool call rejected in headless mode - returning current content", + ); + return true; // Signal early return needed + } + + // Tool results are already added to toolCallStates in executeStreamedToolCalls + // via services.chatHistory.addToolResult() - no need to add them again here. + // Adding them again would be redundant (and previously caused duplicate tool_result messages + // when combined with separate tool history items). return false; }