Repository navigation
feat: let plugin slash commands opt into gateway context #56782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DoctaWasabi
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
DoctaWasabi:hermes/g2-a2-plugin-command-dispatch-20260701
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -530,10 +530,15 @@ def register_command( | |
| handler: Callable, | ||
| description: str = "", | ||
| args_hint: str = "", | ||
| *, | ||
| active_session_bypass: bool = False, | ||
| wants_context: bool = False, | ||
| ) -> None: | ||
| """Register a slash command (e.g. ``/lcm``) available in CLI and gateway sessions. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This public registration option also needs to be accepted and stored by the context-engine collector ( |
||
|
|
||
| The handler signature is ``fn(raw_args: str) -> str | None``. | ||
| The default handler signature is ``fn(raw_args: str) -> str | None``. | ||
| Commands that set ``wants_context=True`` are called as | ||
| ``fn(raw_args: str, **gateway_context)`` by gateway dispatchers. | ||
| It may also be an async callable β the gateway dispatch handles both. | ||
|
|
||
| Unlike ``register_cli_command()`` (which creates ``hermes <subcommand>`` | ||
|
|
@@ -575,6 +580,8 @@ def register_command( | |
| "description": description or "Plugin command", | ||
| "plugin": self.manifest.name, | ||
| "args_hint": (args_hint or "").strip(), | ||
| "active_session_bypass": bool(active_session_bypass), | ||
| "wants_context": bool(wants_context), | ||
| } | ||
| logger.debug("Plugin %s registered command: /%s", self.manifest.name, clean) | ||
|
|
||
|
|
@@ -2162,12 +2169,41 @@ def get_plugin_context_engine(): | |
| return _ensure_plugins_discovered()._context_engine | ||
|
|
||
|
|
||
| def get_plugin_command_entry(name: str) -> Optional[dict]: | ||
| """Return metadata for a plugin-registered slash command, if present.""" | ||
| entry = _ensure_plugins_discovered()._plugin_commands.get(name) | ||
| return dict(entry) if entry else None | ||
|
|
||
|
|
||
| def get_plugin_command_handler(name: str) -> Optional[Callable]: | ||
| """Return the handler for a plugin-registered slash command, or ``None``.""" | ||
| entry = _ensure_plugins_discovered()._plugin_commands.get(name) | ||
| entry = get_plugin_command_entry(name) | ||
| return entry["handler"] if entry else None | ||
|
|
||
|
|
||
| def plugin_command_allows_active_session_bypass(name: str) -> bool: | ||
| """Return True when a plugin command may run while a session is busy.""" | ||
| entry = get_plugin_command_entry(name) | ||
| return bool(entry and entry.get("active_session_bypass")) | ||
|
|
||
|
|
||
| def invoke_plugin_command(name: str, raw_args: str, **gateway_context: Any) -> Any: | ||
| """Invoke a plugin slash command with optional gateway context. | ||
|
|
||
| Legacy commands receive only ``raw_args``. Commands registered with | ||
| ``wants_context=True`` receive ``raw_args`` plus the provided keyword | ||
| context, allowing gateway plugins to inspect the triggering event without | ||
| changing the default one-argument command contract. | ||
| """ | ||
| entry = get_plugin_command_entry(name) | ||
| if not entry: | ||
| return None | ||
| handler = entry["handler"] | ||
| if entry.get("wants_context"): | ||
| return handler(raw_args, **gateway_context) | ||
| return handler(raw_args) | ||
|
|
||
|
|
||
| _PLUGIN_COMMAND_AWAIT_TIMEOUT_SECS = 30.0 | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import pytest | ||
|
|
||
| from gateway.config import Platform | ||
| from gateway.platforms.base import MessageEvent, MessageType | ||
| from gateway.run import GatewayRunner | ||
| from gateway.session import SessionSource | ||
| from hermes_cli.plugins import PluginContext, PluginManager, PluginManifest | ||
|
|
||
|
|
||
| def _event(text="/context-cmd now"): | ||
| source = SessionSource( | ||
| platform=Platform.TELEGRAM, | ||
| chat_id="chat-1", | ||
| user_id="user-1", | ||
| user_name="User", | ||
| chat_type="dm", | ||
| ) | ||
| return MessageEvent(text=text, message_type=MessageType.TEXT, source=source) | ||
|
|
||
|
|
||
| def _register_context_command(monkeypatch, *, active_session_bypass=False): | ||
| manager = PluginManager() | ||
| ctx = PluginContext(PluginManifest(name="example", key="example"), manager) | ||
| seen = {} | ||
|
|
||
| def handler(raw_args, *, event=None, gateway=None, source=None, session_key=None): | ||
| seen.update( | ||
| raw_args=raw_args, | ||
| event=event, | ||
| gateway=gateway, | ||
| source=source, | ||
| session_key=session_key, | ||
| ) | ||
| return "plugin-ok" | ||
|
|
||
| ctx.register_command( | ||
| "context-cmd", | ||
| handler, | ||
| wants_context=True, | ||
| active_session_bypass=active_session_bypass, | ||
| ) | ||
| monkeypatch.setattr("hermes_cli.plugins._ensure_plugins_discovered", lambda force=False: manager) | ||
| return seen | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_gateway_dispatches_plugin_command_with_context(monkeypatch): | ||
| seen = _register_context_command(monkeypatch) | ||
| runner = object.__new__(GatewayRunner) | ||
| event = _event() | ||
|
|
||
| result = await runner._dispatch_plugin_command("context-cmd", event, "session-1") | ||
|
|
||
| assert result == "plugin-ok" | ||
| assert seen["raw_args"] == "now" | ||
| assert seen["event"] is event | ||
| assert seen["gateway"] is runner | ||
| assert seen["source"] is event.source | ||
| assert seen["session_key"] == "session-1" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_busy_path_plugin_command_bypasses_active_session_when_registered(monkeypatch): | ||
| _register_context_command(monkeypatch, active_session_bypass=True) | ||
| runner = object.__new__(GatewayRunner) | ||
| event = _event() | ||
|
|
||
| result = await runner._dispatch_active_session_plugin_command("context-cmd", event, "session-1") | ||
|
|
||
| assert result == "plugin-ok" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_busy_path_plugin_command_without_bypass_falls_through(monkeypatch): | ||
| _register_context_command(monkeypatch, active_session_bypass=False) | ||
| runner = object.__new__(GatewayRunner) | ||
| event = _event() | ||
|
|
||
| result = await runner._dispatch_active_session_plugin_command("context-cmd", event, "session-1") | ||
|
|
||
| assert result is None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| from hermes_cli.plugins import PluginContext, PluginManager, PluginManifest | ||
|
|
||
|
|
||
| def _context(): | ||
| manager = PluginManager() | ||
| manifest = PluginManifest(name="example", key="example") | ||
| return PluginContext(manifest, manager), manager | ||
|
|
||
|
|
||
| def test_plugin_command_registration_records_active_session_bypass(): | ||
| ctx, manager = _context() | ||
|
|
||
| def handler(raw_args): | ||
| return f"handled {raw_args}" | ||
|
|
||
| ctx.register_command("queue-status", handler, active_session_bypass=True) | ||
|
|
||
| assert manager._plugin_commands["queue-status"]["active_session_bypass"] is True | ||
|
|
||
|
|
||
| def test_plugin_command_registration_defaults_to_no_active_session_bypass(): | ||
| ctx, manager = _context() | ||
|
|
||
| def handler(raw_args): | ||
| return f"handled {raw_args}" | ||
|
|
||
| ctx.register_command("ordinary", handler) | ||
|
|
||
| assert manager._plugin_commands["ordinary"]["active_session_bypass"] is False | ||
|
|
||
|
|
||
| def test_plugin_command_can_request_gateway_context(monkeypatch): | ||
| ctx, manager = _context() | ||
| monkeypatch.setattr("hermes_cli.plugins._ensure_plugins_discovered", lambda force=False: manager) | ||
| seen = {} | ||
|
|
||
| def handler(raw_args, *, event=None, gateway=None): | ||
| seen["raw_args"] = raw_args | ||
| seen["event"] = event | ||
| seen["gateway"] = gateway | ||
| return "ok" | ||
|
|
||
| ctx.register_command("context-cmd", handler, wants_context=True) | ||
|
|
||
| from hermes_cli.plugins import invoke_plugin_command | ||
|
|
||
| result = invoke_plugin_command("context-cmd", "now", event="event-object", gateway="gateway-object") | ||
|
|
||
| assert result == "ok" | ||
| assert seen == { | ||
| "raw_args": "now", | ||
| "event": "event-object", | ||
| "gateway": "gateway-object", | ||
| } | ||
|
|
||
|
|
||
| def test_plugin_command_without_context_keeps_legacy_one_arg_call(monkeypatch): | ||
| ctx, manager = _context() | ||
| monkeypatch.setattr("hermes_cli.plugins._ensure_plugins_discovered", lambda force=False: manager) | ||
| seen = [] | ||
|
|
||
| def handler(raw_args): | ||
| seen.append(raw_args) | ||
| return "ok" | ||
|
|
||
| ctx.register_command("legacy", handler) | ||
|
|
||
| from hermes_cli.plugins import invoke_plugin_command | ||
|
|
||
| result = invoke_plugin_command("legacy", "args", event="ignored") | ||
|
|
||
| assert result == "ok" | ||
| assert seen == ["args"] | ||
|
|
||
|
|
||
| def test_async_plugin_command_context_result_can_be_resolved(monkeypatch): | ||
| ctx, manager = _context() | ||
| monkeypatch.setattr("hermes_cli.plugins._ensure_plugins_discovered", lambda force=False: manager) | ||
|
|
||
| async def handler(raw_args, *, event=None): | ||
| return f"{raw_args}:{event}" | ||
|
|
||
| ctx.register_command("async-cmd", handler, wants_context=True) | ||
|
|
||
| from hermes_cli.plugins import invoke_plugin_command, resolve_plugin_command_result | ||
|
|
||
| result = invoke_plugin_command("async-cmd", "go", event="evt") | ||
|
|
||
| assert resolve_plugin_command_result(result) == "go:evt" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This dispatcher is not reached for normal busy-session plugin commands:
BasePlatformAdapter.handle_message()queues an active-session event unlessshould_bypass_active_session(cmd)succeeds (gateway/platforms/base.py:4675-4731), and that predicate only uses built-inresolve_command()(hermes_cli/commands.py:389-409). Please wire a shared metadata-aware predicate into the adapter guard and add an adapter-level integration test.