Skip to content

Commit d63d945

Browse files
committed
Fix Matrix chat history loading and backend compatibility
Enhanced Session Mapping Service: - Modified getBackendSessionId to return null for Matrix sessions without mappings - Added shouldMakeBackendCalls method to check if backend calls should be made - Added ensureMappingExists method for creating mappings for existing Matrix rooms - Prevents 404 errors when loading Matrix chat history without existing mappings Updated Backend Integration: - useChatEngine now skips session token fetching for Matrix sessions without mappings - useMessageStream SSE handling skips session data fetching for unmapped Matrix sessions - Proper null checking prevents backend API calls with Matrix room IDs - Comprehensive logging for debugging session mapping decisions Matrix Chat History Support: - Matrix sessions loaded from history no longer cause backend 404 errors - Sessions without mappings gracefully skip backend calls while maintaining Matrix functionality - Future enhancement: can create mappings on-demand for existing Matrix rooms - Maintains full compatibility with existing Goose sessions This resolves the 'GET /sessions/!roomId 404 Not Found' errors when loading Matrix chat history while preserving all Matrix collaborative features and backend compatibility for mapped sessions.
1 parent 7058ea7 commit d63d945

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

ui/desktop/src/hooks/useChatEngine.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ export const useChatEngine = ({
7878

7979
// Get the appropriate session ID for backend API calls
8080
const backendSessionId = sessionMappingService.getBackendSessionId(chat.sessionId);
81+
const shouldMakeBackendCalls = sessionMappingService.shouldMakeBackendCalls(chat.sessionId);
8182

8283
console.log('📋 useChatEngine: Session ID mapping:', {
8384
originalSessionId: chat.sessionId,
8485
backendSessionId,
8586
isMatrixSession: chat.sessionId.startsWith('!'),
87+
shouldMakeBackendCalls,
8688
});
8789

8890
const {
@@ -104,7 +106,7 @@ export const useChatEngine = ({
104106
id: chat.sessionId, // Keep original ID for frontend state management
105107
initialMessages: chat.messages,
106108
body: {
107-
session_id: backendSessionId, // Use mapped session ID for backend
109+
session_id: backendSessionId || chat.sessionId, // Use mapped session ID for backend, fallback to original
108110
session_working_dir: window.appConfig.get('GOOSE_WORKING_DIR'),
109111
...(chat.recipeConfig?.title
110112
? {
@@ -253,8 +255,15 @@ export const useChatEngine = ({
253255
originalSessionId: chat.sessionId,
254256
backendSessionId,
255257
isMatrixSession: chat.sessionId.startsWith('!'),
258+
shouldMakeBackendCalls: sessionMappingService.shouldMakeBackendCalls(chat.sessionId),
256259
});
257260

261+
// Skip backend calls if no mapping exists for Matrix sessions
262+
if (backendSessionId === null) {
263+
console.log('📋 Skipping session token fetch - no mapping for Matrix session:', chat.sessionId);
264+
return;
265+
}
266+
258267
const response = await getSession<true>({
259268
path: { session_id: backendSessionId },
260269
throwOnError: true,

ui/desktop/src/hooks/useMessageStream.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,15 @@ export function useMessageStream({
353353
originalSessionId: sessionId,
354354
backendSessionId,
355355
isMatrixSession: sessionId.startsWith('!'),
356+
shouldMakeBackendCalls: sessionMappingService.shouldMakeBackendCalls(sessionId),
356357
});
357358

359+
// Skip backend calls if no mapping exists for Matrix sessions
360+
if (backendSessionId === null) {
361+
console.log('📋 Skipping session data fetch in SSE Finish - no mapping for Matrix session:', sessionId);
362+
break;
363+
}
364+
358365
const sessionResponse = await getSession({
359366
path: { session_id: backendSessionId },
360367
throwOnError: true,

ui/desktop/src/services/SessionMappingService.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,46 @@ export class SessionMappingService {
167167
* Get the appropriate session ID for backend API calls
168168
* Returns the mapped Goose session ID if it's a Matrix room, otherwise returns the original ID
169169
*/
170-
public getBackendSessionId(sessionId: string): string {
170+
public getBackendSessionId(sessionId: string): string | null {
171171
if (SessionMappingService.isMatrixRoomId(sessionId)) {
172172
const gooseSessionId = this.getGooseSessionId(sessionId);
173173
if (gooseSessionId) {
174174
return gooseSessionId;
175175
}
176-
console.warn('📋 SessionMappingService: No mapping found for Matrix room ID:', sessionId);
177-
// Return original ID as fallback (will likely cause backend errors, but better than crashing)
178-
return sessionId;
176+
console.warn('📋 SessionMappingService: No mapping found for Matrix room ID:', sessionId, '- skipping backend calls');
177+
// Return null to indicate that backend calls should be skipped
178+
return null;
179179
}
180180
return sessionId;
181181
}
182182

183+
/**
184+
* Check if backend API calls should be made for this session ID
185+
* Returns false for Matrix sessions without mappings
186+
*/
187+
public shouldMakeBackendCalls(sessionId: string): boolean {
188+
if (SessionMappingService.isMatrixRoomId(sessionId)) {
189+
const gooseSessionId = this.getGooseSessionId(sessionId);
190+
return gooseSessionId !== null;
191+
}
192+
return true; // Always make backend calls for regular Goose sessions
193+
}
194+
195+
/**
196+
* Create a mapping for an existing Matrix room if one doesn't exist
197+
* This is useful for Matrix rooms loaded from history that don't have mappings yet
198+
*/
199+
public ensureMappingExists(matrixRoomId: string, title?: string): SessionMapping {
200+
const existingMapping = this.getMapping(matrixRoomId);
201+
if (existingMapping) {
202+
return existingMapping;
203+
}
204+
205+
// Create a new mapping for this Matrix room
206+
console.log('📋 SessionMappingService: Creating mapping for existing Matrix room:', matrixRoomId);
207+
return this.createMapping(matrixRoomId, [], title || `Matrix Room ${matrixRoomId.substring(1, 8)}`);
208+
}
209+
183210
/**
184211
* Get all mappings (for debugging/management)
185212
*/

0 commit comments

Comments
 (0)