Browse Source

fix: defer chat history restore until DOM is ready — restoreSession and autoResumeSession now run after DOMContentLoaded so chat container exists before messages are appended

Made-with: Cursor
pull/6453/head
Priyanka Punukollu 1 month ago
parent
commit
8e3a00baf0
  1. 55
      agent/chat_ui.html

55
agent/chat_ui.html

@ -5539,7 +5539,7 @@
})(); })();
// ── Restore session from localStorage ── // ── Restore session from localStorage ──
(function restoreSession() { function restoreSession() {
try { try {
const saved = localStorage.getItem(STORAGE_KEY); const saved = localStorage.getItem(STORAGE_KEY);
if (!saved) return; if (!saved) return;
@ -5548,18 +5548,20 @@
history = hist; history = hist;
if (stats) sessionStats = stats; if (stats) sessionStats = stats;
emptyEl.style.display = 'none'; emptyEl.style.display = 'none';
// Messages appear silently — no notification needed // Replay messages into DOM (stored role is 'user'|'assistant'; addMessage expects 'user'|'agent')
// Replay messages into DOM for (let i = 0; i < hist.length; i++) {
for (let i = 0; i < hist.length; i += 2) { const msg = hist[i];
const u = hist[i]; if (!msg) continue;
const a = hist[i + 1]; if (msg.role === 'user') {
if (u) addMessage('user', u.content, null, true); addMessage('user', msg.content, null, true);
if (a) addMessage('agent', a.content, (a && a.meta) ? a.meta : null, true); } else {
addMessage('agent', msg.content, msg.meta || null, true);
}
} }
} catch { } catch (e) {
/* silently skip */ console.warn('restoreSession failed:', e);
} }
})(); }
function saveSession() { function saveSession() {
try { try {
@ -7028,7 +7030,7 @@
// ── Auto-resume last session on page load ── // ── Auto-resume last session on page load ──
// When STORAGE_KEY (flat cache) was empty — e.g. user never sent a message // When STORAGE_KEY (flat cache) was empty — e.g. user never sent a message
// this session, or the flat cache was cleared — try restoring from SESSIONS_KEY. // this session, or the flat cache was cleared — try restoring from SESSIONS_KEY.
(function autoResumeSession() { function autoResumeSession() {
if (history.length > 0) return; // already restored by restoreSession() if (history.length > 0) return; // already restored by restoreSession()
// If user deliberately clicked "New Chat" before reloading, respect that. // If user deliberately clicked "New Chat" before reloading, respect that.
if (localStorage.getItem('gf_new_chat')) { if (localStorage.getItem('gf_new_chat')) {
@ -7047,17 +7049,19 @@
localStorage.setItem(ACTIVE_SESSION_KEY, target.id); localStorage.setItem(ACTIVE_SESSION_KEY, target.id);
history = target.messages.slice(); history = target.messages.slice();
emptyEl.style.display = 'none'; emptyEl.style.display = 'none';
for (let i = 0; i < history.length; i += 2) { for (let i = 0; i < history.length; i++) {
if (history[i]) addMessage('user', history[i].content, null, true); const msg = history[i];
if (history[i + 1]) { if (!msg) continue;
const a = history[i + 1]; if (msg.role === 'user') {
addMessage('agent', a.content, (a && a.meta) ? a.meta : null, true); addMessage('user', msg.content, null, true);
} else {
addMessage('agent', msg.content, msg.meta || null, true);
} }
} }
document.title = target.title + ' — Ghostfolio'; document.title = target.title + ' — Ghostfolio';
updateHeaderTitle(); updateHeaderTitle();
updateChatsBadge(); updateChatsBadge();
})(); }
// ── Rename session inline in drawer ── // ── Rename session inline in drawer ──
function startDrawerRename(id, titleEl) { function startDrawerRename(id, titleEl) {
@ -8785,6 +8789,21 @@
document.getElementById('memory-panel').classList.remove('open'); document.getElementById('memory-panel').classList.remove('open');
updateHeaderTitle(); updateHeaderTitle();
}); });
// Run after DOM is fully ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function () {
restoreSession();
if (typeof autoResumeSession === 'function') {
autoResumeSession();
}
});
} else {
restoreSession();
if (typeof autoResumeSession === 'function') {
autoResumeSession();
}
}
</script> </script>
</body> </body>
</html> </html>

Loading…
Cancel
Save