-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels_dev.rs
More file actions
654 lines (623 loc) · 22.7 KB
/
Copy pathmodels_dev.rs
File metadata and controls
654 lines (623 loc) · 22.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
//! Models.dev runtime enrichment.
//!
//! `https://models.dev/models.json` is the authoritative cross-provider
//! model-capabilities registry (context window, max output, reasoning,
//! vision). OpenCode itself uses it. We fetch it once (cached 24h on disk)
//! and use it to enrich model capabilities for providers whose `/v1/models`
//! endpoint returns only bare `{id}` lists — which is the common case for
//! OpenAI-compatible gateways (DeepSeek, Groq, Mistral, OpenRouter, etc.).
//!
//! Enrichment is an OVERLAY: it only fills in fields that the curated table
//! and the live API left at generic defaults. Values from the live API
//! (`apply_live_model_fields`) and the curated table always take priority.
use serde::Deserialize;
use serde_json::Value;
use std::collections::HashMap;
use std::time::Duration;
const MODELS_DEV_URL: &str = "https://models.dev/models.json";
/// 24h TTL — model caps don't change often, and this avoids hitting the
/// registry on every discovery call.
const MODELS_DEV_CACHE_TTL: u64 = 86400;
/// A single model entry from models.dev (only the fields we use).
#[derive(Clone, Debug)]
pub struct ModelsDevEntry {
#[allow(dead_code)]
pub name: String,
pub context: u32,
pub output: u32,
pub reasoning: bool,
pub vision: bool,
pub input: Vec<String>,
pub output_modalities: Vec<String>,
pub tool_call: bool,
pub structured_output: bool,
}
/// Parse the models.dev JSON into a flat lookup keyed by `provider/model-id`.
fn parse_registry(data: &Value) -> HashMap<String, ModelsDevEntry> {
let Some(obj) = data.as_object() else {
return HashMap::new();
};
let mut out = HashMap::new();
for (key, val) in obj {
let context = val
.get("limit")
.and_then(|l| l.get("context"))
.and_then(|c| c.as_u64())
.unwrap_or(0) as u32;
let output = val
.get("limit")
.and_then(|l| l.get("output"))
.and_then(|c| c.as_u64())
.unwrap_or(0) as u32;
let reasoning = val
.get("reasoning")
.and_then(|r| r.as_bool())
.unwrap_or(false);
let modalities = |kind: &str| {
val.get("modalities")
.and_then(|m| m.get(kind))
.and_then(|v| v.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str().map(str::to_string))
.collect::<Vec<_>>()
})
.unwrap_or_default()
};
let input = modalities("input");
let output_modalities = modalities("output");
let vision = input.iter().any(|v| v == "image");
let name = val
.get("name")
.and_then(|n| n.as_str())
.unwrap_or("")
.to_string();
out.insert(
key.to_lowercase(),
ModelsDevEntry {
name,
context,
output,
reasoning,
vision,
input,
output_modalities,
tool_call: val
.get("tool_call")
.and_then(|v| v.as_bool())
.unwrap_or(false),
structured_output: val
.get("structured_output")
.and_then(|v| v.as_bool())
.unwrap_or(false),
},
);
}
out
}
/// Fetch the models.dev registry, using a 24h disk cache. Returns `None` on
/// any failure (non-fatal — discovery proceeds with curated/live defaults).
pub async fn fetch_models_dev(client: &reqwest::Client) -> Option<HashMap<String, ModelsDevEntry>> {
// 1. Disk cache (fresh: < 24h).
if let Some(cached) = read_disk_cache() {
return Some(cached);
}
// 2. Live fetch (10s timeout; non-fatal on failure).
let resp = client
.get(MODELS_DEV_URL)
.timeout(Duration::from_secs(10))
.header("Accept", "application/json")
.send()
.await
.ok()?;
if !resp.status().is_success() {
// Fall back to stale cache if the network failed mid-TTL.
return read_disk_cache_stale();
}
let raw = resp.text().await.ok()?;
let data: Value = serde_json::from_str(&raw).ok()?;
let map = parse_registry(&data);
if map.is_empty() {
return read_disk_cache_stale();
}
// 3. Write disk cache.
write_disk_cache(&raw);
Some(map)
}
/// Map a provider's base_url host to its models.dev provider slug.
fn provider_slug(base_url: &str) -> Option<&'static str> {
let host = base_host(base_url);
Some(match host.as_str() {
"api.deepseek.com" => "deepseek",
"api.moonshot.ai" | "api.kimi.com" => "moonshotai",
"api.mistral.ai" => "mistral",
"api.x.ai" => "xai",
"api.z.ai" | "open.bigmodel.cn" => "zhipuai",
"api.minimax.io" | "api.minimaxi.com" => "minimax",
"api.cohere.ai" => "cohere",
"integrate.api.nvidia.com" => "nvidia",
"api.perplexity.ai" => "perplexity",
"api.cerebras.ai" => "cerebras",
"api.fireworks.ai" => "fireworks",
"api.openai.com" => "openai",
"api.anthropic.com" => "anthropic",
"portal.qwen.ai" => "alibaba",
"copilot.tencent.com" => "tencent",
"api.xiaomimimo.com" | "token-plan-sgp.xiaomimimo.com" => "xiaomi",
// OpenRouter / Together / Groq / Hyperbolic / SiliconFlow serve
// third-party models — no single slug; rely on direct or suffix match.
_ => return None,
})
}
/// Look up a model in the models.dev registry, trying multiple key forms:
/// 1. Direct match on the full model id (OpenRouter-style `provider/model-id`)
/// 2. Provider-prefixed match (`slug/model-id`) based on the base_url host
/// 3. Suffix match: any key whose model-id part (after `/`) equals the id
pub fn lookup_entry<'a>(
map: &'a HashMap<String, ModelsDevEntry>,
id: &str,
base_url: &str,
) -> Option<&'a ModelsDevEntry> {
let id_lower = id.to_lowercase();
// 1. Direct match (handles OpenRouter-style prefixed IDs).
if let Some(e) = map.get(&id_lower) {
return Some(e);
}
// 2. Provider-prefixed match.
if let Some(slug) = provider_slug(base_url) {
let key = format!("{slug}/{id_lower}");
if let Some(e) = map.get(&key) {
return Some(e);
}
// Also try without version suffixes: some providers append
// "-latest" or date stamps that models.dev doesn't have.
if let Some(stripped) = strip_common_suffixes(&id_lower) {
let key = format!("{slug}/{stripped}");
if let Some(e) = map.get(&key) {
return Some(e);
}
}
}
// 3. Suffix match: find any key whose part after `/` equals the id.
// Handles providers like Groq/Together/Fireworks that serve
// third-party models under bare IDs.
if let Some(stripped) = strip_common_suffixes(&id_lower) {
if let Some(e) = suffix_match(map, &stripped) {
return Some(e);
}
}
suffix_match(map, &id_lower)
}
/// Match any models.dev key whose model-id part (after `/`) equals `id`.
fn suffix_match<'a>(
map: &'a HashMap<String, ModelsDevEntry>,
id: &str,
) -> Option<&'a ModelsDevEntry> {
// Build a reverse index on first call per lookup? No — just scan.
// The registry has ~300 entries; a linear scan is fine for a one-time
// enrichment pass per discovery.
let needle = id.to_lowercase();
for (key, entry) in map {
if let Some(model_part) = key.split('/').nth(1) {
if model_part == needle {
return Some(entry);
}
}
}
None
}
/// Strip common version/date suffixes that providers append to model IDs but
/// models.dev doesn't have (e.g. `mistral-large-latest` → `mistral-large`).
fn strip_common_suffixes(id: &str) -> Option<String> {
for suffix in ["-latest", ":latest", "-2407", "-2411"] {
if let Some(stripped) = id.strip_suffix(suffix) {
return Some(stripped.to_string());
}
}
// Strip date patterns like `-2025-01-01` or `-20250101`.
if let Some(pos) = id.rfind("-20") {
let tail = &id[pos + 1..];
if tail.len() >= 8 && tail.chars().all(|c| c.is_ascii_digit() || c == '-') {
return Some(id[..pos].to_string());
}
}
None
}
/// The generic default from `openai_model_caps`'s else branch.
/// Used to detect models that fell through the curated table.
const GENERIC_DEFAULT_CTX: u32 = 200_000;
const GENERIC_DEFAULT_MAX: u32 = 8_192;
/// Enrich a list of ModelInfos with models.dev capabilities. Overlays context
/// window, max output, reasoning, and vision ONLY for models that the curated
/// table left at generic defaults (detected by context_window == 200_000 AND
/// thinking_levels is empty). Live-API fields (from `apply_live_model_fields`)
/// always take priority since those were already applied.
pub fn enrich_models(
models: &mut [crate::protocol::ModelInfo],
map: &HashMap<String, ModelsDevEntry>,
base_url: &str,
) {
for m in models.iter_mut() {
// Only enrich models that fell through to the generic default
// (curated table missed + no live API context_length override).
let is_generic = m.context_window == GENERIC_DEFAULT_CTX
&& m.max_tokens == GENERIC_DEFAULT_MAX
&& m.thinking_levels.is_empty();
if !is_generic {
// Even for curated models, check if models.dev has vision info
// that the curated table missed (the curated else-branch sets
// vision=false as a generic default).
if m.vision {
continue; // Already has vision from curated table or live API.
}
if let Some(entry) = lookup_entry(map, &m.id, base_url) {
if entry.vision {
m.vision = true;
}
}
continue;
}
let Some(entry) = lookup_entry(map, &m.id, base_url) else {
continue;
};
// Overlay from models.dev (authoritative for this model).
// Skip zeros: an empty/broken registry entry must not pin max_tokens:0
// or wipe a usable curated/default budget.
if entry.context > 0 {
m.context_window = entry.context;
}
if entry.output > 0 {
m.max_tokens = entry.output;
}
// models.dev occasionally lists output >= context (or equal). Keep a
// generation budget strictly below the context window so compaction /
// request builders always leave prompt room.
if m.max_tokens == 0 || m.max_tokens >= m.context_window {
if m.context_window > 1 {
m.max_tokens = (m.context_window.saturating_mul(3) / 4)
.max(1)
.min(65_536)
.min(m.context_window - 1);
} else {
m.max_tokens = 1;
}
}
m.reasoning = entry.reasoning;
m.vision = entry.vision;
m.input = entry.input.clone();
m.output = entry.output_modalities.clone();
m.tool_call = entry.tool_call;
m.structured_output = entry.structured_output;
if entry.reasoning && m.thinking_levels.is_empty() {
m.thinking_levels = crate::provider::DEFAULT_THINKING_LEVELS
.iter()
.map(|s| s.to_string())
.collect();
}
}
}
// --- disk cache ---
fn disk_cache_path() -> Option<std::path::PathBuf> {
let home = crate::config::home_dir()?;
Some(home.join(".config/catalyst-code/models-dev-cache.json"))
}
fn read_disk_cache() -> Option<HashMap<String, ModelsDevEntry>> {
let path = disk_cache_path()?;
let content = std::fs::read_to_string(&path).ok()?;
let cache: DiskCache = serde_json::from_str(&content).ok()?;
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.ok()?
.as_secs();
if now.saturating_sub(cache.fetched_at) > MODELS_DEV_CACHE_TTL {
return None;
}
Some(parse_registry(&cache.data))
}
fn read_disk_cache_stale() -> Option<HashMap<String, ModelsDevEntry>> {
let path = disk_cache_path()?;
let content = std::fs::read_to_string(&path).ok()?;
let cache: DiskCache = serde_json::from_str(&content).ok()?;
Some(parse_registry(&cache.data))
}
fn write_disk_cache(raw: &str) {
let path = match disk_cache_path() {
Some(p) => p,
None => return,
};
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(parent);
}
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
let cache = DiskCache {
fetched_at: now,
data: serde_json::from_str(raw).unwrap_or(Value::Null),
};
if let Ok(text) = serde_json::to_string_pretty(&cache) {
let _ = std::fs::write(&path, text);
}
}
#[derive(Deserialize, serde::Serialize)]
struct DiskCache {
fetched_at: u64,
data: Value,
}
fn base_host(base_url: &str) -> String {
base_url
.split("://")
.nth(1)
.unwrap_or(base_url)
.split(['/', '?'])
.next()
.unwrap_or("")
.split(':')
.next()
.unwrap_or("")
.to_ascii_lowercase()
}
#[cfg(test)]
mod tests {
use super::*;
fn make_entry(context: u32, output: u32, reasoning: bool, vision: bool) -> ModelsDevEntry {
ModelsDevEntry {
name: "test".into(),
context,
output,
reasoning,
vision,
input: if vision {
vec!["text".into(), "image".into()]
} else {
vec!["text".into()]
},
output_modalities: vec!["text".into()],
tool_call: true,
structured_output: false,
}
}
#[test]
fn parse_registry_extracts_caps() {
let json = serde_json::json!({
"deepseek/deepseek-chat": {
"name": "DeepSeek Chat",
"reasoning": false,
"attachment": true,
"tool_call": true,
"limit": {"context": 1000000, "output": 384000},
"modalities": {"input": ["text"], "output": ["text"]}
},
"openai/gpt-4o": {
"name": "GPT-4o",
"reasoning": false,
"limit": {"context": 128000, "output": 16384},
"modalities": {"input": ["text", "image", "pdf"], "output": ["text"]}
}
});
let map = parse_registry(&json);
assert_eq!(map.len(), 2);
let ds = &map["deepseek/deepseek-chat"];
assert_eq!(ds.context, 1000000);
assert_eq!(ds.output, 384000);
assert!(!ds.reasoning);
assert!(!ds.vision);
let gpt4o = &map["openai/gpt-4o"];
assert_eq!(gpt4o.context, 128000);
assert_eq!(gpt4o.output, 16384);
assert!(gpt4o.vision);
}
#[test]
fn lookup_direct_match_works_for_openrouter_style_ids() {
let mut map = HashMap::new();
map.insert(
"openai/gpt-4o".into(),
make_entry(128000, 16384, false, true),
);
let e = lookup_entry(&map, "openai/gpt-4o", "https://openrouter.ai/api/v1");
assert!(e.is_some());
assert_eq!(e.unwrap().context, 128000);
}
#[test]
fn lookup_provider_prefixed_match_works_for_bare_ids() {
let mut map = HashMap::new();
map.insert(
"deepseek/deepseek-chat".into(),
make_entry(1000000, 384000, false, false),
);
let e = lookup_entry(&map, "deepseek-chat", "https://api.deepseek.com");
assert!(e.is_some());
assert_eq!(e.unwrap().context, 1000000);
}
#[test]
fn lookup_suffix_match_works_for_third_party_gateways() {
let mut map = HashMap::new();
map.insert(
"meta/llama-3.3-70b-instruct".into(),
make_entry(128000, 4096, false, false),
);
// Groq serves this under a bare id.
let e = lookup_entry(
&map,
"llama-3.3-70b-instruct",
"https://api.groq.com/openai/v1",
);
assert!(e.is_some());
assert_eq!(e.unwrap().context, 128000);
}
#[test]
fn lookup_strips_latest_suffix() {
let mut map = HashMap::new();
map.insert(
"mistral/mistral-large".into(),
make_entry(128000, 8192, false, false),
);
let e = lookup_entry(&map, "mistral-large-latest", "https://api.mistral.ai/v1");
assert!(e.is_some());
}
#[test]
fn enrich_overlays_generic_defaults() {
let mut map = HashMap::new();
map.insert(
"deepseek/deepseek-chat".into(),
make_entry(1000000, 384000, false, false),
);
let mut models = vec![crate::protocol::ModelInfo {
id: "deepseek-chat".into(),
name: "DeepSeek Chat".into(),
reasoning: true,
context_window: 200_000,
max_tokens: 8_192,
thinking_levels: Vec::new(),
vision: false,
provider: String::new(),
..Default::default()
}];
enrich_models(&mut models, &map, "https://api.deepseek.com");
assert_eq!(models[0].context_window, 1000000);
assert_eq!(models[0].max_tokens, 384000);
assert!(!models[0].reasoning); // deepseek-chat is not a reasoning model
}
#[test]
fn enrich_preserves_curated_values() {
// A model that matched the curated table (non-default context + thinking_levels)
// should NOT have its context/max overwritten by models.dev.
let mut map = HashMap::new();
map.insert(
"openai/gpt-4o".into(),
make_entry(128000, 16384, false, true),
);
let mut models = vec![crate::protocol::ModelInfo {
id: "gpt-4o".into(),
name: "GPT-4o".into(),
reasoning: false,
context_window: 128000,
max_tokens: 16384,
thinking_levels: vec!["low".into()],
vision: false,
provider: String::new(),
..Default::default()
}];
enrich_models(&mut models, &map, "https://api.openai.com/v1");
// Context and max preserved (not generic default).
assert_eq!(models[0].context_window, 128000);
assert_eq!(models[0].max_tokens, 16384);
// But vision should be overlaid since it was false and models.dev says true.
assert!(models[0].vision);
}
#[test]
fn enrich_adds_thinking_levels_for_reasoning_models() {
let mut map = HashMap::new();
map.insert(
"deepseek/deepseek-reasoner".into(),
make_entry(1000000, 384000, true, false),
);
let mut models = vec![crate::protocol::ModelInfo {
id: "deepseek-reasoner".into(),
name: "DeepSeek Reasoner".into(),
reasoning: true,
context_window: 200_000,
max_tokens: 8_192,
thinking_levels: Vec::new(),
vision: false,
provider: String::new(),
..Default::default()
}];
enrich_models(&mut models, &map, "https://api.deepseek.com");
assert!(models[0].reasoning);
assert!(!models[0].thinking_levels.is_empty());
}
#[test]
fn enrich_clamps_output_that_meets_or_exceeds_context() {
let mut map = HashMap::new();
map.insert(
"vendor/equal-caps".into(),
make_entry(1000, 1000, false, false),
);
map.insert(
"vendor/over-caps".into(),
make_entry(1000, 5000, false, false),
);
let mut models = vec![
crate::protocol::ModelInfo {
id: "equal-caps".into(),
name: "Equal".into(),
reasoning: false,
context_window: 200_000,
max_tokens: 8_192,
thinking_levels: Vec::new(),
vision: false,
provider: String::new(),
..Default::default()
},
crate::protocol::ModelInfo {
id: "over-caps".into(),
name: "Over".into(),
reasoning: false,
context_window: 200_000,
max_tokens: 8_192,
thinking_levels: Vec::new(),
vision: false,
provider: String::new(),
..Default::default()
},
];
enrich_models(&mut models, &map, "https://api.example.com/v1");
assert_eq!(models[0].context_window, 1000);
assert!(models[0].max_tokens < 1000 && models[0].max_tokens > 0);
assert_eq!(models[1].context_window, 1000);
assert!(models[1].max_tokens < 1000 && models[1].max_tokens > 0);
}
#[test]
fn enrich_skips_zero_output_from_registry() {
let mut map = HashMap::new();
map.insert("vendor/broken".into(), make_entry(128000, 0, false, false));
let mut models = vec![crate::protocol::ModelInfo {
id: "broken".into(),
name: "Broken".into(),
reasoning: false,
context_window: 200_000,
max_tokens: 8_192,
thinking_levels: Vec::new(),
vision: false,
provider: String::new(),
..Default::default()
}];
enrich_models(&mut models, &map, "https://api.example.com/v1");
assert_eq!(models[0].context_window, 128000);
assert!(models[0].max_tokens > 0);
assert!(models[0].max_tokens < models[0].context_window);
}
#[test]
fn provider_slug_maps_known_hosts() {
assert_eq!(provider_slug("https://api.deepseek.com"), Some("deepseek"));
assert_eq!(
provider_slug("https://api.moonshot.ai/v1"),
Some("moonshotai")
);
assert_eq!(provider_slug("https://api.mistral.ai/v1"), Some("mistral"));
assert_eq!(
provider_slug("https://api.z.ai/api/coding/paas/v4"),
Some("zhipuai")
);
assert_eq!(provider_slug("https://openrouter.ai/api/v1"), None);
assert_eq!(provider_slug("https://api.groq.com/openai/v1"), None);
}
#[test]
fn strip_suffixes() {
assert_eq!(
strip_common_suffixes("mistral-large-latest"),
Some("mistral-large".into())
);
assert_eq!(
strip_common_suffixes("model-2025-01-01"),
Some("model".into())
);
assert_eq!(
strip_common_suffixes("model-20250101"),
Some("model".into())
);
assert_eq!(strip_common_suffixes("deepseek-chat"), None);
}
}