codex_wrapper/lib.rs
1//! A type-safe Codex CLI wrapper for Rust.
2//!
3//! `codex-wrapper` provides a builder-pattern interface for invoking the
4//! `codex` CLI programmatically. It follows the same design philosophy as
5//! [`claude-wrapper`](https://crates.io/crates/claude-wrapper) and
6//! [`docker-wrapper`](https://crates.io/crates/docker-wrapper):
7//! each CLI subcommand is a builder struct that produces typed output.
8//!
9//! # Quick Start
10//!
11//! ```no_run
12//! use codex_wrapper::{Codex, CodexCommand, ExecCommand, SandboxMode};
13//!
14//! # async fn example() -> codex_wrapper::Result<()> {
15//! let codex = Codex::builder().build()?;
16//!
17//! let output = ExecCommand::new("summarize this repository")
18//! .sandbox(SandboxMode::WorkspaceWrite)
19//! .ephemeral()
20//! .execute(&codex)
21//! .await?;
22//!
23//! println!("{}", output.stdout);
24//! # Ok(())
25//! # }
26//! ```
27//!
28//! ## Defaults
29//!
30//! | Type | Default variant |
31//! |------|-----------------|
32//! | [`SandboxMode`] | [`SandboxMode::WorkspaceWrite`] |
33//! | [`ApprovalPolicy`] | [`ApprovalPolicy::OnRequest`] |
34//!
35//! # Two-Layer Builder
36//!
37//! The [`Codex`] client holds shared config (binary path, env vars, timeout,
38//! retry policy). Command builders hold per-invocation options and call
39//! `execute(&codex)`.
40//!
41//! ```no_run
42//! use codex_wrapper::{Codex, CodexCommand, ExecCommand, ApprovalPolicy, RetryPolicy};
43//! use std::time::Duration;
44//!
45//! # async fn example() -> codex_wrapper::Result<()> {
46//! // Configure once, reuse across commands
47//! let codex = Codex::builder()
48//! .env("OPENAI_API_KEY", "sk-...")
49//! .timeout_secs(300)
50//! .retry(RetryPolicy::new().max_attempts(3).exponential())
51//! .build()?;
52//!
53//! // Each command is a separate builder
54//! let output = ExecCommand::new("fix the failing tests")
55//! .model("o3")
56//! .approval_policy(ApprovalPolicy::Never)
57//! .skip_git_repo_check()
58//! .ephemeral()
59//! .execute(&codex)
60//! .await?;
61//! # Ok(())
62//! # }
63//! ```
64//!
65//! # JSONL Output Parsing
66//!
67//! Use `execute_json_lines()` to get structured events from `--json` mode:
68//!
69//! ```no_run
70//! use codex_wrapper::{Codex, ExecCommand};
71//!
72//! # async fn example() -> codex_wrapper::Result<()> {
73//! let codex = Codex::builder().build()?;
74//! let events = ExecCommand::new("what is 2+2?")
75//! .ephemeral()
76//! .execute_json_lines(&codex)
77//! .await?;
78//!
79//! for event in &events {
80//! println!("{}: {:?}", event.event_type, event.extra);
81//! }
82//! # Ok(())
83//! # }
84//! ```
85//!
86//! # Available Commands
87//!
88//! | Command | CLI equivalent |
89//! |---------|---------------|
90//! | [`ExecCommand`] | `codex exec <prompt>` |
91//! | [`ExecResumeCommand`] | `codex exec resume` |
92//! | [`ReviewCommand`] | `codex exec review` |
93//! | [`ResumeCommand`] | `codex resume` |
94//! | [`ForkCommand`] | `codex fork` |
95//! | [`LoginCommand`] | `codex login` |
96//! | [`LoginStatusCommand`] | `codex login status` |
97//! | [`LogoutCommand`] | `codex logout` |
98//! | [`McpListCommand`] | `codex mcp list` |
99//! | [`McpGetCommand`] | `codex mcp get` |
100//! | [`McpAddCommand`] | `codex mcp add` |
101//! | [`McpRemoveCommand`] | `codex mcp remove` |
102//! | [`McpLoginCommand`] | `codex mcp login` |
103//! | [`McpLogoutCommand`] | `codex mcp logout` |
104//! | [`McpServerCommand`] | `codex mcp-server` |
105//! | [`CompletionCommand`] | `codex completion` |
106//! | [`SandboxCommand`] | `codex sandbox` |
107//! | [`ApplyCommand`] | `codex apply` |
108//! | [`FeaturesListCommand`] | `codex features list` |
109//! | [`FeaturesEnableCommand`] | `codex features enable` |
110//! | [`FeaturesDisableCommand`] | `codex features disable` |
111//! | [`VersionCommand`] | `codex --version` |
112//! | [`RawCommand`] | Escape hatch for arbitrary args |
113//!
114//! # Error Handling
115//!
116//! All commands return [`Result<T>`], with typed errors via [`thiserror`]:
117//!
118//! ```no_run
119//! use codex_wrapper::{Codex, CodexCommand, ExecCommand, Error};
120//!
121//! # async fn example() -> codex_wrapper::Result<()> {
122//! let codex = Codex::builder().build()?;
123//! match ExecCommand::new("test").execute(&codex).await {
124//! Ok(output) => println!("{}", output.stdout),
125//! Err(Error::CommandFailed { stderr, exit_code, .. }) => {
126//! eprintln!("failed (exit {}): {}", exit_code, stderr);
127//! }
128//! Err(Error::Timeout { .. }) => eprintln!("timed out"),
129//! Err(e) => eprintln!("{e}"),
130//! }
131//! # Ok(())
132//! # }
133//! ```
134//!
135//! # Features
136//!
137//! - `json` *(enabled by default)* - JSONL output parsing via `serde_json`
138
139pub mod command;
140pub mod error;
141pub mod exec;
142pub mod retry;
143pub mod types;
144pub mod version;
145
146use std::collections::HashMap;
147use std::path::{Path, PathBuf};
148use std::time::Duration;
149
150pub use command::CodexCommand;
151pub use command::apply::ApplyCommand;
152pub use command::completion::{CompletionCommand, Shell};
153pub use command::exec::{ExecCommand, ExecResumeCommand};
154pub use command::features::{FeaturesDisableCommand, FeaturesEnableCommand, FeaturesListCommand};
155pub use command::fork::ForkCommand;
156pub use command::login::{LoginCommand, LoginStatusCommand, LogoutCommand};
157pub use command::mcp::{
158 McpAddCommand, McpGetCommand, McpListCommand, McpLoginCommand, McpLogoutCommand,
159 McpRemoveCommand,
160};
161pub use command::mcp_server::McpServerCommand;
162pub use command::raw::RawCommand;
163pub use command::resume::ResumeCommand;
164pub use command::review::ReviewCommand;
165pub use command::sandbox::{SandboxCommand, SandboxPlatform};
166pub use command::version::VersionCommand;
167pub use error::{Error, Result};
168pub use exec::CommandOutput;
169pub use retry::{BackoffStrategy, RetryPolicy};
170pub use types::*;
171pub use version::{CliVersion, VersionParseError};
172
173/// Shared Codex CLI client configuration.
174///
175/// Holds the binary path, working directory, environment variables, global
176/// arguments, timeout, and retry policy. Cheap to [`Clone`]; intended to be
177/// created once and reused across many command invocations.
178///
179/// # Example
180///
181/// ```no_run
182/// # fn example() -> codex_wrapper::Result<()> {
183/// let codex = codex_wrapper::Codex::builder()
184/// .env("OPENAI_API_KEY", "sk-...")
185/// .timeout_secs(120)
186/// .build()?;
187/// # Ok(())
188/// # }
189/// ```
190#[derive(Debug, Clone)]
191pub struct Codex {
192 pub(crate) binary: PathBuf,
193 pub(crate) working_dir: Option<PathBuf>,
194 pub(crate) env: HashMap<String, String>,
195 pub(crate) global_args: Vec<String>,
196 pub(crate) timeout: Option<Duration>,
197 pub(crate) retry_policy: Option<RetryPolicy>,
198}
199
200impl Codex {
201 /// Create a new [`CodexBuilder`].
202 #[must_use]
203 pub fn builder() -> CodexBuilder {
204 CodexBuilder::default()
205 }
206
207 /// Path to the resolved `codex` binary.
208 #[must_use]
209 pub fn binary(&self) -> &Path {
210 &self.binary
211 }
212
213 /// Working directory for command execution, if set.
214 #[must_use]
215 pub fn working_dir(&self) -> Option<&Path> {
216 self.working_dir.as_deref()
217 }
218
219 /// Return a clone of this client with a different working directory.
220 #[must_use]
221 pub fn with_working_dir(&self, dir: impl Into<PathBuf>) -> Self {
222 let mut clone = self.clone();
223 clone.working_dir = Some(dir.into());
224 clone
225 }
226
227 /// Query the installed Codex CLI version.
228 pub async fn cli_version(&self) -> Result<CliVersion> {
229 let output = VersionCommand::new().execute(self).await?;
230 CliVersion::parse_version_output(&output.stdout).map_err(|e| Error::Io {
231 message: format!("failed to parse CLI version: {e}"),
232 source: std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()),
233 working_dir: None,
234 })
235 }
236
237 /// Verify the installed CLI meets a minimum version requirement.
238 ///
239 /// Returns [`Error::VersionMismatch`] if the installed version is too old.
240 pub async fn check_version(&self, minimum: &CliVersion) -> Result<CliVersion> {
241 let version = self.cli_version().await?;
242 if version.satisfies_minimum(minimum) {
243 Ok(version)
244 } else {
245 Err(Error::VersionMismatch {
246 found: version,
247 minimum: *minimum,
248 })
249 }
250 }
251}
252
253/// Builder for creating a [`Codex`] client.
254///
255/// All options are optional. By default the builder discovers the `codex`
256/// binary via `PATH`.
257#[derive(Debug, Default)]
258pub struct CodexBuilder {
259 binary: Option<PathBuf>,
260 working_dir: Option<PathBuf>,
261 env: HashMap<String, String>,
262 global_args: Vec<String>,
263 timeout: Option<Duration>,
264 retry_policy: Option<RetryPolicy>,
265}
266
267impl CodexBuilder {
268 /// Set an explicit path to the `codex` binary (skips `PATH` lookup).
269 #[must_use]
270 pub fn binary(mut self, path: impl Into<PathBuf>) -> Self {
271 self.binary = Some(path.into());
272 self
273 }
274
275 /// Set the working directory for all commands.
276 #[must_use]
277 pub fn working_dir(mut self, path: impl Into<PathBuf>) -> Self {
278 self.working_dir = Some(path.into());
279 self
280 }
281
282 /// Set a single environment variable for child processes.
283 #[must_use]
284 pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
285 self.env.insert(key.into(), value.into());
286 self
287 }
288
289 /// Set multiple environment variables for child processes.
290 #[must_use]
291 pub fn envs(
292 mut self,
293 vars: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
294 ) -> Self {
295 for (key, value) in vars {
296 self.env.insert(key.into(), value.into());
297 }
298 self
299 }
300
301 /// Set the command timeout in seconds.
302 #[must_use]
303 pub fn timeout_secs(mut self, seconds: u64) -> Self {
304 self.timeout = Some(Duration::from_secs(seconds));
305 self
306 }
307
308 /// Set the command timeout as a [`Duration`].
309 #[must_use]
310 pub fn timeout(mut self, duration: Duration) -> Self {
311 self.timeout = Some(duration);
312 self
313 }
314
315 /// Append a raw global argument passed before any subcommand.
316 #[must_use]
317 pub fn arg(mut self, arg: impl Into<String>) -> Self {
318 self.global_args.push(arg.into());
319 self
320 }
321
322 /// Add a global config override (`-c key=value`).
323 #[must_use]
324 pub fn config(mut self, key_value: impl Into<String>) -> Self {
325 self.global_args.push("-c".into());
326 self.global_args.push(key_value.into());
327 self
328 }
329
330 /// Enable a feature flag globally (`--enable <name>`).
331 #[must_use]
332 pub fn enable(mut self, feature: impl Into<String>) -> Self {
333 self.global_args.push("--enable".into());
334 self.global_args.push(feature.into());
335 self
336 }
337
338 /// Disable a feature flag globally (`--disable <name>`).
339 #[must_use]
340 pub fn disable(mut self, feature: impl Into<String>) -> Self {
341 self.global_args.push("--disable".into());
342 self.global_args.push(feature.into());
343 self
344 }
345
346 /// Set a default [`RetryPolicy`] for all commands.
347 #[must_use]
348 pub fn retry(mut self, policy: RetryPolicy) -> Self {
349 self.retry_policy = Some(policy);
350 self
351 }
352
353 /// Build the [`Codex`] client.
354 ///
355 /// Returns [`Error::NotFound`] if no binary path was set and `codex` is
356 /// not found in `PATH`.
357 pub fn build(self) -> Result<Codex> {
358 let binary = match self.binary {
359 Some(path) => path,
360 None => which::which("codex").map_err(|_| Error::NotFound)?,
361 };
362
363 Ok(Codex {
364 binary,
365 working_dir: self.working_dir,
366 env: self.env,
367 global_args: self.global_args,
368 timeout: self.timeout,
369 retry_policy: self.retry_policy,
370 })
371 }
372}
373
374#[cfg(test)]
375mod tests {
376 use super::*;
377
378 #[test]
379 fn builder_with_binary() {
380 let codex = Codex::builder()
381 .binary("/usr/local/bin/codex")
382 .env("FOO", "bar")
383 .timeout_secs(60)
384 .build()
385 .unwrap();
386
387 assert_eq!(codex.binary, PathBuf::from("/usr/local/bin/codex"));
388 assert_eq!(codex.env.get("FOO").unwrap(), "bar");
389 assert_eq!(codex.timeout, Some(Duration::from_secs(60)));
390 }
391
392 #[test]
393 fn builder_global_args() {
394 let codex = Codex::builder()
395 .binary("/usr/local/bin/codex")
396 .config("model=\"gpt-5\"")
397 .enable("foo")
398 .disable("bar")
399 .build()
400 .unwrap();
401
402 assert_eq!(
403 codex.global_args,
404 vec![
405 "-c",
406 "model=\"gpt-5\"",
407 "--enable",
408 "foo",
409 "--disable",
410 "bar"
411 ]
412 );
413 }
414}