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:
committed by
Teknium
parent
a23c0b378c
commit
156f4fba92
@@ -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", "")
|
||||
|
||||
Reference in New Issue
Block a user