rho-coding-agent 3.10.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Whether someone chose the conversation model.
//!
//! Nothing chooses one for a machine: a missing `config.toml`, one without
//! a `[model]` table, and a table that names only one of `provider` and
//! `model` all leave it unset. Until someone sets it, through the TUI's
//! setup, a client's setup screen, or the file, every session that would
//! run on it fails to open with [`ModelRequired`]; one whose request or
//! agent names its own model still opens.
//!
//! TODO(after 3.10): hold the conversation model as
//! `Option<ConversationModel>` on [`Config`] instead of its three strings,
//! empty while unset, so the type rules out reading them unset. Until
//! then, [`Config::conversation_model`] is the only way to ask whether it
//! is chosen, and every way into a session checks it before anything reads
//! the strings.

use super::{AuthOrigin, Config};

/// The conversation model someone chose.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct ConversationModel<'a> {
    pub(crate) provider: &'a str,
    pub(crate) model: &'a str,
    pub(crate) auth: &'a str,
}

/// Whether `provider` and `model` name a model: both are set. The one
/// test of it, for the conversation model and for a model a session not
/// opened yet runs on, which names none while nobody chose one.
pub(crate) fn names_a_model(provider: &str, model: &str) -> bool {
    !provider.is_empty() && !model.is_empty()
}

/// A session needed the conversation model, and nobody has chosen one.
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
#[error(
    "no model is set up: run `rho` and choose one, pick one in rho-ui, or set `[model] provider` and `model` in config.toml"
)]
pub(crate) struct ModelRequired;

impl Config {
    /// The conversation model, or `None` while nobody has chosen one.
    pub(crate) fn conversation_model(&self) -> Option<ConversationModel<'_>> {
        names_a_model(&self.provider, &self.model).then_some(ConversationModel {
            provider: &self.provider,
            model: &self.model,
            auth: &self.auth,
        })
    }

    /// The conversation model, for a session that runs on it.
    pub(crate) fn require_conversation_model(
        &self,
    ) -> Result<ConversationModel<'_>, ModelRequired> {
        self.conversation_model().ok_or(ModelRequired)
    }

    /// Chooses `provider`'s `model` on `auth`, whose origin is `origin`, as
    /// the conversation model, dropping the alias it may have come from: a
    /// person's choice, or, on a session's own config, the model it opens
    /// on in place of the conversation model.
    pub(crate) fn set_conversation_model(
        &mut self,
        provider: String,
        model: String,
        auth: String,
        origin: AuthOrigin,
    ) {
        self.provider = provider;
        self.model = model;
        self.auth = auth;
        self.auth_origin = origin;
        self.model_alias = None;
    }

    /// The default config with a conversation model chosen, for tests whose
    /// sessions run on one but do not care which: OpenAI's `gpt-5.5` on an
    /// API key.
    #[cfg(test)]
    pub(crate) fn with_test_model() -> Self {
        let mut config = Self::default();
        config.set_conversation_model(
            "openai".into(),
            "gpt-5.5".into(),
            "api-key".into(),
            AuthOrigin::Inferred,
        );
        config
    }

    /// Leaves the conversation model unset, as a machine starts.
    pub(crate) fn clear_conversation_model(&mut self) {
        self.provider.clear();
        self.model.clear();
        self.auth.clear();
        self.auth_origin = AuthOrigin::Inferred;
        self.model_alias = None;
    }
}