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 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
89#[cfg(test)]
90pub(crate) mod tests {
91 use project::agent_server_store::AgentServerCommand;
92
93 use super::*;
94 use std::path::Path;
95
96 crate::common_e2e_tests!(async |_, _, _| Gemini, allow_option_id = "proceed_once");
97
98 pub fn local_command() -> AgentServerCommand {
99 let cli_path = Path::new(env!("CARGO_MANIFEST_DIR"))
100 .join("../../../gemini-cli/packages/cli")
101 .to_string_lossy()
102 .to_string();
103
104 AgentServerCommand {
105 path: "node".into(),
106 args: vec![cli_path],
107 env: None,
108 }
109 }
110}