Add bytes support to FileStore write operations (#6019)

Co-authored-by: openhands <openhands@all-hands.dev>
This commit is contained in:
Robert Brennan 2025-01-03 18:30:25 -05:00 committed by GitHub
parent ec70af9412
commit 510c1644dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 7 additions and 5 deletions

View File

@ -5,7 +5,7 @@ class E2BFileStore(FileStore):
def __init__(self, filesystem):
self.filesystem = filesystem
def write(self, path: str, contents: str) -> None:
def write(self, path: str, contents: str | bytes) -> None:
self.filesystem.write(path, contents)
def read(self, path: str) -> str:

View File

@ -3,7 +3,7 @@ from abc import abstractmethod
class FileStore:
@abstractmethod
def write(self, path: str, contents: str) -> None:
def write(self, path: str, contents: str | bytes) -> None:
pass
@abstractmethod

View File

@ -12,7 +12,9 @@ class InMemoryFileStore(FileStore):
def __init__(self, files: dict[str, str] = IN_MEMORY_FILES):
self.files = files
def write(self, path: str, contents: str) -> None:
def write(self, path: str, contents: str | bytes) -> None:
if isinstance(contents, bytes):
contents = contents.decode('utf-8')
self.files[path] = contents
def read(self, path: str) -> str:

View File

@ -15,8 +15,8 @@ class S3FileStore(FileStore):
self.bucket = os.getenv('AWS_S3_BUCKET')
self.client = Minio(endpoint, access_key, secret_key, secure=secure)
def write(self, path: str, contents: str) -> None:
as_bytes = contents.encode('utf-8')
def write(self, path: str, contents: str | bytes) -> None:
as_bytes = contents.encode('utf-8') if isinstance(contents, str) else contents
stream = io.BytesIO(as_bytes)
try:
self.client.put_object(self.bucket, path, stream, len(as_bytes))