/** * Search-box.ai * Author: Simon-Pierre Boucher * Contact: contact@spboucher.ai * File: apps/web/app/api/research/route.ts * Description: POST — start a research session; GET — list recent sessions. */ import { NextResponse } from "next/server"; import { sessions } from "@search-box/db"; import { ensureMigrated, startResearch } from "@/lib/runner"; export const runtime = "nodejs"; export const dynamic = "force-dynamic"; export async function POST(req: Request): Promise { let body: unknown; try { body = await req.json(); } catch { return NextResponse.json({ error: "invalid JSON body" }, { status: 400 }); } const question = typeof body === "object" && body !== null && "question" in body ? String((body as { question: unknown }).question ?? "").trim() : ""; if (question.length < 8 || question.length > 2000) { return NextResponse.json({ error: "question must be 8–2000 characters" }, { status: 400 }); } const id = await startResearch(question); return NextResponse.json({ id }, { status: 201 }); } export async function GET(): Promise { await ensureMigrated(); const list = await sessions.list(30); return NextResponse.json({ sessions: list.map((s) => ({ id: s.id, question: s.question, status: s.status, createdAt: s.createdAt })) }); }