(ctx context.Context, input map[string]any, tc *tools.ToolContext)
| 141 | } |
| 142 | |
| 143 | func (t *EditTool) Execute(ctx context.Context, input map[string]any, tc *tools.ToolContext) (any, error) { |
| 144 | path, _ := input["path"].(string) |
| 145 | oldStr, _ := input["old_string"].(string) |
| 146 | newStr, _ := input["new_string"].(string) |
| 147 | replaceAll := false |
| 148 | if ra, ok := input["replace_all"].(bool); ok { |
| 149 | replaceAll = ra |
| 150 | } |
| 151 | |
| 152 | // 路径安全验证 |
| 153 | if t.middleware != nil { |
| 154 | validatedPath, err := t.middleware.validatePath(path) |
| 155 | if err != nil { |
| 156 | return map[string]any{ |
| 157 | "ok": false, |
| 158 | "error": fmt.Sprintf("path validation failed: %v", err), |
| 159 | }, nil |
| 160 | } |
| 161 | path = validatedPath |
| 162 | } |
| 163 | |
| 164 | result, err := t.backend.Edit(ctx, path, oldStr, newStr, replaceAll) |
| 165 | if err != nil { |
| 166 | return map[string]any{ |
| 167 | "ok": false, |
| 168 | "error": fmt.Sprintf("failed to edit file: %v", err), |
| 169 | }, nil |
| 170 | } |
| 171 | |
| 172 | // 检查 Error 字段判断是否成功 |
| 173 | if result.Error != "" { |
| 174 | return map[string]any{ |
| 175 | "ok": false, |
| 176 | "error": result.Error, |
| 177 | }, nil |
| 178 | } |
| 179 | |
| 180 | return map[string]any{ |
| 181 | "ok": true, |
| 182 | "path": path, |
| 183 | "replacements": result.ReplacementsMade, |
| 184 | "replace_all": replaceAll, |
| 185 | }, nil |
| 186 | } |
| 187 | |
| 188 | func (t *EditTool) Prompt() string { |
| 189 | return `Use this tool for precise file editing via string replacement. |
nothing calls this directly
no test coverage detected