rho-coding-agent 3.10.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The runs a save links the `agent` call results it writes to, so a replay
//! names each call's subagent after the session's subagents forget the run,
//! as after a daemon restart.

use rho_sdk::model::{ContentBlock, Message};

use crate::{
    app::subagent_manager::SubagentManager, session::tree::DisplaySubagents,
    tools::agent::starts_subagent,
};

/// The run each of `rows`, the display rows a save of `history` adds,
/// links, by its place among them: a successful result of an `agent` call
/// whose run `subagents` knows, as the run showed when the call finished.
pub(super) fn linked_subagents(
    rows: &[Message],
    history: &[Message],
    subagents: Option<&SubagentManager>,
) -> DisplaySubagents {
    let Some(subagents) = subagents else {
        return DisplaySubagents::new();
    };
    rows.iter()
        .enumerate()
        .filter_map(|(row, message)| {
            let Message::ToolResult(result) = message else {
                return None;
            };
            if result.is_failure() || !starts_subagent(call_name(history, &result.id)?) {
                return None;
            }
            Some((row, subagents.run_started_by(&result.id)?))
        })
        .collect()
}

/// The tool the latest call `history` made under `id` called.
fn call_name<'h>(history: &'h [Message], id: &str) -> Option<&'h str> {
    history
        .iter()
        .rev()
        .filter_map(Message::completed_assistant_content)
        .flat_map(|content| content.iter().rev())
        .find_map(|block| match block {
            ContentBlock::ToolCall(call) if call.id == id => Some(call.name.as_str()),
            ContentBlock::ToolCall(_) | ContentBlock::Text(_) | ContentBlock::Image(_) => None,
        })
}

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