Tool for opening a file inside a repository. Args: path (str): The path to the repository. language (str): The language of the file. Attributes: name (str): The name of the tool. description (str): The description of the tool. args_schema (class
| 157 | end_line: Optional[int] = Field(..., description="The ending line number of the file we want to open") |
| 158 | |
| 159 | class OpenFileToolForGenerator(BaseTool): |
| 160 | """ |
| 161 | Tool for opening a file inside a repository. |
| 162 | |
| 163 | Args: |
| 164 | path (str): The path to the repository. |
| 165 | language (str): The language of the file. |
| 166 | |
| 167 | Attributes: |
| 168 | name (str): The name of the tool. |
| 169 | description (str): The description of the tool. |
| 170 | args_schema (class): The schema for the tool's arguments. |
| 171 | path (str): The path to the repository. |
| 172 | |
| 173 | Methods: |
| 174 | _run(relative_file_path: str, max_new_line: int = 500) -> str: |
| 175 | Runs the tool to open the specified file. |
| 176 | |
| 177 | """ |
| 178 | |
| 179 | name = "open_file" |
| 180 | description = """Useful when you want to open a file inside a repo for editing. If you have a keyword in mind, you can use it to search the keyword in the file. Otherwise, you can specify the start and end line to view the content of the file. The number of lines to show is limited at 150 for example (100:250). |
| 181 | """ |
| 182 | args_schema = OpenFileToolForGeneratorArgs |
| 183 | path = "" |
| 184 | language = "" |
| 185 | parser: Optional[Callable] = None |
| 186 | |
| 187 | def __init__(self, path, language=None): |
| 188 | super().__init__() |
| 189 | self.path = path |
| 190 | self.parser = get_parser(language) |
| 191 | self.language = language |
| 192 | |
| 193 | def _run(self, relative_file_path: str, start_line: Optional[int] = None, end_line: Optional[int] = None, keywords: List[str] = [], preview_size: int = 10, max_num_result: int = 5): |
| 194 | """ |
| 195 | Opens the specified file and returns its content. |
| 196 | |
| 197 | Args: |
| 198 | relative_file_path (str): The relative path to the file. |
| 199 | max_new_line (int, optional): The maximum number of lines to include in the returned content. Defaults to 500. |
| 200 | |
| 201 | Returns: |
| 202 | str: The content of the file. |
| 203 | |
| 204 | """ |
| 205 | abs_path = os.path.join(self.path, relative_file_path) |
| 206 | if not os.path.exists(abs_path): |
| 207 | abs_path = find_matching_file_path(self.path, relative_file_path) |
| 208 | if abs_path is None: |
| 209 | return "File not found, please check the path again" |
| 210 | |
| 211 | if len(keywords) == 0 and start_line is None and end_line is None: |
| 212 | return "Please specify the keyword or start and end line to view the content of the file." |
| 213 | |
| 214 | source = open(abs_path, "r").read() |
| 215 | lines = source.split("\n") |
| 216 |