codex-cli-sdk 0.0.1

Rust SDK for the OpenAI Codex CLI
Documentation
//! Sandbox policies — control filesystem and network access.
//!
//! Shows the three `SandboxPolicy` levels and how to grant additional
//! writable directories or enable network access inside the sandbox.
//!
//! ```bash
//! cargo run --example 05_sandbox
//! ```

use codex_cli_sdk::config::{SandboxPolicy, WebSearchMode};
use codex_cli_sdk::{Codex, CodexConfig, ThreadOptions};
use std::path::PathBuf;

#[tokio::main]
async fn main() -> codex_cli_sdk::Result<()> {
    let codex = Codex::new(CodexConfig::default())?;

    // ── Example 1: Restricted (read-only) ────────────────────────
    // The agent can read files but cannot write or execute anything.
    println!("=== Restricted sandbox ===");
    let options = ThreadOptions::builder()
        .sandbox(SandboxPolicy::Restricted)
        .build();

    let mut thread = codex.start_thread(options);
    let turn = thread
        .run(
            "List all Rust source files in src/ and count the lines in each",
            Default::default(),
        )
        .await?;
    println!("{}", turn.final_response);

    // ── Example 2: WorkspaceWrite with extra directory ────────────
    // Default policy: workspace directory is writable. Extend it with
    // an additional directory the agent is allowed to modify.
    println!("\n=== WorkspaceWrite + extra dir ===");
    let options = ThreadOptions::builder()
        .sandbox(SandboxPolicy::WorkspaceWrite)
        .additional_directories(vec![PathBuf::from("/tmp/my-output")])
        .build();

    let mut thread = codex.start_thread(options);
    let turn = thread
        .run(
            "Write a summary of the project to /tmp/my-output/summary.txt",
            Default::default(),
        )
        .await?;
    println!("{}", turn.final_response);

    // ── Example 3: WorkspaceWrite + network access ────────────────
    // Enable outbound network access inside the sandbox (e.g. for
    // fetching dependencies or calling external APIs).
    println!("\n=== WorkspaceWrite + network + web search ===");
    let options = ThreadOptions::builder()
        .sandbox(SandboxPolicy::WorkspaceWrite)
        .network_access(true)
        .web_search(WebSearchMode::Live)
        .build();

    let mut thread = codex.start_thread(options);
    let turn = thread
        .run(
            "Search for the latest stable version of the tokio crate",
            Default::default(),
        )
        .await?;
    println!("{}", turn.final_response);

    Ok(())
}