1use client::{Client, ProxySettings, RefreshLlmTokenListener, UserStore};
2use db::AppDatabase;
3use extension::ExtensionHostProxy;
4use fs::RealFs;
5use gpui::http_client::read_proxy_from_env;
6use gpui::{App, AppContext, Entity};
7use gpui_tokio::Tokio;
8use language::LanguageRegistry;
9use language_extension::LspAccess;
10use node_runtime::{NodeBinaryOptions, NodeRuntime};
11use project::project_settings::ProjectSettings;
12use release_channel::{AppCommitSha, AppVersion};
13use reqwest_client::ReqwestClient;
14use settings::{Settings, SettingsStore};
15use std::path::PathBuf;
16use std::sync::Arc;
17use util::ResultExt as _;
18
19/// Headless subset of `workspace::AppState`.
20pub struct EpAppState {
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) -> EpAppState {
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 release_channel::init(app_version.clone(), cx);
37 gpui_tokio::init(cx);
38
39 let settings_store = SettingsStore::new(cx, &settings::default_settings());
40 cx.set_global(settings_store);
41
42 // Set User-Agent so we can download language servers from GitHub
43 let user_agent = format!(
44 "Zeta 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
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 tx, 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 tx.send(Some(options)).log_err();
104 })
105 .detach();
106 let node_runtime = NodeRuntime::new(client.http_client(), None, rx);
107
108 let extension_host_proxy = ExtensionHostProxy::global(cx);
109
110 debug_adapter_extension::init(extension_host_proxy.clone(), cx);
111 language_extension::init(LspAccess::Noop, extension_host_proxy, languages.clone());
112 language_model::init(cx);
113 RefreshLlmTokenListener::register(client.clone(), user_store.clone(), cx);
114 language_models::init(user_store.clone(), client.clone(), cx);
115 languages::init(languages.clone(), fs.clone(), node_runtime.clone(), cx);
116 prompt_store::init(cx);
117 terminal_view::init(cx);
118
119 EpAppState {
120 languages,
121 client,
122 user_store,
123 fs,
124 node_runtime,
125 }
126}