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 gpui::{App, SharedString, Task};
  8use language_models::api_key_for_gemini_cli;
  9use project::agent_server_store::GEMINI_NAME;
 10
 11#[derive(Clone)]
 12pub struct Gemini;
 13
 14impl AgentServer for Gemini {
 15    fn telemetry_id(&self) -> &'static str {
 16        "gemini-cli"
 17    }
 18
 19    fn name(&self) -> SharedString {
 20        "Gemini CLI".into()
 21    }
 22
 23    fn logo(&self) -> ui::IconName {
 24        ui::IconName::AiGemini
 25    }
 26
 27    fn connect(
 28        &self,
 29        root_dir: Option<&Path>,
 30        delegate: AgentServerDelegate,
 31        cx: &mut App,
 32    ) -> Task<Result<(Rc<dyn AgentConnection>, Option<task::SpawnInTerminal>)>> {
 33        let name = self.name();
 34        let telemetry_id = self.telemetry_id();
 35        let root_dir = root_dir.map(|root_dir| root_dir.to_string_lossy().into_owned());
 36        let is_remote = delegate.project.read(cx).is_via_remote_server();
 37        let store = delegate.store.downgrade();
 38        let mut extra_env = load_proxy_env(cx);
 39        let default_mode = self.default_mode(cx);
 40        let default_model = self.default_model(cx);
 41
 42        cx.spawn(async move |cx| {
 43            extra_env.insert("SURFACE".to_owned(), "zed".to_owned());
 44
 45            if let Some(api_key) = cx.update(api_key_for_gemini_cli)?.await.ok() {
 46                extra_env.insert("GEMINI_API_KEY".into(), api_key);
 47            }
 48            let (command, root_dir, login) = store
 49                .update(cx, |store, cx| {
 50                    let agent = store
 51                        .get_external_agent(&GEMINI_NAME.into())
 52                        .context("Gemini CLI is not registered")?;
 53                    anyhow::Ok(agent.get_command(
 54                        root_dir.as_deref(),
 55                        extra_env,
 56                        delegate.status_tx,
 57                        delegate.new_version_available,
 58                        &mut cx.to_async(),
 59                    ))
 60                })??
 61                .await?;
 62
 63            let connection = crate::acp::connect(
 64                name,
 65                telemetry_id,
 66                command,
 67                root_dir.as_ref(),
 68                default_mode,
 69                default_model,
 70                is_remote,
 71                cx,
 72            )
 73            .await?;
 74            Ok((connection, login))
 75        })
 76    }
 77
 78    fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
 79        self
 80    }
 81}
 82
 83#[cfg(test)]
 84pub(crate) mod tests {
 85    use project::agent_server_store::AgentServerCommand;
 86
 87    use super::*;
 88    use std::path::Path;
 89
 90    crate::common_e2e_tests!(async |_, _, _| Gemini, allow_option_id = "proceed_once");
 91
 92    pub fn local_command() -> AgentServerCommand {
 93        let cli_path = Path::new(env!("CARGO_MANIFEST_DIR"))
 94            .join("../../../gemini-cli/packages/cli")
 95            .to_string_lossy()
 96            .to_string();
 97
 98        AgentServerCommand {
 99            path: "node".into(),
100            args: vec![cli_path],
101            env: None,
102        }
103    }
104}