(self, params: dict[str, Any])
| 41 | } |
| 42 | |
| 43 | def execute(self, params: dict[str, Any]) -> ToolResult: |
| 44 | filepath = Path(params["path"]).expanduser() |
| 45 | old_string = params.get("old_string", "") |
| 46 | new_string = params.get("new_string", "") |
| 47 | |
| 48 | if not filepath.exists(): |
| 49 | return ToolResult(output=f"Error: file not found: {filepath}", is_error=True) |
| 50 | if not old_string: |
| 51 | return ToolResult(output="Error: old_string must not be empty", is_error=True) |
| 52 | |
| 53 | try: |
| 54 | content = filepath.read_text(errors="replace") |
| 55 | except Exception as exc: |
| 56 | return ToolResult(output=f"Error reading file: {exc}", is_error=True) |
| 57 | |
| 58 | count = content.count(old_string) |
| 59 | if count == 0: |
| 60 | return ToolResult(output="Error: old_string not found in file", is_error=True) |
| 61 | if count > 1: |
| 62 | return ToolResult( |
| 63 | output=f"Error: old_string found {count} times -- must be unique. Add more context.", |
| 64 | is_error=True, |
| 65 | ) |
| 66 | |
| 67 | new_content = content.replace(old_string, new_string, 1) |
| 68 | try: |
| 69 | filepath.write_text(new_content) |
| 70 | except Exception as exc: |
| 71 | return ToolResult(output=f"Error writing file: {exc}", is_error=True) |
| 72 | |
| 73 | return ToolResult(output=f"Replaced 1 occurrence in {filepath}") |
nothing calls this directly
no test coverage detected