1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! # codex-cli-sdk
//!
//! Rust SDK for the [OpenAI Codex CLI](https://github.com/openai/codex).
//!
//! This crate provides a programmatic interface to the Codex CLI by spawning
//! it as a subprocess and communicating via JSONL. It mirrors the API of the
//! official [TypeScript SDK](https://www.npmjs.com/package/@openai/codex-sdk).
//!
//! ## Quick Start
//!
//! ```rust,no_run
//! use codex_cli_sdk::{Codex, CodexConfig, ThreadOptions};
//!
//! #[tokio::main]
//! async fn main() -> codex_cli_sdk::Result<()> {
//! let codex = Codex::new(CodexConfig::default())?;
//! let mut thread = codex.start_thread(ThreadOptions::default());
//! let turn = thread.run("List the files in this directory", Default::default()).await?;
//! println!("{}", turn.final_response);
//! Ok(())
//! }
//! ```
//!
//! ## Streaming
//!
//! ```rust,no_run
//! use codex_cli_sdk::{Codex, CodexConfig, ThreadOptions, ThreadEvent, ThreadItem};
//! use tokio_stream::StreamExt;
//!
//! #[tokio::main]
//! async fn main() -> codex_cli_sdk::Result<()> {
//! let codex = Codex::new(CodexConfig::default())?;
//! let mut thread = codex.start_thread(ThreadOptions::default());
//! let mut stream = thread.run_streamed("Explain this codebase", Default::default()).await?;
//!
//! while let Some(event) = stream.next().await {
//! match event? {
//! ThreadEvent::ItemUpdated { item: ThreadItem::AgentMessage { text, .. } } => {
//! print!("{}", text);
//! }
//! ThreadEvent::ItemStarted { item: ThreadItem::CommandExecution { command, .. } } => {
//! println!("\n> Running: {}", command);
//! }
//! ThreadEvent::TurnCompleted { usage } => {
//! println!("\n--- done ({} tokens) ---", usage.output_tokens);
//! }
//! _ => {}
//! }
//! }
//! Ok(())
//! }
//! ```
//!
//! ## Resuming Threads
//!
//! ```rust,no_run
//! use codex_cli_sdk::{Codex, CodexConfig, ThreadOptions};
//!
//! #[tokio::main]
//! async fn main() -> codex_cli_sdk::Result<()> {
//! let codex = Codex::new(CodexConfig::default())?;
//!
//! // First turn
//! let mut thread = codex.start_thread(ThreadOptions::default());
//! let turn = thread.run("Create a hello.py file", Default::default()).await?;
//! let thread_id = thread.id().unwrap();
//!
//! // Resume later
//! let mut thread = codex.resume_thread(&thread_id, ThreadOptions::default());
//! let turn = thread.run("Now add error handling to hello.py", Default::default()).await?;
//! Ok(())
//! }
//! ```
//!
//! ## Approval Handling
//!
//! ```rust,no_run
//! use codex_cli_sdk::{Codex, CodexConfig, ThreadOptions};
//! use codex_cli_sdk::permissions::{ApprovalCallback, ApprovalDecision};
//! use std::sync::Arc;
//!
//! #[tokio::main]
//! async fn main() -> codex_cli_sdk::Result<()> {
//! let codex = Codex::new(CodexConfig::default())?;
//! let options = ThreadOptions::builder()
//! .approval(codex_cli_sdk::config::ApprovalPolicy::OnRequest)
//! .build();
//!
//! let callback: ApprovalCallback = Arc::new(|ctx| {
//! Box::pin(async move {
//! println!("Agent wants to run: {}", ctx.request.command);
//! ApprovalDecision::Approved.into()
//! })
//! });
//!
//! let mut thread = codex.start_thread(options)
//! .with_approval_callback(callback);
//! let turn = thread.run("Set up a new Rust project", Default::default()).await?;
//! Ok(())
//! }
//! ```
pub
pub
// ── Re-exports ─────────────────────────────────────────────────
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use Transport;
pub use ;
pub use ;
pub use ThreadItem;
// ── Top-level convenience functions ────────────────────────────
/// One-shot query: start a thread, run the prompt, return all events as a `Turn`.
pub async
/// Streaming query: start a thread, run the prompt, return event stream.
pub async
/// Collect all events from a `StreamedTurn` into a `Vec<ThreadEvent>`.
pub async