diff --git a/core/config/types.ts b/core/config/types.ts index 8fc855779..1b4bff556 100644 --- a/core/config/types.ts +++ b/core/config/types.ts @@ -24,7 +24,7 @@ declare global { region?: string; projectId?: string; - _fetch?: (input: any, init?: RequestInit) => Promise; + _fetch?: (input: any, init?: any) => Promise; complete(prompt: string, options?: LLMFullCompletionOptions): Promise; @@ -148,6 +148,7 @@ declare global { export interface ChatHistoryItem { message: ChatMessage; + editorState?: any; contextItems: ContextItemWithId[]; promptLogs?: [string, string][]; // [prompt, completion] } @@ -267,6 +268,10 @@ declare global { description: string; params?: { [key: string]: any }; run: (sdk: ContinueSDK) => AsyncGenerator; + + // If true, this command will be run in NodeJs and have access to the filesystem and other Node-only APIs + // You must make sure to dynamically import any Node-only dependencies in your command so that it doesn't break in the browser + runInNodeJs?: boolean; } // Config @@ -302,6 +307,7 @@ declare global { | "anthropic" | "chatml" | "none" + | "openchat" | "deepseek"; type ModelProvider = @@ -319,7 +325,8 @@ declare global { | "lmstudio" | "llamafile" | "gemini" - | "mistral"; + | "mistral" + | "bedrock"; export type ModelName = // OpenAI @@ -362,7 +369,7 @@ declare global { verifySsl?: boolean; caBundlePath?: string | string[]; proxy?: string; - headers?: Record; + headers?: { [key: string]: string }; } export interface StepWithParams { @@ -483,6 +490,7 @@ declare global { userToken?: string; } + } export {}; diff --git a/core/ide/messaging.ts b/core/ide/messaging.ts index 590bb43aa..4ab5d1686 100644 --- a/core/ide/messaging.ts +++ b/core/ide/messaging.ts @@ -103,5 +103,11 @@ export async function* ideStreamRequest( await new Promise((resolve) => setTimeout(resolve, 50)); } + if (buffer.length > index) { + const chunk = buffer.slice(index); + index = buffer.length; + yield chunk; + } + clearTimeout(staleTimeout); } diff --git a/core/index.d.ts b/core/index.d.ts index fa719fc3b..07f572ba0 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -266,6 +266,10 @@ export interface SlashCommand { description: string; params?: { [key: string]: any }; run: (sdk: ContinueSDK) => AsyncGenerator; + + // If true, this command will be run in NodeJs and have access to the filesystem and other Node-only APIs + // You must make sure to dynamically import any Node-only dependencies in your command so that it doesn't break in the browser + runInNodeJs?: boolean; } // Config diff --git a/extensions/vscode/src/debugPanel.ts b/extensions/vscode/src/debugPanel.ts index d7a00124d..44042838d 100644 --- a/extensions/vscode/src/debugPanel.ts +++ b/extensions/vscode/src/debugPanel.ts @@ -11,7 +11,7 @@ import { ideProtocolClient, windowId } from "./activation/activate"; import { getContinueServerUrl } from "./bridge"; import historyManager from "./history"; import { VsCodeIde } from "./ideProtocol"; -import { llmFromTitle } from "./loadConfig"; +import { configHandler, llmFromTitle } from "./loadConfig"; import { getExtensionUri, getNonce, getUniqueId } from "./util/vscode"; let sockets: { [url: string]: io.Socket | undefined } = {}; @@ -436,6 +436,39 @@ export function getSidebarContent( respond({ content: completion }); break; } + case "runNodeJsSlashCommand": { + const { + input, + history, + modelTitle, + slashCommandName, + contextItems, + params, + } = data.message; + + const config = await configHandler.loadConfig(ide); + const llm = await llmFromTitle(modelTitle); + const slashCommand = config.slashCommands?.find( + (sc) => sc.name === slashCommandName + ); + if (!slashCommand) { + throw new Error(`Unknown slash command ${slashCommandName}`); + } + + for await (const update of slashCommand.run({ + input, + history, + llm, + contextItems, + params, + ide, + addContextItem: () => {}, + })) { + respond({ content: update }); + } + respond({ done: true }); + break; + } } } catch (e) { vscode.window.showErrorMessage( diff --git a/extensions/vscode/src/ideProtocol.ts b/extensions/vscode/src/ideProtocol.ts index 3caefa3dc..64c3103b6 100644 --- a/extensions/vscode/src/ideProtocol.ts +++ b/extensions/vscode/src/ideProtocol.ts @@ -32,7 +32,7 @@ async function buildConfigTs(browser: boolean) { platform: browser ? "browser" : "node", format: browser ? "esm" : "cjs", outfile: getConfigJsPath(!browser), - external: ["fetch"], + external: ["fetch", "fs", "path", "os", "child_process"], }); } catch (e) { console.log(e); diff --git a/gui/src/components/dialogs/KeyboardShortcuts.tsx b/gui/src/components/dialogs/KeyboardShortcuts.tsx index 374ea7096..286465ee7 100644 --- a/gui/src/components/dialogs/KeyboardShortcuts.tsx +++ b/gui/src/components/dialogs/KeyboardShortcuts.tsx @@ -62,7 +62,7 @@ const vscodeShortcuts: KeyboardShortcutProps[] = [ { mac: "⌘ ⇧ M", windows: "⌃ ⇧ M", - description: "Edit Highlighted Code", + description: "Ask Follow-Up about Highlighted Code", }, { mac: "⌘ ⇧ ⏎", diff --git a/gui/src/components/mainInput/TipTapEditor.tsx b/gui/src/components/mainInput/TipTapEditor.tsx index e397997d4..bc1253d3c 100644 --- a/gui/src/components/mainInput/TipTapEditor.tsx +++ b/gui/src/components/mainInput/TipTapEditor.tsx @@ -125,6 +125,9 @@ function TipTapEditor(props: TipTapEditorProps) { const firstResultsRef = useUpdatingRef(firstResults); const historyLengthRef = useUpdatingRef(historyLength); const onEnterRef = useUpdatingRef(props.onEnter); + const availableSlashCommandsRef = useUpdatingRef( + props.availableSlashCommands + ); const editor = useEditor({ extensions: [ @@ -190,7 +193,7 @@ function TipTapEditor(props: TipTapEditorProps) { class: "mention", }, suggestion: getCommandSuggestion( - props.availableSlashCommands, + availableSlashCommandsRef, onClose, onOpen ), diff --git a/gui/src/components/mainInput/getSuggestion.ts b/gui/src/components/mainInput/getSuggestion.ts index f17f9db05..dd50691c1 100644 --- a/gui/src/components/mainInput/getSuggestion.ts +++ b/gui/src/components/mainInput/getSuggestion.ts @@ -157,13 +157,13 @@ export function getMentionSuggestion( } export function getCommandSuggestion( - availableSlashCommands: ComboBoxItem[], + availableSlashCommandsRef: MutableRefObject, onClose: () => void, onOpen: () => void ) { const items = ({ query }) => { return ( - availableSlashCommands?.filter((slashCommand) => { + availableSlashCommandsRef.current?.filter((slashCommand) => { const sc = slashCommand.title.substring(1).toLowerCase(); const iv = query.toLowerCase(); return sc.startsWith(iv); diff --git a/gui/src/hooks/useChatHandler.ts b/gui/src/hooks/useChatHandler.ts index 1d24bff17..4eaf52109 100644 --- a/gui/src/hooks/useChatHandler.ts +++ b/gui/src/hooks/useChatHandler.ts @@ -3,6 +3,7 @@ import { Dispatch } from "@reduxjs/toolkit"; import { JSONContent } from "@tiptap/react"; import { ChatHistory, ChatHistoryItem, ChatMessage, SlashCommand } from "core"; import { ExtensionIde } from "core/ide"; +import { ideStreamRequest } from "core/ide/messaging"; import { constructMessages } from "core/llm/constructMessages"; import { usePostHog } from "posthog-js/react"; import { useEffect, useRef } from "react"; @@ -70,26 +71,51 @@ function useChatHandler(dispatch: Dispatch) { return [slashCommand, input]; }; + async function* _streamSlashCommandFromVsCode( + messages: ChatMessage[], + slashCommand: SlashCommand, + input: string + ): AsyncGenerator { + const modelTitle = defaultModel.title; + + for await (const update of ideStreamRequest("runNodeJsSlashCommand", { + input, + history: messages, + modelTitle, + slashCommandName: slashCommand.name, + contextItems, + params: slashCommand.params, + })) { + yield update; + } + } + async function _streamSlashCommand( messages: ChatMessage[], slashCommand: SlashCommand, input: string ) { - const sdk = { - input, - history: messages, - ide: new ExtensionIde(), - llm: defaultModel, - addContextItem: (item) => { - dispatch(addContextItems([item])); - }, - contextItems, - params: slashCommand.params, - }; + let generator: AsyncGenerator; + if (slashCommand.runInNodeJs) { + generator = _streamSlashCommandFromVsCode(messages, slashCommand, input); + } else { + const sdk = { + input, + history: messages, + ide: new ExtensionIde(), + llm: defaultModel, + addContextItem: (item) => { + dispatch(addContextItems([item])); + }, + contextItems, + params: slashCommand.params, + }; + generator = slashCommand.run(sdk); + } // TODO: if the model returned fast enough it would immediately break // Ideally you aren't trusting that results of dispatch show up before the first yield - for await (const update of slashCommand.run(sdk)) { + for await (const update of generator) { if (!activeRef.current) { break; }