Files
hermes-agent/gateway/rich_sent_store.py
T
Teknium c8b56d43da fix(whatsapp): quoting the bot's own image/voice/document attaches the file
Salvage of #77660 covers quotes of INBOUND media via the bridge's download
cache. The reported case is the other half: a user quotes an image the bot
sent (a cron-delivered chart) and asks "what is this?" — the bridge cache
knows only inbound messages, and Meta's Cloud webhook ``context`` carries
only the quoted wamid, so the agent received a text-only turn and could not
see the image it had itself delivered.

Widen ``gateway/rich_sent_store`` (already the (chat_id, message_id) → text
index Telegram and the Cloud adapter use for quoted text) with
``record_media`` / ``lookup_media``: both WhatsApp adapters index the local
path + MIME of every media send at send time and every inbound media
receive, and on a quoted reply fold the resolved ``(path, mime)`` into the
event's own ``media_urls``/``media_types`` so the existing vision/audio
pipeline handles it like a direct attachment. ``lookup_media`` drops entries
whose file no longer exists. Baileys: the outbound index is the fallback
when the bridge cache misses; the cache-dir guard stays on bridge-supplied
paths only (our own sends are paths we chose). Cloud: link sends (public
URL, no local bytes) are not indexed.

One invariant test per adapter, red on origin/main.
2026-09-11 19:04:38 -07:00

88 lines
3.5 KiB
Python

"""Local index of what we've sent (and, for WhatsApp, received) keyed by ``(chat_id, message_id)``.
Telegram does NOT echo a rich message's content back in ``reply_to_message`` (``.text``/``.caption``
empty, ``.api_kwargs`` None), and WhatsApp quotes carry only the quoted message's id (Cloud API) or a
thumbnail stub (Baileys) — never the original bytes. So a reply to something we sent arrives with no
quotable text and no way to re-fetch a quoted attachment. We remember ``message_id -> text`` and
``message_id -> [(local_path, mime)]`` at send/receive time and look them up by ``reply_to_id`` on
inbound. Best-effort and dependency-free: every operation swallows errors and degrades to a no-op /
``None`` / ``[]`` so it can never break a send or an inbound message.
"""
from __future__ import annotations
import json
import os
import time
from typing import Optional
_MAX_ENTRIES = 1000
_MAX_TEXT_CHARS = 2000
def _store_path() -> str:
from hermes_constants import get_hermes_home # honors the active profile override
return os.path.join(str(get_hermes_home()), "state", "rich_sent_index.json")
def _load(path: str) -> dict:
try:
with open(path, "r", encoding="utf-8") as fh:
data = json.load(fh)
except (FileNotFoundError, ValueError):
return {}
return data if isinstance(data, dict) else {}
def _update(chat_id, message_id, fields: dict) -> None:
"""Merge ``fields`` into the ``(chat_id, message_id)`` entry. No-op on any failure."""
path = _store_path()
try:
os.makedirs(os.path.dirname(path), exist_ok=True)
data = _load(path)
key = f"{chat_id}:{message_id}"
entry = data.get(key)
entry = entry if isinstance(entry, dict) else {}
data[key] = {**entry, **fields, "ts": int(time.time())}
if len(data) > _MAX_ENTRIES: # trim oldest by timestamp
for k, _ in sorted(data.items(), key=lambda kv: kv[1].get("ts", 0))[: len(data) - _MAX_ENTRIES]:
data.pop(k, None)
tmp = f"{path}.tmp.{os.getpid()}"
with open(tmp, "w", encoding="utf-8") as fh:
json.dump(data, fh, ensure_ascii=False)
os.replace(tmp, path) # atomic; tolerates concurrent writers racing
except Exception:
return
def record(chat_id, message_id, text: Optional[str]) -> None:
"""Persist ``text`` for ``(chat_id, message_id)``. No-op on any failure."""
if not text or message_id is None or chat_id is None:
return
_update(chat_id, message_id, {"t": text[:_MAX_TEXT_CHARS]})
def record_media(chat_id, message_id, media: list[tuple[str, str]]) -> None:
"""Persist local attachment ``(path, mime)`` pairs for ``(chat_id, message_id)``."""
if not media or message_id is None or chat_id is None:
return
_update(chat_id, message_id, {"m": [[str(p), str(mt or "")] for p, mt in media if p]})
def _entry(chat_id, message_id) -> dict:
if message_id is None or chat_id is None:
return {}
entry = _load(_store_path()).get(f"{chat_id}:{message_id}")
return entry if isinstance(entry, dict) else {}
def lookup(chat_id, message_id) -> Optional[str]:
"""Return stored text for ``(chat_id, message_id)`` or ``None``."""
return _entry(chat_id, message_id).get("t") or None
def lookup_media(chat_id, message_id) -> list[tuple[str, str]]:
"""Return stored ``(path, mime)`` pairs whose file still exists (attachments may be temp files)."""
pairs = _entry(chat_id, message_id).get("m") or []
return [(p, mt) for p, mt in pairs if isinstance(p, str) and os.path.isfile(p)]