import { readIterationFile, readIterations, type Iteration } from "yt-dlp-transcript-common/lib/report/iterations"; import { REFRESH_FORMAT, deltaLine, goneParts, type RefreshFile, type RefreshPlan } from "yt-dlp-transcript-common/lib/report/draft/refresh/types"; import { articleHref } from "@/lib/hrefs"; import { ARTICLES_KEY } from "@/lib/articles/key.mjs"; import { askEditor, enqueueDraft } from "./generation"; import { sitesPaths } from "./sites"; // --------------------------------------------------------------------------- // A REFRESH, AS UMTOOL SEES IT (plans/umtool-redesign.md, R4): re-fetch an // article's scope, see what changed, update. The editor plans it and runs it // (`report-draft {"task": "refresh"}`, common's controller/reportRefresh.ts); // umtool asks for the plan, confirms it, and reads what each refresh kept // (its iteration's refresh.json) -- it writes nothing of the article store. // // SERVER-ONLY. // --------------------------------------------------------------------------- export type { RefreshPlan }; /** What a refresh of `article` would do (the editor's `dryRun`): nothing is written. */ export async function planArticleRefresh(article: string, o: { iteration?: number; fetch?: boolean } = {}): Promise { const answer = await askEditor<{ plan: RefreshPlan }>("/api/ops/report-draft", { method: "POST", body: { task: "refresh", article, dryRun: true, ...(o.iteration ? { iteration: o.iteration } : {}), ...(o.fetch === false ? { fetch: false } : {}) }, }); return answer.plan; } /** Start it: one editor job. Answers its job, the new iteration, and where to watch it. */ export async function startArticleRefresh(article: string, o: { iteration?: number; fetch?: boolean } = {}): Promise<{ jobId: string; iteration: number; href: string }> { const out = await enqueueDraft({ task: "refresh", article, ...(o.iteration ? { iteration: o.iteration } : {}), ...(o.fetch === false ? { fetch: false } : {}) }); return { ...out, href: articleHref({ site: ARTICLES_KEY, id: article }) }; } /** One refresh iteration, as a page shows it. */ export type RefreshRow = { n: number; parent: number | null; status: Iteration["status"]; // The refresh's own: running, no-change, partial (R6.1), updated, held-by-cap, failed, cancelled. refresh: RefreshFile["status"] | null; summary: string; // changed: null when the refreshed iteration recorded no matching-line counts (not tracked). counts: { newIds: number; changed: number | null; drifted: number; deleted: number }; // The new recordings' titles (up to 8), the quotes that changed. news: { id: string; title: string | null; date: string | null }[]; drifted: { citation: string; quote: string; now: string | null }[]; deleted: string[]; // The sources gone, said by where ("1 source deleted on YouTube", "1 source gone from the archive"). gone: string[]; fetch: { channel: string; action: string; status: string; reason: string | null }[]; build: { site: string | null; status: string; reason: string | null } | null; update: { estimateUsd: number; capUsd: number | null; refusal: string | null; usd: number | null; model: string } | null; since: string | null; at: string; endedAt: string | null; jobId: string | null; hasReport: boolean; warnings: string[]; }; function rowOf(it: Iteration, f: RefreshFile | null, hasReport: boolean): RefreshRow { const d = f?.delta; return { n: it.n, parent: it.parent, status: it.status, refresh: f?.status ?? null, summary: f?.summary || (d ? deltaLine(d, f?.basis.at) : it.status === "queued" ? "Queued" : it.status === "running" ? "Fetching and searching again…" : (it.error ?? "")), counts: { newIds: d?.newIds.length ?? 0, changed: d && d.changedTracked === false ? null : (d?.changed.length ?? 0), drifted: d?.drifted.length ?? 0, deleted: d?.deleted.length ?? 0 }, news: (d?.new ?? []).slice(0, 8).map((r) => ({ id: r.id, title: r.title, date: r.date })), drifted: (d?.quotes ?? []).filter((q) => q.status === "drifted").slice(0, 8).map((q) => ({ citation: q.citation, quote: q.quote, now: q.now ?? null })), deleted: d?.deleted ?? [], gone: d ? goneParts(d) : [], fetch: (f?.fetch ?? []).map((s) => ({ channel: s.channel, action: s.action, status: s.status, reason: s.reason ?? null })), build: f?.build ? { site: f.build.site, status: f.build.status, reason: f.build.reason ?? null } : null, update: f?.update ? { estimateUsd: f.update.estimateUsd, capUsd: f.update.capUsd, refusal: f.update.refusal ?? null, usd: f.update.usd ?? null, model: f.update.model } : null, since: f?.basis.at ?? null, at: it.createdAt, endedAt: it.endedAt ?? null, jobId: it.jobId ?? null, hasReport, warnings: f?.warnings ?? [], }; } /** A refresh iteration's refresh.json, or null. */ export async function readRefreshFile(article: string, n: number): Promise { const text = await readIterationFile(article, n, "refresh.json", sitesPaths()).catch(() => null); if (!text) return null; try { const f = JSON.parse(text) as RefreshFile; return f?.format === REFRESH_FORMAT ? f : null; } catch { return null; } } /** Every refresh of an article, newest first. */ export async function readRefreshes(article: string): Promise { const read = await readIterations(article, sitesPaths()).catch(() => null); if (!read) return []; const its = read.iterations.filter((it) => it.kind === "refresh").sort((a, b) => b.n - a.n); return Promise.all( its.map(async (it) => rowOf(it, await readRefreshFile(article, it.n), (await readIterationFile(article, it.n, "report.json", sitesPaths()).catch(() => null)) !== null)), ); }