Skip to main content

vtcode_config/models/model_id/
capabilities.rs

1use std::cmp::Reverse;
2
3use crate::models::{Provider, ProviderModelSupport};
4
5use super::ModelId;
6
7mod capability_generated {
8    include!(concat!(env!("OUT_DIR"), "/model_capabilities.rs"));
9}
10
11/// Catalog metadata generated from `docs/models.json`.
12#[derive(Clone, Copy, Debug, PartialEq)]
13pub struct ModelPricing {
14    pub input: Option<f64>,
15    pub output: Option<f64>,
16    pub cache_read: Option<f64>,
17    pub cache_write: Option<f64>,
18}
19
20#[derive(Clone, Copy, Debug, PartialEq)]
21pub struct ModelCatalogEntry {
22    pub(crate) provider: &'static str,
23    id: &'static str,
24    pub display_name: &'static str,
25    pub description: &'static str,
26    pub context_window: usize,
27    max_output_tokens: Option<usize>,
28    pub reasoning: bool,
29    pub reasoning_efforts: &'static [&'static str],
30    pub is_pro: bool,
31    pub lightweight_model: Option<&'static str>,
32    pub tool_call: bool,
33    pub vision: bool,
34    pub input_modalities: &'static [&'static str],
35    pub caching: bool,
36    pub structured_output: bool,
37    pub supports_sampling: bool,
38    pub supports_logprobs: bool,
39    pub prompt_cache_ttl: Option<&'static str>,
40    pub prompt_contract: Option<&'static str>,
41    pub pricing: ModelPricing,
42}
43
44fn catalog_provider_key(provider: &str) -> &str {
45    if provider.eq_ignore_ascii_case("google") || provider.eq_ignore_ascii_case("gemini") {
46        "gemini"
47    } else if provider.eq_ignore_ascii_case("openai") {
48        "openai"
49    } else if provider.eq_ignore_ascii_case("anthropic") {
50        "anthropic"
51    } else if provider.eq_ignore_ascii_case("deepseek") {
52        "deepseek"
53    } else if provider.eq_ignore_ascii_case("meta") || provider.eq_ignore_ascii_case("meta-ai") {
54        "meta"
55    } else if provider.eq_ignore_ascii_case("openrouter") {
56        "openrouter"
57    } else if provider.eq_ignore_ascii_case("ollama") {
58        "ollama"
59    } else if provider.eq_ignore_ascii_case("lmstudio") {
60        "lmstudio"
61    } else if provider.eq_ignore_ascii_case("llamacpp") || provider.eq_ignore_ascii_case("llama.cpp") {
62        "llamacpp"
63    } else if provider.eq_ignore_ascii_case("moonshot") {
64        "moonshot"
65    } else if provider.eq_ignore_ascii_case("zai") {
66        "zai"
67    } else if provider.eq_ignore_ascii_case("minimax") {
68        "minimax"
69    } else if provider.eq_ignore_ascii_case("huggingface") {
70        "huggingface"
71    } else if provider.eq_ignore_ascii_case("stepfun") {
72        "stepfun"
73    } else if provider.eq_ignore_ascii_case("evolink") {
74        "evolink"
75    } else if provider.eq_ignore_ascii_case("poolside") {
76        "poolside"
77    } else if provider.eq_ignore_ascii_case("xai") {
78        "xai"
79    } else if provider.eq_ignore_ascii_case("nvidia") {
80        "nvidia"
81    } else if provider.eq_ignore_ascii_case("merge-gateway") {
82        "merge-gateway"
83    } else if provider.eq_ignore_ascii_case("vercel") || provider.eq_ignore_ascii_case("vercel-ai-gateway") {
84        "vercel"
85    } else {
86        provider
87    }
88}
89
90fn capability_provider_key(provider: Provider) -> &'static str {
91    match provider {
92        Provider::Gemini => "gemini",
93        Provider::OpenAI => "openai",
94        Provider::Anthropic => "anthropic",
95        Provider::Copilot => "copilot",
96        Provider::DeepSeek => "deepseek",
97        Provider::Meta => "meta",
98        Provider::OpenRouter => "openrouter",
99        Provider::Ollama => "ollama",
100        Provider::OllamaCloud => "ollama-cloud",
101        Provider::LmStudio => "lmstudio",
102        Provider::LlamaCpp => "llamacpp",
103        Provider::Moonshot => "moonshot",
104        Provider::ZAI => "zai",
105        Provider::Minimax => "minimax",
106        Provider::MiMo => "mimo",
107        Provider::Mistral => "mistral",
108        Provider::HuggingFace => "huggingface",
109        Provider::OpenCodeZen => "opencode-zen",
110        Provider::OpenCodeGo => "opencode-go",
111        Provider::Qwen => "qwen",
112        Provider::StepFun => "stepfun",
113        Provider::Evolink => "evolink",
114        Provider::Poolside => "poolside",
115        Provider::XAI => "xai",
116        Provider::NVIDIA => "nvidia",
117        Provider::MergeGateway => "merge-gateway",
118        Provider::Vercel => "vercel",
119    }
120}
121
122fn catalog_lookup_id<'a>(provider: &str, id: &'a str) -> &'a str {
123    let provider_key = catalog_provider_key(provider);
124    if provider_key == "evolink"
125        && let Some((prefix, model_id)) = id.split_once('/')
126        && prefix.eq_ignore_ascii_case(provider_key)
127    {
128        // Evolink namespaces its ModelId values to avoid collisions with
129        // first-class providers, while its catalog stores the upstream model
130        // id that the gateway receives (for example, `deepseek-v4-pro`).
131        return model_id;
132    }
133    id
134}
135
136fn generated_catalog_entry(provider: &str, id: &str) -> Option<ModelCatalogEntry> {
137    let provider_key = catalog_provider_key(provider);
138    let lookup_id = catalog_lookup_id(provider_key, id);
139    // Exact match wins so gateway providers that store prefixed IDs
140    // (`merge-gateway`/`vercel` keep `openai/gpt-5.6-luna`) keep working.
141    // Fall back to stripping a provider-prefixed request model
142    // (`openai/gpt-5.6-luna` on provider `openai` stores `gpt-5.6-luna`).
143    // The strip is provider-scoped (prefix must equal the provider key) so an
144    // unrelated prefix (`other-org/gpt-5.6-luna`) does not resolve to the bare
145    // model's capabilities. Without this, reasoning-effort and context
146    // lookups miss and the harness omits the effort with an empty
147    // `supported:` diagnostic.
148    let entry = capability_generated::metadata_for(provider_key, lookup_id).or_else(|| {
149        lookup_id
150            .split_once('/')
151            .filter(|(prefix, _)| prefix.eq_ignore_ascii_case(provider_key))
152            .and_then(|(_, rest)| capability_generated::metadata_for(provider_key, rest))
153    });
154    entry.map(|entry| ModelCatalogEntry {
155        provider: entry.provider,
156        id: entry.id,
157        display_name: entry.display_name,
158        description: entry.description,
159        context_window: entry.context_window,
160        max_output_tokens: entry.max_output_tokens,
161        reasoning: entry.reasoning,
162        reasoning_efforts: entry.reasoning_efforts,
163        is_pro: entry.is_pro,
164        lightweight_model: entry.lightweight_model,
165        tool_call: entry.tool_call,
166        vision: entry.vision,
167        input_modalities: entry.input_modalities,
168        caching: entry.caching,
169        structured_output: entry.structured_output,
170        supports_sampling: entry.supports_sampling,
171        supports_logprobs: entry.supports_logprobs,
172        prompt_cache_ttl: entry.prompt_cache_ttl,
173        prompt_contract: entry.prompt_contract,
174        pricing: ModelPricing {
175            input: entry.pricing.input,
176            output: entry.pricing.output,
177            cache_read: entry.pricing.cache_read,
178            cache_write: entry.pricing.cache_write,
179        },
180    })
181}
182
183pub fn model_catalog_entry(provider: &str, id: &str) -> Option<ModelCatalogEntry> {
184    generated_catalog_entry(provider, id)
185}
186
187pub fn supported_models_for_provider(provider: &str) -> Option<&'static [&'static str]> {
188    capability_generated::models_for_provider(catalog_provider_key(provider))
189}
190
191pub fn catalog_provider_keys() -> &'static [&'static str] {
192    capability_generated::PROVIDERS
193}
194
195/// Numeric components of a generation string, e.g. `v2.6` -> `[2, 6]`.
196fn generation_components(generation: &str) -> Vec<u64> {
197    let mut components = Vec::new();
198    let mut digits = String::new();
199    for character in generation.chars() {
200        if character.is_ascii_digit() {
201            digits.push(character);
202        } else if !digits.is_empty() {
203            components.push(digits.parse().unwrap_or_default());
204            digits.clear();
205        }
206    }
207    if !digits.is_empty() {
208        components.push(digits.parse().unwrap_or_default());
209    }
210    components
211}
212
213impl ModelId {
214    fn generated_capabilities(&self) -> Option<ModelCatalogEntry> {
215        generated_catalog_entry(capability_provider_key(self.provider()), &self.as_str())
216    }
217
218    /// Preferred built-in lightweight sibling or lower-tier fallback for this model.
219    pub fn preferred_lightweight_variant(&self) -> Option<Self> {
220        let target_id = self.generated_capabilities()?.lightweight_model?;
221        Self::all_models().into_iter().find(|candidate| {
222            candidate != self && candidate.provider() == self.provider() && candidate.as_str() == target_id
223        })
224    }
225
226    /// Attempt to find a non-reasoning variant for this model.
227    pub fn non_reasoning_variant(&self) -> Option<Self> {
228        if let Some(meta) = self.openrouter_metadata() {
229            if !meta.reasoning {
230                return None;
231            }
232
233            let vendor = meta.vendor;
234            let mut candidates: Vec<Self> = Self::openrouter_vendor_groups()
235                .into_iter()
236                .find(|(candidate_vendor, _)| *candidate_vendor == vendor)
237                .map(|(_, models)| {
238                    models
239                        .iter()
240                        .filter(|&candidate| candidate != self)
241                        .filter(|&candidate| {
242                            candidate.openrouter_metadata().map(|other| !other.reasoning).unwrap_or(false)
243                        })
244                        .cloned()
245                        .collect()
246                })
247                .unwrap_or_default();
248
249            if candidates.is_empty() {
250                return None;
251            }
252
253            candidates.sort_by_key(|candidate| {
254                candidate
255                    .openrouter_metadata()
256                    .map(|data| (!data.efficient, data.display))
257                    .unwrap_or((true, ""))
258            });
259
260            return candidates.into_iter().next();
261        }
262
263        self.preferred_lightweight_variant()
264            .filter(|candidate| !candidate.is_reasoning_variant())
265    }
266
267    /// Check if this is a "flash" variant (optimized for speed)
268    pub fn is_flash_variant(&self) -> bool {
269        matches!(
270            self,
271            ModelId::Gemini38Flash
272                | ModelId::MergeGatewayGoogleGemini38Flash
273                | ModelId::VercelGoogleGemini38Flash
274                | ModelId::VercelDeepseekFlash
275                | ModelId::VercelOpenAiGpt56Luna
276                | ModelId::VercelAnthropicClaudeHaiku45
277                | ModelId::ClaudeHaiku55
278                | ModelId::MergeGatewayAnthropicClaudeHaiku4520251001
279                | ModelId::MergeGatewayAnthropicClaudeHaiku55
280                | ModelId::StepFun37Flash
281                | ModelId::MergeGatewayDeepseekFlash
282                | ModelId::DeepSeekFlash
283                | ModelId::OllamaDeepSeekV41FlashCloud
284                | ModelId::ZaiGlm53Flash
285                | ModelId::ZaiGlm53Flashx
286                | ModelId::OllamaGlm53FlashCloud
287                | ModelId::OllamaMinimaxM31FlashCloud
288                | ModelId::MergeGatewayZaiGlm53Flash
289                | ModelId::MergeGatewayZaiGlm53Flashx
290                | ModelId::HuggingFaceGlm53FlashTogether
291                | ModelId::MiMoV26Flash
292                | ModelId::MergeGatewayXiaomimimoMimoV26Flash
293                | ModelId::MinimaxM31Flash
294                | ModelId::MergeGatewayMinimaxM31Flash
295                | ModelId::OpenRouterMinimaxM31FlashFree
296        )
297    }
298
299    /// Check if this is a "pro" variant (optimized for capability)
300    pub fn is_pro_variant(&self) -> bool {
301        self.generated_capabilities().is_some_and(|entry| entry.is_pro)
302    }
303
304    /// Check if this is an optimized/efficient variant
305    pub fn is_efficient_variant(&self) -> bool {
306        if let Some(meta) = self.openrouter_metadata() {
307            return meta.efficient;
308        }
309        matches!(
310            self,
311            ModelId::Gemini38Flash
312                | ModelId::MergeGatewayGoogleGemini38Flash
313                | ModelId::Gemini4Neon
314                | ModelId::Gemini4Hydrogen
315                | ModelId::MergeGatewayGoogleGemini4Neon
316                | ModelId::MergeGatewayGoogleGemini4Hydrogen
317                | ModelId::GPT56Luna
318                | ModelId::GPT6Luna
319                | ModelId::MergeGatewayOpenAIGpt56Luna
320                | ModelId::MergeGatewayOpenAIGpt6Luna
321                | ModelId::CopilotGPT54Mini
322                | ModelId::ClaudeHaiku55
323                | ModelId::MergeGatewayAnthropicClaudeHaiku55
324                | ModelId::DeepSeekFlash
325                | ModelId::OllamaDeepSeekV41FlashCloud
326                | ModelId::MergeGatewayDeepseekFlash
327                | ModelId::MetaMuseEmber10
328                | ModelId::MergeGatewayMinimaxH3
329                | ModelId::MiMoV26Flash
330                | ModelId::ZaiGlm53Flash
331                | ModelId::ZaiGlm53Flashx
332                | ModelId::OllamaGlm53FlashCloud
333                | ModelId::OllamaMinimaxM31FlashCloud
334                | ModelId::MergeGatewayZaiGlm53Flash
335                | ModelId::MergeGatewayZaiGlm53Flashx
336                | ModelId::HuggingFaceGlm53FlashTogether
337                | ModelId::VercelDeepseekFlash
338                | ModelId::MergeGatewayXiaomimimoMimoV26Flash
339                | ModelId::MinimaxM31Flash
340                | ModelId::MergeGatewayMinimaxM31Flash
341                | ModelId::OpenRouterMinimaxM31FlashFree
342        )
343    }
344
345    /// Check if this is a top-tier model
346    pub fn is_top_tier(&self) -> bool {
347        if let Some(meta) = self.openrouter_metadata() {
348            return meta.top_tier;
349        }
350        matches!(
351            self,
352            ModelId::Gemini38Flash
353                | ModelId::MergeGatewayGoogleGemini38Flash
354                | ModelId::Gemini4Argon
355                | ModelId::Gemini4Carbon
356                | ModelId::Gemini4Neon
357                | ModelId::Gemini4Hydrogen
358                | ModelId::MergeGatewayGoogleGemini4Argon
359                | ModelId::MergeGatewayGoogleGemini4Carbon
360                | ModelId::MergeGatewayGoogleGemini4Neon
361                | ModelId::MergeGatewayGoogleGemini4Hydrogen
362                | ModelId::GPT62Astra
363                | ModelId::GPT62Sol
364                | ModelId::GPT6Astra
365                | ModelId::GPT6Sol
366                | ModelId::GPT61Sol
367                | ModelId::GPT56Sol
368                | ModelId::MergeGatewayOpenAIGpt56Sol
369                | ModelId::MergeGatewayOpenAIGpt56Terra
370                | ModelId::MergeGatewayOpenAIGpt56Luna
371                | ModelId::MergeGatewayOpenAIGpt62Astra
372                | ModelId::MergeGatewayOpenAIGpt62Sol
373                | ModelId::MergeGatewayOpenAIGpt6Astra
374                | ModelId::MergeGatewayOpenAIGpt6Sol
375                | ModelId::MergeGatewayOpenAIGpt61Sol
376                | ModelId::MergeGatewayOpenAIGpt61Terra
377                | ModelId::MergeGatewayOpenAIGpt6Luna
378                | ModelId::ClaudeSonnet5
379                | ModelId::ClaudeSonnet55
380                | ModelId::ClaudeFable5
381                | ModelId::ClaudeFable51
382                | ModelId::ClaudeFable52
383                | ModelId::ClaudeOpus5
384                | ModelId::ClaudeOpus55
385                | ModelId::MistralLarge4
386                | ModelId::OpenCodeGoGlm53
387                | ModelId::MiMoV26Pro
388                | ModelId::MiMoV26ProUltraspeed
389                | ModelId::OpenCodeGoMinimaxM3
390                | ModelId::MergeGatewayDeepseekFlash
391                | ModelId::VercelDeepseekFlash
392                | ModelId::MetaMuseSpark13
393                | ModelId::MetaMuseSpark13Contributor
394                | ModelId::MetaMuseWonder10
395                | ModelId::MetaMuseWonderFull10
396                | ModelId::ZaiGlm53
397                | ModelId::ZaiGlm53Flash
398                | ModelId::ZaiGlm53Flashx
399                | ModelId::MergeGatewayZaiGlm53Flash
400                | ModelId::MergeGatewayZaiGlm53Flashx
401                | ModelId::HuggingFaceGlm53FlashTogether
402                | ModelId::HuggingFaceGlm53Together
403                | ModelId::HuggingFaceMinimaxM3Novita
404                | ModelId::StepFun5Preview
405                | ModelId::OpenRouterMoonshotaiKimiK3
406                | ModelId::MoonshotKimiK31Pro
407                | ModelId::MoonshotKimiK3
408                | ModelId::MergeGatewayMoonshotKimiK3
409                | ModelId::MergeGatewayMoonshotKimiK31Pro
410                | ModelId::OllamaGlm53Cloud
411                | ModelId::OllamaGlm53FlashCloud
412                | ModelId::OllamaDeepSeekV41FlashCloud
413                | ModelId::OllamaMistralLarge4Cloud
414                | ModelId::OllamaMuseSparkOpenCloud
415                | ModelId::XaiGrok46
416                | ModelId::XaiGrok47
417                | ModelId::MergeGatewayXaiGrok46
418                | ModelId::MergeGatewayXaiGrok47
419                | ModelId::VercelSpacexaiGrok47
420                | ModelId::MergeGatewayXiaomimimoMimoV26Pro
421                | ModelId::MergeGatewayXiaomimimoMimoV26Flash
422                | ModelId::MergeGatewayMistralLarge4
423                | ModelId::MergeGatewayMetaMuseSparkOpen
424                | ModelId::HuggingFaceMuseSparkOpenBaseten
425        )
426    }
427
428    /// Determine whether the model is a reasoning-capable variant
429    pub fn is_reasoning_variant(&self) -> bool {
430        if let Some(meta) = self.openrouter_metadata() {
431            return meta.reasoning;
432        }
433        self.provider().supports_reasoning(&self.as_str())
434    }
435
436    /// Determine whether the model supports tool calls/function execution
437    pub fn supports_tool_calls(&self) -> bool {
438        if let Some(meta) = self.generated_capabilities() {
439            return meta.tool_call;
440        }
441        if let Some(meta) = self.openrouter_metadata() {
442            return meta.tool_call;
443        }
444        true
445    }
446
447    /// Ordered list of supported input modalities when VT Code has metadata for this model.
448    pub fn input_modalities(&self) -> &'static [&'static str] {
449        self.generated_capabilities().map(|meta| meta.input_modalities).unwrap_or(&[])
450    }
451
452    /// Get the generation/version string for this model
453    pub fn generation(&self) -> &'static str {
454        if let Some(meta) = self.openrouter_metadata() {
455            return meta.generation;
456        }
457        match self {
458            // Gemini generations
459            // OpenAI generations
460            ModelId::GPT62Astra | ModelId::GPT62Sol => "6.2",
461            ModelId::GPT6Astra | ModelId::GPT6Sol | ModelId::GPT6Luna => "6",
462            ModelId::GPT61Sol | ModelId::GPT61Terra => "6.1",
463            ModelId::GPT56Sol | ModelId::GPT56Terra | ModelId::GPT56Luna => "5.6",
464            ModelId::OpenAIGptOss20b | ModelId::OpenAIGptOss120b => "5",
465            // Anthropic generations
466            ModelId::ClaudeSonnet5 => "5",
467            ModelId::ClaudeSonnet55 => "5.5",
468            ModelId::ClaudeHaiku55 => "5.5",
469            ModelId::ClaudeFable5 => "5",
470            ModelId::ClaudeFable51 => "5.1",
471            ModelId::ClaudeFable52 => "5.2",
472            ModelId::ClaudeOpus5 => "5",
473            ModelId::ClaudeOpus55 => "5.5",
474            // DeepSeek generations
475            ModelId::DeepSeekFlash => "4",
476            ModelId::OllamaDeepSeekV41FlashCloud => "4.1",
477            // Mistral generations
478            ModelId::MistralLarge3 => "3",
479            ModelId::MistralLarge4 => "4",
480            ModelId::OllamaMistralLarge4Cloud => "4",
481            ModelId::MergeGatewayDeepseekFlash => "4.1",
482            ModelId::MetaMuseSpark13 | ModelId::MetaMuseSpark13Contributor => "Muse-Spark-1.3",
483            ModelId::MergeGatewayMetaMuseSparkOpen | ModelId::HuggingFaceMuseSparkOpenBaseten => "Muse-Spark-Open",
484            ModelId::OllamaMuseSparkOpenCloud => "Muse-Spark-Open",
485            ModelId::MetaMuseWonder10 => "Muse-Wonder-1.0",
486            ModelId::MetaMuseWonderFull10 => "Muse-Wonder-Full-1.0",
487            ModelId::MetaMuseEmber10 => "Muse-Ember-1.0",
488            // Z.AI generations
489            ModelId::ZaiGlm53
490            | ModelId::ZaiGlm53Flash
491            | ModelId::ZaiGlm53Flashx
492            | ModelId::MergeGatewayZaiGlm53Flash
493            | ModelId::MergeGatewayZaiGlm53Flashx => "5.3",
494            ModelId::OllamaGlm53FlashCloud => "5.3",
495            ModelId::Gemini38Flash => "3.8",
496            ModelId::MergeGatewayGoogleGemini38Flash => "3.8",
497            ModelId::Gemini4Argon => "4",
498            ModelId::Gemini4Carbon => "4",
499            ModelId::Gemini4Neon => "4",
500            ModelId::Gemini4Hydrogen => "4",
501            ModelId::MergeGatewayGoogleGemini4Argon => "4",
502            ModelId::MergeGatewayGoogleGemini4Carbon => "4",
503            ModelId::MergeGatewayGoogleGemini4Neon => "4",
504            ModelId::MergeGatewayGoogleGemini4Hydrogen => "4",
505            ModelId::OpenCodeGoGlm53 => "5.3",
506            ModelId::OpenCodeGoGpt56Luna => "5.6-luna",
507            ModelId::OpenCodeGoKimiK3 => "k3",
508            ModelId::MiMoV26Pro | ModelId::MiMoV26Flash | ModelId::MiMoV26ProUltraspeed => "v2.6",
509            ModelId::MergeGatewayXiaomimimoMimoV26Pro | ModelId::MergeGatewayXiaomimimoMimoV26Flash => "v2.6",
510            ModelId::MergeGatewayMistralLarge4 => "4",
511            ModelId::OpenCodeGoMinimaxM3 => "m3",
512            ModelId::OllamaGptOss20b => "oss",
513            ModelId::OllamaGptOss20bCloud => "oss-cloud",
514            ModelId::OllamaGptOss120bCloud => "oss-cloud",
515            ModelId::OllamaMinimaxM3Cloud => "minimax-m3",
516            ModelId::OllamaMinimaxM31FlashCloud => "minimax-m3.1",
517            ModelId::OllamaGlm53Cloud => "glm-5.3",
518            ModelId::OllamaGemma4 => "gemma-4",
519            ModelId::LlamaCppGemma426bA4b => "4",
520            ModelId::LlamaCppGemma4E4b => "4",
521            ModelId::LlamaCppGptOss20b => "oss",
522            // MiniMax models
523            ModelId::MinimaxM3 => "M3",
524            ModelId::MinimaxM31Flash | ModelId::MergeGatewayMinimaxM31Flash => "M3.1",
525            // StepFun models
526            ModelId::StepFun37Flash => "3.7",
527            ModelId::StepFun5Preview => "5",
528            // Moonshot models
529            ModelId::MoonshotKimiK31Pro => "k3.1",
530            ModelId::MoonshotKimiK3 => "k3",
531            ModelId::MergeGatewayMoonshotKimiK3 => "k3",
532            ModelId::MergeGatewayMoonshotKimiK31Pro => "k3.1",
533            // Hugging Face generations
534            ModelId::HuggingFaceOpenAIGptOss20b => "oss",
535            ModelId::HuggingFaceOpenAIGptOss120b => "oss",
536            ModelId::HuggingFaceMinimaxM3Novita => "m3",
537            ModelId::HuggingFaceGlm53FlashTogether => "5.3",
538            ModelId::HuggingFaceGlm53Together => "5.3",
539            // xAI models
540            ModelId::XaiGrok46 => "4.6",
541            ModelId::XaiGrok47 => "4.7",
542            ModelId::MergeGatewayXaiGrok46 => "4.6",
543            ModelId::MergeGatewayXaiGrok47 => "4.7",
544            // Qwen models
545            ModelId::MergeGatewayDefaultRouting => "routing",
546            ModelId::MergeGatewayAnthropicClaudeOpus5 => "5",
547            ModelId::MergeGatewayAnthropicClaudeOpus55 => "5.5",
548            ModelId::MergeGatewayAnthropicClaudeSonnet5 => "5",
549            ModelId::MergeGatewayAnthropicClaudeSonnet55 => "5.5",
550            ModelId::MergeGatewayAnthropicClaudeHaiku55 => "5.5",
551            ModelId::MergeGatewayAnthropicClaudeFable51 => "5.1",
552            ModelId::MergeGatewayAnthropicClaudeFable52 => "5.2",
553            ModelId::MergeGatewayAnthropicClaudeHaiku4520251001 => "4.5",
554            ModelId::MergeGatewayMinimaxH3 => "H3",
555            ModelId::MergeGatewayThinkingMachinesInkling => "Inkling",
556            ModelId::MergeGatewayOpenAIGpt56Luna
557            | ModelId::MergeGatewayOpenAIGpt56Sol
558            | ModelId::MergeGatewayOpenAIGpt56Terra => "5.6",
559            ModelId::MergeGatewayOpenAIGpt62Astra | ModelId::MergeGatewayOpenAIGpt62Sol => "6.2",
560            ModelId::MergeGatewayOpenAIGpt6Astra
561            | ModelId::MergeGatewayOpenAIGpt6Sol
562            | ModelId::MergeGatewayOpenAIGpt6Luna => "6",
563            ModelId::MergeGatewayOpenAIGpt61Sol | ModelId::MergeGatewayOpenAIGpt61Terra => "6.1",
564            // Vercel AI Gateway models
565            ModelId::VercelAnthropicClaudeSonnet5 => "5",
566            ModelId::VercelAnthropicClaudeOpus5 => "5",
567            ModelId::VercelAnthropicClaudeOpus55 => "5.5",
568            ModelId::VercelAnthropicClaudeHaiku45 => "4.5",
569            ModelId::VercelOpenAiGpt56Sol | ModelId::VercelOpenAiGpt56Luna => "5.6",
570            ModelId::VercelOpenAiGpt6Sol | ModelId::VercelOpenAiGpt6Luna => "6",
571            ModelId::VercelOpenAiGpt6Astra => "6",
572            ModelId::VercelGoogleGemini38Flash => "3.8",
573            ModelId::VercelDeepseekFlash => "4.1",
574            ModelId::VercelMoonshotaiKimiK3 => "k3",
575            ModelId::VercelMinimaxM3 => "M3",
576            ModelId::VercelSpacexaiGrok47 => "4.7",
577            _ => "unknown",
578        }
579    }
580
581    /// Sort key that orders models by generation, newest first.
582    ///
583    /// Generation strings are heterogeneous (`6.2`, `v2.6`, `M3.1`,
584    /// `Muse-Spark-1.3`, `oss`), so every run of ASCII digits is read as a
585    /// version component. Models without digits sort last, and equal keys
586    /// keep their input order when used with a stable sort.
587    pub fn generation_sort_key(&self) -> Reverse<Vec<u64>> {
588        Reverse(generation_components(self.generation()))
589    }
590
591    /// Determine if this model supports GPT-5.1+/5.2+/5.3+ shell tool type
592    pub(crate) fn supports_shell_tool(&self) -> bool {
593        matches!(
594            self,
595            ModelId::GPT62Astra
596                | ModelId::GPT62Sol
597                | ModelId::GPT6Astra
598                | ModelId::GPT6Sol
599                | ModelId::GPT61Sol
600                | ModelId::GPT6Luna
601                | ModelId::GPT56Sol
602                | ModelId::GPT56Terra
603                | ModelId::GPT56Luna
604        )
605    }
606
607    /// Determine if this model supports optimized apply_patch tool
608    pub fn supports_apply_patch_tool(&self) -> bool {
609        false
610    }
611}
612
613#[cfg(test)]
614mod tests {
615    use super::*;
616
617    #[test]
618    fn prefixed_request_model_resolves_on_bare_provider() {
619        // Regression for `openai/gpt-5.6-luna` on provider `openai` resolving
620        // to empty `supported:` and omitting reasoning effort: exact bare hit
621        // and prefixed fallback must agree, while gateway prefixed entries
622        // keep their exact match.
623        let bare = model_catalog_entry("openai", "gpt-5.6-luna").expect("bare openai entry exists");
624        let prefixed = model_catalog_entry("openai", "openai/gpt-5.6-luna").expect("prefixed fallback must resolve");
625        assert_eq!(bare.reasoning_efforts, prefixed.reasoning_efforts);
626        assert!(!prefixed.reasoning_efforts.is_empty());
627
628        let gateway =
629            model_catalog_entry("merge-gateway", "openai/gpt-5.6-luna").expect("gateway prefixed entry exists");
630        assert!(!gateway.reasoning_efforts.is_empty());
631
632        // Asymmetric boundary: unknown model stays missing on both shapes,
633        // and an unrelated prefix must not resolve to the bare model.
634        assert!(model_catalog_entry("openai", "no-such-model-xyz").is_none());
635        assert!(model_catalog_entry("openai", "openai/no-such-model-xyz").is_none());
636        assert!(model_catalog_entry("openai", "other-org/gpt-5.6-luna").is_none());
637    }
638
639    #[test]
640    fn generation_components_reads_digit_runs_across_generation_shapes() {
641        assert_eq!(generation_components("6.2"), [6, 2]);
642        assert_eq!(generation_components("v2.6"), [2, 6]);
643        assert_eq!(generation_components("M3.1"), [3, 1]);
644        assert_eq!(generation_components("Muse-Spark-1.3"), [1, 3]);
645        assert_eq!(generation_components("5.6-luna"), [5, 6]);
646        assert_eq!(generation_components("oss"), Vec::<u64>::new());
647        assert_eq!(generation_components(""), Vec::<u64>::new());
648    }
649
650    #[test]
651    fn generation_sort_key_orders_versions_newest_first_and_keeps_ties_stable() {
652        let mut models = vec![
653            ModelId::GPT56Sol, // 5.6
654            ModelId::GPT62Sol, // 6.2
655            ModelId::GPT6Sol,  // 6
656            ModelId::GPT61Sol, // 6.1
657        ];
658        models.sort_by_key(|model| model.generation_sort_key());
659        assert_eq!(
660            models,
661            [
662                ModelId::GPT62Sol,
663                ModelId::GPT61Sol,
664                ModelId::GPT6Sol,
665                ModelId::GPT56Sol
666            ]
667        );
668
669        // Equal generations keep their arrival order (stable sort).
670        let mut ties = vec![ModelId::GPT6Luna, ModelId::GPT6Astra, ModelId::GPT6Sol]; // all "6"
671        ties.sort_by_key(|model| model.generation_sort_key());
672        assert_eq!(ties, [ModelId::GPT6Luna, ModelId::GPT6Astra, ModelId::GPT6Sol]);
673
674        // Generations with no numeric component sort after versioned ones.
675        let mut mixed = vec![ModelId::OllamaGptOss20b, ModelId::GPT6Sol]; // "oss", "6"
676        mixed.sort_by_key(|model| model.generation_sort_key());
677        assert_eq!(mixed, [ModelId::GPT6Sol, ModelId::OllamaGptOss20b]);
678    }
679}