agent-commander 0.3.2

A Rust library to control agents enclosed in CLI commands like Anthropic Claude Code CLI
Documentation
//! Codex CLI tool configuration
//! Based on hive-mind's codex.lib.mjs implementation

use super::usage::{self, count};
use crate::streaming::parse_ndjson;
use crate::tools::shell::{build_command_head, escape_arg, escape_single_quotes};
use serde_json::Value;
use std::collections::HashMap;

/// Get the Codex model map
pub fn get_model_map() -> HashMap<&'static str, &'static str> {
    let mut map = HashMap::new();
    // Pinned Hive Mind catalogue; generated by experiments/sync-model-catalog.py.
    map.insert("gpt5-codex", "gpt-5-codex");
    map.insert("o3", "o3");
    map.insert("claude", "claude-3-5-sonnet");
    map.insert("sonnet", "claude-3-5-sonnet");
    map.insert("opus", "claude-3-opus");
    map.insert("formal-ai", "formal-ai");
    map.insert("formalai/formal-ai", "formal-ai");
    map.insert("gpt-6.1-sol", "gpt-6.1-sol");
    map.insert("gpt-6-sol", "gpt-6-sol");
    map.insert("gpt-6-luna", "gpt-6-luna");
    map.insert("gpt-reserve", "gpt-reserve");
    map.insert("gpt-6-astra", "gpt-6-astra");
    map.insert("gpt5", "gpt-5");
    map.insert("gpt-5", "gpt-5");
    map.insert("gpt-5.5", "gpt-5.5");
    map.insert("gpt-5.5-mini", "gpt-5.5-mini");
    map.insert("gpt-5.5-nano", "gpt-5.5-nano");
    map.insert("gpt-5.6-sol", "gpt-5.6-sol");
    map.insert("gpt-5.6-terra", "gpt-5.6-terra");
    map.insert("gpt-5.6-luna", "gpt-5.6-luna");
    map.insert("gpt-5.6-cyber", "gpt-5.6-cyber");
    map.insert("gpt-5.4", "gpt-5.4");
    map.insert("gpt-5.4-mini", "gpt-5.4-mini");
    map.insert("gpt-5.4-nano", "gpt-5.4-nano");
    map.insert("gpt-5.2", "gpt-5.2");
    map.insert("gpt-5.2-codex", "gpt-5.2-codex");
    map.insert("gpt-5.3-codex", "gpt-5.3-codex");
    map.insert("gpt-5.3-codex-spark", "gpt-5.3-codex-spark");
    map.insert("gpt-5.1-codex-max", "gpt-5.1-codex-max");
    map.insert("gpt-daybreak-blue-latest", "gpt-daybreak-blue-latest");
    map.insert("gpt-daybreak-red-latest", "gpt-daybreak-red-latest");
    map.insert("openai.gpt-5.5", "openai.gpt-5.5");
    map.insert("openai.gpt-5.4", "openai.gpt-5.4");
    map.insert("openai.gpt-5.6-sol", "openai.gpt-5.6-sol");
    map.insert("openai.gpt-5.6-terra", "openai.gpt-5.6-terra");
    map.insert("openai.gpt-5.6-luna", "openai.gpt-5.6-luna");
    map.insert("codex-auto-review", "codex-auto-review");
    map.insert("o3-mini", "o3-mini");
    map.insert("gpt4", "gpt-4");
    map.insert("gpt-4", "gpt-4");
    map.insert("gpt4o", "gpt-4o");
    map.insert("gpt-4o", "gpt-4o");
    map.insert("sol", "gpt-5.6-sol");
    map.insert("terra", "gpt-5.6-terra");
    map.insert("luna", "gpt-5.6-luna");
    map.insert("openai/formal-ai", "openai/formal-ai");
    map.insert("openai.formal-ai", "openai.formal-ai");
    map.insert("openai/gpt-6.1-sol", "openai/gpt-6.1-sol");
    map.insert("openai.gpt-6.1-sol", "openai.gpt-6.1-sol");
    map.insert("openai/gpt-6-sol", "openai/gpt-6-sol");
    map.insert("openai.gpt-6-sol", "openai.gpt-6-sol");
    map.insert("openai/gpt-6-luna", "openai/gpt-6-luna");
    map.insert("openai.gpt-6-luna", "openai.gpt-6-luna");
    map.insert("openai/gpt-reserve", "openai/gpt-reserve");
    map.insert("openai.gpt-reserve", "openai.gpt-reserve");
    map.insert("openai/gpt-6-astra", "openai/gpt-6-astra");
    map.insert("openai.gpt-6-astra", "openai.gpt-6-astra");
    map.insert("openai/gpt-5", "openai/gpt-5");
    map.insert("openai.gpt-5", "openai.gpt-5");
    map.insert("openai/gpt-5.5", "openai/gpt-5.5");
    map.insert("openai/gpt-5.5-mini", "openai/gpt-5.5-mini");
    map.insert("openai.gpt-5.5-mini", "openai.gpt-5.5-mini");
    map.insert("openai/gpt-5.5-nano", "openai/gpt-5.5-nano");
    map.insert("openai.gpt-5.5-nano", "openai.gpt-5.5-nano");
    map.insert("openai/gpt-5.6-sol", "openai/gpt-5.6-sol");
    map.insert("openai/gpt-5.6-terra", "openai/gpt-5.6-terra");
    map.insert("openai/gpt-5.6-luna", "openai/gpt-5.6-luna");
    map.insert("openai/gpt-5.6-cyber", "openai/gpt-5.6-cyber");
    map.insert("openai.gpt-5.6-cyber", "openai.gpt-5.6-cyber");
    map.insert("openai/gpt-5.4", "openai/gpt-5.4");
    map.insert("openai/gpt-5.4-mini", "openai/gpt-5.4-mini");
    map.insert("openai.gpt-5.4-mini", "openai.gpt-5.4-mini");
    map.insert("openai/gpt-5.4-nano", "openai/gpt-5.4-nano");
    map.insert("openai.gpt-5.4-nano", "openai.gpt-5.4-nano");
    map.insert("openai/gpt-5.2", "openai/gpt-5.2");
    map.insert("openai.gpt-5.2", "openai.gpt-5.2");
    map.insert("openai/gpt-5.2-codex", "openai/gpt-5.2-codex");
    map.insert("openai.gpt-5.2-codex", "openai.gpt-5.2-codex");
    map.insert("openai/gpt-5.3-codex", "openai/gpt-5.3-codex");
    map.insert("openai.gpt-5.3-codex", "openai.gpt-5.3-codex");
    map.insert("openai/gpt-5.3-codex-spark", "openai/gpt-5.3-codex-spark");
    map.insert("openai.gpt-5.3-codex-spark", "openai.gpt-5.3-codex-spark");
    map.insert("openai/gpt-5.1-codex-max", "openai/gpt-5.1-codex-max");
    map.insert("openai.gpt-5.1-codex-max", "openai.gpt-5.1-codex-max");
    map.insert(
        "openai/gpt-daybreak-blue-latest",
        "openai/gpt-daybreak-blue-latest",
    );
    map.insert(
        "openai.gpt-daybreak-blue-latest",
        "openai.gpt-daybreak-blue-latest",
    );
    map.insert(
        "openai/gpt-daybreak-red-latest",
        "openai/gpt-daybreak-red-latest",
    );
    map.insert(
        "openai.gpt-daybreak-red-latest",
        "openai.gpt-daybreak-red-latest",
    );
    map.insert("openai/codex-auto-review", "openai/codex-auto-review");
    map.insert("openai.codex-auto-review", "openai.codex-auto-review");
    map.insert("openai/o3-mini", "openai/o3-mini");
    map.insert("openai.o3-mini", "openai.o3-mini");
    map.insert("openai/gpt-4", "openai/gpt-4");
    map.insert("openai.gpt-4", "openai.gpt-4");
    map.insert("openai/gpt-4o", "openai/gpt-4o");
    map.insert("openai.gpt-4o", "openai.gpt-4o");
    map.insert("openai/sol", "openai/gpt-5.6-sol");
    map.insert("openai.sol", "openai.gpt-5.6-sol");
    map.insert("openai/terra", "openai/gpt-5.6-terra");
    map.insert("openai.terra", "openai.gpt-5.6-terra");
    map.insert("openai/luna", "openai/gpt-5.6-luna");
    map.insert("openai.luna", "openai.gpt-5.6-luna");
    map
}

/// Map model alias to full model ID
///
/// # Arguments
/// * `model` - Model alias or full ID
///
/// # Returns
/// Full model ID
pub fn map_model_to_id(model: &str) -> String {
    let model_map = get_model_map();
    model_map
        .get(model)
        .map(|s| s.to_string())
        .unwrap_or_else(|| model.to_string())
}

/// Codex command build options
#[derive(Debug, Clone, Default)]
pub struct CodexBuildOptions {
    pub prompt: Option<String>,
    pub prompt_file: Option<String>,
    pub system_prompt: Option<String>,
    pub model: Option<String>,
    pub json: bool,
    pub resume: Option<String>,
    pub read_only: bool,
    pub executable: Option<String>,
    pub extra_env: Vec<(String, String)>,
    pub extra_args: Vec<String>,
    pub skip_default_safety_flags: bool,
    pub sandbox_mode: Option<String>,
    pub approval_mode: Option<String>,
}

/// Build command line arguments for Codex
///
/// # Arguments
/// * `options` - Build options
///
/// # Returns
/// Vector of CLI arguments
pub fn build_args(options: &CodexBuildOptions) -> Vec<String> {
    let mut args = vec!["exec".to_string()];

    if let Some(ref resume) = options.resume {
        args.push("resume".to_string());
        args.push(resume.clone());
    }

    if let Some(ref model) = options.model {
        let mapped_model = map_model_to_id(model);
        args.push("--model".to_string());
        args.push(mapped_model);
    }

    // Default to json=true like JavaScript version
    if options.json {
        args.push("--json".to_string());
    }

    // Codex-specific flags for autonomous execution
    args.push("--skip-git-repo-check".to_string());
    if options.read_only {
        args.push("--sandbox".to_string());
        args.push("read-only".to_string());
    } else if let Some(ref sandbox_mode) = options.sandbox_mode {
        args.push("--sandbox".to_string());
        args.push(sandbox_mode.clone());
    }

    if !options.read_only
        && options.sandbox_mode.is_none()
        && options.approval_mode.is_none()
        && !options.skip_default_safety_flags
    {
        args.push("--dangerously-bypass-approvals-and-sandbox".to_string());
    }

    args.extend(options.extra_args.clone());

    args
}

/// Build complete command string for Codex
/// Codex uses stdin for prompt input
///
/// # Arguments
/// * `options` - Build options
///
/// # Returns
/// Complete command string
pub fn build_command(options: &CodexBuildOptions) -> String {
    let args = build_args(options);
    let args_str: Vec<String> = args.iter().map(|a| escape_arg(a)).collect();

    // Codex expects prompt via stdin, combine system and user prompts
    let combined_prompt = match (&options.system_prompt, &options.prompt) {
        (Some(sys), Some(prompt)) => format!("{}\n\n{}", sys, prompt),
        (Some(sys), None) => sys.clone(),
        (None, Some(prompt)) => prompt.clone(),
        (None, None) => String::new(),
    };

    // Build command with stdin piping
    let input_command = options.prompt_file.as_ref().map_or_else(
        || format!("printf '%s' '{}'", escape_single_quotes(&combined_prompt)),
        |prompt_file| format!("cat {}", escape_arg(prompt_file)),
    );
    let executable = options.executable.as_deref().unwrap_or("codex");
    let mut prefix_args = Vec::new();
    if options.read_only {
        prefix_args.push("--ask-for-approval".to_string());
        prefix_args.push("never".to_string());
    } else if let Some(ref approval_mode) = options.approval_mode {
        prefix_args.push("--ask-for-approval".to_string());
        prefix_args.push(approval_mode.clone());
    }

    format!(
        "{} | {} {}",
        input_command,
        build_command_head(executable, &options.extra_env, &prefix_args),
        args_str.join(" ")
    )
    .trim()
    .to_string()
}

/// Parse JSON messages from Codex output
/// Codex outputs NDJSON format
///
/// # Arguments
/// * `output` - Raw output string
///
/// # Returns
/// Vector of parsed JSON messages
pub fn parse_output(output: &str) -> Vec<Value> {
    parse_ndjson(output)
}

/// Extract session/thread ID from Codex output
/// Codex uses thread_id instead of session_id
///
/// # Arguments
/// * `output` - Raw output string
///
/// # Returns
/// Session ID or None
pub fn extract_session_id(output: &str) -> Option<String> {
    let messages = parse_output(output);

    for msg in messages {
        if let Some(thread_id) = msg.get("thread_id").and_then(|v| v.as_str()) {
            return Some(thread_id.to_string());
        }
        if let Some(session_id) = msg.get("session_id").and_then(|v| v.as_str()) {
            return Some(session_id.to_string());
        }
    }

    None
}

/// Usage statistics
#[derive(Debug, Clone, Default, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CodexUsage {
    pub input_tokens: u64,
    pub output_tokens: u64,
    pub cache_read_tokens: u64,
    pub cache_creation_tokens: u64,
    pub reasoning_tokens: u64,
}

/// Extract usage statistics from Codex output
///
/// # Arguments
/// * `output` - Raw output string
///
/// # Returns
/// Usage statistics
pub fn extract_usage(output: &str) -> CodexUsage {
    let messages = parse_output(output);
    let mut result = CodexUsage::default();
    for record in usage::records("codex", &messages) {
        result.input_tokens += count(record, usage::INPUT);
        result.output_tokens += count(record, usage::OUTPUT);
        result.cache_read_tokens += count(record, usage::CACHE_READ);
        result.cache_creation_tokens += count(record, usage::CACHE_WRITE);
        result.reasoning_tokens += count(record, usage::REASONING);
        result.input_tokens -= count(record, usage::INPUT).min(count(record, usage::CACHE_READ));
    }
    result
}

/// Codex tool configuration
#[derive(Debug, Clone)]
pub struct CodexTool {
    pub name: &'static str,
    pub display_name: &'static str,
    pub executable: &'static str,
    pub supports_json_output: bool,
    pub supports_json_input: bool,
    pub supports_system_prompt: bool,
    pub supports_resume: bool,
    pub supports_read_only: bool,
    pub supports_ask: bool,
    pub default_model: &'static str,
}

impl Default for CodexTool {
    fn default() -> Self {
        Self {
            name: "codex",
            display_name: "Codex CLI",
            executable: "codex",
            supports_json_output: true,
            supports_json_input: false, // stdin carries plain prompt text
            supports_system_prompt: false, // System prompt is combined with user prompt
            supports_resume: true,
            supports_read_only: true, // Supports --sandbox read-only
            supports_ask: false, // Approval is coupled with the sandbox, not a relayable JSON stream
            default_model: "gpt-6-sol",
        }
    }
}

// Tests are in rust/tests/codex_tests.rs