read file tool
This commit is contained in:
parent
7d24806056
commit
5d4e6e9494
16 changed files with 110 additions and 19 deletions
1
core/index.d.ts
vendored
1
core/index.d.ts
vendored
|
|
@ -930,6 +930,7 @@ export interface Tool {
|
|||
|
||||
displayTitle: string;
|
||||
wouldLikeTo: string;
|
||||
readonly: boolean;
|
||||
}
|
||||
|
||||
interface BaseCompletionOptions {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
export enum BuiltInToolNames {
|
||||
ReadFile = "read_file",
|
||||
CreateNewFile = "create_new_file",
|
||||
RunTerminalCommand = "run_terminal_command",
|
||||
ViewSubdirectory = "view_subdirectory",
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
25
core/tools/definitions/readFile.ts
Normal file
25
core/tools/definitions/readFile.ts
Normal 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.",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ export const searchWebTool: Tool = {
|
|||
type: "function",
|
||||
displayTitle: "Search Web",
|
||||
wouldLikeTo: "search the web",
|
||||
readonly: true,
|
||||
function: {
|
||||
name: BuiltInToolNames.SearchWeb,
|
||||
description:
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
14
core/tools/implementations/readFile.ts
Normal file
14
core/tools/implementations/readFile.ts
Normal 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,
|
||||
},
|
||||
];
|
||||
};
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
14
gui/src/pages/gui/ToolCallDiv/ExactSearch.tsx
Normal file
14
gui/src/pages/gui/ToolCallDiv/ExactSearch.tsx
Normal 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}\`"`}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue