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;
 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, path::Path, 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 default_mode(&self, _cx: &mut App) -> Option<agent_client_protocol::SessionModeId> {
 60        None
 61    }
 62    fn set_default_mode(
 63        &self,
 64        _mode_id: Option<agent_client_protocol::SessionModeId>,
 65        _fs: Arc<dyn Fs>,
 66        _cx: &mut App,
 67    ) {
 68    }
 69
 70    fn default_model(&self, _cx: &mut App) -> Option<agent_client_protocol::ModelId> {
 71        None
 72    }
 73
 74    fn set_default_model(
 75        &self,
 76        _model_id: Option<agent_client_protocol::ModelId>,
 77        _fs: Arc<dyn Fs>,
 78        _cx: &mut App,
 79    ) {
 80    }
 81
 82    fn connect(
 83        &self,
 84        root_dir: Option<&Path>,
 85        delegate: AgentServerDelegate,
 86        cx: &mut App,
 87    ) -> Task<Result<(Rc<dyn AgentConnection>, Option<task::SpawnInTerminal>)>>;
 88
 89    fn into_any(self: Rc<Self>) -> Rc<dyn Any>;
 90}
 91
 92impl dyn AgentServer {
 93    pub fn downcast<T: 'static + AgentServer + Sized>(self: Rc<Self>) -> Option<Rc<T>> {
 94        self.into_any().downcast().ok()
 95    }
 96}
 97
 98/// Load the default proxy environment variables to pass through to the agent
 99pub fn load_proxy_env(cx: &mut App) -> HashMap<String, String> {
100    let proxy_url = cx
101        .read_global(|settings: &SettingsStore, _| settings.get::<ProxySettings>(None).proxy_url());
102    let mut env = HashMap::default();
103
104    if let Some(proxy_url) = &proxy_url {
105        let env_var = if proxy_url.scheme() == "https" {
106            "HTTPS_PROXY"
107        } else {
108            "HTTP_PROXY"
109        };
110        env.insert(env_var.to_owned(), proxy_url.to_string());
111    }
112
113    if let Some(no_proxy) = read_no_proxy_from_env() {
114        env.insert("NO_PROXY".to_owned(), no_proxy);
115    } else if proxy_url.is_some() {
116        // We sometimes need local MCP servers that we don't want to proxy
117        env.insert("NO_PROXY".to_owned(), "localhost,127.0.0.1".to_owned());
118    }
119
120    env
121}