import { type NextRequest } from "next/server"; export const runtime = "nodejs"; export const dynamic = "force-dynamic"; import { forwardResponse, handleApiError, requireClients, successResponse, } from "@/lib/server/api-helpers"; import { paginateAll } from "@tablestore/tablestore-agent-sdk"; /** * GET /api/v1/knowledge-bases List all KBs (auto-paginated). * POST /api/v1/knowledge-bases Create a new KB. */ export async function GET(request: NextRequest) { try { const auth = requireClients(request); if (!("kb" in auth)) return auth; const { kb } = auth; const { items, totalCount } = await paginateAll( (req) => kb.listKnowledgeBases(req), {}, "knowledgeBases", ); return successResponse({ items, totalCount }); } catch (error) { return handleApiError(error); } } export async function POST(request: NextRequest) { try { const auth = requireClients(request); if (!("kb" in auth)) return auth; const { kb } = auth; let body: Record; try { body = (await request.json()) as Record; } catch { return forwardResponse({ code: "BAD_REQUEST", data: null, message: "Request body must be JSON", }); } const resp = await kb.createKnowledgeBase(body); return forwardResponse(resp); } catch (error) { return handleApiError(error); } }