Elffuss BraidHub

kikocis/continue public

main · 1.6 KiBRawPermalink
1import ignore from "ignore";
2
3import { ToolImpl } from ".";
4import { walkDir } from "../../indexing/walkDir";
5import { resolveRelativePathInDir } from "../../util/ideUtils";
6
7export function resolveLsToolDirPath(dirPath: string | undefined) {
8 if (!dirPath || dirPath === ".") {
9 return "/";
10 }
11 if (dirPath.startsWith(".")) {
12 return dirPath.slice(1);
13 }
14 return dirPath.replace(/\\/g, "/");
15}
16
17const MAX_LS_TOOL_LINES = 200;
18
19export const lsToolImpl: ToolImpl = async (args, extras) => {
20 const dirPath = resolveLsToolDirPath(args?.dirPath);
21 const uri = await resolveRelativePathInDir(dirPath, extras.ide);
22 if (!uri) {
23 throw new Error(
24 `Directory ${args.dirPath} not found. Make sure to use forward-slash paths`,
25 );
26 }
27
28 const entries = await walkDir(uri, extras.ide, {
29 returnRelativeUrisPaths: true,
30 include: "both",
31 recursive: args?.recursive ?? false,
32 overrideDefaultIgnores: ignore(), // Show all directories including dist/, build/, etc.
33 });
34
35 const lines = entries.slice(0, MAX_LS_TOOL_LINES);
36
37 let content =
38 lines.length > 0
39 ? lines.join("\n")
40 : `No files/folders found in ${dirPath}`;
41
42 const contextItems = [
43 {
44 name: "File/folder list",
45 description: `Files/folders in ${dirPath}`,
46 content,
47 },
48 ];
49
50 if (entries.length > MAX_LS_TOOL_LINES) {
51 let warningContent = `${entries.length - MAX_LS_TOOL_LINES} ls entries were truncated`;
52 if (args?.recursive) {
53 warningContent += ". Try using a non-recursive search";
54 }
55 contextItems.push({
56 name: "Truncation warning",
57 description: "",
58 content: warningContent,
59 });
60 }
61
62 return contextItems;
63};