turn.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from __future__ import annotations
  2. from contextlib import contextmanager
  3. from contextvars import ContextVar
  4. from typing import Any, Iterator
  5. __all__ = [
  6. "CARD",
  7. "LINK",
  8. "SIDE",
  9. "SIDE_AGENT_TOOL_DROPS",
  10. "agent_connection_surface",
  11. "connection_surface",
  12. "scoped_connection_surface",
  13. "side_agent_tool_drops",
  14. ]
  15. CARD = "card"
  16. SIDE = "side"
  17. LINK = "link"
  18. _SURFACE: ContextVar[str] = ContextVar("hermes_connection_surface", default=LINK)
  19. def connection_surface() -> str:
  20. return _SURFACE.get()
  21. def agent_connection_surface(agent: Any) -> str:
  22. if getattr(agent, "connection_callback", None) is not None:
  23. return CARD
  24. return SIDE if getattr(agent, "side_agent", False) else LINK
  25. SIDE_AGENT_TOOL_DROPS = frozenset({"manage_connections"})
  26. def side_agent_tool_drops(agent: Any) -> frozenset:
  27. return SIDE_AGENT_TOOL_DROPS if getattr(agent, "side_agent", False) else frozenset()
  28. @contextmanager
  29. def scoped_connection_surface(surface: str) -> Iterator[None]:
  30. token = _SURFACE.set(surface)
  31. try:
  32. yield
  33. finally:
  34. _SURFACE.reset(token)