Skip to content

Your lightweight open-source personal agent.

fix(cli): reuse WebUI model discovery in onboarding

chengyongruad52fa97c2ac
diff --git a/nanobot/cli/models.py b/nanobot/cli/models.py
index 129169ee2..396ea2234 100644
--- a/nanobot/cli/models.py
+++ b/nanobot/cli/models.py
@@ -1,31 +1,58 @@
-"""Model information helpers for the onboard wizard.
-
-Model database / autocomplete is temporarily disabled while litellm is
-being replaced. All public function signatures are preserved so callers
-continue to work without changes.
-"""
+"""Model discovery for the onboard wizard, shared with WebUI settings."""
from __future__ import annotations
-from typing import Any
-
-
-def get_all_models() -> list[str]:
- return []
-
-
-def find_model_info(model_name: str) -> dict[str, Any] | None:
+from dataclasses import dataclass
+
+import httpx
+
+from nanobot.config.schema import Config, ModelPresetConfig
+from nanobot.webui.settings_contracts import WebUISettingsError
+from nanobot.webui.settings_models import provider_models_payload
+
+
+@dataclass(frozen=True)
+class ModelInfo:
+ id: str
+ context_window: int | None
+
+
+def get_model_catalog(
+ config: Config, provider: str, model: str = "",
+) -> list[ModelInfo]:
+ """Read the selected provider's advisory catalog using the current wizard draft."""
+ if provider == "auto":
+ provider = config.get_provider_name(preset=ModelPresetConfig(model=model)) or ""
+ if not provider:
+ return []
+ try:
+ payload = provider_models_payload(
+ config, {"provider": [provider]}, http_get=httpx.get,
+ )
+ except WebUISettingsError:
+ return []
+ return [
+ ModelInfo(row["id"], row["context_window"])
+ for row in payload["models"]
+ ]
+
+
+def get_model_context_limit(
+ model: str, provider: str, *, config: Config,
+) -> int | None:
+ if provider == "auto":
+ provider = config.get_provider_name(preset=ModelPresetConfig(model=model)) or ""
+ rows = get_model_catalog(config, provider, model)
+ for row in rows:
+ if row.id == model:
+ return row.context_window
+ # Explicit provider prefixes are accepted by the wizard as well as bare IDs.
+ prefix, separator, bare_model = model.partition("/")
+ if separator and prefix.replace("-", "_") == provider.replace("-", "_"):
+ return next((row.context_window for row in rows if row.id == bare_model), None)
return None
-def get_model_context_limit(model: str, provider: str = "auto") -> int | None:
- return None
-
-
-def get_model_suggestions(_partial: str, provider: str = "auto", limit: int = 20) -> list[str]:
- return []
-
-
def format_token_count(tokens: int) -> str:
"""Format token count for display (e.g., 200000 -> '200,000')."""
return f"{tokens:,}"
diff --git a/nanobot/cli/onboard.py b/nanobot/cli/onboard.py
index 87ae569db..575286d82 100644
--- a/nanobot/cli/onboard.py
+++ b/nanobot/cli/onboard.py
@@ -27,8 +27,8 @@ from rich.table import Table
from nanobot.cli.models import (
format_token_count,
+ get_model_catalog,
get_model_context_limit,
- get_model_suggestions,
)
from nanobot.config.loader import get_config_path, load_config, resolve_config_env_vars
from nanobot.config.schema import Config, ModelPresetConfig
@@ -650,18 +650,17 @@ def _get_current_provider(model: BaseModel) -> str:
def _input_model_with_autocomplete(
- display_name: str, current: Any, provider: str
+ display_name: str, current: Any, provider: str, *, config: Config
) -> str | None | object:
"""Get model input with autocomplete suggestions.
"""
default = str(current) if current else ""
- class DynamicModelCompleter(Completer):
- """Completer that dynamically fetches model suggestions."""
+ models = get_model_catalog(config, provider, default)
- def __init__(self, provider_name: str):
- self.provider = provider_name
+ class DynamicModelCompleter(Completer):
+ """Filter one provider snapshot without network requests while typing."""
def get_completions(
self,
@@ -670,11 +669,8 @@ def _input_model_with_autocomplete(
) -> Iterable[Completion]:
_ = complete_event
text = document.text_before_cursor
- suggestions = get_model_suggestions(text, provider=self.provider, limit=50)
+ suggestions = [row.id for row in models if text.lower() in row.id.lower()][:50]
for model in suggestions:
- # Skip if model doesn't contain the typed text
- if text.lower() not in model.lower():
- continue
yield Completion(
model,
start_position=-len(text),
@@ -685,7 +681,7 @@ def _input_model_with_autocomplete(
_get_questionary().autocomplete(
f"{display_name}:",
choices=[""], # Placeholder, actual completions from completer
- completer=DynamicModelCompleter(provider),
+ completer=DynamicModelCompleter(),
default=default,
key_bindings=_input_back_key_bindings(),
qmark=">",
@@ -698,7 +694,7 @@ def _input_model_with_autocomplete(
def _input_context_window_with_recommendation(
- display_name: str, current: Any, model_obj: BaseModel
+ display_name: str, current: Any, model_obj: BaseModel, *, config: Config
) -> int | None | object:
"""Get context window input with option to fetch recommended value."""
current_val = current if current else ""
@@ -728,7 +724,7 @@ def _input_context_window_with_recommendation(
return None
provider = _get_current_provider(model_obj)
- context_limit = get_model_context_limit(model_name, provider)
+ context_limit = get_model_context_limit(model_name, provider, config=config)
if context_limit:
console.print(
@@ -760,24 +756,26 @@ def _input_context_window_with_recommendation(
def _handle_model_field(
- working_model: BaseModel, field_name: str, field_display: str, current_value: Any
+ working_model: BaseModel, field_name: str, field_display: str, current_value: Any,
+ *, config: Config,
) -> None:
"""Handle the 'model' field with autocomplete and context-window auto-fill."""
provider = _get_current_provider(working_model)
- new_value = _input_model_with_autocomplete(field_display, current_value, provider)
+ new_value = _input_model_with_autocomplete(field_display, current_value, provider, config=config)
if new_value is _BACK_PRESSED:
return
if new_value is not None and new_value != current_value:
setattr(working_model, field_name, new_value)
- _try_auto_fill_context_window(working_model, cast(str, new_value))
+ _try_auto_fill_context_window(working_model, cast(str, new_value), config=config)
def _handle_context_window_field(
- working_model: BaseModel, field_name: str, field_display: str, current_value: Any
+ working_model: BaseModel, field_name: str, field_display: str, current_value: Any,
+ *, config: Config,
) -> None:
"""Handle context_window_tokens with recommendation lookup."""
new_value = _input_context_window_with_recommendation(
- field_display, current_value, working_model
+ field_display, current_value, working_model, config=config
)
if new_value is _BACK_PRESSED:
return
@@ -930,6 +928,7 @@ def _configure_pydantic_model(
display_name: str,
*,
skip_fields: set[str] | None = None,
+ config: Config | None = None,
) -> _ModelT | None:
"""Configure a Pydantic model interactively.
@@ -997,7 +996,7 @@ def _configure_pydantic_model(
if nested is None and ftype.inner_type:
nested = ftype.inner_type()
if nested and isinstance(nested, BaseModel):
- updated = _configure_pydantic_model(nested, field_display)
+ updated = _configure_pydantic_model(nested, field_display, config=config)
if updated is not None:
setattr(working_model, field_name, updated)
elif created:
@@ -1007,7 +1006,10 @@ def _configure_pydantic_model(
# Registered special-field handlers
handler = _resolve_field_handler(working_model, field_name)
if handler:
- handler(working_model, field_name, field_display, current_value)
+ if field_name in {"model", "context_window_tokens"}:
+ handler(working_model, field_name, field_display, current_value, config=config or Config())
+ else:
+ handler(working_model, field_name, field_display, current_value)
continue
# Select fields with hints (e.g. reasoning_effort)
@@ -1050,7 +1052,9 @@ def _configure_pydantic_model(
setattr(working_model, field_name, new_value)
-def _try_auto_fill_context_window(model: BaseModel, new_model_name: str) -> None:
+def _try_auto_fill_context_window(
+ model: BaseModel, new_model_name: str, *, config: Config,
+) -> None:
"""Try to auto-fill context_window_tokens if it's at default value.
Note:
@@ -1074,7 +1078,7 @@ def _try_auto_fill_context_window(model: BaseModel, new_model_name: str) -> None
return # User has customized it, don't override
provider = _get_current_provider(model)
- context_limit = get_model_context_limit(new_model_name, provider)
+ context_limit = get_model_context_limit(new_model_name, provider, config=config)
if context_limit:
setattr(model, "context_window_tokens", context_limit)
@@ -1083,7 +1087,7 @@ def _try_auto_fill_context_window(model: BaseModel, new_model_name: str) -> None
f"{format_token_count(context_limit)} tokens[/]"
)
else:
- console.print("[dim]Could not auto-fill context window - model not in database[/dim]")
+ console.print("[dim]Could not auto-fill context window - provider did not return a context limit[/dim]")
# --- Model Preset Configuration ---
@@ -1157,7 +1161,7 @@ def _configure_model_presets(config: Config) -> None:
_pause()
continue
new_preset = ModelPresetConfig(model="")
- updated = _configure_pydantic_model(new_preset, f"New Preset: {name}")
+ updated = _configure_pydantic_model(new_preset, f"New Preset: {name}", config=config)
if updated is not None:
config.model_presets[name] = updated
_sync_preset_cache(config)
@@ -1197,7 +1201,7 @@ def _configure_model_presets(config: Config) -> None:
continue
if action == "Edit":
- updated = _configure_pydantic_model(preset, f"Edit Preset: {preset_name}")
+ updated = _configure_pydantic_model(preset, f"Edit Preset: {preset_name}", config=config)
if updated is not None:
config.model_presets[preset_name] = updated
_sync_preset_cache(config)
@@ -1493,7 +1497,7 @@ def _configure_general_settings(config: Config, section: str) -> None:
return
display_name, _subtitle, skip = meta
model = _SETTINGS_GETTER[section](config)
- updated = _configure_pydantic_model(model, display_name, skip_fields=skip)
+ updated = _configure_pydantic_model(model, display_name, skip_fields=skip, config=config)
if updated is not None:
_SETTINGS_SETTER[section](config, updated)
@@ -1837,10 +1841,18 @@ def _configure_quick_start_provider(config: Config) -> bool | object:
console.print(f"[red]Unknown provider: {provider_name}[/red]")
return False
+ catalog_config = config.model_copy(deep=True)
+ catalog_provider = getattr(catalog_config.providers, provider_name)
+ if api_key is not None:
+ catalog_provider.api_key = api_key
+ if api_base and (base_was_prompted or not catalog_provider.api_base):
+ catalog_provider.api_base = api_base
+
model = _input_model_with_autocomplete(
"Model ID",
provider_info.default_model if provider_info else "",
provider_name,
+ config=catalog_config,
)
if model is _BACK_PRESSED:
continue
diff --git a/tests/agent/test_onboard_logic.py b/tests/agent/test_onboard_logic.py
index e075f4511..81284c63b 100644
--- a/tests/agent/test_onboard_logic.py
+++ b/tests/agent/test_onboard_logic.py
@@ -1016,7 +1016,7 @@ class TestMainMenuUpdate:
def fail_api_key_prompt(*_args, **_kwargs):
raise AssertionError("OpenAI Codex Quick Start should not ask for an API key")
- def fake_model_input(prompt, current, provider):
+ def fake_model_input(prompt, current, provider, *, config):
model_prompts.append((prompt, current, provider))
return current
@@ -1351,7 +1351,7 @@ class TestMainMenuUpdate:
monkeypatch.setattr(onboard_wizard, "_select_with_back", lambda *a, **kw: "OpenRouter")
monkeypatch.setattr(onboard_wizard, "_input_text", lambda *a, **kw: "sk-or-test")
- def fake_model_input(prompt, current, provider):
+ def fake_model_input(prompt, current, provider, *, config):
model_prompts.append((prompt, current, provider))
return "openai/gpt-4o-mini"
diff --git a/tests/cli/test_models.py b/tests/cli/test_models.py
new file mode 100644
index 000000000..fa875a60f
--- /dev/null
+++ b/tests/cli/test_models.py
@@ -0,0 +1,97 @@
+"""CLI model discovery exercises the same provider boundary as WebUI."""
+
+from types import SimpleNamespace
+
+import httpx
+import pytest
+from prompt_toolkit.completion import CompleteEvent
+from prompt_toolkit.document import Document
+
+from nanobot.cli import models, onboard
+from nanobot.config.schema import Config, ModelPresetConfig
+
+
+def catalog_http(monkeypatch):
+ calls = []
+
+ def get(url, **kwargs):
+ calls.append((url, kwargs))
+ return httpx.Response(200, request=httpx.Request("GET", url), json={"data": [
+ {"id": "alpha", "context_length": 123456},
+ {"id": "beta"},
+ ]})
+
+ monkeypatch.setattr(models.httpx, "get", get)
+ return calls
+
+
+def configured():
+ config = Config()
+ config.providers.custom.api_base = "https://catalog.example/v1"
+ config.providers.custom.api_key = "draft-key"
+ return config
+
+
+def test_catalog_uses_unsaved_endpoint_and_credentials(catalog_http):
+ config = configured()
+ before = config.model_dump()
+ assert models.get_model_context_limit("alpha", "custom", config=config) == 123456
+ assert models.get_model_context_limit("beta", "custom", config=config) is None
+ assert catalog_http[0][0] == "https://catalog.example/v1/models"
+ assert catalog_http[0][1]["headers"]["Authorization"] == "Bearer draft-key"
+ assert config.model_dump() == before
+
+
+def test_auto_provider_uses_edited_model_not_active_preset(catalog_http):
+ config = configured()
+ config.model_presets["active"] = ModelPresetConfig(model="unrelated", provider="anthropic")
+ config.agents.defaults.model_preset = "active"
+ assert models.get_model_context_limit("custom/alpha", "auto", config=config) == 123456
+
+
+def test_autocomplete_fetches_once_and_allows_manual_model(catalog_http, monkeypatch):
+ def autocomplete(*args, **kwargs):
+ completer = kwargs["completer"]
+ event = CompleteEvent()
+ assert [c.text for c in completer.get_completions(Document("AL"), event)] == ["alpha"]
+ assert [c.text for c in completer.get_completions(Document("be"), event)] == ["beta"]
+ return SimpleNamespace(ask=lambda: "not-in-catalog")
+
+ monkeypatch.setattr(onboard, "questionary", SimpleNamespace(autocomplete=autocomplete))
+ assert onboard._input_model_with_autocomplete(
+ "Model", "", "custom", config=configured(),
+ ) == "not-in-catalog"
+ assert len(catalog_http) == 1
+
+
+def test_discovery_failure_keeps_manual_input(monkeypatch):
+ def unavailable(*args, **kwargs):
+ raise httpx.ConnectError("offline")
+
+ monkeypatch.setattr(models.httpx, "get", unavailable)
+ assert models.get_model_catalog(configured(), "custom") == []
+ assert models.get_model_context_limit("alpha", "custom", config=configured()) is None
+
+
+def test_unconfigured_provider_does_not_fetch(catalog_http):
+ assert models.get_model_catalog(Config(), "openai") == []
+ assert catalog_http == []
+
+
+def test_quick_start_discovery_uses_draft_without_committing_on_back(monkeypatch):
+ config = Config()
+ before = config.model_dump()
+ choices = iter(["OpenRouter", onboard._BACK_PRESSED])
+ monkeypatch.setattr(onboard, "_select_with_back", lambda *a, **kw: next(choices))
+ monkeypatch.setattr(onboard, "_show_quick_start_progress", lambda *a: None)
+ monkeypatch.setattr(onboard, "_input_text", lambda *a: "new-key")
+
+ def model_input(*args, config):
+ assert config.providers.openrouter.api_key == "new-key"
+ return onboard._BACK_PRESSED
+
+ monkeypatch.setattr(onboard, "_input_model_with_autocomplete", model_input)
+ assert onboard._configure_quick_start_provider(config) is onboard._BACK_PRESSED
+ assert config.model_dump() == before
+