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