import { isReportId } from "yt-dlp-transcript-common/lib/report/schema"; import { cancelJob, getJob, startJob } from "@/lib/jobs"; import { ARTICLES_DIR } from "@/lib/paths"; import { planChain } from "@/lib/report/chain.mjs"; import { chainKind, chainViewOf, errorResponse, projectsRootOf, readArticleIterations, readPolicy, type ChainMode } from "@/lib/articles/generation"; export const dynamic = "force-dynamic"; // MAKE THE VIDEO (release 27 S8): the pipeline chain over a done iteration // (lib/report/chain.mjs, S7) as a umtool job (lib/jobs.ts: one process group // per step, cancel, per-step timeouts). The chain records each step in the // iteration's `steps`, so a start resumes at the first step not done, or at // `from`. The projects root is the editor's policy's (generation.projectsRoot). // // GET ?article=&n=[&mode=draft-all][&deck=1] -> { chain } the steps, where it resumes, its job // POST { article, n, from?, draftAll?, model?, deck?, stopBeforeRender?, allowWholeRecording? } -> { ok, job } started // POST { cancel: } -> { cancelled } // // `draftAll` is Draft everything (release 28 M9): the chain with its // generation steps — each a report-draft job on `model` (else the task's // route) — and the fast build; a job of its own kind, resumed as the chain is. // `deck` (release 29 G1, Draft everything's option, off by default) adds the // deck step before the flips: the minimal on-screen deck on a cut with none. // `stopBeforeRender` (demo epic D1) ends the run before the voices: nothing // voiced or built; the next run resumes there. `allowWholeRecording` (the // windows step's one press) lets a remote-only channel's Rumble windows // download their whole recording, refused with its size otherwise. function parse(article: unknown, n: unknown): { article: string; n: number } | string { const a = String(article ?? ""); const k = Number(n); if (!isReportId(a)) return `"${a}" is not an article id`; if (!Number.isInteger(k) || k < 1) return "n is an iteration number"; return { article: a, n: k }; } async function chainOf(article: string, n: number, mode: ChainMode = "video", deck = false) { const its = await readArticleIterations(article); const it = its.iterations.find((r) => r.n === n); if (!it) return null; return chainViewOf(article, it, mode, { deck }); } const modeOf = (v: unknown): ChainMode => (v === "draft-all" || v === true ? "draft-all" : "video"); export async function GET(request: Request) { const url = new URL(request.url); const p = parse(url.searchParams.get("article"), url.searchParams.get("n")); if (typeof p === "string") return Response.json({ error: p }, { status: 400 }); try { const chain = await chainOf(p.article, p.n, modeOf(url.searchParams.get("mode")), url.searchParams.get("deck") === "1"); if (!chain) return Response.json({ error: `no iteration i${p.n}` }, { status: 404 }); return Response.json({ chain }, { headers: { "cache-control": "no-store" } }); } catch (err) { return errorResponse(err); } } export async function POST(request: Request) { const body = (await request.json().catch(() => ({}))) as { article?: unknown; n?: unknown; from?: unknown; cancel?: unknown; draftAll?: unknown; model?: unknown; deck?: unknown; stopBeforeRender?: unknown; allowWholeRecording?: unknown; }; if (body.cancel !== undefined) { const id = String(body.cancel); if (!getJob(id)) return Response.json({ error: "no such job" }, { status: 404 }); return Response.json({ cancelled: cancelJob(id) }); } const p = parse(body.article, body.n); if (typeof p === "string") return Response.json({ error: p }, { status: 400 }); try { let projectsRoot: string | undefined; try { projectsRoot = projectsRootOf((await readPolicy()).generation); } catch { projectsRoot = undefined; // the editor unreachable: the chain reads settings.json itself } const from = body.from ? String(body.from) : null; const mode = modeOf(body.draftAll === true); const model = typeof body.model === "string" && body.model.trim() ? body.model.trim() : undefined; if (model && mode !== "draft-all") return Response.json({ error: "a model is Draft everything's (draftAll): the model its drafts run on" }, { status: 400 }); const deck = body.deck === true; if (deck && mode !== "draft-all") return Response.json({ error: "the deck is Draft everything's option (draftAll)" }, { status: 400 }); const plan = planChain({ articlesDir: ARTICLES_DIR, article: p.article, n: p.n, from, mode, ...(deck ? { deck } : {}), ...(model ? { model } : {}), ...(projectsRoot ? { projectsRoot } : {}), ...(body.stopBeforeRender === true ? { stopBeforeRender: true } : {}), ...(body.allowWholeRecording === true ? { allowWholeRecording: true } : {}), }); if (plan.steps.length === 0) return Response.json({ error: `every step of ${mode === "draft-all" ? "Draft everything" : "the chain"} is done — pick a step to run from` }, { status: 400 }); const job = startJob(chainKind(p.article, p.n, mode), plan.steps); return Response.json({ ok: true, job: { id: job.id }, chain: await chainOf(p.article, p.n, mode, deck) }); } catch (err) { return Response.json({ error: (err as Error).message }, { status: 400 }); } }