limit ls tool output and add tests

This commit is contained in:
Dallin Romney 2025-07-03 02:46:28 -07:00
parent 7f756d7590
commit 2b1997e02f
2 changed files with 120 additions and 8 deletions

View file

@ -2,29 +2,51 @@ import { ToolImpl } from ".";
import { walkDir } from "../../indexing/walkDir";
import { resolveRelativePathInDir } from "../../util/ideUtils";
export function resolveLsToolDirPath(dirPath: string | undefined) {
if (!dirPath || dirPath === ".") {
return "/";
}
if (dirPath.startsWith(".")) {
return dirPath.slice(1);
}
return dirPath.replace(/\\/g, "/");
}
const MAX_LS_TOOL_LINES = 200;
export const lsToolImpl: ToolImpl = async (args, extras) => {
const uri = await resolveRelativePathInDir(args.dirPath, extras.ide);
const dirPath = resolveLsToolDirPath(args?.dirPath);
const uri = await resolveRelativePathInDir(dirPath, extras.ide);
if (!uri) {
throw new Error(
`Directory ${args.dirPath} not found. Make sure to use forward-slash paths. Do not use e.g. "."`,
`Directory ${args.dirPath} not found. Make sure to use forward-slash paths`,
);
}
const entries = await walkDir(uri, extras.ide, {
returnRelativeUrisPaths: true,
include: "both",
recursive: args.recursive ?? false,
recursive: args?.recursive ?? false,
});
const content =
entries.length > 0
? entries.join("\n")
: `No files/folders found in ${args.dirPath}`;
const lines = entries.slice(0, MAX_LS_TOOL_LINES);
let content =
lines.length > 0
? lines.join("\n")
: `No files/folders found in ${dirPath}`;
if (entries.length > MAX_LS_TOOL_LINES) {
content += `\n... ${entries.length - MAX_LS_TOOL_LINES} entries were truncated`;
if (args?.recursive) {
content += ". Try using a non-recursive search";
}
}
return [
{
name: "File/folder list",
description: `Files/folders in ${args.dirPath}`,
description: `Files/folders in ${dirPath}`,
content,
},
];

View file

@ -0,0 +1,90 @@
import { expect, test, vi } from "vitest";
import { ToolExtras } from "../..";
import * as walkDirModule from "../../indexing/walkDir";
import { lsToolImpl, resolveLsToolDirPath } from "./lsTool";
vi.mock("../../indexing/walkDir");
const mockExtras = {
ide: {
fileExists: async (path: string) => true,
getWorkspaceDirs: async () => ["dir1"],
},
} as unknown as ToolExtras;
test("resolveLsToolDirPath handles undefined path", () => {
expect(resolveLsToolDirPath(undefined)).toBe("/");
});
test("resolveLsToolDirPath handles empty string", () => {
expect(resolveLsToolDirPath("")).toBe("/");
});
test("resolveLsToolDirPath handles dot", () => {
expect(resolveLsToolDirPath(".")).toBe("/");
});
test("resolveLsToolDirPath handles dot relative", () => {
expect(resolveLsToolDirPath("./hi")).toBe("/hi");
});
test("resolveLsToolDirPath normalizes backslashes to forward slashes", () => {
expect(resolveLsToolDirPath("path\\to\\dir")).toBe("path/to/dir");
});
test("resolveLsToolDirPath preserves forward slashes", () => {
expect(resolveLsToolDirPath("path/to/dir")).toBe("path/to/dir");
});
test("lsToolImpl truncates output when entries exceed MAX_LS_TOOL_LINES", async () => {
// Generate more than MAX_LS_TOOL_LINES entries (which is 200)
const mockEntries = Array.from({ length: 250 }, (_, i) => `file${i}.txt`);
// Mock walkDir to return our large array
vi.spyOn(walkDirModule, "walkDir").mockResolvedValue(mockEntries);
const result = await lsToolImpl(
{ dirPath: "testDir", recursive: true },
mockExtras,
);
// Check that the result contains the truncation message
expect(result[0].content).toContain("... 50 entries were truncated");
// Check that only MAX_LS_TOOL_LINES entries are included
const contentLines = result[0].content.split("\n");
// Account for the truncation message line
expect(contentLines.length).toBe(201);
// Check the suggestion to use non-recursive search is included
expect(result[0].content).toContain("Try using a non-recursive search");
});
test("lsToolImpl shows truncation message without suggestion for non-recursive search", async () => {
// Generate more than MAX_LS_TOOL_LINES entries
const mockEntries = Array.from({ length: 250 }, (_, i) => `file${i}.txt`);
// Mock walkDir to return our large array
vi.spyOn(walkDirModule, "walkDir").mockResolvedValue(mockEntries);
const result = await lsToolImpl(
{ dirPath: "testDir", recursive: false },
mockExtras,
);
// Check that the result contains the truncation message
expect(result[0].content).toContain("... 50 entries were truncated");
// Check that the suggestion to use non-recursive search is NOT included
expect(result[0].content).not.toContain("Try using a non-recursive search");
});
test("lsToolImpl shows message when no files are found", async () => {
// Mock walkDir to return empty array
vi.spyOn(walkDirModule, "walkDir").mockResolvedValue([]);
const result = await lsToolImpl({ dirPath: "emptyDir" }, mockExtras);
// Check that the result contains the "no files found" message
expect(result[0].content).toBe("No files/folders found in emptyDir");
});