1use std::sync::Arc;
5
6use gpui_kit::{App, AppContext as _, WindowOptions};
7use subspace_agent_tools::{ToolEnvironment, ToolHost};
8use subspace_core::config;
9use subspace_core::credentials::Credentials;
10use subspace_dashboard::dock_panel::DockPanelRegistration;
11use subspace_dashboard::{Services, prefs};
12use subspace_github_auth::OAuthClient;
13use subspace_product::ProductContext;
14
15mod account;
16pub(crate) mod app;
17mod assets;
18mod build_metadata;
19mod bundled_fonts;
20#[cfg(any(test, all(debug_assertions, feature = "capture")))]
21mod capture;
22#[cfg(windows)]
23mod console;
24mod diagnostics;
25#[cfg(debug_assertions)]
26mod inspector;
27mod jig;
28mod logging;
29mod mcp;
30mod notifications;
31mod palette;
32mod product;
33mod registry;
34mod search_runtime;
35mod settings;
36mod startup;
37mod startup_failure;
38mod startup_path;
39mod status_bar;
40mod terminal;
41mod theme;
42pub(crate) mod toolbar_labels;
43
44pub use account::{AccountRegistration, AccountSession, AccountTokenSource, account_session};
45pub use bundled_fonts::BundledFont;
46pub use product::{config_file, prepare_context};
47pub use registry::reload_configured_boards;
48pub use registry::{BoardRegistration, ConfiguredBoards};
49pub use search_runtime::RerankerFactory;
50pub use settings::extensions::SettingsExtension;
51pub use startup::{ProductBuild, launch};
52pub use startup_failure::report_startup_failure;
53pub use theme::record_new_install_theme;
54
55#[cfg(any(test, feature = "test-support"))]
56pub mod test_support;
57
58pub(crate) struct DesktopOptions {
60 pub account: Option<AccountRegistration>,
61 pub display_name_prefix: Option<&'static str>,
62 pub reranker: Option<RerankerFactory>,
63 pub extensions: &'static [SettingsExtension],
64 pub dock_panel: Option<DockPanelRegistration>,
65 pub github_oauth: Option<OAuthClient>,
66 pub fonts: &'static [BundledFont],
67 #[cfg(feature = "report-issue")]
68 pub report_issue_repository: Option<&'static str>,
69}
70
71fn run_desktop(
73 context: ProductContext,
74 boards: Vec<BoardRegistration>,
75 configured: Option<ConfiguredBoards>,
76 artwork: &'static [(&'static str, &'static [u8])],
77 options: DesktopOptions,
78) -> Result<(), String> {
79 theme::validate_product_themes(&context.product)?;
80 let files = product::ProductFiles::for_context(&context);
81 let sign_in = options.github_oauth.is_some();
82 let (mut environment, mut notices) =
83 tool_environment(&files.config, &boards, ToolHost::App, sign_in);
84 #[cfg(feature = "report-issue")]
85 notices.extend(app::report_issue::target(options.report_issue_repository).err());
86 notices.extend(logging::start(&files, &context));
87 let (registrations, board_notices) =
88 registry::desktop_sets(&context.product, &boards, configured, &environment.config)?;
89 notices.extend(board_notices);
90 if let Err(error) = search_runtime::initialize(
91 &mut environment,
92 &context,
93 ®istrations.active,
94 &files,
95 options.reranker,
96 ) {
97 notices.push(format!("Full-text search unavailable: {error}"));
98 }
99 let tools = registry::tool_catalog(®istrations.active, &environment)?;
100 let mcp = mcp::McpService::new(®istrations.active, environment.clone());
101 let context = Arc::new(context);
102 #[cfg(windows)]
103 console::detach()?;
104 let application =
105 gpui_kit::application().with_assets(assets::Assets::new(artwork, ®istrations.included));
106 application.on_reopen(ensure_product_window);
107 application.run(move |cx| {
108 gpui_kit::init(cx);
109 product::install_context(context.clone(), options.display_name_prefix, cx);
110 install_registrations(
111 boards.clone(),
112 configured,
113 registrations,
114 options.extensions,
115 cx,
116 );
117 terminal::install(options.dock_panel, cx);
118 prefs::init_file(Some(files.state.clone()), cx);
119 let config_file = files.config.clone();
120 cx.set_global(files);
121 let notices = start_theme(notices, options.fonts, cx);
122 install_services(environment, tools, config_file, options.github_oauth, cx);
123 account::init(options.account, cx);
124 #[cfg(feature = "report-issue")]
125 app::report_issue::install(options.report_issue_repository, cx);
126 bind_keys(cx);
127 app::install_menus(cx);
128 notifications::init(cx);
129 search_runtime::start(cx);
130 registry::sync_backgrounds(cx);
131 mcp::init(mcp, cx);
132 let name = product::desktop_name(cx).to_owned();
133 open_product_window(notices, &name, cx);
134 });
135 Ok(())
136}
137
138fn start_theme(notices: Vec<String>, fonts: &'static [BundledFont], cx: &mut App) -> Vec<String> {
141 notices.into_iter().chain(theme::start(fonts, cx)).collect()
142}
143
144fn install_services(
147 environment: ToolEnvironment,
148 tools: subspace_dashboard::Catalog,
149 config_file: std::path::PathBuf,
150 github_oauth: Option<OAuthClient>,
151 cx: &mut App,
152) {
153 cx.set_global(Services {
154 config: environment.config,
155 credentials: environment.credentials,
156 tools,
157 search: environment.search,
158 config_file,
159 });
160 subspace_dashboard::github_sign_in::init(github_oauth, cx);
161}
162
163fn install_registrations(
164 boards: Vec<BoardRegistration>,
165 configured: Option<ConfiguredBoards>,
166 registrations: registry::RegistrationSets,
167 extensions: &'static [SettingsExtension],
168 cx: &mut App,
169) {
170 registry::retain_provider(boards, configured, cx);
171 notifications::install_defaults(®istrations.included, cx);
172 cx.set_global(settings::extensions::SettingsExtensions(extensions));
173 cx.set_global(registry::IncludedBoards(registrations.included));
174 cx.set_global(registry::BoardFactories(registrations.active));
175}
176
177fn bind_keys(cx: &mut App) {
178 app::bind_keys(cx);
179 terminal::bind_keys(cx);
180 subspace_dashboard::bind_keys(cx);
181 #[cfg(debug_assertions)]
182 inspector::init(cx);
183}
184
185pub(crate) fn tool_environment(
190 config_file: &std::path::Path,
191 boards: &[BoardRegistration],
192 host: ToolHost,
193 sign_in: bool,
194) -> (ToolEnvironment, Vec<String>) {
195 let (config, notices) =
196 config::load_with_boards(config_file, boards.iter().map(|board| board.id));
197 let credentials = Arc::new(Credentials::from_system(&config).with_sign_in_offered(sign_in));
198 let environment = ToolEnvironment {
199 config: Arc::new(config),
200 credentials,
201 search: None,
202 config_file: Some(config_file.to_owned()),
203 host,
204 };
205 (
206 environment,
207 notices.iter().map(ToString::to_string).collect(),
208 )
209}
210
211fn open_product_window(notices: Vec<String>, name: &str, cx: &mut App) {
212 let options = WindowOptions {
213 window_bounds: Some(app::window_bounds(cx)),
214 titlebar: Some(app::titlebar::options(name.to_owned())),
215 app_owns_titlebar_drag: app::titlebar::INTEGRATED,
216 ..Default::default()
217 };
218 let opened = gpui_kit::open_window(options, cx, |window, cx| {
219 cx.new(|cx| app::SubspaceApp::new(notices, window, cx))
220 });
221 if let Err(error) = opened {
222 let message = format!("{name} could not open its window: {error}");
223 subspace_core::log::Logger::new("app").record(
224 subspace_core::log::Level::Error,
225 &message,
226 &[],
227 );
228 eprintln!("{message}");
229 cx.quit();
230 }
231 #[cfg(all(debug_assertions, feature = "capture"))]
232 capture::start(cx);
233}
234
235fn ensure_product_window(cx: &mut App) {
237 let Some(runtime) = cx.try_global::<notifications::Runtime>() else {
238 return;
239 };
240 if let Some((window, app)) = runtime.window.clone()
241 && let Some(app) = app.upgrade()
242 && window
243 .update(cx, |_, window, cx| {
244 let pending = cx.global_mut::<notifications::Runtime>().pending.take();
245 if let Some(route) = pending {
246 app.update(cx, |app, cx| app.open_notification(route, window, cx));
247 }
248 window.activate_window();
249 })
250 .is_ok()
251 {
252 return;
253 }
254 let name = product::desktop_name(cx).to_owned();
255 open_product_window(Vec::new(), &name, cx);
256}