run tools

This commit is contained in:
Nate 2024-11-17 18:36:45 -08:00
parent 0c4f80bca8
commit 47dd44da55
8 changed files with 64 additions and 10 deletions

View file

@ -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;

View file

@ -16,6 +16,7 @@ import type {
SerializedContinueConfig,
SessionInfo,
SiteIndexingConfig,
ToolCall,
} from "../";
export type ProtocolGeneratorType<T> = AsyncGenerator<{
@ -158,4 +159,5 @@ export type ToCoreFromIdeOrWebviewProtocol = {
"profiles/switch": [{ id: string }, undefined];
"auth/getAuthUrl": [undefined, { url: string }];
"tools/call": [{ toolCall: ToolCall }, { result: any }];
};

View file

@ -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",
];

View file

@ -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}`);
}
}

View file

@ -57,6 +57,12 @@
],
"main": "./out/extension.js",
"contributes": {
"authentication": [
{
"id": "continue",
"label": "Continue"
}
],
"languages": [
{
"filenames": [

View file

@ -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" ? (
<div>{stripImages(item.message.content)}</div>
) : item.message.role === "assistant" &&
item.message.toolCalls ? (
<div>

View file

@ -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 (
<Container>
<FunctionSpecificToolCallDiv {...props.toolCall} />
<ButtonContainer>
<RejectButton>Cancel</RejectButton>
<AcceptButton>Continue</AcceptButton>
<RejectButton onClick={() => {}}>Cancel</RejectButton>
<AcceptButton onClick={callTool}>Continue</AcceptButton>
</ButtonContainer>
</Container>
);

View file

@ -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: [],