refactor(config)!: retire the old OpenAI API selector (#6615)

Co-authored-by: chengyongru <chengyongru.ai@gmail.com>
This commit is contained in:
Xubin Renandchengyongru authored and GitHub committed 2026-11-11 17:07:18 +08:00
1 parent e3a1af2ed3
commit 5c1083df7f
14 files changed
+112 -141

No files matched your search

+13 -43
View File
@@ -72,60 +72,30 @@ def test_agent_timezones_use_packaged_data_without_system_database() -> None:
assert result.returncode == 0, result.stderr
@pytest.mark.parametrize("field", ["apiType", "api_type"])
@pytest.mark.parametrize("legacy", ["auto", "chat_completions", "responses"])
def test_legacy_provider_api_migrates_and_saves_current_format(tmp_path, field, legacy):
def test_old_installation_defaults_load_without_changing_provider_api(tmp_path):
from nanobot.config.loader import load_config, save_config
path = tmp_path / "config.json"
path.write_text(json.dumps({"providers": {"openai": {"apiKey": "sk-test", field: legacy}}}), encoding="utf-8")
path.write_text(json.dumps({"providers": {
"openai": {"apiType": "auto", "apiKey": "fixture"},
"custom": {"apiType": "auto", "apiBase": "https://example.test/v1"},
}}), encoding="utf-8")
config = load_config(path)
expected = None if legacy == "auto" else {
"supportedApis": [legacy], "preferredApi": legacy,
}
assert config.providers.openai.api is None
assert config.providers.custom.api is None
save_config(config, path)
saved = json.loads(path.read_text(encoding="utf-8"))["providers"]["openai"]
assert saved.get("api") == expected
assert field not in saved
assert load_config(path).providers.openai.api == config.providers.openai.api
assert load_config(path).providers.custom.api_base == "https://example.test/v1"
@pytest.mark.parametrize("api", [None, {"supportedApis": ["chat_completions"]}])
def test_current_provider_api_takes_precedence_over_legacy_input(api):
def test_current_provider_api_stays_authoritative_if_old_key_remains(api):
config = Config.model_validate({"providers": {"openai": {
"apiType": "responses", "api": api,
}}})
if api is None:
assert config.providers.openai.api is None
else:
assert config.providers.openai.api.to_capabilities().preferred_api == "chat_completions"
def test_legacy_provider_api_rejects_invalid_selector():
with pytest.raises(ValueError, match="apiType must be"):
Config.model_validate({"providers": {"openai": {"apiType": "response"}}})
def test_provider_api_type_is_openai_only() -> None:
with pytest.raises(ValueError, match="only supported"):
Config.model_validate({
"providers": {
"custom": {
"apiBase": "https://example.test/v1",
"apiType": "responses",
}
}
})
with pytest.raises(ValueError, match="only supported"):
Config.model_validate({
"providers": {
"my-company-api": {
"apiBase": "https://example.test/v1",
"apiType": "responses",
}
}
})
resolved = config.providers.openai.api
assert (resolved.to_capabilities().preferred_api if resolved else None) == (
"chat_completions" if api else None
)
@pytest.mark.parametrize("provider_name", ["openai-codex", "github-copilot", "lm-studio"])
+16 -10
View File
@@ -707,17 +707,20 @@ async def test_explicit_model_api_overrides_connection_default(bind_transport, p
assert paths == [path]
@pytest.mark.parametrize("legacy,path", [
@pytest.mark.parametrize("selector,path", [
("auto", "/v1/chat/completions"),
("chat_completions", "/v1/chat/completions"),
("responses", "/v1/responses"),
])
async def test_migrated_openai_default_routes_and_allows_model_override(bind_transport, tmp_path, legacy, path):
async def test_openai_connection_api_round_trip_and_model_override(bind_transport, tmp_path, selector, path):
from nanobot.config.loader import load_config, save_config
config_path = tmp_path / "config.json"
config_path.write_text(json.dumps({
"providers": {"openai": {"apiKey": "fixture", "apiType": legacy}},
"providers": {"openai": {
"apiKey": "fixture",
"api": None if selector == "auto" else {"supportedApis": [selector]},
}},
"agents": {"defaults": {"model": "gpt-4o", "provider": "openai"}},
}), encoding="utf-8")
config = load_config(config_path)
@@ -733,9 +736,9 @@ async def test_migrated_openai_default_routes_and_allows_model_override(bind_tra
assert (await provider.chat([{"role": "user", "content": "hello"}])).content == "ok"
assert requests == [path]
assert resolve_automatic_model_api(config, preset=config.resolve_preset())[1] == (
"responses" if legacy == "responses" else "chat_completions"
"responses" if selector == "responses" else "chat_completions"
)
opposite = "chat_completions" if legacy == "responses" else "responses"
opposite = "chat_completions" if selector == "responses" else "responses"
config.model_presets["override"] = ModelPresetConfig(
model="gpt-4o", provider="openai", api=ModelAPIConfig(supported_apis=(opposite,)),
)
@@ -744,18 +747,21 @@ async def test_migrated_openai_default_routes_and_allows_model_override(bind_tra
assert requests[-1] == ("/v1/responses" if opposite == "responses" else "/v1/chat/completions")
@pytest.mark.parametrize("env_field", ["API_TYPE", "APITYPE"])
@pytest.mark.parametrize("legacy,path", [
@pytest.mark.parametrize("selector,path", [
("auto", "/v1/chat/completions"),
("chat_completions", "/v1/chat/completions"),
("responses", "/v1/responses"),
])
async def test_legacy_openai_environment_selector_keeps_request_api(
bind_transport, tmp_path, monkeypatch, env_field, legacy, path,
async def test_openai_environment_api_object_selects_request_api(
bind_transport, tmp_path, monkeypatch, selector, path,
):
from nanobot.config.loader import load_config
monkeypatch.setenv(f"NANOBOT_PROVIDERS__OPENAI__{env_field}", legacy)
if selector != "auto":
monkeypatch.setenv(
"NANOBOT_PROVIDERS__OPENAI__API",
json.dumps({"supportedApis": [selector]}),
)
config_path = tmp_path / "config.json"
config_path.write_text(json.dumps({
"providers": {"openai": {"apiKey": "fixture", "apiBase": "https://tenant.test/v1"}},
+3 -4
View File
@@ -998,7 +998,7 @@ def test_update_provider_settings_persists_provider_specific_advanced_options(
update_provider_settings(
{
"provider": ["openai"],
"apiType": ["responses"],
"api": [json.dumps({"supportedApis": ["responses"]})],
"use_proxy": ["true"],
"extraHeaders": [json.dumps({"X-Trace": "enabled"})],
"extraBody": [json.dumps({"service_tier": "priority"})],
@@ -2852,13 +2852,12 @@ def test_custom_connection_cannot_remove_api_used_by_legacy_or_inline_model(tmp_
assert config_path.read_bytes() == before
@pytest.mark.parametrize("field", ["apiType", "api_type"])
def test_legacy_openai_settings_can_return_to_auto(monkeypatch, tmp_path, field):
def test_openai_settings_can_return_to_auto(monkeypatch, tmp_path):
config_path = tmp_path / "config.json"
config = Config.model_validate({"providers": {"openai": {
"apiKey": "fixture", "api": {"supportedApis": ["responses"]},
}}})
save_config(config, config_path)
monkeypatch.setattr("nanobot.config.loader._current_config_path", config_path)
update_provider_settings({"provider": ["openai"], field: ["auto"]})
update_provider_settings({"provider": ["openai"], "api": ["null"]})
assert load_config(config_path).providers.openai.api is None
+19
View File
@@ -764,6 +764,25 @@ async def test_model_update_route_forwards_session_rename_dependency(monkeypatch
refresh_runtime_config.assert_called_once_with()
@pytest.mark.asyncio
async def test_older_client_does_not_partially_update_provider_settings(tmp_path) -> None:
config_path = tmp_path / "config.json"
before = '{"providers":{"openai":{"apiKey":"fixture","api":{"supportedApis":["responses"]}}}}'
config_path.write_text(before, encoding="utf-8")
refresh = MagicMock()
router = _router(config_path=config_path, refresh_runtime_config=refresh)
path = "/api/settings/provider/update"
request = _mutation_request(path, {"provider": "openai", "apiType": "auto", "apiBase": "https://example.test/v1"})
response = await router.dispatch(None, request, path)
assert response is not None
assert response.status_code == 400
assert "Update the client" in json.loads(response.body)["error"]
assert config_path.read_text(encoding="utf-8") == before
refresh.assert_not_called()
@pytest.mark.asyncio
async def test_settings_get_mutation_route_is_method_not_allowed() -> None:
path = "/api/settings/provider/update"