rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The person who attends a run: who the driver hands what the agent asks
//! a person for (a tool approval, a plan approval, a form), and whose answer
//! it sends back to the agent.
//!
//! A run nobody attends has no [`Person`]: its policy answers every ask
//! itself, and the driver advertises no elicitation, so the agent never asks
//! a question nobody would answer.

use rho_sdk::{HostInputRefusal, HostInputRequest, HostInputResponse};
use tokio::sync::{mpsc, oneshot};

use crate::permission::PermissionMode;

/// Whether a person attends a run, decided when its session opens.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Attendance {
    /// A person opened the session and answers what its agent asks.
    Attended,
    /// Nobody answers: the run's policy answers every ask itself.
    Unattended,
}

impl Attendance {
    /// A run's attendance: attended when it has a person to ask.
    pub(crate) fn of(person: Option<&Person>) -> Self {
        match person {
            Some(_) => Self::Attended,
            None => Self::Unattended,
        }
    }
}

/// What an agent asks the person.
#[derive(Debug)]
pub(crate) enum PersonAsk {
    /// May the agent run its tool call `tool_call`, which it titles `title`
    /// when it gave one?
    Tool {
        tool_call: String,
        title: Option<String>,
    },
    /// May the agent leave plan mode to carry out `plan` in mode `to`?
    PlanExit {
        tool_call: String,
        plan: String,
        to: PermissionMode,
    },
    /// The agent's form, as Rho's questions, asked by tool call `tool_call`
    /// when one did.
    Form {
        tool_call: Option<String>,
        request: HostInputRequest,
    },
}

/// The person's answer to a [`PersonAsk`].
#[derive(Debug, PartialEq)]
pub(crate) enum PersonAnswer {
    /// Run the tool call; a plan exit's approval.
    Allow,
    /// Do not; for a plan exit, keep planning.
    Deny,
    /// The answers to a form, or why there are none.
    Form(Result<HostInputResponse, HostInputRefusal>),
}

/// What the person hears of a run.
pub(crate) enum Heard {
    /// An ask, which waits for their answer.
    Asked(Asked),
    /// The agent left plan mode for this mode, as the person approved: the
    /// run's later asks, and its later runs, are that mode's.
    LeftPlan(PermissionMode),
}

/// One ask on its way to the person, with where the answer goes.
pub(crate) struct Asked {
    pub(crate) ask: PersonAsk,
    pub(crate) reply: oneshot::Sender<PersonAnswer>,
}

/// The line to the person who attends a run.
#[derive(Clone)]
pub(crate) struct Person {
    heard: mpsc::UnboundedSender<Heard>,
}

impl Person {
    /// A person who hears the run on the receiver.
    pub(crate) fn channel() -> (Self, mpsc::UnboundedReceiver<Heard>) {
        let (heard, receiver) = mpsc::unbounded_channel();
        (Self { heard }, receiver)
    }

    /// Tells the person the agent left plan mode for `mode`.
    pub(crate) fn left_plan(&self, mode: PermissionMode) {
        // A person's side that went away hears nothing more.
        let _ = self.heard.send(Heard::LeftPlan(mode));
    }

    /// Asks `ask`. `None` once nobody will answer: the person's side went
    /// away, or withdrew the ask.
    pub(crate) fn ask(
        &self,
        ask: PersonAsk,
    ) -> impl std::future::Future<Output = Option<PersonAnswer>> + Send + 'static {
        let (reply, answer) = oneshot::channel();
        let sent = self.heard.send(Heard::Asked(Asked { ask, reply })).is_ok();
        async move {
            match sent {
                true => answer.await.ok(),
                false => None,
            }
        }
    }
}