|
| 1 | +import * as path from "@std/path"; |
| 2 | +import { defineDeck } from "jsr:@bolt-foundry/gambit"; |
| 3 | +import { z } from "npm:zod"; |
| 4 | +import { resolveBotPath } from "./bot_fs.ts"; |
| 5 | + |
| 6 | +const MAX_RECURSION_DEPTH = 6; |
| 7 | + |
| 8 | +type BotListEntry = { |
| 9 | + path: string; |
| 10 | + type: "file" | "dir"; |
| 11 | + size?: number; |
| 12 | + modifiedAt?: string; |
| 13 | +}; |
| 14 | + |
| 15 | +export default defineDeck({ |
| 16 | + label: "bot_list", |
| 17 | + contextSchema: z.object({ |
| 18 | + path: z.string().default(".").describe( |
| 19 | + "Relative directory path under the bot root. Use '.' for root.", |
| 20 | + ), |
| 21 | + recursive: z.boolean().default(false).describe( |
| 22 | + "When true, include nested entries.", |
| 23 | + ), |
| 24 | + maxDepth: z.number().int().min(1).max(MAX_RECURSION_DEPTH).default(2) |
| 25 | + .describe( |
| 26 | + "Max nested directory depth when recursive is true.", |
| 27 | + ), |
| 28 | + }), |
| 29 | + responseSchema: z.object({ |
| 30 | + status: z.number().optional(), |
| 31 | + message: z.string().optional(), |
| 32 | + payload: z.object({ |
| 33 | + path: z.string(), |
| 34 | + recursive: z.boolean(), |
| 35 | + maxDepth: z.number().int(), |
| 36 | + entries: z.array(z.object({ |
| 37 | + path: z.string(), |
| 38 | + type: z.enum(["file", "dir"]), |
| 39 | + size: z.number().optional(), |
| 40 | + modifiedAt: z.string().optional(), |
| 41 | + })), |
| 42 | + }).optional(), |
| 43 | + }), |
| 44 | + async run(ctx) { |
| 45 | + let resolved; |
| 46 | + try { |
| 47 | + resolved = await resolveBotPath(ctx.input.path || "."); |
| 48 | + } catch (err) { |
| 49 | + return { |
| 50 | + status: 400, |
| 51 | + message: err instanceof Error ? err.message : String(err), |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + let rootStat: Deno.FileInfo; |
| 56 | + try { |
| 57 | + rootStat = await Deno.stat(resolved.fullPath); |
| 58 | + } catch (err) { |
| 59 | + if (err instanceof Deno.errors.NotFound) { |
| 60 | + return { status: 404, message: "path not found" }; |
| 61 | + } |
| 62 | + return { |
| 63 | + status: 500, |
| 64 | + message: err instanceof Error ? err.message : String(err), |
| 65 | + }; |
| 66 | + } |
| 67 | + if (!rootStat.isDirectory) { |
| 68 | + return { status: 409, message: "path is not a directory" }; |
| 69 | + } |
| 70 | + |
| 71 | + const recursive = Boolean(ctx.input.recursive); |
| 72 | + const maxDepth = recursive |
| 73 | + ? Math.min( |
| 74 | + Math.max(1, Math.trunc(ctx.input.maxDepth ?? 2)), |
| 75 | + MAX_RECURSION_DEPTH, |
| 76 | + ) |
| 77 | + : 1; |
| 78 | + const entries: Array<BotListEntry> = []; |
| 79 | + const baseRelativePath = normalizeRelativePath(resolved.relativePath); |
| 80 | + const walk = async (dir: string, prefix: string, depth: number) => { |
| 81 | + const dirEntries = []; |
| 82 | + for await (const entry of Deno.readDir(dir)) { |
| 83 | + if (entry.isSymlink) continue; |
| 84 | + dirEntries.push(entry); |
| 85 | + } |
| 86 | + dirEntries.sort((a, b) => a.name.localeCompare(b.name)); |
| 87 | + for (const entry of dirEntries) { |
| 88 | + const fullPath = path.join(dir, entry.name); |
| 89 | + const relativePath = prefix === "." |
| 90 | + ? entry.name |
| 91 | + : path.join(prefix, entry.name); |
| 92 | + if (entry.isDirectory) { |
| 93 | + entries.push({ path: relativePath, type: "dir" }); |
| 94 | + if (recursive && depth < maxDepth) { |
| 95 | + await walk(fullPath, relativePath, depth + 1); |
| 96 | + } |
| 97 | + continue; |
| 98 | + } |
| 99 | + if (entry.isFile) { |
| 100 | + const info = await Deno.stat(fullPath); |
| 101 | + entries.push({ |
| 102 | + path: relativePath, |
| 103 | + type: "file", |
| 104 | + size: info.size, |
| 105 | + modifiedAt: info.mtime ? info.mtime.toISOString() : undefined, |
| 106 | + }); |
| 107 | + } |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + try { |
| 112 | + await walk(resolved.fullPath, baseRelativePath, 0); |
| 113 | + } catch (err) { |
| 114 | + return { |
| 115 | + status: 500, |
| 116 | + message: err instanceof Error ? err.message : String(err), |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + return { |
| 121 | + status: 200, |
| 122 | + payload: { |
| 123 | + path: baseRelativePath, |
| 124 | + recursive, |
| 125 | + maxDepth, |
| 126 | + entries, |
| 127 | + }, |
| 128 | + }; |
| 129 | + }, |
| 130 | +}); |
| 131 | + |
| 132 | +function normalizeRelativePath(relativePath: string): string { |
| 133 | + if (!relativePath || relativePath === ".") return "."; |
| 134 | + return relativePath.replaceAll("\\", "/"); |
| 135 | +} |
0 commit comments