fix: RAG检索状态消息在AI回复后自动清除

给 appendSystemMessage 增加可选 tempClass 参数,
所有 RAG 状态消息标记为 'rag-status' class,
流式结束后和报错时统一移除这些临时消息。
This commit is contained in:
Metona
2026-04-05 00:44:12 +08:00
parent 1e3f4d4ba5
commit 6b793f669b
2 changed files with 13 additions and 6 deletions
+2 -1
View File
@@ -309,9 +309,10 @@ export function appendAssistantPlaceholder() {
} }
/** 追加系统提示消息 */ /** 追加系统提示消息 */
export function appendSystemMessage(text) { export function appendSystemMessage(text, tempClass) {
const div = document.createElement('div'); const div = document.createElement('div');
div.style.cssText = 'text-align:center;padding:8px;font-size:12px;color:var(--text-muted);'; div.style.cssText = 'text-align:center;padding:8px;font-size:12px;color:var(--text-muted);';
if (tempClass) div.classList.add(tempClass);
div.textContent = text; div.textContent = text;
emptyStateEl.style.display = 'none'; emptyStateEl.style.display = 'none';
messagesContainerEl.style.display = ''; messagesContainerEl.style.display = '';
+11 -5
View File
@@ -404,7 +404,7 @@ export async function sendMessage() {
// RAG 检索增强 // RAG 检索增强
let ragSources = null; let ragSources = null;
if (isRagEnabled()) { if (isRagEnabled()) {
appendSystemMessage('🧠 正在检索知识库...'); appendSystemMessage('🧠 正在检索知识库...', 'rag-status');
try { try {
const ragResult = await performRagRetrieval(text); const ragResult = await performRagRetrieval(text);
if (ragResult && ragResult.results && ragResult.results.length > 0) { if (ragResult && ragResult.results && ragResult.results.length > 0) {
@@ -434,7 +434,7 @@ export async function sendMessage() {
if (ragContext.length + templateOverhead > ragBudgetChars) { if (ragContext.length + templateOverhead > ragBudgetChars) {
ragContext = ragContext.slice(0, Math.max(0, ragBudgetChars - templateOverhead - 50)) + '\n\n[知识库内容过长,已截取最相关的部分]'; ragContext = ragContext.slice(0, Math.max(0, ragBudgetChars - templateOverhead - 50)) + '\n\n[知识库内容过长,已截取最相关的部分]';
appendSystemMessage(`⚠️ 知识库内容已截取(预算 ${ragBudgetChars} 字符)`); appendSystemMessage(`⚠️ 知识库内容已截取(预算 ${ragBudgetChars} 字符)`, 'rag-status');
} }
const ragPrompt = `你是一个知识库问答助手。以下是检索到的相关文档片段,请基于这些内容回答用户的问题。如果检索到的内容无法回答问题,请如实说明。 const ragPrompt = `你是一个知识库问答助手。以下是检索到的相关文档片段,请基于这些内容回答用户的问题。如果检索到的内容无法回答问题,请如实说明。
@@ -455,13 +455,13 @@ ${ragContext}
text: truncate(r.text, 100) text: truncate(r.text, 100)
})); }));
const sourceNames = [...new Set(ragSources.map(s => s.filename))]; const sourceNames = [...new Set(ragSources.map(s => s.filename))];
appendSystemMessage(`🧠 已检索 ${ragSources.length} 个相关片段(来源:${sourceNames.join('、')}`); appendSystemMessage(`🧠 已检索 ${ragSources.length} 个相关片段(来源:${sourceNames.join('、')}`, 'rag-status');
} else { } else {
appendSystemMessage('🧠 知识库未检索到相关内容,使用通用知识回答'); appendSystemMessage('🧠 知识库未检索到相关内容,使用通用知识回答', 'rag-status');
} }
} catch (ragErr) { } catch (ragErr) {
console.error('[RAG] 检索失败:', ragErr); console.error('[RAG] 检索失败:', ragErr);
appendSystemMessage(`🧠 知识库检索失败: ${ragErr.message}`); appendSystemMessage(`🧠 知识库检索失败: ${ragErr.message}`, 'rag-status');
} }
} }
@@ -485,6 +485,9 @@ ${ragContext}
} }
}, abortController); }, abortController);
// 清除 RAG 临时状态消息
document.querySelectorAll('.rag-status').forEach(el => el.remove());
// 如果流结束但没有任何内容 // 如果流结束但没有任何内容
if (!assistantContent && !thinkContent) { if (!assistantContent && !thinkContent) {
appendSystemMessage('⚠️ 模型未返回任何内容,可能是上下文过长或模型不支持当前请求'); appendSystemMessage('⚠️ 模型未返回任何内容,可能是上下文过长或模型不支持当前请求');
@@ -510,6 +513,9 @@ ${ragContext}
} catch (err) { } catch (err) {
console.error('[InputArea] 流式聊天错误:', err); console.error('[InputArea] 流式聊天错误:', err);
// 清除 RAG 临时状态消息
document.querySelectorAll('.rag-status').forEach(el => el.remove());
if (err.name === 'AbortError') { if (err.name === 'AbortError') {
const placeholder = document.querySelector('#messagesContainer .message.assistant.loading'); const placeholder = document.querySelector('#messagesContainer .message.assistant.loading');
const loadingDots = placeholder?.querySelector('.loading-dots'); const loadingDots = placeholder?.querySelector('.loading-dots');