rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The devices paired with the bridge, as `rho/device/list` and
//! `rho/device/revoke` see them: one per daemon HTTP token
//! ([`DaemonTokenSet`]), in the credential store rho opened.

use std::collections::BTreeSet;

use rho_protocol::Device;

use super::tokens::DaemonTokenSet;
use crate::app::mcp_server::token_store;

/// Every paired device, by name, `connected` naming those with a
/// connection open through the bridge now.
pub(crate) fn list(connected: &BTreeSet<String>) -> anyhow::Result<Vec<Device>> {
    let tokens: DaemonTokenSet =
        token_store::load(&crate::credential_store::UntrackedCredentialStore)?;
    Ok(devices(&tokens, connected))
}

/// The devices `tokens` pair, sorted by name.
pub(super) fn devices(tokens: &DaemonTokenSet, connected: &BTreeSet<String>) -> Vec<Device> {
    let mut devices: Vec<Device> = tokens
        .tokens
        .iter()
        .map(|token| {
            Device::new(&token.name)
                .with_platform(token.platform.clone())
                .with_times(token.paired_at, token.last_seen)
                .connected(connected.contains(&token.name))
        })
        .collect();
    devices.sort_by(|left, right| left.name.cmp(&right.name));
    devices
}

/// Deletes device `name`'s token; whether it was paired. Its connections
/// close once the bridge reads the tokens again.
pub(crate) fn revoke(name: &str) -> anyhow::Result<bool> {
    token_store::update(|tokens: &mut DaemonTokenSet| Ok(tokens.revoke(name)))
}

#[cfg(test)]
#[path = "devices_tests.rs"]
mod tests;