From 684ca0f273183b08aa6bd71c2531bb78255c5e61 Mon Sep 17 00:00:00 2001 From: Dillon DuPont Date: Tue, 1 Jul 2025 11:08:45 -0400 Subject: [PATCH] Added read/write text encoding and large text support --- .../computer/computer/interface/generic.py | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/libs/python/computer/computer/interface/generic.py b/libs/python/computer/computer/interface/generic.py index 46c949c3..a3521816 100644 --- a/libs/python/computer/computer/interface/generic.py +++ b/libs/python/computer/computer/interface/generic.py @@ -337,16 +337,30 @@ class GenericComputerInterface(BaseComputerInterface): content_b64 = result.get("content_b64", "") return decode_base64_image(content_b64) - async def read_text(self, path: str) -> str: - result = await self._send_command("read_text", {"path": path}) - if not result.get("success", False): - raise RuntimeError(result.get("error", "Failed to read file")) - return result.get("content", "") + async def read_text(self, path: str, encoding: str = 'utf-8') -> str: + """Read text from a file with specified encoding. + + Args: + path: Path to the file to read + encoding: Text encoding to use (default: 'utf-8') + + Returns: + str: The decoded text content of the file + """ + content_bytes = await self.read_bytes(path) + return content_bytes.decode(encoding) - async def write_text(self, path: str, content: str) -> None: - result = await self._send_command("write_text", {"path": path, "content": content}) - if not result.get("success", False): - raise RuntimeError(result.get("error", "Failed to write file")) + async def write_text(self, path: str, content: str, encoding: str = 'utf-8', append: bool = False) -> None: + """Write text to a file with specified encoding. + + Args: + path: Path to the file to write + content: Text content to write + encoding: Text encoding to use (default: 'utf-8') + append: Whether to append to the file instead of overwriting + """ + content_bytes = content.encode(encoding) + await self.write_bytes(path, content_bytes, append) async def get_file_size(self, path: str) -> int: result = await self._send_command("get_file_size", {"path": path})