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): deliver AutoPilot clarifying questions to bot platforms (#14435)

Bently7ed6d5d440ba
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/base.py b/autogpt_platform/backend/backend/copilot/bot/adapters/base.py
index dc3b3f9a64..9e899454d4 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/base.py
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/base.py
@@ -201,6 +201,61 @@ class PlatformAdapter(ABC):
"""
...
+ @property
+ def max_choice_label_length(self) -> int:
+ """Longest option label this platform's native widget shows in full.
+
+ Past it the widget silently clips, so two options sharing a prefix
+ render identically while still dispatching their own full text — the
+ user cannot tell which button they are pressing. A question with any
+ option over this goes as numbered text instead, which shows all of
+ it. Discord buttons 80, Slack 75, Telegram 64, Teams 60.
+ """
+ return 80
+
+ @property
+ def max_choice_options(self) -> int:
+ """How many options this platform's native widget renders legibly.
+
+ `base` documents that an adapter decides whether `options` fits its
+ native widget, so the number belongs here rather than as one
+ platform-blind constant in the caller: Teams' Adaptive Cards render
+ roughly six actions and silently drop the tail, while Discord rows,
+ Slack action blocks and Telegram keyboards take the full ten.
+ """
+ return 10
+
+ @property
+ def supports_choice_buttons(self) -> bool:
+ """Whether `send_choice_buttons` can render native option buttons.
+
+ Default False — only platforms overriding `send_choice_buttons`
+ below flip this. Checked before spending a `bot.choices` token on a
+ question, so unsupported adapters never pay that cost.
+ """
+ return False
+
+ async def send_choice_buttons(
+ self,
+ channel_id: str,
+ text: str,
+ options: list[str],
+ token: str,
+ mentionable_users: tuple[tuple[str, str], ...] = (),
+ ) -> bool:
+ """Send `text` with native clickable option buttons/select where the
+ platform supports it, returning True once sent.
+
+ A click carries `token` and the clicked option's index (not the
+ option text -- Telegram's callback_data caps at 64 bytes); the
+ adapter resolves it via `bot.choices.resolve_choice` and feeds the
+ resolved text through its own `on_message` callback, exactly as if
+ the user had typed it. Returns False when the platform doesn't
+ implement this (or `options` doesn't fit its native widget), telling
+ the caller to fall back to plain numbered text. Default: unsupported.
+ """
+ return False
+
@abstractmethod
async def send_reply(
self,
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 34e6f2266e..021710b2dd 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py
@@ -37,7 +37,7 @@ from ..base import (
ReferencedConversation,
SocketAdapter,
)
-from . import commands, config, intro
+from . import choice_ui, commands, config, intro
from .references import (
ReferenceTarget,
extract_referenced_targets,
@@ -132,6 +132,9 @@ class DiscordAdapter(SocketAdapter):
def on_message(self, callback: MessageCallback) -> None:
self._on_message_callback = callback
+ # Choice buttons are stateless and outlive this process, so their
+ # click handler is registered once here rather than per sent message.
+ choice_ui.register_choice_handler(self._client, self, callback)
async def start(self) -> None:
await self._client.start(config.get_bot_token())
@@ -187,6 +190,27 @@ class DiscordAdapter(SocketAdapter):
)
await channel.send(text, view=view, tts=False)
+ @property
+ def supports_choice_buttons(self) -> bool:
+ return True
+
+ async def send_choice_buttons(
+ self,
+ channel_id: str,
+ text: str,
+ options: list[str],
+ token: str,
+ mentionable_users: tuple[tuple[str, str], ...] = (),
+ ) -> bool:
+ channel = await self._resolve_channel(channel_id)
+ if channel is None or not isinstance(channel, discord.abc.Messageable):
+ return False
+ if self._on_message_callback is None:
+ return False
+ view = choice_ui.build_choice_view(token, options)
+ await channel.send(text, view=view, tts=False)
+ return True
+
async def send_file(self, channel_id: str, text: str, file: FileAttachment) -> None:
channel = await self._resolve_channel(channel_id)
if channel is None or not isinstance(channel, discord.abc.Messageable):
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 141b886c46..af99f2d0c1 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
@@ -15,6 +15,8 @@ from backend.copilot.bot.adapters.discord.adapter import (
_mention_queries,
_resolve_mentions,
)
+from backend.copilot.bot.adapters.discord.choice_ui import _ChoiceButton
+from backend.copilot.bot.turn_stream import _clarification_message
def _bare_adapter(bot_id: int | None = 1000) -> tuple[DiscordAdapter, MagicMock]:
@@ -355,6 +357,69 @@ class TestSendMethods:
# Default empty mentionable_users → AllowedMentions.none()
assert isinstance(kwargs["allowed_mentions"], discord.AllowedMentions)
+ @pytest.mark.asyncio
+ async def test_send_message_delivers_clarification_question(self):
+ """SECRT-2604: an ask_question payload must reach Discord as a plain
+ text message with the numbered options intact, unmangled by the
+ adapter's real send path (channel.send)."""
+ adapter, client = _bare_adapter()
+ channel = MagicMock(spec=discord.TextChannel)
+ channel.send = AsyncMock()
+ client.get_channel.return_value = channel
+
+ text = _clarification_message(
+ {"questions": [{"question": "Which region?", "options": ["US", "EU"]}]}
+ )
+ await adapter.send_message("123", text)
+
+ sent = channel.send.await_args.args[0]
+ assert "Which region?" in sent
+ assert "1. US" in sent
+ assert "2. EU" in sent
+ assert "Reply with a number" in sent
+
+ @pytest.mark.asyncio
+ async def test_send_choice_buttons_sends_one_button_per_option(self):
+ adapter, client = _bare_adapter()
+ adapter._on_message_callback = AsyncMock()
+ channel = MagicMock(spec=discord.TextChannel)
+ channel.send = AsyncMock()
+ client.get_channel.return_value = channel
+
+ sent = await adapter.send_choice_buttons(
+ "123", "❓ Which region?", ["US", "EU"], "tok"
+ )
+
+ assert sent is True
+ assert adapter.supports_choice_buttons is True
+ channel.send.assert_awaited_once()
+ args, kwargs = channel.send.await_args
+ assert args == ("❓ Which region?",)
+ view = kwargs["view"]
+ # Each child is a DynamicItem wrapping the Button, so the label lives
+ # on `.item`; `custom_id` proxies through.
+ buttons = [cast(_ChoiceButton, child) for child in view.children]
+ assert [b.item.label for b in buttons] == ["US", "EU"]
+ # Stateless, like the other three adapters: everything needed to
+ # resolve a click rides in the custom_id, so a button posted before a
+ # deploy still works after it. A random discord.py-generated id plus
+ # an in-memory view would go dead on restart and on View.timeout,
+ # giving "This interaction failed" while the token stays live.
+ assert [b.custom_id for b in buttons] == ["qans:tok:0", "qans:tok:1"]
+ assert view.timeout is None
+
+ @pytest.mark.asyncio
+ async def test_send_choice_buttons_returns_false_without_message_callback(self):
+ adapter, client = _bare_adapter()
+ channel = MagicMock(spec=discord.TextChannel)
+ channel.send = AsyncMock()
+ client.get_channel.return_value = channel
+
+ sent = await adapter.send_choice_buttons("123", "❓ Q?", ["US"], "tok")
+
+ assert sent is False
+ channel.send.assert_not_awaited()
+
@pytest.mark.asyncio
async def test_send_message_silently_drops_when_channel_missing(self):
adapter, client = _bare_adapter()
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/choice_ui.py b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/choice_ui.py
new file mode 100644
index 0000000000..2b2dda8da1
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/choice_ui.py
@@ -0,0 +1,153 @@
+"""Native Discord buttons for ask_question, and the click handler that turns
+a press into an ordinary inbound message.
+
+Kept out of ``adapter.py`` (already large) — the only thing the adapter needs
+from here is ``build_choice_view`` and ``register_choice_handler``.
+
+The buttons are **stateless**, like Slack's, Telegram's and Teams'. Everything
+needed to resolve a click — the ``bot.choices`` token and the option index —
+is encoded in each button's ``custom_id`` and parsed back out on click, so no
+per-message state lives in this process. A view held in memory instead would
+go dead on every deploy and once discord.py's own view timeout fired, and a
+click on those dead buttons produces Discord's generic "This interaction
+failed" while the Redis token stays valid for the rest of its hour.
+"""
+
+import logging
+import re
+
+import discord
+
+from backend.copilot.bot import choices
+from backend.copilot.bot.adapters.base import (
+ ChannelType,
+ MessageCallback,
+ MessageContext,
+ PlatformAdapter,
+)
+
+logger = logging.getLogger(__name__)
+
+_EXPIRED_NOTICE = "This question has expired — type your answer instead."
+_NOT_YOUR_QUESTION = (
+ "This question was for someone else — they still need to answer it."
+)
+
+# Set once by `register_choice_handler`. The click handler is reconstructed by
+# discord.py from the custom_id alone (possibly in a process that never sent
+# the message), so it has no closure to read these from.
+_adapter: PlatformAdapter | None = None
+_on_message: MessageCallback | None = None
+
+
+def register_choice_handler(
+ client: discord.Client, adapter: PlatformAdapter, on_message: MessageCallback
+) -> None:
+ """Wire clicks on choice buttons to ``on_message`` for this process.
+
+ Registered once at startup rather than per message: that is what lets a
+ button posted before a restart still work afterwards.
+ """
+ global _adapter, _on_message
+ _adapter = adapter
+ _on_message = on_message
+ client.add_dynamic_items(_ChoiceButton)
+
+
+def build_choice_view(token: str, options: list[str]) -> discord.ui.View:
+ """One button per option (View auto-wraps into rows of 5).
+
+ ``timeout=None`` because the buttons carry their own state: expiry is the
+ Redis token's job, and an expired token gives the user the notice above
+ rather than Discord's generic failure.
+ """
+ view = discord.ui.View(timeout=None)
+ for index, option in enumerate(options):
+ view.add_item(_ChoiceButton.for_option(token, index, option))
+ return view
+
+
+class _ChoiceButton(
+ discord.ui.DynamicItem[discord.ui.Button],
+ # Deliberately wider than today's token grammar (`uuid4().hex[:12]`):
+ # this pattern is what routes a click on a button that may have been
+ # posted weeks ago, so a future change to how tokens are minted must not
+ # silently orphan every live button.
+ template=r"qans:(?P<token>[0-9A-Za-z_-]{1,64}):(?P<index>[0-9]{1,3})",
+):
+ def __init__(self, token: str, index: int, label: str = "") -> None:
+ self._token = token
+ self._index = index
+ super().__init__(
+ discord.ui.Button(
+ style=discord.ButtonStyle.secondary,
+ label=label[:80],
+ custom_id=f"qans:{token}:{index}",
+ )
+ )
+
+ @classmethod
+ def for_option(cls, token: str, index: int, label: str) -> "_ChoiceButton":
+ return cls(token, index, label)
+
+ @classmethod
+ async def from_custom_id(
+ cls,
+ interaction: discord.Interaction,
+ item: discord.ui.Item[discord.ui.View],
+ match: re.Match[str],
+ /,
+ ) -> "_ChoiceButton":
+ # Rebuilt from the click alone — the label is whatever Discord still
+ # renders on the message, so it is not needed here.
+ return cls(match["token"], int(match["index"]))
+
+ async def callback(self, interaction: discord.Interaction) -> None:
+ if _adapter is None or _on_message is None:
+ logger.error("Choice button clicked before the handler was registered")
+ return
+ resolved = await choices.resolve_choice(
+ "discord", self._token, self._index, str(interaction.user.id)
+ )
+ if resolved.text is None:
+ await interaction.response.send_message(
+ _NOT_YOUR_QUESTION if resolved.refused else _EXPIRED_NOTICE,
+ ephemeral=True,
+ )
+ return
+ option = resolved.text
+ # The token is already consumed, so the answer exists only in this
+ # call. The ack is cosmetic (a 404 on a deleted message, or 40060 on
+ # a fast double-click) and must never cost the user their answer.
+ try:
+ await interaction.response.edit_message(
+ content=f"✅ You answered: {option}", view=None
+ )
+ except discord.HTTPException:
+ logger.exception("Failed to acknowledge choice click; continuing the turn")
+ ctx = _context_from_interaction(interaction, option)
+ if ctx is not None:
+ await _on_message(ctx, _adapter)
+
+
+def _context_from_interaction(
+ interaction: discord.Interaction, option: str
+) -> MessageContext | None:
+ if interaction.channel_id is None or interaction.user is None:
+ return None
+ channel_type: ChannelType = "channel"
+ if interaction.guild_id is None:
+ channel_type = "dm"
+ elif isinstance(interaction.channel, discord.Thread):
+ channel_type = "thread"
+ return MessageContext(
+ platform="discord",
+ channel_type=channel_type,
+ server_id=str(interaction.guild_id) if interaction.guild_id else None,
+ channel_id=str(interaction.channel_id),
+ message_id=str(interaction.id),
+ user_id=str(interaction.user.id),
+ username=interaction.user.display_name,
+ text=option,
+ bot_mentioned=True,
+ )
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/choice_ui_test.py b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/choice_ui_test.py
new file mode 100644
index 0000000000..0a61093c74
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/choice_ui_test.py
@@ -0,0 +1,162 @@
+"""Tests for Discord native choice buttons (SECRT-2604)."""
+
+from typing import cast
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import discord
+import pytest
+
+from backend.copilot.bot.choices import ResolvedChoice
+
+from . import choice_ui
+from .choice_ui import _ChoiceButton, build_choice_view
+
+_CHOICES = "backend.copilot.bot.adapters.discord.choice_ui.choices"
+
+
+def _buttons(view: discord.ui.View) -> list[_ChoiceButton]:
+ """``View.children`` is typed as the base ``Item``; ours are all ours."""
+ return [cast(_ChoiceButton, child) for child in view.children]
+
+
+def _interaction(
+ *, channel_id: int = 111, guild_id: int | None = 222, user_id: int = 9
+) -> MagicMock:
+ interaction = MagicMock()
+ interaction.channel_id = channel_id
+ interaction.guild_id = guild_id
+ interaction.channel = MagicMock(spec=discord.TextChannel)
+ interaction.id = 555
+ interaction.user = MagicMock()
+ interaction.user.id = user_id
+ interaction.user.display_name = "Bently"
+ interaction.response = MagicMock()
+ interaction.response.send_message = AsyncMock()
+ interaction.response.edit_message = AsyncMock()
+ return interaction
+
+
+def handler():
+ """Register a click handler, as the adapter does once at startup.
+
+ The buttons are stateless, so the adapter and callback are module-level
+ rather than captured per view — a click has to resolve in a process that
+ may never have sent the message.
+ """
+ adapter = MagicMock()
+ on_message = AsyncMock()
+ choice_ui.register_choice_handler(MagicMock(), adapter, on_message)
+ yield adapter, on_message
+ choice_ui._adapter = None
+ choice_ui._on_message = None
+
+
+class TestBuildChoiceView:
+ @pytest.mark.asyncio
+ async def test_one_button_per_option(self):
+ view = build_choice_view("tok", ["US", "EU", "AP"])
+ assert len(view.children) == 3
+ assert [b.item.label for b in _buttons(view)] == ["US", "EU", "AP"]
+
+ @pytest.mark.asyncio
+ async def test_labels_truncate_to_discord_button_cap(self):
+ view = build_choice_view("tok", ["x" * 200])
+ label = _buttons(view)[0].item.label
+ assert label is not None and len(label) == 80
+
+ @pytest.mark.asyncio
+ async def test_buttons_are_stateless_so_they_survive_a_restart(self):
+ # Everything needed to resolve a click rides in the custom_id, and
+ # the view never times out — an in-memory view with generated ids
+ # went dead on every deploy and on View.timeout, giving the user
+ # "This interaction failed" while the Redis token stayed valid.
+ view = build_choice_view("abc123", ["US", "EU"])
+ assert [b.custom_id for b in _buttons(view)] == [
+ "qans:abc123:0",
+ "qans:abc123:1",
+ ]
+ assert view.timeout is None
+
+
+class TestChoiceButtonCallback:
+ @pytest.mark.asyncio
+ async def test_click_resolves_and_dispatches_as_message(self, handler):
+ adapter, on_message = handler
+ interaction = _interaction()
+
+ with patch(
+ f"{_CHOICES}.resolve_choice",
+ new=AsyncMock(return_value=ResolvedChoice(text="EU")),
+ ):
+ view = build_choice_view("tok", ["US", "EU"])
+ await view.children[1].callback(interaction)
+
+ interaction.response.edit_message.assert_awaited_once_with(
+ content="✅ You answered: EU", view=None
+ )
+ on_message.assert_awaited_once()
+ ctx, dispatched_adapter = on_message.await_args.args
+ assert dispatched_adapter is adapter
+ assert ctx.text == "EU"
+ assert ctx.platform == "discord"
+ assert ctx.channel_id == "111"
+ assert ctx.user_id == "9"
+ assert ctx.username == "Bently"
+ assert ctx.channel_type == "channel"
+ assert ctx.bot_mentioned is True
+
+ @pytest.mark.asyncio
+ async def test_dm_interaction_maps_to_dm_channel_type(self, handler):
+ _, on_message = handler
+ interaction = _interaction(guild_id=None)
+
+ with patch(
+ f"{_CHOICES}.resolve_choice",
+ new=AsyncMock(return_value=ResolvedChoice(text="US")),
+ ):
+ view = build_choice_view("tok", ["US"])
+ await view.children[0].callback(interaction)
+
+ ctx, _ = on_message.await_args.args
+ assert ctx.channel_type == "dm"
+ assert ctx.server_id is None
+
+ @pytest.mark.asyncio
+ async def test_expired_token_shows_ephemeral_notice_and_does_not_dispatch(
+ self, handler
+ ):
+ _, on_message = handler
+ interaction = _interaction()
+
+ with patch(
+ f"{_CHOICES}.resolve_choice",
+ new=AsyncMock(return_value=ResolvedChoice(text=None)),
+ ):
+ view = build_choice_view("tok", ["US", "EU"])
+ await view.children[0].callback(interaction)
+
+ interaction.response.send_message.assert_awaited_once()
+ assert "expired" in interaction.response.send_message.await_args.args[0]
+ interaction.response.edit_message.assert_not_awaited()
+ on_message.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_dispatches_even_if_the_ack_edit_fails(self, handler):
+ # resolve_choice has already consumed the token, so the answer exists
+ # only in this call: a 404 on a deleted message (or 40060 on a fast
+ # double-click) must not cost the user their turn.
+ _, on_message = handler
+ interaction = _interaction()
+ interaction.response.edit_message = AsyncMock(
+ side_effect=discord.HTTPException(MagicMock(status=404), "gone")
+ )
+
+ with patch(
+ f"{_CHOICES}.resolve_choice",
+ new=AsyncMock(return_value=ResolvedChoice(text="EU")),
+ ):
+ view = build_choice_view("tok", ["US", "EU"])
+ await view.children[1].callback(interaction)
+
+ on_message.assert_awaited_once()
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/slack/adapter.py b/autogpt_platform/backend/backend/copilot/bot/adapters/slack/adapter.py
index a8c9fb39f7..a74e7c6b90 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/slack/adapter.py
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/slack/adapter.py
@@ -26,7 +26,7 @@ from fastapi import FastAPI, Request, Response
from fastapi.responses import JSONResponse, PlainTextResponse
from slack_sdk.web.async_client import AsyncWebClient
-from backend.copilot.bot import threads
+from backend.copilot.bot import choices, threads
from backend.copilot.bot.adapters.base import (
ChannelInfo,
ChannelType,
@@ -49,13 +49,18 @@ from backend.copilot.bot.text import iter_chunks, resolve_mentions
from backend.data.db_accessors import bot_installs_db
from backend.platform_linking.models import Platform
-from . import commands, config, history, oauth, signing
+from . import choice_ui, commands, config, history, oauth, signing
from .text import to_mrkdwn
logger = logging.getLogger(__name__)
EVENTS_PATH = "/api/copilot-webhooks/slack/events"
COMMANDS_PATH = "/api/copilot-webhooks/slack/commands"
+INTERACTIVE_PATH = "/api/copilot-webhooks/slack/interactive"
+_EXPIRED_NOTICE = "This question has expired — type your answer instead."
+_NOT_YOUR_QUESTION = (
+ "This question was for someone else — they still need to answer it."
+)
# Slack lifecycle events that end a workspace's install — revoke its token.
_UNINSTALL_EVENTS = {"app_uninstalled", "tokens_revoked"}
@@ -137,6 +142,9 @@ class SlackAdapter(WebhookAdapter):
def register_routes(self, app: FastAPI) -> None:
app.add_api_route(EVENTS_PATH, self._handle_event_request, methods=["POST"])
app.add_api_route(COMMANDS_PATH, self._handle_command_request, methods=["POST"])
+ app.add_api_route(
+ INTERACTIVE_PATH, self._handle_interactive_request, methods=["POST"]
+ )
# Multi-workspace "Add to Slack" install + OAuth callback (no-op unless
# the app's client id/secret are configured). A (re)install replaces the
# workspace's token, so it must drop this replica's cached client.
@@ -225,6 +233,97 @@ class SlackAdapter(WebhookAdapter):
}
return await commands.handle(self._api, form)
+ async def _handle_interactive_request(self, request: Request) -> Response:
+ # Same body-then-form sequence as _handle_command_request: Slack's
+ # interactivity payload is also form-encoded (a JSON `payload` field).
+ if await read_verified_webhook_body(request, _verify_signature) is None:
+ return unauthorized_webhook_response()
+ form_data = await request.form()
+ raw_payload = form_data.get("payload")
+ if not isinstance(raw_payload, str):
+ return PlainTextResponse("ok")
+ payload = json.loads(raw_payload)
+ if payload.get("type") == "block_actions":
+ # Fire-and-forget so we ACK within Slack's 3s window.
+ task = asyncio.create_task(self._dispatch_block_action(payload))
+ self._event_tasks.add(task)
+ task.add_done_callback(self._event_tasks.discard)
+ return PlainTextResponse("ok")
+
+ async def _dispatch_block_action(self, payload: dict[str, Any]) -> None:
+ """Resolve a clicked ask_question choice button and feed the answer
+ back through the normal message pipeline, exactly like a typed
+ reply."""
+ actions = payload.get("actions") or []
+ if not actions:
+ return
+ parsed = choice_ui.parse_action_id(actions[0].get("action_id") or "")
+ if parsed is None:
+ return
+ token, index = parsed
+ team_id = (payload.get("team") or {}).get("id") or ""
+ channel_id = (payload.get("channel") or {}).get("id")
+ client = await self._client_for(team_id)
+ clicker_id = (payload.get("user") or {}).get("id", "")
+ resolved = await choices.resolve_choice("slack", token, index, clicker_id)
+ if resolved.text is None:
+ if client and channel_id:
+ await client.chat_postEphemeral(
+ channel=channel_id,
+ user=clicker_id,
+ text=(_NOT_YOUR_QUESTION if resolved.refused else _EXPIRED_NOTICE),
+ )
+ return
+ option = resolved.text
+ message_ts = (payload.get("container") or {}).get("message_ts")
+ if client and channel_id and message_ts:
+ # `resolve_choice` already consumed the token, so the answer now
+ # exists only in this call. The ack is cosmetic and the turn is
+ # not: a `message_not_found`/`ratelimited` here must not abort
+ # the dispatch and lose the answer with nothing logged.
+ try:
+ await client.chat_update(
+ channel=channel_id,
+ ts=message_ts,
+ # The option text is model-authored, so it goes through
+ # the same escaper as every other Slack send — otherwise
+ # an option containing `<!channel>` pings the workspace,
+ # bypassing the mentionable_users allowlist.
+ text=self.localize_markup(f"✅ You answered: {option}"),
+ blocks=[],
+ )
+ except Exception:
+ logger.exception(
+ "Failed to acknowledge Slack choice click; continuing the turn"
+ )
+ if self._on_message_callback is None:
+ return
+ ctx = await self._context_from_block_action(payload, option)
+ if ctx is not None:
+ await self._on_message_callback(ctx, self)
+
+ async def _context_from_block_action(
+ self, payload: dict[str, Any], option: str
+ ) -> Optional[MessageContext]:
+ channel = (payload.get("channel") or {}).get("id")
+ team = (payload.get("team") or {}).get("id")
+ user = (payload.get("user") or {}).get("id")
+ ts = (payload.get("container") or {}).get("message_ts")
+ if not (channel and team and user and ts):
+ return None
+ event = {
+ "channel": channel,
+ "ts": ts,
+ "user": user,
+ "text": option,
+ "team": team,
+ "thread_ts": (payload.get("message") or {}).get("thread_ts"),
+ }
+ # Slack channel IDs are prefixed by kind; "D" is a 1:1 DM.
+ return await self._build_context(
+ event, team, bot_mentioned=True, is_dm=channel.startswith("D")
+ )
+
async def _dispatch_event(
self, event: dict[str, Any], team_id: Optional[str] = None
) -> None:
@@ -513,6 +612,35 @@ class SlackAdapter(WebhookAdapter):
blocks=_link_blocks(rendered, link_label, link_url),
)
+ @property
+ def max_choice_label_length(self) -> int:
+ return 75
+
+ @property
+ def supports_choice_buttons(self) -> bool:
+ return True
+
+ async def send_choice_buttons(
+ self,
+ channel_id: str,
+ text: str,
+ options: list[str],
+ token: str,
+ mentionable_users: tuple[tuple[str, str], ...] = (),
+ ) -> bool:
+ team, channel, thread_ts = _decode_target(channel_id)
+ client = await self._client_for(team)
+ if client is None:
+ return False
+ rendered = self.localize_markup(text)
+ await client.chat_postMessage(
+ channel=channel,
+ text=rendered,
+ thread_ts=thread_ts,
+ blocks=choice_ui.choice_blocks(rendered, token, options),
+ )
+ return True
+
async def send_file(self, channel_id: str, text: str, file: FileAttachment) -> None:
team, channel, thread_ts = _decode_target(channel_id)
client = await self._client_for(team)
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/slack/adapter_test.py b/autogpt_platform/backend/backend/copilot/bot/adapters/slack/adapter_test.py
index 7d4e470da1..d4c1ae9e75 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/slack/adapter_test.py
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/slack/adapter_test.py
@@ -8,6 +8,8 @@ import pytest
from slack_sdk.errors import SlackApiError
from backend.copilot.bot.adapters.base import FileAttachment
+from backend.copilot.bot.choices import ResolvedChoice
+from backend.copilot.bot.turn_stream import _clarification_message
from backend.data.bot_installs import BotInstallCredentials
from . import config
@@ -34,6 +36,7 @@ def _mock_client() -> MagicMock:
client.token = "xoxb-test"
client.chat_postMessage = AsyncMock(return_value={"ts": "111.222"})
client.chat_postEphemeral = AsyncMock()
+ client.chat_update = AsyncMock()
client.chat_getPermalink = AsyncMock(return_value={"permalink": "https://x/p"})
client.files_upload_v2 = AsyncMock()
client.conversations_info = AsyncMock(return_value={"ok": True})
@@ -482,7 +485,151 @@ class TestUninstall:
assert "T5" not in adapter._bot_user_ids
+class TestChoiceButtons:
+ @pytest.mark.asyncio
+ async def test_send_choice_buttons_posts_blocks(self, adapter):
+ sent = await adapter.send_choice_buttons(
+ "T1|C1|", "Which region?", ["US", "EU"], "abcdef012345"
+ )
+ assert sent is True
+ assert adapter.supports_choice_buttons is True
+ kwargs = adapter._clients["T1"].chat_postMessage.await_args.kwargs
+ assert kwargs["blocks"][1]["elements"][0]["action_id"] == "qans:abcdef012345:0"
+
+ @pytest.mark.asyncio
+ async def test_block_action_click_resolves_updates_message_and_dispatches(
+ self, adapter
+ ):
+ adapter._on_message_callback = AsyncMock()
+ payload = {
+ "type": "block_actions",
+ "team": {"id": "T1"},
+ "channel": {"id": "C1"},
+ "user": {"id": "U9"},
+ "container": {"type": "message", "message_ts": "111.222"},
+ "message": {"ts": "111.222"},
+ "actions": [{"action_id": "qans:abcdef012345:1"}],
+ }
+ with patch(
+ "backend.copilot.bot.adapters.slack.adapter.choices.resolve_choice",
+ new=AsyncMock(return_value=ResolvedChoice(text="EU")),
+ ):
+ await adapter._dispatch_block_action(payload)
+
+ update_kwargs = adapter._clients["T1"].chat_update.await_args.kwargs
+ assert update_kwargs["text"] == adapter.localize_markup("✅ You answered: EU")
+ adapter._on_message_callback.assert_awaited_once()
+ ctx, dispatched_adapter = adapter._on_message_callback.await_args.args
+ assert dispatched_adapter is adapter
+ assert ctx.text == "EU"
+ assert ctx.platform == "slack"
+
+ @pytest.mark.asyncio
+ async def test_choice_ack_escapes_model_authored_option_text(self, adapter):
+ # Options are LLM-authored, so an unescaped `<!channel>` in the ack
+ # reaches chat.update as a live mention and pings the workspace,
+ # bypassing the mentionable_users allowlist. Every other Slack send
+ # path in this file escapes; this one used to interpolate raw.
+ adapter._on_message_callback = AsyncMock()
+ payload = {
+ "type": "block_actions",
+ "team": {"id": "T1"},
+ "channel": {"id": "C1"},
+ "user": {"id": "U9"},
+ "container": {"type": "message", "message_ts": "111.222"},
+ "message": {"ts": "111.222"},
+ "actions": [{"action_id": "qans:abcdef012345:1"}],
+ }
+ with patch(
+ "backend.copilot.bot.adapters.slack.adapter.choices.resolve_choice",
+ new=AsyncMock(return_value=ResolvedChoice(text="<!channel> ping")),
+ ):
+ await adapter._dispatch_block_action(payload)
+
+ text = adapter._clients["T1"].chat_update.await_args.kwargs["text"]
+ assert "<!channel>" not in text
+
+ @pytest.mark.asyncio
+ async def test_choice_click_dispatches_even_if_the_ack_update_fails(self, adapter):
+ # chat_update runs *after* resolve_choice has GETDEL'd the token, so
+ # a SlackApiError here would lose the answer permanently, silently.
+ adapter._on_message_callback = AsyncMock()
+ adapter._clients["T1"].chat_update = AsyncMock(
+ side_effect=SlackApiError("nope", {"error": "message_not_found"})
+ )
+ payload = {
+ "type": "block_actions",
+ "team": {"id": "T1"},
+ "channel": {"id": "C1"},
+ "user": {"id": "U9"},
+ "container": {"type": "message", "message_ts": "111.222"},
+ "message": {"ts": "111.222"},
+ "actions": [{"action_id": "qans:abcdef012345:1"}],
+ }
+ with patch(
+ "backend.copilot.bot.adapters.slack.adapter.choices.resolve_choice",
+ new=AsyncMock(return_value=ResolvedChoice(text="EU")),
+ ):
+ await adapter._dispatch_block_action(payload)
+
+ adapter._on_message_callback.assert_awaited_once()
+
+ @pytest.mark.asyncio
+ async def test_expired_token_sends_ephemeral_notice_and_does_not_dispatch(
+ self, adapter
+ ):
+ adapter._on_message_callback = AsyncMock()
+ payload = {
+ "type": "block_actions",
+ "team": {"id": "T1"},
+ "channel": {"id": "C1"},
+ "user": {"id": "U9"},
+ "container": {"type": "message", "message_ts": "111.222"},
+ "actions": [{"action_id": "qans:abcdef012345:0"}],
+ }
+ with patch(
+ "backend.copilot.bot.adapters.slack.adapter.choices.resolve_choice",
+ new=AsyncMock(return_value=ResolvedChoice(text=None)),
+ ):
+ await adapter._dispatch_block_action(payload)
+
+ adapter._clients["T1"].chat_postEphemeral.assert_awaited_once()
+ assert (
+ "expired"
+ in adapter._clients["T1"].chat_postEphemeral.await_args.kwargs["text"]
+ )
+ adapter._clients["T1"].chat_update.assert_not_awaited()
+ adapter._on_message_callback.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_malformed_action_id_is_a_no_op(self, adapter):
+ adapter._on_message_callback = AsyncMock()
+ payload = {
+ "type": "block_actions",
+ "team": {"id": "T1"},
+ "channel": {"id": "C1"},
+ "actions": [{"action_id": "not-a-choice-action"}],
+ }
+ await adapter._dispatch_block_action(payload)
+ adapter._on_message_callback.assert_not_awaited()
+
+
class TestOutbound:
+ @pytest.mark.asyncio
+ async def test_send_message_delivers_clarification_question(self, adapter):
+ """SECRT-2604: an ask_question payload must reach Slack as a plain
+ text message with the numbered options intact, unmangled by the
+ adapter's real send path (chat_postMessage + mrkdwn conversion)."""
+ text = _clarification_message(
+ {"questions": [{"question": "Which region?", "options": ["US", "EU"]}]}
+ )
+ await adapter.send_message("T1|C1|", text)
+ sent = adapter._clients["T1"].chat_postMessage.await_args.kwargs["text"]
+ assert "Which region?" in sent
+ assert "1. US" in sent
+ assert "2. EU" in sent
+ assert "Reply with a number" in sent
+
@pytest.mark.asyncio
async def test_send_message_renders_mrkdwn_and_mentions_and_threads(self, adapter):
await adapter.send_message("T1|C1|1.2", "**hi** @Bently", (("Bently", "U9"),))
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/slack/app-manifest.yaml b/autogpt_platform/backend/backend/copilot/bot/adapters/slack/app-manifest.yaml
index 80f1f6c76e..6691f3cfa0 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/slack/app-manifest.yaml
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/slack/app-manifest.yaml
@@ -60,6 +60,9 @@ oauth_config:
- team:read # workspace metadata for SERVER linking
settings:
+ interactivity:
+ is_enabled: true
+ request_url: <BASE_URL>/api/copilot-webhooks/slack/interactive
event_subscriptions:
request_url: <BASE_URL>/api/copilot-webhooks/slack/events
bot_events:
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/slack/choice_ui.py b/autogpt_platform/backend/backend/copilot/bot/adapters/slack/choice_ui.py
new file mode 100644
index 0000000000..a37c72eba3
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/slack/choice_ui.py
@@ -0,0 +1,39 @@
+"""Block Kit builder + action-id codec for native Slack choice buttons.
+
+Kept separate from ``adapter.py`` (already large) — the dispatch side (which
+needs the adapter's client/context helpers) stays there; this module only
+builds outbound blocks and parses the click payload's action_id.
+"""
+
+import re
+from typing import Any
+
+_ACTION_ID_RE = re.compile(r"^qans:([0-9a-f]{12}):(\d+)$")
+
+
+def choice_blocks(text: str, token: str, options: list[str]) -> list[dict[str, Any]]:
+ return [
+ {"type": "section", "text": {"type": "mrkdwn", "text": text}},
+ {
+ "type": "actions",
+ "elements": [
+ {
+ "type": "button",
+ "text": {"type": "plain_text", "text": option[:75]},
+ "action_id": _action_id(token, index),
+ }
+ for index, option in enumerate(options)
+ ],
+ },
+ ]
+
+
+def _action_id(token: str, index: int) -> str:
+ return f"qans:{token}:{index}"
+
+
+def parse_action_id(action_id: str) -> tuple[str, int] | None:
+ match = _ACTION_ID_RE.match(action_id)
+ if not match:
+ return None
+ return match.group(1), int(match.group(2))
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/slack/choice_ui_test.py b/autogpt_platform/backend/backend/copilot/bot/adapters/slack/choice_ui_test.py
new file mode 100644
index 0000000000..28ae68b75b
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/slack/choice_ui_test.py
@@ -0,0 +1,32 @@
+"""Tests for the Slack choice-button block builder and action-id codec."""
+
+from .choice_ui import choice_blocks, parse_action_id
+
+
+class TestChoiceBlocks:
+ def test_builds_a_button_per_option_with_encoded_action_ids(self):
+ blocks = choice_blocks("Which region?", "abcdef012345", ["US", "EU"])
+ assert blocks[0] == {
+ "type": "section",
+ "text": {"type": "mrkdwn", "text": "Which region?"},
+ }
+ elements = blocks[1]["elements"]
+ assert [e["text"]["text"] for e in elements] == ["US", "EU"]
+ assert elements[0]["action_id"] == "qans:abcdef012345:0"
+ assert elements[1]["action_id"] == "qans:abcdef012345:1"
+
+ def test_button_text_truncates_to_slack_cap(self):
+ blocks = choice_blocks("Q?", "abcdef012345", ["x" * 200])
+ assert len(blocks[1]["elements"][0]["text"]["text"]) == 75
+
+
+class TestParseActionId:
+ def test_round_trips_token_and_index(self):
+ blocks = choice_blocks("Q?", "abcdef012345", ["US", "EU"])
+ action_id = blocks[1]["elements"][1]["action_id"]
+ assert parse_action_id(action_id) == ("abcdef012345", 1)
+
+ def test_rejects_malformed_action_id(self):
+ assert parse_action_id("not-a-choice-action") is None
+ assert parse_action_id("qans:short:0") is None
+ assert parse_action_id("qans:abcdef012345:notanumber") is None
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/teams/adapter.py b/autogpt_platform/backend/backend/copilot/bot/adapters/teams/adapter.py
index b5757c44b1..f40ecf5a57 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/teams/adapter.py
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/teams/adapter.py
@@ -28,6 +28,7 @@ import httpx
from fastapi import FastAPI, Request, Response
from fastapi.responses import JSONResponse, PlainTextResponse
+from backend.copilot.bot import choices
from backend.copilot.bot.adapters.base import (
ChannelInfo,
ChannelType,
@@ -43,13 +44,17 @@ from backend.copilot.bot.config import MAX_INBOUND_ATTACHMENTS
from backend.copilot.bot.text import iter_chunks, resolve_mentions
from backend.data.redis_client import get_redis_async
-from . import auth, commands, config
+from . import auth, choice_ui, commands, config
from .api_client import TeamsApiError, TeamsClient
from .text import mention_entities, mention_token, to_teams_markdown
logger = logging.getLogger(__name__)
MESSAGES_PATH = "/api/copilot-webhooks/teams/messages"
+_EXPIRED_NOTICE = "This question has expired — type your answer instead."
+_NOT_YOUR_QUESTION = (
+ "This question was for someone else — they still need to answer it."
+)
# Conversations we keep a learned serviceUrl for. Evicting one is cheap:
# the next reply falls back to the default host until it is relearned.
@@ -197,6 +202,15 @@ class TeamsAdapter(WebhookAdapter):
if _is_own_id((activity.get("from") or {}).get("id"), _configured_bot_ids()):
return # Our own echo.
+ # An Action.Submit click on a choice card arrives as an ordinary
+ # message activity carrying `value` (Teams' classic card-action
+ # flow, not the newer Universal Actions invoke) -- not a command or
+ # typed text.
+ parsed_choice = choice_ui.parse_choice_value(activity.get("value"))
+ if parsed_choice is not None:
+ await self._dispatch_choice_click(activity, *parsed_choice)
+ return
+
command = commands.parse_command(_activity_text(activity))
if command is not None:
try:
@@ -215,6 +229,61 @@ class TeamsAdapter(WebhookAdapter):
except Exception:
logger.exception("Teams activity handler failed")
+ async def _dispatch_choice_click(
+ self, activity: dict[str, Any], token: str, index: int
+ ) -> None:
+ """Resolve a clicked ask_question choice button and feed the answer
+ back through the normal message pipeline, exactly like a typed
+ reply.
+
+ Teams' classic card actions have no update-the-original-card API
+ wired here (unlike the other adapters' edit-in-place ack) — a short
+ confirmation is posted as a new message instead, then the answer
+ flows into a normal turn.
+ """
+ conversation_id = (activity.get("conversation") or {}).get("id")
+ if not conversation_id:
+ return
+ clicker_id = str((activity.get("from") or {}).get("id", ""))
+ resolved = await choices.resolve_choice("teams", token, index, clicker_id)
+ if resolved.text is None:
+ await self._post(
+ conversation_id,
+ {
+ "type": "message",
+ "text": (
+ _NOT_YOUR_QUESTION if resolved.refused else _EXPIRED_NOTICE
+ ),
+ },
+ )
+ return
+ option = resolved.text
+ # The token is already consumed, so the answer exists only here. The
+ # ack is cosmetic; a Connector error must not cost the user the turn.
+ try:
+ await self._post(
+ conversation_id,
+ {
+ "type": "message",
+ "text": self.localize_markup(f"✅ You answered: {option}"),
+ },
+ )
+ except Exception:
+ logger.exception(
+ "Failed to acknowledge Teams choice click; continuing the turn"
+ )
+ if self._on_message_callback is None:
+ return
+ # `bot_mentioned=True` explicitly, as every other adapter does on a
+ # click. An Action.Submit carries no mention entities, so deriving it
+ # from the activity yields False, and in a *channel* the turn is then
+ # dropped by the handler's `if not ctx.bot_mentioned: return` — after
+ # the ack above has already told the user their answer was accepted.
+ ctx = await self._build_context({**activity, "text": option})
+ if ctx is not None:
+ ctx.bot_mentioned = True
+ await self._on_message_callback(ctx, self)
+
async def _is_duplicate_activity(self, activity: dict[str, Any]) -> bool:
"""Drop redeliveries — first delivery of an activity id wins.
@@ -345,6 +414,37 @@ class TeamsAdapter(WebhookAdapter):
}
await self._post(channel_id, activity)
+ @property
+ def max_choice_label_length(self) -> int:
+ return 60
+
+ @property
+ def max_choice_options(self) -> int:
+ # An Adaptive Card renders about six actions before Teams collapses
+ # the rest into an overflow the user can miss entirely.
+ return 6
+
+ @property
+ def supports_choice_buttons(self) -> bool:
+ return True
+
+ async def send_choice_buttons(
+ self,
+ channel_id: str,
+ text: str,
+ options: list[str],
+ token: str,
+ mentionable_users: tuple[tuple[str, str], ...] = (),
+ ) -> bool:
+ activity = {
+ "type": "message",
+ "attachments": [
+ choice_ui.choice_card(self.localize_markup(text), token, options)
+ ],
+ }
+ await self._post(channel_id, activity)
+ return True
+
async def send_file(self, channel_id: str, text: str, file: FileAttachment) -> None:
"""Inline a small image; degrade anything else to a note.
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/teams/adapter_test.py b/autogpt_platform/backend/backend/copilot/bot/adapters/teams/adapter_test.py
index 6ec28e7cba..d616984e16 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/teams/adapter_test.py
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/teams/adapter_test.py
@@ -15,6 +15,8 @@ from backend.copilot.bot.adapters.teams.adapter import (
_inbound_files,
)
from backend.copilot.bot.adapters.teams.text import mention_entities, to_teams_markdown
+from backend.copilot.bot.choices import ResolvedChoice
+from backend.copilot.bot.turn_stream import _clarification_message
from backend.util.settings import AppEnvironment
_APP_ID = "11111111-2222-3333-4444-555555555555"
@@ -387,6 +389,160 @@ async def test_create_thread_rules(app_id, conversation_id, expected):
# ── Sending ────────────────────────────────────────────────────────
+async def test_send_message_delivers_clarification_question(app_id):
+ """SECRT-2604: an ask_question payload must reach Teams as a plain text
+ activity with the numbered options intact, unmangled by the adapter's
+ real send path (send_activity + Teams markdown downgrade)."""
+ adapter = TeamsAdapter(MagicMock())
+ adapter._client.send_activity = AsyncMock(return_value="activity-9")
+ text = _clarification_message(
+ {"questions": [{"question": "Which region?", "options": ["US", "EU"]}]}
+ )
+ await adapter.send_message("a:chat", text)
+ activity = adapter._client.send_activity.await_args.args[2]
+ assert "Which region?" in activity["text"]
+ assert "1. US" in activity["text"]
+ assert "2. EU" in activity["text"]
+ assert "Reply with a number" in activity["text"]
+
+
+async def test_send_choice_buttons_posts_adaptive_card(app_id):
+ adapter = TeamsAdapter(MagicMock())
+ adapter._client.send_activity = AsyncMock(return_value="activity-9")
+
+ sent = await adapter.send_choice_buttons(
+ "a:chat", "Which region?", ["US", "EU"], "abcdef012345"
+ )
+
+ assert sent is True
+ assert adapter.supports_choice_buttons is True
+ activity = adapter._client.send_activity.await_args.args[2]
+ card = activity["attachments"][0]["content"]
+ assert [a["title"] for a in card["actions"]] == ["US", "EU"]
+ assert card["actions"][0]["data"] == {
+ "qans_token": "abcdef012345",
+ "qans_index": 0,
+ }
+
+
+async def test_choice_click_resolves_posts_confirmation_and_dispatches(app_id):
+ api = MagicMock()
+ adapter = TeamsAdapter(api)
+ adapter._client.send_activity = AsyncMock(return_value="activity-9")
+ adapter._on_message_callback = AsyncMock()
+ activity = _activity(
+ text="",
+ value={"qans_token": "abcdef012345", "qans_index": 1},
+ )
+
+ with patch(
+ "backend.copilot.bot.adapters.teams.adapter.choices.resolve_choice",
+ new=AsyncMock(return_value=ResolvedChoice(text="EU")),
+ ):
+ await adapter._dispatch_activity(activity)
+
+ posted = [c.args[2] for c in adapter._client.send_activity.await_args_list]
+ assert any("You answered: EU" in a.get("text", "") for a in posted)
+ adapter._on_message_callback.assert_awaited_once()
+ ctx, dispatched_adapter = adapter._on_message_callback.await_args.args
+ assert dispatched_adapter is adapter
+ assert ctx.text == "EU"
+ assert ctx.platform == "teams"
+
+
+async def test_choice_click_in_a_channel_is_dispatched_as_mentioned(app_id):
+ """An Action.Submit carries no mention entities, so deriving
+ `bot_mentioned` from the activity yields False — and in a *channel* the
+ handler's `if not ctx.bot_mentioned: return` then silently drops the
+ turn, after the ack has already told the user their answer was accepted.
+ Every other adapter forces it True on a click.
+ """
+ api = MagicMock()
+ adapter = TeamsAdapter(api)
+ adapter._client.send_activity = AsyncMock(return_value="activity-9")
+ adapter._on_message_callback = AsyncMock()
+ activity = _activity(
+ text="",
+ value={"qans_token": "abcdef012345", "qans_index": 1},
+ conversation={"id": "19:[email protected]", "conversationType": "channel"},
+ channelData={"team": {"id": "19:[email protected]", "name": "Eng"}},
+ )
+
+ with patch(
+ "backend.copilot.bot.adapters.teams.adapter.choices.resolve_choice",
+ new=AsyncMock(return_value=ResolvedChoice(text="EU")),
+ ):
+ await adapter._dispatch_activity(activity)
+
+ adapter._on_message_callback.assert_awaited_once()
+ ctx, _ = adapter._on_message_callback.await_args.args
+ assert ctx.bot_mentioned is True
+ assert ctx.text == "EU"
+
+
+async def test_choice_click_dispatches_even_if_the_ack_post_fails(app_id):
+ # The token is consumed before the ack, so a Connector error here would
+ # otherwise lose the answer entirely.
+ api = MagicMock()
+ adapter = TeamsAdapter(api)
+ adapter._client.send_activity = AsyncMock(side_effect=RuntimeError("connector"))
+ adapter._on_message_callback = AsyncMock()
+ activity = _activity(text="", value={"qans_token": "abcdef012345", "qans_index": 1})
+
+ with patch(
+ "backend.copilot.bot.adapters.teams.adapter.choices.resolve_choice",
+ new=AsyncMock(return_value=ResolvedChoice(text="EU")),
+ ):
+ await adapter._dispatch_activity(activity)
+
+ adapter._on_message_callback.assert_awaited_once()
+
+
+async def test_teams_caps_choice_options_below_the_shared_default(app_id):
+ # An Adaptive Card renders ~6 actions; the rest silently vanish.
+ adapter = TeamsAdapter(MagicMock())
+ assert adapter.max_choice_options == 6
+
+
+async def test_choice_click_expired_token_posts_notice_and_does_not_dispatch(app_id):
+ api = MagicMock()
+ adapter = TeamsAdapter(api)
+ adapter._client.send_activity = AsyncMock(return_value="activity-9")
+ adapter._on_message_callback = AsyncMock()
+ activity = _activity(text="", value={"qans_token": "abcdef012345", "qans_index": 0})
+
+ with patch(
+ "backend.copilot.bot.adapters.teams.adapter.choices.resolve_choice",
+ new=AsyncMock(return_value=ResolvedChoice(text=None)),
+ ):
+ await adapter._dispatch_activity(activity)
+
+ posted = [c.args[2] for c in adapter._client.send_activity.await_args_list]
+ assert any("expired" in a.get("text", "") for a in posted)
+ adapter._on_message_callback.assert_not_awaited()
+
+
+async def test_activity_without_choice_value_is_not_treated_as_a_click(app_id):
+ api = MagicMock()
+ adapter = TeamsAdapter(api)
+ adapter._on_message_callback = AsyncMock()
+ activity = _activity(text="hello") # no "value" field at all
+
+ await adapter._dispatch_activity(activity)
+
+ adapter._on_message_callback.assert_awaited_once()
+ ctx, _ = adapter._on_message_callback.await_args.args
+ assert ctx.text == "hello"
+
+
@pytest.mark.asyncio
async def test_send_message_posts_markdown_activity(app_id):
adapter = TeamsAdapter(MagicMock())
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/teams/choice_ui.py b/autogpt_platform/backend/backend/copilot/bot/adapters/teams/choice_ui.py
new file mode 100644
index 0000000000..13a4574bb8
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/teams/choice_ui.py
@@ -0,0 +1,50 @@
+"""Adaptive Card builder + submit-data codec for native Teams choice buttons.
+
+Teams delivers an ``Action.Submit`` click as an ordinary inbound "message"
+activity carrying ``value`` (not a separate invoke — that's the newer
+Universal Actions schema this bot doesn't use), so the dispatch side stays a
+small branch in ``adapter.py``'s existing message path; this module only
+builds the outbound card and parses that ``value`` payload.
+"""
+
+from typing import Any
+
+
+def choice_card(text: str, token: str, options: list[str]) -> dict[str, Any]:
+ """A minimal Adaptive Card carrying one Action.Submit button per option.
+
+ Pinned to schema 1.2, matching ``_link_card`` — the highest version every
+ Teams client, including mobile, renders reliably.
+ """
+ return {
+ "contentType": "application/vnd.microsoft.card.adaptive",
+ "content": {
+ "type": "AdaptiveCard",
+ "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
+ "version": "1.2",
+ "body": [{"type": "TextBlock", "text": text, "wrap": True}],
+ "actions": [
+ {
+ "type": "Action.Submit",
+ "title": option[:60],
+ "data": {"qans_token": token, "qans_index": index},
+ }
+ for index, option in enumerate(options)
+ ],
+ },
+ }
+
+
+def parse_choice_value(value: Any) -> tuple[str, int] | None:
+ """Extract (token, index) from an Action.Submit activity's ``value``."""
+ if not isinstance(value, dict):
+ return None
+ token = value.get("qans_token")
+ index = value.get("qans_index")
+ if not isinstance(token, str) or not token:
+ return None
+ # bool is an int subclass in Python -- exclude it explicitly rather than
+ # let a stray True/False masquerade as index 1/0.
+ if isinstance(index, bool) or not isinstance(index, int):
+ return None
+ return token, index
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/teams/choice_ui_test.py b/autogpt_platform/backend/backend/copilot/bot/adapters/teams/choice_ui_test.py
new file mode 100644
index 0000000000..a16c86cb05
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/teams/choice_ui_test.py
@@ -0,0 +1,39 @@
+"""Tests for the Teams choice-card builder and Action.Submit value codec."""
+
+from .choice_ui import choice_card, parse_choice_value
+
+
+class TestChoiceCard:
+ def test_one_action_submit_per_option(self):
+ card = choice_card("Which region?", "abcdef012345", ["US", "EU"])
+ content = card["content"]
+ assert content["body"][0]["text"] == "Which region?"
+ actions = content["actions"]
+ assert [a["title"] for a in actions] == ["US", "EU"]
+ assert actions[0]["data"] == {"qans_token": "abcdef012345", "qans_index": 0}
+ assert actions[1]["data"] == {"qans_token": "abcdef012345", "qans_index": 1}
+
+ def test_title_truncates_to_card_button_cap(self):
+ card = choice_card("Q?", "abcdef012345", ["x" * 200])
+ assert len(card["content"]["actions"][0]["title"]) == 60
+
+
+class TestParseChoiceValue:
+ def test_round_trips_token_and_index(self):
+ card = choice_card("Q?", "abcdef012345", ["US", "EU"])
+ value = card["content"]["actions"][1]["data"]
+ assert parse_choice_value(value) == ("abcdef012345", 1)
+
+ def test_non_dict_value_returns_none(self):
+ assert parse_choice_value(None) is None
+ assert parse_choice_value("not a dict") is None
+
+ def test_missing_or_wrong_typed_fields_return_none(self):
+ assert parse_choice_value({}) is None
+ assert parse_choice_value({"qans_token": "", "qans_index": 0}) is None
+ assert parse_choice_value({"qans_token": "tok", "qans_index": "0"}) is None
+ assert parse_choice_value({"qans_token": "tok", "qans_index": None}) is None
+
+ def test_bool_index_is_rejected(self):
+ # bool is an int subclass -- must not silently pass as index 0/1.
+ assert parse_choice_value({"qans_token": "tok", "qans_index": True}) is None
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/adapter.py b/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/adapter.py
index eb1fc8a290..0c38a8441e 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/adapter.py
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/adapter.py
@@ -23,6 +23,7 @@ from typing import Any, Optional
from fastapi import FastAPI, Request, Response
from fastapi.responses import PlainTextResponse
+from backend.copilot.bot import choices
from backend.copilot.bot.adapters.base import (
ChannelInfo,
ChannelType,
@@ -40,7 +41,7 @@ from backend.copilot.bot.bot_backend import BotBackend
from backend.copilot.bot.config import MAX_INBOUND_ATTACHMENTS
from backend.copilot.bot.text import iter_chunks, resolve_mentions
-from . import commands, config
+from . import choice_ui, commands, config
from .api_client import TelegramClient
from .targets import decode_target as _decode_target
from .targets import encode_target as _encode_target
@@ -48,6 +49,11 @@ from .text import to_html
logger = logging.getLogger(__name__)
+_EXPIRED_NOTICE = "This question has expired — type your answer instead."
+_NOT_YOUR_QUESTION = (
+ "This question was for someone else — they still need to answer it."
+)
+
UPDATES_PATH = "/api/copilot-webhooks/telegram/updates"
# Telegram chat IDs are integers of any length; groups/supergroups are
@@ -140,6 +146,10 @@ class TelegramAdapter(WebhookAdapter):
if membership:
self._track_membership_change(membership)
return
+ callback_query = update.get("callback_query")
+ if callback_query:
+ await self._dispatch_callback_query(callback_query)
+ return
message = update.get("message")
if not message:
return # Edits, reactions, other member updates — not conversation input.
@@ -180,6 +190,60 @@ class TelegramAdapter(WebhookAdapter):
except Exception:
logger.debug("Telegram reaction ack failed", exc_info=True)
+ async def _dispatch_callback_query(self, callback_query: dict[str, Any]) -> None:
+ """Resolve a clicked ask_question choice button and feed the answer
+ back through the normal message pipeline, exactly like a typed
+ reply."""
+ query_id = callback_query.get("id")
+ parsed = choice_ui.parse_callback_data(callback_query.get("data") or "")
+ if parsed is None:
+ if query_id:
+ await self._answer_callback_query(query_id)
+ return
+ token, index = parsed
+ clicker_id = str((callback_query.get("from") or {}).get("id", ""))
+ resolved = await choices.resolve_choice("telegram", token, index, clicker_id)
+ if resolved.text is None:
+ if query_id:
+ await self._answer_callback_query(
+ query_id,
+ text=(_NOT_YOUR_QUESTION if resolved.refused else _EXPIRED_NOTICE),
+ show_alert=True,
+ )
+ return
+ option = resolved.text
+ if query_id:
+ await self._answer_callback_query(query_id)
+ message = callback_query.get("message") or {}
+ chat_id = str((message.get("chat") or {}).get("id", ""))
+ message_id = message.get("message_id")
+ if chat_id and message_id is not None:
+ try:
+ await self._client.call(
+ "editMessageText",
+ chat_id=chat_id,
+ message_id=message_id,
+ text=f"✅ You answered: {option}",
+ )
+ except Exception:
+ logger.debug(
+ "Telegram editMessageText after choice click failed",
+ exc_info=True,
+ )
+ if self._on_message_callback is None:
+ return
+ ctx = _context_from_callback_query(callback_query, option)
+ if ctx is not None:
+ await self._on_message_callback(ctx, self)
+
+ async def _answer_callback_query(self, query_id: str, **kwargs: Any) -> None:
+ try:
+ await self._client.call(
+ "answerCallbackQuery", callback_query_id=query_id, **kwargs
+ )
+ except Exception:
+ logger.debug("Telegram answerCallbackQuery failed", exc_info=True)
+
def _track_membership_change(self, membership: dict[str, Any]) -> None:
"""Keep the admin server roster current: the bot being added to /
removed from a group arrives as a my_chat_member update."""
@@ -382,6 +446,33 @@ class TelegramAdapter(WebhookAdapter):
params["text"] += html.escape(f"\n\n{link_label}: {link_url}")
await self._client.call("sendMessage", **params)
+ @property
+ def max_choice_label_length(self) -> int:
+ return 64
+
+ @property
+ def supports_choice_buttons(self) -> bool:
+ return True
+
+ async def send_choice_buttons(
+ self,
+ channel_id: str,
+ text: str,
+ options: list[str],
+ token: str,
+ mentionable_users: tuple[tuple[str, str], ...] = (),
+ ) -> bool:
+ chat_id, thread_id = _decode_target(channel_id)
+ await self._client.call(
+ "sendMessage",
+ chat_id=chat_id,
+ text=self.localize_markup(text),
+ parse_mode="HTML",
+ message_thread_id=thread_id,
+ reply_markup=choice_ui.choice_keyboard(token, options),
+ )
+ return True
+
async def send_file(self, channel_id: str, text: str, file: FileAttachment) -> None:
chat_id, thread_id = _decode_target(channel_id)
# Images render inline as photos; everything else arrives as a document.
@@ -554,6 +645,31 @@ def _verify_secret(request: Request, _body: bytes) -> bool:
return bool(expected) and hmac.compare_digest(provided, expected)
+def _context_from_callback_query(
+ callback_query: dict[str, Any], option: str
+) -> Optional[MessageContext]:
+ message = callback_query.get("message") or {}
+ chat = message.get("chat") or {}
+ chat_id = str(chat.get("id", ""))
+ sender = callback_query.get("from") or {}
+ user_id = sender.get("id")
+ if not chat_id or user_id is None:
+ return None
+ is_private = chat.get("type") == "private"
+ target_id = _encode_target(chat_id, message.get("message_thread_id"))
+ return MessageContext(
+ platform="telegram",
+ channel_type="dm" if is_private else "channel",
+ server_id=None if is_private else chat_id,
+ channel_id=target_id,
+ message_id=str(message.get("message_id", "")),
+ user_id=str(user_id),
+ username=sender.get("username") or sender.get("first_name") or "unknown",
+ text=option,
+ bot_mentioned=True,
+ )
+
+
def _collect_mentionable_users(message: dict[str, Any]) -> tuple[tuple[str, str], ...]:
# text_mention entities carry full user objects (users without a public
# @username); those are the only inbound mentions with a numeric id we
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/adapter_test.py b/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/adapter_test.py
index 10d35c1681..ffa4b33451 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/adapter_test.py
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/adapter_test.py
@@ -8,6 +8,8 @@ import pytest
from backend.copilot.bot.adapters.base import FileAttachment, StreamDraftOutcome
from backend.copilot.bot.adapters.telegram.api_client import TelegramAPIError
+from backend.copilot.bot.choices import ResolvedChoice
+from backend.copilot.bot.turn_stream import _clarification_message
from .adapter import (
TelegramAdapter,
@@ -258,6 +260,22 @@ class TestAnalytics:
class TestOutbound:
+ @pytest.mark.asyncio
+ async def test_send_message_delivers_clarification_question(self):
+ """SECRT-2604: an ask_question payload must reach Telegram as a
+ plain text message with the numbered options intact, unmangled by
+ the adapter's real send path (sendMessage + HTML conversion)."""
+ a = _adapter()
+ text = _clarification_message(
+ {"questions": [{"question": "Which region?", "options": ["US", "EU"]}]}
+ )
+ await a.send_message("-100555|7", text)
+ sent = a._client.call.call_args.kwargs["text"]
+ assert "Which region?" in sent
+ assert "1. US" in sent
+ assert "2. EU" in sent
+ assert "Reply with a number" in sent
+
@pytest.mark.asyncio
async def test_send_message_renders_html_and_threads(self):
a = _adapter()
@@ -269,6 +287,19 @@ class TestOutbound:
assert kwargs["parse_mode"] == "HTML"
assert kwargs["text"] == "<b>bold</b> &amp; plain"
+ @pytest.mark.asyncio
+ async def test_send_choice_buttons_sends_inline_keyboard(self):
+ a = _adapter()
+ sent = await a.send_choice_buttons(
+ "-100555|7", "Which region?", ["US", "EU"], "abcdef012345"
+ )
+ assert sent is True
+ assert a.supports_choice_buttons is True
+ kwargs = a._client.call.call_args.kwargs
+ assert kwargs["chat_id"] == "-100555"
+ rows = kwargs["reply_markup"]["inline_keyboard"]
+ assert rows[0][0]["callback_data"] == "qans:abcdef012345:0"
+
@pytest.mark.asyncio
async def test_send_link_prefers_login_url_for_https(self):
# Telegram attaches a signed identity when the user taps a login_url
@@ -461,3 +492,74 @@ class TestProactiveChunking:
html.unescape(re.sub(r"<[^>]+>", "", c.kwargs["text"])) for c in calls
)
assert joined.count("Tom") == 200 # nothing dropped across the chunks
+
+
+class TestChoiceCallbackQuery:
+ @pytest.mark.asyncio
+ async def test_click_resolves_updates_message_and_dispatches(self):
+ a = _adapter()
+ a._on_message_callback = AsyncMock()
+ callback_query = {
+ "id": "cbq1",
+ "data": "qans:abcdef012345:1",
+ "from": {"id": 42, "username": "bently"},
+ "message": {
+ "message_id": 9,
+ "chat": {"id": 42, "type": "private"},
+ },
+ }
+ with patch(
+ f"{_ADAPTER}.choices.resolve_choice",
+ new=AsyncMock(return_value=ResolvedChoice(text="EU")),
+ ):
+ await a._dispatch_callback_query(callback_query)
+
+ calls = {c.args[0]: c.kwargs for c in a._client.call.call_args_list}
+ assert calls["answerCallbackQuery"]["callback_query_id"] == "cbq1"
+ assert calls["editMessageText"]["text"] == "✅ You answered: EU"
+ a._on_message_callback.assert_awaited_once()
+ ctx, dispatched_adapter = a._on_message_callback.await_args.args
+ assert dispatched_adapter is a
+ assert ctx.text == "EU"
+ assert ctx.platform == "telegram"
+ assert ctx.channel_type == "dm"
+
+ @pytest.mark.asyncio
+ async def test_expired_token_shows_alert_and_does_not_dispatch(self):
+ a = _adapter()
+ a._on_message_callback = AsyncMock()
+ callback_query = {
+ "id": "cbq1",
+ "data": "qans:abcdef012345:0",
+ "from": {"id": 42},
+ "message": {"message_id": 9, "chat": {"id": 42, "type": "private"}},
+ }
+ with patch(
+ f"{_ADAPTER}.choices.resolve_choice",
+ new=AsyncMock(return_value=ResolvedChoice(text=None)),
+ ):
+ await a._dispatch_callback_query(callback_query)
+
+ answer_calls = [
+ c
+ for c in a._client.call.call_args_list
+ if c.args == ("answerCallbackQuery",)
+ ]
+ assert len(answer_calls) == 1
+ assert answer_calls[0].kwargs["show_alert"] is True
+ edit_calls = [
+ c for c in a._client.call.call_args_list if c.args == ("editMessageText",)
+ ]
+ assert edit_calls == []
+ a._on_message_callback.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_malformed_callback_data_acks_without_dispatching(self):
+ a = _adapter()
+ a._on_message_callback = AsyncMock()
+ callback_query = {"id": "cbq1", "data": "not-a-choice-callback"}
+
+ await a._dispatch_callback_query(callback_query)
+
+ assert a._client.call.call_args.args == ("answerCallbackQuery",)
+ a._on_message_callback.assert_not_awaited()
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/choice_ui.py b/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/choice_ui.py
new file mode 100644
index 0000000000..c7fca0f722
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/choice_ui.py
@@ -0,0 +1,34 @@
+"""Inline-keyboard builder + callback_data codec for native Telegram choice
+buttons.
+
+Kept separate from ``adapter.py`` (already large) — the dispatch side (which
+needs the adapter's client/context helpers) stays there; this module only
+builds the outbound keyboard and parses the click payload's callback_data.
+"""
+
+import re
+from typing import Any
+
+# callback_data caps at 64 bytes on the wire; "qans:" + 12 hex chars + ":" +
+# a small index stays well under that.
+_CALLBACK_RE = re.compile(r"^qans:([0-9a-f]{12}):(\d+)$")
+
+
+def choice_keyboard(token: str, options: list[str]) -> dict[str, Any]:
+ return {
+ "inline_keyboard": [
+ [{"text": option[:64], "callback_data": _callback_data(token, index)}]
+ for index, option in enumerate(options)
+ ]
+ }
+
+
+def _callback_data(token: str, index: int) -> str:
+ return f"qans:{token}:{index}"
+
+
+def parse_callback_data(data: str) -> tuple[str, int] | None:
+ match = _CALLBACK_RE.match(data)
+ if not match:
+ return None
+ return match.group(1), int(match.group(2))
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/choice_ui_test.py b/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/choice_ui_test.py
new file mode 100644
index 0000000000..c3d3a4cd13
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/choice_ui_test.py
@@ -0,0 +1,30 @@
+"""Tests for the Telegram choice-button keyboard builder and callback codec."""
+
+from .choice_ui import choice_keyboard, parse_callback_data
+
+
+class TestChoiceKeyboard:
+ def test_builds_one_button_per_row_with_encoded_callback_data(self):
+ keyboard = choice_keyboard("abcdef012345", ["US", "EU"])
+ rows = keyboard["inline_keyboard"]
+ assert [row[0]["text"] for row in rows] == ["US", "EU"]
+ assert rows[0][0]["callback_data"] == "qans:abcdef012345:0"
+ assert rows[1][0]["callback_data"] == "qans:abcdef012345:1"
+
+ def test_button_text_truncates_and_callback_data_stays_under_64_bytes(self):
+ keyboard = choice_keyboard("abcdef012345", ["x" * 200])
+ button = keyboard["inline_keyboard"][0][0]
+ assert len(button["text"]) == 64
+ assert len(button["callback_data"].encode()) <= 64
+
+
+class TestParseCallbackData:
+ def test_round_trips_token_and_index(self):
+ keyboard = choice_keyboard("abcdef012345", ["US", "EU"])
+ data = keyboard["inline_keyboard"][1][0]["callback_data"]
+ assert parse_callback_data(data) == ("abcdef012345", 1)
+
+ def test_rejects_malformed_callback_data(self):
+ assert parse_callback_data("not-a-choice-callback") is None
+ assert parse_callback_data("qans:short:0") is None
+ assert parse_callback_data("qans:abcdef012345:notanumber") is None
diff --git a/autogpt_platform/backend/backend/copilot/bot/bot_backend.py b/autogpt_platform/backend/backend/copilot/bot/bot_backend.py
index 960cdd56e4..18328f1661 100644
--- a/autogpt_platform/backend/backend/copilot/bot/bot_backend.py
+++ b/autogpt_platform/backend/backend/copilot/bot/bot_backend.py
@@ -123,6 +123,16 @@ SetupDroppedCallback = Callable[
Awaitable[None],
]
+# Fired when AutoPilot's ask_question tool asks the user something and pauses
+# the turn for an answer. Args: (session_id, clarification_output, tool_name).
+# Without this, the question is parked on the session for the web "Needs You"
+# UI but never reaches a bot conversation, so the user sees the turn end with
+# nothing to reply to.
+ClarificationNeededCallback = Callable[
+ [str, dict[str, Any], str | None],
+ Awaitable[None],
+]
+
class BotBackend:
"""Bot-side linking + chat operations, routed over cluster-internal RPC."""
@@ -439,6 +449,7 @@ class BotBackend:
on_session_id: Optional[Callable[[str], Awaitable[None]]] = None,
on_setup_required: SetupRequiredCallback | None = None,
on_setup_dropped: SetupDroppedCallback | None = None,
+ on_clarification_needed: ClarificationNeededCallback | None = None,
) -> AsyncGenerator[str, None]:
"""Start a copilot turn and yield text deltas from the stream.
@@ -478,6 +489,7 @@ class BotBackend:
setup_notified = False
setup_drop_notified = False
+ clarification_notified = False
# Track which text block each delta belongs to. Otto emits text in
# separate blocks around tool calls / reasoning (each with its own id);
# the frontend renders them as distinct parts, but here we concatenate
@@ -532,6 +544,18 @@ class BotBackend:
# prompt that will never arrive.
setup_drop_notified = True
await on_setup_dropped(handle.session_id, chunk.toolName)
+ clarification_output = _extract_clarification_needed(chunk.output)
+ if (
+ clarification_output
+ and on_clarification_needed
+ and not clarification_notified
+ ):
+ clarification_notified = True
+ await on_clarification_needed(
+ handle.session_id,
+ clarification_output,
+ chunk.toolName,
+ )
elif isinstance(chunk, StreamFinish):
return
elif isinstance(chunk, StreamError):
@@ -592,3 +616,41 @@ def _extract_setup_requirements(output: str | dict[str, Any]) -> dict[str, Any]
if parsed.get("type") != "setup_requirements":
return None
return parsed
+
+
+def _extract_clarification_needed(
+ output: str | dict[str, Any],
+) -> dict[str, Any] | None:
+ """Return clarification-needed payloads from structured tool output.
+
+ ``ask_question`` pauses the turn on a question for the user; if this
+ returns ``None`` for well-formed output, the bot conversation ends with
+ nothing to reply to and the user never sees why AutoPilot stopped.
+ """
+ if isinstance(output, str):
+ try:
+ parsed: Any = json.loads(output)
+ except json.JSONDecodeError:
+ if '"agent_builder_clarification_needed"' in output:
+ logger.warning(
+ "Dropping unparseable clarification tool output "
+ "(%d chars) — question will not be sent",
+ len(output),
+ )
+ return None
+ else:
+ parsed = output
+
+ if not isinstance(parsed, dict):
+ return None
+ if parsed.get("type") != "agent_builder_clarification_needed":
+ return None
+ # A truthy non-list `questions` (a bare string, say) would be handed on
+ # and then iterated by the renderer, raising TypeError inside the stream
+ # callback — which surfaces as the generic "something went wrong" and
+ # loses the question entirely, the same failure the native-choice
+ # fallback exists to prevent.
+ questions = parsed.get("questions")
+ if not isinstance(questions, list) or not questions:
+ return None
+ return parsed
diff --git a/autogpt_platform/backend/backend/copilot/bot/bot_backend_test.py b/autogpt_platform/backend/backend/copilot/bot/bot_backend_test.py
index 36371bd430..b25935c1d3 100644
--- a/autogpt_platform/backend/backend/copilot/bot/bot_backend_test.py
+++ b/autogpt_platform/backend/backend/copilot/bot/bot_backend_test.py
@@ -1,6 +1,7 @@
"""Tests for the bot's thin facade over PlatformLinkingManagerClient."""
import asyncio
+import json
import logging
from datetime import datetime, timezone
from unittest.mock import AsyncMock, MagicMock, patch
@@ -32,6 +33,7 @@ from .bot_backend import (
BotBackend,
BotStreamError,
ChatTurnDeniedError,
+ _extract_clarification_needed,
_extract_setup_requirements,
_is_corrupted_setup_requirements,
)
@@ -348,6 +350,68 @@ class TestStreamChat:
assert setup_calls == []
assert dropped_calls == [("sess", "connect_integration")]
+ @pytest.mark.asyncio
+ async def test_notifies_clarification_needed_tool_output(self, api: BotBackend):
+ handle = ChatTurnHandle(session_id="sess", turn_id="turn", user_id="u1")
+ api._client.start_chat_turn = AsyncMock(return_value=handle)
+
+ questions = [
+ {
+ "question": "Which region?",
+ "keyword": "region",
+ "options": ["US", "EU"],
+ }
+ ]
+ queue: asyncio.Queue = asyncio.Queue()
+ await queue.put(
+ StreamToolOutputAvailable(
+ toolCallId="tool-1",
+ toolName="ask_question",
+ output=json.dumps(
+ {
+ "type": "agent_builder_clarification_needed",
+ "message": "Which region?",
+ "questions": questions,
+ }
+ ),
+ )
+ )
+ await queue.put(StreamTextDelta(id="1", delta="After question"))
+ await queue.put(StreamFinish())
+
+ clarification_calls: list[tuple[str, dict, str | None]] = []
+
+ async def on_clarification(
+ session_id: str, output: dict, tool_name: str | None
+ ):
+ clarification_calls.append((session_id, output, tool_name))
+
+ with (
+ patch(
+ "backend.copilot.bot.bot_backend.stream_registry.subscribe_to_session",
+ new=AsyncMock(return_value=queue),
+ ),
+ patch(
+ "backend.copilot.bot.bot_backend.stream_registry.unsubscribe_from_session",
+ new=AsyncMock(),
+ ),
+ ):
+ chunks: list[str] = []
+ async for chunk in api.stream_chat(
+ platform="discord",
+ platform_user_id="u1",
+ message="hi",
+ on_clarification_needed=on_clarification,
+ ):
+ chunks.append(chunk)
+
+ assert chunks == ["After question"]
+ assert len(clarification_calls) == 1
+ session_id, output, tool_name = clarification_calls[0]
+ assert session_id == "sess"
+ assert tool_name == "ask_question"
+ assert output["questions"] == questions
+
@pytest.mark.asyncio
async def test_duplicate_message_propagates(self, api: BotBackend):
api._client.start_chat_turn = AsyncMock(
@@ -425,6 +489,72 @@ class TestExtractSetupRequirements:
assert not caplog.records
+class TestExtractClarificationNeeded:
+ def test_extracts_from_json_string(self):
+ payload = json.dumps(
+ {
+ "type": "agent_builder_clarification_needed",
+ "message": "Which region?",
+ "questions": [{"question": "Which region?", "keyword": "region"}],
+ }
+ )
+ result = _extract_clarification_needed(payload)
+ assert result is not None
+ assert result["questions"] == [
+ {"question": "Which region?", "keyword": "region"}
+ ]
+
+ def test_extracts_from_dict(self):
+ payload = {
+ "type": "agent_builder_clarification_needed",
+ "message": "Which region?",
+ "questions": [{"question": "Which region?", "keyword": "region"}],
+ }
+ assert _extract_clarification_needed(payload) == payload
+
+ def test_no_questions_returns_none(self):
+ payload = json.dumps(
+ {
+ "type": "agent_builder_clarification_needed",
+ "message": "Which region?",
+ "questions": [],
+ }
+ )
+ assert _extract_clarification_needed(payload) is None
+
+ def test_non_list_questions_returns_none(self):
+ # A truthy non-list would be passed on and then iterated by the
+ # renderer, raising TypeError inside the stream callback — which the
+ # user sees as the generic error, with the question lost.
+ payload = json.dumps(
+ {
+ "type": "agent_builder_clarification_needed",
+ "message": "Which region?",
+ "questions": "Which region?",
+ }
+ )
+ assert _extract_clarification_needed(payload) is None
+
+ def test_other_tool_output_returns_none(self):
+ payload = '{"type":"setup_requirements","message":"Connect GitHub"}'
+ assert _extract_clarification_needed(payload) is None
+
+ def test_truncated_clarification_logs_warning(
+ self, caplog: pytest.LogCaptureFixture
+ ):
+ truncated = '{"type":"agent_builder_clarification_needed","message":"Which reg'
+ with caplog.at_level(logging.WARNING):
+ assert _extract_clarification_needed(truncated) is None
+ assert any("clarification" in record.message for record in caplog.records)
+
+ def test_non_clarification_unparseable_output_stays_silent(
+ self, caplog: pytest.LogCaptureFixture
+ ):
+ with caplog.at_level(logging.WARNING):
+ assert _extract_clarification_needed("plain text tool result") is None
+ assert not caplog.records
+
+
class TestIsCorruptedSetupRequirements:
def test_truncated_setup_requirements_is_corrupted(self):
assert _is_corrupted_setup_requirements(
diff --git a/autogpt_platform/backend/backend/copilot/bot/choices.py b/autogpt_platform/backend/backend/copilot/bot/choices.py
new file mode 100644
index 0000000000..4656e97353
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/bot/choices.py
@@ -0,0 +1,118 @@
+"""Redis-backed storage for in-flight ask_question choice buttons.
+
+A native choice button/select (Discord component, Slack block action,
+Telegram inline keyboard, Teams Adaptive Card action) round-trips a short
+opaque token rather than the option text itself -- Telegram's
+``callback_data`` caps at 64 bytes, too tight for arbitrary option text, and
+carrying every platform's payload the same way keeps the click handlers
+uniform. The click handler looks the real text up here by (platform, token,
+index).
+
+A token is bound to the person the question was asked of. In a shared
+channel the buttons are visible to everyone, and a click is a great deal
+easier than typing a reply -- without the binding, any passer-by could
+consume the token and leave the person who was actually asked with nothing
+but "this question has expired".
+"""
+
+import json
+import uuid
+from typing import Optional
+
+from pydantic import BaseModel
+
+from backend.data.redis_client import get_redis_async
+
+CHOICE_TTL = 3600 # 1 hour -- long enough to answer, short enough that a
+# stale button reliably reports "expired" instead of silently misfiring.
+
+
+class ResolvedChoice(BaseModel):
+ """Outcome of a click on a choice button.
+
+ ``text`` is the chosen option, or ``None`` when the token is expired,
+ forged, already used, or the index is out of range. ``refused`` marks
+ the one case that is none of those: somebody other than the person
+ asked clicked it. The token is deliberately left intact then, so the
+ right person can still answer.
+ """
+
+ text: Optional[str]
+ refused: bool = False
+
+
+def _key(platform: str, token: str) -> str:
+ return f"copilot-bot:choice:{platform}:{token}"
+
+
+async def store_choice(platform: str, options: list[str], owner_id: str) -> str:
+ """Persist a question's options against the user it was asked of.
+
+ Returns an opaque token that native button/select payloads can carry
+ within their size limits.
+ """
+ token = uuid.uuid4().hex[:12]
+ redis = await get_redis_async()
+ await redis.set(
+ _key(platform, token),
+ json.dumps({"options": options, "owner": owner_id}),
+ ex=CHOICE_TTL,
+ )
+ return token
+
+
+async def resolve_choice(
+ platform: str, token: str, index: int, clicker_id: str
+) -> ResolvedChoice:
+ """Fetch-and-clear the option text for ``token``/``index``.
+
+ Two steps, and the order is what makes both properties hold:
+
+ - A read first, so a click by anyone other than the owner can be
+ refused *without mutating anything*. That is what stops a bystander
+ in a shared channel destroying the question.
+ - Then GETDEL, so the owner's own double-click (or a platform delivery
+ retry racing itself) still consumes exactly once -- each extra
+ continuation is a billable AutoPilot turn. The loser gets ``None``,
+ the same as an expired or forged payload.
+
+ There is no window between them worth closing: only the owner ever
+ reaches the GETDEL, and their concurrent clicks serialise on it.
+ """
+ redis = await get_redis_async()
+ key = _key(platform, token)
+ raw = await redis.get(key)
+ if not raw:
+ return ResolvedChoice(text=None)
+ if _owner_of(raw) != clicker_id:
+ return ResolvedChoice(text=None, refused=True)
+
+ claimed = await redis.getdel(key)
+ if not claimed:
+ return ResolvedChoice(text=None)
+ options = _options_of(claimed)
+ if not (0 <= index < len(options)):
+ return ResolvedChoice(text=None)
+ return ResolvedChoice(text=options[index])
+
+
+async def clear_choice(platform: str, token: str) -> None:
+ """Invalidate a token that was never resolved (e.g. the native send
+ itself failed) -- ``resolve_choice`` already clears on a successful
+ resolve, so this is not needed on that path."""
+ redis = await get_redis_async()
+ await redis.delete(_key(platform, token))
+
+
+def _owner_of(raw: bytes | str) -> Optional[str]:
+ try:
+ return str(json.loads(raw)["owner"])
+ except (ValueError, TypeError, KeyError):
+ return None
+
+
+def _options_of(raw: bytes | str) -> list[str]:
+ try:
+ return [str(option) for option in json.loads(raw)["options"]]
+ except (ValueError, TypeError, KeyError):
+ return []
diff --git a/autogpt_platform/backend/backend/copilot/bot/choices_test.py b/autogpt_platform/backend/backend/copilot/bot/choices_test.py
new file mode 100644
index 0000000000..d12f6e33cf
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/bot/choices_test.py
@@ -0,0 +1,153 @@
+"""Tests for the ask_question choice-button token store."""
+
+import json
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+
+from backend.copilot.bot import choices
+
+_KEY = "copilot-bot:choice:discord:tok"
+
+
+def _stored(options: list[str], owner: str = "u1") -> str:
+ return json.dumps({"options": options, "owner": owner})
+
+
+def _redis(*, get=None, getdel=None) -> MagicMock:
+ redis = MagicMock()
+ redis.set = AsyncMock()
+ redis.delete = AsyncMock()
+ redis.get = AsyncMock(return_value=get)
+ redis.getdel = AsyncMock(return_value=getdel)
+ return redis
+
+
+def _patched(redis: MagicMock):
+ return patch(
+ "backend.copilot.bot.choices.get_redis_async",
+ new=AsyncMock(return_value=redis),
+ )
+
+
+async def test_store_choice_writes_options_and_owner_with_ttl():
+ redis = _redis()
+ with _patched(redis):
+ token = await choices.store_choice("discord", ["US", "EU"], "u1")
+
+ assert len(token) == 12
+ key, value = redis.set.await_args.args
+ assert key == f"copilot-bot:choice:discord:{token}"
+ assert json.loads(value) == {"options": ["US", "EU"], "owner": "u1"}
+ assert redis.set.await_args.kwargs["ex"] == choices.CHOICE_TTL
+
+
+async def test_resolve_choice_returns_option_by_index():
+ redis = _redis(get=_stored(["US", "EU"]), getdel=_stored(["US", "EU"]))
+ with _patched(redis):
+ resolved = await choices.resolve_choice("discord", "tok", 1, "u1")
+
+ assert resolved.text == "EU"
+ assert resolved.refused is False
+ redis.getdel.assert_awaited_once_with(_KEY)
+
+
+async def test_resolve_choice_missing_token_returns_none():
+ redis = _redis(get=None)
+ with _patched(redis):
+ resolved = await choices.resolve_choice("discord", "gone", 0, "u1")
+
+ assert resolved.text is None
+ assert resolved.refused is False
+ redis.getdel.assert_not_awaited()
+
+
+async def test_resolve_choice_out_of_range_index_returns_none():
+ """A forged or stale payload must not index-error into a 500."""
+ redis = _redis(get=_stored(["US", "EU"]), getdel=_stored(["US", "EU"]))
+ with _patched(redis):
+ resolved = await choices.resolve_choice("discord", "tok", 5, "u1")
+
+ assert resolved.text is None
+
+
+async def test_a_bystanders_click_is_refused_and_consumes_nothing():
+ """The buttons are visible to a whole channel and are far easier to hit
+ than typing a reply. A passer-by must not be able to destroy the token
+ and leave the person who was actually asked with "expired"."""
+ redis = _redis(get=_stored(["US", "EU"], owner="asker"))
+ with _patched(redis):
+ resolved = await choices.resolve_choice("discord", "tok", 1, "bystander")
+
+ assert resolved.text is None
+ assert resolved.refused is True
+ redis.getdel.assert_not_awaited()
+
+
+async def test_the_asker_can_still_answer_after_a_bystander_clicked():
+ store = {_KEY: _stored(["US", "EU"], owner="asker")}
+
+ async def fake_get(key: str):
+ return store.get(key)
+
+ async def fake_getdel(key: str):
+ return store.pop(key, None)
+
+ redis = MagicMock()
+ redis.get = AsyncMock(side_effect=fake_get)
+ redis.getdel = AsyncMock(side_effect=fake_getdel)
+ with _patched(redis):
+ assert (
+ await choices.resolve_choice("discord", "tok", 0, "bystander")
+ ).refused is True
+ assert (await choices.resolve_choice("discord", "tok", 1, "asker")).text == "EU"
+
+
+async def test_resolve_choice_is_single_use_for_the_owner():
+ """GETDEL keeps the owner's own double-click (or a platform delivery
+ retry racing itself) from resolving twice — each resolve continues the
+ paused AutoPilot turn, which is billable."""
+ store = {_KEY: _stored(["US"], owner="u1")}
+
+ async def fake_get(key: str):
+ return store.get(key)
+
+ async def fake_getdel(key: str):
+ return store.pop(key, None)
+
+ redis = MagicMock()
+ redis.get = AsyncMock(side_effect=fake_get)
+ redis.getdel = AsyncMock(side_effect=fake_getdel)
+ with _patched(redis):
+ assert (await choices.resolve_choice("discord", "tok", 0, "u1")).text == "US"
+ second = await choices.resolve_choice("discord", "tok", 0, "u1")
+
+ assert second.text is None
+ assert second.refused is False
+
+
+async def test_a_malformed_record_is_refused_rather_than_consumed():
+ redis = _redis(get="not json")
+ with _patched(redis):
+ resolved = await choices.resolve_choice("discord", "tok", 0, "u1")
+
+ assert resolved.text is None
+ assert resolved.refused is True
+ redis.getdel.assert_not_awaited()
+
+
+async def test_clear_choice_deletes_key():
+ redis = _redis()
+ with _patched(redis):
+ await choices.clear_choice("discord", "tok")
+
+ redis.delete.assert_awaited_once_with(_KEY)
diff --git a/autogpt_platform/backend/backend/copilot/bot/handler_test.py b/autogpt_platform/backend/backend/copilot/bot/handler_test.py
index dca07c4b7e..b3fd3a133f 100644
--- a/autogpt_platform/backend/backend/copilot/bot/handler_test.py
+++ b/autogpt_platform/backend/backend/copilot/bot/handler_test.py
@@ -72,6 +72,8 @@ def _adapter() -> MagicMock:
adapter.stop_typing = AsyncMock()
adapter.supports_stream_drafts = False
adapter.send_stream_draft = AsyncMock(return_value=False)
+ adapter.supports_choice_buttons = False
+ adapter.send_choice_buttons = AsyncMock(return_value=False)
adapter.create_thread = AsyncMock(return_value="thread-new")
adapter.rename_thread = AsyncMock(return_value=True)
return adapter
@@ -646,6 +648,142 @@ class TestBatching:
adapter.send_link.assert_awaited_once()
adapter.send_message.assert_not_awaited()
+ @pytest.mark.asyncio
+ async def test_clarification_needed_sends_question_with_options(self):
+ api = _api()
+
+ async def clarification_stream(*args, **kwargs):
+ await kwargs["on_clarification_needed"](
+ "session-1",
+ {
+ "type": "agent_builder_clarification_needed",
+ "message": "Which region?",
+ "questions": [
+ {
+ "question": "Which region?",
+ "keyword": "region",
+ "options": ["US", "EU"],
+ }
+ ],
+ },
+ "ask_question",
+ )
+ yield "Once you pick, I'll continue."
+
+ api.stream_chat = clarification_stream
+ handler = MessageHandler(api)
+ adapter = _adapter()
+
+ with (
+ patch(
+ "backend.copilot.bot.turn_stream.get_redis_async",
+ new=AsyncMock(return_value=AsyncMock(get=AsyncMock(return_value=None))),
+ ),
+ ):
+ await handler._stream_batch(
+ [("Bently", "u1", "hi")], _ctx(), adapter, "target-1"
+ )
+
+ # The question is sent as a normal text message (every adapter
+ # already implements send_message, unlike the setup-required flow's
+ # link button) — so both the question and the trailing text arrive
+ # via send_message.
+ assert adapter.send_message.await_count == 2
+ question_text = adapter.send_message.await_args_list[0].args[1]
+ assert "Which region?" in question_text
+ assert "1. US" in question_text
+ assert "2. EU" in question_text
+ assert "Reply with a number" in question_text
+ assert (
+ "Once you pick, I'll continue."
+ in adapter.send_message.await_args_list[1].args[1]
+ )
+
+ @pytest.mark.asyncio
+ async def test_clarification_needed_fires_once_per_turn(self):
+ api = _api()
+
+ async def clarification_stream(*args, **kwargs):
+ payload = {
+ "type": "agent_builder_clarification_needed",
+ "message": "Which region?",
+ "questions": [{"question": "Which region?", "keyword": "region"}],
+ }
+ await kwargs["on_clarification_needed"](
+ "session-1", payload, "ask_question"
+ )
+ await kwargs["on_clarification_needed"](
+ "session-1", payload, "ask_question"
+ )
+ if False:
+ yield ""
+
+ api.stream_chat = clarification_stream
+ handler = MessageHandler(api)
+ adapter = _adapter()
+
+ with (
+ patch(
+ "backend.copilot.bot.turn_stream.get_redis_async",
+ new=AsyncMock(return_value=AsyncMock(get=AsyncMock(return_value=None))),
+ ),
+ ):
+ await handler._stream_batch(
+ [("Bently", "u1", "hi")], _ctx(), adapter, "target-1"
+ )
+
+ adapter.send_message.assert_awaited_once()
+
+ @pytest.mark.asyncio
+ async def test_clarification_needed_splits_oversized_message(self):
+ """Regression: ask_question allows up to 10 questions of 25 options
+ each, which can render past a platform's message cap (Discord 2000
+ chars) — the clarification send must chunk like any other long
+ message rather than risk send_message raising on an oversized post.
+ """
+ api = _api()
+
+ async def clarification_stream(*args, **kwargs):
+ await kwargs["on_clarification_needed"](
+ "session-1",
+ {
+ "type": "agent_builder_clarification_needed",
+ "message": "Which region?",
+ "questions": [
+ {
+ "question": "Which region?",
+ "keyword": "region",
+ "options": [f"Option {i}" for i in range(25)],
+ }
+ ],
+ },
+ "ask_question",
+ )
+ if False:
+ yield ""
+
+ api.stream_chat = clarification_stream
+ handler = MessageHandler(api)
+ adapter = _adapter()
+ adapter.chunk_flush_at = 50
+
+ with (
+ patch(
+ "backend.copilot.bot.turn_stream.get_redis_async",
+ new=AsyncMock(return_value=AsyncMock(get=AsyncMock(return_value=None))),
+ ),
+ ):
+ await handler._stream_batch(
+ [("Bently", "u1", "hi")], _ctx(), adapter, "target-1"
+ )
+
+ msgs = [c.args[1] for c in adapter.send_message.await_args_list]
+ assert len(msgs) > 1
+ assert all(len(m) <= 50 for m in msgs)
+ assert "Option 0" in "".join(msgs)
+ assert "Option 24" in "".join(msgs)
+ assert "Reply with a number" in "".join(msgs)
+
@pytest.mark.asyncio
async def test_channel_thread_renames_from_generated_chat_title(self):
api = _api()
diff --git a/autogpt_platform/backend/backend/copilot/bot/turn_stream.py b/autogpt_platform/backend/backend/copilot/bot/turn_stream.py
index 257bc7a68f..2eb44d2134 100644
--- a/autogpt_platform/backend/backend/copilot/bot/turn_stream.py
+++ b/autogpt_platform/backend/backend/copilot/bot/turn_stream.py
@@ -23,7 +23,7 @@ from backend.platform_linking.models import TurnDenial
from backend.util.exceptions import DuplicateChatMessageError, NotFoundError
from backend.util.settings import Settings
-from . import sessions
+from . import choices, sessions
from .adapters.base import (
FileAttachment,
MessageContext,
@@ -33,7 +33,7 @@ from .adapters.base import (
from .bot_backend import BotBackend, BotStreamError, ChatTurnDeniedError
from .config import SESSION_TTL
from .prompt import clamp_thread_name
-from .text import format_batch, split_at_boundary
+from .text import format_batch, iter_chunks, split_at_boundary
logger = logging.getLogger(__name__)
@@ -165,6 +165,7 @@ class TurnStreamer:
buffer = ""
sent_any_content = False
setup_prompt_sent = False
+ clarification_prompt_sent = False
async def _on_setup_required(
session_id: str,
@@ -229,6 +230,27 @@ class TurnStreamer:
mentionable_users=ctx.mentionable_users,
)
+ async def _on_clarification_needed(
+ session_id: str,
+ clarification_output: dict[str, Any],
+ _tool_name: str | None,
+ ) -> None:
+ nonlocal active_session_id, buffer, sent_any_content, clarification_prompt_sent
+ if clarification_prompt_sent:
+ return
+ clarification_prompt_sent = True
+ active_session_id = session_id
+ # Drain any pending text first so the question doesn't render
+ # ahead of the message it belongs to.
+ if buffer.strip():
+ if await self._send_text_and_artifacts(
+ adapter, target_id, buffer, ctx, session_id
+ ):
+ sent_any_content = True
+ buffer = ""
+ sent_any_content = True
+ await _send_clarification(adapter, target_id, ctx, clarification_output)
+
started_at = time.monotonic()
reply_chars = 0
draft = DraftStreamer(adapter, target_id)
@@ -244,6 +266,7 @@ class TurnStreamer:
on_session_id=_on_session_id,
on_setup_required=_on_setup_required,
on_setup_dropped=_on_setup_dropped,
+ on_clarification_needed=_on_clarification_needed,
):
buffer += chunk
reply_chars += len(chunk)
@@ -548,3 +571,172 @@ def _setup_required_message(setup_output: dict[str, Any]) -> str:
"Click the button below to open your AutoGPT chat and finish setup "
"there. Reply here when you're done."
)
+
+
+async def _send_clarification(
+ adapter: PlatformAdapter,
+ target_id: str,
+ ctx: MessageContext,
+ clarification_output: dict[str, Any],
+) -> None:
+ """Send the questions as native choice buttons/select, or as the existing
+ numbered-text rendering chunked to the adapter's message cap.
+
+ All-or-nothing per payload. Native sends happen one question at a time
+ while text questions are batched into a single numbered message, so
+ mixing the two in one payload would deliver them out of order (Q1, Q3,
+ then Q2) and the user would answer against the wrong numbering. If any
+ question can't go native, the whole payload goes as text — which is
+ ordered, and is the rendering that already worked.
+ """
+ questions = list(clarification_output.get("questions") or [])
+ if questions and all(_fits_native(adapter, q) for q in questions):
+ if await _send_native_choices(adapter, target_id, ctx, questions):
+ return
+ # A native send failed part-way; fall through and render the whole
+ # payload as text so no question is silently lost.
+
+ if not questions:
+ return
+ message = _clarification_message(clarification_output)
+ for chunk in iter_chunks(message, adapter.chunk_flush_at):
+ await adapter.send_message(
+ target_id, chunk, mentionable_users=ctx.mentionable_users
+ )
+
+
+def _fits_native(adapter: PlatformAdapter, question: Any) -> bool:
+ """Whether ``question`` can be rendered as this adapter's native widget."""
+ if not isinstance(question, dict):
+ return False
+ text = str(question.get("question") or "").strip()
+ options = _question_options(question)
+ if not adapter.supports_choice_buttons or not text or not options:
+ return False
+ if len(options) > adapter.max_choice_options:
+ return False
+ # A clipped label is worse than no button: two options sharing a prefix
+ # render identically while each still dispatches its own full text.
+ if any(len(option) > adapter.max_choice_label_length for option in options):
+ return False
+ # Nothing else chunks the question text, and every adapter's send raises
+ # past its cap — which, before this check, surfaced as a generic turn
+ # error instead of the numbered text that would have fitted. Measure what
+ # actually goes on the wire: each adapter localizes first, and HTML or
+ # mrkdwn escaping can push a question that fitted over the cap.
+ rendered = adapter.localize_markup(_native_question_text(text))
+ return len(rendered) <= adapter.max_message_length
+
+
+def _native_question_text(text: str) -> str:
+ return f"❓ {text}"
+
+
+async def _send_native_choices(
+ adapter: PlatformAdapter,
+ target_id: str,
+ ctx: MessageContext,
+ questions: list[Any],
+) -> bool:
+ """Send every question as native choice buttons; False if any didn't land.
+
+ An adapter may either return False or raise (Telegram and Teams have no
+ non-raising failure path at all), and Redis can fail under either
+ ``store_choice`` or ``clear_choice``. Every one of those must mean "fall
+ back to text": an exception escaping here reaches the caller's generic
+ handler, which reports "AutoGPT ran into an error", abandons the
+ half-consumed turn, and leaves any token alive for its full TTL — with
+ the question delivered in no form at all, not even as numbered text.
+
+ Each token is bound to ``ctx.user_id`` — the person who asked — so a
+ bystander in a shared channel cannot consume the question by clicking.
+
+ On failure every token minted for this payload is cleared, not just the
+ one that failed. A question already sent natively is about to be
+ re-rendered as text, and a live button beside that text would answer the
+ turn a second time; a cleared token makes the stale button report that
+ it has expired, which is exactly right next to the text version.
+ """
+ minted: list[str] = []
+ try:
+ for question in questions:
+ options = _question_options(question)
+ text = str(question.get("question") or "").strip()
+ token = await choices.store_choice(
+ adapter.platform_name, options, ctx.user_id
+ )
+ minted.append(token)
+ if not await adapter.send_choice_buttons(
+ target_id,
+ _native_question_text(text),
+ options,
+ token,
+ mentionable_users=ctx.mentionable_users,
+ ):
+ break
+ else:
+ return True
+ except Exception:
+ logger.exception(
+ "Native choice delivery failed on %s; falling back to text",
+ adapter.platform_name,
+ )
+ await _discard_choice_tokens(adapter.platform_name, minted)
+ return False
+
+
+async def _discard_choice_tokens(platform: str, tokens: list[str]) -> None:
+ """Invalidate tokens whose questions are about to be re-sent as text.
+
+ Never raises: this runs on the failure path, and letting Redis take the
+ text fallback down with it is the failure this whole path exists to
+ prevent. A token that outlives its clear expires on its own TTL.
+ """
+ for token in tokens:
+ try:
+ await choices.clear_choice(platform, token)
+ except Exception:
+ logger.exception("Could not clear choice token on %s", platform)
+
+
+def _question_options(question: dict[str, Any]) -> list[str]:
+ # `options` is model-shaped: a non-list value here would be iterated
+ # character by character (a string) or raise (an int), so anything that
+ # isn't a list means "no options" and the question renders as free text.
+ options = question.get("options")
+ if not isinstance(options, list):
+ return []
+ return [str(option).strip() for option in options if str(option).strip()]
+
+
+def _clarification_message(clarification_output: dict[str, Any]) -> str:
+ """Render an ask_question payload as plain text with numbered options.
+
+ Used for questions ``_send_clarification`` couldn't render as native
+ choice buttons/select (free-text questions, too many options, or an
+ adapter without native support) -- every adapter already supports
+ ``send_message``, and a typed reply (a number or free text) flows into
+ the session exactly like a normal chat message, so this is always a
+ working fallback.
+ """
+ blocks: list[str] = []
+ for question in clarification_output.get("questions") or []:
+ if not isinstance(question, dict):
+ continue
+ text = str(question.get("question") or "").strip()
+ if not text:
+ continue
+ options = _question_options(question)
+ block = f"❓ {text}"
+ if options:
+ numbered = "\n".join(f"{i}. {o}" for i, o in enumerate(options, 1))
+ block = f"{block}\n{numbered}"
+ blocks.append(block)
+
+ if not blocks:
+ fallback = str(clarification_output.get("message") or "").strip()
+ return fallback or "AutoGPT has a question before it can continue."
+
+ body = "\n\n".join(blocks)
+ footer = "Reply with a number, or just type your answer."
+ return f"{body}\n\n{footer}"
diff --git a/autogpt_platform/backend/backend/copilot/bot/turn_stream_test.py b/autogpt_platform/backend/backend/copilot/bot/turn_stream_test.py
index ec3a39db44..c3695f80e3 100644
--- a/autogpt_platform/backend/backend/copilot/bot/turn_stream_test.py
+++ b/autogpt_platform/backend/backend/copilot/bot/turn_stream_test.py
@@ -5,7 +5,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from .adapters.base import ChannelType, MessageContext, StreamDraftOutcome
-from .turn_stream import DraftStreamer, TurnStreamer
+from .turn_stream import DraftStreamer, TurnStreamer, _send_clarification
_MODULE = "backend.copilot.bot.turn_stream"
@@ -17,6 +17,8 @@ def _adapter(*, drafts: bool = False) -> MagicMock:
adapter.send_message = AsyncMock()
adapter.send_link = AsyncMock()
adapter.send_file = AsyncMock()
+ adapter.supports_choice_buttons = False
+ adapter.send_choice_buttons = AsyncMock(return_value=False)
adapter.start_typing = AsyncMock()
adapter.stop_typing = AsyncMock()
adapter.rename_thread = AsyncMock(return_value=True)
@@ -167,3 +169,235 @@ class TestStreamBatchDrafts:
adapter.send_stream_draft.assert_not_awaited()
adapter.send_message.assert_awaited_once()
assert adapter.send_message.await_args.args[1] == "Hello world"
+
+
+# -- Native choice buttons: the branch the feature is named after --
+
+
+def _choice_adapter(
+ *, max_options: int = 10, max_len: int = 4096, max_label: int = 64
+) -> MagicMock:
+ adapter = _adapter()
+ adapter.platform_name = "telegram"
+ adapter.supports_choice_buttons = True
+ adapter.max_choice_options = max_options
+ adapter.max_choice_label_length = max_label
+ adapter.max_message_length = max_len
+ adapter.localize_markup = lambda text: text
+ adapter.send_choice_buttons = AsyncMock(return_value=True)
+ return adapter
+
+
+def _patch_choices():
+ return patch(
+ f"{_MODULE}.choices",
+ new=MagicMock(
+ store_choice=AsyncMock(return_value="tok"),
+ clear_choice=AsyncMock(),
+ ),
+ )
+
+
+async def _clarify(adapter, questions):
+ with _patch_choices() as choices_mock:
+ await _send_clarification(adapter, "42", _ctx(), {"questions": questions})
+ return choices_mock
+
+
+class TestNativeChoices:
+ @pytest.mark.asyncio
+ async def test_sends_native_buttons_and_no_text(self):
+ adapter = _choice_adapter()
+ await _clarify(adapter, [{"question": "Region?", "options": ["EU", "US"]}])
+
+ adapter.send_choice_buttons.assert_awaited_once()
+ assert adapter.send_choice_buttons.await_args.args[1] == "❓ Region?"
+ adapter.send_message.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_adapter_returning_false_falls_back_to_text(self):
+ adapter = _choice_adapter()
+ adapter.send_choice_buttons = AsyncMock(return_value=False)
+
+ choices_mock = await _clarify(
+ adapter, [{"question": "Region?", "options": ["EU", "US"]}]
+ )
+
+ adapter.send_message.assert_awaited()
+ choices_mock.clear_choice.assert_awaited_once()
+
+ @pytest.mark.asyncio
+ async def test_adapter_raising_falls_back_to_text(self):
+ # Telegram and Teams have no non-raising failure path, so a 429 used
+ # to escape into the caller's generic handler: the user got "AutoGPT
+ # ran into an error" and the question was delivered in no form.
+ adapter = _choice_adapter()
+ adapter.send_choice_buttons = AsyncMock(side_effect=RuntimeError("429"))
+
+ choices_mock = await _clarify(
+ adapter, [{"question": "Region?", "options": ["EU", "US"]}]
+ )
+
+ adapter.send_message.assert_awaited()
+ assert "Region?" in adapter.send_message.await_args.args[1]
+ choices_mock.clear_choice.assert_awaited_once()
+
+ @pytest.mark.asyncio
+ async def test_too_many_options_for_this_platform_uses_text(self):
+ adapter = _choice_adapter(max_options=6)
+
+ await _clarify(
+ adapter,
+ [{"question": "Pick", "options": [f"o{i}" for i in range(7)]}],
+ )
+
+ adapter.send_choice_buttons.assert_not_awaited()
+ adapter.send_message.assert_awaited()
+
+ @pytest.mark.asyncio
+ async def test_question_over_the_message_cap_uses_text(self):
+ # Nothing chunks the native question text, and the send raises past
+ # the cap — the numbered text fits because it chunks.
+ adapter = _choice_adapter(max_len=50)
+
+ await _clarify(adapter, [{"question": "x" * 200, "options": ["a", "b"]}])
+
+ adapter.send_choice_buttons.assert_not_awaited()
+ adapter.send_message.assert_awaited()
+
+ @pytest.mark.asyncio
+ async def test_mixed_payload_goes_all_text_to_keep_order(self):
+ # Natives post per-question while text is batched afterwards, so a
+ # mixed payload would arrive Q1, Q3, Q2 and the user would answer
+ # against the wrong numbering.
+ adapter = _choice_adapter()
+
+ await _clarify(
+ adapter,
+ [
+ {"question": "Q1", "options": ["a", "b"]},
+ {"question": "Q2 free text"},
+ {"question": "Q3", "options": ["c", "d"]},
+ ],
+ )
+
+ adapter.send_choice_buttons.assert_not_awaited()
+ sent = "".join(c.args[1] for c in adapter.send_message.await_args_list)
+ assert sent.index("Q1") < sent.index("Q2") < sent.index("Q3")
+
+ @pytest.mark.asyncio
+ async def test_unsupported_adapter_still_uses_text(self):
+ adapter = _choice_adapter()
+ adapter.supports_choice_buttons = False
+
+ await _clarify(adapter, [{"question": "Region?", "options": ["EU", "US"]}])
+
+ adapter.send_choice_buttons.assert_not_awaited()
+ adapter.send_message.assert_awaited()
+
+ @pytest.mark.asyncio
+ async def test_option_longer_than_the_label_cap_uses_text(self):
+ # Native widgets clip labels, so two options sharing a prefix render
+ # identically while each still dispatches its own full text — the
+ # user can't tell which button they're pressing.
+ adapter = _choice_adapter(max_label=10)
+
+ await _clarify(
+ adapter,
+ [{"question": "Pick", "options": ["short", "x" * 40]}],
+ )
+
+ adapter.send_choice_buttons.assert_not_awaited()
+ adapter.send_message.assert_awaited()
+
+ @pytest.mark.asyncio
+ async def test_question_measured_after_localization(self):
+ # Adapters localize before sending, and HTML/mrkdwn escaping can push
+ # a question that fitted over the cap — the send then raises.
+ adapter = _choice_adapter(max_len=50)
+ adapter.localize_markup = lambda text: text * 10
+
+ await _clarify(adapter, [{"question": "short one", "options": ["a", "b"]}])
+
+ adapter.send_choice_buttons.assert_not_awaited()
+ adapter.send_message.assert_awaited()
+
+ @pytest.mark.asyncio
+ async def test_non_list_options_render_as_a_free_text_question(self):
+ # Model-shaped payload: a string here used to be iterated character
+ # by character into one button per letter.
+ adapter = _choice_adapter()
+
+ await _clarify(adapter, [{"question": "Region?", "options": "EU"}])
+
+ adapter.send_choice_buttons.assert_not_awaited()
+ adapter.send_message.assert_awaited()
+
+ @pytest.mark.asyncio
+ async def test_store_choice_failing_still_falls_back_to_text(self):
+ # A Redis failure minting the token used to escape before the
+ # adapter was ever called, reaching the generic stream error.
+ adapter = _choice_adapter()
+ with patch(
+ f"{_MODULE}.choices",
+ new=MagicMock(
+ store_choice=AsyncMock(side_effect=RuntimeError("redis down")),
+ clear_choice=AsyncMock(),
+ ),
+ ):
+ await _send_clarification(
+ adapter,
+ "42",
+ _ctx(),
+ {"questions": [{"question": "Q", "options": ["a"]}]},
+ )
+
+ adapter.send_message.assert_awaited()
+
+ @pytest.mark.asyncio
+ async def test_clear_choice_failing_still_falls_back_to_text(self):
+ adapter = _choice_adapter()
+ adapter.send_choice_buttons = AsyncMock(return_value=False)
+ with patch(
+ f"{_MODULE}.choices",
+ new=MagicMock(
+ store_choice=AsyncMock(return_value="tok"),
+ clear_choice=AsyncMock(side_effect=RuntimeError("redis down")),
+ ),
+ ):
+ await _send_clarification(
+ adapter,
+ "42",
+ _ctx(),
+ {"questions": [{"question": "Q", "options": ["a"]}]},
+ )
+
+ adapter.send_message.assert_awaited()
+
+ @pytest.mark.asyncio
+ async def test_partial_native_delivery_clears_every_token(self):
+ # Q1 sends, Q2 fails, so both are re-rendered as text. Q1's button is
+ # still on screen above that text and must not answer the turn again.
+ adapter = _choice_adapter()
+ adapter.send_choice_buttons = AsyncMock(side_effect=[True, False])
+ tokens = iter(["tok-1", "tok-2"])
+ choices_mock = MagicMock(
+ store_choice=AsyncMock(side_effect=lambda *a, **k: next(tokens)),
+ clear_choice=AsyncMock(),
+ )
+ with patch(f"{_MODULE}.choices", new=choices_mock):
+ await _send_clarification(
+ adapter,
+ "42",
+ _ctx(),
+ {
+ "questions": [
+ {"question": "Q1", "options": ["a", "b"]},
+ {"question": "Q2", "options": ["c", "d"]},
+ ]
+ },
+ )
+
+ cleared = {c.args[1] for c in choices_mock.clear_choice.await_args_list}
+ assert cleared == {"tok-1", "tok-2"}
+ adapter.send_message.assert_awaited()