2026-08-13 12:48:50 +08:00
|
|
|
mod commands;
|
|
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
use tokio::sync::RwLock;
|
2026-08-13 15:37:28 +08:00
|
|
|
use tauri::{Emitter, Manager};
|
2026-08-13 12:48:50 +08:00
|
|
|
use xianren_core::CoreApp;
|
2026-08-13 15:37:28 +08:00
|
|
|
use xianren_core::models as models_db;
|
|
|
|
|
use xianren_core::settings as settings_db;
|
2026-08-13 12:48:50 +08:00
|
|
|
use xianren_engine::EngineManager;
|
2026-08-13 15:37:28 +08:00
|
|
|
use std::path::PathBuf;
|
2026-08-13 12:48:50 +08:00
|
|
|
|
|
|
|
|
pub struct App {
|
|
|
|
|
pub core: Arc<CoreApp>,
|
|
|
|
|
pub engine: EngineManager,
|
|
|
|
|
pub engine_base: Arc<RwLock<Option<String>>>,
|
|
|
|
|
pub server: tokio::sync::Mutex<Option<xianren_api::ApiServer>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn run() {
|
|
|
|
|
tracing_subscriber::fmt()
|
|
|
|
|
.with_env_filter(
|
|
|
|
|
tracing_subscriber::EnvFilter::try_from_default_env()
|
|
|
|
|
.unwrap_or_else(|_| "info".into()),
|
|
|
|
|
)
|
|
|
|
|
.init();
|
|
|
|
|
|
|
|
|
|
tauri::Builder::default()
|
|
|
|
|
.plugin(tauri_plugin_opener::init())
|
|
|
|
|
.setup(|app| {
|
|
|
|
|
let core = CoreApp::init(None)
|
|
|
|
|
.map_err(|e| -> Box<dyn std::error::Error> { e.to_string().into() })?;
|
|
|
|
|
tauri::Manager::manage(
|
|
|
|
|
app,
|
|
|
|
|
App {
|
|
|
|
|
core: Arc::new(core),
|
|
|
|
|
engine: EngineManager::new(),
|
|
|
|
|
engine_base: Arc::new(RwLock::new(None)),
|
|
|
|
|
server: tokio::sync::Mutex::new(None),
|
|
|
|
|
},
|
|
|
|
|
);
|
2026-08-13 15:37:28 +08:00
|
|
|
|
|
|
|
|
// 启动时自动扫描模型目录
|
|
|
|
|
let app_state = app.state::<App>();
|
|
|
|
|
let db = app_state.core.db.lock().unwrap();
|
|
|
|
|
let model_dir = settings_db::get(&db, "model_dir")
|
|
|
|
|
.ok()
|
|
|
|
|
.flatten()
|
|
|
|
|
.map(PathBuf::from)
|
|
|
|
|
.unwrap_or_else(|| app_state.core.models_dir.clone());
|
|
|
|
|
match models_db::scan_directory(&db, &model_dir) {
|
|
|
|
|
Ok(result) => {
|
|
|
|
|
tracing::info!(
|
|
|
|
|
added = result.added,
|
|
|
|
|
updated = result.updated,
|
|
|
|
|
missing = result.missing,
|
|
|
|
|
"auto scan complete"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
tracing::warn!(error = %e, "auto scan failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let _ = app.emit("models://updated", ());
|
2026-08-13 12:48:50 +08:00
|
|
|
Ok(())
|
|
|
|
|
})
|
|
|
|
|
.invoke_handler(tauri::generate_handler![
|
|
|
|
|
commands::app_info,
|
|
|
|
|
commands::list_models,
|
|
|
|
|
commands::import_model,
|
|
|
|
|
commands::remove_model,
|
2026-08-13 15:37:28 +08:00
|
|
|
commands::scan_models,
|
|
|
|
|
commands::add_remote_model,
|
|
|
|
|
commands::search_models,
|
|
|
|
|
commands::list_model_files,
|
2026-08-13 12:48:50 +08:00
|
|
|
commands::settings_get,
|
|
|
|
|
commands::settings_set,
|
|
|
|
|
commands::list_conversations,
|
|
|
|
|
commands::create_conversation,
|
|
|
|
|
commands::delete_conversation,
|
|
|
|
|
commands::get_messages,
|
|
|
|
|
commands::engine_start,
|
|
|
|
|
commands::engine_stop,
|
|
|
|
|
commands::engine_status,
|
2026-08-13 15:55:09 +08:00
|
|
|
commands::deploy_model,
|
2026-08-13 12:48:50 +08:00
|
|
|
commands::chat_send,
|
|
|
|
|
commands::download_enqueue,
|
|
|
|
|
commands::server_start,
|
|
|
|
|
commands::server_stop,
|
|
|
|
|
commands::server_status,
|
|
|
|
|
commands::open_path,
|
|
|
|
|
])
|
|
|
|
|
.run(tauri::generate_context!())
|
|
|
|
|
.expect("error while running tauri application");
|
|
|
|
|
}
|