use std::collections::BTreeMap;
use serde::Deserialize;
use toml_edit::{Array, Item, Value};
use {
crate::model_aliases::ModelAliases,
rho_providers::model::favorites::{favorite_model_values, normalized_favorite_models},
rho_providers::reasoning::ReasoningLevel,
};
use super::{
document::{decode_table, encode_table, ConfigDocument},
inferred_provider_auth,
provider_config::{PartialProviderConfigs, PersistedProviderConfigs},
registry::AGENT_SETTINGS,
setting::{load_settings, save_settings, unknown_keys, AnySetting, ConfigError},
Config, ConfigWarning, InternalAgentModelConfig, InternalAgentTarget, ModelKind,
CLAUDE_CLI_RUNTIME_KEY,
};
struct FileOnly {
key: &'static str,
label: &'static str,
docs: &'static str,
encode: fn(&Config) -> anyhow::Result<Option<Item>>,
}
const FILE_ONLY: [FileOnly; 7] = [
FileOnly {
key: "model.aliases",
label: "Model aliases",
docs: "/configuration#model-aliases",
encode: |config| non_empty(&config.model_aliases, ModelAliases::is_empty),
},
FileOnly {
key: "model.favorite_models",
label: "Favorite models",
docs: "/configuration#common-settings",
encode: |config| {
Ok((!config.favorite_models.is_empty()).then(|| {
Item::Value(Value::Array(Array::from_iter(
config.favorite_models.iter().map(String::as_str),
)))
}))
},
},
FileOnly {
key: "internal_agents",
label: "Internal agent models",
docs: "/configuration#internal-agent-models",
encode: |config| {
let entries = config
.internal_agents
.iter()
.map(|(id, selection)| {
(
id.as_str(),
PersistedInternalAgent::new(selection, &config.model_aliases),
)
})
.collect::<BTreeMap<_, _>>();
non_empty(&entries, BTreeMap::is_empty)
},
},
FileOnly {
key: "providers",
label: "Provider profiles",
docs: "/providers/openai-compatible",
encode: |config| {
non_empty(
&PersistedProviderConfigs::from(&config.providers),
PersistedProviderConfigs::is_empty,
)
},
},
FileOnly {
key: "mcp",
label: "MCP servers",
docs: "/integrations/mcp#configuration-source-and-precedence",
encode: |config| non_empty(&config.mcp, crate::tools::mcp::config::McpConfig::is_empty),
},
FileOnly {
key: "openapi",
label: "OpenAPI sources",
docs: "/integrations/openapi",
encode: |config| {
non_empty(
&config.openapi,
crate::tools::openapi::config::OpenApiConfig::is_empty,
)
},
},
FileOnly {
key: "prompt_templates",
label: "Prompt templates",
docs: "/configuration#prompt-templates",
encode: |config| non_empty(&config.prompt_templates, BTreeMap::is_empty),
},
];
fn non_empty<T: serde::Serialize>(
value: &T,
is_empty: fn(&T) -> bool,
) -> anyhow::Result<Option<Item>> {
if is_empty(value) {
return Ok(None);
}
encode_table(value).map(Some)
}
fn in_file_only_table(key: &str) -> bool {
FILE_ONLY.iter().any(|table| {
key.strip_prefix(table.key)
.is_some_and(|rest| rest.is_empty() || rest.starts_with('.'))
})
}
fn registry_rows() -> Vec<&'static dyn AnySetting<Config>> {
AGENT_SETTINGS
.iter()
.copied()
.filter(|setting| !in_file_only_table(setting.key()))
.collect()
}
pub(super) fn file_only_tables() -> Vec<rho_protocol::FileOnlyTable> {
FILE_ONLY
.iter()
.map(|table| rho_protocol::FileOnlyTable {
key: table.key.into(),
label: table.label.into(),
docs: table.docs.into(),
})
.collect()
}
pub(super) fn emit_warnings(path_display: &str, warnings: &[ConfigWarning]) {
for warning in warnings {
eprintln!("warning: {path_display}: {}", warning.message());
}
}
pub(super) fn errors_error(errors: &[ConfigError]) -> anyhow::Error {
anyhow::anyhow!(errors
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join("\n"))
}
pub(crate) fn resolve(
doc: &ConfigDocument,
) -> Result<(Config, Vec<ConfigWarning>), Vec<ConfigError>> {
let mut config = Config::default();
let mut warnings = Vec::new();
let file_only_keys = FILE_ONLY.map(|table| table.key);
let mut errors = unknown_keys(doc, AGENT_SETTINGS, &file_only_keys);
load_settings(®istry_rows(), doc, &mut config, &mut errors);
let mut internal_agents = None;
for key in file_only_keys {
let Some(item) = doc.get(key) else {
continue;
};
let loaded = match key {
"model.aliases" => decode_table(item).map(|aliases| config.model_aliases = aliases),
"model.favorite_models" => favorite_models(item).map(|models| {
config.favorite_models =
favorite_model_values(&normalized_favorite_models(&models));
}),
"internal_agents" => decode_table::<BTreeMap<String, PartialInternalAgent>>(item)
.map(|entries| internal_agents = Some(entries)),
"providers" => decode_table::<PartialProviderConfigs>(item).and_then(|providers| {
config
.providers
.apply(providers, &mut warnings)
.map_err(|error| format!("{error:#}"))
}),
"mcp" => decode_table(item).map(|mcp| config.mcp = mcp),
"openapi" => decode_table(item).map(|openapi| config.openapi = openapi),
"prompt_templates" => decode_table(item).and_then(|templates| {
crate::prompt_templates::validate(&templates)
.map_err(|error| format!("{error:#}"))?;
config.prompt_templates = templates;
Ok(())
}),
other => unreachable!("FILE_ONLY lists {other} without a loader"),
};
if let Err(message) = loaded {
errors.push(ConfigError::at(key, message));
}
}
if !errors.is_empty() {
return Err(errors);
}
let cross_field = |result: anyhow::Result<()>, errors: &mut Vec<ConfigError>| {
if let Err(error) = result {
errors.push(ConfigError {
key: None,
message: format!("{error:#}"),
});
}
};
cross_field(config.validate_model_aliases(), &mut errors);
cross_field(config.resolve_model_alias(), &mut errors);
if !errors.is_empty() {
return Err(errors);
}
config.internal_agents = internal_agents
.unwrap_or_default()
.into_iter()
.filter_map(|(id, entry)| {
if id == REMOVED_GOAL_JUDGE_ID {
return None;
}
if catalog_entry_cannot_override(&id, &entry, &config.model_aliases) {
warnings.push(ConfigWarning::Normalized {
key: "internal_agents",
from: format!("for agent '{id}'"),
to: "no override; a catalog agent's entry must name a Rho provider and model"
.into(),
});
return None;
}
let selection = internal_agent_selection(&id, entry, &config, &mut warnings);
Some((id, selection))
})
.collect();
cross_field(config.resolve_internal_agent_model_aliases(), &mut errors);
cross_field(config.normalize_provider_profiles(), &mut errors);
if config.compact_target_percent >= config.compact_threshold_percent {
errors.push(ConfigError::at(
"compaction.compact_target_percent",
format!(
"must be below compaction.compact_threshold_percent ({}); got {}",
config.compact_threshold_percent, config.compact_target_percent
),
));
}
if errors.is_empty() {
Ok((config, warnings))
} else {
Err(errors)
}
}
fn favorite_models(item: &Item) -> Result<Vec<String>, String> {
let not_a_list = || "must be a list of model references".to_owned();
item.as_array()
.ok_or_else(not_a_list)?
.iter()
.map(|value| value.as_str().map(str::to_owned).ok_or_else(not_a_list))
.collect()
}
pub(super) fn save_diff(
before: &Config,
after: &Config,
doc: &mut ConfigDocument,
) -> anyhow::Result<()> {
save_settings(®istry_rows(), before, after, doc);
for table in &FILE_ONLY {
let new = (table.encode)(after)?;
let old = (table.encode)(before)?;
if rendered(old.as_ref()) != rendered(new.as_ref()) {
doc.set_item(table.key, new);
}
}
Ok(())
}
fn rendered(item: Option<&Item>) -> Option<String> {
item.map(|item| {
let mut doc = toml_edit::DocumentMut::new();
doc.insert("table", item.clone());
doc.to_string()
})
}
#[derive(serde::Serialize)]
struct PersistedInternalAgent<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
runtime: Option<&'static str>,
#[serde(skip_serializing_if = "Option::is_none")]
provider: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
model: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
auth: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
kind: Option<ModelKind>,
#[serde(skip_serializing_if = "Option::is_none")]
allow_threshold_percent: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
reasoning: Option<ReasoningLevel>,
}
impl<'a> PersistedInternalAgent<'a> {
fn new(selection: &'a InternalAgentModelConfig, aliases: &'a ModelAliases) -> Self {
match &selection.target {
InternalAgentTarget::Rho(rho) => Self {
runtime: None,
provider: Some(rho.provider.as_str()),
model: Some(match selection.current_alias(aliases) {
Some(alias) => format!("@{alias}"),
None => rho.model.clone(),
}),
auth: Some(rho.auth.as_str()),
kind: rho.kind,
allow_threshold_percent: rho.allow_threshold_percent,
reasoning: selection.reasoning,
},
InternalAgentTarget::ClaudeCli { model } => Self {
runtime: Some(CLAUDE_CLI_RUNTIME_KEY),
provider: None,
model: model.clone(),
auth: None,
kind: None,
allow_threshold_percent: None,
reasoning: selection.reasoning,
},
}
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct PartialInternalAgent {
runtime: Option<String>,
provider: Option<String>,
model: Option<String>,
auth: Option<String>,
kind: Option<ModelKind>,
allow_threshold_percent: Option<u8>,
reasoning: Option<ReasoningLevel>,
}
const REMOVED_GOAL_JUDGE_ID: &str = "goal-judge";
fn catalog_entry_cannot_override(
id: &str,
group: &PartialInternalAgent,
aliases: &ModelAliases,
) -> bool {
let catalog =
crate::agent::AgentId::new(id).is_ok_and(|id| !crate::agent::is_reserved_agent_id(&id));
let rho = matches!(
group.runtime.as_deref(),
None | Some(crate::config::RHO_RUNTIME_KEY)
);
let alias_names_provider = |model: &str| {
aliases
.resolve(model)
.map_or(true, |resolved| resolved.provider.is_some())
};
let names_model = match (&group.provider, group.model.as_deref()) {
(Some(_), Some(_)) => true,
(None, Some(model)) => model.starts_with('@') && alias_names_provider(model),
(_, None) => false,
};
catalog && !(rho && names_model)
}
fn internal_agent_selection(
id: &str,
group: PartialInternalAgent,
cfg: &Config,
warnings: &mut Vec<ConfigWarning>,
) -> InternalAgentModelConfig {
let runtime = internal_agent_runtime_key(id, group.runtime.as_deref(), warnings);
let mut selection = match runtime {
InternalAgentRuntimeKey::Rho => {
let provider = group.provider.unwrap_or_else(|| cfg.provider.clone());
let auth = group
.auth
.unwrap_or_else(|| inferred_provider_auth(&provider, &cfg.provider, &cfg.auth));
let model = group.model.unwrap_or_else(|| cfg.model.clone());
let mut selection = InternalAgentModelConfig::new(provider, model, auth);
if let InternalAgentTarget::Rho(rho) = &mut selection.target {
rho.kind = group.kind;
rho.allow_threshold_percent = group.allow_threshold_percent;
}
selection
}
InternalAgentRuntimeKey::Rejected => {
InternalAgentModelConfig::new(cfg.provider.clone(), cfg.model.clone(), cfg.auth.clone())
}
InternalAgentRuntimeKey::ClaudeCli => {
for (key, value) in [
("internal_agents.provider", group.provider),
("internal_agents.auth", group.auth),
] {
if let Some(value) = value {
warnings.push(ConfigWarning::Normalized {
key,
from: format!("\"{value}\""),
to: format!(
"no value; [internal_agents.{id}] runtime claude-cli has no Rho provider or auth"
),
});
}
}
InternalAgentModelConfig::claude_cli(group.model)
}
};
selection.reasoning = group.reasoning;
selection
}
fn internal_agent_runtime_key(
id: &str,
runtime: Option<&str>,
warnings: &mut Vec<ConfigWarning>,
) -> InternalAgentRuntimeKey {
let (from, to) = match runtime {
None | Some(crate::config::RHO_RUNTIME_KEY) => return InternalAgentRuntimeKey::Rho,
Some(crate::config::CLAUDE_CLI_RUNTIME_KEY)
if crate::agent::internal_agent_accepts_claude_runtime(id) =>
{
return InternalAgentRuntimeKey::ClaudeCli
}
Some(crate::config::CLAUDE_CLI_RUNTIME_KEY) => (
crate::config::CLAUDE_CLI_RUNTIME_KEY,
format!(
"\"{}\" with the conversation selection; agent '{id}' cannot delegate",
crate::config::RHO_RUNTIME_KEY
),
),
Some(crate::config::CURSOR_RUNTIME_KEY) => (
crate::config::CURSOR_RUNTIME_KEY,
format!(
"\"{}\" with the conversation selection; [internal_agents] runtime stays rho or claude-cli, not cursor",
crate::config::RHO_RUNTIME_KEY
),
),
Some(other) => (
other,
format!(
"\"{}\" with the conversation selection",
crate::config::RHO_RUNTIME_KEY
),
),
};
warnings.push(ConfigWarning::Normalized {
key: "internal_agents.runtime",
from: format!("\"{from}\""),
to,
});
InternalAgentRuntimeKey::Rejected
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum InternalAgentRuntimeKey {
Rho,
ClaudeCli,
Rejected,
}
#[cfg(test)]
#[path = "config_load_tests.rs"]
mod tests;