Elffuss BraidHub

kikocis/continue public

93a8eb04e36c · 1.6 KiBRawPermalink
1import { ToolImpl } from ".";
2import { walkDir } from "../../indexing/walkDir";
3import { resolveRelativePathInDir } from "../../util/ideUtils";
4
5export 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
15const MAX_LS_TOOL_LINES = 200;
16
17export const lsToolImpl: ToolImpl = async (args, extras) => {
18 const dirPath = resolveLsToolDirPath(args?.dirPath);
19 const uri = await resolveRelativePathInDir(dirPath, extras.ide);
20 if (!uri) {
21 throw new Error(
22 `Directory ${args.dirPath} not found. Make sure to use forward-slash paths`,
23 );
24 }
25
26 const entries = await walkDir(uri, extras.ide, {
27 returnRelativeUrisPaths: true,
28 include: "both",
29 recursive: args?.recursive ?? false,
30 });
31
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 const contextItems = [
40 {
41 name: "File/folder list",
42 description: `Files/folders in ${dirPath}`,
43 content,
44 },
45 ];
46
47 if (entries.length > MAX_LS_TOOL_LINES) {
48 let warningContent = `${entries.length - MAX_LS_TOOL_LINES} ls entries were truncated`;
49 if (args?.recursive) {
50 warningContent += ". Try using a non-recursive search";
51 }
52 contextItems.push({
53 name: "ls truncation warning",
54 description: "Informs the model that ls results were truncated",
55 content: warningContent,
56 });
57 }
58
59 return contextItems;
60};