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 #[cfg(any(
8 target_os = "macos",
9 target_os = "ios",
10 target_os = "android",
11 target_os = "windows"
12 ))]
13 use {
14 super::{resolved_model_config, SELECTED_MODEL},
15 ed_agent_tauri::EdState,
16 log::{error, info},
17 tauri::State,
18 };
19
20 /// Delete the locally cached weights for `model_id` (a HuggingFace repo id from
21 /// `chat_list_models`). Unloads the model first if it is currently loaded.
22 #[cfg(any(
23 target_os = "macos",
24 target_os = "ios",
25 target_os = "android",
26 target_os = "windows"
27 ))]
28 #[tauri::command]
29 pub async fn chat_remove_model(
30 state: State<'_, EdState>,
31 model_id: String,
32 ) -> Result<String, String> {
33 // If we're deleting the model that's currently loaded/selected, unload it
34 // first so the engine isn't left holding weights we're about to remove.
35 let is_selected = SELECTED_MODEL
36 .lock()
37 .map(|g| *g == model_id)
38 .unwrap_or(false);
39
40 if is_selected && state.ed().is_loaded().await {
41 let display_name = resolved_model_config().display_name();
42 // Ed reports the `Unloaded` transition.
43 state.ed().unload().await;
44 info!("Unloaded {display_name} before removing its weights.");
45 }
46
47 onde::hf_cache::delete_local_hf_model(model_id.clone()).map_err(|e| {
48 let msg = format!("Failed to remove model {model_id}: {e}");
49 error!("{msg}");
50 msg
51 })?;
52
53 let msg = format!("Removed downloaded weights for {model_id}.");
54 info!("{msg}");
55 Ok(msg)
56 }
57
58 #[cfg(not(any(
59 target_os = "macos",
60 target_os = "ios",
61 target_os = "android",
62 target_os = "windows"
63 )))]
64 #[tauri::command]
65 pub async fn chat_remove_model(_model_id: String) -> Result<String, String> {
66 log::debug!("Model management is not supported on this platform.");
67 Err("Model management is not supported on this platform.".to_string())
68 }