import { projectRef } from "@/lib/projects"; import { refuseCaller } from "@/lib/report/caller"; import { lintCommand } from "@/lib/report/lintCli.mjs"; import { takeCommand } from "@/lib/report/takeCli.mjs"; import { timelineCommand } from "@/lib/report/timelineCli.mjs"; import { ImportRefused, draftImportArgv } from "@/lib/report/takeImport.mjs"; import { isTakeId, listTakes } from "@/lib/report/takes.mjs"; import { REPORTS_ROOT } from "@/lib/paths"; import { addStep } from "@/lib/queue/store.mjs"; import { kickQueue, queueDir } from "@/lib/queue/runner"; export const dynamic = "force-dynamic"; // THE TAKE TOOLS a person had only from a terminal (slice C of "the apps do the // work", 4 Nov), on a take's card and its walk (lane/ui-simplify, 5 Nov). Each // runs the CLI's own function in this process -- never a shelled-out CLI -- with // --json, and answers with what it printed: // // POST { project, take?, op: "lint" } umtool lint

[--take ] --json // -> { exit, report: { ok, errors[], warnings[], entries } } // POST { project, take, op: "draft-from", id?, label? } umtool take

--id --draft --from --json // -> { exit, id, report } (id: the next free `-d` when none is given) // POST { project, op: "import", refs: ":[,…]", after?, before?, dryRun? } // umtool timeline

import-item … [--dry-run] --json: INTO // -> { exit, report } THE DRAFT (the Cut tab's Add items from another project; L3: // every edit lands in the draft). With `take`: the old take // patch path, umtool take

--id --import-item … // POST { project, op: "draft-new", label, group?, style?, patch?, window? } // umtool take

--id --label L --draft // -> { exit, id, report } [--group G] [--style S] [--patch-json P] [--full | --at A --dur D] // POST { project, take?, op: "queue-refetch", clip } QUEUED: umtool fetch

--refetch [--take ] (its // -> { exit: 0, queued: } cached windows evicted through the editor, then fetched again) // POST { project, take, op: "queue-check-joins" } QUEUED: umtool check-joins

--take (the measured // -> { exit: 0, queued: } SSIM over the master is long: a queue step, its table in // the step's log on Jobs) export async function POST(request: Request) { const refused = refuseCaller(request, "/api/report/takes/tools"); if (refused) return refused; const body = (await request.json().catch(() => null)) as Record | null; if (!body || typeof body !== "object") return Response.json({ error: "bad json" }, { status: 400 }); const project = await projectRef(String(body.project ?? "")); if (!project) return Response.json({ error: "no such project" }, { status: 404 }); const take = body.take === undefined || body.take === null || body.take === "" ? null : body.take; if (take !== null && !isTakeId(take)) return Response.json({ error: "take must be a take id" }, { status: 400 }); const out: string[] = []; const err: string[] = []; const io = { reportsRoot: REPORTS_ROOT, log: (s: string) => out.push(s), err: (s: string) => err.push(s) }; const answer = (exit: number, extra: Record = {}) => { let report: unknown = null; try { report = out.length ? JSON.parse(out.join("\n")) : null; } catch { report = null; } const status = exit === 2 ? 400 : 200; return Response.json({ exit, report, said: [...err, ...(report === null ? out : [])].join("\n"), ...extra }, { status, headers: { "cache-control": "no-store" } }); }; try { if (body.op === "lint") { const exit = await lintCommand([project.id, ...(take ? ["--take", take] : []), "--json"], io); return answer(exit); } if (body.op === "draft-new") { // A draft from the New take form: recorded, nothing rendered (`umtool take … --draft`), walked before it renders. const label = typeof body.label === "string" ? body.label.replace(/\s+/g, " ").trim() : ""; if (!label) return Response.json({ error: "a draft needs a label" }, { status: 400 }); const have = new Set(((await listTakes(project.dir)).takes as { id: string }[]).map((t) => t.id)); const base = label.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 40) || "draft"; let id = base; for (let n = 2; have.has(id) && n < 1000; n++) id = `${base}-${n}`; if (!isTakeId(id)) return Response.json({ error: `"${id}" is not a take id` }, { status: 400 }); const w = body.window as { at?: number; dur?: number } | "full" | undefined; const args = [project.id, "--id", id, "--label", label, "--draft", "--json"]; if (typeof body.group === "string" && body.group) args.push("--group", body.group); if (typeof body.style === "string" && body.style) args.push("--style", body.style); if (body.patch && typeof body.patch === "object") args.push("--patch-json", JSON.stringify(body.patch)); if (w === "full") args.push("--full"); else if (w && typeof w === "object" && Number.isFinite(w.at) && Number.isFinite(w.dur)) args.push("--at", String(w.at), "--dur", String(w.dur)); const exit = await takeCommand(args, io); return answer(exit, { id }); } if (body.op === "queue-refetch") { const clip = typeof body.clip === "string" ? body.clip : ""; if (!/^[A-Za-z0-9._-]{1,80}$/.test(clip)) return Response.json({ error: "queue-refetch needs a clip id" }, { status: 400 }); const step = (await addStep(queueDir(), { argv: ["fetch", project.id, "--refetch", clip, ...(take ? ["--take", take] : [])], label: `fetch ${clip}'s window again (${project.id}${take ? ` take ${take}` : ""})`, target: { project: project.id, take }, by: "the umtool app (the clip bench)", })) as { qid: string }; kickQueue(); return Response.json({ exit: 0, queued: step.qid }, { headers: { "cache-control": "no-store" } }); } if (body.op === "import" && !take) { // Every word checked before it becomes argv (draftImportArgv): no flag rides in on the refs. let argv: string[]; try { argv = draftImportArgv(project.id, { refs: body.refs, after: body.after, before: body.before, dryRun: !!body.dryRun }); } catch (e) { if (e instanceof ImportRefused) return Response.json({ error: e.message }, { status: 400 }); throw e; } const exit = await timelineCommand(argv, { root: REPORTS_ROOT, out: io.log, err: io.err }); return answer(exit); } if (!take) return Response.json({ error: `${String(body.op)} needs a take` }, { status: 400 }); if (body.op === "draft-from") { let id = typeof body.id === "string" && body.id ? body.id : null; if (!id) { const have = new Set(((await listTakes(project.dir)).takes as { id: string }[]).map((t) => t.id)); for (let n = 1; n < 1000 && !id; n++) if (!have.has(`${take}-d${n}`)) id = `${take}-d${n}`; } if (!id || !isTakeId(id)) return Response.json({ error: "no free draft id" }, { status: 400 }); const label = typeof body.label === "string" && body.label.trim() ? ["--label", body.label.trim()] : []; const exit = await takeCommand([project.id, "--id", id, "--draft", "--from", take, ...label, "--json"], io); return answer(exit, { id }); } if (body.op === "queue-check-joins") { const step = (await addStep(queueDir(), { argv: ["check-joins", project.id, "--take", take], label: `check the joins of ${project.id} take ${take}`, target: { project: project.id, take }, by: "the umtool app (take tools)", })) as { qid: string }; kickQueue(); return Response.json({ exit: 0, queued: step.qid }, { headers: { "cache-control": "no-store" } }); } if (body.op === "import") { const refs = typeof body.refs === "string" ? body.refs.trim() : ""; if (!refs) return Response.json({ error: "import needs :[,…]" }, { status: 400 }); const place = typeof body.after === "string" && body.after ? ["--after", body.after] : typeof body.before === "string" && body.before ? ["--before", body.before] : []; const exit = await takeCommand([project.id, "--id", take, ...refs.split(/\s+/).flatMap((r) => ["--import-item", r]), ...place, ...(body.dryRun ? ["--dry-run"] : []), "--json"], io); return answer(exit); } return Response.json({ error: "op must be lint, draft-from, draft-new, import or queue-check-joins" }, { status: 400 }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 500 }); } }