session_route_identity.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """One resolved-route identity for launch-route gates.
  2. Two launch routes are the same route when the runtime resolver would send them to the same
  3. endpoint: compare what it resolves (canonical provider, configured named entry, effective
  4. URL), never raw spellings. ``ollama``/``vllm`` resolve to ``custom``, ``deep-infra`` to
  5. ``deepinfra``, and a configured ``providers:``/``custom_providers`` name (or its
  6. ``custom:<name>`` spelling) to that entry.
  7. """
  8. # Every ``model:`` key that belongs to one endpoint; a route change drops them all, so none of the
  9. # replaced endpoint's values is paired with the new one:
  10. # - route itself (provider, base_url): which endpoint the turn goes to;
  11. # - wire protocol (api_mode): how that endpoint speaks; another host may speak another protocol;
  12. # - window (context_length): the replaced model's context size, wrong for the new one;
  13. # - credentials (api_key, api, api_key_from, key_env, api_key_env): a key authenticates only the
  14. # endpoint that issued it, and sending it anywhere else leaks it.
  15. ROUTE_BOUND_KEYS = ('provider', 'base_url', 'api_mode', 'context_length',
  16. 'api_key', 'api', 'api_key_from', 'key_env', 'api_key_env')
  17. def _named_entry(name: str, config: dict) -> dict | None:
  18. from hermes_cli.runtime_provider_custom import _get_named_custom_provider
  19. return _get_named_custom_provider(name, config=config, resolve_credentials=False)
  20. def _canonical(name: str) -> str:
  21. from hermes_cli.models import normalize_provider
  22. canonical = normalize_provider(name)
  23. from hermes_cli.auth import AuthError, resolve_provider
  24. try:
  25. return resolve_provider(canonical)
  26. except AuthError: # not a registry provider: its canonical spelling is its identity
  27. return canonical
  28. def configured_entry_key(identity: str, config: dict) -> str:
  29. """The bare config key of the configured entry behind a ``custom:<key>`` identity, else ``''``.
  30. Raw provider resolution defers a bare name that is a canonical built-in to the built-in, so a
  31. ``providers:`` entry keyed ``deepseek`` is reachable only through its ``custom:`` spelling. Only
  32. that key names a keyed entry: its display name may be a real built-in's slug (``name: OpenRouter``
  33. on ``providers.work``), and a pick of that built-in is not this endpoint (#140107). A legacy
  34. ``custom_providers:`` entry has no key, so its normalized display name is its identity and its
  35. key here (``custom_provider_slug``): ``name: OpenRouter`` there answers ``openrouter``, as
  36. Desktop's ``customDefaultSupersedesPick`` does.
  37. """
  38. identity = str(identity or '').strip().lower()
  39. if not identity.startswith('custom:') or not _named_entry(identity, config):
  40. return ''
  41. return identity.split(':', 1)[1].strip()
  42. def profile_route_identity(config: dict) -> str:
  43. """The identity the profile's own ``model`` block resolves to — ``custom`` for an endpoint with
  44. no provider named (or ``auto``), which already routes to ``custom`` at that URL."""
  45. held = dict(config.get('model') or {}) if isinstance(config.get('model'), dict) else {}
  46. if str(held.get('provider') or '').strip().lower() in ('', 'auto') and str(held.get('base_url') or '').strip():
  47. held['provider'] = 'custom'
  48. return resolved_route(held, config)[0]
  49. def resolved_route(model_cfg: dict, config: dict) -> tuple[str, str]:
  50. """``(provider_identity, effective_base_url)`` the runtime resolves ``model_cfg`` to."""
  51. model_cfg = model_cfg if isinstance(model_cfg, dict) else {}
  52. config = config if isinstance(config, dict) else {}
  53. name = str(model_cfg.get('provider') or '').strip().lower()
  54. if name in ('', 'auto'):
  55. return 'auto', ''
  56. base_url = str(model_cfg.get('base_url') or '').strip()
  57. entry = _named_entry(name, config)
  58. if entry:
  59. from hermes_cli.providers import custom_provider_slug
  60. identity = custom_provider_slug(str(entry.get('name') or ''), str(entry.get('provider_key') or ''))
  61. base_url = base_url or str(entry.get('base_url') or '').strip()
  62. else:
  63. identity = _canonical(name)
  64. return identity, base_url.rstrip('/')
  65. def same_route(a_cfg: dict, b_cfg: dict, config: dict) -> bool:
  66. """True when both configs resolve to one endpoint; ``auto`` on either side keeps the route."""
  67. a_id, a_url = resolved_route(a_cfg, config)
  68. b_id, b_url = resolved_route(b_cfg, config)
  69. if 'auto' in (a_id, b_id):
  70. return True
  71. if a_url or b_url:
  72. from hermes_cli.route_identity import normalize_route_base_url
  73. return normalize_route_base_url(a_url) == normalize_route_base_url(b_url)
  74. return a_id == b_id