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>
This commit is contained in:
dekun
2026-06-10 22:56:22 +08:00
parent 38bbc7145a
commit 39181f21ad
7 changed files with 118 additions and 39 deletions
+21 -2
View File
@@ -1,18 +1,30 @@
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) {
const message = (await res.text()).trim();
throw new Error(message || `AI 请求失败 (${res.status})`);
throw new Error(parseApiError(await res.text(), res.status));
}
if (!res.body) {
@@ -31,4 +43,11 @@ export async function streamAiCompletion(
text += decoder.decode(value, { stream: true });
onUpdate(text);
}
text += decoder.decode();
onUpdate(text);
if (!text.trim()) {
throw new Error("AI 返回内容为空,请检查模型配置或稍后重试");
}
}