Skip to content

The agent that grows with you

fix(relay): a relayed Discord modal submit carries its submitted values

Teknium6cc4e77cafb5
diff --git a/gateway/relay/adapter_discord_interaction.py b/gateway/relay/adapter_discord_interaction.py
index 93098b205a1..aecc4cb53ed 100644
--- a/gateway/relay/adapter_discord_interaction.py
+++ b/gateway/relay/adapter_discord_interaction.py
@@ -134,6 +134,8 @@ class RelayDiscordInteractionMixin:
message_type = MessageType.COMMAND
elif itype == 3:
text = str(data.get("custom_id") or "")
+ elif itype == 5:
+ text = self._render_modal_submit(data)
else:
text = ""
member = payload.get("member") or {}
@@ -209,6 +211,26 @@ class RelayDiscordInteractionMixin:
return None
return parts[1], parts[2]
+ @staticmethod
+ def _render_modal_submit(data) -> str:
+ """A MODAL_SUBMIT as text: the modal's custom_id, then one ``input_id: value`` line per
+ submitted input. Inputs sit in action rows (``components``) or labels (``component``);
+ an empty optional input is left out."""
+ lines = [str(data.get("custom_id") or "")]
+ stack = list(reversed(data.get("components") or []))
+ while stack:
+ comp = stack.pop()
+ if not isinstance(comp, dict):
+ continue
+ nested = comp.get("components") or ([comp["component"]] if comp.get("component") else [])
+ stack.extend(reversed(nested if isinstance(nested, list) else []))
+ value = comp.get("value", comp.get("values"))
+ if isinstance(value, list):
+ value = ", ".join(str(v) for v in value)
+ if value is not None and str(value).strip():
+ lines.append(f"{comp.get('custom_id') or ''}: {value}")
+ return "\n".join(lines).strip()
+
@staticmethod
def _render_interaction_options(options) -> list:
"""Render Discord interaction options to text parts: scalars contribute their
diff --git a/tests/gateway/relay/test_relay_passthrough.py b/tests/gateway/relay/test_relay_passthrough.py
index 9ceb2d465c3..d5d67312c39 100644
--- a/tests/gateway/relay/test_relay_passthrough.py
+++ b/tests/gateway/relay/test_relay_passthrough.py
@@ -298,3 +298,33 @@ async def test_routed_profile_round_trips_on_every_egress_frame(adapter, monkeyp
async def _noop_handle(event):
return None
+
+
+async def test_modal_submit_renders_its_submitted_values(adapter, monkeypatch):
+ """A MODAL_SUBMIT (type 5) reaches the agent with what the user typed, not as an empty turn:
+ one ``input_id: value`` line per filled input (action-row and label layouts), an empty
+ optional input left out, keyed to the thread it was submitted in."""
+ await adapter.connect()
+ seen = []
+
+ async def fake_handle(event):
+ seen.append(event)
+
+ monkeypatch.setattr(adapter, "handle_message", fake_handle)
+ fwd = _interaction_forward({
+ "id": "i-modal", "type": 5, "channel_id": "th1", "guild_id": "g1",
+ "channel": {"type": 11, "parent_id": "c1"},
+ "member": {"user": {"id": "u1", "username": "ben"}},
+ "data": {"custom_id": "feedback", "components": [
+ {"type": 1, "components": [{"type": 4, "custom_id": "title", "value": "Café ☕ outage"}]},
+ {"type": 1, "components": [{"type": 4, "custom_id": "notes", "value": ""}]},
+ {"type": 18, "component": {"type": 4, "custom_id": "steps", "value": "1. deploy\n2. 💥"}},
+ ]},
+ })
+ await adapter._transport.push_passthrough(fwd)
+
+ assert len(seen) == 1
+ ev = seen[0]
+ assert ev.text == "feedback\ntitle: Café ☕ outage\nsteps: 1. deploy\n2. 💥"
+ assert (ev.source.chat_type, ev.source.thread_id, ev.source.parent_chat_id) == ("thread", "th1", "c1")