diff --git a/core/core.ts b/core/core.ts index 464f8a5e3..f2dc9387c 100644 --- a/core/core.ts +++ b/core/core.ts @@ -34,6 +34,7 @@ import { TTS } from "./util/tts"; import type { ContextItemId, IDE, IndexingProgressUpdate } from "."; import type { FromCoreProtocol, ToCoreProtocol } from "./protocol"; +import { instantiateTool } from "./tools"; import type { IMessenger, Message } from "./util/messenger"; export class Core { @@ -740,6 +741,12 @@ export class Core { recentlyEditedFilesCache.set(filepath, filepath); } }); + + on("tools/call", async ({ data: { toolCall } }) => { + const tool = instantiateTool(toolCall.function.name, { ide: this.ide }); + const result = await tool.action(toolCall.function.arguments); + return { result }; + }); } private indexingCancellationController: AbortController | undefined; diff --git a/core/protocol/core.ts b/core/protocol/core.ts index 196192a38..27cd3fcdd 100644 --- a/core/protocol/core.ts +++ b/core/protocol/core.ts @@ -16,6 +16,7 @@ import type { SerializedContinueConfig, SessionInfo, SiteIndexingConfig, + ToolCall, } from "../"; export type ProtocolGeneratorType = AsyncGenerator<{ @@ -158,4 +159,5 @@ export type ToCoreFromIdeOrWebviewProtocol = { "profiles/switch": [{ id: string }, undefined]; "auth/getAuthUrl": [undefined, { url: string }]; + "tools/call": [{ toolCall: ToolCall }, { result: any }]; }; diff --git a/core/protocol/passThrough.ts b/core/protocol/passThrough.ts index 1c0f5e962..f2b1af96e 100644 --- a/core/protocol/passThrough.ts +++ b/core/protocol/passThrough.ts @@ -47,6 +47,7 @@ export const WEBVIEW_TO_CORE_PASS_THROUGH: (keyof ToCoreFromWebviewProtocol)[] = "config/listProfiles", "profiles/switch", "didChangeSelectedProfile", + "tools/call", ]; // Message types to pass through from core to webview @@ -60,5 +61,5 @@ export const CORE_TO_WEBVIEW_PASS_THROUGH: (keyof ToWebviewFromCoreProtocol)[] = "isContinueInputFocused", "didChangeAvailableProfiles", "setTTSActive", - "getWebviewHistoryLength" + "getWebviewHistoryLength", ]; diff --git a/core/tools/index.ts b/core/tools/index.ts index aec23de36..ab592b757 100644 --- a/core/tools/index.ts +++ b/core/tools/index.ts @@ -1,9 +1,13 @@ import { IDE, Tool } from ".."; -export const makeCreateNewFileTool = (ide: IDE): Tool => ({ +export interface ToolParams { + ide: IDE; +} + +export const makeCreateNewFileTool = ({ ide }: ToolParams): Tool => ({ type: "function", - action: async (...args: any) => { - await ide.writeFile(args[0], args[1]); + action: async (args: any) => { + await ide.writeFile(args.filepath, args.contents); }, function: { name: "create_new_file", @@ -25,10 +29,10 @@ export const makeCreateNewFileTool = (ide: IDE): Tool => ({ }, }); -export const runTerminalCommandTool = (ide: IDE): Tool => ({ +export const runTerminalCommandTool = ({ ide }: ToolParams): Tool => ({ type: "function", - action: async (...args) => { - await ide.runCommand(args[0]); + action: async (args) => { + await ide.runCommand(args.command); }, function: { name: "run_terminal_command", @@ -46,3 +50,14 @@ export const runTerminalCommandTool = (ide: IDE): Tool => ({ }, }, }); + +export function instantiateTool(name: string, { ide }: ToolParams): Tool { + switch (name) { + case "create_new_file": + return makeCreateNewFileTool({ ide }); + case "run_terminal_command": + return runTerminalCommandTool({ ide }); + default: + throw new Error(`Unknown tool ${name}`); + } +} diff --git a/extensions/vscode/package.json b/extensions/vscode/package.json index e7b6c8271..abcf80fb4 100644 --- a/extensions/vscode/package.json +++ b/extensions/vscode/package.json @@ -57,6 +57,12 @@ ], "main": "./out/extension.js", "contributes": { + "authentication": [ + { + "id": "continue", + "label": "Continue" + } + ], "languages": [ { "filenames": [ diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 0b1b1756c..6f642d9ea 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -6,6 +6,7 @@ import { } from "@heroicons/react/24/outline"; import { JSONContent } from "@tiptap/react"; import { InputModifiers } from "core"; +import { stripImages } from "core/llm/images"; import { usePostHog } from "posthog-js/react"; import { Fragment, @@ -342,6 +343,8 @@ export function Chat() { editorState={item.editorState} contextItems={item.contextItems} /> + ) : item.message.role === "tool" ? ( +
{stripImages(item.message.content)}
) : item.message.role === "assistant" && item.message.toolCalls ? (
diff --git a/gui/src/pages/gui/ToolCallDiv/index.tsx b/gui/src/pages/gui/ToolCallDiv/index.tsx index dbe7c3bed..de89fbb9a 100644 --- a/gui/src/pages/gui/ToolCallDiv/index.tsx +++ b/gui/src/pages/gui/ToolCallDiv/index.tsx @@ -1,4 +1,6 @@ import { ToolCall } from "core"; +import { useContext } from "react"; +import { useDispatch } from "react-redux"; import styled from "styled-components"; import { defaultBorderRadius, @@ -6,6 +8,8 @@ import { vscButtonBackground, vscButtonForeground, } from "../../../components"; +import { IdeMessengerContext } from "../../../context/IdeMessenger"; +import { streamUpdate } from "../../../redux/slices/stateSlice"; import { CreateFile } from "./CreateFile"; import { RunTerminalCommand } from "./RunTerminalCommand"; @@ -84,12 +88,29 @@ function FunctionSpecificToolCallDiv(toolCall: ToolCall) { } export function ToolCallDiv(props: ToolCallDivProps) { + const ideMessenger = useContext(IdeMessengerContext); + const dispatch = useDispatch(); + + async function callTool() { + const result = await ideMessenger.request("tools/call", { + toolCall: props.toolCall, + }); + if (result.status === "success") { + dispatch( + streamUpdate({ + role: "tool", + content: JSON.stringify(result.content.result), + }), + ); + } + } + return ( - Cancel - Continue + {}}>Cancel + Continue ); diff --git a/gui/src/redux/slices/stateSlice.ts b/gui/src/redux/slices/stateSlice.ts index db6dbf859..647c18188 100644 --- a/gui/src/redux/slices/stateSlice.ts +++ b/gui/src/redux/slices/stateSlice.ts @@ -266,7 +266,6 @@ export const stateSlice = createSlice({ action.payload.role && lastMessage.message.role !== action.payload.role ) { - console.log("NEW: ", action.payload); // Create a new message state.history.push({ contextItems: [],