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 if let Some(api_key) = cx.update(GoogleLanguageModelProvider::api_key)?.await.ok() {
43 extra_env.insert("GEMINI_API_KEY".into(), api_key.key);
44 }
45 let (command, root_dir, login) = store
46 .update(cx, |store, cx| {
47 let agent = store
48 .get_external_agent(&GEMINI_NAME.into())
49 .context("Gemini CLI is not registered")?;
50 anyhow::Ok(agent.get_command(
51 root_dir.as_deref(),
52 extra_env,
53 delegate.status_tx,
54 delegate.new_version_available,
55 &mut cx.to_async(),
56 ))
57 })??
58 .await?;
59
60 let connection = crate::acp::connect(
61 name,
62 command,
63 root_dir.as_ref(),
64 default_mode,
65 is_remote,
66 cx,
67 )
68 .await?;
69 Ok((connection, login))
70 })
71 }
72
73 fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
74 self
75 }
76}
77
78#[cfg(test)]
79pub(crate) mod tests {
80 use project::agent_server_store::AgentServerCommand;
81
82 use super::*;
83 use std::path::Path;
84
85 crate::common_e2e_tests!(async |_, _, _| Gemini, allow_option_id = "proceed_once");
86
87 pub fn local_command() -> AgentServerCommand {
88 let cli_path = Path::new(env!("CARGO_MANIFEST_DIR"))
89 .join("../../../gemini-cli/packages/cli")
90 .to_string_lossy()
91 .to_string();
92
93 AgentServerCommand {
94 path: "node".into(),
95 args: vec![cli_path],
96 env: None,
97 }
98 }
99}