-
Notifications
You must be signed in to change notification settings - Fork 6.7k
make model optional in config
#7769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+838
−429
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| use chrono::DateTime; | ||
| use chrono::Utc; | ||
| use codex_core::openai_models::model_presets::all_model_presets; | ||
| use codex_protocol::openai_models::ClientVersion; | ||
| use codex_protocol::openai_models::ConfigShellToolType; | ||
| use codex_protocol::openai_models::ModelInfo; | ||
| use codex_protocol::openai_models::ModelPreset; | ||
| use codex_protocol::openai_models::ModelVisibility; | ||
| use serde_json::json; | ||
| use std::path::Path; | ||
|
|
||
| /// Convert a ModelPreset to ModelInfo for cache storage. | ||
| fn preset_to_info(preset: &ModelPreset, priority: i32) -> ModelInfo { | ||
| ModelInfo { | ||
| slug: preset.id.clone(), | ||
| display_name: preset.display_name.clone(), | ||
| description: Some(preset.description.clone()), | ||
| default_reasoning_level: preset.default_reasoning_effort, | ||
| supported_reasoning_levels: preset.supported_reasoning_efforts.clone(), | ||
| shell_type: ConfigShellToolType::ShellCommand, | ||
| visibility: if preset.show_in_picker { | ||
| ModelVisibility::List | ||
| } else { | ||
| ModelVisibility::Hide | ||
| }, | ||
| minimal_client_version: ClientVersion(0, 1, 0), | ||
| supported_in_api: true, | ||
| priority, | ||
| upgrade: preset.upgrade.as_ref().map(|u| u.id.clone()), | ||
| base_instructions: None, | ||
| } | ||
| } | ||
|
|
||
| /// Write a models_cache.json file to the codex home directory. | ||
| /// This prevents ModelsManager from making network requests to refresh models. | ||
| /// The cache will be treated as fresh (within TTL) and used instead of fetching from the network. | ||
| /// Uses the built-in model presets from ModelsManager, converted to ModelInfo format. | ||
| pub fn write_models_cache(codex_home: &Path) -> std::io::Result<()> { | ||
| // Get all presets and filter for show_in_picker (same as builtin_model_presets does) | ||
| let presets: Vec<&ModelPreset> = all_model_presets() | ||
| .iter() | ||
| .filter(|preset| preset.show_in_picker) | ||
| .collect(); | ||
| // Convert presets to ModelInfo, assigning priorities (higher = earlier in list) | ||
| // Priority is used for sorting, so first model gets highest priority | ||
| let models: Vec<ModelInfo> = presets | ||
| .iter() | ||
| .enumerate() | ||
| .map(|(idx, preset)| { | ||
| // Higher priority = earlier in list, so reverse the index | ||
| let priority = (presets.len() - idx) as i32; | ||
| preset_to_info(preset, priority) | ||
| }) | ||
| .collect(); | ||
|
|
||
| write_models_cache_with_models(codex_home, models) | ||
| } | ||
|
|
||
| /// Write a models_cache.json file with specific models. | ||
| /// Useful when tests need specific models to be available. | ||
| pub fn write_models_cache_with_models( | ||
| codex_home: &Path, | ||
| models: Vec<ModelInfo>, | ||
| ) -> std::io::Result<()> { | ||
| let cache_path = codex_home.join("models_cache.json"); | ||
| // DateTime<Utc> serializes to RFC3339 format by default with serde | ||
| let fetched_at: DateTime<Utc> = Utc::now(); | ||
| let cache = json!({ | ||
| "fetched_at": fetched_at, | ||
| "etag": null, | ||
| "models": models | ||
| }); | ||
| std::fs::write(cache_path, serde_json::to_string_pretty(&cache)?) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -181,10 +181,15 @@ impl Codex { | |
| let exec_policy = Arc::new(RwLock::new(exec_policy)); | ||
|
|
||
| let config = Arc::new(config); | ||
|
|
||
| if config.features.enabled(Feature::RemoteModels) | ||
| && let Err(err) = models_manager.refresh_available_models(&config).await | ||
| { | ||
| error!("failed to refresh available models: {err:?}"); | ||
| } | ||
| let model = models_manager.get_model(&config.model, &config).await; | ||
| let session_configuration = SessionConfiguration { | ||
| provider: config.model_provider.clone(), | ||
| model: config.model.clone(), | ||
| model: model.clone(), | ||
| model_reasoning_effort: config.model_reasoning_effort, | ||
| model_reasoning_summary: config.model_reasoning_summary, | ||
| developer_instructions: config.developer_instructions.clone(), | ||
|
|
@@ -398,10 +403,11 @@ pub(crate) struct SessionSettingsUpdate { | |
| } | ||
|
|
||
| impl Session { | ||
| /// Don't expand the number of mutated arguments on config. We are in the process of getting rid of it. | ||
| fn build_per_turn_config(session_configuration: &SessionConfiguration) -> Config { | ||
| // todo(aibrahim): store this state somewhere else so we don't need to mut config | ||
| let config = session_configuration.original_config_do_not_use.clone(); | ||
| let mut per_turn_config = (*config).clone(); | ||
| per_turn_config.model = session_configuration.model.clone(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are we confident nobody is reading this?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Supposedly yeah. I replaced it with turn context |
||
| per_turn_config.model_reasoning_effort = session_configuration.model_reasoning_effort; | ||
| per_turn_config.model_reasoning_summary = session_configuration.model_reasoning_summary; | ||
| per_turn_config.features = config.features.clone(); | ||
|
|
@@ -421,7 +427,7 @@ impl Session { | |
| ) -> TurnContext { | ||
| let otel_event_manager = otel_event_manager.clone().with_model( | ||
| session_configuration.model.as_str(), | ||
| model_family.slug.as_str(), | ||
| model_family.get_model_slug(), | ||
| ); | ||
|
|
||
| let per_turn_config = Arc::new(per_turn_config); | ||
|
|
@@ -544,14 +550,11 @@ impl Session { | |
| }); | ||
| } | ||
|
|
||
| let model_family = models_manager | ||
| .construct_model_family(&config.model, &config) | ||
| .await; | ||
| // todo(aibrahim): why are we passing model here while it can change? | ||
| let otel_event_manager = OtelEventManager::new( | ||
| conversation_id, | ||
| config.model.as_str(), | ||
| model_family.slug.as_str(), | ||
| session_configuration.model.as_str(), | ||
| session_configuration.model.as_str(), | ||
| auth_manager.auth().and_then(|a| a.get_account_id()), | ||
| auth_manager.auth().and_then(|a| a.get_account_email()), | ||
| auth_manager.auth().map(|a| a.mode), | ||
|
|
@@ -780,7 +783,7 @@ impl Session { | |
| let model_family = self | ||
| .services | ||
| .models_manager | ||
| .construct_model_family(&per_turn_config.model, &per_turn_config) | ||
| .construct_model_family(session_configuration.model.as_str(), &per_turn_config) | ||
| .await; | ||
| let mut turn_context: TurnContext = Self::make_turn_context( | ||
| Some(Arc::clone(&self.services.auth_manager)), | ||
|
|
@@ -1444,16 +1447,6 @@ async fn submission_loop(sess: Arc<Session>, config: Arc<Config>, rx_sub: Receiv | |
| let mut previous_context: Option<Arc<TurnContext>> = | ||
| Some(sess.new_turn(SessionSettingsUpdate::default()).await); | ||
|
|
||
| if config.features.enabled(Feature::RemoteModels) | ||
| && let Err(err) = sess | ||
| .services | ||
| .models_manager | ||
| .refresh_available_models(&config.model_provider) | ||
| .await | ||
| { | ||
| error!("failed to refresh available models: {err}"); | ||
| } | ||
|
|
||
| // To break out of this loop, send Op::Shutdown. | ||
| while let Ok(sub) = rx_sub.recv().await { | ||
| debug!(?sub, "Submission"); | ||
|
|
@@ -1925,7 +1918,6 @@ async fn spawn_review_thread( | |
|
|
||
| // Build per‑turn client with the requested model/family. | ||
| let mut per_turn_config = (*config).clone(); | ||
| per_turn_config.model = model.clone(); | ||
| per_turn_config.model_reasoning_effort = Some(ReasoningEffortConfig::Low); | ||
| per_turn_config.model_reasoning_summary = ReasoningSummaryConfig::Detailed; | ||
| per_turn_config.features = review_features.clone(); | ||
|
|
@@ -1934,7 +1926,7 @@ async fn spawn_review_thread( | |
| .client | ||
| .get_otel_event_manager() | ||
| .with_model( | ||
| per_turn_config.model.as_str(), | ||
| config.review_model.as_str(), | ||
| review_model_family.slug.as_str(), | ||
| ); | ||
|
|
||
|
|
@@ -2555,9 +2547,10 @@ mod tests { | |
| ) | ||
| .expect("load default test config"); | ||
| let config = Arc::new(config); | ||
| let model = ModelsManager::get_model_offline(config.model.as_deref()); | ||
| let session_configuration = SessionConfiguration { | ||
| provider: config.model_provider.clone(), | ||
| model: config.model.clone(), | ||
| model, | ||
| model_reasoning_effort: config.model_reasoning_effort, | ||
| model_reasoning_summary: config.model_reasoning_summary, | ||
| developer_instructions: config.developer_instructions.clone(), | ||
|
|
@@ -2626,9 +2619,10 @@ mod tests { | |
| ) | ||
| .expect("load default test config"); | ||
| let config = Arc::new(config); | ||
| let model = ModelsManager::get_model_offline(config.model.as_deref()); | ||
| let session_configuration = SessionConfiguration { | ||
| provider: config.model_provider.clone(), | ||
| model: config.model.clone(), | ||
| model, | ||
| model_reasoning_effort: config.model_reasoning_effort, | ||
| model_reasoning_summary: config.model_reasoning_summary, | ||
| developer_instructions: config.developer_instructions.clone(), | ||
|
|
@@ -2803,7 +2797,7 @@ mod tests { | |
| ) -> OtelEventManager { | ||
| OtelEventManager::new( | ||
| conversation_id, | ||
| config.model.as_str(), | ||
| ModelsManager::get_model_offline(config.model.as_deref()).as_str(), | ||
| model_family.slug.as_str(), | ||
| None, | ||
| Some("[email protected]".to_string()), | ||
|
|
@@ -2827,9 +2821,10 @@ mod tests { | |
| let auth_manager = | ||
| AuthManager::from_auth_for_testing(CodexAuth::from_api_key("Test API Key")); | ||
| let models_manager = Arc::new(ModelsManager::new(auth_manager.clone())); | ||
| let model = ModelsManager::get_model_offline(config.model.as_deref()); | ||
| let session_configuration = SessionConfiguration { | ||
| provider: config.model_provider.clone(), | ||
| model: config.model.clone(), | ||
| model, | ||
| model_reasoning_effort: config.model_reasoning_effort, | ||
| model_reasoning_summary: config.model_reasoning_summary, | ||
| developer_instructions: config.developer_instructions.clone(), | ||
|
|
@@ -2844,8 +2839,10 @@ mod tests { | |
| session_source: SessionSource::Exec, | ||
| }; | ||
| let per_turn_config = Session::build_per_turn_config(&session_configuration); | ||
| let model_family = | ||
| ModelsManager::construct_model_family_offline(&per_turn_config.model, &per_turn_config); | ||
| let model_family = ModelsManager::construct_model_family_offline( | ||
| session_configuration.model.as_str(), | ||
| &per_turn_config, | ||
| ); | ||
| let otel_event_manager = | ||
| otel_event_manager(conversation_id, config.as_ref(), &model_family); | ||
|
|
||
|
|
@@ -2909,9 +2906,10 @@ mod tests { | |
| let auth_manager = | ||
| AuthManager::from_auth_for_testing(CodexAuth::from_api_key("Test API Key")); | ||
| let models_manager = Arc::new(ModelsManager::new(auth_manager.clone())); | ||
| let model = ModelsManager::get_model_offline(config.model.as_deref()); | ||
| let session_configuration = SessionConfiguration { | ||
| provider: config.model_provider.clone(), | ||
| model: config.model.clone(), | ||
| model, | ||
| model_reasoning_effort: config.model_reasoning_effort, | ||
| model_reasoning_summary: config.model_reasoning_summary, | ||
| developer_instructions: config.developer_instructions.clone(), | ||
|
|
@@ -2926,8 +2924,10 @@ mod tests { | |
| session_source: SessionSource::Exec, | ||
| }; | ||
| let per_turn_config = Session::build_per_turn_config(&session_configuration); | ||
| let model_family = | ||
| ModelsManager::construct_model_family_offline(&per_turn_config.model, &per_turn_config); | ||
| let model_family = ModelsManager::construct_model_family_offline( | ||
| session_configuration.model.as_str(), | ||
| &per_turn_config, | ||
| ); | ||
| let otel_event_manager = | ||
| otel_event_manager(conversation_id, config.as_ref(), &model_family); | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.