Luminaria / frontend /app /api /chat /route.ts
senlinyy's picture
feat: add default env vars
6be14c8
Raw
History Blame Contribute Delete
1.55 kB
import { type NextRequest, NextResponse } from "next/server";
import { backendUrl } from "@/lib/backend-url";
export const dynamic = "force-dynamic";
export const runtime = "nodejs";
const BACKEND_RETRY_ATTEMPTS = 20;
const BACKEND_RETRY_DELAY_MS = 500;
function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export async function POST(request: NextRequest) {
const backend = backendUrl();
const body = await request.text();
let response: Response | undefined;
for (let attempt = 1; ; attempt += 1) {
try {
response = await fetch(`${backend}/chat`, {
method: "POST",
body,
headers: {
"content-type": request.headers.get("content-type") ?? "application/json",
},
});
break;
} catch {
if (attempt >= BACKEND_RETRY_ATTEMPTS) {
return NextResponse.json(
{ detail: `Could not reach the backend chat service at ${backend}.` },
{ status: 502 }
);
}
await delay(BACKEND_RETRY_DELAY_MS);
}
}
if (!response) {
return NextResponse.json(
{ detail: `Could not reach the backend chat service at ${backend}.` },
{ status: 502 }
);
}
const headers = new Headers();
headers.set(
"content-type",
response.headers.get("content-type") ?? "application/x-ndjson"
);
headers.set("cache-control", "no-cache, no-transform");
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers,
});
}