mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-06-12 08:51:53 +00:00
feat(photon): add agent-facing emoji reaction support
Add `action='react'` to `send_message` tool and expose `add_reaction`/ `remove_reaction` on the Photon adapter. - Track latest inbound message id per chat (`_last_inbound_by_chat`, bounded to 200 entries) so the agent can react without threading message ids through tool calls - New `add_reaction`/`remove_reaction` public methods on PhotonAdapter; unlike the lifecycle tapbacks, these are not gated by PHOTON_REACTIONS - `send_message` gains `action='react'` with `emoji` and optional `message_id` params; resolves target via existing channel-directory and home-channel logic; requires a live gateway adapter
This commit is contained in:
parent
a23c0b378c
commit
156f4fba92
2 changed files with 172 additions and 2 deletions
|
|
@ -230,6 +230,10 @@ class PhotonAdapter(BasePlatformAdapter):
|
|||
# reaction events are only routed to the agent when they target one of
|
||||
# these — a tapback on a human↔human message is not addressed to us.
|
||||
self._sent_message_ids: Dict[str, float] = {}
|
||||
# Latest inbound message id per chat (bounded). Lets the agent-facing
|
||||
# react action default to "the message that triggered me" without
|
||||
# requiring the model to thread message ids through tool calls.
|
||||
self._last_inbound_by_chat: Dict[str, str] = {}
|
||||
|
||||
# Group-chat mention gating (parity with BlueBubbles). When enabled,
|
||||
# group messages are ignored unless they match a wake word; DMs are
|
||||
|
|
@ -538,6 +542,11 @@ class PhotonAdapter(BasePlatformAdapter):
|
|||
)
|
||||
)
|
||||
return
|
||||
# Anything past here is a real (reactable) message — remember it as
|
||||
# the chat's latest inbound so `add_reaction` can target it when the
|
||||
# caller doesn't pass an explicit message id. Recorded before the
|
||||
# mention gate: a reaction to a non-wake-word group message is valid.
|
||||
self._record_last_inbound(space_id, event.get("messageId"))
|
||||
if ctype == "text":
|
||||
text = content.get("text") or ""
|
||||
mtype = MessageType.TEXT
|
||||
|
|
@ -944,6 +953,7 @@ class PhotonAdapter(BasePlatformAdapter):
|
|||
# is a personal-texting channel, and a tapback on every text is noisy.
|
||||
|
||||
_SENT_IDS_MAX = 1000
|
||||
_LAST_INBOUND_CHATS_MAX = 200
|
||||
|
||||
def _record_sent_message(self, message_id: Optional[str]) -> None:
|
||||
if not message_id:
|
||||
|
|
@ -956,6 +966,21 @@ class PhotonAdapter(BasePlatformAdapter):
|
|||
for old in list(sent.keys())[: len(sent) - self._SENT_IDS_MAX]:
|
||||
del sent[old]
|
||||
|
||||
def _record_last_inbound(
|
||||
self, chat_id: Optional[str], message_id: Optional[str]
|
||||
) -> None:
|
||||
if not chat_id or not message_id:
|
||||
return
|
||||
last = self._last_inbound_by_chat
|
||||
if chat_id in last:
|
||||
del last[chat_id] # refresh insertion order
|
||||
last[chat_id] = message_id
|
||||
if len(last) > self._LAST_INBOUND_CHATS_MAX:
|
||||
for old in list(last.keys())[
|
||||
: len(last) - self._LAST_INBOUND_CHATS_MAX
|
||||
]:
|
||||
del last[old]
|
||||
|
||||
def _reactions_enabled(self) -> bool:
|
||||
return os.getenv("PHOTON_REACTIONS", "false").strip().lower() in {
|
||||
"true", "1", "yes", "on",
|
||||
|
|
@ -991,6 +1016,58 @@ class PhotonAdapter(BasePlatformAdapter):
|
|||
logger.debug("[photon] remove_reaction failed: %s", e)
|
||||
return False
|
||||
|
||||
# -- Agent-facing reactions (send_message action="react") ---------------
|
||||
#
|
||||
# Unlike the lifecycle hooks below, these are deliberate agent intents,
|
||||
# so they are NOT gated by PHOTON_REACTIONS (that env var exists to mute
|
||||
# the automatic per-message tapback noise, not explicit requests).
|
||||
|
||||
async def add_reaction(
|
||||
self,
|
||||
chat_id: str,
|
||||
emoji: str,
|
||||
message_id: Optional[str] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""Tapback ``emoji`` onto a message in ``chat_id``.
|
||||
|
||||
Without ``message_id``, targets the chat's most recent inbound
|
||||
message (typically the one the agent is responding to). iMessage
|
||||
maps ❤️👍👎😂‼️❓ to native tapbacks; anything else uses Apple's
|
||||
custom-emoji reaction.
|
||||
"""
|
||||
target = message_id or self._last_inbound_by_chat.get(chat_id)
|
||||
if not target:
|
||||
return {
|
||||
"success": False,
|
||||
"error": "no message to react to — pass message_id (no "
|
||||
"inbound message seen in this chat since the gateway started)",
|
||||
}
|
||||
ok = await self._add_reaction(chat_id, target, emoji)
|
||||
if not ok:
|
||||
return {
|
||||
"success": False,
|
||||
"error": "reaction failed (see gateway debug log)",
|
||||
}
|
||||
return {"success": True, "message_id": target}
|
||||
|
||||
async def remove_reaction(
|
||||
self, chat_id: str, message_id: Optional[str] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""Retract our tapback from a message (best-effort)."""
|
||||
target = message_id or self._last_inbound_by_chat.get(chat_id)
|
||||
if not target:
|
||||
return {
|
||||
"success": False,
|
||||
"error": "no message to unreact — pass message_id",
|
||||
}
|
||||
ok = await self._remove_reaction(chat_id, target)
|
||||
if not ok:
|
||||
return {
|
||||
"success": False,
|
||||
"error": "unreact failed (see gateway debug log)",
|
||||
}
|
||||
return {"success": True, "message_id": target}
|
||||
|
||||
async def on_processing_start(self, event: MessageEvent) -> None:
|
||||
"""Tapback 👀 on the triggering message while the agent works."""
|
||||
if not self._reactions_enabled():
|
||||
|
|
|
|||
|
|
@ -138,8 +138,8 @@ SEND_MESSAGE_SCHEMA = {
|
|||
"properties": {
|
||||
"action": {
|
||||
"type": "string",
|
||||
"enum": ["send", "list"],
|
||||
"description": "Action to perform. 'send' (default) sends a message. 'list' returns all available channels/contacts across connected platforms."
|
||||
"enum": ["send", "list", "react"],
|
||||
"description": "Action to perform. 'send' (default) sends a message. 'list' returns all available channels/contacts across connected platforms. 'react' attaches an emoji reaction to a message (platforms that support it, e.g. photon/iMessage tapbacks)."
|
||||
},
|
||||
"target": {
|
||||
"type": "string",
|
||||
|
|
@ -148,6 +148,14 @@ SEND_MESSAGE_SCHEMA = {
|
|||
"message": {
|
||||
"type": "string",
|
||||
"description": "The message text to send. To send an image or file, include MEDIA:<local_path> (e.g. 'MEDIA:/tmp/report.pdf') in the message — the platform will deliver it as a native media attachment."
|
||||
},
|
||||
"emoji": {
|
||||
"type": "string",
|
||||
"description": "For action='react': the emoji to react with (e.g. '❤️'). On iMessage, ❤️👍👎😂‼️❓ render as native tapbacks; other emoji use custom-emoji reactions."
|
||||
},
|
||||
"message_id": {
|
||||
"type": "string",
|
||||
"description": "For action='react': id of the message to react to. Omit to react to the most recent message received in that chat (usually the one being replied to)."
|
||||
}
|
||||
},
|
||||
"required": []
|
||||
|
|
@ -162,6 +170,9 @@ def send_message_tool(args, **kw):
|
|||
if action == "list":
|
||||
return _handle_list()
|
||||
|
||||
if action == "react":
|
||||
return _handle_react(args)
|
||||
|
||||
return _handle_send(args)
|
||||
|
||||
|
||||
|
|
@ -174,6 +185,88 @@ def _handle_list():
|
|||
return json.dumps(_error(f"Failed to load channel directory: {e}"))
|
||||
|
||||
|
||||
def _handle_react(args):
|
||||
"""Attach an emoji reaction to a message via a live gateway adapter.
|
||||
|
||||
Only adapters that expose an ``add_reaction(chat_id, emoji, message_id)``
|
||||
coroutine support this (e.g. photon/iMessage tapbacks). Requires the
|
||||
gateway to be running in this process — there is no standalone fallback,
|
||||
since 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
|
||||
if not target or not emoji:
|
||||
return tool_error(
|
||||
"Both 'target' and 'emoji' are required when action='react'"
|
||||
)
|
||||
|
||||
parts = target.split(":", 1)
|
||||
platform_name = parts[0].strip().lower()
|
||||
target_ref = parts[1].strip() if len(parts) > 1 else None
|
||||
chat_id = None
|
||||
if target_ref:
|
||||
chat_id, _thread_id, _ = _parse_target_ref(platform_name, target_ref)
|
||||
if not chat_id:
|
||||
try:
|
||||
from gateway.channel_directory import resolve_channel_name
|
||||
resolved = resolve_channel_name(platform_name, target_ref)
|
||||
except Exception:
|
||||
resolved = None
|
||||
# Opaque platform-native ids (e.g. photon space GUIDs like
|
||||
# 'any;-;+1555...') match no parser pattern and no directory
|
||||
# entry — pass them through verbatim; the adapter validates.
|
||||
chat_id = resolved or target_ref
|
||||
|
||||
try:
|
||||
from gateway.config import Platform, load_gateway_config
|
||||
platform = Platform(platform_name)
|
||||
except (ValueError, KeyError):
|
||||
return tool_error(f"Unknown platform: {platform_name}")
|
||||
|
||||
if not chat_id:
|
||||
try:
|
||||
config = load_gateway_config()
|
||||
home = config.get_home_channel(platform)
|
||||
except Exception:
|
||||
home = None
|
||||
if not home:
|
||||
return tool_error(
|
||||
f"No chat specified and no home channel set for {platform_name}. "
|
||||
f"Use '{platform_name}:chat_id'."
|
||||
)
|
||||
chat_id = home.chat_id
|
||||
|
||||
runner = None
|
||||
try:
|
||||
from gateway.run import _gateway_runner_ref
|
||||
runner = _gateway_runner_ref()
|
||||
except Exception:
|
||||
runner = None
|
||||
adapter = runner.adapters.get(platform) if runner is not None else None
|
||||
if adapter is None:
|
||||
return tool_error(
|
||||
f"Reactions require a live {platform_name} adapter in the running "
|
||||
"gateway (not available from cron/standalone contexts)."
|
||||
)
|
||||
react_fn = getattr(adapter, "add_reaction", None)
|
||||
if not callable(react_fn):
|
||||
return tool_error(
|
||||
f"Platform '{platform_name}' does not support message reactions."
|
||||
)
|
||||
|
||||
try:
|
||||
from model_tools import _run_async
|
||||
result = _run_async(
|
||||
react_fn(chat_id=chat_id, emoji=emoji, message_id=message_id)
|
||||
)
|
||||
except Exception as e:
|
||||
return json.dumps(_error(f"Reaction failed: {e}"))
|
||||
if isinstance(result, dict):
|
||||
return json.dumps(result)
|
||||
return json.dumps({"success": bool(result)})
|
||||
|
||||
|
||||
def _handle_send(args):
|
||||
"""Send a message to a platform target."""
|
||||
target = args.get("target", "")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue