(args: z.infer<typeof ArgsSchema>, ctx: ToolContext)
| 19 | argsSchema = ArgsSchema; |
| 20 | |
| 21 | async execute(args: z.infer<typeof ArgsSchema>, ctx: ToolContext): Promise<ToolResult> { |
| 22 | const target = args.path |
| 23 | ? (path.isAbsolute(args.path) ? args.path : path.resolve(ctx.cwd, args.path)) |
| 24 | : ctx.cwd; |
| 25 | |
| 26 | let entries: any[]; |
| 27 | try { |
| 28 | entries = await fs.readdir(target, { withFileTypes: true }); |
| 29 | } catch (e: any) { |
| 30 | if (e.code === 'ENOENT') { |
| 31 | return { content: `[NOT_FOUND] Directory ${target} does not exist.${outsideCwdHint(args.path ?? '', target, ctx.cwd)}`, isError: true }; |
| 32 | } |
| 33 | if (e.code === 'ENOTDIR') { |
| 34 | return { content: `[ERROR] ${target} is not a directory. Use read_file instead.`, isError: true }; |
| 35 | } |
| 36 | return { content: `[ERROR] ${e.message}`, isError: true }; |
| 37 | } |
| 38 | |
| 39 | const visible = entries.filter(e => { |
| 40 | if (!args.show_hidden && e.name.startsWith('.')) return false; |
| 41 | if (IGNORED.has(e.name)) return false; |
| 42 | return true; |
| 43 | }); |
| 44 | |
| 45 | visible.sort((a, b) => { |
| 46 | if (a.isDirectory() !== b.isDirectory()) return a.isDirectory() ? -1 : 1; |
| 47 | return a.name.localeCompare(b.name); |
| 48 | }); |
| 49 | |
| 50 | const lines: string[] = [`Contents of ${target}:`]; |
| 51 | for (const entry of visible) { |
| 52 | if (entry.isDirectory()) { |
| 53 | lines.push(` 📁 ${entry.name}/`); |
| 54 | } else { |
| 55 | try { |
| 56 | const stat = await fs.stat(path.join(target, entry.name)); |
| 57 | const size = stat.size < 1024 ? `${stat.size}B` |
| 58 | : stat.size < 1024 * 1024 ? `${(stat.size / 1024).toFixed(1)}K` |
| 59 | : `${(stat.size / 1024 / 1024).toFixed(1)}M`; |
| 60 | lines.push(` 📄 ${entry.name} (${size})`); |
| 61 | } catch { |
| 62 | lines.push(` 📄 ${entry.name}`); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if (visible.length === 0) { |
| 68 | lines.push(' (empty)'); |
| 69 | } else { |
| 70 | const hidden = entries.length - visible.length; |
| 71 | if (hidden > 0) lines.push(`\n ... ${hidden} hidden/ignored entries`); |
| 72 | } |
| 73 | |
| 74 | return { content: lines.join('\n'), metadata: { count: visible.length } }; |
| 75 | } |
| 76 | } |
nothing calls this directly
no test coverage detected