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::provider::google::GoogleLanguageModelProvider;
  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
 41        cx.spawn(async move |cx| {
 42            extra_env.insert("SURFACE".to_owned(), "zed".to_owned());
 43
 44            if let Some(api_key) = cx
 45                .update(GoogleLanguageModelProvider::api_key_for_gemini_cli)?
 46                .await
 47                .ok()
 48            {
 49                extra_env.insert("GEMINI_API_KEY".into(), api_key);
 50            }
 51            let (command, root_dir, login) = store
 52                .update(cx, |store, cx| {
 53                    let agent = store
 54                        .get_external_agent(&GEMINI_NAME.into())
 55                        .context("Gemini CLI is not registered")?;
 56                    anyhow::Ok(agent.get_command(
 57                        root_dir.as_deref(),
 58                        extra_env,
 59                        delegate.status_tx,
 60                        delegate.new_version_available,
 61                        &mut cx.to_async(),
 62                    ))
 63                })??
 64                .await?;
 65
 66            let connection = crate::acp::connect(
 67                name,
 68                telemetry_id,
 69                command,
 70                root_dir.as_ref(),
 71                default_mode,
 72                is_remote,
 73                cx,
 74            )
 75            .await?;
 76            Ok((connection, login))
 77        })
 78    }
 79
 80    fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
 81        self
 82    }
 83}
 84
 85#[cfg(test)]
 86pub(crate) mod tests {
 87    use project::agent_server_store::AgentServerCommand;
 88
 89    use super::*;
 90    use std::path::Path;
 91
 92    crate::common_e2e_tests!(async |_, _, _| Gemini, allow_option_id = "proceed_once");
 93
 94    pub fn local_command() -> AgentServerCommand {
 95        let cli_path = Path::new(env!("CARGO_MANIFEST_DIR"))
 96            .join("../../../gemini-cli/packages/cli")
 97            .to_string_lossy()
 98            .to_string();
 99
100        AgentServerCommand {
101            path: "node".into(),
102            args: vec![cli_path],
103            env: None,
104        }
105    }
106}