From 8fe8f311a0a830967f1513d0e5f6de97bc52fb5f Mon Sep 17 00:00:00 2001 From: Hakula Chen Date: Tue, 1 Dec 2026 18:12:56 +0800 Subject: [PATCH 1/3] feat(ox-core): add data-driven model catalog and tier profiles --- crates/ox-core/catalog/models.toml | 63 ++++++ crates/ox-core/src/catalog.rs | 332 +++++++++++++++++++++++++++++ crates/ox-core/src/config.rs | 20 ++ crates/ox-core/src/lib.rs | 1 + docs/README.md | 1 + docs/reference/models.md | 49 +++++ 6 files changed, 466 insertions(+) create mode 100644 crates/ox-core/catalog/models.toml create mode 100644 crates/ox-core/src/catalog.rs create mode 100644 docs/reference/models.md diff --git a/crates/ox-core/catalog/models.toml b/crates/ox-core/catalog/models.toml new file mode 100644 index 000000000..b12efe6d4 --- /dev/null +++ b/crates/ox-core/catalog/models.toml @@ -0,0 +1,63 @@ +[[models]] +id = "gemini-4-hydrogen" +provider_wire = "google" +context_window = 1048576 +max_output_tokens = 65536 +supports_reasoning = true +tier = "flagship" + +[[models]] +id = "gemini-4-neon" +provider_wire = "google" +context_window = 1048576 +max_output_tokens = 65536 +supports_reasoning = true +tier = "standard" + +[[models]] +id = "gemini-4-helium" +provider_wire = "google" +context_window = 1048576 +max_output_tokens = 32768 +supports_reasoning = false +tier = "mini" + +[[models]] +id = "gpt-6.2" +provider_wire = "openai" +context_window = 400000 +max_output_tokens = 128000 +supports_reasoning = true +tier = "flagship" + +[[models]] +id = "gpt-6.2-mini" +provider_wire = "openai" +context_window = 400000 +max_output_tokens = 65536 +supports_reasoning = true +tier = "mini" + +[[models]] +id = "claude-opus-5-5" +provider_wire = "anthropic" +context_window = 200000 +max_output_tokens = 64000 +supports_reasoning = true +tier = "flagship" + +[[models]] +id = "claude-sonnet-5-5" +provider_wire = "anthropic" +context_window = 200000 +max_output_tokens = 64000 +supports_reasoning = true +tier = "standard" + +[[models]] +id = "claude-haiku-5-5" +provider_wire = "anthropic" +context_window = 200000 +max_output_tokens = 32000 +supports_reasoning = false +tier = "mini" diff --git a/crates/ox-core/src/catalog.rs b/crates/ox-core/src/catalog.rs new file mode 100644 index 000000000..e91c4eac4 --- /dev/null +++ b/crates/ox-core/src/catalog.rs @@ -0,0 +1,332 @@ +//! Data-driven model catalog and tier profile resolution. + +use std::collections::BTreeMap; +use std::fmt; +use std::path::{Path, PathBuf}; +use std::str::FromStr; + +use serde::{Deserialize, Serialize}; +use thiserror::Error; + +use crate::config::ProviderWire; + +const BUILTIN_CATALOG_TOML: &str = include_str!("../catalog/models.toml"); + +/// Errors produced while loading or querying a model catalog. +#[derive(Debug, Error)] +pub enum CatalogError { + /// Failed to read a catalog file from disk. + #[error("failed to read model catalog {path}: {source}")] + Io { + /// Path to the unreadable catalog file. + path: PathBuf, + /// Underlying I/O error. + source: std::io::Error, + }, + /// Failed to parse TOML catalog data. + #[error("failed to parse model catalog: {0}")] + Toml(#[from] toml::de::Error), + /// Tier name did not match any known [`ModelTier`]. + #[error("unknown model tier `{0}` (expected `flagship`, `standard`, or `mini`)")] + UnknownTier(String), +} + +/// Capability and cost tier assigned to a catalog model. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum ModelTier { + /// Highest-capability frontier model for complex tasks. + Flagship, + /// Balanced general-purpose model for everyday coding. + Standard, + /// Fast, low-cost model for lightweight or high-volume tasks. + Mini, +} + +impl ModelTier { + /// Returns the canonical lowercase name for this tier. + #[must_use] + pub const fn as_str(self) -> &'static str { + match self { + Self::Flagship => "flagship", + Self::Standard => "standard", + Self::Mini => "mini", + } + } +} + +impl fmt::Display for ModelTier { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.as_str()) + } +} + +impl FromStr for ModelTier { + type Err = CatalogError; + + fn from_str(s: &str) -> Result { + match s { + "flagship" => Ok(Self::Flagship), + "standard" => Ok(Self::Standard), + "mini" => Ok(Self::Mini), + other => Err(CatalogError::UnknownTier(other.to_owned())), + } + } +} + +/// Metadata for a single model in a [`ModelCatalog`]. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct CatalogModel { + /// Unique wire model identifier (for example `gemini-4-hydrogen`). + pub id: String, + /// Provider wire protocol used to talk to this model. + #[serde(rename = "provider_wire")] + pub wire: ProviderWire, + /// Maximum input context window in tokens. + pub context_window: u32, + /// Maximum output tokens per response. + pub max_output_tokens: u32, + /// Whether the model supports explicit reasoning effort controls. + #[serde(default)] + pub supports_reasoning: bool, + /// Profile tier assigned to this model. + pub tier: ModelTier, +} + +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +struct RawCatalog { + #[serde(default)] + models: Vec, +} + +/// Collection of [`CatalogModel`] entries keyed by model identifier. +#[derive(Debug, Default, Clone, PartialEq, Eq)] +pub struct ModelCatalog { + models: BTreeMap, +} + +impl ModelCatalog { + /// Loads the built-in model catalog bundled in `crates/ox-core/catalog/models.toml`. + #[must_use] + pub fn builtin() -> Self { + Self::from_toml(BUILTIN_CATALOG_TOML).expect("bundled models.toml must be valid") + } + + /// Parses a [`ModelCatalog`] from TOML data. + pub fn from_toml(input: &str) -> Result { + let raw: RawCatalog = toml::from_str(input)?; + let mut models = BTreeMap::new(); + for model in raw.models { + models.insert(model.id.clone(), model); + } + Ok(Self { models }) + } + + /// Reads and parses a [`ModelCatalog`] from a TOML file on disk. + pub fn load_file(path: impl AsRef) -> Result { + let path = path.as_ref(); + let raw = std::fs::read_to_string(path).map_err(|source| CatalogError::Io { + path: path.to_path_buf(), + source, + })?; + Self::from_toml(&raw) + } + + /// Merges another catalog into `self`, overriding entries with matching IDs. + pub fn merge(&mut self, other: Self) { + self.models.extend(other.models); + } + + /// Looks up a model by its identifier. + #[must_use] + pub fn get(&self, id: &str) -> Option<&CatalogModel> { + self.models.get(id) + } + + /// Iterates over all models in deterministic ID order. + pub fn models(&self) -> impl Iterator { + self.models.values() + } + + /// Returns all models assigned to the given tier in deterministic ID order. + #[must_use] + pub fn by_tier(&self, tier: ModelTier) -> Vec<&CatalogModel> { + self.models + .values() + .filter(|model| model.tier == tier) + .collect() + } + + /// Resolves a [`ModelTier`] to a [`CatalogModel`]. + /// + /// When `overrides` contains an entry for `tier.as_str()`, that model ID is looked up directly + /// in this catalog. Otherwise, the first catalog entry matching `tier` is returned. + #[must_use] + pub fn resolve_tier( + &self, + tier: ModelTier, + overrides: &BTreeMap, + ) -> Option<&CatalogModel> { + if let Some(override_id) = overrides.get(tier.as_str()) { + return self.get(override_id); + } + self.models.values().find(|model| model.tier == tier) + } +} + +#[cfg(test)] +mod tests { + use indoc::indoc; + use tempfile::NamedTempFile; + + use super::*; + + #[test] + fn model_tier_display_and_from_str_roundtrip() { + for (tier, name) in [ + (ModelTier::Flagship, "flagship"), + (ModelTier::Standard, "standard"), + (ModelTier::Mini, "mini"), + ] { + assert_eq!(tier.as_str(), name); + assert_eq!(tier.to_string(), name); + assert_eq!(name.parse::().unwrap(), tier); + } + + let err = "ultra".parse::().unwrap_err(); + assert!(matches!(err, CatalogError::UnknownTier(s) if s == "ultra")); + } + + #[test] + fn builtin_catalog_loads_expected_models_and_tiers() { + let catalog = ModelCatalog::builtin(); + + let hydrogen = catalog.get("gemini-4-hydrogen").unwrap(); + assert_eq!(hydrogen.wire, ProviderWire::Google); + assert_eq!(hydrogen.tier, ModelTier::Flagship); + assert!(hydrogen.supports_reasoning); + + let gpt = catalog.get("gpt-6.2").unwrap(); + assert_eq!(gpt.wire, ProviderWire::OpenAi); + assert_eq!(gpt.tier, ModelTier::Flagship); + + let sonnet = catalog.get("claude-sonnet-5-5").unwrap(); + assert_eq!(sonnet.wire, ProviderWire::Anthropic); + assert_eq!(sonnet.tier, ModelTier::Standard); + + assert_eq!(catalog.by_tier(ModelTier::Flagship).len(), 3); + assert_eq!(catalog.by_tier(ModelTier::Standard).len(), 2); + assert_eq!(catalog.by_tier(ModelTier::Mini).len(), 3); + assert_eq!(catalog.models().count(), 8); + } + + #[test] + fn from_toml_parses_and_rejects_unknown_fields() { + let raw = indoc! {r#" + [[models]] + id = "custom-model" + provider_wire = "openai" + context_window = 128000 + max_output_tokens = 16384 + tier = "standard" + "#}; + + let catalog = ModelCatalog::from_toml(raw).unwrap(); + let model = catalog.get("custom-model").unwrap(); + assert_eq!(model.wire, ProviderWire::OpenAi); + assert_eq!(model.context_window, 128_000); + assert_eq!(model.max_output_tokens, 16_384); + assert!(!model.supports_reasoning); + assert_eq!(model.tier, ModelTier::Standard); + + let bad = indoc! {r#" + [[models]] + id = "bad" + provider_wire = "google" + context_window = 1000 + max_output_tokens = 500 + tier = "mini" + unknown_key = true + "#}; + assert!(matches!( + ModelCatalog::from_toml(bad).unwrap_err(), + CatalogError::Toml(_) + )); + } + + #[test] + fn load_file_reads_disk_and_reports_io_error() { + let file = NamedTempFile::new().unwrap(); + let raw = indoc! {r#" + [[models]] + id = "local-mini" + provider_wire = "openai" + context_window = 64000 + max_output_tokens = 8192 + supports_reasoning = false + tier = "mini" + "#}; + std::fs::write(file.path(), raw).unwrap(); + + let catalog = ModelCatalog::load_file(file.path()).unwrap(); + assert_eq!(catalog.get("local-mini").unwrap().tier, ModelTier::Mini); + + let err = ModelCatalog::load_file("/nonexistent/ox/models.toml").unwrap_err(); + assert!(matches!(err, CatalogError::Io { .. })); + } + + #[test] + fn merge_overrides_existing_entries_and_adds_new_ones() { + let mut catalog = ModelCatalog::builtin(); + let overlay = ModelCatalog::from_toml(indoc! {r#" + [[models]] + id = "gemini-4-hydrogen" + provider_wire = "google" + context_window = 2097152 + max_output_tokens = 131072 + supports_reasoning = true + tier = "flagship" + + [[models]] + id = "local-standard" + provider_wire = "openai" + context_window = 131072 + max_output_tokens = 16384 + supports_reasoning = true + tier = "standard" + "#}) + .unwrap(); + + catalog.merge(overlay); + + assert_eq!( + catalog.get("gemini-4-hydrogen").unwrap().context_window, + 2_097_152 + ); + assert_eq!( + catalog.get("local-standard").unwrap().wire, + ProviderWire::OpenAi + ); + } + + #[test] + fn resolve_tier_honors_overrides_and_falls_back_to_tier_default() { + let catalog = ModelCatalog::builtin(); + let empty = BTreeMap::new(); + + let default_flagship = catalog.resolve_tier(ModelTier::Flagship, &empty).unwrap(); + assert_eq!(default_flagship.tier, ModelTier::Flagship); + + let mut overrides = BTreeMap::new(); + overrides.insert("flagship".into(), "gpt-6.2".into()); + let chosen = catalog + .resolve_tier(ModelTier::Flagship, &overrides) + .unwrap(); + assert_eq!(chosen.id, "gpt-6.2"); + + overrides.insert("mini".into(), "missing-model".into()); + assert!(catalog.resolve_tier(ModelTier::Mini, &overrides).is_none()); + } +} diff --git a/crates/ox-core/src/config.rs b/crates/ox-core/src/config.rs index 166c00074..9dd38359e 100644 --- a/crates/ox-core/src/config.rs +++ b/crates/ox-core/src/config.rs @@ -218,6 +218,9 @@ pub struct Config { /// Named model definitions. #[serde(default)] pub models: BTreeMap, + /// Optional tier-to-model overrides mapping `"flagship"`, `"standard"`, or `"mini"` to a model ID. + #[serde(default)] + pub profiles: BTreeMap, /// Optional default model key into [`Config::models`]. pub default_model: Option, /// Optional TUI theme name (built-in palette) or path to a TOML theme file. @@ -459,6 +462,11 @@ mod tests { max_output_tokens = 4096 reasoning_effort = "high" + [profiles] + flagship = "gemini-4-hydrogen" + standard = "claude-sonnet-5-5" + mini = "gpt-6.2-mini" + [[hooks]] event = "PreToolUse" matcher = "bash" @@ -485,6 +493,18 @@ mod tests { cfg.models["fast"].reasoning_effort, Some(ReasoningEffort::High) ); + assert_eq!( + cfg.profiles.get("flagship").map(String::as_str), + Some("gemini-4-hydrogen") + ); + assert_eq!( + cfg.profiles.get("standard").map(String::as_str), + Some("claude-sonnet-5-5") + ); + assert_eq!( + cfg.profiles.get("mini").map(String::as_str), + Some("gpt-6.2-mini") + ); assert_eq!(cfg.hooks.len(), 1); assert_eq!(cfg.hooks[0].event, crate::hooks::HookEvent::PreToolUse); assert_eq!(cfg.hooks[0].matcher.as_deref(), Some("bash")); diff --git a/crates/ox-core/src/lib.rs b/crates/ox-core/src/lib.rs index 24e729cce..cdd23ebd8 100644 --- a/crates/ox-core/src/lib.rs +++ b/crates/ox-core/src/lib.rs @@ -1,6 +1,7 @@ //! Wire-neutral core types, configuration, and streaming LLM clients for `oxide-code`. pub mod agent; +pub mod catalog; pub mod client; pub mod config; pub mod frontmatter; diff --git a/docs/README.md b/docs/README.md index 65758bc7f..f260b8140 100644 --- a/docs/README.md +++ b/docs/README.md @@ -21,6 +21,7 @@ Documentation for `oxide-code` (`ox`) is organized into three user-facing bucket | [Configuration](reference/configuration.md) | `config.toml` schema for providers, credentials, and model entries | | [Hooks](reference/hooks.md) | Out-of-process command hooks, lifecycle events, and error policy | | [MCP Plugin](reference/mcp-plugin.md) | `ox-plugin-mcp` stdio and Streamable HTTP MCP bridge configuration | +| [Models](reference/models.md) | Data-driven model catalog TOML schema and tier profiles | | [Skills](reference/skills.md) | `SKILL.md` discovery roots, frontmatter schema, and `load_skill` | | [Themes](reference/themes.md) | Built-in palettes and custom TOML theme file schema | diff --git a/docs/reference/models.md b/docs/reference/models.md new file mode 100644 index 000000000..583fb0c99 --- /dev/null +++ b/docs/reference/models.md @@ -0,0 +1,49 @@ +# Model Catalog and Profiles + +`ox-core` loads model metadata from TOML data via `ModelCatalog::builtin`, `ModelCatalog::from_toml`, and `ModelCatalog::load_file`. The built-in catalog dogfoods the same schema and lives in [`crates/ox-core/catalog/models.toml`](../../crates/ox-core/catalog/models.toml). + +## Catalog Schema + +Each entry in a catalog file is a `[[models]]` table. Unknown keys are rejected. + +```toml +[[models]] +id = "gemini-4-hydrogen" +provider_wire = "google" +context_window = 1048576 +max_output_tokens = 65536 +supports_reasoning = true +tier = "flagship" +``` + +| Field | Type | Required | Description | +| -------------------- | ------- | -------- | --------------------------------------------------------- | +| `id` | string | yes | Unique wire model identifier | +| `provider_wire` | string | yes | Wire protocol (`google`, `openai`, or `anthropic`) | +| `context_window` | integer | yes | Maximum input context window in tokens | +| `max_output_tokens` | integer | yes | Maximum output tokens per response | +| `supports_reasoning` | boolean | no | Whether reasoning effort controls apply (default `false`) | +| `tier` | string | yes | Profile tier (`flagship`, `standard`, or `mini`) | + +## Extending or Overriding the Built-in Catalog + +Load a custom TOML file with `ModelCatalog::load_file` and call `ModelCatalog::merge` on `ModelCatalog::builtin()`. Matching `id` entries replace the built-in definitions, and new `id` entries are added. + +## Tier Profiles + +Models are grouped into three capability tiers: + +- **`flagship`**: Highest-capability frontier models for complex tasks. +- **`standard`**: Balanced general-purpose models for everyday coding. +- **`mini`**: Fast, low-cost models for lightweight or high-volume tasks. + +Override the default model selected for any tier in `config.toml` under `[profiles]`: + +```toml +[profiles] +flagship = "gemini-4-hydrogen" +standard = "claude-sonnet-5-5" +mini = "gpt-6.2-mini" +``` + +`ModelCatalog::resolve_tier(tier, &config.profiles)` looks up the configured override ID when present and otherwise returns the first catalog entry in that tier. From d6f7cf13a65d73364b95eeb3545567889966d844 Mon Sep 17 00:00:00 2001 From: Hakula Chen Date: Tue, 1 Dec 2026 18:30:04 +0800 Subject: [PATCH 2/3] fix(ox-core): use real model IDs in catalog and clarify tier fallback order --- crates/ox-core/catalog/models.toml | 22 +++++++++++++++------- crates/ox-core/src/catalog.rs | 24 ++++++++++++------------ crates/ox-core/src/config.rs | 8 ++++---- docs/reference/models.md | 8 ++++---- 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/crates/ox-core/catalog/models.toml b/crates/ox-core/catalog/models.toml index b12efe6d4..61246fbb5 100644 --- a/crates/ox-core/catalog/models.toml +++ b/crates/ox-core/catalog/models.toml @@ -1,5 +1,5 @@ [[models]] -id = "gemini-4-hydrogen" +id = "gemini-4-neon" provider_wire = "google" context_window = 1048576 max_output_tokens = 65536 @@ -7,7 +7,7 @@ supports_reasoning = true tier = "flagship" [[models]] -id = "gemini-4-neon" +id = "gemini-4-carbon" provider_wire = "google" context_window = 1048576 max_output_tokens = 65536 @@ -15,15 +15,15 @@ supports_reasoning = true tier = "standard" [[models]] -id = "gemini-4-helium" +id = "gemini-4-hydrogen" provider_wire = "google" context_window = 1048576 -max_output_tokens = 32768 -supports_reasoning = false +max_output_tokens = 65536 +supports_reasoning = true tier = "mini" [[models]] -id = "gpt-6.2" +id = "gpt-6.2-astra" provider_wire = "openai" context_window = 400000 max_output_tokens = 128000 @@ -31,7 +31,15 @@ supports_reasoning = true tier = "flagship" [[models]] -id = "gpt-6.2-mini" +id = "gpt-6.2-sol" +provider_wire = "openai" +context_window = 400000 +max_output_tokens = 128000 +supports_reasoning = true +tier = "standard" + +[[models]] +id = "gpt-6-luna" provider_wire = "openai" context_window = 400000 max_output_tokens = 65536 diff --git a/crates/ox-core/src/catalog.rs b/crates/ox-core/src/catalog.rs index e91c4eac4..4a6243a6e 100644 --- a/crates/ox-core/src/catalog.rs +++ b/crates/ox-core/src/catalog.rs @@ -78,7 +78,7 @@ impl FromStr for ModelTier { #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct CatalogModel { - /// Unique wire model identifier (for example `gemini-4-hydrogen`). + /// Unique wire model identifier (for example `gemini-4-neon`). pub id: String, /// Provider wire protocol used to talk to this model. #[serde(rename = "provider_wire")] @@ -203,23 +203,23 @@ mod tests { fn builtin_catalog_loads_expected_models_and_tiers() { let catalog = ModelCatalog::builtin(); - let hydrogen = catalog.get("gemini-4-hydrogen").unwrap(); - assert_eq!(hydrogen.wire, ProviderWire::Google); - assert_eq!(hydrogen.tier, ModelTier::Flagship); - assert!(hydrogen.supports_reasoning); + let neon = catalog.get("gemini-4-neon").unwrap(); + assert_eq!(neon.wire, ProviderWire::Google); + assert_eq!(neon.tier, ModelTier::Flagship); + assert!(neon.supports_reasoning); - let gpt = catalog.get("gpt-6.2").unwrap(); - assert_eq!(gpt.wire, ProviderWire::OpenAi); - assert_eq!(gpt.tier, ModelTier::Flagship); + let astra = catalog.get("gpt-6.2-astra").unwrap(); + assert_eq!(astra.wire, ProviderWire::OpenAi); + assert_eq!(astra.tier, ModelTier::Flagship); let sonnet = catalog.get("claude-sonnet-5-5").unwrap(); assert_eq!(sonnet.wire, ProviderWire::Anthropic); assert_eq!(sonnet.tier, ModelTier::Standard); assert_eq!(catalog.by_tier(ModelTier::Flagship).len(), 3); - assert_eq!(catalog.by_tier(ModelTier::Standard).len(), 2); + assert_eq!(catalog.by_tier(ModelTier::Standard).len(), 3); assert_eq!(catalog.by_tier(ModelTier::Mini).len(), 3); - assert_eq!(catalog.models().count(), 8); + assert_eq!(catalog.models().count(), 9); } #[test] @@ -320,11 +320,11 @@ mod tests { assert_eq!(default_flagship.tier, ModelTier::Flagship); let mut overrides = BTreeMap::new(); - overrides.insert("flagship".into(), "gpt-6.2".into()); + overrides.insert("flagship".into(), "gpt-6.2-astra".into()); let chosen = catalog .resolve_tier(ModelTier::Flagship, &overrides) .unwrap(); - assert_eq!(chosen.id, "gpt-6.2"); + assert_eq!(chosen.id, "gpt-6.2-astra"); overrides.insert("mini".into(), "missing-model".into()); assert!(catalog.resolve_tier(ModelTier::Mini, &overrides).is_none()); diff --git a/crates/ox-core/src/config.rs b/crates/ox-core/src/config.rs index 9dd38359e..d12251381 100644 --- a/crates/ox-core/src/config.rs +++ b/crates/ox-core/src/config.rs @@ -463,9 +463,9 @@ mod tests { reasoning_effort = "high" [profiles] - flagship = "gemini-4-hydrogen" + flagship = "gemini-4-neon" standard = "claude-sonnet-5-5" - mini = "gpt-6.2-mini" + mini = "gpt-6-luna" [[hooks]] event = "PreToolUse" @@ -495,7 +495,7 @@ mod tests { ); assert_eq!( cfg.profiles.get("flagship").map(String::as_str), - Some("gemini-4-hydrogen") + Some("gemini-4-neon") ); assert_eq!( cfg.profiles.get("standard").map(String::as_str), @@ -503,7 +503,7 @@ mod tests { ); assert_eq!( cfg.profiles.get("mini").map(String::as_str), - Some("gpt-6.2-mini") + Some("gpt-6-luna") ); assert_eq!(cfg.hooks.len(), 1); assert_eq!(cfg.hooks[0].event, crate::hooks::HookEvent::PreToolUse); diff --git a/docs/reference/models.md b/docs/reference/models.md index 583fb0c99..ddbcbc0da 100644 --- a/docs/reference/models.md +++ b/docs/reference/models.md @@ -8,7 +8,7 @@ Each entry in a catalog file is a `[[models]]` table. Unknown keys are rejected. ```toml [[models]] -id = "gemini-4-hydrogen" +id = "gemini-4-neon" provider_wire = "google" context_window = 1048576 max_output_tokens = 65536 @@ -41,9 +41,9 @@ Override the default model selected for any tier in `config.toml` under `[profil ```toml [profiles] -flagship = "gemini-4-hydrogen" +flagship = "gemini-4-neon" standard = "claude-sonnet-5-5" -mini = "gpt-6.2-mini" +mini = "gpt-6-luna" ``` -`ModelCatalog::resolve_tier(tier, &config.profiles)` looks up the configured override ID when present and otherwise returns the first catalog entry in that tier. +`ModelCatalog::resolve_tier(tier, &config.profiles)` looks up the configured override ID when present and otherwise returns the first catalog entry in that tier (in lexicographical ID order). From 789be02f9fdb165dec86ea31a773d9dae30b2719 Mon Sep 17 00:00:00 2001 From: Hakula Chen Date: Tue, 1 Dec 2026 18:38:40 +0800 Subject: [PATCH 3/3] refactor(ox-core): keep catalog opinion-free and resolve user-defined profiles in Config --- crates/ox-core/catalog/models.toml | 35 +++---- crates/ox-core/src/catalog.rs | 144 +++-------------------------- crates/ox-core/src/config.rs | 36 ++++---- docs/reference/models.md | 28 +++--- 4 files changed, 55 insertions(+), 188 deletions(-) diff --git a/crates/ox-core/catalog/models.toml b/crates/ox-core/catalog/models.toml index 61246fbb5..a4d85cd8d 100644 --- a/crates/ox-core/catalog/models.toml +++ b/crates/ox-core/catalog/models.toml @@ -1,18 +1,16 @@ [[models]] -id = "gemini-4-neon" +id = "gemini-4-carbon" provider_wire = "google" context_window = 1048576 max_output_tokens = 65536 supports_reasoning = true -tier = "flagship" [[models]] -id = "gemini-4-carbon" +id = "gemini-4-neon" provider_wire = "google" context_window = 1048576 max_output_tokens = 65536 supports_reasoning = true -tier = "standard" [[models]] id = "gemini-4-hydrogen" @@ -20,52 +18,45 @@ provider_wire = "google" context_window = 1048576 max_output_tokens = 65536 supports_reasoning = true -tier = "mini" [[models]] id = "gpt-6.2-astra" provider_wire = "openai" -context_window = 400000 +context_window = 1050000 max_output_tokens = 128000 supports_reasoning = true -tier = "flagship" [[models]] id = "gpt-6.2-sol" provider_wire = "openai" -context_window = 400000 +context_window = 1050000 max_output_tokens = 128000 supports_reasoning = true -tier = "standard" [[models]] id = "gpt-6-luna" provider_wire = "openai" -context_window = 400000 -max_output_tokens = 65536 +context_window = 1050000 +max_output_tokens = 128000 supports_reasoning = true -tier = "mini" [[models]] id = "claude-opus-5-5" provider_wire = "anthropic" -context_window = 200000 -max_output_tokens = 64000 +context_window = 1000000 +max_output_tokens = 128000 supports_reasoning = true -tier = "flagship" [[models]] id = "claude-sonnet-5-5" provider_wire = "anthropic" -context_window = 200000 -max_output_tokens = 64000 +context_window = 1000000 +max_output_tokens = 128000 supports_reasoning = true -tier = "standard" [[models]] id = "claude-haiku-5-5" provider_wire = "anthropic" -context_window = 200000 -max_output_tokens = 32000 -supports_reasoning = false -tier = "mini" +context_window = 1000000 +max_output_tokens = 128000 +supports_reasoning = true diff --git a/crates/ox-core/src/catalog.rs b/crates/ox-core/src/catalog.rs index 4a6243a6e..9785f7f0d 100644 --- a/crates/ox-core/src/catalog.rs +++ b/crates/ox-core/src/catalog.rs @@ -1,9 +1,7 @@ -//! Data-driven model catalog and tier profile resolution. +//! Data-driven model catalog storing objective model metadata. use std::collections::BTreeMap; -use std::fmt; use std::path::{Path, PathBuf}; -use std::str::FromStr; use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -12,7 +10,7 @@ use crate::config::ProviderWire; const BUILTIN_CATALOG_TOML: &str = include_str!("../catalog/models.toml"); -/// Errors produced while loading or querying a model catalog. +/// Errors produced while loading a model catalog. #[derive(Debug, Error)] pub enum CatalogError { /// Failed to read a catalog file from disk. @@ -26,59 +24,13 @@ pub enum CatalogError { /// Failed to parse TOML catalog data. #[error("failed to parse model catalog: {0}")] Toml(#[from] toml::de::Error), - /// Tier name did not match any known [`ModelTier`]. - #[error("unknown model tier `{0}` (expected `flagship`, `standard`, or `mini`)")] - UnknownTier(String), } -/// Capability and cost tier assigned to a catalog model. -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum ModelTier { - /// Highest-capability frontier model for complex tasks. - Flagship, - /// Balanced general-purpose model for everyday coding. - Standard, - /// Fast, low-cost model for lightweight or high-volume tasks. - Mini, -} - -impl ModelTier { - /// Returns the canonical lowercase name for this tier. - #[must_use] - pub const fn as_str(self) -> &'static str { - match self { - Self::Flagship => "flagship", - Self::Standard => "standard", - Self::Mini => "mini", - } - } -} - -impl fmt::Display for ModelTier { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(self.as_str()) - } -} - -impl FromStr for ModelTier { - type Err = CatalogError; - - fn from_str(s: &str) -> Result { - match s { - "flagship" => Ok(Self::Flagship), - "standard" => Ok(Self::Standard), - "mini" => Ok(Self::Mini), - other => Err(CatalogError::UnknownTier(other.to_owned())), - } - } -} - -/// Metadata for a single model in a [`ModelCatalog`]. +/// Objective metadata for a single model in a [`ModelCatalog`]. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct CatalogModel { - /// Unique wire model identifier (for example `gemini-4-neon`). + /// Unique wire model identifier (for example `gemini-4-carbon`). pub id: String, /// Provider wire protocol used to talk to this model. #[serde(rename = "provider_wire")] @@ -90,8 +42,6 @@ pub struct CatalogModel { /// Whether the model supports explicit reasoning effort controls. #[serde(default)] pub supports_reasoning: bool, - /// Profile tier assigned to this model. - pub tier: ModelTier, } #[derive(Debug, Deserialize)] @@ -149,31 +99,6 @@ impl ModelCatalog { pub fn models(&self) -> impl Iterator { self.models.values() } - - /// Returns all models assigned to the given tier in deterministic ID order. - #[must_use] - pub fn by_tier(&self, tier: ModelTier) -> Vec<&CatalogModel> { - self.models - .values() - .filter(|model| model.tier == tier) - .collect() - } - - /// Resolves a [`ModelTier`] to a [`CatalogModel`]. - /// - /// When `overrides` contains an entry for `tier.as_str()`, that model ID is looked up directly - /// in this catalog. Otherwise, the first catalog entry matching `tier` is returned. - #[must_use] - pub fn resolve_tier( - &self, - tier: ModelTier, - overrides: &BTreeMap, - ) -> Option<&CatalogModel> { - if let Some(override_id) = overrides.get(tier.as_str()) { - return self.get(override_id); - } - self.models.values().find(|model| model.tier == tier) - } } #[cfg(test)] @@ -184,41 +109,19 @@ mod tests { use super::*; #[test] - fn model_tier_display_and_from_str_roundtrip() { - for (tier, name) in [ - (ModelTier::Flagship, "flagship"), - (ModelTier::Standard, "standard"), - (ModelTier::Mini, "mini"), - ] { - assert_eq!(tier.as_str(), name); - assert_eq!(tier.to_string(), name); - assert_eq!(name.parse::().unwrap(), tier); - } - - let err = "ultra".parse::().unwrap_err(); - assert!(matches!(err, CatalogError::UnknownTier(s) if s == "ultra")); - } - - #[test] - fn builtin_catalog_loads_expected_models_and_tiers() { + fn builtin_catalog_loads_expected_models() { let catalog = ModelCatalog::builtin(); - let neon = catalog.get("gemini-4-neon").unwrap(); - assert_eq!(neon.wire, ProviderWire::Google); - assert_eq!(neon.tier, ModelTier::Flagship); - assert!(neon.supports_reasoning); + let carbon = catalog.get("gemini-4-carbon").unwrap(); + assert_eq!(carbon.wire, ProviderWire::Google); + assert!(carbon.supports_reasoning); let astra = catalog.get("gpt-6.2-astra").unwrap(); assert_eq!(astra.wire, ProviderWire::OpenAi); - assert_eq!(astra.tier, ModelTier::Flagship); let sonnet = catalog.get("claude-sonnet-5-5").unwrap(); assert_eq!(sonnet.wire, ProviderWire::Anthropic); - assert_eq!(sonnet.tier, ModelTier::Standard); - assert_eq!(catalog.by_tier(ModelTier::Flagship).len(), 3); - assert_eq!(catalog.by_tier(ModelTier::Standard).len(), 3); - assert_eq!(catalog.by_tier(ModelTier::Mini).len(), 3); assert_eq!(catalog.models().count(), 9); } @@ -230,7 +133,6 @@ mod tests { provider_wire = "openai" context_window = 128000 max_output_tokens = 16384 - tier = "standard" "#}; let catalog = ModelCatalog::from_toml(raw).unwrap(); @@ -239,7 +141,6 @@ mod tests { assert_eq!(model.context_window, 128_000); assert_eq!(model.max_output_tokens, 16_384); assert!(!model.supports_reasoning); - assert_eq!(model.tier, ModelTier::Standard); let bad = indoc! {r#" [[models]] @@ -247,7 +148,6 @@ mod tests { provider_wire = "google" context_window = 1000 max_output_tokens = 500 - tier = "mini" unknown_key = true "#}; assert!(matches!( @@ -266,12 +166,11 @@ mod tests { context_window = 64000 max_output_tokens = 8192 supports_reasoning = false - tier = "mini" "#}; std::fs::write(file.path(), raw).unwrap(); let catalog = ModelCatalog::load_file(file.path()).unwrap(); - assert_eq!(catalog.get("local-mini").unwrap().tier, ModelTier::Mini); + assert_eq!(catalog.get("local-mini").unwrap().context_window, 64_000); let err = ModelCatalog::load_file("/nonexistent/ox/models.toml").unwrap_err(); assert!(matches!(err, CatalogError::Io { .. })); @@ -282,12 +181,11 @@ mod tests { let mut catalog = ModelCatalog::builtin(); let overlay = ModelCatalog::from_toml(indoc! {r#" [[models]] - id = "gemini-4-hydrogen" + id = "gemini-4-carbon" provider_wire = "google" context_window = 2097152 max_output_tokens = 131072 supports_reasoning = true - tier = "flagship" [[models]] id = "local-standard" @@ -295,14 +193,13 @@ mod tests { context_window = 131072 max_output_tokens = 16384 supports_reasoning = true - tier = "standard" "#}) .unwrap(); catalog.merge(overlay); assert_eq!( - catalog.get("gemini-4-hydrogen").unwrap().context_window, + catalog.get("gemini-4-carbon").unwrap().context_window, 2_097_152 ); assert_eq!( @@ -310,23 +207,4 @@ mod tests { ProviderWire::OpenAi ); } - - #[test] - fn resolve_tier_honors_overrides_and_falls_back_to_tier_default() { - let catalog = ModelCatalog::builtin(); - let empty = BTreeMap::new(); - - let default_flagship = catalog.resolve_tier(ModelTier::Flagship, &empty).unwrap(); - assert_eq!(default_flagship.tier, ModelTier::Flagship); - - let mut overrides = BTreeMap::new(); - overrides.insert("flagship".into(), "gpt-6.2-astra".into()); - let chosen = catalog - .resolve_tier(ModelTier::Flagship, &overrides) - .unwrap(); - assert_eq!(chosen.id, "gpt-6.2-astra"); - - overrides.insert("mini".into(), "missing-model".into()); - assert!(catalog.resolve_tier(ModelTier::Mini, &overrides).is_none()); - } } diff --git a/crates/ox-core/src/config.rs b/crates/ox-core/src/config.rs index d12251381..31ae9001a 100644 --- a/crates/ox-core/src/config.rs +++ b/crates/ox-core/src/config.rs @@ -218,7 +218,8 @@ pub struct Config { /// Named model definitions. #[serde(default)] pub models: BTreeMap, - /// Optional tier-to-model overrides mapping `"flagship"`, `"standard"`, or `"mini"` to a model ID. + /// User-defined named profiles (for example `flagship`, `standard`, or `mini`) mapping to a + /// model alias or ID. #[serde(default)] pub profiles: BTreeMap, /// Optional default model key into [`Config::models`]. @@ -238,6 +239,16 @@ impl Config { pub fn from_toml(input: &str) -> Result { Ok(toml::from_str(input)?) } + + /// Resolves a named profile to its configured model alias or ID, falling back to + /// [`Config::default_model`] when the profile is not defined. + #[must_use] + pub fn resolve_profile(&self, name: &str) -> Option<&str> { + self.profiles + .get(name) + .map(String::as_str) + .or(self.default_model.as_deref()) + } } #[cfg(test)] @@ -463,9 +474,9 @@ mod tests { reasoning_effort = "high" [profiles] - flagship = "gemini-4-neon" - standard = "claude-sonnet-5-5" - mini = "gpt-6-luna" + flagship = "gemini-4-carbon" + standard = "gemini-4-neon" + mini = "gemini-4-hydrogen" [[hooks]] event = "PreToolUse" @@ -493,18 +504,11 @@ mod tests { cfg.models["fast"].reasoning_effort, Some(ReasoningEffort::High) ); - assert_eq!( - cfg.profiles.get("flagship").map(String::as_str), - Some("gemini-4-neon") - ); - assert_eq!( - cfg.profiles.get("standard").map(String::as_str), - Some("claude-sonnet-5-5") - ); - assert_eq!( - cfg.profiles.get("mini").map(String::as_str), - Some("gpt-6-luna") - ); + assert_eq!(cfg.resolve_profile("flagship"), Some("gemini-4-carbon")); + assert_eq!(cfg.resolve_profile("standard"), Some("gemini-4-neon")); + assert_eq!(cfg.resolve_profile("mini"), Some("gemini-4-hydrogen")); + assert_eq!(cfg.resolve_profile("missing"), Some("fast")); + assert_eq!(Config::default().resolve_profile("flagship"), None); assert_eq!(cfg.hooks.len(), 1); assert_eq!(cfg.hooks[0].event, crate::hooks::HookEvent::PreToolUse); assert_eq!(cfg.hooks[0].matcher.as_deref(), Some("bash")); diff --git a/docs/reference/models.md b/docs/reference/models.md index ddbcbc0da..b3f393758 100644 --- a/docs/reference/models.md +++ b/docs/reference/models.md @@ -1,19 +1,18 @@ # Model Catalog and Profiles -`ox-core` loads model metadata from TOML data via `ModelCatalog::builtin`, `ModelCatalog::from_toml`, and `ModelCatalog::load_file`. The built-in catalog dogfoods the same schema and lives in [`crates/ox-core/catalog/models.toml`](../../crates/ox-core/catalog/models.toml). +`ox-core` loads objective model metadata from TOML data via `ModelCatalog::builtin`, `ModelCatalog::from_toml`, and `ModelCatalog::load_file`. The built-in catalog dogfoods the same schema and lives in [`crates/ox-core/catalog/models.toml`](../../crates/ox-core/catalog/models.toml). ## Catalog Schema -Each entry in a catalog file is a `[[models]]` table. Unknown keys are rejected. +Each entry in a catalog file is a `[[models]]` table storing objective model capabilities. Unknown keys are rejected. ```toml [[models]] -id = "gemini-4-neon" +id = "gemini-4-carbon" provider_wire = "google" context_window = 1048576 max_output_tokens = 65536 supports_reasoning = true -tier = "flagship" ``` | Field | Type | Required | Description | @@ -23,27 +22,22 @@ tier = "flagship" | `context_window` | integer | yes | Maximum input context window in tokens | | `max_output_tokens` | integer | yes | Maximum output tokens per response | | `supports_reasoning` | boolean | no | Whether reasoning effort controls apply (default `false`) | -| `tier` | string | yes | Profile tier (`flagship`, `standard`, or `mini`) | ## Extending or Overriding the Built-in Catalog Load a custom TOML file with `ModelCatalog::load_file` and call `ModelCatalog::merge` on `ModelCatalog::builtin()`. Matching `id` entries replace the built-in definitions, and new `id` entries are added. -## Tier Profiles +## User-Defined Profiles -Models are grouped into three capability tiers: - -- **`flagship`**: Highest-capability frontier models for complex tasks. -- **`standard`**: Balanced general-purpose models for everyday coding. -- **`mini`**: Fast, low-cost models for lightweight or high-volume tasks. - -Override the default model selected for any tier in `config.toml` under `[profiles]`: +The built-in catalog does not assign tier preferences. Define named profiles such as `flagship`, `standard`, `mini`, or any custom name in `config.toml` under `[profiles]`: ```toml +default_model = "gemini-4-neon" + [profiles] -flagship = "gemini-4-neon" -standard = "claude-sonnet-5-5" -mini = "gpt-6-luna" +flagship = "gemini-4-carbon" +standard = "gemini-4-neon" +mini = "gemini-4-hydrogen" ``` -`ModelCatalog::resolve_tier(tier, &config.profiles)` looks up the configured override ID when present and otherwise returns the first catalog entry in that tier (in lexicographical ID order). +`Config::resolve_profile(name)` returns the model alias or ID configured for `name`, falling back to `default_model` when that profile is not defined.