import { CaseError } from "@/lib/research/cases.mjs"; import { confirmArticle, confirmScopeSweep, confirmVideo, planArticle, planScopeSweep, planVideo } from "@/lib/research/handoff"; import { fail, ok } from "../../../_respond"; export const dynamic = "force-dynamic"; // HAND-OFFS, plan then confirm (lib/research/handoff.ts; the API is // documented in ../../route.ts). // { to: "article", article?, model?, question? } -> { plan: ArticlePlan } // { to: "article", confirm: true, … } -> { href, jobId, iteration } // { to: "video", slug?, title? } -> { plan: VideoPlan } // { to: "video", confirm: true, … } -> { href } // { to: "sweep", article?, model?, question?, dateFrom?, dateTo? } -> { plan: ArticlePlan } (seeded: false; the dates override the scope's) // { to: "sweep", confirm: true, … } -> { href (the run's page), jobId, iteration } // An article is a seeded report-draft sweep the editor runs (it spends: the // plan says on what model, under which cap); a video is a project scaffolded // under the reports root (it writes one); a sweep (R3) is the article's // sweep over the case's scope with no seed, on the policy's sweep route. export async function POST(request: Request, ctx: { params: Promise<{ id: string }> }) { try { const { id } = await ctx.params; const b = (await request.json().catch(() => null)) as Record | null; if (!b || typeof b !== "object" || Array.isArray(b)) throw new CaseError('expected a JSON object: { to: "article" | "video" | "sweep", confirm? }'); const s = (k: string) => (typeof b[k] === "string" && (b[k] as string).trim() ? (b[k] as string).trim() : undefined); const confirm = b.confirm === true; if (b.to === "article") { const over = { ...(s("article") ? { article: s("article") } : {}), ...(s("model") ? { model: s("model") } : {}), ...(s("question") ? { question: s("question") } : {}) }; return ok(confirm ? await confirmArticle(id, over) : { plan: (await planArticle(id, over)).plan }); } if (b.to === "sweep") { const over = { ...(s("article") ? { article: s("article") } : {}), ...(s("model") ? { model: s("model") } : {}), ...(s("question") ? { question: s("question") } : {}), ...(s("dateFrom") ? { dateFrom: s("dateFrom") } : {}), ...(s("dateTo") ? { dateTo: s("dateTo") } : {}), }; return ok(confirm ? await confirmScopeSweep(id, over) : { plan: (await planScopeSweep(id, over)).plan }); } if (b.to === "video") { const over = { ...(s("slug") ? { slug: s("slug") } : {}), ...(s("title") ? { title: s("title") } : {}) }; return ok(confirm ? await confirmVideo(id, over) : { plan: await planVideo(id, over) }); } throw new CaseError('to is "article", "video" or "sweep"'); } catch (e) { return fail(e); } }