From b58a88d6f452d264ab0ef14b4dd08cc4bd7c79d7 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 2 Sep 2026 23:06:19 -0700 Subject: [PATCH] =?UTF-8?q?refactor(hermes=5Fcli):=20runtime=5Fprovider=20?= =?UTF-8?q?=E2=80=94=20whole=20ladder=20as=20one=20lazy=20rung=20generator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hermes_cli/runtime_provider.py | 37 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/hermes_cli/runtime_provider.py b/hermes_cli/runtime_provider.py index ee7d1e51a1..d631cc0548 100644 --- a/hermes_cli/runtime_provider.py +++ b/hermes_cli/runtime_provider.py @@ -641,10 +641,10 @@ def _resolve_explicit_runtime(*, provider: str, requested_provider: str, model_c if resolver is not None: return resolver(requested_provider, model_cfg, explicit_api_key, explicit_base_url, target_model) pconfig = PROVIDER_REGISTRY.get(provider) - if pconfig and pconfig.auth_type == "api_key": - return _explicit_api_key_provider(provider, pconfig, requested_provider, model_cfg, explicit_api_key, - explicit_base_url, target_model) - return None + if not (pconfig and pconfig.auth_type == "api_key"): + return None + return _explicit_api_key_provider(provider, pconfig, requested_provider, model_cfg, explicit_api_key, explicit_base_url, + target_model) # ── OAuth / auth-store providers ─────────────────────────────────────────────────────────── @@ -670,8 +670,7 @@ _OAUTH_RUNTIME_PROVIDERS: Dict[str, _OAuthRuntimeSpec] = { "openai-codex": _OAuthRuntimeSpec(lambda: resolve_codex_runtime_credentials(), "codex_responses", "hermes-auth-store", "last_refresh", "Auto-detected Codex provider but credentials failed"), "xai-oauth": _OAuthRuntimeSpec(lambda: resolve_xai_oauth_runtime_credentials(), "codex_responses", "hermes-auth-store", - "last_refresh", "Auto-detected xAI OAuth provider but credentials failed", - default_base_url=DEFAULT_XAI_OAUTH_BASE_URL), + "last_refresh", "Auto-detected xAI OAuth provider but credentials failed", DEFAULT_XAI_OAUTH_BASE_URL), "qwen-oauth": _OAuthRuntimeSpec(lambda: resolve_qwen_runtime_credentials(), "chat_completions", "qwen-cli", "expires_at_ms", "Qwen OAuth credentials failed"), } @@ -870,25 +869,25 @@ def resolve_runtime_provider(*, requested: Optional[str] = None, explicit_api_ke OpenCode Zen/Go where different models route through different API surfaces).""" requested_provider = resolve_requested_provider(requested) _raise_if_provider_disabled(requested_provider) - runtime = _resolve_requested_shortcuts(requested_provider, explicit_api_key, explicit_base_url, target_model) - if runtime: - return runtime + return next(r for r in _ladder_rungs(requested_provider, explicit_api_key, explicit_base_url, target_model) if r) + + +def _named_custom_rung(requested_provider, explicit_api_key, explicit_base_url, target_model): runtime = _resolve_named_custom_runtime(requested_provider=requested_provider, explicit_api_key=explicit_api_key, explicit_base_url=explicit_base_url, target_model=target_model) if runtime: runtime["requested_provider"] = requested_provider - return runtime + return runtime + + +def _ladder_rungs(requested_provider, explicit_api_key, explicit_base_url, target_model): + """Ladder rungs 2-8, yielded lazily so each is evaluated only when the previous one returned + nothing; the last rung (OpenRouter / bare-custom fallback) always yields a runtime.""" + yield _resolve_requested_shortcuts(requested_provider, explicit_api_key, explicit_base_url, target_model) + yield _named_custom_rung(requested_provider, explicit_api_key, explicit_base_url, target_model) if not explicit_base_url and not explicit_api_key: - runtime = _local_endpoint_bypass(requested_provider, explicit_api_key, explicit_base_url) - if runtime: - return runtime + yield _local_endpoint_bypass(requested_provider, explicit_api_key, explicit_base_url) provider = resolve_provider(requested_provider, explicit_api_key=explicit_api_key, explicit_base_url=explicit_base_url) - return next(r for r in _provider_rungs(provider, requested_provider, explicit_api_key, explicit_base_url, target_model) if r) - - -def _provider_rungs(provider, requested_provider, explicit_api_key, explicit_base_url, target_model): - """Rungs 5-8 of the ladder, yielded lazily so each is evaluated only when the previous one - returned nothing; the last rung (OpenRouter / bare-custom fallback) always yields a runtime.""" model_cfg = _get_model_config() yield _opencode_free_runtime(provider, requested_provider, model_cfg, target_model) yield _resolve_explicit_runtime(provider=provider, requested_provider=requested_provider, model_cfg=model_cfg,