import { cancelJob, getJob, jobView, runningJob, startJob } from "@/lib/jobs"; import { refuseCaller } from "@/lib/report/caller"; import { UMTOOL_DIR } from "@/lib/report/driver.mjs"; import { reelCutSteps, reelPlayView } from "@/lib/report/reelPlay.mjs"; import { resolveReport } from "@/lib/report/serve.mjs"; import { isTakeId } from "@/lib/report/takes.mjs"; import { DEFAULT_REEL, isReelId, listReels } from "@/lib/report/trailerBeats.mjs"; export const dynamic = "force-dynamic"; // BEAT REELS (lane/beat-reels): a reel's beats played and walked from a take's // rendered master, and Cut the trailer as a job on umtool's runner. // // GET ?project=&reel=&take= the reel on that take's master // (lib/report/reelPlay.mjs): each beat's // master span, the reel's cuts, the // takes with masters, the reels, and the // cut job running for this project, if any // GET ?job=[&since=&sinceEvent=] that job, as the take jobs show one // POST { project, reel, take, finale?, out?, dryRun? } cut it (finale: the master's last S s; out: a NAME; dryRun): `umtool trailer

--reel // --take ` on the runner (one job at a // time; the CLI takes the heavy slot) // ?cancel= stop it // The beats themselves are written through /api/report/trailer-beats, their one writer. const noStore = { "cache-control": "no-store" }; const isCut = (kind: string) => /^trailer /.test(kind); export async function GET(request: Request) { const url = new URL(request.url); const id = url.searchParams.get("job"); if (id) { const job = getJob(id); if (!job) return Response.json({ error: "no such job" }, { status: 404 }); return Response.json(jobView(job, Number(url.searchParams.get("since") ?? 0), Number(url.searchParams.get("sinceEvent") ?? 0)), { headers: noStore }); } const r = await resolveReport(url.searchParams.get("project") ?? ""); if ("error" in r) return Response.json({ error: r.error }, { status: r.status }); const reel = url.searchParams.get("reel") || DEFAULT_REEL; if (!isReelId(reel)) return Response.json({ error: "reel is a reel name" }, { status: 400 }); const take = url.searchParams.get("take") || null; if (take && !isTakeId(take)) return Response.json({ error: "take is a take id" }, { status: 400 }); try { const [view, reels] = await Promise.all([reelPlayView(r.project, { reel, take }), listReels(r.project.dir)]); const running = runningJob(); return Response.json( { ...view, reels, job: running && running.project === r.project.id && isCut(running.kind) ? jobView(running) : null, busy: running && !isCut(running.kind) ? running.kind : null }, { headers: noStore }, ); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400 }); } } export async function POST(request: Request) { const refused = refuseCaller(request, "/api/report/reel"); if (refused) return refused; const url = new URL(request.url); const cancel = url.searchParams.get("cancel"); if (cancel) return Response.json({ cancelled: cancelJob(cancel) }); const body = (await request.json().catch(() => null)) as Record | null; if (!body || typeof body !== "object") return Response.json({ error: "expected JSON" }, { status: 400 }); const r = await resolveReport(String(body.project ?? "")); if ("error" in r) return Response.json({ error: r.error }, { status: r.status }); const reel = typeof body.reel === "string" && body.reel ? body.reel : DEFAULT_REEL; if (!isReelId(reel)) return Response.json({ error: "reel is a reel name" }, { status: 400 }); const take = String(body.take ?? ""); if (!isTakeId(take)) return Response.json({ error: "take is a take id: the take whose master the reel is cut from" }, { status: 400 }); const view = await reelPlayView(r.project, { reel, take }); if (view.noMaster) return Response.json({ error: view.noMaster.why }, { status: 400 }); if (!view.beats && !view.introAsked) return Response.json({ error: `the reel ${reel} has no beats yet` }, { status: 400 }); const running = runningJob(); if (running) return Response.json({ error: `a job is already running (${running.kind})`, running: jobView(running) }, { status: 409 }); const finale = Number(body.finale); const plan = reelCutSteps(r.project, reel, take, { umtoolDir: UMTOOL_DIR, finale: Number.isFinite(finale) && finale > 0 && finale <= 60 ? finale : null, out: typeof body.out === "string" && body.out ? body.out : null, dryRun: body.dryRun === true, }); try { const job = startJob(plan.kind, plan.steps, { project: r.project.id }); return Response.json({ ok: true, job: jobView(job) }, { headers: noStore }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 409 }); } }