Tool to write content to a file.
| 53 | |
| 54 | |
| 55 | class WriteFileTool(Tool): |
| 56 | """Tool to write content to a file.""" |
| 57 | |
| 58 | def __init__(self, allowed_dir: Path | None = None): |
| 59 | self._allowed_dir = allowed_dir |
| 60 | |
| 61 | @property |
| 62 | def name(self) -> str: |
| 63 | return "write_file" |
| 64 | |
| 65 | @property |
| 66 | def description(self) -> str: |
| 67 | return "Write content to a file at the given path. Creates parent directories if needed." |
| 68 | |
| 69 | @property |
| 70 | def parameters(self) -> dict[str, Any]: |
| 71 | return { |
| 72 | "type": "object", |
| 73 | "properties": { |
| 74 | "path": {"type": "string", "description": "The file path to write to"}, |
| 75 | "content": {"type": "string", "description": "The content to write"}, |
| 76 | }, |
| 77 | "required": ["path", "content"], |
| 78 | } |
| 79 | |
| 80 | async def execute(self, path: str, content: str, **kwargs: Any) -> str: |
| 81 | try: |
| 82 | file_path = _resolve_path(path, self._allowed_dir) |
| 83 | file_path.parent.mkdir(parents=True, exist_ok=True) |
| 84 | file_path.write_text(content, encoding="utf-8") |
| 85 | return f"Successfully wrote {len(content)} bytes to {path}" |
| 86 | except PermissionError as e: |
| 87 | return f"Error: {e}" |
| 88 | except Exception as e: |
| 89 | return f"Error writing file: {str(e)}" |
| 90 | |
| 91 | |
| 92 | class EditFileTool(Tool): |
no outgoing calls
no test coverage detected