Skip to content

Commit

Permalink
- fix: process exit in windows (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
agallardol authored Oct 17, 2024
1 parent 6bcb620 commit f995504
Show file tree
Hide file tree
Showing 15 changed files with 177 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::globals::SHINKAI_NODE_MANAGER_INSTANCE;
use crate::local_shinkai_node::process_handlers::logger::LogEntry;
use crate::local_shinkai_node::shinkai_node_options::ShinkaiNodeOptions;
use crate::windows::{show_or_recreate_window, Window};
use crate::windows::{recreate_window, Window};

#[tauri::command]
pub async fn show_shinkai_node_manager_window(app_handle: tauri::AppHandle) {
show_or_recreate_window(app_handle, Window::ShinkaiNodeManager);
recreate_window(app_handle, Window::ShinkaiNodeManager, true);
}

#[tauri::command]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use tauri::{Emitter, Manager};
use tauri_plugin_global_shortcut::{Shortcut, ShortcutEvent};

use crate::windows::{show_or_recreate_window, Window};
use crate::windows::{recreate_window, Window};

pub fn create_chat(app: &tauri::AppHandle, _: Shortcut, _: ShortcutEvent) {
show_or_recreate_window(app.clone(), Window::Main);
recreate_window(app.clone(), Window::Main, true);
if let Some(window) = app.get_webview_window("main") {
if let Err(e) = app.emit("create-chat", ()) {
log::error!("failed to emit 'create-chat': {}", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use tauri::Manager;
use tauri_plugin_global_shortcut::{Shortcut, ShortcutEvent};

use crate::windows::{show_or_recreate_window, Window};
use crate::windows::{recreate_window, Window};

pub fn toggle_spotlight(app: &tauri::AppHandle, _: Shortcut, _: ShortcutEvent) {
log::info!("toggling spotlight window");
if let Some(spotlight_window) = app.get_webview_window(Window::Spotlight.as_str()) {
if spotlight_window.is_visible().unwrap_or(false) && spotlight_window.is_focused().unwrap_or(false) {
let _ = spotlight_window.hide();
return;
}
}
show_or_recreate_window(app.clone(), Window::Spotlight)
recreate_window(app.clone(), Window::Spotlight, true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl ProcessHandler {
}
CommandEvent::Terminated(payload) => {
line = format!(
"Shinkai Node process terminated with code:{:?} and signal:{:?}",
"process terminated with code:{:?} and signal:{:?}",
payload.code, payload.signal
);
}
Expand Down
33 changes: 28 additions & 5 deletions apps/shinkai-desktop/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ use globals::SHINKAI_NODE_MANAGER_INSTANCE;
use local_shinkai_node::shinkai_node_manager::ShinkaiNodeManager;
use tauri::{Emitter, WindowEvent};
use tauri::{Manager, RunEvent};
use tauri_plugin_log::LogLevel;
use tokio::sync::Mutex;
use tray::create_tray;
use windows::Window;
use windows::{recreate_window, Window};

mod audio;
mod commands;
Expand All @@ -40,10 +41,14 @@ struct Payload {
}

fn main() {
log::info!("startin app version: {}", env!("CARGO_PKG_VERSION"));
tauri::Builder::default()
.plugin(tauri_plugin_log::Builder::new().build())
.plugin(
tauri_plugin_log::Builder::new()
.level(log::LevelFilter::Info)
.build(),
)
.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_log::Builder::new().build())
.plugin(tauri_plugin_updater::Builder::new().build())
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_fs::init())
Expand Down Expand Up @@ -99,12 +104,32 @@ fn main() {
)));
}

create_tray(app.handle())?;

/*
This is the initialization pipeline
At some point we will need to add a UI because some tasks can be hard/slow to execute
*/
tauri::async_runtime::spawn({
let app_handle = app.handle().clone();
async move {
// Kill any existing process related to shinkai and/or using shinkai ports
let mut shinkai_node_manager_guard =
SHINKAI_NODE_MANAGER_INSTANCE.get().unwrap().lock().await;
shinkai_node_manager_guard.kill().await;
drop(shinkai_node_manager_guard);

recreate_window(app_handle.clone(), Window::Coordinator, false);
recreate_window(app_handle.clone(), Window::Spotlight, false);
recreate_window(app_handle.clone(), Window::Main, true);
}
});

tauri::async_runtime::spawn({
let app_handle = app.handle().clone();
async move {
let mut shinkai_node_manager_guard =
SHINKAI_NODE_MANAGER_INSTANCE.get().unwrap().lock().await;
let mut receiver = shinkai_node_manager_guard.subscribe_to_events();
drop(shinkai_node_manager_guard);
while let Ok(state_change) = receiver.recv().await {
Expand All @@ -117,8 +142,6 @@ fn main() {
}
});

create_tray(app.handle())?;

Ok(())
})
.build(tauri::generate_context!())
Expand Down
8 changes: 4 additions & 4 deletions apps/shinkai-desktop/src-tauri/src/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tauri::{

use crate::{
globals::SHINKAI_NODE_MANAGER_INSTANCE,
windows::{show_or_recreate_window, Window},
windows::{recreate_window, Window},
};

pub fn create_tray(app: &tauri::AppHandle) -> tauri::Result<()> {
Expand Down Expand Up @@ -38,10 +38,10 @@ pub fn create_tray(app: &tauri::AppHandle) -> tauri::Result<()> {
.menu(&menu)
.on_menu_event(move |tray, event| match event.id().as_ref() {
"show" => {
show_or_recreate_window(tray.app_handle().clone(), Window::Main);
recreate_window(tray.app_handle().clone(), Window::Main, true);
}
"open_shinkai_node_manager_window" => {
show_or_recreate_window(tray.app_handle().clone(), Window::ShinkaiNodeManager);
recreate_window(tray.app_handle().clone(), Window::ShinkaiNodeManager, true);
}
"quit" => {
let app_handle = tray.app_handle().clone();
Expand All @@ -52,7 +52,7 @@ pub fn create_tray(app: &tauri::AppHandle) -> tauri::Result<()> {
if shinkai_node_manager_guard.is_running().await {
shinkai_node_manager_guard.kill().await;
}
app_handle.exit(0);
std::process::exit(0);
});
}
_ => (),
Expand Down
14 changes: 9 additions & 5 deletions apps/shinkai-desktop/src-tauri/src/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use tauri::{AppHandle, Manager, WebviewWindowBuilder};
use tauri::{AppHandle, Listener, Manager, WebviewWindowBuilder};

#[derive(Debug, Clone, Copy)]
pub enum Window {
Main,
ShinkaiNodeManager,
Spotlight,
Coordinator,
}

impl Window {
Expand All @@ -13,17 +14,20 @@ impl Window {
Window::Main => "main",
Window::ShinkaiNodeManager => "shinkai-node-manager",
Window::Spotlight => "spotlight",
Window::Coordinator => "coordinator",
}
}
}

pub fn show_or_recreate_window(app_handle: AppHandle, window_name: Window) {
pub fn recreate_window(app_handle: AppHandle, window_name: Window, focus: bool) {
let label = window_name.as_str();
if let Some(window) = app_handle.get_webview_window(label) {
log::info!("window {} found, bringing to front", label);
window.show().unwrap();
window.center().unwrap();
let _ = window.set_focus();
if focus {
window.show().unwrap();
window.center().unwrap();
let _ = window.set_focus();
}
} else {
log::info!("window {} not found, recreating...", label);
let window_config = app_handle
Expand Down
9 changes: 5 additions & 4 deletions apps/shinkai-desktop/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"app": {
"withGlobalTauri": false,
"security": {
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost; cross-origin-embedder-policy 'require-corp'; cross-origin-opener-policy 'same-origin'"
"csp": null
},
"windows": [
{
Expand All @@ -53,7 +53,8 @@
"resizable": true,
"dragDropEnabled": false,
"titleBarStyle": "Overlay",
"hiddenTitle": true
"hiddenTitle": true,
"create": false
},
{
"fullscreen": false,
Expand All @@ -74,7 +75,7 @@
"visible": false,
"closable": false,
"parent": null,
"create": true
"create": false
},
{
"fullscreen": false,
Expand All @@ -95,7 +96,7 @@
"visible": false,
"closable": false,
"parent": null,
"create": true
"create": false
},
{
"label": "shinkai-node-manager",
Expand Down
5 changes: 5 additions & 0 deletions apps/shinkai-desktop/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { I18nProvider } from '@shinkai_network/shinkai-i18n';
import { QueryProvider } from '@shinkai_network/shinkai-node-state';
import { Toaster } from '@shinkai_network/shinkai-ui';
import { info } from '@tauri-apps/plugin-log';
import { useEffect } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
import { BrowserRouter as Router } from 'react-router-dom';

Expand All @@ -10,6 +12,9 @@ import AppRoutes from './routes';
import { useSyncStorageSecondary } from './store/sync-utils';

function App() {
useEffect(() => {
info('initializing main');
}, []);
useSyncStorageSecondary();
return (
<I18nProvider>
Expand Down
2 changes: 2 additions & 0 deletions apps/shinkai-desktop/src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useGetLLMProviders } from '@shinkai_network/shinkai-node-state/v2/queries/getLLMProviders/useGetLLMProviders';
import { listen } from '@tauri-apps/api/event';
import { debug } from '@tauri-apps/plugin-log';
import React, { useEffect, useRef } from 'react';
import { Navigate, Outlet, Route, Routes, useNavigate } from 'react-router-dom';

Expand Down Expand Up @@ -80,6 +81,7 @@ const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
Node auto start process probably should be in rust side
*/
useEffect(() => {
debug(`initializing autoStartShinkaiNodeTried.current:${autoStartShinkaiNodeTried.current} isInUse:${isInUse} shinkaiNodeIsRunning:${shinkaiNodeIsRunning}`);
if (
!autoStartShinkaiNodeTried.current &&
isInUse &&
Expand Down
Loading

0 comments on commit f995504

Please sign in to comment.