fix: feedback 500错误和多余气泡

This commit is contained in:
sophie committed 2026-04-17 18:26:10 +08:00
1 parent 7bc6a8072b
commit 8336bc9480
4 files changed
+54 -25

No files matched your search

+27 -16
View File
@@ -149,7 +149,7 @@ export function useChat() {
* 用途:发送消息并处理 SSE 流式响应
* 核心逻辑:
* 1. 乐观更新 UI (立即显示用户消息)
* 2. 创建空的 AI 消息占位
* 2. Create assistant message lazily only after receiving real answer chunks.
* 3. 发起 Fetch POST 请求
* 4. 获取 Reader 流式读取
* 5. 逐块解码并追加到当前 AI 消息中
@@ -165,13 +165,19 @@ export function useChat() {
});
// 1. 乐观 UI 更新:立即将用户消息推入列表
messages.value.push({ role: 'user', content: query });
// 创建一个空的 Assistant 消息用于流式追加,并记录其索引
const botMsgIndex = messages.value.push({
role: 'assistant',
content: '',
retriever_resources: [],
attribution_parts: []
}) - 1;
// Lazily create assistant message only when answer/error arrives.
let botMsgIndex: number | null = null;
const ensureBotMessage = () => {
if (botMsgIndex === null) {
botMsgIndex = messages.value.push({
role: 'assistant',
content: '',
retriever_resources: [],
attribution_parts: []
}) - 1;
}
return messages.value[botMsgIndex] || null;
};
isThinking.value = true;
@@ -232,8 +238,6 @@ export function useChat() {
try {
const data = JSON.parse(payload);
const botMsg = messages.value[botMsgIndex];
if (!botMsg) continue;
console.debug('[chat][sse] event', {
event: data?.event,
answerLength: typeof data?.answer === 'string' ? data.answer.length : 0,
@@ -242,9 +246,9 @@ export function useChat() {
});
if (data.event === 'message') {
// event: message LLM 返回文本块事件,即:完整的文本以分块的方式输出。
if (typeof data.answer === 'string' && data.answer.length > 0) {
// answer: LLM 返回文本块内容
const botMsg = ensureBotMessage();
if (!botMsg) continue;
botMsg.content += data.answer;
console.debug('[chat][sse] append answer chunk', {
event: data.event,
@@ -252,13 +256,16 @@ export function useChat() {
total: botMsg.content.length,
});
}
applyEventMetadata(botMsg, data);
if (botMsgIndex !== null && messages.value[botMsgIndex]) {
applyEventMetadata(messages.value[botMsgIndex], data);
}
} else if (data.event === 'message_end') {
if (data.conversation_id) {
conversationId.value = data.conversation_id;
}
// Some providers may put final answer on message_end event.
if (typeof data.answer === 'string' && data.answer.length > 0) {
const botMsg = ensureBotMessage();
if (!botMsg) continue;
botMsg.content += data.answer;
console.debug('[chat][sse] append final answer', {
event: data.event,
@@ -266,8 +273,12 @@ export function useChat() {
total: botMsg.content.length,
});
}
applyEventMetadata(botMsg, data);
if (botMsgIndex !== null && messages.value[botMsgIndex]) {
applyEventMetadata(messages.value[botMsgIndex], data);
}
} else if (data.event === 'error') {
const botMsg = ensureBotMessage();
if (!botMsg) continue;
botMsg.content += `\n[Error: ${data.message}]`;
console.error('[chat][sse] backend error event', data);
}
@@ -279,7 +290,7 @@ export function useChat() {
}
} catch (e: any) {
console.error('[chat][send] failed', e);
const botMsg = messages.value[botMsgIndex];
const botMsg = ensureBotMessage();
if (botMsg) {
botMsg.content += `\n[Error: ${e.message || 'Network Issue'}]`;
}