import { resolveReport } from "@/lib/report/serve.mjs"; import { refuseCaller } from "@/lib/report/caller"; import { anchorContextOf } from "@/lib/report/reelPlay.mjs"; import { BeatsStale, DEFAULT_REEL, TRAILER_LIMITS, addTrailerBeat, anchorTrailerBeats, beatsToken, createReel, introSecondsOf, isReelId, listReels, moveTrailerBeat, orderTrailerBeats, readReelUndo, readTrailerBeats, removeTrailerBeat, setTrailerSettings, trailerCandidatesOf, trailerView, undoTrailerBeats, updateTrailerBeat, } from "@/lib/report/trailerBeats.mjs"; export const dynamic = "force-dynamic"; // THE TRAILER'S BEATS (UI notes 2026-10-30, item 11): promo/trailer-beats.json // through its one writer (lib/report/trailerBeats.mjs), the Trailer page's and // Add to trailer's -- `umtool trailer-beats` is the CLI's twin. // // Every call names a REEL (`reel`, default `trailer`: promo/trailer-beats.json; // any other: promo/-beats.json -- lane/beat-reels). // // GET ?project=&reel= the view (each beat's length, the running length // against the limit, beats outside their clip, the cut's // order), the ★ candidates, the cut's clips, the platform // limits, the token, the project's reels, the undo points // POST { project, reel?, token, op, … } // add { beat: { id, start, end, label? } | { at, dur }, at? } (default: the cut's order) // set { index, patch: { start?, end?, at?, dur?, label?, verdict? } } verdict: confirmed | note | null // remove { index } // move { index, to } // order {} the cut's order // settings { limit?, xfade?, intro?, outro?, title?, take? } null: the default again (outro: true/false/{…}/null) // undo {} the reel as it was before its last write // anchor { only? } anchor beats (all, or the 0-based `only`) to `take` // `take` on add / set / anchor: the take the edit was made against -- each // beat written gets its anchor (lib/report/reelAnchor.mjs), so the reel can // say later what changed under it. `set`'s patch may re-point a beat (`id`). // new-reel { name, from?, title? } a new reel (empty, or a copy of `from`) // A stale token is a 409. const noStore = { "cache-control": "no-store" }; async function view(dir: string, manifest: Record, reel: string) { const doc = await readTrailerBeats(dir, { reel }); const v = trailerView(manifest, doc, { introSeconds: doc.settings?.intro ? await introSecondsOf(dir) : null, reel }); const clips = ((manifest.timeline ?? []) as Record[]) .filter((e) => e?.type === "clip") .map((e) => ({ id: String(e.id), start: Number(e.cutStart ?? e.start), end: Number(e.cutEnd ?? e.end) })); const undo = await readReelUndo(dir, { reel }); return { ...v, candidates: trailerCandidatesOf(manifest, doc.beats), clips, limits: TRAILER_LIMITS, token: await beatsToken(dir, reel), exists: doc.exists, reels: await listReels(dir), undo: { count: undo.length, last: undo.length ? undo[undo.length - 1].what : null }, }; } const reelOf = (v: unknown) => (typeof v === "string" && v ? v : DEFAULT_REEL); 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 }); const reel = reelOf(url.searchParams.get("reel")); if (!isReelId(reel)) return Response.json({ error: "reel is a reel name (lower case, digits and dashes)" }, { status: 400 }); try { return Response.json(await view(r.project.dir, r.manifest, reel), { headers: noStore }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400 }); } } export async function POST(request: Request) { const refused = refuseCaller(request, "/api/report/trailer-beats"); if (refused) return refused; const body = (await request.json().catch(() => null)) as Record | null; if (!body || typeof body !== "object") 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; let reel = reelOf(body.reel); if (!isReelId(reel)) return Response.json({ error: "reel is a reel name (lower case, digits and dashes)" }, { status: 400 }); const token = typeof body.token === "string" ? body.token : null; try { // The take this edit was made against: its anchors go on the beats written. const take = typeof body.take === "string" && body.take ? body.take : null; const ctx = take && ["add", "set", "anchor"].includes(String(body.op)) ? await anchorContextOf(dir, take) : null; if (take && body.op === "anchor" && !ctx) return Response.json({ error: `take ${take} has no finished master to anchor to` }, { status: 400 }); const anchorFor = ctx?.anchorFor ?? null; let did: Record = {}; switch (body.op) { case "add": await addTrailerBeat(dir, (body.beat ?? {}) as Record, { token, at: typeof body.at === "number" ? body.at : null, manifest: r.manifest, reel, anchorFor }); break; case "set": await updateTrailerBeat(dir, Number(body.index), (body.patch ?? {}) as Record, { token, reel, anchorFor }); break; case "remove": await removeTrailerBeat(dir, Number(body.index), { token, reel }); break; case "move": await moveTrailerBeat(dir, Number(body.index), Number(body.to), { token, reel }); break; case "order": await orderTrailerBeats(dir, r.manifest, { token, reel }); break; case "settings": await setTrailerSettings(dir, Object.fromEntries(["limit", "xfade", "intro", "outro", "title", "take"].filter((k) => k in body).map((k) => [k, body[k]])), { token, reel }); break; case "anchor": { if (!anchorFor) return Response.json({ error: "anchor needs the take to anchor to (take)" }, { status: 400 }); const only = Array.isArray(body.only) ? body.only.map(Number) : null; await anchorTrailerBeats(dir, anchorFor, { token, reel, only, take: take ?? "" }); break; } case "undo": { const u = await undoTrailerBeats(dir, { token, reel }); did = { undone: u.undone }; break; } case "new-reel": { const name = String(body.name ?? ""); await createReel(dir, name, { from: typeof body.from === "string" && body.from ? body.from : null, title: typeof body.title === "string" ? body.title : null }); reel = name; break; } default: return Response.json({ error: "op must be add, set, remove, move, order, settings, anchor, undo or new-reel" }, { status: 400 }); } return Response.json({ ok: true, ...did, ...(await view(dir, r.manifest, reel)) }, { headers: noStore }); } catch (e) { if (e instanceof BeatsStale) return Response.json({ error: e.message, stale: true }, { status: 409 }); return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400 }); } }