rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! What the agent asks on v2 (a permission ask, a form), handled as on v1.
//!
//! The connection's handlers pass each ask to the turn ([`AgentAsk`]),
//! which decides it like the v1 client does ([`crate::acp_runtime::asks`]):
//! a call of a tool the run serves is allowed once, and the policy decides
//! the rest, putting it to the person who attends the run when it says so.
//! The shared handling takes v1 shapes, so an ask goes in in v1 form and
//! its answer goes back in v2 form.
//!
//! On v2, a permission ask carries its own `title` and asks about a
//! `subject`, which claude-agent-acp 0.114.0 always makes the tool call, as
//! the same patch its `tool_call_update` carries. A form names its tool
//! call at the top level of its params, which v1 scopes the same way, so it
//! crosses as its JSON.

use super::{bridge, tool_calls};
use crate::acp_runtime::{
    asks::{self, Asker, PendingAsk},
    deciders::Deciders,
    policy::AcpAgentPolicy,
};
use agent_client_protocol::{
    v1::schema as v1,
    v2::{schema as v2, Responder},
    Error,
};
use std::collections::HashSet;

/// One ask of the agent's, with where its answer goes.
pub(super) enum AgentAsk {
    Permission(
        v2::RequestPermissionRequest,
        Responder<v2::RequestPermissionResponse>,
    ),
    Form(
        v2::CreateElicitationRequest,
        Responder<v2::CreateElicitationResponse>,
    ),
}

/// Answers `ask` now, or puts it to the person: the ask that then waits.
pub(super) fn answer<'a, P: AcpAgentPolicy>(
    ask: AgentAsk,
    deciders: &Deciders<'_, P>,
    asker: Asker<'a>,
    rejected: &mut HashSet<v1::ToolCallId>,
) -> Result<Option<PendingAsk<'a>>, Error> {
    match ask {
        AgentAsk::Permission(request, responder) => {
            // Nothing runs on an ask about no tool call: it fails closed.
            let Some(request) = v1_permission(&request) else {
                responder.respond(v2::RequestPermissionResponse::new(cancelled()))?;
                return Ok(None);
            };
            let reply = Box::new(move |response: v1::RequestPermissionResponse| {
                responder.respond(v2::RequestPermissionResponse::new(v2_outcome(
                    response.outcome,
                )))
            });
            asks::permission(request, reply, deciders, asker, rejected)
        }
        AgentAsk::Form(request, responder) => {
            let request = match bridge::<_, v1::CreateElicitationRequest>(&request) {
                Ok(request) => request,
                // A form v1 has no shape for is one Rho cannot show.
                Err(_) => {
                    return responder
                        .respond(v2::CreateElicitationResponse::new(
                            v2::ElicitationAction::Decline,
                        ))
                        .map(|()| None)
                }
            };
            let reply = Box::new(move |response: v1::CreateElicitationResponse| {
                responder.respond(bridge(&response)?)
            });
            asks::form(&request, reply, asker)
        }
    }
}

/// `request` in v1 form, about the tool call it names, which it titles
/// with its own title; `None` for an ask about anything else.
fn v1_permission(request: &v2::RequestPermissionRequest) -> Option<v1::RequestPermissionRequest> {
    let Some(v2::RequestPermissionSubject::ToolCall(subject)) = &request.subject else {
        return None;
    };
    let mut call = tool_calls::v1_update(&subject.tool_call);
    call.fields.title = Some(request.title.clone());
    // An option of a kind v1 does not have is one Rho cannot choose.
    let options = request
        .options
        .iter()
        .filter_map(|option| bridge(option).ok())
        .collect();
    Some(v1::RequestPermissionRequest::new(
        request.session_id.to_string(),
        call,
        options,
    ))
}

fn v2_outcome(outcome: v1::RequestPermissionOutcome) -> v2::RequestPermissionOutcome {
    match outcome {
        v1::RequestPermissionOutcome::Selected(selected) => v2::RequestPermissionOutcome::Selected(
            v2::SelectedPermissionOutcome::new(selected.option_id.to_string()),
        ),
        _ => cancelled(),
    }
}

fn cancelled() -> v2::RequestPermissionOutcome {
    v2::RequestPermissionOutcome::Cancelled(v2::CancelledPermissionOutcome::new())
}

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