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;
11use project::project_settings::ProjectSettings;
12use release_channel::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 ZetaCliAppState {
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
28// TODO: dedupe with crates/eval/src/eval.rs
29pub fn init(cx: &mut App) -> ZetaCliAppState {
30 let app_version = AppVersion::load(env!("ZED_PKG_VERSION"));
31 release_channel::init(app_version, cx);
32 gpui_tokio::init(cx);
33
34 let mut settings_store = SettingsStore::new(cx);
35 settings_store
36 .set_default_settings(settings::default_settings().as_ref(), cx)
37 .unwrap();
38 cx.set_global(settings_store);
39 client::init_settings(cx);
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 Project::init_settings(cx);
62
63 let client = Client::production(cx);
64 cx.set_http_client(client.http_client());
65
66 let git_binary_path = None;
67 let fs = Arc::new(RealFs::new(
68 git_binary_path,
69 cx.background_executor().clone(),
70 ));
71
72 let mut languages = LanguageRegistry::new(cx.background_executor().clone());
73 languages.set_language_server_download_dir(paths::languages_dir().clone());
74 let languages = Arc::new(languages);
75
76 let user_store = cx.new(|cx| UserStore::new(client.clone(), cx));
77
78 extension::init(cx);
79
80 let (mut tx, rx) = watch::channel(None);
81 cx.observe_global::<SettingsStore>(move |cx| {
82 let settings = &ProjectSettings::get_global(cx).node;
83 let options = NodeBinaryOptions {
84 allow_path_lookup: !settings.ignore_system_version,
85 allow_binary_download: true,
86 use_paths: settings.path.as_ref().map(|node_path| {
87 let node_path = PathBuf::from(shellexpand::tilde(node_path).as_ref());
88 let npm_path = settings
89 .npm_path
90 .as_ref()
91 .map(|path| PathBuf::from(shellexpand::tilde(&path).as_ref()));
92 (
93 node_path.clone(),
94 npm_path.unwrap_or_else(|| {
95 let base_path = PathBuf::new();
96 node_path.parent().unwrap_or(&base_path).join("npm")
97 }),
98 )
99 }),
100 };
101 tx.send(Some(options)).log_err();
102 })
103 .detach();
104 let node_runtime = NodeRuntime::new(client.http_client(), None, rx);
105
106 let extension_host_proxy = ExtensionHostProxy::global(cx);
107
108 language::init(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(client.clone(), cx);
112 language_models::init(user_store.clone(), client.clone(), cx);
113 languages::init(languages.clone(), node_runtime.clone(), cx);
114 prompt_store::init(cx);
115 terminal_view::init(cx);
116
117 ZetaCliAppState {
118 languages,
119 client,
120 user_store,
121 fs,
122 node_runtime,
123 }
124}