Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions plugins/platforms/discord/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self, id: int) -> None: # noqa: A002 - matches discord API
_DISCORD_COMMAND_SYNC_STATE_SUBDIR = "gateway"
_DISCORD_COMMAND_SYNC_STATE_FILENAME = "discord_command_sync_state.json"
_DISCORD_NONCONVERSATIONAL_STATE_FILENAME = "discord_nonconversational_messages.json"
_DISCORD_FULL_OPTION_THRESHOLD_UTF16_UNITS = 40

_DISCORD_COMMAND_SYNC_MUTATION_INTERVAL_SECONDS = 4.5
_DISCORD_COMMAND_SYNC_MAX_RATE_LIMIT_SLEEP_SECONDS = 30.0
Expand Down Expand Up @@ -6803,6 +6804,18 @@ def _flatten_choice(c):
value="Pick one below, or click ✏️ Other to type a custom answer.",
inline=False,
)
if any(
utf16_len(choice) > _DISCORD_FULL_OPTION_THRESHOLD_UTF16_UNITS
for choice in clean_choices
):
embed.add_field(
name="Full options",
value="\n".join(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking: this join has no embed-field budget. clarify limits choice count but not individual choice length, so valid long choices can exceed Discord's 1024-character field-value limit and make the entire clarify send fail. Truncate the rendered list with room for any suffix and add an overflow-boundary test.

f"{index}. {choice}"
for index, choice in enumerate(clean_choices, start=1)
),
inline=False,
)
view = ClarifyChoiceView(
choices=clean_choices,
clarify_id=clarify_id,
Expand Down
14 changes: 13 additions & 1 deletion tests/gateway/test_discord_clarify_buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,12 @@ async def test_multi_choice_attaches_view(self):
sent_msg.id = 123456
channel.send = AsyncMock(return_value=sent_msg)
adapter._client.get_channel = MagicMock(return_value=channel)
long_choice = "x" * 41

result = await adapter.send_clarify(
chat_id="9001",
question="Pick a color",
choices=["red", "green", "blue"],
choices=["red", long_choice, "blue"],
clarify_id="cidM",
session_key="sk-M",
)
Expand All @@ -380,6 +381,15 @@ async def test_multi_choice_attaches_view(self):
assert isinstance(kwargs["view"], ClarifyChoiceView)
# 3 choice buttons + 1 Other
assert len(kwargs["view"].children) == 4
fields = {f["name"]: f["value"] for f in kwargs["embed"].fields}
assert (
fields["Choices"]
== "Pick one below, or click ✏️ Other to type a custom answer."
)
assert fields["Full options"] == f"1. red\n2. {long_choice}\n3. blue"
assert kwargs["view"].children[0].label == "1. red"
assert kwargs["view"].children[1].label == f"2. {long_choice}"
assert kwargs["view"].children[2].label == "3. blue"

@pytest.mark.asyncio
async def test_open_ended_omits_view(self):
Expand Down Expand Up @@ -458,6 +468,8 @@ async def test_filters_empty_and_whitespace_choices(self):
)
kwargs = channel.send.call_args.kwargs
view = kwargs["view"]
fields = {f["name"]: f["value"] for f in kwargs["embed"].fields}
assert "Full options" not in fields
# Only 1 real choice + 1 Other = 2 children
assert len(view.children) == 2
assert "real-choice" in view.children[0].label
Expand Down