The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(buzz): preserve inbound media captions
joelbrilliant55136adcc4f7
diff --git a/plugins/platforms/buzz/adapter.py b/plugins/platforms/buzz/adapter.pyindex 86c891bbc6d..cdf14ff3c2e 100644--- a/plugins/platforms/buzz/adapter.py+++ b/plugins/platforms/buzz/adapter.py@@ -269,7 +269,7 @@ _MEDIA_URL_PATTERN = (r"[0-9a-f]{64}(?:\.[a-z0-9]{1,10})?(?:\?[^\s<>\[\]()]*)?")_MARKDOWN_MEDIA_RE = re.compile(- rf"!\[[^\]]*\]\(\s*(?P<url>{_MEDIA_URL_PATTERN})"+ rf"!\[(?P<alt>[^\]]*)\]\(\s*(?P<url>{_MEDIA_URL_PATTERN})"r"(?:\s+[\"'][^\"']*[\"'])?\s*\)",re.IGNORECASE,)@@ -310,10 +310,10 @@ def _is_relay_media_url(url: str, relay_url: str) -> bool:def _find_relay_media_refs(text: str, relay_url: str-) -> Tuple[List[str], List[Tuple[int, int]]]:- """Find same-relay media URLs and the content spans that contain them."""+) -> Tuple[List[str], List[Tuple[int, int, str]]]:+ """Find same-relay media URLs and their safe text replacements."""urls: List[str] = []- spans: List[Tuple[int, int]] = []+ replacements: List[Tuple[int, int, str]] = []markdown_spans: List[Tuple[int, int]] = []for match in _MARKDOWN_MEDIA_RE.finditer(text):@@ -321,7 +321,7 @@ def _find_relay_media_refs(if not _is_relay_media_url(url, relay_url):continuemarkdown_spans.append(match.span())- spans.append(match.span())+ replacements.append((*match.span(), match.group("alt").strip()))if url not in urls:urls.append(url)@@ -334,18 +334,17 @@ def _find_relay_media_refs(url = match.group(0)if not _is_relay_media_url(url, relay_url):continue- spans.append(match.span())+ replacements.append((*match.span(), ""))if url not in urls:urls.append(url)- return urls, spans+ return urls, replacements-def _remove_media_spans(text: str, spans: List[Tuple[int, int]]) -> str:- chars = list(text)- for start, end in sorted(spans, reverse=True):- del chars[start:end]- cleaned = "".join(chars)+def _replace_media_refs(text: str, replacements: List[Tuple[int, int, str]]) -> str:+ cleaned = text+ for start, end, replacement in sorted(replacements, reverse=True):+ cleaned = f"{cleaned[:start]}{replacement}{cleaned[end:]}"cleaned = re.sub(r"[ \t]+\n", "\n", cleaned)cleaned = re.sub(r"\n{3,}", "\n\n", cleaned)return cleaned.strip()@@ -2369,11 +2368,11 @@ class BuzzAdapter(BasePlatformAdapter):Each object is independent: one failed download is logged and skippedwithout discarding the caption or any other successfully cached files."""- urls, spans = _find_relay_media_refs(text, self.relay_url)+ urls, replacements = _find_relay_media_refs(text, self.relay_url)if not urls:return text, [], [], MessageType.TEXT- cleaned_text = _remove_media_spans(text, spans)+ cleaned_text = _replace_media_refs(text, replacements)media_urls: List[str] = []media_types: List[str] = []media_kinds: List[str] = []diff --git a/tests/gateway/test_buzz_adapter.py b/tests/gateway/test_buzz_adapter.pyindex 8781a7ac123..554ff3c3d73 100644--- a/tests/gateway/test_buzz_adapter.py+++ b/tests/gateway/test_buzz_adapter.py@@ -1602,7 +1602,7 @@ class TestInboundMediaLocalisation:return captured, cli_calls@pytest.mark.asyncio- async def test_markdown_relay_image_is_localised_before_dispatch(+ async def test_markdown_relay_image_preserves_alt_text_before_dispatch(self, monkeypatch, tmp_path):monkeypatch.setenv("HERMES_HOME", str(tmp_path / "hermes"))@@ -1611,7 +1611,7 @@ class TestInboundMediaLocalisation:media_url = f"https://test.relay/media/{'a' * 64}.png"await adapter._dispatch_message(- text=f"Please inspect this screenshot\n\n",+ text=f"Please inspect this screenshot\n\n",chat_id=CHANNEL,chat_type="dm",user_id=OTHER_PUBKEY,@@ -1622,7 +1622,7 @@ class TestInboundMediaLocalisation:assert len(captured) == 1event = captured[0]- assert event.text == "Please inspect this screenshot"+ assert event.text == "Please inspect this screenshot\n\nLogin error dialog"assert event.message_type == MessageType.PHOTOassert event.media_types == ["image/png"]assert len(event.media_urls) == 1@@ -1726,7 +1726,7 @@ class TestInboundMediaLocalisation:assert "/cache/documents/" in event.media_urls[0]@pytest.mark.asyncio- async def test_download_failure_keeps_caption_and_dispatches_text(+ async def test_download_failure_preserves_caption_and_alt_text(self, monkeypatch, tmp_path):monkeypatch.setenv("HERMES_HOME", str(tmp_path / "hermes"))@@ -1735,7 +1735,7 @@ class TestInboundMediaLocalisation:captured, _calls = self._capture_dispatch(adapter, failed_urls=[media_url])await adapter._dispatch_message(- text=f"The error is visible here\n",+ text=f"The error is visible here\n",chat_id=CHANNEL,chat_type="dm",user_id=OTHER_PUBKEY,@@ -1745,7 +1745,7 @@ class TestInboundMediaLocalisation:)event = captured[0]- assert event.text == "The error is visible here"+ assert event.text == "The error is visible here\nCheckout error dialog"assert event.message_type == MessageType.TEXTassert event.media_urls == []assert event.media_types == []