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