fix: 修复轮询机制下对话标题不自动更新的问题

- 后端:在标题生成后将 conversation_changed 事件添加到任务事件流
- 前端:在 taskPolling 中添加 conversation_changed 事件处理
- 前端:同步更新当前对话标题和对话列表中的标题
- 兼容:保留 WebSocket 的标题更新逻辑以向后兼容

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
JOJO 2026-04-04 15:12:18 +08:00
parent 321818f7ef
commit 6697da9581
3 changed files with 55 additions and 0 deletions

View File

@ -73,6 +73,25 @@ def generate_conversation_title_background(
if not ok:
return
# 添加标题更新事件到任务事件流(用于轮询机制)
try:
from server.tasks import task_manager
tasks = task_manager.list_tasks(username)
running_task = None
for task in tasks:
if task.status == "running" and getattr(task, "conversation_id", None) == conversation_id:
running_task = task
break
if running_task:
task_manager._append_event(
running_task,
'conversation_changed',
{'conversation_id': conversation_id, 'title': safe_title}
)
except Exception as exc:
debug_logger(f"[TitleGen] 添加任务事件失败: {exc}")
try:
socketio_instance.emit(
'conversation_changed',

View File

@ -116,6 +116,10 @@ export const taskPollingMethods = {
this.handleTokenUpdate(eventData, eventIdx);
break;
case 'conversation_changed':
this.handleConversationChanged(eventData, eventIdx);
break;
case 'conversation_resolved':
this.handleConversationResolved(eventData, eventIdx);
break;
@ -665,6 +669,29 @@ export const taskPollingMethods = {
}
},
handleConversationChanged(data: any, eventIdx: number) {
debugLog('[TaskPolling] 对话标题已更新, idx:', eventIdx, data);
if (data && data.conversation_id === this.currentConversationId) {
// 更新当前对话标题
if (data.title) {
this.currentConversationTitle = data.title;
debugLog('[TaskPolling] 更新当前对话标题:', data.title);
}
// 更新对话列表中的标题
if (data.title && Array.isArray(this.conversations)) {
const conv = this.conversations.find(c => c && c.id === data.conversation_id);
if (conv) {
conv.title = data.title;
debugLog('[TaskPolling] 更新对话列表中的标题:', data.title);
}
}
this.$forceUpdate();
}
},
handleConversationResolved(data: any) {
if (data && data.conversation_id) {
this.currentConversationId = data.conversation_id;

View File

@ -763,6 +763,15 @@ export async function initializeLegacySocket(ctx: any) {
if (ctx.initialRouteResolved) {
ctx.currentConversationId = data.conversation_id;
ctx.currentConversationTitle = data.title || '';
// 更新对话列表中的标题
if (data.title && Array.isArray(ctx.conversations)) {
const conv = ctx.conversations.find(c => c && c.id === data.conversation_id);
if (conv) {
conv.title = data.title;
}
}
ctx.promoteConversationToTop(data.conversation_id);
if (data.cleared) {