Skip to content

Computer use CLI for AI agents

4.6 KB
Raw Blame
//! Runs one tool call as a child process of this same binary and maps its JSON envelope to an MCP result.

use serde_json::{Map, Value, json};
use std::io::Read;
use std::path::Path;
use std::process::{Command, Stdio};

use super::calls::Calls;
use super::catalog::{Output, Tool};

const CANCELLED: &str = "Cancelled";

/// Runs `tool` with `arguments` for request `id`, carrying the server's own global flags, and returns a
/// `CallToolResult`. A cancelled call returns early; the caller sends no response for it.
pub(crate) fn call(
    tool: &Tool,
    arguments: &Map<String, Value>,
    globals: &[String],
    calls: &Calls,
    id: &Value,
) -> Value {
    let argv = match tool.argv(arguments) {
        Ok(argv) => argv,
        Err(message) => return error_result(&message),
    };
    let exe = match std::env::current_exe() {
        Ok(exe) => exe,
        Err(error) => return error_result(&format!("Cannot locate agent-desktop: {error}")),
    };
    let mut command = child_command(&exe, tool, argv, globals);
    let (stdout, stderr) = match calls.start(id, &mut command) {
        None => return error_result(CANCELLED),
        Some(Err(error)) => return error_result(&format!("Cannot run agent-desktop: {error}")),
        Some(Ok(pipes)) => pipes,
    };
    let (stdout, stderr) = std::thread::scope(|scope| {
        let stderr = scope.spawn(|| read_all(stderr));
        (read_all(stdout), stderr.join().unwrap_or_default())
    });
    let Some(mut child) = calls.take(id) else {
        return error_result(CANCELLED);
    };
    let status = child.wait();
    let succeeded = status.as_ref().is_ok_and(|status| status.success());
    let status = status.map_or_else(|error| error.to_string(), |status| status.to_string());
    tool_result(tool, &stdout, &stderr, &status, succeeded)
}

/// Maps a child's output to a result by the kind of output its tool writes, never by what the bytes look like.
/// A passthrough tool hands back its child's own stdout, empty output included, with its exit status.
fn tool_result(tool: &Tool, stdout: &[u8], stderr: &[u8], status: &str, succeeded: bool) -> Value {
    match tool.output {
        Output::Passthrough => json!({
            "content": [{ "type": "text", "text": String::from_utf8_lossy(stdout) }],
            "isError": !succeeded,
        }),
        Output::Envelope => match serde_json::from_slice::<Value>(stdout) {
            Ok(envelope) => envelope_result(envelope),
            Err(_) => error_result(&format!(
                "agent-desktop {} exited with {status} and no JSON envelope: {}",
                tool.path.join(" "),
                String::from_utf8_lossy(stderr).trim()
            )),
        },
    }
}

/// Builds the child command that runs `tool`. Server globals precede an ordinary tool. `sandbox exec` gets
/// none: they describe this host, and the guest command carries its own flags after `--`.
fn child_command(exe: &Path, tool: &Tool, argv: Vec<String>, globals: &[String]) -> Command {
    let globals = match tool.path == ["sandbox", "exec"] {
        true => &[],
        false => globals,
    };
    let mut command = Command::new(exe);
    command
        .args(globals)
        .args(argv)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    command
}

fn read_all(pipe: Option<impl Read>) -> Vec<u8> {
    let mut bytes = Vec::new();
    if let Some(mut pipe) = pipe {
        let _ = pipe.read_to_end(&mut bytes);
    }
    bytes
}

/// Maps an envelope to a result: the envelope as structured content and text, an image block for inline screenshots.
pub(crate) fn envelope_result(mut envelope: Value) -> Value {
    let is_error = envelope["ok"] != Value::Bool(true);
    let image = take_inline_image(&mut envelope);
    let mut content = vec![json!({ "type": "text", "text": envelope.to_string() })];
    content.extend(image);
    json!({ "content": content, "structuredContent": envelope, "isError": is_error })
}

fn take_inline_image(envelope: &mut Value) -> Option<Value> {
    let data = envelope.get_mut("data")?.as_object_mut()?;
    let mime_type = match data.get("format")?.as_str()? {
        "png" => "image/png",
        "jpg" | "jpeg" => "image/jpeg",
        _ => return None,
    };
    if !data.get("data").is_some_and(Value::is_string) {
        return None;
    }
    let Value::String(encoded) = data.remove("data")? else {
        return None;
    };
    Some(json!({ "type": "image", "data": encoded, "mimeType": mime_type }))
}

fn error_result(message: &str) -> Value {
    json!({ "content": [{ "type": "text", "text": message }], "isError": true })
}

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