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

+3 -3
View File
@@ -253,12 +253,12 @@ def submit_feedback():
return jsonify({'error': 'Missing user or agent context'}), 500
# 构造查询:限定在当前用户和 Agent 范围内
query = ChatLog.query.filter_by(user_id=user.id, agent_id=agent.id)
chat_logs_query = db.session.query(ChatLog).filter_by(user_id=user.id, agent_id=agent.id)
if conversation_id:
query = query.filter_by(conversation_id=conversation_id)
chat_logs_query = chat_logs_query.filter_by(conversation_id=conversation_id)
# 取最新一条对话日志
chat_log = query.order_by(ChatLog.created_at.desc()).first()
chat_log = chat_logs_query.order_by(ChatLog.created_at.desc()).first()
if not chat_log:
return jsonify({'error': 'ChatLog not found'}), 404
+5 -5
View File
@@ -345,13 +345,13 @@ watch(
<template>
<div v-if="modelValue && file" class="fixed inset-0 z-[80] flex items-center justify-center bg-black/50 dark:bg-slate-950/70 backdrop-blur-sm p-4">
<div class="bg-white dark:bg-slate-900 rounded-2xl shadow-xl w-full max-w-5xl h-[85vh] flex flex-col overflow-hidden border border-slate-200 dark:border-slate-700">
<div class="px-6 py-4 border-b border-slate-200 dark:border-slate-700 flex items-center justify-between gap-4">
<div class="px-6 py-4 border-b border-slate-200 dark:border-slate-700 flex items-start justify-between gap-4">
<div class="min-w-0">
<div class="text-sm text-slate-500 dark:text-slate-400 uppercase tracking-wide">文件预览</div>
<div class="font-semibold text-slate-900 dark:text-slate-100 truncate">{{ file.name }}</div>
<a :href="file.url" target="_blank" class="text-xs text-brand-600 hover:text-brand-700 break-all">{{ file.url }}</a>
<div class="text-sm font-medium text-slate-500 dark:text-slate-400 tracking-wide">文件预览</div>
<div class="mt-1 font-semibold text-slate-900 dark:text-slate-100 break-words">{{ file.name }}</div>
<a :href="file.url" target="_blank" class="mt-1 block text-xs text-brand-600 hover:text-brand-700 break-all leading-6">{{ file.url }}</a>
</div>
<button @click="close" class="px-3 py-1.5 rounded-lg text-slate-600 dark:text-slate-300 hover:bg-slate-100 dark:hover:bg-slate-800">关闭</button>
<button type="button" @click="close" class="shrink-0 whitespace-nowrap inline-flex items-center justify-center px-4 py-2 rounded-xl border border-slate-200 dark:border-slate-700 text-slate-600 dark:text-slate-300 bg-white/80 dark:bg-slate-900/80 hover:bg-slate-100 dark:hover:bg-slate-800">关闭</button>
</div>
<div class="flex-1 overflow-auto p-4 bg-slate-50 dark:bg-slate-950">
+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'}]`;
}
+19 -1
View File
@@ -338,6 +338,24 @@ const shouldRenderMessage = (msg: any): boolean => {
return Array.isArray(msg?.attribution_parts) && msg.attribution_parts.length > 0;
};
// “正在生成回答...” 仅在当前轮次还没有任何可显示的 assistant 内容时显示。
// 一旦最后一条用户消息之后出现了可渲染的 assistant 内容(思考或答案文本等),
// 该 loading 气泡就会隐藏,避免与正文同时出现。
const shouldShowThinkingBubble = computed(() => {
if (!isThinking.value) return false;
let lastUserIndex = -1;
for (let i = messages.value.length - 1; i >= 0; i--) {
if (messages.value[i]?.role === 'user') {
lastUserIndex = i;
break;
}
}
if (lastUserIndex < 0) return true;
return !messages.value
.slice(lastUserIndex + 1)
.some((msg) => msg?.role === 'assistant' && shouldRenderMessage(msg));
});
const filePreviewVisible = ref(false);
const previewFile = ref<SourceFileLink | null>(null);
@@ -808,7 +826,7 @@ const submitFeedback = async () => {
</div>
</template>
<div v-if="isThinking" class="mx-auto flex max-w-5xl gap-4">
<div v-if="shouldShowThinkingBubble" class="mx-auto flex max-w-5xl gap-4">
<div class="mt-1 flex h-10 w-10 shrink-0 items-center justify-center rounded-xl bg-gradient-to-br from-cyan-500 to-blue-600 shadow-md shadow-cyan-500/30">
<Bot class="h-5 w-5 text-white" />
</div>