rho-coding-agent 3.5.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The settings a daemon session takes as the TUI opens it: a launch
//! flag's and those chosen before a new session's first prompt, which must
//! apply, and the ones carried from the session it replaces, after `/new`
//! or a daemon restart, which apply where the daemon takes them.

use rho_protocol::{
    methods::SessionConfigure, ClientError, SessionChange, SessionConfigureParams, SessionId,
    SessionOptions,
};

use crate::app::daemon_client::Link;

/// How a session reports a change of model it takes as it opens.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum ModelChange {
    /// As its setup, as a launch flag sets it up: nothing to report.
    Setup,
    /// As a switch the person made, which names the prompt file the
    /// session now runs on.
    Switch,
}

/// The changes that move a session from the options `from` to `to`, in
/// the order they apply: a model first, since it decides which
/// reasoning levels and fast mode the others may pick. The session
/// reports the model's change as `model` says.
pub(super) fn changes_between(
    from: &SessionOptions,
    to: &SessionOptions,
    model: ModelChange,
) -> Vec<SessionChange> {
    let mut changes = Vec::new();
    if from.model.current != to.model.current || from.model.auth != to.model.auth {
        changes.push(SessionChange::Model {
            model: to.model.current.clone(),
            auth: Some(to.model.auth.clone()),
            setup: model == ModelChange::Setup,
        });
    }
    if let Some(reasoning) = to.reasoning.as_ref().filter(|reasoning| {
        from.reasoning.as_ref().map(|from| from.current) != Some(reasoning.current)
    }) {
        changes.push(SessionChange::Reasoning {
            level: reasoning.current,
        });
    }
    let fast =
        |options: &SessionOptions| options.fast_mode.as_ref().is_some_and(|fast| fast.enabled);
    if fast(from) != fast(to) {
        changes.push(SessionChange::FastMode { enabled: fast(to) });
    }
    if from.permission_mode.current != to.permission_mode.current {
        changes.push(SessionChange::PermissionMode {
            mode: to.permission_mode.current,
        });
    }
    changes
}

/// A change the daemon refused, and why.
pub(super) struct Refused {
    pub(super) change: SessionChange,
    pub(super) error: ClientError,
}

/// Applies `changes` to `session` in order, returning its options
/// after the last one that applied, or the first refusal.
pub(super) async fn configure_all(
    link: &Link,
    session: &SessionId,
    changes: Vec<SessionChange>,
) -> Result<Option<SessionOptions>, Box<Refused>> {
    let mut options = None;
    for change in changes {
        let params = SessionConfigureParams {
            session: session.clone(),
            change,
        };
        match link.request::<SessionConfigure>(&params).await {
            Ok(response) => options = Some(response.options),
            Err(error) => {
                return Err(Box::new(Refused {
                    change: params.change,
                    error,
                }))
            }
        }
    }
    Ok(options)
}

/// Carries `changes` to `session`; one the daemon refuses keeps its
/// default. Returns the session's options after the last change that
/// applied, if any did.
pub(super) async fn carry(
    link: &Link,
    session: &SessionId,
    changes: Vec<SessionChange>,
) -> Option<SessionOptions> {
    let mut options = None;
    for change in changes {
        match configure_all(link, session, vec![change]).await {
            Ok(changed) => options = changed.or(options),
            Err(refused) => {
                tracing::warn!(error = %refused.error, "could not carry a setting to the new daemon session");
            }
        }
    }
    options
}