use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolUse {
pub id: String,
pub name: String,
pub input: serde_json::Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolResult {
pub tool_use_id: String,
pub output: String,
pub is_error: bool,
}
impl ToolUse {
pub fn extract_from_content(content: &[crate::types::ContentBlock]) -> Vec<Self> {
content
.iter()
.filter_map(|block| match block {
crate::types::ContentBlock::ToolUse { id, name, input } => Some(ToolUse {
id: id.clone(),
name: name.clone(),
input: input.clone(),
}),
_ => None,
})
.collect()
}
}
impl ToolResult {
pub fn success(tool_use_id: impl Into<String>, output: impl Into<String>) -> Self {
Self {
tool_use_id: tool_use_id.into(),
output: output.into(),
is_error: false,
}
}
pub fn error(tool_use_id: impl Into<String>, message: impl Into<String>) -> Self {
Self {
tool_use_id: tool_use_id.into(),
output: message.into(),
is_error: true,
}
}
pub fn into_input_message(self) -> crate::types::InputMessage {
crate::types::InputMessage::tool_result(self.tool_use_id, self.output, self.is_error)
}
}