Files
zhimingge/lib/ai/client-stream.ts
T
dekun 39181f21ad Fix AI result panel visibility and nginx streaming headers.
ResultAI had h-0 collapsing output; add X-Accel-Buffering no, clearer fetch errors, and NGINX.md for gate proxy setup.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-10 22:56:22 +08:00

54 lines
1.3 KiB
TypeScript

import type { AiRequestBody } from "@/lib/ai/types";
function parseApiError(text: string, status: number): string {
const trimmed = text.trim();
if (
trimmed.startsWith("<!DOCTYPE") ||
trimmed.startsWith("<html") ||
trimmed.includes("<title>404")
) {
return `AI 接口未到达后端 (${status})。请确认 Nginx 反代到 3130 且包含 /api/ 路径。`;
}
return trimmed.slice(0, 800) || `AI 请求失败 (${status})`;
}
export async function streamAiCompletion(
body: AiRequestBody,
onUpdate: (text: string) => void,
): Promise<void> {
const res = await fetch("/api/ai", {
method: "POST",
cache: "no-store",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!res.ok) {
throw new Error(parseApiError(await res.text(), res.status));
}
if (!res.body) {
throw new Error("AI 响应为空");
}
const reader = res.body.getReader();
const decoder = new TextDecoder();
let text = "";
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
text += decoder.decode(value, { stream: true });
onUpdate(text);
}
text += decoder.decode();
onUpdate(text);
if (!text.trim()) {
throw new Error("AI 返回内容为空,请检查模型配置或稍后重试");
}
}