headless.rs

  1use std::path::PathBuf;
  2use std::sync::Arc;
  3
  4use client::{Client, ProxySettings, UserStore};
  5use extension::ExtensionHostProxy;
  6use fs::RealFs;
  7use gpui::http_client::read_proxy_from_env;
  8use gpui::{App, AppContext as _, Entity};
  9use gpui_tokio::Tokio;
 10use language::LanguageRegistry;
 11use language_extension::LspAccess;
 12use node_runtime::{NodeBinaryOptions, NodeRuntime};
 13use project::project_settings::ProjectSettings;
 14use prompt_store::PromptBuilder;
 15use release_channel::{AppCommitSha, AppVersion};
 16use reqwest_client::ReqwestClient;
 17use settings::{Settings, SettingsStore};
 18use util::ResultExt as _;
 19
 20pub struct AgentCliAppState {
 21    pub languages: Arc<LanguageRegistry>,
 22    pub client: Arc<Client>,
 23    pub user_store: Entity<UserStore>,
 24    pub fs: Arc<dyn fs::Fs>,
 25    pub node_runtime: NodeRuntime,
 26}
 27
 28pub fn init(cx: &mut App) -> Arc<AgentCliAppState> {
 29    let app_commit_sha = option_env!("ZED_COMMIT_SHA").map(|s| AppCommitSha::new(s.to_owned()));
 30
 31    let app_version = AppVersion::load(
 32        env!("ZED_PKG_VERSION"),
 33        option_env!("ZED_BUILD_ID"),
 34        app_commit_sha,
 35    );
 36
 37    release_channel::init(app_version.clone(), cx);
 38    gpui_tokio::init(cx);
 39
 40    let settings_store = SettingsStore::new(cx, &settings::default_settings());
 41    cx.set_global(settings_store);
 42
 43    let user_agent = format!(
 44        "Zed Agent CLI/{} ({}; {})",
 45        app_version,
 46        std::env::consts::OS,
 47        std::env::consts::ARCH
 48    );
 49    let proxy_str = ProxySettings::get_global(cx).proxy.to_owned();
 50    let proxy_url = proxy_str
 51        .as_ref()
 52        .and_then(|input| input.parse().ok())
 53        .or_else(read_proxy_from_env);
 54    let http = {
 55        let _guard = Tokio::handle(cx).enter();
 56        ReqwestClient::proxy_and_user_agent(proxy_url, &user_agent)
 57            .expect("could not start HTTP client")
 58    };
 59    cx.set_http_client(Arc::new(http));
 60
 61    let client = Client::production(cx);
 62    cx.set_http_client(client.http_client());
 63
 64    let git_binary_path = None;
 65    let fs = Arc::new(RealFs::new(
 66        git_binary_path,
 67        cx.background_executor().clone(),
 68    ));
 69
 70    let mut languages = LanguageRegistry::new(cx.background_executor().clone());
 71    languages.set_language_server_download_dir(paths::languages_dir().clone());
 72    let languages = Arc::new(languages);
 73
 74    let user_store = cx.new(|cx| UserStore::new(client.clone(), cx));
 75
 76    extension::init(cx);
 77
 78    let (mut node_options_tx, node_options_rx) = watch::channel(None);
 79    cx.observe_global::<SettingsStore>(move |cx| {
 80        let settings = &ProjectSettings::get_global(cx).node;
 81        let options = NodeBinaryOptions {
 82            allow_path_lookup: !settings.ignore_system_version,
 83            allow_binary_download: true,
 84            use_paths: settings.path.as_ref().map(|node_path| {
 85                let node_path = PathBuf::from(shellexpand::tilde(node_path).as_ref());
 86                let npm_path = settings
 87                    .npm_path
 88                    .as_ref()
 89                    .map(|path| PathBuf::from(shellexpand::tilde(&path).as_ref()));
 90                (
 91                    node_path.clone(),
 92                    npm_path.unwrap_or_else(|| {
 93                        let base_path = PathBuf::new();
 94                        node_path.parent().unwrap_or(&base_path).join("npm")
 95                    }),
 96                )
 97            }),
 98        };
 99        node_options_tx.send(Some(options)).log_err();
100    })
101    .detach();
102    let node_runtime = NodeRuntime::new(client.http_client(), None, node_options_rx);
103
104    let extension_host_proxy = ExtensionHostProxy::global(cx);
105    debug_adapter_extension::init(extension_host_proxy.clone(), cx);
106    language_extension::init(LspAccess::Noop, extension_host_proxy, languages.clone());
107    language_model::init(user_store.clone(), client.clone(), cx);
108    language_models::init(user_store.clone(), client.clone(), cx);
109    languages::init(languages.clone(), fs.clone(), node_runtime.clone(), cx);
110    prompt_store::init(cx);
111    terminal_view::init(cx);
112
113    let stdout_is_a_pty = false;
114    let prompt_builder = PromptBuilder::load(fs.clone(), stdout_is_a_pty, cx);
115    agent_ui::init(
116        fs.clone(),
117        client.clone(),
118        prompt_builder,
119        languages.clone(),
120        true,
121        cx,
122    );
123
124    Arc::new(AgentCliAppState {
125        languages,
126        client,
127        user_store,
128        fs,
129        node_runtime,
130    })
131}