read file tool

This commit is contained in:
Nate 2024-11-29 08:38:26 -08:00
parent 7d24806056
commit 5d4e6e9494
16 changed files with 110 additions and 19 deletions

1
core/index.d.ts vendored
View file

@ -930,6 +930,7 @@ export interface Tool {
displayTitle: string;
wouldLikeTo: string;
readonly: boolean;
}
interface BaseCompletionOptions {

View file

@ -1,4 +1,5 @@
export enum BuiltInToolNames {
ReadFile = "read_file",
CreateNewFile = "create_new_file",
RunTerminalCommand = "run_terminal_command",
ViewSubdirectory = "view_subdirectory",

View file

@ -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<ContextItem[]> {
switch (uri) {
case BuiltInToolNames.ReadFile:
return await readFileImpl(args, extras);
case BuiltInToolNames.CreateNewFile:
return await createNewFileImpl(args, extras);
case BuiltInToolNames.ExactSearch:

View file

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

View file

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

View file

@ -6,6 +6,7 @@ export const searchWebTool: Tool = {
type: "function",
displayTitle: "Search Web",
wouldLikeTo: "search the web",
readonly: true,
function: {
name: BuiltInToolNames.SearchWeb,
description:

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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 (
<div
className="flex w-full items-center justify-between gap-2 px-2 py-1"

View file

@ -0,0 +1,14 @@
import StyledMarkdownPreview from "../../../components/markdown/StyledMarkdownPreview";
interface ExactSearchToolCallProps {
query: string;
}
export function ExactSearch(props: ExactSearchToolCallProps) {
return (
<StyledMarkdownPreview
isRenderingInStepContainer={true}
source={`Searching for "\`${props.query}\`"`}
/>
);
}

View file

@ -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 <RunTerminalCommand command={args.command} />;
case "exact_search":
return <ExactSearch query={args.query} />;
default:
return null;
}

View file

@ -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<Tool>) => {
state.toolSettings[action.payload.function.name] = action.payload.readonly
? "allowedWithoutPermission"
: "allowedWithPermission";
},
toggleToolSetting: (state, action: PayloadAction<string>) => {
const setting = state.toolSettings[action.payload];
@ -96,6 +103,7 @@ export const {
setShowDialog,
toggleUseTools,
toggleToolSetting,
addTool,
} = uiStateSlice.actions;
export default uiStateSlice.reducer;