import { readFile } from "node:fs/promises"; import { COMBO_SFX } from "umtool-report-to-video/post-combo"; import { projectCombos } from "umtool-report-to-video/post-combo-prints"; import { comboSfxFile } from "umtool-report-to-video/post-combo-render"; import { channelsDirFor } from "@/lib/projects/report.mjs"; import { withEditNotes } from "@/lib/report/guard"; import { StaleToken, StructureRefused, updatePostCombo, updatePostComboRender } from "@/lib/report/manifest.mjs"; import { resolveReport } from "@/lib/report/serve.mjs"; import { refuseCaller } from "@/lib/report/caller"; import { withTakeScope } from "@/lib/report/takeScope.mjs"; import { isTakeId } from "@/lib/report/takes.mjs"; export const dynamic = "force-dynamic"; // THE COMBO COUNTER (report-to-video/post-combo.mjs; umtool/docs/knobs.md, "The // combo counter"): the cut's repeated attachments, the badge's sounds, and its // writers -- what the post bench's combo panel and Play draw with. // // GET ?project { match, groups, combos }: each repeat, each item's count // GET ?sfx=hit|coin a bundled sound (Play plays it as the badge pops) // POST {project, op: "set", id, combo, token} an item's: false (no badge, still counted), null (the cut's), {knobs} // POST {project, op: "cut", patch, token} the cut's: render.posts.combo (a key null: the default) const noStore = { "cache-control": "no-store" }; export async function GET(request: Request) { const q = new URL(request.url).searchParams; const sfx = q.get("sfx"); if (sfx) { if (!COMBO_SFX.includes(sfx) || sfx === "off") return new Response("no such sound", { status: 404 }); const body = await readFile(comboSfxFile(sfx) as string).catch(() => null); if (!body) return new Response("the sound is not on disk", { status: 404 }); return new Response(new Uint8Array(body), { headers: { "content-type": "audio/wav", "cache-control": "max-age=3600" } }); } const r = await resolveReport(q.get("project") ?? ""); if ("error" in r) return Response.json({ error: r.error }, { status: r.status }); try { const m = r.manifest as Record; const { match, groups, combos } = await projectCombos(r.project.dir, m, { channelsDir: channelsDirFor(r.project.dir, m) }); return Response.json({ match, groups, combos }, { headers: noStore }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 500 }); } } // "Edit this take only" (lane/ui-simplify): a body naming `inTake` runs the same write under takeScope.mjs // withTakeScope -- the take's manifest read, the write into its patch, the project's manifest untouched (the // CLI's `--in-take`). The token is then the take's (take.json's), so a client's project token is not checked. export async function POST(request: Request) { const text = await request.text(); let parsed: Record | null = null; try { parsed = JSON.parse(text); } catch { parsed = null; } const take = parsed && typeof parsed.inTake === "string" && parsed.inTake ? parsed.inTake : null; const again = (body: string) => new Request(request.url, { method: "POST", headers: request.headers, body }); if (!take || !parsed) return postHere(again(text)); if (!isTakeId(take)) return Response.json({ error: "inTake must be a take id" }, { status: 400 }); const rr = await resolveReport(String(parsed.project ?? "")); if ("error" in rr) return Response.json({ error: rr.error }, { status: rr.status }); return withTakeScope(rr.project.dir, take, () => postHere(again(JSON.stringify({ ...parsed, token: null })))); } async function postHere(request: Request) { const refused = refuseCaller(request, "/api/report/post-combo"); if (refused) return refused; let body: Record; 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 token = body.token === undefined || body.token === null ? null : String(body.token); try { if (body.op === "set") { const value = body.combo === false ? false : body.combo === null || body.combo === undefined ? null : (body.combo as Record); const { result, editNotes } = await withEditNotes(r.project, () => updatePostCombo(r.project.dir, String(body.id ?? ""), value, { token })); return Response.json({ ok: true, entry: result.entry, combo: result.combo, token: result.token, editNotes }, { headers: noStore }); } if (body.op === "cut") { const { result, editNotes } = await withEditNotes(r.project, () => updatePostComboRender(r.project.dir, (body.patch ?? {}) as Record, { token })); return Response.json({ ok: true, combo: result.combo, token: result.token, editNotes }, { headers: noStore }); } } 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 }); } return Response.json({ error: "op is set or cut" }, { status: 400 }); }