| 1 | import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit"; |
| 2 | import { ChatMessage } from "core"; |
| 3 | import { renderContextItems } from "core/util/messageContent"; |
| 4 | import { |
| 5 | resetNextCodeBlockToApplyIndex, |
| 6 | streamUpdate, |
| 7 | } from "../slices/sessionSlice"; |
| 8 | import { AppThunkDispatch, RootState, ThunkApiType } from "../store"; |
| 9 | import { findToolCallById } from "../util"; |
| 10 | import { streamNormalInput } from "./streamNormalInput"; |
| 11 | import { streamThunkWrapper } from "./streamThunkWrapper"; |
| 12 | |
| 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 | /** |
| 45 | * Determines if we should continue streaming based on tool call completion status. |
| 46 | */ |
| 47 | function shouldContinueStreaming( |
| 48 | assistantMessage: ReturnType<typeof findAssistantMessageWithToolCall>, |
| 49 | ): boolean { |
| 50 | 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 | return true; |
| 59 | } |
| 60 | |
| 61 | // Multiple tool calls - only continue if all are complete |
| 62 | return areAllToolCallsComplete(assistantMessage.toolCallStates); |
| 63 | } |
| 64 | |
| 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])); |
| 79 | } |
| 80 | |
| 81 | export const streamResponseAfterToolCall = createAsyncThunk< |
| 82 | void, |
| 83 | { toolCallId: string }, |
| 84 | ThunkApiType |
| 85 | >( |
| 86 | "chat/streamAfterToolCall", |
| 87 | async ({ toolCallId }, { dispatch, getState }) => { |
| 88 | await dispatch( |
| 89 | streamThunkWrapper(async () => { |
| 90 | const state = getState(); |
| 91 | |
| 92 | const toolCallState = findToolCallById( |
| 93 | state.session.history, |
| 94 | toolCallId, |
| 95 | ); |
| 96 | |
| 97 | if (!toolCallState) { |
| 98 | return; // in cases where edit tool is cancelled mid apply, this will be triggered |
| 99 | } |
| 100 | |
| 101 | const toolOutput = toolCallState.output ?? []; |
| 102 | |
| 103 | dispatch(resetNextCodeBlockToApplyIndex()); |
| 104 | await new Promise((resolve) => setTimeout(resolve, 0)); |
| 105 | |
| 106 | // Create and dispatch the tool message |
| 107 | createAndDispatchToolMessage(dispatch, toolCallId, toolOutput); |
| 108 | |
| 109 | // Check if we should continue streaming based on tool call completion |
| 110 | const history = getState().session.history; |
| 111 | const assistantMessage = findAssistantMessageWithToolCall( |
| 112 | history, |
| 113 | toolCallId, |
| 114 | ); |
| 115 | |
| 116 | if (shouldContinueStreaming(assistantMessage)) { |
| 117 | unwrapResult(await dispatch(streamNormalInput({}))); |
| 118 | } |
| 119 | }), |
| 120 | ); |
| 121 | }, |
| 122 | ); |