2026-07-20 16:12:11 +08:00
|
|
|
|
pub mod auth;
|
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 14:59:55 +08:00
|
|
|
|
pub mod quick_window;
|
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;
|
2026-07-20 14:59:55 +08:00
|
|
|
|
use quick_window::{
|
|
|
|
|
|
DEFAULT_HEIGHT, DEFAULT_WIDTH, MAX_HEIGHT, MAX_WIDTH, MIN_HEIGHT, MIN_WIDTH, QUICK_WINDOW_ID,
|
|
|
|
|
|
QuickWindowState,
|
|
|
|
|
|
};
|
2026-07-20 00:49:15 +08:00
|
|
|
|
use runtime_guards::RuntimeGuards;
|
2026-07-21 14:43:14 +08:00
|
|
|
|
use serde::{Deserialize, Serialize};
|
2026-07-18 22:17:29 +08:00
|
|
|
|
|
2026-07-18 20:34:47 +08:00
|
|
|
|
use tauri::{
|
2026-07-20 20:50:27 +08:00
|
|
|
|
AppHandle, Emitter, Manager, PhysicalPosition, PhysicalSize, WebviewUrl, WebviewWindowBuilder,
|
2026-07-20 14:59:55 +08:00
|
|
|
|
WindowEvent,
|
2026-07-20 20:50:27 +08:00
|
|
|
|
menu::{Menu, MenuItem, PredefinedMenuItem},
|
2026-07-18 20:34:47 +08:00
|
|
|
|
tray::TrayIconBuilder,
|
2026-07-20 20:50:27 +08:00
|
|
|
|
window::Color,
|
2026-07-18 20:34:47 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-07-20 16:28:42 +08:00
|
|
|
|
const PET_WINDOW_ID: &str = "desktop-pet";
|
2026-07-21 14:43:14 +08:00
|
|
|
|
const SESSION_MARKER_FILE: &str = "session.active";
|
|
|
|
|
|
|
|
|
|
|
|
struct SessionMarker(std::path::PathBuf);
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize)]
|
|
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
|
|
struct MonitorInfo {
|
|
|
|
|
|
name: Option<String>,
|
|
|
|
|
|
left: i32,
|
|
|
|
|
|
top: i32,
|
|
|
|
|
|
width: u32,
|
|
|
|
|
|
height: u32,
|
|
|
|
|
|
work_left: i32,
|
|
|
|
|
|
work_top: i32,
|
|
|
|
|
|
work_width: u32,
|
|
|
|
|
|
work_height: u32,
|
|
|
|
|
|
scale_factor: f64,
|
|
|
|
|
|
is_primary: bool,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
|
|
struct PetWindowPreferences {
|
|
|
|
|
|
position_mode: pet::PetPositionMode,
|
|
|
|
|
|
monitor_index: Option<usize>,
|
|
|
|
|
|
always_on_top: bool,
|
|
|
|
|
|
game_mode: bool,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
|
|
struct NetworkDiagnosticRequest {
|
|
|
|
|
|
host: String,
|
|
|
|
|
|
port: u16,
|
|
|
|
|
|
timeout_seconds: Option<u64>,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
|
|
struct ProcessDiagnosticRequest {
|
|
|
|
|
|
process_id: u32,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn available_monitor_info(app: &AppHandle) -> Result<Vec<MonitorInfo>, String> {
|
|
|
|
|
|
let window = app
|
|
|
|
|
|
.get_webview_window("main")
|
|
|
|
|
|
.ok_or_else(|| "main window is unavailable".to_owned())?;
|
|
|
|
|
|
let primary_name = window
|
|
|
|
|
|
.primary_monitor()
|
|
|
|
|
|
.ok()
|
|
|
|
|
|
.flatten()
|
|
|
|
|
|
.and_then(|monitor| monitor.name().cloned());
|
|
|
|
|
|
window
|
|
|
|
|
|
.available_monitors()
|
|
|
|
|
|
.map_err(|error| error.to_string())
|
|
|
|
|
|
.map(|monitors| {
|
|
|
|
|
|
monitors
|
|
|
|
|
|
.into_iter()
|
|
|
|
|
|
.map(|monitor| {
|
|
|
|
|
|
let area = monitor.work_area();
|
|
|
|
|
|
let position = monitor.position();
|
|
|
|
|
|
let size = monitor.size();
|
|
|
|
|
|
let name = monitor.name().cloned();
|
|
|
|
|
|
MonitorInfo {
|
|
|
|
|
|
is_primary: name == primary_name,
|
|
|
|
|
|
name,
|
|
|
|
|
|
left: position.x,
|
|
|
|
|
|
top: position.y,
|
|
|
|
|
|
width: size.width,
|
|
|
|
|
|
height: size.height,
|
|
|
|
|
|
work_left: area.position.x,
|
|
|
|
|
|
work_top: area.position.y,
|
|
|
|
|
|
work_width: area.size.width,
|
|
|
|
|
|
work_height: area.size.height,
|
|
|
|
|
|
scale_factor: monitor.scale_factor(),
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
.collect()
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn place_pet_on_monitor(
|
|
|
|
|
|
window: &tauri::WebviewWindow,
|
|
|
|
|
|
monitor: &MonitorInfo,
|
|
|
|
|
|
mode: pet::PetPositionMode,
|
|
|
|
|
|
) -> Result<(), String> {
|
|
|
|
|
|
let size = window.outer_size().map_err(|error| error.to_string())?;
|
|
|
|
|
|
let pet = pet::MonitorGeometry {
|
|
|
|
|
|
left: monitor.left,
|
|
|
|
|
|
top: monitor.top,
|
|
|
|
|
|
width: monitor.width,
|
|
|
|
|
|
height: monitor.height,
|
|
|
|
|
|
work_left: monitor.work_left,
|
|
|
|
|
|
work_top: monitor.work_top,
|
|
|
|
|
|
work_width: monitor.work_width,
|
|
|
|
|
|
work_height: monitor.work_height,
|
|
|
|
|
|
is_primary: monitor.is_primary,
|
|
|
|
|
|
};
|
|
|
|
|
|
let placement = match mode {
|
|
|
|
|
|
pet::PetPositionMode::Edge | pet::PetPositionMode::Monitor => {
|
|
|
|
|
|
pet::place_at_safe_edge(pet, size.width, size.height, 16)
|
|
|
|
|
|
}
|
|
|
|
|
|
pet::PetPositionMode::Free | pet::PetPositionMode::FollowMainWindow => return Ok(()),
|
|
|
|
|
|
};
|
|
|
|
|
|
window
|
|
|
|
|
|
.set_position(PhysicalPosition::new(placement.x, placement.y))
|
|
|
|
|
|
.map_err(|error| error.to_string())
|
|
|
|
|
|
}
|
2026-07-20 16:28:42 +08:00
|
|
|
|
|
2026-07-18 20:34:47 +08:00
|
|
|
|
#[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)
|
2026-07-20 20:50:27 +08:00
|
|
|
|
.shadow(false)
|
2026-07-20 11:31:41 +08:00
|
|
|
|
.transparent(true)
|
2026-07-20 20:50:27 +08:00
|
|
|
|
.background_color(Color(0, 0, 0, 0))
|
2026-07-20 11:31:41 +08:00
|
|
|
|
.always_on_top(false)
|
|
|
|
|
|
.skip_taskbar(true)
|
|
|
|
|
|
.build()
|
|
|
|
|
|
.map(|_| ())
|
|
|
|
|
|
.map_err(|error| error.to_string())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
2026-07-20 12:34:56 +08:00
|
|
|
|
fn open_quick_chat_window(
|
|
|
|
|
|
app: AppHandle,
|
|
|
|
|
|
slot: Option<desktop_protocol::ActiveSlotKind>,
|
|
|
|
|
|
) -> Result<(), String> {
|
2026-07-20 14:59:55 +08:00
|
|
|
|
if let Some(window) = app.get_webview_window(QUICK_WINDOW_ID) {
|
2026-07-20 11:31:41 +08:00
|
|
|
|
window.show().map_err(|error| error.to_string())?;
|
|
|
|
|
|
window.set_focus().map_err(|error| error.to_string())?;
|
|
|
|
|
|
return Ok(());
|
|
|
|
|
|
}
|
|
|
|
|
|
WebviewWindowBuilder::new(
|
|
|
|
|
|
&app,
|
2026-07-20 14:59:55 +08:00
|
|
|
|
QUICK_WINDOW_ID,
|
2026-07-20 12:34:56 +08:00
|
|
|
|
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(),
|
|
|
|
|
|
}),
|
2026-07-20 11:31:41 +08:00
|
|
|
|
)
|
|
|
|
|
|
.title("AstrBot Quick Chat")
|
2026-07-20 14:59:55 +08:00
|
|
|
|
.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)
|
2026-07-20 11:31:41 +08:00
|
|
|
|
.resizable(true)
|
|
|
|
|
|
.build()
|
|
|
|
|
|
.map_err(|error| error.to_string())
|
2026-07-20 14:59:55 +08:00
|
|
|
|
.map(|window| {
|
|
|
|
|
|
restore_quick_window_state(&window, &app);
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn restore_quick_window_state(window: &tauri::WebviewWindow, app: &AppHandle) {
|
|
|
|
|
|
let database = app.state::<ApplicationDatabase>();
|
|
|
|
|
|
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::<ApplicationDatabase>();
|
|
|
|
|
|
let _ = database.save_window_state(QUICK_WINDOW_ID, &state);
|
2026-07-20 11:31:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-20 17:51:09 +08:00
|
|
|
|
fn exit_application(app: &AppHandle) {
|
2026-07-21 14:43:14 +08:00
|
|
|
|
if let Some(marker) = app.try_state::<SessionMarker>() {
|
|
|
|
|
|
let _ = std::fs::remove_file(&marker.0);
|
|
|
|
|
|
}
|
2026-07-20 20:50:27 +08:00
|
|
|
|
app.cleanup_before_exit();
|
|
|
|
|
|
std::process::exit(0);
|
2026-07-20 17:51:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-21 14:43:14 +08:00
|
|
|
|
fn tray_state_label(name: &str, state: desktop_protocol::ConnectionState) -> String {
|
|
|
|
|
|
let state = match state {
|
|
|
|
|
|
desktop_protocol::ConnectionState::Connected => "在线",
|
|
|
|
|
|
desktop_protocol::ConnectionState::Connecting => "连接中",
|
|
|
|
|
|
desktop_protocol::ConnectionState::Authenticating => "认证中",
|
|
|
|
|
|
desktop_protocol::ConnectionState::Syncing => "同步中",
|
|
|
|
|
|
desktop_protocol::ConnectionState::Reconnecting => "重连中",
|
|
|
|
|
|
desktop_protocol::ConnectionState::AuthExpired => "需要认证",
|
|
|
|
|
|
desktop_protocol::ConnectionState::Disconnected => "未连接",
|
|
|
|
|
|
desktop_protocol::ConnectionState::Offline => "离线",
|
|
|
|
|
|
desktop_protocol::ConnectionState::ServerUnavailable => "服务不可用",
|
|
|
|
|
|
desktop_protocol::ConnectionState::Incompatible => "协议不兼容",
|
|
|
|
|
|
desktop_protocol::ConnectionState::CertificateChanged => "证书已变化",
|
|
|
|
|
|
desktop_protocol::ConnectionState::FatalError => "连接错误",
|
|
|
|
|
|
};
|
|
|
|
|
|
format!("{name}:{state}")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-20 12:34:56 +08:00
|
|
|
|
#[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())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-20 16:28:42 +08:00
|
|
|
|
#[tauri::command]
|
|
|
|
|
|
fn open_pet_window(app: AppHandle) -> Result<(), String> {
|
|
|
|
|
|
if let Some(window) = app.get_webview_window(PET_WINDOW_ID) {
|
2026-07-20 20:50:27 +08:00
|
|
|
|
window
|
|
|
|
|
|
.set_always_on_top(true)
|
|
|
|
|
|
.map_err(|error| error.to_string())?;
|
2026-07-21 14:43:14 +08:00
|
|
|
|
if let Some(monitor) = available_monitor_info(&app)?.first() {
|
|
|
|
|
|
place_pet_on_monitor(&window, monitor, pet::PetPositionMode::Edge)?;
|
|
|
|
|
|
}
|
2026-07-20 16:28:42 +08:00
|
|
|
|
window.show().map_err(|error| error.to_string())?;
|
|
|
|
|
|
return Ok(());
|
|
|
|
|
|
}
|
2026-07-20 20:50:27 +08:00
|
|
|
|
let config = app
|
|
|
|
|
|
.config()
|
|
|
|
|
|
.app
|
|
|
|
|
|
.windows
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.find(|window| window.label == PET_WINDOW_ID)
|
|
|
|
|
|
.ok_or_else(|| "pet window config is unavailable".to_owned())?;
|
|
|
|
|
|
WebviewWindowBuilder::from_config(&app, config)
|
|
|
|
|
|
.map_err(|error| error.to_string())?
|
|
|
|
|
|
.build()
|
|
|
|
|
|
.map(|_| ())
|
|
|
|
|
|
.map_err(|error| error.to_string())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-21 14:43:14 +08:00
|
|
|
|
#[tauri::command]
|
|
|
|
|
|
fn list_monitor_geometries(app: AppHandle) -> Result<Vec<MonitorInfo>, String> {
|
|
|
|
|
|
available_monitor_info(&app)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
|
fn set_pet_window_preferences(
|
|
|
|
|
|
app: AppHandle,
|
|
|
|
|
|
preferences: PetWindowPreferences,
|
|
|
|
|
|
) -> Result<(), String> {
|
|
|
|
|
|
let Some(window) = app.get_webview_window(PET_WINDOW_ID) else {
|
|
|
|
|
|
return Ok(());
|
|
|
|
|
|
};
|
|
|
|
|
|
window
|
|
|
|
|
|
.set_always_on_top(preferences.always_on_top)
|
|
|
|
|
|
.map_err(|error| error.to_string())?;
|
|
|
|
|
|
let monitors = available_monitor_info(&app)?;
|
|
|
|
|
|
let index = preferences
|
|
|
|
|
|
.monitor_index
|
|
|
|
|
|
.unwrap_or(0)
|
|
|
|
|
|
.min(monitors.len().saturating_sub(1));
|
|
|
|
|
|
let mode = if preferences.game_mode {
|
|
|
|
|
|
pet::PetPositionMode::Edge
|
|
|
|
|
|
} else {
|
|
|
|
|
|
preferences.position_mode
|
|
|
|
|
|
};
|
|
|
|
|
|
if let Some(monitor) = monitors.get(index) {
|
|
|
|
|
|
place_pet_on_monitor(&window, monitor, mode)?;
|
|
|
|
|
|
}
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
|
fn inspect_foreground_window() -> Result<windows::foreground_window::ForegroundWindowSummary, String>
|
|
|
|
|
|
{
|
|
|
|
|
|
windows::foreground_window::inspect_foreground_window(std::time::Duration::from_secs(60))
|
|
|
|
|
|
.map(|selection| selection.summary())
|
|
|
|
|
|
.map_err(|error| error.to_string())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
|
fn inspect_process(
|
|
|
|
|
|
request: ProcessDiagnosticRequest,
|
|
|
|
|
|
) -> Result<windows::process::ProcessSummary, String> {
|
|
|
|
|
|
windows::process::inspect_process(request.process_id).map_err(|error| error.to_string())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
|
async fn check_network_target(
|
|
|
|
|
|
request: NetworkDiagnosticRequest,
|
|
|
|
|
|
) -> Result<diagnostics::network::NetworkCheckResult, String> {
|
|
|
|
|
|
diagnostics::network::check_tcp_target(&diagnostics::network::NetworkTarget {
|
|
|
|
|
|
host: request.host,
|
|
|
|
|
|
port: request.port,
|
|
|
|
|
|
timeout: std::time::Duration::from_secs(request.timeout_seconds.unwrap_or(3).min(10)),
|
|
|
|
|
|
})
|
|
|
|
|
|
.await
|
|
|
|
|
|
.map_err(|error| error.to_string())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-20 20:50:27 +08:00
|
|
|
|
#[tauri::command]
|
|
|
|
|
|
fn close_pet_window(app: AppHandle) -> Result<(), String> {
|
|
|
|
|
|
if let Some(window) = app.get_webview_window(PET_WINDOW_ID) {
|
|
|
|
|
|
window.destroy().map_err(|error| error.to_string())?;
|
|
|
|
|
|
}
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
|
fn show_pet_context_menu(app: AppHandle) -> Result<(), String> {
|
|
|
|
|
|
let open_main = MenuItem::with_id(
|
2026-07-20 16:28:42 +08:00
|
|
|
|
&app,
|
2026-07-20 20:50:27 +08:00
|
|
|
|
"pet-open-main",
|
|
|
|
|
|
"\u{6253}\u{5f00}\u{4e3b}\u{754c}\u{9762}",
|
|
|
|
|
|
true,
|
|
|
|
|
|
None::<&str>,
|
2026-07-20 16:28:42 +08:00
|
|
|
|
)
|
2026-07-20 20:50:27 +08:00
|
|
|
|
.map_err(|error| error.to_string())?;
|
|
|
|
|
|
let open_quick = MenuItem::with_id(
|
|
|
|
|
|
&app,
|
|
|
|
|
|
"pet-open-quick",
|
|
|
|
|
|
"\u{6253}\u{5f00}\u{5feb}\u{6377}\u{804a}\u{5929}",
|
|
|
|
|
|
true,
|
|
|
|
|
|
None::<&str>,
|
|
|
|
|
|
)
|
|
|
|
|
|
.map_err(|error| error.to_string())?;
|
|
|
|
|
|
let toggle_game = MenuItem::with_id(
|
|
|
|
|
|
&app,
|
|
|
|
|
|
"pet-game-mode",
|
|
|
|
|
|
"\u{5207}\u{6362}\u{6e38}\u{620f}\u{6a21}\u{5f0f}",
|
|
|
|
|
|
true,
|
|
|
|
|
|
None::<&str>,
|
|
|
|
|
|
)
|
|
|
|
|
|
.map_err(|error| error.to_string())?;
|
|
|
|
|
|
let separator = PredefinedMenuItem::separator(&app).map_err(|error| error.to_string())?;
|
|
|
|
|
|
let hide_pet = MenuItem::with_id(
|
|
|
|
|
|
&app,
|
|
|
|
|
|
"pet-hide",
|
|
|
|
|
|
"\u{5173}\u{95ed}\u{684c}\u{5ba0}",
|
|
|
|
|
|
true,
|
|
|
|
|
|
None::<&str>,
|
|
|
|
|
|
)
|
|
|
|
|
|
.map_err(|error| error.to_string())?;
|
|
|
|
|
|
let quit = MenuItem::with_id(
|
|
|
|
|
|
&app,
|
|
|
|
|
|
"pet-exit",
|
|
|
|
|
|
"\u{5173}\u{95ed}\u{7a0b}\u{5e8f}",
|
|
|
|
|
|
true,
|
|
|
|
|
|
None::<&str>,
|
|
|
|
|
|
)
|
|
|
|
|
|
.map_err(|error| error.to_string())?;
|
|
|
|
|
|
let menu = Menu::with_items(
|
|
|
|
|
|
&app,
|
|
|
|
|
|
&[
|
|
|
|
|
|
&open_main,
|
|
|
|
|
|
&open_quick,
|
|
|
|
|
|
&toggle_game,
|
|
|
|
|
|
&separator,
|
|
|
|
|
|
&hide_pet,
|
|
|
|
|
|
&quit,
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
|
|
|
|
|
.map_err(|error| error.to_string())?;
|
|
|
|
|
|
let window = app
|
|
|
|
|
|
.get_webview_window(PET_WINDOW_ID)
|
|
|
|
|
|
.or_else(|| app.get_webview_window("main"))
|
|
|
|
|
|
.ok_or_else(|| "pet context menu window is unavailable".to_owned())?;
|
|
|
|
|
|
window.popup_menu(&menu).map_err(|error| error.to_string())
|
2026-07-20 16:28:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
}))
|
2026-07-20 20:50:27 +08:00
|
|
|
|
.on_menu_event(|app, event| match event.id.as_ref() {
|
|
|
|
|
|
"pet-open-main" => {
|
|
|
|
|
|
let _ = open_main_window(app.clone());
|
|
|
|
|
|
}
|
|
|
|
|
|
"pet-open-quick" => {
|
|
|
|
|
|
let _ = open_quick_chat_window(app.clone(), None);
|
|
|
|
|
|
}
|
|
|
|
|
|
"pet-game-mode" => {
|
|
|
|
|
|
let _ = app.emit("pet-game-mode-toggle", ());
|
|
|
|
|
|
}
|
|
|
|
|
|
"pet-hide" => {
|
|
|
|
|
|
let _ = close_pet_window(app.clone());
|
|
|
|
|
|
let _ = app.emit("pet-hide-requested", ());
|
|
|
|
|
|
}
|
|
|
|
|
|
"pet-exit" => exit_application(app),
|
|
|
|
|
|
_ => {}
|
|
|
|
|
|
})
|
2026-07-18 20:34:47 +08:00
|
|
|
|
.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-21 14:43:14 +08:00
|
|
|
|
let session_marker = app_data_dir.join(SESSION_MARKER_FILE);
|
|
|
|
|
|
if session_marker.exists() {
|
|
|
|
|
|
tracing::warn!("previous AstrBot Desktop session ended unexpectedly; local state remains available");
|
|
|
|
|
|
}
|
|
|
|
|
|
std::fs::write(&session_marker, std::process::id().to_string())?;
|
|
|
|
|
|
app.manage(SessionMarker(session_marker));
|
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-21 14:43:14 +08:00
|
|
|
|
let event_handle = app.handle().clone();
|
|
|
|
|
|
tauri::async_runtime::spawn(async move {
|
|
|
|
|
|
loop {
|
|
|
|
|
|
let runtime = event_handle.state::<DualSlotRuntime>();
|
|
|
|
|
|
if let Some(event) = runtime.try_recv_event().await {
|
|
|
|
|
|
let database = event_handle.state::<ApplicationDatabase>();
|
|
|
|
|
|
networking::commands::persist_runtime_event(&database, &event);
|
|
|
|
|
|
let _ = event_handle.emit("connection-runtime-event", event);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
let show = MenuItem::with_id(app, "show", "打开主界面", true, None::<&str>)?;
|
|
|
|
|
|
let quick = MenuItem::with_id(app, "quick", "打开快捷聊天", true, None::<&str>)?;
|
|
|
|
|
|
let companion_status =
|
|
|
|
|
|
MenuItem::with_id(app, "companion-status", "陪伴:未连接", false, None::<&str>)?;
|
|
|
|
|
|
let operations_status = MenuItem::with_id(
|
|
|
|
|
|
app,
|
|
|
|
|
|
"operations-status",
|
|
|
|
|
|
"运维:未连接",
|
|
|
|
|
|
false,
|
|
|
|
|
|
None::<&str>,
|
|
|
|
|
|
)?;
|
|
|
|
|
|
let quit = MenuItem::with_id(app, "quit", "退出程序", true, None::<&str>)?;
|
|
|
|
|
|
let menu = Menu::with_items(
|
|
|
|
|
|
app,
|
|
|
|
|
|
&[&show, &quick, &companion_status, &operations_status, &quit],
|
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
|
|
let tray_handle = app.handle().clone();
|
|
|
|
|
|
let companion_status_handle = companion_status.clone();
|
|
|
|
|
|
let operations_status_handle = operations_status.clone();
|
|
|
|
|
|
tauri::async_runtime::spawn(async move {
|
|
|
|
|
|
loop {
|
|
|
|
|
|
let status = tray_handle.state::<DualSlotRuntime>().status().await;
|
|
|
|
|
|
let _ = companion_status_handle
|
|
|
|
|
|
.set_text(tray_state_label("陪伴", status.companion.state));
|
|
|
|
|
|
let _ = operations_status_handle
|
|
|
|
|
|
.set_text(tray_state_label("运维", status.operations.state));
|
|
|
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2026-07-18 20:34:47 +08:00
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-07-21 14:43:14 +08:00
|
|
|
|
"quick" => {
|
|
|
|
|
|
let _ = open_quick_chat_window(app.clone(), None);
|
|
|
|
|
|
}
|
2026-07-20 17:51:09 +08:00
|
|
|
|
"quit" => exit_application(app),
|
2026-07-18 20:34:47 +08:00
|
|
|
|
_ => {}
|
|
|
|
|
|
})
|
|
|
|
|
|
.build(app)?;
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
})
|
2026-07-20 14:59:55 +08:00
|
|
|
|
.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());
|
2026-07-20 15:06:05 +08:00
|
|
|
|
return;
|
2026-07-20 14:59:55 +08:00
|
|
|
|
}
|
2026-07-20 16:28:42 +08:00
|
|
|
|
if window.label() == PET_WINDOW_ID {
|
|
|
|
|
|
api.prevent_close();
|
|
|
|
|
|
let _ = window.hide();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2026-07-18 20:34:47 +08:00
|
|
|
|
api.prevent_close();
|
|
|
|
|
|
let _ = window.hide();
|
|
|
|
|
|
}
|
2026-07-20 14:59:55 +08:00
|
|
|
|
_ => {}
|
2026-07-18 20:34:47 +08:00
|
|
|
|
})
|
2026-07-18 23:36:19 +08:00
|
|
|
|
.invoke_handler(tauri::generate_handler![
|
2026-07-20 16:12:11 +08:00
|
|
|
|
auth::login_profile_jwt,
|
2026-07-18 23:36:19 +08:00
|
|
|
|
bootstrap_status,
|
2026-07-20 11:31:41 +08:00
|
|
|
|
open_quick_chat_window,
|
2026-07-20 12:34:56 +08:00
|
|
|
|
open_main_window,
|
2026-07-20 11:31:41 +08:00
|
|
|
|
open_status_bubble_window,
|
2026-07-20 16:28:42 +08:00
|
|
|
|
open_pet_window,
|
2026-07-20 20:50:27 +08:00
|
|
|
|
close_pet_window,
|
|
|
|
|
|
show_pet_context_menu,
|
2026-07-21 14:43:14 +08:00
|
|
|
|
list_monitor_geometries,
|
|
|
|
|
|
set_pet_window_preferences,
|
|
|
|
|
|
inspect_foreground_window,
|
|
|
|
|
|
inspect_process,
|
|
|
|
|
|
check_network_target,
|
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,
|
2026-07-20 12:25:08 +08:00
|
|
|
|
networking::commands::list_chat_sessions,
|
|
|
|
|
|
networking::commands::mark_chat_session_read,
|
2026-07-20 11:31:41 +08:00
|
|
|
|
networking::commands::probe_connection_slot,
|
|
|
|
|
|
networking::commands::disconnect_connection_slot,
|
2026-07-20 12:25:08 +08:00
|
|
|
|
networking::commands::send_connection_slot_message,
|
2026-07-20 13:30:29 +08:00
|
|
|
|
networking::commands::connection_slot_capabilities,
|
2026-07-20 14:32:07 +08:00
|
|
|
|
networking::commands::connection_slot_sessions,
|
|
|
|
|
|
networking::commands::switch_connection_slot_session,
|
2026-07-20 13:30:29 +08:00
|
|
|
|
networking::commands::upload_connection_slot_file,
|
2026-07-22 02:52:31 +08:00
|
|
|
|
networking::commands::download_connection_slot_attachment,
|
2026-07-20 12:08:06 +08:00
|
|
|
|
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");
|
|
|
|
|
|
}
|