Files
codex/codex-rs/tui/src/local_settings.rs
T
Benjamin Carlsson 5c865592f4 Remember the TUI's Cloud environment choice per project (#65033)
## Why

Selecting a saved Cloud environment for a draft did not set a default
for future conversations in the same project.

## What changed

- Save the environment selected for a Cloud draft in the local user
  configuration under `tui.default_cloud_environments`, keyed by project
  and local user identity. Selecting Default clears the preference.
- Reuse the preference at startup, when creating a new Cloud conversation,
  and when switching to Cloud. Use the normal Cloud environment selection
  when the saved environment is absent from the catalog or the catalog
  cannot be loaded.
- Use the v2 environment catalog, filter for eligible saved environments,
  and deduplicate entries across personal and workspace results.

## Testing

Cover preference persistence and clearing, project scoping, reuse after a
config reload, fallback when the catalog is unavailable, and catalog
pagination, filtering, and deduplication.

GitOrigin-RevId: bc4f66842c5c83dc75c5b10840fd596feedd44df
2026-12-01 16:51:26 +00:00

208 lines
8.4 KiB
Rust

//! Client-owned preferences and persistence paths, independent of active server settings.
//!
//! Startup and reloads read preferences from the client configuration layers.
//! Server thread responses must never refresh these values; live preference changes belong here.
//! Audio preferences exclude project layers so thread cwd cannot route local capture.
//! Effective animations also respect the TUI host's launch-time accessibility preference.
//! The selected transcript ownership and alternate-screen restrictions survive local reloads.
use crate::legacy_core::config::Config;
use crate::legacy_core::config::ConfigTomlLoadResult;
use crate::legacy_core::config::TerminalResizeReflowConfig;
use crate::legacy_core::config::TerminalResizeReflowMaxRows;
use crate::transcript_mode::TranscriptMode;
use codex_config::ConfigLayerStack;
use codex_config::types::CopyOnSelect;
use codex_config::types::History;
use codex_config::types::Notice;
use codex_config::types::Tui;
use codex_utils_absolute_path::AbsolutePathBuf;
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct LocalSettings {
pub(crate) audio: Result<codex_config::config_toml::RealtimeAudioToml, String>,
pub(crate) tui: Tui,
pub(crate) transcript_mode: TranscriptMode,
pub(crate) history: History,
pub(crate) notices: Notice,
pub(crate) custom_notification_filters: bool,
pub(crate) codex_home: AbsolutePathBuf,
pub(crate) user_config_path: AbsolutePathBuf,
}
impl LocalSettings {
pub(crate) fn workspace_enabled(&self, config: &Config) -> bool {
self.transcript_mode.is_owned()
&& config
.features
.enabled(codex_features::Feature::TuiWorkspace)
}
/// Read client preferences without resolving model providers or execution permissions.
pub(crate) fn from_bootstrap(
bootstrap: &ConfigTomlLoadResult,
codex_home: AbsolutePathBuf,
) -> Result<Self, toml::de::Error> {
let config = &bootstrap.config_toml;
let mut tui = match &config.tui {
Some(tui) => tui.clone(),
None => toml::Value::Table(Default::default()).try_into()?,
};
tui.disable_paste_burst = Some(
tui.disable_paste_burst
.or(config.disable_paste_burst)
.unwrap_or(false),
);
tui.session_picker_view = Some(tui.session_picker_view.unwrap_or_default());
tui.screen_reader_detection_done = None;
let mut settings = Self {
audio: Ok(Default::default()),
transcript_mode: TranscriptMode::resolve(
tui.fullscreen_transcript,
tui.alternate_screen != codex_config::types::AltScreenMode::Never,
),
tui,
history: config.history.clone().unwrap_or_default(),
notices: config.notice.clone().unwrap_or_default(),
custom_notification_filters: custom_notification_filters(&bootstrap.config_layer_stack),
user_config_path: bootstrap
.config_layer_stack
.get_user_config_file()
.cloned()
.unwrap_or_else(|| codex_home.join("config.toml")),
codex_home,
};
settings.apply_host_preferences(
&bootstrap.config_layer_stack,
crate::system_motion::mode(),
crate::screen_reader::animation_default(),
);
Ok(settings)
}
/// Build client preferences from configuration layers already in memory.
pub(crate) fn from_layers(
codex_home: &AbsolutePathBuf,
layers: &ConfigLayerStack,
) -> std::io::Result<Self> {
let bootstrap = ConfigTomlLoadResult {
config_toml: crate::legacy_core::config::deserialize_config_toml_with_base(
layers.effective_config(),
codex_home.as_path(),
)?,
config_layer_stack: layers.clone(),
};
Self::from_bootstrap(&bootstrap, codex_home.clone()).map_err(std::io::Error::other)
}
fn apply_host_preferences(
&mut self,
layers: &ConfigLayerStack,
system_motion: crate::motion::MotionMode,
screen_reader_default: crate::motion::MotionMode,
) {
self.tui.default_hosts = layers
.effective_user_config()
.and_then(|config| {
config
.get("tui")
.and_then(|tui| tui.get("default_hosts"))
.and_then(|value| value.clone().try_into().ok())
})
.unwrap_or_default();
self.tui.default_cloud_environments = layers
.effective_user_config()
.and_then(|config| {
config
.get("tui")
.and_then(|tui| tui.get("default_cloud_environments"))
.and_then(|value| value.clone().try_into().ok())
})
.unwrap_or_default();
if screen_reader_default == crate::motion::MotionMode::Reduced {
// Consult the current layers so preferences edited after startup still win.
self.tui.animations &= layers
.effective_config()
.get("tui")
.and_then(|tui| tui.get("animations"))
.is_some();
}
self.tui.animations &= system_motion == crate::motion::MotionMode::Animated;
let mut audio = toml::Value::Table(Default::default());
for layer in layers.layers_low_to_high() {
if !matches!(layer.name, codex_config::ConfigLayerSource::Project { .. })
&& let Some(value) = layer.config.get("audio")
{
codex_config::merge_toml_values(&mut audio, value);
}
}
self.audio = audio
.try_into()
.map_err(|error: toml::de::Error| format!("Invalid machine audio settings: {error}"));
}
}
fn custom_notification_filters(layers: &ConfigLayerStack) -> bool {
layers.effective_user_config().is_some_and(|config| {
config
.get("tui")
.and_then(|tui| tui.get("notifications"))
.is_some_and(toml::Value::is_array)
})
}
impl LocalSettings {
/// Adopt the screen selected before first paint, including command-line restrictions.
pub(crate) fn for_tui(mut self, tui: &crate::tui::Tui) -> Self {
self.transcript_mode = tui.transcript_mode();
if !tui.is_alt_screen_enabled() {
self.tui.alternate_screen = codex_config::types::AltScreenMode::Never;
} else if self.tui.alternate_screen == codex_config::types::AltScreenMode::Never {
self.tui.alternate_screen = codex_config::types::AltScreenMode::Auto;
}
self
}
/// Refresh editable preferences without changing this launch's terminal ownership.
pub(crate) fn reloaded(&self, layers: &ConfigLayerStack) -> std::io::Result<Self> {
let mut settings = Self::from_layers(&self.codex_home, layers)?;
settings.transcript_mode = self.transcript_mode;
settings.tui.alternate_screen = self.tui.alternate_screen;
// Generic reloads do not update these values in the live UI. The config panel updates
// both copies when the user changes one.
settings.tui.animations = self.tui.animations;
settings.tui.auto_recap = self.tui.auto_recap;
settings.tui.effects.starfield = self.tui.effects.starfield;
settings.tui.disable_paste_burst = self.tui.disable_paste_burst;
settings.tui.show_tooltips = self.tui.show_tooltips;
Ok(settings)
}
/// Copy on release unless the default copy shortcut reaches the application.
pub(crate) fn copy_on_select(&self) -> bool {
self.copy_on_select_with(crate::terminal_capabilities::forwards_copy_shortcut())
}
fn copy_on_select_with(&self, forwards_copy_shortcut: bool) -> bool {
match self.tui.copy_on_select {
CopyOnSelect::Always => true,
CopyOnSelect::Never => false,
CopyOnSelect::Auto => !forwards_copy_shortcut,
}
}
pub(crate) fn terminal_resize_reflow(&self) -> TerminalResizeReflowConfig {
TerminalResizeReflowConfig {
max_rows: match self.tui.terminal_resize_reflow_max_rows {
None => TerminalResizeReflowMaxRows::Auto,
Some(0) => TerminalResizeReflowMaxRows::Disabled,
Some(rows) => TerminalResizeReflowMaxRows::Limit(rows),
},
}
}
}
#[cfg(test)]
#[path = "local_settings_tests.rs"]
mod tests;