Register ShengSuanYun as a standard provider
This commit is contained in:
1 parent
0b9e546672
commit
259c05e747
17 files changed
+265
-503
No files matched your search
@@ -2,13 +2,11 @@ from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from typing import Any, cast
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from prompt_toolkit.application import create_app_session
|
||||
from prompt_toolkit.input import create_pipe_input
|
||||
from prompt_toolkit.output import DummyOutput
|
||||
from pydantic import BaseModel, Field
|
||||
from typer.testing import CliRunner
|
||||
|
||||
from nanobot.cli import onboard as onboard_wizard
|
||||
from nanobot.cli.onboard import (
|
||||
@@ -21,11 +19,8 @@ from nanobot.cli.onboard import (
|
||||
_input_text,
|
||||
run_onboard,
|
||||
)
|
||||
from nanobot.config import home as instance_home
|
||||
from nanobot.config import loader
|
||||
from nanobot.config.loader import load_config, merge_missing_defaults, save_config
|
||||
from nanobot.config.loader import merge_missing_defaults
|
||||
from nanobot.config.schema import Config, ModelPresetConfig
|
||||
from nanobot.providers.routing import resolve_provider_route
|
||||
from nanobot.utils.helpers import sync_workspace_templates
|
||||
|
||||
|
||||
@@ -902,7 +897,7 @@ class TestMainMenuInteraction:
|
||||
from nanobot.providers.registry import PROVIDERS
|
||||
|
||||
choices = onboard_wizard._get_quick_start_provider_choices()
|
||||
selected_provider_names = {choice for choice in choices.values() if isinstance(choice, str)}
|
||||
selected_provider_names = set(choices.values())
|
||||
expected_provider_names = set()
|
||||
seen_display_names: set[str] = set()
|
||||
for spec in PROVIDERS:
|
||||
@@ -958,7 +953,7 @@ class TestMainMenuInteraction:
|
||||
or True,
|
||||
)
|
||||
|
||||
assert onboard_wizard._configure_quick_start_provider(config) == "primary"
|
||||
assert onboard_wizard._configure_quick_start_provider(config) is True
|
||||
|
||||
assert oauth_calls == [(config, "openai_codex")]
|
||||
assert model_prompts == [
|
||||
@@ -1118,7 +1113,6 @@ class TestMainMenuInteraction:
|
||||
|
||||
def test_quick_start_summary_reports_missing_codex_oauth(self, monkeypatch):
|
||||
config = Config()
|
||||
config.channels.websocket = {"enabled": True}
|
||||
config.model_presets["primary"] = ModelPresetConfig(
|
||||
model="openai-codex/gpt-5.6-sol",
|
||||
provider="openai_codex",
|
||||
@@ -1137,7 +1131,7 @@ class TestMainMenuInteraction:
|
||||
lambda rows, _title: captured.setdefault("rows", rows),
|
||||
)
|
||||
|
||||
onboard_wizard._show_quick_start_summary(config, "primary")
|
||||
onboard_wizard._show_quick_start_summary(config)
|
||||
|
||||
rows = dict(captured["rows"])
|
||||
assert rows["Status"] == "OpenAI Codex OAuth login missing"
|
||||
@@ -1177,11 +1171,7 @@ class TestMainMenuInteraction:
|
||||
)
|
||||
monkeypatch.setattr(onboard_wizard, "_configure_pydantic_model", fail_websocket_config)
|
||||
monkeypatch.setattr(onboard_wizard, "_print_summary_panel", lambda *a, **kw: None)
|
||||
def pause(message=""):
|
||||
pause_messages.append(message)
|
||||
return True
|
||||
|
||||
monkeypatch.setattr(onboard_wizard, "_pause", pause)
|
||||
monkeypatch.setattr(onboard_wizard, "_pause", lambda message="": pause_messages.append(message))
|
||||
|
||||
assert onboard_wizard._configure_quick_start(config) is True
|
||||
|
||||
@@ -1267,29 +1257,31 @@ class TestMainMenuInteraction:
|
||||
assert config.model_dump(by_alias=True) == original
|
||||
assert getattr(config.channels, "websocket", None) is None
|
||||
|
||||
@pytest.mark.parametrize("existing_base", [None, "https://my-router.example/v1"])
|
||||
def test_quick_start_provider_choice_asks_for_model_id(self, monkeypatch, existing_base):
|
||||
@pytest.mark.parametrize(("label", "provider", "api_base", "model"), [
|
||||
("OpenRouter", "openrouter", "https://openrouter.ai/api/v1", "openai/gpt-4o-mini"),
|
||||
("ShengSuanYun Router", "shengsuanyun", "https://router.shengsuanyun.com/api/v1", "deepseek/deepseek-v4-flash"),
|
||||
])
|
||||
def test_quick_start_provider_choice_asks_for_model_id(self, monkeypatch, label, provider, api_base, model):
|
||||
config = Config()
|
||||
config.providers.openrouter.api_base = existing_base
|
||||
model_prompts: list[tuple[str, str, str]] = []
|
||||
|
||||
monkeypatch.setattr(onboard_wizard, "_show_quick_start_progress", lambda *_args: None)
|
||||
monkeypatch.setattr(onboard_wizard, "_select_with_back", lambda *a, **kw: "OpenRouter")
|
||||
monkeypatch.setattr(onboard_wizard, "_input_text", lambda *a, **kw: "sk-or-test")
|
||||
monkeypatch.setattr(onboard_wizard, "_select_with_back", lambda *a, **kw: label)
|
||||
monkeypatch.setattr(onboard_wizard, "_input_text", lambda *a, **kw: "test-key")
|
||||
|
||||
def fake_model_input(prompt, current, provider, *, config):
|
||||
model_prompts.append((prompt, current, provider))
|
||||
return "openai/gpt-4o-mini"
|
||||
return model
|
||||
|
||||
monkeypatch.setattr(onboard_wizard, "_input_model_with_autocomplete", fake_model_input)
|
||||
|
||||
assert onboard_wizard._configure_quick_start_provider(config) == "primary"
|
||||
assert onboard_wizard._configure_quick_start_provider(config) is True
|
||||
|
||||
assert model_prompts == [("Model ID", "", "openrouter")]
|
||||
assert config.providers.openrouter.api_key == "sk-or-test"
|
||||
assert config.providers.openrouter.api_base == (existing_base or "https://openrouter.ai/api/v1")
|
||||
assert config.model_presets["primary"].provider == "openrouter"
|
||||
assert config.model_presets["primary"].model == "openai/gpt-4o-mini"
|
||||
assert model_prompts == [("Model ID", "", provider)]
|
||||
assert getattr(config.providers, provider).api_key == "test-key"
|
||||
assert getattr(config.providers, provider).api_base == api_base
|
||||
assert config.model_presets["primary"].provider == provider
|
||||
assert config.model_presets["primary"].model == model
|
||||
|
||||
def test_quick_start_local_provider_skips_api_key(self, monkeypatch):
|
||||
config = Config()
|
||||
@@ -1307,7 +1299,7 @@ class TestMainMenuInteraction:
|
||||
lambda *a, **kw: "llama3.2",
|
||||
)
|
||||
|
||||
assert onboard_wizard._configure_quick_start_provider(config) == "primary"
|
||||
assert onboard_wizard._configure_quick_start_provider(config) is True
|
||||
|
||||
assert config.providers.ollama.api_key is None
|
||||
assert config.providers.ollama.api_base == "http://localhost:11434/v1"
|
||||
@@ -1326,7 +1318,7 @@ class TestMainMenuInteraction:
|
||||
lambda *a, **kw: "gpt-4o-mini",
|
||||
)
|
||||
|
||||
assert onboard_wizard._configure_quick_start_provider(config) == "primary"
|
||||
assert onboard_wizard._configure_quick_start_provider(config) is True
|
||||
|
||||
assert config.providers.openai.api_key == "sk-openai-test"
|
||||
assert config.providers.openai.api_base is None
|
||||
@@ -1353,7 +1345,7 @@ class TestMainMenuInteraction:
|
||||
lambda *a, **kw: "gpt-4o-mini",
|
||||
)
|
||||
|
||||
assert onboard_wizard._configure_quick_start_provider(config) == "primary"
|
||||
assert onboard_wizard._configure_quick_start_provider(config) is True
|
||||
|
||||
assert selected_providers == ["DeepSeek", "OpenAI"]
|
||||
assert config.providers.deepseek.api_key is None
|
||||
@@ -1362,7 +1354,6 @@ class TestMainMenuInteraction:
|
||||
|
||||
def test_quick_start_zhipu_coding_plan_uses_coding_base_url(self, monkeypatch):
|
||||
config = Config()
|
||||
config.providers.zhipu.api_base = "https://open.bigmodel.cn/api/paas/v4"
|
||||
choices = iter(["Zhipu AI", "Coding Plan"])
|
||||
|
||||
monkeypatch.setattr(onboard_wizard, "_show_quick_start_progress", lambda *_args: None)
|
||||
@@ -1374,7 +1365,7 @@ class TestMainMenuInteraction:
|
||||
lambda *a, **kw: "glm-4.6",
|
||||
)
|
||||
|
||||
assert onboard_wizard._configure_quick_start_provider(config) == "primary"
|
||||
assert onboard_wizard._configure_quick_start_provider(config) is True
|
||||
|
||||
assert config.providers.zhipu.api_key == "zhipu-key"
|
||||
assert config.providers.zhipu.api_base == "https://open.bigmodel.cn/api/coding/paas/v4"
|
||||
@@ -1394,7 +1385,7 @@ class TestMainMenuInteraction:
|
||||
lambda *a, **kw: "MiniMax-M2",
|
||||
)
|
||||
|
||||
assert onboard_wizard._configure_quick_start_provider(config) == "primary"
|
||||
assert onboard_wizard._configure_quick_start_provider(config) is True
|
||||
|
||||
assert config.providers.minimax.api_key == "minimax-key"
|
||||
assert config.providers.minimax.api_base == "https://api.minimaxi.com/v1"
|
||||
@@ -1414,7 +1405,7 @@ class TestMainMenuInteraction:
|
||||
lambda *a, **kw: "step-3.5-flash",
|
||||
)
|
||||
|
||||
assert onboard_wizard._configure_quick_start_provider(config) == "primary"
|
||||
assert onboard_wizard._configure_quick_start_provider(config) is True
|
||||
|
||||
assert config.providers.stepfun.api_key == "stepfun-key"
|
||||
assert config.providers.stepfun.api_base == "https://api.stepfun.ai/step_plan/v1"
|
||||
@@ -1434,7 +1425,7 @@ class TestMainMenuInteraction:
|
||||
lambda *a, **kw: "mimo-v2.5-pro",
|
||||
)
|
||||
|
||||
assert onboard_wizard._configure_quick_start_provider(config) == "primary"
|
||||
assert onboard_wizard._configure_quick_start_provider(config) is True
|
||||
|
||||
assert config.providers.xiaomi_mimo.api_key == "mimo-key"
|
||||
assert config.providers.xiaomi_mimo.api_base == "https://token-plan-sgp.xiaomimimo.com/v1"
|
||||
@@ -1458,107 +1449,13 @@ class TestMainMenuInteraction:
|
||||
lambda *a, **kw: "custom-model",
|
||||
)
|
||||
|
||||
assert onboard_wizard._configure_quick_start_provider(config) == "primary"
|
||||
assert onboard_wizard._configure_quick_start_provider(config) is True
|
||||
|
||||
assert config.providers.custom.api_key == "sk-custom-test"
|
||||
assert config.providers.custom.api_base == "https://api.example.test/v1"
|
||||
assert config.model_presets["primary"].provider == "custom"
|
||||
assert config.model_presets["primary"].model == "custom-model"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"existing, cancel_at",
|
||||
[(False, None), (True, None), (True, "model"), (True, "review")],
|
||||
ids=["first-run", "existing-setup", "cancel-model", "cancel-review"],
|
||||
)
|
||||
def test_shengsuanyun_quick_start_saves_separate_connection(
|
||||
self, tmp_path, monkeypatch, prompt_input, existing, cancel_at,
|
||||
):
|
||||
from nanobot.cli.commands import app
|
||||
|
||||
path = tmp_path / "config.json"
|
||||
monkeypatch.setattr(loader, "_current_config_path", None)
|
||||
monkeypatch.setattr(instance_home, "_home_path", None)
|
||||
if existing:
|
||||
old = Config.model_validate({
|
||||
"agents": {"defaults": {"workspace": str(tmp_path / "workspace"), "modelPreset": "primary"}},
|
||||
"providers": {
|
||||
"custom": {"apiBase": "https://old.example/v1", "apiKey": "old-key"},
|
||||
"Custom_ShengSuanYun_Router": {"apiBase": "https://other.example/v1"},
|
||||
},
|
||||
"modelPresets": {
|
||||
"primary": {"model": "old-model", "provider": "custom"},
|
||||
"shengsuanyun": {"model": "other-model", "provider": "Custom_ShengSuanYun_Router"},
|
||||
},
|
||||
"channels": {"websocket": {"enabled": True, "tokenIssueSecret": "old-password"}},
|
||||
})
|
||||
save_config(old, path)
|
||||
before = path.read_bytes()
|
||||
|
||||
model_id = "deepseek/example-model"
|
||||
model_url = "https://router.shengsuanyun.com/api/v1/models"
|
||||
calls = []
|
||||
|
||||
def get(url, **kwargs):
|
||||
calls.append((url, kwargs["headers"]["Authorization"]))
|
||||
return httpx.Response(200, request=httpx.Request("GET", url), json={"data": [{"id": model_id}]})
|
||||
|
||||
keys = ["\r", "\x1b[A" * 3 + "\r", "router-test-key\r"]
|
||||
if cancel_at == "model":
|
||||
keys.extend(["\x1b", "\x1b[D"])
|
||||
else:
|
||||
keys.append(f"{model_id}\r")
|
||||
if not existing:
|
||||
keys.append("webui-test-password\r")
|
||||
if cancel_at:
|
||||
keys.append("\x1b[A\r")
|
||||
inputs = iter(keys)
|
||||
ask_prompt = onboard_wizard._ask_prompt
|
||||
pause = onboard_wizard._pause
|
||||
|
||||
def ask_with_input(prompt):
|
||||
prompt_input.send_text(next(inputs))
|
||||
return ask_prompt(prompt)
|
||||
|
||||
def review_with_input(message):
|
||||
prompt_input.send_text("\x03" if cancel_at == "review" else "\r")
|
||||
return pause(message)
|
||||
|
||||
monkeypatch.setattr(httpx, "get", get)
|
||||
monkeypatch.setattr(onboard_wizard, "_ask_prompt", ask_with_input)
|
||||
monkeypatch.setattr(onboard_wizard, "_pause", review_with_input)
|
||||
monkeypatch.setattr(
|
||||
onboard_wizard._get_questionary(), "confirm",
|
||||
lambda *args, **kwargs: SimpleNamespace(ask=lambda: True),
|
||||
)
|
||||
|
||||
result = CliRunner().invoke(app, ["--home", str(tmp_path), "onboard", "--wizard"])
|
||||
assert result.exit_code == 0, result.output
|
||||
assert "router-test-key" not in result.output
|
||||
assert calls == [(model_url, "Bearer router-test-key")]
|
||||
if cancel_at:
|
||||
assert "Configuration discarded" in result.output
|
||||
assert path.read_bytes() == before
|
||||
return
|
||||
saved = load_config(path)
|
||||
preset_name = "shengsuanyun-2" if existing else "shengsuanyun"
|
||||
route = resolve_provider_route(saved, preset=saved.resolve_preset(preset_name))
|
||||
assert route.model == model_id
|
||||
assert route.provider_name == ("custom-shengsuanyun-router-2" if existing else "custom-shengsuanyun-router")
|
||||
assert route.provider_config is not None
|
||||
assert route.provider_config.api_base == model_url.removesuffix("/models")
|
||||
assert route.model_api is not None and route.model_api.preferred_api == "chat_completions"
|
||||
if existing:
|
||||
assert saved.resolve_preset() == old.resolve_preset()
|
||||
assert saved.agents.defaults.model_preset == "primary"
|
||||
assert saved.channels.websocket == old.channels.websocket
|
||||
for key, provider in (old.providers.model_extra or {}).items():
|
||||
assert getattr(saved.providers, key) == provider
|
||||
assert saved.providers.custom == old.providers.custom
|
||||
assert saved.model_presets["shengsuanyun"] == old.model_presets["shengsuanyun"]
|
||||
else:
|
||||
assert saved.agents.defaults.model_preset == preset_name
|
||||
assert saved.channels.websocket["enabled"] is True
|
||||
|
||||
def test_quick_start_provider_without_default_base_url_prompts_for_base(self, monkeypatch):
|
||||
config = Config()
|
||||
text_answers = iter(["azure-key", "https://azure.example.test/openai"])
|
||||
@@ -1572,7 +1469,7 @@ class TestMainMenuInteraction:
|
||||
lambda *a, **kw: "deployment-name",
|
||||
)
|
||||
|
||||
assert onboard_wizard._configure_quick_start_provider(config) == "primary"
|
||||
assert onboard_wizard._configure_quick_start_provider(config) is True
|
||||
|
||||
assert config.providers.azure_openai.api_key == "azure-key"
|
||||
assert config.providers.azure_openai.api_base == "https://azure.example.test/openai"
|
||||
@@ -1686,7 +1583,6 @@ class TestMainMenuInteraction:
|
||||
|
||||
def test_quick_start_summary_calls_out_missing_api_key(self, monkeypatch):
|
||||
config = Config()
|
||||
config.channels.websocket = {"enabled": True}
|
||||
config.model_presets["primary"] = ModelPresetConfig(
|
||||
model="deepseek-v4-flash",
|
||||
provider="deepseek",
|
||||
@@ -1701,7 +1597,7 @@ class TestMainMenuInteraction:
|
||||
lambda rows, _title: captured.setdefault("rows", rows),
|
||||
)
|
||||
|
||||
onboard_wizard._show_quick_start_summary(config, "primary")
|
||||
onboard_wizard._show_quick_start_summary(config)
|
||||
|
||||
rows = dict(captured["rows"])
|
||||
assert rows["Status"] == "DeepSeek API key missing"
|
||||
|
||||
Reference in new issue
Block a user