burncloud_service_models/
model.rs1use serde::{Deserialize, Serialize};
2use uuid::Uuid;
3use chrono::{DateTime, Utc};
4use std::collections::HashMap;
5
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
8pub enum ModelType {
9 Chat,
11 Code,
13 Text,
15 Embedding,
17 Multimodal,
19 ImageGeneration,
21 Speech,
23}
24
25#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
27pub enum ModelSize {
28 Small,
30 Medium,
32 Large,
34 XLarge,
36}
37
38#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
40pub enum ModelStatus {
41 Running,
43 Stopped,
45 Starting,
47 Stopping,
49 Error,
51 Downloading,
53 Installing,
55}
56
57#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct Model {
60 pub id: Uuid,
62 pub name: String,
64 pub display_name: String,
66 pub description: Option<String>,
68 pub version: String,
70 pub model_type: ModelType,
72 pub size_category: ModelSize,
74 pub file_size: u64,
76 pub provider: String,
78 pub license: Option<String>,
80 pub tags: Vec<String>,
82 pub languages: Vec<String>,
84 pub created_at: DateTime<Utc>,
86 pub updated_at: DateTime<Utc>,
88 pub file_path: Option<String>,
90 pub checksum: Option<String>,
92 pub download_url: Option<String>,
94 pub config: HashMap<String, serde_json::Value>,
96 pub rating: Option<f32>,
98 pub download_count: u64,
100 pub is_official: bool,
102}
103
104#[derive(Debug, Clone, Serialize, Deserialize)]
106pub struct InstalledModel {
107 #[serde(flatten)]
109 pub model: Model,
110 pub install_path: String,
112 pub installed_at: DateTime<Utc>,
114 pub status: ModelStatus,
116 pub port: Option<u16>,
118 pub process_id: Option<u32>,
120 pub last_used: Option<DateTime<Utc>>,
122 pub usage_count: u64,
124}
125
126#[derive(Debug, Clone, Serialize, Deserialize)]
128pub struct AvailableModel {
129 #[serde(flatten)]
131 pub model: Model,
132 pub is_installed: bool,
134 pub published_at: DateTime<Utc>,
136 pub last_updated: DateTime<Utc>,
138 pub system_requirements: SystemRequirements,
140}
141
142#[derive(Debug, Clone, Serialize, Deserialize)]
144pub struct SystemRequirements {
145 pub min_memory_gb: f32,
147 pub recommended_memory_gb: f32,
149 pub min_disk_space_gb: f32,
151 pub requires_gpu: bool,
153 pub supported_os: Vec<String>,
155 pub supported_architectures: Vec<String>,
157}
158
159impl Model {
160 pub fn new(
162 name: String,
163 display_name: String,
164 version: String,
165 model_type: ModelType,
166 provider: String,
167 file_size: u64,
168 ) -> Self {
169 let now = Utc::now();
170 Self {
171 id: Uuid::new_v4(),
172 name,
173 display_name,
174 description: None,
175 version,
176 model_type,
177 size_category: Self::calculate_size_category(file_size),
178 file_size,
179 provider,
180 license: None,
181 tags: Vec::new(),
182 languages: Vec::new(),
183 created_at: now,
184 updated_at: now,
185 file_path: None,
186 checksum: None,
187 download_url: None,
188 config: HashMap::new(),
189 rating: None,
190 download_count: 0,
191 is_official: false,
192 }
193 }
194
195 fn calculate_size_category(file_size: u64) -> ModelSize {
197 let size_gb = file_size as f64 / 1024.0 / 1024.0 / 1024.0;
198 match size_gb {
199 s if s < 3.0 => ModelSize::Small,
200 s if s < 8.0 => ModelSize::Medium,
201 s if s < 30.0 => ModelSize::Large,
202 _ => ModelSize::XLarge,
203 }
204 }
205
206 pub fn formatted_size(&self) -> String {
208 let size_gb = self.file_size as f64 / 1024.0 / 1024.0 / 1024.0;
209 format!("{:.1}GB", size_gb)
210 }
211}
212
213impl InstalledModel {
214 pub fn from_model(model: Model, install_path: String) -> Self {
216 Self {
217 model,
218 install_path,
219 installed_at: Utc::now(),
220 status: ModelStatus::Stopped,
221 port: None,
222 process_id: None,
223 last_used: None,
224 usage_count: 0,
225 }
226 }
227
228 pub fn mark_used(&mut self) {
230 self.last_used = Some(Utc::now());
231 self.usage_count += 1;
232 }
233
234 pub fn is_running(&self) -> bool {
236 matches!(self.status, ModelStatus::Running)
237 }
238}
239
240impl AvailableModel {
241 pub fn from_model(model: Model, system_requirements: SystemRequirements) -> Self {
243 Self {
244 is_installed: false,
245 published_at: model.created_at,
246 last_updated: model.updated_at,
247 system_requirements,
248 model,
249 }
250 }
251
252 pub fn is_compatible(&self, available_memory_gb: f32, os: &str, arch: &str) -> bool {
254 available_memory_gb >= self.system_requirements.min_memory_gb
255 && self.system_requirements.supported_os.iter().any(|supported_os| supported_os == os)
256 && self.system_requirements.supported_architectures.iter().any(|supported_arch| supported_arch == arch)
257 }
258}