From eb1df4d7b1142f745f35bb0c403fb0228685690c Mon Sep 17 00:00:00 2001 From: Xubin Ren <52506698+Re-bin@users.noreply.github.com> Date: Sat, 14 Nov 2026 11:36:59 +0800 Subject: [PATCH] fix(providers): preserve existing ShengSuanYun connections --- docs/configuration.md | 1 + nanobot/config/schema.py | 2 +- nanobot/providers/registry.py | 12 +- tests/providers/test_shengsuanyun_provider.py | 106 ++++++++++++++++-- .../settings/models/ProviderSearchList.tsx | 2 +- .../settings/shared/ModelControls.tsx | 2 +- webui/src/lib/provider-brand.ts | 2 +- webui/src/lib/provider-icons.ts | 2 +- webui/src/tests/provider-icon.test.tsx | 2 +- webui/src/tests/settings-providers.test.tsx | 6 +- 10 files changed, 110 insertions(+), 27 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 5d055fbe9..fbbab093b 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -304,6 +304,7 @@ A Base URL is the service address nanobot sends model requests to. Change it onl > - **Kimi Coding Plan**: Use `providers.kimiCoding` with `provider: "kimi_coding"` for Kimi's dedicated Anthropic Messages API endpoint. The endpoint requires a Claude-compatible `User-Agent`; nanobot sends `claude-code/0.1.0` by default, and you can override it with `extraHeaders.User-Agent` if your account requires a different value. > - **VolcEngine / BytePlus Coding Plan**: Subscription endpoints are configured through dedicated providers `volcengineCodingPlan` or `byteplusCodingPlan`, separate from the pay-per-use `volcengine` / `byteplus` providers. > - **OpenCode Zen / Go**: `providers.opencode` (canonical Zen), the legacy-compatible `providers.opencodeZen`, and `providers.opencodeGo` use the same `OPENCODE_API_KEY`, but route to different OpenCode gateways. These providers use OpenCode's OpenAI-compatible `chat/completions` endpoints; choose model IDs from that endpoint family. +> - **ShengSuanYun (胜算云)**: The built-in connection uses `providers.shengsuanyunRouter` and `provider: "shengsuanyun_router"`. Keep the model's namespace, for example `deepseek/deepseek-v4-flash`. Chat Completions is the default; select Responses in a preset's **Advanced options** only for a model that [ShengSuanYun lists as supporting it](https://lean.shengsuanyun.com/openai-api). Existing custom connections named `shengsuanyun` continue to work as configured. > - **Zhipu Coding Plan**: If you're on Zhipu's coding plan, open the zhipu connection in **Settings → Models** and set its base URL to `https://open.bigmodel.cn/api/coding/paas/v4`. > - **Alibaba Cloud BaiLian**: The built-in `dashscope` provider already defaults to `https://dashscope.aliyuncs.com/compatible-mode/v1`; no endpoint override is needed for that service. > - **ModelScope**: The built-in `modelscope` provider already defaults to `https://api-inference.modelscope.cn/v1`; no endpoint override is needed for that service. diff --git a/nanobot/config/schema.py b/nanobot/config/schema.py index 9d42cf46c..e5a8a2bfb 100644 --- a/nanobot/config/schema.py +++ b/nanobot/config/schema.py @@ -324,7 +324,7 @@ class ProvidersConfig(Base): openai: ProviderConfig = Field(default_factory=ProviderConfig) openrouter: ProviderConfig = Field(default_factory=ProviderConfig) orcarouter: ProviderConfig = Field(default_factory=ProviderConfig) - shengsuanyun: ProviderConfig = Field(default_factory=ProviderConfig) + shengsuanyun_router: ProviderConfig = Field(default_factory=ProviderConfig) assemblyai: ProviderConfig = Field(default_factory=ProviderConfig) huggingface: ProviderConfig = Field(default_factory=ProviderConfig) skywork: ProviderConfig = Field(default_factory=ProviderConfig) diff --git a/nanobot/providers/registry.py b/nanobot/providers/registry.py index 8dec4c4da..4ef35bedc 100644 --- a/nanobot/providers/registry.py +++ b/nanobot/providers/registry.py @@ -281,18 +281,18 @@ PROVIDERS: tuple[ProviderSpec, ...] = ( detect_by_base_keyword="orcarouter", default_api_base="https://api.orcarouter.ai/v1", ), - # ShengSuanYun model IDs include the upstream provider (e.g. deepseek/model). + # Keep the Router ID separate from existing custom connections named "shengsuanyun". + # Upstream model IDs retain their provider namespace (e.g. deepseek/model). ProviderSpec( - name="shengsuanyun", - keywords=("shengsuanyun",), + name="shengsuanyun_router", + keywords=("shengsuanyun_router",), env_key="SHENGSUANYUN_API_KEY", request_apis=("chat_completions", "responses"), - display_name="ShengSuanYun", + display_name="ShengSuanYun Router", backend="openai_compat", is_gateway=True, - detect_by_base_keyword="shengsuanyun.com", default_api_base="https://router.shengsuanyun.com/api/v1", - strip_model_prefixes=("shengsuanyun",), + strip_model_prefixes=("shengsuanyun_router",), ), # Eden AI: OpenAI-compatible gateway. Models use the "provider/model" # naming scheme (e.g. "anthropic/claude-sonnet-4-5"); the full id is sent upstream. diff --git a/tests/providers/test_shengsuanyun_provider.py b/tests/providers/test_shengsuanyun_provider.py index ca6678afb..46827f40b 100644 --- a/tests/providers/test_shengsuanyun_provider.py +++ b/tests/providers/test_shengsuanyun_provider.py @@ -1,19 +1,27 @@ import json +from pathlib import Path import httpx import pytest from openai import AsyncOpenAI +from nanobot.config.loader import load_config, save_config from nanobot.config.schema import Config from nanobot.providers.factory import make_provider from nanobot.providers.openai_compat_provider import OpenAICompatProvider -from nanobot.webui.settings_models import model_settings_payload, provider_models_payload +from nanobot.providers.routing import resolve_provider_route +from nanobot.webui.settings_models import ( + create_model_configuration, + model_settings_payload, + provider_models_payload, + update_provider_settings, +) def _config(model: str = "deepseek/deepseek-v4-flash") -> Config: return Config.model_validate({ - "providers": {"shengsuanyun": {"apiKey": "test-key"}}, - "agents": {"defaults": {"provider": "shengsuanyun", "model": model}}, + "providers": {"shengsuanyunRouter": {"apiKey": "test-key"}}, + "agents": {"defaults": {"provider": "shengsuanyun_router", "model": model}}, }) @@ -26,35 +34,66 @@ def test_catalog_uses_gateway_credentials_and_preserves_model_ids() -> None: return httpx.Response(200, json={"data": [{"id": "deepseek/deepseek-v4-flash"}]}, request=httpx.Request("GET", url)) - payload = provider_models_payload(_config(), {"provider": ["shengsuanyun"]}, http_get=get) + payload = provider_models_payload(_config(), {"provider": ["shengsuanyun_router"]}, http_get=get) assert payload["status"] == "available" assert payload["models"][0]["id"] == "deepseek/deepseek-v4-flash" def test_settings_exposes_gateway_without_exposing_api_key() -> None: - payload = model_settings_payload(_config(), oauth_status=lambda _: { - "configured": False, "account": None, "expires_at": None, "login_supported": False, - }) - row = next(row for row in payload["providers"] if row["name"] == "shengsuanyun") + payload = model_settings_payload(_config(), oauth_status=_not_signed_in) + row = next(row for row in payload["providers"] if row["name"] == "shengsuanyun_router") assert row["configured"] is True assert row["default_api_base"] == "https://router.shengsuanyun.com/api/v1" assert "test-key" not in str(payload) +def _not_signed_in(_: object) -> dict[str, object]: + return {"configured": False, "account": None, "expires_at": None, "login_supported": False} + + +def test_existing_custom_shengsuanyun_connection_keeps_anthropic_route(tmp_path: Path) -> None: + path = tmp_path / "config.json" + path.write_text(json.dumps({ + "providers": {"shengsuanyun": { + "apiKey": "old-key", + "apiBase": "https://router.shengsuanyun.com/api", + "api": {"supportedApis": ["anthropic_messages"], "preferredApi": "anthropic_messages"}, + }}, + "agents": {"defaults": {"provider": "shengsuanyun", "model": "anthropic/claude-sonnet-4-6"}}, + }), encoding="utf-8") + + config = load_config(path) + route = resolve_provider_route(config, preset=config.resolve_preset()) + assert route.backend == "anthropic" + assert config.get_api_base() == "https://router.shengsuanyun.com/api" + row = next(item for item in model_settings_payload( + config, oauth_status=_not_signed_in, + )["providers"] if item["name"] == "shengsuanyun") + assert row["is_custom"] and row["provider_api_configurable"] + + assert update_provider_settings(config, { + "provider": ["shengsuanyun"], "api_key": ["rotated-key"], "display_name": [row["label"]], + })[0] + save_config(config, path) + reloaded = load_config(path) + assert reloaded.get_api_key() == "rotated-key" + assert resolve_provider_route(reloaded, preset=reloaded.resolve_preset()).backend == "anthropic" + + def test_explicit_gateway_prefix_routes_even_with_another_provider_configured() -> None: config = Config.model_validate({ "providers": { - "shengsuanyun": {"apiKey": "test-key"}, + "shengsuanyunRouter": {"apiKey": "test-key"}, "deepseek": {"apiKey": "other-key"}, }, - "agents": {"defaults": {"model": "shengsuanyun/deepseek/deepseek-v4-flash"}}, + "agents": {"defaults": {"model": "shengsuanyun_router/deepseek/deepseek-v4-flash"}}, }) - assert config.get_provider_name(config.agents.defaults.model) == "shengsuanyun" + assert config.get_provider_name(config.agents.defaults.model) == "shengsuanyun_router" assert config.get_api_key(config.agents.defaults.model) == "test-key" @pytest.mark.parametrize("model", [ - "deepseek/deepseek-v4-flash", "shengsuanyun/deepseek/deepseek-v4-flash", + "deepseek/deepseek-v4-flash", "shengsuanyun_router/deepseek/deepseek-v4-flash", ]) async def test_chat_and_stream_route_model_and_tools(model: str) -> None: requests = [] @@ -104,3 +143,46 @@ async def test_chat_and_stream_route_model_and_tools(model: str) -> None: assert result.content == "Hello" assert deltas == ["Hello"] assert len(requests) == 2 + + +async def test_saved_responses_preset_uses_router_with_full_model_id( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, +) -> None: + config = Config.model_validate({"providers": {"openai": {"apiKey": "other-key"}}}) + assert update_provider_settings(config, { + "provider": ["shengsuanyun_router"], "api_key": ["test-key"], + })[0] + name = create_model_configuration(config, { + "name": ["Router GPT"], + "provider": ["shengsuanyun_router"], + "model": ["openai/gpt-5.4"], + "api": ['{"supportedApis":["responses"],"preferredApi":"responses"}'], + }, oauth_status=_not_signed_in) + path = tmp_path / "config.json" + save_config(config, path) + + def handler(request: httpx.Request) -> httpx.Response: + assert str(request.url) == "https://router.shengsuanyun.com/api/v1/responses" + assert request.headers["Authorization"] == "Bearer test-key" + body = json.loads(request.content) + assert body["model"] == "openai/gpt-5.4" + assert body["input"][0]["content"][0]["text"] == "Hi" + return httpx.Response(200, json={ + "id": "resp_1", "object": "response", "status": "completed", + "output": [{ + "id": "msg_1", "type": "message", "role": "assistant", "status": "completed", + "content": [{"type": "output_text", "text": "Hello", "annotations": []}], + }], + }) + + async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as http: + def create_client(**kwargs): + return AsyncOpenAI(**(kwargs | {"http_client": http})) + + monkeypatch.setattr("nanobot.providers.openai_compat_provider.AsyncOpenAI", create_client) + provider = make_provider(load_config(path), preset_name=name) + try: + response = await provider.chat([{"role": "user", "content": "Hi"}]) + assert response.content == "Hello" + finally: + await provider.aclose() diff --git a/webui/src/components/settings/models/ProviderSearchList.tsx b/webui/src/components/settings/models/ProviderSearchList.tsx index 838be89fa..653b13899 100644 --- a/webui/src/components/settings/models/ProviderSearchList.tsx +++ b/webui/src/components/settings/models/ProviderSearchList.tsx @@ -27,7 +27,7 @@ const PROVIDER_SEARCH_ALIASES: Record = { minimax: "海螺 稀宇", minimax_anthropic: "海螺 稀宇", siliconflow: "硅基流动 矽基流動 硅基流動", - shengsuanyun: "胜算云 勝算雲", + shengsuanyun_router: "胜算云 勝算雲", stepfun: "阶跃星辰 階躍星辰", xiaomi_mimo: "小米 mimo", longcat: "美团 美團", diff --git a/webui/src/components/settings/shared/ModelControls.tsx b/webui/src/components/settings/shared/ModelControls.tsx index d5561567b..23a9b0523 100644 --- a/webui/src/components/settings/shared/ModelControls.tsx +++ b/webui/src/components/settings/shared/ModelControls.tsx @@ -38,8 +38,8 @@ const DEFERRED_MODEL_LIST_PROVIDERS = new Set([ "openrouter", "orcarouter", "ovms", + "shengsuanyun_router", "siliconflow", - "shengsuanyun", "vllm", "volcengine", "volcengine_coding_plan", diff --git a/webui/src/lib/provider-brand.ts b/webui/src/lib/provider-brand.ts index 8934b7fbb..906ef7da0 100644 --- a/webui/src/lib/provider-brand.ts +++ b/webui/src/lib/provider-brand.ts @@ -228,7 +228,7 @@ const PROVIDER_BRANDS: Record = { initials: "SX", }, siliconflow: brand("siliconflow.cn", "#111827", "SF"), - shengsuanyun: { ...brand("shengsuanyun.com", "#5742EE", "SSY", [ + shengsuanyun_router: { ...brand("shengsuanyun.com", "#5742EE", "SSY", [ "https://www.shengsuanyun.com/svg/logo-C_e87Vaj.svg", ]), logoLayout: "tile" }, skywork: brand("skywork.ai", "#5B5BF6", "SW"), diff --git a/webui/src/lib/provider-icons.ts b/webui/src/lib/provider-icons.ts index 01ccb109e..95adf454b 100644 --- a/webui/src/lib/provider-icons.ts +++ b/webui/src/lib/provider-icons.ts @@ -20,7 +20,7 @@ export const PROVIDER_ICONS: Record = { gemini: Gem, mistral: Orbit, siliconflow: Layers, - shengsuanyun: Cloud, + shengsuanyun_router: Cloud, volcengine: Cloud, volcengine_coding_plan: Cloud, byteplus: Cloud, diff --git a/webui/src/tests/provider-icon.test.tsx b/webui/src/tests/provider-icon.test.tsx index 9dd91aaf1..855552883 100644 --- a/webui/src/tests/provider-icon.test.tsx +++ b/webui/src/tests/provider-icon.test.tsx @@ -48,7 +48,7 @@ describe("settings provider icons", () => { it.each([ "aihubmix", "bedrock", "exa", "groq", "lm_studio", "minimax", "minimax_anthropic", - "modelscope", "moonshot", "olostep", "shengsuanyun", "tavily", "xiaomi_mimo", "zhipu", + "modelscope", "moonshot", "olostep", "shengsuanyun_router", "tavily", "xiaomi_mimo", "zhipu", ])( "does not double-frame %s's primary tile, but preserves unknown fallback assets", (provider) => { diff --git a/webui/src/tests/settings-providers.test.tsx b/webui/src/tests/settings-providers.test.tsx index d9a12b8e3..d2d4366ea 100644 --- a/webui/src/tests/settings-providers.test.tsx +++ b/webui/src/tests/settings-providers.test.tsx @@ -24,12 +24,12 @@ describe("Settings providers", () => { it("finds ShengSuanYun by its Chinese name and opens its credential form", async () => { const user = userEvent.setup(); const payload = settingsPayload(); - payload.providers = [{ name: "shengsuanyun", label: "ShengSuanYun", configured: false }]; + payload.providers = [{ name: "shengsuanyun_router", label: "ShengSuanYun Router", configured: false }]; renderProviderManager({ initialSection: "models", initialSettings: payload }); await user.click(screen.getByRole("button", { name: "Add provider" })); await user.type(screen.getByRole("combobox", { name: "Search providers" }), "胜算云"); - await user.click(screen.getByRole("option", { name: "ShengSuanYun" })); - expect(screen.getByRole("dialog", { name: "ShengSuanYun" })).toBeVisible(); + await user.click(screen.getByRole("option", { name: "ShengSuanYun Router" })); + expect(screen.getByRole("dialog", { name: "ShengSuanYun Router" })).toBeVisible(); expect(screen.getByPlaceholderText("Enter API key")).toBeInTheDocument(); });