use std::collections::BTreeSet;
use serde_json::Value;
#[must_use]
pub fn history_is_strict(messages: &Value) -> bool {
let Some(messages) = messages.as_array() else {
return false;
};
let mut pending: BTreeSet<&str> = BTreeSet::new();
for message in messages {
match message.get("role").and_then(Value::as_str) {
Some("tool") => {
let id = message
.get("tool_call_id")
.and_then(Value::as_str)
.unwrap_or_default();
if !pending.remove(id) {
return false;
}
},
Some(role) => {
if !pending.is_empty() {
return false;
}
if role == "assistant" {
pending = message
.get("tool_calls")
.and_then(Value::as_array)
.map(|calls| {
calls
.iter()
.filter_map(|c| c.get("id").and_then(Value::as_str))
.collect()
})
.unwrap_or_default();
}
},
None => return false,
}
}
pending.is_empty()
}