The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): keep plain audio MEDIA out of WhatsApp/BlueBubbles voice notes
Teknium183cd6cc35fb
diff --git a/plugins/platforms/bluebubbles/adapter.py b/plugins/platforms/bluebubbles/adapter.pyindex eb6c8987bd6..628ee8e379f 100644--- a/plugins/platforms/bluebubbles/adapter.py+++ b/plugins/platforms/bluebubbles/adapter.py@@ -428,8 +428,10 @@ class BlueBubblesAdapter(BasePlatformAdapter):async def send_image_file(self, chat_id, image_path, caption=None, reply_to=None, **kw) -> SendResult:return await self._send_attachment(chat_id, image_path, caption=caption)- async def send_voice(self, chat_id, audio_path, caption=None, reply_to=None, **kw) -> SendResult:- return await self._send_attachment(chat_id, audio_path, caption=caption, is_audio_message=True)+ async def send_voice(self, chat_id, audio_path, caption=None, reply_to=None, is_voice=None, **kw) -> SendResult:+ # is_voice=False is a plain audio MEDIA file (music, a sound effect): a normal attachment,+ # never an iMessage voice memo.+ return await self._send_attachment(chat_id, audio_path, caption=caption, is_audio_message=is_voice is not False)async def send_video(self, chat_id, video_path, caption=None, reply_to=None, **kw) -> SendResult:return await self._send_attachment(chat_id, video_path, caption=caption)diff --git a/plugins/platforms/whatsapp/adapter.py b/plugins/platforms/whatsapp/adapter.pyindex f40f8e6375d..04177d1f063 100644--- a/plugins/platforms/whatsapp/adapter.py+++ b/plugins/platforms/whatsapp/adapter.py@@ -873,7 +873,12 @@ class WhatsAppAdapter(WhatsAppBehaviorMixin, BasePlatformAdapter):async def send_video(self, chat_id: str, video_path: str, caption: Optional[str] = None, reply_to: Optional[str] = None, **kwargs) -> SendResult:return await self._send_media_to_bridge(chat_id, video_path, "video", caption)- async def send_voice(self, chat_id: str, audio_path: str, caption: Optional[str] = None, reply_to: Optional[str] = None, **kwargs) -> SendResult:+ async def send_voice(self, chat_id: str, audio_path: str, caption: Optional[str] = None, reply_to: Optional[str] = None,+ is_voice: Optional[bool] = None, **kwargs) -> SendResult:+ # The bridge turns every "audio" send into a voice note (ptt, transcoded to Opus); a plain+ # audio MEDIA file (is_voice=False: music, a sound effect) goes as a named document instead.+ if is_voice is False:+ return await self.send_document(chat_id, audio_path, caption=caption, reply_to=reply_to)return await self._send_media_to_bridge(chat_id, audio_path, "audio", caption)async def send_document(self, chat_id: str, file_path: str, caption: Optional[str] = None, file_name: Optional[str] = None,diff --git a/plugins/platforms/whatsapp_cloud/adapter.py b/plugins/platforms/whatsapp_cloud/adapter.pyindex ce81898f130..0bc2b619635 100644--- a/plugins/platforms/whatsapp_cloud/adapter.py+++ b/plugins/platforms/whatsapp_cloud/adapter.py@@ -612,11 +612,16 @@ class WhatsAppCloudAdapter(WhatsAppBehaviorMixin, BasePlatformAdapter):chat_id, file_path, "document", caption=caption, filename=file_name or os.path.basename(file_path), reply_to=reply_to,)- async def send_voice(self, chat_id: str, audio_path: str, caption: Optional[str] = None, reply_to: Optional[str] = None, **kwargs) -> SendResult:+ async def send_voice(self, chat_id: str, audio_path: str, caption: Optional[str] = None, reply_to: Optional[str] = None,+ is_voice: Optional[bool] = None, **kwargs) -> SendResult:"""Voice message: ``audio/ogg; codecs=opus`` renders as a voice bubble, so a- local MP3 (Hermes TTS output) is converted via ffmpeg first; other audio is sent as-is."""+ local MP3 (Hermes TTS output) is converted via ffmpeg first; other audio is sent as-is.+ A plain audio MEDIA file (``is_voice=False``) is never converted into a voice bubble."""mime_type: Optional[str] = None- if not audio_path.startswith(_HTTP_PREFIXES) and audio_path.lower().endswith(".mp3") and os.path.exists(audio_path):+ if is_voice is False and audio_path.lower().endswith(".mp3"):+ mime_type = "audio/mpeg"+ elif (not audio_path.startswith(_HTTP_PREFIXES) and audio_path.lower().endswith(".mp3")+ and await asyncio.to_thread(os.path.exists, audio_path)):opus_path = await self._convert_to_opus(audio_path)if opus_path:try:diff --git a/tests/gateway/test_plain_audio_not_voice_note.py b/tests/gateway/test_plain_audio_not_voice_note.pynew file mode 100644index 00000000000..4321821b6c3--- /dev/null+++ b/tests/gateway/test_plain_audio_not_voice_note.py@@ -0,0 +1,68 @@+"""A plain audio MEDIA file (``is_voice=False``: music, a sound effect) is never sent as a voice note.++The media dispatch routes every audio extension to ``send_voice(..., is_voice=<bool>)``; adapters+whose ``send_voice`` is a native voice-note transport must honour the flag. Each case records the+adapter's own wire boundary.+"""+from types import SimpleNamespace+from unittest.mock import AsyncMock, MagicMock++import pytest++from gateway.config import PlatformConfig++[email protected]("is_voice, expected", [(False, "document"), (None, "audio")], ids=["plain", "voice"])+async def test_whatsapp_bridge_sends_plain_audio_as_a_document(tmp_path, is_voice, expected):+ from plugins.platforms.whatsapp.adapter import WhatsAppAdapter++ adapter = WhatsAppAdapter(PlatformConfig(enabled=True, extra={"session_path": str(tmp_path / "wa")}))+ posted = []++ async def post_bridge_message(route, payload, timeout=0):+ posted.append(payload)+ return SimpleNamespace(success=True, message_id=None)++ adapter._post_bridge_message = post_bridge_message+ adapter._bridge_unavailable = AsyncMock(return_value=None)+ path = tmp_path / "door_knock.mp3"+ path.write_bytes(b"ID3\x04" + b"\x00" * 32)++ assert (await adapter.send_voice("15551234567", str(path), is_voice=is_voice)).success+ assert [p["mediaType"] for p in posted] == [expected]++[email protected]("is_voice, audio_message", [(False, False), (None, True)], ids=["plain", "voice"])+async def test_bluebubbles_sends_plain_audio_without_the_voice_memo_flag(tmp_path, monkeypatch, is_voice, audio_message):+ from plugins.platforms.bluebubbles.adapter import BlueBubblesAdapter++ adapter = BlueBubblesAdapter(PlatformConfig(enabled=True, extra={"server_url": "http://localhost:1234", "password": "x"}))+ response = MagicMock()+ response.json.return_value = {"status": 200, "data": {"guid": "g1"}}+ adapter.client = SimpleNamespace(post=AsyncMock(return_value=response))+ monkeypatch.setattr(adapter, "_resolve_chat_guid", AsyncMock(return_value="iMessage;-;+15551234567"))+ path = tmp_path / "door_knock.mp3"+ path.write_bytes(b"ID3\x04" + b"\x00" * 32)++ assert (await adapter.send_voice("+15551234567", str(path), is_voice=is_voice)).success+ data = adapter.client.post.await_args.kwargs["data"]+ assert ("isAudioMessage" in data) is audio_message+ assert data["name"] == "door_knock.mp3"+++async def test_whatsapp_cloud_never_converts_plain_mp3_into_a_voice_bubble(tmp_path):+ from plugins.platforms.whatsapp_cloud.adapter import WhatsAppCloudAdapter++ adapter = WhatsAppCloudAdapter.__new__(WhatsAppCloudAdapter)+ adapter._convert_to_opus = AsyncMock(side_effect=AssertionError("plain audio must not become Opus"))+ adapter._send_media_from_path_or_link = AsyncMock(return_value=SimpleNamespace(success=True))+ path = tmp_path / "door_knock.mp3"+ path.write_bytes(b"ID3\x04" + b"\x00" * 32)++ assert (await adapter.send_voice("15551234567", str(path), is_voice=False)).success+ call = adapter._send_media_from_path_or_link.await_args+ assert call.args[1] == str(path)+ assert call.kwargs["mime_type"] == "audio/mpeg"