gemini.rs

  1use std::rc::Rc;
  2use std::{any::Any, path::Path};
  3
  4use crate::{AgentServer, AgentServerCommand};
  5use acp_thread::{AgentConnection, LoadError};
  6use anyhow::Result;
  7use gpui::{App, Entity, SharedString, Task};
  8use language_models::provider::google::GoogleLanguageModelProvider;
  9use project::Project;
 10use settings::SettingsStore;
 11
 12use crate::AllAgentServersSettings;
 13
 14#[derive(Clone)]
 15pub struct Gemini;
 16
 17const ACP_ARG: &str = "--experimental-acp";
 18
 19impl AgentServer for Gemini {
 20    fn telemetry_id(&self) -> &'static str {
 21        "gemini-cli"
 22    }
 23
 24    fn name(&self) -> SharedString {
 25        "Gemini CLI".into()
 26    }
 27
 28    fn empty_state_headline(&self) -> SharedString {
 29        self.name()
 30    }
 31
 32    fn empty_state_message(&self) -> SharedString {
 33        "Ask questions, edit files, run commands".into()
 34    }
 35
 36    fn logo(&self) -> ui::IconName {
 37        ui::IconName::AiGemini
 38    }
 39
 40    fn connect(
 41        &self,
 42        root_dir: &Path,
 43        project: &Entity<Project>,
 44        cx: &mut App,
 45    ) -> Task<Result<Rc<dyn AgentConnection>>> {
 46        let project = project.clone();
 47        let root_dir = root_dir.to_path_buf();
 48        let server_name = self.name();
 49        cx.spawn(async move |cx| {
 50            let settings = cx.read_global(|settings: &SettingsStore, _| {
 51                settings.get::<AllAgentServersSettings>(None).gemini.clone()
 52            })?;
 53
 54            let Some(mut command) =
 55                AgentServerCommand::resolve("gemini", &[ACP_ARG], None, settings, &project, cx).await
 56            else {
 57                return Err(LoadError::NotInstalled {
 58                    error_message: "Failed to find Gemini CLI binary".into(),
 59                    install_message: "Install Gemini CLI".into(),
 60                    install_command: Self::install_command().into(),
 61                }.into());
 62            };
 63
 64            if let Some(api_key)= cx.update(GoogleLanguageModelProvider::api_key)?.await.ok() {
 65                command.env.get_or_insert_default().insert("GEMINI_API_KEY".to_owned(), api_key.key);
 66            }
 67
 68            let result = crate::acp::connect(server_name, command.clone(), &root_dir, cx).await;
 69            if result.is_err() {
 70                let version_fut = util::command::new_smol_command(&command.path)
 71                    .args(command.args.iter())
 72                    .arg("--version")
 73                    .kill_on_drop(true)
 74                    .output();
 75
 76                let help_fut = util::command::new_smol_command(&command.path)
 77                    .args(command.args.iter())
 78                    .arg("--help")
 79                    .kill_on_drop(true)
 80                    .output();
 81
 82                let (version_output, help_output) = futures::future::join(version_fut, help_fut).await;
 83
 84                let current_version = String::from_utf8(version_output?.stdout)?;
 85                let supported = String::from_utf8(help_output?.stdout)?.contains(ACP_ARG);
 86
 87                if !supported {
 88                    return Err(LoadError::Unsupported {
 89                        error_message: format!(
 90                            "Your installed version of Gemini CLI ({}, version {}) doesn't support the Agentic Coding Protocol (ACP).",
 91                            command.path.to_string_lossy(),
 92                            current_version
 93                        ).into(),
 94                        upgrade_message: "Upgrade Gemini CLI to latest".into(),
 95                        upgrade_command: Self::upgrade_command().into(),
 96                    }.into())
 97                }
 98            }
 99            result
100        })
101    }
102
103    fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
104        self
105    }
106}
107
108impl Gemini {
109    pub fn binary_name() -> &'static str {
110        "gemini"
111    }
112
113    pub fn install_command() -> &'static str {
114        "npm install -g @google/gemini-cli@preview"
115    }
116
117    pub fn upgrade_command() -> &'static str {
118        "npm install -g @google/gemini-cli@preview"
119    }
120}
121
122#[cfg(test)]
123pub(crate) mod tests {
124    use super::*;
125    use crate::AgentServerCommand;
126    use std::path::Path;
127
128    crate::common_e2e_tests!(async |_, _, _| Gemini, allow_option_id = "proceed_once");
129
130    pub fn local_command() -> AgentServerCommand {
131        let cli_path = Path::new(env!("CARGO_MANIFEST_DIR"))
132            .join("../../../gemini-cli/packages/cli")
133            .to_string_lossy()
134            .to_string();
135
136        AgentServerCommand {
137            path: "node".into(),
138            args: vec![cli_path],
139            env: None,
140        }
141    }
142}