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