diff --git a/gui/src/hooks/useChatHandler.ts b/gui/src/hooks/useChatHandler.ts index 2d0281bd4..09b1f8333 100644 --- a/gui/src/hooks/useChatHandler.ts +++ b/gui/src/hooks/useChatHandler.ts @@ -27,7 +27,7 @@ import resolveEditorContent, { } from "../components/mainInput/resolveInput"; import { IIdeMessenger } from "../context/IdeMessenger"; import { defaultModelSelector } from "../redux/selectors/modelSelectors"; -import { selectLastToolCall } from "../redux/selectors/selectLastToolCall"; +import { selectCurrentToolCall } from "../redux/selectors/selectCurrentToolCall"; import { abortStream, acceptToolCall, @@ -70,7 +70,7 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { const toolSettings = useSelector( (store: RootState) => store.uiState.toolSettings, ); - const toolCallState = useSelector(selectLastToolCall); + const toolCallState = useSelector(selectCurrentToolCall); const activeRef = useRef(active); @@ -121,14 +121,18 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { } // If it's a tool call that is automatically accepted, we should call it + + await new Promise((resolve) => setTimeout(resolve, 0)); + if ( - toolSettings[toolCallState?.toolCall.function.name] === - "allowedWithoutPermission" + toolCallState && + toolCallState.status === "generated" && + toolSettings[toolCallState.toolCall.function.name] === + "allowedWithoutPermission" ) { await callTool(); } } catch (e) { - debugger; // If there's an error, we should clear the response so there aren't two input boxes dispatch(clearLastEmptyResponse()); } @@ -419,6 +423,9 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { await handleErrors(async () => { resetStateForNewMessage(); + dispatch(acceptToolCall()); + await new Promise((resolve) => setTimeout(resolve, 0)); + const newMessage: ChatMessage = { role: "tool", content: renderContextItems(toolOutput), @@ -438,10 +445,8 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { })), }), ); - dispatch(acceptToolCall()); activeRef.current = true; dispatch(setActive()); - await new Promise((resolve) => setTimeout(resolve, 0)); const messages = constructMessages( @@ -460,6 +465,10 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { async function callTool() { console.log("calling tool", toolCallState.toolCall); + if (!toolCallState) { + return; + } + // If it goes "generated" -> "calling" -> "done" really quickly // we don't want an abrupt flash so just skip "calling" let setCallingState = true; diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 26c68f04d..00901f1ae 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -35,7 +35,7 @@ import useHistory from "../../hooks/useHistory"; import { useTutorialCard } from "../../hooks/useTutorialCard"; import { useWebviewListener } from "../../hooks/useWebviewListener"; import { defaultModelSelector } from "../../redux/selectors/modelSelectors"; -import { selectLastToolCall } from "../../redux/selectors/selectLastToolCall"; +import { selectCurrentToolCall } from "../../redux/selectors/selectCurrentToolCall"; import { clearLastEmptyResponse, newSession, @@ -134,7 +134,7 @@ export function Chat() { useHistory(dispatch); const toolCallState = useSelector( - selectLastToolCall, + selectCurrentToolCall, ); const snapToBottom = useCallback(() => { diff --git a/gui/src/pages/gui/ToolCallDiv/ToolCallButtonsDiv.tsx b/gui/src/pages/gui/ToolCallDiv/ToolCallButtonsDiv.tsx index 068394086..8b371bf11 100644 --- a/gui/src/pages/gui/ToolCallDiv/ToolCallButtonsDiv.tsx +++ b/gui/src/pages/gui/ToolCallDiv/ToolCallButtonsDiv.tsx @@ -10,7 +10,7 @@ import { import Spinner from "../../../components/markdown/StepContainerPreToolbar/Spinner"; import { IdeMessengerContext } from "../../../context/IdeMessenger"; import useChatHandler from "../../../hooks/useChatHandler"; -import { selectLastToolCall } from "../../../redux/selectors/selectLastToolCall"; +import { selectCurrentToolCall } from "../../../redux/selectors/selectCurrentToolCall"; import { cancelToolCall } from "../../../redux/slices/stateSlice"; const ButtonContainer = styled.div` @@ -52,7 +52,7 @@ interface ToolCallButtonsProps {} export function ToolCallButtons(props: ToolCallButtonsProps) { const dispatch = useDispatch(); - const toolCallState = useSelector(selectLastToolCall); + const toolCallState = useSelector(selectCurrentToolCall); const ideMessenger = useContext(IdeMessengerContext); @@ -73,6 +73,10 @@ export function ToolCallButtons(props: ToolCallButtonsProps) { ]); } + if (!toolCallState) { + return null; + } + return ( <> diff --git a/gui/src/pages/gui/ToolCallDiv/index.tsx b/gui/src/pages/gui/ToolCallDiv/index.tsx index 656d2b248..b53bfa45a 100644 --- a/gui/src/pages/gui/ToolCallDiv/index.tsx +++ b/gui/src/pages/gui/ToolCallDiv/index.tsx @@ -31,8 +31,6 @@ export function ToolCallDiv(props: ToolCallDivProps) { }, [props.toolCall.function.arguments]); function getIcon(state: ToolStatus) { - console.log("State: ", state); - switch (state) { case "generating": return ; @@ -49,7 +47,6 @@ export function ToolCallDiv(props: ToolCallDivProps) { return ( - {props.status} ); diff --git a/gui/src/redux/selectors/selectLastToolCall.ts b/gui/src/redux/selectors/selectCurrentToolCall.ts similarity index 53% rename from gui/src/redux/selectors/selectLastToolCall.ts rename to gui/src/redux/selectors/selectCurrentToolCall.ts index 3625cf8de..cd0b47f50 100644 --- a/gui/src/redux/selectors/selectLastToolCall.ts +++ b/gui/src/redux/selectors/selectCurrentToolCall.ts @@ -1,10 +1,10 @@ import { createSelector } from "@reduxjs/toolkit"; import { RootState } from "../store"; -import { findLastToolCall } from "../util"; +import { findCurrentToolCall } from "../util"; -export const selectLastToolCall = createSelector( +export const selectCurrentToolCall = createSelector( (store: RootState) => store.state.history, (history) => { - return findLastToolCall(history); + return findCurrentToolCall(history); }, ); diff --git a/gui/src/redux/slices/stateSlice.ts b/gui/src/redux/slices/stateSlice.ts index cbed186c8..9aa05d02a 100644 --- a/gui/src/redux/slices/stateSlice.ts +++ b/gui/src/redux/slices/stateSlice.ts @@ -16,7 +16,7 @@ import { BrowserSerializedContinueConfig } from "core/config/load"; import { ConfigValidationError } from "core/config/validation"; import { renderChatMessage } from "core/util/messageContent"; import { v4 as uuidv4, v4 } from "uuid"; -import { findLastToolCall } from "../util"; +import { findCurrentToolCall } from "../util"; // We need this to handle reorderings (e.g. a mid-array deletion) of the messages array. // The proper fix is adding a UUID to all chat messages, but this is the temp workaround. @@ -457,26 +457,26 @@ export const stateSlice = createSlice({ // Related to currentToolCallState cancelToolCall: (state) => { - const toolCallState = findLastToolCall(state.history); + const toolCallState = findCurrentToolCall(state.history); if (!toolCallState) return; toolCallState.status = "canceled"; }, acceptToolCall: (state) => { - const toolCallState = findLastToolCall(state.history); + const toolCallState = findCurrentToolCall(state.history); if (!toolCallState) return; toolCallState.status = "done"; }, setGeneratedOutput: (state, { payload }: PayloadAction) => { - const toolCallState = findLastToolCall(state.history); + const toolCallState = findCurrentToolCall(state.history); if (!toolCallState) return; toolCallState.toolCall = payload; toolCallState.status = "generated"; }, setCalling: (state) => { - const toolCallState = findLastToolCall(state.history); + const toolCallState = findCurrentToolCall(state.history); if (!toolCallState) return; toolCallState.status = "calling"; diff --git a/gui/src/redux/util/index.ts b/gui/src/redux/util/index.ts index debf6b329..06ceebb4a 100644 --- a/gui/src/redux/util/index.ts +++ b/gui/src/redux/util/index.ts @@ -1,20 +1,8 @@ import { ToolCallState } from "core"; import { RootState } from "../store"; -function findLast( - array: T[], - predicate: (value: T, index: number, array: T[]) => boolean, -): T | undefined { - for (let i = array.length - 1; i >= 0; i--) { - if (predicate(array[i], i, array)) { - return array[i]; - } - } - return undefined; -} - -export function findLastToolCall( +export function findCurrentToolCall( state: RootState["state"]["history"], ): ToolCallState | undefined { - return findLast(state, (item) => !!item.toolCallState)?.toolCallState; + return state[state.length - 1]?.toolCallState; }