register.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """Signal plugin registration: configured/validate checks and ``register``."""
  2. import logging
  3. from gateway.config import PlatformConfig
  4. from gateway.platforms._shared import env_is_connected
  5. from gateway.platforms._shared import config_connected_extra
  6. from gateway.platforms.send_targets import NOT_EXPLICIT, StandaloneSendPolicy
  7. # Absolute imports (see __init__.py): one canonical module per file.
  8. from plugins.platforms.signal.adapter import (
  9. MAX_MESSAGE_LENGTH, SignalAdapter, check_signal_requirements, validate_signal_config)
  10. from plugins.platforms.signal.setup import interactive_setup
  11. from plugins.platforms.signal.standalone import standalone_send
  12. logger = logging.getLogger(__name__)
  13. _env_configured = env_is_connected("SIGNAL_HTTP_URL", "SIGNAL_ACCOUNT")
  14. def _is_connected(config: PlatformConfig) -> bool:
  15. """URL + account from config/scoped env, else ``.env``: a plain CLI process never loads ``.env``
  16. into the environment, and the old static setup row read it, so a ``.env``-only profile stays
  17. "configured" in ``hermes gateway setup``."""
  18. return validate_signal_config(config) or _env_configured(config)
  19. def _validate_config(config: PlatformConfig) -> bool:
  20. """``validate_config``: same warning the core instantiation path logged before the move."""
  21. if validate_signal_config(config):
  22. return True
  23. logger.warning("Signal: SIGNAL_HTTP_URL or SIGNAL_ACCOUNT not configured")
  24. return False
  25. # System-prompt platform hint (PlatformEntry.platform_hint); cached-prompt bytes, keep stable.
  26. _PLATFORM_HINT = (
  27. "You are on Signal. Standard markdown (**bold**, *italic*, ~~strike~~, # headers, `code`) auto-converts to "
  28. "Signal formatting; bullets render as \u2022. No tables \u2014 use bullets or labeled lines. "
  29. "You can send files natively: write MEDIA:/absolute/path/to/file in your response. "
  30. "Images (.png, .jpg, .webp) send as photos, other files as documents; ![alt](url) sends as photos."
  31. )
  32. def _parse_send_target(ref):
  33. # "group:<id>" is a native group target; an empty id is not explicit.
  34. stripped = ref.strip()
  35. if not stripped.startswith("group:"):
  36. return None
  37. group_id = stripped[len("group:"):].strip()
  38. return (f"group:{group_id}", None) if group_id else NOT_EXPLICIT
  39. def register(ctx) -> None:
  40. """Plugin entry point — called by the Hermes plugin system."""
  41. from . import gateway_env # env -> GatewayConfig bridge (gateway.config_env)
  42. ctx.register_platform(
  43. target_ref_syntax_fn=_parse_send_target,
  44. # Attachments ride the JSON-RPC param on the final chunk.
  45. standalone_send=StandaloneSendPolicy(label="Signal", media=True, empty_list_between_chunks=True,
  46. text_direct=True),
  47. name="signal", label="Signal", adapter_factory=SignalAdapter,
  48. dashboard_description="Connect through a signal-cli REST bridge.",
  49. docs_url="https://github.com/bbernhard/signal-cli-rest-api",
  50. dashboard_env_vars=("SIGNAL_HTTP_URL", "SIGNAL_ACCOUNT", "SIGNAL_ALLOWED_USERS"),
  51. check_fn=check_signal_requirements, validate_config=_validate_config,
  52. is_connected=_is_connected, required_env=["SIGNAL_HTTP_URL", "SIGNAL_ACCOUNT"],
  53. config_connected_fn=config_connected_extra("http_url"),
  54. install_hint="Signal: runtime requirements not met", setup_fn=interactive_setup,
  55. cron_deliver_env_var="SIGNAL_HOME_CHANNEL",
  56. # Out-of-process sends speak raw JSON-RPC and bypass SignalAdapter.send()'s chunking, so
  57. # send_message splits at the adapter's limit through this field.
  58. standalone_sender_fn=standalone_send, max_message_length=MAX_MESSAGE_LENGTH,
  59. emoji="📡", pii_safe=True, allow_update_command=True,
  60. platform_hint=_PLATFORM_HINT,
  61. display_tier="low",
  62. env_overrides=gateway_env.ENV_OVERRIDES,
  63. env_enable_credentials=gateway_env.ENV_ENABLE_CREDENTIALS, **gateway_env.AUTHZ,
  64. )