agent_servers.rs

  1mod acp;
  2mod claude;
  3mod codex;
  4mod custom;
  5mod gemini;
  6
  7#[cfg(any(test, feature = "test-support"))]
  8pub mod e2e_tests;
  9
 10pub use claude::*;
 11use client::ProxySettings;
 12pub use codex::*;
 13use collections::{HashMap, HashSet};
 14pub use custom::*;
 15use fs::Fs;
 16pub use gemini::*;
 17use http_client::read_no_proxy_from_env;
 18use project::agent_server_store::AgentServerStore;
 19
 20use acp_thread::AgentConnection;
 21use anyhow::Result;
 22use gpui::{App, AppContext, Entity, SharedString, Task};
 23use project::Project;
 24use settings::SettingsStore;
 25use std::{any::Any, rc::Rc, sync::Arc};
 26
 27pub use acp::AcpConnection;
 28
 29pub struct AgentServerDelegate {
 30    store: Entity<AgentServerStore>,
 31    project: Entity<Project>,
 32    status_tx: Option<watch::Sender<SharedString>>,
 33    new_version_available: Option<watch::Sender<Option<String>>>,
 34}
 35
 36impl AgentServerDelegate {
 37    pub fn new(
 38        store: Entity<AgentServerStore>,
 39        project: Entity<Project>,
 40        status_tx: Option<watch::Sender<SharedString>>,
 41        new_version_tx: Option<watch::Sender<Option<String>>>,
 42    ) -> Self {
 43        Self {
 44            store,
 45            project,
 46            status_tx,
 47            new_version_available: new_version_tx,
 48        }
 49    }
 50
 51    pub fn project(&self) -> &Entity<Project> {
 52        &self.project
 53    }
 54}
 55
 56pub trait AgentServer: Send {
 57    fn logo(&self) -> ui::IconName;
 58    fn name(&self) -> SharedString;
 59    fn connect(
 60        &self,
 61        delegate: AgentServerDelegate,
 62        cx: &mut App,
 63    ) -> Task<Result<(Rc<dyn AgentConnection>, Option<task::SpawnInTerminal>)>>;
 64
 65    fn into_any(self: Rc<Self>) -> Rc<dyn Any>;
 66
 67    fn default_mode(&self, _cx: &App) -> Option<agent_client_protocol::SessionModeId> {
 68        None
 69    }
 70
 71    fn set_default_mode(
 72        &self,
 73        _mode_id: Option<agent_client_protocol::SessionModeId>,
 74        _fs: Arc<dyn Fs>,
 75        _cx: &mut App,
 76    ) {
 77    }
 78
 79    fn default_model(&self, _cx: &App) -> Option<agent_client_protocol::ModelId> {
 80        None
 81    }
 82
 83    fn set_default_model(
 84        &self,
 85        _model_id: Option<agent_client_protocol::ModelId>,
 86        _fs: Arc<dyn Fs>,
 87        _cx: &mut App,
 88    ) {
 89    }
 90
 91    fn favorite_model_ids(&self, _cx: &mut App) -> HashSet<agent_client_protocol::ModelId> {
 92        HashSet::default()
 93    }
 94
 95    fn default_config_option(&self, _config_id: &str, _cx: &App) -> Option<String> {
 96        None
 97    }
 98
 99    fn set_default_config_option(
100        &self,
101        _config_id: &str,
102        _value_id: Option<&str>,
103        _fs: Arc<dyn Fs>,
104        _cx: &mut App,
105    ) {
106    }
107
108    fn favorite_config_option_value_ids(
109        &self,
110        _config_id: &agent_client_protocol::SessionConfigId,
111        _cx: &mut App,
112    ) -> HashSet<agent_client_protocol::SessionConfigValueId> {
113        HashSet::default()
114    }
115
116    fn toggle_favorite_config_option_value(
117        &self,
118        _config_id: agent_client_protocol::SessionConfigId,
119        _value_id: agent_client_protocol::SessionConfigValueId,
120        _should_be_favorite: bool,
121        _fs: Arc<dyn Fs>,
122        _cx: &App,
123    ) {
124    }
125
126    fn toggle_favorite_model(
127        &self,
128        _model_id: agent_client_protocol::ModelId,
129        _should_be_favorite: bool,
130        _fs: Arc<dyn Fs>,
131        _cx: &App,
132    ) {
133    }
134}
135
136impl dyn AgentServer {
137    pub fn downcast<T: 'static + AgentServer + Sized>(self: Rc<Self>) -> Option<Rc<T>> {
138        self.into_any().downcast().ok()
139    }
140}
141
142/// Load the default proxy environment variables to pass through to the agent
143pub fn load_proxy_env(cx: &mut App) -> HashMap<String, String> {
144    let proxy_url = cx
145        .read_global(|settings: &SettingsStore, _| settings.get::<ProxySettings>(None).proxy_url());
146    let mut env = HashMap::default();
147
148    if let Some(proxy_url) = &proxy_url {
149        let env_var = if proxy_url.scheme() == "https" {
150            "HTTPS_PROXY"
151        } else {
152            "HTTP_PROXY"
153        };
154        env.insert(env_var.to_owned(), proxy_url.to_string());
155    }
156
157    if let Some(no_proxy) = read_no_proxy_from_env() {
158        env.insert("NO_PROXY".to_owned(), no_proxy);
159    } else if proxy_url.is_some() {
160        // We sometimes need local MCP servers that we don't want to proxy
161        env.insert("NO_PROXY".to_owned(), "localhost,127.0.0.1".to_owned());
162    }
163
164    env
165}