From 5d4e6e9494767bda8b2ee970f05667ae2a333939 Mon Sep 17 00:00:00 2001 From: Nate Date: Fri, 29 Nov 2024 08:38:26 -0800 Subject: [PATCH] read file tool --- core/index.d.ts | 1 + core/tools/builtIn.ts | 1 + core/tools/callTool.ts | 3 ++ core/tools/definitions/readFile.ts | 25 ++++++++++++++ core/tools/definitions/runTerminalCommand.ts | 1 + core/tools/definitions/searchWeb.ts | 1 + core/tools/definitions/viewDiff.ts | 1 + core/tools/definitions/viewRepoMap.ts | 1 + core/tools/definitions/viewSubdirectory.ts | 1 + core/tools/implementations/readFile.ts | 14 ++++++++ .../implementations/runTerminalCommand.ts | 33 ++++++++++++------- core/tools/index.ts | 2 ++ .../InputToolbar/ToolDropdownItem.tsx | 20 +++++++---- gui/src/pages/gui/ToolCallDiv/ExactSearch.tsx | 14 ++++++++ .../FunctionSpecificToolCallDiv.tsx | 3 ++ gui/src/redux/slices/uiStateSlice.ts | 8 +++++ 16 files changed, 110 insertions(+), 19 deletions(-) create mode 100644 core/tools/definitions/readFile.ts create mode 100644 core/tools/implementations/readFile.ts create mode 100644 gui/src/pages/gui/ToolCallDiv/ExactSearch.tsx diff --git a/core/index.d.ts b/core/index.d.ts index 4d7bbed1c..3ec58990d 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -930,6 +930,7 @@ export interface Tool { displayTitle: string; wouldLikeTo: string; + readonly: boolean; } interface BaseCompletionOptions { diff --git a/core/tools/builtIn.ts b/core/tools/builtIn.ts index 421319c14..255967568 100644 --- a/core/tools/builtIn.ts +++ b/core/tools/builtIn.ts @@ -1,4 +1,5 @@ export enum BuiltInToolNames { + ReadFile = "read_file", CreateNewFile = "create_new_file", RunTerminalCommand = "run_terminal_command", ViewSubdirectory = "view_subdirectory", diff --git a/core/tools/callTool.ts b/core/tools/callTool.ts index dbe4a8a9f..80dd69c79 100644 --- a/core/tools/callTool.ts +++ b/core/tools/callTool.ts @@ -2,6 +2,7 @@ import { ContextItem, ToolExtras } from ".."; import { BuiltInToolNames } from "./builtIn"; import { createNewFileImpl } from "./implementations/createNewFile"; import { exactSearchImpl } from "./implementations/exactSearch"; +import { readFileImpl } from "./implementations/readFile"; import { runTerminalCommandImpl } from "./implementations/runTerminalCommand"; import { searchWebImpl } from "./implementations/searchWeb"; import { viewDiffImpl } from "./implementations/viewDiff"; @@ -42,6 +43,8 @@ export async function callTool( extras: ToolExtras, ): Promise { switch (uri) { + case BuiltInToolNames.ReadFile: + return await readFileImpl(args, extras); case BuiltInToolNames.CreateNewFile: return await createNewFileImpl(args, extras); case BuiltInToolNames.ExactSearch: diff --git a/core/tools/definitions/readFile.ts b/core/tools/definitions/readFile.ts new file mode 100644 index 000000000..51d958fc4 --- /dev/null +++ b/core/tools/definitions/readFile.ts @@ -0,0 +1,25 @@ +import { Tool } from "../.."; +import { BuiltInToolNames } from "../builtIn"; + +export const readFileTool: Tool = { + type: "function", + displayTitle: "Read File", + wouldLikeTo: "read a file", + readonly: true, + function: { + name: BuiltInToolNames.ReadFile, + description: + "Use this tool whenever you need to view the contents of a file.", + parameters: { + type: "object", + required: ["filepath"], + properties: { + filepath: { + type: "string", + description: + "The path of the file to read, relative to the root of the workspace.", + }, + }, + }, + }, +}; diff --git a/core/tools/definitions/runTerminalCommand.ts b/core/tools/definitions/runTerminalCommand.ts index b3c40ca32..94a429e23 100644 --- a/core/tools/definitions/runTerminalCommand.ts +++ b/core/tools/definitions/runTerminalCommand.ts @@ -6,6 +6,7 @@ export const runTerminalCommandTool: Tool = { type: "function", displayTitle: "Run Terminal Command", wouldLikeTo: "run a terminal command", + readonly: false, function: { name: BuiltInToolNames.RunTerminalCommand, description: "Run a terminal command in the current directory", diff --git a/core/tools/definitions/searchWeb.ts b/core/tools/definitions/searchWeb.ts index c8ead3c28..d462158c9 100644 --- a/core/tools/definitions/searchWeb.ts +++ b/core/tools/definitions/searchWeb.ts @@ -6,6 +6,7 @@ export const searchWebTool: Tool = { type: "function", displayTitle: "Search Web", wouldLikeTo: "search the web", + readonly: true, function: { name: BuiltInToolNames.SearchWeb, description: diff --git a/core/tools/definitions/viewDiff.ts b/core/tools/definitions/viewDiff.ts index 554f49878..e54086d05 100644 --- a/core/tools/definitions/viewDiff.ts +++ b/core/tools/definitions/viewDiff.ts @@ -6,6 +6,7 @@ export const viewDiffTool: Tool = { type: "function", displayTitle: "View Diff", wouldLikeTo: "view a diff", + readonly: true, function: { name: BuiltInToolNames.ViewDiff, description: "View the current diff of working changes", diff --git a/core/tools/definitions/viewRepoMap.ts b/core/tools/definitions/viewRepoMap.ts index aca91a78b..9cf3d319d 100644 --- a/core/tools/definitions/viewRepoMap.ts +++ b/core/tools/definitions/viewRepoMap.ts @@ -6,6 +6,7 @@ export const viewRepoMapTool: Tool = { type: "function", displayTitle: "View Repo Map", wouldLikeTo: "view the repository map", + readonly: true, function: { name: BuiltInToolNames.ViewRepoMap, description: "View the repository map", diff --git a/core/tools/definitions/viewSubdirectory.ts b/core/tools/definitions/viewSubdirectory.ts index 4ac1b5c40..e3c1d034b 100644 --- a/core/tools/definitions/viewSubdirectory.ts +++ b/core/tools/definitions/viewSubdirectory.ts @@ -6,6 +6,7 @@ export const viewSubdirectoryTool: Tool = { type: "function", displayTitle: "View Subdirectory", wouldLikeTo: "view the contents of a subdirectory", + readonly: true, function: { name: BuiltInToolNames.ViewSubdirectory, description: "View the contents of a subdirectory", diff --git a/core/tools/implementations/readFile.ts b/core/tools/implementations/readFile.ts new file mode 100644 index 000000000..032db3dce --- /dev/null +++ b/core/tools/implementations/readFile.ts @@ -0,0 +1,14 @@ +import { getBasename } from "../../util"; + +import { ToolImpl } from "."; + +export const readFileImpl: ToolImpl = async (args, extras) => { + const content = await extras.ide.readFile(args.filepath); + return [ + { + name: getBasename(args.filepath), + description: args.filepath, + content, + }, + ]; +}; diff --git a/core/tools/implementations/runTerminalCommand.ts b/core/tools/implementations/runTerminalCommand.ts index fb4770cee..ddc8575c4 100644 --- a/core/tools/implementations/runTerminalCommand.ts +++ b/core/tools/implementations/runTerminalCommand.ts @@ -1,22 +1,31 @@ import { ToolImpl } from "."; - const util = require("node:util"); const asyncExec = util.promisify(require("node:child_process").exec); export const runTerminalCommandImpl: ToolImpl = async (args, extras) => { const ideInfo = await extras.ide.getIdeInfo(); + if (ideInfo.remoteName === "local" || ideInfo.remoteName === "") { - // If we're local, can just run the terminal command with child_process - const output = await asyncExec(args.command, { - cwd: (await extras.ide.getWorkspaceDirs())[0], - }); - return [ - { - name: "Terminal", - description: "Terminal command output", - content: output.stdout ?? "", - }, - ]; + try { + const output = await asyncExec(args.command, { + cwd: (await extras.ide.getWorkspaceDirs())[0], + }); + return [ + { + name: "Terminal", + description: "Terminal command output", + content: output.stdout ?? "", + }, + ]; + } catch (error: any) { + return [ + { + name: "Terminal", + description: "Terminal command output", + content: error.stderr ?? error.toString(), + }, + ]; + } } await extras.ide.runCommand(args.command); diff --git a/core/tools/index.ts b/core/tools/index.ts index 7f8eb32fb..86dfc82c5 100644 --- a/core/tools/index.ts +++ b/core/tools/index.ts @@ -1,5 +1,6 @@ import { createNewFileTool } from "./definitions/createNewFile"; import { exactSearchTool } from "./definitions/exactSearch"; +import { readFileTool } from "./definitions/readFile"; import { runTerminalCommandTool } from "./definitions/runTerminalCommand"; import { searchWebTool } from "./definitions/searchWeb"; import { viewDiffTool } from "./definitions/viewDiff"; @@ -7,6 +8,7 @@ import { viewRepoMapTool } from "./definitions/viewRepoMap"; import { viewSubdirectoryTool } from "./definitions/viewSubdirectory"; export const allTools = [ + readFileTool, createNewFileTool, runTerminalCommandTool, viewSubdirectoryTool, diff --git a/gui/src/components/mainInput/InputToolbar/ToolDropdownItem.tsx b/gui/src/components/mainInput/InputToolbar/ToolDropdownItem.tsx index e137c18dc..431e6f36b 100644 --- a/gui/src/components/mainInput/InputToolbar/ToolDropdownItem.tsx +++ b/gui/src/components/mainInput/InputToolbar/ToolDropdownItem.tsx @@ -1,9 +1,7 @@ import { Tool } from "core"; +import { useEffect } from "react"; import { useDispatch, useSelector } from "react-redux"; -import { - DEFAULT_TOOL_SETTING, - toggleToolSetting, -} from "../../../redux/slices/uiStateSlice"; +import { addTool, toggleToolSetting } from "../../../redux/slices/uiStateSlice"; import { RootState } from "../../../redux/store"; interface ToolDropdownItemProps { @@ -13,11 +11,19 @@ interface ToolDropdownItemProps { function ToolDropdownItem(props: ToolDropdownItemProps) { const dispatch = useDispatch(); const settings = useSelector( - (state: RootState) => - state.uiState.toolSettings[props.tool.function.name] ?? - DEFAULT_TOOL_SETTING, + (state: RootState) => state.uiState.toolSettings[props.tool.function.name], ); + useEffect(() => { + if (!settings) { + dispatch(addTool(props.tool)); + } + }, [props.tool.function.name, settings]); + + if (!settings) { + return null; + } + return (
+ ); +} diff --git a/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx b/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx index 68fdeb682..dd06c48ca 100644 --- a/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx +++ b/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx @@ -1,5 +1,6 @@ import { ToolCall, ToolCallState } from "core"; import { CreateFile } from "./CreateFile"; +import { ExactSearch } from "./ExactSearch"; import { RunTerminalCommand } from "./RunTerminalCommand"; function FunctionSpecificToolCallDiv({ @@ -18,6 +19,8 @@ function FunctionSpecificToolCallDiv({ ); case "run_terminal_command": return ; + case "exact_search": + return ; default: return null; } diff --git a/gui/src/redux/slices/uiStateSlice.ts b/gui/src/redux/slices/uiStateSlice.ts index 499a78d7b..3472eb018 100644 --- a/gui/src/redux/slices/uiStateSlice.ts +++ b/gui/src/redux/slices/uiStateSlice.ts @@ -1,4 +1,5 @@ import { createSlice, PayloadAction } from "@reduxjs/toolkit"; +import { Tool } from "core"; import { BuiltInToolNames } from "core/tools/builtIn"; import { defaultOnboardingCardState, @@ -33,6 +34,7 @@ export const uiStateSlice = createSlice({ useTools: false, toolSettings: { + [BuiltInToolNames.ReadFile]: "allowedWithoutPermission", [BuiltInToolNames.CreateNewFile]: "allowedWithPermission", [BuiltInToolNames.RunTerminalCommand]: "allowedWithPermission", [BuiltInToolNames.ViewSubdirectory]: "allowedWithoutPermission", @@ -68,6 +70,11 @@ export const uiStateSlice = createSlice({ toggleUseTools: (state) => { state.useTools = !state.useTools; }, + addTool: (state, action: PayloadAction) => { + state.toolSettings[action.payload.function.name] = action.payload.readonly + ? "allowedWithoutPermission" + : "allowedWithPermission"; + }, toggleToolSetting: (state, action: PayloadAction) => { const setting = state.toolSettings[action.payload]; @@ -96,6 +103,7 @@ export const { setShowDialog, toggleUseTools, toggleToolSetting, + addTool, } = uiStateSlice.actions; export default uiStateSlice.reducer;