diff --git a/desktop/src/components/chat/ChatInput.tsx b/desktop/src/components/chat/ChatInput.tsx index cf64ccf5..ac5da05c 100644 --- a/desktop/src/components/chat/ChatInput.tsx +++ b/desktop/src/components/chat/ChatInput.tsx @@ -531,16 +531,17 @@ export function ChatInput() { value={gitInfo?.workDir || activeSession?.workDir || ''} onChange={async (newWorkDir) => { if (!activeTabId) return - // Recreate session with new workDir + const oldId = activeTabId const { deleteSession, createSession } = useSessionStore.getState() - const { closeTab, openTab } = useTabStore.getState() + const { replaceTabSession } = useTabStore.getState() const { disconnectSession, connectToSession } = useChatStore.getState() - disconnectSession(activeTabId) - closeTab(activeTabId) - await deleteSession(activeTabId) + // Create new session first, then swap tab in-place const newId = await createSession(newWorkDir) - openTab(newId, t('sidebar.newSession')) + disconnectSession(oldId) + replaceTabSession(oldId, newId) connectToSession(newId) + // Clean up old session in background + deleteSession(oldId).catch(() => {}) }} /> )} diff --git a/desktop/src/stores/tabStore.ts b/desktop/src/stores/tabStore.ts index d2a12c8b..4fd0a564 100644 --- a/desktop/src/stores/tabStore.ts +++ b/desktop/src/stores/tabStore.ts @@ -29,6 +29,7 @@ type TabStore = { setActiveTab: (sessionId: string) => void updateTabTitle: (sessionId: string, title: string) => void updateTabStatus: (sessionId: string, status: Tab['status']) => void + replaceTabSession: (oldSessionId: string, newSessionId: string) => void moveTab: (fromIndex: number, toIndex: number) => void saveTabs: () => void @@ -93,6 +94,17 @@ export const useTabStore = create((set, get) => ({ })) }, + replaceTabSession: (oldSessionId, newSessionId) => { + const { activeTabId } = get() + set((s) => ({ + tabs: s.tabs.map((t) => + t.sessionId === oldSessionId ? { ...t, sessionId: newSessionId } : t, + ), + activeTabId: activeTabId === oldSessionId ? newSessionId : activeTabId, + })) + get().saveTabs() + }, + moveTab: (fromIndex, toIndex) => { if (fromIndex === toIndex) return const { tabs } = get()