MCPcopy Create free account
hub / github.com/FSoft-AI4Code/CodeWiki / view

Method view

codewiki/src/be/agent_tools/str_replace_editor.py:483–561  ·  view source on GitHub ↗

Implement the view command

(self, path: Path, view_range: Optional[List[int]] = None)

Source from the content-addressed store, hash-verified

481 return
482 return self.str_replace(_path, old_str, new_str)
483 elif command == "insert":
484 if insert_line is None:
485 self.logs.append("Parameter `insert_line` is required for command: insert")
486 return
487 if new_str is None:
488 self.logs.append("Parameter `new_str` is required for command: insert")
489 return
490 return self.insert(_path, insert_line, new_str)
491 elif command == "undo_edit":
492 return self.undo_edit(_path)
493 self.logs.append(
494 f'Unrecognized command {command}. The allowed commands for the {self.name} tool are: "view", "create", "str_replace", "insert", "undo_edit"'
495 )
496 return
497
498 def validate_path(self, command: str, path: Path):
499 """
500 Check that the path/command combination is valid.
501 """
502 # Check if its an absolute path
503 if not path.is_absolute():
504 suggested_path = Path.cwd() / path
505 self.logs.append(
506 f"The path {self._get_display_path(path)} is not an absolute path, it should start with `/`. Maybe you meant {self._get_display_path(suggested_path)}?"
507 )
508 return False
509 # Check if path exists
510 if not path.exists() and command != "create":
511 self.logs.append(
512 f"The path {self._get_display_path(path)} does not exist. Please provide a valid path."
513 )
514 return False
515 if path.exists() and command == "create":
516 self.logs.append(
517 f"File already exists at: {self._get_display_path(path)}. Cannot overwrite files using command `create`."
518 )
519 return False
520 # Check if the path points to a directory
521 if path.is_dir() and command != "view":
522 self.logs.append(
523 f"The path {self._get_display_path(path)} is a directory and only the `view` command can be used on directories"
524 )
525 return False
526 return True
527
528 def create_file(self, path: Path, file_text: str):
529 # Nested doc pages (hierarchical layout) live in folders that may not
530 # exist yet; create them, but only inside the docs directory.
531 if not path.parent.exists() and self._inside_docs(path):
532 path.parent.mkdir(parents=True, exist_ok=True)
533 if not path.parent.exists():
534 self.logs.append(
535 f"The parent directory {self._get_display_path(path.parent)} does not exist. Please create it first."
536 )
537 return
538 if not self.write_file(path, file_text):
539 return
540 self._remember(path, file_text)

Callers 1

__call__Method · 0.95

Calls 9

_get_display_pathMethod · 0.95
read_fileMethod · 0.95
_make_outputMethod · 0.95
WindowExpanderClass · 0.85
FilemapClass · 0.85
maybe_truncateFunction · 0.85
runMethod · 0.80
expand_windowMethod · 0.80
show_filemapMethod · 0.80

Tested by

no test coverage detected