refactor(auxiliary): fold main_runtime custom-endpoint reuse into the shared client-build path

Follow-up to the #45545 salvage: the cherry-picked fix duplicated the
~35-line header-shaping block (kimi UA, copilot headers, nvidia NIM,
provider-profile defaults, query params) from the explicit_base_url
branch. Route the main_runtime case through the same block instead —
one client-build path, no drift risk. Also uses _create_openai_client
like the sibling branch instead of constructing OpenAI directly.
This commit is contained in:
teknium1 2026-07-05 19:09:49 -07:00 committed by Teknium
parent 92da7a9970
commit 571f2a7fd2

View file

@ -4307,6 +4307,8 @@ def resolve_provider_client(
# ── Custom endpoint (OPENAI_BASE_URL + OPENAI_API_KEY) ───────────
if provider == "custom":
custom_base = ""
custom_key = ""
if explicit_base_url:
custom_base = _to_openai_base_url(explicit_base_url).strip()
custom_key = (
@ -4321,6 +4323,19 @@ def resolve_provider_client(
"but base_url is empty"
)
return None, None
elif main_runtime:
# When main_runtime carries a concrete base_url + api_key for a
# named custom provider (custom:<name>), use it directly instead
# of re-resolving from the bare "custom" provider name.
# Re-resolution loses the provider name and falls back to
# OpenRouter or a wrong API-key provider — the main agent already
# solved this, we just need to reuse its answer. (#45472)
_main_base = str(main_runtime.get("base_url") or "").strip().rstrip("/")
_main_key = str(main_runtime.get("api_key") or "").strip()
if _main_base and _main_key:
custom_base = _main_base
custom_key = _main_key
if custom_base and custom_key:
final_model = _normalize_resolved_model(
model or (main_runtime.get("model") if main_runtime else None) or "gpt-4o-mini",
provider,
@ -4357,49 +4372,6 @@ def resolve_provider_client(
else (client, final_model))
# Try custom first, then API-key providers (Codex excluded here:
# falling through to Codex with no model is a stale-constant trap).
#
# When main_runtime carries a concrete base_url + api_key for a
# named custom provider (custom:<name>), use it directly instead of
# re-resolving from the bare "custom" provider name. Re-resolution
# loses the provider name and falls back to OpenRouter or a wrong
# API-key provider — the main agent already solved this, we just
# need to reuse its answer. (#45472)
if main_runtime:
main_base = str(main_runtime.get("base_url") or "").strip().rstrip("/")
main_key = str(main_runtime.get("api_key") or "").strip()
if main_base and main_key:
final_model = _normalize_resolved_model(
model or main_runtime.get("model") or "gpt-4o-mini",
provider,
)
extra = {}
_clean_base, _dq = _extract_url_query_params(main_base)
if _dq:
extra["default_query"] = _dq
if base_url_host_matches(main_base, "api.kimi.com"):
extra["default_headers"] = {"User-Agent": "claude-code/0.1.0"}
elif base_url_host_matches(main_base, "api.githubcopilot.com"):
from hermes_cli.copilot_auth import copilot_request_headers
extra["default_headers"] = copilot_request_headers(
is_agent_turn=True, is_vision=is_vision
)
elif base_url_host_matches(main_base, "integrate.api.nvidia.com"):
extra["default_headers"] = build_nvidia_nim_headers(main_base)
else:
try:
from providers import get_provider_profile as _gpf_main
_ph_main = _gpf_main(provider)
if _ph_main and _ph_main.default_headers:
extra["default_headers"] = dict(_ph_main.default_headers)
except Exception:
pass
_merged_main = _apply_user_default_headers(extra.get("default_headers"))
if _merged_main:
extra["default_headers"] = _merged_main
client = OpenAI(api_key=main_key, base_url=_clean_base, **extra)
client = _wrap_if_needed(client, final_model, main_base, main_key)
return (_to_async_client(client, final_model, is_vision=is_vision) if async_mode
else (client, final_model))
for try_fn in (_try_custom_endpoint, _resolve_api_key_provider):
client, default = try_fn()
if client is not None: