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 fs::Fs;
9use gpui::{App, AppContext as _, SharedString, Task};
10use project::agent_server_store::{AllAgentServersSettings, CODEX_NAME};
11use settings::{SettingsStore, update_settings_file};
12
13use crate::{AgentServer, AgentServerDelegate, load_proxy_env};
14
15#[derive(Clone)]
16pub struct Codex;
17
18#[cfg(test)]
19pub(crate) mod tests {
20 use super::*;
21
22 crate::common_e2e_tests!(async |_, _, _| Codex, allow_option_id = "proceed_once");
23}
24
25impl AgentServer for Codex {
26 fn name(&self) -> SharedString {
27 "Codex".into()
28 }
29
30 fn logo(&self) -> ui::IconName {
31 ui::IconName::AiOpenAi
32 }
33
34 fn default_mode(&self, cx: &mut App) -> Option<acp::SessionModeId> {
35 let settings = cx.read_global(|settings: &SettingsStore, _| {
36 settings.get::<AllAgentServersSettings>(None).codex.clone()
37 });
38
39 settings
40 .as_ref()
41 .and_then(|s| s.default_mode.clone().map(acp::SessionModeId::new))
42 }
43
44 fn set_default_mode(&self, mode_id: Option<acp::SessionModeId>, fs: Arc<dyn Fs>, cx: &mut App) {
45 update_settings_file(fs, cx, |settings, _| {
46 settings
47 .agent_servers
48 .get_or_insert_default()
49 .codex
50 .get_or_insert_default()
51 .default_mode = mode_id.map(|m| m.to_string())
52 });
53 }
54
55 fn default_model(&self, cx: &mut App) -> Option<acp::ModelId> {
56 let settings = cx.read_global(|settings: &SettingsStore, _| {
57 settings.get::<AllAgentServersSettings>(None).codex.clone()
58 });
59
60 settings
61 .as_ref()
62 .and_then(|s| s.default_model.clone().map(acp::ModelId::new))
63 }
64
65 fn set_default_model(&self, model_id: Option<acp::ModelId>, fs: Arc<dyn Fs>, cx: &mut App) {
66 update_settings_file(fs, cx, |settings, _| {
67 settings
68 .agent_servers
69 .get_or_insert_default()
70 .codex
71 .get_or_insert_default()
72 .default_model = model_id.map(|m| m.to_string())
73 });
74 }
75
76 fn connect(
77 &self,
78 root_dir: Option<&Path>,
79 delegate: AgentServerDelegate,
80 cx: &mut App,
81 ) -> Task<Result<(Rc<dyn AgentConnection>, Option<task::SpawnInTerminal>)>> {
82 let name = self.name();
83 let root_dir = root_dir.map(|root_dir| root_dir.to_string_lossy().into_owned());
84 let is_remote = delegate.project.read(cx).is_via_remote_server();
85 let store = delegate.store.downgrade();
86 let extra_env = load_proxy_env(cx);
87 let default_mode = self.default_mode(cx);
88 let default_model = self.default_model(cx);
89
90 cx.spawn(async move |cx| {
91 let (command, root_dir, login) = store
92 .update(cx, |store, cx| {
93 let agent = store
94 .get_external_agent(&CODEX_NAME.into())
95 .context("Codex is not registered")?;
96 anyhow::Ok(agent.get_command(
97 root_dir.as_deref(),
98 extra_env,
99 delegate.status_tx,
100 delegate.new_version_available,
101 &mut cx.to_async(),
102 ))
103 })??
104 .await?;
105
106 let connection = crate::acp::connect(
107 name,
108 command,
109 root_dir.as_ref(),
110 default_mode,
111 default_model,
112 is_remote,
113 cx,
114 )
115 .await?;
116 Ok((connection, login))
117 })
118 }
119
120 fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
121 self
122 }
123}