use std::collections::BTreeMap;
use rho_protocol::{
Applies, ClientName, ConfigChangedParams, ConfigError, ConfigGetResponse, ConfigNamespace,
ConfigRevision, ConfigSchema, IntegerUnit, ModelChoice, ModelList, ModelRuntime,
ModelSelection, SettingEntry, SettingKey, SettingKind, SettingSpec, SettingValue,
};
use super::{
config_row::ConfigRow, picker::PickerDetail, PickerBadge, PickerBadgeTone, PickerItem,
};
const TUI_CLIENT: &str = "tui";
const CREDENTIAL_STORE_KEY: &str = "behavior.credential_store";
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub(super) enum SettingsNamespace {
Agent,
Tui,
}
impl SettingsNamespace {
pub(super) fn wire(self) -> ConfigNamespace {
match self {
Self::Agent => ConfigNamespace::Agent,
Self::Tui => ConfigNamespace::Client { name: tui_client() },
}
}
fn of(namespace: &ConfigNamespace) -> Option<Self> {
match namespace {
ConfigNamespace::Agent => Some(Self::Agent),
ConfigNamespace::Client { name } if name.as_str() == TUI_CLIENT => Some(Self::Tui),
_ => None,
}
}
}
pub(crate) fn tui_client() -> ClientName {
ClientName::new(TUI_CLIENT).expect("`tui` is a valid client name")
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct NamespaceValues {
pub(super) revision: ConfigRevision,
pub(super) values: BTreeMap<SettingKey, SettingEntry>,
pub(super) errors: Vec<ConfigError>,
}
impl From<ConfigGetResponse> for NamespaceValues {
fn from(response: ConfigGetResponse) -> Self {
Self {
revision: response.revision,
values: response.values,
errors: response.errors,
}
}
}
#[derive(Debug, PartialEq)]
pub(super) struct Applied {
pub(super) namespace: SettingsNamespace,
pub(super) changed: BTreeMap<SettingKey, SettingEntry>,
pub(super) new_problems: Vec<ConfigError>,
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DaemonSettings {
schema: ConfigSchema,
tui_specs: Vec<SettingSpec>,
agent: NamespaceValues,
tui: NamespaceValues,
models: ListedModels,
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum ListedModels {
Listed(ModelList),
Unlisted(String),
}
impl DaemonSettings {
pub(crate) fn new(
mut schema: ConfigSchema,
tui_specs: Vec<SettingSpec>,
agent: NamespaceValues,
tui: NamespaceValues,
models: ListedModels,
) -> Self {
schema
.settings
.retain(|spec| spec.key.as_str() != CREDENTIAL_STORE_KEY);
Self {
schema,
tui_specs,
agent,
tui,
models,
}
}
pub(super) fn models(&self) -> &[ModelChoice] {
self.listed().map_or(&[], |list| &list.models)
}
pub(super) fn unlisted_models(&self) -> Option<&str> {
match &self.models {
ListedModels::Listed(_) => None,
ListedModels::Unlisted(error) => Some(error),
}
}
fn listed(&self) -> Option<&ModelList> {
match &self.models {
ListedModels::Listed(list) => Some(list),
ListedModels::Unlisted(_) => None,
}
}
pub(super) fn set_models(&mut self, models: ModelList) {
self.models = ListedModels::Listed(models);
}
pub(super) fn apply(&mut self, changed: ConfigChangedParams) -> Option<Applied> {
let namespace = SettingsNamespace::of(&changed.namespace)?;
let current = self.values_mut(namespace);
current.revision = changed.revision;
current.values.extend(changed.values.clone());
let new_problems = if current.errors == changed.errors {
Vec::new()
} else {
changed.errors.clone()
};
current.errors = changed.errors;
Some(Applied {
namespace,
changed: changed.values,
new_problems,
})
}
pub(super) fn problems(&self) -> Vec<ConfigError> {
[&self.agent, &self.tui]
.into_iter()
.flat_map(|values| values.errors.iter().cloned())
.collect()
}
pub(super) fn values(&self, namespace: SettingsNamespace) -> &NamespaceValues {
match namespace {
SettingsNamespace::Agent => &self.agent,
SettingsNamespace::Tui => &self.tui,
}
}
fn values_mut(&mut self, namespace: SettingsNamespace) -> &mut NamespaceValues {
match namespace {
SettingsNamespace::Agent => &mut self.agent,
SettingsNamespace::Tui => &mut self.tui,
}
}
fn specs(&self, namespace: SettingsNamespace) -> &[SettingSpec] {
match namespace {
SettingsNamespace::Agent => &self.schema.settings,
SettingsNamespace::Tui => &self.tui_specs,
}
}
pub(super) fn spec(
&self,
namespace: SettingsNamespace,
key: &SettingKey,
) -> Option<&SettingSpec> {
self.specs(namespace).iter().find(|spec| &spec.key == key)
}
pub(super) fn value(
&self,
namespace: SettingsNamespace,
key: &SettingKey,
) -> Option<&SettingValue> {
self.values(namespace).values.get(key)?.value.as_ref()
}
pub(super) fn category_rows(&self) -> Vec<PickerItem> {
let agent = self.schema.categories.iter().map(|category| {
(
SettingsNamespace::Agent,
category.id.clone(),
category.label.clone(),
)
});
let mut tui_categories: Vec<String> = Vec::new();
for spec in &self.tui_specs {
if !tui_categories.contains(&spec.category) {
tui_categories.push(spec.category.clone());
}
}
let tui = tui_categories
.into_iter()
.map(|id| (SettingsNamespace::Tui, id.clone(), capitalized(&id)));
agent
.chain(tui)
.filter_map(|(namespace, id, label)| {
let specs: Vec<_> = self
.specs(namespace)
.iter()
.filter(|spec| spec.category == id)
.collect();
if specs.is_empty() {
return None;
}
let detail = specs
.iter()
.map(|spec| spec.label.as_str())
.collect::<Vec<_>>()
.join(", ");
Some(PickerItem {
section: Some("Settings".into()),
detail: Some(PickerDetail::Text(format!("{detail}."))),
..PickerItem::new(label, ConfigRow::Category { namespace, id }.encode())
})
})
.collect()
}
pub(super) fn category_label(&self, namespace: SettingsNamespace, id: &str) -> String {
match namespace {
SettingsNamespace::Agent => self
.schema
.categories
.iter()
.find(|category| category.id == id)
.map_or_else(|| capitalized(id), |category| category.label.clone()),
SettingsNamespace::Tui => capitalized(id),
}
}
pub(super) fn setting_rows(&self, namespace: SettingsNamespace, id: &str) -> Vec<PickerItem> {
let current = self.values(namespace);
self.specs(namespace)
.iter()
.filter(|spec| spec.category == id)
.map(|spec| {
let value = current
.values
.get(&spec.key)
.and_then(|entry| entry.value.as_ref());
let mut detail = spec.help.clone();
if let Some(when) = applies_suffix(spec.applies) {
detail.push(' ');
detail.push_str(when);
}
for error in current
.errors
.iter()
.filter(|error| error.key.as_ref() == Some(&spec.key))
{
detail.push_str(&format!(
"\n\nThe file's value is not used: {}",
error.message
));
}
PickerItem {
section: spec.section.clone(),
detail: Some(PickerDetail::Text(detail)),
badge: Some(selected_badge(self.display_value(spec, value))),
selection_verb: Some(match spec.kind {
SettingKind::Bool => "toggle",
_ => "change",
}),
space_confirms: matches!(spec.kind, SettingKind::Bool),
..PickerItem::new(
spec.label.clone(),
ConfigRow::Setting {
namespace,
key: spec.key.clone(),
}
.encode(),
)
}
})
.collect()
}
pub(super) fn value_rows(
&self,
namespace: SettingsNamespace,
key: &SettingKey,
) -> Option<Vec<PickerItem>> {
let spec = self.spec(namespace, key)?;
let current = self.value(namespace, key);
let set = |value: SettingValue| ConfigRow::Set {
namespace,
key: key.clone(),
value,
};
let mut rows: Vec<PickerItem> = match &spec.kind {
SettingKind::Choice { choices } => choices
.iter()
.map(|choice| {
let value = SettingValue::Text(choice.value.clone());
PickerItem {
detail: choice.help.clone().map(PickerDetail::Text),
badge: (current == Some(&value)).then(|| selected_badge("current")),
..PickerItem::new(choice.label.clone(), set(value).encode())
}
})
.collect(),
SettingKind::Model { runtimes } => {
let mut rows = Vec::new();
if runtimes.contains(&ModelRuntime::Rho) {
rows.extend(self.models().iter().map(|choice| {
let value = SettingValue::Model(ModelSelection::Rho {
provider: choice.model.provider.clone(),
model: choice.model.model.clone(),
});
PickerItem {
detail: Some(PickerDetail::Text(format!(
"{}/{}",
choice.model.provider, choice.model.model
))),
badge: (current == Some(&value)).then(|| selected_badge("current")),
..PickerItem::new(choice.label.clone(), set(value).encode())
}
}));
}
if runtimes.contains(&ModelRuntime::ClaudeCli) {
rows.extend(self.claude_code_rows(current).into_iter().map(
|(label, detail, model)| {
let value = SettingValue::Model(ModelSelection::ClaudeCli { model });
PickerItem {
detail: Some(PickerDetail::Text(detail)),
badge: (current == Some(&value)).then(|| selected_badge("current")),
..PickerItem::new(label, set(value).encode())
}
},
));
}
rows
}
_ => return None,
};
let from_file = self
.values(namespace)
.values
.get(key)
.is_some_and(|entry| entry.source == rho_protocol::ValueSource::Global);
if from_file {
let default = self.display_value(spec, spec.default.as_ref());
rows.push(PickerItem {
detail: Some(PickerDetail::Text(
"Remove the setting from the file, so its default applies.".into(),
)),
..PickerItem::new(
format!("Default ({default})"),
ConfigRow::Reset {
namespace,
key: key.clone(),
}
.encode(),
)
});
}
Some(rows)
}
fn claude_code_rows(
&self,
current: Option<&SettingValue>,
) -> Vec<(String, String, Option<String>)> {
let offered = self.listed().map_or(&[][..], |list| &list.claude_cli);
let mut rows: Vec<_> = offered
.iter()
.map(|model| {
(
model.label.clone(),
model.detail.clone(),
model.model.clone(),
)
})
.collect();
if let Some(SettingValue::Model(ModelSelection::ClaudeCli { model: Some(model) })) = current
{
if !offered
.iter()
.any(|offered| offered.model.as_ref() == Some(model))
{
rows.push((
rho_providers::provider::model_reference(
crate::claude_runtime::models::CLAUDE_CODE_SOURCE_LABEL,
model,
),
"Runs on Claude Code.".to_owned(),
Some(model.clone()),
));
}
}
rows
}
pub(super) fn display_value(&self, spec: &SettingSpec, value: Option<&SettingValue>) -> String {
let Some(value) = value else {
return "not set".into();
};
match (&spec.kind, value) {
(SettingKind::Bool, SettingValue::Bool(on)) => if *on { "on" } else { "off" }.into(),
(SettingKind::Choice { choices }, SettingValue::Text(text)) => choices
.iter()
.find(|choice| &choice.value == text)
.map_or_else(|| text.clone(), |choice| choice.label.clone()),
(SettingKind::Integer { unit, .. }, SettingValue::Integer(number)) => match unit {
Some(IntegerUnit::Percent) => format!("{number}%"),
Some(IntegerUnit::Seconds) => format!("{number}s"),
Some(IntegerUnit::Lines) => format!("{number} lines"),
Some(IntegerUnit::Bytes) => format!("{number} bytes"),
_ => number.to_string(),
},
(_, SettingValue::Model(ModelSelection::Rho { provider, model })) => self
.models()
.iter()
.find(|choice| &choice.model.provider == provider && &choice.model.model == model)
.map_or_else(
|| format!("{provider}/{model}"),
|choice| choice.label.clone(),
),
(_, SettingValue::Model(ModelSelection::ClaudeCli { model })) => {
format!("claude-code/{}", model.as_deref().unwrap_or("default"))
}
(_, SettingValue::Text(text)) => text.clone(),
(_, SettingValue::Integer(number)) => number.to_string(),
(_, SettingValue::Bool(on)) => on.to_string(),
}
}
}
fn applies_suffix(applies: Applies) -> Option<&'static str> {
match applies {
Applies::Immediately => None,
Applies::NextTurn => Some("Applies from the next turn."),
Applies::NextSession => Some("Applies to new sessions."),
Applies::Restart => Some("Applies once rho restarts."),
_ => None,
}
}
fn selected_badge(text: impl Into<String>) -> PickerBadge {
PickerBadge {
text: text.into(),
tone: PickerBadgeTone::Selected,
}
}
fn capitalized(id: &str) -> String {
let mut chars = id.chars();
match chars.next() {
Some(first) => first.to_uppercase().chain(chars).collect(),
None => String::new(),
}
}
#[cfg(test)]
#[path = "daemon_settings_tests.rs"]
pub(super) mod tests;