session_discovery.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. """Authenticated, profile-bound discovery without legacy execution dispatch."""
  2. import asyncio
  3. from pathlib import Path
  4. from hermes_state_runtime import RuntimeStoreError
  5. async def discover_commands(authority, actor, params, *, completion=False):
  6. if 'session:read' not in actor.capabilities or actor.profile_id != authority.profile_id:
  7. raise RuntimeStoreError('permission_denied')
  8. allowed = {'profile', 'text', 'session_id'} if completion else {'profile', 'session_id'}
  9. if (not isinstance(params, dict) or set(params) - allowed
  10. or any(not isinstance(value, str) for value in params.values())):
  11. raise RuntimeStoreError('invalid_params')
  12. scope = None
  13. if params.get('session_id'):
  14. # Project skills resolve from the session's workspace, not the gateway process cwd.
  15. from gateway.session_commands import _session_workspace
  16. from gateway.session_contract import SessionRef
  17. ref = SessionRef(authority.profile_id, params['session_id'])
  18. authority.authorize(actor, ref, 'session:read')
  19. scope = _session_workspace(authority.runner, authority.sessions[ref.session_id].source)
  20. def discover():
  21. from hermes_cli.profiles import profile_matches_home
  22. home = Path(authority.profile_id)
  23. if params.get('profile') and not profile_matches_home(params['profile'], home):
  24. raise RuntimeStoreError('profile_mismatch')
  25. from gateway.run import _profile_runtime_scope
  26. from tui_gateway.command_discovery import command_catalog, slash_completions
  27. with _profile_runtime_scope(home):
  28. return (slash_completions(params.get('text', ''), scope=scope) if completion
  29. else command_catalog(scope=scope))
  30. return await asyncio.to_thread(discover)
  31. async def list_plugins(authority, actor, params):
  32. """``plugins.list`` for this profile's own plugin manager: what the gateway loaded, with each
  33. plugin's tool/hook/command counts and load error (the classic CLI's ``/plugins`` reads it, so the
  34. CLI process imports no plugin code). Plain data only."""
  35. if 'session:read' not in actor.capabilities or actor.profile_id != authority.profile_id:
  36. raise RuntimeStoreError('permission_denied')
  37. if params:
  38. raise RuntimeStoreError('invalid_params')
  39. def rows():
  40. from gateway.run import _profile_runtime_scope
  41. from hermes_cli.plugins import discover_plugins, get_plugin_manager
  42. from hermes_cli.plugins import get_plugin_toolsets
  43. with _profile_runtime_scope(Path(authority.profile_id)):
  44. discover_plugins() # idempotent: the profile's plugins are normally loaded already
  45. manager = get_plugin_manager()
  46. loaded, toolsets = manager.list_plugins(), get_plugin_toolsets()
  47. portable = sorted(manager.get_portable_mcp_servers())
  48. return {'plugins': [{'name': str(p['name']), 'key': str(p['key']), 'version': str(p['version'] or ''),
  49. 'enabled': bool(p['enabled']), 'tools': int(p['tools']), 'hooks': int(p['hooks']),
  50. 'commands': int(p['commands']), 'error': str(p['error'] or '')} for p in loaded],
  51. # The CLI's /tools and its completion validate plugin toolsets against these.
  52. 'toolsets': [{'key': str(key), 'label': str(label), 'description': str(desc or '')}
  53. for key, label, desc in toolsets],
  54. 'portable_mcp_servers': [str(name) for name in portable]}
  55. return await asyncio.to_thread(rows)