| 1 | import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit"; |
| 2 | import { ChatMessage } from "core"; |
| 3 | import { renderContextItems } from "core/util/messageContent"; |
| 4 | import { |
| 5 | ChatHistoryItemWithMessageId, |
| 6 | resetNextCodeBlockToApplyIndex, |
| 7 | streamUpdate, |
| 8 | } from "../slices/sessionSlice"; |
| 9 | import { ThunkApiType } from "../store"; |
| 10 | import { findToolCallById } from "../util"; |
| 11 | import { streamNormalInput } from "./streamNormalInput"; |
| 12 | import { streamThunkWrapper } from "./streamThunkWrapper"; |
| 13 | |
| 14 | /** |
| 15 | * Determines if we should continue streaming based on tool call completion status. |
| 16 | */ |
| 17 | function areAllToolsDoneStreaming( |
| 18 | assistantMessage: ChatHistoryItemWithMessageId | undefined, |
| 19 | continueAfterToolRejection: boolean | undefined, |
| 20 | ): boolean { |
| 21 | // This might occur because of race conditions, if so, the tools are completed |
| 22 | if (!assistantMessage?.toolCallStates) { |
| 23 | return true; |
| 24 | } |
| 25 | |
| 26 | // Only continue if all tool calls are complete |
| 27 | const completedToolCalls = assistantMessage.toolCallStates.filter( |
| 28 | (tc) => |
| 29 | tc.status === "done" || |
| 30 | tc.status === "errored" || |
| 31 | (continueAfterToolRejection && tc.status === "canceled"), |
| 32 | ); |
| 33 | |
| 34 | return completedToolCalls.length === assistantMessage.toolCallStates.length; |
| 35 | } |
| 36 | |
| 37 | export const streamResponseAfterToolCall = createAsyncThunk< |
| 38 | void, |
| 39 | { toolCallId: string }, |
| 40 | ThunkApiType |
| 41 | >( |
| 42 | "chat/streamAfterToolCall", |
| 43 | async ({ toolCallId }, { dispatch, getState }) => { |
| 44 | await dispatch( |
| 45 | streamThunkWrapper(async () => { |
| 46 | const state = getState(); |
| 47 | |
| 48 | const toolCallState = findToolCallById( |
| 49 | state.session.history, |
| 50 | toolCallId, |
| 51 | ); |
| 52 | |
| 53 | if (!toolCallState) { |
| 54 | return; // in cases where edit tool is cancelled mid apply, this will be triggered |
| 55 | } |
| 56 | |
| 57 | const toolOutput = toolCallState.output ?? []; |
| 58 | |
| 59 | dispatch(resetNextCodeBlockToApplyIndex()); |
| 60 | // await new Promise((resolve) => setTimeout(resolve, 0)); |
| 61 | |
| 62 | // Create and dispatch the tool message |
| 63 | const newMessage: ChatMessage = { |
| 64 | role: "tool", |
| 65 | content: renderContextItems(toolOutput), |
| 66 | toolCallId, |
| 67 | }; |
| 68 | dispatch(streamUpdate([newMessage])); |
| 69 | |
| 70 | // Check if we should continue streaming based on tool call completion |
| 71 | const history = getState().session.history; |
| 72 | const assistantMessage = history.findLast( |
| 73 | (item) => |
| 74 | item.message.role === "assistant" && |
| 75 | item.toolCallStates?.some((tc) => tc.toolCallId === toolCallId), |
| 76 | ); |
| 77 | |
| 78 | if ( |
| 79 | areAllToolsDoneStreaming( |
| 80 | assistantMessage, |
| 81 | state.config.config.ui?.continueAfterToolRejection, |
| 82 | ) |
| 83 | ) { |
| 84 | unwrapResult(await dispatch(streamNormalInput({}))); |
| 85 | } |
| 86 | }), |
| 87 | ); |
| 88 | }, |
| 89 | ); |