From 81847c89ef03ba8021a065afdf6050dd75d8e852 Mon Sep 17 00:00:00 2001 From: panxin5 Date: Tue, 1 Dec 2026 13:48:41 +0800 Subject: [PATCH] fix(wecom): send MEDIA attachments when no live gateway is routed Without a running gateway, core routes a media send to ``standalone_sender_fn`` with ``media_files`` / ``force_document``. ``_send_via`` called only ``adapter.send``, so the text landed and the attachment was dropped while the send reported success. Send the text, then each file through its native adapter method (document when forced) and surface an upload failure instead of dropping it. Relates to NousResearch/hermes-agent#153045. --- wecom/adapter.py | 36 +++++-- wecom/tests/test_wecom_standalone_media.py | 119 +++++++++++++++++++++ 2 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 wecom/tests/test_wecom_standalone_media.py diff --git a/wecom/adapter.py b/wecom/adapter.py index 3cf3195..8b9dc42 100644 --- a/wecom/adapter.py +++ b/wecom/adapter.py @@ -715,14 +715,36 @@ def _fail(log_msg: str, detail: Any, shown: Any) -> None: return None -async def _send_via(adapter, chat_id, message, *, live: bool): +async def _send_via(adapter, chat_id, message, *, live: bool, media_files=None, force_document=False): + """Send the text, then each MEDIA attachment through its native adapter method. + + Without a running gateway, core routes a media send to ``standalone_sender_fn`` with + ``media_files`` / ``force_document``; calling only ``adapter.send`` reported success while the + attachment was dropped (#153045).""" + label = "WeCom live adapter" if live else "WeCom" try: - result = await adapter.send(chat_id, message) + result = None + if (message or "").strip(): + result = await adapter.send(chat_id, message) + if not result.success: + return send_error(f"{label} send failed: {result.error}") + if not media_files: + return {"success": True, "platform": "wecom", "chat_id": chat_id, + "message_id": result.message_id} + from gateway.outbound.senders import _adapter_media_method + for media_path, is_voice in media_files or (): + ext = os.path.splitext(str(media_path))[1].lower() + method, _kind = _adapter_media_method(ext, bool(is_voice), force_document=force_document) + result = await getattr(adapter, method)(chat_id, media_path) + if not result.success: + return send_error(f"{label} media send failed: {result.error}") except Exception as e: - return send_error(f"WeCom{' live adapter' if live else ''} send failed: {describe_proxy_error(e, env_proxy_for_redaction(adapter._ws_url, trust_env=gateway_trust_env())) or e}") + return send_error(f"{label} send failed: {describe_proxy_error(e, env_proxy_for_redaction(adapter._ws_url, trust_env=gateway_trust_env())) or e}") + if result is None: + return send_error(f"{label} send failed: no deliverable text or media") if result.success: return {"success": True, "platform": "wecom", "chat_id": chat_id, "message_id": result.message_id} - return send_error(f"WeCom send failed: {result.error}") + return send_error(f"{label} send failed: {result.error}") async def _standalone_send(pconfig, chat_id, message, *, thread_id=None, media_files=None, force_document=False): @@ -738,7 +760,8 @@ async def _standalone_send(pconfig, chat_id, message, *, thread_id=None, media_f except Exception: adapter = None if adapter is not None: - return await _send_via(adapter, chat_id, message, live=True) + return await _send_via(adapter, chat_id, message, live=True, + media_files=media_files, force_document=force_document) if not check_wecom_requirements(): return send_error("WeCom requirements not met. Need aiohttp + WECOM_BOT_ID/SECRET.") try: @@ -746,7 +769,8 @@ async def _standalone_send(pconfig, chat_id, message, *, thread_id=None, media_f if not await adapter.connect(): return send_error(f"WeCom: failed to connect - {getattr(adapter, 'fatal_error_message', None) or 'unknown error'}") try: - return await _send_via(adapter, chat_id, message, live=False) + return await _send_via(adapter, chat_id, message, live=False, + media_files=media_files, force_document=force_document) finally: await adapter.disconnect() except Exception as e: diff --git a/wecom/tests/test_wecom_standalone_media.py b/wecom/tests/test_wecom_standalone_media.py new file mode 100644 index 0000000..7866cee --- /dev/null +++ b/wecom/tests/test_wecom_standalone_media.py @@ -0,0 +1,119 @@ +"""Standalone MEDIA delivery for WeCom (#153045). + +Core routes a media send with no live gateway to ``standalone_sender_fn`` with ``media_files`` / +``force_document``. ``_send_via`` used to call only ``adapter.send``, so the text landed and the +attachment was dropped while the send reported success. +""" + +from __future__ import annotations + +from unittest.mock import AsyncMock + +import pytest + +from gateway.platforms.base import SendResult +from wecom_plugin import adapter as wecom + + +class _FakeAdapter: + _ws_url = "wss://wecom.invalid" + + def __init__(self, fail_method=None): + self.calls = [] + self.fail_method = fail_method + + async def send(self, chat_id, content, **kwargs): + return self._record("send", chat_id, content) + + async def send_image_file(self, chat_id, path, **kwargs): + return self._record("send_image_file", chat_id, path) + + async def send_document(self, chat_id, path, **kwargs): + return self._record("send_document", chat_id, path) + + async def send_voice(self, chat_id, path, **kwargs): + return self._record("send_voice", chat_id, path) + + async def send_video(self, chat_id, path, **kwargs): + return self._record("send_video", chat_id, path) + + def _record(self, method, chat_id, value): + self.calls.append((method, chat_id, value)) + if method == self.fail_method: + return SendResult(success=False, error="upload rejected") + return SendResult(success=True, message_id=f"m-{method}") + + +@pytest.fixture +def fake_live_adapter(monkeypatch): + def use(adapter): + from gateway.outbound import senders + + monkeypatch.setattr(senders, "_live_adapter", lambda platform, *a, **kw: (None, adapter)) + + return use + + +@pytest.mark.asyncio +async def test_an_image_rides_its_native_method(tmp_path, fake_live_adapter): + image = tmp_path / "shot.png" + image.write_bytes(b"\x89PNG\r\n\x1a\n") + fake = _FakeAdapter() + fake_live_adapter(fake) + + result = await wecom._standalone_send(None, "chat-1", "hello", media_files=[(str(image), False)]) + + assert result["success"] is True + assert fake.calls == [("send", "chat-1", "hello"), ("send_image_file", "chat-1", str(image))] + + +@pytest.mark.asyncio +async def test_force_document_sends_the_same_file_as_a_document(tmp_path, fake_live_adapter): + image = tmp_path / "shot.png" + image.write_bytes(b"\x89PNG\r\n\x1a\n") + fake = _FakeAdapter() + fake_live_adapter(fake) + + result = await wecom._standalone_send( + None, "chat-1", "hello", media_files=[(str(image), False)], force_document=True) + + assert result["success"] is True + assert [call[0] for call in fake.calls] == ["send", "send_document"] + + +@pytest.mark.asyncio +async def test_a_voice_note_sends_without_a_text_bubble(tmp_path, fake_live_adapter): + voice = tmp_path / "note.ogg" + voice.write_bytes(b"OggS") + fake = _FakeAdapter() + fake_live_adapter(fake) + + result = await wecom._standalone_send(None, "chat-1", "", media_files=[(str(voice), True)]) + + assert result["success"] is True + assert fake.calls == [("send_voice", "chat-1", str(voice))] + + +@pytest.mark.asyncio +async def test_a_failed_upload_is_reported_instead_of_dropped(tmp_path, fake_live_adapter): + image = tmp_path / "shot.png" + image.write_bytes(b"\x89PNG\r\n\x1a\n") + fake = _FakeAdapter(fail_method="send_image_file") + fake_live_adapter(fake) + + result = await wecom._standalone_send(None, "chat-1", "hello", media_files=[(str(image), False)]) + + assert "success" not in result + assert "media send failed" in result["error"] + assert "upload rejected" in result["error"] + + +@pytest.mark.asyncio +async def test_a_text_only_send_stays_unchanged(fake_live_adapter): + fake = _FakeAdapter() + fake_live_adapter(fake) + + result = await wecom._standalone_send(None, "chat-1", "hello") + + assert result["success"] is True + assert fake.calls == [("send", "chat-1", "hello")]