import { getJob, jobView, runningJob, startJob } from "@/lib/jobs"; import { projectRef } from "@/lib/projects"; import { kindMakesTakes } from "@/lib/projects/kinds.mjs"; import { readManifest } from "@/lib/projects/report.mjs"; import { refuseCaller } from "@/lib/report/caller"; import { addClipToSpeaker, speakerBadge, speakerCheckSteps } from "@/lib/report/speakerCheck.mjs"; export const dynamic = "force-dynamic"; // THE SPEAKER CHECK ON THE WALK (UI notes 2026-10-30, #15): "Speaker: matches // Ember 0.93 / Kirsche 0.83", with the reference sets it was measured against // (lib/report/speakerCheck.mjs; the voices are /speakers.json). // "Check speaker" is ONE job on the app's runner (lib/jobs.ts: a log, the // NDJSON events, Cancel) running `umtool speaker-check

--clip ` // through the machine's heavy slot (queue-lock --heavy with its estimate: // beside other jobs by memory, after a render the operator is waiting for). // Local: TitaNet-small; no network, no LLM. // // GET ?project=&clip= the clip's row, the voices, and the check running, if any // GET ?job=[&since=&sinceEvent=] that job // POST { project, clip } check that clip's speaker (a job) // POST { project, clip, addTo: "" } put the clip in that voice's reference set (made when new) const headers = { "cache-control": "no-store" }; 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 }); } const project = await projectRef(String(url.searchParams.get("project") ?? "")); if (!project) return Response.json({ error: "no such project" }, { status: 404 }); const clip = String(url.searchParams.get("clip") ?? ""); const m = await readManifest(project.dir); const running = runningJob(); try { return Response.json( { ...(await speakerBadge(project.dir, m ?? {}, clip)), running: running && running.project === project.id && running.kind.startsWith("speaker-check ") ? jobView(running) : null, }, { headers }, ); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400 }); } } 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 refused = refuseCaller(request, "/api/report/speaker-check"); 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 }); if (!kindMakesTakes(project.kind)) return Response.json({ error: "speakers are checked for a report video" }, { status: 400 }); const clip = typeof body.clip === "string" && /^[A-Za-z0-9_-]{1,64}$/.test(body.clip) ? body.clip : null; if (!clip) return Response.json({ error: "which clip?" }, { status: 400 }); if (typeof body.addTo === "string") { try { const speakers = await addClipToSpeaker(project.dir, body.addTo, clip); return Response.json({ ok: true, speakers }, { headers }); } 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 }); const plan = speakerCheckSteps(project, { clip }); try { const job = startJob(plan.kind, plan.steps, { project: project.id }); return Response.json({ ok: true, job: jobView(job) }, { headers }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 409 }); } }