Skip to main content

codex/commands/
features.rs

1use std::ffi::OsString;
2
3use tokio::{process::Command, time};
4
5use crate::{
6    builder::{apply_cli_overrides, resolve_cli_overrides},
7    process::{spawn_with_retry, tee_stream, ConsoleTarget},
8    ApplyDiffArtifacts, CodexClient, CodexError, FeaturesCommandRequest, FeaturesDisableRequest,
9    FeaturesEnableRequest, FeaturesListOutput, FeaturesListRequest,
10};
11
12impl CodexClient {
13    /// Runs `codex features` and returns captured output.
14    pub async fn features(
15        &self,
16        request: FeaturesCommandRequest,
17    ) -> Result<ApplyDiffArtifacts, CodexError> {
18        self.run_simple_command_with_overrides(vec![OsString::from("features")], request.overrides)
19            .await
20    }
21
22    /// Enables a CLI feature via `codex features enable <FEATURE>`.
23    pub async fn features_enable(
24        &self,
25        request: FeaturesEnableRequest,
26    ) -> Result<ApplyDiffArtifacts, CodexError> {
27        let FeaturesEnableRequest { feature, overrides } = request;
28        self.run_simple_command_with_overrides(
29            vec![
30                OsString::from("features"),
31                OsString::from("enable"),
32                OsString::from(feature),
33            ],
34            overrides,
35        )
36        .await
37    }
38
39    /// Disables a CLI feature via `codex features disable <FEATURE>`.
40    pub async fn features_disable(
41        &self,
42        request: FeaturesDisableRequest,
43    ) -> Result<ApplyDiffArtifacts, CodexError> {
44        let FeaturesDisableRequest { feature, overrides } = request;
45        self.run_simple_command_with_overrides(
46            vec![
47                OsString::from("features"),
48                OsString::from("disable"),
49                OsString::from(feature),
50            ],
51            overrides,
52        )
53        .await
54    }
55
56    /// Lists CLI features via `codex features list`.
57    ///
58    /// Requests JSON output when `json(true)` is set and falls back to parsing the text table when
59    /// JSON is unavailable. Shared config/profile/search/approval overrides flow through via the
60    /// request/builder, stdout/stderr are mirrored according to the builder, and non-zero exits
61    /// surface as [`CodexError::NonZeroExit`].
62    pub async fn list_features(
63        &self,
64        request: FeaturesListRequest,
65    ) -> Result<FeaturesListOutput, CodexError> {
66        let FeaturesListRequest { json, overrides } = request;
67
68        let dir_ctx = self.directory_context()?;
69        let resolved_overrides =
70            resolve_cli_overrides(&self.cli_overrides, &overrides, self.model.as_deref());
71
72        let mut command = Command::new(self.command_env.binary_path());
73        command
74            .stdout(std::process::Stdio::piped())
75            .stderr(std::process::Stdio::piped())
76            .kill_on_drop(true)
77            .current_dir(dir_ctx.path());
78
79        apply_cli_overrides(&mut command, &resolved_overrides, true);
80        command.arg("features").arg("list");
81
82        if json {
83            command.arg("--json");
84        }
85
86        self.command_env.apply(&mut command)?;
87
88        let mut child = spawn_with_retry(&mut command, self.command_env.binary_path())?;
89
90        let stdout = child.stdout.take().ok_or(CodexError::StdoutUnavailable)?;
91        let stderr = child.stderr.take().ok_or(CodexError::StderrUnavailable)?;
92
93        let stdout_task = tokio::spawn(tee_stream(
94            stdout,
95            ConsoleTarget::Stdout,
96            self.mirror_stdout,
97        ));
98        let stderr_task = tokio::spawn(tee_stream(stderr, ConsoleTarget::Stderr, !self.quiet));
99
100        let wait_task = async move {
101            let status = child
102                .wait()
103                .await
104                .map_err(|source| CodexError::Wait { source })?;
105            let stdout_bytes = stdout_task
106                .await
107                .map_err(CodexError::Join)?
108                .map_err(CodexError::CaptureIo)?;
109            let stderr_bytes = stderr_task
110                .await
111                .map_err(CodexError::Join)?
112                .map_err(CodexError::CaptureIo)?;
113            Ok::<_, CodexError>((status, stdout_bytes, stderr_bytes))
114        };
115
116        let (status, stdout_bytes, stderr_bytes) = if self.timeout.is_zero() {
117            wait_task.await?
118        } else {
119            match time::timeout(self.timeout, wait_task).await {
120                Ok(result) => result?,
121                Err(_) => {
122                    return Err(CodexError::Timeout {
123                        timeout: self.timeout,
124                    });
125                }
126            }
127        };
128
129        if !status.success() {
130            return Err(CodexError::NonZeroExit {
131                status,
132                stderr: String::from_utf8(stderr_bytes)?,
133            });
134        }
135
136        let stdout_string = String::from_utf8(stdout_bytes)?;
137        let stderr_string = String::from_utf8(stderr_bytes)?;
138        let (features, format) = crate::version::parse_feature_list_output(&stdout_string, json)
139            .map_err(|reason| CodexError::FeatureListParse {
140                reason,
141                stdout: stdout_string.clone(),
142            })?;
143
144        Ok(FeaturesListOutput {
145            status,
146            stdout: stdout_string,
147            stderr: stderr_string,
148            features,
149            format,
150        })
151    }
152}