scripts/digest.ts
scripts/digest.tsBrowse 3 files
386 tokens
1,455 bytes
Token encoding: o200k_base
Snapshot 16b6856
← Back to SKILL.md
1import type { SkillRunContext } from "@cloudflare/think";2 3type WorkspaceEntry = { path: string; type: string; size: number };4 5/**6 * Function-style skill script (`export default run(input, ctx)`). Reads the7 * assistant's shared workspace through `ctx.workspace` (read-only) and a8 * bundled formatting hint through `ctx.files`, then returns a compact digest.9 */10export default async function run(input: unknown, ctx: SkillRunContext) {11 const dir =12 typeof input === "object" &&13 input !== null &&14 typeof (input as { dir?: unknown }).dir === "string"15 ? (input as { dir: string }).dir16 : "/";17 18 const pattern = dir === "/" ? "**/*" : `${dir.replace(/\/$/, "")}/**/*`;19 const entries = ((await ctx.workspace.glob(pattern).catch(() => [])) ??20 []) as WorkspaceEntry[];21 const files = entries.filter((entry) => entry.type === "file");22 23 const totalBytes = files.reduce((sum, file) => sum + (file.size ?? 0), 0);24 const listing = [...files]25 .sort((a, b) => (b.size ?? 0) - (a.size ?? 0))26 .slice(0, 25)27 .map((file) => `- ${file.path} (${file.size ?? 0} bytes)`);28 29 const formatHint = (ctx.files["references/format.md"] ?? "").trim();30 31 return [32 "# Workspace digest",33 "",34 `${files.length} file(s), ${totalBytes} bytes total under ${dir}.`,35 "",36 ...(listing.length ? listing : ["- (workspace is empty)"]),37 ...(formatHint ? ["", "<!-- formatting hint -->", formatHint] : [])38 ].join("\n");39}40