base_exec_approval.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """Shared wording for the exec-approval prompt every messaging surface renders.
  2. The button card (``BasePlatformAdapter._format_exec_approval``) and the plain-text
  3. ``/approve`` fallback (``gateway.run._format_exec_approval_fallback``) must tell the user
  4. the same three things: what Hermes wants to run, why it was flagged, and that silence means
  5. the command does NOT run once ``approvals.timeout`` elapses. Keeping the text here means one
  6. edit changes every platform; adapters only wrap these strings in their own markup.
  7. The ``EA_*`` / ``APPROVAL_TIMED_OUT_NOTICE`` module constants are the English source of truth
  8. (catalog keys ``gateway.exec_approval.*``); anything that renders the prompt calls the sibling
  9. accessors (``ea_header_text()`` …) so the active ``display.language`` is honoured at render time.
  10. No imports from ``gateway.platforms.base`` or ``gateway.run`` — both import this module.
  11. """
  12. from __future__ import annotations
  13. from agent.i18n import t
  14. # Bare strings; adapters add their own bold/HTML around them.
  15. EA_HEADER_TEXT = "Hermes wants to run a command that needs your OK"
  16. EA_REASON_LABEL_TEXT = "Why it was flagged"
  17. # Timeout notice posted when nobody answered the prompt (``{window}`` = "5 minutes").
  18. APPROVAL_TIMED_OUT_NOTICE = (
  19. "⌛ Approval timed out after {window} — the command was NOT run. "
  20. "Ask me to try again if you still want it, or raise approvals.timeout in config.yaml.")
  21. def ea_header_text() -> str:
  22. """Localized ``EA_HEADER_TEXT``."""
  23. return t("gateway.exec_approval.header")
  24. def ea_reason_label_text() -> str:
  25. """Localized ``EA_REASON_LABEL_TEXT``."""
  26. return t("gateway.exec_approval.reason_label")
  27. def ea_smart_deny_line_text() -> str:
  28. """Localized smart-deny sentence (bare; adapters add their own line breaks/markup)."""
  29. return t("gateway.exec_approval.smart_deny_line")
  30. def ea_default_reason_text() -> str:
  31. """Localized default "why it was flagged" reason when the approval carries none."""
  32. return t("gateway.exec_approval.default_reason")
  33. def ea_action_labels() -> dict[str, str]:
  34. """Localized ``choice -> button label`` for the four approval tiers."""
  35. return {
  36. "once": t("gateway.exec_approval.action_once"),
  37. "session": t("gateway.exec_approval.action_session"),
  38. "always": t("gateway.exec_approval.action_always"),
  39. "deny": t("gateway.exec_approval.action_deny"),
  40. }
  41. def approval_timed_out_notice(window: str) -> str:
  42. """Localized ``APPROVAL_TIMED_OUT_NOTICE`` for an already-formatted ``window``."""
  43. return t("gateway.exec_approval.timed_out_notice", window=window)
  44. def approval_timeout_seconds() -> int:
  45. """The configured ``approvals.timeout`` (default 300s); module attribute so tests can pin it."""
  46. from tools.approval_context import _get_approval_timeout
  47. return _get_approval_timeout()
  48. def format_approval_window(seconds: int) -> str:
  49. """Human wording for a timeout (300 → "5 minutes"); one formatter shared with the CLI notice and
  50. the tool result's ``user_summary`` — see ``tools.approval_context.format_approval_window``."""
  51. from tools.approval_context import format_approval_window as _shared
  52. return _shared(seconds)
  53. def format_approval_deadline_line(timeout_s: int) -> str:
  54. """The last line of every approval prompt: doing nothing is a safe no."""
  55. return t("gateway.exec_approval.deadline", window=format_approval_window(timeout_s))
  56. def format_approval_timed_out_notice(timeout_s: int) -> str:
  57. return approval_timed_out_notice(format_approval_window(timeout_s))