The Open Standard for Generative UI
import { cloudInstructions } from "@/lib/cloud-prompt";
import { NextRequest } from "next/server";
import OpenAI from "openai";
import type { ChatCompletionMessageParam } from "openai/resources/chat/completions.mjs";
const systemPrompt = cloudInstructions();
export async function POST(req: NextRequest) {
const { messages } = await req.json();
// Chat Completions → POST /v1/embed/chat/completions
// Matches the frontend's openAIAdapter + openAIMessageFormat.
const client = new OpenAI({
apiKey: process.env.THESYS_API_KEY,
baseURL: "https://api.thesys.dev/v1/embed",
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const cleanMessages = (messages as any[])
.filter((m) => m.role !== "tool")
.map((m) => {
if (m.role === "assistant" && m.tool_calls?.length) {
const { tool_calls: _tc, ...rest } = m; // eslint-disable-line @typescript-eslint/no-unused-vars
return rest;
}
return m;
});
const chatMessages: ChatCompletionMessageParam[] = [
{ role: "system", content: systemPrompt },
...cleanMessages,
];
// A rejected key, rate limit, or bad request fails here, before streaming starts.
// Return it as JSON so the editor can show the real reason.
let stream;
try {
stream = await client.chat.completions.create({
model: "google/gemini-3.6-flash-free",
messages: chatMessages,
stream: true,
});
} catch (error) {
const status = (error as { status?: number }).status ?? 500;
return Response.json(
{ error: error instanceof Error ? error.message : String(error) },
{ status },
);
}
const encoder = new TextEncoder();
let controllerClosed = false;
const readable = new ReadableStream({
async start(controller) {
const enqueue = (data: Uint8Array) => {
if (controllerClosed) return;
try {
controller.enqueue(data);
} catch {
/* already closed */
}
};
const close = () => {
if (controllerClosed) return;
controllerClosed = true;
try {
controller.close();
} catch {
/* already closed */
}
};
try {
for await (const chunk of stream) {
const choice = chunk.choices?.[0];
const delta = choice?.delta;
// Only send chunks that have content or signal completion
if (delta?.content) {
enqueue(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`));
}
if (choice?.finish_reason === "stop") {
enqueue(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`));
}
}
} catch (err) {
const msg = err instanceof Error ? err.message : "Stream error";
console.error("Chat route error:", msg);
enqueue(encoder.encode(`data: ${JSON.stringify({ error: msg })}\n\n`));
} finally {
enqueue(encoder.encode("data: [DONE]\n\n"));
close();
}
},
});
return new Response(readable, {
headers: {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache, no-transform",
Connection: "keep-alive",
},
});
}