fix: prevent duplicate history loading
This commit is contained in:
parent
e8411affc1
commit
ef4479866e
@ -122,6 +122,7 @@ const appOptions = {
|
|||||||
skipConversationLoadedEvent: false,
|
skipConversationLoadedEvent: false,
|
||||||
skipConversationHistoryReload: false,
|
skipConversationHistoryReload: false,
|
||||||
_scrollListenerReady: false,
|
_scrollListenerReady: false,
|
||||||
|
historyLoading: false,
|
||||||
|
|
||||||
// 工具控制菜单
|
// 工具控制菜单
|
||||||
icons: ICONS,
|
icons: ICONS,
|
||||||
@ -844,16 +845,6 @@ const appOptions = {
|
|||||||
this.ensureScrollListener();
|
this.ensureScrollListener();
|
||||||
});
|
});
|
||||||
|
|
||||||
const activeConversationId = this.currentConversationId;
|
|
||||||
if (activeConversationId && !activeConversationId.startsWith('temp_')) {
|
|
||||||
setTimeout(() => {
|
|
||||||
if (this.currentConversationId === activeConversationId) {
|
|
||||||
this.fetchAndDisplayHistory();
|
|
||||||
this.fetchConversationTokenStatistics();
|
|
||||||
this.updateCurrentContextTokens();
|
|
||||||
}
|
|
||||||
}, 250);
|
|
||||||
}
|
|
||||||
this.logMessageState('resetAllStates:after-cleanup', { reason });
|
this.logMessageState('resetAllStates:after-cleanup', { reason });
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -1099,78 +1090,87 @@ const appOptions = {
|
|||||||
// 关键功能:获取并显示历史对话内容
|
// 关键功能:获取并显示历史对话内容
|
||||||
// ==========================================
|
// ==========================================
|
||||||
async fetchAndDisplayHistory() {
|
async fetchAndDisplayHistory() {
|
||||||
console.log('开始获取历史对话内容...');
|
if (this.historyLoading) {
|
||||||
this.logMessageState('fetchAndDisplayHistory:start', { conversationId: this.currentConversationId });
|
console.log('历史消息正在加载,跳过重复请求');
|
||||||
|
|
||||||
if (!this.currentConversationId || this.currentConversationId.startsWith('temp_')) {
|
|
||||||
console.log('没有当前对话ID,跳过历史加载');
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
this.historyLoading = true;
|
||||||
try {
|
try {
|
||||||
// 使用专门的API获取对话消息历史
|
console.log('开始获取历史对话内容...');
|
||||||
const messagesResponse = await fetch(`/api/conversations/${this.currentConversationId}/messages`);
|
this.logMessageState('fetchAndDisplayHistory:start', { conversationId: this.currentConversationId });
|
||||||
|
|
||||||
if (!messagesResponse.ok) {
|
if (!this.currentConversationId || this.currentConversationId.startsWith('temp_')) {
|
||||||
console.warn('无法获取消息历史,尝试备用方法');
|
console.log('没有当前对话ID,跳过历史加载');
|
||||||
// 备用方案:通过状态API获取
|
|
||||||
const statusResponse = await fetch('/api/status');
|
|
||||||
const status = await statusResponse.json();
|
|
||||||
console.log('系统状态:', status);
|
|
||||||
this.applyStatusSnapshot(status);
|
|
||||||
|
|
||||||
// 如果状态中有对话历史字段
|
|
||||||
if (status.conversation_history && Array.isArray(status.conversation_history)) {
|
|
||||||
this.renderHistoryMessages(status.conversation_history);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('备用方案也无法获取历史消息');
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const messagesData = await messagesResponse.json();
|
try {
|
||||||
console.log('获取到消息数据:', messagesData);
|
// 使用专门的API获取对话消息历史
|
||||||
|
const messagesResponse = await fetch(`/api/conversations/${this.currentConversationId}/messages`);
|
||||||
|
|
||||||
if (messagesData.success && messagesData.data && messagesData.data.messages) {
|
if (!messagesResponse.ok) {
|
||||||
const messages = messagesData.data.messages;
|
console.warn('无法获取消息历史,尝试备用方法');
|
||||||
console.log(`发现 ${messages.length} 条历史消息`);
|
// 备用方案:通过状态API获取
|
||||||
|
const statusResponse = await fetch('/api/status');
|
||||||
|
const status = await statusResponse.json();
|
||||||
|
console.log('系统状态:', status);
|
||||||
|
this.applyStatusSnapshot(status);
|
||||||
|
|
||||||
if (messages.length > 0) {
|
// 如果状态中有对话历史字段
|
||||||
// 清空当前显示的消息
|
if (status.conversation_history && Array.isArray(status.conversation_history)) {
|
||||||
this.logMessageState('fetchAndDisplayHistory:before-clear-existing');
|
this.renderHistoryMessages(status.conversation_history);
|
||||||
this.messages = [];
|
return;
|
||||||
this.logMessageState('fetchAndDisplayHistory:after-clear-existing');
|
}
|
||||||
|
|
||||||
// 渲染历史消息 - 这是关键功能
|
console.log('备用方案也无法获取历史消息');
|
||||||
this.renderHistoryMessages(messages);
|
return;
|
||||||
|
|
||||||
// 滚动到底部
|
|
||||||
this.$nextTick(() => {
|
|
||||||
this.scrollToBottom();
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log('历史对话内容显示完成');
|
|
||||||
} else {
|
|
||||||
console.log('对话存在但没有历史消息');
|
|
||||||
this.logMessageState('fetchAndDisplayHistory:no-history-clear');
|
|
||||||
this.messages = [];
|
|
||||||
this.logMessageState('fetchAndDisplayHistory:no-history-cleared');
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
console.log('消息数据格式不正确:', messagesData);
|
|
||||||
this.logMessageState('fetchAndDisplayHistory:invalid-data-clear');
|
|
||||||
this.messages = [];
|
|
||||||
this.logMessageState('fetchAndDisplayHistory:invalid-data-cleared');
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
const messagesData = await messagesResponse.json();
|
||||||
console.error('获取历史对话失败:', error);
|
console.log('获取到消息数据:', messagesData);
|
||||||
console.log('尝试不显示错误弹窗,仅在控制台记录');
|
|
||||||
// 不显示alert,避免打断用户体验
|
if (messagesData.success && messagesData.data && messagesData.data.messages) {
|
||||||
this.logMessageState('fetchAndDisplayHistory:error-clear', { error: error?.message || String(error) });
|
const messages = messagesData.data.messages;
|
||||||
this.messages = [];
|
console.log(`发现 ${messages.length} 条历史消息`);
|
||||||
this.logMessageState('fetchAndDisplayHistory:error-cleared');
|
|
||||||
|
if (messages.length > 0) {
|
||||||
|
// 清空当前显示的消息
|
||||||
|
this.logMessageState('fetchAndDisplayHistory:before-clear-existing');
|
||||||
|
this.messages = [];
|
||||||
|
this.logMessageState('fetchAndDisplayHistory:after-clear-existing');
|
||||||
|
|
||||||
|
// 渲染历史消息 - 这是关键功能
|
||||||
|
this.renderHistoryMessages(messages);
|
||||||
|
|
||||||
|
// 滚动到底部
|
||||||
|
this.$nextTick(() => {
|
||||||
|
this.scrollToBottom();
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('历史对话内容显示完成');
|
||||||
|
} else {
|
||||||
|
console.log('对话存在但没有历史消息');
|
||||||
|
this.logMessageState('fetchAndDisplayHistory:no-history-clear');
|
||||||
|
this.messages = [];
|
||||||
|
this.logMessageState('fetchAndDisplayHistory:no-history-cleared');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log('消息数据格式不正确:', messagesData);
|
||||||
|
this.logMessageState('fetchAndDisplayHistory:invalid-data-clear');
|
||||||
|
this.messages = [];
|
||||||
|
this.logMessageState('fetchAndDisplayHistory:invalid-data-cleared');
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('获取历史对话失败:', error);
|
||||||
|
console.log('尝试不显示错误弹窗,仅在控制台记录');
|
||||||
|
// 不显示alert,避免打断用户体验
|
||||||
|
this.logMessageState('fetchAndDisplayHistory:error-clear', { error: error?.message || String(error) });
|
||||||
|
this.messages = [];
|
||||||
|
this.logMessageState('fetchAndDisplayHistory:error-cleared');
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
this.historyLoading = false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user