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}
[已中断]