diff --git a/core/tools/builtIn.ts b/core/tools/builtIn.ts index 255967568..55e2c5214 100644 --- a/core/tools/builtIn.ts +++ b/core/tools/builtIn.ts @@ -1,5 +1,6 @@ export enum BuiltInToolNames { ReadFile = "read_file", + ReadCurrentlyOpenFile = "read_currently_open_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 d34851787..4e7028d68 100644 --- a/core/tools/callTool.ts +++ b/core/tools/callTool.ts @@ -1,8 +1,10 @@ import { ContextItem, ToolExtras } from ".."; import MCPConnectionSingleton from "../context/mcp"; import { BuiltInToolNames } from "./builtIn"; + import { createNewFileImpl } from "./implementations/createNewFile"; import { exactSearchImpl } from "./implementations/exactSearch"; +import { readCurrentlyOpenFileImpl } from "./implementations/readCurrentlyOpenFile"; import { readFileImpl } from "./implementations/readFile"; import { runTerminalCommandImpl } from "./implementations/runTerminalCommand"; import { searchWebImpl } from "./implementations/searchWeb"; @@ -99,6 +101,8 @@ export async function callTool( return await viewRepoMapImpl(args, extras); case BuiltInToolNames.ViewSubdirectory: return await viewSubdirectoryImpl(args, extras); + case BuiltInToolNames.ReadCurrentlyOpenFile: + return await readCurrentlyOpenFileImpl(args, extras); default: return await callToolFromUri(uri, args, extras); } diff --git a/core/tools/definitions/readCurrentlyOpenFile.ts b/core/tools/definitions/readCurrentlyOpenFile.ts new file mode 100644 index 000000000..79497a158 --- /dev/null +++ b/core/tools/definitions/readCurrentlyOpenFile.ts @@ -0,0 +1,18 @@ +import { Tool } from "../.."; +import { BuiltInToolNames } from "../builtIn"; + +export const readCurrentlyOpenFileTool: Tool = { + type: "function", + displayTitle: "Read Currently Open File", + wouldLikeTo: "read the current file", + readonly: true, + function: { + name: BuiltInToolNames.ReadCurrentlyOpenFile, + description: + "Read the currently open file in the IDE. If the user seems to be referring to a file that you can't see, this is probably it.", + parameters: { + type: "object", + properties: {}, + }, + }, +}; diff --git a/core/tools/implementations/readCurrentlyOpenFile.ts b/core/tools/implementations/readCurrentlyOpenFile.ts new file mode 100644 index 000000000..7fff3b621 --- /dev/null +++ b/core/tools/implementations/readCurrentlyOpenFile.ts @@ -0,0 +1,20 @@ +import { ToolImpl } from "."; +import { getBasename } from "../../util"; + +export const readCurrentlyOpenFileImpl: ToolImpl = async (args, extras) => { + const result = await extras.ide.getCurrentFile(); + + if (!result) { + return []; + } + + const basename = getBasename(result.path); + + return [ + { + name: "Current file", + description: basename, + content: `\`\`\`${basename}\n${result.contents}\n\`\`\``, + }, + ]; +}; diff --git a/core/tools/index.ts b/core/tools/index.ts index 86dfc82c5..c27860a6f 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 { readCurrentlyOpenFileTool } from "./definitions/readCurrentlyOpenFile"; import { readFileTool } from "./definitions/readFile"; import { runTerminalCommandTool } from "./definitions/runTerminalCommand"; import { searchWebTool } from "./definitions/searchWeb"; @@ -16,4 +17,5 @@ export const allTools = [ exactSearchTool, searchWebTool, viewDiffTool, + readCurrentlyOpenFileTool, ];