import { getJob, jobView, runningJob, startJob } from "@/lib/jobs"; import { projectRef } from "@/lib/projects"; import { clipsOf, readManifest } from "@/lib/projects/report.mjs"; import { loadMontageStyles, takeSteps } from "@/lib/report/takeMaker.mjs"; import { renderSeconds } from "@/lib/report/onscreen.mjs"; import { listTakes } from "@/lib/report/takes.mjs"; import { kindMakesTakes } from "@/lib/projects/kinds.mjs"; export const dynamic = "force-dynamic"; // MAKING TAKES (release 33) -- the Takes page's New take, Make all styles and // Rebuild, as ONE job on the app's runner (lib/jobs.ts: one at a time, a log, // the NDJSON events, Cancel). The job runs the CLI (`umtool take`, `umtool // takes make`), so the page and a terminal do the same thing; the render // inside takes the machine's heavy slot (lib/report/takeRender.mjs). // // GET ?job=[&since=&sinceEvent=] that job, as /api/report/build?job= shows one // GET ?project= the styles, and the take job running, if any // POST { project, take: {…} | allStyles: {…} | rebuild: "", force? } // (lib/report/takeMaker.mjs takeSteps says what each may carry) // ?cancel= stop it; a take not finished writes no take.json export async function GET(request: Request) { const url = new URL(request.url); const headers = { "cache-control": "no-store" }; 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 }); } const { styles, error } = await loadMontageStyles(); const running = runningJob(); const project = url.searchParams.get("project"); return Response.json( { styles: Object.entries(styles).map(([name, s]) => ({ name, label: s.label, summary: s.summary })), stylesError: error, running: running && (!project || running.project === project) && /^takes? /.test(running.kind) ? jobView(running) : null, }, { headers }, ); } export async function POST(request: Request) { const url = new URL(request.url); const cancel = url.searchParams.get("cancel"); if (cancel) { const { cancelJob } = await import("@/lib/jobs"); 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: "bad json" }, { status: 400 }); const project = await projectRef(String(body.project ?? "")); if (!project) return Response.json({ error: "no such project" }, { status: 404 }); if (!kindMakesTakes(project.kind)) return Response.json({ error: "takes are made of a report video" }, { status: 400 }); const manifest = await readManifest(project.dir); if (!manifest) return Response.json({ error: "no manifest" }, { status: 400 }); const [{ styles }, listing] = await Promise.all([loadMontageStyles(), listTakes(project.dir)]); let plan; try { plan = takeSteps(project, body, { styles, existing: listing.takes, clipCount: clipsOf(manifest).length, seconds: renderSeconds(manifest), }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400 }); } const running = runningJob(); if (running) { return Response.json({ error: `a job is already running (${running.kind})`, running: jobView(running) }, { status: 409 }); } try { const job = startJob(plan.kind, plan.steps, { project: project.id }); return Response.json({ ok: true, takes: plan.takes, job: jobView(job) }, { headers: { "cache-control": "no-store" } }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 409 }); } }