Read file contents with optional line range. Args: file_path (str): ABSOLUTE path to the file to read. start_line (Optional[int]): Starting line number. end_line (Optional[int]): Ending line number. Returns: ToolRespo
(
self,
file_path: str,
start_line: Optional[int] = None,
end_line: Optional[int] = None,
**kwargs
)
| 41 | super().__init__(require_grad=require_grad, **kwargs) |
| 42 | |
| 43 | async def __call__( |
| 44 | self, |
| 45 | file_path: str, |
| 46 | start_line: Optional[int] = None, |
| 47 | end_line: Optional[int] = None, |
| 48 | **kwargs |
| 49 | ) -> ToolResponse: |
| 50 | """ |
| 51 | Read file contents with optional line range. |
| 52 | |
| 53 | Args: |
| 54 | file_path (str): ABSOLUTE path to the file to read. |
| 55 | start_line (Optional[int]): Starting line number. |
| 56 | end_line (Optional[int]): Ending line number. |
| 57 | |
| 58 | Returns: |
| 59 | ToolResponse: ToolResponse with file contents or error message. |
| 60 | """ |
| 61 | try: |
| 62 | # Validate file path |
| 63 | if not file_path or not file_path.strip(): |
| 64 | return ToolResponse( |
| 65 | success=False, |
| 66 | message="Error: file_path is required." |
| 67 | ) |
| 68 | |
| 69 | file_path = file_path.strip() |
| 70 | |
| 71 | # Check if file exists |
| 72 | if not os.path.exists(file_path): |
| 73 | return ToolResponse( |
| 74 | success=False, |
| 75 | message=f"Error: File not found: {file_path}" |
| 76 | ) |
| 77 | |
| 78 | # Check if it's a file |
| 79 | if not os.path.isfile(file_path): |
| 80 | return ToolResponse( |
| 81 | success=False, |
| 82 | message=f"Error: Path is not a file: {file_path}" |
| 83 | ) |
| 84 | |
| 85 | # Read file content |
| 86 | with open(file_path, 'r', encoding='utf-8') as f: |
| 87 | lines = f.readlines() |
| 88 | |
| 89 | total_lines = len(lines) |
| 90 | |
| 91 | # Handle line range |
| 92 | if start_line is not None or end_line is not None: |
| 93 | # Convert to 0-indexed, with automatic boundary handling |
| 94 | start_idx = (start_line - 1) if start_line else 0 |
| 95 | end_idx = end_line if end_line else total_lines |
| 96 | |
| 97 | # Automatic boundary handling |
| 98 | if start_idx < 0: |
| 99 | start_idx = 0 |
| 100 | if start_idx > total_lines: |