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.
feat(backend/copilot-bot): only auto-reply in bot-created threads (#13269)
Bently811dbf454eba
diff --git a/autogpt_platform/backend/backend/copilot/bot/handler.py b/autogpt_platform/backend/backend/copilot/bot/handler.pyindex 1e5aa36b10..928b156663 100644--- a/autogpt_platform/backend/backend/copilot/bot/handler.py+++ b/autogpt_platform/backend/backend/copilot/bot/handler.py@@ -64,13 +64,20 @@ class MessageHandler:)return- should_subscribe_thread = False+ # In a thread we only auto-reply when we own it (= we created it in+ # response to an @mention in a channel). For any other existing+ # thread we'd been added to, require an explicit @mention each turn+ # so we don't hijack ongoing team conversations.+ include_thread_history = Falseif ctx.channel_type == "thread":is_subscribed = await threads.is_subscribed(ctx.platform, ctx.channel_id)if not is_subscribed:if not ctx.bot_mentioned:return- should_subscribe_thread = True+ # First time we're @-ed into this thread — pull the recent+ # thread history into the prompt so AutoPilot has context,+ # but DON'T subscribe. Future messages here need another @.+ include_thread_history = Trueif not await self._ensure_linked(ctx, adapter):return@@ -79,10 +86,7 @@ class MessageHandler:if not target_id:return # Thread not subscribed, ignore silently- if should_subscribe_thread:- await threads.subscribe(ctx.platform, ctx.channel_id)-- message_text = self._message_text(ctx) if should_subscribe_thread else ctx.text+ message_text = self._message_text(ctx) if include_thread_history else ctx.textawait self._enqueue_and_process(ctx, adapter, target_id, message_text)# -- Target resolution --diff --git a/autogpt_platform/backend/backend/copilot/bot/handler_test.py b/autogpt_platform/backend/backend/copilot/bot/handler_test.pyindex 948a07a554..65f8d82363 100644--- a/autogpt_platform/backend/backend/copilot/bot/handler_test.py+++ b/autogpt_platform/backend/backend/copilot/bot/handler_test.py@@ -235,7 +235,12 @@ class TestResolveTarget:class TestThreadAdoption:@pytest.mark.asyncio- async def test_mentioned_unsubscribed_thread_is_subscribed(self):+ async def test_mentioned_unsubscribed_thread_replies_without_subscribing(self):+ # Bot was @-ed into an existing thread it didn't create. Reply this+ # turn so the user gets an answer, but DON'T subscribe — future+ # messages here need another @ to wake it back up. This keeps the+ # bot from hijacking ongoing team conversations. The reply should+ # carry the thread history so AutoPilot has context.handler = MessageHandler(_api())adapter = _adapter()enqueue = AsyncMock()@@ -255,12 +260,49 @@ class TestThreadAdoption:channel_type="thread",channel_id="thread-existing",bot_mentioned=True,+ thread_history=(+ MessageHistoryEntry("Alice", "u-1", "I think option A"),+ ),),adapter,)- subscribe.assert_awaited_once_with("discord", "thread-existing")+ subscribe.assert_not_awaited()enqueue.assert_awaited_once()+ # The enqueued message_text (4th positional arg) must carry the+ # history-enriched prompt — otherwise include_thread_history is+ # silently bypassed and the LLM gets the bare message.+ message_text = enqueue.await_args.args[3]+ assert "Recent thread context" in message_text+ assert "I think option A" in message_text++ @pytest.mark.asyncio+ async def test_unmentioned_unsubscribed_thread_message_is_ignored(self):+ handler = MessageHandler(_api())+ adapter = _adapter()+ enqueue = AsyncMock()++ with (+ patch.object(handler, "_enqueue_and_process", new=enqueue),+ patch(+ "backend.copilot.bot.handler.threads.is_subscribed",+ new=AsyncMock(return_value=False),+ ),+ patch(+ "backend.copilot.bot.handler.threads.subscribe", new=AsyncMock()+ ) as subscribe,+ ):+ await handler.handle(+ _ctx(+ channel_type="thread",+ channel_id="thread-existing",+ bot_mentioned=False,+ ),+ adapter,+ )++ subscribe.assert_not_awaited()+ enqueue.assert_not_awaited()def test_thread_history_is_included_in_message_text(self):handler = MessageHandler(_api())