import { checkReports, verifyReportQuotes, type QuoteResult, type ReportsCheckProblem } from "yt-dlp-transcript-common/bin/reports-check"; import type { Report } from "yt-dlp-transcript-common/lib/report/schema"; import type { CitedTarget } from "@/lib/clips/types"; import { ARTICLES_KEY } from "@/lib/paths"; import { readReportFile, sitesPaths } from "./sites"; // --------------------------------------------------------------------------- // VERIFY QUOTES and CHECK REPORT on the article page (release 32 R1): the CLI's // `archilyzer reports verify-quotes --article ` and `reports check // --article ` (a site's report: `reports check --reports `), run // in-process through the same functions (common/bin/reports-check.ts) over // umtool's roots. No model, no subprocess. Respellings apply (the report's // own, as the check applies them). Each quote and each problem that names a // span carries what plays it, so a person can hear it. // --------------------------------------------------------------------------- export type QuoteRow = QuoteResult & { quote: string; target: CitedTarget | null }; export type QuoteChecks = { what: "quotes"; report: string; rows: QuoteRow[]; counts: Record; invalid: string[] }; export type CheckProblemRow = ReportsCheckProblem & { quote?: string; target?: CitedTarget | null }; export type ReportCheck = { what: "check"; report: string; status: "pass" | "fail" | "usage" | "empty" | "error"; summary: string; problems: CheckProblemRow[]; allowed: string[]; warnings: string[]; }; type Cite = NonNullable[string]; const targetOfCite = (c: Cite | undefined): CitedTarget | null => c && (c.kind === "video" || c.kind === "audio") && Number.isFinite(c.start) && Number.isFinite(c.end) && c.start < c.end ? { channel: c.channel, video: c.id, from: c.start, to: c.end, ...(c.kind === "audio" ? { audio: true } : {}) } : null; async function reportOf(site: string, reportId: string): Promise { const read = await readReportFile(site, reportId); if (!read.report) throw Object.assign(new Error(read.problems[0]?.message ?? "no report"), { status: 404 }); return read.report; } export async function articleQuoteChecks(site: string, reportId: string, opts: { now?: string } = {}): Promise { const report = await reportOf(site, reportId); const results = await verifyReportQuotes(report, { paths: sitesPaths(), ...(opts.now ? { now: opts.now } : {}) }); const counts: Record = {}; for (const r of results) counts[r.status] = (counts[r.status] ?? 0) + 1; return { what: "quotes", report: reportId, rows: results.map((r) => { const c = report.citations?.[r.citation]; return { ...r, quote: c?.quote ?? "", target: targetOfCite(c) }; }), counts, invalid: [], }; } export async function articleReportCheck(site: string, reportId: string, opts: { allowMissingMedia?: boolean } = {}): Promise { const report = await reportOf(site, reportId); const paths = sitesPaths(); const allowMissingMedia = opts.allowMissingMedia === true; const r = site === ARTICLES_KEY ? await checkReports({ article: reportId, paths, allowMissingMedia }) : await checkReports({ siteId: site, reports: [reportId], paths, allowMissingMedia }); const base = { what: "check" as const, report: reportId, problems: [] as CheckProblemRow[], allowed: [] as string[], warnings: [] as string[] }; switch (r.status) { case "usage": case "error": return { ...base, status: r.status, summary: r.error }; case "empty": return { ...base, status: "empty", summary: "nothing published to check" }; case "pass": { const me = r.reports.find((x) => x.id === reportId); return { ...base, status: "pass", summary: `compose would pass: ${me ? `${me.quotes} quote(s) checked${me.lowest === null ? "" : `, lowest ${me.lowest.toFixed(2)}`}` : `${r.reports.length} report(s)`}, ` + `${r.moments} moment(s)`, allowed: r.allowed, warnings: r.warnings, }; } case "fail": return { ...base, status: "fail", summary: `compose would fail: ${r.problems.length} problem(s)`, problems: r.problems.map((p) => { const cid = p.citation?.startsWith(`${reportId}#`) ? p.citation.slice(reportId.length + 1) : null; const c = cid ? report.citations?.[cid] : undefined; return { ...p, ...(c ? { quote: c.quote, target: targetOfCite(c) } : {}) }; }), }; } }