import { readFile } from "node:fs/promises"; import path from "node:path"; import { readIteration, readIterationFile, reportSourceOf, iterationLabel, iterationUsd, type Iteration } from "yt-dlp-transcript-common/lib/report/iterations"; import { parseReport } from "yt-dlp-transcript-common/lib/report/validate"; import type { Report } from "yt-dlp-transcript-common/lib/report/schema"; import { sitesPaths } from "./sites"; // TWO ITERATIONS SIDE BY SIDE (release 27 S8, /articles//compare): each // side's report (its sections and the findings in them), the ops its revision // applied and refused, its checks that failed, and — when it has a project — // its cut (the clips and the cards' narration). Read-only. export type CompareFinding = { text: string; cites: number }; export type CompareSection = { title: string; findings: CompareFinding[] }; export type CompareOp = { index: number; op: string; detail: string; why?: string }; export type CompareCutEntry = { type: "card" | "clip"; id: string; heading?: string; narration?: string; quote?: string; span?: string }; export type CompareSide = { n: number; label: string; kind: Iteration["kind"]; model: string; status: Iteration["status"]; parent: number | null; copyOf: number | null; feedback: string | null; question: string | null; usd: number; minutes: number | null; /** The iteration whose report this side shows (itself, or the nearest ancestor that made one). */ reportFrom: number | null; title: string | null; summary: string | null; sections: CompareSection[]; applied: CompareOp[]; refused: CompareOp[]; failedChecks: { name: string; detail: string | null }[]; cut: { title: string | null; entries: CompareCutEntry[] } | null; }; const CITE = /\]\(cite:[^)]+\)/g; // A section's findings: its claims, else its body's paragraphs (a sweep's // findings are its paragraphs, each with its citing links). function findingsOf(section: Report["sections"][number]): CompareFinding[] { if (section.claims?.length) { return section.claims.map((c) => ({ text: c.text, cites: (c.citations?.length ?? 0) + (c.text.match(CITE)?.length ?? 0) })); } return (section.body ?? "") .split(/\n\s*\n/) .map((p) => p.trim()) .filter(Boolean) .map((p) => ({ text: p.replace(/\[([^\]]*)\]\(cite:[^)]+\)/g, "“$1”"), cites: p.match(CITE)?.length ?? 0 })); } function opOf(raw: unknown): CompareOp { const r = (raw ?? {}) as { index?: number; op?: Record; why?: string }; const op = r.op ?? {}; const { op: name, ...rest } = op as { op?: string } & Record; const detail = JSON.stringify(rest); return { index: typeof r.index === "number" ? r.index : 0, op: String(name ?? "?"), detail: detail.length > 220 ? `${detail.slice(0, 217)}…` : detail, ...(r.why ? { why: String(r.why) } : {}), }; } async function cutOf(projectDir: string | undefined): Promise { if (!projectDir) return null; let manifest: { title?: string; timeline?: Record[] }; try { manifest = JSON.parse(await readFile(/* turbopackIgnore: true */ path.join(projectDir, "video.manifest.json"), "utf8")); } catch { return null; } const entries: CompareCutEntry[] = []; for (const e of manifest.timeline ?? []) { const id = String(e.id ?? ""); if (e.type === "card") { const narration = (e.narration as { text?: string } | undefined)?.text; entries.push({ type: "card", id, heading: String(e.heading ?? e.title ?? id), ...(narration ? { narration } : {}) }); } else if (e.type === "clip") { const span = typeof e.start === "number" && typeof e.end === "number" ? `${e.video ?? ""} ${e.start}–${e.end}` : undefined; entries.push({ type: "clip", id, ...(e.quote ? { quote: String(e.quote) } : {}), ...(span ? { span } : {}) }); } } return { title: manifest.title ?? null, entries }; } export async function readCompareSide(article: string, n: number): Promise { const paths = sitesPaths(); const it = await readIteration(article, n, paths); if (!it) return null; const from = await reportSourceOf(article, n, paths).catch(() => null); let report: Report | null = null; if (from !== null) { try { const parsed = parseReport(JSON.parse((await readIterationFile(article, from, "report.json", paths)) ?? ""), { id: article }); report = parsed.ok ? parsed.value : null; } catch { report = null; } } return { n, label: iterationLabel(n), kind: it.kind, model: it.model, status: it.status, parent: it.parent, copyOf: it.copyOf, feedback: it.feedback ?? null, question: it.question ?? null, usd: iterationUsd(it), minutes: it.minutes ?? null, reportFrom: from, title: report?.title ?? null, summary: report?.summary ?? null, sections: (report?.sections ?? []).map((s) => ({ title: s.title, findings: findingsOf(s) })), applied: (it.ops?.applied ?? []).map(opOf), refused: (it.ops?.refused ?? []).map(opOf), failedChecks: (it.checks ?? []).filter((c) => !c.ok).map((c) => ({ name: c.name, detail: c.detail ?? null })), cut: await cutOf(it.projectDir), }; }