AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): binary bot file content crashes the JSON service RPC (#13589)
Bentlyddd1d3047120
diff --git a/autogpt_platform/backend/backend/platform_linking/models.py b/autogpt_platform/backend/backend/platform_linking/models.pyindex 55ba224c1e..c6d383a2f1 100644--- a/autogpt_platform/backend/backend/platform_linking/models.py+++ b/autogpt_platform/backend/backend/platform_linking/models.py@@ -1,10 +1,22 @@"""Pydantic models for platform_linking requests and responses."""+import base64from datetime import datetimefrom enum import Enum-from typing import Literal+from typing import Annotated, Literal-from pydantic import BaseModel, Field+from pydantic import BaseModel, BeforeValidator, Field, PlainSerializer++# File content that survives the JSON service RPC. Raw bytes stay bytes+# in-process, but the wire format is base64 — plain ``bytes`` fields are+# strict-UTF-8-decoded by JSON serialization, which fails on binary content.+# Matches how the platform encodes file bytes in JSON elsewhere (the+# ``data:<mime>;base64,`` URIs from ``store_media_file``).+Base64EncodedBytes = Annotated[+ bytes,+ BeforeValidator(lambda v: base64.b64decode(v) if isinstance(v, str) else v),+ PlainSerializer(lambda v: base64.b64encode(v).decode("ascii"), when_used="json"),+]# Hard cap on the assembled bot prompt. The chat bots build a prompt from the# user's message plus channel history / referenced conversations, which on a@@ -139,7 +151,7 @@ class WorkspaceUploadRequest(BaseModel):platform_user_id: str = Field(min_length=1, max_length=255)filename: str = Field(min_length=1, max_length=512)mime_type: str = Field(min_length=1, max_length=255)- content: bytes+ content: Base64EncodedBytes# Write into this session's folder (/sessions/<id>/) so AutoPilot reads the# file during the turn, same as a web upload. Resolved by the caller before# uploading (see ``ensure_chat_session``).@@ -297,7 +309,7 @@ class WorkspaceArtifact(BaseModel):filename: strmime_type: strsize_bytes: int- content: bytes+ content: Base64EncodedBytesclass BotEventInput(BaseModel):diff --git a/autogpt_platform/backend/backend/platform_linking/models_test.py b/autogpt_platform/backend/backend/platform_linking/models_test.pyindex 0f5a0918be..bc7601c665 100644--- a/autogpt_platform/backend/backend/platform_linking/models_test.py+++ b/autogpt_platform/backend/backend/platform_linking/models_test.py@@ -3,6 +3,8 @@import pytestfrom pydantic import ValidationError+from backend.util.json import to_dict+from .models import (BotChatRequest,ConfirmLinkResponse,@@ -12,6 +14,8 @@ from .models import (Platform,ResolveResponse,ResolveServerRequest,+ WorkspaceArtifact,+ WorkspaceUploadRequest,)@@ -176,3 +180,52 @@ class TestResponseModels:def test_delete_link_response(self):resp = DeleteLinkResponse(success=True)assert resp.success is True+++class TestBinaryContentOverJsonRpc:+ """Binary file bytes must survive the JSON service RPC.++ Plain ``bytes`` fields are strict-UTF-8-decoded by JSON serialization,+ so a binary artifact (e.g. a JPEG starting 0xff) fails to serialize on+ the ``fetch_workspace_artifact`` / ``upload_workspace_files`` hops.+ """++ # First bytes of a JPEG — not valid UTF-8.+ _BINARY = b"\xff\xd8\xff\xe0" + bytes(range(256))++ def _artifact(self) -> WorkspaceArtifact:+ return WorkspaceArtifact(+ file_id="f1",+ filename="photo.jpg",+ mime_type="image/jpeg",+ size_bytes=len(self._BINARY),+ content=self._BINARY,+ )++ def test_artifact_binary_content_survives_the_wire(self):+ # Server response leg: FastAPI serializes via jsonable_encoder …+ wire = to_dict(self._artifact())+ assert isinstance(wire["content"], str) # base64, JSON-safe+ # … client leg: TypeAdapter validates the JSON back into the model.+ restored = WorkspaceArtifact.model_validate(wire)+ assert restored.content == self._BINARY++ def test_upload_request_binary_content_survives_the_wire(self):+ request = WorkspaceUploadRequest(+ platform=Platform.TELEGRAM,+ platform_user_id="u1",+ filename="photo.jpg",+ mime_type="image/jpeg",+ content=self._BINARY,+ )+ wire = to_dict(request)+ assert isinstance(wire["content"], str)+ restored = WorkspaceUploadRequest.model_validate(wire)+ assert restored.content == self._BINARY++ def test_raw_bytes_stay_bytes_in_process(self):+ # In-process consumers (adapters attach .content directly) must keep+ # seeing raw bytes — only the JSON wire format is base64.+ artifact = self._artifact()+ assert artifact.content == self._BINARY+ assert artifact.model_dump()["content"] == self._BINARY