mirror of https://github.com/buster-so/buster.git
dedup reasoning message
This commit is contained in:
parent
11cb0d5bdc
commit
415466aa1f
|
@ -394,7 +394,8 @@ pub async fn post_chat_handler(
|
||||||
/// Prepares the final message state from transformed containers
|
/// Prepares the final message state from transformed containers
|
||||||
fn prepare_final_message_state(containers: &[BusterContainer]) -> Result<(Vec<Value>, Vec<Value>)> {
|
fn prepare_final_message_state(containers: &[BusterContainer]) -> Result<(Vec<Value>, Vec<Value>)> {
|
||||||
let mut response_messages = Vec::new();
|
let mut response_messages = Vec::new();
|
||||||
let mut reasoning_messages = Vec::new();
|
// Use a HashMap to track the latest reasoning message for each ID
|
||||||
|
let mut reasoning_map: std::collections::HashMap<String, Value> = std::collections::HashMap::new();
|
||||||
|
|
||||||
for container in containers {
|
for container in containers {
|
||||||
match container {
|
match container {
|
||||||
|
@ -421,38 +422,33 @@ fn prepare_final_message_state(containers: &[BusterContainer]) -> Result<(Vec<Va
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BusterContainer::ReasoningMessage(reasoning) => {
|
BusterContainer::ReasoningMessage(reasoning) => {
|
||||||
let reasoning_value = match &reasoning.reasoning {
|
// Only include reasoning messages that are explicitly marked as completed
|
||||||
BusterReasoningMessage::Pill(thought) => {
|
let should_include = match &reasoning.reasoning {
|
||||||
if thought.status == "completed" {
|
BusterReasoningMessage::Pill(thought) => thought.status == "completed",
|
||||||
serde_json::to_value(thought).ok()
|
BusterReasoningMessage::File(file) => file.status == "completed",
|
||||||
} else {
|
BusterReasoningMessage::Text(text) => text.status.as_deref() == Some("completed"),
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
BusterReasoningMessage::File(file) => {
|
|
||||||
if file.status == "completed" {
|
|
||||||
serde_json::to_value(file).ok()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
BusterReasoningMessage::Text(text) => {
|
|
||||||
if text.status.as_deref() == Some("completed") {
|
|
||||||
serde_json::to_value(text).ok()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(value) = reasoning_value {
|
if should_include {
|
||||||
reasoning_messages.push(value);
|
if let Ok(value) = serde_json::to_value(&reasoning.reasoning) {
|
||||||
|
// Get the ID from the reasoning message
|
||||||
|
let id = match &reasoning.reasoning {
|
||||||
|
BusterReasoningMessage::Pill(thought) => thought.id.clone(),
|
||||||
|
BusterReasoningMessage::File(file) => file.id.clone(),
|
||||||
|
BusterReasoningMessage::Text(text) => text.id.clone(),
|
||||||
|
};
|
||||||
|
// Store or update the message in the map
|
||||||
|
reasoning_map.insert(id, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert the map values into the final vector
|
||||||
|
let reasoning_messages: Vec<Value> = reasoning_map.into_values().collect();
|
||||||
|
|
||||||
Ok((response_messages, reasoning_messages))
|
Ok((response_messages, reasoning_messages))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue