From 2c855083361c8317362ae5449591be95de5182a5 Mon Sep 17 00:00:00 2001 From: "DESKTOP-JKIE562\\PC" <1013397054@qq.com> Date: Wed, 29 Apr 2026 17:55:39 +0800 Subject: [PATCH] repair bug --- backend/app/api/chat.py | 53 +++++++++++++++++++ frontend/src/views/ChatView.vue | 92 ++++++++++++++++++++++++++------- 2 files changed, 127 insertions(+), 18 deletions(-) diff --git a/backend/app/api/chat.py b/backend/app/api/chat.py index 8a505234..e37559b0 100644 --- a/backend/app/api/chat.py +++ b/backend/app/api/chat.py @@ -193,6 +193,59 @@ def get_chat_sessions(): return jsonify(sessions) +@chat_bp.route('/sessions', methods=['DELETE']) +@require_agent_permission +def delete_all_chat_sessions(): + """ + 删除当前用户与当前 Agent 的所有历史会话 + """ + user = getattr(g, 'current_user', None) + agent = getattr(g, 'current_agent', None) + + if user is None or agent is None: + return jsonify({'error': 'Missing context'}), 500 + + chat_logs = db.session.query(ChatLog).filter_by(user_id=user.id, agent_id=agent.id).all() + chat_log_ids = [log.id for log in chat_logs] + if chat_log_ids: + from app.models import GeneralFeedback + db.session.query(Feedback).filter(Feedback.chat_log_id.in_(chat_log_ids)).delete(synchronize_session=False) + db.session.query(GeneralFeedback).filter(GeneralFeedback.chat_log_id.in_(chat_log_ids)).delete(synchronize_session=False) + db.session.query(ChatLog).filter(ChatLog.id.in_(chat_log_ids)).delete(synchronize_session=False) + db.session.commit() + + return jsonify({'status': 'ok'}) + + +@chat_bp.route('/sessions/', methods=['DELETE']) +@require_agent_permission +def delete_chat_session(conversation_id): + """ + 删除指定的会话记录 + """ + user = getattr(g, 'current_user', None) + agent = getattr(g, 'current_agent', None) + + if user is None or agent is None: + return jsonify({'error': 'Missing context'}), 500 + + chat_logs = db.session.query(ChatLog).filter_by( + user_id=user.id, + agent_id=agent.id, + conversation_id=conversation_id + ).all() + + chat_log_ids = [log.id for log in chat_logs] + if chat_log_ids: + from app.models import GeneralFeedback + db.session.query(Feedback).filter(Feedback.chat_log_id.in_(chat_log_ids)).delete(synchronize_session=False) + db.session.query(GeneralFeedback).filter(GeneralFeedback.chat_log_id.in_(chat_log_ids)).delete(synchronize_session=False) + db.session.query(ChatLog).filter(ChatLog.id.in_(chat_log_ids)).delete(synchronize_session=False) + db.session.commit() + + return jsonify({'status': 'ok'}) + + @chat_bp.route('/messages', methods=['GET']) @require_agent_permission def get_chat_messages(): diff --git a/frontend/src/views/ChatView.vue b/frontend/src/views/ChatView.vue index c2a35195..19c7b875 100644 --- a/frontend/src/views/ChatView.vue +++ b/frontend/src/views/ChatView.vue @@ -13,7 +13,7 @@ import { ref, onMounted, computed } from 'vue'; import { useI18n } from 'vue-i18n'; import { useChat } from '../composables/useChat'; -import { Send, Bot, Loader2, ArrowLeft, Plus, Menu, Star, Copy, Edit2, RotateCw, Check } from 'lucide-vue-next'; +import { Send, Bot, Loader2, ArrowLeft, Plus, Menu, Star, Copy, Edit2, RotateCw, Check, X, Trash2 } from 'lucide-vue-next'; import { marked } from 'marked'; import { useRoute, useRouter } from 'vue-router'; import { config } from '../config'; @@ -263,7 +263,9 @@ const getSourceLinks = (resources: any[] = []): SourceFileLink[] => { if (isNoisySourceName(name)) continue; // 根据配置决定是否显示 MD 文件 if ( (isMarkdownFileName(name) || isMarkdownByUrl(url))) continue; - const key = `${name}|${url}`; + + // Deduplicate by name if it looks like a filename, else by name + base URL + const key = looksLikeFileName(name) ? name : `${name}|${url.split('?')[0]}`; if (!map.has(key)) { map.set(key, { name, url }); } @@ -288,12 +290,12 @@ const getMessageLinks = (msg: any): SourceFileLink[] => { if (isNoisySourceName(link.name)) continue; if ( (isMarkdownFileName(link.name) || isMarkdownByUrl(link.url))) continue; if (isMarkdownFileName(link.name)) { - const key = `${link.name}|${link.url}`; + const key = looksLikeFileName(link.name) ? link.name : `${link.name}|${link.url.split('?')[0]}`; if (!map.has(key)) map.set(key, link); continue; } if (isNoisySourceUrl(link.url) && !looksLikeFileName(link.name)) continue; - const key = `${link.name}|${link.url}`; + const key = looksLikeFileName(link.name) ? link.name : `${link.name}|${link.url.split('?')[0]}`; if (!map.has(key)) map.set(key, link); } }; @@ -484,6 +486,43 @@ const loadHistory = async () => { } }; +const clearAllHistory = async () => { + if (!confirm('确定要清除全部聊天历史记录吗?此操作不可恢复。')) return; + try { + const res = await fetch(`/api/chat/sessions?agent_id=${agentId}`, { + method: 'DELETE', + headers: getAuthHeaders() + }); + if (res.ok) { + history.value = []; + if (conversationId.value) { + startNewChat(); + } + } + } catch (e) { + console.error('Failed to clear history', e); + } +}; + +const deleteHistoryItem = async (id: string, event: Event) => { + event.stopPropagation(); + if (!confirm('确定要删除这条聊天记录吗?')) return; + try { + const res = await fetch(`/api/chat/sessions/${id}?agent_id=${agentId}`, { + method: 'DELETE', + headers: getAuthHeaders() + }); + if (res.ok) { + history.value = history.value.filter(h => h.conversation_id !== id); + if (conversationId.value === id) { + startNewChat(); + } + } + } catch (e) { + console.error('Failed to delete history item', e); + } +}; + const loadConversation = async (id: string) => { if (conversationId.value === id) return; @@ -607,25 +646,42 @@ const submitFeedback = async () => { {{ $t('chat.history') }} - +
+ + +
{{ $t('chat.noHistory') }}
- +
+ + +