vecboost 0.2.0

High-performance embedding vector service written in Rust
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
500
501
502
503
504
505
506
507
508
509
510
// Copyright (c) 2025-2026 Kirky.X
//
// Licensed under the MIT License
// See LICENSE file in the project root for full license information.

//! ModuleMeta + AsyncAutoBuilder trait 实现
//!
//! 基于 trait-kit 0.3 的 `AsyncKit`(`Send + Sync`,基于 `Arc<RwLock>`)。
//! 所有模块实现 `AsyncAutoBuilder`,`build` 返回 `Pin<Box<dyn Future>>`,
//! 在 `AsyncKit::build().await` 时按拓扑序异步构造。

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use tokio::sync::RwLock;
use trait_kit::AsyncKit;
use trait_kit::prelude::*;

#[cfg(feature = "auth")]
use super::AuthModule;
use super::RateLimitModule;
use super::{
    AuditModule, AuthEnabled, AuthEnabledModule, CacheConfig, CacheModule, DbConfig, DbModule,
    EmbeddingModule, IpWhitelistModule, MetricsCollectorModule, PipelineEnabled,
    PipelineEnabledModule, PipelineQueueModule, PriorityCalculatorModule,
    PrometheusCollectorModule, RateLimitEnabled, RateLimitEnabledModule, ResponseChannelModule,
    WorkerManagerModule,
};
#[cfg(feature = "auth")]
use super::{CsrfConfigModule, CsrfTokenStoreModule, UserStoreModule};
use crate::audit::AuditLogger;
#[cfg(feature = "auth")]
use crate::auth::{CsrfConfig, CsrfTokenStore, UserStore};
use crate::metrics::PrometheusCollector;
use crate::rate_limit::LimiteronAdapter;
use crate::service::embedding::EmbeddingService;
use crate::{
    metrics::InferenceCollector,
    pipeline::{PriorityCalculator, PriorityRequestQueue, ResponseChannel, WorkerManager},
};

// ---------------------------------------------------------------------------
// EmbeddingModule
// ---------------------------------------------------------------------------

impl ModuleMeta for EmbeddingModule {
    const NAME: &'static str = "embedding";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

impl AsyncAutoBuilder for EmbeddingModule {
    type Capability = Arc<RwLock<EmbeddingService>>;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move { kit.config::<Self::Capability>() })
    }
}

// ---------------------------------------------------------------------------
// AuthModule (auth feature only)
// ---------------------------------------------------------------------------

#[cfg(feature = "auth")]
impl ModuleMeta for AuthModule {
    const NAME: &'static str = "auth";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

#[cfg(feature = "auth")]
impl AsyncAutoBuilder for AuthModule {
    type Capability = Option<Arc<crate::auth::JwtManager>>;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move { kit.config::<Self::Capability>() })
    }
}

// ---------------------------------------------------------------------------
// RateLimitModule
// ---------------------------------------------------------------------------

impl ModuleMeta for RateLimitModule {
    const NAME: &'static str = "rate_limit";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

impl AsyncAutoBuilder for RateLimitModule {
    type Capability = Arc<LimiteronAdapter>;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move { kit.config::<Self::Capability>() })
    }
}

// ---------------------------------------------------------------------------
// CacheModule
// ---------------------------------------------------------------------------

impl ModuleMeta for CacheModule {
    const NAME: &'static str = "cache";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

impl AsyncAutoBuilder for CacheModule {
    type Capability = bool;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move {
            Ok(kit
                .config::<CacheConfig>()
                .map(|c| c.enabled)
                .unwrap_or(false))
        })
    }
}

// ---------------------------------------------------------------------------
// DbModule
// ---------------------------------------------------------------------------

impl ModuleMeta for DbModule {
    const NAME: &'static str = "db";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

impl AsyncAutoBuilder for DbModule {
    type Capability = bool;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move { Ok(kit.config::<DbConfig>().map(|c| c.enabled).unwrap_or(false)) })
    }
}

// ---------------------------------------------------------------------------
// AuditModule
// ---------------------------------------------------------------------------

impl ModuleMeta for AuditModule {
    const NAME: &'static str = "audit";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

impl AsyncAutoBuilder for AuditModule {
    type Capability = Option<Arc<AuditLogger>>;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move { kit.config::<Self::Capability>() })
    }
}

// ===========================================================================
// v0.3.0 D3 重构:覆盖 VecboostState 剩余字段的 13 个 Module 实现
//
// 设计原则与现有 6 个 Module 保持一致:
//   - 复杂类型(Arc<T>、Option<Arc<T>>、Vec<String>)→ `kit.config::<Self::Capability>()`
//     严格模式:missing config = TraitKitError::MissingConfig
//   - bool 类型 → `kit.config::<Newtype>().map(|c| c.0).unwrap_or(false)`
//     宽松模式(与 CacheModule/DbModule 一致):missing config 默认 false
// ===========================================================================

// ---------------------------------------------------------------------------
// UserStoreModule (auth feature)
// ---------------------------------------------------------------------------

#[cfg(feature = "auth")]
impl ModuleMeta for UserStoreModule {
    const NAME: &'static str = "user_store";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

#[cfg(feature = "auth")]
impl AsyncAutoBuilder for UserStoreModule {
    type Capability = Option<Arc<UserStore>>;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move { kit.config::<Self::Capability>() })
    }
}

// ---------------------------------------------------------------------------
// AuthEnabledModule — bool from AuthEnabled newtype
// ---------------------------------------------------------------------------

impl ModuleMeta for AuthEnabledModule {
    const NAME: &'static str = "auth_enabled";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

impl AsyncAutoBuilder for AuthEnabledModule {
    type Capability = bool;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move { Ok(kit.config::<AuthEnabled>().map(|c| c.0).unwrap_or(false)) })
    }
}

// ---------------------------------------------------------------------------
// CsrfConfigModule (auth feature)
// ---------------------------------------------------------------------------

#[cfg(feature = "auth")]
impl ModuleMeta for CsrfConfigModule {
    const NAME: &'static str = "csrf_config";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

#[cfg(feature = "auth")]
impl AsyncAutoBuilder for CsrfConfigModule {
    type Capability = Option<Arc<CsrfConfig>>;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move { kit.config::<Self::Capability>() })
    }
}

// ---------------------------------------------------------------------------
// CsrfTokenStoreModule (auth feature)
// ---------------------------------------------------------------------------

#[cfg(feature = "auth")]
impl ModuleMeta for CsrfTokenStoreModule {
    const NAME: &'static str = "csrf_token_store";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

#[cfg(feature = "auth")]
impl AsyncAutoBuilder for CsrfTokenStoreModule {
    type Capability = Option<Arc<CsrfTokenStore>>;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move { kit.config::<Self::Capability>() })
    }
}

// ---------------------------------------------------------------------------
// MetricsCollectorModule
// ---------------------------------------------------------------------------

impl ModuleMeta for MetricsCollectorModule {
    const NAME: &'static str = "metrics_collector";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

impl AsyncAutoBuilder for MetricsCollectorModule {
    type Capability = Option<Arc<InferenceCollector>>;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move { kit.config::<Self::Capability>() })
    }
}

// ---------------------------------------------------------------------------
// PrometheusCollectorModule
// ---------------------------------------------------------------------------

impl ModuleMeta for PrometheusCollectorModule {
    const NAME: &'static str = "prometheus_collector";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

impl AsyncAutoBuilder for PrometheusCollectorModule {
    type Capability = Option<Arc<PrometheusCollector>>;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move { kit.config::<Self::Capability>() })
    }
}

// ---------------------------------------------------------------------------
// IpWhitelistModule
// ---------------------------------------------------------------------------

impl ModuleMeta for IpWhitelistModule {
    const NAME: &'static str = "ip_whitelist";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

impl AsyncAutoBuilder for IpWhitelistModule {
    type Capability = Vec<String>;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move { kit.config::<Self::Capability>() })
    }
}

// ---------------------------------------------------------------------------
// RateLimitEnabledModule — bool from RateLimitEnabled newtype
// ---------------------------------------------------------------------------

impl ModuleMeta for RateLimitEnabledModule {
    const NAME: &'static str = "rate_limit_enabled";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

impl AsyncAutoBuilder for RateLimitEnabledModule {
    type Capability = bool;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move {
            Ok(kit
                .config::<RateLimitEnabled>()
                .map(|c| c.0)
                .unwrap_or(false))
        })
    }
}

// ---------------------------------------------------------------------------
// PipelineEnabledModule — bool from PipelineEnabled newtype
// ---------------------------------------------------------------------------

impl ModuleMeta for PipelineEnabledModule {
    const NAME: &'static str = "pipeline_enabled";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

impl AsyncAutoBuilder for PipelineEnabledModule {
    type Capability = bool;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move {
            Ok(kit
                .config::<PipelineEnabled>()
                .map(|c| c.0)
                .unwrap_or(false))
        })
    }
}

// ---------------------------------------------------------------------------
// PipelineQueueModule
// ---------------------------------------------------------------------------

impl ModuleMeta for PipelineQueueModule {
    const NAME: &'static str = "pipeline_queue";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

impl AsyncAutoBuilder for PipelineQueueModule {
    type Capability = Arc<PriorityRequestQueue>;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move { kit.config::<Self::Capability>() })
    }
}

// ---------------------------------------------------------------------------
// ResponseChannelModule
// ---------------------------------------------------------------------------

impl ModuleMeta for ResponseChannelModule {
    const NAME: &'static str = "response_channel";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

impl AsyncAutoBuilder for ResponseChannelModule {
    type Capability = Arc<ResponseChannel>;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move { kit.config::<Self::Capability>() })
    }
}

// ---------------------------------------------------------------------------
// PriorityCalculatorModule
// ---------------------------------------------------------------------------

impl ModuleMeta for PriorityCalculatorModule {
    const NAME: &'static str = "priority_calculator";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

impl AsyncAutoBuilder for PriorityCalculatorModule {
    type Capability = Arc<PriorityCalculator>;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move { kit.config::<Self::Capability>() })
    }
}

// ---------------------------------------------------------------------------
// WorkerManagerModule
// ---------------------------------------------------------------------------

impl ModuleMeta for WorkerManagerModule {
    const NAME: &'static str = "worker_manager";

    fn dependencies() -> &'static [(&'static str, std::any::TypeId)] {
        &[]
    }
}

impl AsyncAutoBuilder for WorkerManagerModule {
    type Capability = Arc<WorkerManager>;
    type Error = TraitKitError;

    fn build<'a>(
        kit: &'a AsyncKit,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
        Box::pin(async move { kit.config::<Self::Capability>() })
    }
}