chore(file_operators): use utf-8 as default encoding

This commit is contained in:
Sheng Fan 2025-03-19 14:10:07 +08:00
parent 94e2ab7c86
commit 7e3609f19f

View File

@ -42,17 +42,20 @@ class FileOperator(Protocol):
class LocalFileOperator(FileOperator):
"""File operations implementation for local filesystem."""
def __init__(self, encoding: str = "utf-8"):
self.encoding = encoding
async def read_file(self, path: PathLike) -> str:
"""Read content from a local file."""
try:
return Path(path).read_text()
return Path(path).read_text(encoding=self.encoding)
except Exception as e:
raise ToolError(f"Failed to read {path}: {str(e)}") from None
async def write_file(self, path: PathLike, content: str) -> None:
"""Write content to a local file."""
try:
Path(path).write_text(content)
Path(path).write_text(content, encoding=self.encoding)
except Exception as e:
raise ToolError(f"Failed to write to {path}: {str(e)}") from None