The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway): move plugin message injection into its own runner mixin
Teknium3a2588f01483
diff --git a/gateway/run_inbound.py b/gateway/run_inbound.pyindex 171e536612b..81d340c8474 100644--- a/gateway/run_inbound.py+++ b/gateway/run_inbound.py@@ -10,7 +10,7 @@ from __future__ import annotationsimport loggingfrom typing import TYPE_CHECKINGimport asyncio-import concurrent.futures+import concurrent.futures # noqa: F401 -- kept public after the plugin-injection moveimport dataclassesimport jsonimport os@@ -26,6 +26,7 @@ from gateway.platforms.event import MessageEvent, MessageTypefrom gateway.run_busy import approval_input_wordsfrom gateway.run_common import _UNSETfrom gateway.run_inbound_media import rehome_inbound_media+from gateway.run_plugin_injection import GatewayPluginInjectionMixinfrom gateway.run_inbound_unauthorized import (UnauthorizedOwnerNotifier, pairing_code_reply, pairing_profile_arg, pairing_rate_limited_reply,unauthorized_owner_hint,@@ -66,7 +67,7 @@ def strip_discord_triggering_note(event: Any, message_text: Any) -> Any:return message_text[len(prefix):] if message_text.startswith(prefix) else message_text-class GatewayInboundMixin:+class GatewayInboundMixin(GatewayPluginInjectionMixin):"""Inbound message pipeline (_handle_message, text/media preparation, durable-turn markers, plugin injection) for GatewayRunner."""async def _hm_pre_gateway_dispatch_hook(@@ -1807,117 +1808,6 @@ class GatewayInboundMixin:with suppress(AttributeError):delattr(event, attr)- def _install_plugin_message_injector(self) -> None:- """Publish this live gateway's plugin message scheduler process-wide."""- from hermes_cli.plugins import publish_gateway_message_host-- publish_gateway_message_host(self, self._schedule_plugin_message_injection)-- def _clear_plugin_message_injector(self) -> None:- """Remove this runner's scheduler without clobbering a newer owner."""- from hermes_cli.plugins import clear_published_gateway_message_host-- clear_published_gateway_message_host(self)-- def _schedule_plugin_message_injection(- self, *, session_key: str, content: str, plugin_id: str- ) -> bool:- """Schedule a plugin-triggered turn on the live gateway loop (thread-safe)."""- from gateway.run import safe_schedule_threadsafe- loop = getattr(self, "_gateway_loop", None)- if not getattr(self, "_running", False) or loop is None or loop.is_closed():- return False-- coro = self._dispatch_plugin_message_injection(- session_key=session_key, content=content, plugin_id=plugin_id,- )- try:- current_loop = asyncio.get_running_loop()- except RuntimeError:- current_loop = None-- if current_loop is loop:- try:- future = loop.create_task(coro)- except Exception:- coro.close()- logger.warning("Plugin message injection scheduling failed", exc_info=True)- return False- self._background_tasks.add(future)- future.add_done_callback(self._background_tasks.discard)- else:- future = safe_schedule_threadsafe(- coro, loop, logger=logger, log_message="Plugin message injection scheduling failed",- log_level=logging.WARNING,- )- if future is None:- return False-- def _log_result(completed) -> None:- try:- if completed.result():- return- what, exc = "was not routed", None- except (asyncio.CancelledError, concurrent.futures.CancelledError):- return- except Exception as err:- what, exc = "failed", err- logger.warning(- "Plugin message injection %s: plugin=%s session=%s", what, plugin_id, session_key, exc_info=exc,- )-- future.add_done_callback(_log_result)- return True-- async def _dispatch_plugin_message_injection(- self, *, session_key: str, content: str, plugin_id: str- ) -> bool:- """Route a plugin-triggered turn through the session's live adapter."""- def _accepting() -> bool:- return getattr(self, "_running", False) and not getattr(self, "_draining", False)-- if not _accepting():- return False- entry = await self.async_session_store.lookup_by_session_key(session_key)- if entry is None or entry.origin is None or not _accepting():- return False-- from gateway.session_identity import replace_source- source = replace_source(self._restored_source(entry))- try:- authorized = self._is_user_authorized_for_source(source, allow_adapter_delegation=False)- except Exception:- logger.warning(- "Plugin message injection authorization check failed: plugin=%s session=%s",- plugin_id, session_key, exc_info=True,- )- return False- if not authorized:- logger.warning(- "Plugin message injection denied by current gateway authorization: "- "plugin=%s session=%s", plugin_id, session_key,- )- return False-- adapter = self._delivery_adapter_for(source)- if adapter is None:- return False-- await adapter.handle_message(MessageEvent(- text=content, message_type=MessageType.TEXT, source=source, internal=True,- allow_gateway_control=False,- metadata={- "hermes_plugin_id": plugin_id, "hermes_plugin_injection": True,- "gateway_session_key": session_key, "gateway_session_id": entry.session_id,- "gateway_session_strict": True,- },- ))- logger.info(- "Plugin message injection dispatched: plugin=%s session=%s session_id=%s",- plugin_id, session_key, entry.session_id,- )- return True-def _decide_image_input_mode(self, *, source: Optional[SessionSource] = None, session_key: Optional[str] = None,user_config: Optional[dict] = None, provider: Optional[str] = None,diff --git a/gateway/run_plugin_injection.py b/gateway/run_plugin_injection.pynew file mode 100644index 00000000000..f72fe8bd951--- /dev/null+++ b/gateway/run_plugin_injection.py@@ -0,0 +1,124 @@+"""Plugin-triggered gateway turns (``PluginContext.inject_message``): the process-wide injector this+runner publishes, the thread-safe scheduler, and the dispatch through the adapter message path."""+from __future__ import annotations++import asyncio+import concurrent.futures+import logging++from gateway.platforms.event import MessageEvent, MessageType++logger = logging.getLogger("gateway.run")+++class GatewayPluginInjectionMixin:+ def _install_plugin_message_injector(self) -> None:+ """Publish this live gateway's plugin message scheduler process-wide."""+ from hermes_cli.plugins import publish_gateway_message_host++ publish_gateway_message_host(self, self._schedule_plugin_message_injection)++ def _clear_plugin_message_injector(self) -> None:+ """Remove this runner's scheduler without clobbering a newer owner."""+ from hermes_cli.plugins import clear_published_gateway_message_host++ clear_published_gateway_message_host(self)++ def _schedule_plugin_message_injection(+ self, *, session_key: str, content: str, plugin_id: str+ ) -> bool:+ """Schedule a plugin-triggered turn on the live gateway loop (thread-safe)."""+ from gateway.run import safe_schedule_threadsafe+ loop = getattr(self, "_gateway_loop", None)+ if not getattr(self, "_running", False) or loop is None or loop.is_closed():+ return False++ coro = self._dispatch_plugin_message_injection(+ session_key=session_key, content=content, plugin_id=plugin_id,+ )+ try:+ current_loop = asyncio.get_running_loop()+ except RuntimeError:+ current_loop = None++ if current_loop is loop:+ try:+ future = loop.create_task(coro)+ except Exception:+ coro.close()+ logger.warning("Plugin message injection scheduling failed", exc_info=True)+ return False+ self._background_tasks.add(future)+ future.add_done_callback(self._background_tasks.discard)+ else:+ future = safe_schedule_threadsafe(+ coro, loop, logger=logger, log_message="Plugin message injection scheduling failed",+ log_level=logging.WARNING,+ )+ if future is None:+ return False++ def _log_result(completed) -> None:+ try:+ if completed.result():+ return+ what, exc = "was not routed", None+ except (asyncio.CancelledError, concurrent.futures.CancelledError):+ return+ except Exception as err:+ what, exc = "failed", err+ logger.warning(+ "Plugin message injection %s: plugin=%s session=%s", what, plugin_id, session_key, exc_info=exc,+ )++ future.add_done_callback(_log_result)+ return True++ async def _dispatch_plugin_message_injection(+ self, *, session_key: str, content: str, plugin_id: str+ ) -> bool:+ """Route a plugin-triggered turn through the session's live adapter."""+ def _accepting() -> bool:+ return getattr(self, "_running", False) and not getattr(self, "_draining", False)++ if not _accepting():+ return False+ entry = await self.async_session_store.lookup_by_session_key(session_key)+ if entry is None or entry.origin is None or not _accepting():+ return False++ from gateway.session_identity import replace_source+ source = replace_source(self._restored_source(entry))+ try:+ authorized = self._is_user_authorized_for_source(source, allow_adapter_delegation=False)+ except Exception:+ logger.warning(+ "Plugin message injection authorization check failed: plugin=%s session=%s",+ plugin_id, session_key, exc_info=True,+ )+ return False+ if not authorized:+ logger.warning(+ "Plugin message injection denied by current gateway authorization: "+ "plugin=%s session=%s", plugin_id, session_key,+ )+ return False++ adapter = self._delivery_adapter_for(source)+ if adapter is None:+ return False++ await adapter.handle_message(MessageEvent(+ text=content, message_type=MessageType.TEXT, source=source, internal=True,+ allow_gateway_control=False,+ metadata={+ "hermes_plugin_id": plugin_id, "hermes_plugin_injection": True,+ "gateway_session_key": session_key, "gateway_session_id": entry.session_id,+ "gateway_session_strict": True,+ },+ ))+ logger.info(+ "Plugin message injection dispatched: plugin=%s session=%s session_id=%s",+ plugin_id, session_key, entry.session_id,+ )+ return True