gemini.rs

  1use std::rc::Rc;
  2use std::{any::Any, path::Path};
  3
  4use crate::{AgentServer, AgentServerDelegate, load_proxy_env};
  5use acp_thread::AgentConnection;
  6use anyhow::{Context as _, Result};
  7use collections::HashMap;
  8use gpui::{App, SharedString, Task};
  9use language_models::provider::google::GoogleLanguageModelProvider;
 10use project::agent_server_store::GEMINI_NAME;
 11
 12#[derive(Clone)]
 13pub struct Gemini;
 14
 15impl AgentServer for Gemini {
 16    fn telemetry_id(&self) -> &'static str {
 17        "gemini-cli"
 18    }
 19
 20    fn name(&self) -> SharedString {
 21        "Gemini CLI".into()
 22    }
 23
 24    fn logo(&self) -> ui::IconName {
 25        ui::IconName::AiGemini
 26    }
 27
 28    fn local_login_commands(&self) -> Vec<&'static str> {
 29        vec!["login"]
 30    }
 31
 32    fn remote_login_commands(&self) -> Vec<&'static str> {
 33        // When remote, OAuth doesn't work, so login is handled via the
 34        // auth_commands mapping (oauth-personal -> spawn-gemini-cli)
 35        vec![]
 36    }
 37
 38    fn local_logout_commands(&self) -> Vec<&'static str> {
 39        vec![]
 40    }
 41
 42    fn remote_logout_commands(&self) -> Vec<&'static str> {
 43        vec![]
 44    }
 45
 46    fn connect(
 47        &self,
 48        root_dir: Option<&Path>,
 49        delegate: AgentServerDelegate,
 50        cx: &mut App,
 51    ) -> Task<
 52        Result<(
 53            Rc<dyn AgentConnection>,
 54            HashMap<String, task::SpawnInTerminal>,
 55        )>,
 56    > {
 57        let name = self.name();
 58        let root_dir = root_dir.map(|root_dir| root_dir.to_string_lossy().into_owned());
 59        let is_remote = delegate.project.read(cx).is_via_remote_server();
 60        let store = delegate.store.downgrade();
 61        let mut extra_env = load_proxy_env(cx);
 62        let default_mode = self.default_mode(cx);
 63
 64        cx.spawn(async move |cx| {
 65            extra_env.insert("SURFACE".to_owned(), "zed".to_owned());
 66
 67            if let Some(api_key) = cx
 68                .update(GoogleLanguageModelProvider::api_key_for_gemini_cli)?
 69                .await
 70                .ok()
 71            {
 72                extra_env.insert("GEMINI_API_KEY".into(), api_key);
 73            }
 74            let (command, root_dir, auth_commands) = store
 75                .update(cx, |store, cx| {
 76                    let agent = store
 77                        .get_external_agent(&GEMINI_NAME.into())
 78                        .context("Gemini CLI 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                command,
 92                root_dir.as_ref(),
 93                default_mode,
 94                is_remote,
 95                cx,
 96            )
 97            .await?;
 98            Ok((connection, auth_commands))
 99        })
100    }
101
102    fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
103        self
104    }
105}
106
107#[cfg(test)]
108pub(crate) mod tests {
109    use project::agent_server_store::AgentServerCommand;
110
111    use super::*;
112    use std::path::Path;
113
114    crate::common_e2e_tests!(async |_, _, _| Gemini, allow_option_id = "proceed_once");
115
116    pub fn local_command() -> AgentServerCommand {
117        let cli_path = Path::new(env!("CARGO_MANIFEST_DIR"))
118            .join("../../../gemini-cli/packages/cli")
119            .to_string_lossy()
120            .to_string();
121
122        AgentServerCommand {
123            path: "node".into(),
124            args: vec![cli_path],
125            env: None,
126        }
127    }
128}