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-bot): read the post a Discord thread was created from (#14330)
Bentlyf4856d0005cd
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.pyindex 10281af53a..ef535821f7 100644--- a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py@@ -760,7 +760,7 @@ class DiscordAdapter(SocketAdapter):if not isinstance(message.channel, discord.Thread):return ()try:- return await self._budgeted_history(+ history = await self._budgeted_history(message.channel.history(limit=THREAD_HISTORY_LIMIT,before=message,@@ -770,7 +770,42 @@ class DiscordAdapter(SocketAdapter):)except (discord.Forbidden, discord.HTTPException):logger.warning("Could not fetch Discord thread history", exc_info=True)- return ()+ history = ()+ starter = await self._thread_starter_entry(message.channel)+ return (starter, *history) if starter else history++ async def _thread_starter_entry(+ self, thread: discord.Thread+ ) -> Optional[MessageHistoryEntry]:+ """The message a thread was opened from, as its oldest history entry.++ A thread created from a channel post shares that post's id, but the+ post itself lives in the parent channel and never appears in+ ``thread.history()``. Without it the bot sees a thread whose first+ line is "make this happen" and has no idea what "this" is. Threads+ opened without an origin message have no starter and return None.+ Kept outside the character budget so it is never truncated away.+ """+ starter = thread.starter_message+ if not isinstance(starter, discord.Message):+ parent = thread.parent+ if not isinstance(parent, discord.abc.Messageable):+ return None+ try:+ starter = await parent.fetch_message(thread.id)+ except (discord.NotFound, discord.Forbidden, discord.HTTPException):+ return None+ bot_user_id = self._client.user.id if self._client.user else None+ if bot_user_id is not None and starter.author.id == bot_user_id:+ return None+ text = self._strip_mentions(starter)+ if not text:+ return None+ return MessageHistoryEntry(+ username=starter.author.display_name,+ user_id=str(starter.author.id),+ text=text,+ )async def _budgeted_history(self, history, char_budget: intdiff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter_test.py b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter_test.pyindex 4f719e2a44..141b886c46 100644--- a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter_test.py+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter_test.py@@ -509,6 +509,69 @@ class TestRenameThread:class TestThreadHistory:@pytest.mark.asyncio+ async def test_thread_started_from_a_post_includes_that_post_first(self):+ # Toran turns Nick's channel post into a thread and @mentions the bot.+ # Nick's post is the thread's starter message: it lives in the parent+ # channel, not in thread.history(), so it must be fetched separately.+ adapter, _ = _bare_adapter(bot_id=1000)+ bot = _mention(1000, "AutoGPT")++ starter = MagicMock(spec=discord.Message)+ starter.content = "docs should link to platform sign up"+ starter.mentions = []+ starter.role_mentions = []+ starter.author = MagicMock(bot=False, id=2000, display_name="Nick")++ channel = MagicMock(spec=discord.Thread)+ channel.id = 555+ channel.starter_message = None+ channel.parent = MagicMock(spec=discord.TextChannel)+ channel.parent.fetch_message = AsyncMock(return_value=starter)+ channel.history.return_value = _AsyncHistory([])+ message = _message("<@1000> make this happen please", [bot])+ message.channel = channel++ history = await adapter._thread_history(message)++ channel.parent.fetch_message.assert_awaited_once_with(555)+ assert [entry.username for entry in history] == ["Nick"]+ assert history[0].text == "docs should link to platform sign up"++ async def test_thread_without_origin_message_has_no_starter_entry(self):+ adapter, _ = _bare_adapter(bot_id=1000)+ bot = _mention(1000, "AutoGPT")+ channel = MagicMock(spec=discord.Thread)+ channel.id = 555+ channel.starter_message = None+ channel.parent = MagicMock(spec=discord.TextChannel)+ channel.parent.fetch_message = AsyncMock(+ side_effect=discord.NotFound(MagicMock(status=404), "gone")+ )+ channel.history.return_value = _AsyncHistory([])+ message = _message("<@1000> hi", [bot])+ message.channel = channel++ assert await adapter._thread_history(message) == ()++ async def test_cached_starter_is_used_and_bot_authored_starter_is_skipped(self):+ adapter, _ = _bare_adapter(bot_id=1000)+ bot = _mention(1000, "AutoGPT")+ starter = MagicMock(spec=discord.Message)+ starter.content = "old bot output"+ starter.mentions = []+ starter.role_mentions = []+ starter.author = MagicMock(bot=True, id=1000, display_name="AutoGPT")+ channel = MagicMock(spec=discord.Thread)+ channel.starter_message = starter+ channel.parent = MagicMock(spec=discord.TextChannel)+ channel.parent.fetch_message = AsyncMock()+ channel.history.return_value = _AsyncHistory([])+ message = _message("<@1000> hi", [bot])+ message.channel = channel++ assert await adapter._thread_history(message) == ()+ channel.parent.fetch_message.assert_not_awaited()+async def test_fetches_user_thread_history_chronological(self):# Discord returns history newest-first; the adapter reverses it back to# chronological order, dropping its own outputs.