refactor(models): separate API routing and editor state
This commit is contained in:
1 parent
5eb160099b
commit
ed0eb89178
11 files changed
+357
-319
No files matched your search
+36
-231
@@ -8,20 +8,16 @@ from pathlib import Path
|
||||
from nanobot.config.schema import (
|
||||
Config,
|
||||
InlineFallbackConfig,
|
||||
ModelAPIConfig,
|
||||
ModelPresetConfig,
|
||||
ProviderConfig,
|
||||
)
|
||||
from nanobot.providers.base import GenerationSettings, LLMProvider
|
||||
from nanobot.providers.fallback_provider import FallbackProvider
|
||||
from nanobot.providers.registry import (
|
||||
ModelAPICapabilities,
|
||||
ProviderAPI,
|
||||
ProviderSpec,
|
||||
RequestAPI,
|
||||
create_dynamic_spec,
|
||||
find_by_name,
|
||||
)
|
||||
from nanobot.providers.registry import ProviderSpec, find_by_name
|
||||
from nanobot.providers.routing import resolve_automatic_model_api as resolve_automatic_model_api
|
||||
from nanobot.providers.routing import resolve_model_api as resolve_model_api
|
||||
from nanobot.providers.routing import resolve_provider_route
|
||||
from nanobot.providers.routing import validate_provider_setup as validate_provider_setup
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -34,16 +30,6 @@ class ProviderSnapshot:
|
||||
model_preset: str | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class _ProviderSetup:
|
||||
model: str
|
||||
provider_name: str
|
||||
provider_config: ProviderConfig | None
|
||||
spec: ProviderSpec | None
|
||||
backend: str
|
||||
model_api: ModelAPICapabilities | None
|
||||
|
||||
|
||||
def _resolve_model_preset(
|
||||
config: Config,
|
||||
*,
|
||||
@@ -63,169 +49,6 @@ def _provider_extra_headers(
|
||||
return headers or None
|
||||
|
||||
|
||||
def _provider_spec_for_config(
|
||||
provider_name: str,
|
||||
provider_config: ProviderConfig | None,
|
||||
) -> ProviderSpec | None:
|
||||
spec = find_by_name(provider_name)
|
||||
if (
|
||||
spec is not None
|
||||
and spec.name == "orcarouter"
|
||||
and provider_config is not None
|
||||
and provider_config.api_base
|
||||
and provider_config.api_base.rstrip("/").lower()
|
||||
!= spec.default_api_base.rstrip("/").lower()
|
||||
):
|
||||
# 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(
|
||||
provider_name,
|
||||
display_name=provider_config.display_name or "",
|
||||
thinking_style=provider_config.thinking_style or "",
|
||||
)
|
||||
return spec
|
||||
|
||||
|
||||
def resolve_model_api(
|
||||
spec: ProviderSpec,
|
||||
provider_config: ProviderConfig | None,
|
||||
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:
|
||||
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())
|
||||
if preset_api is not None:
|
||||
api = preset_api.to_capabilities()
|
||||
spec.validate_model_api(api)
|
||||
if connection_api is not None:
|
||||
unsupported = set(api.supported_apis) - set(connection_api.supported_apis)
|
||||
if unsupported:
|
||||
raise ValueError(
|
||||
f"Provider '{spec.name}' does not accept request APIs: {', '.join(sorted(unsupported))}"
|
||||
)
|
||||
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.
|
||||
supported: tuple[RequestAPI, ...] = ("anthropic_messages",) if api.preferred_api == "anthropic_messages" else tuple(
|
||||
item for item in api.supported_apis if item != "anthropic_messages"
|
||||
)
|
||||
return ModelAPICapabilities(supported_apis=supported, preferred_api=api.preferred_api)
|
||||
|
||||
|
||||
def _resolve_provider_setup(
|
||||
config: Config,
|
||||
*,
|
||||
preset: ModelPresetConfig,
|
||||
model: str | None = None,
|
||||
) -> _ProviderSetup:
|
||||
"""Resolve and validate provider configuration without constructing a client."""
|
||||
model = model or preset.model
|
||||
provider_name = config.get_provider_name(model, preset=preset)
|
||||
p = config.get_provider(model, preset=preset)
|
||||
if not provider_name:
|
||||
raise ValueError(f"No provider is configured for model '{model}'.")
|
||||
spec = _provider_spec_for_config(provider_name, p)
|
||||
if not spec and p:
|
||||
if not p.api_base:
|
||||
raise ValueError(f"Provider '{provider_name}' requires api_base in config.")
|
||||
spec = create_dynamic_spec(
|
||||
provider_name,
|
||||
display_name=(p.display_name or "") if p else "",
|
||||
thinking_style=(p.thinking_style or "") if p else "",
|
||||
)
|
||||
if spec and spec.is_transcription_only:
|
||||
raise ValueError(f"Provider '{provider_name}' only supports transcription.")
|
||||
backend = spec.backend if spec else "openai_compat"
|
||||
model_api = resolve_model_api(spec, p, preset.api) if spec is not None else None
|
||||
if model_api is not None and model_api.preferred_api == "anthropic_messages":
|
||||
backend = "anthropic"
|
||||
if p and p.proxy and backend not in {"openai_compat", "openai_codex", "xai_grok", "anthropic"}:
|
||||
raise ValueError(
|
||||
f"providers.{provider_name}.proxy is only supported for "
|
||||
"OpenAI-compatible providers, Anthropic Messages, OpenAI Codex, and xAI Grok."
|
||||
)
|
||||
|
||||
if backend == "azure_openai":
|
||||
if not p or not p.api_base:
|
||||
raise ValueError("Azure OpenAI requires api_base in config.")
|
||||
elif (
|
||||
backend in {"openai_compat", "anthropic"}
|
||||
and spec
|
||||
and spec.is_direct
|
||||
and not spec.default_api_base
|
||||
and not (p and p.api_base)
|
||||
):
|
||||
raise ValueError(f"Provider '{provider_name}' requires api_base in config.")
|
||||
elif backend in {"anthropic", "openai_compat"} and not (
|
||||
backend == "openai_compat" and model.startswith("bedrock/")
|
||||
):
|
||||
needs_key = not (p and p.api_key)
|
||||
exempt = spec and (spec.is_oauth or spec.is_local or spec.is_direct)
|
||||
if needs_key and not exempt:
|
||||
raise ValueError(f"No API key configured for provider '{provider_name}'.")
|
||||
|
||||
return _ProviderSetup(
|
||||
model=model,
|
||||
provider_name=provider_name,
|
||||
provider_config=p,
|
||||
spec=spec,
|
||||
backend=backend,
|
||||
model_api=model_api,
|
||||
)
|
||||
|
||||
|
||||
def resolve_automatic_model_api(
|
||||
config: Config, *, preset: ModelPresetConfig,
|
||||
) -> tuple[str, ProviderAPI]:
|
||||
"""Preview the default request API without live requests or circuit-breaker state."""
|
||||
setup = _resolve_provider_setup(config, preset=preset.model_copy(update={"api": None}))
|
||||
if setup.model_api is not None:
|
||||
return setup.provider_name, setup.model_api.preferred_api
|
||||
spec = setup.spec
|
||||
if spec is None:
|
||||
return setup.provider_name, "chat_completions"
|
||||
if len(spec.request_apis) == 1:
|
||||
return setup.provider_name, spec.request_apis[0]
|
||||
provider_config = setup.provider_config
|
||||
if setup.backend == "github_copilot":
|
||||
from nanobot.providers.github_copilot_provider import cached_github_copilot_model_api
|
||||
|
||||
api = cached_github_copilot_model_api(
|
||||
setup.model, provider_config.proxy if provider_config else None,
|
||||
)
|
||||
if api is not None:
|
||||
return setup.provider_name, api.preferred_api
|
||||
api = spec.default_model_api(
|
||||
setup.model, preset.reasoning_effort,
|
||||
api_base=config.get_api_base(setup.model, preset=preset),
|
||||
api_type=provider_config.api_type if provider_config else "auto",
|
||||
extra_body=provider_config.extra_body if provider_config else None,
|
||||
)
|
||||
return setup.provider_name, api.preferred_api
|
||||
|
||||
|
||||
def validate_provider_setup(
|
||||
config: Config,
|
||||
*,
|
||||
preset_name: str | None = None,
|
||||
preset: ModelPresetConfig | None = None,
|
||||
model: str | None = None,
|
||||
) -> None:
|
||||
"""Validate local provider/model settings without loading a provider client."""
|
||||
resolved = _resolve_model_preset(config, preset_name=preset_name, preset=preset)
|
||||
_resolve_provider_setup(
|
||||
config,
|
||||
preset=resolved,
|
||||
model=model,
|
||||
)
|
||||
|
||||
|
||||
def _make_provider_core(
|
||||
config: Config,
|
||||
*,
|
||||
@@ -233,7 +56,7 @@ def _make_provider_core(
|
||||
model: str | None = None,
|
||||
) -> LLMProvider:
|
||||
"""Create a plain LLM provider without failover wrapping."""
|
||||
setup = _resolve_provider_setup(
|
||||
setup = resolve_provider_route(
|
||||
config,
|
||||
preset=preset,
|
||||
model=model,
|
||||
@@ -409,6 +232,34 @@ def build_unconfigured_provider_snapshot(config: Config, setup_error: str) -> Pr
|
||||
)
|
||||
|
||||
|
||||
def _preset_provider_signature(
|
||||
config: Config, preset: ModelPresetConfig,
|
||||
) -> tuple[object, ...]:
|
||||
provider_config = config.get_provider(preset.model, preset=preset)
|
||||
provider_name = config.get_provider_name(preset.model, preset=preset)
|
||||
return (
|
||||
preset.model,
|
||||
preset.provider,
|
||||
provider_name,
|
||||
config.get_api_key(preset.model, preset=preset),
|
||||
config.get_api_base(preset.model, preset=preset),
|
||||
_provider_extra_headers(find_by_name(provider_name) if provider_name else None, provider_config),
|
||||
provider_config.extra_body if provider_config else None,
|
||||
provider_config.api_type if provider_config else "auto",
|
||||
provider_config.api.model_dump_json() if provider_config and provider_config.api is not None else None,
|
||||
provider_config.extra_query if provider_config else None,
|
||||
getattr(provider_config, "region", None) if provider_config else None,
|
||||
getattr(provider_config, "profile", None) if provider_config else None,
|
||||
preset.max_tokens,
|
||||
preset.temperature,
|
||||
preset.reasoning_effort,
|
||||
preset.context_window_tokens,
|
||||
preset.api.model_dump_json() if preset.api is not None else None,
|
||||
getattr(provider_config, "proxy", None) if provider_config else None,
|
||||
provider_config.thinking_style if provider_config else None,
|
||||
)
|
||||
|
||||
|
||||
def provider_signature(
|
||||
config: Config,
|
||||
*,
|
||||
@@ -417,60 +268,14 @@ def provider_signature(
|
||||
) -> tuple[object, ...]:
|
||||
"""Return the config fields that affect the active provider chain."""
|
||||
resolved = _resolve_model_preset(config, preset_name=preset_name, preset=preset)
|
||||
p = config.get_provider(resolved.model, preset=resolved)
|
||||
fallback_presets = _resolve_fallback_presets(config, resolved)
|
||||
|
||||
def _fallback_signature(fallback: ModelPresetConfig) -> tuple[object, ...]:
|
||||
fp = config.get_provider(fallback.model, preset=fallback)
|
||||
provider_name = config.get_provider_name(fallback.model, preset=fallback)
|
||||
return (
|
||||
fallback.model,
|
||||
fallback.provider,
|
||||
provider_name,
|
||||
config.get_api_key(fallback.model, preset=fallback),
|
||||
config.get_api_base(fallback.model, preset=fallback),
|
||||
_provider_extra_headers(find_by_name(provider_name) if provider_name else None, fp),
|
||||
fp.extra_body if fp else None,
|
||||
fp.api_type if fp else "auto",
|
||||
fp.api.model_dump_json() if fp and fp.api is not None else None,
|
||||
fp.extra_query if fp else None,
|
||||
getattr(fp, "region", None) if fp else None,
|
||||
getattr(fp, "profile", None) if fp else None,
|
||||
fallback.max_tokens,
|
||||
fallback.temperature,
|
||||
fallback.reasoning_effort,
|
||||
fallback.context_window_tokens,
|
||||
fallback.api.model_dump_json() if fallback.api is not None else None,
|
||||
getattr(fp, "proxy", None) if fp else None,
|
||||
fp.thinking_style if fp else None,
|
||||
)
|
||||
|
||||
provider_name = config.get_provider_name(resolved.model, preset=resolved)
|
||||
return (
|
||||
resolved.model,
|
||||
resolved.provider,
|
||||
provider_name,
|
||||
config.get_api_key(resolved.model, preset=resolved),
|
||||
config.get_api_base(resolved.model, preset=resolved),
|
||||
_provider_extra_headers(find_by_name(provider_name) if provider_name else None, p),
|
||||
p.extra_body if p else None,
|
||||
p.api_type if p else "auto",
|
||||
p.api.model_dump_json() if p and p.api is not None else None,
|
||||
p.extra_query if p else None,
|
||||
getattr(p, "region", None) if p else None,
|
||||
getattr(p, "profile", None) if p else None,
|
||||
resolved.max_tokens,
|
||||
resolved.temperature,
|
||||
resolved.reasoning_effort,
|
||||
resolved.context_window_tokens,
|
||||
resolved.api.model_dump_json() if resolved.api is not None else None,
|
||||
getattr(p, "proxy", None) if p else None,
|
||||
p.thinking_style if p else None,
|
||||
*_preset_provider_signature(config, resolved),
|
||||
tuple(
|
||||
fallback if isinstance(fallback, str) else None
|
||||
for fallback in config.agents.defaults.fallback_models
|
||||
),
|
||||
tuple(_fallback_signature(fallback) for fallback in fallback_presets),
|
||||
tuple(_preset_provider_signature(config, fallback) for fallback in fallback_presets),
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -1111,9 +1111,12 @@ class OpenAICompatProvider(LLMProvider):
|
||||
)
|
||||
|
||||
def _responses_capabilities(self) -> ResponsesCapabilities | None:
|
||||
capabilities = self._spec.responses if self._spec is not None else None
|
||||
if capabilities is not None:
|
||||
return capabilities
|
||||
if self._preset_model_api is not None and "responses" in self._preset_model_api.supported_apis:
|
||||
return self._spec.responses if self._spec and self._spec.responses else ResponsesCapabilities()
|
||||
return self._spec.responses if self._spec is not None else None
|
||||
return ResponsesCapabilities()
|
||||
return None
|
||||
|
||||
def _responses_state_provider(self) -> str:
|
||||
spec_name = self._spec.name if self._spec is not None else "custom"
|
||||
@@ -1139,21 +1142,17 @@ class OpenAICompatProvider(LLMProvider):
|
||||
capabilities = self._responses_capabilities()
|
||||
if (
|
||||
not self._responses.native_compaction_available
|
||||
or (
|
||||
self._preset_model_api is not None
|
||||
and (
|
||||
"responses" not in self._preset_model_api.supported_apis
|
||||
or (
|
||||
self._preset_model_api.preferred_api != "responses"
|
||||
and not self._hosted_web_search_enabled()
|
||||
)
|
||||
)
|
||||
)
|
||||
or (self._preset_model_api is None and self._api_type == "chat_completions")
|
||||
or capabilities is None
|
||||
or not capabilities.supports_native_compaction
|
||||
):
|
||||
return False
|
||||
if self._preset_model_api is not None:
|
||||
if "responses" not in self._preset_model_api.supported_apis:
|
||||
return False
|
||||
if self._preset_model_api.preferred_api != "responses" and not self._hosted_web_search_enabled():
|
||||
return False
|
||||
elif self._api_type == "chat_completions":
|
||||
return False
|
||||
if (
|
||||
capabilities.requires_direct_openai_base
|
||||
and not is_direct_openai_base(self._effective_base)
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
"""Resolve provider connections and request APIs without creating SDK clients."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
from nanobot.config.schema import Config, ModelAPIConfig, ModelPresetConfig, ProviderConfig
|
||||
from nanobot.providers.registry import (
|
||||
ModelAPICapabilities,
|
||||
ProviderAPI,
|
||||
ProviderSpec,
|
||||
RequestAPI,
|
||||
create_dynamic_spec,
|
||||
find_by_name,
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ProviderRoute:
|
||||
"""Validated connection and adapter choice, without constructing a client.
|
||||
|
||||
``model_api`` carries configured overrides; ``None`` preserves automatic
|
||||
model discovery and the adapter's request-time defaults.
|
||||
"""
|
||||
|
||||
model: str
|
||||
provider_name: str
|
||||
provider_config: ProviderConfig | None
|
||||
spec: ProviderSpec | None
|
||||
backend: str
|
||||
model_api: ModelAPICapabilities | None
|
||||
|
||||
|
||||
def _provider_spec_for_config(
|
||||
provider_name: str,
|
||||
provider_config: ProviderConfig | None,
|
||||
) -> ProviderSpec | None:
|
||||
spec = find_by_name(provider_name)
|
||||
if (
|
||||
spec is not None
|
||||
and spec.name == "orcarouter"
|
||||
and provider_config is not None
|
||||
and provider_config.api_base
|
||||
and provider_config.api_base.rstrip("/").lower()
|
||||
!= spec.default_api_base.rstrip("/").lower()
|
||||
):
|
||||
# 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(
|
||||
provider_name,
|
||||
display_name=provider_config.display_name or "",
|
||||
thinking_style=provider_config.thinking_style or "",
|
||||
)
|
||||
return spec
|
||||
|
||||
|
||||
def resolve_model_api(
|
||||
spec: ProviderSpec,
|
||||
provider_config: ProviderConfig | None,
|
||||
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:
|
||||
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())
|
||||
if preset_api is not None:
|
||||
api = preset_api.to_capabilities()
|
||||
spec.validate_model_api(api)
|
||||
if connection_api is not None:
|
||||
unsupported = set(api.supported_apis) - set(connection_api.supported_apis)
|
||||
if unsupported:
|
||||
raise ValueError(
|
||||
f"Provider '{spec.name}' does not accept request APIs: {', '.join(sorted(unsupported))}"
|
||||
)
|
||||
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":
|
||||
return ModelAPICapabilities(("anthropic_messages",), "anthropic_messages")
|
||||
supported: tuple[RequestAPI, ...] = tuple(
|
||||
item for item in api.supported_apis if item != "anthropic_messages"
|
||||
)
|
||||
return ModelAPICapabilities(supported_apis=supported, preferred_api=api.preferred_api)
|
||||
|
||||
|
||||
def resolve_provider_route(
|
||||
config: Config,
|
||||
*,
|
||||
preset: ModelPresetConfig,
|
||||
model: str | None = None,
|
||||
) -> ProviderRoute:
|
||||
"""Resolve and validate provider configuration without constructing a client."""
|
||||
model = model or preset.model
|
||||
provider_name = config.get_provider_name(model, preset=preset)
|
||||
provider_config = config.get_provider(model, preset=preset)
|
||||
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"
|
||||
model_api = resolve_model_api(spec, provider_config, preset.api) if spec is not None else None
|
||||
if model_api is not None and model_api.preferred_api == "anthropic_messages":
|
||||
backend = "anthropic"
|
||||
if (
|
||||
provider_config
|
||||
and provider_config.proxy
|
||||
and backend not in {"openai_compat", "openai_codex", "xai_grok", "anthropic"}
|
||||
):
|
||||
raise ValueError(
|
||||
f"providers.{provider_name}.proxy is only supported for "
|
||||
"OpenAI-compatible providers, Anthropic Messages, OpenAI Codex, and xAI Grok."
|
||||
)
|
||||
|
||||
if backend == "azure_openai":
|
||||
if not provider_config or not provider_config.api_base:
|
||||
raise ValueError("Azure OpenAI requires api_base in config.")
|
||||
elif (
|
||||
backend in {"openai_compat", "anthropic"}
|
||||
and spec
|
||||
and spec.is_direct
|
||||
and not spec.default_api_base
|
||||
and not (provider_config and provider_config.api_base)
|
||||
):
|
||||
raise ValueError(f"Provider '{provider_name}' requires api_base in config.")
|
||||
elif backend in {"anthropic", "openai_compat"} and not (
|
||||
backend == "openai_compat" and model.startswith("bedrock/")
|
||||
):
|
||||
needs_key = not (provider_config and provider_config.api_key)
|
||||
exempt = spec and (spec.is_oauth or spec.is_local or spec.is_direct)
|
||||
if needs_key and not exempt:
|
||||
raise ValueError(f"No API key configured for provider '{provider_name}'.")
|
||||
|
||||
return ProviderRoute(
|
||||
model=model,
|
||||
provider_name=provider_name,
|
||||
provider_config=provider_config,
|
||||
spec=spec,
|
||||
backend=backend,
|
||||
model_api=model_api,
|
||||
)
|
||||
|
||||
|
||||
def resolve_automatic_model_api(
|
||||
config: Config, *, preset: ModelPresetConfig,
|
||||
) -> tuple[str, ProviderAPI]:
|
||||
"""Preview the default request API without live requests or circuit-breaker state."""
|
||||
setup = resolve_provider_route(config, preset=preset.model_copy(update={"api": None}))
|
||||
if setup.model_api is not None:
|
||||
return setup.provider_name, setup.model_api.preferred_api
|
||||
spec = setup.spec
|
||||
if spec is None:
|
||||
return setup.provider_name, "chat_completions"
|
||||
if len(spec.request_apis) == 1:
|
||||
return setup.provider_name, spec.request_apis[0]
|
||||
provider_config = setup.provider_config
|
||||
if setup.backend == "github_copilot":
|
||||
from nanobot.providers.github_copilot_provider import cached_github_copilot_model_api
|
||||
|
||||
api = cached_github_copilot_model_api(
|
||||
setup.model, provider_config.proxy if provider_config else None,
|
||||
)
|
||||
if api is not None:
|
||||
return setup.provider_name, api.preferred_api
|
||||
api = spec.default_model_api(
|
||||
setup.model, preset.reasoning_effort,
|
||||
api_base=config.get_api_base(setup.model, preset=preset),
|
||||
api_type=provider_config.api_type if provider_config else "auto",
|
||||
extra_body=provider_config.extra_body if provider_config else None,
|
||||
)
|
||||
return setup.provider_name, api.preferred_api
|
||||
|
||||
|
||||
def validate_provider_setup(
|
||||
config: Config,
|
||||
*,
|
||||
preset_name: str | None = None,
|
||||
preset: ModelPresetConfig | None = None,
|
||||
model: str | None = None,
|
||||
) -> None:
|
||||
"""Validate local provider/model settings without loading a provider client."""
|
||||
resolved = preset if preset is not None else config.resolve_preset(preset_name)
|
||||
resolve_provider_route(
|
||||
config,
|
||||
preset=resolved,
|
||||
model=model,
|
||||
)
|
||||
@@ -32,14 +32,14 @@ from nanobot.config.schema import (
|
||||
ModelPresetConfig,
|
||||
ProviderConfig,
|
||||
)
|
||||
from nanobot.providers.factory import resolve_automatic_model_api, resolve_model_api
|
||||
from nanobot.providers.image_generation import get_image_gen_provider
|
||||
from nanobot.providers.oauth_guidance import OAUTH_CLI_KIT_MISSING_MESSAGE
|
||||
from nanobot.providers.oauth_model_catalog import (
|
||||
get_oauth_model_catalog,
|
||||
invalidate_oauth_model_catalog,
|
||||
)
|
||||
from nanobot.providers.registry import PROVIDERS, create_dynamic_spec, find_by_name
|
||||
from nanobot.providers.registry import PROVIDERS, ProviderSpec, create_dynamic_spec, find_by_name
|
||||
from nanobot.providers.routing import resolve_automatic_model_api, resolve_model_api
|
||||
from nanobot.webui.settings_contracts import (
|
||||
QueryParams,
|
||||
SettingsRequest,
|
||||
@@ -418,7 +418,7 @@ def _dynamic_provider_items(config: Config) -> list[tuple[str, ProviderConfig]]:
|
||||
def resolve_settings_provider(
|
||||
config: Config,
|
||||
provider_name: str,
|
||||
) -> tuple[Any, str, ProviderConfig] | None:
|
||||
) -> tuple[ProviderSpec, str, ProviderConfig] | None:
|
||||
spec = find_by_name(provider_name)
|
||||
if spec is not None:
|
||||
provider_config = getattr(config.providers, spec.name, None)
|
||||
@@ -462,12 +462,13 @@ def _provider_advanced_field_names(name: str, spec: Any) -> list[str]:
|
||||
|
||||
def _provider_settings_row(
|
||||
name: str,
|
||||
spec: Any,
|
||||
spec: ProviderSpec,
|
||||
provider_config: ProviderConfig,
|
||||
oauth_status_reader: OAuthStatusReader,
|
||||
) -> dict[str, Any]:
|
||||
oauth_status = oauth_status_reader(spec) if spec.is_oauth else None
|
||||
is_custom = find_by_name(name) is None
|
||||
request_apis = provider_config.api.supported_apis if provider_config.api else spec.request_apis
|
||||
row = {
|
||||
"name": name,
|
||||
"label": spec.label,
|
||||
@@ -484,11 +485,11 @@ def _provider_settings_row(
|
||||
"default_api_base": spec.default_api_base or None,
|
||||
"model_selectable": not spec.is_transcription_only,
|
||||
"model_catalog": model_catalog_kind(spec),
|
||||
"request_apis": list(provider_config.api.supported_apis if provider_config.api else spec.request_apis),
|
||||
"request_apis": list(request_apis),
|
||||
"adapter_request_apis": list(spec.request_apis),
|
||||
"api": provider_config.api.model_dump(mode="json") if provider_config.api else None,
|
||||
"provider_api_configurable": spec.provider_api_configurable,
|
||||
"model_api_configurable": len(provider_config.api.supported_apis) > 1 if provider_config.api else spec.model_api_configurable,
|
||||
"model_api_configurable": len(request_apis) > 1,
|
||||
"advanced_fields": _provider_advanced_field_names(name, spec),
|
||||
"extra_headers": _redact_provider_secret_values(provider_config.extra_headers),
|
||||
"extra_body": _redact_provider_secret_values(provider_config.extra_body),
|
||||
@@ -1242,8 +1243,9 @@ def _parse_preset_api(
|
||||
entry = resolve_settings_provider(config, provider_name)
|
||||
if entry is None:
|
||||
raise WebUISettingsError("unknown provider")
|
||||
spec, _, provider_config = entry
|
||||
try:
|
||||
resolve_model_api(entry[0], entry[2], api)
|
||||
resolve_model_api(spec, provider_config, api)
|
||||
except ValueError as exc:
|
||||
raise WebUISettingsError(str(exc)) from None
|
||||
return api
|
||||
|
||||
@@ -1,31 +1,13 @@
|
||||
import { useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { modelAPIConfigurable, preferredModelAPI, REQUEST_API_LABELS } from "@/components/settings/models/modelAPI";
|
||||
import { ToggleButton } from "@/components/settings/ToggleButton";
|
||||
import { SettingsRow } from "@/components/settings/shared/SettingsControls";
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import { cn } from "@/lib/utils";
|
||||
import type { ModelAPIConfig, ProviderRequestAPI, SettingsPayload } from "@/lib/types";
|
||||
|
||||
export function modelAPIConfigurable(provider: SettingsPayload["providers"][number] | undefined): boolean {
|
||||
if (Array.isArray(provider?.request_apis)) {
|
||||
const supported = provider.request_apis;
|
||||
return (["chat_completions", "responses", "anthropic_messages"] as const)
|
||||
.filter((api) => supported.includes(api)).length > 1;
|
||||
}
|
||||
return provider?.model_api_configurable === true;
|
||||
}
|
||||
|
||||
export function modelAPISelection(api: ModelAPIConfig | null | undefined): string {
|
||||
if (!api) return "auto";
|
||||
if (api.supported_apis.includes("anthropic_messages")) return "anthropic_messages";
|
||||
if (!api.supported_apis.includes("responses")) return "chat_completions";
|
||||
if (!api.supported_apis.includes("chat_completions")) return "responses";
|
||||
return (api.preferred_api ?? api.supported_apis[0]) === "responses"
|
||||
? "prefer_responses"
|
||||
: "prefer_chat";
|
||||
}
|
||||
|
||||
export function ModelAPIControl({
|
||||
provider,
|
||||
automaticAPI,
|
||||
@@ -43,17 +25,13 @@ export function ModelAPIControl({
|
||||
const configurable = modelAPIConfigurable(provider);
|
||||
const fixedAPI = requestAPIs?.length === 1 ? requestAPIs[0] : undefined;
|
||||
const labels = {
|
||||
responses: "Responses",
|
||||
chat_completions: "Chat Completions",
|
||||
anthropic_messages: "Anthropic Messages",
|
||||
...REQUEST_API_LABELS,
|
||||
bedrock_converse: "Bedrock Converse",
|
||||
transcription: t("settings.models.apiTranscription"),
|
||||
};
|
||||
const fixedLabel = fixedAPI ? labels[fixedAPI] : undefined;
|
||||
const selection = modelAPISelection(value);
|
||||
const selectedAPI = selection === "prefer_responses" ? "responses"
|
||||
: selection === "prefer_chat" ? "chat_completions" : selection;
|
||||
const allowChatFallback = selection === "prefer_responses";
|
||||
const selectedAPI = preferredModelAPI(value);
|
||||
const allowChatFallback = selectedAPI === "responses" && value?.supported_apis.includes("chat_completions") === true;
|
||||
const options = [
|
||||
{
|
||||
value: "auto",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { ProviderIcon } from "@/components/settings/models/ProviderSettings";
|
||||
import { ModelAPIControl } from "@/components/settings/models/ModelAPIControl";
|
||||
import { useAutomaticModelAPI } from "@/components/settings/models/useAutomaticModelAPI";
|
||||
import { ToggleButton } from "@/components/settings/ToggleButton";
|
||||
import { useAutoSave } from "@/components/settings/shared/useAutoSave";
|
||||
import { useEffect, useId, useRef, useState, type Dispatch, type SetStateAction } from "react";
|
||||
@@ -43,8 +44,7 @@ import {
|
||||
} from "@/components/ui/dialog";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { fetchAutomaticModelAPI } from "@/lib/api";
|
||||
import type { AutomaticModelAPIPayload, ModelAPIConfig, SettingsPayload } from "@/lib/types";
|
||||
import type { ModelAPIConfig, SettingsPayload } from "@/lib/types";
|
||||
|
||||
export interface AgentSettingsDraft {
|
||||
model: string;
|
||||
@@ -238,30 +238,15 @@ export function ModelsSettings({
|
||||
const tx = (key: string, fallback: string, values?: Record<string, unknown>) =>
|
||||
t(key, { defaultValue: fallback, ...(values ?? {}) });
|
||||
const [editorOpen, setEditorOpen] = useState(false);
|
||||
const [automaticAPI, setAutomaticAPI] = useState<{
|
||||
scope: string;
|
||||
result: AutomaticModelAPIPayload;
|
||||
} | null>(null);
|
||||
const { provider: apiProvider, model: apiModel, reasoningEffort: apiReasoningEffort } = form;
|
||||
const automaticAPIScope = JSON.stringify([
|
||||
token, apiProvider, apiModel, apiReasoningEffort, settings.providers,
|
||||
]);
|
||||
useEffect(() => {
|
||||
if (!editorOpen || settings.model_api_resolution_supported !== true || !apiModel.trim()) return;
|
||||
let cancelled = false;
|
||||
const timer = window.setTimeout(() => {
|
||||
void fetchAutomaticModelAPI(token, apiProvider, apiModel, apiReasoningEffort)
|
||||
.then((result) => {
|
||||
if (!cancelled) setAutomaticAPI({ scope: automaticAPIScope, result });
|
||||
})
|
||||
.catch(() => {
|
||||
if (!cancelled) setAutomaticAPI(null);
|
||||
});
|
||||
}, 150);
|
||||
return () => { cancelled = true; window.clearTimeout(timer); };
|
||||
}, [editorOpen, settings.model_api_resolution_supported, automaticAPIScope, token, apiProvider, apiModel, apiReasoningEffort]);
|
||||
const resolvedAutomaticAPI = settings.model_api_resolution_supported === true
|
||||
&& automaticAPI?.scope === automaticAPIScope ? automaticAPI.result : null;
|
||||
const resolvedAutomaticAPI = useAutomaticModelAPI({
|
||||
token,
|
||||
provider: form.provider,
|
||||
model: form.model,
|
||||
reasoningEffort: form.reasoningEffort,
|
||||
providers: settings.providers,
|
||||
supported: settings.model_api_resolution_supported === true,
|
||||
editorOpen,
|
||||
});
|
||||
const editorTriggerRef = useRef<HTMLElement | null>(null);
|
||||
const presetNameInputRef = useRef<HTMLInputElement>(null);
|
||||
const suggestedPresetNameRef = useRef<string | null>(null);
|
||||
|
||||
@@ -1,16 +1,11 @@
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { MODEL_REQUEST_APIS, REQUEST_API_LABELS } from "@/components/settings/models/modelAPI";
|
||||
import { ToggleButton } from "@/components/settings/ToggleButton";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import type { ModelAPIConfig, ModelRequestAPI } from "@/lib/types";
|
||||
|
||||
const APIS: Array<{ value: ModelRequestAPI; label: string }> = [
|
||||
{ value: "chat_completions", label: "Chat Completions" },
|
||||
{ value: "responses", label: "Responses" },
|
||||
{ value: "anthropic_messages", label: "Anthropic Messages" },
|
||||
];
|
||||
|
||||
export function ProviderAPIControl({ value, onChange }: {
|
||||
value: ModelAPIConfig | null;
|
||||
onChange: (api: ModelAPIConfig) => void;
|
||||
@@ -33,17 +28,17 @@ export function ProviderAPIControl({ value, onChange }: {
|
||||
<legend className="px-1 text-[12px] font-medium text-muted-foreground">
|
||||
{t("settings.providers.supportedAPIs")}
|
||||
</legend>
|
||||
{APIS.map((api) => {
|
||||
const checked = supported.includes(api.value);
|
||||
{MODEL_REQUEST_APIS.map((api) => {
|
||||
const checked = supported.includes(api);
|
||||
return (
|
||||
<div key={api.value} className="flex items-center justify-between gap-3 text-[13px]">
|
||||
<span>{api.label}</span>
|
||||
<div key={api} className="flex items-center justify-between gap-3 text-[13px]">
|
||||
<span>{REQUEST_API_LABELS[api]}</span>
|
||||
<ToggleButton
|
||||
label={api.label}
|
||||
label={REQUEST_API_LABELS[api]}
|
||||
checked={checked}
|
||||
disabled={checked && supported.length === 1}
|
||||
onChange={(enabled) => {
|
||||
const next = enabled ? [...supported, api.value] : supported.filter((item) => item !== api.value);
|
||||
const next = enabled ? [...supported, api] : supported.filter((item) => item !== api);
|
||||
onChange({ supported_apis: next, preferred_api: next.includes(preferred) ? preferred : next[0] });
|
||||
}}
|
||||
/>
|
||||
@@ -62,8 +57,8 @@ export function ProviderAPIControl({ value, onChange }: {
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{APIS.filter((api) => supported.includes(api.value)).map((api) => (
|
||||
<SelectItem key={api.value} value={api.value}>{api.label}</SelectItem>
|
||||
{MODEL_REQUEST_APIS.filter((api) => supported.includes(api)).map((api) => (
|
||||
<SelectItem key={api} value={api}>{REQUEST_API_LABELS[api]}</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import type { ModelAPIConfig, ModelRequestAPI, SettingsPayload } from "@/lib/types";
|
||||
|
||||
export const MODEL_REQUEST_APIS = ["chat_completions", "responses", "anthropic_messages"] as const;
|
||||
|
||||
export const REQUEST_API_LABELS: Record<ModelRequestAPI, string> = {
|
||||
chat_completions: "Chat Completions",
|
||||
responses: "Responses",
|
||||
anthropic_messages: "Anthropic Messages",
|
||||
};
|
||||
|
||||
export function modelAPIConfigurable(provider: SettingsPayload["providers"][number] | undefined): boolean {
|
||||
if (Array.isArray(provider?.request_apis)) {
|
||||
const supported = provider.request_apis;
|
||||
return MODEL_REQUEST_APIS.filter((api) => supported.includes(api)).length > 1;
|
||||
}
|
||||
return provider?.model_api_configurable === true;
|
||||
}
|
||||
|
||||
export function preferredModelAPI(api: ModelAPIConfig | null | undefined): ModelRequestAPI | "auto" {
|
||||
return api?.preferred_api ?? api?.supported_apis[0] ?? "auto";
|
||||
}
|
||||
|
||||
export function modelAPISelection(api: ModelAPIConfig | null | undefined): string {
|
||||
const preferred = preferredModelAPI(api);
|
||||
if (preferred === "responses" && api?.supported_apis.includes("chat_completions")) {
|
||||
return "prefer_responses";
|
||||
}
|
||||
if (preferred === "chat_completions" && api?.supported_apis.includes("responses")) {
|
||||
return "prefer_chat";
|
||||
}
|
||||
return preferred;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
import { fetchAutomaticModelAPI } from "@/lib/api";
|
||||
import type { AutomaticModelAPIPayload, SettingsPayload } from "@/lib/types";
|
||||
|
||||
export function useAutomaticModelAPI({
|
||||
token, provider, model, reasoningEffort, providers, supported, editorOpen,
|
||||
}: {
|
||||
token: string;
|
||||
provider: string;
|
||||
model: string;
|
||||
reasoningEffort: string;
|
||||
providers: SettingsPayload["providers"];
|
||||
supported: boolean;
|
||||
editorOpen: boolean;
|
||||
}): AutomaticModelAPIPayload | null {
|
||||
const scope = JSON.stringify([token, provider, model, reasoningEffort, providers]);
|
||||
const [preview, setPreview] = useState<{
|
||||
scope: string;
|
||||
result: AutomaticModelAPIPayload;
|
||||
} | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editorOpen || !supported || !model.trim()) return;
|
||||
let cancelled = false;
|
||||
const timer = window.setTimeout(() => {
|
||||
void fetchAutomaticModelAPI(token, provider, model, reasoningEffort)
|
||||
.then((result) => {
|
||||
if (!cancelled) setPreview({ scope, result });
|
||||
})
|
||||
.catch(() => {
|
||||
if (!cancelled) setPreview(null);
|
||||
});
|
||||
}, 150);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
window.clearTimeout(timer);
|
||||
};
|
||||
}, [editorOpen, supported, scope, token, provider, model, reasoningEffort]);
|
||||
|
||||
return supported && preview?.scope === scope ? preview.result : null;
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import type {
|
||||
PendingRestartSections,
|
||||
} from "@/components/settings/contracts";
|
||||
import { agentDraftFromPayload } from "@/components/settings/models/ModelsSettings";
|
||||
import { modelAPISelection } from "@/components/settings/models/ModelAPIControl";
|
||||
import { modelAPISelection } from "@/components/settings/models/modelAPI";
|
||||
import {
|
||||
CUSTOM_PROVIDER_CREATION_KEY,
|
||||
providerFormFromRow,
|
||||
|
||||
@@ -20,7 +20,7 @@ import type {
|
||||
SettingsSectionKey,
|
||||
} from "@/components/settings/contracts";
|
||||
import { agentDraftFromPayload } from "@/components/settings/models/ModelsSettings";
|
||||
import { modelAPISelection } from "@/components/settings/models/ModelAPIControl";
|
||||
import { modelAPISelection } from "@/components/settings/models/modelAPI";
|
||||
import { useModelSettingsActions } from "@/components/settings/models/useModelSettingsActions";
|
||||
import {
|
||||
useProviderFormsSync,
|
||||
|
||||
Reference in new issue
Block a user