Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.
Python agent-orchestrationaiai-agentschatopsdiscordlangchainmcpmicrosoft-teamsmulti-agentopen-sourceopenaiself-hostedslack
fix(discord): delete agent messages through the webhook that sent them (CHOO-1104)
Louis Amaudruz3226708882c7
diff --git a/core/switch_core/bridges/collaboration/discord/adapter.py b/core/switch_core/bridges/collaboration/discord/adapter.pyindex b34316339..8db0ea3e3 100644--- a/core/switch_core/bridges/collaboration/discord/adapter.py+++ b/core/switch_core/bridges/collaboration/discord/adapter.py@@ -399,6 +399,24 @@ class DiscordAdapter(CollaborationAdapter):if not message_id:logger.error("Cannot delete message: invalid message ref %s", message_ref)return++ # Agent posts are authored by the channel webhook, not the bot, so the+ # bot may only remove them with Manage Messages. Delete through the+ # webhook that sent it, mirroring update_message, and keep the bot path+ # for messages the bot really did post (admin notices, DM fallbacks).+ kwargs: dict[str, Any] = {}+ if location_id and location_id != channel_id:+ kwargs["thread"] = discord.Object(id=int(location_id))+ try:+ webhook = await self._get_webhook(int(channel_id))+ await webhook.delete_message(int(message_id), **kwargs)+ return+ except discord.NotFound:+ pass+ except discord.HTTPException as e:+ logger.error("Failed to delete Discord message %s: %s", message_ref, e)+ return+try:target = await self._get_channel(int(location_id or channel_id))await target.get_partial_message(int(message_id)).delete()diff --git a/core/tests/switch_core/bridges/collaboration/test_discord_adapter.py b/core/tests/switch_core/bridges/collaboration/test_discord_adapter.pyindex 526133e27..1546c00e6 100644--- a/core/tests/switch_core/bridges/collaboration/test_discord_adapter.py+++ b/core/tests/switch_core/bridges/collaboration/test_discord_adapter.py@@ -163,7 +163,9 @@ class _FakeWebhook:self.token = tokenself.sent: list[dict[str, Any]] = []self.edits: list[dict[str, Any]] = []+ self.deletes: list[dict[str, Any]] = []self.edit_raises_not_found = False+ self.delete_raises_not_found = Falseasync def send(self, **kwargs: Any) -> Any:self.sent.append(kwargs)@@ -176,6 +178,11 @@ class _FakeWebhook:raise discord.NotFound(_FakeResponse(), "unknown message")self.edits.append({"message_id": message_id, **kwargs})+ async def delete_message(self, message_id: int, **kwargs: Any) -> None:+ if self.delete_raises_not_found:+ raise discord.NotFound(_FakeResponse(), "unknown message")+ self.deletes.append({"message_id": message_id, **kwargs})+class _FakeClient:def __init__(self, channels: dict[int, Any]) -> None:@@ -739,9 +746,44 @@ def test_update_message_falls_back_to_bot_message_edit() -> None:assert bot_msg.edited == "edited"-def test_delete_message_uses_partial_message_in_location_channel() -> None:+def test_delete_message_goes_through_the_webhook_that_sent_it() -> None:+ # Agent posts are authored by the webhook, not the bot. Deleting them as the+ # bot needs Manage Messages and silently fails without it, which left every+ # runtime indicator stranded in the channel.+ adapter = _adapter()+ channel = _FakeChannel()+ webhook = _FakeWebhook()+ channel.existing_webhooks = [webhook]+ adapter._client = _FakeClient({CHANNEL_ID: channel})++ _run(adapter.delete_message(str(CHANNEL_ID), f"{CHANNEL_ID}:901"))++ assert [d["message_id"] for d in webhook.deletes] == [901]+ assert channel.deleted_ids == []+++def test_delete_message_in_a_thread_passes_the_thread_through() -> None:+ adapter = _adapter()+ channel = _FakeChannel()+ webhook = _FakeWebhook()+ channel.existing_webhooks = [webhook]+ thread = _FakeThread(parent=channel, thread_id=4000)+ adapter._client = _FakeClient({CHANNEL_ID: channel, 4000: thread})++ _run(adapter.delete_message(str(CHANNEL_ID), "4000:901"))++ assert webhook.deletes[0]["message_id"] == 901+ assert webhook.deletes[0]["thread"].id == 4000+++def test_delete_message_falls_back_to_the_bot_for_its_own_posts() -> None:+ # Admin notices and DM fallbacks are posted by the bot, so the webhook does+ # not own them and reports NotFound.adapter = _adapter()channel = _FakeChannel()+ webhook = _FakeWebhook()+ webhook.delete_raises_not_found = True+ channel.existing_webhooks = [webhook]thread = _FakeThread(parent=channel, thread_id=4000)thread.deleted_ids = [] # type: ignore[attr-defined]thread.get_partial_message = lambda mid: _FakePartialMessage(thread, mid) # type: ignore[attr-defined]@@ -749,6 +791,7 @@ def test_delete_message_uses_partial_message_in_location_channel() -> None:_run(adapter.delete_message(str(CHANNEL_ID), "4000:901"))+ assert webhook.deletes == []assert thread.deleted_ids == [901] # type: ignore[attr-defined]@@ -831,7 +874,7 @@ def test_runtime_state_detail_edits_message_in_place() -> None:def test_runtime_state_idle_clears_working_message() -> None:- adapter, channel, _ = _runtime_setup()+ adapter, channel, webhook = _runtime_setup()_run(adapter.apply_runtime_state(@@ -848,7 +891,8 @@ def test_runtime_state_idle_clears_working_message() -> None:))- assert channel.deleted_ids == [901]+ assert [d["message_id"] for d in webhook.deletes] == [901]+ assert channel.deleted_ids == []assert (str(CHANNEL_ID), "my-agent") not in adapter._working_msg@@ -891,7 +935,7 @@ def test_runtime_state_awaiting_input_pings_and_resume_clears_pings() -> None:thread_root_id=None,))- assert channel.deleted_ids == [902]+ assert [d["message_id"] for d in webhook.deletes] == [902]assert (str(CHANNEL_ID), "my-agent") not in adapter._input_pings