import { jobView, runningJob, startJob } from "@/lib/jobs"; import path from "node:path"; import { UMTOOL_DIR, narrateCardSteps } from "@/lib/report/driver.mjs"; import { parseTtsTuning, ttsReadingError } from "yt-dlp-transcript-common/lib/ttsTuning.mjs"; import { renderWaitSentence } from "@/lib/report/renderMode.mjs"; import { resolveReport } from "@/lib/report/serve.mjs"; import { narrationsOf } from "umtool-report-to-video/narration"; export const dynamic = "force-dynamic"; // POST { project, card }: synthesize ONE narrated card's voice by asking the // editor (narrate-via-editor.mjs --only), as a job the page follows by id // (GET /api/report/build?job=), then plays from // /api/report/narration/audio. A voice already kept is a quick no-op job. // One job at a time, as every job here: a running build is a 409. // { project, card, again: true, reading?: N | "next", tuning?, text? } (lane line-rerun: Read again, Read edited // text, Tune): ONE READING kept for the card, the card not changed -- `umtool narration

--read …` as the // job; Pick is the narration route's `pick`. const TUNING_FLAG_OF: Record = { temperature: "--temperature", top_p: "--top-p", top_k: "--top-k", min_p: "--min-p", repeat_penalty: "--repeat-penalty", takes: "--chunk-takes" }; export async function POST(request: Request) { const body = (await request.json().catch(() => null)) as { project?: string; card?: string; again?: boolean; reading?: number | "next"; tuning?: unknown; text?: string } | null; const r = await resolveReport(String(body?.project ?? "")); if ("error" in r) return Response.json({ error: r.error }, { status: r.status }); const card = String(body?.card ?? ""); if (!narrationsOf(r.manifest).some((n: { id: string }) => n.id === card)) { return Response.json({ error: `${card || "(no card)"} is not a card with a narration` }, { status: 404 }); } const running = runningJob(); if (running) return Response.json({ error: `a job is already running (${running.kind})` }, { status: 409 }); try { if (body?.again) { const flags: string[] = []; if (body.reading !== undefined && body.reading !== null) { if (body.reading !== "next" && ttsReadingError(body.reading)) return Response.json({ error: ttsReadingError(body.reading) }, { status: 400 }); flags.push("--reading", String(body.reading)); } if (body.tuning !== undefined && body.tuning !== null) { const t = parseTtsTuning(body.tuning); if (!t.ok) return Response.json({ error: t.error }, { status: 400 }); for (const [k, v] of Object.entries(t.value)) flags.push(TUNING_FLAG_OF[k], String(v)); } if (typeof body.text === "string" && body.text.trim()) flags.push("--text", body.text.trim()); const step = { cwd: path.join(/* turbopackIgnore: true */ UMTOOL_DIR, ".."), env: {}, label: `${card}: a reading of its narration (asks the editor)`, timeoutMs: 15 * 60_000, argv: [process.execPath, path.join(/* turbopackIgnore: true */ UMTOOL_DIR, "bin", "umtool.mjs"), "narration", r.project.id, card, "--read", ...flags], }; const job = startJob(`narrate ${r.project.id} ${card}`, [step], { project: r.project.id }); // While render mode gives the machine to a render, the reading waits for it -- said in place. const waiting = renderWaitSentence(); return Response.json({ ok: true, job: jobView(job), ...(waiting ? { waiting } : {}) }, { headers: { "cache-control": "no-store" } }); } const job = startJob(`narrate ${r.project.id} ${card}`, narrateCardSteps(r.project, card), { project: r.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 }); } }