mirror of
https://github.com/openai/codex.git
synced 2026-11-23 23:10:47 +00:00
codex: keep selected tier command clearable
This commit is contained in:
@@ -36,15 +36,26 @@ pub(crate) struct ServiceTierCommand {
|
||||
pub(crate) description: String,
|
||||
}
|
||||
|
||||
fn service_tier_command_from_model_tier(
|
||||
service_tier: &ModelServiceTier,
|
||||
command: String,
|
||||
) -> ServiceTierCommand {
|
||||
ServiceTierCommand {
|
||||
service_tier: service_tier.id.clone(),
|
||||
command,
|
||||
name: service_tier.name.clone(),
|
||||
description: service_tier.description.clone(),
|
||||
impl From<(&ModelServiceTier, String)> for ServiceTierCommand {
|
||||
fn from((model_tier, command): (&ModelServiceTier, String)) -> Self {
|
||||
Self {
|
||||
service_tier: model_tier.id.clone(),
|
||||
command,
|
||||
name: model_tier.name.clone(),
|
||||
description: model_tier.description.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(ServiceTier, String)> for ServiceTierCommand {
|
||||
fn from((service_tier, command): (ServiceTier, String)) -> Self {
|
||||
let name = service_tier.to_string();
|
||||
Self {
|
||||
service_tier,
|
||||
command,
|
||||
name,
|
||||
description: "Clear the selected service tier override.".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -196,11 +207,23 @@ pub(crate) fn service_tier_commands_for_model(model: &ModelPreset) -> Vec<Servic
|
||||
continue;
|
||||
};
|
||||
reserved_names.insert(command.clone());
|
||||
commands.push(service_tier_command_from_model_tier(service_tier, command));
|
||||
commands.push((service_tier, command).into());
|
||||
}
|
||||
commands
|
||||
}
|
||||
|
||||
pub(crate) fn service_tier_command_for_unlisted_selection(
|
||||
service_tier: ServiceTier,
|
||||
existing_commands: &[ServiceTierCommand],
|
||||
) -> Option<ServiceTierCommand> {
|
||||
let reserved_names = existing_commands
|
||||
.iter()
|
||||
.map(|command| command.command.clone())
|
||||
.collect::<HashSet<_>>();
|
||||
let command = command_token_for_service_tier_id(&service_tier, &reserved_names)?;
|
||||
Some((service_tier, command).into())
|
||||
}
|
||||
|
||||
fn command_token_for_service_tier(
|
||||
service_tier: &ModelServiceTier,
|
||||
reserved_names: &HashSet<String>,
|
||||
@@ -214,6 +237,14 @@ fn command_token_for_service_tier(
|
||||
.find(|candidate| !command_token_conflicts(candidate, reserved_names))
|
||||
}
|
||||
|
||||
fn command_token_for_service_tier_id(
|
||||
service_tier: &ServiceTier,
|
||||
reserved_names: &HashSet<String>,
|
||||
) -> Option<String> {
|
||||
let candidate = service_tier.to_string();
|
||||
(!command_token_conflicts(&candidate, reserved_names)).then_some(candidate)
|
||||
}
|
||||
|
||||
fn command_token_conflicts(candidate: &str, reserved_names: &HashSet<String>) -> bool {
|
||||
candidate.is_empty()
|
||||
|| reserved_names.contains(candidate)
|
||||
@@ -455,4 +486,27 @@ mod tests {
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unlisted_service_tier_command_uses_selected_tier_id() {
|
||||
let command = service_tier_command_for_unlisted_selection(
|
||||
ServiceTier::from("priority"),
|
||||
&[ServiceTierCommand {
|
||||
service_tier: ServiceTier::from("express"),
|
||||
command: "express".to_string(),
|
||||
name: "Express".to_string(),
|
||||
description: "Express tier".to_string(),
|
||||
}],
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
command,
|
||||
Some(ServiceTierCommand {
|
||||
service_tier: ServiceTier::from("priority"),
|
||||
command: "priority".to_string(),
|
||||
name: "priority".to_string(),
|
||||
description: "Clear the selected service tier override.".to_string(),
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9214,6 +9214,7 @@ impl ChatWidget {
|
||||
pub(crate) fn set_service_tier(&mut self, service_tier: Option<ServiceTier>) {
|
||||
self.config.service_tier = service_tier.clone();
|
||||
self.effective_service_tier = service_tier;
|
||||
self.sync_service_tier_commands();
|
||||
}
|
||||
|
||||
pub(crate) fn current_service_tier(&self) -> Option<ServiceTier> {
|
||||
|
||||
@@ -6,6 +6,7 @@ use codex_protocol::openai_models::model_supports_service_tier;
|
||||
|
||||
use super::ChatWidget;
|
||||
use crate::bottom_pane::slash_commands::ServiceTierCommand;
|
||||
use crate::bottom_pane::slash_commands::service_tier_command_for_unlisted_selection;
|
||||
use crate::bottom_pane::slash_commands::service_tier_commands_for_model;
|
||||
|
||||
pub(super) fn model_preset(chat: &ChatWidget, model: &str) -> Option<ModelPreset> {
|
||||
@@ -16,9 +17,20 @@ pub(super) fn model_preset(chat: &ChatWidget, model: &str) -> Option<ModelPreset
|
||||
}
|
||||
|
||||
pub(crate) fn available_service_tier_commands(chat: &ChatWidget) -> Vec<ServiceTierCommand> {
|
||||
model_preset(chat, chat.current_model())
|
||||
let mut commands = model_preset(chat, chat.current_model())
|
||||
.map(|model| service_tier_commands_for_model(&model))
|
||||
.unwrap_or_default()
|
||||
.unwrap_or_default();
|
||||
|
||||
if let Some(service_tier) = chat.current_service_tier()
|
||||
&& !commands
|
||||
.iter()
|
||||
.any(|command| command.service_tier == service_tier)
|
||||
&& let Some(command) = service_tier_command_for_unlisted_selection(service_tier, &commands)
|
||||
{
|
||||
commands.push(command);
|
||||
}
|
||||
|
||||
commands
|
||||
}
|
||||
|
||||
pub(crate) fn service_tier_display_name(
|
||||
|
||||
@@ -1064,6 +1064,34 @@ async fn usage_error_slash_command_is_available_from_local_recall() {
|
||||
assert_eq!(recall_latest_after_clearing(&mut chat), "/fast maybe");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn selected_service_tier_can_be_cleared_when_model_lacks_tier_metadata() {
|
||||
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(Some("gpt-5.3-codex")).await;
|
||||
set_fast_mode_test_catalog(&mut chat);
|
||||
chat.set_feature_enabled(Feature::FastMode, /*enabled*/ true);
|
||||
chat.set_service_tier(Some(SERVICE_TIER_PRIORITY.into()));
|
||||
|
||||
submit_composer_text(&mut chat, "/priority off");
|
||||
|
||||
let events = std::iter::from_fn(|| rx.try_recv().ok()).collect::<Vec<_>>();
|
||||
let service_tier_events = events
|
||||
.iter()
|
||||
.filter_map(|event| match event {
|
||||
AppEvent::CodexOp(AppCommand::OverrideTurnContext { service_tier, .. }) => {
|
||||
Some(("override", service_tier.clone()))
|
||||
}
|
||||
AppEvent::PersistServiceTierSelection { service_tier } => {
|
||||
Some(("persist", Some(service_tier.clone())))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
service_tier_events,
|
||||
vec![("override", Some(None)), ("persist", Some(None))]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn unrecognized_slash_command_is_not_added_to_local_recall() {
|
||||
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
|
||||
|
||||
Reference in New Issue
Block a user