standalone.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """Out-of-process Signal send (cron / ``hermes send`` / send_message without a live gateway): raw
  2. signal-cli JSON-RPC with Signal formatting, attachment batching and the process-wide rate-limit
  3. scheduler shared with the gateway adapter. Wired as the plugin's ``standalone_sender_fn``."""
  4. import logging
  5. import os
  6. import time
  7. from plugins.platforms.signal import signal_rate_limit as rl
  8. from plugins.platforms.signal.signal_format import markdown_to_signal
  9. from tools.send_message_senders import _error, _success
  10. logger = logging.getLogger(__name__)
  11. async def _signal_send_batch(post, scheduler, rl, idx, n_batches, att_batch, batch_message):
  12. """One Signal batch under the scheduler with rate-limit retries: None on success, False when
  13. retries were exhausted (batch lost), error dict for a non-rate-limit RPC error."""
  14. n, max_attempts = len(att_batch), rl.SIGNAL_RATE_LIMIT_MAX_ATTEMPTS
  15. for attempt in range(1, max_attempts + 1):
  16. try:
  17. await scheduler.acquire(n)
  18. _rpc_t0 = time.monotonic()
  19. data = await post(att_batch, batch_message)
  20. if "error" not in data:
  21. await scheduler.report_rpc_duration(time.monotonic() - _rpc_t0, n)
  22. return None
  23. err = data["error"]
  24. if not rl._is_signal_rate_limit_error(err):
  25. return _error(f"Signal RPC error on batch {idx + 1}/{n_batches}: {err}")
  26. server_retry_after = rl._extract_retry_after_seconds(err)
  27. scheduler.feedback(server_retry_after, n)
  28. retry_after_label = f"{server_retry_after:.0f}s" if server_retry_after else "unknown"
  29. if attempt >= max_attempts:
  30. logger.error("Signal: rate-limit retries exhausted on batch %d/%d (%d attachments lost, "
  31. "server retry_after=%s)", idx + 1, n_batches, n, retry_after_label)
  32. return False
  33. logger.warning("Signal: rate-limited on batch %d/%d (attempt %d/%d, server retry_after=%s); "
  34. "scheduler will pace the retry",
  35. idx + 1, n_batches, attempt, max_attempts, retry_after_label)
  36. except Exception as e:
  37. if attempt >= max_attempts:
  38. logger.error("Signal: send error on batch %d/%d after %d attempts: %s",
  39. idx + 1, n_batches, attempt, str(e))
  40. return False
  41. logger.warning("Signal: transient error on batch %d/%d (attempt %d/%d): %s; will retry",
  42. idx + 1, n_batches, attempt, max_attempts, str(e))
  43. async def _send_signal(extra, chat_id, message, media_files=None):
  44. """signal-cli JSON-RPC send; attachments go in SIGNAL_MAX_ATTACHMENTS_PER_MSG batches metered
  45. by the process-wide SignalAttachmentScheduler (shared with the gateway adapter's rate-limit state)."""
  46. try:
  47. import httpx
  48. except ImportError:
  49. return {"error": "httpx not installed"}
  50. try:
  51. http_url, account = extra.get("http_url", "http://127.0.0.1:8080").rstrip("/"), extra.get("account", "")
  52. if not account:
  53. return {"error": "Signal account not configured"}
  54. valid_media = media_files or []
  55. attachment_paths = []
  56. for media_path, _is_voice in valid_media:
  57. if os.path.exists(media_path):
  58. attachment_paths.append(media_path)
  59. else:
  60. logger.warning("Signal media file not found, skipping: %s", media_path)
  61. # No attachments still means one (text-only) batch; text rides on batch #0 only.
  62. per_batch = rl.SIGNAL_MAX_ATTACHMENTS_PER_MSG
  63. att_batches = [attachment_paths[i:i + per_batch] for i in range(0, len(attachment_paths), per_batch)] or [[]]
  64. n_batches, (plain_text, text_styles) = len(att_batches), markdown_to_signal(message)
  65. recipient = {"groupId": chat_id[6:]} if chat_id.startswith("group:") else {"recipient": [chat_id]}
  66. async def _rpc_send(text, *, id_prefix, timeout, attachments=None, styled=False):
  67. params = {"account": account, "message": text, **recipient}
  68. if styled and text and text_styles:
  69. params["textStyle" if len(text_styles) == 1 else "textStyles"] = (
  70. text_styles[0] if len(text_styles) == 1 else text_styles)
  71. if attachments:
  72. params["attachments"] = attachments
  73. payload = {"jsonrpc": "2.0", "method": "send", "params": params,
  74. "id": f"{id_prefix}_{int(time.time() * 1000)}"}
  75. async with httpx.AsyncClient(timeout=timeout) as client:
  76. return await client.post(f"{http_url}/api/v1/rpc", json=payload)
  77. async def _post(batch_attachments, batch_message):
  78. resp = await _rpc_send(batch_message, id_prefix="send", attachments=batch_attachments, styled=True,
  79. timeout=rl._signal_send_timeout(len(batch_attachments)))
  80. resp.raise_for_status()
  81. return resp.json()
  82. scheduler = rl.get_scheduler()
  83. logger.info("send_message Signal: scheduler state=%s, %d attachment(s) in %d batch(es)",
  84. scheduler.state(), len(attachment_paths), n_batches)
  85. failed_batches: list[int] = []
  86. for idx, att_batch in enumerate(att_batches):
  87. n = len(att_batch)
  88. if n > 0 and (estimated := scheduler.estimate_wait(n)) >= rl.SIGNAL_BATCH_PACING_NOTICE_THRESHOLD:
  89. # Best-effort one-shot RPC for a user-facing pacing notice.
  90. try:
  91. await _rpc_send(f"(More images coming — pausing ~{rl._format_wait(estimated)} "
  92. f"for Signal rate limit, batch {idx + 1}/{n_batches}.)", id_prefix="notice", timeout=30.0)
  93. except Exception as _e:
  94. logger.warning("Signal: inline notice failed: %s", _e)
  95. outcome = await _signal_send_batch(_post, scheduler, rl, idx, n_batches, att_batch,
  96. plain_text if idx == 0 else "")
  97. if outcome is False:
  98. failed_batches.append(idx + 1)
  99. elif outcome is not None:
  100. return outcome
  101. warnings = []
  102. if len(attachment_paths) < len(valid_media):
  103. warnings.append("Some media files were skipped (not found on disk)")
  104. if failed_batches:
  105. warnings.append(f"Signal rate-limited {len(failed_batches)} batch(es) "
  106. f"(#{', #'.join(str(b) for b in failed_batches)})")
  107. if failed_batches and len(failed_batches) == n_batches:
  108. return _error(f"Signal: every batch ({n_batches}) hit rate limit; no attachments delivered")
  109. # Result-safe chat identifier for tool transcripts/log consumers.
  110. return _success("signal", "group:***" if str(chat_id).startswith("group:") else chat_id, warnings)
  111. except Exception as e:
  112. return _error(f"Signal send failed: {e}")
  113. async def standalone_send(pconfig, chat_id, message, *, thread_id=None, media_files=None, force_document=False):
  114. """``standalone_sender_fn``: attachments ride the JSON-RPC ``attachments`` param (thread_id and
  115. force_document have no Signal equivalent)."""
  116. return await _send_signal(getattr(pconfig, "extra", None) or {}, chat_id, message, media_files=media_files)