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::HashMap;
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 telemetry_id(&self) -> &'static str {
28 "codex"
29 }
30
31 fn name(&self) -> SharedString {
32 "Codex".into()
33 }
34
35 fn logo(&self) -> ui::IconName {
36 ui::IconName::AiOpenAi
37 }
38
39 fn default_mode(&self, cx: &mut App) -> Option<acp::SessionModeId> {
40 let settings = cx.read_global(|settings: &SettingsStore, _| {
41 settings.get::<AllAgentServersSettings>(None).codex.clone()
42 });
43
44 settings
45 .as_ref()
46 .and_then(|s| s.default_mode.clone().map(|m| acp::SessionModeId(m.into())))
47 }
48
49 fn set_default_mode(&self, mode_id: Option<acp::SessionModeId>, fs: Arc<dyn Fs>, cx: &mut App) {
50 update_settings_file(fs, cx, |settings, _| {
51 settings
52 .agent_servers
53 .get_or_insert_default()
54 .codex
55 .get_or_insert_default()
56 .default_mode = mode_id.map(|m| m.to_string())
57 });
58 }
59
60 fn connect(
61 &self,
62 root_dir: Option<&Path>,
63 delegate: AgentServerDelegate,
64 cx: &mut App,
65 ) -> Task<
66 Result<(
67 Rc<dyn AgentConnection>,
68 HashMap<String, task::SpawnInTerminal>,
69 )>,
70 > {
71 let name = self.name();
72 let root_dir = root_dir.map(|root_dir| root_dir.to_string_lossy().into_owned());
73 let is_remote = delegate.project.read(cx).is_via_remote_server();
74 let store = delegate.store.downgrade();
75 let extra_env = load_proxy_env(cx);
76 let default_mode = self.default_mode(cx);
77
78 cx.spawn(async move |cx| {
79 let (command, root_dir, auth_commands) = store
80 .update(cx, |store, cx| {
81 let agent = store
82 .get_external_agent(&CODEX_NAME.into())
83 .context("Codex is not registered")?;
84 anyhow::Ok(agent.get_command(
85 root_dir.as_deref(),
86 extra_env,
87 delegate.status_tx,
88 // For now, report that there are no updates.
89 // (A future PR will use the GitHub Releases API to fetch them.)
90 delegate.new_version_available,
91 &mut cx.to_async(),
92 ))
93 })??
94 .await?;
95
96 let connection = crate::acp::connect(
97 name,
98 command,
99 root_dir.as_ref(),
100 default_mode,
101 is_remote,
102 cx,
103 )
104 .await?;
105 Ok((connection, auth_commands))
106 })
107 }
108
109 fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
110 self
111 }
112}