import { fetchForReader } from "@/lib/research/fetch"; import { SourceError } from "@/lib/research/sources"; import { fail, ok } from "../_respond"; export const dynamic = "force-dynamic"; // POST { source, channel, video, start, end, reason, storedUrl? } | { job } // -> { state: "ready", file, from, to, cached } | { state: "queued", job, status } // | { state: "failed", message, job? } // // The clip behind a cite, through the editor's fetch-window route exactly as // the MCP's fetch_clip asks for it (lib/research/fetch.ts) -- never yt-dlp. // Never waits: a queued fetch is polled with { job }. export async function POST(request: Request) { try { const b = (await request.json().catch(() => null)) as Record | null; if (!b) throw new SourceError("expected a JSON body"); if (typeof b.job === "string" && b.job.trim()) return ok(await fetchForReader({ job: b.job.trim() })); const start = Number(b.start); const end = Number(b.end); return ok( await fetchForReader({ source: String(b.source ?? ""), channel: String(b.channel ?? ""), video: String(b.video ?? ""), start, end, reason: String(b.reason ?? "").slice(0, 400), // "Cut from the stored media URL" (release 32 R6): fetch_clip's field. ...(b.storedUrl === "fallback" || b.storedUrl === "only" ? { storedUrl: b.storedUrl } : {}), }), ); } catch (e) { return fail(e); } }