catalog.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """copilot-acp picker catalog: the signed-in CLI session's models (memoized), else the HTTP
  2. ``copilot`` provider's catalog (same account id space)."""
  3. from __future__ import annotations
  4. import logging
  5. import time
  6. from typing import Optional
  7. logger = logging.getLogger(__name__)
  8. _SESSION_MEMO_TTL = 300.0 # 5 min; SWR disk cache handles the rest
  9. _SESSION_FAIL_TTL = 30.0 # failed probes re-probe quickly so a fresh CLI login is picked up
  10. _session_memo: Optional[tuple[float, float, Optional[list[str]]]] = None # (at, ttl, models)
  11. def session_models(force_refresh: bool) -> Optional[list[str]]:
  12. """Enabled models from a signed-in ``copilot --acp`` session, memoized for a few minutes —
  13. successes AND failures. Model-switch validation (``models_validate._static_catalog``) reads
  14. this uncached on every ``/model`` switch, and each miss is a CLI spawn + handshake (up to the
  15. probe timeout), so without the memo every switch paid a subprocess. A failed probe is
  16. memoized much more briefly so a user who signs in to the CLI right after a miss is picked up
  17. on the next switch (or immediately via ``/model --refresh``, which clears this memo)."""
  18. global _session_memo
  19. now = time.monotonic()
  20. memo = _session_memo
  21. if not force_refresh and memo is not None and now - memo[0] < memo[1]:
  22. return memo[2]
  23. from providers import get_provider_profile
  24. try:
  25. live = get_provider_profile("copilot-acp").fetch_models() or None
  26. except Exception:
  27. logger.debug("copilot-acp session model discovery failed", exc_info=True)
  28. live = None
  29. _session_memo = (now, _SESSION_MEMO_TTL if live else _SESSION_FAIL_TTL, live)
  30. return live
  31. def clear_session_memo() -> None:
  32. """A fresh CLI login must show on the next ``/model`` switch: ``--refresh`` drops the memo
  33. (and its failure memo) so it never serves a stale signed-out probe."""
  34. global _session_memo
  35. _session_memo = None
  36. def acp_picker_catalog(force_refresh: bool = False) -> Optional[list[str]]:
  37. if live := session_models(force_refresh):
  38. return live
  39. from providers import get_provider_profile
  40. copilot = get_provider_profile("copilot")
  41. if copilot is None or copilot.picker_catalog is None:
  42. return None
  43. return copilot.picker_catalog(force_refresh)