(
&self,
args: serde_json::Value,
ctx: ToolContext,
)
| 55 | } |
| 56 | |
| 57 | async fn execute( |
| 58 | &self, |
| 59 | args: serde_json::Value, |
| 60 | ctx: ToolContext, |
| 61 | ) -> Result<ToolResult, ToolError> { |
| 62 | let file_path: String = args["file_path"] |
| 63 | .as_str() |
| 64 | .ok_or_else(|| ToolError::InvalidArguments("file_path is required".into()))? |
| 65 | .to_string(); |
| 66 | |
| 67 | let content: String = args["content"] |
| 68 | .as_str() |
| 69 | .ok_or_else(|| ToolError::InvalidArguments("content is required".into()))? |
| 70 | .to_string(); |
| 71 | |
| 72 | let base_dir = if ctx.directory.is_empty() { |
| 73 | &self.directory |
| 74 | } else { |
| 75 | Path::new(&ctx.directory) |
| 76 | }; |
| 77 | |
| 78 | let path = if Path::new(&file_path).is_absolute() { |
| 79 | PathBuf::from(&file_path) |
| 80 | } else { |
| 81 | base_dir.join(&file_path) |
| 82 | }; |
| 83 | |
| 84 | let path_str = path.to_string_lossy().to_string(); |
| 85 | |
| 86 | if ctx.is_external_path(&path_str) { |
| 87 | let parent = path |
| 88 | .parent() |
| 89 | .map(|p| p.to_string_lossy().to_string()) |
| 90 | .unwrap_or_else(|| path_str.clone()); |
| 91 | |
| 92 | ctx.ask_permission( |
| 93 | crate::PermissionRequest::new("external_directory") |
| 94 | .with_pattern(format!("{}/*", parent)) |
| 95 | .with_metadata("filepath", serde_json::json!(&path_str)) |
| 96 | .with_metadata("parentDir", serde_json::json!(parent)), |
| 97 | ) |
| 98 | .await?; |
| 99 | } |
| 100 | |
| 101 | let old_content = fs::read_to_string(&path).await.unwrap_or_default(); |
| 102 | let exists = !old_content.is_empty(); |
| 103 | |
| 104 | if exists { |
| 105 | ctx.do_file_time_assert(path_str.clone()).await?; |
| 106 | } |
| 107 | |
| 108 | let diff = create_diff(&path_str, &old_content, &content); |
| 109 | |
| 110 | ctx.ask_permission( |
| 111 | crate::PermissionRequest::new("edit") |
| 112 | .with_pattern(&path_str) |
| 113 | .with_metadata("diff", serde_json::json!(diff)) |
| 114 | .always_allow(), |
nothing calls this directly
no test coverage detected