fix(discord): suppress link embeds in tool preview markdown links

Wrap the masked-link destination in angle brackets so Discord does not
unfurl an OG-preview embed under every tool progress bubble. quote()
percent-encodes any <> inside the URL itself, so the wrapper cannot be
broken out of.
This commit is contained in:
Teknium
2026-08-02 21:37:18 -07:00
parent e599f100ec
commit dd600d1ace
4 changed files with 10 additions and 6 deletions
+1
View File
@@ -0,0 +1 @@
coffee-the-dev
+4 -1
View File
@@ -46,11 +46,14 @@ def _format_discord_markdown_link(label: str, url: str) -> str:
URL label can therefore win over the Markdown destination and remain a
broken link. Dropping only the scheme keeps the preview recognizable while
leaving one unambiguous click target.
The destination is wrapped in angle brackets (``<url>``) so Discord does
not unfurl an OG-preview embed under every tool progress bubble.
"""
label = _DISCORD_URL_LABEL_SCHEME_RE.sub("", label, count=1)
escaped_label = _DISCORD_MARKDOWN_LINK_LABEL_RE.sub(r"\\\1", label)
escaped_url = quote(url, safe=":/?#[]@!$&'*+,;=%")
return f"[{escaped_label}]({escaped_url})"
return f"[{escaped_label}](<{escaped_url}>)"
class _Snowflake:
+4 -4
View File
@@ -54,7 +54,7 @@ class TestDiscordToolPreviewFormatting:
out = adapter.format_tool_preview(ToolPreview(visible, truncated=True, url=url))
assert out == f"[hermes-agent.nousresearch...]({url})"
assert out == f"[hermes-agent.nousresearch...](<{url}>)"
def test_truncated_url_label_is_not_a_second_url_target(self):
from agent.display import ToolPreview
@@ -67,7 +67,7 @@ class TestDiscordToolPreviewFormatting:
assert out == (
"[centaur.run/secrets/advanced-...]"
"(https://centaur.run/secrets/advanced-permissioning)"
"(<https://centaur.run/secrets/advanced-permissioning>)"
)
def test_link_escapes_discord_markdown_delimiters(self):
@@ -82,7 +82,7 @@ class TestDiscordToolPreviewFormatting:
assert adapter.format_tool_preview(preview) == (
r"[example.com/docs/\[beta\]...]"
r"(https://example.com/docs/_%28beta%29)"
r"(<https://example.com/docs/_%28beta%29>)"
)
def test_structured_tool_event_uses_clickable_truncated_url(self):
@@ -99,7 +99,7 @@ class TestDiscordToolPreviewFormatting:
)
assert out is not None
assert f"[{visible.removeprefix('https://')}]({url})" in out
assert f"[{visible.removeprefix('https://')}](<{url}>)" in out
def test_untruncated_url_remains_plain(self):
from agent.display import ToolPreview
+1 -1
View File
@@ -577,7 +577,7 @@ def test_discord_truncated_tool_url_links_to_full_destination(monkeypatch, tmp_p
assert adapter.sent
visible = UrlPreviewAgent.URL[:37] + "..."
label = visible.removeprefix("https://")
assert f"[{label}]({UrlPreviewAgent.URL})" in adapter.sent[0]["content"]
assert f"[{label}](<{UrlPreviewAgent.URL}>)" in adapter.sent[0]["content"]
class CommentaryAgent: