From aa4e318b21b5a005dcf883b81487ccf9f5bb5e5a Mon Sep 17 00:00:00 2001 From: fylorn <249551762+fylorn@users.noreply.github.com> Date: Thu, 12 Nov 2026 01:06:48 +0800 Subject: [PATCH 1/3] fix(codex): retain reasoning choices in discovered catalogs Ordinary upstream model lists supplied names without effort metadata, so Codex could select DeepSeek and aliases but could not select reasoning. Whole-definition comparisons also discarded shared efforts across aliases. Use documented effort-only profiles when native metadata is unavailable, resolve them from actual targets, and intersect choices independently of other native fields. Honor manual disabling and protocol conversion limits. --- crates/tw-gateway/data/codex_reasoning.json | 468 ++++++++++++++++++ crates/tw-gateway/data/codex_reasoning.md | 26 + crates/tw-gateway/src/codex_catalog.rs | 38 +- .../tw-gateway/src/codex_catalog/reasoning.rs | 101 ++++ crates/tw-gateway/tests/codex_catalog.rs | 273 +++++++++- docs/codex-model-catalog.md | 24 +- docs/codex-model-catalog.zh-CN.md | 16 +- 7 files changed, 934 insertions(+), 12 deletions(-) create mode 100644 crates/tw-gateway/data/codex_reasoning.json create mode 100644 crates/tw-gateway/data/codex_reasoning.md create mode 100644 crates/tw-gateway/src/codex_catalog/reasoning.rs diff --git a/crates/tw-gateway/data/codex_reasoning.json b/crates/tw-gateway/data/codex_reasoning.json new file mode 100644 index 00000000..d86144ed --- /dev/null +++ b/crates/tw-gateway/data/codex_reasoning.json @@ -0,0 +1,468 @@ +{ + "gpt-6-astra": { + "minimal_client_version": "0.153.0", + "default_reasoning_level": "low", + "supported_reasoning_levels": [ + { + "effort": "low", + "description": "Fast responses with lighter reasoning" + }, + { + "effort": "medium", + "description": "Balances speed and reasoning depth for everyday tasks" + }, + { + "effort": "high", + "description": "Greater reasoning depth for complex problems" + }, + { + "effort": "xhigh", + "description": "Extra high reasoning depth for complex problems" + }, + { + "effort": "max", + "description": "Maximum reasoning depth for the hardest problems" + }, + { + "effort": "ultra", + "description": "Maximum reasoning with automatic task delegation" + } + ] + }, + "gpt-6.1-sol": { + "minimal_client_version": "0.153.0", + "default_reasoning_level": "low", + "supported_reasoning_levels": [ + { + "effort": "low", + "description": "Fast responses with lighter reasoning" + }, + { + "effort": "medium", + "description": "Balances speed and reasoning depth for everyday tasks" + }, + { + "effort": "high", + "description": "Greater reasoning depth for complex problems" + }, + { + "effort": "xhigh", + "description": "Extra high reasoning depth for complex problems" + }, + { + "effort": "max", + "description": "Maximum reasoning depth for the hardest problems" + }, + { + "effort": "ultra", + "description": "Maximum reasoning with automatic task delegation" + } + ] + }, + "gpt-6.1-terra": { + "minimal_client_version": "0.153.0", + "default_reasoning_level": "low", + "supported_reasoning_levels": [ + { + "description": "Fast responses with lighter reasoning", + "effort": "low" + }, + { + "description": "Balances speed and reasoning depth for everyday tasks", + "effort": "medium" + }, + { + "description": "Greater reasoning depth for complex problems", + "effort": "high" + }, + { + "description": "Extra high reasoning depth for complex problems", + "effort": "xhigh" + }, + { + "description": "Maximum reasoning depth for the hardest problems", + "effort": "max" + }, + { + "description": "Maximum reasoning with automatic task delegation", + "effort": "ultra" + } + ] + }, + "gpt-6-sol": { + "minimal_client_version": "0.155.0", + "default_reasoning_level": "medium", + "supported_reasoning_levels": [ + { + "effort": "low", + "description": "Fast responses with lighter reasoning" + }, + { + "effort": "medium", + "description": "Balances speed and reasoning depth for everyday tasks" + }, + { + "effort": "high", + "description": "Greater reasoning depth for complex problems" + }, + { + "effort": "xhigh", + "description": "Extra high reasoning depth for complex problems" + }, + { + "effort": "max", + "description": "Maximum reasoning depth for the hardest problems" + }, + { + "effort": "ultra", + "description": "Maximum reasoning with automatic task delegation" + } + ] + }, + "gpt-6-luna": { + "minimal_client_version": "0.155.0", + "default_reasoning_level": "medium", + "supported_reasoning_levels": [ + { + "effort": "low", + "description": "Fast responses with lighter reasoning" + }, + { + "effort": "medium", + "description": "Balances speed and reasoning depth for everyday tasks" + }, + { + "effort": "high", + "description": "Greater reasoning depth for complex problems" + }, + { + "effort": "xhigh", + "description": "Extra high reasoning depth for complex problems" + }, + { + "effort": "max", + "description": "Maximum reasoning depth for the hardest problems" + } + ] + }, + "gpt-5.6-sol": { + "minimal_client_version": "0.144.0", + "default_reasoning_level": "low", + "supported_reasoning_levels": [ + { + "effort": "low", + "description": "Fast responses with lighter reasoning" + }, + { + "effort": "medium", + "description": "Balances speed and reasoning depth for everyday tasks" + }, + { + "effort": "high", + "description": "Greater reasoning depth for complex problems" + }, + { + "effort": "xhigh", + "description": "Extra high reasoning depth for complex problems" + }, + { + "effort": "max", + "description": "Maximum reasoning depth for the hardest problems" + }, + { + "effort": "ultra", + "description": "Maximum reasoning with automatic task delegation" + } + ] + }, + "gpt-5.6-terra": { + "minimal_client_version": "0.144.0", + "default_reasoning_level": "medium", + "supported_reasoning_levels": [ + { + "effort": "low", + "description": "Fast responses with lighter reasoning" + }, + { + "effort": "medium", + "description": "Balances speed and reasoning depth for everyday tasks" + }, + { + "effort": "high", + "description": "Greater reasoning depth for complex problems" + }, + { + "effort": "xhigh", + "description": "Extra high reasoning depth for complex problems" + }, + { + "effort": "max", + "description": "Maximum reasoning depth for the hardest problems" + }, + { + "effort": "ultra", + "description": "Maximum reasoning with automatic task delegation" + } + ] + }, + "gpt-5.6-luna": { + "minimal_client_version": "0.144.0", + "default_reasoning_level": "medium", + "supported_reasoning_levels": [ + { + "effort": "low", + "description": "Fast responses with lighter reasoning" + }, + { + "effort": "medium", + "description": "Balances speed and reasoning depth for everyday tasks" + }, + { + "effort": "high", + "description": "Greater reasoning depth for complex problems" + }, + { + "effort": "xhigh", + "description": "Extra high reasoning depth for complex problems" + }, + { + "effort": "max", + "description": "Maximum reasoning depth for the hardest problems" + } + ] + }, + "gpt-daybreak-blue-latest": { + "minimal_client_version": "0.142.2", + "default_reasoning_level": "low", + "supported_reasoning_levels": [ + { + "effort": "low", + "description": "Fast responses with lighter reasoning" + }, + { + "effort": "medium", + "description": "Balances speed and reasoning depth for everyday tasks" + }, + { + "effort": "high", + "description": "Greater reasoning depth for complex problems" + }, + { + "effort": "xhigh", + "description": "Extra high reasoning depth for complex problems" + }, + { + "effort": "max", + "description": "Maximum reasoning depth for the hardest problems" + }, + { + "effort": "ultra", + "description": "Maximum reasoning with automatic task delegation" + } + ] + }, + "gpt-daybreak-red-latest": { + "minimal_client_version": "0.142.2", + "default_reasoning_level": "medium", + "supported_reasoning_levels": [ + { + "effort": "low", + "description": "Fast responses with lighter reasoning" + }, + { + "effort": "medium", + "description": "Balances speed and reasoning depth for everyday tasks" + }, + { + "effort": "high", + "description": "Greater reasoning depth for complex problems" + }, + { + "effort": "xhigh", + "description": "Extra high reasoning depth for complex problems" + }, + { + "effort": "max", + "description": "Maximum reasoning depth for the hardest problems" + }, + { + "effort": "ultra", + "description": "Maximum reasoning with automatic task delegation" + } + ] + }, + "gpt-5.5": { + "minimal_client_version": "0.124.0", + "default_reasoning_level": "medium", + "supported_reasoning_levels": [ + { + "effort": "low", + "description": "Fast responses with lighter reasoning" + }, + { + "effort": "medium", + "description": "Balances speed and reasoning depth for everyday tasks" + }, + { + "effort": "high", + "description": "Greater reasoning depth for complex problems" + }, + { + "effort": "xhigh", + "description": "Extra high reasoning depth for complex problems" + } + ] + }, + "codex-auto-review": { + "minimal_client_version": "0.98.0", + "default_reasoning_level": "medium", + "supported_reasoning_levels": [ + { + "effort": "low", + "description": "Fast responses with lighter reasoning" + }, + { + "effort": "medium", + "description": "Balances speed and reasoning depth for everyday tasks" + }, + { + "effort": "high", + "description": "Greater reasoning depth for complex problems" + }, + { + "effort": "xhigh", + "description": "Extra high reasoning depth for complex problems" + }, + { + "effort": "max", + "description": "Maximum reasoning depth for the hardest problems" + } + ] + }, + "deepseek-flash": { + "minimal_client_version": "0.166.0", + "default_reasoning_level": "high", + "supported_reasoning_levels": [ + { + "effort": "low", + "description": "Faster responses with lighter reasoning" + }, + { + "effort": "high", + "description": "Deeper reasoning for complex tasks" + }, + { + "effort": "max", + "description": "Maximum reasoning depth" + } + ] + }, + "deepseek-v4-flash": { + "minimal_client_version": "0.166.0", + "default_reasoning_level": "high", + "supported_reasoning_levels": [ + { + "effort": "low", + "description": "Faster responses with lighter reasoning" + }, + { + "effort": "high", + "description": "Deeper reasoning for complex tasks" + }, + { + "effort": "max", + "description": "Maximum reasoning depth" + } + ] + }, + "deepseek-v4-flash-vision-exp": { + "minimal_client_version": "0.166.0", + "default_reasoning_level": "high", + "supported_reasoning_levels": [ + { + "effort": "low", + "description": "Faster responses with lighter reasoning" + }, + { + "effort": "high", + "description": "Deeper reasoning for complex tasks" + }, + { + "effort": "max", + "description": "Maximum reasoning depth" + } + ] + }, + "deepseek-v4.1-flash": { + "minimal_client_version": "0.166.0", + "default_reasoning_level": "high", + "supported_reasoning_levels": [ + { + "effort": "low", + "description": "Faster responses with lighter reasoning" + }, + { + "effort": "high", + "description": "Deeper reasoning for complex tasks" + }, + { + "effort": "max", + "description": "Maximum reasoning depth" + } + ] + }, + "deepseek-v4.1-flash-1017": { + "minimal_client_version": "0.166.0", + "default_reasoning_level": "high", + "supported_reasoning_levels": [ + { + "effort": "low", + "description": "Faster responses with lighter reasoning" + }, + { + "effort": "high", + "description": "Deeper reasoning for complex tasks" + }, + { + "effort": "max", + "description": "Maximum reasoning depth" + } + ] + }, + "deepseek-v4-pro": { + "minimal_client_version": "0.166.0", + "default_reasoning_level": "high", + "supported_reasoning_levels": [ + { + "effort": "low", + "description": "Faster responses with lighter reasoning" + }, + { + "effort": "high", + "description": "Deeper reasoning for complex tasks" + }, + { + "effort": "max", + "description": "Maximum reasoning depth" + } + ] + }, + "deepseek-v4-pro-0813": { + "minimal_client_version": "0.166.0", + "default_reasoning_level": "high", + "supported_reasoning_levels": [ + { + "effort": "low", + "description": "Faster responses with lighter reasoning" + }, + { + "effort": "high", + "description": "Deeper reasoning for complex tasks" + }, + { + "effort": "max", + "description": "Maximum reasoning depth" + } + ] + } +} diff --git a/crates/tw-gateway/data/codex_reasoning.md b/crates/tw-gateway/data/codex_reasoning.md new file mode 100644 index 00000000..1dbfe88d --- /dev/null +++ b/crates/tw-gateway/data/codex_reasoning.md @@ -0,0 +1,26 @@ +# Codex reasoning profiles + +`codex_reasoning.json` supplies effort choices when an upstream lists ordinary +model IDs without native Codex metadata. It deliberately contains only model +identity, the minimum Codex version, the default effort and supported levels. +It does not copy OpenAI prompts, tools or other capabilities onto a provider. +An upstream native definition takes precedence, including an empty effort list; +an explicit `model_specs.reasoning: false` still disables reasoning choices. + +OpenAI entries are extracted from `codex-rs/models-manager/models.json` at +[`e58bf8f5233b51e7cd1679d947e3028c1ed91b06`](https://github.com/openai/codex/blob/e58bf8f5233b51e7cd1679d947e3028c1ed91b06/codex-rs/models-manager/models.json). +Preserve each entry's minimum client version, default and complete effort list +when refreshing this snapshot. The source is MIT licensed. + +DeepSeek entries follow the official [thinking-mode guide](https://api-docs.deepseek.com/guides/thinking_mode) +and [model details](https://api-docs.deepseek.com/quick_start/pricing), checked +on 2026-11-12: `low`, `high`, `max`, with `high` as the default. The Flash ID, +its documented legacy names and the V4.1-Flash-1017 model version share these +choices, as do V4-Pro and its documented V4-Pro-0813 version. The generic Codex +profile is verified with CLI 0.166.0, so these profiles require that version. + +Profiles are selected by the actual upstream model ID, ignoring a provider +namespace and ASCII case. Alias names never determine capabilities. Unknown +models receive no invented effort choices, even if a price table or manual +specification says they can reason. Add another model only with an authoritative +source for its actual levels and default. diff --git a/crates/tw-gateway/src/codex_catalog.rs b/crates/tw-gateway/src/codex_catalog.rs index a24f2ab0..da24292f 100644 --- a/crates/tw-gateway/src/codex_catalog.rs +++ b/crates/tw-gateway/src/codex_catalog.rs @@ -20,6 +20,8 @@ use tw_types::msg; use crate::error::GatewayError; use crate::state::{AppState, Runtime}; +mod reasoning; + const MAX_BYTES: usize = 1024 * 1024; const MAX_CACHED: usize = 32; const FETCH_TIMEOUT: Duration = Duration::from_secs(8); @@ -350,7 +352,15 @@ pub(crate) async fn list( let book = state.pricing.load(); let models: Vec<_> = names(&rt.config, &catalog, allow.as_deref()) .iter() - .map(|name| project(name, &candidates(&rt, &catalog, name), &book, &upstreams)) + .map(|name| { + project( + name, + &candidates(&rt, &catalog, name), + &book, + &upstreams, + &version, + ) + }) .collect(); let body = serde_json::to_vec(&json!({"models":models})).expect("catalog is JSON"); if body.len() > MAX_BYTES { @@ -411,6 +421,7 @@ fn project( candidates: &[(&Provider, &str)], book: &tw_pricing::PriceBook, upstreams: &HashMap, + version: &str, ) -> Value { let definitions: Vec<_> = candidates .iter() @@ -440,14 +451,22 @@ fn project( .or_else(|| d["context_window"].as_u64()) }), )); - reasoning.push(pick( + // Only effort choices use the bundled profiles. Instructions, tools + // and other native fields still come exclusively from the upstream. + let profile = definition.or_else(|| reasoning::profile(model, version)); + let enabled = pick( spec.reasoning, - definition.map(|d| { + profile.map(|d| { d["supported_reasoning_levels"] .as_array() .is_some_and(|v| !v.is_empty()) }), - )); + ); + reasoning.push(if enabled == Some(false) { + None + } else { + profile + }); images.push(pick( spec.image_input, definition @@ -472,10 +491,13 @@ fn project( object.remove("context_window"); object.remove("max_context_window"); } - if reasoning.contains(&Some(false)) { - out["supported_reasoning_levels"] = json!([]); - out["default_reasoning_level"] = Value::Null; - } + let requires_translation = candidates.iter().any(|(provider, _)| { + !matches!( + provider.effective_protocol(), + Some(tw_config::Protocol::OpenaiResponses | tw_config::Protocol::Chatgpt) + ) + }); + reasoning::apply(&mut out, &reasoning, requires_translation); if !images.is_empty() && images.iter().all(|v| *v == Some(true)) { out["input_modalities"] = json!(["text", "image"]); } else { diff --git a/crates/tw-gateway/src/codex_catalog/reasoning.rs b/crates/tw-gateway/src/codex_catalog/reasoning.rs new file mode 100644 index 00000000..1736b307 --- /dev/null +++ b/crates/tw-gateway/src/codex_catalog/reasoning.rs @@ -0,0 +1,101 @@ +//! Reasoning choices require actual levels, not just a `reasoning: true` flag. + +use std::collections::BTreeMap; +use std::sync::OnceLock; + +use serde_json::{Value, json}; + +pub(super) fn profile(model: &str, version: &str) -> Option<&'static Value> { + static PROFILES: OnceLock> = OnceLock::new(); + let profiles = PROFILES.get_or_init(|| { + serde_json::from_str(include_str!("../../data/codex_reasoning.json")) + .expect("bundled reasoning profiles are valid JSON") + }); + // Providers commonly qualify an unchanged model ID with their namespace. + // Resolve this against the routing candidate, never the public alias name. + let name = model.rsplit('/').next()?.to_ascii_lowercase(); + let profile = profiles.get(&name)?; + let minimum = profile["minimal_client_version"].as_str()?; + (parse_version(version)? >= parse_version(minimum)?).then_some(profile) +} + +fn parse_version(version: &str) -> Option<(u64, u64, u64)> { + let mut parts = version.split('.'); + let parsed = ( + parts.next()?.parse().ok()?, + parts.next()?.parse().ok()?, + parts.next()?.parse().ok()?, + ); + parts.next().is_none().then_some(parsed) +} + +/// Definitions may disagree on prompts or tools while sharing effort choices. +/// Keep only levels that every routing candidate explicitly supports. +pub(super) fn apply(out: &mut Value, profiles: &[Option<&Value>], requires_translation: bool) { + let first = profiles.first().copied().flatten(); + let mut levels = first + .and_then(|p| p["supported_reasoning_levels"].as_array()) + .cloned() + .unwrap_or_default(); + levels.retain(|level| { + level["effort"].as_str().is_some_and(|effort| { + (!requires_translation || tw_dialect::think::parse_openai(effort).is_some()) + && profiles.iter().all(|profile| { + profile + .and_then(|p| p["supported_reasoning_levels"].as_array()) + .is_some_and(|levels| levels.iter().any(|v| v["effort"] == effort)) + }) + }) + }); + let default = first + .and_then(|p| p["default_reasoning_level"].as_str()) + .filter(|effort| levels.iter().any(|v| v["effort"] == *effort)) + .or_else(|| { + levels + .iter() + .find(|v| v["effort"] == "medium") + .or_else(|| levels.first()) + .and_then(|v| v["effort"].as_str()) + }); + out["default_reasoning_level"] = json!(default); + out["supported_reasoning_levels"] = json!(levels); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn every_bundled_profile_has_valid_unique_choices_and_an_available_default() { + let profiles: BTreeMap = + serde_json::from_str(include_str!("../../data/codex_reasoning.json")).unwrap(); + for (model, profile) in profiles { + assert_eq!(model, model.to_ascii_lowercase()); + assert_eq!(profile.as_object().unwrap().len(), 3, "{model}"); + assert!(parse_version(profile["minimal_client_version"].as_str().unwrap()).is_some()); + let levels = profile["supported_reasoning_levels"].as_array().unwrap(); + let mut efforts = std::collections::BTreeSet::new(); + for level in levels { + let effort = level["effort"].as_str().unwrap(); + assert!(matches!( + effort, + "none" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max" | "ultra" + )); + assert!(efforts.insert(effort), "duplicate effort in {model}"); + assert!(!level["description"].as_str().unwrap().trim().is_empty()); + } + assert!(efforts.contains(profile["default_reasoning_level"].as_str().unwrap())); + } + } + + #[test] + fn bundled_profiles_are_gated_by_client_version_and_exact_model_identity() { + assert!(profile("openai/gpt-6-astra", "0.153.0").is_some()); + assert!(profile("gpt-6-astra", "0.152.9").is_none()); + assert!(profile("DeepSeek/DeepSeek-V4.1-Flash", "0.166.0").is_some()); + assert!(profile("deepseek-v4.1-flash", "0.165.0").is_none()); + assert!(profile("deepseek-reasoner", "0.166.0").is_none()); + assert!(profile("deepseek-v99-flash", "0.166.0").is_none()); + assert!(profile("gpt-5.5-custom", "0.166.0").is_none()); + } +} diff --git a/crates/tw-gateway/tests/codex_catalog.rs b/crates/tw-gateway/tests/codex_catalog.rs index 2268b5a0..3455cc86 100644 --- a/crates/tw-gateway/tests/codex_catalog.rs +++ b/crates/tw-gateway/tests/codex_catalog.rs @@ -6,7 +6,7 @@ use std::time::Duration; use axum::extract::{OriginalUri, State}; use axum::http::HeaderMap; -use axum::routing::get; +use axum::routing::{get, post}; use axum::{Json, Router}; use serde_json::{Value, json}; use tw_config::{Client, Config, ModelSpec, Protocol, Provider}; @@ -16,6 +16,7 @@ use tw_gateway::AppState; struct Upstream { body: Arc>, requests: Arc>>, + forwarded: Arc>>, control: &'static str, gate: Option>, started: Arc, @@ -26,6 +27,7 @@ impl Upstream { Self { body: Arc::new(Mutex::new(body)), requests: Default::default(), + forwarded: Default::default(), control: "private, max-age=300", gate: None, started: Default::default(), @@ -55,9 +57,20 @@ impl Upstream { Json(up.body.lock().unwrap().clone()), ) } + async fn completion(State(up): State, Json(body): Json) -> Json { + up.forwarded.lock().unwrap().push(body.clone()); + Json(json!({ + "id":"chatcmpl-test","object":"chat.completion","created":0, + "model":body["model"], + "choices":[{"index":0,"message":{"role":"assistant","content":"OK"}, + "finish_reason":"stop"}], + "usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2} + })) + } let app = Router::new() .route("/v1/models", get(models)) .route("/models", get(models)) + .route("/v1/chat/completions", post(completion)) .with_state(self.clone()); let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let addr = listener.local_addr().unwrap(); @@ -233,6 +246,264 @@ async fn ordinary_model_lists_get_a_generic_profile_and_standard_listing_is_unch assert!(standard.get("models").is_none()); } +fn efforts(model: &Value) -> Vec<&str> { + model["supported_reasoning_levels"] + .as_array() + .unwrap() + .iter() + .map(|level| level["effort"].as_str().unwrap()) + .collect() +} + +#[tokio::test] +async fn ordinary_deepseek_models_and_aliases_offer_levels_that_reach_the_original_model() { + let original = "DeepSeek/DeepSeek-V4.1-Flash"; + let up = Upstream::new(json!({"data":[{"id":original}]})); + let mut p = provider("ordinary", up.serve().await, &[original]); + p.protocol = Some(Protocol::OpenaiChat); + let mut cfg = config(vec![p]); + cfg.aliases = serde_yaml_ng::from_str(&format!("coding: {original}")).unwrap(); + let (_, gw) = serve(cfg).await; + let body = catalog(gw).await; + for model in body["models"].as_array().unwrap() { + assert_eq!(efforts(model), ["low", "high", "max"]); + assert_eq!(model["default_reasoning_level"], "high"); + assert_eq!( + model["base_instructions"], + body["models"][0]["base_instructions"] + ); + } + assert_eq!(slugs(&body), [original, "coding"]); + let alias = body["models"] + .as_array() + .unwrap() + .iter() + .find(|m| m["slug"] == "coding") + .unwrap(); + for effort in efforts(alias) { + let response = http() + .post(format!("http://{gw}/v1/responses")) + .bearer_auth("gateway-test-key") + .json(&json!({"model":"coding","input":"Hello","reasoning":{"effort":effort}})) + .send() + .await + .unwrap(); + let status = response.status(); + let body = response.text().await.unwrap(); + assert_eq!(status, 200, "{body}"); + let sent = up.forwarded.lock().unwrap().last().unwrap().clone(); + assert_eq!(sent["model"], original); + assert_eq!(sent["reasoning_effort"], effort); + } +} + +#[tokio::test] +async fn ordinary_known_openai_models_keep_their_model_specific_choices() { + let up = Upstream::new(json!({"data":[{"id":"gpt-5.5"},{"id":"gpt-6-luna"}]})); + let mut cfg = config(vec![provider( + "ordinary", + up.serve().await, + &["gpt-5.5", "gpt-6-luna"], + )]); + cfg.aliases = serde_yaml_ng::from_str("coding: gpt-5.5").unwrap(); + let (_, gw) = serve(cfg).await; + let body = catalog(gw).await; + for slug in ["coding", "gpt-5.5"] { + let model = body["models"] + .as_array() + .unwrap() + .iter() + .find(|m| m["slug"] == slug) + .unwrap(); + assert_eq!(efforts(model), ["low", "medium", "high", "xhigh"]); + assert_eq!(model["default_reasoning_level"], "medium"); + assert_eq!( + model["node_repl_disabled"], true, + "reasoning profiles must not enable native tools" + ); + } + let model = body["models"] + .as_array() + .unwrap() + .iter() + .find(|m| m["slug"] == "gpt-6-luna") + .unwrap(); + assert_eq!(efforts(model), ["low", "medium", "high", "xhigh", "max"]); + assert_eq!(model["node_repl_disabled"], true); +} + +#[tokio::test] +async fn advertised_efforts_must_survive_the_candidate_protocol_translation() { + let up = Upstream::new(json!({"data":[{"id":"gpt-6.1-sol"}]})); + let addr = up.serve().await; + let mut translated = provider("chat", addr, &["gpt-6.1-sol"]); + translated.protocol = Some(Protocol::OpenaiChat); + let mut cfg = config(vec![ + provider("responses", addr, &["gpt-6.1-sol"]), + translated, + ]); + cfg.aliases = serde_yaml_ng::from_str("coding: gpt-6.1-sol").unwrap(); + let (state, gw) = serve(cfg.clone()).await; + for model in catalog(gw).await["models"].as_array().unwrap() { + assert_eq!(efforts(model), ["low", "medium", "high", "xhigh", "max"]); + } + cfg.providers.remove(1); + state.reload(cfg).unwrap(); + for model in catalog(gw).await["models"].as_array().unwrap() { + assert_eq!( + efforts(model), + ["low", "medium", "high", "xhigh", "max", "ultra"] + ); + } +} + +#[tokio::test] +async fn fallback_choices_follow_manual_overrides_and_conditional_reload() { + let up = Upstream::new(json!({"data":[{"id":"deepseek-v4.1-flash"}]})); + let mut cfg = config(vec![provider( + "ordinary", + up.serve().await, + &["deepseek-v4.1-flash"], + )]); + cfg.aliases = serde_yaml_ng::from_str("coding: deepseek-v4.1-flash").unwrap(); + let (state, gw) = serve(cfg.clone()).await; + let first = request(gw).send().await.unwrap(); + let etag = first.headers()["etag"].clone(); + assert_eq!( + efforts(&first.json::().await.unwrap()["models"][0]), + ["low", "high", "max"] + ); + cfg.providers[0].model_specs.insert( + "deepseek-v4.1-flash".into(), + ModelSpec { + reasoning: Some(false), + ..Default::default() + }, + ); + state.reload(cfg).unwrap(); + let changed = request(gw) + .header("if-none-match", etag) + .send() + .await + .unwrap(); + assert_eq!(changed.status(), 200); + let body: Value = changed.json().await.unwrap(); + for model in body["models"].as_array().unwrap() { + assert!(efforts(model).is_empty()); + assert!(model["default_reasoning_level"].is_null()); + } + assert_eq!(up.requests.lock().unwrap().len(), 1); +} + +#[tokio::test] +async fn unknown_reasoning_models_and_lookalike_aliases_get_no_guessed_levels() { + let up = Upstream::new(json!({"data":[{"id":"private-model"}]})); + let mut cfg = config(vec![provider( + "ordinary", + up.serve().await, + &["private-model"], + )]); + cfg.providers[0].model_specs.insert( + "private-model".into(), + ModelSpec { + reasoning: Some(true), + ..Default::default() + }, + ); + cfg.aliases = serde_yaml_ng::from_str("deepseek-flash: private-model").unwrap(); + let (_, gw) = serve(cfg).await; + for model in catalog(gw).await["models"].as_array().unwrap() { + assert!(efforts(model).is_empty()); + assert!(model["default_reasoning_level"].is_null()); + } +} + +#[tokio::test] +async fn upstream_native_choices_take_precedence_over_bundled_profiles() { + let mut model = native("deepseek-flash"); + model["supported_reasoning_levels"] = json!([]); + model["default_reasoning_level"] = Value::Null; + let up = Upstream::new(json!({"models":[model]})); + let (_, gw) = serve(config(vec![provider( + "native", + up.serve().await, + &["deepseek-flash"], + )])) + .await; + let body = catalog(gw).await; + assert!(efforts(&body["models"][0]).is_empty()); + assert!(body["models"][0]["default_reasoning_level"].is_null()); + assert_eq!( + body["models"][0]["base_instructions"], + "Native coding instructions" + ); +} + +#[tokio::test] +async fn aliases_keep_common_native_levels_even_when_complete_definitions_differ() { + let mut first = native("one"); + first["supported_reasoning_levels"] = json!([ + {"effort":"low","description":"Faster"}, + {"effort":"high","description":"Deeper"} + ]); + first["default_reasoning_level"] = json!("low"); + let mut second = native("two"); + second["base_instructions"] = json!("Different instructions"); + second["supported_reasoning_levels"] = json!([ + {"effort":"high","description":"Other high description"}, + {"effort":"max","description":"Maximum"} + ]); + second["default_reasoning_level"] = json!("max"); + let up = Upstream::new(json!({"models":[first,second]})); + let addr = up.serve().await; + let mut cfg = config(vec![ + provider("first", addr, &["one"]), + provider("second", addr, &["two"]), + ]); + cfg.aliases = serde_yaml_ng::from_str("coding: [one, two]").unwrap(); + let (_, gw) = serve(cfg).await; + let alias = &catalog(gw).await["models"][0]; + assert_eq!(alias["slug"], "coding"); + assert_eq!(efforts(alias), ["high"]); + assert_eq!(alias["default_reasoning_level"], "high"); + assert_ne!(alias["base_instructions"], "Native coding instructions"); +} + +#[tokio::test] +async fn aliases_intersect_bundled_profiles_and_unknown_candidates_remove_choices() { + let up = Upstream::new( + json!({"data":[{"id":"deepseek-flash"},{"id":"gpt-5.5"},{"id":"private-model"}]}), + ); + let addr = up.serve().await; + let mut cfg = config(vec![ + provider("deepseek", addr, &["deepseek-flash"]), + provider("openai", addr, &["gpt-5.5"]), + provider("private", addr, &["private-model"]), + ]); + cfg.aliases = serde_yaml_ng::from_str( + "coding: [gpt-5.5, deepseek-flash]\nmixed: [deepseek-flash, private-model]", + ) + .unwrap(); + let (_, gw) = serve(cfg).await; + let body = catalog(gw).await; + let alias = body["models"] + .as_array() + .unwrap() + .iter() + .find(|m| m["slug"] == "coding") + .unwrap(); + assert_eq!(efforts(alias), ["low", "high"]); + assert!(efforts(alias).contains(&alias["default_reasoning_level"].as_str().unwrap())); + let mixed = body["models"] + .as_array() + .unwrap() + .iter() + .find(|m| m["slug"] == "mixed") + .unwrap(); + assert!(efforts(mixed).is_empty()); + assert!(mixed["default_reasoning_level"].is_null()); +} + #[tokio::test] async fn live_reload_changes_names_aliases_permissions_and_specs_before_conditional_reads() { let up = Upstream::new(json!({"models":[native("one"),native("two")]})); diff --git a/docs/codex-model-catalog.md b/docs/codex-model-catalog.md index 4aad2c62..0955a567 100644 --- a/docs/codex-model-catalog.md +++ b/docs/codex-model-catalog.md @@ -60,8 +60,28 @@ names become the exposed slugs, with no migration target. If routing candidates have different native definitions, Core uses the generic profile and intersects their known capabilities. Explicit model specifications override native values; native values take precedence over the price table. Unknown context windows -are omitted, and reasoning effort choices are only taken from native metadata. -Image input requires affirmative support from every candidate. +are omitted. Image input requires affirmative support from every candidate. + +Reasoning choices come from upstream native metadata when available. For +ordinary model lists or unavailable native metadata, Core also includes +[documented reasoning profiles](../crates/tw-gateway/data/codex_reasoning.md) +for known OpenAI and DeepSeek models. DeepSeek Flash/V4.1-Flash and V4-Pro +offer `low`, `high` and `max`, with `high` as the default. These profiles supply +only effort choices; the generic instructions and tools remain unchanged. +Profiles respect their minimum Codex version. An unknown model does not gain +invented levels merely because its model specification says `reasoning: true`. + +Aliases inherit choices from their actual upstream model IDs, including names +with a provider namespace or different ASCII case. A name that merely resembles +a known model does not confer that model's capabilities. Across routing +candidates, Core intersects effort choices independently of instructions and +tools, and chooses a default from the resulting list. A manual +`model_specs.reasoning: false` or a candidate with no known choices removes +effort choices for that alias. An upstream's explicit native list, including an +empty one, takes precedence over a bundled profile. +When a candidate requires protocol translation, choices are also limited to +efforts Core can represent in that translation. Responses and ChatGPT +passthrough can retain newer native efforts such as `ultra`. ## Cache and limits diff --git a/docs/codex-model-catalog.zh-CN.md b/docs/codex-model-catalog.zh-CN.md index c3009d2f..47e26df5 100644 --- a/docs/codex-model-catalog.zh-CN.md +++ b/docs/codex-model-catalog.zh-CN.md @@ -46,7 +46,21 @@ OpenAI 内置模型的指令套到无关模型上。 只暴露共享网关目录中已经存在的模型,原生目录不会独立添加模型或授予推理权限。 别名作为对外的 slug,不设置迁移目标。多个路由候选的原生定义不一致时,使用通用描述, 对已知能力取交集。手动模型规格优先于原生值,原生值优先于价目表。未知上下文窗口不填, -推理档位仅采用原生目录提供的选项,图片输入要求所有候选都明确支持。 +图片输入要求所有候选都明确支持。 + +推理档位优先采用上游原生元数据。上游只返回普通模型列表或原生元数据不可用时,Core +还会使用已收录的 OpenAI、DeepSeek 模型的[有据可查的推理档位](../crates/tw-gateway/data/codex_reasoning.md)。 +DeepSeek Flash/V4.1-Flash 和 V4-Pro 提供 `low`、`high`、`max`,默认 `high`。 +这些内置定义只补推理档位,不改通用指令和工具,并遵守各自的最低 Codex 版本要求。 +仅凭模型规格中的 `reasoning: true` 不会给未知模型编造档位。 + +别名从实际发给上游的模型名继承档位,支持名称上的厂商前缀和 ASCII 大小写差异。 +给别名起一个已知模型的名字,不会让它获得那个模型的能力。多个路由候选的档位单独取 +交集,不会因为指令或工具不同而全部丢失,默认值也必须在交集内。手动 +`model_specs.reasoning: false` 或任一候选没有已知档位时,该别名不提供档位选项。 +上游明确提供的原生档位列表(包括空列表)优先于内置定义。 +有候选需要协议转换时,还会排除 Core 无法在转换中表达的档位。Responses 和 ChatGPT +直通可以保留 `ultra` 等更新的原生档位。 ## 缓存和限制 From beb1d51403dcb9ad0b2b98d7ea2b3ed0f03bac75 Mon Sep 17 00:00:00 2001 From: fylorn <249551762+fylorn@users.noreply.github.com> Date: Thu, 12 Nov 2026 01:21:45 +0800 Subject: [PATCH 2/3] fix(codex): project capabilities reported by upstream model APIs DeepSeek already reports effort choices and defaults in its ordinary models response. Read those fields instead of maintaining model profiles. Preserve capability metadata through alias projection while retaining the generic instructions and tools for ordinary providers. --- crates/tw-gateway/data/codex_reasoning.json | 468 ------------------ crates/tw-gateway/data/codex_reasoning.md | 26 - crates/tw-gateway/src/codex_catalog.rs | 58 +-- .../tw-gateway/src/codex_catalog/metadata.rs | 117 +++++ .../tw-gateway/src/codex_catalog/reasoning.rs | 101 ---- crates/tw-gateway/tests/codex_catalog.rs | 100 ++-- docs/codex-model-catalog.md | 42 +- docs/codex-model-catalog.zh-CN.md | 28 +- 8 files changed, 243 insertions(+), 697 deletions(-) delete mode 100644 crates/tw-gateway/data/codex_reasoning.json delete mode 100644 crates/tw-gateway/data/codex_reasoning.md create mode 100644 crates/tw-gateway/src/codex_catalog/metadata.rs delete mode 100644 crates/tw-gateway/src/codex_catalog/reasoning.rs diff --git a/crates/tw-gateway/data/codex_reasoning.json b/crates/tw-gateway/data/codex_reasoning.json deleted file mode 100644 index d86144ed..00000000 --- a/crates/tw-gateway/data/codex_reasoning.json +++ /dev/null @@ -1,468 +0,0 @@ -{ - "gpt-6-astra": { - "minimal_client_version": "0.153.0", - "default_reasoning_level": "low", - "supported_reasoning_levels": [ - { - "effort": "low", - "description": "Fast responses with lighter reasoning" - }, - { - "effort": "medium", - "description": "Balances speed and reasoning depth for everyday tasks" - }, - { - "effort": "high", - "description": "Greater reasoning depth for complex problems" - }, - { - "effort": "xhigh", - "description": "Extra high reasoning depth for complex problems" - }, - { - "effort": "max", - "description": "Maximum reasoning depth for the hardest problems" - }, - { - "effort": "ultra", - "description": "Maximum reasoning with automatic task delegation" - } - ] - }, - "gpt-6.1-sol": { - "minimal_client_version": "0.153.0", - "default_reasoning_level": "low", - "supported_reasoning_levels": [ - { - "effort": "low", - "description": "Fast responses with lighter reasoning" - }, - { - "effort": "medium", - "description": "Balances speed and reasoning depth for everyday tasks" - }, - { - "effort": "high", - "description": "Greater reasoning depth for complex problems" - }, - { - "effort": "xhigh", - "description": "Extra high reasoning depth for complex problems" - }, - { - "effort": "max", - "description": "Maximum reasoning depth for the hardest problems" - }, - { - "effort": "ultra", - "description": "Maximum reasoning with automatic task delegation" - } - ] - }, - "gpt-6.1-terra": { - "minimal_client_version": "0.153.0", - "default_reasoning_level": "low", - "supported_reasoning_levels": [ - { - "description": "Fast responses with lighter reasoning", - "effort": "low" - }, - { - "description": "Balances speed and reasoning depth for everyday tasks", - "effort": "medium" - }, - { - "description": "Greater reasoning depth for complex problems", - "effort": "high" - }, - { - "description": "Extra high reasoning depth for complex problems", - "effort": "xhigh" - }, - { - "description": "Maximum reasoning depth for the hardest problems", - "effort": "max" - }, - { - "description": "Maximum reasoning with automatic task delegation", - "effort": "ultra" - } - ] - }, - "gpt-6-sol": { - "minimal_client_version": "0.155.0", - "default_reasoning_level": "medium", - "supported_reasoning_levels": [ - { - "effort": "low", - "description": "Fast responses with lighter reasoning" - }, - { - "effort": "medium", - "description": "Balances speed and reasoning depth for everyday tasks" - }, - { - "effort": "high", - "description": "Greater reasoning depth for complex problems" - }, - { - "effort": "xhigh", - "description": "Extra high reasoning depth for complex problems" - }, - { - "effort": "max", - "description": "Maximum reasoning depth for the hardest problems" - }, - { - "effort": "ultra", - "description": "Maximum reasoning with automatic task delegation" - } - ] - }, - "gpt-6-luna": { - "minimal_client_version": "0.155.0", - "default_reasoning_level": "medium", - "supported_reasoning_levels": [ - { - "effort": "low", - "description": "Fast responses with lighter reasoning" - }, - { - "effort": "medium", - "description": "Balances speed and reasoning depth for everyday tasks" - }, - { - "effort": "high", - "description": "Greater reasoning depth for complex problems" - }, - { - "effort": "xhigh", - "description": "Extra high reasoning depth for complex problems" - }, - { - "effort": "max", - "description": "Maximum reasoning depth for the hardest problems" - } - ] - }, - "gpt-5.6-sol": { - "minimal_client_version": "0.144.0", - "default_reasoning_level": "low", - "supported_reasoning_levels": [ - { - "effort": "low", - "description": "Fast responses with lighter reasoning" - }, - { - "effort": "medium", - "description": "Balances speed and reasoning depth for everyday tasks" - }, - { - "effort": "high", - "description": "Greater reasoning depth for complex problems" - }, - { - "effort": "xhigh", - "description": "Extra high reasoning depth for complex problems" - }, - { - "effort": "max", - "description": "Maximum reasoning depth for the hardest problems" - }, - { - "effort": "ultra", - "description": "Maximum reasoning with automatic task delegation" - } - ] - }, - "gpt-5.6-terra": { - "minimal_client_version": "0.144.0", - "default_reasoning_level": "medium", - "supported_reasoning_levels": [ - { - "effort": "low", - "description": "Fast responses with lighter reasoning" - }, - { - "effort": "medium", - "description": "Balances speed and reasoning depth for everyday tasks" - }, - { - "effort": "high", - "description": "Greater reasoning depth for complex problems" - }, - { - "effort": "xhigh", - "description": "Extra high reasoning depth for complex problems" - }, - { - "effort": "max", - "description": "Maximum reasoning depth for the hardest problems" - }, - { - "effort": "ultra", - "description": "Maximum reasoning with automatic task delegation" - } - ] - }, - "gpt-5.6-luna": { - "minimal_client_version": "0.144.0", - "default_reasoning_level": "medium", - "supported_reasoning_levels": [ - { - "effort": "low", - "description": "Fast responses with lighter reasoning" - }, - { - "effort": "medium", - "description": "Balances speed and reasoning depth for everyday tasks" - }, - { - "effort": "high", - "description": "Greater reasoning depth for complex problems" - }, - { - "effort": "xhigh", - "description": "Extra high reasoning depth for complex problems" - }, - { - "effort": "max", - "description": "Maximum reasoning depth for the hardest problems" - } - ] - }, - "gpt-daybreak-blue-latest": { - "minimal_client_version": "0.142.2", - "default_reasoning_level": "low", - "supported_reasoning_levels": [ - { - "effort": "low", - "description": "Fast responses with lighter reasoning" - }, - { - "effort": "medium", - "description": "Balances speed and reasoning depth for everyday tasks" - }, - { - "effort": "high", - "description": "Greater reasoning depth for complex problems" - }, - { - "effort": "xhigh", - "description": "Extra high reasoning depth for complex problems" - }, - { - "effort": "max", - "description": "Maximum reasoning depth for the hardest problems" - }, - { - "effort": "ultra", - "description": "Maximum reasoning with automatic task delegation" - } - ] - }, - "gpt-daybreak-red-latest": { - "minimal_client_version": "0.142.2", - "default_reasoning_level": "medium", - "supported_reasoning_levels": [ - { - "effort": "low", - "description": "Fast responses with lighter reasoning" - }, - { - "effort": "medium", - "description": "Balances speed and reasoning depth for everyday tasks" - }, - { - "effort": "high", - "description": "Greater reasoning depth for complex problems" - }, - { - "effort": "xhigh", - "description": "Extra high reasoning depth for complex problems" - }, - { - "effort": "max", - "description": "Maximum reasoning depth for the hardest problems" - }, - { - "effort": "ultra", - "description": "Maximum reasoning with automatic task delegation" - } - ] - }, - "gpt-5.5": { - "minimal_client_version": "0.124.0", - "default_reasoning_level": "medium", - "supported_reasoning_levels": [ - { - "effort": "low", - "description": "Fast responses with lighter reasoning" - }, - { - "effort": "medium", - "description": "Balances speed and reasoning depth for everyday tasks" - }, - { - "effort": "high", - "description": "Greater reasoning depth for complex problems" - }, - { - "effort": "xhigh", - "description": "Extra high reasoning depth for complex problems" - } - ] - }, - "codex-auto-review": { - "minimal_client_version": "0.98.0", - "default_reasoning_level": "medium", - "supported_reasoning_levels": [ - { - "effort": "low", - "description": "Fast responses with lighter reasoning" - }, - { - "effort": "medium", - "description": "Balances speed and reasoning depth for everyday tasks" - }, - { - "effort": "high", - "description": "Greater reasoning depth for complex problems" - }, - { - "effort": "xhigh", - "description": "Extra high reasoning depth for complex problems" - }, - { - "effort": "max", - "description": "Maximum reasoning depth for the hardest problems" - } - ] - }, - "deepseek-flash": { - "minimal_client_version": "0.166.0", - "default_reasoning_level": "high", - "supported_reasoning_levels": [ - { - "effort": "low", - "description": "Faster responses with lighter reasoning" - }, - { - "effort": "high", - "description": "Deeper reasoning for complex tasks" - }, - { - "effort": "max", - "description": "Maximum reasoning depth" - } - ] - }, - "deepseek-v4-flash": { - "minimal_client_version": "0.166.0", - "default_reasoning_level": "high", - "supported_reasoning_levels": [ - { - "effort": "low", - "description": "Faster responses with lighter reasoning" - }, - { - "effort": "high", - "description": "Deeper reasoning for complex tasks" - }, - { - "effort": "max", - "description": "Maximum reasoning depth" - } - ] - }, - "deepseek-v4-flash-vision-exp": { - "minimal_client_version": "0.166.0", - "default_reasoning_level": "high", - "supported_reasoning_levels": [ - { - "effort": "low", - "description": "Faster responses with lighter reasoning" - }, - { - "effort": "high", - "description": "Deeper reasoning for complex tasks" - }, - { - "effort": "max", - "description": "Maximum reasoning depth" - } - ] - }, - "deepseek-v4.1-flash": { - "minimal_client_version": "0.166.0", - "default_reasoning_level": "high", - "supported_reasoning_levels": [ - { - "effort": "low", - "description": "Faster responses with lighter reasoning" - }, - { - "effort": "high", - "description": "Deeper reasoning for complex tasks" - }, - { - "effort": "max", - "description": "Maximum reasoning depth" - } - ] - }, - "deepseek-v4.1-flash-1017": { - "minimal_client_version": "0.166.0", - "default_reasoning_level": "high", - "supported_reasoning_levels": [ - { - "effort": "low", - "description": "Faster responses with lighter reasoning" - }, - { - "effort": "high", - "description": "Deeper reasoning for complex tasks" - }, - { - "effort": "max", - "description": "Maximum reasoning depth" - } - ] - }, - "deepseek-v4-pro": { - "minimal_client_version": "0.166.0", - "default_reasoning_level": "high", - "supported_reasoning_levels": [ - { - "effort": "low", - "description": "Faster responses with lighter reasoning" - }, - { - "effort": "high", - "description": "Deeper reasoning for complex tasks" - }, - { - "effort": "max", - "description": "Maximum reasoning depth" - } - ] - }, - "deepseek-v4-pro-0813": { - "minimal_client_version": "0.166.0", - "default_reasoning_level": "high", - "supported_reasoning_levels": [ - { - "effort": "low", - "description": "Faster responses with lighter reasoning" - }, - { - "effort": "high", - "description": "Deeper reasoning for complex tasks" - }, - { - "effort": "max", - "description": "Maximum reasoning depth" - } - ] - } -} diff --git a/crates/tw-gateway/data/codex_reasoning.md b/crates/tw-gateway/data/codex_reasoning.md deleted file mode 100644 index 1dbfe88d..00000000 --- a/crates/tw-gateway/data/codex_reasoning.md +++ /dev/null @@ -1,26 +0,0 @@ -# Codex reasoning profiles - -`codex_reasoning.json` supplies effort choices when an upstream lists ordinary -model IDs without native Codex metadata. It deliberately contains only model -identity, the minimum Codex version, the default effort and supported levels. -It does not copy OpenAI prompts, tools or other capabilities onto a provider. -An upstream native definition takes precedence, including an empty effort list; -an explicit `model_specs.reasoning: false` still disables reasoning choices. - -OpenAI entries are extracted from `codex-rs/models-manager/models.json` at -[`e58bf8f5233b51e7cd1679d947e3028c1ed91b06`](https://github.com/openai/codex/blob/e58bf8f5233b51e7cd1679d947e3028c1ed91b06/codex-rs/models-manager/models.json). -Preserve each entry's minimum client version, default and complete effort list -when refreshing this snapshot. The source is MIT licensed. - -DeepSeek entries follow the official [thinking-mode guide](https://api-docs.deepseek.com/guides/thinking_mode) -and [model details](https://api-docs.deepseek.com/quick_start/pricing), checked -on 2026-11-12: `low`, `high`, `max`, with `high` as the default. The Flash ID, -its documented legacy names and the V4.1-Flash-1017 model version share these -choices, as do V4-Pro and its documented V4-Pro-0813 version. The generic Codex -profile is verified with CLI 0.166.0, so these profiles require that version. - -Profiles are selected by the actual upstream model ID, ignoring a provider -namespace and ASCII case. Alias names never determine capabilities. Unknown -models receive no invented effort choices, even if a price table or manual -specification says they can reason. Add another model only with an authoritative -source for its actual levels and default. diff --git a/crates/tw-gateway/src/codex_catalog.rs b/crates/tw-gateway/src/codex_catalog.rs index da24292f..efcc18c3 100644 --- a/crates/tw-gateway/src/codex_catalog.rs +++ b/crates/tw-gateway/src/codex_catalog.rs @@ -3,7 +3,7 @@ //! Only upstream metadata is cached. Names, aliases, permissions and manual //! capabilities are resolved again on every read, including conditional reads. //! A version/credential-scoped upstream catalog preserves native instructions -//! and tools; ordinary API model lists use a conservative gateway profile. +//! and tools; ordinary model capabilities are projected onto a generic profile. use std::collections::{BTreeMap, HashMap}; use std::sync::{Arc, Mutex}; @@ -20,7 +20,7 @@ use tw_types::msg; use crate::error::GatewayError; use crate::state::{AppState, Runtime}; -mod reasoning; +mod metadata; const MAX_BYTES: usize = 1024 * 1024; const MAX_CACHED: usize = 32; @@ -153,14 +153,22 @@ async fn fetch( } let value: Value = serde_json::from_slice(&body).ok()?; let mut models = BTreeMap::new(); - for model in value.get("models")?.as_array()? { - // A list of IDs is not a native Codex definition. Unknown fields in a - // valid definition survive unchanged, including version-specific tools. - if native(model) { - models - .entry(model["slug"].as_str()?.to_string()) - .or_insert_with(|| model.clone()); - } + let entries = value["models"] + .as_array() + .or_else(|| value["data"].as_array())?; + for model in entries { + // Native definitions retain version-specific fields. Ordinary API + // entries supply only the capabilities their endpoint actually reports. + let definition = if native(model) { + model.clone() + } else if let Some(definition) = metadata::ordinary(model) { + definition + } else { + continue; + }; + models + .entry(definition["slug"].as_str()?.to_string()) + .or_insert(definition); } Some((Arc::new(models), ttl)) } @@ -352,15 +360,7 @@ pub(crate) async fn list( let book = state.pricing.load(); let models: Vec<_> = names(&rt.config, &catalog, allow.as_deref()) .iter() - .map(|name| { - project( - name, - &candidates(&rt, &catalog, name), - &book, - &upstreams, - &version, - ) - }) + .map(|name| project(name, &candidates(&rt, &catalog, name), &book, &upstreams)) .collect(); let body = serde_json::to_vec(&json!({"models":models})).expect("catalog is JSON"); if body.len() > MAX_BYTES { @@ -421,17 +421,14 @@ fn project( candidates: &[(&Provider, &str)], book: &tw_pricing::PriceBook, upstreams: &HashMap, - version: &str, ) -> Value { let definitions: Vec<_> = candidates .iter() .map(|(p, model)| upstreams.get(&p.name).and_then(|models| models.get(*model))) .collect(); - let native = definitions - .first() - .copied() - .flatten() - .filter(|first| definitions.iter().all(|v| v.is_some_and(|v| v == *first))); + let native = definitions.first().copied().flatten().filter(|first| { + native(first) && definitions.iter().all(|v| v.is_some_and(|v| v == *first)) + }); let mut out = native.cloned().unwrap_or_else(|| generic(name)); let mut contexts = Vec::new(); let mut max_contexts = Vec::new(); @@ -451,21 +448,18 @@ fn project( .or_else(|| d["context_window"].as_u64()) }), )); - // Only effort choices use the bundled profiles. Instructions, tools - // and other native fields still come exclusively from the upstream. - let profile = definition.or_else(|| reasoning::profile(model, version)); let enabled = pick( spec.reasoning, - profile.map(|d| { + definition.and_then(|d| { d["supported_reasoning_levels"] .as_array() - .is_some_and(|v| !v.is_empty()) + .map(|v| !v.is_empty()) }), ); reasoning.push(if enabled == Some(false) { None } else { - profile + *definition }); images.push(pick( spec.image_input, @@ -497,7 +491,7 @@ fn project( Some(tw_config::Protocol::OpenaiResponses | tw_config::Protocol::Chatgpt) ) }); - reasoning::apply(&mut out, &reasoning, requires_translation); + metadata::apply_reasoning(&mut out, &reasoning, requires_translation); if !images.is_empty() && images.iter().all(|v| *v == Some(true)) { out["input_modalities"] = json!(["text", "image"]); } else { diff --git a/crates/tw-gateway/src/codex_catalog/metadata.rs b/crates/tw-gateway/src/codex_catalog/metadata.rs new file mode 100644 index 00000000..24e22c02 --- /dev/null +++ b/crates/tw-gateway/src/codex_catalog/metadata.rs @@ -0,0 +1,117 @@ +//! Project reported API model capabilities without adopting native instructions. + +use std::collections::BTreeSet; + +use serde_json::{Value, json}; + +pub(super) fn ordinary(model: &Value) -> Option { + let id = model["id"].as_str().filter(|id| !id.trim().is_empty())?; + let mut out = json!({"slug": id}); + if let Some(context) = model["context_window"].as_u64().filter(|n| *n > 0) { + out["context_window"] = json!(context); + } + if let Some(modalities) = model["input_modalities"].as_array() { + out["input_modalities"] = json!( + modalities + .iter() + .filter(|m| *m == "text" || *m == "image") + .collect::>() + ); + } + if let Some(levels) = model["effort"]["supported_levels"].as_array() { + let mut seen = BTreeSet::new(); + let levels: Vec<_> = levels + .iter() + .filter_map(Value::as_str) + .filter(|effort| { + matches!(*effort, "minimal" | "low" | "medium" | "high" | "xhigh" | "max" | "ultra") + && seen.insert(*effort) + }) + .map(|effort| json!({"effort": effort, "description": format!("Reasoning effort: {effort}")})) + .collect(); + let default = model["effort"]["default_level"] + .as_str() + .filter(|effort| levels.iter().any(|level| level["effort"] == *effort)); + out["supported_reasoning_levels"] = json!(levels); + out["default_reasoning_level"] = json!(default); + } + Some(out) +} + +/// Definitions may disagree on prompts or tools while sharing effort choices. +/// Keep only levels that every routing candidate explicitly supports. +pub(super) fn apply_reasoning( + out: &mut Value, + profiles: &[Option<&Value>], + requires_translation: bool, +) { + let first = profiles.first().copied().flatten(); + let mut levels = first + .and_then(|p| p["supported_reasoning_levels"].as_array()) + .cloned() + .unwrap_or_default(); + levels.retain(|level| { + level["effort"].as_str().is_some_and(|effort| { + (!requires_translation || tw_dialect::think::parse_openai(effort).is_some()) + && profiles.iter().all(|profile| { + profile + .and_then(|p| p["supported_reasoning_levels"].as_array()) + .is_some_and(|levels| levels.iter().any(|v| v["effort"] == effort)) + }) + }) + }); + let default = first + .and_then(|p| p["default_reasoning_level"].as_str()) + .filter(|effort| levels.iter().any(|v| v["effort"] == *effort)) + .or_else(|| { + levels + .iter() + .find(|v| v["effort"] == "medium") + .or_else(|| levels.first()) + .and_then(|v| v["effort"].as_str()) + }); + out["default_reasoning_level"] = json!(default); + out["supported_reasoning_levels"] = json!(levels); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn ordinary_capabilities_are_reported_values_without_instructions_or_tools() { + let out = ordinary(&json!({ + "id":"private-model", "context_window":1048576, + "input_modalities":["text","image","audio"], + "effort":{"supported_levels":["low","high","max"],"default_level":"high"}, + "base_instructions":"Do not copy", "experimental_supported_tools":["do_not_copy"] + })) + .unwrap(); + assert_eq!(out["context_window"], 1048576); + assert_eq!(out["input_modalities"], json!(["text", "image"])); + assert_eq!(out["default_reasoning_level"], "high"); + assert_eq!(out["supported_reasoning_levels"][2]["effort"], "max"); + assert!(out.get("base_instructions").is_none()); + assert!(out.get("experimental_supported_tools").is_none()); + } + + #[test] + fn missing_and_malformed_capabilities_do_not_create_claims_or_invalid_codex_levels() { + assert!(ordinary(&json!({"id":" "})).is_none()); + let mut out = ordinary(&json!({"id":"private-model","context_window":-1})).unwrap(); + assert!(out.get("context_window").is_none()); + assert!(out.get("supported_reasoning_levels").is_none()); + let profile = ordinary(&json!({"id":"private-model","effort":{ + "supported_levels":["high",true,"unknown","max","high"],"default_level":"invalid" + }})) + .unwrap(); + assert!(profile["default_reasoning_level"].is_null()); + out = json!({}); + apply_reasoning(&mut out, &[Some(&profile)], false); + assert_eq!( + out["supported_reasoning_levels"].as_array().unwrap().len(), + 2 + ); + assert_eq!(out["default_reasoning_level"], "high"); + } +} diff --git a/crates/tw-gateway/src/codex_catalog/reasoning.rs b/crates/tw-gateway/src/codex_catalog/reasoning.rs deleted file mode 100644 index 1736b307..00000000 --- a/crates/tw-gateway/src/codex_catalog/reasoning.rs +++ /dev/null @@ -1,101 +0,0 @@ -//! Reasoning choices require actual levels, not just a `reasoning: true` flag. - -use std::collections::BTreeMap; -use std::sync::OnceLock; - -use serde_json::{Value, json}; - -pub(super) fn profile(model: &str, version: &str) -> Option<&'static Value> { - static PROFILES: OnceLock> = OnceLock::new(); - let profiles = PROFILES.get_or_init(|| { - serde_json::from_str(include_str!("../../data/codex_reasoning.json")) - .expect("bundled reasoning profiles are valid JSON") - }); - // Providers commonly qualify an unchanged model ID with their namespace. - // Resolve this against the routing candidate, never the public alias name. - let name = model.rsplit('/').next()?.to_ascii_lowercase(); - let profile = profiles.get(&name)?; - let minimum = profile["minimal_client_version"].as_str()?; - (parse_version(version)? >= parse_version(minimum)?).then_some(profile) -} - -fn parse_version(version: &str) -> Option<(u64, u64, u64)> { - let mut parts = version.split('.'); - let parsed = ( - parts.next()?.parse().ok()?, - parts.next()?.parse().ok()?, - parts.next()?.parse().ok()?, - ); - parts.next().is_none().then_some(parsed) -} - -/// Definitions may disagree on prompts or tools while sharing effort choices. -/// Keep only levels that every routing candidate explicitly supports. -pub(super) fn apply(out: &mut Value, profiles: &[Option<&Value>], requires_translation: bool) { - let first = profiles.first().copied().flatten(); - let mut levels = first - .and_then(|p| p["supported_reasoning_levels"].as_array()) - .cloned() - .unwrap_or_default(); - levels.retain(|level| { - level["effort"].as_str().is_some_and(|effort| { - (!requires_translation || tw_dialect::think::parse_openai(effort).is_some()) - && profiles.iter().all(|profile| { - profile - .and_then(|p| p["supported_reasoning_levels"].as_array()) - .is_some_and(|levels| levels.iter().any(|v| v["effort"] == effort)) - }) - }) - }); - let default = first - .and_then(|p| p["default_reasoning_level"].as_str()) - .filter(|effort| levels.iter().any(|v| v["effort"] == *effort)) - .or_else(|| { - levels - .iter() - .find(|v| v["effort"] == "medium") - .or_else(|| levels.first()) - .and_then(|v| v["effort"].as_str()) - }); - out["default_reasoning_level"] = json!(default); - out["supported_reasoning_levels"] = json!(levels); -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn every_bundled_profile_has_valid_unique_choices_and_an_available_default() { - let profiles: BTreeMap = - serde_json::from_str(include_str!("../../data/codex_reasoning.json")).unwrap(); - for (model, profile) in profiles { - assert_eq!(model, model.to_ascii_lowercase()); - assert_eq!(profile.as_object().unwrap().len(), 3, "{model}"); - assert!(parse_version(profile["minimal_client_version"].as_str().unwrap()).is_some()); - let levels = profile["supported_reasoning_levels"].as_array().unwrap(); - let mut efforts = std::collections::BTreeSet::new(); - for level in levels { - let effort = level["effort"].as_str().unwrap(); - assert!(matches!( - effort, - "none" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max" | "ultra" - )); - assert!(efforts.insert(effort), "duplicate effort in {model}"); - assert!(!level["description"].as_str().unwrap().trim().is_empty()); - } - assert!(efforts.contains(profile["default_reasoning_level"].as_str().unwrap())); - } - } - - #[test] - fn bundled_profiles_are_gated_by_client_version_and_exact_model_identity() { - assert!(profile("openai/gpt-6-astra", "0.153.0").is_some()); - assert!(profile("gpt-6-astra", "0.152.9").is_none()); - assert!(profile("DeepSeek/DeepSeek-V4.1-Flash", "0.166.0").is_some()); - assert!(profile("deepseek-v4.1-flash", "0.165.0").is_none()); - assert!(profile("deepseek-reasoner", "0.166.0").is_none()); - assert!(profile("deepseek-v99-flash", "0.166.0").is_none()); - assert!(profile("gpt-5.5-custom", "0.166.0").is_none()); - } -} diff --git a/crates/tw-gateway/tests/codex_catalog.rs b/crates/tw-gateway/tests/codex_catalog.rs index 3455cc86..ef367aa8 100644 --- a/crates/tw-gateway/tests/codex_catalog.rs +++ b/crates/tw-gateway/tests/codex_catalog.rs @@ -246,6 +246,10 @@ async fn ordinary_model_lists_get_a_generic_profile_and_standard_listing_is_unch assert!(standard.get("models").is_none()); } +fn reported_model(id: &str, levels: &[&str], default: &str) -> Value { + json!({"id":id,"effort":{"supported_levels":levels,"default_level":default}}) +} + fn efforts(model: &Value) -> Vec<&str> { model["supported_reasoning_levels"] .as_array() @@ -258,7 +262,8 @@ fn efforts(model: &Value) -> Vec<&str> { #[tokio::test] async fn ordinary_deepseek_models_and_aliases_offer_levels_that_reach_the_original_model() { let original = "DeepSeek/DeepSeek-V4.1-Flash"; - let up = Upstream::new(json!({"data":[{"id":original}]})); + let up = + Upstream::new(json!({"data":[reported_model(original, &["low","high","max"], "high")]})); let mut p = provider("ordinary", up.serve().await, &[original]); p.protocol = Some(Protocol::OpenaiChat); let mut cfg = config(vec![p]); @@ -298,43 +303,41 @@ async fn ordinary_deepseek_models_and_aliases_offer_levels_that_reach_the_origin } #[tokio::test] -async fn ordinary_known_openai_models_keep_their_model_specific_choices() { - let up = Upstream::new(json!({"data":[{"id":"gpt-5.5"},{"id":"gpt-6-luna"}]})); +async fn ordinary_upstream_capabilities_are_projected_without_native_tools_or_model_name_guesses() { + let mut model = reported_model("private-model", &["low", "high", "max"], "high"); + model["context_window"] = json!(1048576); + model["input_modalities"] = json!(["text", "image"]); + model["base_instructions"] = json!("Must not become native instructions"); + model["experimental_supported_tools"] = json!(["not_a_native_tool"]); + let up = Upstream::new(json!({"data":[model]})); let mut cfg = config(vec![provider( "ordinary", up.serve().await, - &["gpt-5.5", "gpt-6-luna"], + &["private-model"], )]); - cfg.aliases = serde_yaml_ng::from_str("coding: gpt-5.5").unwrap(); + cfg.aliases = serde_yaml_ng::from_str("coding: private-model").unwrap(); let (_, gw) = serve(cfg).await; let body = catalog(gw).await; - for slug in ["coding", "gpt-5.5"] { - let model = body["models"] - .as_array() - .unwrap() - .iter() - .find(|m| m["slug"] == slug) - .unwrap(); - assert_eq!(efforts(model), ["low", "medium", "high", "xhigh"]); - assert_eq!(model["default_reasoning_level"], "medium"); - assert_eq!( - model["node_repl_disabled"], true, - "reasoning profiles must not enable native tools" + for model in body["models"].as_array().unwrap() { + assert_eq!(efforts(model), ["low", "high", "max"]); + assert_eq!(model["default_reasoning_level"], "high"); + assert_eq!(model["context_window"], 1048576); + assert_eq!(model["max_context_window"], 1048576); + assert_eq!(model["input_modalities"], json!(["text", "image"])); + assert_eq!(model["node_repl_disabled"], true); + assert_eq!(model["experimental_supported_tools"], json!([])); + assert_ne!( + model["base_instructions"], + "Must not become native instructions" ); } - let model = body["models"] - .as_array() - .unwrap() - .iter() - .find(|m| m["slug"] == "gpt-6-luna") - .unwrap(); - assert_eq!(efforts(model), ["low", "medium", "high", "xhigh", "max"]); - assert_eq!(model["node_repl_disabled"], true); } #[tokio::test] async fn advertised_efforts_must_survive_the_candidate_protocol_translation() { - let up = Upstream::new(json!({"data":[{"id":"gpt-6.1-sol"}]})); + let up = Upstream::new( + json!({"data":[reported_model("gpt-6.1-sol", &["low","medium","high","xhigh","max","ultra"], "low")]}), + ); let addr = up.serve().await; let mut translated = provider("chat", addr, &["gpt-6.1-sol"]); translated.protocol = Some(Protocol::OpenaiChat); @@ -358,8 +361,10 @@ async fn advertised_efforts_must_survive_the_candidate_protocol_translation() { } #[tokio::test] -async fn fallback_choices_follow_manual_overrides_and_conditional_reload() { - let up = Upstream::new(json!({"data":[{"id":"deepseek-v4.1-flash"}]})); +async fn ordinary_choices_follow_manual_overrides_and_conditional_reload() { + let up = Upstream::new( + json!({"data":[reported_model("deepseek-v4.1-flash", &["low","high","max"], "high")]}), + ); let mut cfg = config(vec![provider( "ordinary", up.serve().await, @@ -419,11 +424,13 @@ async fn unknown_reasoning_models_and_lookalike_aliases_get_no_guessed_levels() } #[tokio::test] -async fn upstream_native_choices_take_precedence_over_bundled_profiles() { +async fn upstream_native_choices_take_precedence_over_ordinary_capabilities() { let mut model = native("deepseek-flash"); model["supported_reasoning_levels"] = json!([]); model["default_reasoning_level"] = Value::Null; - let up = Upstream::new(json!({"models":[model]})); + let up = Upstream::new( + json!({"models":[model], "data":[reported_model("deepseek-flash", &["low","high","max"], "high")]}), + ); let (_, gw) = serve(config(vec![provider( "native", up.serve().await, @@ -470,9 +477,9 @@ async fn aliases_keep_common_native_levels_even_when_complete_definitions_differ } #[tokio::test] -async fn aliases_intersect_bundled_profiles_and_unknown_candidates_remove_choices() { +async fn aliases_intersect_reported_capabilities_and_unknown_candidates_remove_choices() { let up = Upstream::new( - json!({"data":[{"id":"deepseek-flash"},{"id":"gpt-5.5"},{"id":"private-model"}]}), + json!({"data":[reported_model("deepseek-flash", &["low","high","max"], "high"),reported_model("gpt-5.5", &["low","medium","high","xhigh"], "medium"),{"id":"private-model"}]}), ); let addr = up.serve().await; let mut cfg = config(vec![ @@ -504,6 +511,35 @@ async fn aliases_intersect_bundled_profiles_and_unknown_candidates_remove_choice assert!(mixed["default_reasoning_level"].is_null()); } +#[tokio::test] +async fn changed_ordinary_efforts_and_defaults_change_the_conditional_catalog() { + let mut up = + Upstream::new(json!({"data":[reported_model("private-model", &["high","max"], "high")]})); + up.control = "no-store"; + let (_, gw) = serve(config(vec![provider( + "ordinary", + up.serve().await, + &["private-model"], + )])) + .await; + let first = request(gw).send().await.unwrap(); + let etag = first.headers()["etag"].clone(); + assert_eq!( + efforts(&first.json::().await.unwrap()["models"][0]), + ["high", "max"] + ); + up.body.lock().unwrap()["data"][0] = reported_model("private-model", &["low", "high"], "low"); + let changed = request(gw) + .header("if-none-match", etag) + .send() + .await + .unwrap(); + assert_eq!(changed.status(), 200); + let body: Value = changed.json().await.unwrap(); + assert_eq!(efforts(&body["models"][0]), ["low", "high"]); + assert_eq!(body["models"][0]["default_reasoning_level"], "low"); +} + #[tokio::test] async fn live_reload_changes_names_aliases_permissions_and_specs_before_conditional_reads() { let up = Upstream::new(json!({"models":[native("one"),native("two")]})); diff --git a/docs/codex-model-catalog.md b/docs/codex-model-catalog.md index 0955a567..dea137a6 100644 --- a/docs/codex-model-catalog.md +++ b/docs/codex-model-catalog.md @@ -50,8 +50,8 @@ and configured proxy. The inbound gateway key is never forwarded. ChatGPT uses its native models URL. Direct OpenAI API providers use OpenAI's documented native Codex endpoint; other providers use their models endpoint. A full native `models` entry retains its instructions, tools and unknown version-specific -fields. A standard OpenAI `data` list contains no native Codex instructions and -uses a generic coding profile instead. Core does not copy bundled OpenAI model +fields. A standard OpenAI `data` list uses a generic coding profile; reported model +capabilities are projected onto it without adopting native instructions or tools. Core does not copy bundled OpenAI model instructions onto unrelated providers. Only models already in the shared gateway directory are exposed. A native @@ -62,30 +62,26 @@ their known capabilities. Explicit model specifications override native values; native values take precedence over the price table. Unknown context windows are omitted. Image input requires affirmative support from every candidate. -Reasoning choices come from upstream native metadata when available. For -ordinary model lists or unavailable native metadata, Core also includes -[documented reasoning profiles](../crates/tw-gateway/data/codex_reasoning.md) -for known OpenAI and DeepSeek models. DeepSeek Flash/V4.1-Flash and V4-Pro -offer `low`, `high` and `max`, with `high` as the default. These profiles supply -only effort choices; the generic instructions and tools remain unchanged. -Profiles respect their minimum Codex version. An unknown model does not gain -invented levels merely because its model specification says `reasoning: true`. - -Aliases inherit choices from their actual upstream model IDs, including names -with a provider namespace or different ASCII case. A name that merely resembles -a known model does not confer that model's capabilities. Across routing -candidates, Core intersects effort choices independently of instructions and -tools, and chooses a default from the resulting list. A manual -`model_specs.reasoning: false` or a candidate with no known choices removes -effort choices for that alias. An upstream's explicit native list, including an -empty one, takes precedence over a bundled profile. -When a candidate requires protocol translation, choices are also limited to -efforts Core can represent in that translation. Responses and ChatGPT -passthrough can retain newer native efforts such as `ultra`. +For ordinary upstream model lists, Core reads `data[].context_window`, +`data[].input_modalities`, `data[].effort.supported_levels`, and +`data[].effort.default_level`. For example, DeepSeek documents these fields in +its [models endpoint](https://api-docs.deepseek.com/api/list-models). The effort +list becomes Codex's `supported_reasoning_levels`; the reported default is kept +when it is in the list. Optional or malformed fields do not create capabilities. +No model-name table or inference from `reasoning: true` supplies missing levels. +A relay must preserve the metadata for Core to use it. + +Aliases inherit the actual routing targets' reported capabilities. Effort +choices are intersected separately from full native definitions, so differences +in prompts or tools do not discard shared levels. Defaults must belong to the +intersection. Manual `model_specs.reasoning: false` removes choices. When any +candidate requires protocol translation, choices are also limited to efforts +Core can translate; Responses and ChatGPT passthrough can retain newer native +efforts such as `ultra`. ## Cache and limits -Only upstream native metadata is cached. Entries are isolated by the provider, +Only upstream metadata is cached. Entries are isolated by the provider, resolved credentials and headers, URL, client version and proxy configuration. There are at most 32 cache entries and four concurrent metadata fetches. Fetches have an eight-second deadline. Successful upstream cache lifetimes are capped at diff --git a/docs/codex-model-catalog.zh-CN.md b/docs/codex-model-catalog.zh-CN.md index 47e26df5..ca9f2207 100644 --- a/docs/codex-model-catalog.zh-CN.md +++ b/docs/codex-model-catalog.zh-CN.md @@ -40,31 +40,29 @@ Core 向 Responses、Chat Completions 和 ChatGPT 上游获取元数据时,传 Codex `client_version`,使用该上游的凭据和代理,不转发入站网关密钥。ChatGPT 使用原生 模型地址,直连 OpenAI API 时使用其文档指定的原生 Codex 接口,其他上游使用各自的模型接口。 完整的原生 `models` 条目会保留指令、工具和 -版本特有的未知字段。标准 OpenAI `data` 列表没有原生指令,改用通用编程描述,不把 -OpenAI 内置模型的指令套到无关模型上。 +版本特有的未知字段。标准 OpenAI `data` 列表使用通用编程描述,并将上游报告的能力 +映射到其中,不采用普通条目里的指令或工具。 只暴露共享网关目录中已经存在的模型,原生目录不会独立添加模型或授予推理权限。 别名作为对外的 slug,不设置迁移目标。多个路由候选的原生定义不一致时,使用通用描述, 对已知能力取交集。手动模型规格优先于原生值,原生值优先于价目表。未知上下文窗口不填, 图片输入要求所有候选都明确支持。 -推理档位优先采用上游原生元数据。上游只返回普通模型列表或原生元数据不可用时,Core -还会使用已收录的 OpenAI、DeepSeek 模型的[有据可查的推理档位](../crates/tw-gateway/data/codex_reasoning.md)。 -DeepSeek Flash/V4.1-Flash 和 V4-Pro 提供 `low`、`high`、`max`,默认 `high`。 -这些内置定义只补推理档位,不改通用指令和工具,并遵守各自的最低 Codex 版本要求。 -仅凭模型规格中的 `reasoning: true` 不会给未知模型编造档位。 +普通模型列表读取 `data[].context_window`、`data[].input_modalities`、 +`data[].effort.supported_levels` 和 `data[].effort.default_level`。例如 DeepSeek 的 +[模型接口文档](https://api-docs.deepseek.com/api/list-models)已定义这些字段。档位列表转为 +Codex 的 `supported_reasoning_levels`,报告的默认值在列表内时保留。可选字段缺失或 +格式错误时不编造能力,也不按模型名查表或仅凭 `reasoning: true` 推导档位;中转站需要 +透传这些元数据才能使用。 -别名从实际发给上游的模型名继承档位,支持名称上的厂商前缀和 ASCII 大小写差异。 -给别名起一个已知模型的名字,不会让它获得那个模型的能力。多个路由候选的档位单独取 -交集,不会因为指令或工具不同而全部丢失,默认值也必须在交集内。手动 -`model_specs.reasoning: false` 或任一候选没有已知档位时,该别名不提供档位选项。 -上游明确提供的原生档位列表(包括空列表)优先于内置定义。 -有候选需要协议转换时,还会排除 Core 无法在转换中表达的档位。Responses 和 ChatGPT -直通可以保留 `ultra` 等更新的原生档位。 +别名继承实际路由目标报告的能力。推理档位独立于完整原生定义取交集,不会因为指令或 +工具不同而丢掉共有档位,默认值必须在交集内。手动 `model_specs.reasoning: false` +移除档位选项。任一候选需要协议转换时,只保留 Core 能表达的档位;Responses 和 +ChatGPT 直通可以保留 `ultra` 等更新的原生档位。 ## 缓存和限制 -只缓存上游原生元数据。缓存按上游、解析后的凭据及请求头、地址、客户端版本和代理配置 +只缓存上游元数据。缓存按上游、解析后的凭据及请求头、地址、客户端版本和代理配置 隔离,最多 32 项,同时最多四次元数据请求,获取期限为八秒。成功响应最多缓存五分钟, 扣除 `Age`,遵守 `no-cache` 和 `no-store`。获取失败时使用通用描述,30 秒后重试。 上游元数据过大或不可用时也使用通用描述。 From e62a28d7cf4d02c6738ab6bf3974a4f7a0597a52 Mon Sep 17 00:00:00 2001 From: fylorn <249551762+fylorn@users.noreply.github.com> Date: Thu, 12 Nov 2026 01:49:04 +0800 Subject: [PATCH 3/3] chore(release): prepare Core 0.94.0 Bump the workspace and lockfile versions and describe upstream model capability discovery and alias inheritance in the release notes. --- Cargo.lock | 44 ++++++++++++++++++++--------------------- Cargo.toml | 2 +- release-notes/0.94.0.md | 3 +++ 3 files changed, 26 insertions(+), 23 deletions(-) create mode 100644 release-notes/0.94.0.md diff --git a/Cargo.lock b/Cargo.lock index 818ffd18..07cfb531 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3763,7 +3763,7 @@ dependencies = [ [[package]] name = "tw-api" -version = "0.93.0" +version = "0.94.0" dependencies = [ "serde", "serde_json", @@ -3774,7 +3774,7 @@ dependencies = [ [[package]] name = "tw-bedrock" -version = "0.93.0" +version = "0.94.0" dependencies = [ "aws-credential-types", "aws-sigv4", @@ -3795,7 +3795,7 @@ dependencies = [ [[package]] name = "tw-breaker" -version = "0.93.0" +version = "0.94.0" dependencies = [ "serde", "serde_json", @@ -3803,7 +3803,7 @@ dependencies = [ [[package]] name = "tw-config" -version = "0.93.0" +version = "0.94.0" dependencies = [ "blake3", "libc", @@ -3829,7 +3829,7 @@ dependencies = [ [[package]] name = "tw-control" -version = "0.93.0" +version = "0.94.0" dependencies = [ "axum", "base64 0.22.1", @@ -3875,7 +3875,7 @@ dependencies = [ [[package]] name = "tw-dialect" -version = "0.93.0" +version = "0.94.0" dependencies = [ "memchr", "serde", @@ -3884,7 +3884,7 @@ dependencies = [ [[package]] name = "tw-engine" -version = "0.93.0" +version = "0.94.0" dependencies = [ "chrono", "serde", @@ -3898,7 +3898,7 @@ dependencies = [ [[package]] name = "tw-gateway" -version = "0.93.0" +version = "0.94.0" dependencies = [ "arc-swap", "async-stream", @@ -3950,7 +3950,7 @@ dependencies = [ [[package]] name = "tw-guard" -version = "0.93.0" +version = "0.94.0" dependencies = [ "aho-corasick", "base64 0.22.1", @@ -3971,7 +3971,7 @@ dependencies = [ [[package]] name = "tw-handover" -version = "0.93.0" +version = "0.94.0" dependencies = [ "libc", "serde", @@ -3984,7 +3984,7 @@ dependencies = [ [[package]] name = "tw-link" -version = "0.93.0" +version = "0.94.0" dependencies = [ "serde", "serde_json", @@ -3998,7 +3998,7 @@ dependencies = [ [[package]] name = "tw-observe" -version = "0.93.0" +version = "0.94.0" dependencies = [ "tokio", "tracing", @@ -4007,7 +4007,7 @@ dependencies = [ [[package]] name = "tw-pack" -version = "0.93.0" +version = "0.94.0" dependencies = [ "age", "blake3", @@ -4031,7 +4031,7 @@ dependencies = [ [[package]] name = "tw-plugin" -version = "0.93.0" +version = "0.94.0" dependencies = [ "libc", "rand 0.10.2", @@ -4047,7 +4047,7 @@ dependencies = [ [[package]] name = "tw-pricing" -version = "0.93.0" +version = "0.94.0" dependencies = [ "arc-swap", "flate2", @@ -4060,14 +4060,14 @@ dependencies = [ [[package]] name = "tw-secret" -version = "0.93.0" +version = "0.94.0" dependencies = [ "thiserror 2.0.20", ] [[package]] name = "tw-store" -version = "0.93.0" +version = "0.94.0" dependencies = [ "blake3", "bytes", @@ -4087,7 +4087,7 @@ dependencies = [ [[package]] name = "tw-sync" -version = "0.93.0" +version = "0.94.0" dependencies = [ "age", "axum", @@ -4115,7 +4115,7 @@ dependencies = [ [[package]] name = "tw-types" -version = "0.93.0" +version = "0.94.0" dependencies = [ "serde", "serde_json", @@ -4124,7 +4124,7 @@ dependencies = [ [[package]] name = "tw-watch" -version = "0.93.0" +version = "0.94.0" dependencies = [ "libc", "notify", @@ -4135,7 +4135,7 @@ dependencies = [ [[package]] name = "tw-yaml" -version = "0.93.0" +version = "0.94.0" dependencies = [ "saphyr-parser", "serde_yaml_ng", @@ -4145,7 +4145,7 @@ dependencies = [ [[package]] name = "twcore" -version = "0.93.0" +version = "0.94.0" dependencies = [ "anyhow", "axum", diff --git a/Cargo.toml b/Cargo.toml index c920c35d..1369bd05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,7 +61,7 @@ categories = ["network-programming", "web-programming::http-server"] # 二进制走 CalVer,crate 走 SemVer —— 两者是两回事,卖给不同的人。 # 这里是 crate 的版本。 -version = "0.93.0" +version = "0.94.0" [workspace.dependencies] # ── 内部 crate ─────────────────────────────────────────── diff --git a/release-notes/0.94.0.md b/release-notes/0.94.0.md new file mode 100644 index 00000000..625b264e --- /dev/null +++ b/release-notes/0.94.0.md @@ -0,0 +1,3 @@ +Codex can now discover reasoning effort choices, defaults, context windows, and input modalities reported by ordinary upstream model APIs, including DeepSeek. Model aliases inherit the capabilities of their actual routing targets; aliases with multiple targets expose only reasoning levels shared by every candidate. + +Core reads the capabilities supplied by the upstream rather than maintaining a model-name table. Relays must preserve the upstream metadata for it to appear in Codex. Manual reasoning overrides remain effective. ThinkWatch Lite installations need a desktop release that bundles this Core version to receive the fix.