Skip to content

Commit 1f44bf9

Browse files
committed
restart server when llm file change
1 parent 6aeeebe commit 1f44bf9

File tree

10 files changed

+188
-26
lines changed

10 files changed

+188
-26
lines changed

Cargo.lock

Lines changed: 101 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/desktop/src-tauri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ tauri-plugin-autostart = "2"
5454
tauri-plugin-clipboard-manager = { workspace = true }
5555
tauri-plugin-deep-link = { workspace = true }
5656
tauri-plugin-dialog = { workspace = true }
57-
tauri-plugin-fs = "2"
57+
tauri-plugin-fs = { workspace = true, features = ["watch"] }
5858
tauri-plugin-global-shortcut = "2"
5959
tauri-plugin-http = { workspace = true }
6060
tauri-plugin-os = "2"

apps/desktop/src-tauri/capabilities/default.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@
6464
"identifier": "fs:allow-exists",
6565
"allow": [{ "path": "/Applications/*" }]
6666
},
67+
{
68+
"identifier": "fs:allow-watch",
69+
"allow": [
70+
{ "path": "$APPDATA/*" },
71+
{ "path": "$APPDATA/**" }
72+
]
73+
},
6774
{
6875
"identifier": "fs:allow-write-file",
6976
"allow": [

apps/desktop/src/routes/app.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import { commands as localLlmCommands } from "@hypr/plugin-local-llm";
12
import { createFileRoute, Outlet, useRouter } from "@tanstack/react-router";
3+
import { appDataDir, join } from "@tauri-apps/api/path";
24
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
5+
import { watch } from "@tauri-apps/plugin-fs";
36
import { useEffect, useState } from "react";
47

58
import { IndividualizationModal } from "@/components/individualization-modal";
@@ -54,6 +57,7 @@ function Component() {
5457
<LeftSidebarProvider>
5558
<RightPanelProvider>
5659
<AudioPermissions />
60+
<RestartLlmServer />
5761
<MainWindowStateEventSupport />
5862
<SettingsProvider>
5963
<NewNoteProvider>
@@ -105,6 +109,31 @@ function Component() {
105109
);
106110
}
107111

112+
function RestartLlmServer() {
113+
const watchLlm = async () => {
114+
const path = await appDataDir();
115+
const llmPath = await join(path, "llm.gguf");
116+
117+
return watch(llmPath, (_event) => {
118+
localLlmCommands.restartServer();
119+
}, { delayMs: 1000 });
120+
};
121+
122+
useEffect(() => {
123+
let unwatch: () => void;
124+
125+
watchLlm().then((f) => {
126+
unwatch = f;
127+
});
128+
129+
return () => {
130+
unwatch?.();
131+
};
132+
}, []);
133+
134+
return null;
135+
}
136+
108137
function AudioPermissions() {
109138
useEffect(() => {
110139
listenerCommands.checkMicrophoneAccess().then((isGranted) => {

plugins/local-llm/js/bindings.gen.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ async startServer() : Promise<string> {
2727
},
2828
async stopServer() : Promise<null> {
2929
return await TAURI_INVOKE("plugin:local-llm|stop_server");
30+
},
31+
async restartServer() : Promise<string> {
32+
return await TAURI_INVOKE("plugin:local-llm|restart_server");
3033
}
3134
}
3235

plugins/local-llm/src/commands.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,10 @@ pub async fn start_server<R: tauri::Runtime>(app: tauri::AppHandle<R>) -> Result
5050
pub async fn stop_server<R: tauri::Runtime>(app: tauri::AppHandle<R>) -> Result<(), String> {
5151
app.stop_server().await.map_err(|e| e.to_string())
5252
}
53+
54+
#[tauri::command]
55+
#[specta::specta]
56+
pub async fn restart_server<R: tauri::Runtime>(app: tauri::AppHandle<R>) -> Result<String, String> {
57+
app.stop_server().await.map_err(|e| e.to_string())?;
58+
app.start_server().await.map_err(|e| e.to_string())
59+
}

plugins/local-llm/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ fn make_specta_builder<R: tauri::Runtime>() -> tauri_specta::Builder<R> {
4848
commands::download_model::<Wry>,
4949
commands::start_server::<Wry>,
5050
commands::stop_server::<Wry>,
51+
commands::restart_server::<Wry>,
5152
])
5253
.error_handling(tauri_specta::ErrorHandlingMode::Throw)
5354
}
@@ -58,9 +59,12 @@ pub fn init<R: tauri::Runtime>() -> tauri::plugin::TauriPlugin<R> {
5859
tauri::plugin::Builder::new(PLUGIN_NAME)
5960
.invoke_handler(specta_builder.invoke_handler())
6061
.setup(|app, _api| {
61-
let model_path = app.path().app_data_dir().unwrap().join("llm.gguf");
62-
let state: SharedState = Arc::new(Mutex::new(State::new(model_path)));
63-
app.manage(state);
62+
{
63+
let model_path = app.path().app_data_dir().unwrap().join("llm.gguf");
64+
let state: SharedState = Arc::new(Mutex::new(State::new(model_path)));
65+
app.manage(state);
66+
}
67+
6468
Ok(())
6569
})
6670
.build()

plugins/local-stt/js/bindings.gen.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,23 @@ async isModelDownloading(model: SupportedModel) : Promise<boolean> {
2222
async downloadModel(model: SupportedModel, channel: TAURI_CHANNEL<number>) : Promise<null> {
2323
return await TAURI_INVOKE("plugin:local-stt|download_model", { model, channel });
2424
},
25-
async startServer() : Promise<string> {
26-
return await TAURI_INVOKE("plugin:local-stt|start_server");
27-
},
28-
async stopServer() : Promise<null> {
29-
return await TAURI_INVOKE("plugin:local-stt|stop_server");
25+
async listSupportedModels() : Promise<SupportedModel[]> {
26+
return await TAURI_INVOKE("plugin:local-stt|list_supported_models");
3027
},
3128
async getCurrentModel() : Promise<SupportedModel> {
3229
return await TAURI_INVOKE("plugin:local-stt|get_current_model");
3330
},
3431
async setCurrentModel(model: SupportedModel) : Promise<null> {
3532
return await TAURI_INVOKE("plugin:local-stt|set_current_model", { model });
3633
},
37-
async listSupportedModels() : Promise<SupportedModel[]> {
38-
return await TAURI_INVOKE("plugin:local-stt|list_supported_models");
34+
async startServer() : Promise<string> {
35+
return await TAURI_INVOKE("plugin:local-stt|start_server");
36+
},
37+
async stopServer() : Promise<null> {
38+
return await TAURI_INVOKE("plugin:local-stt|stop_server");
39+
},
40+
async restartServer() : Promise<string> {
41+
return await TAURI_INVOKE("plugin:local-stt|restart_server");
3942
}
4043
}
4144

plugins/local-stt/src/commands.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,6 @@ pub async fn download_model<R: tauri::Runtime>(
5454
.map_err(|e| e.to_string())
5555
}
5656

57-
#[tauri::command]
58-
#[specta::specta]
59-
pub async fn start_server<R: tauri::Runtime>(app: tauri::AppHandle<R>) -> Result<String, String> {
60-
app.start_server().await.map_err(|e| e.to_string())
61-
}
62-
63-
#[tauri::command]
64-
#[specta::specta]
65-
pub async fn stop_server<R: tauri::Runtime>(app: tauri::AppHandle<R>) -> Result<(), String> {
66-
app.stop_server().await.map_err(|e| e.to_string())
67-
}
68-
6957
#[tauri::command]
7058
#[specta::specta]
7159
pub fn get_current_model<R: tauri::Runtime>(
@@ -82,3 +70,22 @@ pub fn set_current_model<R: tauri::Runtime>(
8270
) -> Result<(), String> {
8371
app.set_current_model(model).map_err(|e| e.to_string())
8472
}
73+
74+
#[tauri::command]
75+
#[specta::specta]
76+
pub async fn start_server<R: tauri::Runtime>(app: tauri::AppHandle<R>) -> Result<String, String> {
77+
app.start_server().await.map_err(|e| e.to_string())
78+
}
79+
80+
#[tauri::command]
81+
#[specta::specta]
82+
pub async fn stop_server<R: tauri::Runtime>(app: tauri::AppHandle<R>) -> Result<(), String> {
83+
app.stop_server().await.map_err(|e| e.to_string())
84+
}
85+
86+
#[tauri::command]
87+
#[specta::specta]
88+
pub async fn restart_server<R: tauri::Runtime>(app: tauri::AppHandle<R>) -> Result<String, String> {
89+
app.stop_server().await.map_err(|e| e.to_string())?;
90+
app.start_server().await.map_err(|e| e.to_string())
91+
}

plugins/local-stt/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ fn make_specta_builder<R: tauri::Runtime>() -> tauri_specta::Builder<R> {
3838
commands::is_model_downloaded::<Wry>,
3939
commands::is_model_downloading::<Wry>,
4040
commands::download_model::<Wry>,
41-
commands::start_server::<Wry>,
42-
commands::stop_server::<Wry>,
41+
commands::list_supported_models,
4342
commands::get_current_model::<Wry>,
4443
commands::set_current_model::<Wry>,
45-
commands::list_supported_models,
44+
commands::start_server::<Wry>,
45+
commands::stop_server::<Wry>,
46+
commands::restart_server::<Wry>,
4647
])
4748
.events(tauri_specta::collect_events![
4849
events::RecordedProcessingEvent

0 commit comments

Comments
 (0)