raw llm response being handled appropriately

This commit is contained in:
dal 2025-03-19 11:44:56 -06:00
parent 3e6359f066
commit fa89c4eef1
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
1 changed files with 24 additions and 8 deletions

View File

@ -235,24 +235,40 @@ pub async fn post_chat_handler(
// Only store completed messages in raw_llm_messages // Only store completed messages in raw_llm_messages
match &msg { match &msg {
AgentMessage::Assistant { AgentMessage::Assistant {
progress, content, .. progress, content, id, ..
} => { } => {
if let Some(content) = content { // Store chunks in the tracker to ensure deduplication
raw_response_message.push_str(&content); if let Some(content_str) = content {
// Use message ID as chunk ID, or generate a consistent one if missing
let chunk_id = id.clone().unwrap_or_else(|| "assistant_message".to_string());
// Add to chunk tracker to handle deduplication
chunk_tracker.add_chunk(chunk_id.clone(), content_str.clone());
} }
if matches!(progress, MessageProgress::Complete) { if matches!(progress, MessageProgress::Complete) {
if raw_response_message.is_empty() { if let Some(content_str) = content {
raw_llm_messages.push(msg.clone()); // Use message ID as chunk ID, or generate a consistent one if missing
} else { let chunk_id = id.clone().unwrap_or_else(|| "assistant_message".to_string());
// Get the complete deduplicated text from the chunk tracker
let complete_text = chunk_tracker.get_complete_text(chunk_id.clone())
.unwrap_or_else(|| content_str.clone());
// Create a new message with the deduplicated content
raw_llm_messages.push(AgentMessage::Assistant { raw_llm_messages.push(AgentMessage::Assistant {
id: None, id: id.clone(),
content: Some(raw_response_message.clone()), content: Some(complete_text),
name: None, name: None,
tool_calls: None, tool_calls: None,
progress: MessageProgress::Complete, progress: MessageProgress::Complete,
initial: false, initial: false,
}); });
// Clear the chunk from the tracker
chunk_tracker.clear_chunk(chunk_id);
} else {
// If there's no content, just use the original message
raw_llm_messages.push(msg.clone());
} }
} }
} }