diff --git a/src/components/chat/hooks/useChatRealtimeHandlers.ts b/src/components/chat/hooks/useChatRealtimeHandlers.ts index 342ea117..4ae861db 100644 --- a/src/components/chat/hooks/useChatRealtimeHandlers.ts +++ b/src/components/chat/hooks/useChatRealtimeHandlers.ts @@ -3,7 +3,7 @@ import type { Dispatch, MutableRefObject, SetStateAction } from 'react'; import { usePaletteOps } from '../../../contexts/PaletteOpsContext'; import type { PendingPermissionRequest, SessionNavigationOptions } from '../types/types'; -import type { Project, ProjectSession, LLMProvider } from '../../../types/app'; +import type { ProjectSession, LLMProvider } from '../../../types/app'; import type { SessionStore, NormalizedMessage } from '../../../stores/useSessionStore'; type PendingViewSession = { @@ -51,7 +51,6 @@ type LatestChatMessage = { interface UseChatRealtimeHandlersArgs { latestMessage: LatestChatMessage | null; provider: LLMProvider; - selectedProject: Project | null; selectedSession: ProjectSession | null; currentSessionId: string | null; setCurrentSessionId: (sessionId: string | null) => void; @@ -61,7 +60,6 @@ interface UseChatRealtimeHandlersArgs { setTokenBudget: (budget: Record | null) => void; setPendingPermissionRequests: Dispatch>; pendingViewSessionRef: MutableRefObject; - streamBufferRef: MutableRefObject; streamTimerRef: MutableRefObject; accumulatedStreamRef: MutableRefObject; onSessionInactive?: (sessionId?: string | null) => void; @@ -80,7 +78,6 @@ interface UseChatRealtimeHandlersArgs { export function useChatRealtimeHandlers({ latestMessage, provider, - selectedProject, selectedSession, currentSessionId, setCurrentSessionId, @@ -90,7 +87,6 @@ export function useChatRealtimeHandlers({ setTokenBudget, setPendingPermissionRequests, pendingViewSessionRef, - streamBufferRef, streamTimerRef, accumulatedStreamRef, onSessionInactive, @@ -187,7 +183,6 @@ export function useChatRealtimeHandlers({ if (msg.kind === 'stream_delta') { const text = msg.content || ''; if (!text) return; - streamBufferRef.current += text; accumulatedStreamRef.current += text; if (!streamTimerRef.current) { streamTimerRef.current = window.setTimeout(() => { @@ -216,12 +211,18 @@ export function useChatRealtimeHandlers({ sessionStore.finalizeStreaming(sid); } accumulatedStreamRef.current = ''; - streamBufferRef.current = ''; return; } // --- All other messages: route to store --- - if (sid) { + const shouldPersist = + msg.kind !== 'session_created' + && msg.kind !== 'complete' + && msg.kind !== 'status' + && msg.kind !== 'permission_request' + && msg.kind !== 'permission_cancelled'; + + if (sid && shouldPersist) { sessionStore.appendRealtime(sid, msg as NormalizedMessage); } @@ -232,6 +233,8 @@ export function useChatRealtimeHandlers({ if (!newSessionId) break; if (!currentSessionId || currentSessionId.startsWith('new-session-')) { + console.log('Session created with ID:', newSessionId); + console.log('Existing session ID:', currentSessionId); sessionStorage.setItem('pendingSessionId', newSessionId); if (pendingViewSessionRef.current && !pendingViewSessionRef.current.sessionId) { pendingViewSessionRef.current.sessionId = newSessionId; @@ -257,7 +260,6 @@ export function useChatRealtimeHandlers({ sessionStore.finalizeStreaming(sid); } accumulatedStreamRef.current = ''; - streamBufferRef.current = ''; setIsLoading(false); setCanAbortSession(false); @@ -386,7 +388,6 @@ export function useChatRealtimeHandlers({ }, [ latestMessage, provider, - selectedProject, selectedSession, currentSessionId, setCurrentSessionId, @@ -396,7 +397,6 @@ export function useChatRealtimeHandlers({ setTokenBudget, setPendingPermissionRequests, pendingViewSessionRef, - streamBufferRef, streamTimerRef, accumulatedStreamRef, onSessionInactive, diff --git a/src/components/chat/view/ChatInterface.tsx b/src/components/chat/view/ChatInterface.tsx index 8589f29a..46e95abd 100644 --- a/src/components/chat/view/ChatInterface.tsx +++ b/src/components/chat/view/ChatInterface.tsx @@ -50,7 +50,6 @@ function ChatInterface({ const { t } = useTranslation('chat'); const sessionStore = useSessionStore(); - const streamBufferRef = useRef(''); const streamTimerRef = useRef(null); const accumulatedStreamRef = useRef(''); const pendingViewSessionRef = useRef(null); @@ -60,7 +59,6 @@ function ChatInterface({ clearTimeout(streamTimerRef.current); streamTimerRef.current = null; } - streamBufferRef.current = ''; accumulatedStreamRef.current = ''; }, []); @@ -225,7 +223,6 @@ function ChatInterface({ useChatRealtimeHandlers({ latestMessage, provider, - selectedProject, selectedSession, currentSessionId, setCurrentSessionId, @@ -235,7 +232,6 @@ function ChatInterface({ setTokenBudget, setPendingPermissionRequests, pendingViewSessionRef, - streamBufferRef, streamTimerRef, accumulatedStreamRef, onSessionInactive,