headless.rs

  1use std::path::PathBuf;
  2use std::sync::Arc;
  3
  4use client::{Client, ProxySettings, RefreshLlmTokenListener, UserStore};
  5use db::AppDatabase;
  6use extension::ExtensionHostProxy;
  7use fs::RealFs;
  8use gpui::http_client::read_proxy_from_env;
  9use gpui::{App, AppContext as _, Entity};
 10use gpui_tokio::Tokio;
 11use language::LanguageRegistry;
 12use language_extension::LspAccess;
 13use node_runtime::{NodeBinaryOptions, NodeRuntime};
 14use project::project_settings::ProjectSettings;
 15use prompt_store::PromptBuilder;
 16use release_channel::{AppCommitSha, AppVersion};
 17use reqwest_client::ReqwestClient;
 18use settings::{Settings, SettingsStore};
 19use util::ResultExt as _;
 20
 21pub struct AgentCliAppState {
 22    pub languages: Arc<LanguageRegistry>,
 23    pub client: Arc<Client>,
 24    pub user_store: Entity<UserStore>,
 25    pub fs: Arc<dyn fs::Fs>,
 26    pub node_runtime: NodeRuntime,
 27}
 28
 29pub fn init(cx: &mut App) -> Arc<AgentCliAppState> {
 30    let app_commit_sha = option_env!("ZED_COMMIT_SHA").map(|s| AppCommitSha::new(s.to_owned()));
 31
 32    let app_version = AppVersion::load(
 33        env!("ZED_PKG_VERSION"),
 34        option_env!("ZED_BUILD_ID"),
 35        app_commit_sha,
 36    );
 37
 38    release_channel::init(app_version.clone(), cx);
 39    gpui_tokio::init(cx);
 40
 41    let settings_store = SettingsStore::new(cx, &settings::default_settings());
 42    cx.set_global(settings_store);
 43
 44    let user_agent = format!(
 45        "Zed Agent CLI/{} ({}; {})",
 46        app_version,
 47        std::env::consts::OS,
 48        std::env::consts::ARCH
 49    );
 50    let proxy_str = ProxySettings::get_global(cx).proxy.to_owned();
 51    let proxy_url = proxy_str
 52        .as_ref()
 53        .and_then(|input| input.parse().ok())
 54        .or_else(read_proxy_from_env);
 55    let http = {
 56        let _guard = Tokio::handle(cx).enter();
 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    <dyn fs::Fs>::set_global(fs.clone(), cx);
 74
 75    let mut languages = LanguageRegistry::new(cx.background_executor().clone());
 76    languages.set_language_server_download_dir(paths::languages_dir().clone());
 77    let languages = Arc::new(languages);
 78
 79    let user_store = cx.new(|cx| UserStore::new(client.clone(), cx));
 80
 81    extension::init(cx);
 82
 83    let (mut node_options_tx, node_options_rx) = watch::channel(None);
 84    cx.observe_global::<SettingsStore>(move |cx| {
 85        let settings = &ProjectSettings::get_global(cx).node;
 86        let options = NodeBinaryOptions {
 87            allow_path_lookup: !settings.ignore_system_version,
 88            allow_binary_download: true,
 89            use_paths: settings.path.as_ref().map(|node_path| {
 90                let node_path = PathBuf::from(shellexpand::tilde(node_path).as_ref());
 91                let npm_path = settings
 92                    .npm_path
 93                    .as_ref()
 94                    .map(|path| PathBuf::from(shellexpand::tilde(&path).as_ref()));
 95                (
 96                    node_path.clone(),
 97                    npm_path.unwrap_or_else(|| {
 98                        let base_path = PathBuf::new();
 99                        node_path.parent().unwrap_or(&base_path).join("npm")
100                    }),
101                )
102            }),
103        };
104        node_options_tx.send(Some(options)).log_err();
105    })
106    .detach();
107    let node_runtime = NodeRuntime::new(client.http_client(), None, node_options_rx);
108
109    let extension_host_proxy = ExtensionHostProxy::global(cx);
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    let stdout_is_a_pty = false;
120    let prompt_builder = PromptBuilder::load(fs.clone(), stdout_is_a_pty, cx);
121    agent_ui::init(
122        fs.clone(),
123        prompt_builder,
124        languages.clone(),
125        true,
126        true,
127        cx,
128    );
129
130    Arc::new(AgentCliAppState {
131        languages,
132        client,
133        user_store,
134        fs,
135        node_runtime,
136    })
137}