Skip to content

feat(config): add model presets for quick model switching#3696

Closed
chengyongru wants to merge 4 commits into
mainfrom
feat/model-presets
Closed

feat(config): add model presets for quick model switching#3696
chengyongru wants to merge 4 commits into
mainfrom
feat/model-presets

Conversation

@chengyongru
Copy link
Copy Markdown
Collaborator

Summary

Add named model presets with automatic failover and runtime switching support.

  • Model Presets: ModelPresetConfig bundles model + provider + generation parameters. Defined in model_presets dict, activated via model_preset on AgentDefaults.
  • Automatic failover (ModelRouter): When fallback_presets is configured, the primary provider is wrapped with a router that retries provider-internal errors against fallback presets in order.
  • Atomic runtime switching: AgentLoop.model_preset setter resolves preset by name, creates provider via factory, wraps with failover, and atomically updates all subsystems.
  • Factory-based provider creation: make_provider_factory(config) builds a cached factory that creates providers from preset names only.
  • AgentLoop.from_config(): New classmethod that encapsulates bus/provider/loop assembly, used by both CLI (serve, chat) and Nanobot facade.
  • Onboard Wizard: Added [M] Model Presets section with add / edit / delete support.

@xzq-xu thanks for your contribution
@boogieLing thanks for your contribution

(cherry-picked from #3358)

chengyongru and others added 4 commits May 8, 2026 20:19
Add ModelPresetConfig schema and model_presets dictionary to config,
enabling named bundles of model parameters (model, temperature,
max_tokens, reasoning_effort, context_window_tokens) that can be
switched atomically at runtime via the self tool.
When the primary model fails (finish_reason="error" after exhausting
provider-level retries), automatically try each model in the configured
fallback_models list. Supports cross-provider fallback via a cached
provider_factory that resolves the correct provider for each model string.

Config:
  agents.defaults.fallback_models: ["model-b", "provider/model-c"]

Changes:
- AgentDefaults: add fallback_models field
- AgentRunSpec: add fallback_models field
- AgentRunner: add provider_factory, _call_provider, _resolve_fallback_provider
- AgentLoop: accept and forward fallback_models + provider_factory
- nanobot.py: extract _make_provider_for_model, add _make_provider_factory
- cli/commands.py: add _make_cli_provider_factory, wire all AgentLoop sites
- tests/agent/test_runner_fallback.py: 8 test cases covering primary success,
  single/multi fallback, cross-provider, no-factory reuse, caching

Made-with: Cursor
…er factory

- Restrict fallback_models to only reference preset names in model_presets.
- Add schema validation to reject unknown preset names in fallback_models.
- Remove build_provider_for_model() since bare model fallback is no longer supported.
- Simplify make_provider_factory() to only look up presets by name.
- Update onboard UI to remove "Add custom model" option from fallback chain.
- Update tests to use preset names instead of bare model strings in fallback chains.
- Fix test imports referencing deleted _make_provider function.
@chengyongru
Copy link
Copy Markdown
Collaborator Author

This PR is too large and needs to be reasonably split.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants