diff --git a/hermes_plugin_kit/__init__.py b/hermes_plugin_kit/__init__.py index 880ff1e..063d978 100644 --- a/hermes_plugin_kit/__init__.py +++ b/hermes_plugin_kit/__init__.py @@ -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", @@ -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 = "", diff --git a/tests/test_hermes_contract.py b/tests/test_hermes_contract.py index 63fbe3e..d080cd4 100644 --- a/tests/test_hermes_contract.py +++ b/tests/test_hermes_contract.py @@ -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 diff --git a/tests/test_kit.py b/tests/test_kit.py index 2ba88be..cf5a9d5 100644 --- a/tests/test_kit.py +++ b/tests/test_kit.py @@ -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"