fix the duplicated...messages through the websocket?

This commit is contained in:
perf3ct 2025-04-15 01:29:12 +00:00
parent 3bddb60ecc
commit aadb8cce5d
No known key found for this signature in database
GPG Key ID: 569C4EEC436F5232
2 changed files with 41 additions and 43 deletions

View File

@ -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 ? '...' : ''}"`);
// Add to our accumulated response
assistantResponse += message.content;
// 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
assistantResponse += message.content;
}
// Update the UI immediately with each chunk
onContentUpdate(assistantResponse, false);
@ -228,25 +233,21 @@ export async function setupStreamingResponse(
timeoutId = null;
}
// Check if we have content in the done message
if (message.content) {
// Check if we have content in the done message - ONLY process if we haven't received any content yet
if (message.content && !receivedAnyContent) {
console.log(`[${responseId}] Processing content in done message: ${message.content.length} chars`);
receivedAnyContent = true;
// Replace current response if we didn't have content before or if it's empty
if (assistantResponse.length === 0) {
console.log(`[${responseId}] Using content from done message as full response`);
assistantResponse = message.content;
}
// Otherwise append it if it's different
else if (message.content !== assistantResponse) {
console.log(`[${responseId}] Appending content from done message to existing response`);
assistantResponse += message.content;
}
else {
console.log(`[${responseId}] Content in done message is identical to existing response, not appending`);
}
// Use content from done message as full response
console.log(`[${responseId}] Using content from done message as full response`);
assistantResponse = message.content;
onContentUpdate(assistantResponse, true);
} else if (message.content) {
// We already have content, signal as done but don't duplicate
console.log(`[${responseId}] Content in done message ignored as we already have streamed content`);
onContentUpdate(assistantResponse, true);
} else {
// No content in done message, just mark as done
onContentUpdate(assistantResponse, true);
}

View File

@ -544,11 +544,8 @@ class RestChatService {
if (done) {
streamFinished = true;
// Always send the accumulated content with the done=true message
// This ensures the client receives the complete content even if earlier messages were missed
message.content = messageContent;
log.info(`Stream complete, sending final message with ${messageContent.length} chars of content`);
// Don't send another "done:true" message here - we'll let the streaming handler
// handle the completion notification with its own done:true message
// Store the response in the session when done
session.messages.push({
@ -1358,15 +1355,15 @@ class RestChatService {
// 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
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({
type: 'llm-stream',
sessionId,
content: messageContent, // Send the accumulated content
done: true
} as LLMStreamMessage);
log.info(`Sent explicit final completion message with accumulated content`);
} else {
log.info(`Final done flag was already sent with content chunk, no need for extra message`);
}