Skip to content

Commit 225af45

Browse files
committed
codex: rebase custom model aliases onto main
1 parent c25b996 commit 225af45

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

codex-rs/core/src/codex_tests.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ use opentelemetry::trace::TraceContextExt;
6161
use opentelemetry::trace::TraceId;
6262
use opentelemetry::trace::TracerProvider as _;
6363
use opentelemetry_sdk::trace::SdkTracerProvider;
64+
use std::collections::HashMap;
6465
use std::path::Path;
6566
use std::time::Duration;
6667
use tokio::time::sleep;
@@ -1892,6 +1893,7 @@ async fn session_new_fails_when_zsh_fork_enabled_without_zsh_path() {
18921893
config.codex_home.clone(),
18931894
auth_manager.clone(),
18941895
None,
1896+
HashMap::new(),
18951897
CollaborationModesConfig::default(),
18961898
));
18971899
let model = ModelsManager::get_model_offline_for_tests(config.model.as_deref());
@@ -1978,6 +1980,7 @@ pub(crate) async fn make_session_and_context() -> (Session, TurnContext) {
19781980
config.codex_home.clone(),
19791981
auth_manager.clone(),
19801982
None,
1983+
HashMap::new(),
19811984
CollaborationModesConfig::default(),
19821985
));
19831986
let agent_control = AgentControl::default();
@@ -2383,6 +2386,7 @@ pub(crate) async fn make_session_and_context_with_dynamic_tools_and_rx(
23832386
config.codex_home.clone(),
23842387
auth_manager.clone(),
23852388
None,
2389+
HashMap::new(),
23862390
CollaborationModesConfig::default(),
23872391
));
23882392
let agent_control = AgentControl::default();

codex-rs/core/src/config/config_tests.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,6 +2647,7 @@ fn test_precedence_fixture_with_o3_profile() -> std::io::Result<()> {
26472647
review_model: None,
26482648
model_context_window: None,
26492649
model_auto_compact_token_limit: None,
2650+
custom_models: HashMap::new(),
26502651
service_tier: None,
26512652
model_provider_id: "openai".to_string(),
26522653
model_provider: fixture.openai_provider.clone(),
@@ -2776,6 +2777,7 @@ fn test_precedence_fixture_with_gpt3_profile() -> std::io::Result<()> {
27762777
review_model: None,
27772778
model_context_window: None,
27782779
model_auto_compact_token_limit: None,
2780+
custom_models: HashMap::new(),
27792781
service_tier: None,
27802782
model_provider_id: "openai-custom".to_string(),
27812783
model_provider: fixture.openai_custom_provider.clone(),
@@ -2903,6 +2905,7 @@ fn test_precedence_fixture_with_zdr_profile() -> std::io::Result<()> {
29032905
review_model: None,
29042906
model_context_window: None,
29052907
model_auto_compact_token_limit: None,
2908+
custom_models: HashMap::new(),
29062909
service_tier: None,
29072910
model_provider_id: "openai".to_string(),
29082911
model_provider: fixture.openai_provider.clone(),
@@ -3016,6 +3019,7 @@ fn test_precedence_fixture_with_gpt5_profile() -> std::io::Result<()> {
30163019
review_model: None,
30173020
model_context_window: None,
30183021
model_auto_compact_token_limit: None,
3022+
custom_models: HashMap::new(),
30193023
service_tier: None,
30203024
model_provider_id: "openai".to_string(),
30213025
model_provider: fixture.openai_provider.clone(),

codex-rs/core/src/config/mod.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ pub(crate) fn test_config() -> Config {
150150
.expect("load default test config")
151151
}
152152

153+
impl Config {
154+
pub(crate) fn custom_model_alias(&self, alias: &str) -> Option<&CustomModelConfig> {
155+
self.custom_models.get(alias)
156+
}
157+
}
158+
153159
/// Application configuration loaded from disk and merged with overrides.
154160
#[derive(Debug, Clone, PartialEq)]
155161
pub struct Permissions {
@@ -343,6 +349,9 @@ pub struct Config {
343349
/// Combined provider map (defaults merged with user-defined overrides).
344350
pub model_providers: HashMap<String, ModelProviderInfo>,
345351

352+
/// User-defined model aliases shown in the picker.
353+
pub custom_models: HashMap<String, CustomModelConfig>,
354+
346355
/// Maximum number of bytes to include from an AGENTS.md project doc file.
347356
pub project_doc_max_bytes: usize,
348357

@@ -1117,6 +1126,10 @@ pub struct ConfigToml {
11171126
#[serde(default)]
11181127
pub model_providers: HashMap<String, ModelProviderInfo>,
11191128

1129+
/// User-defined model aliases that can override model context settings.
1130+
#[serde(default)]
1131+
pub custom_models: Vec<CustomModelToml>,
1132+
11201133
/// Maximum number of bytes to include from an AGENTS.md project doc file.
11211134
pub project_doc_max_bytes: Option<usize>,
11221135

@@ -1351,6 +1364,29 @@ pub struct RealtimeAudioToml {
13511364
pub speaker: Option<String>,
13521365
}
13531366

1367+
#[derive(Debug, Clone, PartialEq, Eq)]
1368+
pub struct CustomModelConfig {
1369+
/// Provider-facing model slug used on API requests.
1370+
pub model: String,
1371+
/// Optional context window override applied when this alias is selected.
1372+
pub model_context_window: Option<i64>,
1373+
/// Optional auto-compaction token limit override applied when this alias is selected.
1374+
pub model_auto_compact_token_limit: Option<i64>,
1375+
}
1376+
1377+
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq, JsonSchema)]
1378+
#[schemars(deny_unknown_fields)]
1379+
pub struct CustomModelToml {
1380+
/// User-facing alias shown in the model picker.
1381+
pub name: String,
1382+
/// Provider-facing model slug used on API requests.
1383+
pub model: String,
1384+
/// Optional context window override applied when this alias is selected.
1385+
pub model_context_window: Option<i64>,
1386+
/// Optional auto-compaction token limit override applied when this alias is selected.
1387+
pub model_auto_compact_token_limit: Option<i64>,
1388+
}
1389+
13541390
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, JsonSchema)]
13551391
#[schemars(deny_unknown_fields)]
13561392
pub struct ToolsToml {
@@ -1842,6 +1878,24 @@ impl Config {
18421878
for (key, provider) in cfg.model_providers.into_iter() {
18431879
model_providers.entry(key).or_insert(provider);
18441880
}
1881+
let mut custom_models = HashMap::new();
1882+
for custom in cfg.custom_models {
1883+
let alias = custom.name;
1884+
if custom_models.contains_key(&alias) {
1885+
return Err(std::io::Error::new(
1886+
ErrorKind::InvalidInput,
1887+
format!("duplicate custom model alias: {alias}"),
1888+
));
1889+
}
1890+
custom_models.insert(
1891+
alias,
1892+
CustomModelConfig {
1893+
model: custom.model,
1894+
model_context_window: custom.model_context_window,
1895+
model_auto_compact_token_limit: custom.model_auto_compact_token_limit,
1896+
},
1897+
);
1898+
}
18451899

18461900
let model_provider_id = model_provider
18471901
.or(config_profile.model_provider)
@@ -2157,6 +2211,7 @@ impl Config {
21572211
mcp_oauth_callback_port: cfg.mcp_oauth_callback_port,
21582212
mcp_oauth_callback_url: cfg.mcp_oauth_callback_url.clone(),
21592213
model_providers,
2214+
custom_models,
21602215
project_doc_max_bytes: cfg.project_doc_max_bytes.unwrap_or(PROJECT_DOC_MAX_BYTES),
21612216
project_doc_fallback_filenames: cfg
21622217
.project_doc_fallback_filenames

0 commit comments

Comments
 (0)