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