rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The model the CLI's `--provider`, `--model` and `--auth` flags name as
//! they apply, before it becomes the conversation model: a flag may name a
//! provider before, or without, a model it has.

use rho_providers::provider;

use crate::config::{AuthOrigin, Config, ConversationModel};

/// A provider the CLI flags named, on the auth they settled on, when they
/// named no model it has and none is set up: `rho doctor` still reports on
/// it, though no session can run on it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(in crate::app) struct ProviderWithoutModel {
    pub(in crate::app) provider: String,
    pub(in crate::app) auth: String,
}

/// The selection the flags build, seeded from the conversation model.
pub(super) struct CliDraft {
    provider: Option<DraftProvider>,
    model: Option<String>,
    alias: Option<String>,
}

struct DraftProvider {
    name: String,
    auth: String,
    auth_origin: AuthOrigin,
}

impl CliDraft {
    pub(super) fn of(config: &Config) -> Self {
        match &config.conversation_model {
            Some(chosen) => Self {
                provider: Some(DraftProvider {
                    name: chosen.provider.clone(),
                    auth: chosen.auth.clone(),
                    auth_origin: chosen.auth_origin,
                }),
                model: Some(chosen.model.clone()),
                alias: chosen.alias.clone(),
            },
            None => Self {
                provider: None,
                model: None,
                alias: None,
            },
        }
    }

    /// The provider named so far.
    pub(super) fn provider(&self) -> Option<&str> {
        self.provider.as_ref().map(|current| current.name.as_str())
    }

    /// The auth of the provider named so far.
    pub(super) fn auth(&self) -> Option<&str> {
        self.provider.as_ref().map(|current| current.auth.as_str())
    }

    /// Moves onto `provider` and `auth`, which the CLI picked for want of
    /// `--auth` from the provider `reference` named: a retired name such as
    /// `xai-oauth` chose its auth; another provider's auth was only filled
    /// in, as nothing chose one for it; the same provider keeps where its
    /// auth came from.
    pub(super) fn take_unnamed_auth(&mut self, reference: &str, provider: &str, auth: &str) {
        let auth_origin = if provider::legacy_provider_alias(reference).is_some() {
            AuthOrigin::Configured
        } else {
            match &self.provider {
                Some(current) if current.name == provider => current.auth_origin,
                _ => AuthOrigin::Inferred,
            }
        };
        self.provider = Some(DraftProvider {
            name: provider.to_owned(),
            auth: auth.to_owned(),
            auth_origin,
        });
    }

    /// Runs on `auth` of `provider`, which `--auth` chose; nothing while no
    /// provider is named.
    pub(super) fn choose_auth(&mut self, provider: &str, auth: &str) {
        if let Some(current) = &mut self.provider {
            current.name = provider.to_owned();
            current.auth = auth.to_owned();
            current.auth_origin = AuthOrigin::Configured;
        }
    }

    /// Runs `model`, resolved from `alias` when given.
    pub(super) fn set_model(&mut self, model: String, alias: Option<String>) {
        self.model = Some(model);
        self.alias = alias;
    }

    /// Runs `model`, keeping the alias it may have come from, which
    /// [`Config::current_model_alias`] drops once it no longer matches.
    pub(super) fn replace_model(&mut self, model: String) {
        self.model = Some(model);
    }

    /// Makes this `config`'s conversation model: set up when it names a
    /// provider and a model, else unset, with the provider it names
    /// without a model, if any.
    pub(super) fn commit(self, config: &mut Config) -> Option<ProviderWithoutModel> {
        let (chosen, without_model) = match (self.provider, self.model) {
            (Some(provider), Some(model)) => (
                Some(ConversationModel {
                    provider: provider.name,
                    model,
                    auth: provider.auth,
                    auth_origin: provider.auth_origin,
                    alias: self.alias,
                }),
                None,
            ),
            (Some(provider), None) => (
                None,
                Some(ProviderWithoutModel {
                    provider: provider.name,
                    auth: provider.auth,
                }),
            ),
            // A model is only ever named with its provider.
            (None, _) => (None, None),
        };
        config.conversation_model = chosen;
        without_model
    }
}