import { addClaimByHand, defaultHandBase, HandEditError, parseMoment, readArticleReport, startBlankArticle, type HandClaimInput, } from "yt-dlp-transcript-common/controller/handEdits"; import { cuesAround } from "yt-dlp-transcript-common/publish/citedCues"; import { readIterationFile } from "yt-dlp-transcript-common/lib/report/iterations"; import { ArticleRegistryError } from "yt-dlp-transcript-common/lib/report/registry"; import { isReportId, type Report } from "yt-dlp-transcript-common/lib/report/schema"; import { errorResponse } from "@/lib/articles/generation"; import { sitesPaths } from "@/lib/articles/sites"; import { clearPiecesCache } from "@/lib/pieces"; import { clearRailCounts } from "@/lib/rail"; import type { HandBaseView } from "@/lib/articles/hand-types"; export const dynamic = "force-dynamic"; // AN ARTICLE BY HAND (release 32 R2): no model, the iterations store's // writers (common's controller/handEdits.ts). // // GET ?article=[&n=] the report a claim would join: its // sections, citations and respellings (n // absent: the default base) // GET ?moment= a pasted moment (common publish/citedCues.ts) (an archive link, the // house citation, /@) // and the record's cues around it // GET ?channel=&id=&at= the cues around a second // POST {action: "blank", article, title, kind?} an article in the // making: i1 (hand), a titled empty report // POST {action: "claim", article, base?, section, claim, cite?, add?} // a claim by hand; each new quote checked // against the record first const NO_STORE = { "cache-control": "no-store" }; const bad = (error: string, problems: string[] = []) => Response.json({ error, problems }, { status: 400, headers: NO_STORE }); // The cues shown around a moment: 20 s before it, 60 after. const BEFORE = 20; const AFTER = 60; async function baseView(article: string, n: number | null): Promise { const paths = sitesPaths(); const at = n ?? (await defaultHandBase(article, paths)); const empty: HandBaseView = { article, n: at, reportKind: null, title: null, sections: [], citations: [], respellings: [] }; let r: Report; if (at === null) { // An article older than iterations: its own report.json. const own = await readArticleReport(article, paths).catch(() => null); if (!own) return empty; r = own; } else { const text = await readIterationFile(article, at, "report.json", paths); if (text === null) return empty; try { r = JSON.parse(text) as Report; } catch { return empty; } } return { article, n: at, reportKind: r.kind, title: r.title ?? null, sections: (r.sections ?? []).map((s) => ({ id: s.id, title: s.title, claims: (s.claims ?? []).length })), citations: Object.entries(r.citations ?? {}).map(([id, c]) => ({ id, kind: c.kind, where: c.kind === "video" || c.kind === "audio" ? `${c.channel}/${c.id} ${c.start}–${c.end} s` : c.kind === "post" ? `${c.channel}/${c.id}` : c.kind === "page" ? c.url : c.source, quote: c.quote, })), respellings: r.respellings ?? [], }; } export async function GET(request: Request) { const q = new URL(request.url).searchParams; try { const moment = q.get("moment"); if (moment !== null || q.get("channel")) { const m = moment !== null ? parseMoment(moment) : { channel: q.get("channel") ?? "", id: q.get("id") ?? "", kind: "span" as const, at: Number(q.get("at") ?? 0), quote: null }; if (!m || !m.channel || !m.id) return bad("not a moment: paste an archive link, a citation, or /@"); const at = m.at ?? 0; const cues = m.kind === "span" ? await cuesAround(m.channel, m.id, Math.max(0, at - BEFORE), at + AFTER, sitesPaths()) : null; return Response.json({ ...m, at, title: cues?.title ?? null, cues: cues?.cues ?? [], found: !!cues }, { headers: NO_STORE }); } const article = q.get("article") ?? ""; if (!isReportId(article)) return bad(`"${article}" is not an article id`); const nRaw = q.get("n"); const n = nRaw ? Number(nRaw) : null; if (n !== null && !(Number.isInteger(n) && n > 0)) return bad("n is an iteration number"); return Response.json(await baseView(article, n), { headers: NO_STORE }); } catch (err) { if (err instanceof ArticleRegistryError || err instanceof HandEditError) return bad(err.message, err.problems ?? []); return errorResponse(err); } } export async function POST(request: Request) { const body = (await request.json().catch(() => ({}))) as Record; const article = String(body.article ?? ""); if (!isReportId(article)) return bad(`"${article}" is not an article id`); try { if (body.action === "blank") { const kind = body.kind === "sweep" ? "sweep" : "factcheck"; const out = await startBlankArticle(article, { title: String(body.title ?? ""), kind }, sitesPaths()); clearPiecesCache(); clearRailCounts(); return Response.json({ ok: true, iteration: out.iteration.n }, { headers: NO_STORE }); } if (body.action === "claim") { const base = body.base === undefined || body.base === null || body.base === "" ? null : Number(body.base); if (base !== null && !(Number.isInteger(base) && base > 0)) return bad("base is an iteration number"); const input: HandClaimInput = { base, section: (body.section ?? {}) as HandClaimInput["section"], claim: (body.claim ?? {}) as HandClaimInput["claim"], cite: Array.isArray(body.cite) ? body.cite.map(String) : [], add: Array.isArray(body.add) ? (body.add as HandClaimInput["add"]) : [], }; const out = await addClaimByHand(article, input, sitesPaths()); clearPiecesCache(); clearRailCounts(); return Response.json({ ok: true, ...out }, { headers: NO_STORE }); } return bad('action is "blank" or "claim"'); } catch (err) { if (err instanceof ArticleRegistryError || err instanceof HandEditError) return bad(err.message, err.problems ?? []); return errorResponse(err); } }