-
Notifications
You must be signed in to change notification settings - Fork 53.7k
fix(gateway): deliver media before queued follow-ups #46223
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
Closed
Qwinty
wants to merge
2
commits into
NousResearch:main
from
Qwinty:fix/queued-followup-media-delivery
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| from types import SimpleNamespace | ||
| from unittest.mock import AsyncMock | ||
|
|
||
| import pytest | ||
|
|
||
| from gateway.config import Platform | ||
| from gateway.platforms.base import BasePlatformAdapter, MessageEvent | ||
| from gateway.run import GatewayRunner | ||
| from gateway.session import SessionSource | ||
|
|
||
|
|
||
| def _runner() -> GatewayRunner: | ||
| runner = object.__new__(GatewayRunner) | ||
| runner._reply_anchor_for_event = lambda event: event.message_id | ||
| runner._thread_metadata_for_source = lambda source, _reply=None: ( | ||
| {"thread_id": source.thread_id} if source.thread_id else None | ||
| ) | ||
| return runner | ||
|
|
||
|
|
||
| def _event() -> MessageEvent: | ||
| return MessageEvent( | ||
| text="next", | ||
| source=SessionSource( | ||
| platform=Platform.TELEGRAM, | ||
| chat_id="273403055", | ||
| chat_type="dm", | ||
| user_id="273403055", | ||
| thread_id="374476", | ||
| ), | ||
| message_id="25741", | ||
| ) | ||
|
|
||
|
|
||
| def _adapter(): | ||
| return SimpleNamespace( | ||
| name="Telegram", | ||
| extract_media=BasePlatformAdapter.extract_media, | ||
| extract_images=lambda text: ([], text), | ||
| extract_local_files=BasePlatformAdapter.extract_local_files, | ||
| send=AsyncMock(), | ||
| send_voice=AsyncMock(), | ||
| send_video=AsyncMock(), | ||
| send_document=AsyncMock(), | ||
| send_multiple_images=AsyncMock(), | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_queued_first_response_strips_media_and_sends_document(tmp_path): | ||
| html = tmp_path / "pivin-scenario.html" | ||
| html.write_text("<!doctype html><title>Pivin</title>", encoding="utf-8") | ||
|
|
||
| adapter = _adapter() | ||
| response = f"Done.\n\nMEDIA:{html}" | ||
|
|
||
| await _runner()._send_queued_first_response( | ||
| response, | ||
| _event(), | ||
| adapter, | ||
| metadata={"thread_id": "374476"}, | ||
| ) | ||
|
|
||
| adapter.send.assert_awaited_once_with( | ||
| "273403055", | ||
| "Done.", | ||
| metadata={"thread_id": "374476"}, | ||
| ) | ||
| adapter.send_document.assert_awaited_once() | ||
| assert adapter.send_document.await_args.kwargs["chat_id"] == "273403055" | ||
| assert adapter.send_document.await_args.kwargs["file_path"] == str(html) | ||
| assert adapter.send_document.await_args.kwargs["metadata"] == {"thread_id": "374476"} | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_queued_first_response_all_media_sends_no_empty_text(tmp_path): | ||
| html = tmp_path / "handoff.html" | ||
| html.write_text("<!doctype html><title>Only media</title>", encoding="utf-8") | ||
|
|
||
| adapter = _adapter() | ||
|
|
||
| await _runner()._send_queued_first_response( | ||
| f"MEDIA:{html}", | ||
| _event(), | ||
| adapter, | ||
| metadata={"thread_id": "374476"}, | ||
| ) | ||
|
|
||
| adapter.send.assert_not_awaited() | ||
| adapter.send_document.assert_awaited_once() | ||
| assert adapter.send_document.await_args.kwargs["file_path"] == str(html) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_queued_first_response_preserves_remote_image_delivery(): | ||
| adapter = _adapter() | ||
| adapter.extract_images = BasePlatformAdapter.extract_images | ||
|
|
||
| await _runner()._send_queued_first_response( | ||
| "Chart ready.\n\n", | ||
| _event(), | ||
| adapter, | ||
| metadata={"thread_id": "374476"}, | ||
| ) | ||
|
|
||
| adapter.send.assert_awaited_once_with( | ||
| "273403055", | ||
| "Chart ready.", | ||
| metadata={"thread_id": "374476"}, | ||
| ) | ||
| adapter.send_multiple_images.assert_awaited_once_with( | ||
| chat_id="273403055", | ||
| images=[("https://example.com/usage.png", "usage chart")], | ||
| metadata={"thread_id": "374476"}, | ||
| ) |
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.