feat: fetch model metadata from models.dev live with bundled fallback (#12560)

This commit is contained in:
Jack Amadeo
2026-09-28 19:46:13 +00:00
committed by GitHub
parent 5f9deb5141
commit 4dea9b483e
12 changed files with 505 additions and 370 deletions
+1 -1
View File
@@ -170,7 +170,7 @@ build-lean:
cargo build -p goose --bin goose-acp \
--profile lean \
--no-default-features \
--features native-tls
--features native-tls,online-model-meta
# Budgets are per-platform. ELF carries several MiB that Mach-O does not for the
# same code: DWARF .eh_frame instead of compact unwind info, and a .rela.dyn
+2
View File
@@ -109,7 +109,9 @@ default = [
"platform-apps",
"chat-recall",
"acp-http",
"online-model-meta",
]
online-model-meta = ["goose/online-model-meta"]
roaming = ["dep:goose-roaming", "dep:qrcode", "dep:fs2", "dep:agent-client-protocol"]
code-mode = ["goose/code-mode"]
tree-sitter = ["goose/tree-sitter"]
+3
View File
@@ -21,6 +21,9 @@ async fn run() -> Result<()> {
eprintln!("Warning: Failed to initialize logging: {}", e);
}
#[cfg(feature = "online-model-meta")]
goose::model_catalog::initialize();
let result = cli().await;
#[cfg(feature = "otel")]
+2 -2
View File
@@ -583,7 +583,7 @@ pub trait Provider: Send + Sync {
let mut models_with_dates: Vec<(String, Option<String>)> = all_models
.iter()
.filter_map(|model| {
let canonical_model = map_to_canonical_model(provider_name, model, registry)
let canonical_model = map_to_canonical_model(provider_name, model, &registry)
.and_then(|canonical_id| {
let (provider, model_name) = canonical_id.split_once('/')?;
registry.get(provider, model_name)
@@ -653,7 +653,7 @@ pub trait Provider: Send + Sync {
Ok(map_to_canonical_model(
self.get_name(),
provider_model,
registry,
&registry,
))
}
+4 -3
View File
@@ -1,5 +1,6 @@
pub mod catalog;
mod model;
pub mod models_dev;
mod name_builder;
mod registry;
@@ -7,7 +8,7 @@ pub use model::{CanonicalModel, Limit, Modalities, Modality, Pricing, ThinkingMo
pub use name_builder::{
canonical_name, map_provider_name, map_to_canonical_model, strip_version_suffix,
};
pub use registry::CanonicalModelRegistry;
pub use registry::{load_cached_catalog, refresh_remote_catalog, CanonicalModelRegistry};
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ModelMapping {
@@ -166,7 +167,7 @@ fn should_clear_catalog_pricing(provider: &str) -> bool {
pub fn maybe_get_canonical_model(provider: &str, model: &str) -> Option<CanonicalModel> {
let registry = CanonicalModelRegistry::bundled().ok()?;
let canonical_id = map_to_canonical_model(provider, model, registry)?;
let canonical_id = map_to_canonical_model(provider, model, &registry)?;
let mut canonical = if let Some((canon_provider, canon_model)) = canonical_id.split_once('/') {
registry.get(canon_provider, canon_model).cloned()?
} else {
@@ -181,7 +182,7 @@ pub fn maybe_get_canonical_model(provider: &str, model: &str) -> Option<Canonica
// row carries the rate it actually charges to proxy that model, so prefer it. Where
// there is no such row, report nothing: billing paid proxied inference as free is
// worse than showing no estimate at all.
canonical.cost = host_catalog_pricing(provider, model, registry).unwrap_or_default();
canonical.cost = host_catalog_pricing(provider, model, &registry).unwrap_or_default();
}
Some(canonical)
@@ -0,0 +1,243 @@
use super::{
canonical_name, CanonicalModel, CanonicalModelRegistry, Limit, Modalities, Modality, Pricing,
ThinkingMode,
};
use anyhow::{Context, Result};
use serde_json::Value;
use std::collections::BTreeMap;
const DEFAULT_CONTEXT_LIMIT: usize = 128_000;
fn normalize_provider_name(provider: &str) -> &str {
match provider {
"llama" => "meta-llama",
"xai" => "x-ai",
"mistral" => "mistralai",
_ => provider,
}
}
fn get_string(value: &Value, field: &str) -> Option<String> {
value.get(field).and_then(|v| v.as_str()).map(String::from)
}
fn get_thinking_mode(canonical_id: &str, value: &Value) -> Option<ThinkingMode> {
value
.get("thinking_mode")
.and_then(|v| v.as_str())
.and_then(|mode| serde_json::from_value(Value::String(mode.to_string())).ok())
.or_else(|| inferred_thinking_mode(canonical_id))
}
fn inferred_thinking_mode(canonical_id: &str) -> Option<ThinkingMode> {
match canonical_id {
"anthropic/claude-fable-5" => Some(ThinkingMode::AlwaysOnAdaptive),
"anthropic/claude-fable-5.1" => Some(ThinkingMode::AlwaysOnAdaptive),
"anthropic/claude-opus-5" => Some(ThinkingMode::Adaptive),
"anthropic/claude-opus-5.5" => Some(ThinkingMode::AlwaysOnAdaptive),
"anthropic/claude-opus-4.6" => Some(ThinkingMode::Adaptive),
"anthropic/claude-opus-4.7" => Some(ThinkingMode::Adaptive),
"anthropic/claude-opus-4.8" => Some(ThinkingMode::Adaptive),
"anthropic/claude-sonnet-4.6" => Some(ThinkingMode::Adaptive),
"anthropic/claude-sonnet-5" => Some(ThinkingMode::Adaptive),
_ => None,
}
}
fn parse_modalities(model_data: &Value, field: &str) -> Vec<Modality> {
model_data
.get("modalities")
.and_then(|m| m.get(field))
.and_then(|v| v.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str())
.filter_map(|s| {
serde_json::from_value(serde_json::Value::String(s.to_string())).ok()
})
.collect()
})
.unwrap_or_else(|| vec![Modality::Text])
}
fn process_model(
model_id: &str,
model_data: &Value,
normalized_provider: &str,
) -> Result<(String, CanonicalModel)> {
let name = model_data["name"]
.as_str()
.with_context(|| format!("Model {} missing name", model_id))?;
let canonical_id = canonical_name(normalized_provider, model_id);
let modalities = Modalities {
input: parse_modalities(model_data, "input"),
output: parse_modalities(model_data, "output"),
};
let cost = match model_data.get("cost") {
Some(c) if !c.is_null() => Pricing {
input: c.get("input").and_then(|v| v.as_f64()),
output: c.get("output").and_then(|v| v.as_f64()),
cache_read: c.get("cache_read").and_then(|v| v.as_f64()),
cache_write: c.get("cache_write").and_then(|v| v.as_f64()),
},
_ => Pricing {
input: None,
output: None,
cache_read: None,
cache_write: None,
},
};
let limit = Limit {
context: model_data
.get("limit")
.and_then(|l| l.get("context"))
.and_then(|v| v.as_u64())
.unwrap_or(DEFAULT_CONTEXT_LIMIT as u64) as usize,
output: model_data
.get("limit")
.and_then(|l| l.get("output"))
.and_then(|v| v.as_u64())
.map(|v| v as usize),
};
let canonical_model = CanonicalModel {
id: canonical_id.clone(),
name: name.to_string(),
family: get_string(model_data, "family"),
attachment: model_data.get("attachment").and_then(|v| v.as_bool()),
reasoning: model_data.get("reasoning").and_then(|v| v.as_bool()),
thinking_mode: get_thinking_mode(&canonical_id, model_data),
tool_call: model_data
.get("tool_call")
.and_then(|v| v.as_bool())
.unwrap_or(false),
temperature: model_data.get("temperature").and_then(|v| v.as_bool()),
knowledge: get_string(model_data, "knowledge"),
release_date: get_string(model_data, "release_date"),
last_updated: get_string(model_data, "last_updated"),
modalities,
open_weights: model_data.get("open_weights").and_then(|v| v.as_bool()),
cost,
limit,
};
let model_name = canonical_id
.strip_prefix(&format!("{}/", normalized_provider))
.unwrap_or(model_id)
.to_string();
Ok((model_name, canonical_model))
}
fn pick_winning_variant(variants: &[(String, CanonicalModel)]) -> usize {
variants
.iter()
.enumerate()
.min_by(|(_, (id_a, a)), (_, (id_b, b))| {
id_a.len()
.cmp(&id_b.len())
.then_with(|| b.last_updated.cmp(&a.last_updated))
.then_with(|| b.release_date.cmp(&a.release_date))
.then_with(|| id_a.cmp(id_b))
})
.map(|(idx, _)| idx)
.unwrap_or(0)
}
pub fn from_models_dev(content: &str) -> Result<CanonicalModelRegistry> {
let json: Value = serde_json::from_str(content)?;
let providers = json
.as_object()
.context("Expected object in models.dev response")?;
let mut registry = CanonicalModelRegistry::new();
for (provider_key, provider_data) in providers {
let models = match provider_data.get("models").and_then(Value::as_object) {
Some(models) => models,
None => continue,
};
let provider = normalize_provider_name(provider_key);
let mut candidates: BTreeMap<String, Vec<(String, CanonicalModel)>> = BTreeMap::new();
for (model_id, model_data) in models {
let (name, model) = process_model(model_id, model_data, provider)?;
candidates
.entry(name)
.or_default()
.push((model_id.clone(), model));
}
for (name, variants) in candidates {
let winner = pick_winning_variant(&variants);
registry.register(provider, &name, variants[winner].1.clone());
}
}
if registry.count() == 0 {
anyhow::bail!("models.dev catalog is empty");
}
Ok(registry)
}
#[cfg(test)]
mod tests {
use super::*;
fn variant(id: &str, release: Option<&str>, updated: Option<&str>) -> (String, CanonicalModel) {
(
id.to_string(),
CanonicalModel {
id: format!("openai/{id}"),
name: id.to_string(),
family: None,
attachment: None,
reasoning: None,
thinking_mode: None,
tool_call: false,
temperature: None,
knowledge: None,
release_date: release.map(String::from),
last_updated: updated.map(String::from),
modalities: Modalities::default(),
open_weights: None,
cost: Pricing::default(),
limit: Limit::default(),
},
)
}
#[test]
fn shortest_variant_wins() {
let variants = vec![
variant("gpt-4o-2024-08-06", Some("2024-08-06"), Some("2024-08-06")),
variant("gpt-4o", Some("2024-05-13"), Some("2024-08-06")),
variant("gpt-4o-2024-11-20", Some("2024-11-20"), Some("2024-11-20")),
variant("gpt-4o-2024-05-13", Some("2024-05-13"), Some("2024-05-13")),
];
assert_eq!(variants[pick_winning_variant(&variants)].0, "gpt-4o");
let variants = vec![
variant(
"claude-haiku-4-5-20251001",
Some("2025-10-16"),
Some("2025-10-16"),
),
variant("claude-haiku-4-5", Some("2025-10-16"), Some("2025-10-16")),
];
assert_eq!(
variants[pick_winning_variant(&variants)].0,
"claude-haiku-4-5"
);
}
#[test]
fn converts_provider_models_and_rejects_empty_catalog() {
let json = r#"{"openai":{"models":{"gpt-4o":{"name":"GPT-4o","tool_call":true,"limit":{"context":128000,"output":4096},"cost":{"input":2.5}}}}}"#;
let registry = from_models_dev(json).unwrap();
let model = registry.get("openai", "gpt-4o").unwrap();
assert_eq!(model.id, "openai/gpt-4o");
assert_eq!(model.limit.context, 128000);
assert_eq!(model.cost.input, Some(2.5));
assert!(from_models_dev("{}").is_err());
}
}
@@ -334,262 +334,262 @@ mod tests {
// === Direct provider (non-hosting) ===
assert_eq!(
map_to_canonical_model("anthropic", "claude-sonnet-4-5-20250929", r),
map_to_canonical_model("anthropic", "claude-sonnet-4-5-20250929", &r),
Some("anthropic/claude-sonnet-4.5".to_string())
);
assert_eq!(
map_to_canonical_model("openai", "gpt-4o-latest", r),
map_to_canonical_model("openai", "gpt-4o-latest", &r),
Some("openai/gpt-4o".to_string())
);
assert_eq!(
map_to_canonical_model("xai_oauth", "grok-4.5", r),
map_to_canonical_model("xai_oauth", "grok-4.5", &r),
Some("x-ai/grok-4.5".to_string())
);
assert_eq!(
map_to_canonical_model("openai", "gpt-4-turbo-2024-04-09", r),
map_to_canonical_model("openai", "gpt-4-turbo-2024-04-09", &r),
Some("openai/gpt-4-turbo".to_string())
);
assert_eq!(
map_to_canonical_model("chatgpt_codex", "gpt-5.6-sol", r),
map_to_canonical_model("chatgpt_codex", "gpt-5.6-sol", &r),
Some("openai/gpt-5.6-sol".to_string())
);
assert_eq!(
map_to_canonical_model("opencode_go", "kimi-k2.6", r),
map_to_canonical_model("opencode_go", "kimi-k2.6", &r),
Some("opencode-go/kimi-k2.6".to_string())
);
assert_eq!(
map_to_canonical_model("opencode_zen", "kimi-k3", r),
map_to_canonical_model("opencode_zen", "kimi-k3", &r),
Some("opencode/kimi-k3".to_string())
);
// === OpenRouter ===
assert_eq!(
map_to_canonical_model("openrouter", "anthropic/claude-sonnet-4.5", r),
map_to_canonical_model("openrouter", "anthropic/claude-sonnet-4.5", &r),
Some("openrouter/anthropic/claude-sonnet-4.5".to_string())
);
// === Anthropic Claude - basic ===
assert_eq!(
map_to_canonical_model("databricks", "claude-sonnet-4-5", r),
map_to_canonical_model("databricks", "claude-sonnet-4-5", &r),
Some("anthropic/claude-sonnet-4.5".to_string())
);
assert_eq!(
map_to_canonical_model("databricks", "claude-sonnet-4-5-20250929", r),
map_to_canonical_model("databricks", "claude-sonnet-4-5-20250929", &r),
Some("anthropic/claude-sonnet-4.5".to_string())
);
assert_eq!(
map_to_canonical_model("databricks", "claude-sonnet-4-5-latest", r),
map_to_canonical_model("databricks", "claude-sonnet-4-5-latest", &r),
Some("anthropic/claude-sonnet-4.5".to_string())
);
// 4.x: {version}-{model} → {model}-{version}
assert_eq!(
map_to_canonical_model("databricks", "claude-sonnet-4-5", r),
map_to_canonical_model("databricks", "claude-sonnet-4-5", &r),
Some("anthropic/claude-sonnet-4.5".to_string())
);
// 4.x with minor version + prefix stripping
assert_eq!(
map_to_canonical_model("databricks", "raml-claude-opus-4-5", r),
map_to_canonical_model("databricks", "raml-claude-opus-4-5", &r),
Some("anthropic/claude-opus-4.5".to_string())
);
// === Claude with platform suffixes ===
assert_eq!(
map_to_canonical_model("databricks", "claude-sonnet-4-5-bedrock", r),
map_to_canonical_model("databricks", "claude-sonnet-4-5-bedrock", &r),
Some("anthropic/claude-sonnet-4.5".to_string())
);
assert_eq!(
map_to_canonical_model("databricks", "goose-claude-sonnet-4-5-bedrock", r),
map_to_canonical_model("databricks", "goose-claude-sonnet-4-5-bedrock", &r),
Some("anthropic/claude-sonnet-4.5".to_string())
);
assert_eq!(
map_to_canonical_model("bedrock", "claude-sonnet-4-5", r),
map_to_canonical_model("bedrock", "claude-sonnet-4-5", &r),
Some("anthropic/claude-sonnet-4.5".to_string())
);
assert_eq!(
map_to_canonical_model("aws_bedrock", "global.anthropic.claude-sonnet-5", r),
map_to_canonical_model("aws_bedrock", "global.anthropic.claude-sonnet-5", &r),
Some("amazon-bedrock/global.anthropic.claude-sonnet-5".to_string())
);
assert_eq!(
map_to_canonical_model("anthropic", "claude-sonnet-5", r),
map_to_canonical_model("anthropic", "claude-sonnet-5", &r),
Some("anthropic/claude-sonnet-5".to_string())
);
// === OpenAI GPT ===
assert_eq!(
map_to_canonical_model("databricks", "gpt-4o", r),
map_to_canonical_model("databricks", "gpt-4o", &r),
Some("openai/gpt-4o".to_string())
);
assert_eq!(
map_to_canonical_model("databricks", "gpt-4o-2024-11-20", r),
map_to_canonical_model("databricks", "gpt-4o-2024-11-20", &r),
Some("openai/gpt-4o".to_string())
);
assert_eq!(
map_to_canonical_model("databricks", "gpt-4o-latest", r),
map_to_canonical_model("databricks", "gpt-4o-latest", &r),
Some("openai/gpt-4o".to_string())
);
assert_eq!(
map_to_canonical_model("databricks", "kgoose-gpt-4o", r),
map_to_canonical_model("databricks", "kgoose-gpt-4o", &r),
Some("openai/gpt-4o".to_string())
);
assert_eq!(
map_to_canonical_model("azure", "gpt-4o", r),
map_to_canonical_model("azure", "gpt-4o", &r),
Some("openai/gpt-4o".to_string())
);
assert_eq!(
map_to_canonical_model("azure_foundry", "gpt-4o", r),
map_to_canonical_model("azure_foundry", "gpt-4o", &r),
Some("openai/gpt-4o".to_string())
);
assert_eq!(
map_to_canonical_model("azure_foundry", "Phi-4", r),
map_to_canonical_model("azure_foundry", "Phi-4", &r),
Some("azure/phi-4".to_string())
);
// === OpenAI O-series ===
assert_eq!(
map_to_canonical_model("databricks", "goose-o1", r),
map_to_canonical_model("databricks", "goose-o1", &r),
Some("openai/o1".to_string())
);
assert_eq!(
map_to_canonical_model("databricks", "kgoose-o3", r),
map_to_canonical_model("databricks", "kgoose-o3", &r),
Some("openai/o3".to_string())
);
assert_eq!(
map_to_canonical_model("databricks", "headless-goose-o3-mini", r),
map_to_canonical_model("databricks", "headless-goose-o3-mini", &r),
Some("openai/o3-mini".to_string())
);
// === Google Gemini ===
assert_eq!(
map_to_canonical_model("databricks", "gemini-2-5-flash", r),
map_to_canonical_model("databricks", "gemini-2-5-flash", &r),
Some("google/gemini-2.5-flash".to_string())
);
// === Meta Llama ===
assert_eq!(
map_to_canonical_model("databricks", "meta-llama-3-3-70b-instruct", r),
map_to_canonical_model("databricks", "meta-llama-3-3-70b-instruct", &r),
Some("meta-llama/llama-3.3-70b-instruct".to_string())
);
// === Mistral variants ===
assert_eq!(
map_to_canonical_model("databricks", "codestral", r),
map_to_canonical_model("databricks", "codestral", &r),
Some("mistralai/codestral".to_string())
);
assert_eq!(
map_to_canonical_model("databricks", "ministral-3b", r),
map_to_canonical_model("databricks", "ministral-3b", &r),
Some("mistralai/ministral-3b".to_string())
);
// === DeepSeek ===
assert_eq!(
map_to_canonical_model("databricks", "databricks-deepseek-v4-flash", r),
map_to_canonical_model("databricks", "databricks-deepseek-v4-flash", &r),
Some("deepseek/deepseek-v4-flash".to_string())
);
assert_eq!(
map_to_canonical_model("databricks", "deepseek-v4-pro", r),
map_to_canonical_model("databricks", "deepseek-v4-pro", &r),
Some("deepseek/deepseek-v4-pro".to_string())
);
// === Grok (X.AI) ===
assert_eq!(
map_to_canonical_model("databricks", "grok-4.3", r),
map_to_canonical_model("databricks", "grok-4.3", &r),
Some("x-ai/grok-4.3".to_string())
);
assert_eq!(
map_to_canonical_model("databricks", "databricks-grok-4.3", r),
map_to_canonical_model("databricks", "databricks-grok-4.3", &r),
Some("x-ai/grok-4.3".to_string())
);
assert_eq!(
map_to_canonical_model("databricks", "kgoose-grok-4.3", r),
map_to_canonical_model("databricks", "kgoose-grok-4.3", &r),
Some("x-ai/grok-4.3".to_string())
);
// === Cohere Command ===
// Note: version suffix "-2024" is stripped by canonical_name
assert_eq!(
map_to_canonical_model("databricks", "command-r-plus-08-2024", r),
map_to_canonical_model("databricks", "command-r-plus-08-2024", &r),
Some("cohere/command-r-plus-08".to_string())
);
assert_eq!(
map_to_canonical_model("databricks", "goose-command-r-08-2024", r),
map_to_canonical_model("databricks", "goose-command-r-08-2024", &r),
Some("cohere/command-r-08".to_string())
);
// === Provider-prefixed extraction ===
assert_eq!(
map_to_canonical_model("databricks", "anthropic-claude-sonnet-4-5", r),
map_to_canonical_model("databricks", "anthropic-claude-sonnet-4-5", &r),
Some("anthropic/claude-sonnet-4.5".to_string())
);
assert_eq!(
map_to_canonical_model("databricks", "openai-gpt-4o", r),
map_to_canonical_model("databricks", "openai-gpt-4o", &r),
Some("openai/gpt-4o".to_string())
);
assert_eq!(
map_to_canonical_model("databricks", "google-gemini-2-5-flash", r),
map_to_canonical_model("databricks", "google-gemini-2-5-flash", &r),
Some("google/gemini-2.5-flash".to_string())
);
assert_eq!(
map_to_canonical_model("databricks", "mistralai-codestral", r),
map_to_canonical_model("databricks", "mistralai-codestral", &r),
Some("mistralai/codestral".to_string())
);
assert_eq!(
map_to_canonical_model("databricks", "deepseek-deepseek-v4-flash", r),
map_to_canonical_model("databricks", "deepseek-deepseek-v4-flash", &r),
Some("deepseek/deepseek-v4-flash".to_string())
);
assert_eq!(
map_to_canonical_model("databricks", "x-ai-grok-4.3", r),
map_to_canonical_model("databricks", "x-ai-grok-4.3", &r),
Some("x-ai/grok-4.3".to_string())
);
// === Zhipu AI ===
assert_eq!(
map_to_canonical_model("zhipu", "glm-4.7", r),
map_to_canonical_model("zhipu", "glm-4.7", &r),
Some("zhipuai/glm-4.7".to_string())
);
assert_eq!(
map_to_canonical_model("zhipu", "glm-5", r),
map_to_canonical_model("zhipu", "glm-5", &r),
Some("zhipuai/glm-5".to_string())
);
// === Kimi Code ===
assert_eq!(
map_to_canonical_model("kimi_code", "kimi-for-coding", r),
map_to_canonical_model("kimi_code", "kimi-for-coding", &r),
Some("kimi-code-plan-cn/kimi-for-coding".to_string())
);
assert_eq!(
map_to_canonical_model("kimi_code", "kimi-for-coding-highspeed", r),
map_to_canonical_model("kimi_code", "kimi-for-coding-highspeed", &r),
Some("kimi-code-plan-cn/kimi-for-coding-highspeed".to_string())
);
assert_eq!(
map_to_canonical_model("kimi_code", "k3", r),
map_to_canonical_model("kimi_code", "k3", &r),
Some("kimi-code-plan-cn/k3".to_string())
);
// === GCP Vertex AI ===
assert_eq!(
map_to_canonical_model("gcp_vertex_ai", "gemini-2.5-flash", r),
map_to_canonical_model("gcp_vertex_ai", "gemini-2.5-flash", &r),
Some("google-vertex/gemini-2.5-flash".to_string())
);
assert_eq!(
map_to_canonical_model("gcp_vertex_ai", "gemini-2.5-pro", r),
map_to_canonical_model("gcp_vertex_ai", "gemini-2.5-pro", &r),
Some("google-vertex/gemini-2.5-pro".to_string())
);
assert_eq!(
map_to_canonical_model("gcp_vertex_ai", "claude-sonnet-4@20250514", r),
map_to_canonical_model("gcp_vertex_ai", "claude-sonnet-4@20250514", &r),
Some("google-vertex/claude-sonnet-4".to_string())
);
assert_eq!(
map_to_canonical_model("gcp_vertex_ai", "claude-sonnet-4-5@20250929", r),
map_to_canonical_model("gcp_vertex_ai", "claude-sonnet-4-5@20250929", &r),
Some("google-vertex/claude-sonnet-4.5".to_string())
);
assert_eq!(
map_to_canonical_model("gcp_vertex_ai", "claude-opus-4-5@20251101", r),
map_to_canonical_model("gcp_vertex_ai", "claude-opus-4-5@20251101", &r),
Some("google-vertex/claude-opus-4.5".to_string())
);
assert_eq!(
map_to_canonical_model("gcp_vertex_ai", "claude-haiku-4-5@20251001", r),
map_to_canonical_model("gcp_vertex_ai", "claude-haiku-4-5@20251001", &r),
Some("google-vertex/claude-haiku-4.5".to_string())
);
}
@@ -603,16 +603,16 @@ mod tests {
let r = super::super::CanonicalModelRegistry::bundled().unwrap();
assert_eq!(
map_to_canonical_model("databricks_v2", "databricks-gpt-oss-120b", r),
map_to_canonical_model("databricks_v2", "databricks-gpt-oss-120b", &r),
Some("databricks/databricks-gpt-oss-120b".to_string())
);
assert_eq!(
map_to_canonical_model("databricks_v2", "databricks-gpt-oss-20b", r),
map_to_canonical_model("databricks_v2", "databricks-gpt-oss-20b", &r),
Some("databricks/databricks-gpt-oss-20b".to_string())
);
// Legacy provider name resolves identically.
assert_eq!(
map_to_canonical_model("databricks", "databricks-gpt-oss-120b", r),
map_to_canonical_model("databricks", "databricks-gpt-oss-120b", &r),
Some("databricks/databricks-gpt-oss-120b".to_string())
);
@@ -621,7 +621,7 @@ mod tests {
// first-party "anthropic/*" entry (which carries thinking_mode used for
// adaptive thinking), not the metadata-poor "databricks/databricks-*" one.
assert_eq!(
map_to_canonical_model("databricks", "databricks-claude-opus-4-7", r),
map_to_canonical_model("databricks", "databricks-claude-opus-4-7", &r),
Some("anthropic/claude-opus-4.7".to_string())
);
}
@@ -1,35 +1,39 @@
use super::CanonicalModel;
use anyhow::{Context, Result};
use anyhow::{bail, Context, Result};
use futures::StreamExt;
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::path::Path;
use reqwest::header::{ETAG, IF_NONE_MATCH};
use std::collections::{HashMap, HashSet};
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::sync::{RwLock, RwLockReadGuard};
use std::time::Duration;
/// Cached bundled canonical model registry
static BUNDLED_REGISTRY: Lazy<Result<CanonicalModelRegistry>> = Lazy::new(|| {
const CANONICAL_MODELS_ZSTD: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/canonical_models.json.zst"));
const MAX_CATALOG_BYTES: usize = 32 * 1024 * 1024;
const CATALOG_FILENAME: &str = "models_dev_api.json";
const ETAG_FILENAME: &str = "models_dev_api.etag";
let json = zstd::decode_all(CANONICAL_MODELS_ZSTD)
.context("Failed to decompress bundled canonical models JSON")?;
let models: Vec<CanonicalModel> =
serde_json::from_slice(&json).context("Failed to parse bundled canonical models JSON")?;
let mut registry = CanonicalModelRegistry::new();
for model in models {
// Extract provider and model from id (format: "provider/model")
if let Some((provider, model_name)) = model.id.split_once('/') {
let provider = provider.to_string();
let model_name = model_name.to_string();
registry.register(&provider, &model_name, model);
}
}
Ok(registry)
static ACTIVE_REGISTRY: Lazy<RwLock<Result<CanonicalModelRegistry>>> = Lazy::new(|| {
let bundled = zstd::decode_all(
include_bytes!(concat!(env!("OUT_DIR"), "/canonical_models.json.zst")).as_slice(),
)
.context("Failed to decompress bundled canonical models JSON")
.and_then(|json| CanonicalModelRegistry::from_json(std::str::from_utf8(&json)?));
RwLock::new(bundled)
});
pub struct CanonicalModelRegistryGuard(RwLockReadGuard<'static, Result<CanonicalModelRegistry>>);
impl Deref for CanonicalModelRegistryGuard {
type Target = CanonicalModelRegistry;
fn deref(&self) -> &Self::Target {
self.0.as_ref().expect("validated registry")
}
}
#[derive(Debug, Clone)]
pub struct CanonicalModelRegistry {
// Key: (provider, model) tuple
models: HashMap<(String, String), CanonicalModel>,
}
@@ -40,40 +44,50 @@ impl CanonicalModelRegistry {
}
}
pub fn bundled() -> Result<&'static Self> {
BUNDLED_REGISTRY
.as_ref()
.map_err(|e| anyhow::anyhow!("{}", e))
pub fn bundled() -> Result<CanonicalModelRegistryGuard> {
let guard = ACTIVE_REGISTRY
.read()
.map_err(|_| anyhow::anyhow!("canonical model registry lock poisoned"))?;
if let Err(error) = &*guard {
anyhow::bail!("{error}");
}
Ok(CanonicalModelRegistryGuard(guard))
}
pub fn from_json(content: &str) -> Result<Self> {
let models: Vec<CanonicalModel> =
serde_json::from_str(content).context("Failed to parse canonical models JSON")?;
if models.is_empty() {
bail!("canonical model catalog is empty");
}
let mut seen = HashSet::new();
let mut registry = Self::new();
for model in models {
if !seen.insert(model.id.clone()) {
bail!("duplicate canonical model id: {}", model.id);
}
let (provider, model_name) = model
.id
.split_once('/')
.with_context(|| format!("invalid canonical model id: {}", model.id))?;
registry.register(provider, model_name, model.clone());
}
Ok(registry)
}
pub fn from_file(path: impl AsRef<Path>) -> Result<Self> {
let content = std::fs::read_to_string(path.as_ref())
.context("Failed to read canonical models file")?;
let models: Vec<CanonicalModel> =
serde_json::from_str(&content).context("Failed to parse canonical models JSON")?;
let mut registry = Self::new();
for model in models {
if let Some((provider, model_name)) = model.id.split_once('/') {
let provider = provider.to_string();
let model_name = model_name.to_string();
registry.register(&provider, &model_name, model);
}
}
Ok(registry)
Self::from_json(&content)
}
pub fn to_file(&self, path: impl AsRef<Path>) -> Result<()> {
let mut models: Vec<&CanonicalModel> = self.models.values().collect();
models.sort_by(|a, b| a.id.cmp(&b.id));
let json = serde_json::to_string_pretty(&models)
.context("Failed to serialize canonical models")?;
std::fs::write(path.as_ref(), json).context("Failed to write canonical models file")?;
Ok(())
}
@@ -108,3 +122,95 @@ impl Default for CanonicalModelRegistry {
Self::new()
}
}
fn activate(registry: CanonicalModelRegistry) -> Result<()> {
*ACTIVE_REGISTRY
.write()
.map_err(|_| anyhow::anyhow!("canonical model registry lock poisoned"))? = Ok(registry);
Ok(())
}
pub fn load_cached_catalog(cache_dir: &Path) -> Result<bool> {
let path = cache_dir.join(CATALOG_FILENAME);
if !path.exists() {
return Ok(false);
}
let content = std::fs::read_to_string(path)?;
activate(super::models_dev::from_models_dev(&content)?)?;
Ok(true)
}
pub async fn refresh_remote_catalog(url: &str, cache_dir: &Path) -> Result<bool> {
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(15))
.build()?;
let etag_path = cache_dir.join(ETAG_FILENAME);
let mut request = client.get(url).header(
"User-Agent",
"goose/model-catalog (https://github.com/aaif-goose/goose)",
);
if let Ok(etag) = std::fs::read_to_string(&etag_path) {
request = request.header(IF_NONE_MATCH, etag);
}
let response = request.send().await?.error_for_status()?;
if response.status() == reqwest::StatusCode::NOT_MODIFIED {
return Ok(false);
}
if response
.content_length()
.is_some_and(|length| length > MAX_CATALOG_BYTES as u64)
{
bail!("remote canonical model catalog exceeds size limit");
}
let etag = response
.headers()
.get(ETAG)
.and_then(|value| value.to_str().ok())
.map(str::to_owned);
let mut body = Vec::new();
let mut stream = response.bytes_stream();
while let Some(chunk) = stream.next().await {
let chunk = chunk?;
if body.len() + chunk.len() > MAX_CATALOG_BYTES {
bail!("remote canonical model catalog exceeds size limit");
}
body.extend_from_slice(&chunk);
}
let content = std::str::from_utf8(&body).context("canonical model catalog is not UTF-8")?;
let registry = super::models_dev::from_models_dev(content)?;
std::fs::create_dir_all(cache_dir)?;
atomic_write(cache_dir.join(CATALOG_FILENAME), &body)?;
if let Some(etag) = etag {
atomic_write(etag_path, etag.as_bytes())?;
}
activate(registry)?;
Ok(true)
}
fn atomic_write(destination: PathBuf, content: &[u8]) -> Result<()> {
let temporary = destination.with_extension(format!("tmp-{}", uuid::Uuid::new_v4()));
std::fs::write(&temporary, content)?;
if let Err(error) = std::fs::rename(&temporary, &destination) {
let _ = std::fs::remove_file(&temporary);
return Err(error.into());
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rejects_empty_duplicate_and_malformed_catalogs() {
assert!(CanonicalModelRegistry::from_json("[]").is_err());
let model = r#"{"id":"openai/test","name":"Test","tool_call":true}"#;
assert!(CanonicalModelRegistry::from_json(&format!("[{model},{model}]")).is_err());
assert!(CanonicalModelRegistry::from_json(
r#"[{"id":"invalid","name":"Test","tool_call":true}]"#
)
.is_err());
}
}
+1
View File
@@ -10,6 +10,7 @@ description.workspace = true
[features]
default = []
online-model-meta = []
telemetry = []
otel = [
"dep:tracing-opentelemetry",
+14 -259
View File
@@ -10,10 +10,7 @@
use anyhow::{Context, Result};
use clap::Parser;
use goose::providers::create_with_named_model;
use goose_providers::canonical::{
canonical_name, CanonicalModel, CanonicalModelRegistry, Limit, Modalities, Modality,
ModelMapping, Pricing, ThinkingMode,
};
use goose_providers::canonical::ModelMapping;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::{BTreeMap, BTreeSet, HashMap};
@@ -31,7 +28,6 @@ struct ProviderMetadata {
}
const MODELS_DEV_API_URL: &str = "https://models.dev/api.json";
const DEFAULT_CONTEXT_LIMIT: usize = 128_000;
const SEPARATOR: &str =
"================================================================================";
const SUBSEPARATOR: &str =
@@ -342,122 +338,6 @@ async fn fetch_models_dev() -> Result<Value> {
.context("Failed to parse models.dev response")
}
fn get_string(value: &Value, field: &str) -> Option<String> {
value.get(field).and_then(|v| v.as_str()).map(String::from)
}
fn get_thinking_mode(canonical_id: &str, value: &Value) -> Option<ThinkingMode> {
value
.get("thinking_mode")
.and_then(|v| v.as_str())
.and_then(|mode| serde_json::from_value(Value::String(mode.to_string())).ok())
.or_else(|| inferred_thinking_mode(canonical_id))
}
fn inferred_thinking_mode(canonical_id: &str) -> Option<ThinkingMode> {
match canonical_id {
"anthropic/claude-fable-5" => Some(ThinkingMode::AlwaysOnAdaptive),
"anthropic/claude-fable-5.1" => Some(ThinkingMode::AlwaysOnAdaptive),
"anthropic/claude-opus-5" => Some(ThinkingMode::Adaptive),
"anthropic/claude-opus-5.5" => Some(ThinkingMode::AlwaysOnAdaptive),
"anthropic/claude-opus-4.6" => Some(ThinkingMode::Adaptive),
"anthropic/claude-opus-4.7" => Some(ThinkingMode::Adaptive),
"anthropic/claude-opus-4.8" => Some(ThinkingMode::Adaptive),
"anthropic/claude-sonnet-4.6" => Some(ThinkingMode::Adaptive),
"anthropic/claude-sonnet-5" => Some(ThinkingMode::Adaptive),
_ => None,
}
}
fn parse_modalities(model_data: &Value, field: &str) -> Vec<Modality> {
model_data
.get("modalities")
.and_then(|m| m.get(field))
.and_then(|v| v.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str())
.filter_map(|s| {
serde_json::from_value(serde_json::Value::String(s.to_string())).ok()
})
.collect()
})
.unwrap_or_else(|| vec![Modality::Text])
}
fn process_model(
model_id: &str,
model_data: &Value,
normalized_provider: &str,
) -> Result<(String, CanonicalModel)> {
let name = model_data["name"]
.as_str()
.with_context(|| format!("Model {} missing name", model_id))?;
let canonical_id = canonical_name(normalized_provider, model_id);
let modalities = Modalities {
input: parse_modalities(model_data, "input"),
output: parse_modalities(model_data, "output"),
};
let cost = match model_data.get("cost") {
Some(c) if !c.is_null() => Pricing {
input: c.get("input").and_then(|v| v.as_f64()),
output: c.get("output").and_then(|v| v.as_f64()),
cache_read: c.get("cache_read").and_then(|v| v.as_f64()),
cache_write: c.get("cache_write").and_then(|v| v.as_f64()),
},
_ => Pricing {
input: None,
output: None,
cache_read: None,
cache_write: None,
},
};
let limit = Limit {
context: model_data
.get("limit")
.and_then(|l| l.get("context"))
.and_then(|v| v.as_u64())
.unwrap_or(DEFAULT_CONTEXT_LIMIT as u64) as usize,
output: model_data
.get("limit")
.and_then(|l| l.get("output"))
.and_then(|v| v.as_u64())
.map(|v| v as usize),
};
let canonical_model = CanonicalModel {
id: canonical_id.clone(),
name: name.to_string(),
family: get_string(model_data, "family"),
attachment: model_data.get("attachment").and_then(|v| v.as_bool()),
reasoning: model_data.get("reasoning").and_then(|v| v.as_bool()),
thinking_mode: get_thinking_mode(&canonical_id, model_data),
tool_call: model_data
.get("tool_call")
.and_then(|v| v.as_bool())
.unwrap_or(false),
temperature: model_data.get("temperature").and_then(|v| v.as_bool()),
knowledge: get_string(model_data, "knowledge"),
release_date: get_string(model_data, "release_date"),
last_updated: get_string(model_data, "last_updated"),
modalities,
open_weights: model_data.get("open_weights").and_then(|v| v.as_bool()),
cost,
limit,
};
let model_name = canonical_id
.strip_prefix(&format!("{}/", normalized_provider))
.unwrap_or(model_id)
.to_string();
Ok((model_name, canonical_model))
}
fn collect_provider_metadata(
providers_obj: &serde_json::Map<String, Value>,
) -> Vec<ProviderMetadata> {
@@ -525,99 +405,25 @@ fn collect_provider_metadata(
metadata_list
}
fn pick_winning_variant(variants: &[(String, CanonicalModel)]) -> usize {
variants
.iter()
.enumerate()
.min_by(|(_, (id_a, a)), (_, (id_b, b))| {
id_a.len()
.cmp(&id_b.len())
.then_with(|| b.last_updated.cmp(&a.last_updated))
.then_with(|| b.release_date.cmp(&a.release_date))
.then_with(|| id_a.cmp(id_b))
})
.map(|(idx, _)| idx)
.unwrap_or(0)
}
async fn build_canonical_models() -> Result<()> {
let json = fetch_models_dev().await?;
let registry = goose_providers::canonical::models_dev::from_models_dev(&json.to_string())?;
let output_path = data_file_path("canonical_models.json");
registry.to_file(&output_path)?;
println!(
"Wrote {} models to {}",
registry.count(),
output_path.display()
);
let providers_obj = json
.as_object()
.context("Expected object in models.dev response")?;
let mut registry = CanonicalModelRegistry::new();
let mut total_models = 0;
for (provider_key, provider_data) in providers_obj {
let models = match provider_data.get("models").and_then(|v| v.as_object()) {
Some(m) => m,
None => continue,
};
let normalized_provider = normalize_provider_name(provider_key);
println!(
"\nProcessing {} ({} models)...",
normalized_provider,
models.len()
);
let mut candidates: BTreeMap<String, Vec<(String, CanonicalModel)>> = BTreeMap::new();
for (model_id, model_data) in models {
let (model_name, canonical_model) =
process_model(model_id, model_data, normalized_provider)?;
candidates
.entry(model_name)
.or_default()
.push((model_id.clone(), canonical_model));
}
for (canonical_key, mut variants) in candidates {
let winner = if variants.len() == 1 {
variants.pop().unwrap().1
} else {
let chosen_idx = pick_winning_variant(&variants);
let chosen_id = variants[chosen_idx].0.clone();
println!(
" ⚠ {} variants collide on key '{}/{}': [{}] — keeping '{}'",
variants.len(),
normalized_provider,
canonical_key,
variants
.iter()
.map(|(id, _)| id.as_str())
.collect::<Vec<_>>()
.join(", "),
chosen_id,
);
variants.swap_remove(chosen_idx).1
};
registry.register(normalized_provider, &canonical_key, winner);
total_models += 1;
}
}
let output_path = data_file_path("canonical_models.json");
registry.to_file(&output_path)?;
println!(
"\n✓ Wrote {} models to {}",
total_models,
output_path.display()
);
println!("\n\nCollecting provider metadata from models.dev...");
let provider_metadata_list = collect_provider_metadata(providers_obj);
let provider_metadata_path = data_file_path("provider_metadata.json");
let provider_metadata_json = serde_json::to_string_pretty(&provider_metadata_list)?;
std::fs::write(&provider_metadata_path, provider_metadata_json)?;
println!(
"✓ Wrote {} providers metadata to {}",
provider_metadata_list.len(),
provider_metadata_path.display()
);
let metadata = collect_provider_metadata(providers_obj);
std::fs::write(
data_file_path("provider_metadata.json"),
serde_json::to_string_pretty(&metadata)?,
)?;
Ok(())
}
@@ -733,54 +539,3 @@ async fn main() -> Result<()> {
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn variant(id: &str, release: Option<&str>, updated: Option<&str>) -> (String, CanonicalModel) {
(
id.to_string(),
CanonicalModel {
id: format!("openai/{}", id),
name: id.to_string(),
family: None,
attachment: None,
reasoning: None,
thinking_mode: None,
tool_call: false,
temperature: None,
knowledge: None,
release_date: release.map(String::from),
last_updated: updated.map(String::from),
modalities: Modalities::default(),
open_weights: None,
cost: Pricing::default(),
limit: Limit::default(),
},
)
}
#[test]
fn shortest_variant_wins() {
let variants = vec![
variant("gpt-4o-2024-08-06", Some("2024-08-06"), Some("2024-08-06")),
variant("gpt-4o", Some("2024-05-13"), Some("2024-08-06")),
variant("gpt-4o-2024-11-20", Some("2024-11-20"), Some("2024-11-20")),
variant("gpt-4o-2024-05-13", Some("2024-05-13"), Some("2024-05-13")),
];
let idx = pick_winning_variant(&variants);
assert_eq!(variants[idx].0, "gpt-4o");
let variants = vec![
variant(
"claude-haiku-4-5-20251001",
Some("2025-10-16"),
Some("2025-10-16"),
),
variant("claude-haiku-4-5", Some("2025-10-16"), Some("2025-10-16")),
];
let idx = pick_winning_variant(&variants);
assert_eq!(variants[idx].0, "claude-haiku-4-5");
}
}
+2
View File
@@ -28,6 +28,8 @@ pub mod instance_id;
mod live_voice;
pub mod logging;
pub mod mcp_utils;
#[cfg(feature = "online-model-meta")]
pub mod model_catalog;
pub mod model_config;
pub mod oauth;
#[cfg(feature = "otel")]
+22
View File
@@ -0,0 +1,22 @@
use crate::config::paths::Paths;
use goose_providers::canonical::{load_cached_catalog, refresh_remote_catalog};
use std::path::PathBuf;
const CATALOG_URL: &str = "https://models.dev/api.json";
fn cache_dir() -> PathBuf {
Paths::in_data_dir("model_catalog")
}
pub fn initialize() {
let cache_dir = cache_dir();
if let Err(error) = load_cached_catalog(&cache_dir) {
tracing::warn!(%error, "ignoring invalid cached model catalog");
}
tokio::spawn(async move {
if let Err(error) = refresh_remote_catalog(CATALOG_URL, &cache_dir).await {
tracing::warn!(%error, "failed to refresh remote model catalog");
}
});
}