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 settings_store = SettingsStore::new(cx, &settings::default_settings());
35 cx.set_global(settings_store);
36 SettingsStore::load_registered_settings(cx);
37
38 client::init_settings(cx);
39
40 // Set User-Agent so we can download language servers from GitHub
41 let user_agent = format!(
42 "Zeta CLI/{} ({}; {})",
43 app_version,
44 std::env::consts::OS,
45 std::env::consts::ARCH
46 );
47 let proxy_str = ProxySettings::get_global(cx).proxy.to_owned();
48 let proxy_url = proxy_str
49 .as_ref()
50 .and_then(|input| input.parse().ok())
51 .or_else(read_proxy_from_env);
52 let http = {
53 let _guard = Tokio::handle(cx).enter();
54
55 ReqwestClient::proxy_and_user_agent(proxy_url, &user_agent)
56 .expect("could not start HTTP client")
57 };
58 cx.set_http_client(Arc::new(http));
59
60 Project::init_settings(cx);
61
62 let client = Client::production(cx);
63 cx.set_http_client(client.http_client());
64
65 let git_binary_path = None;
66 let fs = Arc::new(RealFs::new(
67 git_binary_path,
68 cx.background_executor().clone(),
69 ));
70
71 let mut languages = LanguageRegistry::new(cx.background_executor().clone());
72 languages.set_language_server_download_dir(paths::languages_dir().clone());
73 let languages = Arc::new(languages);
74
75 let user_store = cx.new(|cx| UserStore::new(client.clone(), cx));
76
77 extension::init(cx);
78
79 let (mut tx, rx) = watch::channel(None);
80 cx.observe_global::<SettingsStore>(move |cx| {
81 let settings = &ProjectSettings::get_global(cx).node;
82 let options = NodeBinaryOptions {
83 allow_path_lookup: !settings.ignore_system_version,
84 allow_binary_download: true,
85 use_paths: settings.path.as_ref().map(|node_path| {
86 let node_path = PathBuf::from(shellexpand::tilde(node_path).as_ref());
87 let npm_path = settings
88 .npm_path
89 .as_ref()
90 .map(|path| PathBuf::from(shellexpand::tilde(&path).as_ref()));
91 (
92 node_path.clone(),
93 npm_path.unwrap_or_else(|| {
94 let base_path = PathBuf::new();
95 node_path.parent().unwrap_or(&base_path).join("npm")
96 }),
97 )
98 }),
99 };
100 tx.send(Some(options)).log_err();
101 })
102 .detach();
103 let node_runtime = NodeRuntime::new(client.http_client(), None, rx);
104
105 let extension_host_proxy = ExtensionHostProxy::global(cx);
106
107 language::init(cx);
108 debug_adapter_extension::init(extension_host_proxy.clone(), cx);
109 language_extension::init(LspAccess::Noop, extension_host_proxy, languages.clone());
110 language_model::init(client.clone(), cx);
111 language_models::init(user_store.clone(), client.clone(), cx);
112 languages::init(languages.clone(), fs.clone(), node_runtime.clone(), cx);
113 prompt_store::init(cx);
114 terminal_view::init(cx);
115
116 ZetaCliAppState {
117 languages,
118 client,
119 user_store,
120 fs,
121 node_runtime,
122 }
123}