rho-coding-agent 3.2.0

A fast Rust agent harness with a small footprint and opinionated defaults
//! The watch tools as MCP publishes them: `watch` and `unwatch`, and the
//! `notify` argument the HTTP adapter adds to `new` and `prompt`. The core
//! never sees `notify`: [`take_notify`] strips it before the call, since the
//! core's params refuse unknown fields.

use rho_protocol::SessionId;
use rmcp::model::{Tool, ToolAnnotations};
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::{Map, Value};

use crate::app::session_control::{schema_of, Action, ControlError};

pub(super) const WATCH: &str = "watch";
pub(super) const UNWATCH: &str = "unwatch";
const NOTIFY: &str = "notify";

/// A tool the HTTP server answers, parsed once from its name: a core
/// action, or a watch tool.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum HttpTool {
    Core(Action),
    Watch,
    Unwatch,
}

impl HttpTool {
    /// The tool called `name`, if there is one.
    pub(crate) fn from_name(name: &str) -> Option<Self> {
        match name {
            WATCH => Some(Self::Watch),
            UNWATCH => Some(Self::Unwatch),
            _ => Action::from_name(name).map(Self::Core),
        }
    }
}

/// Params of `watch`.
#[derive(Debug, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub(super) struct WatchParams {
    pub(super) session: SessionId,
    /// The webhook to POST the session's news to, by name; one bound to
    /// the caller's token.
    pub(super) webhook: String,
}

/// Params of `unwatch`.
#[derive(Debug, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub(super) struct UnwatchParams {
    pub(super) session: SessionId,
}

/// `watch` and `unwatch`.
pub(crate) fn tools() -> [Tool; 2] {
    [
        Tool::new(
            WATCH,
            "Have this server POST a rho session's news to a webhook: each time it stops to \
             wait for a person, and once when it settles, which ends the watch. Returns where \
             the session is now, as `wait` does; when it settled already, that is the news \
             and this call adds no watch; `watching` says whether one is left, such as one a \
             concurrent `prompt` with `notify` set. `notify` on the next `prompt` watches \
             again. Calling it again replaces the session's watch. `watch_error`, when present, \
             says the server could not save the change, so a restart of the server loses it.",
            schema_of::<WatchParams>(),
        )
        .with_annotations(ToolAnnotations::with_title(WATCH)),
        Tool::new(
            UNWATCH,
            "Stop watching a rho session; nothing more is POSTed for it.",
            schema_of::<UnwatchParams>(),
        )
        .with_annotations(ToolAnnotations::with_title(UNWATCH).idempotent(true)),
    ]
}

/// `action`'s input schema, with `notify` for the actions that start a
/// turn.
pub(crate) fn with_notify(action: Action, mut schema: Map<String, Value>) -> Map<String, Value> {
    if matches!(action, Action::New | Action::Prompt) {
        if let Some(Value::Object(properties)) = schema.get_mut("properties") {
            properties.insert(
                NOTIFY.to_owned(),
                serde_json::json!({
                    "type": "string",
                    "description": "A webhook, by name, to POST the session's news to, as \
                        `watch` does, starting when this turn starts. Once the turn starts the \
                        call succeeds; `watch_error`, when present, says what went wrong with \
                        the watch.",
                }),
            );
        }
    }
    schema
}

/// Removes `notify` from the arguments of an action that starts a turn:
/// the webhook it names, if any. Any other action's arguments are left for
/// the core, which refuses the field.
pub(super) fn take_notify(
    action: Action,
    arguments: &mut Map<String, Value>,
) -> Result<Option<String>, ControlError> {
    if !matches!(action, Action::New | Action::Prompt) {
        return Ok(None);
    }
    match arguments.remove(NOTIFY) {
        None | Some(Value::Null) => Ok(None),
        Some(Value::String(webhook)) => Ok(Some(webhook)),
        Some(other) => Err(ControlError::Invalid(format!(
            "invalid `{}` arguments: `{NOTIFY}` is a webhook name, not {other}",
            action.name()
        ))),
    }
}