profile_model_seed.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. """Copy the model and its selected custom endpoint into a new profile, and merge that endpoint
  2. over same-identity entries the new profile already has."""
  3. from hermes_cli.config import _expand_env_vars, find_provider_entry
  4. from hermes_cli.config_providers import _normalize_custom_provider_entry
  5. from hermes_cli.providers import custom_provider_aliases
  6. from hermes_cli.runtime_provider_custom import (
  7. _get_named_custom_provider,
  8. _normalize_custom_provider_name,
  9. _shadowed_by_builtin,
  10. )
  11. def _requested_provider(model_cfg) -> str | None:
  12. provider = model_cfg.get("provider") if isinstance(model_cfg, dict) else None
  13. # Runtime expands ``model.provider: ${VAR}`` before resolving; select with the same value.
  14. return str(_expand_env_vars(provider)) if isinstance(provider, str) else None
  15. # Fields name lookups match on. Runtime sees them ``${VAR}``-expanded (load_config expands the
  16. # whole file), so matching expands only these; copied entries stay raw and secrets stay refs.
  17. _ROUTING_FIELDS = ("name", "provider_key", "base_url", "baseUrl", "url", "api", "enabled")
  18. def _expand_routing_fields(entry):
  19. if not isinstance(entry, dict):
  20. return entry
  21. return {k: _expand_env_vars(v) if k in _ROUTING_FIELDS else v for k, v in entry.items()}
  22. def _routing_view(cfg: dict) -> dict:
  23. """*cfg* with endpoint routing fields expanded; same keys and list order, so a key or index
  24. found here addresses the raw entry in *cfg*."""
  25. view = dict(cfg)
  26. providers = cfg.get("providers")
  27. if isinstance(providers, dict):
  28. view["providers"] = {k: _expand_routing_fields(v) for k, v in providers.items()}
  29. custom = cfg.get("custom_providers")
  30. if isinstance(custom, list):
  31. view["custom_providers"] = [_expand_routing_fields(e) for e in custom]
  32. return view
  33. def _resolve(provider: str, view: dict):
  34. """Runtime named-endpoint lookup on a routing *view* without reading any credential."""
  35. return _get_named_custom_provider(provider, config=view, resolve_credentials=False)
  36. def _source_of(cfg: dict, resolved: dict):
  37. """Where in *cfg* the resolver found *resolved*: ``("providers", key)`` or
  38. ``("custom_providers", index)``. Keyed results carry ``provider_key``; legacy results do not
  39. (their normalizer runs without one), so they are located by normalized name and URL."""
  40. key, entry = find_provider_entry(cfg.get("providers"), resolved.get("provider_key"))
  41. if entry is not None:
  42. return "providers", key
  43. custom = cfg.get("custom_providers")
  44. for index, entry in enumerate(custom if isinstance(custom, list) else ()):
  45. normalized = _normalize_custom_provider_entry(entry)
  46. if normalized and (normalized["name"], normalized["base_url"]) == (
  47. resolved["name"],
  48. resolved["base_url"],
  49. ):
  50. return "custom_providers", index
  51. return None
  52. def launch_model_seed(source_cfg: dict) -> dict:
  53. """The config a fresh profile needs to run the launch profile's model: its ``model`` block plus
  54. the endpoint definition it points at. ``model.provider: custom:lab`` alone is "Unknown provider"
  55. on the first turn. The selected ``providers:`` or ``custom_providers:`` entry is copied as
  56. written (``${VAR}`` refs stay refs), and an exact ``providers.<built-in>`` / ``providers.custom``
  57. key travels for its tuning even without a URL. ``{}`` when the launch profile has no model."""
  58. model_cfg = source_cfg.get("model")
  59. if not model_cfg:
  60. return {}
  61. seed = {"model": model_cfg}
  62. provider = _requested_provider(model_cfg)
  63. if provider is None:
  64. return seed
  65. requested = _normalize_custom_provider_name(provider)
  66. providers = source_cfg.get("providers")
  67. providers = providers if isinstance(providers, dict) else {}
  68. # Resolve and locate on ONE view so the found key/index addresses the raw entry.
  69. view = _routing_view(source_cfg)
  70. selected = _resolve(provider, view)
  71. if selected is None:
  72. exact = providers.get(provider)
  73. # Bare ``custom`` runs as provider ``custom`` and reads ``providers.custom`` tuning too.
  74. if (
  75. isinstance(exact, dict)
  76. and requested not in {"", "auto"}
  77. and (requested == "custom" or _shadowed_by_builtin(requested))
  78. ):
  79. seed["providers"] = {provider: exact}
  80. return seed
  81. found = _source_of(view, selected)
  82. if found is not None:
  83. section, where = found
  84. seed[section] = (
  85. {where: providers[where]}
  86. if section == "providers"
  87. else [source_cfg["custom_providers"][where]]
  88. )
  89. return seed
  90. def _aliases(entry, key: str = "") -> frozenset[str]:
  91. """Every identity name-based lookups (runtime, ``/model`` pinning, pickers) accept for *entry*."""
  92. if not isinstance(entry, dict):
  93. return frozenset()
  94. entry = _expand_routing_fields(entry)
  95. key = key or str(entry.get("provider_key") or "")
  96. return custom_provider_aliases(str(entry.get("name") or key), key)
  97. def merge_endpoint_seed(dst_cfg: dict, seed: dict) -> dict:
  98. """Return a copy of *dst_cfg* with the seeded endpoint written first; a same-key destination
  99. entry is replaced whole. When the seed is an endpoint (it resolves), destination entries that
  100. claim any of its identities are dropped in both sections: Hermes has several name lookups with
  101. different skip rules (URL validation, ``enabled``, keyed-first), so only removing every
  102. same-identity entry guarantees each of them lands on the inherited endpoint. A URL-less tuning
  103. seed (``providers.custom``) claims no identity and replaces only its own key. Dropped entries
  104. include URL-less or disabled ones sharing a name (e.g. ``providers.openrouter`` tuning under a
  105. custom endpoint named OpenRouter); this only runs for a new profile without a model."""
  106. seed_providers = seed.get("providers") or {}
  107. seed_custom = seed.get("custom_providers") or []
  108. provider = _requested_provider(seed.get("model"))
  109. claimed: frozenset[str] = frozenset()
  110. if provider and _resolve(provider, _routing_view(seed)) is not None:
  111. claimed = frozenset().union(
  112. *(_aliases(e, str(k)) for k, e in seed_providers.items()),
  113. *(_aliases(e) for e in seed_custom),
  114. )
  115. merged = dict(dst_cfg)
  116. dst_providers = dst_cfg.get("providers")
  117. if isinstance(dst_providers, dict) or seed_providers:
  118. kept = {
  119. k: v for k, v in (dst_providers if isinstance(dst_providers, dict) else {}).items()
  120. if k not in seed_providers and not claimed & _aliases(v, str(k))
  121. }
  122. merged["providers"] = {**seed_providers, **kept}
  123. dst_custom = dst_cfg.get("custom_providers")
  124. if isinstance(dst_custom, list) or seed_custom:
  125. merged["custom_providers"] = seed_custom + [
  126. e for e in (dst_custom if isinstance(dst_custom, list) else [])
  127. if not claimed & _aliases(e)
  128. ]
  129. return merged