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