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