import { errorResponse } from "@/lib/articles/generation"; import { planArticleRefresh, readRefreshes, startArticleRefresh } from "@/lib/articles/refresh"; export const dynamic = "force-dynamic"; // A REFRESH of an article (plans/umtool-redesign.md, R4): re-fetch its scope, // see what changed, update -- planned, then confirmed. The editor plans and // runs it (`report-draft {"task": "refresh"}`); umtool writes nothing here. // // GET ?article= { refreshes }: each refresh // iteration, newest first, with // what its refresh.json kept // POST { article, iteration?, fetch? } { plan }: what it would do // POST { article, iteration?, fetch?, confirm: true } // { jobId, iteration, href } const ID = /^[a-z0-9][a-z0-9-]{0,63}$/; const headers = { "cache-control": "no-store" }; export async function GET(request: Request) { const article = new URL(request.url).searchParams.get("article") ?? ""; if (!ID.test(article)) return Response.json({ error: "name the article (?article=)" }, { status: 400, headers }); return Response.json({ refreshes: await readRefreshes(article) }, { headers }); } export async function POST(request: Request) { try { const body = (await request.json().catch(() => null)) as Record | null; if (!body || typeof body !== "object" || Array.isArray(body)) return Response.json({ error: "send { article }" }, { status: 400, headers }); const article = typeof body.article === "string" ? body.article : ""; if (!ID.test(article)) return Response.json({ error: '"article" is an article id' }, { status: 400, headers }); const iteration = body.iteration === undefined || body.iteration === null ? undefined : Number(body.iteration); if (iteration !== undefined && !(Number.isInteger(iteration) && iteration > 0)) return Response.json({ error: '"iteration" is an iteration number' }, { status: 400, headers }); const o = { ...(iteration ? { iteration } : {}), ...(body.fetch === false ? { fetch: false } : {}) }; if (body.confirm === true) return Response.json({ ok: true, ...(await startArticleRefresh(article, o)) }, { headers }); return Response.json({ plan: await planArticleRefresh(article, o) }, { headers }); } catch (err) { return errorResponse(err); } }