pub mod auth; pub mod credentials; pub mod database; pub mod diagnostics; pub mod file_transfer; pub mod logging; pub mod networking; pub mod pet; pub mod policy; pub mod profiles; pub mod quick_window; pub mod runtime_guards; pub mod secure_store; pub mod slots; pub mod windows; use database::ApplicationDatabase; use networking::{DualSlotRuntime, auto_connect_configured_slots}; use policy::ConnectionPolicyStatus; use quick_window::{ DEFAULT_HEIGHT, DEFAULT_WIDTH, MAX_HEIGHT, MAX_WIDTH, MIN_HEIGHT, MIN_WIDTH, QUICK_WINDOW_ID, QuickWindowState, }; use runtime_guards::RuntimeGuards; use serde::Serialize; use tauri::{ AppHandle, Manager, PhysicalPosition, PhysicalSize, WebviewUrl, WebviewWindowBuilder, WindowEvent, menu::{Menu, MenuItem}, tray::TrayIconBuilder, }; const PET_WINDOW_ID: &str = "desktop-pet"; #[derive(Debug, Clone, Serialize)] #[serde(rename_all = "camelCase")] struct BootstrapStatus { app_version: &'static str, database_schema_version: u32, platform: &'static str, rust_core: &'static str, protocol_version: &'static str, } #[tauri::command] fn open_status_bubble_window(app: AppHandle) -> Result<(), String> { if let Some(window) = app.get_webview_window("status-bubble") { window.show().map_err(|error| error.to_string())?; return Ok(()); } WebviewWindowBuilder::new( &app, "status-bubble", WebviewUrl::App("index.html?view=bubble".into()), ) .title("AstrBot Status") .inner_size(108.0, 108.0) .resizable(false) .decorations(false) .transparent(true) .always_on_top(false) .skip_taskbar(true) .build() .map(|_| ()) .map_err(|error| error.to_string()) } #[tauri::command] fn open_quick_chat_window( app: AppHandle, slot: Option, ) -> Result<(), String> { if let Some(window) = app.get_webview_window(QUICK_WINDOW_ID) { window.show().map_err(|error| error.to_string())?; window.set_focus().map_err(|error| error.to_string())?; return Ok(()); } WebviewWindowBuilder::new( &app, QUICK_WINDOW_ID, WebviewUrl::App(match slot { Some(desktop_protocol::ActiveSlotKind::Companion) => { "index.html?view=quick&slot=companion".into() } Some(desktop_protocol::ActiveSlotKind::Operations) => { "index.html?view=quick&slot=operations".into() } None => "index.html?view=quick".into(), }), ) .title("AstrBot Quick Chat") .inner_size(DEFAULT_WIDTH as f64, DEFAULT_HEIGHT as f64) .min_inner_size(MIN_WIDTH as f64, MIN_HEIGHT as f64) .max_inner_size(MAX_WIDTH as f64, MAX_HEIGHT as f64) .resizable(true) .build() .map_err(|error| error.to_string()) .map(|window| { restore_quick_window_state(&window, &app); }) } fn restore_quick_window_state(window: &tauri::WebviewWindow, app: &AppHandle) { let database = app.state::(); let Ok(Some(state)) = database.load_window_state(QUICK_WINDOW_ID) else { return; }; let state = state.normalized(); let monitor = state .monitor_name .as_deref() .and_then(|name| { window .available_monitors() .ok()? .into_iter() .find(|monitor| monitor.name().map(String::as_str) == Some(name)) }) .or_else(|| window.primary_monitor().ok().flatten()); let Some(monitor) = monitor else { return; }; let area = monitor.work_area(); let state = state.clamp_to_work_area( area.position.x, area.position.y, area.size.width, area.size.height, ); let _ = window.set_size(PhysicalSize::new(state.width, state.height)); let _ = window.set_position(PhysicalPosition::new(state.x, state.y)); } fn save_quick_window_state(window: &tauri::Window, app: &AppHandle) { let Ok(position) = window.outer_position() else { return; }; let Ok(size) = window.inner_size() else { return; }; let monitor_name = window .current_monitor() .ok() .flatten() .and_then(|monitor| monitor.name().cloned()); let state = QuickWindowState { x: position.x, y: position.y, width: size.width, height: size.height, monitor_name, } .normalized(); let database = app.state::(); let _ = database.save_window_state(QUICK_WINDOW_ID, &state); } #[tauri::command] fn open_main_window(app: AppHandle) -> Result<(), String> { let window = app .get_webview_window("main") .ok_or_else(|| "main window is unavailable".to_owned())?; window.show().map_err(|error| error.to_string())?; window.set_focus().map_err(|error| error.to_string()) } #[tauri::command] fn open_pet_window(app: AppHandle) -> Result<(), String> { if let Some(window) = app.get_webview_window(PET_WINDOW_ID) { window.show().map_err(|error| error.to_string())?; return Ok(()); } WebviewWindowBuilder::new( &app, PET_WINDOW_ID, WebviewUrl::App("index.html?view=pet".into()), ) .title("AstrBot Pet") .inner_size(180.0, 220.0) .resizable(false) .decorations(false) .transparent(true) .always_on_top(false) .skip_taskbar(true) .build() .map(|_| ()) .map_err(|error| error.to_string()) } #[tauri::command] fn bootstrap_status( database: tauri::State<'_, ApplicationDatabase>, ) -> Result { let database_schema_version = database .schema_version() .map_err(|_| "database status is unavailable".to_owned())?; Ok(BootstrapStatus { app_version: env!("CARGO_PKG_VERSION"), platform: "windows", rust_core: "ready", protocol_version: desktop_protocol::PROTOCOL_VERSION, database_schema_version, }) } #[tauri::command] fn connection_policy_status( database: tauri::State<'_, ApplicationDatabase>, guards: tauri::State<'_, RuntimeGuards>, ) -> Result { let switch = guards .operations_switch_status() .map_err(|_| "operations switch guard is unavailable".to_owned())?; let warnings = policy::list_duplicate_server_instance_warnings(&database) .map_err(|_| "database status is unavailable".to_owned())?; Ok(ConnectionPolicyStatus { operations_switch_locked: switch.locked, active_operations_task_count: switch.active_task_count, duplicate_server_instance_warnings: warnings, }) } pub fn run() { logging::init(); tauri::Builder::default() .plugin(tauri_plugin_single_instance::init(|app, _, _| { if let Some(window) = app.get_webview_window("main") { let _ = window.show(); let _ = window.set_focus(); } })) .setup(|app| { let app_data_dir = app.path().app_data_dir()?; std::fs::create_dir_all(&app_data_dir)?; app.manage(ApplicationDatabase::open( app_data_dir.join("astrbot-desktop.sqlite3"), )?); app.manage(RuntimeGuards::new()); app.manage(DualSlotRuntime::new()); let startup_handle = app.handle().clone(); tauri::async_runtime::spawn(async move { let database = startup_handle.state::(); let runtime = startup_handle.state::(); auto_connect_configured_slots(&database, &runtime).await; }); let show = MenuItem::with_id(app, "show", "Show AstrBot Desktop", true, None::<&str>)?; let quit = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?; let menu = Menu::with_items(app, &[&show, &quit])?; TrayIconBuilder::with_id("main") .menu(&menu) .on_menu_event(|app, event| match event.id.as_ref() { "show" => { if let Some(window) = app.get_webview_window("main") { let _ = window.show(); let _ = window.set_focus(); } } "quit" => app.exit(0), _ => {} }) .build(app)?; Ok(()) }) .on_window_event(|window, event| match event { WindowEvent::Moved(_) | WindowEvent::Resized(_) => { if window.label() == QUICK_WINDOW_ID { save_quick_window_state(window, window.app_handle()); } } WindowEvent::CloseRequested { api, .. } => { if window.label() == QUICK_WINDOW_ID { save_quick_window_state(window, window.app_handle()); return; } if window.label() == PET_WINDOW_ID { api.prevent_close(); let _ = window.hide(); return; } api.prevent_close(); let _ = window.hide(); } _ => {} }) .invoke_handler(tauri::generate_handler![ auth::login_profile_jwt, bootstrap_status, open_quick_chat_window, open_main_window, open_status_bubble_window, open_pet_window, connection_policy_status, profiles::list_connection_profiles, profiles::create_connection_profile, profiles::update_connection_profile, profiles::delete_connection_profile, slots::list_connection_slots, slots::assign_connection_slot, slots::clear_connection_slot, credentials::enroll_profile_credential, credentials::clear_profile_credential, networking::commands::connection_runtime_status, networking::commands::drain_connection_runtime_events, networking::commands::load_chat_messages, networking::commands::list_chat_sessions, networking::commands::mark_chat_session_read, networking::commands::probe_connection_slot, networking::commands::disconnect_connection_slot, networking::commands::send_connection_slot_message, networking::commands::connection_slot_capabilities, networking::commands::connection_slot_sessions, networking::commands::switch_connection_slot_session, networking::commands::upload_connection_slot_file, networking::commands::cancel_connection_slot_message, ]) .run(tauri::generate_context!()) .expect("failed to run AstrBot Desktop"); }