diff --git a/crates/ox-core/catalog/models.toml b/crates/ox-core/catalog/models.toml new file mode 100644 index 000000000..a4d85cd8d --- /dev/null +++ b/crates/ox-core/catalog/models.toml @@ -0,0 +1,62 @@ +[[models]] +id = "gemini-4-carbon" +provider_wire = "google" +context_window = 1048576 +max_output_tokens = 65536 +supports_reasoning = true + +[[models]] +id = "gemini-4-neon" +provider_wire = "google" +context_window = 1048576 +max_output_tokens = 65536 +supports_reasoning = true + +[[models]] +id = "gemini-4-hydrogen" +provider_wire = "google" +context_window = 1048576 +max_output_tokens = 65536 +supports_reasoning = true + +[[models]] +id = "gpt-6.2-astra" +provider_wire = "openai" +context_window = 1050000 +max_output_tokens = 128000 +supports_reasoning = true + +[[models]] +id = "gpt-6.2-sol" +provider_wire = "openai" +context_window = 1050000 +max_output_tokens = 128000 +supports_reasoning = true + +[[models]] +id = "gpt-6-luna" +provider_wire = "openai" +context_window = 1050000 +max_output_tokens = 128000 +supports_reasoning = true + +[[models]] +id = "claude-opus-5-5" +provider_wire = "anthropic" +context_window = 1000000 +max_output_tokens = 128000 +supports_reasoning = true + +[[models]] +id = "claude-sonnet-5-5" +provider_wire = "anthropic" +context_window = 1000000 +max_output_tokens = 128000 +supports_reasoning = true + +[[models]] +id = "claude-haiku-5-5" +provider_wire = "anthropic" +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 new file mode 100644 index 000000000..9785f7f0d --- /dev/null +++ b/crates/ox-core/src/catalog.rs @@ -0,0 +1,210 @@ +//! Data-driven model catalog storing objective model metadata. + +use std::collections::BTreeMap; +use std::path::{Path, PathBuf}; + +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 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), +} + +/// 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-carbon`). + 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, +} + +#[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() + } +} + +#[cfg(test)] +mod tests { + use indoc::indoc; + use tempfile::NamedTempFile; + + use super::*; + + #[test] + fn builtin_catalog_loads_expected_models() { + let catalog = ModelCatalog::builtin(); + + 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); + + let sonnet = catalog.get("claude-sonnet-5-5").unwrap(); + assert_eq!(sonnet.wire, ProviderWire::Anthropic); + + assert_eq!(catalog.models().count(), 9); + } + + #[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 + "#}; + + 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); + + let bad = indoc! {r#" + [[models]] + id = "bad" + provider_wire = "google" + context_window = 1000 + max_output_tokens = 500 + 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 + "#}; + std::fs::write(file.path(), raw).unwrap(); + + let catalog = ModelCatalog::load_file(file.path()).unwrap(); + 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 { .. })); + } + + #[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-carbon" + provider_wire = "google" + context_window = 2097152 + max_output_tokens = 131072 + supports_reasoning = true + + [[models]] + id = "local-standard" + provider_wire = "openai" + context_window = 131072 + max_output_tokens = 16384 + supports_reasoning = true + "#}) + .unwrap(); + + catalog.merge(overlay); + + assert_eq!( + catalog.get("gemini-4-carbon").unwrap().context_window, + 2_097_152 + ); + assert_eq!( + catalog.get("local-standard").unwrap().wire, + ProviderWire::OpenAi + ); + } +} diff --git a/crates/ox-core/src/config.rs b/crates/ox-core/src/config.rs index 166c00074..31ae9001a 100644 --- a/crates/ox-core/src/config.rs +++ b/crates/ox-core/src/config.rs @@ -218,6 +218,10 @@ pub struct Config { /// Named model definitions. #[serde(default)] pub models: BTreeMap, + /// 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`]. pub default_model: Option, /// Optional TUI theme name (built-in palette) or path to a TOML theme file. @@ -235,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)] @@ -459,6 +473,11 @@ mod tests { max_output_tokens = 4096 reasoning_effort = "high" + [profiles] + flagship = "gemini-4-carbon" + standard = "gemini-4-neon" + mini = "gemini-4-hydrogen" + [[hooks]] event = "PreToolUse" matcher = "bash" @@ -485,6 +504,11 @@ mod tests { cfg.models["fast"].reasoning_effort, Some(ReasoningEffort::High) ); + 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/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..b3f393758 --- /dev/null +++ b/docs/reference/models.md @@ -0,0 +1,43 @@ +# Model Catalog and Profiles + +`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 storing objective model capabilities. Unknown keys are rejected. + +```toml +[[models]] +id = "gemini-4-carbon" +provider_wire = "google" +context_window = 1048576 +max_output_tokens = 65536 +supports_reasoning = true +``` + +| 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`) | + +## 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. + +## User-Defined 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-carbon" +standard = "gemini-4-neon" +mini = "gemini-4-hydrogen" +``` + +`Config::resolve_profile(name)` returns the model alias or ID configured for `name`, falling back to `default_model` when that profile is not defined.