diff --git a/core/tools/builtIn.ts b/core/tools/builtIn.ts index d97ba718a..6d5ea9ffd 100644 --- a/core/tools/builtIn.ts +++ b/core/tools/builtIn.ts @@ -1,7 +1,6 @@ export enum BuiltInToolNames { ReadFile = "read_file", ReadFileRange = "read_file_range", - ReadFileHead = "read_file_head", EditExistingFile = "edit_existing_file", SearchAndReplaceInFile = "search_and_replace_in_file", SingleFindAndReplace = "single_find_and_replace", diff --git a/core/tools/callTool.ts b/core/tools/callTool.ts index b06b47102..c1d203972 100644 --- a/core/tools/callTool.ts +++ b/core/tools/callTool.ts @@ -12,7 +12,7 @@ import { grepSearchImpl } from "./implementations/grepSearch"; import { lsToolImpl } from "./implementations/lsTool"; import { readCurrentlyOpenFileImpl } from "./implementations/readCurrentlyOpenFile"; import { readFileImpl } from "./implementations/readFile"; -import { readFileHeadImpl } from "./implementations/readFileHead"; + import { readFileRangeImpl } from "./implementations/readFileRange"; import { requestRuleImpl } from "./implementations/requestRule"; import { runTerminalCommandImpl } from "./implementations/runTerminalCommand"; @@ -149,8 +149,6 @@ export async function callBuiltInTool( return await readFileImpl(args, extras); case BuiltInToolNames.ReadFileRange: return await readFileRangeImpl(args, extras); - case BuiltInToolNames.ReadFileHead: - return await readFileHeadImpl(args, extras); case BuiltInToolNames.CreateNewFile: return await createNewFileImpl(args, extras); case BuiltInToolNames.GrepSearch: diff --git a/core/tools/definitions/index.ts b/core/tools/definitions/index.ts index 77a0f30f0..9ddb6d5b6 100644 --- a/core/tools/definitions/index.ts +++ b/core/tools/definitions/index.ts @@ -9,7 +9,7 @@ export { lsTool } from "./ls"; export { multiEditTool } from "./multiEdit"; export { readCurrentlyOpenFileTool } from "./readCurrentlyOpenFile"; export { readFileTool } from "./readFile"; -export { readFileHeadTool } from "./readFileHead"; + export { readFileRangeTool } from "./readFileRange"; export { requestRuleTool } from "./requestRule"; export { runTerminalCommandTool } from "./runTerminalCommand"; @@ -19,3 +19,4 @@ export { singleFindAndReplaceTool } from "./singleFindAndReplace"; export { viewDiffTool } from "./viewDiff"; export { viewRepoMapTool } from "./viewRepoMap"; export { viewSubdirectoryTool } from "./viewSubdirectory"; + diff --git a/core/tools/definitions/readFileHead.ts b/core/tools/definitions/readFileHead.ts deleted file mode 100644 index e14edc749..000000000 --- a/core/tools/definitions/readFileHead.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { Tool } from "../.."; -import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; - -export const readFileHeadTool: Tool = { - type: "function", - displayTitle: "Read File Head", - wouldLikeTo: "read the first {{{ lines }}} lines of {{{ filepath }}}", - isCurrently: "reading the first {{{ lines }}} lines of {{{ filepath }}}", - hasAlready: "viewed the first {{{ lines }}} lines of {{{ filepath }}}", - readonly: true, - isInstant: true, - group: BUILT_IN_GROUP_NAME, - function: { - name: BuiltInToolNames.ReadFileHead, - description: - "Use this tool to read the first N lines from the beginning of a file. Similar to the Unix 'head' command.", - parameters: { - type: "object", - required: ["filepath", "lines"], - properties: { - filepath: { - type: "string", - description: - "The path of the file to read, relative to the root of the workspace (NOT uri or absolute path)", - }, - lines: { - type: "number", - description: - "The number of lines to read from the beginning of the file (must be positive)", - }, - }, - }, - }, - systemMessageDescription: { - prefix: `To read the first N lines of a file, use the ${BuiltInToolNames.ReadFileHead} tool. For example, to read the first 20 lines of a file:`, - exampleArgs: [ - ["filepath", "path/to/the_file.txt"], - ["lines", "20"], - ], - }, - defaultToolPolicy: "allowedWithoutPermission", -}; diff --git a/core/tools/implementations/readFileHead.integration.vitest.ts b/core/tools/implementations/readFileHead.integration.vitest.ts deleted file mode 100644 index 554a2abbd..000000000 --- a/core/tools/implementations/readFileHead.integration.vitest.ts +++ /dev/null @@ -1,111 +0,0 @@ -import { expect, test, vi } from "vitest"; -import { ToolExtras } from "../.."; -import { readFileHeadImpl } from "./readFileHead"; - -// Mock the dependencies -vi.mock("../../util/ideUtils", () => ({ - resolveRelativePathInDir: vi.fn(), -})); - -vi.mock("../../util/uri", () => ({ - getUriPathBasename: vi.fn(), -})); - -vi.mock("./readFileLimit", () => ({ - throwIfFileExceedsHalfOfContext: vi.fn(), -})); - -test("readFileHeadImpl reads first N lines correctly", async () => { - const { resolveRelativePathInDir } = await import("../../util/ideUtils"); - const { getUriPathBasename } = await import("../../util/uri"); - const { throwIfFileExceedsHalfOfContext } = await import("./readFileLimit"); - - vi.mocked(resolveRelativePathInDir).mockResolvedValue("file:///test.txt"); - vi.mocked(getUriPathBasename).mockReturnValue("test.txt"); - vi.mocked(throwIfFileExceedsHalfOfContext).mockResolvedValue(undefined); - - const expectedContent = "line1\nline2\nline3"; - const mockIde = { - readRangeInFile: vi.fn().mockResolvedValue(expectedContent), - }; - - const mockExtras = { - ide: mockIde, - config: { selectedModelByRole: { chat: { contextLength: 8192 } } }, - } as unknown as ToolExtras; - - const result = await readFileHeadImpl( - { - filepath: "src/test.py", - lines: 3, - }, - mockExtras, - ); - - expect(result).toHaveLength(1); - expect(result[0]).toEqual({ - name: "test.txt", - description: "src/test.py (first 3 lines)", - content: expectedContent, - uri: { - type: "file", - value: "file:///test.txt", - }, - }); - - // Verify readRangeInFile was called with correct 0-based range - expect(mockIde.readRangeInFile).toHaveBeenCalledWith("file:///test.txt", { - start: { line: 0, character: 0 }, // First line (0-based) - end: { line: 2, character: Number.MAX_SAFE_INTEGER }, // 3rd line (0-based) - }); -}); - -test("readFileHeadImpl validates lines parameter", async () => { - const mockExtras = { - ide: { readRangeInFile: vi.fn() }, - config: { selectedModelByRole: { chat: { contextLength: 8192 } } }, - } as unknown as ToolExtras; - - // Test lines < 1 - await expect( - readFileHeadImpl( - { - filepath: "test.txt", - lines: 0, - }, - mockExtras, - ), - ).rejects.toThrow("lines must be 1 or greater"); - - await expect( - readFileHeadImpl( - { - filepath: "test.txt", - lines: -5, - }, - mockExtras, - ), - ).rejects.toThrow("lines must be 1 or greater"); -}); - -test("readFileHead handles file not found errors", async () => { - const { resolveRelativePathInDir } = await import("../../util/ideUtils"); - - vi.mocked(resolveRelativePathInDir).mockResolvedValue(undefined); - - const mockExtras = { - ide: { readFile: vi.fn(), readRangeInFile: vi.fn() }, - config: { selectedModelByRole: { chat: { contextLength: 8192 } } }, - } as unknown as ToolExtras; - - // Test readFileHead with non-existent file - await expect( - readFileHeadImpl( - { - filepath: "nonexistent.txt", - lines: 5, - }, - mockExtras, - ), - ).rejects.toThrow('File "nonexistent.txt" does not exist'); -}); diff --git a/core/tools/implementations/readFileHead.ts b/core/tools/implementations/readFileHead.ts deleted file mode 100644 index aed12d2c2..000000000 --- a/core/tools/implementations/readFileHead.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { resolveRelativePathInDir } from "../../util/ideUtils"; -import { getUriPathBasename } from "../../util/uri"; - -import { ToolImpl } from "."; -import { getNumberArg, getStringArg } from "../parseArgs"; -import { throwIfFileExceedsHalfOfContext } from "./readFileLimit"; - -export const readFileHeadImpl: ToolImpl = async (args, extras) => { - const filepath = getStringArg(args, "filepath"); - const lines = getNumberArg(args, "lines"); - - if (lines < 1) { - throw new Error("lines must be 1 or greater"); - } - - const firstUriMatch = await resolveRelativePathInDir(filepath, extras.ide); - if (!firstUriMatch) { - throw new Error( - `File "${filepath}" does not exist. You might want to check the path and try again.`, - ); - } - - // Use the IDE's readRangeInFile method to read first N lines - // Lines 1 to N (converted to 0-based: 0 to N-1) - const content = await extras.ide.readRangeInFile(firstUriMatch, { - start: { - line: 0, // First line (0-based) - character: 0, - }, - end: { - line: lines - 1, // Nth line (0-based) - character: Number.MAX_SAFE_INTEGER, // Read to end of line - }, - }); - - await throwIfFileExceedsHalfOfContext( - filepath, - content, - extras.config.selectedModelByRole.chat, - ); - - return [ - { - name: getUriPathBasename(firstUriMatch), - description: `${filepath} (first ${lines} lines)`, - content, - uri: { - type: "file", - value: firstUriMatch, - }, - }, - ]; -}; diff --git a/core/tools/implementations/readFileHead.vitest.ts b/core/tools/implementations/readFileHead.vitest.ts deleted file mode 100644 index e3ab3b163..000000000 --- a/core/tools/implementations/readFileHead.vitest.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { test, expect, vi } from "vitest"; -import { getNumberArg } from "../parseArgs"; - -test("readFileHead validates input arguments", () => { - // Test valid positive numbers - expect(() => getNumberArg({ lines: 1 }, "lines")).not.toThrow(); - expect(() => getNumberArg({ lines: 10 }, "lines")).not.toThrow(); - expect(() => getNumberArg({ lines: "5" }, "lines")).not.toThrow(); - - // Test missing arguments - expect(() => getNumberArg({}, "lines")).toThrow( - "Argument `lines` is required (type number)", - ); - - // Test invalid arguments - expect(() => getNumberArg({ lines: "not-a-number" }, "lines")).toThrow( - "Argument `lines` must be a valid number", - ); - expect(() => getNumberArg({ lines: true }, "lines")).toThrow( - "Argument `lines` must be a valid number", - ); -}); - -test("readFileHead handles line number conversion correctly", () => { - // Test that parsing works for various inputs - expect(getNumberArg({ lines: 1 }, "lines")).toBe(1); - expect(getNumberArg({ lines: "10" }, "lines")).toBe(10); - expect(getNumberArg({ lines: 5.7 }, "lines")).toBe(5); // Should floor - expect(getNumberArg({ lines: "3.9" }, "lines")).toBe(3); // Should floor - - // Test that negative numbers are parsed (validation happens in implementation) - expect(getNumberArg({ lines: -5 }, "lines")).toBe(-5); -}); diff --git a/core/tools/index.ts b/core/tools/index.ts index e63aee0de..27b683913 100644 --- a/core/tools/index.ts +++ b/core/tools/index.ts @@ -4,8 +4,7 @@ import * as toolDefinitions from "./definitions"; // I'm writing these as functions because we've messed up 3 TIMES by pushing to const, causing duplicate tool definitions on subsequent config loads. export const getBaseToolDefinitions = () => [ toolDefinitions.readFileTool, - toolDefinitions.readFileRangeTool, - toolDefinitions.readFileHeadTool, + toolDefinitions.createNewFileTool, toolDefinitions.runTerminalCommandTool, toolDefinitions.globSearchTool, @@ -35,6 +34,7 @@ export const getConfigDependentToolDefinitions = ( toolDefinitions.viewRepoMapTool, toolDefinitions.viewSubdirectoryTool, toolDefinitions.codebaseTool, + toolDefinitions.readFileRangeTool, ); }