import { projectRef } from "@/lib/projects"; import { kindMakesTakes } from "@/lib/projects/kinds.mjs"; import { OpError, StaleToken, StructureRefused, clipEditorView, clipNeighbourhood, runClipEditorOp } from "@/lib/report/clipEditor.mjs"; import { refuseCaller } from "@/lib/report/caller"; export const dynamic = "force-dynamic"; // THE CLIP EDITOR (release 35 C2): `/projects//clips` on a report video // whose cut is not a montage -- an assistant's clips (the clip finder's), or // any report video's, adjusted by a person. lib/report/clipEditor.mjs is the // one place each write goes through. // // GET ?project= the view: clips, token, speakers, unused proposals // GET ?project=&clip=&from=&to= the strip for a span: turns, laughs, words | cues // [&video=] words only when they are that recording's (the bench's zoom) // POST { project, op, token?, … } one operation (clipEditor.mjs runClipEditorOp): // trim | restore | split | merge | add | bring-in | ruling | role | … | audio | blur // -> { op, result, carried, view }; 409 on a stale token (reload), 400 with // the sentence on a refusal (the build's own, for a structural one) const headers = { "cache-control": "no-store" }; async function editorProject(id: unknown) { const project = await projectRef(String(id ?? "")); if (!project) return { error: Response.json({ error: "no such project" }, { status: 404, headers }) }; if (!kindMakesTakes(project.kind)) return { error: Response.json({ error: "the clip editor is a report video's" }, { status: 400, headers }) }; return { project }; } export async function GET(request: Request) { const url = new URL(request.url); const r = await editorProject(url.searchParams.get("project")); if ("error" in r) return r.error; try { if (url.searchParams.has("from")) { const n = await clipNeighbourhood(r.project, { clip: url.searchParams.get("clip"), from: Number(url.searchParams.get("from")), to: Number(url.searchParams.get("to")), // The clip bench, zoomed in: that recording's words only. video: url.searchParams.get("video") || null, }); return Response.json(n, { headers }); } const view = await clipEditorView(r.project); if (!view) return Response.json({ error: "no manifest" }, { status: 400, headers }); return Response.json(view, { headers }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400, headers }); } } export async function POST(request: Request) { const refused = refuseCaller(request, "/api/report/clip-editor"); 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, headers }); const r = await editorProject(body.project); if ("error" in r) return r.error; try { return Response.json(await runClipEditorOp(r.project, body), { headers }); } catch (e) { if (e instanceof StaleToken) return Response.json({ error: e.message }, { status: 409, headers }); if (e instanceof StructureRefused) return Response.json({ error: e.message, errors: e.errors }, { status: 400, headers }); const status = e instanceof OpError ? e.status : 400; return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status, headers }); } }