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