import { invalidateProjects } from "@/lib/projects"; import { StaleToken, StructureRefused, manifestToken } from "@/lib/report/manifest.mjs"; import { CandidatesError, candidatesView, dismissCandidate, insertCandidate } from "@/lib/report/candidates.mjs"; import { projectCache, resolveReport } from "@/lib/report/serve.mjs"; import { refuseCaller } from "@/lib/report/caller"; import { recordingRegionsOf } from "umtool-report-to-video/clip-censor"; export const dynamic = "force-dynamic"; // THE CANDIDATES INBOX (UI notes item 14): a project's proposed clips // (/candidates.json, lib/report/candidates.mjs) for the Cut tab's // panel. Proposals come in from a terminal only (`umtool candidates

// import|add`); this route is the operator's two presses. // // GET ?project= { …candidatesView, token, playable: { : true }, hidden: { : its recording's regions } } // (playable: a cached window holds the cited second, so Play plays it here) // POST { project, op: "insert", id, section | group, token } through `timeline insert`'s writer // → { ok, inserted, clip, window, token, editNotes, attributionError? } | 409 stale // POST { project, op: "dismiss", id, note? } Not this: recorded, hidden, never deleted const headers = { "cache-control": "no-store" }; export async function GET(request: Request) { const url = new URL(request.url); const r = await resolveReport(url.searchParams.get("project") ?? ""); if ("error" in r) return Response.json({ error: r.error }, { status: r.status, headers }); const view = await candidatesView(r.project.dir, r.manifest); const playable: Record = {}; const waiting = view.candidates.filter((c: { status: string }) => c.status === "proposed"); if (waiting.length) { const cache = await projectCache(r.project, r.manifest).catch(() => null); for (const c of waiting as { id: string; video: string; t: number }[]) { const wins = cache ? (cache.windows(c.video) as { from: number; to: number }[]) : []; if (wins.some((w) => w.from <= c.t && w.to >= c.t + 2)) playable[c.id] = true; } } // Each candidate's recording's hidden regions (render.censor): overlaid where it plays, as on every raw window. const hidden: Record> = {}; for (const c of view.candidates as { id: string; video: string; channel?: string }[]) { const g = recordingRegionsOf(c.video, c.channel ?? null, r.manifest.render ?? {}, r.manifest.provenance ?? {}); if (g.length) hidden[c.id] = g; } return Response.json({ ...view, token: await manifestToken(r.project.dir), playable, hidden }, { headers }); } export async function POST(request: Request) { const refused = refuseCaller(request, "/api/report/candidates"); 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, headers }); const r = await resolveReport(String(body.project ?? "")); if ("error" in r) return Response.json({ error: r.error }, { status: r.status, headers }); const id = String(body.id ?? ""); try { if (body.op === "dismiss") { const note = typeof body.note === "string" ? body.note : null; const inCut = new Set(((r.manifest.timeline ?? []) as { type?: string; id?: string }[]).filter((e) => e?.type === "clip").map((e) => String(e.id))); return Response.json({ ok: true, ...(await dismissCandidate(r.project.dir, id, { note, inCut })) }, { headers }); } if (body.op !== "insert") return Response.json({ error: 'op is "insert" or "dismiss"' }, { status: 400, headers }); const section = body.section === undefined || body.section === null || body.section === "" ? null : Number(body.section); const group = typeof body.group === "string" && body.group ? body.group : null; if (section !== null && !Number.isInteger(section)) return Response.json({ error: "section is a section's number" }, { status: 400, headers }); const token = body.token === undefined || body.token === null ? null : String(body.token); // insertCandidate writes through `timeline insert`'s helper, which notes a generated cut's edits itself. const result = await insertCandidate(r.project, id, { section, group, token }); invalidateProjects(); return Response.json({ ok: true, ...result, token: await manifestToken(r.project.dir) }, { headers }); } catch (e) { if (e instanceof StaleToken) return Response.json({ error: e.message, stale: true }, { status: 409, headers }); if (e instanceof StructureRefused) return Response.json({ error: e.message, errors: (e as StructureRefused & { errors?: string[] }).errors }, { status: 400, headers }); const status = e instanceof CandidatesError ? ((e as CandidatesError & { status?: number }).status ?? 400) : 400; return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status, headers }); } }