From 6b793f669b6b34606aa2d567d7f28cfbdda1d211 Mon Sep 17 00:00:00 2001 From: Metona Date: Sun, 5 Apr 2026 00:44:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20RAG=E6=A3=80=E7=B4=A2=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=9C=A8AI=E5=9B=9E=E5=A4=8D=E5=90=8E?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=B8=85=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 给 appendSystemMessage 增加可选 tempClass 参数, 所有 RAG 状态消息标记为 'rag-status' class, 流式结束后和报错时统一移除这些临时消息。 --- js/components/chat-area.js | 3 ++- js/components/input-area.js | 16 +++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/js/components/chat-area.js b/js/components/chat-area.js index fc0b097..7dded6c 100644 --- a/js/components/chat-area.js +++ b/js/components/chat-area.js @@ -309,9 +309,10 @@ export function appendAssistantPlaceholder() { } /** 追加系统提示消息 */ -export function appendSystemMessage(text) { +export function appendSystemMessage(text, tempClass) { const div = document.createElement('div'); div.style.cssText = 'text-align:center;padding:8px;font-size:12px;color:var(--text-muted);'; + if (tempClass) div.classList.add(tempClass); div.textContent = text; emptyStateEl.style.display = 'none'; messagesContainerEl.style.display = ''; diff --git a/js/components/input-area.js b/js/components/input-area.js index 13a5422..39e0519 100644 --- a/js/components/input-area.js +++ b/js/components/input-area.js @@ -404,7 +404,7 @@ export async function sendMessage() { // RAG 检索增强 let ragSources = null; if (isRagEnabled()) { - appendSystemMessage('🧠 正在检索知识库...'); + appendSystemMessage('🧠 正在检索知识库...', 'rag-status'); try { const ragResult = await performRagRetrieval(text); if (ragResult && ragResult.results && ragResult.results.length > 0) { @@ -434,7 +434,7 @@ export async function sendMessage() { if (ragContext.length + templateOverhead > ragBudgetChars) { ragContext = ragContext.slice(0, Math.max(0, ragBudgetChars - templateOverhead - 50)) + '\n\n[知识库内容过长,已截取最相关的部分]'; - appendSystemMessage(`⚠️ 知识库内容已截取(预算 ${ragBudgetChars} 字符)`); + appendSystemMessage(`⚠️ 知识库内容已截取(预算 ${ragBudgetChars} 字符)`, 'rag-status'); } const ragPrompt = `你是一个知识库问答助手。以下是检索到的相关文档片段,请基于这些内容回答用户的问题。如果检索到的内容无法回答问题,请如实说明。 @@ -455,13 +455,13 @@ ${ragContext} text: truncate(r.text, 100) })); const sourceNames = [...new Set(ragSources.map(s => s.filename))]; - appendSystemMessage(`🧠 已检索 ${ragSources.length} 个相关片段(来源:${sourceNames.join('、')})`); + appendSystemMessage(`🧠 已检索 ${ragSources.length} 个相关片段(来源:${sourceNames.join('、')})`, 'rag-status'); } else { - appendSystemMessage('🧠 知识库未检索到相关内容,使用通用知识回答'); + appendSystemMessage('🧠 知识库未检索到相关内容,使用通用知识回答', 'rag-status'); } } catch (ragErr) { console.error('[RAG] 检索失败:', ragErr); - appendSystemMessage(`🧠 知识库检索失败: ${ragErr.message}`); + appendSystemMessage(`🧠 知识库检索失败: ${ragErr.message}`, 'rag-status'); } } @@ -485,6 +485,9 @@ ${ragContext} } }, abortController); + // 清除 RAG 临时状态消息 + document.querySelectorAll('.rag-status').forEach(el => el.remove()); + // 如果流结束但没有任何内容 if (!assistantContent && !thinkContent) { appendSystemMessage('⚠️ 模型未返回任何内容,可能是上下文过长或模型不支持当前请求'); @@ -510,6 +513,9 @@ ${ragContext} } catch (err) { console.error('[InputArea] 流式聊天错误:', err); + // 清除 RAG 临时状态消息 + document.querySelectorAll('.rag-status').forEach(el => el.remove()); + if (err.name === 'AbortError') { const placeholder = document.querySelector('#messagesContainer .message.assistant.loading'); const loadingDots = placeholder?.querySelector('.loading-dots');