diff --git a/core/index.d.ts b/core/index.d.ts index a4f76ead0..46454c603 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -403,6 +403,7 @@ interface ToolCallState { toolCallId: string; toolCall: ToolCall; status: ToolStatus; + parsedArgs: any; } export interface ChatHistoryItem { diff --git a/core/llm/constructMessages.ts b/core/llm/constructMessages.ts index 3af1b1a4f..b9e196b02 100644 --- a/core/llm/constructMessages.ts +++ b/core/llm/constructMessages.ts @@ -74,6 +74,9 @@ function constructSystemPrompt(model: string): string | null { return null; } +const CANCELED_TOOL_CALL_MESSAGE = + "This tool call was cancelled by the user. You should clarify next steps, as they don't wish for you to use this tool."; + export function constructMessages( history: ChatHistory, model: string, @@ -104,6 +107,12 @@ export function constructMessages( ...historyItem.message, content, }); + } else if (historyItem.toolCallState?.status === "canceled") { + // Canceled tool call + msgs.push({ + ...historyItem.message, + content: CANCELED_TOOL_CALL_MESSAGE, + }); } else { msgs.push(historyItem.message); } diff --git a/gui/src/components/StepContainer/ResponseActions.tsx b/gui/src/components/StepContainer/ResponseActions.tsx index 3769907ef..af8623939 100644 --- a/gui/src/components/StepContainer/ResponseActions.tsx +++ b/gui/src/components/StepContainer/ResponseActions.tsx @@ -37,7 +37,7 @@ export default function ResponseActions({ } return ( -
+
{shouldHideActions || ( <> {isTruncated && ( diff --git a/gui/src/components/StepContainer/StepContainer.tsx b/gui/src/components/StepContainer/StepContainer.tsx index 8c28d5ddd..4ee912901 100644 --- a/gui/src/components/StepContainer/StepContainer.tsx +++ b/gui/src/components/StepContainer/StepContainer.tsx @@ -35,8 +35,16 @@ export default function StepContainer(props: StepContainerProps) { const isInEditMode = useSelector( (store: RootState) => store.editModeState.isInEditMode, ); + + const historyItemAfterThis = useSelector( + (store: RootState) => store.state.history[props.index + 1], + ); + const uiConfig = useUIConfig(); - const shouldHideActions = active && props.isLast; + const shouldHideActions = + (active && props.isLast) || + historyItemAfterThis?.message.role === "assistant"; + // const isStepAheadOfCurCheckpoint = // isInEditMode && Math.floor(props.index / 2) > curCheckpointIndex; @@ -96,15 +104,17 @@ export default function StepContainer(props: StepContainerProps) { )} {/* We want to occupy space in the DOM regardless of whether the actions are visible to avoid jank on */} -
- +
+ {shouldHideActions ? null : ( + + )}
); diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index 640f80a1d..3d794d4ce 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -1,4 +1,7 @@ +import { ContextItemWithId, SymbolWithRange } from "core"; +import { ctxItemToRifWithContents } from "core/commands/util"; import { memo, useEffect, useRef } from "react"; +import { useSelector } from "react-redux"; import { useRemark } from "react-remark"; import rehypeHighlight, { Options } from "rehype-highlight"; import rehypeKatex from "rehype-katex"; @@ -11,19 +14,16 @@ import { vscEditorBackground, vscForeground, } from ".."; +import { RootState } from "../../redux/store"; import { getFontSize, isJetBrains } from "../../util"; import FilenameLink from "./FilenameLink"; import "./katex.css"; import "./markdown.css"; import StepContainerPreActionButtons from "./StepContainerPreActionButtons"; import StepContainerPreToolbar from "./StepContainerPreToolbar"; +import SymbolLink from "./SymbolLink"; import { SyntaxHighlightedPre } from "./SyntaxHighlightedPre"; import { patchNestedMarkdown } from "./utils/patchNestedMarkdown"; -import { RootState } from "../../redux/store"; -import { ContextItemWithId, SymbolWithRange } from "core"; -import SymbolLink from "./SymbolLink"; -import { useSelector } from "react-redux"; -import { ctxItemToRifWithContents } from "core/commands/util"; const StyledMarkdown = styled.div<{ fontSize?: number; diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 208cdf4ec..cbef496e2 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -360,8 +360,8 @@ export function Chat() { {item.message.toolCalls?.map((toolCall) => { return ( ); })} diff --git a/gui/src/pages/gui/ToolCallDiv/CreateFile.tsx b/gui/src/pages/gui/ToolCallDiv/CreateFile.tsx index 260266c5f..59eec0c97 100644 --- a/gui/src/pages/gui/ToolCallDiv/CreateFile.tsx +++ b/gui/src/pages/gui/ToolCallDiv/CreateFile.tsx @@ -1,16 +1,17 @@ +import { getMarkdownLanguageTagForFile } from "core/util"; +import StyledMarkdownPreview from "../../../components/markdown/StyledMarkdownPreview"; + interface CreateFileToolCallProps { filepath: string; fileContents: string; } export function CreateFile(props: CreateFileToolCallProps) { + const src = `\`\`\`${getMarkdownLanguageTagForFile(props.filepath ?? "javascript")} ${props.filepath}\n${props.fileContents ?? ""}\n\`\`\``; + return ( <> - {props.fileContents} - {/* */} + ); } diff --git a/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx b/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx index db72817c9..68fdeb682 100644 --- a/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx +++ b/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx @@ -1,10 +1,15 @@ -import { ToolCall } from "core"; -import { incrementalParseJson } from "core/util/incrementalParseJson"; +import { ToolCall, ToolCallState } from "core"; import { CreateFile } from "./CreateFile"; import { RunTerminalCommand } from "./RunTerminalCommand"; -function FunctionSpecificToolCallDiv({ toolCall }: { toolCall: ToolCall }) { - const [_, args] = incrementalParseJson(toolCall.function.arguments); +function FunctionSpecificToolCallDiv({ + toolCall, + toolCallState, +}: { + toolCall: ToolCall; + toolCallState: ToolCallState; +}) { + const args = toolCallState.parsedArgs; switch (toolCall.function.name) { case "create_new_file": diff --git a/gui/src/pages/gui/ToolCallDiv/ToolCallButtonsDiv.tsx b/gui/src/pages/gui/ToolCallDiv/ToolCallButtonsDiv.tsx index d447b7fef..2b6a7fc4f 100644 --- a/gui/src/pages/gui/ToolCallDiv/ToolCallButtonsDiv.tsx +++ b/gui/src/pages/gui/ToolCallDiv/ToolCallButtonsDiv.tsx @@ -1,4 +1,3 @@ -import { useContext } from "react"; import { useSelector } from "react-redux"; import styled from "styled-components"; import { @@ -8,7 +7,6 @@ import { vscButtonForeground, } from "../../../components"; import Spinner from "../../../components/markdown/StepContainerPreToolbar/Spinner"; -import { IdeMessengerContext } from "../../../context/IdeMessenger"; import { selectCurrentToolCall } from "../../../redux/selectors/selectCurrentToolCall"; import { cancelToolCall } from "../../../redux/slices/stateSlice"; import { useAppDispatch } from "../../../redux/store"; @@ -56,21 +54,12 @@ export function ToolCallButtons(props: ToolCallButtonsProps) { const dispatch = useAppDispatch(); const toolCallState = useSelector(selectCurrentToolCall); - const ideMessenger = useContext(IdeMessengerContext); - async function cancelTool() { dispatch(cancelToolCall()); dispatch( streamResponseAfterToolCall({ toolCallId: toolCallState.toolCallId, - toolOutput: [ - { - name: "Cancelled", - description: "Cancelled", - content: - "This tool call was cancelled by the user. You should try something else, likely just chatting instead of using another tool.", - }, - ], + toolOutput: [], }), ); } diff --git a/gui/src/pages/gui/ToolCallDiv/index.tsx b/gui/src/pages/gui/ToolCallDiv/index.tsx index b53bfa45a..4d6838f26 100644 --- a/gui/src/pages/gui/ToolCallDiv/index.tsx +++ b/gui/src/pages/gui/ToolCallDiv/index.tsx @@ -1,53 +1,37 @@ -import { CheckIcon, PlayIcon, XMarkIcon } from "@heroicons/react/24/outline"; -import { ToolCall, ToolStatus } from "core"; -import { incrementalParseJson } from "core/util/incrementalParseJson"; -import { useEffect } from "react"; -import { useDispatch } from "react-redux"; -import { lightGray } from "../../../components"; +import { CheckIcon, XMarkIcon } from "@heroicons/react/24/outline"; +import { ToolCall, ToolCallState, ToolStatus } from "core"; import Spinner from "../../../components/markdown/StepContainerPreToolbar/Spinner"; -import { setGeneratedOutput } from "../../../redux/slices/stateSlice"; import FunctionSpecificToolCallDiv from "./FunctionSpecificToolCallDiv"; import { ThreadDiv } from "./ThreadDiv"; interface ToolCallDivProps { toolCall: ToolCall; - status: ToolStatus; + toolCallState: ToolCallState; } export function ToolCallDiv(props: ToolCallDivProps) { - const dispatch = useDispatch(); - - useEffect(() => { - // Once the JSON can successfully parse, then set state to "generated" - if (props.toolCall.function.arguments.length === 0) { - dispatch(setGeneratedOutput(props.toolCall)); - return; - } - - const [done, j] = incrementalParseJson(props.toolCall.function.arguments); - if (done) { - dispatch(setGeneratedOutput(props.toolCall)); - } - }, [props.toolCall.function.arguments]); - function getIcon(state: ToolStatus) { switch (state) { case "generating": + case "calling": + case "generated": return ; case "done": return ; - case "calling": - return ; case "canceled": return ; - case "generated": - return ; } } return ( - - + + ); } diff --git a/gui/src/redux/slices/stateSlice.ts b/gui/src/redux/slices/stateSlice.ts index e8dcfdb63..d0f3e0790 100644 --- a/gui/src/redux/slices/stateSlice.ts +++ b/gui/src/redux/slices/stateSlice.ts @@ -19,6 +19,7 @@ import { } from "core"; import { BrowserSerializedContinueConfig } from "core/config/load"; import { ConfigValidationError } from "core/config/validation"; +import { incrementalParseJson } from "core/util/incrementalParseJson"; import { renderChatMessage } from "core/util/messageContent"; import { v4 as uuidv4, v4 } from "uuid"; import { streamResponseThunk } from "../thunks/streamResponse"; @@ -319,10 +320,14 @@ export const stateSlice = createSlice({ }; if (action.payload.role === "assistant" && action.payload.toolCalls) { + const [_, parsedArgs] = incrementalParseJson( + action.payload.toolCalls[0].function.arguments, + ); historyItem.toolCallState = { status: "generating", toolCall: action.payload.toolCalls[0] as ToolCall, toolCallId: action.payload.toolCalls[0].id, + parsedArgs, }; } @@ -346,6 +351,14 @@ export const stateSlice = createSlice({ } else { msg.toolCalls[i].function.arguments += toolCall.function.arguments; + + const [_, parsedArgs] = incrementalParseJson( + msg.toolCalls[i].function.arguments, + ); + + state.history[ + state.history.length - 1 + ].toolCallState.parsedArgs = parsedArgs; } }); } @@ -462,6 +475,12 @@ export const stateSlice = createSlice({ }, // Related to currentToolCallState + setToolGenerated: (state) => { + const toolCallState = findCurrentToolCall(state.history); + if (!toolCallState) return; + + toolCallState.status = "generated"; + }, cancelToolCall: (state) => { const toolCallState = findCurrentToolCall(state.history); if (!toolCallState) return; @@ -474,13 +493,6 @@ export const stateSlice = createSlice({ toolCallState.status = "done"; }, - setGeneratedOutput: (state, { payload }: PayloadAction) => { - const toolCallState = findCurrentToolCall(state.history); - if (!toolCallState) return; - - toolCallState.toolCall = payload; - toolCallState.status = "generated"; - }, setCalling: (state) => { const toolCallState = findCurrentToolCall(state.history); if (!toolCallState) return; @@ -572,8 +584,8 @@ export const { setIndexingChatPeekHidden, setCalling, cancelToolCall, - setGeneratedOutput, acceptToolCall, + setToolGenerated, } = stateSlice.actions; export default stateSlice.reducer; diff --git a/gui/src/redux/thunks/callTool.ts b/gui/src/redux/thunks/callTool.ts index 936b57cf4..f326423aa 100644 --- a/gui/src/redux/thunks/callTool.ts +++ b/gui/src/redux/thunks/callTool.ts @@ -1,6 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; import { selectCurrentToolCall } from "../selectors/selectCurrentToolCall"; -import { setCalling } from "../slices/stateSlice"; import { ThunkApiType } from "../store"; import { streamResponseAfterToolCall } from "./streamResponseAfterToolCall"; @@ -15,16 +14,6 @@ export const callTool = createAsyncThunk( return; } - // If it goes "generated" -> "calling" -> "done" really quickly - // we don't want an abrupt flash so just skip "calling" - let setCallingState = true; - - const timer = setTimeout(() => { - if (setCallingState) { - dispatch(setCalling()); - } - }, 800); - if (toolCallState.status !== "generated") { return; } @@ -33,9 +22,6 @@ export const callTool = createAsyncThunk( toolCall: toolCallState.toolCall, }); - setCallingState = false; - clearTimeout(timer); - if (result.status === "success") { const contextItems = result.content.contextItems; // Send to the LLM to continue the conversation diff --git a/gui/src/redux/thunks/streamNormalInput.ts b/gui/src/redux/thunks/streamNormalInput.ts index e793fe96b..2bb5a160f 100644 --- a/gui/src/redux/thunks/streamNormalInput.ts +++ b/gui/src/redux/thunks/streamNormalInput.ts @@ -7,6 +7,7 @@ import { abortStream, addPromptCompletionPair, clearLastEmptyResponse, + setToolGenerated, streamUpdate, } from "../slices/stateSlice"; import { ThunkApiType } from "../store"; @@ -64,13 +65,15 @@ export const streamNormalInput = createAsyncThunk< // If it's a tool call that is automatically accepted, we should call it const toolCallState = selectCurrentToolCall(getState()); - if ( - toolCallState && - toolCallState.status === "generated" && - toolSettings[toolCallState.toolCall.function.name] === + if (toolCallState) { + dispatch(setToolGenerated()); + + if ( + toolSettings[toolCallState.toolCall.function.name] === "allowedWithoutPermission" - ) { - await dispatch(callTool()); + ) { + await dispatch(callTool()); + } } } catch (e) { // If there's an error, we should clear the response so there aren't two input boxes diff --git a/gui/src/redux/thunks/streamResponseAfterToolCall.ts b/gui/src/redux/thunks/streamResponseAfterToolCall.ts index a14e97d68..97f7c15d9 100644 --- a/gui/src/redux/thunks/streamResponseAfterToolCall.ts +++ b/gui/src/redux/thunks/streamResponseAfterToolCall.ts @@ -59,13 +59,7 @@ export const streamResponseAfterToolCall = createAsyncThunk< const updatedHistory = getState().state.history; const messages = constructMessages( - [ - ...updatedHistory, - { - message: newMessage, - contextItems: [], - }, - ], + [...updatedHistory], defaultModel.model, ); await dispatch(streamNormalInput(messages));