feat(llm): handle error catching in streaming better

This commit is contained in:
perf3ct 2025-06-09 00:07:00 +00:00
parent e98fabcc9d
commit ca6277f6e9
No known key found for this signature in database
GPG Key ID: 569C4EEC436F5232

View File

@ -420,19 +420,31 @@ 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 ===");
try {
const chatNoteId = req.params.chatNoteId; const chatNoteId = req.params.chatNoteId;
const { content, useAdvancedContext, showThinking, mentions } = req.body; 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'
}]; });
// Mark response as handled to prevent further processing
(res as any).triliumResponseHandled = true;
return;
} }
// Start background streaming process immediately (before sending response) // Send immediate success response
const backgroundPromise = handleStreamingProcess(chatNoteId, content, useAdvancedContext, showThinking, mentions) res.status(200).json({
success: true,
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 => { .catch(error => {
log.error(`Background streaming error: ${error.message}`); log.error(`Background streaming error: ${error.message}`);
@ -449,12 +461,19 @@ async function streamMessage(req: Request, res: Response) {
}); });
}); });
// Return immediate acknowledgment that streaming has been initiated } catch (error) {
// The background process will handle the actual streaming // Handle any synchronous errors
return { log.error(`Synchronous error in streamMessage: ${error}`);
success: true,
message: 'Streaming initiated successfully' 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;
}
} }
/** /**