use rho_protocol::{
Applies, ConfigLayer, ConfigSetParams, SettingChange, SettingKey, SettingKind, SettingSpec,
SettingValue,
};
use super::{
config_row::ConfigRow,
daemon_settings::{DaemonSettings, SettingsNamespace},
text_input::TextInput,
App, ComposerMode, Entry, PickerItem, TuiSettings, UiPicker,
};
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum ConfigPage {
Root,
SessionModel,
SessionPermissionMode,
Category {
namespace: SettingsNamespace,
id: String,
},
Setting {
namespace: SettingsNamespace,
key: SettingKey,
},
AdvisorOn,
}
pub(super) enum SettingsUpdate {
Loaded(Box<DaemonSettings>),
Changed(Box<rho_protocol::ConfigChangedParams>),
ModelsChanged(Box<rho_protocol::ModelList>),
Unreadable(String),
}
impl App {
pub(super) fn open_daemon_config(&mut self) {
if let Some(picker) = self.config_page_picker(&ConfigPage::Root) {
self.input_ui.set_composer(ComposerMode::Picker(picker));
}
}
pub(super) fn config_page_picker(&self, page: &ConfigPage) -> Option<UiPicker> {
let mut picker = match page {
ConfigPage::Root => self.daemon_root_picker()?,
ConfigPage::SessionModel => self.daemon_model_picker()?,
ConfigPage::SessionPermissionMode => self.daemon_permission_picker()?,
ConfigPage::Category { namespace, id } => {
let settings = self.daemon_settings.as_ref()?;
UiPicker::config(
settings.category_label(*namespace, id),
settings.setting_rows(*namespace, id),
)
}
ConfigPage::Setting { namespace, key } => {
let settings = self.daemon_settings.as_ref()?;
let label = settings.spec(*namespace, key)?.label.clone();
UiPicker::config(label, settings.value_rows(*namespace, key)?)
}
ConfigPage::AdvisorOn => self.advisor_on_picker()?,
};
match page {
ConfigPage::Setting { .. } | ConfigPage::AdvisorOn => picker.select_badged(),
ConfigPage::Root
| ConfigPage::Category { .. }
| ConfigPage::SessionModel
| ConfigPage::SessionPermissionMode => {}
}
Some(picker.with_config_page(page.clone()))
}
fn daemon_root_picker(&self) -> Option<UiPicker> {
let options = self.daemon_options.as_ref()?;
let session = |label: &str, row: ConfigRow| PickerItem {
section: Some("Session".into()),
..PickerItem::new(label.into(), row.encode())
};
let mut items = vec![session("Model", ConfigRow::SessionModel)];
if options.reasoning.is_some() {
items.push(session("Reasoning", ConfigRow::SessionReasoning));
}
if options.fast_mode.is_some() {
items.push(session("Fast mode", ConfigRow::SessionFastMode));
}
items.push(session("Permission mode", ConfigRow::SessionPermissionMode));
if let Some(settings) = &self.daemon_settings {
items.extend(settings.category_rows());
}
let local = |label: &str, row: ConfigRow, detail: &str| PickerItem {
section: Some("This terminal".into()),
detail: Some(super::picker::PickerDetail::Text(detail.into())),
..PickerItem::new(label.into(), row.encode())
};
items.push(local(
"Theme",
ConfigRow::Theme,
"The color theme, saved in tui.toml.",
));
items.push(local(
"Clear prompt history",
ConfigRow::ClearPromptHistory,
"Delete every saved composer prompt, after a confirmation.",
));
Some(UiPicker::config("Config · saves automatically", items))
}
pub(super) fn refreshed_config_page(&mut self, mut stale: UiPicker) -> UiPicker {
let Some(mut fresh) = stale
.config_page()
.and_then(|page| self.config_page_picker(&page))
else {
return stale;
};
let selected = stale
.selected_item()
.map(|item| item.value.clone())
.unwrap_or_default();
Self::restore_picker_position(&mut fresh, &selected, std::mem::take(&mut stale.filter));
match stale.take_parent() {
Some(parent) => fresh.with_parent(parent),
None => fresh,
}
}
pub(super) fn reopen_config_root(&mut self, selected: &str) {
match self.config_page_picker(&ConfigPage::Root) {
Some(mut picker) => {
Self::restore_picker_position(&mut picker, selected, String::new());
self.input_ui.set_composer(ComposerMode::Picker(picker));
}
None => self.input_ui.set_composer(ComposerMode::Input),
}
}
pub(super) fn refresh_open_config_page(&mut self) {
let ComposerMode::Picker(picker) = self.input_ui.composer() else {
return;
};
let Some(page) = picker.config_page() else {
return;
};
if self.config_page_picker(&page).is_none() {
self.reopen_config_root("");
return;
}
let ComposerMode::Picker(stale) = self.input_ui.take_composer() else {
unreachable!("checked above");
};
let fresh = self.refreshed_config_page(stale);
self.input_ui.set_composer(ComposerMode::Picker(fresh));
}
pub(super) fn commit_daemon_config(&mut self, value: &str) -> anyhow::Result<()> {
let Some(row) = ConfigRow::parse(value) else {
anyhow::bail!("unknown settings row");
};
match row {
ConfigRow::SessionModel => self.open_config_page(ConfigPage::SessionModel),
ConfigRow::SessionPermissionMode => {
self.open_config_page(ConfigPage::SessionPermissionMode);
}
ConfigRow::Category { namespace, id } => {
self.open_config_page(ConfigPage::Category { namespace, id });
}
ConfigRow::SessionReasoning => return self.cycle_daemon_reasoning(),
ConfigRow::SessionFastMode => return self.set_daemon_fast(""),
ConfigRow::SetPermissionMode { mode } => {
self.close_value_page();
self.set_daemon_permission(mode)?;
}
ConfigRow::Theme => return self.open_theme_picker_from_config(),
ConfigRow::ClearPromptHistory => return self.prompt_clear_prompt_history(),
ConfigRow::Setting { namespace, key } => self.change_setting(namespace, key),
ConfigRow::Set {
namespace,
key,
value,
} => {
self.close_value_page();
self.write_setting(namespace, SettingChange::Set { key, value });
}
ConfigRow::Reset { namespace, key } => {
self.close_value_page();
self.write_setting(namespace, SettingChange::Reset { key });
}
ConfigRow::EnableAdvisor { model } => {
self.close_value_page();
self.enable_advisor(model);
}
}
Ok(())
}
fn open_config_page(&mut self, page: ConfigPage) {
if let Some(picker) = self.config_page_picker(&page) {
self.open_child_picker(picker);
}
}
pub(super) fn close_value_page(&mut self) {
if !self.pop_picker_level() {
self.input_ui.set_composer(ComposerMode::Input);
}
}
fn change_setting(&mut self, namespace: SettingsNamespace, key: SettingKey) {
let Some(settings) = &self.daemon_settings else {
return;
};
let Some(spec) = settings.spec(namespace, &key) else {
return;
};
let current = settings.value(namespace, &key).cloned();
match &spec.kind {
SettingKind::Bool => {
let on = matches!(
current.or_else(|| spec.default.clone()),
Some(SettingValue::Bool(true))
);
self.write_setting(
namespace,
SettingChange::Set {
key,
value: SettingValue::Bool(!on),
},
);
}
SettingKind::Choice { .. } | SettingKind::Model { .. } => {
self.open_config_page(ConfigPage::Setting { namespace, key });
}
SettingKind::Integer { min, max, unit } => {
let range = match max {
Some(max) => format!("{min}–{max}"),
None => format!("at least {min}"),
};
let unit = match unit {
Some(rho_protocol::IntegerUnit::Percent) => " %",
Some(rho_protocol::IntegerUnit::Seconds) => " seconds",
Some(rho_protocol::IntegerUnit::Lines) => " lines",
Some(rho_protocol::IntegerUnit::Bytes) => " bytes",
_ => "",
};
let label = format!("{} ({range}{unit})", status_label(&spec.label));
let value = match current {
Some(SettingValue::Integer(number)) => number.to_string(),
_ => String::new(),
};
self.open_setting_input(TextInput::setting(namespace, key, label, value));
}
SettingKind::Text => {
let label = status_label(&spec.label);
let value = match current {
Some(SettingValue::Text(text)) => text,
_ => String::new(),
};
self.open_setting_input(TextInput::setting(namespace, key, label, value));
}
_ => self.set_status(format!(
"this rho cannot change {}",
status_label(&spec.label)
)),
}
}
fn open_setting_input(&mut self, input: TextInput) {
let ComposerMode::Picker(page) = self.input_ui.take_composer() else {
return;
};
self.set_status_quiet(format!("edit {}", input.label()));
self.input_ui
.set_composer(ComposerMode::TextInput(input.with_return_picker(page)));
}
pub(super) fn commit_setting_input(
&mut self,
namespace: SettingsNamespace,
key: SettingKey,
text: &str,
) {
let Some(spec) = self
.daemon_settings
.as_ref()
.and_then(|settings| settings.spec(namespace, &key))
else {
return;
};
let text = text.trim();
let change = if text.is_empty() {
Ok(SettingChange::Reset { key })
} else {
match &spec.kind {
SettingKind::Integer { .. } => text
.parse()
.map(|number| SettingChange::Set {
key,
value: SettingValue::Integer(number),
})
.map_err(|_| "enter a whole number"),
_ => Ok(SettingChange::Set {
key,
value: SettingValue::Text(text.to_owned()),
}),
}
};
match change {
Ok(change) => {
self.close_setting_input();
self.write_setting(namespace, change);
}
Err(problem) => self.insert_entry(&Entry::Error(format!(
"could not save {}: {problem}",
status_label(&spec.label)
))),
}
}
pub(super) fn close_setting_input(&mut self) {
let page = match self.input_ui.composer_mut() {
ComposerMode::TextInput(input) => input.take_return_picker(),
_ => None,
};
match page {
Some(page) => {
let page = self.refreshed_config_page(page);
self.set_status_quiet(page.title.clone());
self.input_ui.set_composer(ComposerMode::Picker(page));
}
None => self.input_ui.set_composer(ComposerMode::Input),
}
}
pub(super) fn write_setting(&mut self, namespace: SettingsNamespace, change: SettingChange) {
if self.resets_the_advisor_model(namespace, &change) {
self.reset_advisor_model();
return;
}
let lowered = self
.daemon_settings
.as_ref()
.and_then(|settings| settings.spec(namespace, change.key()))
.and_then(|spec| {
lowered_prompt_history_limit(
namespace,
spec,
&change,
self.info.services.settings.prompt_history_limit,
)
});
if let Some(limit) = lowered {
if let Err(error) = self.confirm_prompt_history_limit(limit, change) {
self.insert_entry(&Entry::Error(error.to_string()));
}
return;
}
self.save_setting(namespace, change);
}
pub(super) fn save_setting(&mut self, namespace: SettingsNamespace, change: SettingChange) {
let Some(settings) = self.daemon_settings.as_ref() else {
return;
};
let Some(spec) = settings.spec(namespace, change.key()) else {
return;
};
let label = status_label(&spec.label);
let shown = match &change {
SettingChange::Set { value, .. } => settings.display_value(spec, Some(value)),
SettingChange::Reset { .. } => "default".into(),
};
let status = match applies_status(spec.applies) {
Some(when) => format!("{label}: {shown}; {when}"),
None => format!("{label}: {shown}"),
};
if self.send_settings(namespace, vec![change], &label) {
self.set_status(status);
}
}
pub(super) fn send_settings(
&mut self,
namespace: SettingsNamespace,
changes: Vec<SettingChange>,
label: &str,
) -> bool {
let Some(settings) = &self.daemon_settings else {
return false;
};
let params = ConfigSetParams {
namespace: namespace.wire(),
layer: ConfigLayer::Global,
revision: settings.values(namespace).revision.clone(),
writer: None,
changes,
};
match self
.info
.session
.daemon
.set_config(params, label.to_owned())
{
Ok(()) => true,
Err(error) => {
self.insert_entry(&Entry::Error(format!("could not save {label}: {error}")));
false
}
}
}
pub(super) fn show_daemon_settings(&mut self, update: SettingsUpdate) {
let (tui_values, problems) = match update {
SettingsUpdate::Loaded(settings) => {
if let Some(error) = settings.unlisted_models() {
self.insert_entry(&Entry::Error(format!("could not list the models: {error}")));
}
let problems = settings.problems();
let mut values = settings.values(SettingsNamespace::Tui).values.clone();
if self.streams.mode_from_shortcut {
values.remove(&SettingKey::new(
super::client_settings::OUTPUT_STREAMING_KEY,
));
}
self.daemon_settings = Some(*settings);
(Some(values), problems)
}
SettingsUpdate::Unreadable(error) => {
self.daemon_settings = None;
self.insert_entry(&Entry::Error(format!(
"could not read the rho settings: {error}"
)));
self.refresh_open_config_page();
return;
}
SettingsUpdate::ModelsChanged(models) => {
if let Some(settings) = &mut self.daemon_settings {
settings.set_models(*models);
}
self.model_lists_refreshed();
self.refresh_open_config_page();
return;
}
SettingsUpdate::Changed(changed) => {
let Some(applied) = self
.daemon_settings
.as_mut()
.and_then(|settings| settings.apply(*changed))
else {
return;
};
(
(applied.namespace == SettingsNamespace::Tui).then_some(applied.changed),
applied.new_problems,
)
}
};
if !problems.is_empty() {
let problems = problems
.into_iter()
.map(|error| error.message)
.collect::<Vec<_>>()
.join("; ");
self.insert_entry(&Entry::Error(format!(
"could not use the edited settings, so the last good ones stay: {problems}"
)));
}
if let Some(values) = tui_values {
if values.contains_key(&SettingKey::new(
super::client_settings::OUTPUT_STREAMING_KEY,
)) {
self.streams.mode_from_shortcut = false;
}
let next = self.info.services.settings.with_client_values(&values);
self.apply_tui_settings(next);
}
self.refresh_open_config_page();
}
fn apply_tui_settings(&mut self, next: TuiSettings) {
let TuiSettings {
zen_mode,
show_reasoning_output,
output_streaming,
show_header_hints,
notifications,
cache_miss_notices,
max_tool_output_lines,
prompt_history_limit,
check_for_updates,
theme: _,
keybindings,
minimal_mode: _,
} = next.clone();
let runtime = &mut self.info.runtime;
let rerender = runtime.zen_mode != zen_mode
|| runtime.show_reasoning_output != show_reasoning_output
|| runtime.max_tool_output_lines != max_tool_output_lines;
runtime.zen_mode = zen_mode;
runtime.show_reasoning_output = show_reasoning_output;
runtime.max_tool_output_lines = max_tool_output_lines.max(1);
runtime.show_header_hints = show_header_hints;
runtime.notifications = notifications;
runtime.cache_miss_notices = cache_miss_notices;
let vim_changed = runtime.keybindings.editing_mode != keybindings.editing_mode;
runtime.keybindings.editing_mode = keybindings.editing_mode;
if rerender {
self.history.invalidate_from(0);
}
if vim_changed {
self.input_ui.reset_vim();
}
if !check_for_updates {
self.info.services.update_notice = None;
}
self.apply_streaming_mode(output_streaming);
let history_limit_changed =
self.info.services.settings.prompt_history_limit != prompt_history_limit;
self.info.services.settings = next;
if history_limit_changed {
self.apply_prompt_history_limit(prompt_history_limit);
}
}
}
fn lowered_prompt_history_limit(
namespace: SettingsNamespace,
spec: &SettingSpec,
change: &SettingChange,
current: usize,
) -> Option<u64> {
if namespace != SettingsNamespace::Tui
|| spec.key.as_str() != super::client_settings::PROMPT_HISTORY_LIMIT_KEY
{
return None;
}
let value = match change {
SettingChange::Set { value, .. } => Some(value),
SettingChange::Reset { .. } => spec.default.as_ref(),
};
match value {
Some(&SettingValue::Integer(limit)) if limit > 0 && limit < current as u64 => Some(limit),
_ => None,
}
}
pub(super) fn status_label(label: &str) -> String {
let first = label.split_whitespace().next().unwrap_or_default();
if first.chars().skip(1).any(char::is_uppercase) {
return label.to_owned();
}
let mut chars = label.chars();
match chars.next() {
Some(head) => head.to_lowercase().chain(chars).collect(),
None => String::new(),
}
}
fn applies_status(applies: Applies) -> Option<&'static str> {
match applies {
Applies::NextTurn => Some("applies next turn"),
Applies::NextSession => Some("applies to new sessions"),
Applies::Restart => Some("applies once rho restarts"),
Applies::Immediately | Applies::Unknown => None,
_ => None,
}
}
#[cfg(test)]
#[path = "settings_screen_tests.rs"]
mod tests;