Elffuss BraidHub

kikocis/continue public

main · 1.9 KiBRawPermalink
1import * as fs from "fs";
2import * as path from "path";
3
4import { formatToolArgument } from "./formatters.js";
5import { Tool } from "./types.js";
6
7// List files in a directory
8export const listFilesTool: Tool = {
9 name: "List",
10 displayName: "List",
11 description: "List files in a directory",
12 parameters: {
13 type: "object",
14 required: ["dirpath"],
15 properties: {
16 dirpath: {
17 type: "string",
18 description: "The path to the directory to list",
19 },
20 },
21 },
22 readonly: true,
23 isBuiltIn: true,
24 preprocess: async (args) => {
25 // Resolve relative paths
26 const normalizedPath = path.normalize(args.dirpath);
27 const dirPath = path.resolve(process.cwd(), normalizedPath);
28
29 if (!fs.existsSync(dirPath)) {
30 throw new Error(`Directory does not exist: ${dirPath}`);
31 }
32
33 if (!fs.statSync(dirPath).isDirectory()) {
34 throw new Error(`Error: Path is not a directory: ${dirPath}`);
35 }
36
37 return {
38 args: {
39 dirpath: dirPath,
40 },
41 preview: [
42 {
43 type: "text",
44 content: dirPath
45 ? `Will list files in: ${formatToolArgument(dirPath)}`
46 : "Will list files in current directory",
47 },
48 ],
49 };
50 },
51 run: async (args: { dirpath: string }): Promise<string> => {
52 try {
53 const files = fs.readdirSync(args.dirpath);
54 const fileDetails = files.map((file) => {
55 const fullPath = path.join(args.dirpath, file);
56 const stats = fs.statSync(fullPath);
57 const type = stats.isDirectory() ? "directory" : "file";
58 const size = stats.isFile() ? `${stats.size} bytes` : "";
59 return `${file} (${type}${size ? `, ${size}` : ""})`;
60 });
61
62 return `Files in ${args.dirpath}:\n${fileDetails.join("\n")}`;
63 } catch (error) {
64 return `Error listing files: ${
65 error instanceof Error ? error.message : String(error)
66 }`;
67 }
68 },
69};