fix: ghost session cleanup, new chat switching, and markdown overflow handling (#287)

This commit is contained in:
DigHuang
2026-03-04 18:54:47 +08:00
committed by GitHub
Unverified
parent 89028756e1
commit 76df84e68c
3 changed files with 37 additions and 7 deletions

View File

@@ -99,6 +99,7 @@ interface ChatState {
switchSession: (key: string) => void;
newSession: () => void;
deleteSession: (key: string) => Promise<void>;
cleanupEmptySession: () => void;
loadHistory: (quiet?: boolean) => Promise<void>;
sendMessage: (text: string, attachments?: Array<{ fileName: string; mimeType: string; fileSize: number; stagedPath: string; preview: string | null }>) => Promise<void>;
abortRun: () => Promise<void>;
@@ -973,8 +974,11 @@ export const useChatStore = create<ChatState>((set, get) => ({
}
}
if (!dedupedSessions.find((s) => s.key === nextSessionKey) && dedupedSessions.length > 0) {
// Current session not found at all — switch to the first available session
nextSessionKey = dedupedSessions[0].key;
// Current session not found in the backend list
const isNewEmptySession = get().messages.length === 0;
if (!isNewEmptySession) {
nextSessionKey = dedupedSessions[0].key;
}
}
const sessionsWithCurrent = !dedupedSessions.find((s) => s.key === nextSessionKey) && nextSessionKey
@@ -1153,6 +1157,27 @@ export const useChatStore = create<ChatState>((set, get) => ({
}));
},
// ── Cleanup empty session on navigate away ──
cleanupEmptySession: () => {
const { currentSessionKey, messages } = get();
// Only remove non-main sessions that were never used (no messages sent).
// This mirrors the "leavingEmpty" logic in switchSession so that creating
// a new session and immediately navigating away doesn't leave a ghost entry
// in the sidebar.
const isEmptyNonMain = !currentSessionKey.endsWith(':main') && messages.length === 0;
if (!isEmptyNonMain) return;
set((s) => ({
sessions: s.sessions.filter((sess) => sess.key !== currentSessionKey),
sessionLabels: Object.fromEntries(
Object.entries(s.sessionLabels).filter(([k]) => k !== currentSessionKey),
),
sessionLastActivity: Object.fromEntries(
Object.entries(s.sessionLastActivity).filter(([k]) => k !== currentSessionKey),
),
}));
},
// ── Load chat history ──
loadHistory: async (quiet = false) => {