Fix stale deleted sessions in sidebar (#1203)

Co-authored-by: ghreprimand <203024559+ghreprimand@users.noreply.github.com>
This commit is contained in:
ghreprimand
2026-06-02 09:52:22 -05:00
committed by GitHub
parent 87babb58d5
commit e72b9a8a95
3 changed files with 70 additions and 8 deletions

View File

@@ -3130,10 +3130,7 @@ function initializeEventListeners() {
const idx = sessions.findIndex(s => s.id === currentId);
const nextSession = sessions.filter(s => !s.archived && s.id !== currentId)[Math.max(0, idx)] ||
sessions.find(s => !s.archived && s.id !== currentId);
const res = await fetch(`${API_BASE}/api/session/${currentId}/archive`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
});
const res = await fetch(`${API_BASE}/api/session/${currentId}`, { method: 'DELETE' });
if (res.ok) {
await sessionModule.loadSessions();
if (nextSession) {

View File

@@ -78,6 +78,42 @@ function _deselectCurrentSession(sid) {
if (window._updateSendBtnIcon) window._updateSendBtnIcon();
}
function _removeSessionFromLocalState(sid) {
if (!sid) return;
const id = String(sid);
sessions = sessions.filter(s => String(s.id) !== id);
_selectedIds.delete(id);
try {
const savedOrder = Storage.get('session-order');
if (savedOrder) {
const orderIds = JSON.parse(savedOrder);
if (Array.isArray(orderIds) && orderIds.some(x => String(x) === id)) {
Storage.set('session-order', JSON.stringify(orderIds.filter(x => String(x) !== id)));
}
}
} catch (e) {
console.warn('Failed to prune deleted session order:', e);
}
document.querySelectorAll('.list-item[data-session-id]').forEach(el => {
if (String(el.dataset.sessionId) === id) el.remove();
});
_deselectCurrentSession(id);
}
function _normalizeSessionsList(fetched) {
if (!Array.isArray(fetched)) return [];
const seen = new Set();
const unique = [];
for (const session of fetched) {
if (!session || session.id == null) continue;
const id = String(session.id);
if (seen.has(id)) continue;
seen.add(id);
unique.push(session);
}
return unique;
}
// Initialize dependencies from app.js (no-op: dependencies now imported directly)
export function initDependencies() {}
@@ -620,15 +656,13 @@ function createSessionItem(s) {
_forceSidebarOpen();
return;
}
// Optimistic: remove from UI immediately
const sessionEl = document.querySelector(`.list-item[data-session-id="${s.id}"]`);
if (sessionEl) sessionEl.remove();
const wasCurrentSession = currentSessionId === s.id;
// If streaming, abort it before deleting
if (wasCurrentSession && window.chatModule && window.chatModule.abortCurrentRequest) {
window.chatModule.abortCurrentRequest();
}
_deselectCurrentSession(s.id);
_removeSessionFromLocalState(s.id);
_skipAutoSelect = true;
// Clean up persistent chat mapping
try {
@@ -1321,7 +1355,7 @@ export async function loadSessions() {
const res = await fetch(`${API_BASE}/api/sessions`);
fetched = await res.json();
}
sessions = fetched;
sessions = _normalizeSessionsList(fetched);
renderSessionList();
const sessionsSection = uiModule.el('sessions-section');