fix: 优化错误提示

This commit is contained in:
sophie committed 2026-10-08 09:49:57 +08:00
1 parent d57ecc62f9
commit 239d373503
14 files changed
+333 -38

No files matched your search

@@ -7,7 +7,7 @@
import { ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { getAuthHeaders } from '@/shared/utils/auth';
import { applyStreamErrorToMessage, type StreamErrorLabels } from '../utils/streamErrorMessage';
import { applyStreamErrorToMessage, isUserFacingStreamError, type StreamErrorLabels } from '../utils/streamErrorMessage';
import type {
ChatMessage,
ChatMessageAttachment,
@@ -533,7 +533,7 @@ export function useChat() {
* - Dify Chatflow 失败时常以 `workflow_finished + data.status=failed` 返回,HTTP 仍是 200;
* - 若这里只按“终态成功”处理,页面会出现“没有任何响应”;
* - 因此需要优先从 top-level / nested data / error 对象里抽取失败文案;
* - 上下文过长与缺省失败文案走 chat.streamError.contextTooLong / workflowFailed。
* - 上下文过长与缺省失败文案走 chat.streamError.contextTooLong / workflowFailed,并按用户可读文案展示。
* @param data - SSE 事件 JSON
* @returns 用户可读错误;非失败终态返回空串
*/
@@ -766,11 +766,6 @@ export function useChat() {
run.status = workflowFailureMessage ? 'error' : 'done';
clearLikelyCompletedStreamTimer();
if (messages.value === runMessages) isThinking.value = false;
if (workflowFailureMessage) {
const botMsg = ensureBotMessage();
if (!botMsg) continue;
applyStreamErrorToMessage(botMsg, workflowFailureMessage, streamErrorLabels());
}
if (typeof data.answer === 'string' && data.answer.length > 0) {
const botMsg = ensureBotMessage();
if (!botMsg) continue;
@@ -797,6 +792,16 @@ export function useChat() {
botMsg.content += normalizedAnswer;
}
}
if (workflowFailureMessage) {
const botMsg = ensureBotMessage();
if (botMsg) {
// 后端 user_facing 与本函数生成的 i18n 文案都已是给用户看的,不再收成通用失败句。
const userFacing = isUserFacingStreamError(data)
|| workflowFailureMessage === t('chat.streamError.contextTooLong')
|| workflowFailureMessage === t('chat.streamError.workflowFailed');
applyStreamErrorToMessage(botMsg, workflowFailureMessage, streamErrorLabels(), { userFacing });
}
}
if (botMsgIndex !== null) {
const currentBotMsg = runMessages[botMsgIndex];
if (currentBotMsg) applyEventMetadata(currentBotMsg, data);
@@ -807,7 +812,12 @@ export function useChat() {
// [STEP 2] SSE error 对用户来说也是当前轮终态,脱离活动视图后可继续发下一问,
// 同时保留底层连接在后台自然收尾,避免后续交互误触 abort。
detachActiveStreamFromView();
applyStreamErrorToMessage(botMsg, formatStreamError(data.message), streamErrorLabels());
applyStreamErrorToMessage(
botMsg,
formatStreamError(data.message),
streamErrorLabels(),
{ userFacing: isUserFacingStreamError(data) },
);
run.status = 'error';
clearLikelyCompletedStreamTimer();
if (messages.value === runMessages) isThinking.value = false;
@@ -3,10 +3,11 @@
* 用途:统一发起 Chat Turn(POST /api/chat/turns/stream),复用与 sendMessage 相同的 SSE 渲染链路。
* 依赖:chat-turn.types、useChat 的 shipping UI 同步、streamErrorMessage、auth headers。
* 逻辑:useChat 构造 startChatTurn 并导出;货代/请假等业务只构造 actionType + payload。
* error / run.failed 若带 user_facing,则展示后端原文,同一轮只写一次。
*/
import { type Ref } from 'vue';
import { getAuthHeaders } from '@/shared/utils/auth';
import { applyStreamErrorToMessage, type StreamErrorLabels } from '../utils/streamErrorMessage';
import { applyStreamErrorToMessage, isUserFacingStreamError, type StreamErrorLabels } from '../utils/streamErrorMessage';
import type { StartChatTurnOptions } from '../types/chat-turn.types';
import type { ChatMessage, ChatRun } from '../types/chat.types';
import { applyShippingUiStateToMessage } from './useChat';
@@ -210,8 +211,14 @@ export function createStartChatTurn(options: CreateStartChatTurnOptions) {
hasTerminalEvent = true;
run.status = 'error';
const botMsg = ensureBotMessage();
if (botMsg) {
applyStreamErrorToMessage(botMsg, options.formatStreamError(data.message), options.streamErrorLabels());
// run.failed 与兼容用的 error 帧会连发;已写入的失败提示不再追加第二遍。
if (botMsg && !botMsg.stream_error) {
applyStreamErrorToMessage(
botMsg,
options.formatStreamError(data.message),
options.streamErrorLabels(),
{ userFacing: isUserFacingStreamError(data) },
);
}
}
} catch {