Skip to content

Commit 025aefc

Browse files
committed
Fix SSE Finish session mapping error in useMessageStream
- Enhanced SSE Finish case to properly handle session ID mapping - Added comprehensive logging for session ID mapping decisions - Improved error handling to prevent SSE stream interruption - Renamed sessionId to originalSessionId for clarity - Added detailed error logging with session context This resolves the persistent 'Failed to read session: Session not found' error at line 335 by ensuring proper session mapping before getSession API calls.
1 parent fd0b9d0 commit 025aefc

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

ui/desktop/src/hooks/useMessageStream.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getSession, Session } from '../api';
55
import { ChatState } from '../types/chatState';
66
import { sessionMappingService } from '../services/SessionMappingService';
77

8-
// Force rebuild timestamp: 2025-01-15T02:45:00Z - Enhanced Matrix session SSE error handling
8+
// Force rebuild timestamp: 2025-01-15T02:50:00Z - Fixed SSE Finish session mapping error
99

1010
let messageIdCounter = 0;
1111

@@ -341,38 +341,45 @@ export function useMessageStream({
341341
onFinish(lastMessage, parsedEvent.reason);
342342
}
343343

344-
const sessionId = (extraMetadataRef.current.body as Record<string, unknown>)
344+
const originalSessionId = (extraMetadataRef.current.body as Record<string, unknown>)
345345
?.session_id as string;
346346

347-
if (sessionId) {
347+
if (originalSessionId) {
348348
try {
349349
// Use session mapping service to get the appropriate backend session ID
350-
const backendSessionId = sessionMappingService.getBackendSessionId(sessionId);
350+
const backendSessionId = sessionMappingService.getBackendSessionId(originalSessionId);
351351

352-
console.log('📋 Fetching session data in SSE Finish:', {
353-
originalSessionId: sessionId,
352+
console.log('📋 SSE Finish - Session ID mapping:', {
353+
originalSessionId,
354354
backendSessionId,
355-
isMatrixSession: sessionId.startsWith('!'),
356-
shouldMakeBackendCalls: sessionMappingService.shouldMakeBackendCalls(sessionId),
355+
isMatrixSession: originalSessionId.startsWith('!'),
356+
shouldMakeBackendCalls: sessionMappingService.shouldMakeBackendCalls(originalSessionId),
357357
});
358358

359359
// Skip backend calls if no mapping exists for Matrix sessions
360360
if (backendSessionId === null) {
361-
console.log('📋 Skipping session data fetch in SSE Finish - no mapping for Matrix session:', sessionId);
361+
console.log('📋 Skipping session data fetch in SSE Finish - no mapping for Matrix session:', originalSessionId);
362362
break;
363363
}
364364

365+
console.log('📋 Making getSession call with backend session ID:', backendSessionId);
366+
365367
const sessionResponse = await getSession({
366368
path: { session_id: backendSessionId },
367369
throwOnError: true,
368370
});
369371

370372
if (sessionResponse.data) {
373+
console.log('📋 Successfully fetched session data for:', backendSessionId);
371374
setSession(sessionResponse.data);
372375
}
373376
} catch (err) {
374-
console.error('Error fetching session data:', err);
375-
// Don't throw here, just log the error
377+
console.error('📋 Error fetching session data in SSE Finish:', {
378+
originalSessionId,
379+
error: err instanceof Error ? err.message : String(err),
380+
errorName: err instanceof Error ? err.name : 'Unknown',
381+
});
382+
// Don't throw here, just log the error to prevent SSE stream interruption
376383
}
377384
}
378385
break;

0 commit comments

Comments
 (0)