auxiliary_managed_local.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """Which endpoint is the llama-server this Hermes manages (sibling of ``auxiliary_client``)."""
  2. from __future__ import annotations
  3. import json
  4. import time
  5. from typing import Optional
  6. from urllib.parse import urlparse
  7. _MANAGED_LOCAL_STATE_TTL_S = 15.0
  8. _managed_local_cache: tuple[float, str] = (0.0, "")
  9. def _managed_local_netloc() -> str:
  10. """host:port of the managed local llama-server ("" when none), read with a short TTL from
  11. the supervisor state file provider resolution also uses (exact match)."""
  12. global _managed_local_cache
  13. now = time.monotonic()
  14. ts, cached = _managed_local_cache
  15. if now - ts < _MANAGED_LOCAL_STATE_TTL_S:
  16. return cached
  17. try:
  18. from hermes_cli.local_runtime.supervisor import state_path
  19. raw = state_path().read_text(encoding="utf-8-sig")
  20. base = str((json.loads(raw) or {}).get("base_url", ""))
  21. netloc = urlparse(base).netloc.lower()
  22. except (OSError, ValueError, AttributeError): # no state file, bad JSON, or not an object
  23. netloc = ""
  24. _managed_local_cache = (now, netloc)
  25. return netloc
  26. def _is_managed_local_endpoint(base_url: Optional[str]) -> bool:
  27. """True when *base_url* targets the llama-server this Hermes manages."""
  28. if not base_url:
  29. return False
  30. managed = _managed_local_netloc()
  31. if not managed:
  32. return False
  33. try:
  34. return urlparse(str(base_url)).netloc.lower() == managed
  35. except ValueError:
  36. return False