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, AppContext as _, 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 let settings = cx.read_global(|settings: &SettingsStore, _| {
41 settings.get::<AllAgentServersSettings>(None).gemini.clone()
42 });
43
44 cx.spawn(async move |cx| {
45 let ignore_system_version = settings
46 .as_ref()
47 .and_then(|settings| settings.ignore_system_version)
48 .unwrap_or(true);
49 let mut command = if let Some(settings) = settings
50 && let Some(command) = settings.custom_command()
51 {
52 command
53 } else {
54 cx.update(|cx| {
55 delegate.get_or_npm_install_builtin_agent(
56 Self::BINARY_NAME.into(),
57 Self::PACKAGE_NAME.into(),
58 format!("node_modules/{}/dist/index.js", Self::PACKAGE_NAME).into(),
59 ignore_system_version,
60 Some(Self::MINIMUM_VERSION.parse().unwrap()),
61 cx,
62 )
63 })?
64 .await?
65 };
66 command.args.push("--experimental-acp".into());
67
68 if let Some(api_key) = cx.update(GoogleLanguageModelProvider::api_key)?.await.ok() {
69 command
70 .env
71 .get_or_insert_default()
72 .insert("GEMINI_API_KEY".to_owned(), api_key.key);
73 }
74
75 let result = crate::acp::connect(server_name, command.clone(), &root_dir, cx).await;
76 match &result {
77 Ok(connection) => {
78 if let Some(connection) = connection.clone().downcast::<AcpConnection>()
79 && !connection.prompt_capabilities().image
80 {
81 let version_output = util::command::new_smol_command(&command.path)
82 .args(command.args.iter())
83 .arg("--version")
84 .kill_on_drop(true)
85 .output()
86 .await;
87 let current_version =
88 String::from_utf8(version_output?.stdout)?.trim().to_owned();
89 if !connection.prompt_capabilities().image {
90 return Err(LoadError::Unsupported {
91 current_version: current_version.into(),
92 command: command.path.to_string_lossy().to_string().into(),
93 minimum_version: Self::MINIMUM_VERSION.into(),
94 }
95 .into());
96 }
97 }
98 }
99 Err(_) => {
100 let version_fut = util::command::new_smol_command(&command.path)
101 .args(command.args.iter())
102 .arg("--version")
103 .kill_on_drop(true)
104 .output();
105
106 let help_fut = util::command::new_smol_command(&command.path)
107 .args(command.args.iter())
108 .arg("--help")
109 .kill_on_drop(true)
110 .output();
111
112 let (version_output, help_output) =
113 futures::future::join(version_fut, help_fut).await;
114
115 let current_version = std::str::from_utf8(&version_output?.stdout)?
116 .trim()
117 .to_string();
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 minimum_version: Self::MINIMUM_VERSION.into(),
125 }
126 .into());
127 }
128 }
129 }
130 result
131 })
132 }
133
134 fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
135 self
136 }
137}
138
139impl Gemini {
140 const PACKAGE_NAME: &str = "@google/gemini-cli";
141
142 const MINIMUM_VERSION: &str = "0.2.1";
143
144 const BINARY_NAME: &str = "gemini";
145}
146
147#[cfg(test)]
148pub(crate) mod tests {
149 use super::*;
150 use crate::AgentServerCommand;
151 use std::path::Path;
152
153 crate::common_e2e_tests!(async |_, _, _| Gemini, allow_option_id = "proceed_once");
154
155 pub fn local_command() -> AgentServerCommand {
156 let cli_path = Path::new(env!("CARGO_MANIFEST_DIR"))
157 .join("../../../gemini-cli/packages/cli")
158 .to_string_lossy()
159 .to_string();
160
161 AgentServerCommand {
162 path: "node".into(),
163 args: vec![cli_path],
164 env: None,
165 }
166 }
167}