Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions hermes_plugin_kit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def register(ctx):
"deliver_media",
"resolve_delivery_target",
"MEDIA_DELIVERED_REPLY",
"strip_media_mirror_lines",
"transform_media_delivery_output",
"clear_media_delivery_state",
"MediaType",
Expand Down Expand Up @@ -1874,6 +1875,27 @@ def _mark_media_delivery_success(context: dict[str, Any]) -> None:
# The turn's visible reply after the attachment itself was already sent.
MEDIA_DELIVERED_REPLY = "✅"

# Hermes' send_message records each media send as an assistant line so the model
# remembers it (tools/send_message_tool.py _describe_media_for_mirror). Models copy
# those lines into real replies, where the user receives them as text.
_MEDIA_MIRROR_LINE = re.compile(
r"\[Sent (?:voice message|(?:image|video|audio|document) attachment|\d+ media attachments)\]"
)


def strip_media_mirror_lines(response_text: str) -> str | None:
"""Remove whole lines that copy Hermes' sent-media history notes from a final reply.

Returns ``None`` when there is nothing to remove. A reply left empty, or holding
only ``NO_REPLY``, becomes :data:`MEDIA_DELIVERED_REPLY`: the note follows a send.
"""
lines = str(response_text or "").splitlines()
kept = [line for line in lines if not _MEDIA_MIRROR_LINE.fullmatch(line.strip())]
if len(kept) == len(lines):
return None
rest = re.sub(r"\n{3,}", "\n\n", "\n".join(kept)).strip()
return MEDIA_DELIVERED_REPLY if rest in ("", "NO_REPLY") else rest


def transform_media_delivery_output(
response_text: str = "",
Expand Down
16 changes: 16 additions & 0 deletions tests/test_hermes_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,22 @@ def test_media_delivered_reply_is_not_a_hermes_silence_marker(self) -> None:
self.assertTrue(is_intentional_silence_response("NO_REPLY"))
self.assertFalse(is_intentional_silence_response(hpk.MEDIA_DELIVERED_REPLY))

def test_strip_media_mirror_lines_matches_every_hermes_mirror_shape(self) -> None:
"""Each note Hermes' send_message writes into history must be strippable."""
from tools.send_message_tool import _describe_media_for_mirror # type: ignore

cases = [
[("/m/memo.ogg", True)],
[("/m/pic.png", False)],
[("/m/clip.mp4", False)],
[("/m/song.mp3", False)],
[("/m/notes.pdf", False)],
[("/m/a.png", False), ("/m/b.png", False)],
]
for media in cases:
note = _describe_media_for_mirror(media)
with self.subTest(note=note):
self.assertEqual(hpk.strip_media_mirror_lines(f"Done.\n{note}"), "Done.")
def test_reserved_tool_names_match_the_host(self) -> None:
"""The kit's offline fallback must equal what the deployed Hermes intercepts."""
from model_tools import _AGENT_LOOP_TOOLS, _LEGACY_TOOL_ALIASES # type: ignore
Expand Down
10 changes: 10 additions & 0 deletions tests/test_kit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,16 @@ def test_successful_delivery_suppresses_the_same_turn_final_response_once(self)
)
)

def test_strip_media_mirror_lines_removes_only_whole_placeholder_lines(self) -> None:
self.assertEqual(
hpk.strip_media_mirror_lines("Here she is.\n\nMEDIA:/x.jpg\n[Sent voice message]\n"),
"Here she is.\n\nMEDIA:/x.jpg",
)
self.assertEqual(hpk.strip_media_mirror_lines("NO_REPLY\n[Sent image attachment]"), "✅")
self.assertEqual(hpk.strip_media_mirror_lines("[Sent 3 media attachments]"), "✅")
self.assertIsNone(hpk.strip_media_mirror_lines("I saw [Sent voice message] in the log"))
self.assertIsNone(hpk.strip_media_mirror_lines("hello"))

def test_successful_delivery_does_not_suppress_another_session(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "memo.ogg"
Expand Down
Loading