codex-wrapper 0.3.1

A type-safe Codex CLI wrapper for Rust
Documentation
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/// Resume a previous interactive session.
///
/// Wraps the top-level `codex resume` command (distinct from `codex exec resume`).
use crate::Codex;
use crate::command::CodexCommand;
use crate::command::exec::effective_sandbox;
use crate::error::Result;
use crate::exec::{self, CommandOutput};
use crate::types::{ApprovalPolicy, SandboxMode};

/// Resume a previous interactive Codex session.
#[derive(Debug, Clone)]
pub struct ResumeCommand {
    approve_for_me: bool,
    session_id: Option<String>,
    prompt: Option<String>,
    last: bool,
    all: bool,
    config_overrides: Vec<String>,
    enabled_features: Vec<String>,
    disabled_features: Vec<String>,
    images: Vec<String>,
    model: Option<String>,
    oss: bool,
    local_provider: Option<String>,
    profile: Option<String>,
    sandbox: Option<SandboxMode>,
    approval_policy: Option<ApprovalPolicy>,
    full_auto: bool,
    dangerously_bypass_approvals_and_sandbox: bool,
    dangerously_bypass_hook_trust: bool,
    strict_config: bool,
    no_alt_screen: bool,
    include_non_interactive: bool,
    remote: Option<String>,
    remote_auth_token_env: Option<String>,
    cd: Option<String>,
    search: bool,
    add_dirs: Vec<String>,
}

impl ResumeCommand {
    #[must_use]
    pub fn new() -> Self {
        Self {
            approve_for_me: false,
            session_id: None,
            prompt: None,
            last: false,
            all: false,
            config_overrides: Vec::new(),
            enabled_features: Vec::new(),
            disabled_features: Vec::new(),
            images: Vec::new(),
            model: None,
            oss: false,
            local_provider: None,
            profile: None,
            sandbox: None,
            approval_policy: None,
            full_auto: false,
            dangerously_bypass_approvals_and_sandbox: false,
            dangerously_bypass_hook_trust: false,
            strict_config: false,
            no_alt_screen: false,
            include_non_interactive: false,
            remote: None,
            remote_auth_token_env: None,
            cd: None,
            search: false,
            add_dirs: Vec::new(),
        }
    }

    /// Session ID (UUID) or thread name to resume.
    #[must_use]
    pub fn session_id(mut self, id: impl Into<String>) -> Self {
        self.session_id = Some(id.into());
        self
    }

    /// Optional prompt to start the resumed session with.
    #[must_use]
    pub fn prompt(mut self, prompt: impl Into<String>) -> Self {
        self.prompt = Some(prompt.into());
        self
    }

    /// Continue the most recent session without showing the picker.
    #[must_use]
    pub fn last(mut self) -> Self {
        self.last = true;
        self
    }

    /// Show all sessions (disables cwd filtering).
    #[must_use]
    pub fn all(mut self) -> Self {
        self.all = true;
        self
    }

    #[must_use]
    pub fn config(mut self, key_value: impl Into<String>) -> Self {
        self.config_overrides.push(key_value.into());
        self
    }

    #[must_use]
    pub fn enable(mut self, feature: impl Into<String>) -> Self {
        self.enabled_features.push(feature.into());
        self
    }

    #[must_use]
    pub fn disable(mut self, feature: impl Into<String>) -> Self {
        self.disabled_features.push(feature.into());
        self
    }

    #[must_use]
    pub fn image(mut self, path: impl Into<String>) -> Self {
        self.images.push(path.into());
        self
    }

    #[must_use]
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = Some(model.into());
        self
    }

    #[must_use]
    pub fn oss(mut self) -> Self {
        self.oss = true;
        self
    }

    #[must_use]
    pub fn local_provider(mut self, provider: impl Into<String>) -> Self {
        self.local_provider = Some(provider.into());
        self
    }

    #[must_use]
    pub fn profile(mut self, profile: impl Into<String>) -> Self {
        self.profile = Some(profile.into());
        self
    }

    #[must_use]
    pub fn sandbox(mut self, sandbox: SandboxMode) -> Self {
        self.sandbox = Some(sandbox);
        self
    }

    #[must_use]
    pub fn approval_policy(mut self, policy: ApprovalPolicy) -> Self {
        self.approval_policy = Some(policy);
        self
    }

    /// Run in full-auto mode, emitted as `--sandbox workspace-write`.
    ///
    /// `codex resume` rejects `--full-auto` outright in `codex-cli` 0.145.0
    /// ("unexpected argument"), so this emits the replacement the CLI names
    /// for the exec family. An explicit [`sandbox`](Self::sandbox) call is
    /// more specific and wins over it.
    #[must_use]
    pub fn full_auto(mut self) -> Self {
        self.full_auto = true;
        self
    }

    /// Route approval requests through automatic review, using the
    /// workspace-write sandbox (`--approve-for-me`).
    ///
    /// Added in `codex-cli` 0.147.0. Older releases reject it as an unexpected
    /// argument, so this is the one builder method with a floor above the
    /// wrapper's tested minimum. `codex exec review` and `codex exec resume`
    /// do not accept it.
    #[must_use]
    pub fn approve_for_me(mut self) -> Self {
        self.approve_for_me = true;
        self
    }

    #[must_use]
    pub(crate) fn set_bypass_approvals_and_sandbox(mut self) -> Self {
        self.dangerously_bypass_approvals_and_sandbox = true;
        self
    }

    /// Bypass the hook trust prompt (`--dangerously-bypass-hook-trust`).
    #[must_use]
    pub(crate) fn set_bypass_hook_trust(mut self) -> Self {
        self.dangerously_bypass_hook_trust = true;
        self
    }

    /// Error on unrecognized config keys (`--strict-config`).
    #[must_use]
    pub fn strict_config(mut self) -> Self {
        self.strict_config = true;
        self
    }

    /// Do not use the terminal alternate screen (`--no-alt-screen`).
    #[must_use]
    pub fn no_alt_screen(mut self) -> Self {
        self.no_alt_screen = true;
        self
    }

    /// Include non-interactive sessions in the picker
    /// (`--include-non-interactive`).
    #[must_use]
    pub fn include_non_interactive(mut self) -> Self {
        self.include_non_interactive = true;
        self
    }

    /// Connect the TUI to a remote app server endpoint (`--remote <ADDR>`).
    #[must_use]
    pub fn remote(mut self, addr: impl Into<String>) -> Self {
        self.remote = Some(addr.into());
        self
    }

    /// Env var holding the bearer token for the remote app server
    /// (`--remote-auth-token-env <ENV_VAR>`).
    #[must_use]
    pub fn remote_auth_token_env(mut self, env_var: impl Into<String>) -> Self {
        self.remote_auth_token_env = Some(env_var.into());
        self
    }

    #[must_use]
    pub fn cd(mut self, dir: impl Into<String>) -> Self {
        self.cd = Some(dir.into());
        self
    }

    /// Enable live web search.
    #[must_use]
    pub fn search(mut self) -> Self {
        self.search = true;
        self
    }

    #[must_use]
    pub fn add_dir(mut self, dir: impl Into<String>) -> Self {
        self.add_dirs.push(dir.into());
        self
    }
}

impl Default for ResumeCommand {
    fn default() -> Self {
        Self::new()
    }
}

impl CodexCommand for ResumeCommand {
    type Output = CommandOutput;

    fn args(&self) -> Vec<String> {
        let mut args = vec!["resume".into()];

        for v in &self.config_overrides {
            args.push("-c".into());
            args.push(v.clone());
        }
        for v in &self.enabled_features {
            args.push("--enable".into());
            args.push(v.clone());
        }
        for v in &self.disabled_features {
            args.push("--disable".into());
            args.push(v.clone());
        }
        if self.last {
            args.push("--last".into());
        }
        if self.all {
            args.push("--all".into());
        }
        for v in &self.images {
            args.push("--image".into());
            args.push(v.clone());
        }
        if let Some(model) = &self.model {
            args.push("--model".into());
            args.push(model.clone());
        }
        if self.oss {
            args.push("--oss".into());
        }
        if let Some(provider) = &self.local_provider {
            args.push("--local-provider".into());
            args.push(provider.clone());
        }
        if let Some(profile) = &self.profile {
            args.push("--profile".into());
            args.push(profile.clone());
        }
        if let Some(sandbox) = effective_sandbox(self.sandbox, self.full_auto) {
            args.push("--sandbox".into());
            args.push(sandbox.as_arg().into());
        }
        if let Some(policy) = self.approval_policy {
            args.push("--ask-for-approval".into());
            args.push(policy.as_arg().into());
        }
        if self.approve_for_me {
            args.push("--approve-for-me".into());
        }
        if self.dangerously_bypass_approvals_and_sandbox {
            args.push("--dangerously-bypass-approvals-and-sandbox".into());
        }
        if self.dangerously_bypass_hook_trust {
            args.push("--dangerously-bypass-hook-trust".into());
        }
        if self.strict_config {
            args.push("--strict-config".into());
        }
        if self.no_alt_screen {
            args.push("--no-alt-screen".into());
        }
        if self.include_non_interactive {
            args.push("--include-non-interactive".into());
        }
        if let Some(remote) = &self.remote {
            args.push("--remote".into());
            args.push(remote.clone());
        }
        if let Some(env_var) = &self.remote_auth_token_env {
            args.push("--remote-auth-token-env".into());
            args.push(env_var.clone());
        }
        if let Some(cd) = &self.cd {
            args.push("--cd".into());
            args.push(cd.clone());
        }
        if self.search {
            args.push("--search".into());
        }
        for v in &self.add_dirs {
            args.push("--add-dir".into());
            args.push(v.clone());
        }
        if let Some(id) = &self.session_id {
            args.push(id.clone());
        }
        if let Some(prompt) = &self.prompt {
            args.push(prompt.clone());
        }
        args
    }

    async fn execute(&self, codex: &Codex) -> Result<CommandOutput> {
        exec::run_codex(codex, self.args()).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn resume_last_args() {
        let args = ResumeCommand::new()
            .last()
            .model("gpt-5")
            .prompt("continue")
            .args();
        assert_eq!(
            args,
            vec!["resume", "--last", "--model", "gpt-5", "continue"]
        );
    }

    #[test]
    fn resume_session_id_args() {
        let args = ResumeCommand::new()
            .session_id("abc-123")
            .sandbox(SandboxMode::WorkspaceWrite)
            .search()
            .args();
        assert_eq!(
            args,
            vec![
                "resume",
                "--sandbox",
                "workspace-write",
                "--search",
                "abc-123"
            ]
        );
    }

    #[test]
    fn resume_new_flags_args() {
        let args = ResumeCommand::new()
            .last()
            .strict_config()
            .include_non_interactive()
            .no_alt_screen()
            .remote("ws://host:9000")
            .args();
        assert_eq!(
            args,
            vec![
                "resume",
                "--last",
                "--strict-config",
                "--no-alt-screen",
                "--include-non-interactive",
                "--remote",
                "ws://host:9000",
            ]
        );
    }

    /// `codex resume` rejects `--full-auto` outright. See #55.
    #[test]
    fn resume_full_auto_emits_sandbox_workspace_write() {
        let args = ResumeCommand::new().last().full_auto().args();
        assert_eq!(
            args,
            vec!["resume", "--last", "--sandbox", "workspace-write"]
        );
        assert!(!args.iter().any(|a| a == "--full-auto"));
    }

    #[test]
    fn resume_explicit_sandbox_wins_over_full_auto() {
        let args = ResumeCommand::new()
            .last()
            .full_auto()
            .sandbox(SandboxMode::DangerFullAccess)
            .args();
        assert_eq!(
            args,
            vec!["resume", "--last", "--sandbox", "danger-full-access"]
        );
    }
}