use anyhow::Result;
use async_trait::async_trait;
use serde_json::Value;
use std::path::Path;
use super::{Tool, ToolOutput};
use crate::tools::file_ops::write_file_in_workspace;
pub struct WriteFileTool;
impl WriteFileTool {
pub fn new() -> Self {
Self
}
}
impl Default for WriteFileTool {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl Tool for WriteFileTool {
fn name(&self) -> &str {
"write_file"
}
fn description(&self) -> &str {
"Write a text file in the workspace."
}
fn parameters_schema(&self) -> Value {
serde_json::json!({
"type": "object",
"properties": {
"path": { "type": "string" },
"content": { "type": "string" }
},
"required": ["path", "content"],
"additionalProperties": false
})
}
async fn execute(&self, input: Value) -> Result<ToolOutput> {
let path = input["path"]
.as_str()
.ok_or_else(|| anyhow::anyhow!("Missing required parameter: path"))?;
let content = input["content"]
.as_str()
.ok_or_else(|| anyhow::anyhow!("Missing required parameter: content"))?;
let workspace_root = std::env::current_dir()?;
match write_file_in_workspace(path, content, &workspace_root) {
Ok(output) => {
let serialized = serde_json::to_string_pretty(&output)?;
Ok(ToolOutput::success(serialized))
}
Err(e) => Ok(ToolOutput::error(format!("Error writing file: {}", e))),
}
}
}