import { currentClaims, proposalIsCurrent, readVerdictProposals, waitingProposals, type CurrentClaim, type VerdictProposal } from "yt-dlp-transcript-common/lib/report/verdictProposals"; import { articleFile } from "yt-dlp-transcript-common/lib/report/registry"; import { readJsonFile } from "yt-dlp-transcript-common/lib/jsonFile-server"; import { isVerdict, type Verdict } from "yt-dlp-transcript-common/lib/report/verdicts"; import { isReportId } from "yt-dlp-transcript-common/lib/report/schema"; import { askEditor, EditorError } from "./generation"; import { sitesPaths } from "./sites"; // VERDICT PROPOSALS, AS UMTOOL SEES THEM (release 29 M8): a model's proposed // verdicts for a fact-check's unruled claims, read from the article's // `verdicts.proposed.json` (common's lib/report/verdictProposals.ts; read // here, never written) and joined with its report.json as it is now — a // proposal waits while its claim is still unruled. The operator's approve or // reject goes to the editor's `verdict-review` (the one writer: approve sets // the claim's verdict through the registry). // // A waiting proposal whose claim changed under its id (a revision) is STALE: // counted, never shown (review fix 1). A sidecar that does not read is an // error the tab shows and the Inbox raises (review fix 7). // // SERVER-ONLY. export type WaitingProposal = { claim: string; text: string; section: string; verdict: Verdict; reason: string; model: string; promptVersion: string; at: string; }; export type ArticleProposals = { waiting: WaitingProposal[]; approved: number; rejected: number; // Waiting proposals whose claim is not the one they judged now (a revision // moved the ids, or changed the claim or its citations): never shown, never // reviewable (review fix 1). stale: number; // The sidecar did not read (it is never taken for empty). error?: string; }; /** An article's proposals: the waiting ones on still-unruled, unchanged claims, in the report's order. */ export async function readArticleProposals(article: string): Promise { if (!isReportId(article)) return null; let f; try { f = await readVerdictProposals(article, sitesPaths()); } catch (e) { return { waiting: [], approved: 0, rejected: 0, stale: 0, error: (e as Error).message }; } if (!f) return null; const read = await readJsonFile(articleFile(sitesPaths(), article)); const raw = read.ok ? (read.value as { kind?: unknown }) : null; const claims = raw && raw.kind === "factcheck" ? currentClaims(raw) : new Map(); const order = [...claims.keys()]; const all = Object.values(f.proposals) as VerdictProposal[]; let stale = 0; const waiting: WaitingProposal[] = []; for (const [id, p] of waitingProposals(f)) { const c = claims.get(id); if (c?.ruled) continue; if (!c || !proposalIsCurrent(p, c)) { stale++; continue; } waiting.push({ claim: id, text: c.text, section: c.section, verdict: p.verdict, reason: p.reason, model: p.model, promptVersion: p.promptVersion, at: p.at }); } waiting.sort((a, b) => order.indexOf(a.claim) - order.indexOf(b.claim)); return { waiting, approved: all.filter((p) => p.status === "approved").length, rejected: all.filter((p) => p.status === "rejected").length, stale, }; } /** Approve or reject one proposal through the editor's verdict-review. */ export async function reviewArticleProposal(body: unknown): Promise<{ proposal: unknown }> { const b = (body ?? {}) as Record; if (typeof b.article !== "string" || typeof b.claim !== "string") throw new EditorError('send {"article", "claim", "decision": "approve" | "reject", "note"?}', 400); if (b.decision !== "approve" && b.decision !== "reject") throw new EditorError('"decision" is "approve" or "reject"', 400); if (b.note !== undefined && typeof b.note !== "string") throw new EditorError('"note" is text', 400); // The proposal the operator was shown: the editor's review is a compare-and-set on it. if (!isVerdict(b.verdict) || typeof b.at !== "string" || !b.at) throw new EditorError('send the "verdict" and "at" of the proposal shown', 400); const sent = { article: b.article, claim: b.claim, decision: b.decision, verdict: b.verdict, at: b.at, ...(typeof b.note === "string" && b.note.trim() ? { note: b.note.trim() } : {}) }; const answer = await askEditor<{ proposal?: unknown }>("/api/ops/verdict-review", { method: "POST", body: sent }); return { proposal: answer.proposal ?? null }; }