gemini.rs

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