Skip to main content

codex/cli/
exec_server.rs

1use std::path::PathBuf;
2
3use crate::{CliOverridesPatch, ConfigOverride, FlagState};
4
5/// Request for `codex exec-server`.
6#[derive(Clone, Debug, Eq, PartialEq)]
7pub struct ExecServerRequest {
8    /// Optional address passed via `--listen`.
9    pub listen: Option<String>,
10    /// Optional working directory override for the spawned process.
11    pub working_dir: Option<PathBuf>,
12    /// Per-call CLI overrides layered on top of the builder.
13    pub overrides: CliOverridesPatch,
14}
15
16impl ExecServerRequest {
17    pub fn new() -> Self {
18        Self {
19            listen: None,
20            working_dir: None,
21            overrides: CliOverridesPatch::default(),
22        }
23    }
24
25    /// Sets the optional address passed via `--listen`.
26    pub fn listen(mut self, listen: impl Into<String>) -> Self {
27        let listen = listen.into();
28        self.listen = (!listen.trim().is_empty()).then_some(listen);
29        self
30    }
31
32    /// Sets the working directory used to resolve relative paths.
33    pub fn working_dir(mut self, dir: impl Into<PathBuf>) -> Self {
34        self.working_dir = Some(dir.into());
35        self
36    }
37
38    /// Replaces the default CLI overrides for this request.
39    pub fn with_overrides(mut self, overrides: CliOverridesPatch) -> Self {
40        self.overrides = overrides;
41        self
42    }
43
44    /// Adds a `--config key=value` override for this request.
45    pub fn config_override(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
46        self.overrides
47            .config_overrides
48            .push(ConfigOverride::new(key, value));
49        self
50    }
51
52    /// Adds a raw `--config key=value` override without validation.
53    pub fn config_override_raw(mut self, raw: impl Into<String>) -> Self {
54        self.overrides
55            .config_overrides
56            .push(ConfigOverride::from_raw(raw));
57        self
58    }
59
60    /// Sets the config profile (`--profile`) for this request.
61    pub fn profile(mut self, profile: impl Into<String>) -> Self {
62        let profile = profile.into();
63        self.overrides.profile = (!profile.trim().is_empty()).then_some(profile);
64        self
65    }
66
67    /// Requests the CLI `--oss` flag for this call.
68    pub fn oss(mut self, enable: bool) -> Self {
69        self.overrides.oss = if enable {
70            FlagState::Enable
71        } else {
72            FlagState::Disable
73        };
74        self
75    }
76
77    /// Adds a `--enable <feature>` toggle for this call.
78    pub fn enable_feature(mut self, name: impl Into<String>) -> Self {
79        self.overrides.feature_toggles.enable.push(name.into());
80        self
81    }
82
83    /// Adds a `--disable <feature>` toggle for this call.
84    pub fn disable_feature(mut self, name: impl Into<String>) -> Self {
85        self.overrides.feature_toggles.disable.push(name.into());
86        self
87    }
88
89    /// Controls whether `--search` is passed through to Codex.
90    pub fn search(mut self, enable: bool) -> Self {
91        self.overrides.search = if enable {
92            FlagState::Enable
93        } else {
94            FlagState::Disable
95        };
96        self
97    }
98}
99
100impl Default for ExecServerRequest {
101    fn default() -> Self {
102        Self::new()
103    }
104}