Files
openhuman/app/src-tauri/src/lib.rs
T
Steven EnamakelandGitHub 36fc5bdfa6 refactor(tauri): reduce desktop host to sidecar lifecycle + deep-link (#84)
* feat(onboarding): introduce DEV_FORCE_ONBOARDING flag to control onboarding flow

- Added DEV_FORCE_ONBOARDING configuration to bypass onboarding checks during development.
- Updated onboarding logic in AppRoutes to respect the new flag, ensuring onboarding is always shown when enabled.
- Introduced new utility functions for managing onboarding state based on the flag.
- Updated Cargo.lock and Cargo.toml to reflect dependency changes and version updates.
- Removed deprecated openhuman command module and refactored related functionalities into daemon_host module for better organization.

* feat(daemon): refactor daemon health monitoring and window management

- Updated DaemonHealthService to use polling via `openhuman.health_snapshot` instead of event listening for health updates.
- Introduced a new focusMainWindow function to centralize window focus logic across deep link handling and Tauri commands.
- Replaced `invoke` calls with direct window management methods for showing, hiding, and toggling the main window.
- Removed deprecated core_rpc module and related commands for improved organization and clarity.
- Updated Cargo.toml and Cargo.lock to reflect dependency changes.

* refactor(daemonHealthService): streamline imports and simplify tool object structure

- Moved the import of `callCoreRpc` to a more appropriate location in DaemonHealthService.
- Simplified the structure of the tools object in the aiGetConfig function for better readability.
- Ensured consistency in Tauri command invocations by removing unnecessary line breaks.
2026-03-30 11:41:29 -07:00

110 lines
3.7 KiB
Rust

#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
compile_error!("src-tauri host is desktop-only. Non-desktop targets are not supported.");
mod core_process;
use tauri::{Manager, RunEvent};
#[cfg(any(windows, target_os = "linux"))]
use tauri_plugin_deep_link::DeepLinkExt;
#[tauri::command]
fn core_rpc_url() -> String {
std::env::var("OPENHUMAN_CORE_RPC_URL")
.unwrap_or_else(|_| "http://127.0.0.1:7788/rpc".to_string())
}
fn is_daemon_mode() -> bool {
std::env::args().any(|arg| arg == "daemon" || arg == "--daemon")
}
pub fn run() {
let daemon_mode = is_daemon_mode();
let default_filter = std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string());
let _ = env_logger::Builder::new()
.parse_filters(&default_filter)
.try_init();
tauri::Builder::default()
.plugin(tauri_plugin_deep_link::init())
.setup(move |app| {
#[cfg(any(windows, target_os = "linux"))]
{
app.deep_link().register_all()?;
}
let core_run_mode = core_process::default_core_run_mode(daemon_mode);
let core_bin = if matches!(core_run_mode, core_process::CoreRunMode::ChildProcess) {
core_process::default_core_bin()
} else {
None
};
let core_handle = core_process::CoreProcessHandle::new(
core_process::default_core_port(),
core_bin,
core_run_mode,
);
std::env::set_var("OPENHUMAN_CORE_RPC_URL", core_handle.rpc_url());
app.manage(core_handle.clone());
tauri::async_runtime::spawn(async move {
if let Err(err) = core_handle.ensure_running().await {
log::error!("[core] failed to start core process: {err}");
} else {
log::info!("[core] core process ready");
}
});
if daemon_mode {
if let Some(window) = app.get_webview_window("main") {
let _ = window.hide();
}
}
Ok(())
})
.invoke_handler(tauri::generate_handler![core_rpc_url])
.build({
let mut context = tauri::generate_context!();
if daemon_mode {
context.config_mut().app.windows.clear();
}
context
})
.expect("error while building tauri application")
.run(move |app_handle, event| match event {
#[cfg(target_os = "macos")]
RunEvent::Reopen { .. } => {
if !daemon_mode {
if let Some(window) = app_handle.get_webview_window("main") {
let _ = window.show();
let _ = window.unminimize();
let _ = window.set_focus();
}
}
}
RunEvent::Exit => {
if let Some(core) = app_handle.try_state::<core_process::CoreProcessHandle>() {
let core = core.inner().clone();
tauri::async_runtime::block_on(async move {
core.shutdown().await;
});
}
}
_ => {}
});
}
pub fn run_core_from_args(args: &[String]) -> Result<(), String> {
let core_bin = crate::core_process::default_core_bin()
.ok_or_else(|| "openhuman core binary not found".to_string())?;
let status = std::process::Command::new(core_bin)
.args(args)
.status()
.map_err(|e| format!("failed to execute core binary: {e}"))?;
if !status.success() {
return Err(format!("core binary exited with status {status}"));
}
Ok(())
}