import { runSearch, scopeFrom } from "@/lib/research/corpus"; import { parseAsk } from "@/lib/research/query.mjs"; import { SourceError } from "@/lib/research/sources"; import { fail, ok } from "../_respond"; export const dynamic = "force-dynamic"; // POST { q, source, channels?, from?, to?, states?, media?, posts?, exclude?, fresh? } // -> SearchResponse (lib/research/types.ts) // // ONE scan for the whole match set -- enumerate_matches' own, with snippets -- // and never a page of it (the coverage rule). `q` is the ask bar's text // (lib/research/query.mjs: a phrase, /regex/, or a near:N b). The answer // says whether the set is complete, or which channels a cap left unread; it // carries the facets and the session's reading log. export async function POST(request: Request) { try { const body = (await request.json().catch(() => null)) as Record | null; if (!body || typeof body !== "object") throw new SourceError("expected a JSON body"); const ask = parseAsk(typeof body.q === "string" ? body.q : ""); if (ask.error) throw new SourceError(ask.error); const exclude = Array.isArray(body.exclude) ? body.exclude.filter((x): x is string => typeof x === "string" && x.trim() !== "") : undefined; return ok( await runSearch({ ...scopeFrom(body), query: ask.query, regex: ask.regex, ...(exclude?.length ? { exclude } : {}), fresh: body.fresh === true, }), ); } catch (e) { return fail(e); } }