39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
|
|
export const runtime = "nodejs";
|
|
|
|
const API_GATEWAY_URL =
|
|
process.env.API_GATEWAY_URL ?? process.env.AI_SERVICE_URL ?? "http://localhost:8079";
|
|
|
|
export async function GET(request: Request) {
|
|
const { searchParams } = new URL(request.url);
|
|
const q = searchParams.get("q")?.trim() || "";
|
|
|
|
if (!q) {
|
|
return NextResponse.json({ suggestions: [] });
|
|
}
|
|
|
|
try {
|
|
const targetUrl = API_GATEWAY_URL.includes("/v1/rag")
|
|
? `${API_GATEWAY_URL.replace(/\/query$/, "/suggest")}?q=${encodeURIComponent(q)}`
|
|
: `${API_GATEWAY_URL}/v1/rag/suggest?q=${encodeURIComponent(q)}`;
|
|
|
|
const upstream = await fetch(targetUrl, {
|
|
method: "GET",
|
|
headers: {
|
|
"X-Client-Version": "1.0.0",
|
|
},
|
|
cache: "no-store",
|
|
});
|
|
|
|
if (!upstream.ok) {
|
|
return NextResponse.json({ suggestions: [] });
|
|
}
|
|
|
|
const data = await upstream.json();
|
|
return NextResponse.json(data);
|
|
} catch {
|
|
return NextResponse.json({ suggestions: [] });
|
|
}
|
|
}
|