mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	fix the duplicated...messages through the websocket?
This commit is contained in:
		| @@ -142,8 +142,13 @@ export async function setupStreamingResponse( | |||||||
|  |  | ||||||
|                 console.log(`[${responseId}] Received content chunk of length ${message.content.length}, preview: "${message.content.substring(0, 50)}${message.content.length > 50 ? '...' : ''}"`); |                 console.log(`[${responseId}] Received content chunk of length ${message.content.length}, preview: "${message.content.substring(0, 50)}${message.content.length > 50 ? '...' : ''}"`); | ||||||
|  |  | ||||||
|  |                 // Check if this is a duplicated message containing the same content we already have | ||||||
|  |                 if (message.done && assistantResponse.includes(message.content)) { | ||||||
|  |                     console.log(`[${responseId}] Ignoring duplicated content in done message`); | ||||||
|  |                 } else { | ||||||
|                     // Add to our accumulated response |                     // Add to our accumulated response | ||||||
|                     assistantResponse += message.content; |                     assistantResponse += message.content; | ||||||
|  |                 } | ||||||
|  |  | ||||||
|                 // Update the UI immediately with each chunk |                 // Update the UI immediately with each chunk | ||||||
|                 onContentUpdate(assistantResponse, false); |                 onContentUpdate(assistantResponse, false); | ||||||
| @@ -228,25 +233,21 @@ export async function setupStreamingResponse( | |||||||
|                     timeoutId = null; |                     timeoutId = null; | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|                 // Check if we have content in the done message |                 // Check if we have content in the done message - ONLY process if we haven't received any content yet | ||||||
|                 if (message.content) { |                 if (message.content && !receivedAnyContent) { | ||||||
|                     console.log(`[${responseId}] Processing content in done message: ${message.content.length} chars`); |                     console.log(`[${responseId}] Processing content in done message: ${message.content.length} chars`); | ||||||
|                     receivedAnyContent = true; |                     receivedAnyContent = true; | ||||||
|  |  | ||||||
|                     // Replace current response if we didn't have content before or if it's empty |                     // Use content from done message as full response | ||||||
|                     if (assistantResponse.length === 0) { |  | ||||||
|                     console.log(`[${responseId}] Using content from done message as full response`); |                     console.log(`[${responseId}] Using content from done message as full response`); | ||||||
|                     assistantResponse = message.content; |                     assistantResponse = message.content; | ||||||
|                     } |                     onContentUpdate(assistantResponse, true); | ||||||
|                     // Otherwise append it if it's different |                 } else if (message.content) { | ||||||
|                     else if (message.content !== assistantResponse) { |                     // We already have content, signal as done but don't duplicate | ||||||
|                         console.log(`[${responseId}] Appending content from done message to existing response`); |                     console.log(`[${responseId}] Content in done message ignored as we already have streamed content`); | ||||||
|                         assistantResponse += message.content; |                     onContentUpdate(assistantResponse, true); | ||||||
|                     } |                 } else { | ||||||
|                     else { |                     // No content in done message, just mark as done | ||||||
|                         console.log(`[${responseId}] Content in done message is identical to existing response, not appending`); |  | ||||||
|                     } |  | ||||||
|  |  | ||||||
|                     onContentUpdate(assistantResponse, true); |                     onContentUpdate(assistantResponse, true); | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -544,11 +544,8 @@ class RestChatService { | |||||||
|                         if (done) { |                         if (done) { | ||||||
|                             streamFinished = true; |                             streamFinished = true; | ||||||
|  |  | ||||||
|                             // Always send the accumulated content with the done=true message |                             // Don't send another "done:true" message here - we'll let the streaming handler | ||||||
|                             // This ensures the client receives the complete content even if earlier messages were missed |                             // handle the completion notification with its own done:true message | ||||||
|                             message.content = messageContent; |  | ||||||
|  |  | ||||||
|                             log.info(`Stream complete, sending final message with ${messageContent.length} chars of content`); |  | ||||||
|  |  | ||||||
|                             // Store the response in the session when done |                             // Store the response in the session when done | ||||||
|                             session.messages.push({ |                             session.messages.push({ | ||||||
| @@ -1358,15 +1355,15 @@ class RestChatService { | |||||||
|                             // Only send final done message if it wasn't already sent with content |                             // Only send final done message if it wasn't already sent with content | ||||||
|                             // This ensures we don't duplicate the content but still mark completion |                             // This ensures we don't duplicate the content but still mark completion | ||||||
|                             if (!chunk.text) { |                             if (!chunk.text) { | ||||||
|                                 // Send final message with both content and done flag together |                                 log.info(`No content in final chunk, sending explicit completion message`); | ||||||
|  |  | ||||||
|  |                                 // Send final message with done flag only (no content) | ||||||
|  |                                 // This avoids sending the entire messageContent again and causing duplicates | ||||||
|                                 wsService.sendMessageToAllClients({ |                                 wsService.sendMessageToAllClients({ | ||||||
|                                     type: 'llm-stream', |                                     type: 'llm-stream', | ||||||
|                                     sessionId, |                                     sessionId, | ||||||
|                                     content: messageContent, // Send the accumulated content |  | ||||||
|                                     done: true |                                     done: true | ||||||
|                                 } as LLMStreamMessage); |                                 } as LLMStreamMessage); | ||||||
|  |  | ||||||
|                                 log.info(`Sent explicit final completion message with accumulated content`); |  | ||||||
|                             } else { |                             } else { | ||||||
|                                 log.info(`Final done flag was already sent with content chunk, no need for extra message`); |                                 log.info(`Final done flag was already sent with content chunk, no need for extra message`); | ||||||
|                             } |                             } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user