From a8e26362b9c909b447673a2338993f8c7ba75535 Mon Sep 17 00:00:00 2001 From: Felix <24791380+vcfgv@users.noreply.github.com> Date: Thu, 12 Feb 2026 12:09:14 +0800 Subject: [PATCH] refactor: Directly add new chat session to state upon creation (#62) --- src/stores/chat.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/stores/chat.ts b/src/stores/chat.ts index e5f1fe7f2..cd7d93306 100644 --- a/src/stores/chat.ts +++ b/src/stores/chat.ts @@ -383,9 +383,9 @@ export const useChatStore = create((set, get) => ({ const sessionsWithCurrent = !dedupedSessions.find((s) => s.key === nextSessionKey) && nextSessionKey ? [ - ...dedupedSessions, - { key: nextSessionKey, displayName: nextSessionKey }, - ] + ...dedupedSessions, + { key: nextSessionKey, displayName: nextSessionKey }, + ] : dedupedSessions; set({ sessions: sessionsWithCurrent, currentSessionKey: nextSessionKey }); @@ -423,8 +423,10 @@ export const useChatStore = create((set, get) => ({ // Generate a new unique session key and switch to it const prefix = getCanonicalPrefixFromSessions(get().sessions) ?? DEFAULT_CANONICAL_PREFIX; const newKey = `${prefix}:session-${Date.now()}`; - set({ + const newSessionEntry: ChatSession = { key: newKey, displayName: newKey }; + set((s) => ({ currentSessionKey: newKey, + sessions: [...s.sessions, newSessionEntry], messages: [], streamingText: '', streamingMessage: null, @@ -433,9 +435,7 @@ export const useChatStore = create((set, get) => ({ error: null, pendingFinal: false, lastUserMessageAt: null, - }); - // Reload sessions list to include the new one after first message - get().loadSessions(); + })); }, // ── Load chat history ──