Elffuss BraidHub

kikocis/continue public

Update streamResponseAfterToolCall.ts
Changesetf960a00a2c654423b2e16c5f086b4f019431acad6e095106ed2cce1e4a7e7070
AuthorPatrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-07-15 22:36 -07:00
CommitterPatrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-07-15 22:36 -07:00
Parents920ebdc17685

Browse the files at this changeset

1 file changed

modified gui/src/redux/thunks/streamResponseAfterToolCall.ts +21 −63
@@ -2,80 +2,32 @@
22 import { ChatMessage } from "core";
33 import { renderContextItems } from "core/util/messageContent";
44 import {
5+ ChatHistoryItemWithMessageId,
56 resetNextCodeBlockToApplyIndex,
67 streamUpdate,
78 } from "../slices/sessionSlice";
8-import { AppThunkDispatch, RootState, ThunkApiType } from "../store";
9+import { ThunkApiType } from "../store";
910 import { findToolCallById } from "../util";
1011 import { streamNormalInput } from "./streamNormalInput";
1112 import { streamThunkWrapper } from "./streamThunkWrapper";
1213
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-
4414 /**
4515 * Determines if we should continue streaming based on tool call completion status.
4616 */
47-function shouldContinueStreaming(
48- assistantMessage: ReturnType<typeof findAssistantMessageWithToolCall>,
17+function areAllToolsDoneStreaming(
18+ assistantMessage: ChatHistoryItemWithMessageId | undefined,
4919 ): boolean {
20+ // This might occur because of race conditions, if so, the tools are completed
5021 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) {
5822 return true;
5923 }
6024
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+ );
6429
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;
7931 }
8032
8133 export const streamResponseAfterToolCall = createAsyncThunk<
@@ -104,16 +56,22 @@
10456 await new Promise((resolve) => setTimeout(resolve, 0));
10557
10658 // 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]));
10865
10966 // Check if we should continue streaming based on tool call completion
11067 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),
11472 );
11573
116- if (shouldContinueStreaming(assistantMessage)) {
74+ if (areAllToolsDoneStreaming(assistantMessage)) {
11775 unwrapResult(await dispatch(streamNormalInput({})));
11876 }
11977 }),