1use std::rc::Rc;
2use std::{any::Any, path::Path};
3
4use crate::{AcpAgentServer, AgentServer, AgentServerDelegate, load_proxy_env};
5use acp_thread::AgentConnection;
6use anyhow::{Context as _, Result};
7use collections::HashMap;
8use gpui::{App, SharedString, Task};
9use language_models::provider::google::GoogleLanguageModelProvider;
10use project::agent_server_store::GEMINI_NAME;
11
12#[derive(Clone)]
13pub struct Gemini;
14
15impl AgentServer for Gemini {
16 fn telemetry_id(&self) -> &'static str {
17 "gemini-cli"
18 }
19
20 fn name(&self) -> SharedString {
21 "Gemini CLI".into()
22 }
23
24 fn logo(&self) -> ui::IconName {
25 ui::IconName::AiGemini
26 }
27
28 fn connect(
29 &self,
30 root_dir: Option<&Path>,
31 delegate: AgentServerDelegate,
32 cx: &mut App,
33 ) -> Task<
34 Result<(
35 Rc<dyn AgentConnection>,
36 HashMap<String, task::SpawnInTerminal>,
37 )>,
38 > {
39 let name = self.name();
40 let root_dir = root_dir.map(|root_dir| root_dir.to_string_lossy().into_owned());
41 let is_remote = delegate.project.read(cx).is_via_remote_server();
42 let store = delegate.store.downgrade();
43 let mut extra_env = load_proxy_env(cx);
44 let default_mode = self.default_mode(cx);
45
46 cx.spawn(async move |cx| {
47 extra_env.insert("SURFACE".to_owned(), "zed".to_owned());
48
49 if let Some(api_key) = cx
50 .update(GoogleLanguageModelProvider::api_key_for_gemini_cli)?
51 .await
52 .ok()
53 {
54 extra_env.insert("GEMINI_API_KEY".into(), api_key);
55 }
56 let (command, root_dir, auth_commands) = store
57 .update(cx, |store, cx| {
58 let agent = store
59 .get_external_agent(&GEMINI_NAME.into())
60 .context("Gemini CLI is not registered")?;
61 anyhow::Ok(agent.get_command(
62 root_dir.as_deref(),
63 extra_env,
64 delegate.status_tx,
65 delegate.new_version_available,
66 &mut cx.to_async(),
67 ))
68 })??
69 .await?;
70
71 let connection = crate::acp::connect(
72 name,
73 command,
74 root_dir.as_ref(),
75 default_mode,
76 is_remote,
77 cx,
78 )
79 .await?;
80 Ok((connection, auth_commands))
81 })
82 }
83
84 fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
85 self
86 }
87}
88
89impl AcpAgentServer for Gemini {
90 fn local_login_commands(&self) -> Vec<String> {
91 vec!["login".to_string()]
92 }
93
94 fn remote_login_commands(&self) -> Vec<String> {
95 // When remote, OAuth doesn't work, so login is handled via the
96 // auth_commands mapping (oauth-personal -> spawn-gemini-cli)
97 vec![]
98 }
99
100 fn local_logout_commands(&self) -> Vec<String> {
101 vec![]
102 }
103
104 fn remote_logout_commands(&self) -> Vec<String> {
105 vec![]
106 }
107}
108
109#[cfg(test)]
110pub(crate) mod tests {
111 use project::agent_server_store::AgentServerCommand;
112
113 use super::*;
114 use std::path::Path;
115
116 crate::common_e2e_tests!(async |_, _, _| Gemini, allow_option_id = "proceed_once");
117
118 pub fn local_command() -> AgentServerCommand {
119 let cli_path = Path::new(env!("CARGO_MANIFEST_DIR"))
120 .join("../../../gemini-cli/packages/cli")
121 .to_string_lossy()
122 .to_string();
123
124 AgentServerCommand {
125 path: "node".into(),
126 args: vec![cli_path],
127 env: None,
128 }
129 }
130}