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 executor identifier passed via `--executor-id`.
11    pub executor_id: Option<String>,
12    /// Optional display name passed via `--name`.
13    pub name: Option<String>,
14    /// Optional working directory override for the spawned process.
15    pub working_dir: Option<PathBuf>,
16    /// Per-call CLI overrides layered on top of the builder.
17    pub overrides: CliOverridesPatch,
18}
19
20impl ExecServerRequest {
21    pub fn new() -> Self {
22        Self {
23            listen: None,
24            executor_id: None,
25            name: None,
26            working_dir: None,
27            overrides: CliOverridesPatch::default(),
28        }
29    }
30
31    /// Sets the optional address passed via `--listen`.
32    pub fn listen(mut self, listen: impl Into<String>) -> Self {
33        let listen = listen.into();
34        self.listen = (!listen.trim().is_empty()).then_some(listen);
35        self
36    }
37
38    /// Sets the optional executor identifier passed via `--executor-id`.
39    pub fn executor_id(mut self, executor_id: impl Into<String>) -> Self {
40        let executor_id = executor_id.into();
41        self.executor_id = (!executor_id.trim().is_empty()).then_some(executor_id);
42        self
43    }
44
45    /// Sets the optional display name passed via `--name`.
46    pub fn name(mut self, name: impl Into<String>) -> Self {
47        let name = name.into();
48        self.name = (!name.trim().is_empty()).then_some(name);
49        self
50    }
51
52    /// Sets the working directory used to resolve relative paths.
53    pub fn working_dir(mut self, dir: impl Into<PathBuf>) -> Self {
54        self.working_dir = Some(dir.into());
55        self
56    }
57
58    /// Replaces the default CLI overrides for this request.
59    pub fn with_overrides(mut self, overrides: CliOverridesPatch) -> Self {
60        self.overrides = overrides;
61        self
62    }
63
64    /// Adds a `--config key=value` override for this request.
65    pub fn config_override(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
66        self.overrides
67            .config_overrides
68            .push(ConfigOverride::new(key, value));
69        self
70    }
71
72    /// Adds a raw `--config key=value` override without validation.
73    pub fn config_override_raw(mut self, raw: impl Into<String>) -> Self {
74        self.overrides
75            .config_overrides
76            .push(ConfigOverride::from_raw(raw));
77        self
78    }
79
80    /// Sets the config profile (`--profile`) for this request.
81    pub fn profile(mut self, profile: impl Into<String>) -> Self {
82        let profile = profile.into();
83        self.overrides.profile = (!profile.trim().is_empty()).then_some(profile);
84        self
85    }
86
87    /// Requests the CLI `--oss` flag for this call.
88    pub fn oss(mut self, enable: bool) -> Self {
89        self.overrides.oss = if enable {
90            FlagState::Enable
91        } else {
92            FlagState::Disable
93        };
94        self
95    }
96
97    /// Adds a `--enable <feature>` toggle for this call.
98    pub fn enable_feature(mut self, name: impl Into<String>) -> Self {
99        self.overrides.feature_toggles.enable.push(name.into());
100        self
101    }
102
103    /// Adds a `--disable <feature>` toggle for this call.
104    pub fn disable_feature(mut self, name: impl Into<String>) -> Self {
105        self.overrides.feature_toggles.disable.push(name.into());
106        self
107    }
108
109    /// Controls whether `--search` is passed through to Codex.
110    pub fn search(mut self, enable: bool) -> Self {
111        self.overrides.search = if enable {
112            FlagState::Enable
113        } else {
114            FlagState::Disable
115        };
116        self
117    }
118}
119
120impl Default for ExecServerRequest {
121    fn default() -> Self {
122        Self::new()
123    }
124}