use serde_json::{Value, json};
use super::call::{GuardedToolCall, InteropError, ToolCallRequest};
use super::wire;
pub const DIALECT: &str = "anthropic";
pub fn parse_tool_calls(payload: &Value) -> Result<Vec<ToolCallRequest>, InteropError> {
wire::call_array(payload, "content", DIALECT)?
.iter()
.filter(|block| block.get("type").and_then(Value::as_str) == Some("tool_use"))
.map(parse_block)
.collect()
}
fn parse_block(block: &Value) -> Result<ToolCallRequest, InteropError> {
let name = wire::required_str(block, "name", DIALECT)?;
let arguments = wire::parse_arguments(block.get("input"), DIALECT, name)?;
let mut request = ToolCallRequest::new(name, arguments);
if let Some(id) = wire::optional_str(block, "id") {
request = request.with_call_id(id);
}
Ok(request)
}
pub fn filter_tools(tools: &Value, keep: &dyn Fn(&str) -> bool) -> Value {
wire::filter_tool_array(tools, |item| wire::optional_str(item, "name"), keep)
}
pub fn denial(call: &GuardedToolCall) -> Option<Value> {
call.denial_message().map(|content| {
json!({
"type": "tool_result",
"tool_use_id": call.request.call_id.clone().unwrap_or_default(),
"content": content,
"is_error": true,
})
})
}