Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ───────────────────────────────────────────
Expand Down
60 changes: 38 additions & 22 deletions crates/tw-gateway/src/codex_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -20,6 +20,8 @@ use tw_types::msg;
use crate::error::GatewayError;
use crate::state::{AppState, Runtime};

mod metadata;

const MAX_BYTES: usize = 1024 * 1024;
const MAX_CACHED: usize = 32;
const FETCH_TIMEOUT: Duration = Duration::from_secs(8);
Expand Down Expand Up @@ -151,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))
}
Expand Down Expand Up @@ -416,11 +426,9 @@ fn project(
.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();
Expand All @@ -440,14 +448,19 @@ fn project(
.or_else(|| d["context_window"].as_u64())
}),
));
reasoning.push(pick(
let enabled = pick(
spec.reasoning,
definition.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 {
*definition
});
images.push(pick(
spec.image_input,
definition
Expand All @@ -472,10 +485,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)
)
});
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 {
Expand Down
Loading
Loading