rho-coding-agent 3.10.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! What a tool call shows besides its card, decided once for every call
//! rho runs: a Rho session's live call ([`super::LiveEvents`]) and a call
//! rho serves an agent on another runtime (the daemon's served tools, whose
//! cards go through the run's journal), so a client reads both alike.

use std::path::Path;

use rho_protocol::{ToolContent, ToolStatus};
use rho_sdk::{tool::ToolProgress, ToolCompletion};

use super::{output_content, text_content};

/// The A2UI surface a call's `progress` shows, as a waiting `ui` call
/// reports it: kept by the caller to show again when the call ends
/// ([`finished_content`]).
pub(crate) fn progress_surface(progress: &ToolProgress) -> Option<ToolContent> {
    crate::tools::ui::shown_surface(progress.metadata())
}

/// What a call shows besides its card as it reports `progress`: the
/// progress message, then `surface`, the one the progress shows
/// ([`progress_surface`]).
pub(crate) fn progress_content(
    progress: &ToolProgress,
    surface: Option<ToolContent>,
) -> Vec<ToolContent> {
    std::iter::once(text_content(progress.message()))
        .chain(surface)
        .collect()
}

/// What a call that ended with `result` shows besides its card: its result
/// as the model got it, with the files it changed (paths under `cwd`),
/// then the A2UI surface its output names, else `reported`, the last one it
/// showed while it ran, then the browser tab it acted on.
pub(crate) fn finished_content(
    result: &ToolCompletion,
    cwd: &Path,
    reported: Option<ToolContent>,
) -> Vec<ToolContent> {
    let mut content = match result {
        ToolCompletion::Completed(output) => output_content(output, cwd),
        ToolCompletion::Failure(failure) => vec![text_content(failure.message())],
        // The model called a tool that does not exist; say so on the card.
        ToolCompletion::Unavailable => vec![text_content("tool is unavailable")],
        // ToolCompletion is non_exhaustive: an unknown result shows nothing.
        _ => Vec::new(),
    };
    let presentation = result.output().map(|output| output.presentation());
    let surface = presentation
        .and_then(crate::tools::ui::shown_surface)
        .or(reported);
    content.extend(surface);
    content.extend(
        presentation
            .map(crate::browser::view::shown_tab)
            .unwrap_or_default(),
    );
    content
}

/// How a call that ended with `result` ended, as its card's update says.
pub(super) fn finished_status(result: &ToolCompletion) -> ToolStatus {
    match result {
        ToolCompletion::Completed(output) if !output.is_failure() => ToolStatus::Completed,
        // A failed output, a failure, a tool that does not exist, and a
        // result a newer SDK adds (`ToolCompletion` is non_exhaustive),
        // which cannot be shown as success.
        _ => ToolStatus::Failed,
    }
}