Update streamResponseAfterToolCall.ts

This commit is contained in:
Patrick Erichsen 2025-07-15 22:36:14 -07:00
parent d4623aa51e
commit 9501e61ae9

View file

@ -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<typeof findAssistantMessageWithToolCall>
>["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<typeof findAssistantMessageWithToolCall>,
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({})));
}
}),