Files
hermes-agent/gateway/whatsapp_identity.py
kshitijk4poor 8503ee4459 refactor(send): route WhatsApp mentions through the existing standalone chunker
Follow-up to the salvaged #92440 commit, shape-gate cleanup only; behaviour is unchanged
(one mention-bearing payload per logical send, captioned media keeps its caption).

- Fold `_send_whatsapp_with_mentions` into `_send_plugin_standalone` (it was a line-for-line
  copy of the caption split + `_send_chunks` loop); `mentions` is attached to the first
  payload only via a one-shot kwarg dict.
- Drop the `inspect.signature(sender)` probe: the only registered WhatsApp standalone sender
  is the in-tree `_standalone_send`, which gains `mentions` in the same change; a foreign
  sender already surfaces as a TypeError through `_handle_send`'s error path.
- Collapse `_normalize_outbound_mentions` to dedupe-only; its input is argparse `list[str]`
  already validated by the CLI.
- Revert the `\d` -> `[0-9]` edit to `_BARE_PHONE_RE` / `to_whatsapp_jid`: unrelated to the
  feature (`normalize_whatsapp_mention_jid` already rejects non-ASCII via `isascii()`) and it
  changed output for nine other `to_whatsapp_jid` callers.
- Split the single 137-line test into a `whatsapp_bridge` fixture + two invariants
  (rejections never reach the bridge; mentions ride the first payload only, stale bridge
  fails closed); drop the fabricated legacy-sender branch that only existed to cover the
  deleted probe. Mutation check: removing the first-payload gate turns the new test red.
2026-09-19 00:24:50 +05:30

111 lines
4.6 KiB
Python

"""Shared helpers for canonicalising WhatsApp sender identity.
The bridge can surface one human as a LID (``999...@lid``) or a phone JID
(``1555...@s.whatsapp.net``) within one conversation. Authorisation (:mod:`gateway.run`) and
session keys (:mod:`gateway.session`) both resolve aliases here so they never drift apart;
plugins should use :func:`canonical_whatsapp_identifier` to line up with Hermes' session keys.
"""
from __future__ import annotations
import json
import logging
import re
from typing import Set
from hermes_constants import get_hermes_dir
logger = logging.getLogger(__name__)
# WhatsApp JIDs are numeric (or plus-prefixed) with ``@``/``.``/``:`` separators.
# Explicit ASCII class so full-width digits / Unicode word chars can't sneak through.
_SAFE_IDENTIFIER_RE = re.compile(r"^[A-Za-z0-9@.+\-]+$")
# "Just a phone number": optional ``+`` then digits and human separators.
# Anything carrying ``@`` is already a JID (``@g.us``, ``@lid``, ``status@broadcast``).
_BARE_PHONE_RE = re.compile(r"^\+?[\d\s().\-]+$")
def normalize_whatsapp_identifier(value: str) -> str:
"""Strip JID/LID/device/plus syntax down to the bare numeric identifier:
``"6012:47@s.whatsapp.net"``, ``"6012@lid"`` and ``"+6012"`` all become ``"6012"``."""
return str(value or "").strip().replace("+", "", 1).split(":", 1)[0].split("@", 1)[0]
def to_whatsapp_jid(value: str) -> str:
"""Normalize an *outbound* target to a bridge-safe JID (inverse of normalize). Baileys'
``jidDecode`` crashes on a bare phone, so bare phones become ``<digits>@s.whatsapp.net``;
``user:device@domain`` collapses to ``user@domain``; anything else is returned unchanged
so the bridge can surface a real error. ``""`` for empty input."""
if not value:
return ""
normalized = str(value).strip()
if ":" in normalized and "@" in normalized:
prefix, _, domain = normalized.partition("@")
normalized = f"{prefix.split(':', 1)[0]}@{domain}"
if "@" in normalized:
return normalized
if _BARE_PHONE_RE.fullmatch(normalized):
digits = re.sub(r"\D+", "", normalized)
if digits:
return f"{digits}@s.whatsapp.net"
return normalized
def normalize_whatsapp_mention_jid(value: str) -> str:
"""Return a valid participant JID for an outbound mention, or ``""``."""
jid = to_whatsapp_jid(value)
user, separator, domain = jid.partition("@")
return (
jid
if separator
and user.isascii()
and user.isdigit()
and domain in {"s.whatsapp.net", "lid"}
else ""
)
def expand_whatsapp_aliases(identifier: str) -> Set[str]:
"""All identifiers transitively reachable via the bridge's ``lid-mapping-*.json`` files;
always includes the normalized input itself (empty set if it normalizes to empty)."""
normalized = normalize_whatsapp_identifier(identifier)
if not normalized:
return set()
session_dir = get_hermes_dir("platforms/whatsapp/session", "whatsapp/session")
resolved: Set[str] = set()
queue = [normalized]
while queue:
current = queue.pop(0)
# _SAFE_IDENTIFIER_RE: defense-in-depth against path separators / traversal in the
# ``lid-mapping-{current}`` filename (the fixed prefix already prevents escape).
if not current or current in resolved or not _SAFE_IDENTIFIER_RE.match(current):
continue
resolved.add(current)
for suffix in ("", "_reverse"):
mapping_path = session_dir / f"lid-mapping-{current}{suffix}.json"
if not mapping_path.exists():
continue
try:
raw = json.loads(mapping_path.read_text(encoding="utf-8"))
mapped = normalize_whatsapp_identifier(raw)
except (OSError, json.JSONDecodeError) as exc:
logger.debug("whatsapp_identity: failed to read %s: %s", mapping_path, exc)
continue
if mapped and mapped not in resolved:
queue.append(mapped)
return resolved
def canonical_whatsapp_identifier(identifier: str) -> str:
"""Stable sender identity across phone-JID/LID variants (DM ``chat_id`` and group
``participant_id`` alike): the shortest alias from :func:`expand_whatsapp_aliases`, which
degrades to the normalized input when no mapping files exist. ``""`` for empty input."""
normalized = normalize_whatsapp_identifier(identifier)
if not normalized:
return ""
# expand_whatsapp_aliases includes ``normalized`` itself, so min() degrades to it
# when no lid-mapping files are present.
aliases = expand_whatsapp_aliases(normalized)
return min(aliases, key=lambda c: (len(c), c))