rho-coding-agent 3.7.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Rho's `_meta` markers on ACP v1 types, for what v1 cannot express. The
//! adapter speaks v1 (`updates.rs`); the ACP v2 boundary reads the markers
//! back to send v2's own fields, and strips them. A v1 host may ignore
//! them: each sits on a v1 shape that still says what happened.

use std::path::{Path, PathBuf};

use agent_client_protocol::v1::schema::Meta;
use rho_protocol::SubagentRef;
use serde_json::Value;

/// On a deleted file's diff: v1 diffs cannot tell it from an emptied one.
pub(super) const DELETED_FILE: &str = "rho/fileDeleted";

/// On the diff of the new file a move created, naming the source, whose
/// deletion the same call reports: v1 diffs have no move.
pub(super) const MOVED_FROM: &str = "rho/movedFrom";

/// On a call a cancel interrupted, which closes `failed`: v1 has no
/// `cancelled` tool status.
pub(super) const TOOL_CANCELLED: &str = "rho/toolCancelled";

/// On the content Rho puts in an `agent` call for the subagent it started,
/// holding the subagent (`rho_protocol::SubagentRef`): ACP v2 shows it as a
/// `session` content item, and v1 has nothing to show it as.
pub(super) const SUBAGENT: &str = "rho/subagent";

/// `_meta` holding marker `key`.
pub(super) fn flag(key: &str) -> Meta {
    Meta::from_iter([(key.to_owned(), Value::Bool(true))])
}

/// `_meta` holding marker `key` naming `path`, or `None` for a path JSON
/// cannot hold.
pub(super) fn path(key: &str, path: &Path) -> Option<Meta> {
    let path = path.to_str()?;
    Some(Meta::from_iter([(key.to_owned(), Value::from(path))]))
}

/// `_meta` holding the [`SUBAGENT`] marker naming `child`.
pub(super) fn subagent(child: &SubagentRef) -> Option<Meta> {
    let child = serde_json::to_value(child).ok()?;
    Some(Meta::from_iter([(SUBAGENT.to_owned(), child)]))
}

/// The subagent the [`SUBAGENT`] marker in `meta` names, left in place.
pub(super) fn subagent_in(meta: Option<&Meta>) -> Option<SubagentRef> {
    serde_json::from_value(meta?.get(SUBAGENT)?.clone()).ok()
}

/// The subagent the [`SUBAGENT`] marker in `meta` names, which it loses.
pub(super) fn take_subagent(meta: &mut Option<Meta>) -> Option<SubagentRef> {
    serde_json::from_value(take(meta, SUBAGENT)?).ok()
}

/// Whether `meta` holds flag `key`, which it loses.
pub(super) fn take_flag(meta: &mut Option<Meta>, key: &str) -> bool {
    take(meta, key).is_some_and(|marker| marker == true)
}

/// The path marker `key` in `meta` names, which it loses.
pub(super) fn take_path(meta: &mut Option<Meta>, key: &str) -> Option<PathBuf> {
    take(meta, key)?.as_str().map(PathBuf::from)
}

/// Removes marker `key` from `meta`, and `meta` itself once empty.
fn take(meta: &mut Option<Meta>, key: &str) -> Option<Value> {
    let marker = meta.as_mut()?.remove(key);
    if meta.as_ref().is_some_and(Meta::is_empty) {
        *meta = None;
    }
    marker
}