plugin_session.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """The session a plugin hook or tool is running for, read from the turn's task-local context.
  2. Plugin hooks and tools run in the gateway process, whose own cwd is the install root and is shared
  3. by every session (it is never ``os.chdir``-ed). These read the calling session's key, client surface
  4. and recorded working directory instead.
  5. """
  6. from __future__ import annotations
  7. import os
  8. def _active_policy():
  9. from gateway.session_policy import active_policy
  10. return active_policy()
  11. def session_key() -> str:
  12. """Route key of the session whose turn is running in this context, ``""`` outside one."""
  13. from gateway.session_context import get_session_env
  14. return get_session_env("HERMES_SESSION_KEY", "")
  15. def session_surface() -> str:
  16. """``cli``/``tui``/``desktop``/``acp`` for a local session's turn, the messaging platform
  17. (``telegram``, ``discord``, ...) for a chat's turn, ``""`` outside any session."""
  18. policy = _active_policy()
  19. if policy is not None:
  20. return policy.platform
  21. from gateway.session_context import get_session_env
  22. return get_session_env("HERMES_SESSION_PLATFORM", "")
  23. def session_cwd() -> str:
  24. """The calling session's recorded working directory (a local session's workspace, else the
  25. profile's terminal cwd), ``""`` when it has none. Never the gateway process's own cwd.
  26. In ingress hooks (``pre_gateway_dispatch``), which run before any session, it is the profile's
  27. terminal cwd, not the workspace of the session the message will reach."""
  28. from agent.runtime_cwd import scope_terminal_cwd, scoped_session_cwd
  29. policy = _active_policy()
  30. return scoped_session_cwd() or (policy.cwd if policy is not None else "") or scope_terminal_cwd().strip()
  31. def context_surface(manager) -> str:
  32. """``session_surface()`` for a plugin context: the classic CLI process itself is ``cli``."""
  33. return "cli" if manager._cli_ref is not None else session_surface()
  34. def context_cwd(manager) -> str:
  35. """``session_cwd()`` for a plugin context: the classic CLI process keeps its launch directory."""
  36. return os.getcwd() if manager._cli_ref is not None else session_cwd()