mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 02:16:05 +01:00 
			
		
		
		
	Do a better job of not having the frontend lockup if the session doesn't exist
This commit is contained in:
		| @@ -28,9 +28,9 @@ export async function createChatSession(): Promise<string | null> { | |||||||
|  */ |  */ | ||||||
| export async function checkSessionExists(sessionId: string): Promise<boolean> { | export async function checkSessionExists(sessionId: string): Promise<boolean> { | ||||||
|     try { |     try { | ||||||
|         const sessionCheck = await server.get<any>(`llm/sessions/${sessionId}`); |         const sessionCheck = await server.getWithSilentNotFound<any>(`llm/sessions/${sessionId}`); | ||||||
|         return !!(sessionCheck && sessionCheck.id); |         return !!(sessionCheck && sessionCheck.id); | ||||||
|     } catch (error) { |     } catch (error: any) { | ||||||
|         console.log(`Error checking session ${sessionId}:`, error); |         console.log(`Error checking session ${sessionId}:`, error); | ||||||
|         return false; |         return false; | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -328,7 +328,7 @@ export default class LlmChatPanel extends BasicWidget { | |||||||
|  |  | ||||||
|                             // If we successfully restored a session, also fetch the latest session data |                             // If we successfully restored a session, also fetch the latest session data | ||||||
|                             try { |                             try { | ||||||
|                                 const sessionData = await server.get<{ |                                 const sessionData = await server.getWithSilentNotFound<{ | ||||||
|                                     metadata?: { |                                     metadata?: { | ||||||
|                                         model?: string; |                                         model?: string; | ||||||
|                                         provider?: string; |                                         provider?: string; | ||||||
| @@ -356,6 +356,7 @@ export default class LlmChatPanel extends BasicWidget { | |||||||
|                                         content?: string; |                                         content?: string; | ||||||
|                                     }>; |                                     }>; | ||||||
|                                 }>(`llm/sessions/${savedData.sessionId}`); |                                 }>(`llm/sessions/${savedData.sessionId}`); | ||||||
|  |  | ||||||
|                                 if (sessionData && sessionData.metadata) { |                                 if (sessionData && sessionData.metadata) { | ||||||
|                                     // Update our metadata with the latest from the server |                                     // Update our metadata with the latest from the server | ||||||
|                                     this.metadata = { |                                     this.metadata = { | ||||||
| @@ -368,9 +369,18 @@ export default class LlmChatPanel extends BasicWidget { | |||||||
|                                     if (sessionData.sources && sessionData.sources.length > 0) { |                                     if (sessionData.sources && sessionData.sources.length > 0) { | ||||||
|                                         this.sources = sessionData.sources; |                                         this.sources = sessionData.sources; | ||||||
|                                     } |                                     } | ||||||
|  |                                 } else { | ||||||
|  |                                     // Session data is missing or incomplete, create a new session | ||||||
|  |                                     console.log(`Invalid or incomplete session data for ${savedData.sessionId}, creating a new session`); | ||||||
|  |                                     this.sessionId = null; | ||||||
|  |                                     await this.createChatSession(); | ||||||
|                                 } |                                 } | ||||||
|                             } catch (fetchError) { |                             } catch (fetchError: any) { | ||||||
|  |                                 // Handle fetch errors (this should now only happen for network issues, not 404s) | ||||||
|                                 console.warn(`Could not fetch latest session data: ${fetchError}`); |                                 console.warn(`Could not fetch latest session data: ${fetchError}`); | ||||||
|  |                                 console.log(`Creating a new session after fetch error`); | ||||||
|  |                                 this.sessionId = null; | ||||||
|  |                                 await this.createChatSession(); | ||||||
|                             } |                             } | ||||||
|                         } else { |                         } else { | ||||||
|                             console.log(`Saved session ${savedData.sessionId} not found, will create new one`); |                             console.log(`Saved session ${savedData.sessionId} not found, will create new one`); | ||||||
|   | |||||||
| @@ -1604,7 +1604,15 @@ class RestChatService { | |||||||
|             // Check if session exists |             // Check if session exists | ||||||
|             const session = sessions.get(sessionId); |             const session = sessions.get(sessionId); | ||||||
|             if (!session) { |             if (!session) { | ||||||
|                 throw new Error(`Session with ID ${sessionId} not found`); |                 // Instead of throwing an error, return a structured 404 response | ||||||
|  |                 // that the frontend can handle gracefully | ||||||
|  |                 res.status(404).json({ | ||||||
|  |                     error: true, | ||||||
|  |                     message: `Session with ID ${sessionId} not found`, | ||||||
|  |                     code: 'session_not_found', | ||||||
|  |                     sessionId | ||||||
|  |                 }); | ||||||
|  |                 return null; // Return null to prevent further processing | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             // Return session with metadata and additional fields |             // Return session with metadata and additional fields | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user