From e6c17cd43d9f1ea0e30580ca9655d753af8aa2fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B9=8F=E5=AE=87?= Date: Fri, 12 Jun 2026 15:37:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E8=AF=86=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../static/src/js/ai_chat_action.js | 26 +++++++++--------- yuthon_ai_agent/static/src/js/ai_floating.js | 27 ++++++++++--------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/yuthon_ai_agent/static/src/js/ai_chat_action.js b/yuthon_ai_agent/static/src/js/ai_chat_action.js index 81e78b8e2..3fc860609 100644 --- a/yuthon_ai_agent/static/src/js/ai_chat_action.js +++ b/yuthon_ai_agent/static/src/js/ai_chat_action.js @@ -95,21 +95,23 @@ export class AiChatAction extends Component { body: formData, credentials: "same-origin", }); + // 先读 text,再尝试解析 JSON(避免 body stream already read) + const text = await resp.text(); + let data; + try { + data = JSON.parse(text); + } catch (_) { + data = null; + } + if (!resp.ok) { - let detail = ""; - try { - const errData = await resp.json(); - detail = errData.error || JSON.stringify(errData); - } catch (_) { - const text = await resp.text(); - detail = text.substring(0, 500); - } + let detail = (data && data.error) || text.substring(0, 300); console.error("[AI] Upload HTTP", resp.status, detail); - alert("上传失败 (HTTP " + resp.status + ")\n" + (detail || "未知错误")); + alert("上传失败 (HTTP " + resp.status + ")\n" + detail); return; } - const data = await resp.json(); - if (data.success) { + + if (data && data.success) { this.state.attachments = [ ...this.state.attachments, { @@ -119,7 +121,7 @@ export class AiChatAction extends Component { }, ]; } else { - alert(data.error || "上传失败"); + alert((data && data.error) || "上传失败"); } } catch (e) { console.error("[AI] Upload failed:", e); diff --git a/yuthon_ai_agent/static/src/js/ai_floating.js b/yuthon_ai_agent/static/src/js/ai_floating.js index bcc8d269e..d5ab1c833 100644 --- a/yuthon_ai_agent/static/src/js/ai_floating.js +++ b/yuthon_ai_agent/static/src/js/ai_floating.js @@ -95,22 +95,23 @@ async function uploadFile(file) { body: formData, credentials: "same-origin", }); + // 先读 text,再尝试解析 JSON(避免 body stream already read) + const text = await resp.text(); + let data; + try { + data = JSON.parse(text); + } catch (_) { + data = null; + } + if (!resp.ok) { - // 服务器返回错误状态码,尝试读取错误详情 - let detail = ""; - try { - const errData = await resp.json(); - detail = errData.error || JSON.stringify(errData); - } catch (_) { - const text = await resp.text(); - detail = text.substring(0, 500); - } + let detail = (data && data.error) || text.substring(0, 300); console.error("[AI] Upload HTTP", resp.status, detail); - alert("上传失败 (HTTP " + resp.status + ")\n" + (detail || "未知错误")); + alert("上传失败 (HTTP " + resp.status + ")\n" + detail); return; } - const data = await resp.json(); - if (data.success) { + + if (data && data.success) { pendingAttachments.push({ attachment_id: data.attachment_id, filename: data.filename, @@ -119,7 +120,7 @@ async function uploadFile(file) { }); renderAttachmentPreview(); } else { - alert(data.error || "上传失败"); + alert((data && data.error) || "上传失败"); } } catch (e) { console.error("[AI] Upload failed:", e);