Skip to content

The agent that grows with you

feat(relay): stamp slack unfurl_links/unfurl_media onto outbound frame metadata (gateway-directed)

Victor Kyriazakoscc2fb4627510
diff --git a/gateway/relay/adapter.py b/gateway/relay/adapter.py
index c7ea8854362..23b7371fb32 100644
--- a/gateway/relay/adapter.py
+++ b/gateway/relay/adapter.py
@@ -1080,6 +1080,28 @@ class RelayAdapter(BasePlatformAdapter):
except Exception: # noqa: BLE001 - config shape is operator-owned
return True
+ def _slack_unfurl_hints(self, platform: Optional[str]) -> Optional[Dict[str, bool]]:
+ """Slack-only outbound link-preview suppression, relay-namespaced.
+
+ Mirrors the native SlackAdapter's unfurl controls
+ (``platforms.slack.extra.unfurl_links`` / ``unfurl_media``) but reads
+ the relay namespace (``platforms.relay.extra.slack.*``) per the
+ ``reply_in_thread`` seam: relay-fronted Slack reads its subset here;
+ the native ``platforms.slack`` block keeps meaning native-adapter
+ settings. Only explicitly-configured booleans are returned — omitted
+ keys preserve Slack's default unfurling. Non-Slack platforms return
+ None so the metadata is never polluted for other fronted platforms.
+ """
+ if str(platform or "").lower() != Platform.SLACK.value:
+ return None
+ extra = self._relay_slack_extra()
+ hints: Dict[str, bool] = {}
+ for knob in ("unfurl_links", "unfurl_media"):
+ val = extra.get(knob)
+ if isinstance(val, bool):
+ hints[knob] = val
+ return hints or None
+
def _stamp_slack_session_thread(self, event) -> None:
"""Native session-keying parity for fronted Slack DMs.
@@ -1734,6 +1756,9 @@ class RelayAdapter(BasePlatformAdapter):
)
if self._transport is None:
return SendResult(success=False, error="no transport")
+ _sfp_unfurl = self._slack_unfurl_hints(str(platform_value))
+ if _sfp_unfurl:
+ _sfp_metadata.update(_sfp_unfurl)
result = await self._transport.send_outbound(
{
"op": "send",
@@ -1917,6 +1942,9 @@ class RelayAdapter(BasePlatformAdapter):
effective_reply_to = self._apply_slack_thread_anchor(
chat_id, reply_to, send_metadata
)
+ _unfurl = self._slack_unfurl_hints(self._platform_by_chat.get(str(chat_id)))
+ if _unfurl:
+ send_metadata.update(_unfurl)
result = await self._transport.send_outbound(
{
"op": "send",
@@ -2413,6 +2441,9 @@ class RelayAdapter(BasePlatformAdapter):
effective_reply_to = self._apply_slack_thread_anchor(
chat_id, reply_to, media_metadata
)
+ _media_unfurl = self._slack_unfurl_hints(self._platform_by_chat.get(str(chat_id)))
+ if _media_unfurl:
+ media_metadata.update(_media_unfurl)
action: Dict[str, Any] = {
"op": "send_media",
"chat_id": chat_id,
diff --git a/tests/gateway/relay/test_relay_slack_unfurl.py b/tests/gateway/relay/test_relay_slack_unfurl.py
new file mode 100644
index 00000000000..c36bb3e6795
--- /dev/null
+++ b/tests/gateway/relay/test_relay_slack_unfurl.py
@@ -0,0 +1,157 @@
+"""Relay-side Slack unfurl suppression: gateway-directed metadata stamping.
+
+The gateway resolves ``platforms.relay.extra.slack.unfurl_links`` /
+``unfurl_media`` and stamps them onto the outbound frame metadata; the
+connector just forwards whatever the gateway resolved (no connector config).
+Contract under test:
+- Slack chats: explicit booleans are stamped; omitted keys are absent.
+- Non-Slack chats: never stamped (metadata not polluted cross-platform).
+- Non-boolean values (hostile/hand-edited config) are dropped.
+- The scheduled/cron lane (send_for_platform) stamps too.
+"""
+
+import pytest
+
+from gateway.config import Platform, PlatformConfig
+from gateway.relay.adapter import RelayAdapter
+from gateway.relay.descriptor import CONTRACT_VERSION, CapabilityDescriptor
+
+
+def make_desc(**kw) -> CapabilityDescriptor:
+ base = dict(
+ contract_version=CONTRACT_VERSION,
+ platform="slack",
+ label="Slack",
+ max_message_length=39000,
+ supports_draft_streaming=False,
+ supports_edit=True,
+ supports_threads=True,
+ markdown_dialect="slack",
+ len_unit="char",
+ emoji="\U0001f4bc",
+ platform_hint="",
+ pii_safe=False,
+ )
+ base.update(kw)
+ return CapabilityDescriptor(**base)
+
+
+class _CaptureTransport:
+ def __init__(self):
+ self.sent = None
+ self.sent_platform = None
+ # Advertise a slack identity so fronts_platform() passes for the
+ # send_for_platform (cron) lane.
+ self._identities = [("slack", None)]
+
+ def set_inbound_handler(self, h): # noqa: D401
+ self._h = h
+
+ async def send_outbound(self, action, *, platform=None):
+ self.sent = action
+ self.sent_platform = platform
+ return {"success": True, "message_id": "m1"}
+
+
+def _slack_adapter(extra):
+ a = RelayAdapter(
+ PlatformConfig(extra=extra), make_desc(platform="slack"), transport=_CaptureTransport()
+ )
+ return a
+
+
+def _mark_slack_chat(a, chat_id="chan-1"):
+ from gateway.platforms.base import MessageEvent, MessageType
+ from gateway.session import SessionSource
+
+ src = SessionSource(
+ platform=Platform.SLACK, chat_id=chat_id, chat_type="channel", scope_id="w-1"
+ )
+ ev = MessageEvent(text="hi", source=src, message_type=MessageType.TEXT)
+ a._capture_scope(ev)
+
+
+class TestUnfurlHints:
+ def test_non_slack_returns_none(self):
+ a = _slack_adapter({"slack": {"unfurl_links": False}})
+ assert a._slack_unfurl_hints("discord") is None
+ assert a._slack_unfurl_hints(None) is None
+
+ def test_slack_explicit_bools_returned(self):
+ a = _slack_adapter({"slack": {"unfurl_links": False, "unfurl_media": False}})
+ assert a._slack_unfurl_hints("slack") == {
+ "unfurl_links": False,
+ "unfurl_media": False,
+ }
+
+ def test_omitted_keys_return_none(self):
+ a = _slack_adapter({"slack": {}})
+ assert a._slack_unfurl_hints("slack") is None
+
+ def test_non_bool_values_dropped(self):
+ a = _slack_adapter({"slack": {"unfurl_links": "false", "unfurl_media": 0}})
+ assert a._slack_unfurl_hints("slack") is None
+
+ def test_flat_legacy_key_fallback(self):
+ # _relay_slack_extra falls back to the flat extra when no "slack"
+ # object exists (legacy staging configs).
+ a = _slack_adapter({"unfurl_links": False})
+ assert a._slack_unfurl_hints("slack") == {"unfurl_links": False}
+
+
+class TestSendStampsUnfurl:
+ @pytest.mark.asyncio
+ async def test_send_stamps_explicit_bools(self):
+ a = _slack_adapter({"slack": {"unfurl_links": False, "unfurl_media": False}})
+ _mark_slack_chat(a)
+ await a.send("chan-1", "see https://example.com")
+ assert a._transport.sent["metadata"]["unfurl_links"] is False
+ assert a._transport.sent["metadata"]["unfurl_media"] is False
+
+ @pytest.mark.asyncio
+ async def test_send_omits_when_unconfigured(self):
+ a = _slack_adapter({"slack": {}})
+ _mark_slack_chat(a)
+ await a.send("chan-1", "plain text")
+ assert "unfurl_links" not in a._transport.sent["metadata"]
+ assert "unfurl_media" not in a._transport.sent["metadata"]
+
+ @pytest.mark.asyncio
+ async def test_non_slack_chat_never_stamped(self):
+ a = _slack_adapter({"slack": {"unfurl_links": False}})
+ # A chat mapped to discord, not slack, must not carry the hint.
+ from gateway.platforms.base import MessageEvent, MessageType
+ from gateway.session import SessionSource
+
+ src = SessionSource(
+ platform=Platform.DISCORD,
+ chat_id="chan-1",
+ chat_type="channel",
+ scope_id="w-1",
+ )
+ a._capture_scope(
+ MessageEvent(text="hi", source=src, message_type=MessageType.TEXT)
+ )
+ await a.send("chan-1", "see https://example.com")
+ assert "unfurl_links" not in a._transport.sent["metadata"]
+
+
+class TestSendForPlatformStampsUnfurl:
+ @pytest.mark.asyncio
+ async def test_cron_lane_stamps_explicit_bools(self):
+ a = _slack_adapter({"slack": {"unfurl_links": False, "unfurl_media": False}})
+ from gateway.config import Platform as P
+
+ res = await a.send_for_platform(P.SLACK, "C123", "brief https://x.dev")
+ assert res.success is True
+ assert a._transport.sent["metadata"]["unfurl_links"] is False
+ assert a._transport.sent["metadata"]["unfurl_media"] is False
+
+ @pytest.mark.asyncio
+ async def test_cron_lane_omits_when_unconfigured(self):
+ a = _slack_adapter({"slack": {}})
+ from gateway.config import Platform as P
+
+ await a.send_for_platform(P.SLACK, "C123", "brief")
+ assert "unfurl_links" not in a._transport.sent["metadata"]
+ assert "unfurl_media" not in a._transport.sent["metadata"]