import { getJob, jobView, runningJob, startJob } from "@/lib/jobs"; import { projectRef } from "@/lib/projects"; import { kindMakesTakes } from "@/lib/projects/kinds.mjs"; import { readVoiceMatch } from "yt-dlp-transcript-common/lib/montage/voiceMatch.mjs"; import { voiceMatchSteps } from "@/lib/report/voiceMatch.mjs"; export const dynamic = "force-dynamic"; // WHO IS SPEAKING, PER MONTAGE CLIP (release 34) -- the Cut tab's "Score the // voices", 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 voice-match`), so // the button and a terminal do the same thing. Local and light (TitaNet-small // on a few seconds a clip, nice'd): no heavy slot, no network, no LLM. // // GET ?job=[&since=&sinceEvent=] that job // GET ?project= the project's voice-match.json summary, and the job running, if any // POST { project, reference?: "auto" | "rulings" | "recordings" } // ?cancel= stop it; nothing is written until the run is done 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 project = await projectRef(String(url.searchParams.get("project") ?? "")); if (!project) return Response.json({ error: "no such project" }, { status: 404 }); const doc = await readVoiceMatch(project.dir); const running = runningJob(); return Response.json( { voiceMatch: doc ? { madeAt: doc.madeAt, counts: doc.counts, reference: doc.reference, thresholds: doc.thresholds } : null, running: running && running.project === project.id && running.kind.startsWith("voice-match ") ? 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: "voices are scored for a report video" }, { 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 = voiceMatchSteps(project, { reference: typeof body.reference === "string" ? body.reference : undefined }); try { const job = startJob(plan.kind, plan.steps, { project: project.id }); return Response.json({ ok: true, job: jobView(job) }, { headers: { "cache-control": "no-store" } }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 409 }); } }