返回 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 //! * [`codewhale_models::context_window_for_model`] /
7 //! the models module's context-window lookup for context windows,
8 //! * [`codewhale_models::max_output_tokens_for_model`] for output caps,
9 //! * [`codewhale_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 //! `codewhale_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 codewhale_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 /// Mistral AI la Plateforme models.
77 Mistral,
78 /// Google Gemini models (official OpenAI-compatible route).
79 Google,
80 /// Anything not otherwise classified (still gets real metadata via the
81 /// `models.rs` heuristics where possible).
82 Other,
83 }
84
85 /// One row of model facts, looked up in [`lookup`].
86 ///
87 /// All numeric fields are seeded from `codewhale_models` so they stay in lockstep
88 /// with the legacy lookups (see module docs).
89 #[derive(Debug, Clone, PartialEq, Eq)]
90 pub struct ModelMetadata {
91 /// Canonical model id as sent to the provider (e.g. `"deepseek-v4-pro"`).
92 pub id: &'static str,
93 /// Coarse provider grouping.
94 pub provider: ModelProvider,
95 /// Approximate context window in tokens, if known.
96 pub context_window: Option<u32>,
97 /// Approximate maximum output tokens, if known.
98 pub max_output: Option<u32>,
99 /// Whether the model emits reasoning / thinking content that must be kept
100 /// out of answer prose.
101 pub supports_reasoning: bool,
102 }
103
104 impl ModelMetadata {
105 /// Build a metadata row for `id` by seeding every fact from the existing
106 /// `codewhale_models` lookups. This is the only constructor, which is what
107 /// keeps the registry from drifting away from `models.rs`.
108 fn seed(id: &'static str, provider: ModelProvider) -> Self {
109 Self {
110 id,
111 provider,
112 context_window: context_window_for_model(id),
113 max_output: max_output_tokens_for_model(id),
114 supports_reasoning: model_supports_reasoning(id),
115 }
116 }
117 }
118
119 /// Canonical `(model id, provider)` seeds for the registry.
120 ///
121 /// These mirror the provider defaults shipped by `crates/config/src/lib.rs`
122 /// (the `DEFAULT_*_MODEL` constants) plus the explicitly-enumerated models in
123 /// the models module's context-window lookup. Keep this list curated:
124 /// it is the set of models we make first-class promises about. Unknown ids are
125 /// still answered by [`lookup`] via the `models.rs` heuristics, they just are
126 /// not pre-seeded here.
127 const SEED_MODEL_IDS: &[(&str, ModelProvider)] = &[
128 // --- DeepSeek (first-class; config DEFAULT_DEEPSEEK_MODEL / NIM / OpenAI
129 // / Atlascloud / Novita / Fireworks / Siliconflow / SGLang / vLLM /
130 // Huggingface / Together / Volcengine / WanjieArk / Ollama defaults) ---
131 ("deepseek-v4-pro", ModelProvider::DeepSeek),
132 // The official V4.1 id and the config default: it must be seeded here so
133 // this list keeps mirroring the DEFAULT_* constants it documents.
134 ("deepseek-flash", ModelProvider::DeepSeek),
135 ("deepseek-v4-flash", ModelProvider::DeepSeek),
136 ("deepseek-v4-flash-vision-exp", ModelProvider::DeepSeek),
137 ("deepseek-ai/deepseek-v4-pro", ModelProvider::DeepSeek),
138 ("deepseek-ai/deepseek-v4-flash", ModelProvider::DeepSeek),
139 ("deepseek/deepseek-v4-pro", ModelProvider::DeepSeek),
140 ("deepseek/deepseek-v4-flash", ModelProvider::DeepSeek),
141 ("deepseek-reasoner", ModelProvider::DeepSeek),
142 ("deepseek-coder:1.3b", ModelProvider::DeepSeek),
143 // --- Anthropic (config DEFAULT_ANTHROPIC_MODEL + models.rs rows) ---
144 ("claude-opus-4-8", ModelProvider::Anthropic),
145 ("claude-opus-5", ModelProvider::Anthropic),
146 ("claude-sonnet-4-6", ModelProvider::Anthropic),
147 ("claude-sonnet-5", ModelProvider::Anthropic),
148 ("claude-fable-5", ModelProvider::Anthropic),
149 ("claude-haiku-4-5", ModelProvider::Anthropic),
150 // --- OpenAI public API + Codex (config DEFAULT_OPENAI_CODEX_MODEL) ---
151 ("gpt-5.5", ModelProvider::OpenAi),
152 ("gpt-5.5-pro", ModelProvider::OpenAi),
153 ("gpt-5.6", ModelProvider::OpenAi),
154 ("gpt-5.6-sol", ModelProvider::OpenAi),
155 ("gpt-5.6-terra", ModelProvider::OpenAi),
156 ("gpt-5.6-luna", ModelProvider::OpenAi),
157 ("gpt-5-codex", ModelProvider::OpenAiCodex),
158 ("gpt-5.3-codex", ModelProvider::OpenAi),
159 // --- Moonshot / Kimi (config DEFAULT_MOONSHOT_MODEL / KIMI_CODE) ---
160 ("kimi-k2.7-code", ModelProvider::Moonshot),
161 ("kimi-k2.7-code-highspeed", ModelProvider::Moonshot),
162 ("kimi-k2.6", ModelProvider::Moonshot),
163 ("kimi-for-coding", ModelProvider::Moonshot),
164 ("moonshotai/kimi-k2.7-code", ModelProvider::Moonshot),
165 ("moonshotai/kimi-k2.6", ModelProvider::Moonshot),
166 // --- Z.ai GLM (config DEFAULT_ZAI_MODEL) ---
167 ("z-ai/glm-5.1", ModelProvider::Zai),
168 ("z-ai/glm-5.2", ModelProvider::Zai),
169 ("z-ai/glm-5.3", ModelProvider::Zai),
170 ("z-ai/glm-5.3-flash", ModelProvider::Zai),
171 ("glm-5.1", ModelProvider::Zai),
172 ("glm-5.2", ModelProvider::Zai),
173 ("glm-5.3", ModelProvider::Zai),
174 ("glm-5.3-flash", ModelProvider::Zai),
175 // --- MiniMax (config DEFAULT_MINIMAX_MODEL) ---
176 ("minimax/minimax-m3", ModelProvider::Minimax),
177 ("minimax-m3", ModelProvider::Minimax),
178 ("minimax/minimax-m2.7", ModelProvider::Minimax),
179 ("minimax-m2.7", ModelProvider::Minimax),
180 // --- Qwen (OpenRouter routing defaults) ---
181 ("qwen/qwen3.6-flash", ModelProvider::Qwen),
182 ("qwen/qwen3.6-plus", ModelProvider::Qwen),
183 ("qwen/qwen3.7-plus", ModelProvider::Qwen),
184 ("qwen/qwen3.6-35b-a3b", ModelProvider::Qwen),
185 // --- Arcee Trinity (config DEFAULT_ARCEE_MODEL) ---
186 ("trinity-large-thinking", ModelProvider::Arcee),
187 ("arcee-ai/trinity-large-thinking", ModelProvider::Arcee),
188 ("trinity-mini", ModelProvider::Arcee),
189 // --- Together / Thinking Machines ---
190 ("thinkingmachines/inkling", ModelProvider::Together),
191 // --- Sakana / Fugu (config DEFAULT_SAKANA_MODEL) ---
192 ("fugu-ultra-20260615", ModelProvider::Other),
193 ("fugu-ultra", ModelProvider::Other),
194 // --- StepFun (config DEFAULT_STEPFUN_MODEL) ---
195 ("step-3.7-flash", ModelProvider::Other),
196 ("step-5-preview", ModelProvider::Other),
197 ("step-3.5-flash", ModelProvider::Other),
198 ("step-3.5-flash-2603", ModelProvider::Other),
199 // --- Xiaomi MiMo (config DEFAULT_XIAOMI_MIMO_MODEL) ---
200 ("mimo-v2.5-pro", ModelProvider::XiaomiMimo),
201 ("mimo-v2.5-pro-ultraspeed", ModelProvider::XiaomiMimo),
202 ("mimo-v2.5", ModelProvider::XiaomiMimo),
203 // --- Meta Model API (config DEFAULT_META_MODEL) ---
204 ("muse-spark-1.1", ModelProvider::Meta),
205 ("muse-spark-1.2", ModelProvider::Meta),
206 ("muse-spark-1.2-contributor", ModelProvider::Meta),
207 // --- xAI / Grok (config DEFAULT_XAI_MODEL) ---
208 ("grok-4.6", ModelProvider::Xai),
209 ("grok-4.5", ModelProvider::Xai),
210 ("grok-4.3", ModelProvider::Xai),
211 ("grok-build", ModelProvider::Xai),
212 ("grok-composer-2.5-fast", ModelProvider::Xai),
213 ("grok-4.20-0309-reasoning", ModelProvider::Xai),
214 ("grok-4.20-0309-non-reasoning", ModelProvider::Xai),
215 // --- Mistral AI (current first-party roster; deprecated Magistral remains
216 // accepted through the long-tail models.rs compatibility path) ---
217 // --- Google Gemini (official OpenAI-compatible route; preview flagships
218 // as listed by Google's model pages, 2026-08).
219 ("gemini-3.1-pro-preview", ModelProvider::Google),
220 ("gemini-3-pro-preview", ModelProvider::Google),
221 ("gemini-3.7-flash", ModelProvider::Google),
222 ("gemini-3.6-flash", ModelProvider::Google),
223 ("gemini-3.5-flash", ModelProvider::Google),
224 ("gemini-3.5-flash-lite", ModelProvider::Google),
225 ("gemini-2.5-pro", ModelProvider::Google),
226 ("gemini-2.5-flash", ModelProvider::Google),
227 ("mistral-code-latest", ModelProvider::Mistral),
228 ("mistral-medium-latest", ModelProvider::Mistral),
229 ("mistral-small-latest", ModelProvider::Mistral),
230 ("mistral-large-latest", ModelProvider::Mistral),
231 ];
232
233 fn registry() -> &'static BTreeMap<&'static str, ModelMetadata> {
234 static REGISTRY: OnceLock<BTreeMap<&'static str, ModelMetadata>> = OnceLock::new();
235 REGISTRY.get_or_init(|| {
236 SEED_MODEL_IDS
237 .iter()
238 .map(|&(id, provider)| (id, ModelMetadata::seed(id, provider)))
239 .collect()
240 })
241 }
242
243 /// Look up model facts by id.
244 ///
245 /// Returns a pre-seeded [`ModelMetadata`] when `model` is one of the canonical
246 /// [`SEED_MODEL_IDS`] (case-insensitive). For any other id, this falls back to
247 /// the same `codewhale_models` heuristics (explicit `_Nk` suffix, DeepSeek/Claude
248 /// family rules, etc.) and reports the provider as [`ModelProvider::Other`], so
249 /// callers always get a usable answer rather than `None` for a real model.
250 ///
251 /// Returns `None` only when the id is unrecognised by every existing source
252 /// (no seed match and `models.rs` yields no context window).
253 #[must_use]
254 pub fn lookup(model: &str) -> Option<ModelMetadata> {
255 if let Some(meta) = registry().get(model) {
256 return Some(meta.clone());
257 }
258 // Case-insensitive seed match (model ids are compared lowercased by the
259 // legacy `models.rs` helpers, so honour that here too).
260 let lowered = model.to_lowercase();
261 if lowered != model
262 && let Some(meta) = registry().get(lowered.as_str())
263 {
264 return Some(meta.clone());
265 }
266
267 // Not pre-seeded: defer to the existing heuristics. If they recognise the
268 // model at all (any known context window), surface a synthetic row so the
269 // single lookup entry point still works for the long tail of ids.
270 let context_window = context_window_for_model(model);
271 let max_output = max_output_tokens_for_model(model);
272 let supports_reasoning = model_supports_reasoning(model);
273 if context_window.is_none() && max_output.is_none() && !supports_reasoning {
274 return None;
275 }
276 Some(ModelMetadata {
277 // The id is not 'static here; we cannot store it, so this synthetic row
278 // reports an empty id. Pre-seeded rows (the common case) carry the real
279 // id. This keeps the public type `'static`-clean without leaking.
280 id: "",
281 provider: ModelProvider::Other,
282 context_window,
283 max_output,
284 supports_reasoning,
285 })
286 }
287
288 #[cfg(test)]
289 mod tests {
290 use super::*;
291
292 /// DRIFT GUARD (#3071, #3073).
293 ///
294 /// The registry must agree with `codewhale_models` for the context window of
295 /// every model it claims to know. Today they agree because the registry is
296 /// *seeded* from `models.rs`; this test exists so that if a future change
297 /// replaces a seed with a hard-coded literal that drifts from `models.rs`,
298 /// CI fails here instead of shipping two disagreeing sources of truth.
299 #[test]
300 fn registry_context_window_matches_models_rs() {
301 // A representative sample spanning every provider grouping and every
302 // distinct window bucket the legacy table produces.
303 let sample = [
304 ("deepseek-v4-pro", Some(1_000_000)),
305 ("deepseek-v4-flash", Some(1_000_000)),
306 ("deepseek-coder:1.3b", None),
307 ("claude-opus-4-8", Some(1_000_000)),
308 ("claude-opus-5", Some(1_000_000)),
309 ("claude-sonnet-4-6", Some(1_000_000)),
310 ("claude-sonnet-5", Some(1_000_000)),
311 ("claude-fable-5", Some(1_000_000)),
312 ("claude-haiku-4-5", Some(200_000)),
313 ("gpt-5.5", Some(1_050_000)),
314 ("gpt-5.6", Some(1_050_000)),
315 ("gpt-5.6-terra", Some(1_050_000)),
316 ("gpt-5-codex", Some(400_000)),
317 ("kimi-k2.7-code", Some(262_144)),
318 ("kimi-k2.7-code-highspeed", Some(262_144)),
319 ("kimi-k2.6", Some(262_144)),
320 ("z-ai/glm-5.1", Some(202_752)),
321 ("z-ai/glm-5.2", Some(1_000_000)),
322 ("z-ai/glm-5.3", Some(1_000_000)),
323 ("z-ai/glm-5.3-flash", Some(1_000_000)),
324 ("minimax/minimax-m3", Some(1_000_000)),
325 ("minimax-m2.7", Some(204_800)),
326 ("qwen/qwen3.6-flash", Some(1_000_000)),
327 ("qwen/qwen3.6-35b-a3b", Some(262_144)),
328 ("trinity-large-thinking", Some(262_144)),
329 ("trinity-mini", Some(128_000)),
330 ("mimo-v2.5-pro", Some(1_000_000)),
331 ("mimo-v2.5-pro-ultraspeed", Some(1_000_000)),
332 ("mimo-v2.5", Some(1_000_000)),
333 ("muse-spark-1.1", Some(1_000_000)),
334 ("muse-spark-1.2", Some(1_000_000)),
335 ("muse-spark-1.2-contributor", Some(1_000_000)),
336 ("grok-4.6", Some(500_000)),
337 ("grok-4.5", Some(500_000)),
338 ("grok-4.3", Some(1_000_000)),
339 ("grok-4.20-0309-reasoning", Some(2_000_000)),
340 ("gemini-3.7-flash", Some(1_048_576)),
341 ("gemini-3.1-pro-preview", Some(1_048_576)),
342 ("mistral-code-latest", Some(256_000)),
343 ("mistral-medium-latest", Some(262_144)),
344 ("mistral-small-latest", Some(262_144)),
345 ("mistral-large-latest", Some(262_144)),
346 ];
347 for (model, expected) in sample {
348 let meta = lookup(model)
349 .unwrap_or_else(|| panic!("seeded model {model} should be in the registry"));
350 // 1. Registry value equals the documented expectation.
351 assert_eq!(
352 meta.context_window, expected,
353 "registry context window for {model} drifted from expected"
354 );
355 // 2. Registry value equals the LIVE models.rs value (the real guard:
356 // catches any future hard-coded literal that drifts).
357 assert_eq!(
358 meta.context_window,
359 context_window_for_model(model),
360 "registry context window for {model} drifted from models.rs"
361 );
362 }
363 }
364
365 #[test]
366 fn registry_max_output_and_reasoning_match_models_rs() {
367 for &(id, _) in SEED_MODEL_IDS {
368 let meta = lookup(id).unwrap_or_else(|| panic!("{id} should be seeded"));
369 assert_eq!(
370 meta.max_output,
371 max_output_tokens_for_model(id),
372 "registry max_output for {id} drifted from models.rs"
373 );
374 assert_eq!(
375 meta.supports_reasoning,
376 model_supports_reasoning(id),
377 "registry supports_reasoning for {id} drifted from models.rs"
378 );
379 }
380 }
381
382 #[test]
383 fn deepseek_models_are_classified_as_deepseek() {
384 // Branding / first-class DeepSeek support guard: the default DeepSeek
385 // models must be present and classified as DeepSeek.
386 for id in [
387 "deepseek-v4-pro",
388 "deepseek-v4-flash",
389 "deepseek-ai/deepseek-v4-pro",
390 ] {
391 let meta = lookup(id).expect("DeepSeek default should be seeded");
392 assert_eq!(meta.provider, ModelProvider::DeepSeek);
393 assert_eq!(meta.context_window, Some(1_000_000));
394 }
395 }
396
397 #[test]
398 fn xai_models_are_classified_as_xai() {
399 let meta = lookup("grok-4.6").expect("xAI default should be seeded");
400 assert_eq!(meta.provider, ModelProvider::Xai);
401 assert_eq!(meta.context_window, Some(500_000));
402 assert!(meta.supports_reasoning);
403
404 let fast = lookup("grok-4.20-0309-non-reasoning").expect("xAI fast model should be seeded");
405 assert_eq!(fast.provider, ModelProvider::Xai);
406 assert_eq!(fast.context_window, Some(2_000_000));
407 assert!(!fast.supports_reasoning);
408 }
409
410 #[test]
411 fn mistral_models_are_classified_with_truthful_reasoning() {
412 for (id, expected_reasoning) in [
413 ("mistral-code-latest", false),
414 ("mistral-medium-latest", true),
415 ("mistral-small-latest", true),
416 ("mistral-large-latest", false),
417 ] {
418 let meta = lookup(id).expect("Mistral model should be seeded");
419 assert_eq!(meta.provider, ModelProvider::Mistral, "{id}");
420 assert_eq!(meta.supports_reasoning, expected_reasoning, "{id}");
421 }
422 }
423
424 #[test]
425 fn meta_muse_spark_is_classified_as_meta() {
426 let meta = lookup("muse-spark-1.1").expect("Muse Spark default should be seeded");
427 assert_eq!(meta.provider, ModelProvider::Meta);
428 assert_eq!(meta.context_window, Some(1_000_000));
429 assert_eq!(meta.max_output, Some(32_000));
430 assert!(meta.supports_reasoning);
431 for id in ["muse-spark-1.2", "muse-spark-1.2-contributor"] {
432 let m = lookup(id).unwrap_or_else(|| panic!("{id} should be seeded"));
433 assert_eq!(m.provider, ModelProvider::Meta);
434 assert_eq!(m.context_window, Some(1_000_000));
435 assert!(m.supports_reasoning);
436 }
437 }
438
439 #[test]
440 fn v090_model_metadata_is_provider_correct_and_conservative() {
441 let gpt = lookup("gpt-5.3-codex").expect("GPT-5.3 Codex seed");
442 assert_eq!(gpt.provider, ModelProvider::OpenAi);
443
444 let qwen = lookup("qwen/qwen3.7-plus").expect("Qwen 3.7 Plus seed");
445 assert_eq!(qwen.provider, ModelProvider::Qwen);
446 assert_eq!(qwen.context_window, None);
447 assert_eq!(qwen.max_output, None);
448 assert!(qwen.supports_reasoning);
449
450 let trinity = lookup("trinity-mini").expect("Trinity Mini seed");
451 assert_eq!(trinity.provider, ModelProvider::Arcee);
452 assert_eq!(trinity.context_window, Some(128_000));
453 assert_eq!(trinity.max_output, None);
454 assert!(trinity.supports_reasoning);
455
456 let inkling = lookup("thinkingmachines/inkling").expect("Inkling seed");
457 assert_eq!(inkling.provider, ModelProvider::Together);
458 assert_eq!(inkling.context_window, None);
459 assert_eq!(inkling.max_output, None);
460 assert!(inkling.supports_reasoning);
461 }
462
463 #[test]
464 fn lookup_is_case_insensitive_for_seeded_ids() {
465 let lower = lookup("deepseek-v4-pro").expect("seeded");
466 let upper = lookup("DeepSeek-V4-Pro").expect("case-insensitive seed match");
467 assert_eq!(upper.id, "deepseek-v4-pro");
468 assert_eq!(upper.context_window, lower.context_window);
469 assert_eq!(upper.provider, ModelProvider::DeepSeek);
470 }
471
472 #[test]
473 fn lookup_falls_back_to_models_rs_for_unseeded_known_ids() {
474 // `deepseek-v3.2-256k-preview` is not in SEED_MODEL_IDS but models.rs
475 // recognises it via the explicit `_Nk` hint. The single lookup entry
476 // point must still answer it rather than returning None.
477 let meta = lookup("deepseek-v3.2-256k-preview").expect("known via models.rs heuristics");
478 assert_eq!(meta.context_window, Some(256_000));
479 assert_eq!(
480 meta.context_window,
481 context_window_for_model("deepseek-v3.2-256k-preview")
482 );
483 assert_eq!(meta.provider, ModelProvider::Other);
484 }
485
486 #[test]
487 fn lookup_returns_none_for_completely_unknown_model() {
488 assert!(lookup("totally-made-up-model-xyz").is_none());
489 }
490 }
491
491 lines RUST