use cpa_common::gostr::GoStr;
use cpa_common::thinking::parse_suffix;
use cpa_core::config::Config;
use cpa_core::config::credentials;
use cpa_core::credential::{Credential, Source};
use cpa_core::exec::{ResolvedModel, ResolvedSource};
use cpa_core::registry::dynamic::{self, ConfigModel};
use cpa_core::registry::{ModelInfo, ThinkingSupport, pinned};
use serde_json::{Map, Value};
pub fn resolve_attempt(
cfg: &Config,
c: &Credential,
route_model: &str,
upstream_model: &str,
execution_model: Option<&str>,
) -> Option<ResolvedModel> {
match execution_model {
Some(_) if !c.provider.trim().go_eq_fold("codex") => None,
Some(model) => resolve(cfg, c, model, model),
None => resolve(cfg, c, route_model, upstream_model),
}
}
pub fn resolve(cfg: &Config, c: &Credential, route_model: &str, upstream_model: &str) -> Option<ResolvedModel> {
let api_key = configured(cfg, c, route_model, upstream_model).or_else(|| unlisted_codex(cfg, c, upstream_model));
if let Some(info) = api_key {
return Some(ResolvedModel {
info,
source: ResolvedSource::ApiKey,
});
}
codex_oauth(c, upstream_model).map(|info| ResolvedModel {
info,
source: ResolvedSource::CodexOAuth,
})
}
pub fn bind_home(c: &Credential, local: Option<&ResolvedModel>) -> Option<ResolvedModel> {
let wire: Map<String, Value> = serde_json::from_str(c.attributes.get(crate::remote::MODEL_INFO)?).ok()?;
let mut info = home_model_info(&wire)?;
let base = |id: &str| parse_suffix(id.trim()).model_name.trim().to_owned();
let flag = |info: &ModelInfo, key: &str| info.raw.get(key) == Some(&Value::Bool(true));
let support = match wire.get("support_configuration_update").and_then(Value::as_bool) {
Some(explicit) => explicit,
None => local.is_some_and(|local| {
base(&local.info.id).go_eq_fold(&base(&info.id)) && flag(&local.info, "support_configuration_update")
}),
};
set_flag(&mut info, "support_configuration_update", support);
if let Some(options) = c.attributes.get(credentials::HOME_MODEL_OPTIONS) {
let options: Map<String, Value> = serde_json::from_str(options).unwrap_or_default();
set_flag(
&mut info,
"is_compat",
options.get("is-compat") == Some(&Value::Bool(true)),
);
}
Some(ResolvedModel {
info,
source: ResolvedSource::Home,
})
}
fn home_model_info(wire: &Map<String, Value>) -> Option<ModelInfo> {
let text = |key: &str| {
wire.get(key)
.and_then(Value::as_str)
.unwrap_or_default()
.trim()
.to_owned()
};
let id = text("id");
if id.is_empty() {
return None;
}
let mut raw = Map::new();
raw.insert("id".into(), id.into());
let kind = text("type");
if !kind.is_empty() {
raw.insert("type".into(), kind.into());
}
for key in [
"inputTokenLimit",
"outputTokenLimit",
"context_length",
"max_completion_tokens",
] {
if let Some(n) = wire.get(key).and_then(Value::as_i64).filter(|n| *n != 0) {
raw.insert(key.into(), n.into());
}
}
for key in ["thinking", "native_capabilities"] {
if let Some(value) = wire.get(key).filter(|v| !v.is_null()) {
raw.insert(key.into(), go_zero_nulls(value));
}
}
if wire.get("user_defined") == Some(&Value::Bool(true)) {
raw.insert("user_defined".into(), true.into());
}
ModelInfo::from_raw(raw).ok()
}
fn go_zero_nulls(value: &Value) -> Value {
match value {
Value::Object(map) => Value::Object(
map.iter()
.filter(|(_, v)| !v.is_null())
.map(|(k, v)| (k.clone(), go_zero_nulls(v)))
.collect(),
),
Value::Array(items) => Value::Array(
items
.iter()
.map(|v| match v {
Value::Null => Value::String(String::new()),
v => go_zero_nulls(v),
})
.collect(),
),
other => other.clone(),
}
}
fn attr<'a>(c: &'a Credential, key: &str) -> &'a str {
c.attributes.get(key).map_or("", |v| v.trim())
}
fn configured_routing(c: &Credential) -> bool {
dynamic::is_api_key(c) || (matches!(c.source, Source::Config { .. }) && !attr(c, "compat_name").is_empty())
}
struct Configured {
name: String,
alias: String,
thinking: Option<ThinkingSupport>,
is_compat: bool,
support_configuration_update: Option<bool>,
}
fn entry_models(cfg: &Config, c: &Credential) -> Option<(Vec<Configured>, &'static str)> {
let provider = c.provider.trim().go_lower();
let (family, kind) = match provider.as_str() {
"gemini" => ("gemini", "gemini"),
"gemini-interactions" => ("interactions", "interactions"),
"claude" => ("claude", "claude"),
"codex" => ("codex", "codex"),
"xai" => ("xai", "xai"),
"vertex" => ("vertex", "gemini"),
"meta" => ("meta", "meta"),
_ => return Some((compat_models(cfg, c)?, "openai-compatibility")),
};
let entry = credentials::resolve_api_key_entry(cfg, family, c)?;
let models = entry
.models
.iter()
.filter_map(|m| serde_json::from_value::<ConfigModel>(m.clone()).ok())
.map(|m| Configured {
name: m.name,
alias: m.alias,
thinking: m.thinking,
is_compat: m.is_compat && family != "vertex",
support_configuration_update: (family == "codex").then_some(m.support_configuration_update),
})
.collect();
Some((models, kind))
}
fn compat_models(cfg: &Config, c: &Credential) -> Option<Vec<Configured>> {
let text = |v: Option<&serde_yaml_ng::Value>| match v {
Some(serde_yaml_ng::Value::String(s)) => s.trim().to_owned(),
Some(serde_yaml_ng::Value::Number(n)) => n.to_string(),
Some(serde_yaml_ng::Value::Bool(b)) => b.to_string(),
_ => String::new(),
};
let entries: Vec<&serde_yaml_ng::Value> = cfg
.document
.get("api-keys")
.and_then(|k| k.get("openai-compatibility"))
.and_then(serde_yaml_ng::Value::as_sequence)
.into_iter()
.flatten()
.filter(|g| !text(g.get("base-url")).is_empty())
.collect();
let disabled = |g: &serde_yaml_ng::Value| g.get("disabled").and_then(serde_yaml_ng::Value::as_bool) == Some(true);
let by_index = matches!(c.source, Source::Config { .. })
.then(|| attr(c, "config_index").parse::<usize>().ok())
.flatten()
.and_then(|i| entries.get(i).copied())
.filter(|g| !disabled(g));
let entry = by_index.or_else(|| {
let candidates = [attr(c, "compat_name"), attr(c, "provider_key"), c.provider.trim()];
entries.iter().copied().find(|g| {
let name = text(g.get("name"));
!disabled(g) && candidates.iter().any(|cand| !cand.is_empty() && cand.go_eq_fold(&name))
})
})?;
let models = entry
.get("models")
.and_then(serde_yaml_ng::Value::as_sequence)
.into_iter()
.flatten()
.filter_map(|m| serde_yaml_ng::from_value::<ConfigModel>(m.clone()).ok())
.map(|m| Configured {
thinking: m.thinking.or_else(|| {
(!m.image).then(|| ThinkingSupport {
levels: vec!["low".into(), "medium".into(), "high".into()],
..ThinkingSupport::default()
})
}),
name: m.name,
alias: m.alias,
is_compat: m.is_compat,
support_configuration_update: None,
})
.collect();
Some(models)
}
fn candidates(model: &str) -> Vec<String> {
let model = model.trim();
if model.is_empty() {
return Vec::new();
}
let base = parse_suffix(model).model_name;
let base = if base.is_empty() { model.to_owned() } else { base };
if base == model {
vec![model.to_owned()]
} else {
vec![model.to_owned(), base]
}
}
fn fallback_matches(configured: &str, selected: &str) -> bool {
let configured = parse_suffix(configured.trim());
!configured.has_suffix
&& configured
.model_name
.trim()
.go_eq_fold(parse_suffix(selected.trim()).model_name.trim())
}
fn configured(cfg: &Config, c: &Credential, route_model: &str, upstream_model: &str) -> Option<ModelInfo> {
if !configured_routing(c) {
return None;
}
let (models, kind) = entry_models(cfg, c)?;
let mut routes: Vec<(String, String, usize)> = Vec::new();
for (index, m) in models.iter().enumerate() {
let (mut name, mut alias) = (m.name.trim(), m.alias.trim());
if name.is_empty() {
name = alias;
}
if alias.is_empty() {
alias = name;
}
if name.is_empty() {
continue;
}
let mut seen: Vec<String> = Vec::new();
for candidate in [alias, name].into_iter().flat_map(candidates) {
let key = candidate.trim().go_lower();
if key.is_empty() || seen.contains(&key) {
continue;
}
seen.push(key.clone());
if !routes.iter().any(|(k, up, _)| *k == key && up.go_eq_fold(name)) {
routes.push((key, name.to_owned(), index));
}
}
}
let requested = dynamic::strip_prefix(route_model.trim(), c);
let matched: Vec<(&str, usize)> = candidates(requested)
.iter()
.flat_map(|candidate| {
let key = candidate.trim().go_lower();
routes
.iter()
.filter(move |(k, ..)| *k == key)
.map(|(_, up, i)| (up.as_str(), *i))
})
.collect();
let selected = upstream_model.trim();
let (name, index) = matched
.iter()
.find(|(up, _)| up.trim().go_eq_fold(selected))
.or_else(|| matched.iter().find(|(up, _)| fallback_matches(up, selected)))?;
let m = &models[*index];
let mut info = resolve_model_info(name, kind, m.thinking.clone());
set_flag(&mut info, "is_compat", m.is_compat);
if let Some(support) = m.support_configuration_update {
set_flag(&mut info, "support_configuration_update", support);
}
Some(info)
}
fn unlisted_codex(cfg: &Config, c: &Credential, upstream_model: &str) -> Option<ModelInfo> {
let upstream = upstream_model.trim();
if !dynamic::is_api_key(c) || !c.provider.trim().go_eq_fold("codex") || upstream.is_empty() {
return None;
}
let entry = credentials::resolve_api_key_entry(cfg, "codex", c)?;
let (key, base) = (attr(c, "api_key"), attr(c, "base_url"));
let entry_base = entry.base_url.trim();
if (key.is_empty() && base.is_empty())
|| !key.go_eq_fold(entry.api_key.trim())
|| (!entry_base.is_empty() && !base.go_eq_fold(entry_base))
{
return None;
}
let configured = entry
.models
.iter()
.filter_map(|m| serde_json::from_value::<ConfigModel>(m.clone()).ok())
.find(|m| m.name.trim().go_eq_fold(upstream) || fallback_matches(&m.name, upstream));
let mut info = resolve_model_info(
upstream_model,
"codex",
configured.as_ref().and_then(|m| m.thinking.clone()),
);
let (support, compat) = configured.map_or((false, false), |m| (m.support_configuration_update, m.is_compat));
set_flag(&mut info, "support_configuration_update", support);
set_flag(&mut info, "is_compat", compat);
Some(info)
}
fn codex_oauth(c: &Credential, upstream_model: &str) -> Option<ModelInfo> {
if dynamic::auth_kind(c) != Some("oauth") || !c.provider.trim().go_eq_fold("codex") {
return None;
}
let channel = match attr(c, "plan_type").go_lower().as_str() {
"plus" => "codex-plus",
"team" | "business" | "go" => "codex-team",
"free" => "codex-free",
_ => "codex-pro",
};
let selected = parse_suffix(upstream_model.trim()).model_name;
let selected = selected.trim();
pinned()
.channel(channel)
.iter()
.find(|m| m.id.go_eq_fold(selected))
.cloned()
}
fn resolve_model_info(name: &str, kind: &str, thinking: Option<ThinkingSupport>) -> ModelInfo {
let name = name.trim();
let base = parse_suffix(name).model_name;
let mut info = pinned().lookup(base.trim()).cloned().unwrap_or_else(|| ModelInfo {
id: String::new(),
kind: String::new(),
thinking: None,
raw: Map::new(),
});
info.id = name.to_owned();
info.raw.insert("id".into(), name.into());
info.kind = kind.trim().to_owned();
info.raw.insert("type".into(), info.kind.clone().into());
if let Some(support) = thinking {
let mut support = dynamic::normalize_thinking(support);
if support.disable_type.is_empty()
&& let Some(catalog) = &info.thinking
{
support.disable_type.clone_from(&catalog.disable_type);
}
info.raw.insert("thinking".into(), dynamic::thinking_value(&support));
info.thinking = Some(support);
}
info.raw.remove("user_defined");
info
}
fn set_flag(info: &mut ModelInfo, key: &str, value: bool) {
if value {
info.raw.insert(key.into(), Value::Bool(true));
} else {
info.raw.remove(key);
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::Path;
#[test]
fn resolved_models_match_go() {
let fixture: Value = serde_json::from_str(include_str!("../tests/fixtures/server_go.json")).unwrap();
let cfg = Config::parse(fixture["resolved_config"].as_str().unwrap()).unwrap();
let synthesized = credentials::from_config(&cfg);
let cases = fixture["resolved"].as_array().unwrap();
assert_eq!(cases.len(), 32);
let (mut bound, mut codex_oauth) = (0, 0);
for case in cases {
let name = case["name"].as_str().unwrap();
let id = case["auth_id"].as_str().unwrap();
let attributes: std::collections::BTreeMap<String, String> = case["attributes"]
.as_object()
.map(|a| {
a.iter()
.map(|(k, v)| (k.clone(), v.as_str().unwrap().to_owned()))
.collect()
})
.unwrap_or_default();
let c = if case["synthetic"].as_bool().unwrap() {
let mut c = synthesized
.iter()
.find(|c| c.id == id)
.unwrap_or_else(|| panic!("{name}: no synthesized credential {id}"))
.clone();
c.attributes
.insert("config_index".into(), attributes["config_index"].clone());
c
} else {
let meta = case["metadata"].as_object().cloned().unwrap_or_default();
let path = format!("/auth/{id}");
let mut c = Credential::from_file(Path::new("/auth"), Path::new(&path), meta).unwrap();
c.attributes = attributes;
c
};
let restore = case["restore"].as_bool().unwrap();
let got = resolve_attempt(
&cfg,
&c,
case["route"].as_str().unwrap(),
case["upstream"].as_str().unwrap(),
restore.then(|| case["req_model"].as_str().unwrap()),
);
let source = match got.as_ref().map(|r| r.source) {
None => "",
Some(ResolvedSource::ApiKey) => "api_key",
Some(ResolvedSource::CodexOAuth) => "codex_oauth",
Some(ResolvedSource::Home) => "home",
};
assert_eq!(source, case["source"].as_str().unwrap(), "{name}: source");
let Some(got) = got else { continue };
let mut want = case["info"].clone();
if want["id"] == "claude-opus-4-7" {
want["thinking"] = serde_json::json!({
"zero_allowed": true,
"dynamic_allowed": true,
"levels": ["low", "medium", "high", "xhigh", "max"]
});
}
bound += 1;
codex_oauth += usize::from(got.source == ResolvedSource::CodexOAuth);
let visible = |m: &Map<String, Value>| -> Map<String, Value> {
m.iter()
.filter(|(k, v)| {
!matches!(
k.as_str(),
"support_configuration_update" | "is_compat" | "user_defined" | "native_capabilities"
) && !matches!(v, Value::Null)
&& *v != &Value::from("")
&& *v != &Value::from(0)
})
.map(|(k, v)| (k.clone(), v.clone()))
.collect()
};
assert_eq!(
Value::Object(visible(&got.info.raw)),
Value::Object(visible(want.as_object().unwrap())),
"{name}: info"
);
assert_eq!(got.info.id, want["id"].as_str().unwrap(), "{name}: id");
assert_eq!(got.info.kind, want["type"].as_str().unwrap(), "{name}: type");
assert_eq!(
got.is_compat(),
case["is_compat"].as_bool().unwrap(),
"{name}: is_compat"
);
let caps = cpa_common::thinking::ModelCaps::from(&got.info);
assert_eq!(
caps.support_configuration_update,
case["support_configuration_update"].as_bool().unwrap(),
"{name}: support_configuration_update"
);
assert_eq!(
caps.user_defined,
case["user_defined"].as_bool().unwrap(),
"{name}: user_defined"
);
assert_eq!(
got.info.thinking.as_ref().map(dynamic::thinking_value),
want.get("thinking").cloned(),
"{name}: typed thinking"
);
}
assert_eq!((bound, codex_oauth), (25, 4));
}
#[test]
fn configured_thinking_keeps_the_catalog_disable_type() {
let support = |yaml: &str| serde_yaml_ng::from_str::<ThinkingSupport>(yaml).unwrap();
let configured = support("levels: [low, medium, high, none]");
let info = resolve_model_info("claude-sonnet-5-5", "claude", Some(configured.clone()));
let thinking = info.thinking.as_ref().unwrap();
assert_eq!(thinking.disable_type, "between_tools");
assert_eq!(thinking.levels, ["low", "medium", "high", "none"]);
let body = br#"{"model":"claude-sonnet-5-5","messages":[{"role":"user","content":"hi"}],"thinking":{"type":"disabled"}}"#;
let caps = cpa_common::thinking::ModelCaps::from(&info);
let out = cpa_common::thinking::apply_thinking_with_model_info(
body,
&[],
"claude-sonnet-5-5",
"claude",
"claude",
"claude",
Some(&caps),
)
.unwrap();
let out: Value = serde_json::from_slice(&out).unwrap();
assert_eq!(out["thinking"], serde_json::json!({"type": "between_tools"}), "{out}");
let opus = resolve_model_info("claude-opus-5-5", "claude", Some(support("levels: [low, none]")));
let body = br#"{"model":"claude-opus-5-5","messages":[{"role":"user","content":"hi"}],"thinking":{"type":"disabled"}}"#;
let caps = cpa_common::thinking::ModelCaps::from(&opus);
let out = cpa_common::thinking::apply_thinking_with_model_info(
body,
&[],
"claude-opus-5-5",
"claude",
"claude",
"claude",
Some(&caps),
)
.unwrap();
let out: Value = serde_json::from_slice(&out).unwrap();
assert_eq!(out["thinking"], serde_json::json!({"type": "disabled"}), "{out}");
let explicit = support("levels: [low]\ndisable-type: disabled");
let explicit = resolve_model_info("claude-sonnet-5-5", "claude", Some(explicit));
assert_eq!(explicit.thinking.unwrap().disable_type, "disabled");
let other = resolve_model_info("claude-opus-5", "claude", Some(configured.clone()));
assert_eq!(other.thinking.unwrap().disable_type, "");
let variant = resolve_model_info("claude-sonnet-5-5[1m]", "claude", Some(configured));
assert_eq!(variant.thinking.unwrap().disable_type, "between_tools");
}
}