codex.rs

  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 local_login_commands(&self) -> Vec<&'static str> {
 40        vec![]
 41    }
 42
 43    fn remote_login_commands(&self) -> Vec<&'static str> {
 44        vec![]
 45    }
 46
 47    fn local_logout_commands(&self) -> Vec<&'static str> {
 48        vec![]
 49    }
 50
 51    fn remote_logout_commands(&self) -> Vec<&'static str> {
 52        vec![]
 53    }
 54
 55    fn default_mode(&self, cx: &mut App) -> Option<acp::SessionModeId> {
 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_mode.clone().map(|m| acp::SessionModeId(m.into())))
 63    }
 64
 65    fn set_default_mode(&self, mode_id: Option<acp::SessionModeId>, 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_mode = mode_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<
 82        Result<(
 83            Rc<dyn AgentConnection>,
 84            HashMap<String, task::SpawnInTerminal>,
 85        )>,
 86    > {
 87        let name = self.name();
 88        let root_dir = root_dir.map(|root_dir| root_dir.to_string_lossy().into_owned());
 89        let is_remote = delegate.project.read(cx).is_via_remote_server();
 90        let store = delegate.store.downgrade();
 91        let extra_env = load_proxy_env(cx);
 92        let default_mode = self.default_mode(cx);
 93
 94        cx.spawn(async move |cx| {
 95            let (command, root_dir, auth_commands) = store
 96                .update(cx, |store, cx| {
 97                    let agent = store
 98                        .get_external_agent(&CODEX_NAME.into())
 99                        .context("Codex is not registered")?;
100                    anyhow::Ok(agent.get_command(
101                        root_dir.as_deref(),
102                        extra_env,
103                        delegate.status_tx,
104                        // For now, report that there are no updates.
105                        // (A future PR will use the GitHub Releases API to fetch them.)
106                        delegate.new_version_available,
107                        &mut cx.to_async(),
108                    ))
109                })??
110                .await?;
111
112            let connection = crate::acp::connect(
113                name,
114                command,
115                root_dir.as_ref(),
116                default_mode,
117                is_remote,
118                cx,
119            )
120            .await?;
121            Ok((connection, auth_commands))
122        })
123    }
124
125    fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
126        self
127    }
128}