rho-coding-agent 3.10.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The card a served call draws, the one a Rho session draws for its call
//! of the same tool, from the same presenter: started from the call's
//! arguments and the tool's start metadata, updated by its progress, and
//! finished by its result. What it shows besides its card is what a Rho
//! session's call shows (`session_events/call_content.rs`): its progress
//! and its result, a `ui` call's A2UI surface while it waits and once it
//! ends, the browser tab a browser call acted on, and what the call
//! links once it ends ([`LinkSources::content`]).

use std::path::PathBuf;

use rho_protocol::ToolContent;
use rho_sdk::{
    model::ToolCall,
    tool::{ToolMetadata, ToolProgress},
    ToolCallId, ToolCompletion,
};
use serde_json::Value;

use crate::{
    acp_runtime::agent_tools::CallCards,
    app::{
        interactive_presenter::InteractiveToolPresenter,
        session_events::{
            call_content::{finished_content, progress_content, progress_surface},
            Link, LinkSources,
        },
    },
    cli_runtime::agent_event::ServedCard,
    tools::mcp::exported_name::McpToolNames,
};

/// What a session's served cards draw with: its working directory, which
/// the cards show paths relative to, and its processes and subagents,
/// which they link.
#[derive(Clone)]
pub(super) struct CardScope {
    pub(super) cwd: PathBuf,
    pub(super) links: LinkSources,
}

/// One served call's card, from its start to its end.
pub(super) struct CallCard {
    /// Draws this call alone, as a Rho session's presenter draws each of
    /// its calls.
    presenter: InteractiveToolPresenter,
    id: ToolCallId,
    cards: CallCards,
    scope: CardScope,
    /// The last surface the call's progress showed, which its end shows
    /// again unless its output names one.
    reported: Option<ToolContent>,
    /// What the call links once it ends, if anything.
    link: Option<Link>,
}

impl CallCard {
    /// Draws call `id` of tool `name` starting, with `arguments` and the
    /// tool's start `metadata`, in the session `scope` names.
    pub(super) fn start(
        cards: CallCards,
        scope: CardScope,
        id: ToolCallId,
        name: &str,
        arguments: &Value,
        metadata: ToolMetadata,
    ) -> Self {
        let link = Link::of(name, Some(arguments));
        // rho's own tools, none of an MCP server's.
        let mut presenter =
            InteractiveToolPresenter::new(scope.cwd.clone(), McpToolNames::default());
        presenter.proposed(ToolCall::new(
            id.to_string(),
            name.to_owned(),
            arguments.clone(),
        ));
        let card = presenter
            .started(id.clone(), name.to_owned(), metadata)
            .card;
        cards.draw(ServedCard::Started(card));
        Self {
            presenter,
            id,
            cards,
            scope,
            reported: None,
            link,
        }
    }

    /// Draws the call's `progress`, with what it shows besides the card.
    pub(super) fn progress(&mut self, progress: &ToolProgress) {
        let card = self.presenter.updated(&self.id, progress).card;
        let surface = progress_surface(progress);
        if surface.is_some() {
            self.reported.clone_from(&surface);
        }
        let content = progress_content(progress, surface);
        self.cards.draw(ServedCard::Updated { card, content });
    }

    /// Draws the call ended with `result`, with what it shows besides the
    /// card, then what it links, as a Rho session's call does
    /// (`session_events/live.rs`). The tool ran under this call's id, so
    /// the work it started names the call by it.
    pub(super) fn finished(mut self, result: &ToolCompletion) {
        let (_ok, shown) = self.presenter.finished(&self.id, result);
        let mut content = finished_content(result, &self.scope.cwd, self.reported.take());
        let linked = self
            .link
            .and_then(|link| self.scope.links.content(link, self.id.as_str(), result));
        content.extend(linked);
        self.cards.draw(ServedCard::Finished {
            presentation: shown.presentation,
            content,
        });
    }
}

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