The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(aux): _normalize_api_key is the one callable-or-string key normaliser
kshitijk4pooreb6747084d5c
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.pyindex e82ad668113..5d4c5505026 100644--- a/agent/auxiliary_client.py+++ b/agent/auxiliary_client.py@@ -21,7 +21,7 @@ import threadingimport timeimport uuidfrom types import SimpleNamespace-from typing import Any, Callable, Dict, List, NamedTuple, Optional, Tuple, TYPE_CHECKING+from typing import Any, Callable, Dict, List, NamedTuple, Optional, Tuple, TYPE_CHECKING, Unionfrom urllib.parse import urlparse, parse_qs, urlunparsefrom agent.error_classifier import _BILLING_PATTERNS, _OVERLOADED_PATTERNS@@ -2641,7 +2641,7 @@ def set_runtime_main("requested_provider": (requested_provider or "").strip().lower(),"model": (model or "").strip(),"base_url": (base_url or "").strip(),- "api_key": api_key.strip() if isinstance(api_key, str) else api_key if callable(api_key) else "",+ "api_key": _normalize_api_key(api_key),"api_mode": (api_mode or "").strip(),"auth_mode": (auth_mode or "").strip().lower(),"session_id": (session_id or "").strip(),@@ -4727,7 +4727,7 @@ class _ResolveRequest(NamedTuple):async_mode: boolraw_codex: boolexplicit_base_url: Optional[str]- explicit_api_key: Optional[str]+ explicit_api_key: Optional[Union[str, Callable[[], str]]]api_mode: Optional[str]main_runtime: Optional[Dict[str, Any]]is_vision: bool@@ -4737,11 +4737,11 @@ class _ResolveRequest(NamedTuple):_ResolveResult = Tuple[Optional[Any], Optional[str]]-def _explicit_key(raw: Any) -> Any:+def _normalize_api_key(raw: Any) -> Union[str, Callable[[], str]]:"""A key_cmd/Entra callable passes through uncalled; strings are stripped; anything else is ''."""if callable(raw) and not isinstance(raw, str):return raw- return str(raw or "").strip()+ return raw.strip() if isinstance(raw, str) else ""def _log_once_debug(seen: set, key: Any, msg: str, *args: Any) -> None:@@ -4929,10 +4929,10 @@ def _resolve_custom_branch(req: _ResolveRequest) -> _ResolveResult:# SECURITY: a local-server alias never borrows OPENAI_API_KEY or the main key —# the alias means "this is my own server"; sending an OpenAI secret to whatever# host base_url names is never intended. Explicit api_key or the placeholder only.- custom_key = _explicit_key(req.explicit_api_key) or "no-key-required"+ custom_key = _normalize_api_key(req.explicit_api_key) or "no-key-required"else:custom_key = (- _explicit_key(req.explicit_api_key)+ _normalize_api_key(req.explicit_api_key)or _scoped_key_env("OPENAI_API_KEY")or _read_main_api_key_if_same_host(custom_base)or "no-key-required" # local servers don't need auth@@ -4946,7 +4946,7 @@ def _resolve_custom_branch(req: _ResolveRequest) -> _ResolveResult:# 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 = _explicit_key(main_runtime.get("api_key"))+ _main_key = _normalize_api_key(main_runtime.get("api_key"))if _main_base and _main_key:custom_base, custom_key = _main_base, _main_keyif custom_base and custom_key:@@ -5015,7 +5015,7 @@ def _resolve_named_custom_branch(req: _ResolveRequest) -> Optional[_ResolveResul# whatever the caller left blank, never replaces what the caller set (compression prompts carry# conversation history, so a silently swapped destination is a data-routing bug, not a nuisance).custom_base = (req.explicit_base_url or custom_entry.get("base_url") or "").strip()- custom_key = _explicit_key(req.explicit_api_key) or _named_custom_api_key(custom_entry, provider, custom_base)+ custom_key = _normalize_api_key(req.explicit_api_key) or _named_custom_api_key(custom_entry, provider, custom_base)if custom_key == "no-key-required":logger.warning("resolve_provider_client: named custom provider %r has no resolvable ""api_key — request will be sent with placeholder no-key-required "@@ -5112,8 +5112,7 @@ def _resolve_api_key_branch(req: _ResolveRequest, pconfig: Any, resolve_creds: Capi_key = str(creds.get("api_key", "")).strip()# Explicit api_key override (fallback_model / custom_providers entry) lets callers# authenticate where no built-in credential is registered for this alias.- if req.explicit_api_key:- api_key = _explicit_key(req.explicit_api_key) or api_key+ api_key = _normalize_api_key(req.explicit_api_key) or api_keyraw_base_url = str(creds.get("base_url", "")).strip().rstrip("/") or pconfig.inference_base_urlif req.explicit_base_url:raw_base_url = req.explicit_base_url.strip().rstrip("/")@@ -5268,8 +5267,8 @@ _EXPLICIT_PROVIDER_BRANCHES: Dict[str, Callable[[_ResolveRequest], _ResolveResuldef resolve_provider_client(provider: str, model: str = None, async_mode: bool = False, raw_codex: bool = False,- explicit_base_url: str = None, explicit_api_key: str = None, api_mode: str = None,- main_runtime: Optional[Dict[str, Any]] = None, is_vision: bool = False,+ explicit_base_url: str = None, explicit_api_key: Optional[Union[str, Callable[[], str]]] = None,+ api_mode: str = None, main_runtime: Optional[Dict[str, Any]] = None, is_vision: bool = False,task: Optional[str] = None,) -> Tuple[Optional[Any], Optional[str]]:"""Central router: return a configured client (auth, base URL, API format) for a provider + optional model.diff --git a/tests/agent/test_command_token_source.py b/tests/agent/test_command_token_source.pyindex eb7bc8fe27c..528e63378ca 100644--- a/tests/agent/test_command_token_source.py+++ b/tests/agent/test_command_token_source.py@@ -351,7 +351,7 @@ class TestAuxiliaryResolverHonoursKeyCmd:class TestExplicitCallableSurvivesCustomResolution:"""Callers hand ``resolve_provider_client`` a callable (key_cmd token, or the- main runtime's callable credential via ``_route_via_main_provider``) and every+ main runtime's callable credential via ``_try_main_provider_route``) and everyresolution branch must pass it through *uncalled*: ``.strip()`` on it raisedAttributeError (#88667); ``str()`` sent the object repr as the bearer.Adapted from SiaoZeng's #107344.