rho-coding-agent 3.4.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! ACP request fields as the daemon protocol carries them: prompt content,
//! host MCP servers, a session's setup, and the session list.

use std::{
    collections::{BTreeMap, BTreeSet},
    path::{Path, PathBuf},
};

use agent_client_protocol::{
    v1::schema::{
        ContentBlock as AcpContentBlock, EmbeddedResourceResource, ListSessionsResponse,
        McpServer as AcpMcpServer, SessionId, SessionInfo,
    },
    Error as AcpError,
};
use rho_protocol::{
    ChildThreads, ContentBlock, McpServer, SecretMap, SessionListResponse, SessionPart,
    SessionSetup, SessionSummary,
};

use super::{
    host_features::{Forms, HostFeatures, Turns},
    t3_prompt,
};
use crate::app::prompt_content::fence_context;

/// What `rho acp`'s own flags put in every session it opens or reopens:
/// `--agent`, `--add-dir`, the `--no-*` flags, and `--t3code`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(super) struct Launch {
    pub(super) agent: Option<String>,
    /// `--add-dir` directories, resolved against the process cwd.
    pub(super) added_dirs: Vec<PathBuf>,
    /// What the `--no-*` flags leave out.
    pub(super) without: BTreeSet<SessionPart>,
    pub(super) host: LaunchHost,
}

/// The host `rho acp` was launched for.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(super) enum LaunchHost {
    /// `--t3code`: child agents run as T3 threads, and T3's wrapping of
    /// each prompt reaches the model as context, not the user message.
    T3Code,
    #[default]
    Other,
}

/// Which build the daemon behind this adapter is.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum DaemonBuild {
    /// This one, which reads every block this adapter sends.
    Same,
    /// Another, which may read a block newer than it as unknown.
    Other,
}

impl Launch {
    /// Prompt `prompt` as the daemon takes it from this launch's host. A
    /// daemon of another build gets T3's wrapping as text, as before
    /// context blocks, so an older one that does not know them still hands
    /// it to the model.
    pub(super) fn prompt_content(
        &self,
        prompt: &[AcpContentBlock],
        daemon: DaemonBuild,
    ) -> Vec<ContentBlock> {
        let content = prompt_content(prompt);
        match (self.host, daemon) {
            (LaunchHost::T3Code, DaemonBuild::Same) => t3_prompt::split_context(content),
            (LaunchHost::T3Code, DaemonBuild::Other) | (LaunchHost::Other, _) => content,
        }
    }

    /// The setup of a session the host opens with `directories` and
    /// `servers`, as far as the host's `features` allow. A host that draws
    /// no forms gets no questionnaire, since it could not answer one.
    pub(super) fn setup(
        &self,
        directories: &[PathBuf],
        servers: &[AcpMcpServer],
        features: HostFeatures,
    ) -> SessionSetup {
        let mut additional_directories = self.added_dirs.clone();
        for directory in directories {
            if !additional_directories.contains(directory) {
                additional_directories.push(directory.clone());
            }
        }
        let mut without = self.without.clone();
        match features.forms {
            Forms::Available(_) => {}
            Forms::Unavailable => {
                without.insert(SessionPart::Questions);
            }
        }
        SessionSetup {
            additional_directories,
            mcp_servers: protocol_servers(servers),
            without,
            child_threads: match self.host {
                LaunchHost::T3Code => ChildThreads::Host,
                LaunchHost::Other => ChildThreads::Daemon,
            },
            idle_wake: match features.turns {
                // The client is told of a turn only as its own prompt's, so
                // a prompt waits for the background work it started.
                Turns::Prompted => rho_protocol::IdleWake::Never,
                // The client follows turns the daemon starts as background
                // work finishes, so a prompt ends while that work runs on.
                Turns::Any => rho_protocol::IdleWake::Turn,
            },
            // A missing login is the ACP client's `auth_required`.
            sign_in: rho_protocol::SignIn::Required,
            // ACP has no A2UI surfaces to draw.
            surfaces: rho_protocol::Surfaces::Unsupported,
        }
    }
}

/// Accepts only an absolute path that already exists as a directory, as a
/// new session's workspace.
pub(super) fn session_cwd(cwd: &Path) -> Result<PathBuf, AcpError> {
    if !cwd.is_absolute() {
        return Err(AcpError::invalid_params().data("ACP session cwd must be an absolute path"));
    }
    if !cwd.is_dir() {
        return Err(
            AcpError::invalid_params().data("ACP session cwd must be an existing directory")
        );
    }
    Ok(cwd.to_path_buf())
}

/// ACP prompt blocks as the daemon's prompt content. Blocks the wire has no
/// kind for (audio, a binary resource, an ACP block this build does not
/// know) become fenced text, so the model still sees them.
fn prompt_content(prompt: &[AcpContentBlock]) -> Vec<ContentBlock> {
    prompt.iter().map(wire_block).collect()
}

fn wire_block(block: &AcpContentBlock) -> ContentBlock {
    let fenced = |kind: &str, header: &str, body: &str| ContentBlock::Text {
        text: fence_context(kind, header, body),
    };
    match block {
        AcpContentBlock::Text(text) => ContentBlock::Text {
            text: text.text.clone(),
        },
        AcpContentBlock::Image(image) => ContentBlock::Image {
            mime_type: image.mime_type.clone(),
            data: image.data.clone(),
        },
        AcpContentBlock::Audio(audio) => fenced("audio", &audio.mime_type, &audio.data),
        AcpContentBlock::ResourceLink(link) => ContentBlock::ResourceLink {
            uri: link.uri.clone(),
            name: link.name.clone(),
            description: link.description.clone(),
        },
        AcpContentBlock::Resource(embedded) => match &embedded.resource {
            EmbeddedResourceResource::TextResourceContents(resource) => ContentBlock::Resource {
                uri: resource.uri.clone(),
                text: resource.text.clone(),
            },
            EmbeddedResourceResource::BlobResourceContents(resource) => {
                let header = match &resource.mime_type {
                    Some(mime_type) => format!("{} {mime_type}", resource.uri),
                    None => resource.uri.clone(),
                };
                fenced("resource", &header, &resource.blob)
            }
            _ => fenced("resource", "embedded", ""),
        },
        _ => fenced("context", "unsupported", ""),
    }
}

/// The servers ACP `servers` name, in order, but those whose transport Rho
/// cannot speak, which are logged and skipped.
fn protocol_servers(servers: &[AcpMcpServer]) -> Vec<McpServer> {
    servers.iter().filter_map(protocol_server).collect()
}

fn protocol_server(server: &AcpMcpServer) -> Option<McpServer> {
    match server {
        AcpMcpServer::Stdio(stdio) => Some(McpServer::Stdio {
            name: stdio.name.clone(),
            command: stdio.command.clone(),
            args: stdio.args.clone(),
            env: by_name(
                &stdio.name,
                stdio
                    .env
                    .iter()
                    .map(|variable| (variable.name.as_str(), variable.value.as_str())),
            ),
        }),
        AcpMcpServer::Http(http) => Some(McpServer::Http {
            name: http.name.clone(),
            url: http.url.clone(),
            headers: by_name(
                &http.name,
                http.headers
                    .iter()
                    .map(|header| (header.name.as_str(), header.value.as_str())),
            ),
        }),
        AcpMcpServer::Sse(sse) => {
            tracing::warn!(
                server = %sse.name,
                "skipping host MCP server: SSE transport is not supported"
            );
            None
        }
        // McpServer is non_exhaustive; a transport Rho does not know cannot connect.
        _ => {
            tracing::warn!("skipping host MCP server with an unsupported transport");
            None
        }
    }
}

/// Server `server`'s environment variables or headers, an ACP list, by
/// name. When a name repeats, the last value wins.
fn by_name<'a>(server: &str, listed: impl Iterator<Item = (&'a str, &'a str)>) -> SecretMap {
    let mut values = BTreeMap::new();
    for (name, value) in listed {
        if values.insert(name.to_owned(), value.to_owned()).is_some() {
            tracing::warn!(
                server,
                entry = name,
                "host MCP server lists a name twice; keeping the last value"
            );
        }
    }
    values.into_iter().collect()
}

/// The daemon's session list page as ACP lists it.
pub(super) fn session_list(page: SessionListResponse) -> ListSessionsResponse {
    ListSessionsResponse::new(page.sessions.into_iter().map(session_info).collect())
        .next_cursor(page.next_cursor)
}

/// `summary` labelled with its title, else its first user message, with its
/// last save as an RFC 3339 time. Its directories are those it saved that
/// still exist; a load adds its request's to them.
fn session_info(summary: SessionSummary) -> SessionInfo {
    let title = summary.title.or(summary.first_user_message);
    let updated_at = i64::try_from(summary.updated_at)
        .ok()
        .and_then(|secs| chrono::DateTime::from_timestamp(secs, 0))
        .map(|time| time.to_rfc3339_opts(chrono::SecondsFormat::Secs, /*use_z*/ true));
    SessionInfo::new(SessionId::new(summary.session.as_str()), summary.cwd)
        .additional_directories(summary.added_dirs)
        .title(title)
        .updated_at(updated_at)
}

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