repair bug
This commit is contained in:
1 parent
dc805b250f
commit
2c85508336
2 files changed
+127
-18
No files matched your search
@@ -193,6 +193,59 @@ def get_chat_sessions():
|
|||||||
return jsonify(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/<conversation_id>', 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'])
|
@chat_bp.route('/messages', methods=['GET'])
|
||||||
@require_agent_permission
|
@require_agent_permission
|
||||||
def get_chat_messages():
|
def get_chat_messages():
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
import { ref, onMounted, computed } from 'vue';
|
import { ref, onMounted, computed } from 'vue';
|
||||||
import { useI18n } from 'vue-i18n';
|
import { useI18n } from 'vue-i18n';
|
||||||
import { useChat } from '../composables/useChat';
|
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 { marked } from 'marked';
|
||||||
import { useRoute, useRouter } from 'vue-router';
|
import { useRoute, useRouter } from 'vue-router';
|
||||||
import { config } from '../config';
|
import { config } from '../config';
|
||||||
@@ -263,7 +263,9 @@ const getSourceLinks = (resources: any[] = []): SourceFileLink[] => {
|
|||||||
if (isNoisySourceName(name)) continue;
|
if (isNoisySourceName(name)) continue;
|
||||||
// 根据配置决定是否显示 MD 文件
|
// 根据配置决定是否显示 MD 文件
|
||||||
if ( (isMarkdownFileName(name) || isMarkdownByUrl(url))) continue;
|
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)) {
|
if (!map.has(key)) {
|
||||||
map.set(key, { name, url });
|
map.set(key, { name, url });
|
||||||
}
|
}
|
||||||
@@ -288,12 +290,12 @@ const getMessageLinks = (msg: any): SourceFileLink[] => {
|
|||||||
if (isNoisySourceName(link.name)) continue;
|
if (isNoisySourceName(link.name)) continue;
|
||||||
if ( (isMarkdownFileName(link.name) || isMarkdownByUrl(link.url))) continue;
|
if ( (isMarkdownFileName(link.name) || isMarkdownByUrl(link.url))) continue;
|
||||||
if (isMarkdownFileName(link.name)) {
|
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);
|
if (!map.has(key)) map.set(key, link);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (isNoisySourceUrl(link.url) && !looksLikeFileName(link.name)) 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);
|
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) => {
|
const loadConversation = async (id: string) => {
|
||||||
if (conversationId.value === id) return;
|
if (conversationId.value === id) return;
|
||||||
|
|
||||||
@@ -607,25 +646,42 @@ const submitFeedback = async () => {
|
|||||||
<span class="text-sm font-semibold uppercase tracking-[0.14em] text-slate-500 dark:text-slate-300">{{ $t('chat.history') }}</span>
|
<span class="text-sm font-semibold uppercase tracking-[0.14em] text-slate-500 dark:text-slate-300">{{ $t('chat.history') }}</span>
|
||||||
<ThemeToggleButton />
|
<ThemeToggleButton />
|
||||||
</div>
|
</div>
|
||||||
<button @click="startNewChat(); isSidebarOpen = false;" class="mt-3 inline-flex w-[92%] max-w-[320px] self-center items-center justify-center gap-2 rounded-xl bg-gradient-to-r from-cyan-500 to-blue-500 px-5 py-2.5 text-sm font-semibold text-white shadow-sm shadow-cyan-500/20 transition-all hover:from-cyan-400 hover:to-blue-400 hover:shadow-md hover:shadow-cyan-500/25 md:mt-4 md:w-[88%] md:max-w-none md:py-3">
|
<div class="mt-3 flex items-center gap-2 md:mt-4">
|
||||||
<Plus class="h-4 w-4" /> {{ $t('chat.newChat') }}
|
<button @click="startNewChat(); isSidebarOpen = false;" class="flex-1 inline-flex items-center justify-center gap-2 rounded-xl bg-gradient-to-r from-cyan-500 to-blue-500 px-4 py-2.5 text-sm font-semibold text-white shadow-sm shadow-cyan-500/20 transition-all hover:from-cyan-400 hover:to-blue-400 hover:shadow-md hover:shadow-cyan-500/25 md:py-3">
|
||||||
</button>
|
<Plus class="h-4 w-4" /> {{ $t('chat.newChat') }}
|
||||||
|
</button>
|
||||||
|
<button v-if="history.length > 0" @click="clearAllHistory" class="inline-flex items-center justify-center gap-1 rounded-xl border border-red-200 bg-red-50 px-2 py-2.5 text-xs font-semibold text-red-600 transition-all hover:bg-red-100 hover:text-red-700 dark:border-red-900/30 dark:bg-red-900/20 dark:text-red-400 dark:hover:bg-red-900/40 md:px-3 md:py-3" title="全部清除">
|
||||||
|
<Trash2 class="h-4 w-4" /> <span class="hidden md:inline">全部清除</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="flex-1 overflow-y-auto px-2.5 py-2.5 scrollbar-thin md:px-3 md:py-3">
|
<div class="flex-1 overflow-y-auto px-2.5 py-2.5 scrollbar-thin md:px-3 md:py-3">
|
||||||
<div v-if="history.length === 0" class="py-8 text-center text-sm italic text-slate-400 md:py-10">{{ $t('chat.noHistory') }}</div>
|
<div v-if="history.length === 0" class="py-8 text-center text-sm italic text-slate-400 md:py-10">{{ $t('chat.noHistory') }}</div>
|
||||||
<div v-else class="divide-y divide-slate-200/70 dark:divide-slate-700/70">
|
<div v-else class="divide-y divide-slate-200/70 dark:divide-slate-700/70">
|
||||||
<button
|
<div
|
||||||
v-for="item in history"
|
v-for="item in history"
|
||||||
:key="item.conversation_id"
|
:key="item.conversation_id"
|
||||||
class="w-full truncate px-2 py-3 text-left text-sm transition-colors md:px-3"
|
class="group relative flex w-full items-center justify-between transition-colors"
|
||||||
:class="conversationId === item.conversation_id
|
:class="conversationId === item.conversation_id ? 'bg-slate-100/50 dark:bg-slate-800/30' : 'hover:bg-slate-100/70 dark:hover:bg-slate-800/60'"
|
||||||
? 'font-semibold text-cyan-700 dark:text-cyan-200'
|
>
|
||||||
: 'text-slate-600 hover:bg-slate-100/70 hover:text-slate-900 dark:text-slate-300 dark:hover:bg-slate-800/60 dark:hover:text-slate-100'"
|
<button
|
||||||
@click="loadConversation(item.conversation_id); isSidebarOpen = false;"
|
class="flex-1 truncate px-2 py-3 text-left text-sm md:px-3 pr-8"
|
||||||
>
|
:class="conversationId === item.conversation_id
|
||||||
{{ item.title || $t('chat.newChat') }}
|
? 'font-semibold text-cyan-700 dark:text-cyan-200'
|
||||||
</button>
|
: 'text-slate-600 hover:text-slate-900 dark:text-slate-300 dark:hover:text-slate-100'"
|
||||||
|
@click="loadConversation(item.conversation_id); isSidebarOpen = false;"
|
||||||
|
>
|
||||||
|
{{ item.title || $t('chat.newChat') }}
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
@click="deleteHistoryItem(item.conversation_id, $event)"
|
||||||
|
class="absolute right-2 p-1.5 text-slate-400 opacity-0 transition-opacity hover:text-red-500 group-hover:opacity-100 dark:hover:text-red-400"
|
||||||
|
title="清除该记录"
|
||||||
|
>
|
||||||
|
<X class="h-4 w-4" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user