2026-07-20 11:31:41 +08:00
|
|
|
pub mod credentials;
|
2026-07-18 20:34:47 +08:00
|
|
|
pub mod database;
|
|
|
|
|
pub mod diagnostics;
|
|
|
|
|
pub mod file_transfer;
|
2026-07-18 22:17:29 +08:00
|
|
|
pub mod logging;
|
2026-07-18 20:34:47 +08:00
|
|
|
pub mod networking;
|
2026-07-20 11:31:41 +08:00
|
|
|
pub mod pet;
|
2026-07-20 00:49:15 +08:00
|
|
|
pub mod policy;
|
2026-07-18 23:36:19 +08:00
|
|
|
pub mod profiles;
|
2026-07-20 00:49:15 +08:00
|
|
|
pub mod runtime_guards;
|
2026-07-18 20:34:47 +08:00
|
|
|
pub mod secure_store;
|
2026-07-19 21:37:10 +08:00
|
|
|
pub mod slots;
|
2026-07-18 20:34:47 +08:00
|
|
|
pub mod windows;
|
|
|
|
|
|
2026-07-18 22:17:29 +08:00
|
|
|
use database::ApplicationDatabase;
|
2026-07-20 12:01:18 +08:00
|
|
|
use networking::{DualSlotRuntime, auto_connect_configured_slots};
|
2026-07-20 00:49:15 +08:00
|
|
|
use policy::ConnectionPolicyStatus;
|
|
|
|
|
use runtime_guards::RuntimeGuards;
|
2026-07-18 20:34:47 +08:00
|
|
|
use serde::Serialize;
|
2026-07-18 22:17:29 +08:00
|
|
|
|
2026-07-18 20:34:47 +08:00
|
|
|
use tauri::{
|
2026-07-20 11:31:41 +08:00
|
|
|
AppHandle, Manager, WebviewUrl, WebviewWindowBuilder, WindowEvent,
|
2026-07-18 20:34:47 +08:00
|
|
|
menu::{Menu, MenuItem},
|
|
|
|
|
tray::TrayIconBuilder,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize)]
|
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
|
struct BootstrapStatus {
|
|
|
|
|
app_version: &'static str,
|
2026-07-18 22:17:29 +08:00
|
|
|
database_schema_version: u32,
|
2026-07-18 20:34:47 +08:00
|
|
|
platform: &'static str,
|
|
|
|
|
rust_core: &'static str,
|
|
|
|
|
protocol_version: &'static str,
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 11:31:41 +08:00
|
|
|
#[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) -> Result<(), String> {
|
|
|
|
|
if let Some(window) = app.get_webview_window("quick-chat") {
|
|
|
|
|
window.show().map_err(|error| error.to_string())?;
|
|
|
|
|
window.set_focus().map_err(|error| error.to_string())?;
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
WebviewWindowBuilder::new(
|
|
|
|
|
&app,
|
|
|
|
|
"quick-chat",
|
|
|
|
|
WebviewUrl::App("index.html?view=quick".into()),
|
|
|
|
|
)
|
|
|
|
|
.title("AstrBot Quick Chat")
|
|
|
|
|
.inner_size(420.0, 620.0)
|
|
|
|
|
.min_inner_size(320.0, 420.0)
|
|
|
|
|
.resizable(true)
|
|
|
|
|
.build()
|
|
|
|
|
.map(|_| ())
|
|
|
|
|
.map_err(|error| error.to_string())
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-18 20:34:47 +08:00
|
|
|
#[tauri::command]
|
2026-07-18 22:17:29 +08:00
|
|
|
fn bootstrap_status(
|
|
|
|
|
database: tauri::State<'_, ApplicationDatabase>,
|
|
|
|
|
) -> Result<BootstrapStatus, String> {
|
|
|
|
|
let database_schema_version = database
|
|
|
|
|
.schema_version()
|
|
|
|
|
.map_err(|_| "database status is unavailable".to_owned())?;
|
|
|
|
|
|
|
|
|
|
Ok(BootstrapStatus {
|
2026-07-18 20:34:47 +08:00
|
|
|
app_version: env!("CARGO_PKG_VERSION"),
|
|
|
|
|
platform: "windows",
|
|
|
|
|
rust_core: "ready",
|
|
|
|
|
protocol_version: desktop_protocol::PROTOCOL_VERSION,
|
2026-07-18 22:17:29 +08:00
|
|
|
database_schema_version,
|
|
|
|
|
})
|
2026-07-18 20:34:47 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-20 00:49:15 +08:00
|
|
|
#[tauri::command]
|
|
|
|
|
fn connection_policy_status(
|
|
|
|
|
database: tauri::State<'_, ApplicationDatabase>,
|
|
|
|
|
guards: tauri::State<'_, RuntimeGuards>,
|
|
|
|
|
) -> Result<ConnectionPolicyStatus, String> {
|
|
|
|
|
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,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-18 20:34:47 +08:00
|
|
|
pub fn run() {
|
2026-07-18 22:17:29 +08:00
|
|
|
logging::init();
|
2026-07-18 20:34:47 +08:00
|
|
|
|
|
|
|
|
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| {
|
2026-07-18 22:17:29 +08:00
|
|
|
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"),
|
|
|
|
|
)?);
|
2026-07-20 00:49:15 +08:00
|
|
|
app.manage(RuntimeGuards::new());
|
2026-07-20 11:31:41 +08:00
|
|
|
app.manage(DualSlotRuntime::new());
|
2026-07-18 22:17:29 +08:00
|
|
|
|
2026-07-20 12:01:18 +08:00
|
|
|
let startup_handle = app.handle().clone();
|
|
|
|
|
tauri::async_runtime::spawn(async move {
|
|
|
|
|
let database = startup_handle.state::<ApplicationDatabase>();
|
|
|
|
|
let runtime = startup_handle.state::<DualSlotRuntime>();
|
|
|
|
|
auto_connect_configured_slots(&database, &runtime).await;
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-18 20:34:47 +08:00
|
|
|
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| {
|
|
|
|
|
if let WindowEvent::CloseRequested { api, .. } = event {
|
|
|
|
|
api.prevent_close();
|
|
|
|
|
let _ = window.hide();
|
|
|
|
|
}
|
|
|
|
|
})
|
2026-07-18 23:36:19 +08:00
|
|
|
.invoke_handler(tauri::generate_handler![
|
|
|
|
|
bootstrap_status,
|
2026-07-20 11:31:41 +08:00
|
|
|
open_quick_chat_window,
|
|
|
|
|
open_status_bubble_window,
|
2026-07-20 00:49:15 +08:00
|
|
|
connection_policy_status,
|
2026-07-18 23:36:19 +08:00
|
|
|
profiles::list_connection_profiles,
|
|
|
|
|
profiles::create_connection_profile,
|
|
|
|
|
profiles::update_connection_profile,
|
|
|
|
|
profiles::delete_connection_profile,
|
2026-07-19 21:37:10 +08:00
|
|
|
slots::list_connection_slots,
|
|
|
|
|
slots::assign_connection_slot,
|
|
|
|
|
slots::clear_connection_slot,
|
2026-07-20 11:31:41 +08:00
|
|
|
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::probe_connection_slot,
|
|
|
|
|
networking::commands::disconnect_connection_slot,
|
2026-07-20 12:08:06 +08:00
|
|
|
networking::commands::send_connection_slot_message,
|
|
|
|
|
networking::commands::cancel_connection_slot_message,
|
2026-07-18 23:36:19 +08:00
|
|
|
])
|
2026-07-18 20:34:47 +08:00
|
|
|
.run(tauri::generate_context!())
|
|
|
|
|
.expect("failed to run AstrBot Desktop");
|
|
|
|
|
}
|