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