import { isReportId } from "yt-dlp-transcript-common/lib/report/schema"; import { errorResponse } from "@/lib/articles/generation"; import { readResearch, settleResearch } from "@/lib/articles/research"; export const dynamic = "force-dynamic"; // A RESEARCH RUN'S QUEUE (release 27 S11), through the editor's // `research-approve`: // // GET ?article=&n= -> { research }: its proposals and phase, the // enqueued ones refreshed from their jobs // POST { article, n, ids, approve, note? } // -> { research, outcomes }: yes runs each // proposal through the editor's own action; // no rejects it (note: why) // The continuation is /api/articles/generate { task: "sweep", iteration: n }. const bad = (error: string) => Response.json({ error }, { status: 400 }); export async function GET(request: Request) { const url = new URL(request.url); const article = url.searchParams.get("article") ?? ""; const n = Number(url.searchParams.get("n")); if (!isReportId(article)) return bad(`"${article}" is not an article id`); if (!Number.isInteger(n) || n < 1) return bad("n is an iteration number"); try { return Response.json({ research: await readResearch(article, n) }, { headers: { "cache-control": "no-store" } }); } catch (err) { return errorResponse(err); } } export async function POST(request: Request) { const body = (await request.json().catch(() => ({}))) as { article?: unknown; n?: unknown; ids?: unknown; approve?: unknown; note?: unknown }; const article = String(body.article ?? ""); const n = Number(body.n); if (!isReportId(article)) return bad(`"${article}" is not an article id`); if (!Number.isInteger(n) || n < 1) return bad("n is an iteration number"); if (!Array.isArray(body.ids) || !body.ids.every((x) => typeof x === "string")) return bad("ids is a list of proposal ids"); if (typeof body.approve !== "boolean") return bad("approve is true or false"); try { const out = await settleResearch(article, n, body.ids as string[], body.approve, typeof body.note === "string" ? body.note : undefined); return Response.json({ ok: true, ...out }, { headers: { "cache-control": "no-store" } }); } catch (err) { return errorResponse(err); } }