Skip to main content

matrixcode_core/tools/
edit.rs

1use anyhow::Result;
2use async_trait::async_trait;
3use serde_json::{Value, json};
4
5use super::{Tool, ToolDefinition};
6use crate::approval::RiskLevel;
7
8pub struct EditTool;
9
10#[async_trait]
11impl Tool for EditTool {
12    fn definition(&self) -> ToolDefinition {
13        ToolDefinition {
14            name: "edit".to_string(),
15            description: "Replace an exact string match in a file with new content".to_string(),
16            parameters: json!({
17                "type": "object",
18                "properties": {
19                    "path": {
20                        "type": "string",
21                        "description": "The file path to edit"
22                    },
23                    "old_string": {
24                        "type": "string",
25                        "description": "The exact string to find and replace"
26                    },
27                    "new_string": {
28                        "type": "string",
29                        "description": "The replacement string"
30                    }
31                },
32                "required": ["path", "old_string", "new_string"]
33            }),
34        }
35    }
36
37    async fn execute(&self, params: Value) -> Result<String> {
38        // Create spinner immediately at the start to fill the gap before actual operation
39        // let mut spinner = ToolSpinner::new("preparing edit");
40
41        let path = params["path"].as_str().ok_or_else(|| anyhow::anyhow!("missing 'path'"))?;
42        let old_string = params["old_string"].as_str().ok_or_else(|| anyhow::anyhow!("missing 'old_string'"))?;
43        let new_string = params["new_string"].as_str().ok_or_else(|| anyhow::anyhow!("missing 'new_string'"))?;
44
45        // Update spinner message for the actual edit operation
46        // spinner.set_message(&format!("editing {}", path));
47
48        let content = tokio::fs::read_to_string(path).await?;
49
50        let count = content.matches(old_string).count();
51        if count == 0 {
52            // spinner.finish_error("not found");
53            anyhow::bail!("old_string not found in {}", path);
54        }
55        if count > 1 {
56            // spinner.finish_error("multiple matches");
57            anyhow::bail!("old_string found {} times in {} — must be unique", count, path);
58        }
59
60        let new_content = content.replacen(old_string, new_string, 1);
61        tokio::fs::write(path, &new_content).await?;
62
63        // spinner.finish_success("edited");
64        Ok(format!("Successfully edited {}", path))
65    }
66
67    fn risk_level(&self) -> RiskLevel {
68        RiskLevel::Mutating
69    }
70}