import { readdir } from "node:fs/promises"; import { invalidateProjects } from "@/lib/projects"; import { withEditNotes } from "@/lib/report/guard"; import { StaleToken, manifestToken } from "@/lib/report/manifest.mjs"; import { provenanceView, updateProvenance } from "@/lib/report/inspectorEdits.mjs"; import { resolveReport } from "@/lib/report/serve.mjs"; import { refuseCaller } from "@/lib/report/caller"; import { crossSiteRefusal } from "@/lib/report/sameOrigin.mjs"; import { sitesByOrigin } from "@/lib/report/siteDeploys.mjs"; import { CHANNELS_DIR, SITES_DIR } from "@/lib/paths.mjs"; export const dynamic = "force-dynamic"; // THE PROJECT'S PROVENANCE (umtool redesign L6, the inspector's Provenance panel; `umtool provenance` is the // same writer): provenance.siteOrigin, channelSlug and channel -- the keys agents set by hand this week (a // `umtool new` with no --from leaves channelSlug empty, and `check` blocks on it). The link policy beside them // is /api/report/link-policy's. // // GET ?project= the three keys, the link policy, the cut's channels (with their clip counts) and the // corpus's, the sites (SITES_DIR, each with a siteUrl), the token // PUT { project, token, patch: { siteOrigin?, channelSlug?, channel? } } set them ("" or null removes one) const noStore = { "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 }); const m = r.manifest as { provenance?: Record; timeline?: { type?: string; channel?: string }[] }; const counts = new Map(); for (const e of m.timeline ?? []) if (e?.type === "clip" && typeof e.channel === "string" && e.channel) counts.set(e.channel, (counts.get(e.channel) ?? 0) + 1); const corpus = await readdir(/* turbopackIgnore: true */ CHANNELS_DIR, { withFileTypes: true }).then( (d) => d.filter((x) => x.isDirectory() && !x.name.startsWith(".")).map((x) => x.name).sort().slice(0, 4000), () => [] as string[], ); return Response.json( { provenance: provenanceView(m), linkPolicy: m.provenance?.linkPolicy ?? null, channels: [...counts].map(([slug, clips]) => ({ slug, clips })).sort((a, b) => b.clips - a.clips || a.slug.localeCompare(b.slug)), corpusChannels: corpus, sites: [...sitesByOrigin(SITES_DIR)].map(([origin, s]) => ({ origin, siteId: s.siteId, title: s.title })), token: await manifestToken(r.project.dir), }, { headers: noStore }, ); } export async function PUT(request: Request) { const refused = refuseCaller(request, "/api/report/provenance"); if (refused) return refused; const cross = crossSiteRefusal(request.headers); if (cross) return Response.json({ error: cross }, { status: 403 }); let body: Record; try { body = await request.json(); } catch { return Response.json({ error: "expected JSON" }, { status: 400 }); } const r = await resolveReport(String(body.project ?? "")); if ("error" in r) return Response.json({ error: r.error }, { status: r.status }); try { const { result, editNotes } = await withEditNotes(r.project, () => updateProvenance(r.project.dir, (body.patch ?? {}) as Record, { token: typeof body.token === "string" ? body.token : null }), ); invalidateProjects(); return Response.json({ ok: true, ...result, editNotes }, { headers: noStore }); } catch (e) { if (e instanceof StaleToken) return Response.json({ error: e.message, stale: true }, { status: 409 }); return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400 }); } }