MCPcopy Create free account
hub / github.com/FSoft-AI4Code/HyperAgent / _run

Method _run

src/hyperagent/tools/gen_tools.py:193–303  ·  view source on GitHub ↗

Opens the specified file and returns its content. Args: relative_file_path (str): The relative path to the file. max_new_line (int, optional): The maximum number of lines to include in the returned content. Defaults to 500. Returns: str:

(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)

Source from the content-addressed store, hash-verified

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
217 #TODO: heuristics to limit the number of lines to show
218 import_source = add_num_line("\n".join(lines[:80]), 1)
219
220 try:
221 if start_line is not None and end_line is not None and len(keywords) == 0:
222 if end_line - start_line > 90:
223 return f"The number of lines to show is limited at 90, the requested number of lines is {end_line - start_line}, please specify the start and end line again or using keyword instead. For example {start_line}:{start_line+90}"
224
225 if start_line > len(lines):
226 return f"Invalid start line, the start line is greater than the total number of lines in the file, the total number of lines in the file is {len(lines)}"
227
228 source = "\n".join(lines[start_line:end_line])
229 else:
230 out_str = "The content of " + relative_file_path.replace(self.path, "") + f" is: {import_source}\n"
231 line_ranges_overall = []
232 for keyword in keywords:
233 returned_source = []
234 out_str += f"\nResults for keyword: {keyword}\n"
235 # if any([keyword in line for line in lines[start_line:end_line]]) and start_line is not None and end_line is not None:
236 # expanded_source = "\n".join(lines[start_line:end_line])
237 # expanded_source = add_num_line(expanded_source, start_line)
238 # returned_source.append(expanded_source)
239
240 line_idx = []
241 for i, line in enumerate(lines):
242 if keyword in line:
243 line_idx.append(i)
244
245 line_idx = line_idx[:max_num_result]
246 line_ranges = [None for _ in line_idx]
247
248 root_node = parse_code(source, self.language).root_node
249 function_list = self.parser.get_function_list(root_node)
250 class_list = self.parser.get_class_list(root_node)

Callers

nothing calls this directly

Calls 4

find_matching_file_pathFunction · 0.90
add_num_lineFunction · 0.85
replaceMethod · 0.80
appendMethod · 0.80

Tested by

no test coverage detected