Edit file contents with multiple operations. Args: file_path (str): ABSOLUTE path to the file to edit. edits (List[Dict]): List of edit operations. Each dict can have: - start_line (int, optional): Starting line number (1-indexed)
(
self,
file_path: str,
edits: List[Dict[str, Any]],
**kwargs
)
| 57 | super().__init__(require_grad=require_grad, **kwargs) |
| 58 | |
| 59 | async def __call__( |
| 60 | self, |
| 61 | file_path: str, |
| 62 | edits: List[Dict[str, Any]], |
| 63 | **kwargs |
| 64 | ) -> ToolResponse: |
| 65 | """ |
| 66 | Edit file contents with multiple operations. |
| 67 | |
| 68 | Args: |
| 69 | file_path (str): ABSOLUTE path to the file to edit. |
| 70 | edits (List[Dict]): List of edit operations. Each dict can have: |
| 71 | - start_line (int, optional): Starting line number (1-indexed) |
| 72 | - end_line (int, optional): Ending line number (inclusive) |
| 73 | - content (str, required): Content to insert/replace |
| 74 | |
| 75 | Returns: |
| 76 | ToolResponse: ToolResponse with edit results or error message. |
| 77 | """ |
| 78 | try: |
| 79 | # Validate file path |
| 80 | if not file_path or not file_path.strip(): |
| 81 | return ToolResponse( |
| 82 | success=False, |
| 83 | message="Error: file_path is required." |
| 84 | ) |
| 85 | |
| 86 | file_path = file_path.strip() |
| 87 | |
| 88 | # Check if file exists |
| 89 | if not os.path.exists(file_path): |
| 90 | return ToolResponse( |
| 91 | success=False, |
| 92 | message=f"Error: File not found: {file_path}" |
| 93 | ) |
| 94 | |
| 95 | # Check if it's a file |
| 96 | if not os.path.isfile(file_path): |
| 97 | return ToolResponse( |
| 98 | success=False, |
| 99 | message=f"Error: Path is not a file: {file_path}" |
| 100 | ) |
| 101 | |
| 102 | # Validate edits |
| 103 | if not edits or not isinstance(edits, list): |
| 104 | return ToolResponse( |
| 105 | success=False, |
| 106 | message="Error: edits must be a non-empty list of edit operations." |
| 107 | ) |
| 108 | |
| 109 | # Read current file content |
| 110 | with open(file_path, 'r', encoding='utf-8') as f: |
| 111 | lines = f.readlines() |
| 112 | |
| 113 | original_lines = len(lines) |
| 114 | |
| 115 | # Validate and normalize each edit |
| 116 | normalized_edits = [] |