runtime_readiness.py 5.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. """Provider readiness probe for the session authority's ``setup.runtime_check``.
  2. Answers with the legacy ``tui_gateway/methods_config.py`` handler's payload so a client reads one
  3. contract whichever backend it reached: ``ok``/``provider``/``model``/``source``, ``error`` on
  4. failure (naming the configured pin, never the fallback chain's tail), ``free_tier_route`` on success.
  5. """
  6. import logging
  7. logger = logging.getLogger(__name__)
  8. def missing_credentials_suffix(provider) -> str:
  9. """``" <hint>"`` from *provider*'s profile (``ProviderProfile.missing_credentials_hint``) for a
  10. "No usable credentials found" message, else ``""``. Plugin boundary: a raising hook (or a plugin
  11. host that is down) or a non-string answer yields ``""`` and is logged once per provider, so the
  12. AuthError it decorates (and the fallback chain walking on it) is never replaced."""
  13. from providers import get_provider_profile
  14. name = str(provider or "").strip().lower()
  15. profile = get_provider_profile(name) if name else None
  16. if profile is None:
  17. return ""
  18. try:
  19. hint = profile.missing_credentials_hint()
  20. except Exception: # health: allow BLE001 -- plugin hook boundary; logged with traceback once below
  21. if name not in _HINT_FAILURES_LOGGED:
  22. _HINT_FAILURES_LOGGED.add(name)
  23. logger.warning("%s missing_credentials_hint failed; showing the error without it", name, exc_info=True)
  24. return ""
  25. return f" {hint.strip()}" if isinstance(hint, str) and hint.strip() else ""
  26. _HINT_FAILURES_LOGGED: set[str] = set()
  27. def readiness_failure(exc) -> dict:
  28. """``setup.runtime_check``'s answer when resolving the runtime raised *exc*.
  29. ``error_code`` rides along only for a failure provider setup cannot repair, so a client keeps
  30. the app open with the error instead of sending the user back through provider sign-in.
  31. """
  32. from hermes_cli.runtime_provider_custom import LocalRuntimeUnavailableError
  33. code = {'error_code': exc.error_code} if isinstance(exc, LocalRuntimeUnavailableError) else {}
  34. return {'ok': False, 'error': str(exc), **code}
  35. def check_runtime_readiness(requested=None, *, strict_profile_scope=False, resolve=None):
  36. """ok/error verdict for the runtime a session would be built with.
  37. Without ``requested`` the probe runs the SAME resolver as session creation (the configured
  38. model + provider pin, then the configured fallback chain) — a probe that ignores the chain
  39. shows onboarding for a backend whose sessions build fine (#111775). ``resolve`` replaces that
  40. resolver with the caller's own ``() -> (model, runtime)``. An explicit ``requested`` provider
  41. stays a strict single-provider check so onboarding can verify the provider just connected
  42. without another provider's fallback masking a failed connection. Both branches report the model.
  43. """
  44. from hermes_cli.auth import has_usable_secret
  45. from hermes_cli.config import load_config
  46. from hermes_cli.main import _has_any_provider_configured
  47. from hermes_cli.runtime_provider import resolve_runtime_provider, resolve_runtime_with_fallback
  48. config = load_config()
  49. cfg_model = config.get('model')
  50. if isinstance(cfg_model, dict):
  51. configured_model = str(cfg_model.get('default') or cfg_model.get('model') or '') or None
  52. else:
  53. configured_model = cfg_model if isinstance(cfg_model, str) and cfg_model else None
  54. if resolve is not None:
  55. model, runtime = resolve()
  56. elif requested:
  57. model = configured_model
  58. runtime = resolve_runtime_provider(requested=requested, target_model=configured_model)
  59. else:
  60. runtime, entry = resolve_runtime_with_fallback(config, target_model=configured_model)
  61. model = (entry or {}).get('model') or configured_model or runtime.get('model')
  62. configured = bool(_has_any_provider_configured(strict_profile_scope=strict_profile_scope))
  63. provider = runtime.get('provider') or 'provider'
  64. source = str(runtime.get('source') or '')
  65. # When the chain only resolves at its tail the runtime stops there; a failure must name the
  66. # provider the user pinned, not a tail they never chose (#124939). Auto mode names the route.
  67. pinned = requested or (str(cfg_model.get('provider') or '').strip() if isinstance(cfg_model, dict) else '')
  68. blamed = pinned or provider
  69. def fail(error, src):
  70. return {'ok': False, 'provider': blamed, 'model': model, 'source': src, 'error': error}
  71. if not configured and provider == 'bedrock' and source in {'iam-role', 'aws-sdk-default-chain'}:
  72. return fail('No Hermes provider is configured.', source)
  73. api_key = runtime.get('api_key')
  74. api_key_text = '' if callable(api_key) else str(api_key or '').strip()
  75. if not (callable(api_key) or api_key_text in {'aws-sdk', 'no-key-required'}
  76. or has_usable_secret(api_key_text) or bool(runtime.get('command'))):
  77. return fail(f'No usable credentials found for {blamed}.{missing_credentials_suffix(blamed)}', runtime.get('source'))
  78. from hermes_cli.anon_auth import route_is_welcome_host
  79. # free_tier_route is keyed on the SELECTED route (the welcome host serves only nous/welcome), not
  80. # on profile state: a paid Nous key beside a free-tier identity must not read as free.
  81. return {'ok': True, 'provider': runtime.get('provider'), 'model': model, 'source': runtime.get('source'),
  82. 'free_tier_route': provider == 'nous' and route_is_welcome_host(runtime.get('base_url'))}