import { invalidateProjects } from "@/lib/projects"; import { withEditNotes } from "@/lib/report/guard"; import { StaleToken, StructureRefused, manifestToken, updateNarration } from "@/lib/report/manifest.mjs"; import { narrationReviewView } from "@/lib/report/narration-review.mjs"; import { resolveReport } from "@/lib/report/serve.mjs"; import { narrationsOf } from "umtool-report-to-video/narration"; import { setNarrationReview } from "umtool-report-to-video/narration-review"; import { refuseCaller } from "@/lib/report/caller"; export const dynamic = "force-dynamic"; // The narration review (release 25, T5): a cut's narration script, card by // card, before synthesis. // // GET ?project= the view (lib/report/narration-review.mjs) and the token // POST { project, card, action, ... } one action on one narrated card: // approve { note? } approved on its CURRENT words and voice // reject { note } rejected; the note says what is wrong (required) // clear {} no verdict (the note is kept) // note { note } the note alone // pick { key } another reading kept for the card plays (umtool narration --pick) // edit { text, token? } the card's narration words, through the manifest // writer (snapshot, validators, the edit-notes // guard); an edit clears the verdict // // Verdicts live in /drafts.review.json as `narrate//narration` // (report-to-video's narration-review.mjs over drafts-review.mjs; release 25's // narration.review.json is still read); the build refuses an un-approved // narrated card once the cut's narration has a verdict, and a final build a // drafted one. type Body = Record; const noStore = { "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 }); return Response.json( { ...(await narrationReviewView(r.project.dir, r.manifest)), token: await manifestToken(r.project.dir) }, { headers: noStore }, ); } export async function POST(request: Request) { const refused = refuseCaller(request, "/api/report/narration"); if (refused) return refused; let body: Body; try { body = await request.json(); } catch { return Response.json({ error: "expected JSON" }, { status: 400 }); } const r = await resolveReport(String(body.project ?? "")); if ("error" in r) return Response.json({ error: r.error }, { status: r.status }); const dir = r.project.dir; const card = String(body.card ?? ""); const n = narrationsOf(r.manifest).find((x: { id: string }) => x.id === card); if (!n) return Response.json({ error: `${card || "(no card)"} is not a card with a narration` }, { status: 404 }); const note = typeof body.note === "string" ? body.note : undefined; let editNotes = null; try { switch (body.action) { case "approve": await setNarrationReview(dir, card, { verdict: "approved", key: n.key, text: n.text, ...(note !== undefined ? { note } : {}) }); break; case "reject": await setNarrationReview(dir, card, { verdict: "rejected", key: n.key, text: n.text, note: note ?? "" }); break; case "clear": await setNarrationReview(dir, card, { verdict: null }); break; case "note": await setNarrationReview(dir, card, { note: note ?? "" }); break; case "edit": { const token = typeof body.token === "string" ? body.token : null; const w = await withEditNotes(r.project, () => updateNarration(dir, card, String(body.text ?? ""), { token })); editNotes = w.editNotes; // New words are not the words anybody approved. if ((w.result as { changed?: boolean }).changed) await setNarrationReview(dir, card, { verdict: null }); invalidateProjects(); break; } case "pick": { // ANOTHER READING the card plays (lane line-rerun): `umtool narration

--pick `, the CLI's own // command -- its words, reading and tuning; a verdict on the same words kept. const key = String(body.key ?? ""); if (!/^[0-9a-f]{64}$/.test(key)) return Response.json({ error: "key must be a kept reading's key" }, { status: 400 }); const { cardNarrationCommand } = await import("@/lib/report/cardNarrationCli.mjs"); const said: string[] = []; const code = await cardNarrationCommand([r.project.id, card, "--pick", key, "--json"], { out: (x: string) => said.push(x), err: (x: string) => said.push(x) }); if (code !== 0) return Response.json({ error: (() => { try { return JSON.parse(said.join("\n")).error; } catch { return said.join(" "); } })() }, { status: 400 }); invalidateProjects(); break; } default: return Response.json({ error: "action must be approve, reject, clear, note, edit or pick" }, { status: 400 }); } } catch (e) { if (e instanceof StaleToken) return Response.json({ error: e.message, stale: true }, { status: 409 }); if (e instanceof StructureRefused) return Response.json({ error: e.message, errors: e.errors }, { status: 400 }); return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400 }); } const fresh = await resolveReport(String(body.project)); const manifest = "error" in fresh ? r.manifest : fresh.manifest; return Response.json( { ok: true, ...(await narrationReviewView(dir, manifest)), token: await manifestToken(dir), editNotes }, { headers: noStore }, ); }