gemini.rs

  1use std::rc::Rc;
  2use std::{any::Any, path::Path};
  3
  4use crate::acp::AcpConnection;
  5use crate::{AgentServer, AgentServerDelegate};
  6use acp_thread::{AgentConnection, LoadError};
  7use anyhow::Result;
  8use gpui::{App, SharedString, Task};
  9use language_models::provider::google::GoogleLanguageModelProvider;
 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 logo(&self) -> ui::IconName {
 29        ui::IconName::AiGemini
 30    }
 31
 32    fn connect(
 33        &self,
 34        root_dir: &Path,
 35        delegate: AgentServerDelegate,
 36        cx: &mut App,
 37    ) -> Task<Result<Rc<dyn AgentConnection>>> {
 38        let root_dir = root_dir.to_path_buf();
 39        let server_name = self.name();
 40        cx.spawn(async move |cx| {
 41            let settings = cx.read_global(|settings: &SettingsStore, _| {
 42                settings.get::<AllAgentServersSettings>(None).gemini.clone()
 43            })?;
 44
 45            let mut command = cx
 46                .update(|cx| {
 47                    delegate.get_or_npm_install_builtin_agent(
 48                        Self::BINARY_NAME.into(),
 49                        Self::PACKAGE_NAME.into(),
 50                        format!("node_modules/{}/dist/index.js", Self::PACKAGE_NAME).into(),
 51                        settings,
 52                        Some("0.2.1".parse().unwrap()),
 53                        cx,
 54                    )
 55                })?
 56                .await?;
 57            command.args.push("--experimental-acp".into());
 58
 59            if let Some(api_key) = cx.update(GoogleLanguageModelProvider::api_key)?.await.ok() {
 60                command
 61                    .env
 62                    .get_or_insert_default()
 63                    .insert("GEMINI_API_KEY".to_owned(), api_key.key);
 64            }
 65
 66            let result = crate::acp::connect(server_name, command.clone(), &root_dir, cx).await;
 67            match &result {
 68                Ok(connection) => {
 69                    if let Some(connection) = connection.clone().downcast::<AcpConnection>()
 70                        && !connection.prompt_capabilities().image
 71                    {
 72                        let version_output = util::command::new_smol_command(&command.path)
 73                            .args(command.args.iter())
 74                            .arg("--version")
 75                            .kill_on_drop(true)
 76                            .output()
 77                            .await;
 78                        let current_version =
 79                            String::from_utf8(version_output?.stdout)?.trim().to_owned();
 80                        if !connection.prompt_capabilities().image {
 81                            return Err(LoadError::Unsupported {
 82                                current_version: current_version.into(),
 83                                command: command.path.to_string_lossy().to_string().into(),
 84                                minimum_version: Self::MINIMUM_VERSION.into(),
 85                            }
 86                            .into());
 87                        }
 88                    }
 89                }
 90                Err(_) => {
 91                    let version_fut = util::command::new_smol_command(&command.path)
 92                        .args(command.args.iter())
 93                        .arg("--version")
 94                        .kill_on_drop(true)
 95                        .output();
 96
 97                    let help_fut = util::command::new_smol_command(&command.path)
 98                        .args(command.args.iter())
 99                        .arg("--help")
100                        .kill_on_drop(true)
101                        .output();
102
103                    let (version_output, help_output) =
104                        futures::future::join(version_fut, help_fut).await;
105
106                    let current_version = std::str::from_utf8(&version_output?.stdout)?
107                        .trim()
108                        .to_string();
109                    let supported = String::from_utf8(help_output?.stdout)?.contains(ACP_ARG);
110
111                    if !supported {
112                        return Err(LoadError::Unsupported {
113                            current_version: current_version.into(),
114                            command: command.path.to_string_lossy().to_string().into(),
115                            minimum_version: Self::MINIMUM_VERSION.into(),
116                        }
117                        .into());
118                    }
119                }
120            }
121            result
122        })
123    }
124
125    fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
126        self
127    }
128}
129
130impl Gemini {
131    const PACKAGE_NAME: &str = "@google/gemini-cli";
132
133    const MINIMUM_VERSION: &str = "0.2.1";
134
135    const BINARY_NAME: &str = "gemini";
136}
137
138#[cfg(test)]
139pub(crate) mod tests {
140    use super::*;
141    use crate::AgentServerCommand;
142    use std::path::Path;
143
144    crate::common_e2e_tests!(async |_, _, _| Gemini, allow_option_id = "proceed_once");
145
146    pub fn local_command() -> AgentServerCommand {
147        let cli_path = Path::new(env!("CARGO_MANIFEST_DIR"))
148            .join("../../../gemini-cli/packages/cli")
149            .to_string_lossy()
150            .to_string();
151
152        AgentServerCommand {
153            path: "node".into(),
154            args: vec![cli_path],
155            env: None,
156        }
157    }
158}