1use std::rc::Rc;
2use std::{any::Any, path::Path};
3
4use crate::{AgentServer, AgentServerCommand};
5use acp_thread::{AgentConnection, LoadError};
6use anyhow::Result;
7use gpui::{Entity, Task};
8use language_models::provider::google::GoogleLanguageModelProvider;
9use project::Project;
10use settings::SettingsStore;
11use ui::App;
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 name(&self) -> &'static str {
22 "Gemini CLI"
23 }
24
25 fn empty_state_headline(&self) -> &'static str {
26 self.name()
27 }
28
29 fn empty_state_message(&self) -> &'static str {
30 "Ask questions, edit files, run commands"
31 }
32
33 fn logo(&self) -> ui::IconName {
34 ui::IconName::AiGemini
35 }
36
37 fn connect(
38 &self,
39 root_dir: &Path,
40 project: &Entity<Project>,
41 cx: &mut App,
42 ) -> Task<Result<Rc<dyn AgentConnection>>> {
43 let project = project.clone();
44 let root_dir = root_dir.to_path_buf();
45 let server_name = self.name();
46 cx.spawn(async move |cx| {
47 let settings = cx.read_global(|settings: &SettingsStore, _| {
48 settings.get::<AllAgentServersSettings>(None).gemini.clone()
49 })?;
50
51 let Some(mut command) =
52 AgentServerCommand::resolve("gemini", &[ACP_ARG], None, settings, &project, cx).await
53 else {
54 return Err(LoadError::NotInstalled {
55 error_message: "Failed to find Gemini CLI binary".into(),
56 install_message: "Install Gemini CLI".into(),
57 install_command: "npm install -g @google/gemini-cli@preview".into()
58 }.into());
59 };
60
61 if let Some(api_key)= cx.update(GoogleLanguageModelProvider::api_key)?.await.ok() {
62 command.env.get_or_insert_default().insert("GEMINI_API_KEY".to_owned(), api_key.key);
63 }
64
65 let result = crate::acp::connect(server_name, command.clone(), &root_dir, cx).await;
66 if result.is_err() {
67 let version_fut = util::command::new_smol_command(&command.path)
68 .args(command.args.iter())
69 .arg("--version")
70 .kill_on_drop(true)
71 .output();
72
73 let help_fut = util::command::new_smol_command(&command.path)
74 .args(command.args.iter())
75 .arg("--help")
76 .kill_on_drop(true)
77 .output();
78
79 let (version_output, help_output) = futures::future::join(version_fut, help_fut).await;
80
81 let current_version = String::from_utf8(version_output?.stdout)?;
82 let supported = String::from_utf8(help_output?.stdout)?.contains(ACP_ARG);
83
84 if !supported {
85 return Err(LoadError::Unsupported {
86 error_message: format!(
87 "Your installed version of Gemini CLI ({}, version {}) doesn't support the Agentic Coding Protocol (ACP).",
88 command.path.to_string_lossy(),
89 current_version
90 ).into(),
91 upgrade_message: "Upgrade Gemini CLI to latest".into(),
92 upgrade_command: "npm install -g @google/gemini-cli@preview".into(),
93 }.into())
94 }
95 }
96 result
97 })
98 }
99
100 fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
101 self
102 }
103}
104
105#[cfg(test)]
106pub(crate) mod tests {
107 use super::*;
108 use crate::AgentServerCommand;
109 use std::path::Path;
110
111 crate::common_e2e_tests!(async |_, _, _| Gemini, allow_option_id = "proceed_once");
112
113 pub fn local_command() -> AgentServerCommand {
114 let cli_path = Path::new(env!("CARGO_MANIFEST_DIR"))
115 .join("../../../gemini-cli/packages/cli")
116 .to_string_lossy()
117 .to_string();
118
119 AgentServerCommand {
120 path: "node".into(),
121 args: vec![cli_path],
122 env: None,
123 }
124 }
125}