use anyhow::Result;
use async_trait::async_trait;
use serde::Deserialize;
use serde_json::{Value, json};
use super::{Tool, ToolOutput};
pub struct WriteTool;
#[derive(Deserialize)]
struct Params {
file_path: String,
content: String,
}
#[async_trait]
impl Tool for WriteTool {
fn name(&self) -> &str {
"Write"
}
fn description(&self) -> &str {
"Write content to a file. Creates parent directories if needed."
}
fn input_schema(&self) -> Value {
json!({
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "Absolute path to the file to write"
},
"content": {
"type": "string",
"description": "Content to write to the file"
}
},
"required": ["file_path", "content"]
})
}
fn is_read_only(&self) -> bool {
false
}
fn summarize(&self, input: &Value) -> String {
input["file_path"].as_str().unwrap_or("?").to_string()
}
async fn execute(&self, input: Value) -> Result<ToolOutput> {
let params: Params = serde_json::from_value(input)?;
let path = crate::tools::read::expand_tilde(¶ms.file_path);
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
std::fs::write(&path, ¶ms.content)?;
Ok(ToolOutput {
content: format!("Successfully wrote to {}", params.file_path),
is_error: false,
})
}
}