28 lines
947 B
TypeScript
28 lines
947 B
TypeScript
import { NextRequest } from "next/server";
|
|
import { CLOUD_BASE, FRONTEND_API_KEY } from "@/lib/cloud";
|
|
|
|
// Proxy the editor's AI chat requests to Frontend Cloud, injecting the API key
|
|
// server-side so it is never exposed to the client. The cloud response is
|
|
// streamed straight back to the caller.
|
|
export async function POST(req: NextRequest) {
|
|
const upstream = await fetch(`${CLOUD_BASE}/api/chat`, {
|
|
method: "POST",
|
|
headers: {
|
|
"content-type": req.headers.get("content-type") ?? "application/json",
|
|
"x-api-key": FRONTEND_API_KEY,
|
|
},
|
|
body: req.body,
|
|
// Required by undici when forwarding a streaming request body.
|
|
// @ts-expect-error - duplex is valid but missing from the lib types.
|
|
duplex: "half",
|
|
});
|
|
|
|
return new Response(upstream.body, {
|
|
status: upstream.status,
|
|
headers: {
|
|
"content-type":
|
|
upstream.headers.get("content-type") ?? "application/octet-stream",
|
|
},
|
|
});
|
|
}
|