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::{Entity, Task};
  8use project::Project;
  9use settings::SettingsStore;
 10use ui::App;
 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) -> &'static str {
 21        "Gemini CLI"
 22    }
 23
 24    fn empty_state_headline(&self) -> &'static str {
 25        "Welcome to Gemini CLI"
 26    }
 27
 28    fn empty_state_message(&self) -> &'static str {
 29        "Ask questions, edit files, run commands"
 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(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            let result = crate::acp::connect(server_name, command.clone(), &root_dir, cx).await;
 61            if result.is_err() {
 62                let version_fut = util::command::new_smol_command(&command.path)
 63                    .args(command.args.iter())
 64                    .arg("--version")
 65                    .kill_on_drop(true)
 66                    .output();
 67
 68                let help_fut = util::command::new_smol_command(&command.path)
 69                    .args(command.args.iter())
 70                    .arg("--help")
 71                    .kill_on_drop(true)
 72                    .output();
 73
 74                let (version_output, help_output) = futures::future::join(version_fut, help_fut).await;
 75
 76                let current_version = String::from_utf8(version_output?.stdout)?;
 77                let supported = String::from_utf8(help_output?.stdout)?.contains(ACP_ARG);
 78
 79                if !supported {
 80                    return Err(LoadError::Unsupported {
 81                        error_message: format!(
 82                            "Your installed version of Gemini CLI ({}, version {}) doesn't support the Agentic Coding Protocol (ACP).",
 83                            command.path.to_string_lossy(),
 84                            current_version
 85                        ).into(),
 86                        upgrade_message: "Upgrade Gemini CLI to latest".into(),
 87                        upgrade_command: "npm install -g @google/gemini-cli@latest".into(),
 88                    }.into())
 89                }
 90            }
 91            result
 92        })
 93    }
 94
 95    fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
 96        self
 97    }
 98}
 99
100#[cfg(test)]
101pub(crate) mod tests {
102    use super::*;
103    use crate::AgentServerCommand;
104    use std::path::Path;
105
106    crate::common_e2e_tests!(Gemini, allow_option_id = "proceed_once");
107
108    pub fn local_command() -> AgentServerCommand {
109        let cli_path = Path::new(env!("CARGO_MANIFEST_DIR"))
110            .join("../../../gemini-cli/packages/cli")
111            .to_string_lossy()
112            .to_string();
113
114        AgentServerCommand {
115            path: "node".into(),
116            args: vec![cli_path],
117            env: None,
118        }
119    }
120}