From eb04a6baa83e0e6a4898c7d5a55001465b6dcdc6 Mon Sep 17 00:00:00 2001 From: thzxx Date: Fri, 3 Apr 2026 12:39:59 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8E=86=E5=8F=B2=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E5=88=86=E9=A1=B5=20=E2=80=94=20=E4=BC=9A=E8=AF=9D=E4=B8=A2?= =?UTF-8?q?=E5=A4=B1=E5=AF=BC=E8=87=B4=E6=95=B0=E6=8D=AE=E4=B8=8D=E5=AE=8C?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题根因: 1. startNewSession() 未保存当前会话 → 点「新建会话」时旧会话丢失 2. 用户消息发送后未立即保存 → 刷新/关闭页面丢失对话 3. 通用错误处理中 saveCurrentSession() 缺少 await 修复: - startNewSession: 新建前先保存当前会话(有消息时) - sendMessage: 发送用户消息后立即 saveCurrentSession - catch 块中 saveCurrentSession 补上 await --- js/app.js | 10 +++++++++- js/components/input-area.js | 5 ++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/js/app.js b/js/app.js index d1f025c..73e9068 100644 --- a/js/app.js +++ b/js/app.js @@ -33,7 +33,15 @@ function createNewSession() { }; } -function startNewSession() { +async function startNewSession() { + // 先保存当前会话(如果有的话) + const db = state.get(KEYS.DB); + const currentSession = state.get(KEYS.CURRENT_SESSION); + if (db && currentSession && currentSession.messages.length > 0) { + currentSession.updatedAt = Date.now(); + await db.saveSession(currentSession); + } + const session = createNewSession(); state.set(KEYS.CURRENT_SESSION, session); state.set(KEYS.IS_HISTORY_VIEW, false); diff --git a/js/components/input-area.js b/js/components/input-area.js index 58ef29e..aab23a3 100644 --- a/js/components/input-area.js +++ b/js/components/input-area.js @@ -174,6 +174,9 @@ export async function sendMessage() { msgsToAdd.forEach(m => currentSession.messages.push(m)); renderMessages(); + // 立即保存(防止新建会话或刷新时丢失用户消息) + await saveCurrentSession(); + // 清空输入 chatInputEl.value = ''; pendingImages = []; @@ -307,7 +310,7 @@ export async function sendMessage() { currentSession.messages.push(partialMsg); const contentDiv = placeholder?.querySelector('.msg-content'); if (contentDiv) contentDiv.innerHTML = `

${existingContent}

[已中断]

`; - saveCurrentSession(); + await saveCurrentSession(); } else { if (placeholder) placeholder.remove(); }