Files
duocthu/apps/web/app/api/section-text/route.ts
T

47 lines
1.7 KiB
TypeScript

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 params = new URL(request.url).searchParams;
const drugId = params.get("drug_id")?.trim() ?? "";
const sectionKey = params.get("section_key")?.trim() ?? "";
if (
!/^[a-z0-9_]{1,160}$/i.test(drugId) ||
!/^[a-z0-9_]{1,80}$/i.test(sectionKey)
) {
return NextResponse.json({ error: "invalid_section_request" }, { status: 400 });
}
try {
const upstream = await fetch(
`${ragBaseUrl()}/v1/rag/section-text?drug_id=${encodeURIComponent(
drugId
)}&section_key=${encodeURIComponent(sectionKey)}`,
{
headers: { "X-Client-Version": "1.0.0" },
cache: "no-store",
signal: AbortSignal.timeout(12_000),
}
);
if (!upstream.ok) {
return NextResponse.json({ error: "section_text_unavailable" }, { status: upstream.status });
}
return NextResponse.json(await upstream.json());
} catch {
return NextResponse.json({ error: "section_text_unavailable" }, { status: 502 });
}
}