-
Notifications
You must be signed in to change notification settings - Fork 46k
feat(backend/copilot): attach uploaded images and PDFs as multimodal vision blocks #12273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
majdyz
merged 17 commits into
dev
from
otto/open-3022-featbackendcopilot-attach-uploaded-images-and-pdfs-as
Mar 5, 2026
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f7874ee
feat(backend/copilot): attach uploaded images and PDFs as multimodal …
Otto-AGPT 5cdb297
refactor: clean up noqa, use builder pattern, add more file types
Otto-AGPT da8802f
fix: address PR review feedback
Otto-AGPT d8e9dd7
fix: address remaining PR review comments for multimodal file attachm…
majdyz 438036a
fix: bump image base64 limit, normalize MIME in _extract_content_block
majdyz 07e049f
refactor: extract _split_content_blocks helper for MCP result handling
majdyz cbff614
refactor: use file paths instead of inline base64 for multimodal atta…
majdyz c87798f
fix: embed images as vision content blocks in user message
majdyz 13e34b6
fix: return 409 with message when uploading duplicate filename
majdyz 24ae013
feat(frontend/copilot): render inline image previews for uploaded fil…
majdyz 3ab4a2e
merge: resolve conflicts with dev branch
majdyz 897bbe6
refactor(frontend/copilot): use OutputRenderers system for media rend…
majdyz 20375c5
refactor: simplify workspace file handling, remove unused binary-to-c…
majdyz 2ca9ba1
fix: guard _prepare_file_attachments against missing user_id
majdyz 10f06e0
merge: resolve conflicts with dev branch
majdyz bec8fe4
fix: add **_kwargs to stream_chat_completion_dummy for file_ids compat
majdyz de1ce73
merge: resolve conflicts with dev, convert PreparedAttachments to Bas…
majdyz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
autogpt_platform/backend/backend/copilot/sdk/service_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| """Tests for SDK service helpers.""" | ||
|
|
||
| import base64 | ||
| import os | ||
| from dataclasses import dataclass | ||
| from unittest.mock import AsyncMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from .service import _prepare_file_attachments | ||
|
|
||
|
|
||
| @dataclass | ||
| class _FakeFileInfo: | ||
| id: str | ||
| name: str | ||
| path: str | ||
| mime_type: str | ||
| size_bytes: int | ||
|
|
||
|
|
||
| _PATCH_TARGET = "backend.copilot.sdk.service.get_manager" | ||
|
|
||
|
|
||
| class TestPrepareFileAttachments: | ||
| @pytest.mark.asyncio | ||
| async def test_empty_list_returns_empty(self, tmp_path): | ||
| result = await _prepare_file_attachments([], "u", "s", str(tmp_path)) | ||
| assert result.hint == "" | ||
| assert result.image_blocks == [] | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_image_embedded_as_vision_block(self, tmp_path): | ||
| """JPEG images should become vision content blocks, not files on disk.""" | ||
| raw = b"\xff\xd8\xff\xe0fake-jpeg" | ||
| info = _FakeFileInfo( | ||
| id="abc", | ||
| name="photo.jpg", | ||
| path="/photo.jpg", | ||
| mime_type="image/jpeg", | ||
| size_bytes=len(raw), | ||
| ) | ||
| mgr = AsyncMock() | ||
| mgr.get_file_info.return_value = info | ||
| mgr.read_file_by_id.return_value = raw | ||
|
|
||
| with patch(_PATCH_TARGET, new_callable=AsyncMock, return_value=mgr): | ||
| result = await _prepare_file_attachments( | ||
| ["abc"], "user1", "sess1", str(tmp_path) | ||
| ) | ||
|
|
||
| assert "1 file" in result.hint | ||
| assert "photo.jpg" in result.hint | ||
| assert "embedded as image" in result.hint | ||
| assert len(result.image_blocks) == 1 | ||
| block = result.image_blocks[0] | ||
| assert block["type"] == "image" | ||
| assert block["source"]["media_type"] == "image/jpeg" | ||
| assert block["source"]["data"] == base64.b64encode(raw).decode("ascii") | ||
| # Image should NOT be written to disk (embedded instead) | ||
| assert not os.path.exists(os.path.join(tmp_path, "photo.jpg")) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_pdf_saved_to_disk(self, tmp_path): | ||
| """PDFs should be saved to disk for Read tool access, not embedded.""" | ||
| info = _FakeFileInfo("f1", "doc.pdf", "/doc.pdf", "application/pdf", 50) | ||
| mgr = AsyncMock() | ||
| mgr.get_file_info.return_value = info | ||
| mgr.read_file_by_id.return_value = b"%PDF-1.4 fake" | ||
|
|
||
| with patch(_PATCH_TARGET, new_callable=AsyncMock, return_value=mgr): | ||
| result = await _prepare_file_attachments(["f1"], "u", "s", str(tmp_path)) | ||
|
|
||
| assert result.image_blocks == [] | ||
| saved = tmp_path / "doc.pdf" | ||
| assert saved.exists() | ||
| assert saved.read_bytes() == b"%PDF-1.4 fake" | ||
| assert str(saved) in result.hint | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_mixed_images_and_files(self, tmp_path): | ||
| """Images become blocks, non-images go to disk.""" | ||
| infos = { | ||
| "id1": _FakeFileInfo("id1", "a.png", "/a.png", "image/png", 4), | ||
| "id2": _FakeFileInfo("id2", "b.pdf", "/b.pdf", "application/pdf", 4), | ||
| "id3": _FakeFileInfo("id3", "c.txt", "/c.txt", "text/plain", 4), | ||
| } | ||
| mgr = AsyncMock() | ||
| mgr.get_file_info.side_effect = lambda fid: infos[fid] | ||
| mgr.read_file_by_id.return_value = b"data" | ||
|
|
||
| with patch(_PATCH_TARGET, new_callable=AsyncMock, return_value=mgr): | ||
| result = await _prepare_file_attachments( | ||
| ["id1", "id2", "id3"], "u", "s", str(tmp_path) | ||
| ) | ||
|
|
||
| assert "3 files" in result.hint | ||
| assert "a.png" in result.hint | ||
| assert "b.pdf" in result.hint | ||
| assert "c.txt" in result.hint | ||
| # Only the image should be a vision block | ||
| assert len(result.image_blocks) == 1 | ||
| assert result.image_blocks[0]["source"]["media_type"] == "image/png" | ||
| # Non-image files should be on disk | ||
| assert (tmp_path / "b.pdf").exists() | ||
| assert (tmp_path / "c.txt").exists() | ||
| # Read tool hint should appear (has non-image files) | ||
| assert "Read tool" in result.hint | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_singular_noun(self, tmp_path): | ||
| info = _FakeFileInfo("x", "only.txt", "/only.txt", "text/plain", 2) | ||
| mgr = AsyncMock() | ||
| mgr.get_file_info.return_value = info | ||
| mgr.read_file_by_id.return_value = b"hi" | ||
|
|
||
| with patch(_PATCH_TARGET, new_callable=AsyncMock, return_value=mgr): | ||
| result = await _prepare_file_attachments(["x"], "u", "s", str(tmp_path)) | ||
|
|
||
| assert "1 file." in result.hint | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_missing_file_skipped(self, tmp_path): | ||
| mgr = AsyncMock() | ||
| mgr.get_file_info.return_value = None | ||
|
|
||
| with patch(_PATCH_TARGET, new_callable=AsyncMock, return_value=mgr): | ||
| result = await _prepare_file_attachments( | ||
| ["missing-id"], "u", "s", str(tmp_path) | ||
| ) | ||
|
|
||
| assert result.hint == "" | ||
| assert result.image_blocks == [] | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_image_only_no_read_hint(self, tmp_path): | ||
| """When all files are images, no Read tool hint should appear.""" | ||
| info = _FakeFileInfo("i1", "cat.png", "/cat.png", "image/png", 4) | ||
| mgr = AsyncMock() | ||
| mgr.get_file_info.return_value = info | ||
| mgr.read_file_by_id.return_value = b"data" | ||
|
|
||
| with patch(_PATCH_TARGET, new_callable=AsyncMock, return_value=mgr): | ||
| result = await _prepare_file_attachments(["i1"], "u", "s", str(tmp_path)) | ||
|
|
||
| assert "Read tool" not in result.hint | ||
| assert len(result.image_blocks) == 1 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.