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