Replace RSC streaming with /api/ai Route Handler for Docker reliability.

Server Actions + createStreamableValue kept failing in production; fetch-based text stream avoids RSC serialization issues and shows readable error messages.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-10 22:39:09 +08:00
parent 0f3bc2c50a
commit dba0245cb1
13 changed files with 342 additions and 384 deletions
+44
View File
@@ -0,0 +1,44 @@
import { streamText } from "ai";
import { createOpenAI } from "@ai-sdk/openai";
import { buildAiMessages } from "@/lib/ai/build-messages";
import {
getOpenAiApiKey,
getOpenAiBaseUrl,
getOpenAiModel,
} from "@/lib/ai/config";
import type { AiRequestBody } from "@/lib/ai/types";
export const runtime = "nodejs";
export async function POST(req: Request) {
try {
const body = (await req.json()) as AiRequestBody;
if (!body?.mode || !body.payload) {
return new Response("请求格式错误", { status: 400 });
}
const { system, user } = await buildAiMessages(body);
const openai = createOpenAI({
apiKey: getOpenAiApiKey(),
baseURL: getOpenAiBaseUrl(),
});
const result = streamText({
temperature: 0.5,
model: openai(getOpenAiModel()),
messages: [
{ role: "system", content: system },
{ role: "user", content: user },
],
maxRetries: 0,
});
return result.toTextStreamResponse();
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
return new Response(message, {
status: 500,
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
}
}