config_provider_env.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """The provider-profile entries of ``OPTIONAL_ENV_VARS``: descriptive metadata that reads no config.
  2. They used to come from ``list_providers()`` while ``hermes_cli.config`` imported, and provider
  3. discovery reads config (``plugins.enabled``, ``plugins.isolation``): the launch profile's config in
  4. a process serving several profiles, and a deadlock with a config backend whose own load imports
  5. ``hermes_cli.auth``. Now the bundled providers' entries are built from :func:`providers.bundled_profiles`
  6. (no config, no discovery) on the table's first read, and discovery's completion hook adds the
  7. process-wide plugin providers it registered.
  8. """
  9. from __future__ import annotations
  10. _STATIC_NAMES: frozenset[str] = frozenset() # declared in core (config_defaults), known at import
  11. _PROVIDER_NAMES: set[str] = set() # added from provider profiles by the fill
  12. def arm(table, static_names) -> None:
  13. """Called once by ``hermes_cli.config`` at the end of its import."""
  14. global _STATIC_NAMES
  15. _STATIC_NAMES = frozenset(static_names)
  16. table.arm(_provider_entries, _PROVIDER_NAMES.update)
  17. _CATALOG: dict | None = None # the bundled providers' entries: config-free, computed once
  18. def _entries_for(profiles, skip) -> dict:
  19. """Entries for the ``auth_type="api_key"`` profiles' env vars, minus ``skip``; the first profile
  20. to declare a name wins."""
  21. entries: dict = {}
  22. for pp in profiles:
  23. if pp.auth_type != "api_key":
  24. continue
  25. for var in pp.env_vars:
  26. if var in skip or var in entries:
  27. continue
  28. is_key = not var.endswith(("_BASE_URL", "_URL"))
  29. label = pp.display_name or pp.name
  30. entries[var] = {
  31. "description": f"{label} {'API key' if is_key else 'base URL override'}",
  32. "prompt": f"{label} {'API key' if is_key else 'base URL (leave empty for default)'}",
  33. "url": pp.signup_url or None,
  34. "password": is_key,
  35. "category": "provider",
  36. "advanced": True}
  37. return entries
  38. def _provider_entries() -> dict:
  39. """The bundled provider profiles' entries. Reads no config and runs no discovery (so it has no
  40. partial state and nothing to wait for): a plain idempotent cache, and concurrent first callers
  41. just compute the same result. A core entry wins; a provider entry replaces one a platform plugin
  42. manifest added (it was added first only because this runs late)."""
  43. global _CATALOG
  44. if _CATALOG is None:
  45. try:
  46. from providers import bundled_profiles
  47. profiles = bundled_profiles()
  48. except Exception: # health: allow BLE001 -- as before: a broken provider layer must not break config readers
  49. profiles = []
  50. _CATALOG = _entries_for(profiles, _STATIC_NAMES)
  51. return dict(_CATALOG)
  52. def sync_registered_providers() -> None:
  53. """Provider discovery's completion hook: add the env vars of process-wide plugin providers it
  54. registered (entry points enabled in config). Reads the process-wide registry and its source
  55. record as they stand: never config, never discovery. Not ``provider_source()``: that one is
  56. profile-aware and scans the bound home's plugin dirs, whose isolation check reads config.
  57. ``$HERMES_HOME`` plugins (per profile) are not process-wide and are left out."""
  58. import providers
  59. from hermes_cli.config_defaults import OPTIONAL_ENV_VARS
  60. sources = dict(providers._SOURCES)
  61. profiles = [p for name, p in list(providers._REGISTRY.items()) if sources.get(name) != "bundled"]
  62. entries = _entries_for(profiles, _STATIC_NAMES | _PROVIDER_NAMES | set(_CATALOG or ()))
  63. for var, entry in entries.items():
  64. dict.__setitem__(OPTIONAL_ENV_VARS, var, entry)
  65. _PROVIDER_NAMES.update(entries)
  66. def core_declared_env_names() -> frozenset[str]:
  67. """Names declared in core: config_defaults' plus the provider profiles'. A platform manifest
  68. never reclassifies one."""
  69. from hermes_cli.config_defaults import OPTIONAL_ENV_VARS
  70. OPTIONAL_ENV_VARS.get("") # runs the fill once
  71. return _STATIC_NAMES | frozenset(_PROVIDER_NAMES)