continue-custom/core/tools/implementations/lsTool.ts
Dallin Romney 1c5df69a3f
fix: grep escaped chars and empty results (#7323)
* fix: grep search escape and better truncation warnings

* fix: grep search tweaks

* fix: grep no results

* Fix: url test
2025-08-25 07:20:26 -07:00

63 lines
1.6 KiB
TypeScript

import ignore from "ignore";
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 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`,
);
}
const entries = await walkDir(uri, extras.ide, {
returnRelativeUrisPaths: true,
include: "both",
recursive: args?.recursive ?? false,
overrideDefaultIgnores: ignore(), // Show all directories including dist/, build/, etc.
});
const lines = entries.slice(0, MAX_LS_TOOL_LINES);
let content =
lines.length > 0
? lines.join("\n")
: `No files/folders found in ${dirPath}`;
const contextItems = [
{
name: "File/folder list",
description: `Files/folders in ${dirPath}`,
content,
},
];
if (entries.length > MAX_LS_TOOL_LINES) {
let warningContent = `${entries.length - MAX_LS_TOOL_LINES} ls entries were truncated`;
if (args?.recursive) {
warningContent += ". Try using a non-recursive search";
}
contextItems.push({
name: "Truncation warning",
description: "",
content: warningContent,
});
}
return contextItems;
};