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 fs = delegate.project().read(cx).fs().clone();
40 let server_name = self.name();
41 let settings = cx.read_global(|settings: &SettingsStore, _| {
42 settings.get::<AllAgentServersSettings>(None).gemini.clone()
43 });
44 let project = delegate.project().clone();
45
46 cx.spawn(async move |cx| {
47 let ignore_system_version = settings
48 .as_ref()
49 .and_then(|settings| settings.ignore_system_version)
50 .unwrap_or(true);
51 let mut project_env = project
52 .update(cx, |project, cx| {
53 project.directory_environment(root_dir.as_path().into(), cx)
54 })?
55 .await
56 .unwrap_or_default();
57 let mut command = if let Some(settings) = settings
58 && let Some(command) = settings.custom_command()
59 {
60 command
61 } else {
62 cx.update(|cx| {
63 delegate.get_or_npm_install_builtin_agent(
64 Self::BINARY_NAME.into(),
65 Self::PACKAGE_NAME.into(),
66 format!("node_modules/{}/dist/index.js", Self::PACKAGE_NAME).into(),
67 ignore_system_version,
68 Some(Self::MINIMUM_VERSION.parse().unwrap()),
69 cx,
70 )
71 })?
72 .await?
73 };
74 if !command.args.contains(&ACP_ARG.into()) {
75 command.args.push(ACP_ARG.into());
76 }
77 if let Some(api_key) = cx.update(GoogleLanguageModelProvider::api_key)?.await.ok() {
78 project_env
79 .insert("GEMINI_API_KEY".to_owned(), api_key.key);
80 }
81 project_env.extend(command.env.take().unwrap_or_default());
82 command.env = Some(project_env);
83
84 let root_dir_exists = fs.is_dir(&root_dir).await;
85 anyhow::ensure!(
86 root_dir_exists,
87 "Session root {} does not exist or is not a directory",
88 root_dir.to_string_lossy()
89 );
90
91 let result = crate::acp::connect(server_name, command.clone(), &root_dir, cx).await;
92 match &result {
93 Ok(connection) => {
94 if let Some(connection) = connection.clone().downcast::<AcpConnection>()
95 && !connection.prompt_capabilities().image
96 {
97 let version_output = util::command::new_smol_command(&command.path)
98 .args(command.args.iter())
99 .arg("--version")
100 .kill_on_drop(true)
101 .output()
102 .await;
103 let current_version =
104 String::from_utf8(version_output?.stdout)?.trim().to_owned();
105
106 log::error!("connected to gemini, but missing prompt_capabilities.image (version is {current_version})");
107 return Err(LoadError::Unsupported {
108 current_version: current_version.into(),
109 command: (command.path.to_string_lossy().to_string() + " " + &command.args.join(" ")).into(),
110 minimum_version: Self::MINIMUM_VERSION.into(),
111 }
112 .into());
113 }
114 }
115 Err(e) => {
116 let version_fut = util::command::new_smol_command(&command.path)
117 .args(command.args.iter())
118 .arg("--version")
119 .kill_on_drop(true)
120 .output();
121
122 let help_fut = util::command::new_smol_command(&command.path)
123 .args(command.args.iter())
124 .arg("--help")
125 .kill_on_drop(true)
126 .output();
127
128 let (version_output, help_output) =
129 futures::future::join(version_fut, help_fut).await;
130 let Some(version_output) = version_output.ok().and_then(|output| String::from_utf8(output.stdout).ok()) else {
131 return result;
132 };
133 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 {
134 return result;
135 };
136
137 let current_version = version_output.trim().to_string();
138 let supported = help_stdout.contains(ACP_ARG) || current_version.parse::<semver::Version>().is_ok_and(|version| version >= Self::MINIMUM_VERSION.parse::<semver::Version>().unwrap());
139
140 log::error!("failed to create ACP connection to gemini (version is {current_version}, supported: {supported}): {e}");
141 log::debug!("gemini --help stdout: {help_stdout:?}");
142 log::debug!("gemini --help stderr: {help_stderr:?}");
143 if !supported {
144 return Err(LoadError::Unsupported {
145 current_version: current_version.into(),
146 command: (command.path.to_string_lossy().to_string() + " " + &command.args.join(" ")).into(),
147 minimum_version: Self::MINIMUM_VERSION.into(),
148 }
149 .into());
150 }
151 }
152 }
153 result
154 })
155 }
156
157 fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
158 self
159 }
160}
161
162impl Gemini {
163 const PACKAGE_NAME: &str = "@google/gemini-cli";
164
165 const MINIMUM_VERSION: &str = "0.2.1";
166
167 const BINARY_NAME: &str = "gemini";
168}
169
170#[cfg(test)]
171pub(crate) mod tests {
172 use super::*;
173 use crate::AgentServerCommand;
174 use std::path::Path;
175
176 crate::common_e2e_tests!(async |_, _, _| Gemini, allow_option_id = "proceed_once");
177
178 pub fn local_command() -> AgentServerCommand {
179 let cli_path = Path::new(env!("CARGO_MANIFEST_DIR"))
180 .join("../../../gemini-cli/packages/cli")
181 .to_string_lossy()
182 .to_string();
183
184 AgentServerCommand {
185 path: "node".into(),
186 args: vec![cli_path],
187 env: None,
188 }
189 }
190}