rho-coding-agent 3.7.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! `rho/config/table/*`: the structured tables of `config.toml` a client
//! copies between daemons row by row, such as provider profiles, read and
//! written entry by entry (`crate::config::read_table`).
//!
//! A table write never moves the agent namespace's revision, which hashes
//! only the keys settings read as the file holds them
//! (`crate::config::agent_revision`), so it never makes a pending
//! `rho/config/set` stale. A write that moves a setting's resolved value,
//! such as the alias an `@alias` model names, still publishes the agent
//! namespace's new values, under the same revision, and the config reaches
//! turns and sessions as a `rho/config/set` does.

use rho_protocol::{
    ConfigTable, ConfigTableGetParams, ConfigTableGetResponse, ConfigTableSetParams,
    ConfigTableSetResponse, ErrorCode, RpcError,
};

use super::{change_error, file, ConfigStore};
use crate::config::{apply_table_changes, read_table, table_revision, EditableTable};

impl ConfigStore {
    /// Answers `rho/config/table/get`: the table as `config.toml` holds it,
    /// whether or not the file loads.
    pub(in crate::app::daemon_host) async fn get_table(
        &self,
        params: ConfigTableGetParams,
    ) -> Result<ConfigTableGetResponse, RpcError> {
        let table = editable(params.table)?;
        let (revision, entries) = read_table(&file::read_document(&self.agent_path)?, table);
        Ok(ConfigTableGetResponse::new(revision, entries))
    }

    /// Answers `rho/config/table/set`: applies every change, or none, while
    /// the table is still at the params' revision and the file still loads
    /// with them.
    pub(in crate::app::daemon_host) async fn set_table(
        &self,
        params: ConfigTableSetParams,
    ) -> Result<ConfigTableSetResponse, RpcError> {
        let table = editable(params.table)?;
        let mut file = self.agent.lock().await;
        // Locked before the revision check, so the table the client read is
        // the one changed.
        let mut edit = file::Edit::open(&self.agent_path)?;
        if table_revision(&edit.doc, table) != params.revision {
            return Err(RpcError::new(
                ErrorCode::ConfigConflict,
                "the table changed since that revision; read it again",
            ));
        }
        let loaded =
            apply_table_changes(&mut edit.doc, table, &params.changes).map_err(change_error)?;
        let (doc, _) = self.save_agent(&mut file, edit, loaded)?;
        Ok(ConfigTableSetResponse::new(table_revision(&doc, table)))
    }
}

fn editable(table: ConfigTable) -> Result<EditableTable, RpcError> {
    EditableTable::from_wire(table)
        .ok_or_else(|| RpcError::new(ErrorCode::InvalidParams, "unknown config table"))
}