返回 CodeWhale
model_registry.rs
根目录 / crates / tui / src / model_registry.rs
1 //! Single source of model facts for CodeWhale (#3071, #3073).
2 //!
3 //! Historically, "what is this model's context window / max output / does it
4 //! reason?" was answered by several hard-coded sites:
5 //!
6 //! * [`crate::models::context_window_for_model`] /
7 //! the models module's context-window lookup for context windows,
8 //! * [`crate::models::max_output_tokens_for_model`] for output caps,
9 //! * [`crate::models::model_supports_reasoning`] for the reasoning flag,
10 //! * the `DEFAULT_*` model-id constants in `crates/config/src/lib.rs` for the
11 //! canonical model each provider ships by default.
12 //!
13 //! This module is the **foundation** for collapsing those into one place: a
14 //! [`ModelMetadata`] registry keyed by model id, plus a single [`lookup`]
15 //! entry point. It is intentionally *additive* — the existing call sites are
16 //! left untouched in this pass and will be migrated to consume the registry in
17 //! a later change (so behaviour is unchanged today).
18 //!
19 //! ## Seeding discipline (no drift)
20 //!
21 //! The registry does not re-declare context-window / max-output / reasoning
22 //! numbers. Instead it **seeds** each entry by calling the existing
23 //! `crate::models` functions, so the registry can never silently disagree with
24 //! `models.rs`. The canonical model ids come from the same provider defaults
25 //! the config crate ships (see [`SEED_MODEL_IDS`]). The
26 //! The `registry_context_window_matches_models_rs` drift guard then
27 //! re-asserts the equivalence for a sample so that if a future change replaces
28 //! a seed with a hard-coded literal, CI catches the drift immediately.
29 //!
30 //! Production consumers: [`crate::model_profile`] (capability bridge),
31 //! `crate::tui::model_picker` (picker hints), and
32 //! [`crate::fleet::capability_badges`] (Fleet setup/roster badges, #5038).
33
34 use std::collections::BTreeMap;
35 use std::sync::OnceLock;
36
37 use crate::models::{
38 context_window_for_model, max_output_tokens_for_model, model_supports_reasoning,
39 };
40
41 /// Coarse provider grouping for a model entry.
42 ///
43 /// This is deliberately a small, stable enum rather than a re-export of
44 /// `config::ApiProvider`: the registry's job is to answer "what kind of model
45 /// is this", and many models (Kimi, GLM, Qwen, …) are reachable through
46 /// several concrete providers. Routing decisions still live in
47 /// `config::ApiProvider` / `model_routing`; this is only a hint.
48 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
49 pub enum ModelProvider {
50 /// DeepSeek-family models (first-class; preserve full support).
51 DeepSeek,
52 /// Anthropic Claude models.
53 Anthropic,
54 /// OpenAI public API models (GPT-5.5 / GPT-5.6 families).
55 OpenAi,
56 /// OpenAI Codex route models (gpt-5*-codex).
57 OpenAiCodex,
58 /// Moonshot / Kimi models.
59 Moonshot,
60 /// Z.ai GLM models.
61 Zai,
62 /// MiniMax models.
63 Minimax,
64 /// Alibaba Qwen models.
65 Qwen,
66 /// Arcee Trinity models.
67 Arcee,
68 /// Together-hosted models with provider-owned wire identities.
69 Together,
70 /// Xiaomi MiMo models.
71 XiaomiMimo,
72 /// Meta Muse models.
73 Meta,
74 /// xAI / Grok models.
75 Xai,
76 /// Anything not otherwise classified (still gets real metadata via the
77 /// `models.rs` heuristics where possible).
78 Other,
79 }
80
81 /// One row of model facts, looked up in [`lookup`].
82 ///
83 /// All numeric fields are seeded from `crate::models` so they stay in lockstep
84 /// with the legacy lookups (see module docs).
85 #[derive(Debug, Clone, PartialEq, Eq)]
86 pub struct ModelMetadata {
87 /// Canonical model id as sent to the provider (e.g. `"deepseek-v4-pro"`).
88 pub id: &'static str,
89 /// Coarse provider grouping.
90 pub provider: ModelProvider,
91 /// Approximate context window in tokens, if known.
92 pub context_window: Option<u32>,
93 /// Approximate maximum output tokens, if known.
94 pub max_output: Option<u32>,
95 /// Whether the model emits reasoning / thinking content that must be kept
96 /// out of answer prose.
97 pub supports_reasoning: bool,
98 }
99
100 impl ModelMetadata {
101 /// Build a metadata row for `id` by seeding every fact from the existing
102 /// `crate::models` lookups. This is the only constructor, which is what
103 /// keeps the registry from drifting away from `models.rs`.
104 fn seed(id: &'static str, provider: ModelProvider) -> Self {
105 Self {
106 id,
107 provider,
108 context_window: context_window_for_model(id),
109 max_output: max_output_tokens_for_model(id),
110 supports_reasoning: model_supports_reasoning(id),
111 }
112 }
113 }
114
115 /// Canonical `(model id, provider)` seeds for the registry.
116 ///
117 /// These mirror the provider defaults shipped by `crates/config/src/lib.rs`
118 /// (the `DEFAULT_*_MODEL` constants) plus the explicitly-enumerated models in
119 /// the models module's context-window lookup. Keep this list curated:
120 /// it is the set of models we make first-class promises about. Unknown ids are
121 /// still answered by [`lookup`] via the `models.rs` heuristics, they just are
122 /// not pre-seeded here.
123 const SEED_MODEL_IDS: &[(&str, ModelProvider)] = &[
124 // --- DeepSeek (first-class; config DEFAULT_DEEPSEEK_MODEL / NIM / OpenAI
125 // / Atlascloud / Novita / Fireworks / Siliconflow / SGLang / vLLM /
126 // Huggingface / Together / Volcengine / WanjieArk / Ollama defaults) ---
127 ("deepseek-v4-pro", ModelProvider::DeepSeek),
128 ("deepseek-v4-flash", ModelProvider::DeepSeek),
129 ("deepseek-ai/deepseek-v4-pro", ModelProvider::DeepSeek),
130 ("deepseek-ai/deepseek-v4-flash", ModelProvider::DeepSeek),
131 ("deepseek/deepseek-v4-pro", ModelProvider::DeepSeek),
132 ("deepseek/deepseek-v4-flash", ModelProvider::DeepSeek),
133 ("deepseek-reasoner", ModelProvider::DeepSeek),
134 ("deepseek-coder:1.3b", ModelProvider::DeepSeek),
135 // --- Anthropic (config DEFAULT_ANTHROPIC_MODEL + models.rs rows) ---
136 ("claude-opus-4-8", ModelProvider::Anthropic),
137 ("claude-sonnet-4-6", ModelProvider::Anthropic),
138 ("claude-sonnet-5", ModelProvider::Anthropic),
139 ("claude-fable-5", ModelProvider::Anthropic),
140 ("claude-haiku-4-5", ModelProvider::Anthropic),
141 // --- OpenAI public API + Codex (config DEFAULT_OPENAI_CODEX_MODEL) ---
142 ("gpt-5.5", ModelProvider::OpenAi),
143 ("gpt-5.5-pro", ModelProvider::OpenAi),
144 ("gpt-5.6", ModelProvider::OpenAi),
145 ("gpt-5.6-sol", ModelProvider::OpenAi),
146 ("gpt-5.6-terra", ModelProvider::OpenAi),
147 ("gpt-5.6-luna", ModelProvider::OpenAi),
148 ("gpt-5-codex", ModelProvider::OpenAiCodex),
149 ("gpt-5.3-codex", ModelProvider::OpenAi),
150 // --- Moonshot / Kimi (config DEFAULT_MOONSHOT_MODEL / KIMI_CODE) ---
151 ("kimi-k2.7-code", ModelProvider::Moonshot),
152 ("kimi-k2.6", ModelProvider::Moonshot),
153 ("kimi-for-coding", ModelProvider::Moonshot),
154 ("moonshotai/kimi-k2.7-code", ModelProvider::Moonshot),
155 ("moonshotai/kimi-k2.6", ModelProvider::Moonshot),
156 // --- Z.ai GLM (config DEFAULT_ZAI_MODEL) ---
157 ("z-ai/glm-5.1", ModelProvider::Zai),
158 ("z-ai/glm-5.2", ModelProvider::Zai),
159 ("z-ai/glm-5.3", ModelProvider::Zai),
160 ("glm-5.1", ModelProvider::Zai),
161 ("glm-5.2", ModelProvider::Zai),
162 ("glm-5.3", ModelProvider::Zai),
163 // --- MiniMax (config DEFAULT_MINIMAX_MODEL) ---
164 ("minimax/minimax-m3", ModelProvider::Minimax),
165 ("minimax-m3", ModelProvider::Minimax),
166 ("minimax/minimax-m2.7", ModelProvider::Minimax),
167 ("minimax-m2.7", ModelProvider::Minimax),
168 // --- Qwen (OpenRouter routing defaults) ---
169 ("qwen/qwen3.6-flash", ModelProvider::Qwen),
170 ("qwen/qwen3.6-plus", ModelProvider::Qwen),
171 ("qwen/qwen3.7-plus", ModelProvider::Qwen),
172 ("qwen/qwen3.6-35b-a3b", ModelProvider::Qwen),
173 // --- Arcee Trinity (config DEFAULT_ARCEE_MODEL) ---
174 ("trinity-large-thinking", ModelProvider::Arcee),
175 ("arcee-ai/trinity-large-thinking", ModelProvider::Arcee),
176 ("trinity-mini", ModelProvider::Arcee),
177 // --- Together / Thinking Machines ---
178 ("thinkingmachines/inkling", ModelProvider::Together),
179 // --- Sakana / Fugu (config DEFAULT_SAKANA_MODEL) ---
180 ("fugu-ultra-20260615", ModelProvider::Other),
181 ("fugu-ultra", ModelProvider::Other),
182 // --- StepFun (config DEFAULT_STEPFUN_MODEL) ---
183 ("step-3.7-flash", ModelProvider::Other),
184 // --- Xiaomi MiMo (config DEFAULT_XIAOMI_MIMO_MODEL) ---
185 ("mimo-v2.5-pro", ModelProvider::XiaomiMimo),
186 ("mimo-v2.5-pro-ultraspeed", ModelProvider::XiaomiMimo),
187 ("mimo-v2.5", ModelProvider::XiaomiMimo),
188 // --- Meta Model API (config DEFAULT_META_MODEL) ---
189 ("muse-spark-1.1", ModelProvider::Meta),
190 ("muse-spark-1.2", ModelProvider::Meta),
191 ("muse-spark-1.2-contributor", ModelProvider::Meta),
192 // --- xAI / Grok (config DEFAULT_XAI_MODEL) ---
193 ("grok-4.5", ModelProvider::Xai),
194 ("grok-4.3", ModelProvider::Xai),
195 ("grok-build", ModelProvider::Xai),
196 ("grok-composer-2.5-fast", ModelProvider::Xai),
197 ("grok-4.20-0309-reasoning", ModelProvider::Xai),
198 ("grok-4.20-0309-non-reasoning", ModelProvider::Xai),
199 ];
200
201 fn registry() -> &'static BTreeMap<&'static str, ModelMetadata> {
202 static REGISTRY: OnceLock<BTreeMap<&'static str, ModelMetadata>> = OnceLock::new();
203 REGISTRY.get_or_init(|| {
204 SEED_MODEL_IDS
205 .iter()
206 .map(|&(id, provider)| (id, ModelMetadata::seed(id, provider)))
207 .collect()
208 })
209 }
210
211 /// Look up model facts by id.
212 ///
213 /// Returns a pre-seeded [`ModelMetadata`] when `model` is one of the canonical
214 /// [`SEED_MODEL_IDS`] (case-insensitive). For any other id, this falls back to
215 /// the same `crate::models` heuristics (explicit `_Nk` suffix, DeepSeek/Claude
216 /// family rules, etc.) and reports the provider as [`ModelProvider::Other`], so
217 /// callers always get a usable answer rather than `None` for a real model.
218 ///
219 /// Returns `None` only when the id is unrecognised by every existing source
220 /// (no seed match and `models.rs` yields no context window).
221 #[must_use]
222 pub fn lookup(model: &str) -> Option<ModelMetadata> {
223 if let Some(meta) = registry().get(model) {
224 return Some(meta.clone());
225 }
226 // Case-insensitive seed match (model ids are compared lowercased by the
227 // legacy `models.rs` helpers, so honour that here too).
228 let lowered = model.to_lowercase();
229 if lowered != model
230 && let Some(meta) = registry().get(lowered.as_str())
231 {
232 return Some(meta.clone());
233 }
234
235 // Not pre-seeded: defer to the existing heuristics. If they recognise the
236 // model at all (any known context window), surface a synthetic row so the
237 // single lookup entry point still works for the long tail of ids.
238 let context_window = context_window_for_model(model);
239 let max_output = max_output_tokens_for_model(model);
240 let supports_reasoning = model_supports_reasoning(model);
241 if context_window.is_none() && max_output.is_none() && !supports_reasoning {
242 return None;
243 }
244 Some(ModelMetadata {
245 // The id is not 'static here; we cannot store it, so this synthetic row
246 // reports an empty id. Pre-seeded rows (the common case) carry the real
247 // id. This keeps the public type `'static`-clean without leaking.
248 id: "",
249 provider: ModelProvider::Other,
250 context_window,
251 max_output,
252 supports_reasoning,
253 })
254 }
255
256 #[cfg(test)]
257 mod tests {
258 use super::*;
259
260 /// DRIFT GUARD (#3071, #3073).
261 ///
262 /// The registry must agree with `crate::models` for the context window of
263 /// every model it claims to know. Today they agree because the registry is
264 /// *seeded* from `models.rs`; this test exists so that if a future change
265 /// replaces a seed with a hard-coded literal that drifts from `models.rs`,
266 /// CI fails here instead of shipping two disagreeing sources of truth.
267 #[test]
268 fn registry_context_window_matches_models_rs() {
269 // A representative sample spanning every provider grouping and every
270 // distinct window bucket the legacy table produces.
271 let sample = [
272 ("deepseek-v4-pro", Some(1_000_000)),
273 ("deepseek-v4-flash", Some(1_000_000)),
274 ("deepseek-coder:1.3b", Some(128_000)),
275 ("claude-opus-4-8", Some(1_000_000)),
276 ("claude-sonnet-4-6", Some(1_000_000)),
277 ("claude-sonnet-5", Some(1_000_000)),
278 ("claude-fable-5", Some(1_000_000)),
279 ("claude-haiku-4-5", Some(200_000)),
280 ("gpt-5.5", Some(1_050_000)),
281 ("gpt-5.6", Some(1_050_000)),
282 ("gpt-5.6-terra", Some(1_050_000)),
283 ("gpt-5-codex", Some(400_000)),
284 ("kimi-k2.7-code", Some(262_144)),
285 ("kimi-k2.6", Some(262_144)),
286 ("z-ai/glm-5.1", Some(202_752)),
287 ("z-ai/glm-5.2", Some(1_000_000)),
288 ("z-ai/glm-5.3", Some(1_000_000)),
289 ("minimax/minimax-m3", Some(1_000_000)),
290 ("minimax-m2.7", Some(204_800)),
291 ("qwen/qwen3.6-flash", Some(1_000_000)),
292 ("qwen/qwen3.6-35b-a3b", Some(262_144)),
293 ("trinity-large-thinking", Some(262_144)),
294 ("trinity-mini", Some(128_000)),
295 ("mimo-v2.5-pro", Some(1_000_000)),
296 ("mimo-v2.5-pro-ultraspeed", Some(1_000_000)),
297 ("mimo-v2.5", Some(1_000_000)),
298 ("muse-spark-1.1", Some(1_000_000)),
299 ("muse-spark-1.2", Some(1_000_000)),
300 ("muse-spark-1.2-contributor", Some(1_000_000)),
301 ("grok-4.5", Some(500_000)),
302 ("grok-4.3", Some(1_000_000)),
303 ("grok-4.20-0309-reasoning", Some(2_000_000)),
304 ];
305 for (model, expected) in sample {
306 let meta = lookup(model)
307 .unwrap_or_else(|| panic!("seeded model {model} should be in the registry"));
308 // 1. Registry value equals the documented expectation.
309 assert_eq!(
310 meta.context_window, expected,
311 "registry context window for {model} drifted from expected"
312 );
313 // 2. Registry value equals the LIVE models.rs value (the real guard:
314 // catches any future hard-coded literal that drifts).
315 assert_eq!(
316 meta.context_window,
317 context_window_for_model(model),
318 "registry context window for {model} drifted from models.rs"
319 );
320 }
321 }
322
323 #[test]
324 fn registry_max_output_and_reasoning_match_models_rs() {
325 for &(id, _) in SEED_MODEL_IDS {
326 let meta = lookup(id).unwrap_or_else(|| panic!("{id} should be seeded"));
327 assert_eq!(
328 meta.max_output,
329 max_output_tokens_for_model(id),
330 "registry max_output for {id} drifted from models.rs"
331 );
332 assert_eq!(
333 meta.supports_reasoning,
334 model_supports_reasoning(id),
335 "registry supports_reasoning for {id} drifted from models.rs"
336 );
337 }
338 }
339
340 #[test]
341 fn deepseek_models_are_classified_as_deepseek() {
342 // Branding / first-class DeepSeek support guard: the default DeepSeek
343 // models must be present and classified as DeepSeek.
344 for id in [
345 "deepseek-v4-pro",
346 "deepseek-v4-flash",
347 "deepseek-ai/deepseek-v4-pro",
348 ] {
349 let meta = lookup(id).expect("DeepSeek default should be seeded");
350 assert_eq!(meta.provider, ModelProvider::DeepSeek);
351 assert_eq!(meta.context_window, Some(1_000_000));
352 }
353 }
354
355 #[test]
356 fn xai_models_are_classified_as_xai() {
357 let meta = lookup("grok-4.5").expect("xAI default should be seeded");
358 assert_eq!(meta.provider, ModelProvider::Xai);
359 assert_eq!(meta.context_window, Some(500_000));
360 assert!(meta.supports_reasoning);
361
362 let fast = lookup("grok-4.20-0309-non-reasoning").expect("xAI fast model should be seeded");
363 assert_eq!(fast.provider, ModelProvider::Xai);
364 assert_eq!(fast.context_window, Some(2_000_000));
365 assert!(!fast.supports_reasoning);
366 }
367
368 #[test]
369 fn meta_muse_spark_is_classified_as_meta() {
370 let meta = lookup("muse-spark-1.1").expect("Muse Spark default should be seeded");
371 assert_eq!(meta.provider, ModelProvider::Meta);
372 assert_eq!(meta.context_window, Some(1_000_000));
373 assert_eq!(meta.max_output, Some(32_000));
374 assert!(meta.supports_reasoning);
375 for id in ["muse-spark-1.2", "muse-spark-1.2-contributor"] {
376 let m = lookup(id).unwrap_or_else(|| panic!("{id} should be seeded"));
377 assert_eq!(m.provider, ModelProvider::Meta);
378 assert_eq!(m.context_window, Some(1_000_000));
379 assert!(m.supports_reasoning);
380 }
381 }
382
383 #[test]
384 fn v090_model_metadata_is_provider_correct_and_conservative() {
385 let gpt = lookup("gpt-5.3-codex").expect("GPT-5.3 Codex seed");
386 assert_eq!(gpt.provider, ModelProvider::OpenAi);
387
388 let qwen = lookup("qwen/qwen3.7-plus").expect("Qwen 3.7 Plus seed");
389 assert_eq!(qwen.provider, ModelProvider::Qwen);
390 assert_eq!(qwen.context_window, None);
391 assert_eq!(qwen.max_output, None);
392 assert!(qwen.supports_reasoning);
393
394 let trinity = lookup("trinity-mini").expect("Trinity Mini seed");
395 assert_eq!(trinity.provider, ModelProvider::Arcee);
396 assert_eq!(trinity.context_window, Some(128_000));
397 assert_eq!(trinity.max_output, None);
398 assert!(trinity.supports_reasoning);
399
400 let inkling = lookup("thinkingmachines/inkling").expect("Inkling seed");
401 assert_eq!(inkling.provider, ModelProvider::Together);
402 assert_eq!(inkling.context_window, None);
403 assert_eq!(inkling.max_output, None);
404 assert!(inkling.supports_reasoning);
405 }
406
407 #[test]
408 fn lookup_is_case_insensitive_for_seeded_ids() {
409 let lower = lookup("deepseek-v4-pro").expect("seeded");
410 let upper = lookup("DeepSeek-V4-Pro").expect("case-insensitive seed match");
411 assert_eq!(upper.id, "deepseek-v4-pro");
412 assert_eq!(upper.context_window, lower.context_window);
413 assert_eq!(upper.provider, ModelProvider::DeepSeek);
414 }
415
416 #[test]
417 fn lookup_falls_back_to_models_rs_for_unseeded_known_ids() {
418 // `deepseek-v3.2-256k-preview` is not in SEED_MODEL_IDS but models.rs
419 // recognises it via the explicit `_Nk` hint. The single lookup entry
420 // point must still answer it rather than returning None.
421 let meta = lookup("deepseek-v3.2-256k-preview").expect("known via models.rs heuristics");
422 assert_eq!(meta.context_window, Some(256_000));
423 assert_eq!(
424 meta.context_window,
425 context_window_for_model("deepseek-v3.2-256k-preview")
426 );
427 assert_eq!(meta.provider, ModelProvider::Other);
428 }
429
430 #[test]
431 fn lookup_returns_none_for_completely_unknown_model() {
432 assert!(lookup("totally-made-up-model-xyz").is_none());
433 }
434 }
435
435 lines RUST