import { makeCurrent } from "yt-dlp-transcript-common/lib/report/iterations"; import { ArticleRegistryError } from "yt-dlp-transcript-common/lib/report/registry"; import { isReportId } from "yt-dlp-transcript-common/lib/report/schema"; import { errorResponse, readArticleIterations } from "@/lib/articles/generation"; import { sitesPaths } from "@/lib/articles/sites"; export const dynamic = "force-dynamic"; // AN ARTICLE'S ITERATIONS (release 27 S8). // // GET ?article= -> the list (lib/articles/generation.ts), polled by the // Iterations tab while one is queued or running // POST { article, n, action: "use" } // -> USE THIS ITERATION: common's makeCurrent, the // registry's one writer of an article's report.json from // an iteration (locked, validated; the article's // audience and attached video kept). The page never // writes it. export async function GET(request: Request) { const article = new URL(request.url).searchParams.get("article") ?? ""; if (!isReportId(article)) return Response.json({ error: `"${article}" is not an article id` }, { status: 400 }); try { return Response.json(await readArticleIterations(article), { 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; action?: unknown }; const article = String(body.article ?? ""); const n = Number(body.n); if (!isReportId(article)) return Response.json({ error: `"${article}" is not an article id` }, { status: 400 }); if (!Number.isInteger(n) || n < 1) return Response.json({ error: "n is an iteration number" }, { status: 400 }); if (body.action !== "use") return Response.json({ error: 'action is "use"' }, { status: 400 }); try { const { current } = await makeCurrent(article, n, sitesPaths()); return Response.json({ ok: true, current }); } catch (err) { if (err instanceof ArticleRegistryError) { return Response.json({ error: err.message, problems: err.problems ?? [] }, { status: 400 }); } return errorResponse(err); } }