import { withEditNotes } from "@/lib/report/guard"; import { LOOK_KEYS, RenderRefused, StaleToken, effectiveManifest, lookOf, manifestToken, updateRender, updateVariants } from "@/lib/report/manifest.mjs"; import { renderModeOf } from "yt-dlp-transcript-common/lib/report/look.mjs"; import { VARIANTS_DEFAULT, variantsProblems } from "@/lib/report/knobs.mjs"; import { resolveReport } from "@/lib/report/serve.mjs"; import { deckOn } from "umtool-report-to-video/deck"; import { validateLook } from "umtool-report-to-video/look"; import { TRANSITION_STYLES, validateTransitionStyle } from "umtool-report-to-video/transition"; import { refuseCaller } from "@/lib/report/caller"; import { INTRO_STYLES } from "yt-dlp-transcript-common/lib/report/intro.mjs"; import { channelNamesWarnings } from "yt-dlp-transcript-common/lib/report/channelNames.mjs"; import { channelsOfCut } from "@/lib/projects/report.mjs"; export const dynamic = "force-dynamic"; // THE LOOK (release 36 lane C): the render keys the Look panel sets -- // headerHeight, footerHeight, qr, transition, transitionStyle, loudness, // captions -- never render.chrome (the deck's own route). The client sends a // project id, a PATCH of those keys (null removes one) and the token it read; // the writer (lib/report/manifest.mjs updateRender) validates with the // build's own checks, so a look this route accepts is one the build accepts. // // GET ?project= { look, mode, preset, deck, montage, styles, introStyles, keys, errors, token } // `mode`: "deck" | "basic" as the build resolves it (the look preset under the // manifest); the Look panel's Basic | Deck switch writes `patch: {mode}` // `channels`: the cut's channels for Channel names (render.channelNames): // [{slug, clips, recordNames, name}]; `warnings`: a name naming no clip's channel // PUT { project, token, patch } { ok, look, token, warnings? } | 409 stale | 400 { errors } // PUT { project, token, variants } render.variants, the ladder Make variants encodes // (null: the default ladder) → { ok, variants, token } 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 }); const render = (r.manifest.render ?? {}) as Record; // The mode as the build reads it: over the cut's look preset, when it stands on one. let mode: "deck" | "basic" = renderModeOf(render); try { mode = renderModeOf((effectiveManifest(r.manifest) as { render?: Record }).render); } catch { // An unknown preset: the manifest's own word (the build says what is wrong). } return Response.json( { look: lookOf(render), mode, preset: typeof r.manifest.preset === "string" ? r.manifest.preset : null, deck: deckOn(render), montage: render.montage !== undefined && render.montage !== null, styles: TRANSITION_STYLES, // The brand intro's styles (render.intro), for the Look panel's Intro. introStyles: INTRO_STYLES, keys: LOOK_KEYS, // A look somebody hand-edited into a state the build would refuse. errors: [...validateTransitionStyle(r.manifest), ...validateLook(r.manifest), ...variantsProblems(render.variants)], // The encode ladder (video knobs): as written, and the default when it is absent. variants: render.variants ?? null, variantsDefault: VARIANTS_DEFAULT, // The channels its clips are from, and what each is called (render.channelNames, lane channel-title). channels: await channelsOfCut(r.project.dir, r.manifest), warnings: channelNamesWarnings(r.manifest), token: await manifestToken(r.project.dir), }, { headers: noStore }, ); } export async function PUT(request: Request) { const refused = refuseCaller(request, "/api/report/render"); 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 }); if ("variants" in body && !("patch" in body)) { try { const { result: res, editNotes } = await withEditNotes(r.project, () => updateVariants(r.project.dir, (body.variants ?? null) as unknown[] | null, { token: body.token === undefined || body.token === null ? null : String(body.token), }), ); return Response.json({ ok: true, variants: res.variants, token: res.token, editNotes }, { headers: noStore }); } catch (e) { if (e instanceof StaleToken) return Response.json({ error: e.message, expected: e.expected, got: e.got, stale: true }, { status: 409 }); if (e instanceof RenderRefused) return Response.json({ error: e.message, errors: e.errors }, { status: 400 }); return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400 }); } } try { const { result: res, editNotes } = await withEditNotes(r.project, () => updateRender(r.project.dir, (body.patch ?? null) as Record, { token: body.token === undefined || body.token === null ? null : String(body.token), }), ); return Response.json({ ok: true, look: res.look, token: res.token, warnings: res.warnings ?? [], editNotes }, { headers: noStore }); } catch (e) { if (e instanceof StaleToken) { return Response.json({ error: e.message, expected: e.expected, got: e.got, stale: true }, { status: 409 }); } if (e instanceof RenderRefused) return Response.json({ error: e.message, errors: e.errors }, { status: 400 }); return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400 }); } }