rho-coding-agent 3.13.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 [`Config::conversation_model`] `None`. 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.

use super::{AuthOrigin, Config};

/// The conversation model someone chose.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ConversationModel {
    /// The provider. Empty only while loading, or applying a wire change,
    /// holds an `@alias` model that names it, until
    /// `Config::resolve_model_alias` resolves the alias.
    pub provider: String,
    /// A concrete model id: load resolves an `@alias` to its target, the
    /// only raw value it holds and only in the same window as `provider`.
    pub model: String,
    /// The auth profile. A change that decides whether someone chose it
    /// goes through [`Self::choose_auth`] or [`Self::infer_auth`], which
    /// set [`Self::auth_origin`] with it.
    pub auth: String,
    /// Whether someone chose [`Self::auth`] or it was filled in, which
    /// decides whether a session starts on it as it is.
    pub(crate) auth_origin: AuthOrigin,
    /// The alias `provider`/`model` was resolved from, if any. Consult it
    /// through [`Config::current_model_alias`], which drops it once the
    /// selection no longer matches the alias table.
    pub(crate) alias: Option<String>,
}

impl ConversationModel {
    /// `provider`'s `model` on `auth`, whose origin is `auth_origin`, named
    /// directly rather than through an alias.
    pub(crate) fn new(
        provider: impl Into<String>,
        model: impl Into<String>,
        auth: impl Into<String>,
        auth_origin: AuthOrigin,
    ) -> Self {
        Self {
            provider: provider.into(),
            model: model.into(),
            auth: auth.into(),
            auth_origin,
            alias: None,
        }
    }
}

/// 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, for a session that runs on it.
    pub(crate) fn require_conversation_model(&self) -> Result<&ConversationModel, ModelRequired> {
        self.conversation_model.as_ref().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.conversation_model = Some(ConversationModel::new(provider, model, auth, origin));
    }

    /// Runs on `provider`'s `model` on `auth`, keeping whether the auth was
    /// chosen and the alias the model came from, which
    /// [`Config::current_model_alias`] drops once it no longer matches: a
    /// session's own config moved onto the model it runs on.
    pub(crate) fn run_conversation_on(
        &mut self,
        provider: String,
        model: String,
        auth: String,
    ) -> &mut ConversationModel {
        let (auth_origin, alias) = self
            .conversation_model
            .take()
            .map_or((AuthOrigin::Inferred, None), |chosen| {
                (chosen.auth_origin, chosen.alias)
            });
        self.conversation_model.insert(ConversationModel {
            provider,
            model,
            auth,
            auth_origin,
            alias,
        })
    }

    /// The conversation model, for a test that set one up.
    #[cfg(test)]
    pub(crate) fn expect_conversation_model(&self) -> &ConversationModel {
        self.conversation_model
            .as_ref()
            .expect("the test sets up a conversation model")
    }

    /// 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 {
        Self::with_model("openai", "gpt-5.5", "api-key")
    }

    /// The default config with `provider`'s `model` on `auth`, filled in
    /// rather than chosen, as the conversation model.
    #[cfg(test)]
    pub(crate) fn with_model(
        provider: impl Into<String>,
        model: impl Into<String>,
        auth: impl Into<String>,
    ) -> Self {
        Self {
            conversation_model: Some(ConversationModel::new(
                provider,
                model,
                auth,
                AuthOrigin::Inferred,
            )),
            ..Self::default()
        }
    }
}