Skip to content

The agent that grows with you

fix(cli): the attached classic chat is the plugin manager's CLI inject slot again

Teknium6ad50e9c4f6e
diff --git a/hermes_cli/gateway_chat_inject.py b/hermes_cli/gateway_chat_inject.py
new file mode 100644
index 00000000000..eb04f4009be
--- /dev/null
+++ b/hermes_cli/gateway_chat_inject.py
@@ -0,0 +1,43 @@
+"""The attached classic view as the plugin manager's CLI slot.
+
+Main's classic REPL registered itself as ``PluginManager._cli_ref``, so ``ctx.inject_message(text)``
+with no ``session_key``/``origin`` (the documented "Active CLI conversation" form) queued into the
+REPL. The classic chat is now a gateway client: the same call submits into the attached session at
+the gateway (same FIFO as a typed line) from whatever thread the plugin calls on."""
+from __future__ import annotations
+
+import asyncio
+from contextlib import contextmanager
+import logging
+
+logger = logging.getLogger(__name__)
+
+
+class _InjectSlot:
+ _agent_running = False # one queue: the gateway FIFO orders it behind a running turn
+
+ def __init__(self, view, loop):
+ self._view, self._loop = view, loop
+ self._pending_input = self._interrupt_queue = self
+
+ def put(self, message):
+ asyncio.run_coroutine_threadsafe(self._submit(message), self._loop)
+
+ async def _submit(self, message):
+ print(f"\n{'─' * 40}\n● {message}", flush=True)
+ try:
+ await self._view.submit(message)
+ except Exception:
+ logger.warning("plugin inject_message: submit to %s failed", self._view.session_id, exc_info=True)
+
+
+@contextmanager
+def plugin_inject_slot(view):
+ from hermes_cli.plugins import get_plugin_manager
+ manager = get_plugin_manager()
+ slot = manager._cli_ref = _InjectSlot(view, asyncio.get_running_loop())
+ try:
+ yield
+ finally:
+ if manager._cli_ref is slot:
+ manager._cli_ref = None
diff --git a/hermes_cli/gateway_chat_view.py b/hermes_cli/gateway_chat_view.py
index 7ed52d546b2..d0a22d7edb0 100644
--- a/hermes_cli/gateway_chat_view.py
+++ b/hermes_cli/gateway_chat_view.py
@@ -456,7 +456,8 @@ class GatewayChatView:
bottom_toolbar=lambda: f" \u2624 {self.model} \u2502 {self.session_id} ")
prompt_symbol = get_active_prompt_symbol("❯ ")
self._composer = prompt if sys.stdin.isatty() else None
- with patch_stdout():
+ from hermes_cli.gateway_chat_inject import plugin_inject_slot
+ with patch_stdout(), plugin_inject_slot(self):
while not self.failure:
try:
# Empty when the stream failed first: the loop guard then raises it.
diff --git a/tests/hermes_cli/test_gateway_chat_plugin_inject.py b/tests/hermes_cli/test_gateway_chat_plugin_inject.py
new file mode 100644
index 00000000000..a75f011d728
--- /dev/null
+++ b/tests/hermes_cli/test_gateway_chat_plugin_inject.py
@@ -0,0 +1,44 @@
+"""`ctx.inject_message(text)` with no session_key/origin (plugins.md "Active CLI conversation") reaches
+the attached classic `hermes chat` session as a prompt.submit, from a plugin's own thread (B2-11)."""
+import asyncio
+import threading
+
+import pytest
+
+
+async def test_no_key_inject_submits_into_the_attached_classic_session(capsys):
+ from hermes_cli.gateway_chat_view import GatewayChatView
+ from hermes_cli.plugins import PluginContext, get_plugin_manager
+ from hermes_cli.plugins_manifest import PluginManifest
+ submitted = asyncio.Event()
+
+ class Peer:
+ events = asyncio.Queue()
+ calls = []
+
+ async def rpc(self, method, **params):
+ self.calls.append((method, params))
+ if method == "prompt.submit":
+ submitted.set()
+ return {"admission_id": "adm"}
+
+ view = GatewayChatView(Peer(), {"stored_session_id": "sid"})
+ ctx = PluginContext(PluginManifest(name="watcher"), get_plugin_manager())
+ accepted = []
+
+ async def read_line(prompt, symbol):
+ if accepted:
+ return "/quit"
+ thread = threading.Thread(target=lambda: accepted.append(ctx.inject_message("New data arrived")))
+ thread.start()
+ thread.join()
+ await asyncio.wait_for(submitted.wait(), 5)
+ return ""
+
+ view._read_line = read_line
+ assert await view.run() == 0
+ assert accepted == [True]
+ assert [(m, p["session_id"], p["text"]) for m, p in Peer.calls] == [("prompt.submit", "sid", "New data arrived")]
+ assert get_plugin_manager()._cli_ref is None # detached: the slot is released
+ assert "● New data arrived" in capsys.readouterr().out