Rebase onto upstream (a4d95fd)
#12
Binary file not shown.
Binary file not shown.
@@ -420,16 +420,165 @@ class MemoryTool20250818(BaseAnthropicTool):
|
||||
async def _insert(
|
||||
self, path: str | None, insert_line: int | None, insert_text: str | None
|
||||
) -> CLIResult:
|
||||
"""Placeholder for insert command."""
|
||||
return CLIResult(exit_code=1, output="", error="Not implemented yet")
|
||||
"""Insert text at a specific line number.
|
||||
|
||||
Args:
|
||||
path: File path to modify
|
||||
insert_line: Line number to insert at (0 = beginning)
|
||||
insert_text: Text to insert
|
||||
|
||||
Returns:
|
||||
CLIResult with success message or error
|
||||
"""
|
||||
if path is None or insert_line is None or insert_text is None:
|
||||
return CLIResult(
|
||||
exit_code=1,
|
||||
output="",
|
||||
error="Error: path, insert_line, and insert_text are required for insert command"
|
||||
)
|
||||
|
||||
path_str = path
|
||||
validated_path = self._validate_memory_path(path)
|
||||
|
||||
if not validated_path.exists() or validated_path.is_dir():
|
||||
return CLIResult(
|
||||
exit_code=1,
|
||||
output="",
|
||||
error=f"Error: The path {path_str} does not exist. Please provide a valid path."
|
||||
)
|
||||
|
||||
# Read current content
|
||||
content = validated_path.read_text()
|
||||
lines = content.splitlines(keepends=True)
|
||||
|
||||
# Validate insert_line
|
||||
if insert_line < 0 or insert_line > len(lines):
|
||||
return CLIResult(
|
||||
exit_code=1,
|
||||
output="",
|
||||
error=f"Invalid `insert_line` parameter: {insert_line}. It should be within 0 to {len(lines)}"
|
||||
)
|
||||
|
||||
# Insert text at specified line
|
||||
lines.insert(insert_line, insert_text)
|
||||
new_content = "".join(lines)
|
||||
validated_path.write_text(new_content)
|
||||
|
||||
return CLIResult(
|
||||
exit_code=0,
|
||||
output=f"The file {path_str} has been edited.",
|
||||
error=""
|
||||
)
|
||||
|
||||
async def _delete(self, path: str | None) -> CLIResult:
|
||||
"""Placeholder for delete command."""
|
||||
return CLIResult(exit_code=1, output="", error="Not implemented yet")
|
||||
"""Delete a file or directory.
|
||||
|
||||
Args:
|
||||
path: Path to delete
|
||||
|
||||
Returns:
|
||||
CLIResult with success message or error
|
||||
"""
|
||||
if path is None:
|
||||
return CLIResult(
|
||||
exit_code=1,
|
||||
output="",
|
||||
error="Error: path is required for delete command"
|
||||
)
|
||||
|
||||
path_str = path
|
||||
validated_path = self._validate_memory_path(path)
|
||||
|
||||
if not validated_path.exists():
|
||||
return CLIResult(
|
||||
exit_code=1,
|
||||
output="",
|
||||
error=f"Error: The path {path_str} does not exist. Please provide a valid path."
|
||||
)
|
||||
|
||||
# Delete file or directory
|
||||
try:
|
||||
if validated_path.is_dir():
|
||||
import shutil
|
||||
shutil.rmtree(validated_path)
|
||||
else:
|
||||
validated_path.unlink()
|
||||
except Exception as e:
|
||||
return CLIResult(
|
||||
exit_code=1,
|
||||
output="",
|
||||
error=f"Error deleting {path_str}: {e}"
|
||||
)
|
||||
|
||||
return CLIResult(
|
||||
exit_code=0,
|
||||
output=f"Successfully deleted {path_str}",
|
||||
error=""
|
||||
)
|
||||
|
||||
async def _rename(self, old_path: str | None, new_path: str | None) -> CLIResult:
|
||||
"""Placeholder for rename command."""
|
||||
return CLIResult(exit_code=1, output="", error="Not implemented yet")
|
||||
"""Rename or move a file or directory.
|
||||
|
||||
Args:
|
||||
old_path: Source path
|
||||
new_path: Destination path
|
||||
|
||||
Returns:
|
||||
CLIResult with success message or error
|
||||
"""
|
||||
if old_path is None or new_path is None:
|
||||
return CLIResult(
|
||||
exit_code=1,
|
||||
output="",
|
||||
error="Error: old_path and new_path are required for rename command"
|
||||
)
|
||||
|
||||
old_path_str = old_path
|
||||
new_path_str = new_path
|
||||
validated_old = self._validate_memory_path(old_path)
|
||||
validated_new = self._validate_memory_path(new_path)
|
||||
|
||||
# Check if source exists
|
||||
if not validated_old.exists():
|
||||
return CLIResult(
|
||||
exit_code=1,
|
||||
output="",
|
||||
error=f"Error: The path {old_path_str} does not exist. Please provide a valid path."
|
||||
)
|
||||
|
||||
# Check if destination already exists
|
||||
if validated_new.exists():
|
||||
return CLIResult(
|
||||
exit_code=1,
|
||||
output="",
|
||||
error=f"Error: The destination {new_path_str} already exists. Please provide a different destination."
|
||||
)
|
||||
|
||||
# Create parent directories if needed
|
||||
try:
|
||||
validated_new.parent.mkdir(parents=True, exist_ok=True)
|
||||
except Exception as e:
|
||||
return CLIResult(
|
||||
exit_code=1,
|
||||
output="",
|
||||
error=f"Error creating parent directories: {e}"
|
||||
)
|
||||
|
||||
# Rename/move
|
||||
try:
|
||||
validated_old.rename(validated_new)
|
||||
except Exception as e:
|
||||
return CLIResult(
|
||||
exit_code=1,
|
||||
output="",
|
||||
error=f"Error renaming {old_path_str}: {e}"
|
||||
)
|
||||
|
||||
return CLIResult(
|
||||
exit_code=0,
|
||||
output=f"Successfully renamed {old_path_str} to {new_path_str}",
|
||||
error=""
|
||||
)
|
||||
|
||||
def to_params(self) -> dict[str, Any]:
|
||||
"""Convert to Anthropic API tool parameter format.
|
||||
|
||||
Reference in New Issue
Block a user