matrixcode_core/tools/
edit.rs1use 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: "在文件中查找精确匹配的字符串并替换为新内容".to_string(),
16 parameters: json!({
17 "type": "object",
18 "properties": {
19 "path": {
20 "type": "string",
21 "description": "要编辑的文件路径"
22 },
23 "old_string": {
24 "type": "string",
25 "description": "要查找并替换的原始字符串(必须精确匹配)"
26 },
27 "new_string": {
28 "type": "string",
29 "description": "替换后的新字符串"
30 }
31 },
32 "required": ["path", "old_string", "new_string"]
33 }),
34 }
35 }
36
37 async fn execute(&self, params: Value) -> Result<String> {
38 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 let content = tokio::fs::read_to_string(path).await?;
49
50 let count = content.matches(old_string).count();
51 if count == 0 {
52 anyhow::bail!("old_string not found in {}", path);
54 }
55 if count > 1 {
56 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 let old_lines: Vec<&str> = old_string.lines().collect();
65 let new_lines: Vec<&str> = new_string.lines().collect();
66 let mut diff = format!("Successfully edited {}\n", path);
67 for line in &old_lines {
68 diff.push_str(&format!("- {}\n", line));
69 }
70 for line in &new_lines {
71 diff.push_str(&format!("+ {}\n", line));
72 }
73 Ok(diff)
75 }
76
77 fn risk_level(&self) -> RiskLevel {
78 RiskLevel::Mutating
79 }
80}