rho-coding-agent 3.10.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The daemon's side of `rho/runtime/list`: the runtimes a session may run
//! on, whether the client's machine can run each, and the models a session
//! on each may run, as far as the daemon knows them without one.
//!
//! Rho's models are `rho/model/list`'s. Claude Code's are the list rho
//! ships, the CLI's default first, as `rho/model/list`'s `claude_cli`
//! offers them for a model setting. Antigravity lists its models only in a
//! session's config options, so they are the ones its last new session
//! offered, by a run or a `rho/model/refresh` probe, and unlisted before
//! one ([`crate::antigravity_runtime::models`]).

use rho_protocol::{self as wire, ModelChoice, ModelRef, RpcError, RuntimeListParams};

use super::sessions::session_environment;
use crate::{
    agent::AgentRuntime, app::session_events::runtime_model,
    claude_runtime::models::offered_models, session_environment::Ambient,
};

/// Answers `rho/runtime/list` for the client whose environment `params`
/// carries: every runtime, rho's first.
pub(super) fn list(params: RuntimeListParams) -> Result<wire::RuntimeListResponse, RpcError> {
    let ambient = session_environment(&params.environment)?;
    let runtimes = AgentRuntime::ALL
        .into_iter()
        .map(|runtime| {
            wire::RuntimeInfo::new(
                runtime.into(),
                unavailable(runtime, &ambient),
                models(runtime),
            )
        })
        .collect();
    Ok(wire::RuntimeListResponse::new(runtimes))
}

/// Why `ambient`'s machine cannot run a session on `runtime`: its program
/// is not on the `PATH`. Signed out is not a reason: the agent signs in
/// itself, and its first turn says so.
pub(super) fn unavailable(runtime: AgentRuntime, ambient: &Ambient) -> Option<String> {
    match runtime {
        AgentRuntime::Rho => None,
        AgentRuntime::ClaudeCli => crate::claude_runtime::executable::resolve(ambient)
            .err()
            .map(|missing| missing.to_string()),
        AgentRuntime::Antigravity => crate::antigravity_runtime::executable::resolve(ambient)
            .err()
            .map(|missing| missing.to_string()),
    }
}

/// The models a session on `runtime` may run, as the daemon knows them
/// before one runs.
fn models(runtime: AgentRuntime) -> wire::RuntimeModels {
    match runtime {
        AgentRuntime::Rho => wire::RuntimeModels::ModelList,
        AgentRuntime::ClaudeCli => {
            // Named as the session's options name them, the CLI's default
            // included.
            let models = offered_models()
                .map(|(model, _)| {
                    let model = runtime_model(model);
                    ModelChoice {
                        model: runtime_ref(runtime, model),
                        label: model.to_owned(),
                    }
                })
                .collect();
            wire::RuntimeModels::Static {
                models,
                default: runtime_ref(runtime, runtime_model(None)),
            }
        }
        AgentRuntime::Antigravity => match crate::antigravity_runtime::models::last_seen() {
            Some(seen) => wire::RuntimeModels::LastSeen {
                models: seen
                    .models
                    .into_iter()
                    .map(|model| ModelChoice {
                        model: runtime_ref(runtime, &model.model),
                        label: model.name,
                    })
                    .collect(),
                default: runtime_ref(runtime, &seen.default),
                seen_at: seen.seen_at,
            },
            None => wire::RuntimeModels::Unlisted,
        },
    }
}

/// Reads again the models of each runtime that lists them as last seen,
/// whose program `ambient`'s machine has, for a full `rho/model/refresh`.
/// A failure is logged and leaves the models last seen, as a provider's
/// failed list leaves the others.
pub(super) async fn refresh_models(ambient: &Ambient) {
    let refreshes = AgentRuntime::ALL.into_iter().map(|runtime| async move {
        match runtime {
            // Rho's are the provider lists the refresh fetches; Claude
            // Code's ship with rho.
            AgentRuntime::Rho | AgentRuntime::ClaudeCli => {}
            AgentRuntime::Antigravity => {
                if unavailable(runtime, ambient).is_some() {
                    return;
                }
                if let Err(error) = crate::antigravity_runtime::models::probe(ambient).await {
                    tracing::warn!("could not list antigravity's models: {error}");
                }
            }
        }
    });
    futures_util::future::join_all(refreshes).await;
}

/// `model` of `runtime` as a session on it names it.
fn runtime_ref(runtime: AgentRuntime, model: &str) -> ModelRef {
    ModelRef {
        provider: runtime
            .model_provider()
            .expect("only an agent's runtime lists its models here")
            .to_owned(),
        model: model.to_owned(),
    }
}

/// The runtime that runs `model`: an agent's runtime for the provider it
/// names its models under, which no custom provider may take, else rho's.
pub(super) fn model_runtime(model: &ModelRef) -> AgentRuntime {
    AgentRuntime::ALL
        .into_iter()
        .find(|runtime| runtime.model_provider() == Some(model.provider.as_str()))
        .unwrap_or(AgentRuntime::Rho)
}