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 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_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 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 debug_adapter_extension::init(extension_host_proxy.clone(), cx);
108 language_extension::init(LspAccess::Noop, extension_host_proxy, languages.clone());
109 language_model::init(client.clone(), cx);
110 language_models::init(user_store.clone(), client.clone(), cx);
111 languages::init(languages.clone(), fs.clone(), node_runtime.clone(), cx);
112 prompt_store::init(cx);
113 terminal_view::init(cx);
114
115 ZetaCliAppState {
116 languages,
117 client,
118 user_store,
119 fs,
120 node_runtime,
121 }
122}