@@ -4,7 +4,7 @@ import { apiService } from '../../services/apiService';
44import { sessionsAtom , activeSessionIdAtom } from '../atoms/session' ;
55import { messagesAtom } from '../atoms/message' ;
66import { toolResultsAtom , toolCallResultMap } from '../atoms/tool' ;
7- import { sessionPanelContentAtom , isProcessingAtom } from '../atoms/ui' ;
7+ import { sessionPanelContentAtom , sessionProcessingStatesAtom , isProcessingAtom } from '../atoms/ui' ;
88import { processEventAction } from './eventProcessors' ;
99import { Message , SessionInfo } from '@/common/types' ;
1010import { connectionStatusAtom } from '../atoms/ui' ;
@@ -150,7 +150,7 @@ export const setActiveSessionAction = atom(null, async (get, set, sessionId: str
150150 } ) ;
151151 }
152152
153- // Processing state will be managed by SSE events
153+
154154
155155 toolCallResultMap . clear ( ) ;
156156
@@ -169,6 +169,27 @@ export const setActiveSessionAction = atom(null, async (get, set, sessionId: str
169169 }
170170 }
171171
172+ // Status recovery with higher priority - always called after events processing
173+ // This ensures accurate processing state when SSE connection is established
174+ // and overrides any incorrect state from events processing
175+ if ( ! replayState . isActive ) {
176+ try {
177+ const status = await apiService . getSessionStatus ( sessionId ) ;
178+ set ( sessionProcessingStatesAtom , ( prev ) => ( {
179+ ...prev ,
180+ [ sessionId ] : status . isProcessing ,
181+ } ) ) ;
182+ console . log ( `Recovered processing state for session ${ sessionId } : ${ status . isProcessing } ` ) ;
183+ } catch ( error ) {
184+ console . warn ( `Failed to recover session status for ${ sessionId } :` , error ) ;
185+ // Default to false on error to avoid stuck processing state
186+ set ( sessionProcessingStatesAtom , ( prev ) => ( {
187+ ...prev ,
188+ [ sessionId ] : false ,
189+ } ) ) ;
190+ }
191+ }
192+
172193 // Always ensure we have the latest session metadata (including modelConfig)
173194 // This is lightweight since server always provides it
174195 try {
@@ -410,46 +431,3 @@ export const abortQueryAction = atom(null, async (get, set) => {
410431 return false ;
411432 }
412433} ) ;
413-
414- // Cache to prevent frequent status checks for the same session
415- const statusCheckCache = new Map < string , { timestamp : number ; promise ?: Promise < any > } > ( ) ;
416- const STATUS_CACHE_TTL = 2000 ; // 2 seconds cache
417-
418- export const checkSessionStatusAction = atom ( null , async ( get , set , sessionId : string ) => {
419- if ( ! sessionId ) return ;
420-
421- const now = Date . now ( ) ;
422- const cached = statusCheckCache . get ( sessionId ) ;
423-
424- // If we have a recent check or an ongoing request, skip
425- if ( cached ) {
426- if ( cached . promise ) {
427- // There's already an ongoing request for this session
428- return cached . promise ;
429- }
430- if ( now - cached . timestamp < STATUS_CACHE_TTL ) {
431- // Recent check, skip
432- return ;
433- }
434- }
435-
436- try {
437- // Mark that we're making a request
438- const promise = apiService . getSessionStatus ( sessionId ) ;
439- statusCheckCache . set ( sessionId , { timestamp : now , promise } ) ;
440-
441- const status = await promise ;
442-
443- // Update simple processing state
444- set ( isProcessingAtom , status . isProcessing ) ;
445-
446- // Clear the promise and update timestamp
447- statusCheckCache . set ( sessionId , { timestamp : now } ) ;
448-
449- return status ;
450- } catch ( error ) {
451- console . error ( 'Failed to check session status:' , error ) ;
452- // Clear the failed request
453- statusCheckCache . delete ( sessionId ) ;
454- }
455- } ) ;
0 commit comments