import { NextResponse } from "next/server"; export const runtime = "nodejs"; // RAG never goes through api-gateway. The gateway proxies `/auth/*` only -- // apps/api-gateway/src/proxy/ holds a single AuthProxyController -- so // preferring AI_SERVICE_URL here pointed every RAG call at a service with no // such route the moment apiGateway was enabled, breaking chat, suggest, // history, sections, section-text and feedback at once. Resolve ai-service // directly and let AI_SERVICE_URL mean what its name says: auth only. const AI_SERVICE_URL = process.env.AI_SERVICE_URL ?? "http://localhost:8000"; function ragBaseUrl() { return AI_SERVICE_URL.replace(/\/v1\/rag(?:\/query)?\/?$/, ""); } export async function GET(request: Request) { const drugId = new URL(request.url).searchParams.get("drug_id")?.trim() ?? ""; if (!/^[a-z0-9_]{1,160}$/i.test(drugId)) { return NextResponse.json({ error: "invalid_drug_id" }, { status: 400 }); } try { const upstream = await fetch( `${ragBaseUrl()}/v1/rag/sections?drug_id=${encodeURIComponent(drugId)}`, { headers: { "X-Client-Version": "1.0.0" }, cache: "no-store", signal: AbortSignal.timeout(8_000), } ); if (!upstream.ok) { return NextResponse.json({ error: "sections_unavailable" }, { status: upstream.status }); } return NextResponse.json(await upstream.json()); } catch { return NextResponse.json({ error: "sections_unavailable" }, { status: 502 }); } }