use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct ToolResultDisplay {
#[serde(default, skip_serializing, skip_deserializing)]
pub edit_diff: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ToolResult {
pub tool_name: String,
pub success: bool,
pub content: String,
pub metadata: Value,
#[serde(default, skip_serializing, skip_deserializing)]
pub display: ToolResultDisplay,
}
impl ToolResult {
pub(crate) fn structured_content(&self) -> Option<&Value> {
match self.tool_name.as_str() {
"read" | "find" => self.metadata.get("structured_result"),
name if name.starts_with("mcp__") => self.metadata.get("structured_result"),
_ => None,
}
}
pub(crate) fn structured_result(&self, provider_output: &str) -> Value {
let content = self
.structured_content()
.cloned()
.unwrap_or_else(|| serde_json::json!({"text":provider_output}));
if self.success {
serde_json::json!({"success":true,"content":content})
} else {
serde_json::json!({"success":false,"content":content,"error":{"code":"tool_error","message":provider_output}})
}
}
}