The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor: move the Discord exec-approval view into a sibling module
kshitijk4poore0e3349ed332
diff --git a/plugins/platforms/discord/adapter.py b/plugins/platforms/discord/adapter.pyindex 6f22b82c4e7..fa6694f8bd4 100644--- a/plugins/platforms/discord/adapter.py+++ b/plugins/platforms/discord/adapter.py@@ -1202,6 +1202,7 @@ class VoiceReceiver:from plugins.platforms.discord.adapter_config_readers import (_DISCORD_PROMPT_TIMEOUT_DEFAULT, _DISCORD_PROMPT_TIMEOUT_MIN, _read_discord_prompt_timeout, _read_dm_role_auth_guild)+from plugins.platforms.discord.adapter_exec_approval_view import build_exec_approval_viewdef _env_bool(name: str, default: bool = False) -> bool:@@ -6475,146 +6476,7 @@ def _define_discord_view_classes() -> None:self._disable_all()await self._expire_embed(t("platform.discord.prompt.expired_footer"))- class ExecApprovalView(_HermesView):- """Allow Once / Allow Session / Always Allow / Deny buttons for a dangerous command.- Clicks call ``resolve_gateway_approval()`` — the same mechanism as the text ``/approve`` flow."""-- def __init__(- self, session_key: str, allowed_user_ids: set, allowed_role_ids: Optional[set] = None,- require_admin: bool = False, admin_user_ids: Optional[set] = None,- allow_permanent: bool = True, allow_session: bool = True, smart_denied: bool = False,- request_id: Optional[str] = None,- ):- super().__init__(allowed_user_ids, allowed_role_ids, timeout=_read_discord_prompt_timeout())- self.session_key = session_key- self.request_id = request_id- self.pending_retire: Optional[str] = None # settle reason that arrived before the card was sent- self._backend_settled = False- self._retire_lock = asyncio.Lock()- self.require_admin = require_admin- self.admin_user_ids = {str(a).strip() for a in (admin_user_ids or set()) if str(a).strip()}- self._localize_buttons(- allow_once="gateway.exec_approval.action_once", allow_session="gateway.exec_approval.action_session",- allow_always="gateway.exec_approval.action_always", deny="gateway.exec_approval.action_deny")- if smart_denied or not allow_session:- self.remove_item(self.allow_session)- self.remove_item(self.allow_always)- elif not allow_permanent:- self.remove_item(self.allow_always)-- def _check_auth(self, interaction: discord.Interaction) -> bool:- """Base admission always required; with ``require_admin`` the clicker must- also be an admin. Fails closed (logged once) when no admins are configured."""- if not super()._check_auth(interaction):- return False- if not self.require_admin:- return True- user = getattr(interaction, "user", None)- try:- uid = str(getattr(user, "id", "") or "")- except Exception:- uid = ""- if uid and uid in self.admin_user_ids:- return True- if not self.admin_user_ids:- logger.warning(- "[Discord] require_admin_for_exec_approval is enabled but "- "no admins are configured (allow_admin_from is empty) — "- "exec approval buttons are disabled for everyone. Add "- "admin user IDs under the discord platform's "- "allow_admin_from, or disable the toggle."- )- return False-- async def on_timeout(self):- await self.retire("timeout", backend_settled=False)-- async def retire(self, reason="resolved", *, backend_settled=True):- """Disable a card when its particular backend request is no longer pending."""- self.stop()- # A UI timeout can precede a text answer. Serialize their edits, and- # never let a queued UI timeout overwrite a backend settlement.- async with self._retire_lock:- if self._backend_settled:- return- self._backend_settled = backend_settled- self.resolved = True- self._disable_all()- footer = t({- "resolved": "platform.discord.approval.already_resolved",- "timeout": "platform.discord.prompt.expired_footer",- "gone": "platform.discord.approval.no_longer_pending",- }.get(reason, "platform.discord.slash_confirm.resolved_cancel"))- if self._message:- try:- embed = self._first_embed(self._message)- if embed:- embed.color = discord.Color.greyple()- embed.set_footer(text=footer)- # Notices edit content independently; neither restores controls.- await self._message.edit(embed=embed, view=None)- except Exception:- logger.debug("[Discord] Could not retire approval card", exc_info=True)-- async def _resolve(self, interaction: discord.Interaction, choice: str, color: discord.Color, label_key: str):- """Resolve the approval via the gateway approval queue and update the embed."""- if not await self._gate(- interaction, resolved_msg=t("platform.discord.approval.already_resolved"),- unauth_msg=_unauthorized(),- ):- return- label = t(label_key)- self.resolved = True- self._backend_settled = True- # Unblock the waiting agent thread FIRST. A click after the approval- # wait timed out (count == 0) must not claim "Approved".- try:- from tools.approval import resolve_gateway_approval- count = resolve_gateway_approval(self.session_key, choice, request_id=self.request_id)- logger.info(- "Discord button resolved %d approval(s) for session %s (choice=%s, user=%s)",- count, self.session_key, choice, interaction.user.display_name,- )- except Exception as exc:- logger.error("Failed to resolve gateway approval from button: %s", exc)- # Nothing was recorded and the request may still be pending: tell only the clicker- # and keep the card live for a retry. A later click, the request's observer, or- # (with no request id) the view timeout does the final edit.- self.resolved = False- self._backend_settled = False- await interaction.response.send_message(- t("platform.discord.approval.click_failed"), ephemeral=True)- return- if not count:- self._backend_settled = False- if self.request_id:- # The request already settled through another path, whose observer owns this- # card; editing it here too could land last and hide the real outcome.- await interaction.response.send_message(- t("platform.discord.approval.no_longer_pending"), ephemeral=True)- return- color = discord.Color.dark_grey()- label = t("platform.discord.approval.expired")- await self._finalize_embed(- interaction, color,- t("platform.discord.approval.by_user", label=label, user=interaction.user.display_name) if count else label)-- # Decorator labels are placeholders; ``_localize_buttons`` in __init__ sets the real text.- @discord.ui.button(label="Allow Once", style=discord.ButtonStyle.green)- async def allow_once(self, interaction: discord.Interaction, button: discord.ui.Button):- await self._resolve(interaction, "once", discord.Color.green(), "platform.discord.approval.resolved_once")-- @discord.ui.button(label="Allow Session", style=discord.ButtonStyle.grey)- async def allow_session(self, interaction: discord.Interaction, button: discord.ui.Button):- await self._resolve(interaction, "session", discord.Color.blue(), "platform.discord.approval.resolved_session")-- @discord.ui.button(label="Always Allow", style=discord.ButtonStyle.blurple)- async def allow_always(self, interaction: discord.Interaction, button: discord.ui.Button):- await self._resolve(interaction, "always", discord.Color.purple(), "platform.discord.approval.resolved_always")-- @discord.ui.button(label="Deny", style=discord.ButtonStyle.red)- async def deny(self, interaction: discord.Interaction, button: discord.ui.Button):- await self._resolve(interaction, "deny", discord.Color.red(), "platform.discord.approval.resolved_deny")+ ExecApprovalView = build_exec_approval_view(discord, _HermesView, unauthorized=_unauthorized)class SlashConfirmView(_HermesView):"""Approve Once / Always Approve / Cancel for slash-command confirmations (``/reload-mcp``,diff --git a/plugins/platforms/discord/adapter_exec_approval_view.py b/plugins/platforms/discord/adapter_exec_approval_view.pynew file mode 100644index 00000000000..aa3af1c00c4--- /dev/null+++ b/plugins/platforms/discord/adapter_exec_approval_view.py@@ -0,0 +1,159 @@+"""The Discord exec-approval card's button view.++Built by ``build_exec_approval_view`` from ``adapter._define_discord_view_classes``, because discord.py can+be installed lazily after the adapter module is imported.+"""+import asyncio+import logging+from typing import Optional++from agent.i18n import t+from plugins.platforms.discord.adapter_config_readers import _read_discord_prompt_timeout++logger = logging.getLogger(__name__)+++def build_exec_approval_view(discord, base, *, unauthorized):+ """Return the ``ExecApprovalView`` class on ``base`` (the adapter's shared ``_HermesView``)."""+ class ExecApprovalView(base):+ """Allow Once / Allow Session / Always Allow / Deny buttons for a dangerous command.+ Clicks call ``resolve_gateway_approval()`` — the same mechanism as the text ``/approve`` flow."""++ def __init__(+ self, session_key: str, allowed_user_ids: set, allowed_role_ids: Optional[set] = None,+ require_admin: bool = False, admin_user_ids: Optional[set] = None,+ allow_permanent: bool = True, allow_session: bool = True, smart_denied: bool = False,+ request_id: Optional[str] = None,+ ):+ super().__init__(allowed_user_ids, allowed_role_ids, timeout=_read_discord_prompt_timeout())+ self.session_key = session_key+ self.request_id = request_id+ self.pending_retire: Optional[str] = None # settle reason that arrived before the card was sent+ self._backend_settled = False+ self._retire_lock = asyncio.Lock()+ self.require_admin = require_admin+ self.admin_user_ids = {str(a).strip() for a in (admin_user_ids or set()) if str(a).strip()}+ self._localize_buttons(+ allow_once="gateway.exec_approval.action_once", allow_session="gateway.exec_approval.action_session",+ allow_always="gateway.exec_approval.action_always", deny="gateway.exec_approval.action_deny")+ if smart_denied or not allow_session:+ self.remove_item(self.allow_session)+ self.remove_item(self.allow_always)+ elif not allow_permanent:+ self.remove_item(self.allow_always)++ def _check_auth(self, interaction: discord.Interaction) -> bool:+ """Base admission always required; with ``require_admin`` the clicker must+ also be an admin. Fails closed (logged once) when no admins are configured."""+ if not super()._check_auth(interaction):+ return False+ if not self.require_admin:+ return True+ user = getattr(interaction, "user", None)+ try:+ uid = str(getattr(user, "id", "") or "")+ except Exception:+ uid = ""+ if uid and uid in self.admin_user_ids:+ return True+ if not self.admin_user_ids:+ logger.warning(+ "[Discord] require_admin_for_exec_approval is enabled but "+ "no admins are configured (allow_admin_from is empty) — "+ "exec approval buttons are disabled for everyone. Add "+ "admin user IDs under the discord platform's "+ "allow_admin_from, or disable the toggle."+ )+ return False++ async def on_timeout(self):+ await self.retire("timeout", backend_settled=False)++ async def retire(self, reason="resolved", *, backend_settled=True):+ """Disable a card when its particular backend request is no longer pending."""+ self.stop()+ # A UI timeout can precede a text answer. Serialize their edits, and+ # never let a queued UI timeout overwrite a backend settlement.+ async with self._retire_lock:+ if self._backend_settled:+ return+ self._backend_settled = backend_settled+ self.resolved = True+ self._disable_all()+ footer = t({+ "resolved": "platform.discord.approval.already_resolved",+ "timeout": "platform.discord.prompt.expired_footer",+ "gone": "platform.discord.approval.no_longer_pending",+ }.get(reason, "platform.discord.slash_confirm.resolved_cancel"))+ if self._message:+ try:+ embed = self._first_embed(self._message)+ if embed:+ embed.color = discord.Color.greyple()+ embed.set_footer(text=footer)+ # Notices edit content independently; neither restores controls.+ await self._message.edit(embed=embed, view=None)+ except Exception:+ logger.debug("[Discord] Could not retire approval card", exc_info=True)++ async def _resolve(self, interaction: discord.Interaction, choice: str, color: discord.Color, label_key: str):+ """Resolve the approval via the gateway approval queue and update the embed."""+ if not await self._gate(+ interaction, resolved_msg=t("platform.discord.approval.already_resolved"),+ unauth_msg=unauthorized(),+ ):+ return+ label = t(label_key)+ self.resolved = True+ self._backend_settled = True+ # Unblock the waiting agent thread FIRST. A click after the approval+ # wait timed out (count == 0) must not claim "Approved".+ try:+ from tools.approval import resolve_gateway_approval+ count = resolve_gateway_approval(self.session_key, choice, request_id=self.request_id)+ logger.info(+ "Discord button resolved %d approval(s) for session %s (choice=%s, user=%s)",+ count, self.session_key, choice, interaction.user.display_name,+ )+ except Exception as exc:+ logger.error("Failed to resolve gateway approval from button: %s", exc, exc_info=True)+ # Nothing was recorded and the request may still be pending: tell only the clicker+ # and keep the card live for a retry. A later click, the request's observer, or+ # (with no request id) the view timeout does the final edit.+ self.resolved = False+ self._backend_settled = False+ await interaction.response.send_message(+ t("platform.discord.approval.click_failed"), ephemeral=True)+ return+ if not count:+ self._backend_settled = False+ if self.request_id:+ # The request already settled through another path, whose observer owns this+ # card; editing it here too could land last and hide the real outcome.+ await interaction.response.send_message(+ t("platform.discord.approval.no_longer_pending"), ephemeral=True)+ return+ color = discord.Color.dark_grey()+ label = t("platform.discord.approval.expired")+ await self._finalize_embed(+ interaction, color,+ t("platform.discord.approval.by_user", label=label, user=interaction.user.display_name) if count else label)++ # Decorator labels are placeholders; ``_localize_buttons`` in __init__ sets the real text.+ @discord.ui.button(label="Allow Once", style=discord.ButtonStyle.green)+ async def allow_once(self, interaction: discord.Interaction, button: discord.ui.Button):+ await self._resolve(interaction, "once", discord.Color.green(), "platform.discord.approval.resolved_once")++ @discord.ui.button(label="Allow Session", style=discord.ButtonStyle.grey)+ async def allow_session(self, interaction: discord.Interaction, button: discord.ui.Button):+ await self._resolve(interaction, "session", discord.Color.blue(), "platform.discord.approval.resolved_session")++ @discord.ui.button(label="Always Allow", style=discord.ButtonStyle.blurple)+ async def allow_always(self, interaction: discord.Interaction, button: discord.ui.Button):+ await self._resolve(interaction, "always", discord.Color.purple(), "platform.discord.approval.resolved_always")++ @discord.ui.button(label="Deny", style=discord.ButtonStyle.red)+ async def deny(self, interaction: discord.Interaction, button: discord.ui.Button):+ await self._resolve(interaction, "deny", discord.Color.red(), "platform.discord.approval.resolved_deny")++ return ExecApprovalView