rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! An agent's form elicitation (`elicitation/create` in form mode) as Rho's
//! questions, and the person's answers back as the agent's response. ACP's
//! form schema is the same JSON Schema subset as MCP's, so the form maps as
//! an MCP server's does ([`ElicitationForm`]). Only a run a person attends
//! advertises forms ([`super::handshake::initialize_request`]).
//!
//! Whatever Rho cannot show or send back declines, which the agent carries
//! on from; claude-agent-acp tells the model the user skipped the
//! question. Cancel is only for a person who dismissed the form or a run
//! that stopped asking.

use std::collections::BTreeMap;

use agent_client_protocol::v1::schema::{
    CreateElicitationRequest, CreateElicitationResponse, ElicitationAcceptAction,
    ElicitationAction, ElicitationContentValue, ElicitationMode, ElicitationScope,
};
use rho_sdk::{HostInputRefusal, HostInputRequest, HostInputResponse, HostInputSource};

use super::person::{PersonAnswer, PersonAsk};
use crate::tools::mcp::elicitation_form::ElicitationForm;

/// A form an agent asks, ready for the person.
pub(super) struct AgentForm {
    form: ElicitationForm,
}

impl AgentForm {
    /// `request` as the ask to put to the person, and the form that reads
    /// their answers back; or why it declines.
    pub(super) fn of(
        request: &CreateElicitationRequest,
    ) -> Result<(Self, PersonAsk), CreateElicitationResponse> {
        let ElicitationMode::Form(mode) = &request.mode else {
            return Err(declined());
        };
        let tool_call = match &mode.scope {
            ElicitationScope::Session(scope) => {
                scope.tool_call_id.as_ref().map(|id| id.to_string())
            }
            // A form outside a session's turn has nobody to answer it.
            _ => return Err(declined()),
        };
        // The two crates' schemas are the same JSON; one reads the other's.
        let form = serde_json::to_value(&mode.requested_schema)
            .ok()
            .and_then(|schema| serde_json::from_value(schema).ok())
            .and_then(|schema| ElicitationForm::from_schema(&schema).ok())
            .ok_or_else(declined)?;
        let request = HostInputRequest::questionnaire(&request.message, form.questions().to_vec())
            // Whatever is answered, the agent takes as the user's.
            .map(HostInputRequest::user_only)
            .map_err(|_| declined())?;
        Ok((Self { form }, PersonAsk::Form { tool_call, request }))
    }

    /// The agent's response to the person's `answer`; `None` when nobody
    /// answered.
    pub(super) fn response(&self, answer: Option<PersonAnswer>) -> CreateElicitationResponse {
        match answer {
            Some(PersonAnswer::Form(Ok(response))) => self.accepted(&response),
            Some(PersonAnswer::Form(Err(HostInputRefusal::Dismissed))) | None => {
                CreateElicitationResponse::new(ElicitationAction::Cancel)
            }
            // Declined, or a refusal a newer SDK adds.
            Some(PersonAnswer::Form(Err(_))) => declined(),
            // A form takes only a form's answer.
            Some(PersonAnswer::Allow | PersonAnswer::Deny) => declined(),
        }
    }

    fn accepted(&self, response: &HostInputResponse) -> CreateElicitationResponse {
        // Only the user accepts for the user.
        if response.source() != HostInputSource::User {
            return declined();
        }
        let content = self.form.content(response).ok().and_then(|content| {
            serde_json::from_value::<BTreeMap<String, ElicitationContentValue>>(content).ok()
        });
        match content {
            Some(content) => CreateElicitationResponse::new(ElicitationAction::Accept(
                ElicitationAcceptAction::new().content(content),
            )),
            None => declined(),
        }
    }
}

fn declined() -> CreateElicitationResponse {
    CreateElicitationResponse::new(ElicitationAction::Decline)
}