From 10b6f9b692ef1772a0afd40561ba509a883f303e Mon Sep 17 00:00:00 2001 From: sophie Date: Fri, 19 Jun 2026 21:25:14 +0800 Subject: [PATCH] =?UTF-8?q?fix(chat):=20=E5=B0=86=E6=B5=81=E5=BC=8F?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E4=BF=A1=E6=81=AF=E6=98=A0=E5=B0=84=E4=B8=BA?= =?UTF-8?q?=E9=9D=A2=E5=90=91=E7=94=A8=E6=88=B7=E7=9A=84=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/features/chat/composables/useChat.ts | 14 ++- .../src/features/chat/types/chat.types.ts | 2 + .../features/chat/utils/streamErrorMessage.ts | 88 +++++++++++++++++++ .../chat/utils/toolRunProgressConfig.ts | 1 + frontend/src/locales/en/chat.json | 6 ++ frontend/src/locales/th/chat.json | 6 ++ frontend/src/locales/zh-CN/chat.json | 6 ++ frontend/src/locales/zh-TW/chat.json | 6 ++ 8 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 frontend/src/features/chat/utils/streamErrorMessage.ts diff --git a/frontend/src/features/chat/composables/useChat.ts b/frontend/src/features/chat/composables/useChat.ts index 0ef6d555..85f5f17f 100644 --- a/frontend/src/features/chat/composables/useChat.ts +++ b/frontend/src/features/chat/composables/useChat.ts @@ -3,7 +3,9 @@ * SSE 流式对话核心:消息列表、runs、发送与接收。 */ import { ref } from 'vue'; +import { useI18n } from 'vue-i18n'; import { getAuthHeaders } from '@/shared/utils/auth'; +import { applyStreamErrorToMessage, type StreamErrorLabels } from '../utils/streamErrorMessage'; import type { ChatMessage, ChatRun, RetrieverResource } from '../types/chat.types'; const normalizeResources = (resources: unknown) => { @@ -110,6 +112,7 @@ const applyEventMetadata = (msg: ChatMessage, data: Record) => }; export function useChat() { + const { t } = useI18n(); const messages = ref([]); const isThinking = ref(false); const viewId = ref(0); @@ -169,6 +172,13 @@ export function useChat() { return String(message ?? 'Unknown error'); }; + const streamErrorLabels = (): StreamErrorLabels => ({ + networkDisconnectedEmpty: t('chat.streamError.networkDisconnectedEmpty'), + partialInterrupted: t('chat.streamError.partialInterrupted'), + upstreamDisconnected: t('chat.streamError.upstreamDisconnected'), + generic: t('chat.streamError.generic'), + }); + const sendMessage = async ( query: string, agentId: number, @@ -338,7 +348,7 @@ export function useChat() { } else if (eventName === 'error') { const botMsg = ensureBotMessage(); if (!botMsg) continue; - botMsg.content += `\n[Error: ${formatStreamError(data.message)}]`; + applyStreamErrorToMessage(botMsg, formatStreamError(data.message), streamErrorLabels()); run.status = 'error'; if (messages.value === runMessages) isThinking.value = false; } else if (typeof data.answer === 'string' && data.answer.length > 0) { @@ -356,7 +366,7 @@ export function useChat() { const botMsg = ensureBotMessage(); if (botMsg) { const message = e instanceof Error ? e.message : 'Network Issue'; - botMsg.content += `\n[Error: ${message}]`; + applyStreamErrorToMessage(botMsg, message, streamErrorLabels()); } } finally { if (messages.value === runMessages) { diff --git a/frontend/src/features/chat/types/chat.types.ts b/frontend/src/features/chat/types/chat.types.ts index 3f5f0fb0..d9584985 100644 --- a/frontend/src/features/chat/types/chat.types.ts +++ b/frontend/src/features/chat/types/chat.types.ts @@ -35,6 +35,8 @@ export interface ChatMessage { variant?: 'primary' | 'secondary' | 'danger'; }>; shipping_actions_used?: boolean; + /** 流式对话是否因网络/上游中断而附带错误提示 */ + stream_error?: boolean; } export type ChatRunStatus = 'running' | 'done' | 'error'; diff --git a/frontend/src/features/chat/utils/streamErrorMessage.ts b/frontend/src/features/chat/utils/streamErrorMessage.ts new file mode 100644 index 00000000..a8e692de --- /dev/null +++ b/frontend/src/features/chat/utils/streamErrorMessage.ts @@ -0,0 +1,88 @@ +/** + * features/chat/utils/streamErrorMessage.ts + * 用途:将 SSE / 浏览器流式读错映射为用户可读文案。 + * 依赖:vue-i18n chat.streamError 词条。 + * 逻辑:useChat 在 catch 与 error 事件中调用 applyStreamErrorToMessage。 + */ +import type { ChatMessage } from '../types/chat.types'; + +const NETWORK_DISCONNECT_PATTERNS = [ + 'network error', + 'failed to fetch', + 'load failed', + 'networkerror when attempting to fetch resource', + 'network issue', +]; + +const UPSTREAM_DISCONNECT_PATTERNS = [ + 'response ended prematurely', + 'chunkedencodingerror', +]; + +export interface StreamErrorLabels { + networkDisconnectedEmpty: string; + partialInterrupted: string; + upstreamDisconnected: string; + generic: string; +} + +/** + * 判断是否为浏览器侧 SSE / fetch 连接中断类错误。 + */ +export function isNetworkDisconnectError(message: string): boolean { + const normalized = message.trim().toLowerCase(); + if (!normalized) return false; + return NETWORK_DISCONNECT_PATTERNS.some( + (pattern) => normalized === pattern || normalized.includes(pattern), + ); +} + +/** + * 判断是否为上游(Dify)流式响应提前结束。 + */ +export function isUpstreamDisconnectError(message: string): boolean { + const normalized = message.trim().toLowerCase(); + return UPSTREAM_DISCONNECT_PATTERNS.some((pattern) => normalized.includes(pattern)); +} + +/** + * 将原始错误信息映射为面向用户的提示文案。 + * + * @param rawMessage - 浏览器或后端返回的原始错误 + * @param hasContent - 是否已有部分回答内容 + * @param labels - i18n 文案集合 + */ +export function resolveStreamErrorText( + rawMessage: string, + hasContent: boolean, + labels: StreamErrorLabels, +): string { + if (isNetworkDisconnectError(rawMessage)) { + return hasContent ? labels.partialInterrupted : labels.networkDisconnectedEmpty; + } + if (isUpstreamDisconnectError(rawMessage)) { + return hasContent ? labels.partialInterrupted : labels.upstreamDisconnected; + } + if (/[\u4e00-\u9fff]/.test(rawMessage)) { + return rawMessage; + } + return labels.generic; +} + +/** + * 把流式错误写入 assistant 消息:无内容时整段替换,有部分内容时在末尾追加提示。 + */ +export function applyStreamErrorToMessage( + msg: ChatMessage, + rawMessage: string, + labels: StreamErrorLabels, +): void { + const hasContent = Boolean(msg.content.trim()); + const text = resolveStreamErrorText(rawMessage, hasContent, labels); + msg.stream_error = true; + if (hasContent) { + msg.content += `\n\n${text}`; + } else { + msg.content = text; + } +} diff --git a/frontend/src/features/chat/utils/toolRunProgressConfig.ts b/frontend/src/features/chat/utils/toolRunProgressConfig.ts index 51819839..bb0a4e18 100644 --- a/frontend/src/features/chat/utils/toolRunProgressConfig.ts +++ b/frontend/src/features/chat/utils/toolRunProgressConfig.ts @@ -41,6 +41,7 @@ export function resolveMessageAnchorKey(msg: ChatMessage, index: number): string } export function assistantMessageHasError(msg: ChatMessage | null | undefined): boolean { + if (msg?.stream_error) return true; return String(msg?.content || '').includes('[Error:'); } diff --git a/frontend/src/locales/en/chat.json b/frontend/src/locales/en/chat.json index 09bfa3b4..75280711 100644 --- a/frontend/src/locales/en/chat.json +++ b/frontend/src/locales/en/chat.json @@ -293,6 +293,12 @@ "searchByNameOrEmployeeId": "Search by name or employee ID, press Enter", "name": "Name", "employeeIdRequired": "Employee ID is required.", + "streamError": { + "networkDisconnectedEmpty": "Connection lost before a reply was received. Please try again.", + "partialInterrupted": "The reply was interrupted. The content above may be incomplete—please ask again.", + "upstreamDisconnected": "The AI service connection was interrupted. Please try again.", + "generic": "The request failed. Please try again." + }, "actions": { "copy": "Copy", "edit": "Edit", diff --git a/frontend/src/locales/th/chat.json b/frontend/src/locales/th/chat.json index 92114b71..766fa52c 100644 --- a/frontend/src/locales/th/chat.json +++ b/frontend/src/locales/th/chat.json @@ -293,6 +293,12 @@ "searchByNameOrEmployeeId": "ค้นหาด้วยชื่อหรือรหัสพนักงาน แล้วกด Enter", "name": "ชื่อ", "employeeIdRequired": "รหัสพนักงานห้ามว่าง", + "streamError": { + "networkDisconnectedEmpty": "การเชื่อมต่อขาดหายก่อนได้รับคำตอบ โปรดลองใหม่อีกครั้ง", + "partialInterrupted": "การสร้างคำตอบถูกขัดจังหวะ เนื้อหาด้านบนอาจไม่สมบูรณ์ โปรดถามใหม่", + "upstreamDisconnected": "การเชื่อมต่อบริการ AI ขาดหาย โปรดลองใหม่อีกครั้ง", + "generic": "คำขอล้มเหลว โปรดลองใหม่อีกครั้ง" + }, "actions": { "copy": "คัดลอก", "edit": "แก้ไข", diff --git a/frontend/src/locales/zh-CN/chat.json b/frontend/src/locales/zh-CN/chat.json index 1a234a8d..a30a41ac 100644 --- a/frontend/src/locales/zh-CN/chat.json +++ b/frontend/src/locales/zh-CN/chat.json @@ -293,6 +293,12 @@ "searchByNameOrEmployeeId": "输入姓名或工号搜索,按回车键查询", "name": "姓名", "employeeIdRequired": "工号不能为空。", + "streamError": { + "networkDisconnectedEmpty": "连接中断,未能获取回答,请稍后重试。", + "partialInterrupted": "回答生成中断,以上内容可能不完整,请重新提问。", + "upstreamDisconnected": "AI 服务连接中断,请稍后重试。", + "generic": "请求失败,请稍后重试。" + }, "actions": { "copy": "复制", "edit": "编辑", diff --git a/frontend/src/locales/zh-TW/chat.json b/frontend/src/locales/zh-TW/chat.json index 06cc8cb7..57ffdd81 100644 --- a/frontend/src/locales/zh-TW/chat.json +++ b/frontend/src/locales/zh-TW/chat.json @@ -293,6 +293,12 @@ "searchByNameOrEmployeeId": "輸入姓名或工號搜尋,按 Enter 查詢", "name": "姓名", "employeeIdRequired": "工號不能為空。", + "streamError": { + "networkDisconnectedEmpty": "連線中斷,未能取得回答,請稍後重試。", + "partialInterrupted": "回答生成中斷,以上內容可能不完整,請重新提問。", + "upstreamDisconnected": "AI 服務連線中斷,請稍後重試。", + "generic": "請求失敗,請稍後重試。" + }, "actions": { "copy": "複製", "edit": "編輯",