cllient 0.2.1

A comprehensive Rust client for LLM APIs with unified interface and model management
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
500
501
502
503
504
505
506
507
508
509
510
511
use std::sync::Arc;
use regex::Regex;
use crate::client::ConfigProvider;
use crate::embedded_config::EmbeddedConfigLoader;
use crate::config::{ModelConfig, ServiceConfig, VerificationStatus};
use crate::error::{ClientError, Result};
use crate::types::RequestBuilder;
use crate::export::{
    RegistryExport, ServiceExport, ModelExport, RegistryStats, RateLimitsExport,
};
use crate::registry_index::{RegistryIndex, IndexStats, BrokenReference};
use crate::query::ModelQuery;
use crate::validation::{ConfigValidator, ValidationLevel, ValidationReport};

/// Main runtime object that contains all model configurations and provides
/// a fluent API for model selection and request building
pub struct ModelRegistry {
    config_provider: Arc<dyn ConfigProvider + Send + Sync>,
    index: RegistryIndex,
}

impl ModelRegistry {
    /// Create a new runtime with embedded configurations.
    ///
    /// This builds the bidirectional index at initialization time.
    /// If there are broken references (models referencing non-existent services),
    /// an error is returned.
    pub fn new() -> Result<Self> {
        let embedded_loader = EmbeddedConfigLoader::new()?;
        let index = RegistryIndex::build(&embedded_loader);

        // Validate cross-references at startup
        index.validate_refs()?;

        Ok(Self {
            config_provider: Arc::new(embedded_loader),
            index,
        })
    }

    /// Create a new runtime without strict validation.
    ///
    /// Unlike `new()`, this allows broken references and orphan services.
    /// Use this for debugging or when you need to inspect broken configs.
    pub fn new_permissive() -> Result<Self> {
        let embedded_loader = EmbeddedConfigLoader::new()?;
        let index = RegistryIndex::build(&embedded_loader);

        Ok(Self {
            config_provider: Arc::new(embedded_loader),
            index,
        })
    }

    /// Create a runtime with a custom config provider.
    ///
    /// This builds the bidirectional index from the provider.
    /// If there are broken references, an error is returned.
    pub fn with_provider<T: ConfigProvider + Send + Sync + 'static>(provider: T) -> Result<Self> {
        let index = RegistryIndex::build(&provider);

        // Validate cross-references
        index.validate_refs()?;

        Ok(Self {
            config_provider: Arc::new(provider),
            index,
        })
    }

    /// Create a runtime with a custom config provider without strict validation.
    pub fn with_provider_permissive<T: ConfigProvider + Send + Sync + 'static>(provider: T) -> Self {
        let index = RegistryIndex::build(&provider);
        Self {
            config_provider: Arc::new(provider),
            index,
        }
    }
    
    /// Create a request builder for a specific model ID
    pub fn from_id(&self, model_id: &str) -> Result<RequestBuilder> {
        // Validate that the model exists
        let _ = self.config_provider.get_model(model_id)?;
        
        RequestBuilder::new(model_id.to_string(), Arc::clone(&self.config_provider))
    }
    
    /// Find the cheapest model matching a pattern and create a request builder
    pub fn use_cheapest(&self, pattern: &str) -> Result<RequestBuilder> {
        let model_id = self.find_cheapest_model(pattern)?;
        self.from_id(&model_id)
    }
    
    /// Find the fastest model (lowest latency/highest throughput) matching a pattern
    pub fn use_fastest(&self, pattern: &str) -> Result<RequestBuilder> {
        let model_id = self.find_fastest_model(pattern)?;
        self.from_id(&model_id)
    }
    
    /// Find the best quality model matching a pattern
    pub fn use_best_quality(&self, pattern: &str) -> Result<RequestBuilder> {
        let model_id = self.find_best_quality_model(pattern)?;
        self.from_id(&model_id)
    }
    
    /// List all available models
    pub fn list_models(&self) -> Vec<&str> {
        self.config_provider.list_models()
    }
    
    /// List models matching a pattern
    pub fn list_models_matching(&self, pattern: &str) -> Result<Vec<&str>> {
        let regex = self.pattern_to_regex(pattern)?;
        let models = self.config_provider.list_models();
        
        Ok(models.into_iter()
            .filter(|model_id| regex.is_match(model_id))
            .collect())
    }
    
    /// Get model information
    pub fn get_model_info(&self, model_id: &str) -> Result<&ModelConfig> {
        self.config_provider.get_model(model_id)
    }
    
    /// List all available model families
    pub fn list_families(&self) -> Vec<String> {
        // Use the index for O(1) family lookup instead of iterating all models
        self.index.all_families().into_iter().map(String::from).collect()
    }

    /// List models in a specific family
    pub fn list_models_in_family(&self, family: &str) -> Vec<&str> {
        // Use the index for O(1) lookup instead of filtering all models
        self.index.models_in_family(family)
            .iter()
            .map(String::as_str)
            .collect()
    }

    // === Service Access Methods (delegated from ConfigProvider) ===

    /// List all available services
    pub fn list_services(&self) -> Vec<&str> {
        self.config_provider.list_services()
    }

    /// Get service configuration by name
    pub fn get_service(&self, name: &str) -> Result<&ServiceConfig> {
        self.config_provider.get_service(name)
    }

    /// Get both model and service configuration in one call
    pub fn get_model_with_service(&self, model_id: &str) -> Result<(&ModelConfig, &ServiceConfig)> {
        self.config_provider.get_model_with_service(model_id)
    }

    // === Verification Status Methods ===

    /// List only verified models
    pub fn list_verified_models(&self) -> Vec<&str> {
        // Use the index for O(1) status lookup instead of filtering all models
        self.index.models_with_status(&VerificationStatus::Verified)
            .iter()
            .map(String::as_str)
            .collect()
    }

    /// List models by verification status
    pub fn list_models_by_status(&self, status: VerificationStatus) -> Vec<&str> {
        // Use the index for O(1) status lookup instead of filtering all models
        self.index.models_with_status(&status)
            .iter()
            .map(String::as_str)
            .collect()
    }

    /// Check if a model is verified
    pub fn is_verified(&self, model_id: &str) -> bool {
        self.config_provider.get_model(model_id)
            .map(|cfg| cfg.model.status == VerificationStatus::Verified)
            .unwrap_or(false)
    }

    // === Registry Export ===

    /// Export the full registry as a serializable structure.
    ///
    /// This is useful for RPC boundaries where you need to expose
    /// the complete registry state in a single call.
    pub fn export(&self) -> RegistryExport {
        // Use the index for model counts per service (avoids redundant lookups)
        let service_model_counts = self.index.model_counts_by_service();

        // Use the index for verification counts (O(1) lookup)
        let model_status_counts = self.index.model_counts_by_status();
        let verified_count = model_status_counts.get(&VerificationStatus::Verified).copied().unwrap_or(0);
        let unverified_count = model_status_counts.get(&VerificationStatus::Unverified).copied().unwrap_or(0);

        // Build model exports using From impl
        let models: Vec<ModelExport> = self.config_provider.list_models()
            .into_iter()
            .filter_map(|model_id| {
                let cfg = self.config_provider.get_model(model_id).ok()?;
                Some(ModelExport::from(cfg))
            })
            .collect();

        // Build service exports
        let services: Vec<ServiceExport> = self.config_provider.list_services()
            .into_iter()
            .filter_map(|service_name| {
                let cfg = self.config_provider.get_service(service_name).ok()?;

                let rate_limits = if cfg.rate_limits.requests_per_minute.is_some()
                    || cfg.rate_limits.tokens_per_minute.is_some()
                    || cfg.rate_limits.concurrent_requests.is_some()
                {
                    Some(RateLimitsExport::from(&cfg.rate_limits))
                } else {
                    None
                };

                Some(ServiceExport {
                    name: service_name.to_string(),
                    base_url: cfg.service.base_url.clone(),
                    message_format: cfg.message_builder.clone().unwrap_or_default(),
                    rate_limits,
                    model_count: service_model_counts.get(service_name).copied().unwrap_or(0),
                })
            })
            .collect();

        // Use the index for families (avoids redundant iteration)
        let families = self.list_families();

        RegistryExport {
            stats: RegistryStats {
                service_count: services.len(),
                family_count: families.len(),
                model_count: models.len(),
                verified_count,
                unverified_count,
            },
            services,
            families,
            models,
        }
    }

    /// Export only verified models and their services
    pub fn export_verified(&self) -> RegistryExport {
        self.export().verified_only()
    }

    /// Export models for a specific service
    pub fn export_by_service(&self, service: &str) -> RegistryExport {
        self.export().filter_by_service(service)
    }

    /// Export models for a specific family
    pub fn export_by_family(&self, family: &str) -> RegistryExport {
        self.export().filter_by_family(family)
    }

    // === Query API ===

    /// Create a fluent query builder for filtering models.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let models = registry.query()
    ///     .service("openai")
    ///     .verified()
    ///     .with_vision()
    ///     .fuzzy("gpt turbo")
    ///     .list();
    /// ```
    pub fn query(&self) -> ModelQuery<'_, dyn ConfigProvider + Send + Sync> {
        ModelQuery::new(self.config_provider.as_ref(), &self.index)
    }

    // === Index Access ===

    /// Get the bidirectional registry index.
    pub fn index(&self) -> &RegistryIndex {
        &self.index
    }

    /// Get index statistics.
    pub fn index_stats(&self) -> IndexStats {
        self.index.stats()
    }

    /// Get all models for a specific service (fast indexed lookup).
    pub fn models_for_service(&self, service: &str) -> &[String] {
        self.index.models_for_service(service)
    }

    /// Get the service for a specific model (fast indexed lookup).
    pub fn service_for_model(&self, model_id: &str) -> Option<&str> {
        self.index.service_for_model(model_id)
    }

    /// Get all broken references (models referencing non-existent services).
    pub fn broken_refs(&self) -> &[BrokenReference] {
        self.index.broken_refs()
    }

    /// Get all orphan services (services with no models).
    pub fn orphan_services(&self) -> &[String] {
        self.index.orphan_services()
    }

    /// Check if the registry has any broken references.
    pub fn has_broken_refs(&self) -> bool {
        self.index.has_broken_refs()
    }

    // === Validation API ===

    /// Run validation at the specified levels.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let report = registry.validate(&[
    ///     ValidationLevel::Schema,
    ///     ValidationLevel::CrossRef,
    ///     ValidationLevel::Semantic,
    /// ]);
    /// if !report.passed {
    ///     for error in report.errors() {
    ///         eprintln!("{}", error);
    ///     }
    /// }
    /// ```
    pub fn validate(&self, levels: &[ValidationLevel]) -> ValidationReport {
        let validator = ConfigValidator::new(self.config_provider.as_ref(), &self.index);
        validator.validate(levels)
    }

    /// Validate a single model.
    pub fn validate_model(&self, model_id: &str, levels: &[ValidationLevel]) -> ValidationReport {
        let validator = ConfigValidator::new(self.config_provider.as_ref(), &self.index);
        validator.validate_model(model_id, levels)
    }

    /// Validate a single service.
    pub fn validate_service(&self, service_name: &str, levels: &[ValidationLevel]) -> ValidationReport {
        let validator = ConfigValidator::new(self.config_provider.as_ref(), &self.index);
        validator.validate_service(service_name, levels)
    }

    /// Quick check if the registry is valid (no errors at any level).
    pub fn is_valid(&self) -> bool {
        let report = self.validate(&[
            ValidationLevel::Schema,
            ValidationLevel::CrossRef,
            ValidationLevel::Semantic,
        ]);
        report.passed
    }

    // Private helper methods
    
    fn find_cheapest_model(&self, pattern: &str) -> Result<String> {
        let matching_models = self.list_models_matching(pattern)?;
        
        if matching_models.is_empty() {
            return Err(ClientError::Config(crate::error::ConfigError::ModelNotFound(
                format!("No models found matching pattern: {}", pattern)
            )));
        }
        
        let mut cheapest_model = None;
        let mut lowest_cost = f64::MAX;
        
        for model_id in matching_models {
            if let Ok(model_config) = self.config_provider.get_model(model_id) {
                // Calculate cost as average of input + output (weighted for typical usage)
                // Assume 70% input, 30% output for typical usage
                let avg_cost = (model_config.pricing.input_per_1k_tokens * 0.7) + 
                              (model_config.pricing.output_per_1k_tokens * 0.3);
                
                if avg_cost < lowest_cost {
                    lowest_cost = avg_cost;
                    cheapest_model = Some(model_id.to_string());
                }
            }
        }
        
        cheapest_model.ok_or_else(|| ClientError::Config(crate::error::ConfigError::ModelNotFound(
            format!("No valid models found matching pattern: {}", pattern)
        )))
    }
    
    fn find_fastest_model(&self, pattern: &str) -> Result<String> {
        let matching_models = self.list_models_matching(pattern)?;
        
        if matching_models.is_empty() {
            return Err(ClientError::Config(crate::error::ConfigError::ModelNotFound(
                format!("No models found matching pattern: {}", pattern)
            )));
        }
        
        // For now, prioritize by model variant (haiku > sonnet > opus for Claude)
        // In the future, this could use actual latency measurements
        let speed_order = ["haiku", "sonnet", "opus", "mini", "turbo"];
        
        for variant in &speed_order {
            for model_id in &matching_models {
                if model_id.contains(variant) {
                    return Ok(model_id.to_string());
                }
            }
        }
        
        // If no speed indicators found, return the first match
        Ok(matching_models[0].to_string())
    }
    
    fn find_best_quality_model(&self, pattern: &str) -> Result<String> {
        let matching_models = self.list_models_matching(pattern)?;
        
        if matching_models.is_empty() {
            return Err(ClientError::Config(crate::error::ConfigError::ModelNotFound(
                format!("No models found matching pattern: {}", pattern)
            )));
        }
        
        // Prioritize by model variant (opus > sonnet > haiku for Claude)
        let quality_order = ["opus", "sonnet", "haiku", "turbo", "mini"];
        
        for variant in &quality_order {
            for model_id in &matching_models {
                if model_id.contains(variant) {
                    return Ok(model_id.to_string());
                }
            }
        }
        
        // If no quality indicators found, return the first match
        Ok(matching_models[0].to_string())
    }
    
    fn pattern_to_regex(&self, pattern: &str) -> Result<Regex> {
        // Convert glob-style pattern to regex
        let escaped = regex::escape(pattern);
        let regex_pattern = escaped.replace(r"\*", ".*").replace(r"\?", ".");
        let regex_pattern = format!("^{}$", regex_pattern);
        
        Regex::new(&regex_pattern).map_err(|e| ClientError::Config(
            crate::error::ConfigError::InvalidPath(format!("Invalid pattern: {}", e))
        ))
    }
}

impl Default for ModelRegistry {
    fn default() -> Self {
        Self::new().expect("Failed to create default runtime")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_runtime_creation() {
        let runtime = ModelRegistry::new().unwrap();
        let models = runtime.list_models();
        assert!(!models.is_empty());
    }
    
    #[test]
    fn test_model_pattern_matching() {
        let runtime = ModelRegistry::new().unwrap();
        
        // Test exact match
        let exact_matches = runtime.list_models_matching("claude-3-haiku-20240307").unwrap();
        assert!(exact_matches.contains(&"claude-3-haiku-20240307"));
        
        // Test wildcard pattern
        let haiku_matches = runtime.list_models_matching("claude-3-haiku-*").unwrap();
        assert!(!haiku_matches.is_empty());
        assert!(haiku_matches.iter().all(|m| m.contains("claude-3-haiku")));
    }
    
    #[test]
    fn test_cheapest_model_selection() {
        let runtime = ModelRegistry::new().unwrap();
        
        // This should find a claude-3-haiku variant as it's typically cheapest
        let cheapest = runtime.use_cheapest("claude-3-haiku-*").unwrap();
        // Just verify we can create the request builder without error
        assert!(cheapest.model_id.contains("claude-3-haiku"));
    }
    
    #[test]
    fn test_model_info_retrieval() {
        let runtime = ModelRegistry::new().unwrap();
        let models = runtime.list_models();
        
        if let Some(model_id) = models.first() {
            let model_info = runtime.get_model_info(model_id).unwrap();
            assert_eq!(&model_info.model.id, model_id);
        }
    }
}