Skip to content

Your lightweight open-source personal agent.

refactor(providers): streamline API default resolution

chengyongru2748b647aa4d
diff --git a/nanobot/providers/model_api.py b/nanobot/providers/model_api.py
index e4eb2ef5e..7cf7851ba 100644
--- a/nanobot/providers/model_api.py
+++ b/nanobot/providers/model_api.py
@@ -129,13 +129,12 @@ class ResponsesCapabilities:
extra_body: dict[str, Any] | None = None,
) -> ModelAPICapabilities:
"""Apply endpoint, reasoning, and hosted-tool rules to automatic selection."""
- body = self.request_extra_body(extra_body)
- effective_base = api_base or default_api_base or None
- search_model_base = None if hosted_web_search_enabled(body) else effective_base
- if hosted_web_search_enabled(body) and (
- self.route_reasoning or self.matches_model(model, api_base=search_model_base)
+ if (
+ self.supports_hosted_web_search
+ and hosted_web_search_enabled(extra_body or {})
+ and (self.route_reasoning or self.matches_model(model))
):
return ModelAPICapabilities(("responses",), "responses")
if self.requires_direct_openai_base and not is_direct_openai_base(api_base):
return ModelAPICapabilities()
- return self.model_api(model, reasoning_effort, api_base=effective_base)
+ return self.model_api(model, reasoning_effort, api_base=api_base or default_api_base or None)
diff --git a/nanobot/providers/routing.py b/nanobot/providers/routing.py
index 7e83c3a7f..7b04ad14c 100644
--- a/nanobot/providers/routing.py
+++ b/nanobot/providers/routing.py
@@ -41,7 +41,11 @@ def _provider_spec_for_config(
# Before OrcaRouter became a built-in provider, this name was valid for a
# dynamic custom provider. Preserve that provider's model-prefix behavior
# when an existing config points the name at a different endpoint.
- return create_dynamic_spec(
+ spec = None
+ if spec is None and provider_config is not None:
+ if not provider_config.api_base:
+ raise ValueError(f"Provider '{provider_name}' requires api_base in config.")
+ spec = create_dynamic_spec(
provider_name,
display_name=provider_config.display_name or "",
thinking_style=provider_config.thinking_style or "",
@@ -55,11 +59,12 @@ def resolve_model_api(
preset_api: ModelAPIConfig | None,
) -> ModelAPICapabilities | None:
"""Apply the connection's API ceiling and default to a model declaration."""
- connection_api = provider_config.api if provider_config else None
- if connection_api is not None:
+ connection_api = None
+ if provider_config is not None and provider_config.api is not None:
if not spec.provider_api_configurable:
raise ValueError(f"Provider '{spec.name}' does not support connection API declarations.")
- spec.validate_model_api(connection_api.to_capabilities())
+ connection_api = provider_config.api.to_capabilities()
+ spec.validate_model_api(connection_api)
if preset_api is not None:
api = preset_api.to_capabilities()
spec.validate_model_api(api)
@@ -73,14 +78,13 @@ def resolve_model_api(
return api
if connection_api is None:
return None
- api = connection_api.to_capabilities()
# Selecting a default adapter does not authorize fallback across API families.
- if api.preferred_api == "anthropic_messages":
+ if connection_api.preferred_api == "anthropic_messages":
return ModelAPICapabilities(("anthropic_messages",), "anthropic_messages")
supported: tuple[RequestAPI, ...] = tuple(
- item for item in api.supported_apis if item != "anthropic_messages"
+ item for item in connection_api.supported_apis if item != "anthropic_messages"
)
- return ModelAPICapabilities(supported_apis=supported, preferred_api=api.preferred_api)
+ return ModelAPICapabilities(supported_apis=supported, preferred_api=connection_api.preferred_api)
def resolve_provider_route(
@@ -96,14 +100,6 @@ def resolve_provider_route(
if not provider_name:
raise ValueError(f"No provider is configured for model '{model}'.")
spec = _provider_spec_for_config(provider_name, provider_config)
- if not spec and provider_config:
- if not provider_config.api_base:
- raise ValueError(f"Provider '{provider_name}' requires api_base in config.")
- spec = create_dynamic_spec(
- provider_name,
- display_name=provider_config.display_name or "",
- thinking_style=provider_config.thinking_style or "",
- )
if spec and spec.is_transcription_only:
raise ValueError(f"Provider '{provider_name}' only supports transcription.")
backend = spec.backend if spec else "openai_compat"