import { EditorError } from "@/lib/articles/generation"; import { armSourceWatch, citedChannels, clearSourceWatch, SourceWatchError } from "@/lib/report/sourceBacklog.mjs"; import { prioritize, readBacklog } from "@/lib/report/sourceBacklog"; import { resolveReport } from "@/lib/report/serve.mjs"; import { refuseCaller } from "@/lib/report/caller"; export const dynamic = "force-dynamic"; // THE SOURCE BACKLOG of a report video (UI notes item 16, lib/report/sourceBacklog.*): // // GET ?project= { channels: [{ slug, listed, held, transcribed, done, focused, … }], // focus, editorError, watch, fired } (the read is recorded: a watch may fire) // POST { project, op: "prioritize", slugs? } the editor's focus gains them (default: every cited channel), // through its POST /api/ops/channel-priority -- never settings directly // POST { project, op: "watch" } Re-sweep when complete: the Inbox says so once every cited channel the // editor archives is done (one it does not is left out: `left`) // POST { project, op: "unwatch" } const headers = { "cache-control": "no-store" }; export async function GET(request: Request) { const url = new URL(request.url); const r = await resolveReport(url.searchParams.get("project") ?? ""); if ("error" in r) return Response.json({ error: r.error }, { status: r.status, headers }); return Response.json(await readBacklog(r.project.dir, r.manifest), { headers }); } export async function POST(request: Request) { const refused = refuseCaller(request, "/api/report/source-backlog"); if (refused) return refused; const body = (await request.json().catch(() => null)) as Record | null; if (!body || typeof body !== "object") return Response.json({ error: "bad json" }, { status: 400, headers }); const r = await resolveReport(String(body.project ?? "")); if ("error" in r) return Response.json({ error: r.error }, { status: r.status, headers }); const cited = (citedChannels(r.manifest) as { slug: string }[]).map((c) => c.slug); try { if (body.op === "prioritize") { const slugs = Array.isArray(body.slugs) ? body.slugs.map(String).filter((s) => cited.includes(s)) : cited; return Response.json({ ok: true, ...(await prioritize(slugs)) }, { headers }); } if (body.op === "watch") { // Only what the editor archives can ever be complete: a channel it does not answer for is left out, and said. const now = await readBacklog(r.project.dir, r.manifest); if (now.editorError) return Response.json({ error: `the editor did not answer: ${now.editorError}` }, { status: 502, headers }); const slugs = now.channels.filter((c) => !c.error).map((c) => c.slug); const left = now.channels.filter((c) => c.error).map((c) => c.slug); return Response.json({ ok: true, ...(await armSourceWatch(r.project.dir, slugs)), ...(left.length ? { left } : {}) }, { headers }); } if (body.op === "unwatch") return Response.json({ ok: true, ...(await clearSourceWatch(r.project.dir)) }, { headers }); return Response.json({ error: 'op is "prioritize", "watch" or "unwatch"' }, { status: 400, headers }); } catch (e) { const status = e instanceof EditorError ? e.status : e instanceof SourceWatchError ? ((e as SourceWatchError & { status?: number }).status ?? 400) : 500; return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status, headers }); } }