AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend/copilot): stop bot setup prompts in unrelated threads (#12994)
Bentlybb52366fb044
diff --git a/autogpt_platform/backend/backend/copilot/bot/handler.py b/autogpt_platform/backend/backend/copilot/bot/handler.pyindex 130107c793..af36fabf3f 100644--- a/autogpt_platform/backend/backend/copilot/bot/handler.py+++ b/autogpt_platform/backend/backend/copilot/bot/handler.py@@ -54,6 +54,11 @@ class MessageHandler:)return+ if ctx.channel_type == "thread" and not await threads.is_subscribed(+ ctx.platform, ctx.channel_id+ ):+ return+if not await self._ensure_linked(ctx, adapter):return@@ -72,9 +77,7 @@ class MessageHandler:return ctx.channel_idif ctx.channel_type == "thread":- if await threads.is_subscribed(ctx.platform, ctx.channel_id):- return ctx.channel_id- return None+ return ctx.channel_id# channel_type == "channel" — create a thread and subscribethread_name = f"{ctx.username} × AutoPilot"diff --git a/autogpt_platform/backend/backend/copilot/bot/handler_test.py b/autogpt_platform/backend/backend/copilot/bot/handler_test.pyindex 0462af9aeb..ae0a133ca9 100644--- a/autogpt_platform/backend/backend/copilot/bot/handler_test.py+++ b/autogpt_platform/backend/backend/copilot/bot/handler_test.py@@ -93,6 +93,20 @@ class TestEnsureLinked:assert "isn't linked" in call_args[1]assert "/setup" in call_args[1]+ @pytest.mark.asyncio+ async def test_unlinked_unsubscribed_thread_is_ignored(self):+ api = _api(server_linked=False)+ handler = MessageHandler(api)+ adapter = _adapter()+ with patch(+ "backend.copilot.bot.handler.threads.is_subscribed",+ new=AsyncMock(return_value=False),+ ):+ await handler.handle(_ctx(channel_type="thread"), adapter)++ api.resolve_server.assert_not_awaited()+ adapter.send_message.assert_not_awaited()+@pytest.mark.asyncioasync def test_unlinked_dm_prompts_link_flow(self):handler = MessageHandler(_api(user_linked=False))@@ -133,26 +147,18 @@ class TestResolveTarget:assert result == "dm-42"@pytest.mark.asyncio- async def test_unsubscribed_thread_returns_none(self):+ async def test_resolve_target_reuses_thread_after_subscription_gate(self):handler = MessageHandler(_api())adapter = _adapter()ctx = _ctx(channel_type="thread", channel_id="thread-old")- with patch(- "backend.copilot.bot.handler.threads.is_subscribed",- new=AsyncMock(return_value=False),- ):- assert await handler._resolve_target(ctx, adapter) is None+ assert await handler._resolve_target(ctx, adapter) == "thread-old"@pytest.mark.asyncioasync def test_subscribed_thread_keeps_channel(self):handler = MessageHandler(_api())adapter = _adapter()ctx = _ctx(channel_type="thread", channel_id="thread-ok")- with patch(- "backend.copilot.bot.handler.threads.is_subscribed",- new=AsyncMock(return_value=True),- ):- assert await handler._resolve_target(ctx, adapter) == "thread-ok"+ assert await handler._resolve_target(ctx, adapter) == "thread-ok"@pytest.mark.asyncioasync def test_channel_creates_and_subscribes_thread(self):