From cc57c35014ba9b45bd84590cca24bae4c993d3ed Mon Sep 17 00:00:00 2001 From: Xubin Ren <52506698+Re-bin@users.noreply.github.com> Date: Fri, 13 Nov 2026 10:37:56 +0800 Subject: [PATCH] feat(providers): add ShengSuanYun gateway --- docs/provider-cookbook.md | 30 +++++ nanobot/config/schema.py | 1 + nanobot/providers/registry.py | 13 +++ tests/providers/test_shengsuanyun_provider.py | 106 ++++++++++++++++++ .../settings/models/ProviderSearchList.tsx | 1 + .../settings/shared/ModelControls.tsx | 1 + webui/src/lib/provider-brand.ts | 3 + webui/src/lib/provider-icons.ts | 1 + webui/src/tests/provider-icon.test.tsx | 2 +- webui/src/tests/settings-providers.test.tsx | 12 ++ 10 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 tests/providers/test_shengsuanyun_provider.py diff --git a/docs/provider-cookbook.md b/docs/provider-cookbook.md index 876521a94..9e5af1b5a 100644 --- a/docs/provider-cookbook.md +++ b/docs/provider-cookbook.md @@ -15,6 +15,7 @@ Match the recipe to the credential or endpoint you already have: | What you have | Recipe | Must match | |---|---|---| | A gateway key and model IDs that include a model family path, such as `provider/model-name` | [OpenRouter Gateway](#recipe-openrouter-gateway) | API key, provider config key, preset provider, and gateway model ID | +| A ShengSuanYun (胜算云) API key | [ShengSuanYun](#recipe-shengsuanyun) | API key, `shengsuanyun` provider, and the full model ID | | An OpenCode Zen or Go key | [OpenCode Zen or Go](#recipe-opencode-zen-or-go) | `OPENCODE_API_KEY`, the Zen/Go provider key, and a model ID from the matching OpenCode endpoint | | An OpenAI platform API key and OpenAI model ID | [OpenAI Direct](#recipe-openai-direct) | `OPENAI_API_KEY`, `provider: "openai"`, and an OpenAI model available to that account | | An Anthropic API key and Anthropic model ID | [Anthropic Direct](#recipe-anthropic-direct) | `ANTHROPIC_API_KEY`, `provider: "anthropic"`, and a non-gateway model ID | @@ -104,6 +105,35 @@ nanobot agent -m "Hello!" If this fails with `401` or `unauthorized`, check that `OPENROUTER_API_KEY` is visible in the same terminal or service that starts nanobot. If it fails with `model not found`, choose a model ID that OpenRouter lists for your account. +## Recipe: ShengSuanYun + +In **Settings → Models → New preset**, add **ShengSuanYun**, enter your API key, +and select an available model. Its default API endpoint is +`https://router.shengsuanyun.com/api/v1`. Model IDs include their upstream provider, +for example `deepseek/deepseek-v4-flash`; use the exact ID from the model list. +Availability and pricing depend on your ShengSuanYun account. +See the [official API documentation](https://lean.shengsuanyun.com/openai-api). + +
+Manual configuration + +```json +{ + "providers": { + "shengsuanyun": { "apiKey": "${SHENGSUANYUN_API_KEY}" } + }, + "modelPresets": { + "primary": { + "provider": "shengsuanyun", + "model": "deepseek/deepseek-v4-flash" + } + }, + "agents": { "defaults": { "modelPreset": "primary" } } +} +``` + +
+ ## Recipe: OpenCode Zen or Go In **Settings → Models → New preset**, choose OpenCode Zen or OpenCode Go to match your account, add its credential, select a model from that endpoint, and save. Select the preset in chat. diff --git a/nanobot/config/schema.py b/nanobot/config/schema.py index 62db8a6f8..9d42cf46c 100644 --- a/nanobot/config/schema.py +++ b/nanobot/config/schema.py @@ -324,6 +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) 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 f1e8c584d..8dec4c4da 100644 --- a/nanobot/providers/registry.py +++ b/nanobot/providers/registry.py @@ -281,6 +281,19 @@ 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). + ProviderSpec( + name="shengsuanyun", + keywords=("shengsuanyun",), + env_key="SHENGSUANYUN_API_KEY", + request_apis=("chat_completions", "responses"), + display_name="ShengSuanYun", + 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",), + ), # 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. ProviderSpec( diff --git a/tests/providers/test_shengsuanyun_provider.py b/tests/providers/test_shengsuanyun_provider.py new file mode 100644 index 000000000..ca6678afb --- /dev/null +++ b/tests/providers/test_shengsuanyun_provider.py @@ -0,0 +1,106 @@ +import json + +import httpx +import pytest +from openai import AsyncOpenAI + +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 + + +def _config(model: str = "deepseek/deepseek-v4-flash") -> Config: + return Config.model_validate({ + "providers": {"shengsuanyun": {"apiKey": "test-key"}}, + "agents": {"defaults": {"provider": "shengsuanyun", "model": model}}, + }) + + +def test_catalog_uses_gateway_credentials_and_preserves_model_ids() -> None: + def get(url: str, **kwargs: object) -> httpx.Response: + assert url == "https://router.shengsuanyun.com/api/v1/models" + assert kwargs["headers"] == { + "Accept": "application/json", "Authorization": "Bearer test-key", + } + 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) + 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") + assert row["configured"] is True + assert row["default_api_base"] == "https://router.shengsuanyun.com/api/v1" + assert "test-key" not in str(payload) + + +def test_explicit_gateway_prefix_routes_even_with_another_provider_configured() -> None: + config = Config.model_validate({ + "providers": { + "shengsuanyun": {"apiKey": "test-key"}, + "deepseek": {"apiKey": "other-key"}, + }, + "agents": {"defaults": {"model": "shengsuanyun/deepseek/deepseek-v4-flash"}}, + }) + assert config.get_provider_name(config.agents.defaults.model) == "shengsuanyun" + assert config.get_api_key(config.agents.defaults.model) == "test-key" + + +@pytest.mark.parametrize("model", [ + "deepseek/deepseek-v4-flash", "shengsuanyun/deepseek/deepseek-v4-flash", +]) +async def test_chat_and_stream_route_model_and_tools(model: str) -> None: + requests = [] + + def handler(request: httpx.Request) -> httpx.Response: + requests.append(json.loads(request.content)) + assert str(request.url) == "https://router.shengsuanyun.com/api/v1/chat/completions" + assert request.headers["Authorization"] == "Bearer test-key" + assert requests[-1]["model"] == "deepseek/deepseek-v4-flash" + assert requests[-1]["tools"][0]["function"]["name"] == "weather" + if requests[-1].get("stream"): + events = [ + {"id": "1", "object": "chat.completion.chunk", "created": 0, + "model": model, "choices": [{"index": 0, "delta": {"content": "Hello"}, + "finish_reason": None}]}, + {"id": "1", "object": "chat.completion.chunk", "created": 0, + "model": model, "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]}, + ] + content = "".join(f"data: {json.dumps(event)}\n\n" for event in events) + "data: [DONE]\n\n" + return httpx.Response(200, text=content, headers={"content-type": "text/event-stream"}) + return httpx.Response(200, json={ + "id": "1", "object": "chat.completion", "created": 0, "model": model, + "choices": [{"index": 0, "finish_reason": "tool_calls", "message": { + "role": "assistant", "content": None, "tool_calls": [{ + "id": "call_1", "type": "function", + "function": {"name": "weather", "arguments": '{"city":"Shanghai"}'}, + }], + }}], + }) + + provider = make_provider(_config(model)) + assert isinstance(provider, OpenAICompatProvider) + async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as http: + provider._client = AsyncOpenAI(api_key="test-key", base_url=provider._effective_base, + http_client=http, max_retries=0) + tools = [{"type": "function", "function": {"name": "weather", "parameters": {"type": "object"}}}] + result = await provider.chat([{"role": "user", "content": "Weather?"}], tools=tools) + assert result.tool_calls[0].name == "weather" + assert result.tool_calls[0].arguments == {"city": "Shanghai"} + deltas = [] + + async def on_delta(text: str) -> None: + deltas.append(text) + + result = await provider.chat_stream([{"role": "user", "content": "Hi"}], tools=tools, + on_content_delta=on_delta) + assert result.content == "Hello" + assert deltas == ["Hello"] + assert len(requests) == 2 diff --git a/webui/src/components/settings/models/ProviderSearchList.tsx b/webui/src/components/settings/models/ProviderSearchList.tsx index e7bff56f8..838be89fa 100644 --- a/webui/src/components/settings/models/ProviderSearchList.tsx +++ b/webui/src/components/settings/models/ProviderSearchList.tsx @@ -27,6 +27,7 @@ const PROVIDER_SEARCH_ALIASES: Record = { minimax: "海螺 稀宇", minimax_anthropic: "海螺 稀宇", siliconflow: "硅基流动 矽基流動 硅基流動", + shengsuanyun: "胜算云 勝算雲", stepfun: "阶跃星辰 階躍星辰", xiaomi_mimo: "小米 mimo", longcat: "美团 美團", diff --git a/webui/src/components/settings/shared/ModelControls.tsx b/webui/src/components/settings/shared/ModelControls.tsx index bbb2b8f7a..d5561567b 100644 --- a/webui/src/components/settings/shared/ModelControls.tsx +++ b/webui/src/components/settings/shared/ModelControls.tsx @@ -39,6 +39,7 @@ const DEFERRED_MODEL_LIST_PROVIDERS = new Set([ "orcarouter", "ovms", "siliconflow", + "shengsuanyun", "vllm", "volcengine", "volcengine_coding_plan", diff --git a/webui/src/lib/provider-brand.ts b/webui/src/lib/provider-brand.ts index c1aaa53a2..8934b7fbb 100644 --- a/webui/src/lib/provider-brand.ts +++ b/webui/src/lib/provider-brand.ts @@ -228,6 +228,9 @@ const PROVIDER_BRANDS: Record = { initials: "SX", }, siliconflow: brand("siliconflow.cn", "#111827", "SF"), + shengsuanyun: { ...brand("shengsuanyun.com", "#5742EE", "SSY", [ + "https://www.shengsuanyun.com/svg/logo-C_e87Vaj.svg", + ]), logoLayout: "tile" }, skywork: brand("skywork.ai", "#5B5BF6", "SW"), stepfun: brand("stepfun.com", "#2F6BFF", "SF", [ "https://www.stepfun.com/step_favicon.svg", diff --git a/webui/src/lib/provider-icons.ts b/webui/src/lib/provider-icons.ts index d003127d2..01ccb109e 100644 --- a/webui/src/lib/provider-icons.ts +++ b/webui/src/lib/provider-icons.ts @@ -20,6 +20,7 @@ export const PROVIDER_ICONS: Record = { gemini: Gem, mistral: Orbit, siliconflow: Layers, + shengsuanyun: 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 cbdb3515c..9dd91aaf1 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", "tavily", "xiaomi_mimo", "zhipu", + "modelscope", "moonshot", "olostep", "shengsuanyun", "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 e882e8984..d9a12b8e3 100644 --- a/webui/src/tests/settings-providers.test.tsx +++ b/webui/src/tests/settings-providers.test.tsx @@ -21,6 +21,18 @@ async function chooseProviderToConfigure(label: string) { describe("Settings providers", () => { installSettingsViewTestHooks(); + 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 }]; + 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(); + expect(screen.getByPlaceholderText("Enter API key")).toBeInTheDocument(); + }); + it("manages providers unused by presets and returns focus through the manager", async () => { const user = userEvent.setup(); const payload = settingsPayload();