Skip to main content

cersei_tools/
file_write.rs

1//! File write tool.
2
3use super::*;
4use crate::tool_primitives::fs as pfs;
5use serde::Deserialize;
6
7pub struct FileWriteTool;
8
9#[async_trait]
10impl Tool for FileWriteTool {
11    fn name(&self) -> &str {
12        "Write"
13    }
14    fn description(&self) -> &str {
15        "Write content to a file, creating it if it doesn't exist."
16    }
17    fn permission_level(&self) -> PermissionLevel {
18        PermissionLevel::Write
19    }
20    fn category(&self) -> ToolCategory {
21        ToolCategory::FileSystem
22    }
23
24    fn input_schema(&self) -> Value {
25        serde_json::json!({
26            "type": "object",
27            "properties": {
28                "file_path": { "type": "string", "description": "Absolute path to the file" },
29                "content": { "type": "string", "description": "Content to write" }
30            },
31            "required": ["file_path", "content"]
32        })
33    }
34
35    async fn execute(&self, input: Value, _ctx: &ToolContext) -> ToolResult {
36        #[derive(Deserialize)]
37        struct Input {
38            file_path: String,
39            content: String,
40        }
41
42        let input: Input = match serde_json::from_value(input) {
43            Ok(i) => i,
44            Err(e) => return ToolResult::error(format!("Invalid input: {}", e)),
45        };
46
47        let path = std::path::Path::new(&input.file_path);
48        match pfs::write_file(path, &input.content).await {
49            Ok(()) => {
50                ToolResult::success(format!("File created successfully at: {}", input.file_path))
51            }
52            Err(e) => ToolResult::error(format!("Failed to write file: {}", e)),
53        }
54    }
55}