feat(messaging): expose action='unreact' in send_message + react dispatch tests

Follow-up for salvaged PR #44486: the adapter shipped remove_reaction but
the tool only exposed 'react'. Generalize _handle_react(remove=) and add
tool-level dispatch tests for react/unreact (missing from the original PR).
This commit is contained in:
teknium1 2026-06-11 23:36:53 -07:00 committed by Teknium
parent b4e95a2efe
commit 05470aa1b6
2 changed files with 124 additions and 14 deletions

View file

@ -0,0 +1,97 @@
"""Tests for send_message action='react'/'unreact' dispatch.
Kept separate from ``test_send_message_tool.py`` because that module skips
wholesale when optional Telegram dependencies are not installed.
"""
import json
from types import SimpleNamespace
from unittest.mock import patch
import tools.send_message_tool as smt
class _FakePhotonAdapter:
"""Adapter exposing add_reaction/remove_reaction coroutines."""
def __init__(self):
self.calls = []
async def add_reaction(self, chat_id, emoji, message_id=None):
self.calls.append(("add", chat_id, emoji, message_id))
return {"success": True, "emoji": emoji}
async def remove_reaction(self, chat_id, message_id=None):
self.calls.append(("remove", chat_id, message_id))
return {"success": True}
class _NoReactionAdapter:
"""Adapter with no reaction support at all."""
def _runner_with(adapter):
from gateway.config import Platform
return SimpleNamespace(adapters={Platform("photon"): adapter})
def _call(args):
return json.loads(smt.send_message_tool(args))
def test_react_dispatches_to_add_reaction():
adapter = _FakePhotonAdapter()
with patch("gateway.run._gateway_runner_ref", lambda: _runner_with(adapter)):
result = _call(
{"action": "react", "target": "photon:+15551234567", "emoji": "❤️"}
)
assert result["success"] is True
assert adapter.calls == [("add", "+15551234567", "❤️", None)]
def test_unreact_dispatches_to_remove_reaction():
adapter = _FakePhotonAdapter()
with patch("gateway.run._gateway_runner_ref", lambda: _runner_with(adapter)):
result = _call(
{
"action": "unreact",
"target": "photon:+15551234567",
"message_id": "msg-9",
}
)
assert result["success"] is True
assert adapter.calls == [("remove", "+15551234567", "msg-9")]
def test_react_requires_emoji():
result = _call({"action": "react", "target": "photon:+15551234567"})
assert result.get("success") is not True
assert "emoji" in json.dumps(result)
def test_unreact_does_not_require_emoji():
adapter = _FakePhotonAdapter()
with patch("gateway.run._gateway_runner_ref", lambda: _runner_with(adapter)):
result = _call({"action": "unreact", "target": "photon:+15551234567"})
assert result["success"] is True
assert adapter.calls == [("remove", "+15551234567", None)]
def test_react_unsupported_platform_adapter():
adapter = _NoReactionAdapter()
with patch("gateway.run._gateway_runner_ref", lambda: _runner_with(adapter)):
result = _call(
{"action": "react", "target": "photon:+15551234567", "emoji": "👍"}
)
assert result.get("success") is not True
assert "does not support" in json.dumps(result)
def test_react_without_live_gateway():
with patch("gateway.run._gateway_runner_ref", lambda: None):
result = _call(
{"action": "react", "target": "photon:+15551234567", "emoji": "👍"}
)
assert result.get("success") is not True
assert "live" in json.dumps(result)

View file

@ -138,8 +138,8 @@ SEND_MESSAGE_SCHEMA = {
"properties": {
"action": {
"type": "string",
"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)."
"enum": ["send", "list", "react", "unreact"],
"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). 'unreact' retracts a previously-added reaction."
},
"target": {
"type": "string",
@ -155,7 +155,7 @@ SEND_MESSAGE_SCHEMA = {
},
"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)."
"description": "For action='react'/'unreact': id of the message to react to. Omit to target the most recent message received in that chat (usually the one being replied to)."
}
},
"required": []
@ -173,6 +173,9 @@ def send_message_tool(args, **kw):
if action == "react":
return _handle_react(args)
if action == "unreact":
return _handle_react(args, remove=True)
return _handle_send(args)
@ -185,20 +188,24 @@ 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.
def _handle_react(args, remove=False):
"""Attach (or with ``remove=True`` retract) an emoji reaction on 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.
Only adapters that expose ``add_reaction(chat_id, emoji, message_id)`` /
``remove_reaction(chat_id, message_id)`` coroutines 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:
if not target or (not remove and not emoji):
return tool_error(
"Both 'target' and 'emoji' are required when action='react'"
if not remove
else "'target' is required when action='unreact'"
)
parts = target.split(":", 1)
@ -249,7 +256,8 @@ def _handle_react(args):
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)
fn_name = "remove_reaction" if remove else "add_reaction"
react_fn = getattr(adapter, fn_name, None)
if not callable(react_fn):
return tool_error(
f"Platform '{platform_name}' does not support message reactions."
@ -257,9 +265,14 @@ def _handle_react(args):
try:
from model_tools import _run_async
result = _run_async(
react_fn(chat_id=chat_id, emoji=emoji, message_id=message_id)
)
if remove:
result = _run_async(
react_fn(chat_id=chat_id, message_id=message_id)
)
else:
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):