mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-06-11 08:42:11 +00:00
Adds the last missing parity piece vs the established channels: group chats can be made opt-in via a mention wake word, exactly like the BlueBubbles iMessage channel. - require_mention + mention_patterns, read from config.extra (config.yaml via the generic gateway bridge) or PHOTON_REQUIRE_MENTION / PHOTON_MENTION_PATTERNS env vars. Same shapes BlueBubbles accepts (list / JSON / comma / newline), same default Hermes wake words. - _dispatch_inbound drops unmatched group messages and strips the leading wake word from matched ones; DMs are never gated. - plugin.yaml + docs document both knobs and the config.yaml form. - New test_mention_gating.py (8 tests): default-off, group drop/pass, wake-word strip, DM bypass, custom patterns, env comma-list, invalid regex skip. The config.yaml -> extra bridge needed no core change — the generic shared-key loop in gateway/config.py already iterates plugin platforms (_shared_loop_targets += plugin_entries()), so require_mention / mention_patterns flow through automatically. Note: outbound media is the one capability Photon still can't reach — Photon exposes no HTTP send-attachment endpoint yet (documented API limitation), so the sidecar can't send files. Not faked. Validation: 34/34 photon tests; E2E confirms config.yaml require_mention + custom mention_patterns bridge through load_gateway_config into a live adapter and gate/strip correctly.
146 lines
5.4 KiB
Python
146 lines
5.4 KiB
Python
"""Group-chat mention-gating tests for PhotonAdapter.
|
|
|
|
Parity with the BlueBubbles iMessage channel: when ``require_mention`` is
|
|
enabled, group messages are dropped unless they hit a wake-word pattern,
|
|
and the leading wake word is stripped from the ones that pass. DMs are
|
|
never gated.
|
|
|
|
These call ``_dispatch_inbound`` directly (no aiohttp / ports) and assert
|
|
on what reaches ``handle_message``.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from typing import List
|
|
|
|
import pytest
|
|
|
|
from gateway.config import PlatformConfig
|
|
from gateway.platforms.base import MessageEvent
|
|
from plugins.platforms.photon.adapter import PhotonAdapter
|
|
|
|
|
|
def _make_adapter(monkeypatch: pytest.MonkeyPatch, extra: dict | None = None) -> PhotonAdapter:
|
|
monkeypatch.setenv("PHOTON_PROJECT_ID", "test-project-id")
|
|
monkeypatch.setenv("PHOTON_PROJECT_SECRET", "test-project-secret")
|
|
monkeypatch.delenv("PHOTON_WEBHOOK_SECRET", raising=False)
|
|
monkeypatch.delenv("PHOTON_REQUIRE_MENTION", raising=False)
|
|
monkeypatch.delenv("PHOTON_MENTION_PATTERNS", raising=False)
|
|
cfg = PlatformConfig(enabled=True, token="", extra=extra or {})
|
|
return PhotonAdapter(cfg)
|
|
|
|
|
|
def _group_payload(text: str) -> dict:
|
|
return {
|
|
"event": "messages",
|
|
"message": {
|
|
"id": f"grp-{abs(hash(text))}",
|
|
"timestamp": "2026-05-14T19:06:32.000Z",
|
|
"sender": {"id": "+15551234567"},
|
|
"space": {"id": "any;+;group-guid-xyz"},
|
|
"content": {"type": "text", "text": text},
|
|
},
|
|
}
|
|
|
|
|
|
def _dm_payload(text: str) -> dict:
|
|
return {
|
|
"event": "messages",
|
|
"message": {
|
|
"id": f"dm-{abs(hash(text))}",
|
|
"timestamp": "2026-05-14T19:06:32.000Z",
|
|
"sender": {"id": "+15551234567"},
|
|
"space": {"id": "any;-;+15551234567"},
|
|
"content": {"type": "text", "text": text},
|
|
},
|
|
}
|
|
|
|
|
|
def _capture(adapter: PhotonAdapter, monkeypatch: pytest.MonkeyPatch) -> List[MessageEvent]:
|
|
captured: List[MessageEvent] = []
|
|
|
|
async def fake_handle(event: MessageEvent) -> None:
|
|
captured.append(event)
|
|
|
|
monkeypatch.setattr(adapter, "handle_message", fake_handle)
|
|
return captured
|
|
|
|
|
|
def test_require_mention_defaults_off(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
adapter = _make_adapter(monkeypatch)
|
|
assert adapter.require_mention is False
|
|
# Defaults compile to the two Hermes wake-word patterns.
|
|
assert len(adapter._mention_patterns) == 2
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_group_message_dropped_without_mention(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
adapter = _make_adapter(monkeypatch, extra={"require_mention": True})
|
|
captured = _capture(adapter, monkeypatch)
|
|
|
|
await adapter._dispatch_inbound(_group_payload("just chatting, no wake word"))
|
|
assert captured == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_group_message_passes_and_strips_wake_word(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
adapter = _make_adapter(monkeypatch, extra={"require_mention": True})
|
|
captured = _capture(adapter, monkeypatch)
|
|
|
|
await adapter._dispatch_inbound(_group_payload("Hermes what's the weather"))
|
|
assert len(captured) == 1
|
|
# Leading wake word stripped before dispatch.
|
|
assert captured[0].text == "what's the weather"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_dm_never_gated(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
adapter = _make_adapter(monkeypatch, extra={"require_mention": True})
|
|
captured = _capture(adapter, monkeypatch)
|
|
|
|
await adapter._dispatch_inbound(_dm_payload("no wake word here"))
|
|
assert len(captured) == 1
|
|
assert captured[0].text == "no wake word here"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_require_mention_off_passes_group_messages(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
adapter = _make_adapter(monkeypatch) # require_mention defaults off
|
|
captured = _capture(adapter, monkeypatch)
|
|
|
|
await adapter._dispatch_inbound(_group_payload("plain group chatter"))
|
|
assert len(captured) == 1
|
|
assert captured[0].text == "plain group chatter"
|
|
|
|
|
|
def test_custom_mention_patterns_from_config(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
adapter = _make_adapter(
|
|
monkeypatch,
|
|
extra={"require_mention": True, "mention_patterns": [r"(?<![\w@])@?amos\b[,:\-]?"]},
|
|
)
|
|
assert adapter.require_mention is True
|
|
assert len(adapter._mention_patterns) == 1
|
|
assert adapter._message_matches_mention_patterns("amos help me") is True
|
|
assert adapter._message_matches_mention_patterns("hermes help me") is False
|
|
|
|
|
|
def test_mention_patterns_env_comma_separated(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setenv("PHOTON_PROJECT_ID", "test-project-id")
|
|
monkeypatch.setenv("PHOTON_PROJECT_SECRET", "test-project-secret")
|
|
monkeypatch.delenv("PHOTON_WEBHOOK_SECRET", raising=False)
|
|
monkeypatch.setenv("PHOTON_REQUIRE_MENTION", "true")
|
|
monkeypatch.setenv("PHOTON_MENTION_PATTERNS", r"bot\b, assistant\b")
|
|
cfg = PlatformConfig(enabled=True, token="", extra={})
|
|
adapter = PhotonAdapter(cfg)
|
|
assert adapter.require_mention is True
|
|
assert len(adapter._mention_patterns) == 2
|
|
assert adapter._message_matches_mention_patterns("hey bot") is True
|
|
|
|
|
|
def test_invalid_pattern_skipped(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
adapter = _make_adapter(
|
|
monkeypatch,
|
|
extra={"require_mention": True, "mention_patterns": ["(unclosed", r"good\b"]},
|
|
)
|
|
# Bad regex dropped, good one kept.
|
|
assert len(adapter._mention_patterns) == 1
|
|
assert adapter._message_matches_mention_patterns("a good thing") is True
|