almaze-api/tools/file/overwrite_file.py
2025-02-11 23:14:01 +05:30

16 lines
547 B
Python

from pathlib import Path
from langchain_core.tools import tool
@tool
def overwrite_file(filepath: str, content: str) -> str:
"""Overwrite content in an existing file."""
try:
path = Path(filepath)
if not path.exists():
return f"Error: File {filepath} does not exist"
with open(filepath, 'w', encoding='utf-8') as f:
f.write(content)
return f"Successfully overwrote {filepath}"
except Exception as e:
return f"Error overwriting file: {str(e)}"