Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(slack): draw agent avatars on Slack's background, not white (#239)

Louis Amaudruz18b1bc48d873
diff --git a/core/switch_core/bridges/collaboration/slack/adapter.py b/core/switch_core/bridges/collaboration/slack/adapter.py
index d245bbf29..fcb57c8a0 100644
--- a/core/switch_core/bridges/collaboration/slack/adapter.py
+++ b/core/switch_core/bridges/collaboration/slack/adapter.py
@@ -34,6 +34,7 @@ from switch_core.bridges.collaboration.models import (
InboundUserJoin,
OutboundAttachment,
)
+from switch_core.bridges.collaboration.slack.avatar import on_slack_background
logger = logging.getLogger(__name__)
@@ -170,6 +171,12 @@ class SlackAdapter(CollaborationAdapter):
)
return None
+ async def agent_icon_url(self, agent_name: str) -> str:
+ # Overridden for Slack alone: it flattens a transparent avatar onto
+ # white. Wrapping the resolver rather than each call site keeps every
+ # place that posts as an agent on the same background.
+ return on_slack_background(await super().agent_icon_url(agent_name))
+
def slash_invite_hint(self) -> str:
# Slack passes a slash command's whole tail through as free text, so the
# invocation reads exactly like the `!` form.
diff --git a/core/switch_core/bridges/collaboration/slack/avatar.py b/core/switch_core/bridges/collaboration/slack/avatar.py
new file mode 100644
index 000000000..6299057aa
--- /dev/null
+++ b/core/switch_core/bridges/collaboration/slack/avatar.py
@@ -0,0 +1,44 @@
+"""Making an agent's avatar sit on Slack's background rather than on white.
+
+Slack composites a transparent avatar onto **white**, so a PNG with an alpha
+channel wears a bright square in an otherwise dark message list. The other
+platforms do not need this — Discord keeps the transparency and the bot sits
+directly on Discord's own surface — which is why the fix lives here and not in
+the URL the console generates or in the shared adapter base. One background
+colour cannot be right everywhere, and Slack's is not Discord's.
+
+It applies only to a DiceBear avatar, because that is the only URL whose
+background this can ask for. A link an operator supplied themselves is an opaque
+image to us: there is no parameter to add, and rewriting someone else's URL on a
+guess would be worse than leaving it alone. Such an icon keeps whatever
+background it was authored with, transparent included.
+"""
+
+from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit
+
+# Slack's resting dark surface, sampled from a real client. A hovered row
+# lightens and the edge becomes faintly visible; matching the hover state
+# instead would make it visible in the far commoner resting one.
+SLACK_SURFACE = "1a1d21"
+
+_DICEBEAR_HOST = "api.dicebear.com"
+_BACKGROUND_PARAM = "backgroundColor"
+
+
+def on_slack_background(icon_url: str) -> str:
+ """Return `icon_url` drawn on Slack's background, when that is possible.
+
+ A DiceBear URL with no background of its own gains one. Anything else —
+ another host, or a DiceBear URL whose background was chosen deliberately —
+ is returned untouched.
+ """
+ parts = urlsplit(icon_url)
+ if (parts.hostname or "").lower() != _DICEBEAR_HOST:
+ return icon_url
+
+ query = parse_qsl(parts.query, keep_blank_values=True)
+ if any(key == _BACKGROUND_PARAM for key, _ in query):
+ return icon_url
+
+ query.append((_BACKGROUND_PARAM, SLACK_SURFACE))
+ return urlunsplit(parts._replace(query=urlencode(query)))
diff --git a/core/tests/switch_core/bridges/collaboration/test_slack_avatar.py b/core/tests/switch_core/bridges/collaboration/test_slack_avatar.py
new file mode 100644
index 000000000..92ef51239
--- /dev/null
+++ b/core/tests/switch_core/bridges/collaboration/test_slack_avatar.py
@@ -0,0 +1,82 @@
+import asyncio
+from urllib.parse import parse_qs, urlsplit
+
+from switch_core.agent_icon import default_icon_url
+from switch_core.bridges.collaboration.slack.adapter import (
+ SlackAdapter,
+ SlackConnectionConfig,
+)
+from switch_core.bridges.collaboration.slack.avatar import (
+ SLACK_SURFACE,
+ on_slack_background,
+)
+
+DICEBEAR = "https://api.dicebear.com/9.x/bottts/png?seed=worker&size=256"
+
+
+def _background(url: str) -> list[str]:
+ return parse_qs(urlsplit(url).query).get("backgroundColor", [])
+
+
+def test_gives_a_dicebear_avatar_slacks_background() -> None:
+ # Without this the PNG is transparent and Slack flattens it onto white,
+ # putting a bright square around every agent in a dark message list.
+ assert _background(on_slack_background(DICEBEAR)) == [SLACK_SURFACE]
+
+
+def test_keeps_the_rest_of_the_url_intact() -> None:
+ # The seed decides which bot is drawn: lose it and the agent changes face.
+ query = parse_qs(urlsplit(on_slack_background(DICEBEAR)).query)
+ assert query["seed"] == ["worker"]
+ assert query["size"] == ["256"]
+
+
+def test_leaves_a_background_that_was_already_chosen() -> None:
+ chosen = f"{DICEBEAR}&backgroundColor=ff0000"
+ assert on_slack_background(chosen) == chosen
+
+
+def test_leaves_an_operators_own_image_alone() -> None:
+ # There is no parameter to add to someone else's URL, and rewriting one on
+ # a guess would be worse than leaving it as authored.
+ custom = "https://example.com/avatar.png"
+ assert on_slack_background(custom) == custom
+
+
+def test_leaves_the_initials_default_alone() -> None:
+ # ui-avatars already draws an opaque background, so it has no white square
+ # to fix and its own colour must survive.
+ default = default_icon_url("switch_worker")
+ assert on_slack_background(default) == default
+
+
+def test_does_not_match_a_lookalike_host() -> None:
+ # `api.dicebear.com.evil.test` is not DiceBear; only the exact host is.
+ impostor = "https://api.dicebear.com.evil.test/9.x/bottts/png?seed=worker"
+ assert on_slack_background(impostor) == impostor
+
+
+def test_adds_the_background_once_when_applied_twice() -> None:
+ assert _background(on_slack_background(on_slack_background(DICEBEAR))) == [
+ SLACK_SURFACE
+ ]
+
+
+def test_the_slack_adapter_applies_it_to_a_resolved_icon() -> None:
+ """The override wired to the resolver, which is the thing that actually has
+ to work and which none of the tests above prove on its own."""
+ adapter = SlackAdapter(
+ config=SlackConnectionConfig(
+ bot_token="xoxb-test",
+ app_token="xapp-test",
+ workspace_id="T123",
+ )
+ )
+
+ async def resolver(name: str) -> str | None:
+ return DICEBEAR
+
+ adapter.set_agent_icon_resolver(resolver)
+
+ resolved = asyncio.run(adapter.agent_icon_url("worker"))
+ assert _background(resolved) == [SLACK_SURFACE]