Changesets · Branches · Tags · Compare
| Changeset | f960a00a2c654423b2e16c5f086b4f019431acad6e095106ed2cce1e4a7e7070 |
|---|---|
| Author | Patrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-07-15 22:36 -07:00 |
| Committer | Patrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-07-15 22:36 -07:00 |
| Parents | 920ebdc17685 |
1 file changed
modified gui/src/redux/thunks/streamResponseAfterToolCall.ts +21 −63
| @@ -2,80 +2,32 @@ | ||
| 2 | 2 | import { ChatMessage } from "core"; |
| 3 | 3 | import { renderContextItems } from "core/util/messageContent"; |
| 4 | 4 | import { |
| 5 | + ChatHistoryItemWithMessageId, | |
| 5 | 6 | resetNextCodeBlockToApplyIndex, |
| 6 | 7 | streamUpdate, |
| 7 | 8 | } from "../slices/sessionSlice"; |
| 8 | -import { AppThunkDispatch, RootState, ThunkApiType } from "../store"; | |
| 9 | +import { ThunkApiType } from "../store"; | |
| 9 | 10 | import { findToolCallById } from "../util"; |
| 10 | 11 | import { streamNormalInput } from "./streamNormalInput"; |
| 11 | 12 | import { streamThunkWrapper } from "./streamThunkWrapper"; |
| 12 | 13 | |
| 13 | -/** | |
| 14 | - * Finds the assistant message that contains the specified tool call. | |
| 15 | - */ | |
| 16 | -function findAssistantMessageWithToolCall( | |
| 17 | - history: RootState["session"]["history"], | |
| 18 | - toolCallId: string, | |
| 19 | -) { | |
| 20 | - return history.find( | |
| 21 | - (item) => | |
| 22 | - item.message.role === "assistant" && | |
| 23 | - item.toolCallStates?.some((tc) => tc.toolCallId === toolCallId), | |
| 24 | - ); | |
| 25 | -} | |
| 26 | - | |
| 27 | -/** | |
| 28 | - * Checks if all parallel tool calls in an assistant message are complete. | |
| 29 | - */ | |
| 30 | -function areAllToolCallsComplete( | |
| 31 | - toolCallStates: NonNullable< | |
| 32 | - ReturnType<typeof findAssistantMessageWithToolCall> | |
| 33 | - >["toolCallStates"], | |
| 34 | -): boolean { | |
| 35 | - if (!toolCallStates) return false; | |
| 36 | - | |
| 37 | - const completedToolCalls = toolCallStates.filter( | |
| 38 | - (tc) => tc.status === "done", | |
| 39 | - ); | |
| 40 | - | |
| 41 | - return completedToolCalls.length === toolCallStates.length; | |
| 42 | -} | |
| 43 | - | |
| 44 | 14 | /** |
| 45 | 15 | * Determines if we should continue streaming based on tool call completion status. |
| 46 | 16 | */ |
| 47 | -function shouldContinueStreaming( | |
| 48 | - assistantMessage: ReturnType<typeof findAssistantMessageWithToolCall>, | |
| 17 | +function areAllToolsDoneStreaming( | |
| 18 | + assistantMessage: ChatHistoryItemWithMessageId | undefined, | |
| 49 | 19 | ): boolean { |
| 20 | + // This might occur because of race conditions, if so, the tools are completed | |
| 50 | 21 | if (!assistantMessage?.toolCallStates) { |
| 51 | - return false; // No assistant message found - don't stream | |
| 52 | - } | |
| 53 | - | |
| 54 | - const totalToolCalls = assistantMessage.toolCallStates.length; | |
| 55 | - | |
| 56 | - // Single tool call - always continue streaming | |
| 57 | - if (totalToolCalls === 1) { | |
| 58 | 22 | return true; |
| 59 | 23 | } |
| 60 | 24 | |
| 61 | - // Multiple tool calls - only continue if all are complete | |
| 62 | - return areAllToolCallsComplete(assistantMessage.toolCallStates); | |
| 63 | -} | |
| 25 | + // Only continue if all tool calls are complete | |
| 26 | + const completedToolCalls = assistantMessage.toolCallStates.filter( | |
| 27 | + (tc) => tc.status === "done", | |
| 28 | + ); | |
| 64 | 29 | |
| 65 | -/** | |
| 66 | - * Creates and dispatches a tool message for the completed tool call. | |
| 67 | - */ | |
| 68 | -function createAndDispatchToolMessage( | |
| 69 | - dispatch: AppThunkDispatch, | |
| 70 | - toolCallId: string, | |
| 71 | - toolOutput: any[], | |
| 72 | -): void { | |
| 73 | - const newMessage: ChatMessage = { | |
| 74 | - role: "tool", | |
| 75 | - content: renderContextItems(toolOutput), | |
| 76 | - toolCallId, | |
| 77 | - }; | |
| 78 | - dispatch(streamUpdate([newMessage])); | |
| 30 | + return completedToolCalls.length === assistantMessage.toolCallStates.length; | |
| 79 | 31 | } |
| 80 | 32 | |
| 81 | 33 | export const streamResponseAfterToolCall = createAsyncThunk< |
| @@ -104,16 +56,22 @@ | ||
| 104 | 56 | await new Promise((resolve) => setTimeout(resolve, 0)); |
| 105 | 57 | |
| 106 | 58 | // Create and dispatch the tool message |
| 107 | - createAndDispatchToolMessage(dispatch, toolCallId, toolOutput); | |
| 59 | + const newMessage: ChatMessage = { | |
| 60 | + role: "tool", | |
| 61 | + content: renderContextItems(toolOutput), | |
| 62 | + toolCallId, | |
| 63 | + }; | |
| 64 | + dispatch(streamUpdate([newMessage])); | |
| 108 | 65 | |
| 109 | 66 | // Check if we should continue streaming based on tool call completion |
| 110 | 67 | const history = getState().session.history; |
| 111 | - const assistantMessage = findAssistantMessageWithToolCall( | |
| 112 | - history, | |
| 113 | - toolCallId, | |
| 68 | + const assistantMessage = history.find( | |
| 69 | + (item) => | |
| 70 | + item.message.role === "assistant" && | |
| 71 | + item.toolCallStates?.some((tc) => tc.toolCallId === toolCallId), | |
| 114 | 72 | ); |
| 115 | 73 | |
| 116 | - if (shouldContinueStreaming(assistantMessage)) { | |
| 74 | + if (areAllToolsDoneStreaming(assistantMessage)) { | |
| 117 | 75 | unwrapResult(await dispatch(streamNormalInput({}))); |
| 118 | 76 | } |
| 119 | 77 | }), |