models_detect.py 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """Live-catalog and credential guards for ``detect_provider_for_model``.
  2. Split out of ``hermes_cli.models``. The detection ladder there consults static catalogs, then the
  3. OpenRouter catalog, and its answer used to be applied blindly. Two guards close the class of
  4. "put the user on a provider they never selected":
  5. * the CURRENT provider's live catalog outranks every static guess (Codex early-access ids, Nous
  6. Portal slugs, Ollama Cloud models absent from ``_PROVIDER_MODELS`` — #97487, the $100 Astra
  7. incident);
  8. * an auto-detected TARGET must be a provider the user has credentials for. Guessing a vendor the
  9. user never signed into either 401s or, for OpenRouter (whose runtime resolves with an empty key
  10. instead of raising), silently bills a metered aggregator.
  11. """
  12. from __future__ import annotations
  13. from typing import Optional
  14. # Providers where the ladder's own OpenRouter step is the answer, or where there is no catalog to
  15. # consult; ``custom`` endpoints are never auto-switched away from (handled upstream).
  16. _SKIP = frozenset({"", "auto", "openrouter", "custom"})
  17. def current_provider_catalog_match(model_name: str, current_provider: str) -> Optional[str]:
  18. """Return the current provider's own spelling of *model_name* when its live (disk-cached)
  19. catalog serves it — exact id, or the bare part after ``vendor/`` — else ``None``.
  20. Goes through :func:`hermes_cli.models.cached_provider_model_ids` (1h TTL, stale-while-
  21. revalidate) so a model switch does not block on a cold ``/v1/models`` round-trip in the
  22. common case; a fetch failure yields an empty catalog and the ladder continues unchanged."""
  23. from hermes_cli.models import cached_provider_model_ids, normalize_provider
  24. provider = (current_provider or "").strip().lower()
  25. if provider in _SKIP or provider.startswith("custom:") or normalize_provider(provider) in _SKIP:
  26. return None
  27. wanted = (model_name or "").strip().lower()
  28. if not wanted:
  29. return None
  30. try:
  31. catalog = cached_provider_model_ids(provider)
  32. except Exception:
  33. return None
  34. return next((mid for mid in catalog if mid.lower() == wanted), None) or next(
  35. (mid for mid in catalog if "/" in mid and mid.split("/", 1)[1].lower() == wanted), None)
  36. def current_provider_owns_vendor(model_name: str, current_provider: str) -> bool:
  37. """True when *model_name* belongs to the vendor a single-vendor first-party provider natively
  38. serves (``gpt-6-astra`` on ``openai-codex``, ``grok-4.6`` on ``xai-oauth``).
  39. A first-party session plus that vendor's own id is a selection, not a guess: when the live
  40. catalog could not confirm the id (fetch failed, static fallback lags an early-access rollout)
  41. the answer is "stay and let the vendor accept or reject it", never "a reseller lists it, so
  42. switch there". Aggregators, custom endpoints and multi-vendor resellers (nvidia, alibaba, ...)
  43. have no single native vendor and are skipped."""
  44. from hermes_cli.model_normalize import detect_vendor
  45. from hermes_cli.models import _AGGREGATOR_PROVIDERS, _PROVIDER_MODELS, normalize_provider
  46. provider = (current_provider or "").strip().lower()
  47. if provider in _SKIP or provider.startswith("custom:"):
  48. return False
  49. normalized = normalize_provider(provider)
  50. if normalized in _SKIP or normalized in _AGGREGATOR_PROVIDERS:
  51. return False
  52. vendor = detect_vendor(model_name or "")
  53. if not vendor:
  54. return False
  55. # An id the classifier cannot place (Bedrock ``us.anthropic.claude-…``) is evidence the
  56. # provider is NOT single-vendor; only a fully classified, single-vendor catalog owns the name.
  57. native = {detect_vendor(mid) for mid in _PROVIDER_MODELS.get(normalized, ())}
  58. return native == {vendor}
  59. def provider_has_credentials(provider: str) -> bool:
  60. """Whether *provider* can be switched to without the user typing a key: env/.env key, auth
  61. store login, or a usable credential-pool entry. ``custom``/``custom:*`` targets only come out
  62. of the ladder when the user declared them in config, so they count as authenticated."""
  63. from hermes_cli.auth import get_auth_status, has_usable_secret
  64. from hermes_cli.config import get_env_value_prefer_dotenv
  65. pid = (provider or "").strip().lower()
  66. if not pid:
  67. return False
  68. if pid == "custom" or pid.startswith("custom:"):
  69. return True
  70. try:
  71. if pid == "openrouter" and has_usable_secret(get_env_value_prefer_dotenv("OPENROUTER_API_KEY")):
  72. return True
  73. status = get_auth_status(pid) or {}
  74. if status.get("logged_in") or status.get("configured"):
  75. return True
  76. from agent.credential_pool import load_pool
  77. pool = load_pool(pid)
  78. return bool(pool.has_credentials() and pool.has_available())
  79. except Exception:
  80. return False