Changesets · Branches · Tags · Compare
| Changeset | 5232d029f7d644b17094ac3b2a08e556fba952dd5bfdad500a883e83077c38ae |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-07-03 02:46 -07:00 |
| Committer | Dallin Romney <dallinromney@gmail.com> · 2025-07-03 15:03 -07:00 |
| Parents | b2cd4c5fbf76 |
2 files changed
modified core/tools/implementations/lsTool.ts +30 −8
| @@ -2,29 +2,51 @@ | ||
| 2 | 2 | import { walkDir } from "../../indexing/walkDir"; |
| 3 | 3 | import { resolveRelativePathInDir } from "../../util/ideUtils"; |
| 4 | 4 | |
| 5 | +export function resolveLsToolDirPath(dirPath: string | undefined) { | |
| 6 | + if (!dirPath || dirPath === ".") { | |
| 7 | + return "/"; | |
| 8 | + } | |
| 9 | + if (dirPath.startsWith(".")) { | |
| 10 | + return dirPath.slice(1); | |
| 11 | + } | |
| 12 | + return dirPath.replace(/\\/g, "/"); | |
| 13 | +} | |
| 14 | + | |
| 15 | +const MAX_LS_TOOL_LINES = 200; | |
| 16 | + | |
| 5 | 17 | export const lsToolImpl: ToolImpl = async (args, extras) => { |
| 6 | - const uri = await resolveRelativePathInDir(args.dirPath, extras.ide); | |
| 18 | + const dirPath = resolveLsToolDirPath(args?.dirPath); | |
| 19 | + const uri = await resolveRelativePathInDir(dirPath, extras.ide); | |
| 7 | 20 | if (!uri) { |
| 8 | 21 | throw new Error( |
| 9 | - `Directory ${args.dirPath} not found. Make sure to use forward-slash paths. Do not use e.g. "."`, | |
| 22 | + `Directory ${args.dirPath} not found. Make sure to use forward-slash paths`, | |
| 10 | 23 | ); |
| 11 | 24 | } |
| 12 | 25 | |
| 13 | 26 | const entries = await walkDir(uri, extras.ide, { |
| 14 | 27 | returnRelativeUrisPaths: true, |
| 15 | 28 | include: "both", |
| 16 | - recursive: args.recursive ?? false, | |
| 29 | + recursive: args?.recursive ?? false, | |
| 17 | 30 | }); |
| 18 | 31 | |
| 19 | - const content = | |
| 20 | - entries.length > 0 | |
| 21 | - ? entries.join("\n") | |
| 22 | - : `No files/folders found in ${args.dirPath}`; | |
| 32 | + const lines = entries.slice(0, MAX_LS_TOOL_LINES); | |
| 33 | + | |
| 34 | + let content = | |
| 35 | + lines.length > 0 | |
| 36 | + ? lines.join("\n") | |
| 37 | + : `No files/folders found in ${dirPath}`; | |
| 38 | + | |
| 39 | + if (entries.length > MAX_LS_TOOL_LINES) { | |
| 40 | + content += `\n... ${entries.length - MAX_LS_TOOL_LINES} entries were truncated`; | |
| 41 | + if (args?.recursive) { | |
| 42 | + content += ". Try using a non-recursive search"; | |
| 43 | + } | |
| 44 | + } | |
| 23 | 45 | |
| 24 | 46 | return [ |
| 25 | 47 | { |
| 26 | 48 | name: "File/folder list", |
| 27 | - description: `Files/folders in ${args.dirPath}`, | |
| 49 | + description: `Files/folders in ${dirPath}`, | |
| 28 | 50 | content, |
| 29 | 51 | }, |
| 30 | 52 | ]; |
added core/tools/implementations/lsTool.vitest.ts +90 −0
| @@ -0,0 +1,90 @@ | ||
| 1 | +import { expect, test, vi } from "vitest"; | |
| 2 | +import { ToolExtras } from "../.."; | |
| 3 | +import * as walkDirModule from "../../indexing/walkDir"; | |
| 4 | +import { lsToolImpl, resolveLsToolDirPath } from "./lsTool"; | |
| 5 | + | |
| 6 | +vi.mock("../../indexing/walkDir"); | |
| 7 | + | |
| 8 | +const mockExtras = { | |
| 9 | + ide: { | |
| 10 | + fileExists: async (path: string) => true, | |
| 11 | + getWorkspaceDirs: async () => ["dir1"], | |
| 12 | + }, | |
| 13 | +} as unknown as ToolExtras; | |
| 14 | + | |
| 15 | +test("resolveLsToolDirPath handles undefined path", () => { | |
| 16 | + expect(resolveLsToolDirPath(undefined)).toBe("/"); | |
| 17 | +}); | |
| 18 | + | |
| 19 | +test("resolveLsToolDirPath handles empty string", () => { | |
| 20 | + expect(resolveLsToolDirPath("")).toBe("/"); | |
| 21 | +}); | |
| 22 | + | |
| 23 | +test("resolveLsToolDirPath handles dot", () => { | |
| 24 | + expect(resolveLsToolDirPath(".")).toBe("/"); | |
| 25 | +}); | |
| 26 | + | |
| 27 | +test("resolveLsToolDirPath handles dot relative", () => { | |
| 28 | + expect(resolveLsToolDirPath("./hi")).toBe("/hi"); | |
| 29 | +}); | |
| 30 | + | |
| 31 | +test("resolveLsToolDirPath normalizes backslashes to forward slashes", () => { | |
| 32 | + expect(resolveLsToolDirPath("path\\to\\dir")).toBe("path/to/dir"); | |
| 33 | +}); | |
| 34 | + | |
| 35 | +test("resolveLsToolDirPath preserves forward slashes", () => { | |
| 36 | + expect(resolveLsToolDirPath("path/to/dir")).toBe("path/to/dir"); | |
| 37 | +}); | |
| 38 | + | |
| 39 | +test("lsToolImpl truncates output when entries exceed MAX_LS_TOOL_LINES", async () => { | |
| 40 | + // Generate more than MAX_LS_TOOL_LINES entries (which is 200) | |
| 41 | + const mockEntries = Array.from({ length: 250 }, (_, i) => `file${i}.txt`); | |
| 42 | + | |
| 43 | + // Mock walkDir to return our large array | |
| 44 | + vi.spyOn(walkDirModule, "walkDir").mockResolvedValue(mockEntries); | |
| 45 | + | |
| 46 | + const result = await lsToolImpl( | |
| 47 | + { dirPath: "testDir", recursive: true }, | |
| 48 | + mockExtras, | |
| 49 | + ); | |
| 50 | + | |
| 51 | + // Check that the result contains the truncation message | |
| 52 | + expect(result[0].content).toContain("... 50 entries were truncated"); | |
| 53 | + | |
| 54 | + // Check that only MAX_LS_TOOL_LINES entries are included | |
| 55 | + const contentLines = result[0].content.split("\n"); | |
| 56 | + // Account for the truncation message line | |
| 57 | + expect(contentLines.length).toBe(201); | |
| 58 | + | |
| 59 | + // Check the suggestion to use non-recursive search is included | |
| 60 | + expect(result[0].content).toContain("Try using a non-recursive search"); | |
| 61 | +}); | |
| 62 | + | |
| 63 | +test("lsToolImpl shows truncation message without suggestion for non-recursive search", async () => { | |
| 64 | + // Generate more than MAX_LS_TOOL_LINES entries | |
| 65 | + const mockEntries = Array.from({ length: 250 }, (_, i) => `file${i}.txt`); | |
| 66 | + | |
| 67 | + // Mock walkDir to return our large array | |
| 68 | + vi.spyOn(walkDirModule, "walkDir").mockResolvedValue(mockEntries); | |
| 69 | + | |
| 70 | + const result = await lsToolImpl( | |
| 71 | + { dirPath: "testDir", recursive: false }, | |
| 72 | + mockExtras, | |
| 73 | + ); | |
| 74 | + | |
| 75 | + // Check that the result contains the truncation message | |
| 76 | + expect(result[0].content).toContain("... 50 entries were truncated"); | |
| 77 | + | |
| 78 | + // Check that the suggestion to use non-recursive search is NOT included | |
| 79 | + expect(result[0].content).not.toContain("Try using a non-recursive search"); | |
| 80 | +}); | |
| 81 | + | |
| 82 | +test("lsToolImpl shows message when no files are found", async () => { | |
| 83 | + // Mock walkDir to return empty array | |
| 84 | + vi.spyOn(walkDirModule, "walkDir").mockResolvedValue([]); | |
| 85 | + | |
| 86 | + const result = await lsToolImpl({ dirPath: "emptyDir" }, mockExtras); | |
| 87 | + | |
| 88 | + // Check that the result contains the "no files found" message | |
| 89 | + expect(result[0].content).toBe("No files/folders found in emptyDir"); | |
| 90 | +}); |