Skip to content

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 Discord context users point the bot at (links, replies, forwards), securely (#13396)

Bently6e926d9f2139
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/base.py b/autogpt_platform/backend/backend/copilot/bot/adapters/base.py
index d5832de46d..c9ba9dec28 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/base.py
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/base.py
@@ -30,6 +30,22 @@ class MessageHistoryEntry:
text: str
+@dataclass
+class ReferencedConversation:
+ """A different thread/channel the incoming message linked or @-referenced,
+ fetched by the bot via its own gateway so the model can read it directly
+ instead of trying to web-fetch a JS-rendered Discord page.
+
+ ``channel_id`` ties this content back to the raw link/mention in the user's
+ message so the renderer can rewrite that link into a readable ``#name``.
+ ``messages`` is in chronological order and already bounded to a char budget.
+ """
+
+ title: str
+ channel_id: str
+ messages: tuple[MessageHistoryEntry, ...]
+
+
class FileAttachment(BaseModel):
"""A workspace artifact ready to attach to a platform message.
@@ -89,6 +105,9 @@ class MessageContext:
# `(display_name, platform_user_id)` pairs. Anyone not in this list won't
# get pinged even if the LLM produces `@theirname` in its output.
mentionable_users: tuple[tuple[str, str], ...] = ()
+ # Other threads/channels the message linked or @-referenced, fetched by the
+ # bot up-front so the model has their content without web-fetching Discord.
+ referenced_conversations: tuple[ReferencedConversation, ...] = ()
@property
def is_dm(self) -> bool:
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py
index 519e3aec50..33cee6252c 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py
@@ -26,8 +26,14 @@ from ..base import (
MessageHistoryEntry,
PlatformAdapter,
PostedRef,
+ ReferencedConversation,
)
from . import commands, config, intro
+from .references import (
+ ReferenceTarget,
+ extract_referenced_targets,
+ replace_referenced_links,
+)
logger = logging.getLogger(__name__)
@@ -41,6 +47,17 @@ THREAD_HISTORY_LIMIT = 1000
THREAD_HISTORY_CHAR_BUDGET = 24000
_HISTORY_TRUNCATION_MARKER = "\n… [message truncated]"
+# When a message links or @-mentions other threads/channels, the bot fetches
+# their recent content up-front (same guild only) so the model can read it
+# instead of trying to web-fetch a JS-rendered Discord page. Bounded so a
+# link-heavy message can't fan out into many large reads.
+MAX_REFERENCED_CONVERSATIONS = 3
+REFERENCED_HISTORY_LIMIT = 200
+REFERENCED_CHAR_BUDGET = 8000
+# When a link names a specific message, fetch that message plus a little of the
+# conversation leading up to it (rather than the channel's latest activity).
+REFERENCED_MESSAGE_CONTEXT = 15
+
class DiscordAdapter(PlatformAdapter):
def __init__(self, api: BotBackend):
@@ -410,6 +427,23 @@ class DiscordAdapter(PlatformAdapter):
if channel_type == "thread" and bot_mentioned:
thread_history = await self._thread_history(message)
+ own_text = self._strip_mentions(message)
+ # Scan ONLY the user's own typed message for references. Links inside
+ # a forwarded or replied-to message are quoted context, not the
+ # user's request, so they must not be auto-fetched or rewritten.
+ referenced = await self._fetch_referenced_conversations(message, own_text)
+ if referenced:
+ # Swap the raw channel links/mentions for readable "#name"
+ # tokens — the fetched content is supplied under those names, so
+ # this stops the model from treating the paste as a URL it must
+ # open (and then claiming it can't access Discord).
+ own_text = replace_referenced_links(
+ own_text, {c.channel_id: c.title for c in referenced}
+ )
+ # Fold in forwarded content and the replied-to message as quoted
+ # context — both verbatim, with their links left untouched.
+ message_text = self._compose_with_forward(message, own_text)
+ message_text = await self._with_reply_context(message, message_text)
ctx = MessageContext(
platform="discord",
channel_type=channel_type,
@@ -418,10 +452,11 @@ class DiscordAdapter(PlatformAdapter):
message_id=str(message.id),
user_id=str(message.author.id),
username=message.author.display_name,
- text=self._message_text(message),
+ text=message_text,
bot_mentioned=bot_mentioned,
thread_history=thread_history,
mentionable_users=self._collect_mentionable_users(message),
+ referenced_conversations=referenced,
)
await self._on_message_callback(ctx, self)
@@ -477,7 +512,14 @@ class DiscordAdapter(PlatformAdapter):
message entirely, so the LLM acts on just the comment and can badly
misread the request. Stitch the forwarded content back in here.
"""
- own = self._strip_mentions(message)
+ return self._compose_with_forward(message, self._strip_mentions(message))
+
+ def _compose_with_forward(self, message: discord.Message, own: str) -> str:
+ """Fold any forwarded snapshot content onto the user's own text.
+
+ Kept separate from reference scanning so forwarded content (quoted, not
+ the user's request) is never mined for channel links to auto-fetch.
+ """
forwarded = self._forwarded_text(message)
if not forwarded:
return own
@@ -485,6 +527,46 @@ class DiscordAdapter(PlatformAdapter):
return f"{own}\n\n[Forwarded message]\n{forwarded}"
return f"[Forwarded message]\n{forwarded}"
+ async def _with_reply_context(
+ self, message: discord.Message, message_text: str
+ ) -> str:
+ """Prepend the replied-to message so the bot sees what's being answered.
+
+ A Discord reply (``message.reference``) only carries the short reply
+ text; the message it replies to holds the actual intent. It's always in
+ the same channel the user is already posting in, so surfacing it leaks
+ nothing they can't already see.
+ """
+ replied = await self._resolve_reply(message)
+ if replied is None:
+ return message_text
+ quoted = self._message_text(replied)
+ if not quoted:
+ return message_text
+ prefix = f"[Replying to {replied.author.display_name}]\n{quoted}"
+ return f"{prefix}\n\n{message_text}" if message_text else prefix
+
+ async def _resolve_reply(
+ self, message: discord.Message
+ ) -> Optional[discord.Message]:
+ ref = message.reference
+ if ref is None:
+ return None
+ # Forwards also use ``reference`` but carry their body in snapshots,
+ # which ``_message_text`` already stitches in — don't re-resolve those.
+ if getattr(message, "message_snapshots", None):
+ return None
+ if isinstance(ref.resolved, discord.Message):
+ return ref.resolved
+ if ref.message_id is None or not isinstance(
+ message.channel, discord.abc.Messageable
+ ):
+ return None
+ try:
+ return await message.channel.fetch_message(ref.message_id)
+ except (discord.NotFound, discord.Forbidden, discord.HTTPException):
+ return None
+
@staticmethod
def _forwarded_text(message: discord.Message) -> str:
"""Flatten any forwarded message snapshots into labelled text."""
@@ -533,57 +615,157 @@ class DiscordAdapter(PlatformAdapter):
) -> tuple[MessageHistoryEntry, ...]:
if not isinstance(message.channel, discord.Thread):
return ()
+ try:
+ return await self._budgeted_history(
+ message.channel.history(
+ limit=THREAD_HISTORY_LIMIT,
+ before=message,
+ oldest_first=False,
+ ),
+ THREAD_HISTORY_CHAR_BUDGET,
+ )
+ except (discord.Forbidden, discord.HTTPException):
+ logger.warning("Could not fetch Discord thread history", exc_info=True)
+ return ()
+
+ async def _budgeted_history(
+ self, history, char_budget: int
+ ) -> tuple[MessageHistoryEntry, ...]:
+ """Drain a newest-first message iterator into chronological entries,
+ capped at ``char_budget`` chars (most-recent kept when over budget).
+ Skips the bot's own messages (copilot has its own transcript for those)
+ but keeps other bots' messages as context.
+ """
entries: list[MessageHistoryEntry] = []
used_chars = 0
bot_user_id = self._client.user.id if self._client.user else None
+ async for prior in history:
+ if bot_user_id is not None and prior.author.id == bot_user_id:
+ continue
+ text = self._strip_mentions(prior)
+ if not text:
+ continue
+ remaining = char_budget - used_chars
+ if remaining <= 0:
+ break
+ oversized = len(text) > remaining
+ if oversized and entries:
+ # No room for another whole message — keep what we have.
+ break
+ if oversized:
+ # Lone most-recent message is itself over budget: keep a head.
+ text = _truncate_to_budget(text, remaining)
+ used_chars += len(text)
+ entries.append(
+ MessageHistoryEntry(
+ username=prior.author.display_name,
+ user_id=str(prior.author.id),
+ text=text,
+ )
+ )
+ if oversized:
+ break
+ entries.reverse() # chronological order for the prompt
+ return tuple(entries)
+
+ async def _fetch_referenced_conversations(
+ self, message: discord.Message, text: str
+ ) -> tuple[ReferencedConversation, ...]:
+ """Fetch the recent content of any thread/channel ``text`` references.
+
+ Read as the *requesting member* would: same-guild only, and only
+ channels they can actually see. The bot's gateway can read more than
+ the user can, so we must never surface a private channel's history to
+ someone who lacks access to it themselves.
+ """
+ if message.guild is None:
+ return ()
+ # ``message.author`` IS the guild Member (the gateway attaches it to the
+ # event); ``get_member`` only reads the cache, which is empty without the
+ # privileged members intent — so prefer the author and fall back to the
+ # cache only if it somehow isn't a Member (e.g. a webhook).
+ requester = (
+ message.author
+ if isinstance(message.author, discord.Member)
+ else message.guild.get_member(message.author.id)
+ )
+ if requester is None:
+ return ()
+ targets = extract_referenced_targets(
+ text,
+ exclude_channel_id=str(message.channel.id),
+ limit=MAX_REFERENCED_CONVERSATIONS,
+ )
+ conversations: list[ReferencedConversation] = []
+ for target in targets:
+ convo = await self._fetch_one_referenced(message.guild, requester, target)
+ if convo is not None:
+ conversations.append(convo)
+ return tuple(conversations)
+
+ async def _fetch_one_referenced(
+ self, guild: discord.Guild, requester: discord.Member, target: ReferenceTarget
+ ) -> Optional[ReferencedConversation]:
+ channel = await self._resolve_channel(target.channel_id)
+ if not isinstance(channel, (discord.TextChannel, discord.Thread)):
+ return None
+ if channel.guild.id != guild.id:
+ # Cross-guild reference — don't read content from another server.
+ return None
+ if not await self._can_requester_read(channel, requester):
+ return None
try:
- # Newest-first so that when a long thread exceeds the size budget we
- # keep the most recent (most relevant) messages instead of the
- # oldest; we reverse back to chronological order before returning.
- async for prior in message.channel.history(
- limit=THREAD_HISTORY_LIMIT,
- before=message,
- oldest_first=False,
- ):
- # Skip our own outputs — copilot has its own transcript for
- # that side. Other bots' messages are kept as context.
- if bot_user_id is not None and prior.author.id == bot_user_id:
- continue
- text = self._strip_mentions(prior)
- if not text:
- continue
- remaining = THREAD_HISTORY_CHAR_BUDGET - used_chars
- if remaining <= 0:
- break
- oversized = len(text) > remaining
- if oversized and entries:
- # No room for another whole message — stop and keep what we
- # have (the more recent messages).
- break
- if oversized:
- # Lone most-recent message is itself over budget: keep a
- # truncated head of it.
- text = _truncate_to_budget(text, remaining)
- used_chars += len(text)
- entries.append(
- MessageHistoryEntry(
- username=prior.author.display_name,
- user_id=str(prior.author.id),
- text=text,
- )
+ if target.message_id is not None:
+ # A permalink to a specific message: read that message and the
+ # turns leading up to it (``before`` the next snowflake includes
+ # the message itself), so "what was said here <link>" works even
+ # for an older message not in the channel's latest activity.
+ history = channel.history(
+ limit=REFERENCED_MESSAGE_CONTEXT,
+ before=discord.Object(id=int(target.message_id) + 1),
+ oldest_first=False,
)
- if oversized:
- # We just truncated the newest message to the budget; older
- # messages are both less relevant and would otherwise sneak
- # into the whitespace `rstrip` freed up. Stop here.
- break
+ else:
+ history = channel.history(
+ limit=REFERENCED_HISTORY_LIMIT, oldest_first=False
+ )
+ messages = await self._budgeted_history(history, REFERENCED_CHAR_BUDGET)
except (discord.Forbidden, discord.HTTPException):
- logger.warning("Could not fetch Discord thread history", exc_info=True)
- return ()
+ logger.warning(
+ "Could not fetch referenced channel %s",
+ target.channel_id,
+ exc_info=True,
+ )
+ return None
+ if not messages:
+ return None
+ return ReferencedConversation(
+ title=channel.name, channel_id=target.channel_id, messages=messages
+ )
- entries.reverse() # chronological order for the prompt
- return tuple(entries)
+ async def _can_requester_read(
+ self, channel: "discord.TextChannel | discord.Thread", requester: discord.Member
+ ) -> bool:
+ """Whether ``requester`` may read ``channel`` themselves.
+
+ Channel-level ``view_channel`` + ``read_message_history`` is the base
+ gate. For a *private* thread that is not enough — Discord requires the
+ member to actually be in the thread (``manage_threads`` bypasses, as it
+ does in the client), so check membership explicitly to avoid leaking a
+ private thread the bot happens to be in.
+ """
+ perms = channel.permissions_for(requester)
+ if not (perms.view_channel and perms.read_message_history):
+ return False
+ if isinstance(channel, discord.Thread) and channel.is_private():
+ if perms.manage_threads:
+ return True
+ try:
+ await channel.fetch_member(requester.id)
+ except (discord.NotFound, discord.Forbidden, discord.HTTPException):
+ return False
+ return True
def _truncate_to_budget(text: str, limit: int) -> str:
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter_test.py b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter_test.py
index 5a9668972a..8f52c4438b 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
@@ -789,6 +789,202 @@ class TestLockedThread:
callback.assert_awaited_once()
+ @pytest.mark.asyncio
+ async def test_on_message_rewrites_referenced_links_in_text(self):
+ # End-to-end through on_message: a channel @mention linking another
+ # channel attaches that conversation and rewrites the raw link into a
+ # readable #name in the forwarded text.
+ adapter, client = _bare_adapter(bot_id=1000)
+ callback = AsyncMock()
+ adapter.on_message(callback)
+ handlers = _register_events_with_mocked_decorator(adapter)
+
+ ref = _referenced_channel(
+ "general", 111, [_prior(2000, "Krz", "the decision was X")]
+ )
+ client.get_channel.return_value = ref
+
+ guild = MagicMock(id=111)
+ guild.get_member = MagicMock(return_value=MagicMock(spec=discord.Member))
+ msg = MagicMock()
+ msg.id = 999
+ msg.author = MagicMock(id=2000, bot=False, display_name="Bently")
+ msg.guild = guild
+ msg.channel = MagicMock(id=555) # a normal channel, not a Thread
+ msg.content = "<@1000> read https://discord.com/channels/111/222/333"
+ msg.mentions = [_mention(1000, "AutoPilot")]
+ msg.message_snapshots = []
+
+ await handlers["on_message"](msg)
+
+ callback.assert_awaited_once()
+ ctx = callback.await_args.args[0]
+ assert len(ctx.referenced_conversations) == 1
+ assert ctx.referenced_conversations[0].title == "general"
+ assert "#general" in ctx.text
+ assert "discord.com/channels" not in ctx.text
+
+
+class TestReplyContext:
+ @staticmethod
+ def _replied(content: str, author_name: str = "Bently") -> MagicMock:
+ replied = MagicMock(spec=discord.Message)
+ replied.content = content
+ replied.mentions = []
+ replied.message_snapshots = []
+ replied.author = MagicMock(display_name=author_name)
+ return replied
+
+ @pytest.mark.asyncio
+ async def test_resolve_reply_uses_resolved_message(self):
+ adapter, _ = _bare_adapter()
+ replied = self._replied("fact about space")
+ msg = MagicMock()
+ msg.message_snapshots = []
+ msg.reference = MagicMock(resolved=replied)
+ assert await adapter._resolve_reply(msg) is replied
+
+ @pytest.mark.asyncio
+ async def test_no_reference_resolves_to_none(self):
+ adapter, _ = _bare_adapter()
+ msg = MagicMock()
+ msg.reference = None
+ assert await adapter._resolve_reply(msg) is None
+
+ @pytest.mark.asyncio
+ async def test_forward_is_not_treated_as_reply(self):
+ # A forward also populates ``reference`` but is handled via snapshots.
+ adapter, _ = _bare_adapter()
+ msg = MagicMock()
+ msg.message_snapshots = [MagicMock()]
+ msg.reference = MagicMock(resolved=self._replied("x"))
+ assert await adapter._resolve_reply(msg) is None
+
+ @pytest.mark.asyncio
+ async def test_fetches_reply_when_not_resolved(self):
+ adapter, _ = _bare_adapter()
+ replied = self._replied("fetched body")
+ channel = MagicMock(spec=discord.TextChannel)
+ channel.fetch_message = AsyncMock(return_value=replied)
+ msg = MagicMock()
+ msg.message_snapshots = []
+ msg.reference = MagicMock(resolved=None, message_id=42)
+ msg.channel = channel
+ assert await adapter._resolve_reply(msg) is replied
+ channel.fetch_message.assert_awaited_once_with(42)
+
+ @pytest.mark.asyncio
+ async def test_with_reply_context_prepends_quoted_message(self):
+ adapter, _ = _bare_adapter()
+ replied = self._replied("fact about space", author_name="AutoBoostBot")
+ msg = MagicMock()
+ msg.message_snapshots = []
+ msg.reference = MagicMock(resolved=replied)
+ out = await adapter._with_reply_context(msg, "can you tell me?")
+ assert "[Replying to AutoBoostBot]" in out
+ assert "fact about space" in out
+ assert out.endswith("can you tell me?")
+
+ @pytest.mark.asyncio
+ async def test_with_reply_context_noop_without_reply(self):
+ adapter, _ = _bare_adapter()
+ msg = MagicMock()
+ msg.reference = None
+ assert await adapter._with_reply_context(msg, "hi") == "hi"
+
+ @pytest.mark.asyncio
+ async def test_on_message_includes_replied_message(self):
+ adapter, _ = _bare_adapter(bot_id=1000)
+ callback = AsyncMock()
+ adapter.on_message(callback)
+ handlers = _register_events_with_mocked_decorator(adapter)
+
+ replied = self._replied("fact about space", author_name="AutoBoostBot")
+ guild = MagicMock(id=111)
+ guild.get_member = MagicMock(return_value=MagicMock(spec=discord.Member))
+ msg = MagicMock()
+ msg.id = 999
+ msg.author = MagicMock(id=2000, bot=False, display_name="Bently")
+ msg.guild = guild
+ msg.channel = MagicMock(id=555) # a normal channel, not a Thread
+ msg.content = "<@1000> can you tell me?"
+ msg.mentions = [_mention(1000, "AutoPilot")]
+ msg.message_snapshots = []
+ msg.reference = MagicMock(resolved=replied)
+
+ await handlers["on_message"](msg)
+
+ callback.assert_awaited_once()
+ ctx = callback.await_args.args[0]
+ assert "fact about space" in ctx.text
+ assert "can you tell me?" in ctx.text
+
+ @pytest.mark.asyncio
+ async def test_links_inside_replied_message_are_not_fetched(self):
+ # A link that only appears in the quoted reply (not the user's own
+ # message) is context, not a request — it must not trigger a fetch or
+ # get rewritten. Guards against scanning the combined text.
+ adapter, client = _bare_adapter(bot_id=1000)
+ callback = AsyncMock()
+ adapter.on_message(callback)
+ handlers = _register_events_with_mocked_decorator(adapter)
+
+ replied = self._replied(
+ "see https://discord.com/channels/111/222/333", author_name="Krz"
+ )
+ guild = MagicMock(id=111)
+ guild.get_member = MagicMock(return_value=MagicMock(spec=discord.Member))
+ msg = MagicMock()
+ msg.id = 999
+ msg.author = MagicMock(id=2000, bot=False, display_name="Bently")
+ msg.guild = guild
+ msg.channel = MagicMock(id=555)
+ msg.content = "<@1000> thanks!" # no link of its own
+ msg.mentions = [_mention(1000, "AutoPilot")]
+ msg.message_snapshots = []
+ msg.reference = MagicMock(resolved=replied)
+
+ await handlers["on_message"](msg)
+
+ callback.assert_awaited_once()
+ ctx = callback.await_args.args[0]
+ # No channel history fetched, and the quoted link is left as-is.
+ client.get_channel.assert_not_called()
+ assert ctx.referenced_conversations == ()
+ assert "https://discord.com/channels/111/222/333" in ctx.text
+
+ @pytest.mark.asyncio
+ async def test_links_inside_forwarded_message_are_not_fetched(self):
+ # A link that only appears in a forwarded message is quoted context,
+ # not the user's request — it must not be fetched or rewritten.
+ adapter, client = _bare_adapter(bot_id=1000)
+ callback = AsyncMock()
+ adapter.on_message(callback)
+ handlers = _register_events_with_mocked_decorator(adapter)
+
+ guild = MagicMock(id=111)
+ guild.get_member = MagicMock(return_value=MagicMock(spec=discord.Member))
+ msg = MagicMock()
+ msg.id = 999
+ msg.author = MagicMock(id=2000, bot=False, display_name="Bently")
+ msg.guild = guild
+ msg.channel = MagicMock(id=555)
+ msg.content = "<@1000> look at this" # no link of its own
+ msg.mentions = [_mention(1000, "AutoPilot")]
+ msg.message_snapshots = [
+ _snapshot("see https://discord.com/channels/111/222/333")
+ ]
+ msg.reference = None
+
+ await handlers["on_message"](msg)
+
+ callback.assert_awaited_once()
+ ctx = callback.await_args.args[0]
+ client.get_channel.assert_not_called()
+ assert ctx.referenced_conversations == ()
+ assert "[Forwarded message]" in ctx.text
+ assert "https://discord.com/channels/111/222/333" in ctx.text
+
# ── Proactive output (backend → platform) ──────────────────────────────
@@ -958,3 +1154,255 @@ class TestProactiveOutput:
assert ref is not None
assert ref.id == "777"
+
+
+# ── Referenced-conversation fetch ──────────────────────────────────────
+
+
+def _referenced_channel(
+ name: str, guild_id: int, priors: list[MagicMock], *, can_access: bool = True
+) -> MagicMock:
+ channel = MagicMock(spec=discord.TextChannel)
+ channel.name = name
+ channel.guild = MagicMock(id=guild_id)
+ channel.history = MagicMock(return_value=_AsyncHistory(priors))
+ perms = MagicMock(view_channel=can_access, read_message_history=can_access)
+ channel.permissions_for = MagicMock(return_value=perms)
+ return channel
+
+
+def _referenced_thread(
+ guild_id: int,
+ priors: list[MagicMock],
+ *,
+ is_private: bool = True,
+ is_member: bool = True,
+ manage_threads: bool = False,
+) -> MagicMock:
+ thread = MagicMock(spec=discord.Thread)
+ thread.name = "secret-thread"
+ thread.guild = MagicMock(id=guild_id)
+ thread.history = MagicMock(return_value=_AsyncHistory(priors))
+ thread.is_private = MagicMock(return_value=is_private)
+ perms = MagicMock(
+ view_channel=True, read_message_history=True, manage_threads=manage_threads
+ )
+ thread.permissions_for = MagicMock(return_value=perms)
+ if is_member:
+ thread.fetch_member = AsyncMock(return_value=MagicMock())
+ else:
+ thread.fetch_member = AsyncMock(
+ side_effect=discord.NotFound(MagicMock(status=404), "not a member")
+ )
+ return thread
+
+
+def _prior(author_id: int, display_name: str, content: str) -> MagicMock:
+ msg = MagicMock()
+ msg.author = MagicMock(id=author_id, display_name=display_name)
+ msg.content = content
+ msg.mentions = []
+ return msg
+
+
+def _incoming(guild_id: int | None, channel_id: int) -> MagicMock:
+ message = MagicMock()
+ message.channel = MagicMock(id=channel_id)
+ message.author = MagicMock(id=42)
+ if guild_id is None:
+ message.guild = None
+ else:
+ guild = MagicMock(id=guild_id)
+ # By default the requester is a member of the guild; tests override
+ # get_member / channel perms to exercise the access checks.
+ guild.get_member = MagicMock(return_value=MagicMock(spec=discord.Member))
+ message.guild = guild
+ return message
+
+
+class TestFetchReferencedConversations:
+ @pytest.mark.asyncio
+ async def test_fetches_same_guild_referenced_thread(self):
+ adapter, client = _bare_adapter()
+ ref = _referenced_channel(
+ "Release v0.6.61", 111, [_prior(2000, "Krz", "bump the version")]
+ )
+ client.get_channel.return_value = ref
+
+ result = await adapter._fetch_referenced_conversations(
+ _incoming(111, 555),
+ "find my mentions in https://discord.com/channels/111/222/333",
+ )
+
+ assert len(result) == 1
+ assert result[0].title == "Release v0.6.61"
+ assert result[0].channel_id == "222"
+ assert result[0].messages[0].text == "bump the version"
+
+ @pytest.mark.asyncio
+ async def test_uses_message_author_when_member_cache_is_empty(self):
+ # The bot runs without the privileged members intent, so
+ # guild.get_member can return None. message.author is the authoritative
+ # Member for guild messages and must be used instead.
+ adapter, client = _bare_adapter()
+ ref = _referenced_channel("general", 111, [_prior(1, "A", "hi there")])
+ client.get_channel.return_value = ref
+ message = _incoming(111, 555)
+ message.guild.get_member = MagicMock(return_value=None) # cache miss
+ message.author = MagicMock(spec=discord.Member, id=42)
+
+ result = await adapter._fetch_referenced_conversations(
+ message, "https://discord.com/channels/111/222/333"
+ )
+
+ assert len(result) == 1
+ assert result[0].messages[0].text == "hi there"
+
+ @pytest.mark.asyncio
+ async def test_fetches_specific_message_in_current_channel(self):
+ # A permalink to a specific message in the channel the user is posting
+ # in is a real "read this exact message" request, not redundant context.
+ adapter, client = _bare_adapter()
+ ref = _referenced_channel("autopilot", 111, [_prior(1, "A", "the answer")])
+ client.get_channel.return_value = ref
+
+ result = await adapter._fetch_referenced_conversations(
+ _incoming(111, 222), # current channel == the linked channel
+ "https://discord.com/channels/111/222/333",
+ )
+
+ assert len(result) == 1
+ assert result[0].messages[0].text == "the answer"
+
+ @pytest.mark.asyncio
+ async def test_skips_private_thread_when_requester_not_member(self):
+ # The bot is in the private thread, but the requester isn't — its
+ # content must not leak to them.
+ adapter, client = _bare_adapter()
+ thread = _referenced_thread(111, [_prior(1, "A", "secret")], is_member=False)
+ client.get_channel.return_value = thread
+
+ result = await adapter._fetch_referenced_conversations(
+ _incoming(111, 555), "https://discord.com/channels/111/222/333"
+ )
+
+ assert result == ()
+
+ @pytest.mark.asyncio
+ async def test_reads_private_thread_when_requester_is_member(self):
+ adapter, client = _bare_adapter()
+ thread = _referenced_thread(111, [_prior(1, "A", "secret")], is_member=True)
+ client.get_channel.return_value = thread
+
+ result = await adapter._fetch_referenced_conversations(
+ _incoming(111, 555), "https://discord.com/channels/111/222/333"
+ )
+
+ assert len(result) == 1
+ assert result[0].messages[0].text == "secret"
+
+ @pytest.mark.asyncio
+ async def test_reads_private_thread_with_manage_threads(self):
+ # manage_threads grants access like it does in the Discord client.
+ adapter, client = _bare_adapter()
+ thread = _referenced_thread(
+ 111, [_prior(1, "A", "secret")], is_member=False, manage_threads=True
+ )
+ client.get_channel.return_value = thread
+
+ result = await adapter._fetch_referenced_conversations(
+ _incoming(111, 555), "https://discord.com/channels/111/222/333"
+ )
+
+ assert len(result) == 1
+
+ @pytest.mark.asyncio
+ async def test_skips_cross_guild_reference(self):
+ adapter, client = _bare_adapter()
+ ref = _referenced_channel("Other", 999, [_prior(2000, "X", "secret")])
+ client.get_channel.return_value = ref
+
+ result = await adapter._fetch_referenced_conversations(
+ _incoming(111, 555), "https://discord.com/channels/999/222/333"
+ )
+
+ assert result == ()
+
+ @pytest.mark.asyncio
+ async def test_skips_channel_requester_cannot_view(self):
+ # The bot's gateway can read this channel, but the requesting member
+ # can't — its history must not leak to them.
+ adapter, client = _bare_adapter()
+ ref = _referenced_channel(
+ "Private", 111, [_prior(2000, "X", "secret")], can_access=False
+ )
+ client.get_channel.return_value = ref
+
+ result = await adapter._fetch_referenced_conversations(
+ _incoming(111, 555), "https://discord.com/channels/111/222/333"
+ )
+
+ assert result == ()
+
+ @pytest.mark.asyncio
+ async def test_skips_when_requester_not_a_member(self):
+ adapter, client = _bare_adapter()
+ message = _incoming(111, 555)
+ message.guild.get_member = MagicMock(return_value=None)
+
+ result = await adapter._fetch_referenced_conversations(
+ message, "https://discord.com/channels/111/222/333"
+ )
+
+ assert result == ()
+ client.get_channel.assert_not_called()
+
+ @pytest.mark.asyncio
+ async def test_no_reference_makes_no_fetch(self):
+ adapter, client = _bare_adapter()
+ result = await adapter._fetch_referenced_conversations(
+ _incoming(111, 555), "just a normal message"
+ )
+ assert result == ()
+ client.get_channel.assert_not_called()
+
+ @pytest.mark.asyncio
+ async def test_dm_with_no_guild_skips(self):
+ adapter, client = _bare_adapter()
+ result = await adapter._fetch_referenced_conversations(
+ _incoming(None, 555), "<#222>"
+ )
+ assert result == ()
+ client.get_channel.assert_not_called()
+
+ @pytest.mark.asyncio
+ async def test_skips_non_text_channel(self):
+ # A reference that resolves to e.g. a voice/category channel — not
+ # something we can read history from.
+ adapter, client = _bare_adapter()
+ client.get_channel.return_value = MagicMock() # not a TextChannel/Thread
+ result = await adapter._fetch_referenced_conversations(
+ _incoming(111, 555), "https://discord.com/channels/111/222/333"
+ )
+ assert result == ()
+
+ @pytest.mark.asyncio
+ async def test_returns_nothing_when_history_errors(self):
+ adapter, client = _bare_adapter()
+ ref = _referenced_channel("Boom", 111, [])
+ ref.history = MagicMock(side_effect=discord.HTTPException(MagicMock(), "boom"))
+ client.get_channel.return_value = ref
+ result = await adapter._fetch_referenced_conversations(
+ _incoming(111, 555), "https://discord.com/channels/111/222/333"
+ )
+ assert result == ()
+
+ @pytest.mark.asyncio
+ async def test_skips_channel_with_no_readable_messages(self):
+ adapter, client = _bare_adapter()
+ ref = _referenced_channel("Empty", 111, [])
+ client.get_channel.return_value = ref
+ result = await adapter._fetch_referenced_conversations(
+ _incoming(111, 555), "https://discord.com/channels/111/222/333"
+ )
+ assert result == ()
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/references.py b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/references.py
new file mode 100644
index 0000000000..2af9a1d811
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/references.py
@@ -0,0 +1,87 @@
+"""Parse Discord channel/thread references out of message text.
+
+A user often points the bot at another conversation by pasting its link
+(``https://discord.com/channels/<guild>/<channel>/<message>``) or @-mentioning
+a channel (``<#channel_id>``). This module extracts the referenced
+channel/thread (and the specific message, when the link names one) so the
+adapter can fetch their content via the gateway — no parsing logic that needs a
+live Discord connection lives here, so it's cheap to test.
+"""
+
+import re
+
+from pydantic import BaseModel, ConfigDict
+
+# A single positional pass over either form so references are returned in the
+# order they appear in the text:
+# - link group: discord.com / discordapp.com / canary. / ptb. channel link —
+# the second path segment is the channel/thread ID, the optional third is a
+# specific message ID.
+# - mention group: ``<#channel_id>``, how "#frontend" serializes on the wire.
+_REFERENCE_RE = re.compile(
+ r"https?://(?:\w+\.)?discord(?:app)?\.com/channels/\d+/(\d+)(?:/(\d+))?"
+ r"|<#(\d+)>",
+ re.IGNORECASE,
+)
+
+
+class ReferenceTarget(BaseModel):
+ """A channel/thread the message points at, plus the specific message ID
+ when the link named one (a ``.../channel/message`` permalink)."""
+
+ model_config = ConfigDict(frozen=True)
+
+ channel_id: str
+ message_id: str | None
+
+
+def _match_target(match: "re.Match[str]") -> ReferenceTarget:
+ # group(1)/group(2): channel + optional message from a permalink;
+ # group(3): channel from a ``<#id>`` mention (never names a message).
+ channel_id = match.group(1) or match.group(3)
+ return ReferenceTarget(channel_id=channel_id, message_id=match.group(2))
+
+
+def extract_referenced_targets(
+ text: str,
+ *,
+ exclude_channel_id: str,
+ limit: int,
+) -> list[ReferenceTarget]:
+ """Return de-duplicated reference targets in ``text``, in first-seen order.
+
+ A bare channel reference to the *current* channel is skipped — its history
+ is already supplied as context. A permalink to a *specific message* is kept
+ even in the current channel: "what was said here <link>" is a request to
+ read that exact message, not redundant. Capped at ``limit`` so a link-heavy
+ message can't fan out into unbounded fetches.
+ """
+ seen: set[tuple[str, str | None]] = set()
+ targets: list[ReferenceTarget] = []
+ for match in _REFERENCE_RE.finditer(text):
+ target = _match_target(match)
+ if target.message_id is None and target.channel_id == exclude_channel_id:
+ continue
+ key = (target.channel_id, target.message_id)
+ if key in seen:
+ continue
+ seen.add(key)
+ targets.append(target)
+ return targets[:limit]
+
+
+def replace_referenced_links(text: str, labels: dict[str, str]) -> str:
+ """Rewrite each channel link / ``<#id>`` mention into a readable ``#label``.
+
+ Only references whose channel ID is in ``labels`` (the ones we actually
+ fetched) are rewritten; anything else is left untouched. This keeps the
+ model from fixating on a raw URL it thinks it must open, when the linked
+ content has already been supplied to it under that ``#label``.
+ """
+
+ def _sub(match: "re.Match[str]") -> str:
+ channel_id = match.group(1) or match.group(3)
+ label = labels.get(channel_id)
+ return f"#{label}" if label else match.group(0)
+
+ return _REFERENCE_RE.sub(_sub, text)
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/references_test.py b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/references_test.py
new file mode 100644
index 0000000000..fc1e96fb9b
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/references_test.py
@@ -0,0 +1,96 @@
+"""Tests for Discord reference extraction."""
+
+from backend.copilot.bot.adapters.discord.references import (
+ ReferenceTarget,
+ extract_referenced_targets,
+ replace_referenced_links,
+)
+
+
+def _extract(
+ text: str, exclude: str = "0", limit: int = 3
+) -> list[tuple[str, str | None]]:
+ targets = extract_referenced_targets(text, exclude_channel_id=exclude, limit=limit)
+ return [(t.channel_id, t.message_id) for t in targets]
+
+
+def test_extracts_channel_and_message_from_permalink():
+ text = "find my mentions in https://discord.com/channels/111/222/333 please"
+ assert _extract(text) == [("222", "333")]
+
+
+def test_extracts_channel_mention_has_no_message():
+ assert _extract("can you help with <#999>?") == [("999", None)]
+
+
+def test_link_without_message_id():
+ assert _extract("https://discord.com/channels/111/222") == [("222", None)]
+
+
+def test_handles_subdomains_and_discordapp():
+ text = (
+ "old https://discordapp.com/channels/1/2/3 "
+ "canary https://canary.discord.com/channels/1/4/5"
+ )
+ assert _extract(text) == [("2", "3"), ("4", "5")]
+
+
+def test_dedupes_and_preserves_order():
+ text = "<#10> https://discord.com/channels/1/20/3 <#10> <#30>"
+ assert _extract(text) == [("10", None), ("20", "3"), ("30", None)]
+
+
+def test_excludes_current_channel_for_bare_reference():
+ text = "https://discord.com/channels/1/222 and <#222>"
+ assert _extract(text, exclude="222") == []
+
+
+def test_keeps_specific_message_even_in_current_channel():
+ # "what was said here <permalink>" targets one message, so a permalink to
+ # the current channel is a real request — not redundant context.
+ text = "https://discord.com/channels/1/222/333"
+ assert _extract(text, exclude="222") == [("222", "333")]
+
+
+def test_respects_limit():
+ text = "<#1> <#2> <#3> <#4> <#5>"
+ assert _extract(text, limit=2) == [("1", None), ("2", None)]
+
+
+def test_no_references_returns_empty():
+ assert _extract("just a normal message, no links") == []
+
+
+# ── replace_referenced_links ───────────────────────────────────────────
+
+
+def test_replace_rewrites_link_to_readable_name():
+ text = "read into https://discord.com/channels/111/222/333 please"
+ out = replace_referenced_links(text, {"222": "Random Fact"})
+ assert out == "read into #Random Fact please"
+
+
+def test_replace_rewrites_channel_mention():
+ out = replace_referenced_links("look at <#999> ok", {"999": "standup"})
+ assert out == "look at #standup ok"
+
+
+def test_replace_leaves_unknown_references_untouched():
+ # Only channels we actually fetched (in the label map) get rewritten.
+ text = "https://discord.com/channels/1/222/3 and <#777>"
+ out = replace_referenced_links(text, {"222": "known"})
+ assert "#known" in out
+ assert "<#777>" in out
+
+
+def test_replace_without_labels_is_noop():
+ text = "https://discord.com/channels/1/2/3"
+ assert replace_referenced_links(text, {}) == text
+
+
+def test_reference_target_is_hashable():
+ # Frozen model — equal values compare equal and collapse in a set.
+ a = ReferenceTarget(channel_id="1", message_id="2")
+ b = ReferenceTarget(channel_id="1", message_id="2")
+ assert a == b
+ assert len({a, b}) == 1
diff --git a/autogpt_platform/backend/backend/copilot/bot/handler.py b/autogpt_platform/backend/backend/copilot/bot/handler.py
index 533b0022fe..e97a64f064 100644
--- a/autogpt_platform/backend/backend/copilot/bot/handler.py
+++ b/autogpt_platform/backend/backend/copilot/bot/handler.py
@@ -25,7 +25,12 @@ from backend.util.exceptions import (
from backend.util.settings import Settings
from . import sessions, threads
-from .adapters.base import FileAttachment, MessageContext, PlatformAdapter
+from .adapters.base import (
+ FileAttachment,
+ MessageContext,
+ MessageHistoryEntry,
+ PlatformAdapter,
+)
from .bot_backend import BotBackend, BotStreamError
from .config import SESSION_TTL
from .text import format_batch, split_at_boundary
@@ -99,7 +104,7 @@ class MessageHandler:
char_count=len(ctx.text),
)
- message_text = self._message_text(ctx) if include_thread_history else ctx.text
+ message_text = self._message_text(ctx, include_thread_history)
await self._enqueue_and_process(ctx, adapter, target_id, message_text)
# -- Target resolution --
@@ -126,22 +131,49 @@ class MessageHandler:
# -- Batched streaming --
- def _message_text(self, ctx: MessageContext) -> str:
- if not ctx.thread_history:
+ def _message_text(self, ctx: MessageContext, include_thread_history: bool) -> str:
+ # Referenced conversations (links/@-mentions the user pasted) are always
+ # surfaced — that's the whole point of fetching them. Thread history is
+ # only included on the first @-into a thread we don't own; a subscribed
+ # thread's prior turns already live in the session.
+ thread_history = ctx.thread_history if include_thread_history else ()
+ if not thread_history and not ctx.referenced_conversations:
return ctx.text
platform_display = ctx.platform.capitalize()
- lines = ["[Recent thread context before this message]"]
- for entry in ctx.thread_history:
- user = (
- f"{entry.username} ({platform_display} user ID: {entry.user_id})"
- if entry.user_id
- else entry.username
+ lines: list[str] = []
+ if ctx.referenced_conversations:
+ # The user pointed at other channels/threads; their content is
+ # already fetched and inlined below. Lead with a firm instruction so
+ # the model answers from it instead of fixating on the link and
+ # claiming it can't access the platform.
+ lines.append(
+ f"[The {platform_display} channel(s) the user referenced have "
+ f"already been read for you — their full content is included "
+ f"below under each #name. Answer directly from it. Do NOT say you "
+ f"can't open links or access {platform_display}; you already have "
+ f"the content.]"
)
- lines.append(f"\n[From {user}]\n{entry.text}")
+ for convo in ctx.referenced_conversations:
+ lines.append(f"\n[Content of #{convo.title}]")
+ for entry in convo.messages:
+ lines.append(self._format_history_entry(entry, platform_display))
+ if thread_history:
+ lines.append("\n[Recent thread context before this message]")
+ for entry in thread_history:
+ lines.append(self._format_history_entry(entry, platform_display))
lines.append(f"\n[Current message]\n{ctx.text}")
return "\n".join(lines)
+ @staticmethod
+ def _format_history_entry(entry: MessageHistoryEntry, platform_display: str) -> str:
+ user = (
+ f"{entry.username} ({platform_display} user ID: {entry.user_id})"
+ if entry.user_id
+ else entry.username
+ )
+ return f"\n[From {user}]\n{entry.text}"
+
async def _enqueue_and_process(
self,
ctx: MessageContext,
diff --git a/autogpt_platform/backend/backend/copilot/bot/handler_test.py b/autogpt_platform/backend/backend/copilot/bot/handler_test.py
index d27d216fb6..4568d7df7d 100644
--- a/autogpt_platform/backend/backend/copilot/bot/handler_test.py
+++ b/autogpt_platform/backend/backend/copilot/bot/handler_test.py
@@ -9,7 +9,12 @@ import pytest
from backend.platform_linking.models import WorkspaceArtifact
from backend.util.exceptions import DuplicateChatMessageError, NotFoundError
-from .adapters.base import ChannelType, MessageContext, MessageHistoryEntry
+from .adapters.base import (
+ ChannelType,
+ MessageContext,
+ MessageHistoryEntry,
+ ReferencedConversation,
+)
from .bot_backend import LinkTokenResult, ResolveResult
from .handler import MessageHandler, TargetState, build_thread_name, clamp_thread_name
@@ -25,6 +30,7 @@ def _ctx(
text: str = "hello bot",
bot_mentioned: bool = False,
thread_history: tuple[MessageHistoryEntry, ...] = (),
+ referenced_conversations: tuple[ReferencedConversation, ...] = (),
) -> MessageContext:
return MessageContext(
platform="discord",
@@ -37,6 +43,7 @@ def _ctx(
text=text,
bot_mentioned=bot_mentioned,
thread_history=thread_history,
+ referenced_conversations=referenced_conversations,
)
@@ -318,7 +325,8 @@ class TestThreadAdoption:
MessageHistoryEntry("Alice", "u-1", "I think option A"),
MessageHistoryEntry("Bob", "u-2", "Option B is safer"),
),
- )
+ ),
+ include_thread_history=True,
)
assert "Recent thread context" in text
@@ -327,6 +335,45 @@ class TestThreadAdoption:
assert "Current message" in text
assert "what should we do?" in text
+ @pytest.mark.asyncio
+ async def test_channel_mention_enqueues_referenced_conversation(self):
+ # A plain channel @mention (include_thread_history is False here) that
+ # references another conversation must still carry that fetched content
+ # into the enqueued prompt. Regression guard: the handler previously
+ # only rendered referenced conversations when pulling thread history,
+ # so channel mentions silently dropped them.
+ handler = MessageHandler(_api())
+ adapter = _adapter()
+ enqueue = AsyncMock()
+
+ with (
+ patch.object(handler, "_enqueue_and_process", new=enqueue),
+ patch("backend.copilot.bot.handler.threads.subscribe", new=AsyncMock()),
+ ):
+ await handler.handle(
+ _ctx(
+ channel_type="channel",
+ bot_mentioned=True,
+ text="summarize the linked thread",
+ referenced_conversations=(
+ ReferencedConversation(
+ title="Random Fact About Something",
+ channel_id="c-1",
+ messages=(
+ MessageHistoryEntry("Krz", "u-2", "did you know X"),
+ ),
+ ),
+ ),
+ ),
+ adapter,
+ )
+
+ enqueue.assert_awaited_once()
+ message_text = enqueue.await_args.args[3]
+ assert "[Content of #Random Fact About Something]" in message_text
+ assert "did you know X" in message_text
+ assert "can't open links or access Discord" in message_text
+
class TestBatching:
@pytest.mark.asyncio
@@ -820,3 +867,57 @@ class TestDeliverArtifact:
adapter.send_file.assert_not_awaited()
adapter.send_message.assert_awaited_once()
assert "x.png" in adapter.send_message.await_args.args[1]
+
+
+class TestMessageTextReferencedConversations:
+ def test_no_context_returns_bare_text(self):
+ handler = MessageHandler(_api())
+ assert (
+ handler._message_text(_ctx(text="hi"), include_thread_history=False) == "hi"
+ )
+
+ def test_referenced_conversation_is_rendered_with_access_instruction(self):
+ handler = MessageHandler(_api())
+ ctx = _ctx(
+ text="find my mentions",
+ referenced_conversations=(
+ ReferencedConversation(
+ title="Release v0.6.61",
+ channel_id="c-1",
+ messages=(
+ MessageHistoryEntry("Krz", "u-2", "remember to bump version"),
+ MessageHistoryEntry("Nick", "u-3", "and tag the release"),
+ ),
+ ),
+ ),
+ )
+ # include_thread_history=False on purpose: a plain channel @mention must
+ # still surface referenced conversations (the bug this guards against).
+ out = handler._message_text(ctx, include_thread_history=False)
+ assert "[Content of #Release v0.6.61]" in out
+ # Firm instruction so the model answers from the supplied content instead
+ # of claiming it can't access the platform.
+ assert "can't open links or access Discord" in out
+ assert "remember to bump version" in out
+ assert "and tag the release" in out
+ assert "[Current message]\nfind my mentions" in out
+
+ def test_referenced_and_thread_history_both_rendered(self):
+ handler = MessageHandler(_api())
+ ctx = _ctx(
+ text="now",
+ thread_history=(MessageHistoryEntry("Alice", "u-1", "earlier point"),),
+ referenced_conversations=(
+ ReferencedConversation(
+ title="other-thread",
+ channel_id="c-9",
+ messages=(MessageHistoryEntry("Bob", "u-9", "linked content"),),
+ ),
+ ),
+ )
+ out = handler._message_text(ctx, include_thread_history=True)
+ assert "linked content" in out
+ assert "Recent thread context" in out
+ assert "earlier point" in out
+ # Referenced block precedes the immediate thread context.
+ assert out.index("linked content") < out.index("earlier point")