diff --git a/core/index.d.ts b/core/index.d.ts index 51fc65048..094d5b69f 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -432,12 +432,12 @@ export interface PromptLog { export type MessageModes = "chat" | "agent" | "plan"; export type ToolStatus = - | "generating" - | "generated" - | "calling" - | "errored" - | "done" - | "canceled"; + | "generating" // Tool call arguments are being streamed from the LLM + | "generated" // Tool call is complete and ready for execution (awaiting approval) + | "calling" // Tool is actively being executed + | "errored" // Tool execution failed with an error + | "done" // Tool execution completed successfully + | "canceled"; // Tool call was canceled by user or system // Will exist only on "assistant" messages with tool calls interface ToolCallState { diff --git a/gui/src/components/AcceptRejectDiffButtons.tsx b/gui/src/components/AcceptRejectDiffButtons.tsx index d38be0792..621fbeead 100644 --- a/gui/src/components/AcceptRejectDiffButtons.tsx +++ b/gui/src/components/AcceptRejectDiffButtons.tsx @@ -2,8 +2,7 @@ import { CheckIcon, XMarkIcon } from "@heroicons/react/24/outline"; import { ApplyState } from "core"; import { useContext } from "react"; import { IdeMessengerContext } from "../context/IdeMessenger"; -import { useAppDispatch, useAppSelector } from "../redux/hooks"; -import { selectCurrentToolCallApplyState } from "../redux/selectors/selectToolCalls"; +import { useAppDispatch } from "../redux/hooks"; import { cancelToolCall } from "../redux/slices/sessionSlice"; import { getMetaKeyLabel } from "../util"; import { ToolTip } from "./gui/Tooltip"; @@ -23,21 +22,22 @@ export default function AcceptRejectAllButtons({ (state) => state.status === "done", ); const ideMessenger = useContext(IdeMessengerContext); - const currentToolCallApplyState = useAppSelector( - selectCurrentToolCallApplyState, - ); const dispatch = useAppDispatch(); async function handleAcceptOrReject(status: AcceptOrRejectOutcome) { - if ( - status === "rejectDiff" && - currentToolCallApplyState?.status === "done" - ) { - dispatch( - cancelToolCall({ - toolCallId: currentToolCallApplyState.toolCallId!, - }), - ); + // For reject operations, cancel all tool calls associated with pending apply states + if (status === "rejectDiff") { + for (const applyState of pendingApplyStates) { + if (applyState.toolCallId && applyState.status === "done") { + dispatch( + cancelToolCall({ + toolCallId: applyState.toolCallId, + }), + ); + } + } } + + // Process all pending apply states for (const { filepath = "", streamId } of pendingApplyStates) { ideMessenger.post(status, { filepath, diff --git a/gui/src/components/mainInput/Lump/LumpToolbar/GeneratingIndicator.tsx b/gui/src/components/mainInput/Lump/LumpToolbar/GeneratingIndicator.tsx index 8165fc482..e6d38d85a 100644 --- a/gui/src/components/mainInput/Lump/LumpToolbar/GeneratingIndicator.tsx +++ b/gui/src/components/mainInput/Lump/LumpToolbar/GeneratingIndicator.tsx @@ -8,11 +8,8 @@ export function GeneratingIndicator({ testId?: string; }) { return ( -
- {text} +
+ {text}
); diff --git a/gui/src/components/mainInput/Lump/LumpToolbar/LumpToolbar.tsx b/gui/src/components/mainInput/Lump/LumpToolbar/LumpToolbar.tsx index 21d74f02c..6b68d40af 100644 --- a/gui/src/components/mainInput/Lump/LumpToolbar/LumpToolbar.tsx +++ b/gui/src/components/mainInput/Lump/LumpToolbar/LumpToolbar.tsx @@ -3,8 +3,8 @@ import { useSelector } from "react-redux"; import { IdeMessengerContext } from "../../../../context/IdeMessenger"; import { useAppDispatch, useAppSelector } from "../../../../redux/hooks"; import { - selectCurrentToolCall, selectFirstPendingToolCall, + selectPendingToolCalls, } from "../../../../redux/selectors/selectToolCalls"; import { cancelToolCall } from "../../../../redux/slices/sessionSlice"; import { callToolById } from "../../../../redux/thunks/callToolById"; @@ -42,7 +42,7 @@ export function LumpToolbar() { const isStreaming = useAppSelector((state) => state.session.isStreaming); const isInEdit = useAppSelector((state) => state.session.isInEdit); const jetbrains = isJetBrains(); - const toolCallState = useSelector(selectCurrentToolCall); + const pendingToolCalls = useAppSelector(selectPendingToolCalls); const firstPendingToolCall = useAppSelector(selectFirstPendingToolCall); const editApplyState = useAppSelector( (state) => state.editModeState.applyState, @@ -107,7 +107,7 @@ export function LumpToolbar() { return ; } - if (toolCallState?.status === "generated") { + if (pendingToolCalls.length > 0) { return ; } diff --git a/gui/src/hooks/ParallelListeners.tsx b/gui/src/hooks/ParallelListeners.tsx index 3a67a499d..b6d707876 100644 --- a/gui/src/hooks/ParallelListeners.tsx +++ b/gui/src/hooks/ParallelListeners.tsx @@ -4,10 +4,6 @@ import { IdeMessengerContext } from "../context/IdeMessenger"; import { EDIT_MODE_STREAM_ID } from "core/edit/constants"; import { FromCoreProtocol } from "core/protocol"; import { useAppDispatch, useAppSelector } from "../redux/hooks"; -import { - selectCurrentToolCall, - selectCurrentToolCallApplyState, -} from "../redux/selectors/selectToolCalls"; import { setConfigLoading, setConfigResult } from "../redux/slices/configSlice"; import { setLastNonEditSessionEmpty, @@ -55,13 +51,9 @@ function ParallelListeners() { (store) => store.profiles.selectedProfileId, ); const hasDoneInitialConfigLoad = useRef(false); - const currentToolCallApplyState = useAppSelector( - selectCurrentToolCallApplyState, - ); const autoAcceptEditToolDiffs = useAppSelector( (store) => store.config.config.ui?.autoAcceptEditToolDiffs, ); - const currentToolCall = useAppSelector(selectCurrentToolCall); // Load symbols for chat on any session change const sessionId = useAppSelector((state) => state.session.id); @@ -274,11 +266,8 @@ function ParallelListeners() { // chat or agent dispatch(updateApplyState(state)); - // Handle apply status updates that are associated with current tool call - if ( - currentToolCallApplyState && - currentToolCallApplyState.streamId === state.streamId - ) { + // Handle apply status updates - use toolCallId from event payload + if (state.toolCallId) { if (state.status === "done" && autoAcceptEditToolDiffs) { ideMessenger.post("acceptDiff", { streamId: state.streamId, @@ -286,15 +275,20 @@ function ParallelListeners() { }); } if (state.status === "closed") { - if (currentToolCall?.status !== "canceled") { + // Find the tool call to check if it was canceled + const toolCallState = findToolCallById( + store.getState().session.history, + state.toolCallId, + ); + if (toolCallState && toolCallState.status !== "canceled") { dispatch( acceptToolCall({ - toolCallId: currentToolCallApplyState.toolCallId!, + toolCallId: state.toolCallId, }), ); void dispatch( streamResponseAfterToolCall({ - toolCallId: currentToolCallApplyState.toolCallId!, + toolCallId: state.toolCallId, }), ); } @@ -308,12 +302,7 @@ function ParallelListeners() { } } }, - [ - currentToolCall, - currentToolCallApplyState, - autoAcceptEditToolDiffs, - ideMessenger, - ], + [autoAcceptEditToolDiffs, ideMessenger], ); useEffect(() => { diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 98fe87dd4..d0803b4cd 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -28,8 +28,7 @@ import { IdeMessengerContext } from "../../context/IdeMessenger"; import { useWebviewListener } from "../../hooks/useWebviewListener"; import { useAppDispatch, useAppSelector } from "../../redux/hooks"; import { - selectCurrentToolCall, - selectCurrentToolCallApplyState, + selectPendingToolCalls, } from "../../redux/selectors/selectToolCalls"; import { cancelToolCall, @@ -141,9 +140,11 @@ export function Chat() { isStreaming, ); - const currentToolCallState = useAppSelector(selectCurrentToolCall); - const currentToolCallApplyState = useAppSelector( - selectCurrentToolCallApplyState, + const pendingToolCalls = useAppSelector(selectPendingToolCalls); + const pendingApplyStates = useAppSelector( + (state) => state.session.codeBlockApplyStates.states.filter( + (applyState) => applyState.status === "done" + ) ); const sendInput = useCallback( @@ -153,19 +154,21 @@ export function Chat() { index?: number, editorToClearOnSend?: Editor, ) => { - if (currentToolCallState) { + // Cancel all pending tool calls + pendingToolCalls.forEach((toolCallState) => { dispatch( cancelToolCall({ - toolCallId: currentToolCallState.toolCallId, + toolCallId: toolCallState.toolCallId, }), ); - } - if ( - currentToolCallApplyState && - currentToolCallApplyState.status !== "closed" - ) { - ideMessenger.post("rejectDiff", currentToolCallApplyState); - } + }); + + // Reject all pending apply states + pendingApplyStates.forEach((applyState) => { + if (applyState.status !== "closed") { + ideMessenger.post("rejectDiff", applyState); + } + }); const model = isInEdit ? (selectedModels?.edit ?? selectedModels?.chat) : selectedModels?.chat; @@ -222,8 +225,8 @@ export function Chat() { mode, isInEdit, codeToEdit, - currentToolCallState, - currentToolCallApplyState, + pendingToolCalls, + pendingApplyStates, ], ); diff --git a/gui/src/redux/selectors/selectToolCalls.ts b/gui/src/redux/selectors/selectToolCalls.ts index 295829321..a47efd9fc 100644 --- a/gui/src/redux/selectors/selectToolCalls.ts +++ b/gui/src/redux/selectors/selectToolCalls.ts @@ -4,6 +4,7 @@ import { RootState } from "../store"; import { findAllCurToolCalls, findAllCurToolCallsByStatus, + findToolCallById, hasCurrentToolCalls, } from "../util"; @@ -26,12 +27,6 @@ export const selectToolCallsByStatus = createSelector( (history, status) => findAllCurToolCallsByStatus(history, status), ); -// Convenience selectors for single tool calls -export const selectCurrentToolCall = createSelector( - selectCurrentToolCalls, - (toolCalls) => toolCalls[0] || undefined, -); - export const selectFirstPendingToolCall = createSelector( (store: RootState) => store.session.history, (history) => { @@ -40,20 +35,22 @@ export const selectFirstPendingToolCall = createSelector( }, ); -// Apply state selectors -export const selectCurrentToolCallApplyState = createSelector( +// ID-based selectors for specific tool calls +export const selectToolCallById = createSelector( [ - selectCurrentToolCalls, - (store: RootState) => store.session.codeBlockApplyStates, + (store: RootState) => store.session.history, + (_store: RootState, toolCallId: string) => toolCallId, ], - (toolCalls, applyStates) => { - if (toolCalls.length === 0) { - return undefined; - } - const firstToolCall = toolCalls[0]; - return applyStates.states.find( - (state) => state.toolCallId === firstToolCall.toolCallId, - ); + (history, toolCallId) => findToolCallById(history, toolCallId), +); + +export const selectApplyStateByToolCallId = createSelector( + [ + (store: RootState) => store.session.codeBlockApplyStates, + (_store: RootState, toolCallId: string) => toolCallId, + ], + (applyStates, toolCallId) => { + return applyStates.states.find((state) => state.toolCallId === toolCallId); }, ); diff --git a/gui/src/redux/thunks/streamNormalInput.ts b/gui/src/redux/thunks/streamNormalInput.ts index 185d2f555..0c1992a48 100644 --- a/gui/src/redux/thunks/streamNormalInput.ts +++ b/gui/src/redux/thunks/streamNormalInput.ts @@ -35,10 +35,10 @@ async function handleToolCallExecution( const newState = getState(); const toolSettings = newState.ui.toolSettings; const allToolCallStates = selectCurrentToolCalls(newState); - + // Only process tool calls that are in "generating" status (newly created during this streaming session) const toolCallStates = allToolCallStates.filter( - (toolCallState) => toolCallState.status === "generating" + (toolCallState) => toolCallState.status === "generating", ); // If no generating tool calls, nothing to process