mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-05-11 15:31:50 +00:00
Fix New session issues and websocket issues (#738)
* fix: reset-state-on-new-session-click * fix(chat): preserve continuity while session ids settle New conversations were crossing a short but important consistency gap. The route could already point at a newly created session id while the projects payload had not refreshed yet, and realtime/optimistic messages could still be keyed under a provisional id. In that window the UI could stop reading the active session store, briefly render the conversation as missing, and then repopulate it a moment later. That same gap also made duplication more likely. Optimistic local user messages could survive long enough to appear beside the persisted copy, and finalized assistant streaming rows could sit directly next to the server-backed assistant message with the same content before realtime state was cleared. The result was a chat view that felt unstable exactly when a new session was being created. This commit makes session-id reconciliation a first-class part of the chat flow instead of assuming every layer will agree immediately. The session store now understands canonical session aliases and can migrate one conversation from a provisional id to the real id without dropping its in-memory state. The route navigation path can replace the provisional URL entry instead of stacking it in history, and the project/session selection logic keeps a synthetic selected session alive long enough for the sidebar and project payloads to catch up. The practical goal is to keep one visible conversation throughout the whole creation lifecycle: no dead window between websocket events and project refresh, no stale provisional URL after the real id is known, and no extra optimistic/local bubbles when server history catches up. * fix(cli): resolve executable path for Claude CLI on Windows * fix(session-synchronizer): improve session name extraction for Claude and Codex
This commit is contained in:
@@ -5,6 +5,7 @@ import { api } from '../utils/api';
|
||||
import type {
|
||||
AppSocketMessage,
|
||||
AppTab,
|
||||
LLMProvider,
|
||||
LoadingProgress,
|
||||
Project,
|
||||
ProjectSession,
|
||||
@@ -261,6 +262,27 @@ export function useProjectsState({
|
||||
const [showSettings, setShowSettings] = useState(false);
|
||||
const [settingsInitialTab, setSettingsInitialTab] = useState('agents');
|
||||
const [externalMessageUpdate, setExternalMessageUpdate] = useState(0);
|
||||
/**
|
||||
* `newSessionTrigger` is an explicit, monotonic intent signal for user-driven
|
||||
* New Session actions.
|
||||
*
|
||||
* It exists because `handleNewSession` can be invoked while the app is already in
|
||||
* the same visible state (`selectedSession === null`, `activeTab === 'chat'`,
|
||||
* route already `/`). In that case, React/router updates are idempotent and no
|
||||
* downstream reset logic runs.
|
||||
*
|
||||
* Usage across the codebase:
|
||||
* 1) Produced here in `handleNewSession` via increment (always changes).
|
||||
* 2) Returned from this hook and threaded through:
|
||||
* useProjectsState -> AppContent -> MainContent -> ChatInterface.
|
||||
* 3) Consumed in `useChatSessionState` as an effect dependency to forcibly clear
|
||||
* chat-local state (`currentSessionId`, pending draft message, streaming flags,
|
||||
* pending session storage keys, pagination/scroll artifacts).
|
||||
*
|
||||
* Keeping this signal dedicated avoids coupling resets to unrelated counters/events
|
||||
* (for example websocket/project refresh updates) that could cause accidental resets.
|
||||
*/
|
||||
const [newSessionTrigger, setNewSessionTrigger] = useState(0);
|
||||
|
||||
const loadingProgressTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const lastHandledMessageRef = useRef<AppSocketMessage | null>(null);
|
||||
@@ -536,7 +558,42 @@ export function useProjectsState({
|
||||
return;
|
||||
}
|
||||
}
|
||||
}, [sessionId, projects, selectedProject?.projectId, selectedSession?.id, selectedSession?.__provider]);
|
||||
|
||||
// Session id is in the URL but not yet present on any project payload (common
|
||||
// right after `session_created` + navigate, before the next projects refresh).
|
||||
// Without a `selectedSession`, chat state clears `currentSessionId` and the
|
||||
// UI stops reading the session store even though messages stream under this id.
|
||||
if (selectedSession?.id === sessionId) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!selectedProject) {
|
||||
return;
|
||||
}
|
||||
|
||||
let providerFromStorage: string | null = null;
|
||||
try {
|
||||
providerFromStorage = localStorage.getItem('selected-provider');
|
||||
} catch {
|
||||
providerFromStorage = null;
|
||||
}
|
||||
|
||||
const normalizedProvider: LLMProvider =
|
||||
providerFromStorage === 'cursor'
|
||||
? 'cursor'
|
||||
: providerFromStorage === 'codex'
|
||||
? 'codex'
|
||||
: providerFromStorage === 'gemini'
|
||||
? 'gemini'
|
||||
: 'claude';
|
||||
|
||||
setSelectedSession({
|
||||
id: sessionId,
|
||||
__provider: normalizedProvider,
|
||||
__projectId: selectedProject.projectId,
|
||||
summary: '',
|
||||
});
|
||||
}, [sessionId, projects, selectedProject, selectedSession?.id, selectedSession?.__provider]);
|
||||
|
||||
const handleProjectSelect = useCallback(
|
||||
(project: Project) => {
|
||||
@@ -587,6 +644,7 @@ export function useProjectsState({
|
||||
setSelectedProject(project);
|
||||
setSelectedSession(null);
|
||||
setActiveTab('chat');
|
||||
setNewSessionTrigger((previous) => previous + 1);
|
||||
navigate('/');
|
||||
|
||||
if (isMobile) {
|
||||
@@ -806,6 +864,7 @@ export function useProjectsState({
|
||||
showSettings,
|
||||
settingsInitialTab,
|
||||
externalMessageUpdate,
|
||||
newSessionTrigger,
|
||||
setActiveTab,
|
||||
setSidebarOpen,
|
||||
setIsInputFocused,
|
||||
|
||||
Reference in New Issue
Block a user