1use std::rc::Rc;
2use std::sync::Arc;
3use std::{any::Any, path::Path};
4
5use acp_thread::AgentConnection;
6use agent_client_protocol as acp;
7use anyhow::{Context as _, Result};
8use collections::HashSet;
9use fs::Fs;
10use gpui::{App, AppContext as _, SharedString, Task};
11use project::agent_server_store::{AllAgentServersSettings, CODEX_NAME};
12use settings::{SettingsStore, update_settings_file};
13
14use crate::{AgentServer, AgentServerDelegate, load_proxy_env};
15
16#[derive(Clone)]
17pub struct Codex;
18
19#[cfg(test)]
20pub(crate) mod tests {
21 use super::*;
22
23 crate::common_e2e_tests!(async |_, _, _| Codex, allow_option_id = "proceed_once");
24}
25
26impl AgentServer for Codex {
27 fn name(&self) -> SharedString {
28 "Codex".into()
29 }
30
31 fn logo(&self) -> ui::IconName {
32 ui::IconName::AiOpenAi
33 }
34
35 fn default_mode(&self, cx: &App) -> Option<acp::SessionModeId> {
36 let settings = cx.read_global(|settings: &SettingsStore, _| {
37 settings.get::<AllAgentServersSettings>(None).codex.clone()
38 });
39
40 settings
41 .as_ref()
42 .and_then(|s| s.default_mode.clone().map(acp::SessionModeId::new))
43 }
44
45 fn set_default_mode(&self, mode_id: Option<acp::SessionModeId>, fs: Arc<dyn Fs>, cx: &mut App) {
46 update_settings_file(fs, cx, |settings, _| {
47 settings
48 .agent_servers
49 .get_or_insert_default()
50 .codex
51 .get_or_insert_default()
52 .default_mode = mode_id.map(|m| m.to_string())
53 });
54 }
55
56 fn default_model(&self, cx: &App) -> Option<acp::ModelId> {
57 let settings = cx.read_global(|settings: &SettingsStore, _| {
58 settings.get::<AllAgentServersSettings>(None).codex.clone()
59 });
60
61 settings
62 .as_ref()
63 .and_then(|s| s.default_model.clone().map(acp::ModelId::new))
64 }
65
66 fn set_default_model(&self, model_id: Option<acp::ModelId>, fs: Arc<dyn Fs>, cx: &mut App) {
67 update_settings_file(fs, cx, |settings, _| {
68 settings
69 .agent_servers
70 .get_or_insert_default()
71 .codex
72 .get_or_insert_default()
73 .default_model = model_id.map(|m| m.to_string())
74 });
75 }
76
77 fn favorite_model_ids(&self, cx: &mut App) -> HashSet<acp::ModelId> {
78 let settings = cx.read_global(|settings: &SettingsStore, _| {
79 settings.get::<AllAgentServersSettings>(None).codex.clone()
80 });
81
82 settings
83 .as_ref()
84 .map(|s| {
85 s.favorite_models
86 .iter()
87 .map(|id| acp::ModelId::new(id.clone()))
88 .collect()
89 })
90 .unwrap_or_default()
91 }
92
93 fn toggle_favorite_model(
94 &self,
95 model_id: acp::ModelId,
96 should_be_favorite: bool,
97 fs: Arc<dyn Fs>,
98 cx: &App,
99 ) {
100 update_settings_file(fs, cx, move |settings, _| {
101 let favorite_models = &mut settings
102 .agent_servers
103 .get_or_insert_default()
104 .codex
105 .get_or_insert_default()
106 .favorite_models;
107
108 let model_id_str = model_id.to_string();
109 if should_be_favorite {
110 if !favorite_models.contains(&model_id_str) {
111 favorite_models.push(model_id_str);
112 }
113 } else {
114 favorite_models.retain(|id| id != &model_id_str);
115 }
116 });
117 }
118
119 fn default_config_option(&self, config_id: &str, cx: &App) -> Option<String> {
120 let settings = cx.read_global(|settings: &SettingsStore, _| {
121 settings.get::<AllAgentServersSettings>(None).codex.clone()
122 });
123
124 settings
125 .as_ref()
126 .and_then(|s| s.default_config_options.get(config_id).cloned())
127 }
128
129 fn set_default_config_option(
130 &self,
131 config_id: &str,
132 value_id: Option<&str>,
133 fs: Arc<dyn Fs>,
134 cx: &mut App,
135 ) {
136 let config_id = config_id.to_string();
137 let value_id = value_id.map(|s| s.to_string());
138 update_settings_file(fs, cx, move |settings, _| {
139 let config_options = &mut settings
140 .agent_servers
141 .get_or_insert_default()
142 .codex
143 .get_or_insert_default()
144 .default_config_options;
145
146 if let Some(value) = value_id.clone() {
147 config_options.insert(config_id.clone(), value);
148 } else {
149 config_options.remove(&config_id);
150 }
151 });
152 }
153
154 fn favorite_config_option_value_ids(
155 &self,
156 config_id: &acp::SessionConfigId,
157 cx: &mut App,
158 ) -> HashSet<acp::SessionConfigValueId> {
159 let settings = cx.read_global(|settings: &SettingsStore, _| {
160 settings.get::<AllAgentServersSettings>(None).codex.clone()
161 });
162
163 settings
164 .as_ref()
165 .and_then(|s| s.favorite_config_option_values.get(config_id.0.as_ref()))
166 .map(|values| {
167 values
168 .iter()
169 .cloned()
170 .map(acp::SessionConfigValueId::new)
171 .collect()
172 })
173 .unwrap_or_default()
174 }
175
176 fn toggle_favorite_config_option_value(
177 &self,
178 config_id: acp::SessionConfigId,
179 value_id: acp::SessionConfigValueId,
180 should_be_favorite: bool,
181 fs: Arc<dyn Fs>,
182 cx: &App,
183 ) {
184 let config_id = config_id.to_string();
185 let value_id = value_id.to_string();
186
187 update_settings_file(fs, cx, move |settings, _| {
188 let favorites = &mut settings
189 .agent_servers
190 .get_or_insert_default()
191 .codex
192 .get_or_insert_default()
193 .favorite_config_option_values;
194
195 let entry = favorites.entry(config_id.clone()).or_insert_with(Vec::new);
196
197 if should_be_favorite {
198 if !entry.iter().any(|v| v == &value_id) {
199 entry.push(value_id.clone());
200 }
201 } else {
202 entry.retain(|v| v != &value_id);
203 if entry.is_empty() {
204 favorites.remove(&config_id);
205 }
206 }
207 });
208 }
209
210 fn connect(
211 &self,
212 root_dir: Option<&Path>,
213 delegate: AgentServerDelegate,
214 cx: &mut App,
215 ) -> Task<Result<(Rc<dyn AgentConnection>, Option<task::SpawnInTerminal>)>> {
216 let name = self.name();
217 let root_dir = root_dir.map(|root_dir| root_dir.to_string_lossy().into_owned());
218 let is_remote = delegate.project.read(cx).is_via_remote_server();
219 let store = delegate.store.downgrade();
220 let extra_env = load_proxy_env(cx);
221 let default_mode = self.default_mode(cx);
222 let default_model = self.default_model(cx);
223 let default_config_options = cx.read_global(|settings: &SettingsStore, _| {
224 settings
225 .get::<AllAgentServersSettings>(None)
226 .codex
227 .as_ref()
228 .map(|s| s.default_config_options.clone())
229 .unwrap_or_default()
230 });
231
232 cx.spawn(async move |cx| {
233 let (command, root_dir, login) = store
234 .update(cx, |store, cx| {
235 let agent = store
236 .get_external_agent(&CODEX_NAME.into())
237 .context("Codex is not registered")?;
238 anyhow::Ok(agent.get_command(
239 root_dir.as_deref(),
240 extra_env,
241 delegate.status_tx,
242 delegate.new_version_available,
243 &mut cx.to_async(),
244 ))
245 })??
246 .await?;
247
248 let connection = crate::acp::connect(
249 name,
250 command,
251 root_dir.as_ref(),
252 default_mode,
253 default_model,
254 default_config_options,
255 is_remote,
256 cx,
257 )
258 .await?;
259 Ok((connection, login))
260 })
261 }
262
263 fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
264 self
265 }
266}