diff --git a/core/core.ts b/core/core.ts index b1393f8cd..11fecffb2 100644 --- a/core/core.ts +++ b/core/core.ts @@ -871,8 +871,7 @@ export class Core { } }); - on("tools/call", async (msg) => { - const { toolCall, selectedModelTitle } = msg.data; + on("tools/call", async ({ data: { toolCall, selectedModelTitle } }) => { const { config } = await this.configHandler.loadConfig(); if (!config) { throw new Error("Config not loaded"); diff --git a/core/index.d.ts b/core/index.d.ts index ba97377d3..98f724fda 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -1141,6 +1141,7 @@ export interface ApplyState { numDiffs?: number; filepath?: string; fileContent?: string; + toolCallId?: string; } export interface RangeInFileWithContents { diff --git a/core/protocol/ideWebview.ts b/core/protocol/ideWebview.ts index a7cd16e5b..6aae337f0 100644 --- a/core/protocol/ideWebview.ts +++ b/core/protocol/ideWebview.ts @@ -4,7 +4,6 @@ import { ToWebviewFromIdeOrCoreProtocol } from "./webview"; import type { ApplyState, CodeToEdit, - ContextSubmenuItem, EditStatus, MessageContent, RangeInFileWithContents, @@ -20,6 +19,7 @@ export type ToIdeFromWebviewProtocol = ToIdeFromWebviewOrCoreProtocol & { streamId: string; curSelectedModelTitle: string; filepath?: string; + toolCallId?: string; }, void, ]; diff --git a/core/tools/definitions/exactSearch.ts b/core/tools/definitions/exactSearch.ts index d20db3cec..7f6e03d6b 100644 --- a/core/tools/definitions/exactSearch.ts +++ b/core/tools/definitions/exactSearch.ts @@ -6,7 +6,7 @@ export const exactSearchTool: Tool = { displayTitle: "Exact Search", wouldLikeTo: 'search for "{{{ query }}}" in the repository', isCurrently: 'getting search results for "{{{ query }}}"', - hasAlready: 'retreived search results for "{{{ query }}}"', + hasAlready: 'retrieved search results for "{{{ query }}}"', readonly: true, group: BUILT_IN_GROUP_NAME, function: { diff --git a/core/tools/implementations/editFile.ts b/core/tools/implementations/editFile.ts deleted file mode 100644 index cb829af6a..000000000 --- a/core/tools/implementations/editFile.ts +++ /dev/null @@ -1 +0,0 @@ -// Edit has custom handling on the GUI side diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/protocol/ideWebview.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/protocol/ideWebview.kt index 07988ad0e..5c587c54d 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/protocol/ideWebview.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/protocol/ideWebview.kt @@ -12,7 +12,8 @@ data class ApplyToFileParams( val text: String, val streamId: String, val curSelectedModelTitle: String, - val filepath: String? + val filepath: String?, + val toolCallId: String? ) data class InsertAtCursorParams( diff --git a/extensions/vscode/src/commands.ts b/extensions/vscode/src/commands.ts index 836360e51..32d240d5f 100644 --- a/extensions/vscode/src/commands.ts +++ b/extensions/vscode/src/commands.ts @@ -247,6 +247,7 @@ async function processDiff( verticalDiffManager: VerticalDiffManager, newFileUri?: string, streamId?: string, + toolCallId?: string, ) { captureCommandTelemetry(`${action}Diff`); @@ -280,6 +281,7 @@ async function processDiff( streamId, status: "closed", numDiffs: 0, + toolCallId, }); } diff --git a/extensions/vscode/src/diff/vertical/manager.ts b/extensions/vscode/src/diff/vertical/manager.ts index 34c3345ac..4d4256a1f 100644 --- a/extensions/vscode/src/diff/vertical/manager.ts +++ b/extensions/vscode/src/diff/vertical/manager.ts @@ -190,6 +190,7 @@ export class VerticalDiffManager { diffStream: AsyncGenerator, instant: boolean, streamId: string, + toolCallId?: string, ) { vscode.commands.executeCommand("setContext", "continue.diffVisible", true); @@ -226,6 +227,7 @@ export class VerticalDiffManager { numDiffs, fileContent, filepath: fileUri, + toolCallId, }), }, ); @@ -276,6 +278,7 @@ export class VerticalDiffManager { quickEdit?: string, range?: vscode.Range, newCode?: string, + toolCallId?: string, ): Promise { vscode.commands.executeCommand("setContext", "continue.diffVisible", true); @@ -351,6 +354,7 @@ export class VerticalDiffManager { numDiffs, fileContent, filepath: fileUri, + toolCallId, }), }, ); diff --git a/extensions/vscode/src/extension/VsCodeMessenger.ts b/extensions/vscode/src/extension/VsCodeMessenger.ts index 23669305a..98fe8489b 100644 --- a/extensions/vscode/src/extension/VsCodeMessenger.ts +++ b/extensions/vscode/src/extension/VsCodeMessenger.ts @@ -18,6 +18,7 @@ import { stripImages } from "core/util/messageContent"; import { getUriPathBasename } from "core/util/uri"; import * as vscode from "vscode"; +import { ILLM } from "core"; import { VerticalDiffManager } from "../diff/vertical/manager"; import EditDecorationManager from "../quickEdit/EditDecorationManager"; import { @@ -28,7 +29,6 @@ import { showTutorial } from "../util/tutorial"; import { getExtensionUri } from "../util/vscode"; import { VsCodeIde } from "../VsCodeIde"; import { VsCodeWebviewProtocol } from "../webviewProtocol"; -import { ILLM } from "core"; /** * A shared messenger class between Core and Webview @@ -133,6 +133,7 @@ export class VsCodeMessenger { streamId: data.streamId, status: "streaming", fileContent: data.text, + toolCallId: data.toolCallId, }); if (data.filepath) { @@ -164,6 +165,7 @@ export class VsCodeMessenger { status: "closed", numDiffs: 0, fileContent: data.text, + toolCallId: data.toolCallId, }); return; @@ -232,6 +234,7 @@ export class VsCodeMessenger { undefined, rangeToApplyTo, data.text, + data.toolCallId, ); } }); diff --git a/gui/src/components/mainInput/InputToolbar.tsx b/gui/src/components/mainInput/InputToolbar.tsx index 3d0a38cec..27c034721 100644 --- a/gui/src/components/mainInput/InputToolbar.tsx +++ b/gui/src/components/mainInput/InputToolbar.tsx @@ -98,11 +98,16 @@ function InputToolbar(props: InputToolbarProps) { const hasCodeToEdit = useAppSelector(selectHasCodeToEdit); const toolCallState = useAppSelector(selectCurrentToolCall); const isEditModeAndNoCodeToEdit = isInEditMode && !hasCodeToEdit; + const activeToolCallStreamId = useAppSelector( + (store) => store.session.activeToolStreamId, + ); const isEnterDisabled = props.disabled || isEditModeAndNoCodeToEdit || - toolCallState?.status === "generated"; + toolCallState?.status === "generated" || + activeToolCallStreamId; + const toolsSupported = defaultModel && modelSupportsTools(defaultModel); const supportsImages = diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index b473bb6f0..52ede7cbd 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -131,7 +131,7 @@ interface StyledMarkdownPreviewProps { itemIndex?: number; useParentBackgroundColor?: boolean; disableManualApply?: boolean; - firstCodeblockStreamId?: string; + singleCodeblockStreamId?: string; } const HLJS_LANGUAGE_CLASSNAME_PREFIX = "language-"; @@ -199,15 +199,18 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( const pastFileInfoRef = useUpdatingRef(pastFileInfo); const codeblockState = useRef<{ streamId: string; isGenerating: boolean }[]>( - props.firstCodeblockStreamId - ? [ - { - streamId: props.firstCodeblockStreamId, - isGenerating: false, - }, - ] - : [], + [], ); + useEffect(() => { + if (props.singleCodeblockStreamId) { + codeblockState.current = [ + { + streamId: props.singleCodeblockStreamId, + isGenerating: false, + }, + ]; + } + }, [props.singleCodeblockStreamId]); const [reactContent, setMarkdownSource] = useRemark({ remarkPlugins: [ diff --git a/gui/src/hooks/useSetup.ts b/gui/src/hooks/useSetup.ts index f1909afc5..914b96089 100644 --- a/gui/src/hooks/useSetup.ts +++ b/gui/src/hooks/useSetup.ts @@ -1,9 +1,9 @@ -import { useCallback, useContext, useEffect, useRef } from "react"; +import { useCallback, useContext, useEffect, useMemo, useRef } from "react"; import { VSC_THEME_COLOR_VARS } from "../components"; import { IdeMessengerContext } from "../context/IdeMessenger"; import { ConfigResult } from "@continuedev/config-yaml"; -import { BrowserSerializedContinueConfig } from "core"; +import { BrowserSerializedContinueConfig, ContextItem } from "core"; import { initializeProfilePreferencesThunk, selectProfileThunk, @@ -16,11 +16,13 @@ import { } from "../redux/slices/configSlice"; import { updateIndexingStatus } from "../redux/slices/indexingSlice"; import { + acceptToolCall, addContextItemsAtIndex, - selectLastEditToolApplyState, setInactive, + setToolCallOutput, } from "../redux/slices/sessionSlice"; import { setTTSActive } from "../redux/slices/uiSlice"; +import { streamResponseAfterToolCall } from "../redux/thunks"; import { refreshSessionMetadata } from "../redux/thunks/session"; import { streamResponseThunk } from "../redux/thunks/streamResponse"; import { updateFileSymbolsFromHistory } from "../redux/thunks/updateFileSymbols"; @@ -247,12 +249,38 @@ function useSetup() { [defaultModel], ); - const lastEditToolApplyState = useAppSelector(selectLastEditToolApplyState); + const activeToolStreamId = useAppSelector( + (store) => store.session.activeToolStreamId, + ); + const applyStates = useAppSelector( + (store) => store.session.codeBlockApplyStates, + ); + const activeToolApplyState = useMemo(() => { + if (!activeToolStreamId) { + return undefined; + } + return applyStates.states.find( + (state) => state.streamId && state.streamId === activeToolStreamId, + ); + }, [applyStates, activeToolStreamId]); + useEffect(() => { - ideMessenger.post("showToast", ["info", "updated last apply state"]); - // dispatch(setToolCallOutput(output)); - // dispatch(acceptToolCall()); - }, [lastEditToolApplyState]); + if (activeToolApplyState?.status === "closed") { + const output: ContextItem = { + name: "Edit tool output", + content: "Completed edit", + description: "", + }; + dispatch(acceptToolCall()); + dispatch(setToolCallOutput([output])); + dispatch( + streamResponseAfterToolCall({ + toolCallId: activeToolApplyState.toolCallId!, + toolOutput: [output], + }), + ); + } + }, [activeToolApplyState?.status]); } export default useSetup; diff --git a/gui/src/pages/gui/ToolCallDiv/EditFile.tsx b/gui/src/pages/gui/ToolCallDiv/EditFile.tsx index b3cb2bb63..56263b0a1 100644 --- a/gui/src/pages/gui/ToolCallDiv/EditFile.tsx +++ b/gui/src/pages/gui/ToolCallDiv/EditFile.tsx @@ -2,32 +2,25 @@ import { getMarkdownLanguageTagForFile } from "core/util"; import { useMemo } from "react"; import StyledMarkdownPreview from "../../../components/markdown/StyledMarkdownPreview"; import AcceptRejectAllButtons from "../../../components/StepContainer/AcceptRejectAllButtons"; -import { useAppDispatch, useAppSelector } from "../../../redux/hooks"; -import { selectApplyStateByStreamId } from "../../../redux/slices/sessionSlice"; +import { useAppSelector } from "../../../redux/hooks"; type EditToolCallProps = { relativeFilePath: string; newContents: string; + toolCallId?: string; }; export function EditFile(props: EditToolCallProps) { const src = `\`\`\`${getMarkdownLanguageTagForFile(props.relativeFilePath ?? "test.txt")} ${props.relativeFilePath}\n${props.newContents ?? ""}\n\`\`\``; - const dispatch = useAppDispatch(); - const isStreaming = useAppSelector((state) => state.session.isStreaming); - const lastApplyToolStreamId = useAppSelector( - (state) => state.session.lastApplyToolStreamId, - ); - const streamId = useMemo(() => { - return lastApplyToolStreamId; - }, []); + const states = useAppSelector((store) => store.session.codeBlockApplyStates); - console.log("STREAM ID", streamId); - - const applyState = useAppSelector((state) => - selectApplyStateByStreamId(state, streamId), - ); + const applyState = useMemo(() => { + return states.states.find( + (state) => state.toolCallId && state.toolCallId === props.toolCallId, + ); + }, [states, props.toolCallId]); if (!props.relativeFilePath) { return null; @@ -39,11 +32,15 @@ export function EditFile(props: EditToolCallProps) { isRenderingInStepContainer={true} source={src} disableManualApply={true} - firstCodeblockStreamId={streamId} + singleCodeblockStreamId={applyState?.streamId} /> - + {isStreaming && applyState?.status === "streaming" && ( +
+ Generating... +
+ )} {!isStreaming && applyState?.status === "done" && ( -
+
{}} diff --git a/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx b/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx index ee8726fae..55ed79e2a 100644 --- a/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx +++ b/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx @@ -26,6 +26,7 @@ function FunctionSpecificToolCallDiv({ ); case BuiltInToolNames.RunTerminalCommand: diff --git a/gui/src/redux/slices/sessionSlice.ts b/gui/src/redux/slices/sessionSlice.ts index 544a8c401..bf22a1858 100644 --- a/gui/src/redux/slices/sessionSlice.ts +++ b/gui/src/redux/slices/sessionSlice.ts @@ -21,6 +21,7 @@ import { ToolCallDelta, ToolCallState, } from "core"; +import { BuiltInToolNames } from "core/tools/builtIn"; import { NEW_SESSION_TITLE } from "core/util/constants"; import { incrementalParseJson } from "core/util/incrementalParseJson"; import { renderChatMessage } from "core/util/messageContent"; @@ -54,7 +55,7 @@ type SessionState = { curIndex: number; }; newestCodeblockForInput: Record; - lastApplyToolStreamId?: string; + activeToolStreamId?: string; }; function isCodeToEditEqual(a: CodeToEdit, b: CodeToEdit) { @@ -367,6 +368,19 @@ export const sessionSlice = createSlice({ ); } + if ( + toolCallDelta.id && + toolCallDelta.function?.name === + BuiltInToolNames.EditExistingFile + ) { + const streamId = uuidv4(); + state.codeBlockApplyStates.states.push({ + streamId, + toolCallId: toolCallDelta.id, + status: "streaming", + }); + state.activeToolStreamId = streamId; + } historyItem.toolCallState = toolCallDeltaToState(toolCallDelta); } state.history.push(historyItem); @@ -552,9 +566,7 @@ export const sessionSlice = createSlice({ state.history[state.history.length - 1].contextItems = contextItems; }, - setLastToolApplyStreamId: (state, { payload }: PayloadAction) => { - state.lastApplyToolStreamId = payload; - }, + updateCurCheckpoint: ( state, { payload }: PayloadAction<{ filepath: string; content: string }>, @@ -632,6 +644,7 @@ export const sessionSlice = createSlice({ toolCallState.status = "canceled"; }, acceptToolCall: (state) => { + state.activeToolStreamId = undefined; const toolCallState = findCurrentToolCall(state.history); if (!toolCallState) return; @@ -729,16 +742,6 @@ export const selectApplyStateByStreamId = createSelector( }, ); -export const selectLastEditToolApplyState = createSelector( - [ - (state: RootState) => state.session.codeBlockApplyStates.states, - (state: RootState) => state.session.lastApplyToolStreamId, - ], - (states, streamId) => { - return states.find((state) => state.streamId === streamId); - }, -); - export const { updateFileSymbols, setContextItemsAtIndex, @@ -776,7 +779,6 @@ export const { deleteSessionMetadata, setNewestCodeblocksForInput, cycleMode, - setLastToolApplyStreamId, } = sessionSlice.actions; export const { diff --git a/gui/src/redux/thunks/callTool.ts b/gui/src/redux/thunks/callTool.ts index 190c25ccf..7ee80973f 100644 --- a/gui/src/redux/thunks/callTool.ts +++ b/gui/src/redux/thunks/callTool.ts @@ -3,7 +3,6 @@ import { ContextItem } from "core"; import { BuiltInToolNames } from "core/tools/builtIn"; import { EditToolArgs } from "core/tools/definitions/editFile"; import { resolveRelativePathInDir } from "core/util/ideUtils"; -import { v4 as uuidv4 } from "uuid"; import { IIdeMessenger } from "../../context/IdeMessenger"; import { selectCurrentToolCall } from "../selectors/selectCurrentToolCall"; import { selectDefaultModel } from "../slices/configSlice"; @@ -11,7 +10,6 @@ import { acceptToolCall, cancelToolCall, setCalling, - setLastToolApplyStreamId, setToolCallOutput, } from "../slices/sessionSlice"; import { ThunkApiType } from "../store"; @@ -44,17 +42,19 @@ export const callTool = createAsyncThunk( if ( toolCallState.toolCall.function.name === BuiltInToolNames.EditExistingFile ) { - const newToolStreamId = uuidv4(); - dispatch(setLastToolApplyStreamId(newToolStreamId)); const args = JSON.parse( toolCallState.toolCall.function.arguments || "{}", ); try { + if (!state.session.activeToolStreamId) { + throw new Error("Invalid apply state"); + } await customGuiEditImpl( args, extra.ideMessenger, - newToolStreamId, + state.session.activeToolStreamId, defaultModel.title, + toolCallState.toolCallId, ); } catch (e) { errorMessage = "Failed to call edit tool"; @@ -85,7 +85,7 @@ export const callTool = createAsyncThunk( icon: "problems", name: "Tool Call Error", description: "Tool Call Failed", - content: `The tool call failed with the message:\n\n${errorMessage}\n\nPlease try something else or request further instructions.`, + content: `${toolCallState.toolCall.function.name} failed with the message:\n\n${errorMessage}\n\nPlease try something else or request further instructions.`, hidden: false, }, ], @@ -114,23 +114,19 @@ async function customGuiEditImpl( ideMessenger: IIdeMessenger, streamId: string, modelTitle: string, + toolCallId: string, ) { const firstUriMatch = await resolveRelativePathInDir( args.filepath, ideMessenger.ide, ); if (!firstUriMatch) { - return [ - { - name: "Edit failure", - description: `editing ${args.filepath}`, - content: `Failed to edit ${args.filepath}: does not exist`, - }, - ]; + throw new Error(`${args.filepath} does not exist`); } ideMessenger.post("applyToFile", { streamId, text: args.new_contents, curSelectedModelTitle: modelTitle, + toolCallId, }); }