1use client::{Client, ProxySettings, UserStore};
2use extension::ExtensionHostProxy;
3use fs::RealFs;
4use gpui::http_client::read_proxy_from_env;
5use gpui::{App, AppContext, Entity};
6use gpui_tokio::Tokio;
7use language::LanguageRegistry;
8use language_extension::LspAccess;
9use node_runtime::{NodeBinaryOptions, NodeRuntime};
10use project::project_settings::ProjectSettings;
11use release_channel::{AppCommitSha, AppVersion};
12use reqwest_client::ReqwestClient;
13use settings::{Settings, SettingsStore};
14use std::path::PathBuf;
15use std::sync::Arc;
16use util::ResultExt as _;
17
18/// Headless subset of `workspace::AppState`.
19pub struct EpAppState {
20 pub languages: Arc<LanguageRegistry>,
21 pub client: Arc<Client>,
22 pub user_store: Entity<UserStore>,
23 pub fs: Arc<dyn fs::Fs>,
24 pub node_runtime: NodeRuntime,
25}
26
27pub fn init(cx: &mut App) -> EpAppState {
28 let app_commit_sha = option_env!("ZED_COMMIT_SHA").map(|s| AppCommitSha::new(s.to_owned()));
29
30 let app_version = AppVersion::load(
31 env!("ZED_PKG_VERSION"),
32 option_env!("ZED_BUILD_ID"),
33 app_commit_sha,
34 );
35 release_channel::init(app_version.clone(), cx);
36 gpui_tokio::init(cx);
37
38 let settings_store = SettingsStore::new(cx, &settings::default_settings());
39 cx.set_global(settings_store);
40
41 // Set User-Agent so we can download language servers from GitHub
42 let user_agent = format!(
43 "Zeta CLI/{} ({}; {})",
44 app_version,
45 std::env::consts::OS,
46 std::env::consts::ARCH
47 );
48 let proxy_str = ProxySettings::get_global(cx).proxy.to_owned();
49 let proxy_url = proxy_str
50 .as_ref()
51 .and_then(|input| input.parse().ok())
52 .or_else(read_proxy_from_env);
53 let http = {
54 let _guard = Tokio::handle(cx).enter();
55
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 tx, 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 tx.send(Some(options)).log_err();
100 })
101 .detach();
102 let node_runtime = NodeRuntime::new(client.http_client(), None, rx);
103
104 let extension_host_proxy = ExtensionHostProxy::global(cx);
105
106 debug_adapter_extension::init(extension_host_proxy.clone(), cx);
107 language_extension::init(LspAccess::Noop, extension_host_proxy, languages.clone());
108 language_model::init(client.clone(), cx);
109 language_models::init(user_store.clone(), client.clone(), cx);
110 languages::init(languages.clone(), fs.clone(), node_runtime.clone(), cx);
111 prompt_store::init(cx);
112 terminal_view::init(cx);
113
114 EpAppState {
115 languages,
116 client,
117 user_store,
118 fs,
119 node_runtime,
120 }
121}