agent-commander 0.5.1

A Rust library to control agents enclosed in CLI commands like Anthropic Claude Code CLI
Documentation
//! Launch options shared by every tool (issue #57).
//!
//! Reasoning effort, MCP servers, extra workspace directories, file
//! attachments and a JSON Schema for structured output. Each tool maps them onto its own native flag; a tool without a native flag rejects the option instead of
//! silently dropping it.

use serde_json::Value;

/// Launch options shared by every tool
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct LaunchOptions {
    /// Native reasoning effort / variant level (`--reasoning-effort`)
    pub reasoning_effort: Option<String>,
    /// MCP servers: config file path or inline `{"mcpServers": {...}}` JSON
    pub mcp_config: Option<String>,
    /// Use only the servers from `mcp_config`
    pub strict_mcp_config: bool,
    /// Extra directories the agent may access (`--add-dir`)
    pub additional_directories: Vec<String>,
    /// Files attached to the prompt (`--attach`): Codex `--image`, OpenCode
    /// `--file`. Relative paths resolve against the working directory.
    pub attachments: Vec<String>,
    /// JSON Schema text the final answer must match (`--json-schema`):
    /// Claude and Qwen `--json-schema`, Codex `--output-schema`. The result
    /// is reported as `metadata.structured_output`.
    pub json_schema: Option<String>,
    /// File holding `json_schema` (written by [`crate::Agent::start`] so the
    /// schema does not appear on the command line; issue #60). Codex reads it
    /// with `--output-schema <file>`, Qwen with `--json-schema @<file>` and
    /// Claude through `--json-schema "$(cat <file>)"`.
    pub json_schema_file: Option<String>,
    /// Claude `--system-prompt-file` (replaces the inline `--system-prompt`)
    pub system_prompt_file: Option<String>,
    /// Claude `--append-system-prompt-file` (replaces the inline
    /// `--append-system-prompt`)
    pub append_system_prompt_file: Option<String>,
}

/// What a tool natively supports among the launch options
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LaunchCapabilities {
    pub reasoning_effort: bool,
    /// Documented effort levels; `None` when the tool or provider defines them
    pub reasoning_efforts: Option<&'static [&'static str]>,
    pub mcp_config: bool,
    pub strict_mcp_config: bool,
    pub additional_directories: bool,
    pub attachments: bool,
    pub json_schema: bool,
}

/// Claude Code `--effort` levels
pub const CLAUDE_REASONING_EFFORTS: &[&str] = &["low", "medium", "high", "xhigh", "max"];

/// Codex `model_reasoning_effort` levels
pub const CODEX_REASONING_EFFORTS: &[&str] = &["none", "minimal", "low", "medium", "high", "xhigh"];

const TOOLS: &[&str] = &["claude", "codex", "opencode", "agent", "gemini", "qwen"];

/// Describe what a tool natively supports among the launch options.
pub fn launch_capabilities(tool: &str) -> LaunchCapabilities {
    // gemini 0.64, opencode 1.19 and agent 0.26 have no schema flag.
    let (effort, efforts, mcp, strict, dirs, attachments, json_schema) = match tool {
        "claude" => (
            true,
            Some(CLAUDE_REASONING_EFFORTS),
            true,
            true,
            true,
            false,
            true,
        ),
        "codex" => (
            true,
            Some(CODEX_REASONING_EFFORTS),
            true,
            false,
            true,
            true,
            true,
        ),
        // `--variant` names are defined by the model provider.
        "opencode" => (true, None, false, false, false, true, false),
        "gemini" => (false, None, false, false, true, false, false),
        "qwen" => (false, None, true, false, true, false, true),
        _ => (false, None, false, false, false, false, false),
    };
    LaunchCapabilities {
        reasoning_effort: effort,
        reasoning_efforts: efforts,
        mcp_config: mcp,
        strict_mcp_config: strict,
        additional_directories: dirs,
        attachments,
        json_schema,
    }
}

/// Check that every requested launch option has a native flag in the tool.
///
/// # Errors
/// Returns the message naming the tools that do support the option.
pub fn check_launch_options_supported(tool: &str, launch: &LaunchOptions) -> Result<(), String> {
    check_launch_options_against(tool, launch, &launch_capabilities(tool))
}

/// Check the requested launch options against the given capabilities, such
/// as a `ToolOverrides::launch_capabilities` replacement.
///
/// # Errors
/// Returns the message naming the tools that do support the option.
pub fn check_launch_options_against(
    tool: &str,
    launch: &LaunchOptions,
    capabilities: &LaunchCapabilities,
) -> Result<(), String> {
    type Pick = fn(&LaunchCapabilities) -> bool;
    let checks: [(bool, Pick, &str); 6] = [
        (
            launch.reasoning_effort.is_some(),
            |c| c.reasoning_effort,
            "reasoning effort (--reasoning-effort)",
        ),
        (
            launch.mcp_config.is_some(),
            |c| c.mcp_config,
            "MCP server configuration (--mcp-config)",
        ),
        (
            launch.strict_mcp_config,
            |c| c.strict_mcp_config,
            "strict MCP configuration (--strict-mcp-config)",
        ),
        (
            !launch.additional_directories.is_empty(),
            |c| c.additional_directories,
            "additional directories (--add-dir)",
        ),
        (
            !launch.attachments.is_empty(),
            |c| c.attachments,
            "file attachments (--attach)",
        ),
        (
            launch.json_schema.is_some(),
            |c| c.json_schema,
            "structured output (--json-schema)",
        ),
    ];
    for (requested, pick, label) in checks {
        if requested && !pick(capabilities) {
            let supporting: Vec<&str> = TOOLS
                .iter()
                .copied()
                .filter(|name| pick(&launch_capabilities(name)))
                .collect();
            return Err(format!(
                "Tool \"{}\" does not support {}. Choose one of: {}; or omit the option.",
                tool,
                label,
                supporting.join(", ")
            ));
        }
    }
    Ok(())
}

/// Validate a reasoning effort / variant name.
///
/// # Panics
/// Panics when the level contains characters other than `[A-Za-z0-9_-]`.
pub fn normalize_reasoning_effort(reasoning_effort: &str) -> String {
    assert!(
        !reasoning_effort.is_empty()
            && reasoning_effort
                .chars()
                .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-'),
        "Invalid reasoning effort \"{}\". Use a level name such as low, medium or high.",
        reasoning_effort
    );
    reasoning_effort.to_string()
}

/// Validate the JSON Schema the final answer must match and return it as
/// compact JSON text.
///
/// # Panics
/// Panics when the text is not JSON or not a JSON object.
pub fn json_schema_arg(json_schema: &str) -> String {
    let schema: Value = serde_json::from_str(json_schema)
        .unwrap_or_else(|error| panic!("jsonSchema is not valid JSON: {error}"));
    assert!(
        schema.is_object(),
        "jsonSchema must be a JSON Schema object or its JSON text"
    );
    schema.to_string()
}

fn push_paths(args: &mut Vec<String>, flag: &str, paths: &[String], name: &str) {
    for path in paths {
        assert!(!path.is_empty(), "{name} must not be empty");
        args.push(flag.to_string());
        args.push(path.clone());
    }
}

/// Push `flag value` for each extra directory.
pub fn push_directories(args: &mut Vec<String>, flag: &str, directories: &[String]) {
    push_paths(args, flag, directories, "additional directories");
}

/// Push `flag file` for each attachment; a repeated flag keeps variadic
/// flags (Codex `--image`, OpenCode `--file`) from swallowing other values.
pub fn push_attachments(args: &mut Vec<String>, flag: &str, attachments: &[String]) {
    push_paths(args, flag, attachments, "attachments");
}

/// Encode a TOML key segment (bare when possible, quoted otherwise).
fn toml_key(key: &str) -> String {
    if !key.is_empty()
        && key
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
    {
        key.to_string()
    } else {
        Value::String(key.to_string()).to_string()
    }
}

/// Encode a JSON value as an inline TOML value. JSON string escapes are a
/// subset of TOML basic-string escapes, so strings reuse JSON encoding.
///
/// # Errors
/// Returns an error for `null` at the top level.
pub fn to_toml_value(value: &Value) -> Result<String, String> {
    match value {
        Value::String(_) | Value::Number(_) | Value::Bool(_) => Ok(value.to_string()),
        Value::Array(items) => {
            let parts: Result<Vec<String>, String> = items.iter().map(to_toml_value).collect();
            Ok(format!("[{}]", parts?.join(", ")))
        }
        Value::Object(map) => {
            let mut parts = Vec::new();
            for (key, item) in map.iter().filter(|(_, item)| !item.is_null()) {
                parts.push(format!("{} = {}", toml_key(key), to_toml_value(item)?));
            }
            Ok(format!("{{{}}}", parts.join(", ")))
        }
        Value::Null => Err("Cannot encode null as a TOML value".to_string()),
    }
}

/// Translate a Claude-style `{"mcpServers": {...}}` JSON document into one
/// Codex `-c mcp_servers={...}` override.
///
/// `headers` becomes `http_headers`
/// and the transport `type` is dropped (Codex infers it from `command` or
/// `url`). Codex merges the table into the servers from `config.toml`.
///
/// # Errors
/// Returns an error for a file path, invalid JSON or a missing `mcpServers`.
pub fn codex_mcp_servers_override(mcp_config: &str) -> Result<String, String> {
    if !mcp_config.trim_start().starts_with('{') {
        return Err("Tool \"codex\" takes MCP servers as config overrides, not a file path. Pass mcpConfig as inline JSON.".to_string());
    }
    let config: Value = serde_json::from_str(mcp_config)
        .map_err(|error| format!("Invalid mcpConfig JSON: {error}"))?;
    let servers = config
        .get("mcpServers")
        .and_then(Value::as_object)
        .ok_or_else(|| "mcpConfig must contain an mcpServers object".to_string())?;
    let mut table = serde_json::Map::new();
    for (name, server) in servers {
        let mut server = server
            .as_object()
            .cloned()
            .ok_or_else(|| format!("MCP server \"{name}\" must be an object"))?;
        server.remove("type");
        if let Some(headers) = server.remove("headers") {
            server.insert("http_headers".to_string(), headers);
        }
        table.insert(name.clone(), Value::Object(server));
    }
    Ok(format!(
        "mcp_servers={}",
        to_toml_value(&Value::Object(table))?
    ))
}