Skip to content

The agent that grows with you

refactor(tools): dedupe send_message helpers (success payloads, target resolve, signal batch, slack forms)

Teknium79f0c2884085
diff --git a/tools/react_to_message_tool.py b/tools/react_to_message_tool.py
index fa37347fda4..4859d25f38d 100644
--- a/tools/react_to_message_tool.py
+++ b/tools/react_to_message_tool.py
@@ -1,12 +1,12 @@
#!/usr/bin/env python3
"""Let the agent react to a message with an emoji in the Hermes desktop app.
-The conversational counterpart to the user's tapback: same reaction store,
-same one-per-author semantics, written with ``author="agent"``. Lives in the
-``desktop_ui`` toolset so it costs nothing on other surfaces (platform adapters
-expose reactions via ``send_message(action="react")``). Defaults to the message
-that triggered this turn so the model needn't thread row ids through tool calls,
-and emits ``message.reaction`` so the renderer paints it without a resume.
+The conversational counterpart to the user's tapback: same reaction store, same
+one-per-author semantics, written with ``author="agent"``. Lives in the ``desktop_ui``
+toolset so it costs nothing on other surfaces (platform adapters expose reactions via
+``send_message(action="react")``). Defaults to the message that triggered this turn so
+the model needn't thread row ids through tool calls, and emits ``message.reaction`` so
+the renderer paints it without a resume.
"""
import json
@@ -30,15 +30,14 @@ def _react(emoji: str, message_row_id, messages_back, *, db, session_key: str) -
row_id = message_row_id
target_role = "user"
if row_id is None:
- # Default target: the latest user message. `messages_back` steps to earlier
- # user turns (1 = the one before) — ids aren't visible to the model and
- # quoting text is ambiguous, but "two messages ago" is how a person thinks.
+ # Default target: the latest user message. `messages_back` steps to earlier user
+ # turns (1 = the one before) — ids aren't visible to the model and quoting text is
+ # ambiguous, but "two messages ago" is how a person thinks.
back = max(0, int(messages_back or 0))
row_id = db.latest_message_row_id(session_key, role="user", offset=back)
if row_id is None:
return tool_error(
- f"No user message found {back} back." if back else "No user message to react to yet."
- )
+ f"No user message found {back} back." if back else "No user message to react to yet.")
else:
target_role = db.get_message_role(session_key, int(row_id)) or "user"
@@ -49,28 +48,22 @@ def _react(emoji: str, message_row_id, messages_back, *, db, session_key: str) -
if reactions is None:
return tool_error(f"Message {row_id} is not part of this conversation.")
- # Paint it live. A missing bridge (non-desktop surface) is not an error — the
- # reaction is persisted and shows on next load. `role` lets the renderer match
- # a live message that doesn't know its durable row id yet (learned on resume).
+ # Paint it live. A missing bridge (non-desktop surface) is not an error — the reaction
+ # is persisted and shows on next load. `role` lets the renderer match a live message
+ # that doesn't know its durable row id yet (learned on resume).
try:
desktop_ui.emit(
- "message.reaction",
- {"row_id": int(row_id), "reactions": reactions, "role": target_role},
- )
+ "message.reaction", {"row_id": int(row_id), "reactions": reactions, "role": target_role})
except Exception:
pass
- return json.dumps(
- {"success": True, "row_id": int(row_id), "reactions": reactions}, ensure_ascii=False
- )
+ return json.dumps({"success": True, "row_id": int(row_id), "reactions": reactions}, ensure_ascii=False)
def react_to_message_tool(emoji: str, message_row_id=None, messages_back=None) -> str:
"""Attach (or with an empty ``emoji`` retract) the agent's reaction."""
emoji = (emoji or "").strip()
- session_key = get_session_env("HERMES_SESSION_KEY", "") or get_session_env(
- "HERMES_SESSION_ID", ""
- )
+ session_key = get_session_env("HERMES_SESSION_KEY", "") or get_session_env("HERMES_SESSION_ID", "")
if not session_key:
return tool_error("No active session — reactions need a persisted conversation.")
diff --git a/tools/send_message_senders.py b/tools/send_message_senders.py
index 556c859a0e5..4357f78856c 100644
--- a/tools/send_message_senders.py
+++ b/tools/send_message_senders.py
@@ -14,23 +14,19 @@ _IMAGE_EXTS = {".jpg", ".jpeg", ".png", ".webp", ".gif"}
_VIDEO_EXTS = {".mp4", ".mov", ".avi", ".mkv", ".webm", ".3gp"}
_AUDIO_EXTS = {".ogg", ".opus", ".mp3", ".m2a", ".wav", ".m4a", ".flac"}
_VOICE_EXTS = {".ogg", ".opus"}
-# Telegram's Bot API sendAudio only accepts MP3 / M4A. Other audio
-# formats either route through sendVoice (Opus/OGG) or fall back to
-# document delivery.
+# Telegram's sendAudio only accepts MP3 / M4A; other audio goes via sendVoice
+# (Opus/OGG) or falls back to document delivery.
_TELEGRAM_SEND_AUDIO_EXTS = {".mp3", ".m4a"}
-# Extensions that carry a native caption on the media bubble itself
-# (photo/video/document). Voice/audio notes are excluded: a caption on a
-# voice note reads as a separate label rather than a bubble caption, and the
-# established convention is to keep the accompanying text as its own message.
+# Extensions that carry a native caption on the media bubble (photo/video/document).
+# Voice/audio notes are excluded: a caption on a voice note reads as a separate label,
+# so the accompanying text stays its own message.
_CAPTIONABLE_EXTS = _IMAGE_EXTS | _VIDEO_EXTS | {
".pdf", ".doc", ".docx", ".txt", ".md", ".csv", ".xlsx", ".zip",
}
-# Per-platform native caption length limits (characters). Text longer than
-# the limit can't ride on the media bubble and stays a separate body message.
-# Telegram's photo/video caption cap is 1024; WhatsApp and Discord are far
-# more generous, so a conservative shared ceiling keeps behavior predictable.
+# Native caption length limits (characters). Telegram's photo/video cap is 1024;
+# WhatsApp/Discord are far more generous, so one conservative shared ceiling.
_TELEGRAM_CAPTION_LIMIT = 1024
_DEFAULT_CAPTION_LIMIT = 4096
@@ -38,16 +34,15 @@ _DEFAULT_CAPTION_LIMIT = 4096
def _media_caption_split(text, media_files, *, max_caption_len):
"""Decide whether the accompanying text rides on the media bubble.
- Single chokepoint for ``MEDIA:<path> caption`` across every sender.
- Returns ``(caption, "")`` — text becomes the native caption, no separate
- body message — only for exactly one media file of a captionable kind
- (image/video/document, not a voice/audio note) whose text fits
- ``max_caption_len``. Otherwise ``(None, text)``: text goes as a separate
- message (multi-file caption→file association is ambiguous).
+ Single chokepoint for ``MEDIA:<path> caption`` across every sender. Returns
+ ``(caption, "")`` — text becomes the native caption, no separate body — only for
+ exactly one captionable file (not a voice/audio note) whose text fits
+ ``max_caption_len``. Otherwise ``(None, text)``: text goes as a separate message
+ (multi-file caption→file association is ambiguous).
- Length is measured in codepoints: never under-counts Telegram's UTF-16
- units for BMP text, so over-counting only fails safe. The Telegram sender
- re-checks the *formatted* caption since escaping inflates it.
+ Length is measured in codepoints: never under-counts Telegram's UTF-16 units for
+ BMP text, so over-counting only fails safe. The Telegram sender re-checks the
+ *formatted* caption since escaping inflates it.
"""
stripped = (text or "").strip()
media = media_files or []
@@ -59,6 +54,8 @@ def _media_caption_split(text, media_files, *, max_caption_len):
if len(stripped) > max_caption_len:
return None, text
return stripped, ""
+
+
_URL_SECRET_QUERY_RE = re.compile(
r"([?&](?:access_token|api[_-]?key|auth[_-]?token|token|signature|sig)=)([^&#\s]+)",
re.IGNORECASE,
@@ -73,8 +70,7 @@ def _sanitize_error_text(text) -> str:
"""Redact secrets from error text before surfacing it to users/models."""
redacted = redact_sensitive_text(text)
redacted = _URL_SECRET_QUERY_RE.sub(lambda m: f"{m.group(1)}***", redacted)
- redacted = _GENERIC_SECRET_ASSIGN_RE.sub(lambda m: f"{m.group(1)}=***", redacted)
- return redacted
+ return _GENERIC_SECRET_ASSIGN_RE.sub(lambda m: f"{m.group(1)}=***", redacted)
def _error(message: str) -> dict:
@@ -82,6 +78,14 @@ def _error(message: str) -> dict:
return {"error": _sanitize_error_text(message)}
+def _success(platform: str, chat_id, warnings=None, **fields) -> dict:
+ """Standard success payload; ``warnings`` is only included when non-empty."""
+ result = {"success": True, "platform": platform, "chat_id": chat_id, **fields}
+ if warnings:
+ result["warnings"] = warnings
+ return result
+
+
def _display_chat_id(platform_name: str, chat_id: str) -> str:
"""Return a result-safe chat identifier for tool transcripts/log consumers."""
if platform_name == "signal" and str(chat_id).startswith("group:"):
@@ -89,6 +93,8 @@ def _display_chat_id(platform_name: str, chat_id: str) -> str:
return chat_id
+_NO_DELIVERABLE = "No deliverable text or media remained after processing MEDIA tags"
+
_TELEGRAM_TRANSIENT_MARKERS = (
"bad gateway", "502", "too many requests", "429",
"service unavailable", "503", "gateway timeout", "504",
@@ -125,10 +131,7 @@ async def _send_telegram_message_with_retry(bot, *, attempts: int = 3, **kwargs)
raise
logger.warning(
"Transient Telegram send failure (attempt %d/%d), retrying in %.1fs: %s",
- attempt + 1,
- attempts,
- delay,
- _sanitize_error_text(exc),
+ attempt + 1, attempts, delay, _sanitize_error_text(exc),
)
await asyncio.sleep(delay)
@@ -205,10 +208,12 @@ async def _telegram_send_text_chunk(bot, chat_id, chunk, parse_mode, has_html, t
"""Send one formatted text chunk with the adapter-matching fallbacks:
thread-not-found -> retry without ``message_thread_id`` (dropped from
``text_kwargs`` for later chunks too); parse failure -> plain text."""
- try:
+ async def send(text, mode):
return await _send_telegram_message_with_retry(
- bot, chat_id=chat_id, text=chunk, parse_mode=parse_mode, **text_kwargs
- )
+ bot, chat_id=chat_id, text=text, parse_mode=mode, **text_kwargs)
+
+ try:
+ return await send(chunk, parse_mode)
except Exception as md_error:
if _is_telegram_thread_not_found(md_error) and text_kwargs.get("message_thread_id") is not None:
logger.warning(
@@ -216,20 +221,14 @@ async def _telegram_send_text_chunk(bot, chat_id, chunk, parse_mode, has_html, t
text_kwargs.get("message_thread_id"),
)
text_kwargs.pop("message_thread_id", None)
- return await _send_telegram_message_with_retry(
- bot, chat_id=chat_id, text=chunk, parse_mode=parse_mode, **text_kwargs
- )
+ return await send(chunk, parse_mode)
err_text = str(md_error).lower()
if "parse" in err_text or "markdown" in err_text or "html" in err_text:
logger.warning(
"Parse mode %s failed in _send_telegram, falling back to plain text: %s",
- parse_mode,
- _sanitize_error_text(md_error),
- )
- plain = chunk if has_html else _strip_mdv2_safe(chunk)
- return await _send_telegram_message_with_retry(
- bot, chat_id=chat_id, text=plain, parse_mode=None, **text_kwargs
+ parse_mode, _sanitize_error_text(md_error),
)
+ return await send(chunk if has_html else _strip_mdv2_safe(chunk), None)
raise
@@ -288,10 +287,10 @@ async def _telegram_send_one_media(
async def _send_telegram(token, chat_id, message, media_files=None, thread_id=None, disable_link_previews=False, force_document=False):
"""Send via Telegram Bot API (one-shot, no polling needed).
- Markdown is converted to MarkdownV2 via the gateway adapter's
- ``format_message`` so bold/links/headers render; a message that already
- contains HTML tags skips that and is sent with ``parse_mode='HTML'``.
- Parse failures fall back to plain text so the message still delivers.
+ Markdown is converted to MarkdownV2 via the gateway adapter's ``format_message``
+ so bold/links/headers render; a message that already contains HTML tags skips
+ that and is sent with ``parse_mode='HTML'``. Parse failures fall back to plain
+ text so the message still delivers.
"""
try:
from telegram.constants import ParseMode
@@ -324,35 +323,30 @@ async def _send_telegram(token, chat_id, message, media_files=None, thread_id=No
last_msg = None
warnings = []
- # MEDIA caption: a single captionable file + short text rides on the
- # bubble as its *formatted* caption. Formatting can inflate a raw
- # <1024 string past Telegram's 1024 cap, so re-check in UTF-16 units
- # and fall back to a separate body message.
+ # MEDIA caption: a single captionable file + short text rides on the bubble as
+ # its *formatted* caption. Formatting can inflate a raw <1024 string past
+ # Telegram's cap, so re-check in UTF-16 units and fall back to a separate body.
_tg_caption = None
from gateway.platforms.base import BasePlatformAdapter, utf16_len
- _cap, _ = _media_caption_split(
- message, media_files, max_caption_len=_TELEGRAM_CAPTION_LIMIT
- )
+ _cap, _ = _media_caption_split(message, media_files, max_caption_len=_TELEGRAM_CAPTION_LIMIT)
if _cap is not None and utf16_len(formatted) <= _TELEGRAM_CAPTION_LIMIT:
_tg_caption = formatted
formatted = "" # suppress the separate text send below
if formatted.strip():
- # Chunk *after* formatting, in UTF-16 units: MarkdownV2/HTML
- # escaping inflates text, so a raw-<4096 message can exceed the
- # limit once formatted and be rejected as "Message is too long".
+ # Chunk *after* formatting, in UTF-16 units: MarkdownV2/HTML escaping inflates
+ # text, so a raw-<4096 message can exceed the limit once formatted.
for chunk in BasePlatformAdapter.truncate_message(formatted, 4096, len_fn=utf16_len):
last_msg = await _telegram_send_text_chunk(
- bot, int_chat_id, chunk, send_parse_mode, _has_html, text_kwargs
- )
+ bot, int_chat_id, chunk, send_parse_mode, _has_html, text_kwargs)
for media_path, is_voice in media_files:
if not os.path.exists(media_path):
warning = f"Media file not found, skipping: {media_path}"
logger.warning(warning)
warnings.append(warning)
- # Caption mode suppressed the text send; if the file it was
- # meant to caption is gone, deliver the words on their own.
+ # Caption mode suppressed the text send; if the file it was meant to
+ # caption is gone, deliver the words on their own.
if _tg_caption is not None and last_msg is None:
try:
last_msg = await _send_telegram_message_with_retry(
@@ -379,20 +373,10 @@ async def _send_telegram(token, chat_id, message, media_files=None, thread_id=No
warnings.append(warning)
if last_msg is None:
- error = "No deliverable text or media remained after processing MEDIA tags"
if warnings:
- return {"error": error, "warnings": warnings}
- return {"error": error}
-
- result = {
- "success": True,
- "platform": "telegram",
- "chat_id": chat_id,
- "message_id": str(last_msg.message_id),
- }
- if warnings:
- result["warnings"] = warnings
- return result
+ return {"error": _NO_DELIVERABLE, "warnings": warnings}
+ return {"error": _NO_DELIVERABLE}
+ return _success("telegram", chat_id, warnings, message_id=str(last_msg.message_id))
except ImportError:
return {"error": "python-telegram-bot not installed. Run: pip install python-telegram-bot"}
except Exception as e:
@@ -505,6 +489,55 @@ async def _resolve_slack_user_target(token, chat_id):
return None, _error(f"Slack DM resolution failed: {e}")
+async def _signal_send_batch(post, scheduler, rl, idx, n_batches, att_batch, batch_message):
+ """Send one Signal batch under the scheduler with rate-limit retries.
+ Returns None on success, False when retries were exhausted (batch lost),
+ or an error dict for a non-rate-limit RPC error."""
+ n = len(att_batch)
+ for attempt in range(1, rl.SIGNAL_RATE_LIMIT_MAX_ATTEMPTS + 1):
+ try:
+ await scheduler.acquire(n)
+ _rpc_t0 = time.monotonic()
+ data = await post(att_batch, batch_message)
+ _rpc_duration = time.monotonic() - _rpc_t0
+ if "error" not in data:
+ await scheduler.report_rpc_duration(_rpc_duration, n)
+ return None
+
+ err = data["error"]
+ if not rl._is_signal_rate_limit_error(err):
+ return _error(f"Signal RPC error on batch {idx + 1}/{n_batches}: {err}")
+
+ server_retry_after = rl._extract_retry_after_seconds(err)
+ scheduler.feedback(server_retry_after, n)
+ retry_after_label = f"{server_retry_after:.0f}s" if server_retry_after else "unknown"
+
+ if attempt >= rl.SIGNAL_RATE_LIMIT_MAX_ATTEMPTS:
+ logger.error(
+ "Signal: rate-limit retries exhausted on batch %d/%d "
+ "(%d attachments lost, server retry_after=%s)",
+ idx + 1, n_batches, n, retry_after_label,
+ )
+ return False
+ logger.warning(
+ "Signal: rate-limited on batch %d/%d "
+ "(attempt %d/%d, server retry_after=%s); "
+ "scheduler will pace the retry",
+ idx + 1, n_batches, attempt, rl.SIGNAL_RATE_LIMIT_MAX_ATTEMPTS, retry_after_label,
+ )
+ except Exception as e:
+ if attempt >= rl.SIGNAL_RATE_LIMIT_MAX_ATTEMPTS:
+ logger.error(
+ "Signal: send error on batch %d/%d after %d attempts: %s",
+ idx + 1, n_batches, attempt, str(e)
+ )
+ return False
+ logger.warning(
+ "Signal: transient error on batch %d/%d (attempt %d/%d): %s; will retry",
+ idx + 1, n_batches, attempt, rl.SIGNAL_RATE_LIMIT_MAX_ATTEMPTS, str(e)
+ )
+
+
async def _send_signal(extra, chat_id, message, media_files=None):
"""Send via signal-cli JSON-RPC, text and/or attachments.
@@ -517,16 +550,7 @@ async def _send_signal(extra, chat_id, message, media_files=None):
except ImportError:
return {"error": "httpx not installed"}
- from gateway.platforms.signal_rate_limit import (
- SIGNAL_BATCH_PACING_NOTICE_THRESHOLD,
- SIGNAL_MAX_ATTACHMENTS_PER_MSG,
- SIGNAL_RATE_LIMIT_MAX_ATTEMPTS,
- _extract_retry_after_seconds,
- _format_wait,
- _is_signal_rate_limit_error,
- _signal_send_timeout,
- get_scheduler,
- )
+ from gateway.platforms import signal_rate_limit as rl
from gateway.platforms.signal_format import markdown_to_signal
try:
@@ -545,13 +569,11 @@ async def _send_signal(extra, chat_id, message, media_files=None):
# No attachments still means one (text-only) batch; with attachments
# the text rides on batch #0 so it isn't repeated per batch.
- if attachment_paths:
- att_batches = [
- attachment_paths[i:i + SIGNAL_MAX_ATTACHMENTS_PER_MSG]
- for i in range(0, len(attachment_paths), SIGNAL_MAX_ATTACHMENTS_PER_MSG)
- ]
- else:
- att_batches = [[]]
+ per_batch = rl.SIGNAL_MAX_ATTACHMENTS_PER_MSG
+ att_batches = [
+ attachment_paths[i:i + per_batch] for i in range(0, len(attachment_paths), per_batch)
+ ] or [[]]
+ n_batches = len(att_batches)
plain_text, text_styles = markdown_to_signal(message)
@@ -582,83 +604,38 @@ async def _send_signal(extra, chat_id, message, media_files=None):
params["textStyles"] = text_styles
if batch_attachments:
params["attachments"] = batch_attachments
- timeout = _signal_send_timeout(len(batch_attachments) if batch_attachments else 0)
+ timeout = rl._signal_send_timeout(len(batch_attachments) if batch_attachments else 0)
resp = await _rpc_send(params, id_prefix="send", timeout=timeout)
resp.raise_for_status()
return resp.json()
- async def _send_inline_notice(text: str) -> None:
- """Best-effort one-shot RPC for a user-facing pacing notice."""
- try:
- await _rpc_send(_rpc_params(text), id_prefix="notice", timeout=30.0)
- except Exception as _e:
- logger.warning("Signal: inline notice failed: %s", _e)
-
- scheduler = get_scheduler()
+ scheduler = rl.get_scheduler()
logger.info(
"send_message Signal: scheduler state=%s, %d attachment(s) in %d batch(es)",
- scheduler.state(), len(attachment_paths), len(att_batches),
+ scheduler.state(), len(attachment_paths), n_batches,
)
failed_batches: list[int] = []
for idx, att_batch in enumerate(att_batches):
n = len(att_batch)
if n > 0:
estimated = scheduler.estimate_wait(n)
- if estimated >= SIGNAL_BATCH_PACING_NOTICE_THRESHOLD:
- await _send_inline_notice(
- f"(More images coming — pausing ~{_format_wait(estimated)} "
- f"for Signal rate limit, batch {idx + 1}/{len(att_batches)}.)"
+ if estimated >= rl.SIGNAL_BATCH_PACING_NOTICE_THRESHOLD:
+ # Best-effort one-shot RPC for a user-facing pacing notice.
+ notice = (
+ f"(More images coming — pausing ~{rl._format_wait(estimated)} "
+ f"for Signal rate limit, batch {idx + 1}/{n_batches}.)"
)
+ try:
+ await _rpc_send(_rpc_params(notice), id_prefix="notice", timeout=30.0)
+ except Exception as _e:
+ logger.warning("Signal: inline notice failed: %s", _e)
- batch_message = plain_text if idx == 0 else ""
-
- for attempt in range(1, SIGNAL_RATE_LIMIT_MAX_ATTEMPTS + 1):
- try:
- await scheduler.acquire(n)
- _rpc_t0 = time.monotonic()
- data = await _post(att_batch, batch_message)
- _rpc_duration = time.monotonic() - _rpc_t0
- if "error" not in data:
- await scheduler.report_rpc_duration(_rpc_duration, n)
- break
-
- err = data["error"]
-
- if not _is_signal_rate_limit_error(err):
- return _error(f"Signal RPC error on batch {idx + 1}/{len(att_batches)}: {err}")
-
- server_retry_after = _extract_retry_after_seconds(err)
- scheduler.feedback(server_retry_after, n)
-
- if attempt >= SIGNAL_RATE_LIMIT_MAX_ATTEMPTS:
- failed_batches.append(idx + 1)
- logger.error(
- "Signal: rate-limit retries exhausted on batch %d/%d "
- "(%d attachments lost, server retry_after=%s)",
- idx + 1, len(att_batches), n,
- f"{server_retry_after:.0f}s" if server_retry_after else "unknown",
- )
- break
- logger.warning(
- "Signal: rate-limited on batch %d/%d "
- "(attempt %d/%d, server retry_after=%s); "
- "scheduler will pace the retry",
- idx + 1, len(att_batches),
- attempt, SIGNAL_RATE_LIMIT_MAX_ATTEMPTS,
- f"{server_retry_after:.0f}s" if server_retry_after else "unknown",
- )
- except Exception as e:
- if attempt >= SIGNAL_RATE_LIMIT_MAX_ATTEMPTS:
- failed_batches.append(idx + 1)
- logger.error(
- "Signal: send error on batch %d/%d after %d attempts: %s",
- idx + 1, len(att_batches), attempt, str(e)
- )
- break
- logger.warning(
- "Signal: transient error on batch %d/%d (attempt %d/%d): %s; will retry",
- idx + 1, len(att_batches), attempt, SIGNAL_RATE_LIMIT_MAX_ATTEMPTS, str(e)
- )
+ outcome = await _signal_send_batch(
+ _post, scheduler, rl, idx, n_batches, att_batch, plain_text if idx == 0 else "")
+ if outcome is False:
+ failed_batches.append(idx + 1)
+ elif outcome is not None:
+ return outcome
warnings = []
if len(attachment_paths) < len(valid_media):
@@ -669,16 +646,13 @@ async def _send_signal(extra, chat_id, message, media_files=None):
f"(#{', #'.join(str(b) for b in failed_batches)})"
)
- if failed_batches and len(failed_batches) == len(att_batches):
+ if failed_batches and len(failed_batches) == n_batches:
return _error(
- f"Signal: every batch ({len(att_batches)}) hit rate limit; "
+ f"Signal: every batch ({n_batches}) hit rate limit; "
f"no attachments delivered"
)
- result = {"success": True, "platform": "signal", "chat_id": _display_chat_id("signal", chat_id)}
- if warnings:
- result["warnings"] = warnings
- return result
+ return _success("signal", _display_chat_id("signal", chat_id), warnings)
except Exception as e:
return _error(f"Signal send failed: {e}")
@@ -686,10 +660,10 @@ async def _send_signal(extra, chat_id, message, media_files=None):
async def _send_matrix_via_adapter(pconfig, chat_id, message, media_files=None, thread_id=None):
"""Send via the Matrix adapter so native media uploads are preserved.
- Prefer the live gateway adapter: one persistent olm/megolm session for all
- sends. Ephemeral per-send connects re-init E2EE and claim one-time keys,
- which under bursts exhausts recipient OTKs and silently drops messages —
- so the ephemeral connect/disconnect path is only for standalone/cron.
+ Prefer the live gateway adapter: one persistent olm/megolm session for all sends.
+ Ephemeral per-send connects re-init E2EE and claim one-time keys, which under
+ bursts exhausts recipient OTKs and silently drops messages — so the ephemeral
+ connect/disconnect path is only for standalone/cron.
"""
media_files = media_files or []
metadata = {"thread_id": thread_id} if thread_id else None
@@ -713,9 +687,7 @@ async def _send_matrix_via_adapter(pconfig, chat_id, message, media_files=None,
if live_adapter is not None:
# Owned by the gateway — must NOT be disconnected; return before the
# ephemeral adapter (and its ``finally`` disconnect) exists.
- return await _matrix_send_core(
- live_adapter, chat_id, message, media_files, metadata
- )
+ return await _matrix_send_core(live_adapter, chat_id, message, media_files, metadata)
# --- Fallback: ephemeral adapter (standalone / cron context) ---
try:
@@ -728,9 +700,7 @@ async def _send_matrix_via_adapter(pconfig, chat_id, message, media_files=None,
connected = await adapter.connect()
if not connected:
return _error("Matrix connect failed")
- return await _matrix_send_core(
- adapter, chat_id, message, media_files, metadata
- )
+ return await _matrix_send_core(adapter, chat_id, message, media_files, metadata)
except Exception as e:
return _error(f"Matrix send failed: {e}")
finally:
@@ -768,14 +738,8 @@ async def _matrix_send_core(adapter, chat_id, message, media_files, metadata):
return _error(f"Matrix media send failed: {last_result.error}")
if last_result is None:
- return {"error": "No deliverable text or media remained after processing MEDIA tags"}
-
- return {
- "success": True,
- "platform": "matrix",
- "chat_id": chat_id,
- "message_id": last_result.message_id,
- }
+ return {"error": _NO_DELIVERABLE}
+ return _success("matrix", chat_id, message_id=last_result.message_id)
async def _send_weixin(pconfig, chat_id, message, media_files=None):
@@ -789,11 +753,8 @@ async def _send_weixin(pconfig, chat_id, message, media_files=None):
try:
return await send_weixin_direct(
- extra=pconfig.extra,
- token=pconfig.token,
- chat_id=chat_id,
- message=message,
- media_files=media_files,
+ extra=pconfig.extra, token=pconfig.token, chat_id=chat_id,
+ message=message, media_files=media_files,
)
except Exception as e:
return _error(f"Weixin send failed: {e}")
@@ -810,8 +771,7 @@ async def _send_bluebubbles(extra, chat_id, message):
try:
from gateway.config import PlatformConfig
- pconfig = PlatformConfig(extra=extra)
- adapter = BlueBubblesAdapter(pconfig)
+ adapter = BlueBubblesAdapter(PlatformConfig(extra=extra))
connected = await adapter.connect()
if not connected:
return _error("BlueBubbles: failed to connect to server")
@@ -819,7 +779,7 @@ async def _send_bluebubbles(extra, chat_id, message):
result = await adapter.send(chat_id, message)
if not result.success:
return _error(f"BlueBubbles send failed: {result.error}")
- return {"success": True, "platform": "bluebubbles", "chat_id": chat_id, "message_id": result.message_id}
+ return _success("bluebubbles", chat_id, message_id=result.message_id)
finally:
await adapter.disconnect()
except Exception as e:
@@ -846,20 +806,18 @@ async def _send_qqbot(pconfig, chat_id, message):
try:
async with httpx.AsyncClient(timeout=15) as client:
- # Step 1: Get access token
token_resp = await client.post(
"https://bots.qq.com/app/getAppAccessToken",
json={"appId": str(appid), "clientSecret": str(secret)},
)
if token_resp.status_code != 200:
return _error(f"QQBot token request failed: {token_resp.status_code}")
- token_data = token_resp.json()
- access_token = token_data.get("access_token")
+ access_token = token_resp.json().get("access_token")
if not access_token:
return _error("QQBot: no access_token in response")
- # Step 2: QQ Bot API has separate endpoints for guild channels, C2C
- # (private) and groups; try them in that order, first 2xx wins.
+ # QQ Bot API has separate endpoints for guild channels, C2C (private)
+ # and groups; try them in that order, first 2xx wins.
headers = {
"Authorization": f"QQBot {access_token}",
"Content-Type": "application/json",
@@ -874,8 +832,7 @@ async def _send_qqbot(pconfig, chat_id, message):
for kind, url in endpoints:
resp = await client.post(url, json=payload, headers=headers)
if resp.status_code in {200, 201}:
- return {"success": True, "platform": "qqbot", "chat_id": chat_id,
- "message_id": resp.json().get("id")}
+ return _success("qqbot", chat_id, message_id=resp.json().get("id"))
statuses.append(f"{kind}={resp.status_code}")
return _error(f"QQBot send failed: {' '.join(statuses)}")
except Exception as e:
diff --git a/tools/send_message_targets.py b/tools/send_message_targets.py
index ec2fc5210d8..1cd5c57a2f2 100644
--- a/tools/send_message_targets.py
+++ b/tools/send_message_targets.py
@@ -7,12 +7,10 @@ logger = logging.getLogger("tools.send_message_tool")
_TELEGRAM_TOPIC_TARGET_RE = re.compile(r"^\s*(-?\d+)(?::(\d+))?\s*$")
_FEISHU_TARGET_RE = re.compile(r"^\s*((?:oc|ou|on|chat|open)_[-A-Za-z0-9]+)(?::([-A-Za-z0-9_]+))?\s*$")
-# Slack conversation IDs: C (public channel), G (private/group channel), D (DM).
-# Must be uppercase alphanumeric, 9+ chars. User IDs (U...) are parsed as
-# explicit user targets (``user:U...``) and are converted to D... conversations
-# via conversations.open before chat.postMessage — posting directly to a U/W
-# ID fails because the API requires a conversation ID. ``@handle`` targets are
-# resolved through users.list first (``user_name:...``).
+# Slack conversation IDs: C (public), G (private/group), D (DM); uppercase alnum, 9+ chars.
+# User IDs (U...) become ``user:U...`` and are opened as D... conversations before
+# chat.postMessage (posting straight to a U/W id fails); ``@handle`` -> ``user_name:...``
+# is resolved through users.list first.
_SLACK_TARGET_RE = re.compile(r"^\s*([CGD][A-Z0-9]{8,})\s*$")
_SLACK_USER_ID_RE = re.compile(r"^\s*(U[A-Z0-9]{8,})\s*$")
_SLACK_USER_NAME_RE = re.compile(r"^\s*@([A-Za-z0-9._-]{1,80})\s*$")
@@ -23,47 +21,32 @@ _WEIXIN_TARGET_RE = re.compile(r"^\s*((?:wxid|gh|v\d+|wm|wb)_[A-Za-z0-9_-]+|[A-Z
_YUANBAO_TARGET_RE = re.compile(r"^\s*((?:group|direct):[^:]+)\s*$")
# Discord snowflake IDs are numeric, same regex pattern as Telegram topic targets.
_NUMERIC_TOPIC_RE = _TELEGRAM_TOPIC_TARGET_RE
-# Platforms that address recipients by phone number and accept E.164 format
-# (with a leading '+'). Without this, "+15551234567" fails the isdigit() check
-# below and falls through to channel-name resolution, which has no way to
-# resolve a raw phone number. Keeping the '+' preserves the E.164 form that
-# downstream adapters (signal, etc.) expect.
+# Platforms addressing recipients by E.164 phone number ("+1555..."): without this the
+# '+' fails the isdigit() rule and falls through to channel-name resolution, which cannot
+# resolve a raw number. The '+' is kept because the adapters expect the E.164 form.
_PHONE_PLATFORMS = frozenset({"photon", "signal", "sms", "whatsapp"})
_E164_TARGET_RE = re.compile(r"^\s*\+(\d{7,15})\s*$")
# Photon DM chat GUID (mirrors _DM_CHAT_GUID_RE in the photon adapter).
_PHOTON_DM_GUID_RE = re.compile(r"^any;-;\+\d{6,}$")
-# WhatsApp JIDs: group chats (<digits>@g.us), individual users
-# (<phone>@s.whatsapp.net), linked identities (<id>@lid), and broadcast /
-# newsletter chats. These are explicit native targets the bridge accepts
-# verbatim — they must NOT fall through to home-channel resolution.
+# WhatsApp JIDs (groups <digits>@g.us, users <phone>@s.whatsapp.net, linked ids @lid,
+# broadcast/newsletter): native targets the bridge accepts verbatim — never home-channel.
_WHATSAPP_JID_RE = re.compile(
- r"^\s*[\w-]+@(?:g\.us|s\.whatsapp\.net|lid|broadcast|newsletter)\s*$",
- re.IGNORECASE,
-)
-# Buzz channels and DMs use native UUID identifiers. They are explicit
-# targets and must never substitute the configured home channel.
+ r"^\s*[\w-]+@(?:g\.us|s\.whatsapp\.net|lid|broadcast|newsletter)\s*$", re.IGNORECASE)
+# Buzz channels/DMs are native UUIDs: explicit targets, never the home channel.
_BUZZ_UUID_RE = re.compile(
- r"^\s*[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\s*$",
- re.IGNORECASE,
-)
-# Email addresses — a valid email like "[email protected]" should be treated as
-# an explicit target for the email platform, not fall through to channel-name
-# resolution which has no way to resolve a raw address.
+ r"^\s*[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\s*$", re.IGNORECASE)
+# A valid address is an explicit email target, not a channel name to resolve.
_EMAIL_TARGET_RE = re.compile(r"^\s*[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\s*$")
-# Most platforms read their home channel from "<PLATFORM>_HOME_CHANNEL", but a
-# few diverge. Email reads EMAIL_HOME_ADDRESS (see gateway/config.py), so the
-# generic "<PLATFORM>_HOME_CHANNEL" hint would point users at a variable that is
-# never read. Map the exceptions so the error guidance is actually actionable.
+# Home-channel env var exceptions to "<PLATFORM>_HOME_CHANNEL" (email reads
+# EMAIL_HOME_ADDRESS, see gateway/config.py) so the error guidance is actionable.
_HOME_CHANNEL_ENV_OVERRIDES = {"email": "EMAIL_HOME_ADDRESS"}
-
_UNRESOLVED = object() # sentinel: stop parsing, target is NOT explicit (skip generic rules)
-# Per-platform explicit-target parsers: target_ref -> (chat_id, thread_id),
-# None to fall through to the generic rules in _parse_target_ref, or
-# _UNRESOLVED. Order inside each parser matters (e.g. Slack thread form
-# before bare conversation id).
+# Per-platform explicit-target parsers: target_ref -> (chat_id, thread_id), None to fall
+# through to the generic rules in _parse_target_ref, or _UNRESOLVED. Order inside each
+# parser matters (e.g. Slack thread form before bare conversation id).
def _parse_regex_groups(regex, *, thread_group=True):
"""Explicit when ``regex`` fully matches: chat_id = group 1, thread = group 2 (or None)."""
def parse(ref):
@@ -74,6 +57,13 @@ def _parse_regex_groups(regex, *, thread_group=True):
return parse
+def _parse_regex_stripped(regex):
+ """Explicit when ``regex`` fully matches; returns the stripped ref verbatim."""
+ def parse(ref):
+ return (ref.strip(), None) if regex.fullmatch(ref) else None
+ return parse
+
+
def _parse_telegram(ref):
# "<chat_id>[:<topic_id>]" or an @username (usernames must not be force-int'd).
parsed = _parse_regex_groups(_TELEGRAM_TOPIC_TARGET_RE)(ref)
@@ -85,24 +75,27 @@ def _parse_telegram(ref):
return (username, None) if username else None
+# (regex, chat_id template, thread comes from group 2) — thread form before bare id.
+_SLACK_FORMS = (
+ (_SLACK_THREAD_TARGET_RE, "{}", True),
+ (_SLACK_TARGET_RE, "{}", False),
+ (_SLACK_USER_ID_RE, "user:{}", False),
+ (_SLACK_MENTION_RE, "user:{}", False),
+ (_SLACK_USER_NAME_RE, "user_name:{}", False),
+)
+
+
def _parse_slack(ref):
- match = _SLACK_THREAD_TARGET_RE.fullmatch(ref)
- if match:
- return match.group(1), match.group(2)
- match = _SLACK_TARGET_RE.fullmatch(ref)
- if match:
- return match.group(1), None
- match = _SLACK_USER_ID_RE.fullmatch(ref) or _SLACK_MENTION_RE.fullmatch(ref)
- if match:
- return f"user:{match.group(1)}", None
- match = _SLACK_USER_NAME_RE.fullmatch(ref)
- return (f"user_name:{match.group(1)}", None) if match else None
+ for regex, template, has_thread in _SLACK_FORMS:
+ match = regex.fullmatch(ref)
+ if match:
+ return template.format(match.group(1)), (match.group(2) if has_thread else None)
+ return None
def _parse_matrix(ref):
- # "<room>:$<event_id>" addresses a thread (rfind: room ids contain ':').
- # Bare "!room" / "@user" are explicit too, but via the generic rule so the
- # numeric check keeps precedence exactly as before.
+ # "<room>:$<event_id>" addresses a thread (rfind: room ids contain ':'). Bare "!room" /
+ # "@user" are explicit too, but via the generic rule so the numeric check keeps precedence.
trimmed = ref.strip()
split_idx = trimmed.rfind(":$")
if split_idx > 0:
@@ -110,16 +103,9 @@ def _parse_matrix(ref):
return None
-def _parse_regex_stripped(regex):
- """Explicit when ``regex`` fully matches; returns the stripped ref verbatim."""
- def parse(ref):
- return (ref.strip(), None) if regex.fullmatch(ref) else None
- return parse
-
-
def _parse_yuanbao(ref):
- # "group:<code>" / "direct:<id>"; a bare number is a group code, never a
- # generic numeric chat id (yuanbao never falls through to the generic rules).
+ # "group:<code>" / "direct:<id>"; a bare number is a group code, never a generic
+ # numeric chat id (yuanbao never falls through to the generic rules).
match = _YUANBAO_TARGET_RE.fullmatch(ref)
if match:
return match.group(1), None
@@ -129,8 +115,8 @@ def _parse_yuanbao(ref):
def _parse_nonempty(ref):
- # ntfy topics and WeCom ids (wr/wc groups, wo/bare users — the adapter
- # picks the send command) are explicit whenever non-empty.
+ # ntfy topics and WeCom ids (wr/wc groups, wo/bare users — the adapter picks the
+ # send command) are explicit whenever non-empty.
stripped = ref.strip()
return (stripped, None) if stripped else None
@@ -167,11 +153,9 @@ _PLATFORM_PARSERS = {
def _parse_target_ref(platform_name: str, target_ref: str):
"""Parse a tool target into (chat_id, thread_id, explicit).
- Platform parser first, then the shared rules in this order: E.164 phone
- numbers (keeping the '+' the signal/sms/whatsapp adapters expect), bare
- numeric ids, Matrix ``!room``/``@user``, XMPP JIDs (user@server or
- [email protected]). Anything else is not explicit and goes to
- channel-directory resolution.
+ Platform parser first, then the shared rules in this order: E.164 phone numbers
+ (keeping the '+' the signal/sms/whatsapp adapters expect), bare numeric ids, Matrix
+ ``!room``/``@user``, XMPP JIDs. Anything else goes to channel-directory resolution.
"""
parser = _PLATFORM_PARSERS.get(platform_name)
if parser is not None:
@@ -196,22 +180,18 @@ def resolve_send_target(
) -> tuple[str | None, str | None, str | None]:
"""Resolve one send target the same way for every caller (model tool, CLI, cron).
- Channel-directory IDs are trusted. Plugin platforms must explicitly parse
- native target syntax; for the model-facing send tool (the default), a
- target that can't be resolved is an error — the model can read the error
- and pick a listed target instead.
-
- ``pass_unresolved_references=True`` restores the old pass-through behavior for
- callers that have no model in the loop (cron delivering a stored job's
- output, react/unreact on platform-native message ids): if the target
- can't be resolved and the platform is built in, or is a plugin platform
- that declares no parser, the string is handed to the adapter exactly as
- written and the adapter decides whether it's valid. A plugin platform
- that DOES declare a parser stays strict for every caller — its parser is
- the authority on native syntax.
-
- The optional validator has the final say over parser-normalized,
- directory-resolved, and passed-through IDs alike.
+ Channel-directory IDs are trusted. Plugin platforms must explicitly parse native
+ target syntax; for the model-facing send tool (the default) an unresolvable target
+ is an error the model can read and pick a listed target instead.
+
+ ``pass_unresolved_references=True`` is for callers with no model in the loop (cron
+ delivering a stored job's output, react/unreact on platform-native message ids): an
+ unresolvable target on a built-in platform, or on a plugin platform that declares no
+ parser, is handed to the adapter exactly as written and the adapter decides. A plugin
+ platform that DOES declare a parser stays strict for every caller.
+
+ The optional validator has the final say over parser-normalized, directory-resolved,
+ and passed-through IDs alike.
"""
from gateway.config import Platform
from gateway.platform_registry import platform_registry
@@ -224,9 +204,7 @@ def resolve_send_target(
try:
verdict = entry.validate_target_ref_fn(candidate)
except Exception:
- logger.debug(
- "Plugin target validator failed for %s", platform_name, exc_info=True
- )
+ logger.debug("Plugin target validator failed for %s", platform_name, exc_info=True)
return f"Target validator failed for platform '{platform_name}'"
if verdict is True:
return None
@@ -234,13 +212,15 @@ def resolve_send_target(
return f"Invalid target '{target_ref}' on {platform_name}: {verdict}"
return f"Invalid target '{target_ref}' on {platform_name}"
+ def _validated(chat_id, thread_id):
+ error = _validate(chat_id)
+ return (None, None, error) if error else (chat_id, thread_id, None)
+
if entry is not None and entry.parse_target_ref_fn is not None:
try:
parsed = entry.parse_target_ref_fn(target_ref)
except Exception:
- logger.debug(
- "Plugin target parser failed for %s", platform_name, exc_info=True
- )
+ logger.debug("Plugin target parser failed for %s", platform_name, exc_info=True)
return None, None, f"Target parser failed for platform '{platform_name}'"
if parsed is not None:
if (
@@ -251,28 +231,14 @@ def resolve_send_target(
or (parsed[1] is not None and not isinstance(parsed[1], str))
):
return (
- None,
- None,
+ None, None,
f"Target parser for platform '{platform_name}' returned an invalid result",
)
- parsed_chat_id, parsed_thread_id = parsed
- error = _validate(parsed_chat_id)
- return (None, None, error) if error else (
- parsed_chat_id,
- parsed_thread_id,
- None,
- )
-
- parsed_chat_id, parsed_thread_id, explicit = _parse_target_ref(
- platform_name, target_ref
- )
+ return _validated(*parsed)
+
+ parsed_chat_id, parsed_thread_id, explicit = _parse_target_ref(platform_name, target_ref)
if explicit and parsed_chat_id is not None:
- error = _validate(parsed_chat_id)
- return (None, None, error) if error else (
- parsed_chat_id,
- parsed_thread_id,
- None,
- )
+ return _validated(parsed_chat_id, parsed_thread_id)
resolution_failed = False
try:
@@ -283,16 +249,8 @@ def resolve_send_target(
resolved = None
resolution_failed = True
if resolved:
- parsed_chat_id, parsed_thread_id, _ = _parse_target_ref(
- platform_name, resolved
- )
- chat_id = parsed_chat_id or resolved
- error = _validate(chat_id)
- return (None, None, error) if error else (
- chat_id,
- parsed_thread_id,
- None,
- )
+ parsed_chat_id, parsed_thread_id, _ = _parse_target_ref(platform_name, resolved)
+ return _validated(parsed_chat_id or resolved, parsed_thread_id)
is_builtin = platform_name in {member.value for member in Platform}
if entry is None and not is_builtin:
@@ -314,8 +272,7 @@ def resolve_send_target(
if pass_unresolved_references and entry.parse_target_ref_fn is None:
return _pass_through_unresolved()
return (
- None,
- None,
+ None, None,
f"Could not resolve '{target_ref}' on {platform_name}. "
"The plugin parser did not recognize it and no channel-directory entry matched.",
)
diff --git a/tools/send_message_tool.py b/tools/send_message_tool.py
index 2d08eca5214..988a449b452 100644
--- a/tools/send_message_tool.py
+++ b/tools/send_message_tool.py
@@ -1,7 +1,7 @@
"""Send Message Tool -- cross-channel messaging via platform APIs.
Sends a message to a user or channel on any connected messaging platform
-(Telegram, Discord, Slack). Supports listing available targets and resolving
+(Telegram, Discord, Slack, ...), lists available targets and resolves
human-friendly channel names to IDs. Works in both CLI and gateway contexts.
"""
@@ -14,6 +14,7 @@ from agent.secret_scope import get_secret
logger = logging.getLogger(__name__)
+# Re-exported: tests and sibling modules import these via tools.send_message_tool.
from tools.send_message_targets import ( # noqa: F401
_BUZZ_UUID_RE,
_E164_TARGET_RE,
@@ -66,6 +67,13 @@ from tools.send_message_senders import ( # noqa: F401
_send_yuanbao,
_telegram_retry_delay,
)
+from tools.registry import tool_error
+
+# NOTE: ``send_message`` is intentionally NOT registered as an agent-callable model
+# tool (the agent must not fire cross-platform messages on its own). The send engine
+# here is the shared transport for cron delivery, the ``hermes send`` CLI, the gateway
+# kanban notifier and the opt-in MCP server, which import the helpers directly.
+
def prepare_send_message_platforms() -> None:
"""Load enabled standalone plugins before tool schemas/cache keys are built."""
@@ -90,6 +98,18 @@ def _split_target(target: str):
return parts[0].strip().lower(), (parts[1].strip() if len(parts) > 1 else None)
+def _resolve_tool_target(target: str, *, pass_unresolved_references: bool = False):
+ """Split and resolve a tool ``target``; returns ``(platform_name, chat_id, thread_id, error)``.
+ ``chat_id`` is None when no ref was given (caller falls back to the home channel)."""
+ platform_name, target_ref = _split_target(target)
+ prepare_send_message_platforms()
+ if not target_ref:
+ return platform_name, None, None, None
+ chat_id, thread_id, resolution_error = resolve_send_target(
+ platform_name, target_ref, pass_unresolved_references=pass_unresolved_references)
+ return platform_name, chat_id, thread_id, resolution_error
+
+
def _live_adapter(platform):
"""Return the running gateway's adapter for ``platform``, or None."""
runner = _live_runner()
@@ -106,11 +126,10 @@ def _handle_list():
def _handle_react(args, remove=False):
- """Attach (or with ``remove=True`` retract) an emoji reaction via a live
- gateway adapter exposing ``add_reaction(chat_id, emoji, message_id)`` /
- ``remove_reaction(chat_id, message_id)`` (e.g. photon/iMessage tapbacks).
- No standalone fallback: reacting needs the adapter's live message-id state.
- """
+ """Attach (or with ``remove=True`` retract) an emoji reaction via a live gateway
+ adapter exposing ``add_reaction(chat_id, emoji, message_id)`` /
+ ``remove_reaction(chat_id, message_id)`` (e.g. photon/iMessage tapbacks). No
+ standalone fallback: reacting needs the adapter's live message-id state."""
target = args.get("target", "")
emoji = (args.get("emoji") or "").strip()
message_id = (args.get("message_id") or "").strip() or None
@@ -121,17 +140,12 @@ def _handle_react(args, remove=False):
else "'target' is required when action='unreact'"
)
- platform_name, target_ref = _split_target(target)
- chat_id = None
- prepare_send_message_platforms()
- if target_ref:
- # Platform-native ids (e.g. photon GUIDs) match no parser/directory
- # entry; hand them to the adapter unchanged and let it validate.
- chat_id, _thread_id, resolution_error = resolve_send_target(
- platform_name, target_ref, pass_unresolved_references=True
- )
- if resolution_error:
- return tool_error(resolution_error)
+ # Platform-native ids (e.g. photon GUIDs) match no parser/directory entry; hand
+ # them to the adapter unchanged and let it validate.
+ platform_name, chat_id, _thread_id, resolution_error = _resolve_tool_target(
+ target, pass_unresolved_references=True)
+ if resolution_error:
+ return tool_error(resolution_error)
try:
from gateway.config import Platform, load_gateway_config
@@ -159,9 +173,7 @@ def _handle_react(args, remove=False):
)
react_fn = getattr(adapter, "remove_reaction" if remove else "add_reaction", None)
if not callable(react_fn):
- return tool_error(
- f"Platform '{platform_name}' does not support message reactions."
- )
+ return tool_error(f"Platform '{platform_name}' does not support message reactions.")
kwargs = {"chat_id": chat_id, "message_id": message_id}
if not remove:
@@ -183,17 +195,9 @@ def _handle_send(args):
if not target or not message:
return tool_error("Both 'target' and 'message' are required when action='send'")
- platform_name, target_ref = _split_target(target)
- chat_id = None
- thread_id = None
-
- prepare_send_message_platforms()
- if target_ref:
- chat_id, thread_id, resolution_error = resolve_send_target(
- platform_name, target_ref
- )
- if resolution_error:
- return tool_error(resolution_error)
+ platform_name, chat_id, thread_id, resolution_error = _resolve_tool_target(target)
+ if resolution_error:
+ return tool_error(resolution_error)
from tools.interrupt import is_interrupted
if is_interrupted():
@@ -211,9 +215,9 @@ def _handle_send(args):
from gateway.platforms.base import BasePlatformAdapter
- # Capture [[as_document]] before extract_media strips it: image files then
- # go through send_document so the original bytes survive (Telegram's
- # sendPhoto recompresses to 1280px).
+ # Capture [[as_document]] before extract_media strips it: image files then go
+ # through send_document so the original bytes survive (Telegram's sendPhoto
+ # recompresses to 1280px).
force_document_attachments = "[[as_document]]" in message
media_files, cleaned_message = BasePlatformAdapter.extract_media(message)
@@ -247,8 +251,7 @@ def _handle_send(args):
if entry is not None and entry.send_message_handler is not None:
send_kwargs["args"] = args
result = _run_async(
- _send_to_platform(platform, pconfig, chat_id, cleaned_message, **send_kwargs)
- )
+ _send_to_platform(platform, pconfig, chat_id, cleaned_message, **send_kwargs))
if isinstance(result, dict) and result.get("success"):
if used_home_channel:
result["note"] = f"Sent to {platform_name} home channel (chat_id: {chat_id})"
@@ -265,9 +268,9 @@ def _handle_send(args):
def _resolve_platform_config(platform_name, config):
"""Return ``(platform, pconfig, registry_entry, error)`` for a send.
- Plugin platforms must be registered; disabled/missing platforms error,
- except Weixin, which may be configured purely via .env (synthesized
- pconfig so send_message and cron delivery work without a gateway.yaml entry).
+ Plugin platforms must be registered; disabled/missing platforms error, except
+ Weixin, which may be configured purely via .env (synthesized pconfig so
+ send_message and cron delivery work without a gateway.yaml entry).
"""
from gateway.config import Platform
from gateway.platform_registry import platform_registry
@@ -303,9 +306,7 @@ def _home_chat_id(config, platform, platform_name):
home = HomeChannel(platform=platform, chat_id=wx_home, name="Weixin Home")
if home:
return home.chat_id, None
- home_env = _HOME_CHANNEL_ENV_OVERRIDES.get(
- platform_name, f"{platform_name.upper()}_HOME_CHANNEL"
- )
+ home_env = _HOME_CHANNEL_ENV_OVERRIDES.get(platform_name, f"{platform_name.upper()}_HOME_CHANNEL")
return None, (
f"No home channel set for {platform_name} to determine where to send the message. "
f"Either specify a channel directly with '{platform_name}:CHANNEL_NAME', "
@@ -314,10 +315,9 @@ def _home_chat_id(config, platform, platform_name):
def _slack_dm_chat_id(pconfig, chat_id):
- """Open Slack user targets as DM conversations: ``user:U...`` /
- ``user_name:@handle`` from the parser, or a bare U... id from session
- metadata / home-channel config. chat.postMessage needs a conversation ID.
- Returns ``(chat_id, None)`` or ``(None, error_dict)``."""
+ """Open Slack user targets as DM conversations: ``user:U...`` / ``user_name:@handle``
+ from the parser, or a bare U... id from session metadata / home-channel config.
+ chat.postMessage needs a conversation ID. Returns ``(chat_id, None)`` or ``(None, error_dict)``."""
dm_target = chat_id
if dm_target.startswith("U") and _SLACK_USER_ID_RE.fullmatch(dm_target):
dm_target = f"user:{dm_target}"
@@ -333,9 +333,7 @@ def _mirror_sent_message(platform_name, chat_id, mirror_text, thread_id):
from gateway.mirror import mirror_to_session
from gateway.session_context import get_session_env
return bool(mirror_to_session(
- platform_name,
- chat_id,
- mirror_text,
+ platform_name, chat_id, mirror_text,
source_label=get_session_env("HERMES_SESSION_PLATFORM", "cli"),
thread_id=thread_id,
user_id=get_session_env("HERMES_SESSION_USER_ID", "") or None,
@@ -378,25 +376,15 @@ def _describe_media_for_mirror(media_files):
return "[Sent document attachment]"
-def _get_cron_auto_delivery_target():
- """Return the cron scheduler's auto-delivery target for the current run, if any."""
- from gateway.session_context import get_session_env
- platform = get_session_env("HERMES_CRON_AUTO_DELIVER_PLATFORM", "").strip().lower()
- chat_id = get_session_env("HERMES_CRON_AUTO_DELIVER_CHAT_ID", "").strip()
- if not platform or not chat_id:
- return None
- thread_id = get_session_env("HERMES_CRON_AUTO_DELIVER_THREAD_ID", "").strip() or None
- return {"platform": platform, "chat_id": chat_id, "thread_id": thread_id}
-
-
def _maybe_skip_cron_duplicate_send(platform_name: str, chat_id: str, thread_id: str | None):
"""Skip redundant cron send_message calls when the scheduler will auto-deliver there."""
- auto_target = _get_cron_auto_delivery_target()
- if not auto_target or not (
- auto_target["platform"] == platform_name
- and str(auto_target["chat_id"]) == str(chat_id)
- and auto_target.get("thread_id") == thread_id
- ):
+ from gateway.session_context import get_session_env
+ auto_platform = get_session_env("HERMES_CRON_AUTO_DELIVER_PLATFORM", "").strip().lower()
+ auto_chat_id = get_session_env("HERMES_CRON_AUTO_DELIVER_CHAT_ID", "").strip()
+ if not auto_platform or not auto_chat_id:
+ return None
+ auto_thread_id = get_session_env("HERMES_CRON_AUTO_DELIVER_THREAD_ID", "").strip() or None
+ if not (auto_platform == platform_name and auto_chat_id == str(chat_id) and auto_thread_id == thread_id):
return None
target_label = f"{platform_name}:{chat_id}"
@@ -424,111 +412,82 @@ def _bounded_send_error(detail, max_chars=900):
return f"{text[: max_chars - 3]}..."
+def _live_media_method(ext, is_voice, force_document):
+ """Adapter media method name and kind for one live-adapter media file."""
+ if force_document:
+ return "send_document", "document"
+ if ext in _IMAGE_EXTS:
+ return "send_image_file", "image"
+ if ext in _VIDEO_EXTS:
+ return "send_video", "video"
+ if is_voice or ext in _AUDIO_EXTS:
+ return "send_voice", "audio"
+ return "send_document", "document"
+
+
async def _send_live_adapter_media(
- adapter,
- chat_id,
- message,
- media_files,
- *,
- thread_id=None,
- metadata=None,
- force_document=False,
+ adapter, chat_id, message, media_files, *, thread_id=None, metadata=None, force_document=False
):
"""Deliver text and every media descriptor through adapter media APIs.
Adapters that only inherit the BasePlatformAdapter stub for a media kind
are treated as unsupported rather than silently no-op'd."""
caption, separate_text = _media_caption_split(
- message, media_files, max_caption_len=_DEFAULT_CAPTION_LIMIT
- )
+ message, media_files, max_caption_len=_DEFAULT_CAPTION_LIMIT)
last_result = None
if separate_text and separate_text.strip():
- last_result = await adapter.send(
- chat_id=chat_id, content=separate_text, metadata=metadata
- )
+ last_result = await adapter.send(chat_id=chat_id, content=separate_text, metadata=metadata)
if not last_result.success:
return {"error": f"Adapter send failed: {_bounded_send_error(last_result.error)}"}
+ from gateway.platforms.base import BasePlatformAdapter
+
total = len(media_files)
for index, descriptor in enumerate(media_files):
+ invalid = {"error": f"Adapter media send failed: invalid media descriptor {index + 1}/{total}"}
if not isinstance(descriptor, (list, tuple)) or not descriptor:
- return {"error": f"Adapter media send failed: invalid media descriptor {index + 1}/{total}"}
+ return invalid
media_path = descriptor[0]
is_voice = bool(descriptor[1]) if len(descriptor) > 1 else False
if not isinstance(media_path, str) or not media_path:
- return {"error": f"Adapter media send failed: invalid media descriptor {index + 1}/{total}"}
+ return invalid
if not os.path.exists(media_path):
return {"error": f"Adapter media send failed: media file {index + 1}/{total} was not found"}
ext = os.path.splitext(media_path)[1].lower()
- kwargs = {
- "caption": caption if index == 0 else None,
- "reply_to": thread_id,
- "metadata": metadata,
- }
- if force_document:
- method_name, media_kind = "send_document", "document"
- elif ext in _IMAGE_EXTS:
- method_name, media_kind = "send_image_file", "image"
- elif ext in _VIDEO_EXTS:
- method_name, media_kind = "send_video", "video"
- elif is_voice or ext in _AUDIO_EXTS:
- method_name, media_kind = "send_voice", "audio"
- else:
- method_name, media_kind = "send_document", "document"
-
- from gateway.platforms.base import BasePlatformAdapter
-
+ method_name, media_kind = _live_media_method(ext, is_voice, force_document)
adapter_method = getattr(type(adapter), method_name, None)
- base_fallback = getattr(BasePlatformAdapter, method_name)
- if adapter_method is None or adapter_method is base_fallback:
+ if adapter_method is None or adapter_method is getattr(BasePlatformAdapter, method_name):
return {
"error": (
f"Live adapter does not implement native {media_kind} delivery; "
f"media file {index + 1}/{total} was not sent"
)
}
+ kwargs = {"caption": caption if index == 0 else None, "reply_to": thread_id, "metadata": metadata}
try:
last_result = await getattr(adapter, method_name)(chat_id, media_path, **kwargs)
except asyncio.CancelledError:
raise
except Exception as exc:
- return {
- "error": (
- f"Adapter media send failed after {index}/{total} files: "
- f"{_bounded_send_error(exc)}"
- )
- }
- if not last_result.success:
+ detail = _bounded_send_error(exc)
+ else:
+ if last_result.success:
+ continue
detail = _bounded_send_error(last_result.error or "media send failed")
- return {
- "error": f"Adapter media send failed after {index}/{total} files: {detail}"
- }
+ return {"error": f"Adapter media send failed after {index}/{total} files: {detail}"}
if last_result is None:
return {"error": "No deliverable text or media remained after processing MEDIA tags"}
- return {
- "success": True,
- "message_id": last_result.message_id,
- "media_delivered": True,
- }
+ return {"success": True, "message_id": last_result.message_id, "media_delivered": True}
async def _send_via_adapter(
- platform,
- pconfig,
- chat_id,
- chunk,
- *,
- thread_id=None,
- media_files=None,
- force_document=False,
+ platform, pconfig, chat_id, chunk, *, thread_id=None, media_files=None, force_document=False
):
- """Send via the live in-process gateway adapter (``_gateway_runner_ref``),
- else the plugin's ``standalone_sender_fn`` (gateway not in this process,
- e.g. cron — the runner weakref is None), else a descriptive error naming
- both options. Media descriptors go through the adapter's native media
- APIs under the same cross-loop rules as text.
- """
+ """Send via the live in-process gateway adapter (``_gateway_runner_ref``), else the
+ plugin's ``standalone_sender_fn`` (gateway not in this process, e.g. cron — the runner
+ weakref is None), else a descriptive error naming both options. Media descriptors go
+ through the adapter's native media APIs under the same cross-loop rules as text."""
platform_name = platform.value if hasattr(platform, "value") else str(platform)
runner = _live_runner()
adapter = None
@@ -545,9 +504,9 @@ async def _send_via_adapter(
if platform_name == "ntfy" and chat_id:
metadata["publish_topic"] = chat_id
metadata = metadata or None
- # adapter.send() uses queues/tasks bound to the gateway's loop.
- # Awaiting it from another loop (the tool worker thread) deadlocks
- # on a cross-loop Future, so dispatch onto the gateway loop instead.
+ # adapter.send() uses queues/tasks bound to the gateway's loop. Awaiting it
+ # from another loop (the tool worker thread) deadlocks on a cross-loop
+ # Future, so dispatch onto the gateway loop instead.
gateway_loop = getattr(runner, "_gateway_loop", None)
try:
current_loop = asyncio.get_running_loop()
@@ -562,14 +521,12 @@ async def _send_via_adapter(
return {"error": "Gateway loop is not running; cannot dispatch adapter send"}
from agent.async_utils import safe_schedule_threadsafe
fut = safe_schedule_threadsafe(
- make_coro(), gateway_loop, logger=logger, log_message=log_message
- )
+ make_coro(), gateway_loop, logger=logger, log_message=log_message)
if fut is None:
return {"error": "Gateway loop unavailable for send dispatch"}
# shield: a cancelled caller (agent interrupt) must not cancel the
- # already-enqueued gateway send, or a retry would duplicate it.
- # No timeout here — the adapter's request timeout and the outer
- # _run_async timeout bound the wait.
+ # already-enqueued gateway send, or a retry would duplicate it. No timeout
+ # here — the adapter's request timeout and the outer _run_async bound the wait.
return await asyncio.shield(asyncio.wrap_future(fut))
if media_files:
@@ -603,12 +560,8 @@ async def _send_via_adapter(
if entry is not None and entry.standalone_sender_fn is not None:
try:
result = await entry.standalone_sender_fn(
- pconfig,
- chat_id,
- chunk,
- thread_id=thread_id,
- media_files=media_files,
- force_document=force_document,
+ pconfig, chat_id, chunk,
+ thread_id=thread_id, media_files=media_files, force_document=force_document,
)
except asyncio.CancelledError:
raise
@@ -651,10 +604,10 @@ async def _send_chunks(chunks, send_one):
def _platform_max_length(platform):
- """Max message length for chunking: the adapter constant for Signal (its
- raw JSON-RPC path never sees SignalAdapter's own chunking, so signal-cli
- would reject long sends whole), the registry's ``max_message_length`` for
- plugins (Slack, Feishu, ...), else None (no chunking)."""
+ """Max message length for chunking: the adapter constant for Signal (its raw
+ JSON-RPC path never sees SignalAdapter's own chunking, so signal-cli would reject
+ long sends whole), the registry's ``max_message_length`` for plugins (Slack,
+ Feishu, ...), else None (no chunking)."""
from gateway.config import Platform
if platform == Platform.SIGNAL:
@@ -673,12 +626,11 @@ def _platform_max_length(platform):
return None
-# Plugin platforms whose media (and, for Discord, all) sends go straight to the
-# registry ``standalone_sender_fn`` — bypassing the live adapter on purpose:
-# Discord's plugin ``_standalone_send`` handles forum channels, threads and
-# multipart uploads (historically the only Discord path); Slack uploads via
-# files_upload_v2; WhatsApp posts each file to the Baileys bridge /send-media so
-# images/videos/audio arrive as native bubbles, not documents.
+# Plugin platforms whose media (and, for Discord, all) sends go straight to the registry
+# ``standalone_sender_fn`` — bypassing the live adapter on purpose: Discord's plugin
+# ``_standalone_send`` handles forum channels, threads and multipart uploads
+# (historically the only Discord path); Slack uploads via files_upload_v2; WhatsApp
+# posts each file to the Baileys bridge /send-media so media arrive as native bubbles.
# platform -> (error label, run discover_plugins first, caption-capable,
# media_files sentinel for non-final chunks, forward force_document)
_PLUGIN_STANDALONE_MEDIA = {
@@ -702,8 +654,7 @@ async def _send_plugin_standalone(
if captionable:
# Cap on the platform's own message limit so the caption is deliverable.
caption, _ = _media_caption_split(
- message, media_files, max_caption_len=(max_len or _DEFAULT_CAPTION_LIMIT)
- )
+ message, media_files, max_caption_len=(max_len or _DEFAULT_CAPTION_LIMIT))
if caption is not None:
return await sender(
pconfig, chat_id, "", thread_id=thread_id, media_files=media_files,
@@ -718,18 +669,17 @@ async def _send_plugin_standalone(
)
-# Native-media chunked routes for built-in platforms; media rides on the final
-# chunk, non-final chunks get the platform's empty-media sentinel.
+# Native-media chunked routes for built-in platforms; media rides on the final chunk,
+# non-final chunks get the platform's empty-media sentinel.
# platform -> (media required, empty-media sentinel,
# sender(platform, pconfig, chat_id, chunk, media, thread_id, force_document))
-# Matrix: ALL sends go through the native adapter so text is encrypted in E2EE
-# rooms too (the raw-HTTP standalone path is not encryption-aware).
+# Matrix: ALL sends go through the native adapter so text is encrypted in E2EE rooms
+# too (the raw-HTTP standalone path is not encryption-aware).
# Signal: attachments ride the JSON-RPC ``attachments`` param.
# Yuanbao: media needs the running gateway adapter's WebSocket.
-# Slack (text; media was intercepted above): prefer the live adapter — it is
-# multi-workspace aware and honors adapter gates like ignored_channels, while
-# the standalone Web-API path may only have a token list — else the plugin's
-# standalone sender, keeping MEDIA delivery on the cron fallback.
+# Slack (text; media was intercepted above): prefer the live adapter — multi-workspace
+# aware and honors adapter gates like ignored_channels, while the standalone Web-API
+# path may only have a token list — else the plugin's standalone sender.
# WeCom: native media only through the live gateway adapter.
# Names resolve at call time so tests can monkeypatch e.g. ``_send_signal``.
_CHUNKED_ROUTES = {
@@ -745,16 +695,16 @@ _CHUNKED_ROUTES = {
p, pc, cid, chunk, thread_id=tid, media_files=media, force_document=fd)),
}
-# Text-only senders for built-in platforms (generic path; media is dropped
-# with a warning). Signature: (pconfig, chat_id, chunk, thread_id) -> result.
+
+def _registry_text_sender(name):
+ return lambda pc, cid, chunk, tid: _registry_standalone_send(name, pc, cid, chunk, tid)
+
+
+# Text-only senders for built-in platforms (generic path; media is dropped with a
+# warning). Signature: (pconfig, chat_id, chunk, thread_id) -> result.
_TEXT_SENDERS = {
- "whatsapp": lambda pc, cid, chunk, tid: _registry_standalone_send("whatsapp", pc, cid, chunk, tid),
+ **{name: _registry_text_sender(name) for name in ("whatsapp", "email", "sms", "dingtalk", "feishu", "wecom")},
"signal": lambda pc, cid, chunk, tid: _send_signal(pc.extra, cid, chunk),
- "email": lambda pc, cid, chunk, tid: _registry_standalone_send("email", pc, cid, chunk, tid),
- "sms": lambda pc, cid, chunk, tid: _registry_standalone_send("sms", pc, cid, chunk, tid),
- "dingtalk": lambda pc, cid, chunk, tid: _registry_standalone_send("dingtalk", pc, cid, chunk, tid),
- "feishu": lambda pc, cid, chunk, tid: _registry_standalone_send("feishu", pc, cid, chunk, tid),
- "wecom": lambda pc, cid, chunk, tid: _registry_standalone_send("wecom", pc, cid, chunk, tid),
"bluebubbles": lambda pc, cid, chunk, tid: _send_bluebubbles(pc.extra, cid, chunk),
"qqbot": lambda pc, cid, chunk, tid: _send_qqbot(pc, cid, chunk),
"yuanbao": lambda pc, cid, chunk, tid: _send_yuanbao(cid, chunk),
@@ -766,12 +716,11 @@ _MEDIA_PLATFORMS_NOTE = "telegram, discord, matrix, weixin, signal, yuanbao, fei
async def _send_to_platform(platform, pconfig, chat_id, message, thread_id=None, media_files=None, force_document=False, args=None):
"""Route a message to the appropriate platform sender.
- Long messages are chunked with the adapters' smart splitter (code-block
- aware, part indicators). Branch order matters: Weixin first (its native
- helper must not be blocked by unrelated optional imports such as
- lark-oapi's heavy Feishu path), Telegram (chunks itself), plugin
- standalone media routes, native chunked routes, then the generic text
- path that drops media with a warning.
+ Long messages are chunked with the adapters' smart splitter (code-block aware,
+ part indicators). Branch order matters: Weixin first (its native helper must not
+ be blocked by unrelated optional imports such as lark-oapi's heavy Feishu path),
+ Telegram (chunks itself), plugin standalone media routes, native chunked routes,
+ then the generic text path that drops media with a warning.
"""
from gateway.config import Platform
@@ -781,19 +730,14 @@ async def _send_to_platform(platform, pconfig, chat_id, message, thread_id=None,
if platform == Platform.WEIXIN:
return await _send_weixin(pconfig, chat_id, message, media_files=media_files)
- # Telegram chunks internally on the *formatted* text in UTF-16 units
- # (MarkdownV2/HTML escaping inflates length), so it gets the whole
- # message; media attaches after all text chunks.
+ # Telegram chunks internally on the *formatted* text in UTF-16 units (MarkdownV2/HTML
+ # escaping inflates length), so it gets the whole message; media attaches after all text.
if platform == Platform.TELEGRAM:
disable_link_previews = bool(getattr(pconfig, "extra", {}) and pconfig.extra.get("disable_link_previews"))
return await _send_telegram(
- pconfig.token,
- chat_id,
- message,
- media_files=media_files,
- thread_id=thread_id,
- disable_link_previews=disable_link_previews,
- force_document=force_document,
+ pconfig.token, chat_id, message,
+ media_files=media_files, thread_id=thread_id,
+ disable_link_previews=disable_link_previews, force_document=force_document,
)
from gateway.platforms.base import BasePlatformAdapter
@@ -815,18 +759,18 @@ async def _send_to_platform(platform, pconfig, chat_id, message, thread_id=None,
media_files if is_last else empty_media, thread_id, force_document,
))
- # --- Generic path: text only. Buzz is a plugin platform with verified
- # native media delivery through _send_via_adapter (media-only sends
- # included), so it is exempt from the media error/warning.
- if media_files and platform_name != "buzz" and not message.strip():
- return {
- "error": (
- f"send_message MEDIA delivery is currently only supported for {_MEDIA_PLATFORMS_NOTE}; "
- f"target {platform_name} had only media attachments"
- )
- }
+ # --- Generic path: text only. Buzz is a plugin platform with verified native media
+ # delivery through _send_via_adapter (media-only sends included), so it is exempt
+ # from the media error/warning.
warning = None
if media_files and platform_name != "buzz":
+ if not message.strip():
+ return {
+ "error": (
+ f"send_message MEDIA delivery is currently only supported for {_MEDIA_PLATFORMS_NOTE}; "
+ f"target {platform_name} had only media attachments"
+ )
+ }
warning = (
f"MEDIA attachments were omitted for {platform_name}; "
f"native send_message media delivery is currently only supported for {_MEDIA_PLATFORMS_NOTE}"
@@ -866,13 +810,3 @@ async def _send_to_platform(platform, pconfig, chat_id, message, thread_id=None,
):
last_result["warnings"] = [*last_result.get("warnings", []), warning]
return last_result
-
-
-# --- Registry ---
-from tools.registry import tool_error
-
-# NOTE: ``send_message`` is intentionally NOT registered as an agent-callable
-# model tool (the agent must not fire cross-platform messages on its own).
-# The send engine here is the shared transport for cron delivery, the
-# ``hermes send`` CLI, the gateway kanban notifier and the opt-in MCP server,
-# which import the helpers directly.