Skip to content

The agent that grows with you

fix(discord): avoid truncated URL link targets

Coffee☕️e599f100ec1b
diff --git a/plugins/platforms/discord/adapter.py b/plugins/platforms/discord/adapter.py
index de40dc51a48..c064716fd52 100644
--- a/plugins/platforms/discord/adapter.py
+++ b/plugins/platforms/discord/adapter.py
@@ -36,10 +36,18 @@ from agent.display import ToolPreview
logger = logging.getLogger(__name__)
_DISCORD_MARKDOWN_LINK_LABEL_RE = re.compile(r"([\\\[\]])")
+_DISCORD_URL_LABEL_SCHEME_RE = re.compile(r"^https?://", re.IGNORECASE)
def _format_discord_markdown_link(label: str, url: str) -> str:
- """Return a Discord Markdown link with escaped delimiters."""
+ """Return a Discord Markdown link whose label is not itself a URL.
+
+ Discord gives URL-shaped link labels their own link behavior. A truncated
+ 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.
+ """
+ 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})"
diff --git a/tests/gateway/test_discord_format.py b/tests/gateway/test_discord_format.py
index b63f5b24cca..56726fc2b2e 100644
--- a/tests/gateway/test_discord_format.py
+++ b/tests/gateway/test_discord_format.py
@@ -54,7 +54,21 @@ class TestDiscordToolPreviewFormatting:
out = adapter.format_tool_preview(ToolPreview(visible, truncated=True, url=url))
- assert out == f"[{visible}]({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
+
+ adapter = _make_discord_adapter()
+ url = "https://centaur.run/secrets/advanced-permissioning"
+ visible = "https://centaur.run/secrets/advanced-..."
+
+ out = adapter.format_tool_preview(ToolPreview(visible, truncated=True, url=url))
+
+ assert out == (
+ "[centaur.run/secrets/advanced-...]"
+ "(https://centaur.run/secrets/advanced-permissioning)"
+ )
def test_link_escapes_discord_markdown_delimiters(self):
from agent.display import ToolPreview
@@ -67,7 +81,7 @@ class TestDiscordToolPreviewFormatting:
)
assert adapter.format_tool_preview(preview) == (
- r"[https://example.com/docs/\[beta\]...]"
+ r"[example.com/docs/\[beta\]...]"
r"(https://example.com/docs/_%28beta%29)"
)
@@ -85,7 +99,7 @@ class TestDiscordToolPreviewFormatting:
)
assert out is not None
- assert f"[{visible}]({url})" in out
+ assert f"[{visible.removeprefix('https://')}]({url})" in out
def test_untruncated_url_remains_plain(self):
from agent.display import ToolPreview
diff --git a/tests/gateway/test_run_progress_topics.py b/tests/gateway/test_run_progress_topics.py
index f26a2d1187d..d3adf9160b8 100644
--- a/tests/gateway/test_run_progress_topics.py
+++ b/tests/gateway/test_run_progress_topics.py
@@ -576,7 +576,8 @@ def test_discord_truncated_tool_url_links_to_full_destination(monkeypatch, tmp_p
assert result["final_response"] == "done"
assert adapter.sent
visible = UrlPreviewAgent.URL[:37] + "..."
- assert f"[{visible}]({UrlPreviewAgent.URL})" in adapter.sent[0]["content"]
+ label = visible.removeprefix("https://")
+ assert f"[{label}]({UrlPreviewAgent.URL})" in adapter.sent[0]["content"]
class CommentaryAgent: