From 9501e61ae927a8648e03100fc00a979bec1ce295 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Tue, 15 Jul 2025 22:36:14 -0700 Subject: [PATCH] Update streamResponseAfterToolCall.ts --- .../thunks/streamResponseAfterToolCall.ts | 84 +++++-------------- 1 file changed, 21 insertions(+), 63 deletions(-) diff --git a/gui/src/redux/thunks/streamResponseAfterToolCall.ts b/gui/src/redux/thunks/streamResponseAfterToolCall.ts index 58a63f598..ed16b5a5a 100644 --- a/gui/src/redux/thunks/streamResponseAfterToolCall.ts +++ b/gui/src/redux/thunks/streamResponseAfterToolCall.ts @@ -2,80 +2,32 @@ import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit"; import { ChatMessage } from "core"; import { renderContextItems } from "core/util/messageContent"; import { + ChatHistoryItemWithMessageId, resetNextCodeBlockToApplyIndex, streamUpdate, } from "../slices/sessionSlice"; -import { AppThunkDispatch, RootState, ThunkApiType } from "../store"; +import { ThunkApiType } from "../store"; import { findToolCallById } from "../util"; import { streamNormalInput } from "./streamNormalInput"; import { streamThunkWrapper } from "./streamThunkWrapper"; -/** - * Finds the assistant message that contains the specified tool call. - */ -function findAssistantMessageWithToolCall( - history: RootState["session"]["history"], - toolCallId: string, -) { - return history.find( - (item) => - item.message.role === "assistant" && - item.toolCallStates?.some((tc) => tc.toolCallId === toolCallId), - ); -} - -/** - * Checks if all parallel tool calls in an assistant message are complete. - */ -function areAllToolCallsComplete( - toolCallStates: NonNullable< - ReturnType - >["toolCallStates"], -): boolean { - if (!toolCallStates) return false; - - const completedToolCalls = toolCallStates.filter( - (tc) => tc.status === "done", - ); - - return completedToolCalls.length === toolCallStates.length; -} - /** * Determines if we should continue streaming based on tool call completion status. */ -function shouldContinueStreaming( - assistantMessage: ReturnType, +function areAllToolsDoneStreaming( + assistantMessage: ChatHistoryItemWithMessageId | undefined, ): boolean { + // This might occur because of race conditions, if so, the tools are completed if (!assistantMessage?.toolCallStates) { - return false; // No assistant message found - don't stream - } - - const totalToolCalls = assistantMessage.toolCallStates.length; - - // Single tool call - always continue streaming - if (totalToolCalls === 1) { return true; } - // Multiple tool calls - only continue if all are complete - return areAllToolCallsComplete(assistantMessage.toolCallStates); -} + // Only continue if all tool calls are complete + const completedToolCalls = assistantMessage.toolCallStates.filter( + (tc) => tc.status === "done", + ); -/** - * Creates and dispatches a tool message for the completed tool call. - */ -function createAndDispatchToolMessage( - dispatch: AppThunkDispatch, - toolCallId: string, - toolOutput: any[], -): void { - const newMessage: ChatMessage = { - role: "tool", - content: renderContextItems(toolOutput), - toolCallId, - }; - dispatch(streamUpdate([newMessage])); + return completedToolCalls.length === assistantMessage.toolCallStates.length; } export const streamResponseAfterToolCall = createAsyncThunk< @@ -104,16 +56,22 @@ export const streamResponseAfterToolCall = createAsyncThunk< await new Promise((resolve) => setTimeout(resolve, 0)); // Create and dispatch the tool message - createAndDispatchToolMessage(dispatch, toolCallId, toolOutput); + const newMessage: ChatMessage = { + role: "tool", + content: renderContextItems(toolOutput), + toolCallId, + }; + dispatch(streamUpdate([newMessage])); // Check if we should continue streaming based on tool call completion const history = getState().session.history; - const assistantMessage = findAssistantMessageWithToolCall( - history, - toolCallId, + const assistantMessage = history.find( + (item) => + item.message.role === "assistant" && + item.toolCallStates?.some((tc) => tc.toolCallId === toolCallId), ); - if (shouldContinueStreaming(assistantMessage)) { + if (areAllToolsDoneStreaming(assistantMessage)) { unwrapResult(await dispatch(streamNormalInput({}))); } }),