rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! What the v2 client renders of its session's updates, as the v1 updates
//! rho's renderer takes, and how a v2 turn ends.
//!
//! The kinds whose wire shape v2 kept cross through [`bridge`]: the agent's
//! message and thought chunks, and usage. Tool calls, which v2 reports as
//! one kind of update, become v1's call and its updates ([`ToolCalls`]).
//! A checklist plan (`plan_update` with items, as claude-agent-acp 0.114.0
//! sends TodoWrite's) becomes v1's `plan`, and its removal an empty one; v1
//! has no shape for a markdown plan. The rest are not rendered on v2: the session's state ends turns
//! ([`TurnEnd`]), and its config options are checked against the mode fence
//! ([`super::setup::check_mode`]).

use super::{bridge, tool_calls::ToolCalls};
use crate::acp_runtime::turn::{classify_stop, TurnClassification};
use agent_client_protocol::{v1::schema as v1, v2::schema as v2, Error};

/// The v1 update rho renders for `update`, given the session's tool calls
/// so far; `None` for a kind the v2 client does not render.
pub(super) fn rendered(
    update: &v2::SessionUpdate,
    tool_calls: &mut ToolCalls,
) -> Option<Result<v1::SessionUpdate, Error>> {
    match update {
        v2::SessionUpdate::AgentMessageChunk(_)
        | v2::SessionUpdate::AgentThoughtChunk(_)
        | v2::SessionUpdate::UsageUpdate(_) => Some(bridge(update)),
        v2::SessionUpdate::ToolCallUpdate(call) => Some(Ok(tool_calls.v1(call))),
        v2::SessionUpdate::PlanUpdate(v2::PlanUpdate {
            plan: v2::PlanUpdateContent::Items(items),
            ..
        }) => Some(
            bridge(&items.entries).map(|entries| v1::SessionUpdate::Plan(v1::Plan::new(entries))),
        ),
        v2::SessionUpdate::PlanRemoved(_) => {
            Some(Ok(v1::SessionUpdate::Plan(v1::Plan::new(Vec::new()))))
        }
        _ => None,
    }
}

/// How a v2 turn ended: the `idle` state that closed foreground work the
/// turn's prompt started.
pub(super) struct TurnEnd {
    pub(super) classification: TurnClassification,
    /// What the renderer labels the turn's end with; `None` for a stop
    /// reason v1 has no name for.
    pub(super) reason: Option<v1::StopReason>,
}

impl TurnEnd {
    /// An agent may leave the stop reason out (v2 makes it optional); a turn
    /// that ended without one ended as the agent meant it to. Once Rho sent
    /// `session/cancel`, the turn is stopped whatever the agent reports.
    pub(super) fn of(stop: Option<&v2::StopReason>, cancelled_by_us: bool) -> Self {
        let reason = match stop {
            None | Some(v2::StopReason::EndTurn) => Some(v1::StopReason::EndTurn),
            Some(v2::StopReason::MaxTokens) => Some(v1::StopReason::MaxTokens),
            Some(v2::StopReason::MaxTurnRequests) => Some(v1::StopReason::MaxTurnRequests),
            Some(v2::StopReason::Refusal) => Some(v1::StopReason::Refusal),
            Some(v2::StopReason::Cancelled) => Some(v1::StopReason::Cancelled),
            Some(_) => None,
        };
        let classification = match (&reason, stop) {
            (Some(reason), _) => classify_stop(reason, cancelled_by_us),
            _ if cancelled_by_us => TurnClassification::Stopped,
            (None, Some(v2::StopReason::Error(stopped))) => TurnClassification::AgentFailed {
                message: stopped
                    .error
                    .as_deref()
                    .map(|failure| failure.message.clone()),
            },
            (None, _) => TurnClassification::Invalid,
        };
        Self {
            classification,
            reason,
        }
    }
}

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