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.
feat(backend/copilot-bot): let users upload files to AutoPilot via Discord (#13427)
Bentlyb1077221803a
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/base.py b/autogpt_platform/backend/backend/copilot/bot/adapters/base.pyindex c9ba9dec28..ed7517f85a 100644--- a/autogpt_platform/backend/backend/copilot/bot/adapters/base.py+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/base.py@@ -60,6 +60,19 @@ class FileAttachment(BaseModel):content: bytes+class InboundAttachment(BaseModel):+ """A file the user attached to an inbound platform message.++ The adapter downloads the bytes from the platform up-front (bounded by the+ adapter's ``max_attachment_bytes``); the handler then uploads them to the+ user's workspace so AutoPilot can read them during the turn.+ """++ filename: str+ mime_type: str+ content: bytes++class ChannelInfo(BaseModel):"""A channel the bot can post to, scoped to a server it's connected to.@@ -108,6 +121,13 @@ class MessageContext:# Other threads/channels the message linked or @-referenced, fetched by the# bot up-front so the model has their content without web-fetching Discord.referenced_conversations: tuple[ReferencedConversation, ...] = ()+ # Files the user attached to this message (bytes already downloaded). The+ # handler uploads these to the workspace and passes their IDs to the turn.+ attachments: tuple[InboundAttachment, ...] = ()+ # Attachments the adapter couldn't ingest at all (too large / download+ # failed), as ``(filename, reason)`` pairs — surfaced to the user and the+ # model so neither thinks the file was read.+ skipped_attachments: tuple[tuple[str, str], ...] = ()@propertydef is_dm(self) -> bool:diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.pyindex 33cee6252c..f16c0c6e12 100644--- a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py@@ -21,6 +21,7 @@ from ..base import (ChannelInfo,ChannelType,FileAttachment,+ InboundAttachment,MessageCallback,MessageContext,MessageHistoryEntry,@@ -54,6 +55,10 @@ _HISTORY_TRUNCATION_MARKER = "\n… [message truncated]"MAX_REFERENCED_CONVERSATIONS = 3REFERENCED_HISTORY_LIMIT = 200REFERENCED_CHAR_BUDGET = 8000++# Cap how many attachments we pull off a single message into the workspace, so+# a message with dozens of files can't fan out into that many uploads/scans.+MAX_INBOUND_ATTACHMENTS = 10# When a link names a specific message, fetch that message plus a little of the# conversation leading up to it (rather than the channel's latest activity).REFERENCED_MESSAGE_CONTEXT = 15@@ -444,6 +449,7 @@ class DiscordAdapter(PlatformAdapter):# context — both verbatim, with their links left untouched.message_text = self._compose_with_forward(message, own_text)message_text = await self._with_reply_context(message, message_text)+ attachments, skipped = await self._extract_attachments(message)ctx = MessageContext(platform="discord",channel_type=channel_type,@@ -457,9 +463,46 @@ class DiscordAdapter(PlatformAdapter):thread_history=thread_history,mentionable_users=self._collect_mentionable_users(message),referenced_conversations=referenced,+ attachments=attachments,+ skipped_attachments=skipped,)await self._on_message_callback(ctx, self)+ async def _extract_attachments(+ self, message: discord.Message+ ) -> tuple[tuple[InboundAttachment, ...], tuple[tuple[str, str], ...]]:+ """Download the user's file attachments so the handler can upload them.++ Returns ``(downloaded, skipped)`` where ``skipped`` is ``(filename,+ reason)`` for files we couldn't ingest (over the per-file cap, beyond+ the per-message count, or a failed download) so the handler can tell+ the user and the model rather than silently dropping them.+ """+ attachments: list[InboundAttachment] = []+ skipped: list[tuple[str, str]] = []+ extra = message.attachments[MAX_INBOUND_ATTACHMENTS:]+ for attachment in extra:+ skipped.append((attachment.filename or "file", "too many files attached"))+ for attachment in message.attachments[:MAX_INBOUND_ATTACHMENTS]:+ name = attachment.filename or "file"+ if attachment.size > self.max_attachment_bytes:+ skipped.append((name, "too large"))+ continue+ try:+ content = await attachment.read()+ except (discord.HTTPException, discord.NotFound):+ logger.warning("Could not download attachment %s", name)+ skipped.append((name, "couldn't be downloaded"))+ continue+ attachments.append(+ InboundAttachment(+ filename=name,+ mime_type=attachment.content_type or "application/octet-stream",+ content=content,+ )+ )+ return tuple(attachments), tuple(skipped)+async def _refresh_known_server_names(self) -> None:"""Push current display names for every guild the bot is in."""for guild in self._client.guilds:diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter_test.py b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter_test.pyindex 8f52c4438b..311e827d6d 100644--- a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter_test.py+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter_test.py@@ -8,6 +8,7 @@ import pytestfrom backend.copilot.bot.adapters.base import FileAttachmentfrom backend.copilot.bot.adapters.discord.adapter import (+ MAX_INBOUND_ATTACHMENTS,THREAD_HISTORY_CHAR_BUDGET,THREAD_HISTORY_LIMIT,DiscordAdapter,@@ -1406,3 +1407,85 @@ class TestFetchReferencedConversations:_incoming(111, 555), "https://discord.com/channels/111/222/333")assert result == ()+++# ── Attachment extraction ──────────────────────────────────────────────+++def _discord_attachment(+ filename: str, content_type: str | None, size: int, data: bytes = b"x"+) -> MagicMock:+ att = MagicMock()+ att.filename = filename+ att.content_type = content_type+ att.size = size+ att.read = AsyncMock(return_value=data)+ return att+++class TestExtractAttachments:+ @pytest.mark.asyncio+ async def test_downloads_attachments_with_mime(self):+ adapter, _ = _bare_adapter()+ msg = MagicMock()+ msg.attachments = [_discord_attachment("a.png", "image/png", 10, b"png")]++ downloaded, skipped = await adapter._extract_attachments(msg)++ assert skipped == ()+ assert len(downloaded) == 1+ assert downloaded[0].filename == "a.png"+ assert downloaded[0].mime_type == "image/png"+ assert downloaded[0].content == b"png"++ @pytest.mark.asyncio+ async def test_skips_oversized_attachment(self):+ adapter, _ = _bare_adapter()+ big = adapter.max_attachment_bytes + 1+ msg = MagicMock()+ msg.attachments = [_discord_attachment("huge.bin", None, big)]++ downloaded, skipped = await adapter._extract_attachments(msg)++ assert downloaded == ()+ assert skipped == (("huge.bin", "too large"),)++ @pytest.mark.asyncio+ async def test_defaults_missing_mime_to_octet_stream(self):+ adapter, _ = _bare_adapter()+ msg = MagicMock()+ msg.attachments = [_discord_attachment("data", None, 5)]++ downloaded, _ = await adapter._extract_attachments(msg)++ assert downloaded[0].mime_type == "application/octet-stream"++ @pytest.mark.asyncio+ async def test_caps_attachment_count_and_reports_extras(self):+ adapter, _ = _bare_adapter()+ msg = MagicMock()+ msg.attachments = [+ _discord_attachment(f"f{i}.txt", "text/plain", 5)+ for i in range(MAX_INBOUND_ATTACHMENTS + 3)+ ]++ downloaded, skipped = await adapter._extract_attachments(msg)++ assert len(downloaded) == MAX_INBOUND_ATTACHMENTS+ assert downloaded[0].filename == "f0.txt"+ # The 3 over the cap are reported as skipped.+ assert len(skipped) == 3+ assert all(reason == "too many files attached" for _, reason in skipped)++ @pytest.mark.asyncio+ async def test_skips_attachment_that_fails_to_download(self):+ adapter, _ = _bare_adapter()+ att = _discord_attachment("a.png", "image/png", 10)+ att.read = AsyncMock(side_effect=discord.HTTPException(MagicMock(), "boom"))+ msg = MagicMock()+ msg.attachments = [att]++ downloaded, skipped = await adapter._extract_attachments(msg)++ assert downloaded == ()+ assert skipped == (("a.png", "couldn't be downloaded"),)diff --git a/autogpt_platform/backend/backend/copilot/bot/bot_backend.py b/autogpt_platform/backend/backend/copilot/bot/bot_backend.pyindex 492b0f548a..2d025507fe 100644--- a/autogpt_platform/backend/backend/copilot/bot/bot_backend.py+++ b/autogpt_platform/backend/backend/copilot/bot/bot_backend.py@@ -31,6 +31,8 @@ from backend.platform_linking.models import (CreateUserLinkTokenRequest,Platform,WorkspaceArtifact,+ WorkspaceUploadRequest,+ WorkspaceUploadResult,)from backend.util.clients import get_platform_linking_manager_clientfrom backend.util.exceptions import (@@ -39,6 +41,8 @@ from backend.util.exceptions import (NotFoundError,)+from .adapters.base import InboundAttachment+# How long to wait for a single chunk from the copilot stream before giving# up. Covers the case where the backend crashes mid-stream and never sends# ``StreamFinish`` — without this, the bot would hang forever on ``queue.get()``.@@ -317,6 +321,48 @@ class BotBackend:for s in resp.sessions]+ async def upload_workspace_files(+ self,+ platform: str,+ platform_user_id: str,+ platform_server_id: str | None,+ attachments: tuple[InboundAttachment, ...],+ ) -> list[WorkspaceUploadResult]:+ """Upload each attachment into the conversation owner's workspace.++ Returns one result per file (with a ``file_id`` on success or an+ ``error`` code) so the caller can attach the successes to the turn and+ tell the user about any that were rejected.+ """+ platform_enum = Platform(platform.upper())+ results: list[WorkspaceUploadResult] = []+ for attachment in attachments:+ # Isolate each upload: a transport/RPC failure (or an unlinked-owner+ # error) on one file must not abort the rest or crash the handler.+ try:+ results.append(+ await self._client.upload_workspace_file(+ request=WorkspaceUploadRequest(+ platform=platform_enum,+ platform_server_id=platform_server_id,+ platform_user_id=platform_user_id,+ filename=attachment.filename,+ mime_type=attachment.mime_type,+ content=attachment.content,+ )+ )+ )+ except Exception:+ logger.exception(+ "Failed to upload inbound attachment %s", attachment.filename+ )+ results.append(+ WorkspaceUploadResult(+ filename=attachment.filename, error="upload_failed"+ )+ )+ return results+async def fetch_workspace_artifact(self, session_id: str, file_id: str, max_bytes: int) -> WorkspaceArtifact | None:@@ -335,6 +381,7 @@ class BotBackend:message: str,session_id: Optional[str] = None,platform_server_id: Optional[str] = None,+ file_ids: Optional[list[str]] = None,on_session_id: Optional[Callable[[str], Awaitable[None]]] = None,on_setup_required: SetupRequiredCallback | None = None,on_setup_dropped: SetupDroppedCallback | None = None,@@ -351,6 +398,7 @@ class BotBackend:message=message,session_id=session_id,platform_server_id=platform_server_id,+ file_ids=file_ids or [],))if on_session_id:diff --git a/autogpt_platform/backend/backend/copilot/bot/bot_backend_test.py b/autogpt_platform/backend/backend/copilot/bot/bot_backend_test.pyindex f2fc52f008..e85280fcf7 100644--- a/autogpt_platform/backend/backend/copilot/bot/bot_backend_test.py+++ b/autogpt_platform/backend/backend/copilot/bot/bot_backend_test.py@@ -18,6 +18,7 @@ from backend.platform_linking.models import (LinkTokenResponse,Platform,ResolveResponse,+ WorkspaceUploadResult,)from backend.util.exceptions import (DuplicateChatMessageError,@@ -25,6 +26,7 @@ from backend.util.exceptions import (NotFoundError,)+from .adapters.base import InboundAttachmentfrom .bot_backend import (BotBackend,BotStreamError,@@ -438,3 +440,77 @@ class TestIsCorruptedSetupRequirements:def test_plain_text_and_dict_outputs_are_not_corrupted(self):assert not _is_corrupted_setup_requirements("plain text tool result")assert not _is_corrupted_setup_requirements({"type": "setup_requirements"})+++class TestUploadWorkspaceFiles:+ @pytest.mark.asyncio+ async def test_forwards_each_attachment_and_returns_results(self, api: BotBackend):+ api._client.upload_workspace_file = AsyncMock(+ side_effect=[+ WorkspaceUploadResult(filename="a.png", file_id="f1"),+ WorkspaceUploadResult(filename="b.exe", error="virus_detected"),+ ]+ )++ results = await api.upload_workspace_files(+ platform="discord",+ platform_user_id="u1",+ platform_server_id="g1",+ attachments=(+ InboundAttachment(+ filename="a.png", mime_type="image/png", content=b"x"+ ),+ InboundAttachment(+ filename="b.exe", mime_type="application/octet-stream", content=b"y"+ ),+ ),+ )++ assert [r.file_id for r in results] == ["f1", None]+ assert results[1].error == "virus_detected"+ assert api._client.upload_workspace_file.await_count == 2+ first = api._client.upload_workspace_file.await_args_list[0].kwargs["request"]+ assert first.platform == Platform.DISCORD+ assert first.filename == "a.png"+ assert first.platform_server_id == "g1"++ @pytest.mark.asyncio+ async def test_one_upload_failure_does_not_abort_the_rest(self, api: BotBackend):+ # A transport/RPC failure on one file becomes an upload_failed result;+ # later files still upload (and the handler never crashes).+ api._client.upload_workspace_file = AsyncMock(+ side_effect=[+ RuntimeError("connection reset"),+ WorkspaceUploadResult(filename="b.png", file_id="f2"),+ ]+ )++ results = await api.upload_workspace_files(+ platform="discord",+ platform_user_id="u1",+ platform_server_id=None,+ attachments=(+ InboundAttachment(+ filename="a.png", mime_type="image/png", content=b"x"+ ),+ InboundAttachment(+ filename="b.png", mime_type="image/png", content=b"y"+ ),+ ),+ )++ assert results[0].error == "upload_failed"+ assert results[0].file_id is None+ assert results[1].file_id == "f2"++ @pytest.mark.asyncio+ async def test_no_attachments_makes_no_calls(self, api: BotBackend):+ api._client.upload_workspace_file = AsyncMock()+ results = await api.upload_workspace_files(+ platform="discord",+ platform_user_id="u1",+ platform_server_id=None,+ attachments=(),+ )+ assert results == []+ api._client.upload_workspace_file.assert_not_awaited()diff --git a/autogpt_platform/backend/backend/copilot/bot/handler.py b/autogpt_platform/backend/backend/copilot/bot/handler.pyindex e97a64f064..d72feeec8d 100644--- a/autogpt_platform/backend/backend/copilot/bot/handler.py+++ b/autogpt_platform/backend/backend/copilot/bot/handler.py@@ -41,6 +41,11 @@ THREAD_NAME_MAX_LENGTH = 100THREAD_NAME_PREFIX = "AutoPilot: "TITLE_RENAME_ATTEMPTS = 5TITLE_RENAME_INTERVAL_SECONDS = 1.0+# Cap on file IDs carried into a single turn — must stay <= the+# ``BotChatRequest.file_ids`` ``max_length``. Batching several file-heavy+# messages can accumulate more than that; cap so the turn runs (with a log)+# instead of failing validation.+MAX_TURN_FILE_IDS = 20@dataclass@@ -55,6 +60,9 @@ class TargetState:processing: bool = Falsepending: list[tuple[str, str, str]] = field(default_factory=list)# Each entry: (username, user_id, text)+ # Workspace file IDs uploaded for messages in `pending`, drained together+ # with them so a batched turn carries every attached file.+ pending_file_ids: list[str] = field(default_factory=list)class MessageHandler:@@ -65,7 +73,7 @@ class MessageHandler:self._rename_tasks: set[asyncio.Task[None]] = set()async def handle(self, ctx: MessageContext, adapter: PlatformAdapter) -> None:- if not ctx.text.strip():+ if not ctx.text.strip() and not ctx.attachments:if ctx.channel_type == "channel":await adapter.send_reply(ctx.channel_id,@@ -104,8 +112,64 @@ class MessageHandler:char_count=len(ctx.text),)- message_text = self._message_text(ctx, include_thread_history)- await self._enqueue_and_process(ctx, adapter, target_id, message_text)+ file_ids, upload_problems = await self._upload_attachments(ctx)++ # Files that didn't make it: adapter-stage (too large / failed download)+ # plus upload-stage (virus / quota). Tell the user AND, below, the model+ # — so neither assumes a dropped file was actually read.+ problems = list(ctx.skipped_attachments) + upload_problems+ if problems:+ await adapter.send_message(target_id, _format_attachment_problems(problems))++ # Decide on NEW input only — typed text or a usable upload. Thread+ # history (folded into the prompt below) is context, not a reason to+ # respond, so don't let it keep an otherwise-empty turn alive.+ if not ctx.text.strip() and not file_ids:+ # The user already got the note. Don't enqueue a blank turn, and+ # unsubscribe a thread we just created for a channel message so it+ # doesn't linger orphaned-but-subscribed for 7 days.+ if ctx.channel_type == "channel" and target_id != ctx.channel_id:+ await threads.unsubscribe(ctx.platform, target_id)+ return++ # A file-only message gets a nudge so AutoPilot looks at the uploads+ # instead of seeing an empty "[Current message]".+ current_text = ctx.text if ctx.text.strip() else "(see the attached file(s))"+ message_text = self._message_text(ctx, include_thread_history, current_text)+ if problems:+ message_text += "\n\n" + _model_attachment_note(problems)+ await self._enqueue_and_process(ctx, adapter, target_id, message_text, file_ids)++ async def _upload_attachments(+ self, ctx: MessageContext+ ) -> tuple[list[str], list[tuple[str, str]]]:+ """Upload the user's attachments to the workspace.++ Returns ``(file_ids, problems)`` — the IDs that succeeded, and+ ``(filename, reason)`` for the ones that were rejected — so the caller+ can attach the successes and surface the failures.+ """+ if not ctx.attachments:+ return [], []+ try:+ results = await self._api.upload_workspace_files(+ platform=ctx.platform,+ platform_user_id=ctx.user_id,+ platform_server_id=ctx.server_id,+ attachments=ctx.attachments,+ )+ except Exception:+ # The upload path itself failed (not a per-file rejection). Don't+ # drop the message — surface it and let any text still go through.+ logger.exception("Attachment upload failed for user %s", ctx.user_id)+ return [], [(a.filename, "couldn't be uploaded") for a in ctx.attachments]+ file_ids = [r.file_id for r in results if r.file_id]+ problems = [+ (r.filename, _UPLOAD_ERROR_TEXT.get(r.error or "", "couldn't be uploaded"))+ for r in results+ if not r.file_id+ ]+ return file_ids, problems# -- Target resolution --@@ -131,14 +195,22 @@ class MessageHandler:# -- Batched streaming --- def _message_text(self, ctx: MessageContext, include_thread_history: bool) -> str:+ def _message_text(+ self,+ ctx: MessageContext,+ include_thread_history: bool,+ current_text: str | None = None,+ ) -> str:+ # ``current_text`` overrides ``ctx.text`` as the "current message" body+ # (e.g. a nudge for a file-only message); defaults to the raw text.+ text = ctx.text if current_text is None else current_text# Referenced conversations (links/@-mentions the user pasted) are always# surfaced — that's the whole point of fetching them. Thread history is# only included on the first @-into a thread we don't own; a subscribed# thread's prior turns already live in the session.thread_history = ctx.thread_history if include_thread_history else ()if not thread_history and not ctx.referenced_conversations:- return ctx.text+ return textplatform_display = ctx.platform.capitalize()lines: list[str] = []@@ -162,7 +234,7 @@ class MessageHandler:lines.append("\n[Recent thread context before this message]")for entry in thread_history:lines.append(self._format_history_entry(entry, platform_display))- lines.append(f"\n[Current message]\n{ctx.text}")+ lines.append(f"\n[Current message]\n{text}")return "\n".join(lines)@staticmethod@@ -180,9 +252,12 @@ class MessageHandler:adapter: PlatformAdapter,target_id: str,message_text: str | None = None,+ file_ids: list[str] | None = None,) -> None:state = self._targets.setdefault(target_id, TargetState())state.pending.append((ctx.username, ctx.user_id, message_text or ctx.text))+ if file_ids:+ state.pending_file_ids.extend(file_ids)if state.processing:# Another invocation is streaming for this target — it will pick@@ -193,8 +268,19 @@ class MessageHandler:try:while state.pending:batch = list(state.pending)+ batch_file_ids = list(state.pending_file_ids)state.pending.clear()- await self._stream_batch(batch, ctx, adapter, target_id)+ state.pending_file_ids.clear()+ if len(batch_file_ids) > MAX_TURN_FILE_IDS:+ logger.warning(+ "Dropping %d batched file(s) over the per-turn cap of %d",+ len(batch_file_ids) - MAX_TURN_FILE_IDS,+ MAX_TURN_FILE_IDS,+ )+ batch_file_ids = batch_file_ids[:MAX_TURN_FILE_IDS]+ await self._stream_batch(+ batch, ctx, adapter, target_id, file_ids=batch_file_ids+ )finally:state.processing = False# Drop the empty state so the dict doesn't grow unbounded across@@ -327,6 +413,7 @@ class MessageHandler:ctx: MessageContext,adapter: PlatformAdapter,target_id: str,+ file_ids: list[str] | None = None,) -> None:prefixed = format_batch(batch, ctx.platform)@@ -425,6 +512,7 @@ class MessageHandler:message=prefixed,session_id=active_session_id,platform_server_id=ctx.server_id,+ file_ids=file_ids,on_session_id=_on_session_id,on_setup_required=_on_setup_required,on_setup_dropped=_on_setup_dropped,@@ -643,6 +731,32 @@ async def _keep_typing(adapter: PlatformAdapter, target_id: str) -> None:logger.debug("Typing loop error", exc_info=True)+_UPLOAD_ERROR_TEXT: dict[str, str] = {+ "virus_detected": "failed a virus scan",+ "scan_unavailable": "couldn't be virus-scanned right now — try again shortly",+ "rejected": "was too large or would exceed your storage limit",+ "upload_failed": "couldn't be uploaded",+}+++def _format_attachment_problems(problems: list[tuple[str, str]]) -> str:+ """User-facing note listing attachments that couldn't be attached and why."""+ lines = ["⚠️ Some files couldn't be attached:"]+ for filename, reason in problems:+ lines.append(f"• `{filename}` {reason}")+ return "\n".join(lines)+++def _model_attachment_note(problems: list[tuple[str, str]]) -> str:+ """Note injected into the turn so AutoPilot knows which files it does NOT+ have, instead of assuming a dropped attachment was read."""+ listed = ", ".join(f"{filename} ({reason})" for filename, reason in problems)+ return (+ f"[Note: the following attachment(s) could NOT be attached and are "+ f"unavailable to you — do not claim to have read them: {listed}]"+ )++def build_thread_name(text: str, username: str) -> str:"""Build a Discord-safe thread name from the user's first prompt."""cleaned = " ".join(text.split())diff --git a/autogpt_platform/backend/backend/copilot/bot/handler_test.py b/autogpt_platform/backend/backend/copilot/bot/handler_test.pyindex 4568d7df7d..1ecd3ccef9 100644--- a/autogpt_platform/backend/backend/copilot/bot/handler_test.py+++ b/autogpt_platform/backend/backend/copilot/bot/handler_test.py@@ -6,17 +6,24 @@ from unittest.mock import AsyncMock, MagicMock, patchimport pytest-from backend.platform_linking.models import WorkspaceArtifact+from backend.platform_linking.models import WorkspaceArtifact, WorkspaceUploadResultfrom backend.util.exceptions import DuplicateChatMessageError, NotFoundErrorfrom .adapters.base import (ChannelType,+ InboundAttachment,MessageContext,MessageHistoryEntry,ReferencedConversation,)from .bot_backend import LinkTokenResult, ResolveResult-from .handler import MessageHandler, TargetState, build_thread_name, clamp_thread_name+from .handler import (+ MAX_TURN_FILE_IDS,+ MessageHandler,+ TargetState,+ build_thread_name,+ clamp_thread_name,+)def _ctx(@@ -31,6 +38,8 @@ def _ctx(bot_mentioned: bool = False,thread_history: tuple[MessageHistoryEntry, ...] = (),referenced_conversations: tuple[ReferencedConversation, ...] = (),+ attachments: tuple[InboundAttachment, ...] = (),+ skipped_attachments: tuple[tuple[str, str], ...] = (),) -> MessageContext:return MessageContext(platform="discord",@@ -44,6 +53,8 @@ def _ctx(bot_mentioned=bot_mentioned,thread_history=thread_history,referenced_conversations=referenced_conversations,+ attachments=attachments,+ skipped_attachments=skipped_attachments,)@@ -74,6 +85,7 @@ def _api(*, server_linked: bool = True, user_linked: bool = True) -> MagicMock:))api.fetch_workspace_artifact = AsyncMock(return_value=None)+ api.upload_workspace_files = AsyncMock(return_value=[])async def _empty_stream(*args, **kwargs):if False:@@ -396,7 +408,7 @@ class TestBatching:stream_calls: list[list] = []- async def fake_stream_batch(batch, ctx, ad, tid):+ async def fake_stream_batch(batch, ctx, ad, tid, file_ids=None):stream_calls.append(list(batch))handler._stream_batch = fake_stream_batch # type: ignore[method-assign]@@ -417,7 +429,7 @@ class TestBatching:seen: list[list] = []- async def fake_stream_batch(batch, ctx, ad, tid):+ async def fake_stream_batch(batch, ctx, ad, tid, file_ids=None):seen.append(list(batch))if len(seen) == 1:# Simulate another caller appending during the first stream.@@ -921,3 +933,263 @@ class TestMessageTextReferencedConversations:assert "earlier point" in out# Referenced block precedes the immediate thread context.assert out.index("linked content") < out.index("earlier point")+++class TestAttachments:+ @staticmethod+ def _upload_recording_api(results):+ api = _api()+ api.upload_workspace_files = AsyncMock(return_value=results)+ captured: dict = {}++ async def _recording_stream(*args, **kwargs):+ captured["file_ids"] = kwargs.get("file_ids")+ captured["message"] = kwargs.get("message")+ if False:+ yield ""++ api.stream_chat = _recording_stream+ return api, captured++ @staticmethod+ def _dm_ctx(text, files):+ return _ctx(+ channel_type="dm",+ server_id=None,+ channel_id="dm-1",+ text=text,+ attachments=tuple(+ InboundAttachment(filename=n, mime_type=m, content=b"x")+ for n, m in files+ ),+ )++ @pytest.mark.asyncio+ async def test_uploads_attachments_and_passes_file_ids(self):+ results = [WorkspaceUploadResult(filename="a.png", file_id="f1")]+ api, captured = self._upload_recording_api(results)+ handler = MessageHandler(api)+ adapter = _adapter()++ await handler.handle(+ self._dm_ctx("look at this", [("a.png", "image/png")]), adapter+ )++ api.upload_workspace_files.assert_awaited_once()+ assert captured["file_ids"] == ["f1"]++ @pytest.mark.asyncio+ async def test_failed_upload_is_reported_and_good_files_still_sent(self):+ results = [+ WorkspaceUploadResult(filename="good.png", file_id="f1"),+ WorkspaceUploadResult(filename="bad.exe", error="virus_detected"),+ ]+ api, captured = self._upload_recording_api(results)+ handler = MessageHandler(api)+ adapter = _adapter()++ await handler.handle(+ self._dm_ctx(+ "files",+ [("good.png", "image/png"), ("bad.exe", "application/octet-stream")],+ ),+ adapter,+ )++ # The clean file still reaches the turn.+ assert captured["file_ids"] == ["f1"]+ # The rejected one is surfaced to the user.+ note = adapter.send_message.await_args_list[0].args[1]+ assert "bad.exe" in note+ assert "virus scan" in note++ @pytest.mark.asyncio+ async def test_dropped_attachments_surfaced_to_user_and_model(self):+ # An adapter-stage skip (too large) and an upload-stage rejection+ # (virus) must both reach the user (a note) AND the model (in the turn+ # text), while a clean file still flows through.+ results = [+ WorkspaceUploadResult(filename="ok.png", file_id="f1"),+ WorkspaceUploadResult(filename="virus.exe", error="virus_detected"),+ ]+ api, captured = self._upload_recording_api(results)+ handler = MessageHandler(api)+ adapter = _adapter()+ ctx = _ctx(+ channel_type="dm",+ server_id=None,+ channel_id="dm-1",+ text="check these",+ attachments=(+ InboundAttachment(+ filename="ok.png", mime_type="image/png", content=b"x"+ ),+ InboundAttachment(+ filename="virus.exe",+ mime_type="application/octet-stream",+ content=b"y",+ ),+ ),+ skipped_attachments=(("huge.zip", "too large"),),+ )++ await handler.handle(ctx, adapter)++ assert captured["file_ids"] == ["f1"]+ user_note = adapter.send_message.await_args_list[0].args[1]+ assert "huge.zip" in user_note and "virus.exe" in user_note+ # The model is told it does NOT have these files.+ assert "do not claim to have read them" in captured["message"]+ assert "huge.zip" in captured["message"]+ assert "virus.exe" in captured["message"]++ @pytest.mark.asyncio+ async def test_file_only_message_is_processed(self):+ results = [WorkspaceUploadResult(filename="a.pdf", file_id="f1")]+ api, captured = self._upload_recording_api(results)+ handler = MessageHandler(api)+ adapter = _adapter()++ # No text, just a file — must not be dropped as an "empty" message.+ await handler.handle(self._dm_ctx("", [("a.pdf", "application/pdf")]), adapter)++ api.upload_workspace_files.assert_awaited_once()+ assert captured["file_ids"] == ["f1"]++ @pytest.mark.asyncio+ async def test_upload_rpc_failure_keeps_text_turn_and_notifies(self):+ # The upload path itself blowing up (not a per-file rejection) must not+ # drop the message — any text still goes through and the user is told.+ api, captured = self._upload_recording_api([])+ api.upload_workspace_files = AsyncMock(side_effect=RuntimeError("rpc down"))+ handler = MessageHandler(api)+ adapter = _adapter()++ await handler.handle(+ self._dm_ctx("summarize this", [("a.pdf", "application/pdf")]), adapter+ )++ assert captured["file_ids"] == []+ note = adapter.send_message.await_args_list[0].args[1]+ assert "a.pdf" in note+ assert "couldn't be uploaded" in note.lower()++ @pytest.mark.asyncio+ async def test_batched_file_ids_capped_to_per_turn_limit(self):+ # Several file-heavy messages batched together can exceed the+ # BotChatRequest file_ids limit; the drain must cap, not fail.+ handler = MessageHandler(_api())+ adapter = _adapter()+ captured: list[list[str] | None] = []++ async def fake_stream_batch(batch, ctx, ad, tid, file_ids=None):+ captured.append(file_ids)++ handler._stream_batch = fake_stream_batch # type: ignore[method-assign]+ too_many = [f"f{i}" for i in range(MAX_TURN_FILE_IDS + 5)]++ await handler._enqueue_and_process(+ _ctx(channel_type="dm", server_id=None, channel_id="dm-1", text="hi"),+ adapter,+ "dm-1",+ "hi",+ too_many,+ )++ assert captured and len(captured[0]) == MAX_TURN_FILE_IDS++ @pytest.mark.asyncio+ async def test_file_only_thread_with_history_all_rejected_does_not_enqueue(self):+ # First @-into an unowned thread renders its history into the prompt, so+ # message_text is non-empty. A file-only message whose uploads all fail+ # must STILL not enqueue a turn (it would answer old context only).+ api = _api()+ api.upload_workspace_files = AsyncMock(+ return_value=[+ WorkspaceUploadResult(filename="bad.exe", error="virus_detected")+ ]+ )+ enqueued = []++ async def _recording_stream(*args, **kwargs):+ enqueued.append(kwargs)+ if False:+ yield ""++ api.stream_chat = _recording_stream+ handler = MessageHandler(api)+ adapter = _adapter()+ ctx = _ctx(+ channel_type="thread",+ bot_mentioned=True,+ text="",+ thread_history=(MessageHistoryEntry("Alice", "u1", "old message"),),+ attachments=(+ InboundAttachment(+ filename="bad.exe",+ mime_type="application/octet-stream",+ content=b"x",+ ),+ ),+ )++ with patch(+ "backend.copilot.bot.handler.threads.is_subscribed",+ new=AsyncMock(return_value=False),+ ):+ await handler.handle(ctx, adapter)++ assert enqueued == [] # no turn, despite the thread history++ @pytest.mark.asyncio+ async def test_channel_file_only_all_rejected_unsubscribes_orphan_thread(self):+ # A channel @mention creates+subscribes a thread; if it's file-only and+ # every upload fails, we must unsubscribe the thread we just created so+ # it doesn't linger orphaned-but-subscribed.+ api = _api()+ api.upload_workspace_files = AsyncMock(+ return_value=[+ WorkspaceUploadResult(filename="bad.exe", error="virus_detected")+ ]+ )+ handler = MessageHandler(api)+ adapter = _adapter() # create_thread → "thread-new"+ ctx = _ctx(+ channel_type="channel",+ text="",+ attachments=(+ InboundAttachment(+ filename="bad.exe",+ mime_type="application/octet-stream",+ content=b"x",+ ),+ ),+ )++ with (+ patch("backend.copilot.bot.handler.threads.subscribe", new=AsyncMock()),+ patch(+ "backend.copilot.bot.handler.threads.unsubscribe", new=AsyncMock()+ ) as unsub,+ ):+ await handler.handle(ctx, adapter)++ unsub.assert_awaited_once_with("discord", "thread-new")++ @pytest.mark.asyncio+ async def test_file_only_message_with_all_uploads_rejected_does_not_enqueue(self):+ # Every upload failed → no successful file_ids and no text. The user+ # already got the rejection note, so we must NOT enqueue a blank turn.+ results = [WorkspaceUploadResult(filename="bad.exe", error="virus_detected")]+ api, captured = self._upload_recording_api(results)+ handler = MessageHandler(api)+ adapter = _adapter()++ await handler.handle(+ self._dm_ctx("", [("bad.exe", "application/octet-stream")]), adapter+ )++ # Rejection note sent, but the turn was never started.+ assert "file_ids" not in captured+ note = adapter.send_message.await_args_list[0].args[1]+ assert "bad.exe" in notediff --git a/autogpt_platform/backend/backend/platform_linking/chat.py b/autogpt_platform/backend/backend/platform_linking/chat.pyindex 0a477f38b8..23464140b4 100644--- a/autogpt_platform/backend/backend/platform_linking/chat.py+++ b/autogpt_platform/backend/backend/platform_linking/chat.py@@ -1,8 +1,10 @@"""Chat-turn orchestration for the platform bot bridge."""import logging+import osfrom uuid import uuid4+from backend.api.features.store.exceptions import VirusDetectedError, VirusScanErrorfrom backend.copilot import stream_registryfrom backend.copilot.executor.utils import enqueue_copilot_turnfrom backend.copilot.model import (@@ -12,8 +14,9 @@ from backend.copilot.model import (get_chat_session,get_user_sessions,)-from backend.data.db_accessors import platform_linking_db+from backend.data.db_accessors import platform_linking_db, workspace_dbfrom backend.util.exceptions import DuplicateChatMessageError, NotFoundError+from backend.util.workspace import WorkspaceManagerfrom .models import (BotChatRequest,@@ -21,6 +24,8 @@ from .models import (ChatTurnHandle,ListUserChatsResponse,Platform,+ WorkspaceUploadRequest,+ WorkspaceUploadResult,)logger = logging.getLogger(__name__)@@ -29,26 +34,91 @@ CHAT_TOOL_CALL_ID = "chat_stream"CHAT_TOOL_NAME = "chat"-async def resolve_chat_owner(request: BotChatRequest) -> str:- """Return the AutoGPT user ID that owns the platform conversation.+async def _resolve_owner(+ platform: str, platform_server_id: str | None, platform_user_id: str+) -> str:+ """Resolve the AutoGPT user that owns a platform conversation.Server context → server owner. DM context → the DM-linked user."""- platform = request.platform.valuedb = platform_linking_db()- if request.platform_server_id:- owner = await db.find_server_link_owner(platform, request.platform_server_id)+ if platform_server_id:+ owner = await db.find_server_link_owner(platform, platform_server_id)if owner is None:raise NotFoundError("This server is not linked to an AutoGPT account.")return owner- owner = await db.find_user_link_owner(platform, request.platform_user_id)+ owner = await db.find_user_link_owner(platform, platform_user_id)if owner is None:raise NotFoundError("Your DMs are not linked to an AutoGPT account.")return owner+async def resolve_chat_owner(request: BotChatRequest) -> str:+ """Return the AutoGPT user ID that owns the platform conversation."""+ return await _resolve_owner(+ request.platform.value,+ request.platform_server_id,+ request.platform_user_id,+ )+++async def upload_workspace_file(+ request: WorkspaceUploadRequest,+) -> WorkspaceUploadResult:+ """Store a user-attached file in the conversation owner's workspace.++ Runs the same machinery as the web upload endpoint+ (``WorkspaceManager.write_file`` → ClamAV scan → storage), so AutoPilot can+ read the file during the turn. Failures map to a stable ``error`` code+ rather than raising, so one bad file doesn't sink the whole message.+ """+ owner_user_id = await _resolve_owner(+ request.platform.value,+ request.platform_server_id,+ request.platform_user_id,+ )+ # Strip any directory components a (possibly hostile) client put in the+ # filename so it can't traverse the workspace path or leak ".."/separators+ # into the storage backend (e.g. GCS blob names). write_file passes the+ # filename through to storage, so use the sanitized name everywhere.+ # Basename alone still yields "."/".." for those inputs, which would re-+ # introduce a special segment into uploads/<uuid>/<name>, so reject them.+ safe_name = os.path.basename(request.filename.replace("\\", "/"))+ if safe_name in {"", ".", ".."}:+ safe_name = "file"+ try:+ workspace = await workspace_db().get_or_create_workspace(owner_user_id)+ manager = WorkspaceManager(owner_user_id, workspace.id)+ stored = await manager.write_file(+ content=request.content,+ filename=safe_name,+ # Unique sub-path so re-uploading a file with the same name (e.g.+ # two ``test.txt``) is a fresh file, not a path conflict.+ path=f"uploads/{uuid4().hex}/{safe_name}",+ mime_type=request.mime_type,+ metadata={"origin": "user-upload"},+ )+ except VirusDetectedError:+ return WorkspaceUploadResult(filename=request.filename, error="virus_detected")+ except VirusScanError:+ return WorkspaceUploadResult(+ filename=request.filename, error="scan_unavailable"+ )+ except NotFoundError:+ # NotFoundError subclasses ValueError; let a missing user/workspace+ # propagate as a linking error instead of being mislabelled "rejected".+ raise+ except ValueError:+ # write_file raises ValueError for size / storage-quota limits.+ return WorkspaceUploadResult(filename=request.filename, error="rejected")+ except Exception:+ logger.exception("Workspace upload failed for %s", request.filename)+ return WorkspaceUploadResult(filename=request.filename, error="upload_failed")+ return WorkspaceUploadResult(filename=request.filename, file_id=stored.id)++async def start_chat_turn(request: BotChatRequest) -> ChatTurnHandle:"""Prepare a copilot turn; caller subscribes via the returned handle.@@ -107,6 +177,7 @@ async def start_chat_turn(request: BotChatRequest) -> ChatTurnHandle:message=request.message,turn_id=turn_id,is_user_message=True,+ file_ids=request.file_ids or None,)logger.info(diff --git a/autogpt_platform/backend/backend/platform_linking/chat_test.py b/autogpt_platform/backend/backend/platform_linking/chat_test.pyindex d00e59b0e9..a2300d107f 100644--- a/autogpt_platform/backend/backend/platform_linking/chat_test.py+++ b/autogpt_platform/backend/backend/platform_linking/chat_test.py@@ -5,10 +5,11 @@ from unittest.mock import AsyncMock, MagicMock, patchimport pytest+from backend.api.features.store.exceptions import VirusDetectedError, VirusScanErrorfrom backend.util.exceptions import DuplicateChatMessageError, NotFoundError-from .chat import list_user_chats, start_chat_turn-from .models import BotChatRequest, Platform+from .chat import list_user_chats, start_chat_turn, upload_workspace_file+from .models import BotChatRequest, Platform, WorkspaceUploadRequestdef _request(**overrides) -> BotChatRequest:@@ -217,3 +218,121 @@ class TestListUserChats:await list_user_chats(Platform.DISCORD, "pu1", limit=10_000, offset=-50)mock_get_sessions.assert_awaited_once_with("owner-1", limit=25, offset=0)+++class TestUploadWorkspaceFile:+ @staticmethod+ def _req(**overrides):+ defaults = dict(+ platform=Platform.DISCORD,+ platform_user_id="pu1",+ filename="a.png",+ mime_type="image/png",+ content=b"data",+ )+ defaults.update(overrides)+ return WorkspaceUploadRequest(**defaults)++ @staticmethod+ def _patches(write_file_mock):+ db = MagicMock()+ db.find_user_link_owner = AsyncMock(return_value="owner-1")+ ws_db = MagicMock()+ ws_db.get_or_create_workspace = AsyncMock(return_value=MagicMock(id="ws-1"))+ manager = MagicMock()+ manager.write_file = write_file_mock+ return (+ patch("backend.platform_linking.chat.platform_linking_db", return_value=db),+ patch("backend.platform_linking.chat.workspace_db", return_value=ws_db),+ patch(+ "backend.platform_linking.chat.WorkspaceManager", return_value=manager+ ),+ )++ @pytest.mark.asyncio+ async def test_success_returns_file_id(self):+ write = AsyncMock(return_value=MagicMock(id="file-1"))+ p1, p2, p3 = self._patches(write)+ with p1, p2, p3:+ result = await upload_workspace_file(self._req())+ assert result.file_id == "file-1"+ assert result.error is None++ @pytest.mark.asyncio+ async def test_virus_detected_maps_to_error(self):+ write = AsyncMock(side_effect=VirusDetectedError("EICAR-Test"))+ p1, p2, p3 = self._patches(write)+ with p1, p2, p3:+ result = await upload_workspace_file(self._req())+ assert result.file_id is None+ assert result.error == "virus_detected"++ @pytest.mark.asyncio+ async def test_scan_unavailable_maps_to_error(self):+ write = AsyncMock(side_effect=VirusScanError("clamd down"))+ p1, p2, p3 = self._patches(write)+ with p1, p2, p3:+ result = await upload_workspace_file(self._req())+ assert result.error == "scan_unavailable"++ @pytest.mark.asyncio+ async def test_size_or_quota_maps_to_rejected(self):+ write = AsyncMock(side_effect=ValueError("File too large"))+ p1, p2, p3 = self._patches(write)+ with p1, p2, p3:+ result = await upload_workspace_file(self._req())+ assert result.error == "rejected"++ @pytest.mark.asyncio+ async def test_unexpected_error_maps_to_upload_failed(self):+ write = AsyncMock(side_effect=RuntimeError("storage exploded"))+ p1, p2, p3 = self._patches(write)+ with p1, p2, p3:+ result = await upload_workspace_file(self._req())+ assert result.error == "upload_failed"++ @pytest.mark.asyncio+ async def test_filename_path_components_are_stripped_from_storage_path(self):+ write = AsyncMock(return_value=MagicMock(id="file-1"))+ p1, p2, p3 = self._patches(write)+ with p1, p2, p3:+ await upload_workspace_file(self._req(filename="../../etc/passwd"))+ # Neither the storage path nor the filename passed to the storage+ # backend may carry traversal segments — only the sanitized basename.+ kwargs = write.await_args.kwargs+ assert ".." not in kwargs["path"]+ assert kwargs["path"].endswith("/passwd")+ assert kwargs["filename"] == "passwd"++ @pytest.mark.asyncio+ @pytest.mark.parametrize("name", [".", "..", "dir/..", "/"])+ async def test_dot_filenames_fall_back_to_safe_name(self, name: str):+ # Basename of "."/".." is still "."/"..", which would re-introduce a+ # special segment into uploads/<uuid>/<name>; they must become "file".+ write = AsyncMock(return_value=MagicMock(id="file-1"))+ p1, p2, p3 = self._patches(write)+ with p1, p2, p3:+ await upload_workspace_file(self._req(filename=name))+ kwargs = write.await_args.kwargs+ assert kwargs["filename"] == "file"+ assert kwargs["path"].endswith("/file")++ @pytest.mark.asyncio+ async def test_unlinked_user_raises_not_found(self):+ db = MagicMock()+ db.find_user_link_owner = AsyncMock(return_value=None)+ with patch(+ "backend.platform_linking.chat.platform_linking_db", return_value=db+ ):+ with pytest.raises(NotFoundError):+ await upload_workspace_file(self._req())++ @pytest.mark.asyncio+ async def test_not_found_in_try_propagates_not_rejected(self):+ # NotFoundError subclasses ValueError; if one is raised inside the+ # write path it must propagate, not be swallowed as "rejected".+ write = AsyncMock(side_effect=NotFoundError("workspace gone"))+ p1, p2, p3 = self._patches(write)+ with p1, p2, p3:+ with pytest.raises(NotFoundError):+ await upload_workspace_file(self._req())diff --git a/autogpt_platform/backend/backend/platform_linking/manager.py b/autogpt_platform/backend/backend/platform_linking/manager.pyindex 4824308a80..953d432c87 100644--- a/autogpt_platform/backend/backend/platform_linking/manager.py+++ b/autogpt_platform/backend/backend/platform_linking/manager.py@@ -6,7 +6,7 @@ from backend.data.db_accessors import bot_analytics_db, platform_linking_dbfrom backend.util.service import AppService, AppServiceClient, endpoint_to_async, exposefrom backend.util.settings import Settings-from .chat import list_user_chats, start_chat_turn+from .chat import list_user_chats, start_chat_turn, upload_workspace_filefrom .models import (BotChatRequest,BotEventInput,@@ -20,6 +20,8 @@ from .models import (Platform,ResolveResponse,WorkspaceArtifact,+ WorkspaceUploadRequest,+ WorkspaceUploadResult,)logger = logging.getLogger(__name__)@@ -79,6 +81,12 @@ class PlatformLinkingManager(AppService):async def start_chat_turn(self, request: BotChatRequest) -> ChatTurnHandle:return await start_chat_turn(request)+ @expose+ async def upload_workspace_file(+ self, request: WorkspaceUploadRequest+ ) -> WorkspaceUploadResult:+ return await upload_workspace_file(request)+@exposeasync def refresh_server_link_name(self, platform: Platform, platform_server_id: str, server_name: str@@ -144,6 +152,9 @@ class PlatformLinkingManagerClient(AppServiceClient):PlatformLinkingManager.list_user_server_ids)start_chat_turn = endpoint_to_async(PlatformLinkingManager.start_chat_turn)+ upload_workspace_file = endpoint_to_async(+ PlatformLinkingManager.upload_workspace_file+ )refresh_server_link_name = endpoint_to_async(PlatformLinkingManager.refresh_server_link_name)diff --git a/autogpt_platform/backend/backend/platform_linking/models.py b/autogpt_platform/backend/backend/platform_linking/models.pyindex 030f62aa16..b23eaeb0d3 100644--- a/autogpt_platform/backend/backend/platform_linking/models.py+++ b/autogpt_platform/backend/backend/platform_linking/models.py@@ -112,11 +112,40 @@ class BotChatRequest(BaseModel):default=None,description="Existing CoPilot session ID. If omitted, a new session is created.",)+ file_ids: list[str] = Field(+ default_factory=list,+ max_length=20,+ description="Workspace file IDs (already uploaded) to attach to the turn.",+ )+++class WorkspaceUploadRequest(BaseModel):+ """Upload a user-attached file into the conversation owner's workspace.++ Resolved to the same owner a chat turn bills to (server owner, or the+ DM-linked user). The file runs through the same AV scan + storage as the+ web upload endpoint."""++ platform: Platform+ platform_server_id: str | None = Field(default=None, min_length=1, max_length=255)+ 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# ── Response Models ────────────────────────────────────────────────────+class WorkspaceUploadResult(BaseModel):+ """Outcome of one file upload. ``file_id`` is set on success; otherwise+ ``error`` carries a stable code the bot maps to user-facing text."""++ filename: str+ file_id: str | None = None+ error: str | None = None++class LinkTokenResponse(BaseModel):token: strexpires_at: datetime