import { askEditor, EditorError } from "@/lib/articles/generation"; import { readManifest } from "@/lib/projects/report.mjs"; import { backlogOf, citedChannels, focusBodyFor, focusedOf, readSourceWatch, recordSourceCheck } from "./sourceBacklog.mjs"; // The source backlog's editor half (UI notes item 16): lib/report/sourceBacklog.mjs // holds the rules and the watch; this asks the editor, with umtool's WORKER_TOKEN, // for each cited channel's snapshot counts and the focus, and asks it to // prioritize. SERVER-ONLY. export type BacklogRow = ReturnType & { clips: number; focused: boolean; error?: string }; export type BacklogView = { channels: BacklogRow[]; focus: unknown; editorError: string | null; watch: { slugs: string[]; armedAt: string; firedAt?: string } | null; watchError: string | null; fired: boolean; }; /** Read every cited channel's backlog, record it (the watch may fire), answer the view. */ export async function readBacklog(dir: string, manifest: Record | null = null): Promise { const m = (manifest ?? (await readManifest(dir)) ?? {}) as Record; const cited = citedChannels(m) as { slug: string; clips: number }[]; let editorError: string | null = null; // Fresh: a read right after Prioritize or a change of counts must see it (askEditor keeps a GET 2 s). const focus = await askEditor<{ value?: { focus?: unknown } }>("/api/ops/settings?key=channelPriority", { fresh: true }) .then((r) => r.value?.focus ?? null) .catch((e: unknown) => { editorError = e instanceof Error ? e.message : String(e); return null; }); const inFocus = new Set(focusedOf(focus) as string[]); const channels: BacklogRow[] = await Promise.all( cited.map(async ({ slug, clips }) => { try { const r = await askEditor(`/api/ops/channel/${encodeURIComponent(slug)}`, { fresh: true }); return { ...(backlogOf(slug, r) as ReturnType), clips, focused: inFocus.has(slug) }; } catch (e) { if (!(e instanceof EditorError) || e.status !== 404) editorError ??= e instanceof Error ? e.message : String(e); return { ...(backlogOf(slug, null) as ReturnType), clips, focused: inFocus.has(slug), error: e instanceof EditorError && e.status === 404 ? "the editor does not archive it" : "not read" }; } }), ); let fired = false; // Recorded only for an armed watch (no file is made in a project nobody asked to watch), and only // a real answer: an editor that did not answer says nothing about the backlog. const before = await readSourceWatch(dir); if (before.doc.watch && !before.doc.watch.firedAt && !editorError && channels.some((c) => c.held !== null)) { fired = await recordSourceCheck(dir, channels.filter((c) => !c.error)) .then((r: { fired: boolean }) => r.fired) .catch(() => false); } const { doc, error } = fired ? await readSourceWatch(dir) : before; return { channels, focus, editorError, watch: doc.watch, watchError: error ?? null, fired }; } /** PRIORITIZE: the editor's focus gains `slugs` (the ones in focus kept). */ export async function prioritize(slugs: string[]): Promise<{ added: string[]; already: string[]; focus: unknown }> { const cur = await askEditor<{ value?: { focus?: unknown } }>("/api/ops/settings?key=channelPriority", { fresh: true }); const { body, added, already } = focusBodyFor(cur.value?.focus ?? null, slugs) as { body: { focus: unknown }; added: string[]; already: string[] }; if (added.length) await askEditor("/api/ops/channel-priority", { method: "POST", body }); return { added, already, focus: body.focus }; }