1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! Native Model Management
//!
//! Handles pull, delete, and refresh operations for native GGUF models.
//! These are async operations spawned as background tasks via `spawn_tracked()`.
use crate::core::backend::DownloadRequest;
use crate::core::models::find_model;
use crate::core::storage::{default_model_dir, HuggingFaceStorage};
use crate::provider::rig::StreamChunk;
use super::App;
impl App {
/// Pull a native model from HuggingFace.
///
/// Spawns a background task to download the model with progress updates
/// via the stream_chunk channel.
pub(crate) fn pull_native_model(&mut self, model: String) {
self.set_status(&format!("Starting pull: {}", model));
// Look up the model in KNOWN_MODELS
let known_model = match find_model(&model) {
Some(m) => m,
None => {
self.set_status(&format!("Unknown model: {}", model));
tracing::warn!("Unknown model ID: {}", model);
return;
}
};
// Clone data for the async task
let tx = self.stream_chunk_tx.clone();
let model_clone = model.clone();
let repo = known_model.hf_repo.to_string();
let filename = known_model.default_file.to_string();
// Send start event
let _ = tx.try_send(StreamChunk::NativeModelPullStarted {
model: model.clone(),
});
self.spawn_tracked(async move {
// Create storage
let storage = match HuggingFaceStorage::new(default_model_dir()) {
Ok(s) => s,
Err(e) => {
let _ = tx
.send(StreamChunk::NativeModelPullFailed {
model: model_clone,
error: e.to_string(),
})
.await;
return;
}
};
// Create download request
let request = DownloadRequest::huggingface(&repo, &filename);
// Clone tx for progress callback
let progress_tx = tx.clone();
let progress_model = model_clone.clone();
// Download with progress
let result = storage
.download(&request, move |progress| {
let _ = progress_tx.try_send(StreamChunk::NativeModelPullProgress {
model: progress_model.clone(),
status: progress.status.clone(),
completed: progress.completed,
total: progress.total,
});
})
.await;
match result {
Ok(download_result) => {
tracing::info!(
"Model {} downloaded to {:?} ({} bytes)",
model_clone,
download_result.path,
download_result.size
);
let _ = tx
.send(StreamChunk::NativeModelPulled {
model: model_clone,
path: download_result.path.display().to_string(),
size: download_result.size,
})
.await;
}
Err(e) => {
tracing::error!("Failed to pull model {}: {}", model_clone, e);
let _ = tx
.send(StreamChunk::NativeModelPullFailed {
model: model_clone,
error: e.to_string(),
})
.await;
}
}
});
}
/// Delete a native model from local storage.
pub(crate) fn delete_native_model(&mut self, model: String) {
self.set_status(&format!("Deleting: {}", model));
let tx = self.stream_chunk_tx.clone();
let model_clone = model.clone();
self.spawn_tracked(async move {
// Create storage
let storage = match HuggingFaceStorage::new(default_model_dir()) {
Ok(s) => s,
Err(e) => {
let _ = tx
.send(StreamChunk::NativeModelDeleteFailed {
model: model_clone,
error: e.to_string(),
})
.await;
return;
}
};
// Delete the model
match storage.delete(&model_clone) {
Ok(()) => {
tracing::info!("Deleted model: {}", model_clone);
let _ = tx
.send(StreamChunk::NativeModelDeleted { model: model_clone })
.await;
}
Err(e) => {
tracing::error!("Failed to delete model {}: {}", model_clone, e);
let _ = tx
.send(StreamChunk::NativeModelDeleteFailed {
model: model_clone,
error: e.to_string(),
})
.await;
}
}
});
}
/// Refresh the list of native models.
pub(crate) fn refresh_native_models(&mut self) {
self.set_status("Refreshing native models...");
let tx = self.stream_chunk_tx.clone();
self.spawn_tracked(async move {
// Create storage
let storage = match HuggingFaceStorage::new(default_model_dir()) {
Ok(s) => s,
Err(e) => {
tracing::error!("Failed to create storage for refresh: {}", e);
return;
}
};
// List models
match storage.list_models() {
Ok(models) => {
let count = models.len();
tracing::info!("Found {} native models", count);
let _ = tx.send(StreamChunk::NativeModelsRefreshed { count }).await;
}
Err(e) => {
tracing::error!("Failed to list native models: {}", e);
}
}
});
}
}