mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 21:11:30 +08:00 
			
		
		
		
	feat(llm): handle error catching in streaming better
This commit is contained in:
		
							parent
							
								
									e98fabcc9d
								
							
						
					
					
						commit
						ca6277f6e9
					
				| @ -420,41 +420,60 @@ async function sendMessage(req: Request, res: Response) { | |||||||
| async function streamMessage(req: Request, res: Response) { | async function streamMessage(req: Request, res: Response) { | ||||||
|     log.info("=== Starting streamMessage ==="); |     log.info("=== Starting streamMessage ==="); | ||||||
|      |      | ||||||
|     const chatNoteId = req.params.chatNoteId; |     try { | ||||||
|     const { content, useAdvancedContext, showThinking, mentions } = req.body; |         const chatNoteId = req.params.chatNoteId; | ||||||
|  |         const { content, useAdvancedContext, showThinking, mentions } = req.body; | ||||||
| 
 | 
 | ||||||
|     // Input validation
 |         // Input validation
 | ||||||
|     if (!content || typeof content !== 'string' || content.trim().length === 0) { |         if (!content || typeof content !== 'string' || content.trim().length === 0) { | ||||||
|         return [400, { |             res.status(400).json({ | ||||||
|             success: false, |                 success: false, | ||||||
|             error: 'Content cannot be empty' |                 error: 'Content cannot be empty' | ||||||
|         }]; |  | ||||||
|     } |  | ||||||
|      |  | ||||||
|     // Start background streaming process immediately (before sending response)
 |  | ||||||
|     const backgroundPromise = handleStreamingProcess(chatNoteId, content, useAdvancedContext, showThinking, mentions) |  | ||||||
|         .catch(error => { |  | ||||||
|             log.error(`Background streaming error: ${error.message}`); |  | ||||||
|              |  | ||||||
|             // Send error via WebSocket since HTTP response was already sent
 |  | ||||||
|             import('../../services/ws.js').then(wsModule => { |  | ||||||
|                 wsModule.default.sendMessageToAllClients({ |  | ||||||
|                     type: 'llm-stream', |  | ||||||
|                     chatNoteId: chatNoteId, |  | ||||||
|                     error: `Error during streaming: ${error.message}`, |  | ||||||
|                     done: true |  | ||||||
|                 }); |  | ||||||
|             }).catch(wsError => { |  | ||||||
|                 log.error(`Could not send WebSocket error: ${wsError}`); |  | ||||||
|             }); |             }); | ||||||
|         }); |             // Mark response as handled to prevent further processing
 | ||||||
|  |             (res as any).triliumResponseHandled = true; | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|          |          | ||||||
|     // Return immediate acknowledgment that streaming has been initiated
 |         // Send immediate success response
 | ||||||
|     // The background process will handle the actual streaming
 |         res.status(200).json({ | ||||||
|     return { |             success: true, | ||||||
|         success: true, |             message: 'Streaming initiated successfully' | ||||||
|         message: 'Streaming initiated successfully' |         }); | ||||||
|     }; |         // Mark response as handled to prevent further processing
 | ||||||
|  |         (res as any).triliumResponseHandled = true; | ||||||
|  |          | ||||||
|  |         // Start background streaming process after sending response
 | ||||||
|  |         handleStreamingProcess(chatNoteId, content, useAdvancedContext, showThinking, mentions) | ||||||
|  |             .catch(error => { | ||||||
|  |                 log.error(`Background streaming error: ${error.message}`); | ||||||
|  |                  | ||||||
|  |                 // Send error via WebSocket since HTTP response was already sent
 | ||||||
|  |                 import('../../services/ws.js').then(wsModule => { | ||||||
|  |                     wsModule.default.sendMessageToAllClients({ | ||||||
|  |                         type: 'llm-stream', | ||||||
|  |                         chatNoteId: chatNoteId, | ||||||
|  |                         error: `Error during streaming: ${error.message}`, | ||||||
|  |                         done: true | ||||||
|  |                     }); | ||||||
|  |                 }).catch(wsError => { | ||||||
|  |                     log.error(`Could not send WebSocket error: ${wsError}`); | ||||||
|  |                 }); | ||||||
|  |             }); | ||||||
|  |              | ||||||
|  |     } catch (error) { | ||||||
|  |         // Handle any synchronous errors
 | ||||||
|  |         log.error(`Synchronous error in streamMessage: ${error}`); | ||||||
|  |          | ||||||
|  |         if (!res.headersSent) { | ||||||
|  |             res.status(500).json({ | ||||||
|  |                 success: false, | ||||||
|  |                 error: 'Internal server error' | ||||||
|  |             }); | ||||||
|  |         } | ||||||
|  |         // Mark response as handled to prevent further processing
 | ||||||
|  |         (res as any).triliumResponseHandled = true; | ||||||
|  |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 perf3ct
						perf3ct