1use std::rc::Rc;
2use std::{any::Any, path::Path};
3
4use crate::acp::AcpConnection;
5use crate::{AgentServer, AgentServerDelegate, AllAgentServersSettings};
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
12#[derive(Clone)]
13pub struct Gemini;
14
15const ACP_ARG: &str = "--experimental-acp";
16
17impl AgentServer for Gemini {
18 fn telemetry_id(&self) -> &'static str {
19 "gemini-cli"
20 }
21
22 fn name(&self) -> SharedString {
23 "Gemini CLI".into()
24 }
25
26 fn logo(&self) -> ui::IconName {
27 ui::IconName::AiGemini
28 }
29
30 fn connect(
31 &self,
32 root_dir: &Path,
33 delegate: AgentServerDelegate,
34 cx: &mut App,
35 ) -> Task<Result<Rc<dyn AgentConnection>>> {
36 let root_dir = root_dir.to_path_buf();
37 let fs = delegate.project().read(cx).fs().clone();
38 let server_name = self.name();
39 let (custom_command, is_system) = cx.read_global(|settings: &SettingsStore, _| {
40 let s = settings.get::<AllAgentServersSettings>(None);
41 (
42 s.get("gemini").cloned(),
43 AllAgentServersSettings::is_system(s, "gemini"),
44 )
45 });
46
47 cx.spawn(async move |cx| {
48 let mut command = if let Some(custom_command) = custom_command
49 {
50 custom_command
51 } else {
52 cx.update(|cx| {
53 delegate.get_or_npm_install_builtin_agent(
54 Self::BINARY_NAME.into(),
55 Self::PACKAGE_NAME.into(),
56 format!("node_modules/{}/dist/index.js", Self::PACKAGE_NAME).into(),
57 is_system,
58 Some(Self::MINIMUM_VERSION.parse().unwrap()),
59 cx,
60 )
61 })?
62 .await?
63 };
64 if !command.args.contains(&ACP_ARG.into()) {
65 command.args.push(ACP_ARG.into());
66 }
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 root_dir_exists = fs.is_dir(&root_dir).await;
76 anyhow::ensure!(
77 root_dir_exists,
78 "Session root {} does not exist or is not a directory",
79 root_dir.to_string_lossy()
80 );
81
82 let result = crate::acp::connect(server_name, command.clone(), &root_dir, cx).await;
83 match &result {
84 Ok(connection) => {
85 if let Some(connection) = connection.clone().downcast::<AcpConnection>()
86 && !connection.prompt_capabilities().image
87 {
88 let version_output = util::command::new_smol_command(&command.path)
89 .args(command.args.iter())
90 .arg("--version")
91 .kill_on_drop(true)
92 .output()
93 .await;
94 let current_version =
95 String::from_utf8(version_output?.stdout)?.trim().to_owned();
96
97 log::error!("connected to gemini, but missing prompt_capabilities.image (version is {current_version})");
98 return Err(LoadError::Unsupported {
99 current_version: current_version.into(),
100 command: (command.path.to_string_lossy().to_string() + " " + &command.args.join(" ")).into(),
101 minimum_version: Self::MINIMUM_VERSION.into(),
102 }
103 .into());
104 }
105 }
106 Err(e) => {
107 let version_fut = util::command::new_smol_command(&command.path)
108 .args(command.args.iter())
109 .arg("--version")
110 .kill_on_drop(true)
111 .output();
112
113 let help_fut = util::command::new_smol_command(&command.path)
114 .args(command.args.iter())
115 .arg("--help")
116 .kill_on_drop(true)
117 .output();
118
119 let (version_output, help_output) =
120 futures::future::join(version_fut, help_fut).await;
121 let Some(version_output) = version_output.ok().and_then(|output| String::from_utf8(output.stdout).ok()) else {
122 return result;
123 };
124 let Some((help_stdout, help_stderr)) = help_output.ok().and_then(|output| String::from_utf8(output.stdout).ok().zip(String::from_utf8(output.stderr).ok())) else {
125 return result;
126 };
127
128 let current_version = version_output.trim().to_string();
129 let supported = help_stdout.contains(ACP_ARG) || current_version.parse::<semver::Version>().is_ok_and(|version| version >= Self::MINIMUM_VERSION.parse::<semver::Version>().unwrap());
130
131 log::error!("failed to create ACP connection to gemini (version is {current_version}, supported: {supported}): {e}");
132 log::debug!("gemini --help stdout: {help_stdout:?}");
133 log::debug!("gemini --help stderr: {help_stderr:?}");
134 if !supported {
135 return Err(LoadError::Unsupported {
136 current_version: current_version.into(),
137 command: (command.path.to_string_lossy().to_string() + " " + &command.args.join(" ")).into(),
138 minimum_version: Self::MINIMUM_VERSION.into(),
139 }
140 .into());
141 }
142 }
143 }
144 result
145 })
146 }
147
148 fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
149 self
150 }
151}
152
153impl Gemini {
154 const PACKAGE_NAME: &str = "@google/gemini-cli";
155
156 const MINIMUM_VERSION: &str = "0.2.1";
157
158 const BINARY_NAME: &str = "gemini";
159}
160
161#[cfg(test)]
162pub(crate) mod tests {
163 use super::*;
164 use crate::AgentServerCommand;
165 use std::path::Path;
166
167 crate::common_e2e_tests!(async |_, _, _| Gemini, allow_option_id = "proceed_once");
168
169 pub fn local_command() -> AgentServerCommand {
170 let cli_path = Path::new(env!("CARGO_MANIFEST_DIR"))
171 .join("../../../gemini-cli/packages/cli")
172 .to_string_lossy()
173 .to_string();
174
175 AgentServerCommand {
176 path: "node".into(),
177 args: vec![cli_path],
178 env: None,
179 }
180 }
181}