release/windows-microsoft-store
rs 66 lines 2.18 KB
Raw
1 //! Tauri command `chat_remove_model`: deletes a downloaded model's weights.
2 //!
3 //! Removes the model's directory from the local HuggingFace cache to free disk
4 //! space. If the model being removed is the one currently loaded in memory, it
5 //! is unloaded first so the engine doesn't keep a now-orphaned model resident.
6
7 use tauri::AppHandle;
8
9 #[cfg(any(
10 target_os = "macos",
11 target_os = "ios",
12 target_os = "android",
13 target_os = "windows"
14 ))]
15 use {
16 super::{emit_chat_status, resolved_model_config, ENGINE, SELECTED_MODEL},
17 crate::constants::ChatStatus,
18 log::{error, info},
19 };
20
21 /// Delete the locally cached weights for `model_id` (a HuggingFace repo id from
22 /// `chat_list_models`). Unloads the model first if it is currently loaded.
23 #[cfg(any(
24 target_os = "macos",
25 target_os = "ios",
26 target_os = "android",
27 target_os = "windows"
28 ))]
29 #[tauri::command]
30 pub async fn chat_remove_model(app: AppHandle, model_id: String) -> Result<String, String> {
31 // If we're deleting the model that's currently loaded/selected, unload it
32 // first so the engine isn't left holding weights we're about to remove.
33 let is_selected = SELECTED_MODEL
34 .lock()
35 .map(|g| *g == model_id)
36 .unwrap_or(false);
37
38 if is_selected && ENGINE.is_loaded().await {
39 let display_name = resolved_model_config().display_name();
40 ENGINE.unload_model().await;
41 info!("Unloaded {display_name} before removing its weights.");
42 emit_chat_status(&app, ChatStatus::Unloaded, None, None);
43 }
44
45 onde::hf_cache::delete_local_hf_model(model_id.clone()).map_err(|e| {
46 let msg = format!("Failed to remove model {model_id}: {e}");
47 error!("{msg}");
48 msg
49 })?;
50
51 let msg = format!("Removed downloaded weights for {model_id}.");
52 info!("{msg}");
53 Ok(msg)
54 }
55
56 #[cfg(not(any(
57 target_os = "macos",
58 target_os = "ios",
59 target_os = "android",
60 target_os = "windows"
61 )))]
62 #[tauri::command]
63 pub async fn chat_remove_model(_app: AppHandle, _model_id: String) -> Result<String, String> {
64 log::debug!("Model management is not supported on this platform.");
65 Err("Model management is not supported on this platform.".to_string())
66 }