headless.rs

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