agent_servers: Fix proxy configuration for Gemini (#37790)

Patsakula Nikita created

Closes #37487 

Proxy settings are now taken from the Zed configuration and passed to
Gemini via the "--proxy" flag.

Release Notes:

- acp: Gemini ACP server now uses proxy settings from Zed configuration.

Change summary

crates/agent_servers/Cargo.toml    |  2 +-
crates/agent_servers/src/gemini.rs | 18 ++++++++++++++++--
2 files changed, 17 insertions(+), 3 deletions(-)

Detailed changes

crates/agent_servers/Cargo.toml 🔗

@@ -23,7 +23,7 @@ action_log.workspace = true
 agent-client-protocol.workspace = true
 agent_settings.workspace = true
 anyhow.workspace = true
-client = { workspace = true, optional = true }
+client.workspace = true
 collections.workspace = true
 env_logger = { workspace = true, optional = true }
 fs.workspace = true

crates/agent_servers/src/gemini.rs 🔗

@@ -4,10 +4,12 @@ use std::{any::Any, path::Path};
 use crate::{AgentServer, AgentServerDelegate};
 use acp_thread::AgentConnection;
 use anyhow::{Context as _, Result};
+use client::ProxySettings;
 use collections::HashMap;
-use gpui::{App, SharedString, Task};
+use gpui::{App, AppContext, SharedString, Task};
 use language_models::provider::google::GoogleLanguageModelProvider;
 use project::agent_server_store::GEMINI_NAME;
+use settings::SettingsStore;
 
 #[derive(Clone)]
 pub struct Gemini;
@@ -35,13 +37,16 @@ impl AgentServer for Gemini {
         let root_dir = root_dir.map(|root_dir| root_dir.to_string_lossy().to_string());
         let is_remote = delegate.project.read(cx).is_via_remote_server();
         let store = delegate.store.downgrade();
+        let proxy_url = cx.read_global(|settings: &SettingsStore, _| {
+            settings.get::<ProxySettings>(None).proxy.clone()
+        });
 
         cx.spawn(async move |cx| {
             let mut extra_env = HashMap::default();
             if let Some(api_key) = cx.update(GoogleLanguageModelProvider::api_key)?.await.ok() {
                 extra_env.insert("GEMINI_API_KEY".into(), api_key.key);
             }
-            let (command, root_dir, login) = store
+            let (mut command, root_dir, login) = store
                 .update(cx, |store, cx| {
                     let agent = store
                         .get_external_agent(&GEMINI_NAME.into())
@@ -55,6 +60,15 @@ impl AgentServer for Gemini {
                     ))
                 })??
                 .await?;
+
+            // Add proxy flag if proxy settings are configured in Zed and not in the args
+            if let Some(proxy_url_value) = &proxy_url
+                && !command.args.iter().any(|arg| arg.contains("--proxy"))
+            {
+                command.args.push("--proxy".into());
+                command.args.push(proxy_url_value.clone());
+            }
+
             let connection =
                 crate::acp::connect(name, command, root_dir.as_ref(), is_remote, cx).await?;
             Ok((connection, login))