use super::super::ToolDefinition;
use super::judge::{Verdict, judge};
pub(crate) const TOOL_CALL: &str = "<tool_call>";
pub(crate) const TOOL_CALL_END: &str = "</tool_call>";
pub fn parse_glm47_answer(text: &str, tools: &[ToolDefinition]) -> (Option<String>, Vec<Verdict>) {
let mut content = String::new();
let mut verdicts = Vec::new();
let mut rest = text;
while let Some(open) = rest.find(TOOL_CALL) {
content.push_str(&rest[..open]);
let body_and_tail = &rest[open + TOOL_CALL.len()..];
match body_and_tail.find(TOOL_CALL_END) {
Some(close) => {
verdicts.push(judge(&body_and_tail[..close], tools));
rest = &body_and_tail[close + TOOL_CALL_END.len()..];
}
None => {
verdicts.push(judge(body_and_tail, tools));
rest = "";
}
}
}
content.push_str(rest);
if verdicts.is_empty() {
return (Some(content).filter(|c| !c.is_empty()), verdicts);
}
let trimmed = content.trim();
((!trimmed.is_empty()).then(|| trimmed.to_string()), verdicts)
}