feat(models): declare provider APIs and simplify preset configuration

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-10 01:33:02 +08:00
1 parent 592f0559a9
commit 994ce51502
26 files changed
+444 -123

No files matched your search

+2 -1
View File
@@ -252,7 +252,8 @@ def _gemini_provider() -> OpenAICompatProvider:
with patch("nanobot.providers.openai_compat_provider.AsyncOpenAI"):
return OpenAICompatProvider(
spec=ProviderSpec(
name="gemini", keywords=("gemini",), env_key="GEMINI_API_KEY"
name="gemini", keywords=("gemini",), env_key="GEMINI_API_KEY",
request_apis=("chat_completions", "responses"),
)
)
+13 -2
View File
@@ -7,8 +7,8 @@ import pytest
from openai import AsyncOpenAI
from pydantic import ValidationError
from nanobot.config.schema import Config, InlineFallbackConfig, ModelAPIConfig
from nanobot.providers.factory import make_provider, provider_signature
from nanobot.config.schema import Config, InlineFallbackConfig, ModelAPIConfig, ModelPresetConfig
from nanobot.providers.factory import make_provider, provider_signature, validate_provider_setup
from nanobot.providers.openai_compat_provider import _RESPONSES_FAILURE_THRESHOLD
@@ -171,6 +171,17 @@ def test_preset_api_preference_must_be_supported():
ModelAPIConfig.model_validate({"supportedApis": ["chat_completions"], "preferredApi": "responses"})
def test_fixed_provider_validates_preset_api_before_loading_client():
config = Config()
preset = ModelPresetConfig(
provider="openai_codex", model="gpt-6-astra", api=ModelAPIConfig(supported_apis=("responses",)),
)
validate_provider_setup(config, preset=preset)
preset.api = ModelAPIConfig(supported_apis=("responses", "chat_completions"))
with pytest.raises(ValueError, match="OpenAI Codex.*does not support chat_completions"):
validate_provider_setup(config, preset=preset)
@pytest.mark.parametrize("apis,preferred", [
(("responses",), "responses"),
(("chat_completions",), "chat_completions"),
@@ -28,6 +28,7 @@ async def test_openai_compat_provider_sets_timeout_on_local_http_client() -> Non
name="local",
keywords=(),
env_key="",
request_apis=("chat_completions", "responses"),
is_local=True,
default_api_base="http://127.0.0.1:11434/v1",
)
@@ -64,6 +64,7 @@ def test_responses_behavior_is_declared_by_capabilities(provider):
name="example",
keywords=("example",),
env_key="EXAMPLE_API_KEY",
request_apis=("chat_completions", "responses"),
responses=ResponsesCapabilities(
models=("example-o3",),
reasoning_replay="plaintext",
@@ -239,6 +240,7 @@ def _deepseek_provider(provider):
name="deepseek",
keywords=("deepseek",),
env_key="DEEPSEEK_API_KEY",
request_apis=("chat_completions", "responses"),
responses=ResponsesCapabilities(
models=("deepseek-v4-flash",),
reasoning_replay="plaintext",
@@ -15,6 +15,7 @@ _STEPFUN_SPEC = ProviderSpec(
name="stepfun",
keywords=("stepfun", "step"),
env_key="STEPFUN_API_KEY",
request_apis=("chat_completions", "responses"),
display_name="Step Fun",
backend="openai_compat",
default_api_base="https://api.stepfun.com/v1",
+23 -1
View File
@@ -764,6 +764,10 @@ def test_custom_preset_api_round_trip_and_reset(tmp_path, monkeypatch):
assert providers["tenant"]["model_api_configurable"] is True
assert providers["github_copilot"]["model_api_configurable"] is True
assert providers["anthropic"]["model_api_configurable"] is False
assert providers["tenant"]["request_apis"] == ["chat_completions", "responses"]
assert providers["openai_codex"]["request_apis"] == ["responses"]
assert providers["anthropic"]["request_apis"] == ["anthropic_messages"]
assert providers["bedrock"]["request_apis"] == ["bedrock_converse"]
saved = load_config(config_path)
assert saved.model_presets["reasoning"].api.to_capabilities().preferred_api == "responses"
assert json.loads(config_path.read_text(encoding="utf-8"))["modelPresets"]["reasoning"]["api"] == {
@@ -784,7 +788,7 @@ def test_custom_preset_api_round_trip_and_reset(tmp_path, monkeypatch):
@pytest.mark.parametrize("provider,api,error", [
("openai", {"supportedApis": ["chat_completions"], "preferredApi": "responses"}, "api must declare"),
("anthropic", {"supportedApis": ["responses"]}, "provider does not support"),
("anthropic", {"supportedApis": ["responses"]}, "does not support"),
])
def test_create_preset_rejects_invalid_api_without_saving(tmp_path, monkeypatch, provider, api, error):
config_path = tmp_path / "config.json"
@@ -801,6 +805,24 @@ def test_create_preset_rejects_invalid_api_without_saving(tmp_path, monkeypatch,
assert load_config(config_path).model_presets == {}
def test_fixed_responses_provider_rejects_chat_without_saving(tmp_path, monkeypatch):
config_path = tmp_path / "config.json"
config = Config.model_validate({
"modelPresets": {"codex": {"provider": "openai_codex", "model": "gpt-6-astra"}},
})
save_config(config, config_path)
monkeypatch.setattr("nanobot.config.loader._current_config_path", config_path)
with pytest.raises(WebUISettingsError, match="OpenAI Codex.*does not support chat_completions"):
update_model_configuration({
"name": ["codex"], "api": [json.dumps({"supportedApis": ["chat_completions"]})],
})
assert load_config(config_path).model_presets["codex"].api is None
update_model_configuration({
"name": ["codex"], "api": [json.dumps({"supportedApis": ["responses"]})],
})
assert load_config(config_path).model_presets["codex"].api.supported_apis == ("responses",)
def test_legacy_migration_preserves_primary_and_fallback_apis(tmp_path, monkeypatch):
config_path = tmp_path / "config.json"
config = Config.model_validate({"providers": {