import { projectRef } from "@/lib/projects"; import { benchPrepState, queueBenchPrep } from "@/lib/report/benchPrep.mjs"; import { isTakeId } from "@/lib/report/takes.mjs"; export const dynamic = "force-dynamic"; // PREPARING THE WALK (lib/report/benchPrep.mjs): what the clip bench makes on a // clip's first open -- a long recording's excerpt, the waveform -- made ahead, // by a runner in the background (`umtool prep --run`), one clip at a // time in the machine's heavy slot. The Cut tab's "Prepare clips for the walk". // // GET ?project=[&take=] ready / to prepare / failed / not fetched, and the runner when one is alive // POST { project, take?, clips? } queue them (all when none are named); returns at once // // `take`: a take's clips (the take's walk), its manifest as the walk reads it // (a draft's too), its own status and runner (`umtool prep

--take `). const headers = { "cache-control": "no-store" }; /** The take named, or null; an error string when it is not a take id. */ function takeOf(v: unknown): string | null | { error: string } { if (v === undefined || v === null || v === "") return null; const t = String(v); return isTakeId(t) ? t : { error: `"${t}" is not a take id` }; } export async function GET(request: Request) { const url = new URL(request.url); const project = await projectRef(url.searchParams.get("project") ?? ""); if (!project) return Response.json({ error: "no such project" }, { status: 404, headers }); const take = takeOf(url.searchParams.get("take")); if (take && typeof take === "object") return Response.json(take, { status: 400, headers }); try { const s = await benchPrepState({ id: project.id, dir: project.dir }, { take }); return Response.json({ ...s, items: undefined }, { headers }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 500, headers }); } } export async function POST(request: Request) { const b = (await request.json().catch(() => null)) as { project?: unknown; clips?: unknown; take?: unknown } | null; if (!b || typeof b !== "object") return Response.json({ error: "bad json" }, { status: 400, headers }); const project = await projectRef(String(b.project ?? "")); if (!project) return Response.json({ error: "no such project" }, { status: 404, headers }); const clips = Array.isArray(b.clips) ? b.clips.map(String).filter(Boolean) : null; const take = takeOf(b.take); if (take && typeof take === "object") return Response.json(take, { status: 400, headers }); const ref = { id: project.id, dir: project.dir }; try { const q = await queueBenchPrep(ref, clips, { retry: true, take }); const s = await benchPrepState(ref, { take }); // A runner just started may not hold its lock yet: it is running all the same. return Response.json( { ...s, running: s.running || q.started, items: undefined, queued: q.queued.length, started: q.started, off: !!q.off }, { headers }, ); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 500, headers }); } }