amberjs 1.16.1

JavaScript/TypeScript runtime built with Rust and V8
Documentation
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// 多模型管理系统
// 实现多模型并行和动态切换系统,包括模型注册、智能路由和负载均衡

use std::collections::{BTreeMap, HashMap};
use std::sync::{atomic::Ordering, Arc, Mutex, RwLock};
use std::time::{Duration, Instant};

/// 模型管理器配置
#[derive(Debug, Clone)]
pub struct ManagerConfig {
    pub max_concurrent_models: usize,
    pub model_timeout: Duration,
    pub enable_auto_scaling: bool,
}
/// 模型信息
#[derive(Debug, Clone)]
pub struct ModelInfo {
    pub name: String,
    pub version: String,
    pub model_type: String, // 简化为字符串类型
    pub endpoint: String,
    pub capabilities: Vec<String>,
}
/// 模型注册中心
pub struct ModelRegistry {
    config: ModelRegistryConfig,
    registered_models: Arc<RwLock<HashMap<String, ModelInfo>>>,
    health_status: Arc<RwLock<HashMap<String, bool>>>,
}
/// 模型注册配置
#[derive(Debug, Clone)]
pub struct ModelRegistryConfig {
    pub auto_discovery: bool,
    pub health_check_interval: Duration,
}
/// 模型路由器
pub struct ModelRouter {
    config: RouterConfig,
    model_metrics: Arc<RwLock<HashMap<String, ModelMetrics>>>,
    route_cache: Arc<RwLock<HashMap<String, (String, Instant)>>>,
}
/// 路由器配置
#[derive(Debug, Clone)]
pub struct RouterConfig {
    pub load_balancing: LoadBalancingStrategy,
    pub fallback_enabled: bool,
    pub route_cache_ttl: Duration,
}
/// 负载均衡策略
#[derive(Debug, Clone, PartialEq)]
pub enum LoadBalancingStrategy {
    RoundRobin,
    LeastConnections,
    WeightedRoundRobin,
    LatencyBased,
}
/// 模型指标
#[derive(Debug, Clone)]
pub struct ModelMetrics {
    pub latency: Duration,
    pub throughput: f64,
    pub error_rate: f64,
    pub load: f64,
}
/// 模型管理器
pub struct ModelManager {
    runtime: Arc<tokio::runtime::Runtime>,
    config: ManagerConfig,
    registry: ModelRegistry,
    router: ModelRouter,
    active_models: Arc<RwLock<HashMap<String, ModelHandle>>>,
    model_handles: Arc<Mutex<Vec<ModelHandle>>>,
}
/// 模型句柄
#[derive(Debug, Clone)]
pub struct ModelHandle {
    pub model_name: String,
    pub handle_id: String,
    pub load_time: Instant,
    pub last_used: Instant,
}
impl ModelRegistry {
    /// 创建新的模型注册中心
    pub fn new(config: ModelRegistryConfig) -> Result<Self, String> {
        Ok(ModelRegistry {
            config: config.clone(),
            registered_models: Arc::new(RwLock::new(HashMap::new())),
            health_status: Arc::new(RwLock::new(HashMap::new())),
        })
    }
    /// 注册模型
    pub fn register_model(&mut self, model_info: ModelInfo) -> Result<(), String> {
        let mut models = self.registered_models.write().unwrap();
        models.insert(model_info.name.clone(), model_info.clone());
        // 初始化健康状态
        let mut health = self.health_status.write().unwrap();
        health.insert(model_info.name.clone(), true);
        Ok(())
    }
    /// 发现模型
    pub fn discover_models(&self) -> Result<Vec<String>, String> {
        let models: _ = self.registered_models.read().unwrap();
        Ok(models.keys().cloned().collect())
    }
    /// 获取模型信息
    pub fn get_model_info(&self, model_name: &str) -> Option<ModelInfo> {
        let models: _ = self.registered_models.read().unwrap();
        models.get(model_name).cloned()
    }
    /// 检查模型健康状态
    pub fn is_healthy(&self, model_name: &str) -> bool {
        let health: _ = self.health_status.read().unwrap();
        health.get(model_name).copied().unwrap_or(false)
    }
    /// 标记模型为不健康
    pub fn mark_unhealthy(&self, model_name: &str) {
        let mut health = self.health_status.write().unwrap();
        health.insert(model_name.to_string(), false);
    }
    /// 标记模型为健康
    pub fn mark_healthy(&self, model_name: &str) {
        let mut health = self.health_status.write().unwrap();
        health.insert(model_name.to_string(), true);
    }
}
impl ModelRouter {
    /// 创建新的模型路由器
    pub fn new(config: RouterConfig) -> Result<Self, String> {
        Ok(ModelRouter {
            config: config.clone(),
            model_metrics: Arc::new(RwLock::new(HashMap::new())),
            route_cache: Arc::new(RwLock::new(HashMap::new())),
        })
    }
    /// 添加模型指标
    pub fn add_model(&self, model_name: String, metrics: ModelMetrics) {
        let mut model_metrics = self.model_metrics.write().unwrap();
        model_metrics.insert(model_name, metrics);
    }
    /// 路由请求
    pub fn route_request(&self, capability: String) -> Result<String, String> {
        // 检查缓存
        let cache_key: _ = format!("route:{}", capability);
        if let Some((cached_model, cached_time)) = self.route_cache.read().unwrap().get(&cache_key)
        {
            if cached_time.elapsed() < self.config.route_cache_ttl {
                return Ok(cached_model.clone());
            }
        }
        // 获取可用模型
        let model_metrics: _ = self.model_metrics.read().unwrap();
        let available_models: Vec<_> = model_metrics
            .iter()
            .filter(|(_, metrics)| metrics.load < 0.9 && metrics.error_rate < 0.1)
            .collect();
        if available_models.is_empty() {
            return Err("No available models".to_string());
        }
        // 根据负载均衡策略选择模型
        let model_name: _ = self.select_model(&available_models)?;
        // 更新缓存
        {
            let mut cache = self.route_cache.write().unwrap();
            cache.insert(cache_key, (model_name.clone(), Instant::now()));
        }
        Ok(model_name)
    }
    /// 选择模型 (返回模型名称而不是引用)
    fn select_model(&self, models: &[(&String, &ModelMetrics)]) -> Result<String, String> {
        match self.config.load_balancing {
            LoadBalancingStrategy::RoundRobin => {
                // 简化实现:选择第一个
                Ok(models[0].0.clone())
            }
            LoadBalancingStrategy::LeastConnections => {
                // 选择负载最低的 (简化比较)
                Ok(models
                    .iter()
                    .min_by(|(_, a), (_, b)| {
                        a.load
                            .partial_cmp(&b.load)
                            .unwrap_or(std::cmp::Ordering::Equal)
                    })
                    .map(|(name, _)| (*name).clone())
                    .unwrap())
            }
            LoadBalancingStrategy::WeightedRoundRobin => {
                // 选择吞吐量最高的 (简化比较)
                Ok(models
                    .iter()
                    .max_by(|(_, a), (_, b)| {
                        a.throughput
                            .partial_cmp(&b.throughput)
                            .unwrap_or(std::cmp::Ordering::Equal)
                    })
                    .map(|(name, _)| (*name).clone())
                    .unwrap())
            }
            LoadBalancingStrategy::LatencyBased => {
                // 选择延迟最低的 (简化比较)
                Ok(models
                    .iter()
                    .min_by(|(_, a), (_, b)| a.latency.cmp(&b.latency))
                    .map(|(name, _)| (*name).clone())
                    .unwrap())
            }
        }
    }
    /// 获取路由准确率
    pub fn get_route_accuracy(&self) -> f64 {
        let cache: _ = self.route_cache.read().unwrap();
        if cache.is_empty() {
            return 0.0;
        }
        // 简化实现:基于缓存命中率
        let valid_entries: _ = cache
            .values()
            .filter(|(_, time)| time.elapsed() < self.config.route_cache_ttl)
            .count();
        valid_entries as f64 / cache.len() as f64
    }
}
impl ModelManager {
    /// 创建新的模型管理器
    pub fn new(
        runtime: &Arc<tokio::runtime::Runtime>,
        config: ManagerConfig,
    ) -> Result<Self, String> {
        let registry_config: _ = ModelRegistryConfig {
            auto_discovery: true,
            health_check_interval: Duration::from_secs(30),
        };
        let router_config: _ = RouterConfig {
            load_balancing: LoadBalancingStrategy::RoundRobin,
            fallback_enabled: true,
            route_cache_ttl: Duration::from_secs(60),
        };
        let registry: _ = ModelRegistry::new(registry_config)?;
        let router: _ = ModelRouter::new(router_config)?;
        Ok(ModelManager {
            runtime: runtime.clone(),
            config: config.clone(),
            registry,
            router,
            active_models: Arc::new(RwLock::new(HashMap::new())),
            model_handles: Arc::new(Mutex::new(Vec::new())),
        })
    }
    /// 加载模型
    pub fn load_model(&mut self, model_name: String) -> Result<ModelHandle, String> {
        // 检查并发限制
        {
            let handles: _ = self.model_handles.lock().unwrap();
            if handles.len() >= self.config.max_concurrent_models {
                return Err("Max concurrent models reached".to_string());
            }
        }
        // 检查模型是否已加载
        {
            let active_models: _ = self.active_models.read().unwrap();
            if let Some(handle) = active_models.get(&model_name) {
                return Ok(handle.clone());
            }
        }
        // 模拟模型加载
        let load_start: _ = Instant::now();
        std::thread::sleep(Duration::from_millis(100)); // 模拟加载时间
        let load_time: _ = load_start.elapsed();
        // 创建模型句柄
        let handle: _ = ModelHandle {
            model_name: model_name.clone(),
            handle_id: format!("{}-{}", model_name, load_time.as_nanos()),
            load_time: Instant::now(),
            last_used: Instant::now(),
        };
        // 注册模型指标
        let metrics: _ = ModelMetrics {
            latency: Duration::from_millis(50),
            throughput: 100.0,
            error_rate: 0.01,
            load: 0.0,
        };
        self.router.add_model(model_name.clone(), metrics);
        // 添加到活跃模型
        {
            let mut active_models = self.active_models.write().unwrap();
            active_models.insert(model_name.clone(), handle.clone());
        }
        {
            let mut handles = self.model_handles.lock().unwrap();
            handles.push(handle.clone());
        }
        Ok(handle)
    }
    /// 执行推理
    pub fn inference(&self, handle: &ModelHandle, input: String) -> Result<String, String> {
        // 更新最后使用时间
        {
            let mut active_models = self.active_models.write().unwrap();
            if let Some(model_handle) = active_models.get_mut(&handle.model_name) {
                model_handle.last_used = Instant::now();
            }
        }
        // 模拟推理
        let inference_start: _ = Instant::now();
        std::thread::sleep(Duration::from_millis(10)); // 模拟推理时间
        let inference_time: _ = inference_start.elapsed();
        // 更新模型指标
        let metrics: _ = ModelMetrics {
            latency: inference_time,
            throughput: 100.0,
            error_rate: 0.01,
            load: 0.5,
        };
        self.router.add_model(handle.model_name.clone(), metrics);
        Ok(format!("Inference result for: {}", input))
    }
    /// 获取模型指标
    pub fn get_model_metrics(&self, model_name: &str) -> Result<ModelMetrics, String> {
        let model_metrics: _ = self.router.model_metrics.read().unwrap();
        if let Some(metrics) = model_metrics.get(model_name) {
            Ok(metrics.clone())
        } else {
            Err("Model not found".to_string())
        }
    }
    /// 清理空闲模型
    pub fn cleanup_idle_models(&self) {
        let now: _ = Instant::now();
        let timeout: _ = self.config.model_timeout;
        let mut to_remove = Vec::new();
        {
            let active_models: _ = self.active_models.read().unwrap();
            for (model_name, handle) in active_models.iter() {
                if now.duration_since(handle.last_used) > timeout {
                    to_remove.push(model_name.clone());
                }
            }
        }
        for model_name in to_remove {
            self.unload_model(&model_name);
        }
    }
    /// 卸载模型
    pub fn unload_model(&self, model_name: &str) {
        // 从活跃模型中移除
        {
            let mut active_models = self.active_models.write().unwrap();
            active_models.remove(model_name);
        }
        // 从句柄列表中移除
        {
            let mut handles = self.model_handles.lock().unwrap();
            handles.retain(|h| h.model_name != model_name);
        }
        // 标记为不健康
        self.registry.mark_unhealthy(model_name);
    }
    /// 强制清理
    pub fn force_cleanup(&self) {
        let mut active_models = self.active_models.write().unwrap();
        active_models.clear();
        let mut handles = self.model_handles.lock().unwrap();
        handles.clear();
    }
    /// 注册模型到管理器
    pub fn register_model(&mut self, model_info: ModelInfo) -> Result<(), String> {
        self.registry.register_model(model_info)
    }
    /// 路由请求
    pub fn route_request(&self, capability: String) -> Result<String, String> {
        self.router.route_request(capability)
    }
}
#[cfg(test)]
mod tests {
    #[test]
    fn test_model_registry_creation() {
        let config: _ = ModelRegistryConfig {
            auto_discovery: true,
            health_check_interval: Duration::from_secs(30),
        };
        let registry: _ = ModelRegistry::new(config);
        assert!(registry.is_ok());
    }
    #[test]
    fn test_model_registration() {
        let mut registry = ModelRegistry::new(ModelRegistryConfig {
            auto_discovery: true,
            health_check_interval: Duration::from_secs(30),
        })
        .unwrap();
        let model_info: _ = ModelInfo {
            name: "test-model".to_string(),
            version: "1.0".to_string(),
            model_type: "LanguageModel".to_string(), // 简化为字符串
            endpoint: "http://localhost:8080".to_string(),
            capabilities: vec!["text-generation".to_string()],
        };
        let result: _ = registry.register_model(model_info);
        assert!(result.is_ok());
    }
    #[test]
    fn test_model_router_creation() {
        let config: _ = RouterConfig {
            load_balancing: LoadBalancingStrategy::RoundRobin,
            fallback_enabled: true,
            route_cache_ttl: Duration::from_secs(60),
        };
        let router: _ = ModelRouter::new(config);
        assert!(router.is_ok());
    }
    #[test]
    fn test_intelligent_routing() {
        let router: _ = ModelRouter::new(RouterConfig {
            load_balancing: LoadBalancingStrategy::LatencyBased,
            fallback_enabled: true,
            route_cache_ttl: Duration::from_secs(60),
        })
        .unwrap();
        router.add_model(
            "model-a".to_string(),
            ModelMetrics {
                latency: Duration::from_millis(50),
                throughput: 100.0,
                error_rate: 0.01,
                load: 0.3,
            },
        );
        router.add_model(
            "model-b".to_string(),
            ModelMetrics {
                latency: Duration::from_millis(80),
                throughput: 150.0,
                error_rate: 0.02,
                load: 0.5,
            },
        );
        let selected: _ = router.route_request("text-generation".to_string());
        assert!(selected.is_ok());
        assert_eq!(selected.unwrap(), "model-a".to_string());
    }
    #[test]
    fn test_model_manager_creation() {
        let runtime: _ = Arc::new(std::sync::Mutex::new(tokio::runtime::Runtime::new()).unwrap());
        let config: _ = ManagerConfig {
            max_concurrent_models: 10,
            model_timeout: Duration::from_secs(300),
            enable_auto_scaling: true,
        };
        let manager: _ = ModelManager::new(&runtime, config);
        assert!(manager.is_ok());
    }
    #[test]
    fn test_model_loading() {
        let runtime: _ = Arc::new(std::sync::Mutex::new(tokio::runtime::Runtime::new()).unwrap());
        let config: _ = ManagerConfig {
            max_concurrent_models: 10,
            model_timeout: Duration::from_secs(300),
            enable_auto_scaling: true,
        };
        let mut manager = ModelManager::new(&runtime, config).unwrap();
        let handle: _ = manager.load_model("test-model".to_string());
        assert!(handle.is_ok());
    }
    #[test]
    fn test_inference() {
        let runtime: _ = Arc::new(std::sync::Mutex::new(tokio::runtime::Runtime::new()).unwrap());
        let config: _ = ManagerConfig {
            max_concurrent_models: 10,
            model_timeout: Duration::from_secs(300),
            enable_auto_scaling: true,
        };
        let mut manager = ModelManager::new(&runtime, config).unwrap();
        let handle: _ = manager.load_model("test-model".to_string()).unwrap();
        let result: _ = manager.inference(&handle, "Test input".to_string());
        assert!(result.is_ok());
        assert!(result.unwrap().contains("Test input"));
    }
    #[test]
    fn test_model_cleanup() {
        let runtime: _ = Arc::new(std::sync::Mutex::new(tokio::runtime::Runtime::new()).unwrap());
        let config: _ = ManagerConfig {
            max_concurrent_models: 10,
            model_timeout: Duration::from_millis(100), // 短超时
            enable_auto_scaling: true,
        };
        let mut manager = ModelManager::new(&runtime, config).unwrap();
        // 加载模型
        manager.load_model("test-model".to_string()).unwrap();
        // 等待超时
        std::thread::sleep(Duration::from_millis(200));
        // 清理空闲模型
        manager.cleanup_idle_models();
        // 验证模型已清理
        let active_models: _ = manager.active_models.read().unwrap();
        assert!(!active_models.contains_key("test-model"));
    }
}