dwctl 8.38.2

The Doubleword Control Layer - A self-hostable observability and analytics platform for LLM applications
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//! Database models for deployments.
//!
//! This module includes support for both regular deployed models (backed by a single
//! inference endpoint) and composite models (virtual models that distribute requests
//! across multiple underlying models based on configurable weights).

use crate::api::models::deployments::{DeployedModelCreate, DeployedModelUpdate};
use crate::types::{DeploymentId, InferenceEndpointId, UserId};
use bon::Builder;
use chrono::{DateTime, NaiveDate, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use serde_with::rust::double_option;
use utoipa::ToSchema;
use uuid::Uuid;

// Mode constants for provider pricing
const MODE_PER_TOKEN: &str = "per_token";
const MODE_HOURLY: &str = "hourly";

/// Provider pricing options (enum for type safety)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, ToSchema)]
#[serde(tag = "mode", rename_all = "snake_case")]
pub enum ProviderPricing {
    PerToken {
        /// Input price per token (sent/returned as string to preserve precision)
        #[schema(value_type = Option<String>)]
        input_price_per_token: Option<Decimal>,
        /// Output price per token (sent/returned as string to preserve precision)
        #[schema(value_type = Option<String>)]
        output_price_per_token: Option<Decimal>,
    },
    Hourly {
        /// Hourly rate (sent/returned as string to preserve precision)
        #[schema(value_type = String)]
        rate: Decimal,
        /// Input token cost ratio (sent/returned as string to preserve precision)
        #[schema(value_type = String)]
        input_token_cost_ratio: Decimal,
    },
}

/// Flat database fields for provider pricing
#[derive(Debug, Clone, Default)]
pub struct ProviderPricingFields {
    pub mode: Option<String>,
    pub input_price_per_token: Option<Decimal>,
    pub output_price_per_token: Option<Decimal>,
    pub hourly_rate: Option<Decimal>,
    pub input_token_cost_ratio: Option<Decimal>,
}

impl ProviderPricing {
    /// Convert flat database fields to structured provider pricing
    pub fn from_flat_fields(fields: ProviderPricingFields) -> Option<Self> {
        match fields.mode.as_deref() {
            Some(MODE_HOURLY) => match (fields.hourly_rate, fields.input_token_cost_ratio) {
                (Some(rate), Some(input_token_cost_ratio)) => Some(ProviderPricing::Hourly {
                    rate,
                    input_token_cost_ratio,
                }),
                _ => None,
            },
            Some(MODE_PER_TOKEN) => Some(ProviderPricing::PerToken {
                input_price_per_token: fields.input_price_per_token,
                output_price_per_token: fields.output_price_per_token,
            }),
            _ => None,
        }
    }

    /// Convert structured provider pricing to flat database fields
    pub fn to_flat_fields(&self) -> ProviderPricingFields {
        match self {
            ProviderPricing::PerToken {
                input_price_per_token,
                output_price_per_token,
            } => ProviderPricingFields {
                mode: Some(MODE_PER_TOKEN.to_string()),
                input_price_per_token: *input_price_per_token,
                output_price_per_token: *output_price_per_token,
                hourly_rate: None,
                input_token_cost_ratio: None,
            },
            ProviderPricing::Hourly {
                rate,
                input_token_cost_ratio,
            } => ProviderPricingFields {
                mode: Some(MODE_HOURLY.to_string()),
                input_price_per_token: None,
                output_price_per_token: None,
                hourly_rate: Some(*rate),
                input_token_cost_ratio: Some(*input_token_cost_ratio),
            },
        }
    }
}

/// Provider pricing update structure for partial updates
#[derive(Debug, Clone, Default, Serialize, Deserialize, ToSchema)]
#[serde(tag = "mode", rename_all = "snake_case")]
pub enum ProviderPricingUpdate {
    #[default]
    /// No change to provider pricing
    NoChange,
    /// Update per-token pricing fields
    PerToken {
        /// Update input pricing: None = no change, Some(None) = clear, Some(price) = set (sent as string to preserve precision)
        #[serde(default, skip_serializing_if = "Option::is_none", with = "double_option")]
        #[schema(value_type = Option<Option<String>>)]
        input_price_per_token: Option<Option<Decimal>>,
        /// Update output pricing: None = no change, Some(None) = clear, Some(price) = set (sent as string to preserve precision)
        #[serde(default, skip_serializing_if = "Option::is_none", with = "double_option")]
        #[schema(value_type = Option<Option<String>>)]
        output_price_per_token: Option<Option<Decimal>>,
    },
    /// Update hourly pricing fields
    Hourly {
        /// Update hourly rate: None = no change, Some(rate) = set (sent as string to preserve precision)
        #[serde(default, skip_serializing_if = "Option::is_none")]
        #[schema(value_type = Option<String>)]
        rate: Option<Decimal>,
        /// Update input token cost ratio: None = no change, Some(ratio) = set (sent as string to preserve precision)
        #[serde(default, skip_serializing_if = "Option::is_none")]
        #[schema(value_type = Option<String>)]
        input_token_cost_ratio: Option<Decimal>,
    },
}

/// Parameters for provider pricing database updates
#[derive(Debug, Clone, Default)]
pub struct ProviderPricingUpdateParams {
    pub should_update_mode: bool,
    pub mode: Option<String>,
    pub should_update_input: bool,
    pub input: Option<Decimal>,
    pub should_update_output: bool,
    pub output: Option<Decimal>,
    pub should_update_hourly: bool,
    pub hourly: Option<Decimal>,
    pub should_update_ratio: bool,
    pub ratio: Option<Decimal>,
}

impl ProviderPricingUpdate {
    /// Convert to parameters for database update query
    pub fn to_update_params(&self) -> ProviderPricingUpdateParams {
        match self {
            ProviderPricingUpdate::NoChange => ProviderPricingUpdateParams::default(),
            ProviderPricingUpdate::PerToken {
                input_price_per_token,
                output_price_per_token,
            } => ProviderPricingUpdateParams {
                should_update_mode: true,
                mode: Some(MODE_PER_TOKEN.to_string()),
                should_update_input: input_price_per_token.is_some(),
                input: input_price_per_token.flatten(),
                should_update_output: output_price_per_token.is_some(),
                output: output_price_per_token.flatten(),
                ..Default::default()
            },
            ProviderPricingUpdate::Hourly {
                rate,
                input_token_cost_ratio,
            } => ProviderPricingUpdateParams {
                should_update_mode: true,
                mode: Some(MODE_HOURLY.to_string()),
                should_update_hourly: rate.is_some(),
                hourly: *rate,
                should_update_ratio: input_token_cost_ratio.is_some(),
                ratio: *input_token_cost_ratio,
                ..Default::default()
            },
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, ToSchema)]
#[serde(rename_all = "UPPERCASE")]
pub enum ModelType {
    Chat,
    Embeddings,
    Reranker,
}

impl ModelType {
    /// Detect model type from model name using common patterns
    /// This helps automatically classify models when syncing from endpoints
    pub fn detect_from_name(model_name: &str) -> Self {
        let name_lower = model_name.to_lowercase();

        // Reranker model patterns - check these first as they're most specific
        let reranker_patterns = [
            "rerank",
            "reranker",
            "cross-encoder",
            "bge-reranker",
            "mixedbread-reranker",
            "mxbai-rerank",
        ];

        // Embedding model patterns
        let embedding_patterns = [
            "embed",
            "embedding",
            "ada", // OpenAI's ada embedding models
            "text-embedding",
            "sentence-transformer",
            "all-minilm",
            "bge-",
            "e5-",
        ];

        // Check if model name contains any reranker patterns
        if reranker_patterns.iter().any(|pattern| name_lower.contains(pattern)) {
            return Self::Reranker;
        }

        // Check if model name contains any embedding patterns
        if embedding_patterns.iter().any(|pattern| name_lower.contains(pattern)) {
            return Self::Embeddings;
        }

        // Default to chat for everything else
        Self::Chat
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum ModelDisplayCategory {
    Generation,
    Embedding,
    Ocr,
}

/// Maximum serialized size of ModelCatalogMetadata in bytes (16 KB).
pub const MODEL_CATALOG_METADATA_MAX_BYTES: usize = 16_384;
/// Maximum number of keys allowed in the `extra` object.
pub const MODEL_CATALOG_METADATA_MAX_EXTRA_KEYS: usize = 50;

/// Catalog-style metadata for display purposes (stored as JSONB).
#[derive(Debug, Clone, Default, Serialize, Deserialize, ToSchema, PartialEq)]
pub struct ModelCatalogMetadata {
    /// Provider name (e.g. "OpenAI", "Anthropic")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub provider: Option<String>,

    /// Display category for catalog organization independent of runtime model type
    #[serde(skip_serializing_if = "Option::is_none")]
    pub display_category: Option<ModelDisplayCategory>,

    /// Intelligence index score (e.g. from Artificial Analysis)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub intelligence_index: Option<f64>,

    /// Context window size in tokens
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context_window: Option<i64>,

    /// When the model was released by its provider
    #[serde(skip_serializing_if = "Option::is_none")]
    pub released_at: Option<NaiveDate>,

    /// Quantization type (e.g. "fp16", "int8")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub quantization: Option<String>,

    /// Attribution source for this metadata (e.g. "artificial_analysis")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attribution: Option<String>,

    /// Arbitrary display-only data (benchmark scores, tags, etc.)
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schema(value_type = Option<Object>)]
    pub extra: Option<serde_json::Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, ToSchema)]
#[serde(rename_all = "lowercase")]
pub enum ModelStatus {
    Active,
    Inactive,
}

impl ModelStatus {
    pub fn to_db_string(&self) -> &'static str {
        match self {
            ModelStatus::Active => "active",
            ModelStatus::Inactive => "inactive",
        }
    }

    pub fn from_db_string(s: &str) -> ModelStatus {
        match s {
            "active" => ModelStatus::Active,
            "inactive" => ModelStatus::Inactive,
            _ => ModelStatus::Active, // Default fallback
        }
    }
}

/// Load balancing strategy for composite models
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum LoadBalancingStrategy {
    /// Distribute requests randomly based on weights (default)
    #[default]
    WeightedRandom,
    /// Try providers in order of weight (highest first), falling back on failure
    Priority,
}

impl LoadBalancingStrategy {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::WeightedRandom => "weighted_random",
            Self::Priority => "priority",
        }
    }

    pub fn try_parse(s: &str) -> Option<Self> {
        match s {
            "weighted_random" => Some(Self::WeightedRandom),
            "priority" => Some(Self::Priority),
            _ => None,
        }
    }
}

fn default_true() -> bool {
    true
}

fn default_fallback_status_codes() -> Vec<i32> {
    vec![429, 500, 502, 503, 504]
}

/// Fallback configuration for composite models
#[derive(Debug, Clone, Default, Serialize, Deserialize, ToSchema)]
pub struct FallbackConfig {
    /// Whether fallback is enabled (default: true)
    #[serde(default = "default_true")]
    pub enabled: bool,
    /// Fall back when provider is rate limited (default: true)
    #[serde(default = "default_true")]
    pub on_rate_limit: bool,
    /// HTTP status codes that trigger fallback (default: [429, 500, 502, 503, 504])
    #[serde(default = "default_fallback_status_codes")]
    pub on_status: Vec<i32>,
    /// When true, weighted random failover samples with replacement (default: false)
    #[serde(default)]
    pub with_replacement: bool,
    /// Maximum number of failover attempts (default: provider count)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_attempts: Option<i32>,
}

impl FallbackConfig {
    pub fn new() -> Self {
        Self {
            enabled: true,
            on_rate_limit: true,
            on_status: default_fallback_status_codes(),
            with_replacement: false,
            max_attempts: None,
        }
    }
}

/// A component of a composite model (a deployed model with a weight)
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct DeploymentComponent {
    #[schema(value_type = String, format = "uuid")]
    pub deployed_model_id: DeploymentId,
    /// Relative weight for load balancing (1-100)
    pub weight: i32,
    /// Whether this component is active
    pub enabled: bool,
    /// Sort order for priority-based routing (lower = higher priority)
    #[serde(default)]
    pub sort_order: i32,
}

/// Database request for adding a component to a composite model
#[derive(Debug, Clone)]
pub struct DeploymentComponentCreateDBRequest {
    pub composite_model_id: DeploymentId,
    pub deployed_model_id: DeploymentId,
    pub weight: i32,
    pub enabled: bool,
    pub sort_order: i32,
}

/// Database response for a deployment component (flat structure with joined model info)
#[derive(Debug, Clone)]
pub struct DeploymentComponentDBResponse {
    // Component fields
    pub id: uuid::Uuid,
    pub composite_model_id: DeploymentId,
    pub deployed_model_id: DeploymentId,
    pub weight: i32,
    pub enabled: bool,
    pub sort_order: i32,
    pub created_at: DateTime<Utc>,
    // Joined model fields
    pub model_alias: String,
    pub model_name: String,
    pub model_description: Option<String>,
    pub model_type: Option<String>,
    // Joined endpoint fields
    pub endpoint_id: Option<InferenceEndpointId>,
    pub endpoint_name: Option<String>,
    pub model_trusted: bool,
    pub model_open_responses_adapter: bool,
}

/// Database request for creating a new deployment
#[derive(Debug, Clone, Builder)]
pub struct DeploymentCreateDBRequest {
    pub created_by: UserId,
    pub model_name: String,
    pub alias: String,
    pub display_name: Option<String>,
    pub description: Option<String>,
    pub model_type: Option<ModelType>,
    pub capabilities: Option<Vec<String>>,
    /// Inference endpoint for regular models. Must be None for composite models.
    pub hosted_on: Option<InferenceEndpointId>,
    pub requests_per_second: Option<f32>,
    pub burst_size: Option<i32>,
    pub capacity: Option<i32>,
    pub batch_capacity: Option<i32>,
    pub throughput: Option<f32>,
    // Provider/downstream pricing
    pub provider_pricing: Option<ProviderPricing>,
    // Composite model fields
    /// Whether this is a composite model
    #[builder(default)]
    pub is_composite: bool,
    /// Load balancing strategy for composite models (defaults to weighted_random)
    pub lb_strategy: Option<LoadBalancingStrategy>,
    /// Fallback configuration for composite models
    pub fallback_enabled: Option<bool>,
    pub fallback_on_rate_limit: Option<bool>,
    pub fallback_on_status: Option<Vec<i32>>,
    pub fallback_with_replacement: Option<bool>,
    pub fallback_max_attempts: Option<i32>,
    /// Whether to sanitize/filter sensitive data from model responses (defaults to false)
    #[builder(default = false)]
    pub sanitize_responses: bool,
    /// Whether to mark provider as trusted in strict mode (bypasses sanitization)
    #[builder(default = false)]
    pub trusted: bool,
    /// Whether to enable the open_responses adapter (converts /v1/responses to /v1/chat/completions)
    #[builder(default = true)]
    pub open_responses_adapter: bool,
    /// Per-model allowed batch completion windows (overrides global config when set)
    pub allowed_batch_completion_windows: Option<Vec<String>>,
    /// Catalog metadata for display purposes (stored as JSONB)
    pub metadata: Option<ModelCatalogMetadata>,
}

impl DeploymentCreateDBRequest {
    /// Creates a deployment request from API model creation data
    pub fn from_api_create(created_by: UserId, create: DeployedModelCreate) -> Self {
        match create {
            DeployedModelCreate::Standard(standard) => Self::builder()
                .created_by(created_by)
                .model_name(standard.model_name.clone())
                .alias(standard.alias.unwrap_or(standard.model_name))
                .maybe_display_name(standard.display_name)
                .maybe_description(standard.description)
                .maybe_model_type(standard.model_type)
                .maybe_capabilities(standard.capabilities)
                .hosted_on(standard.hosted_on)
                .maybe_requests_per_second(standard.requests_per_second)
                .maybe_burst_size(standard.burst_size)
                .maybe_capacity(standard.capacity)
                .maybe_batch_capacity(standard.batch_capacity)
                .maybe_throughput(standard.throughput)
                .maybe_provider_pricing(standard.provider_pricing)
                .is_composite(false)
                .sanitize_responses(standard.sanitize_responses.unwrap_or(false))
                .trusted(standard.trusted.unwrap_or(false))
                .open_responses_adapter(standard.open_responses_adapter.unwrap_or(true))
                .maybe_allowed_batch_completion_windows(standard.allowed_batch_completion_windows)
                .maybe_metadata(standard.metadata)
                .build(),
            DeployedModelCreate::Composite(composite) => Self::builder()
                .created_by(created_by)
                .model_name(composite.model_name.clone())
                .alias(composite.alias.unwrap_or(composite.model_name))
                .maybe_display_name(composite.display_name)
                .maybe_description(composite.description)
                .maybe_model_type(composite.model_type)
                .maybe_capabilities(composite.capabilities)
                .maybe_requests_per_second(composite.requests_per_second)
                .maybe_burst_size(composite.burst_size)
                .maybe_capacity(composite.capacity)
                .maybe_batch_capacity(composite.batch_capacity)
                .maybe_throughput(composite.throughput)
                .is_composite(true)
                .lb_strategy(composite.lb_strategy)
                .fallback_enabled(composite.fallback_enabled)
                .fallback_on_rate_limit(composite.fallback_on_rate_limit)
                .fallback_on_status(composite.fallback_on_status)
                .fallback_with_replacement(composite.fallback_with_replacement)
                .maybe_fallback_max_attempts(composite.fallback_max_attempts)
                .sanitize_responses(composite.sanitize_responses)
                .trusted(composite.trusted.unwrap_or(false))
                .open_responses_adapter(composite.open_responses_adapter.unwrap_or(true))
                .maybe_allowed_batch_completion_windows(composite.allowed_batch_completion_windows)
                .maybe_metadata(composite.metadata)
                .build(),
        }
    }
}

/// Database request for updating a deployment
#[derive(Debug, Clone, Builder)]
pub struct DeploymentUpdateDBRequest {
    pub model_name: Option<String>,
    pub alias: Option<String>,
    pub display_name: Option<String>,
    pub description: Option<Option<String>>,
    pub model_type: Option<Option<ModelType>>,
    pub capabilities: Option<Option<Vec<String>>>,
    pub status: Option<ModelStatus>,
    pub last_sync: Option<Option<DateTime<Utc>>>,
    pub deleted: Option<bool>,
    pub requests_per_second: Option<Option<f32>>,
    pub burst_size: Option<Option<i32>>,
    pub capacity: Option<Option<i32>>,
    pub batch_capacity: Option<Option<i32>>,
    pub throughput: Option<Option<f32>>,
    // Provider pricing updates
    pub provider_pricing: Option<ProviderPricingUpdate>,
    // Composite model fields (only applicable when is_composite = true)
    pub lb_strategy: Option<LoadBalancingStrategy>,
    pub fallback_enabled: Option<bool>,
    pub fallback_on_rate_limit: Option<bool>,
    pub fallback_on_status: Option<Vec<i32>>,
    pub fallback_with_replacement: Option<bool>,
    pub fallback_max_attempts: Option<Option<i32>>,
    /// Whether to sanitize/filter sensitive data from model responses
    pub sanitize_responses: Option<bool>,
    /// Whether to mark provider as trusted in strict mode (bypasses sanitization)
    pub trusted: Option<bool>,
    /// Whether to enable the open_responses adapter (converts /v1/responses to /v1/chat/completions)
    pub open_responses_adapter: Option<bool>,
    /// Per-model allowed batch completion windows (None = no change, Some(None) = clear, Some(windows) = set)
    pub allowed_batch_completion_windows: Option<Option<Vec<String>>>,
    /// Catalog metadata (None = no change, Some(metadata) = replace)
    pub metadata: Option<ModelCatalogMetadata>,
}

impl From<DeployedModelUpdate> for DeploymentUpdateDBRequest {
    fn from(update: DeployedModelUpdate) -> Self {
        Self::builder()
            .maybe_alias(update.alias)
            .maybe_display_name(update.display_name)
            .maybe_description(update.description)
            .maybe_model_type(update.model_type)
            .maybe_capabilities(update.capabilities)
            .maybe_requests_per_second(update.requests_per_second)
            .maybe_burst_size(update.burst_size)
            .maybe_capacity(update.capacity)
            .maybe_batch_capacity(update.batch_capacity)
            .maybe_throughput(update.throughput)
            .maybe_provider_pricing(update.provider_pricing)
            .maybe_lb_strategy(update.lb_strategy)
            .maybe_fallback_enabled(update.fallback_enabled)
            .maybe_fallback_on_rate_limit(update.fallback_on_rate_limit)
            .maybe_fallback_on_status(update.fallback_on_status)
            .maybe_fallback_with_replacement(update.fallback_with_replacement)
            .maybe_fallback_max_attempts(update.fallback_max_attempts)
            .maybe_sanitize_responses(update.sanitize_responses)
            .maybe_trusted(update.trusted)
            .maybe_open_responses_adapter(update.open_responses_adapter)
            .maybe_allowed_batch_completion_windows(update.allowed_batch_completion_windows)
            .maybe_metadata(update.metadata)
            .build()
    }
}

impl DeploymentUpdateDBRequest {
    /// Create an update request for sync operations (status and/or last_sync)
    pub fn status_update(status: Option<ModelStatus>, last_sync: DateTime<Utc>) -> Self {
        Self::builder().maybe_status(status).last_sync(Some(last_sync)).build()
    }

    /// Create an update request for hide/unhide operations
    pub fn visibility_update(deleted: bool) -> Self {
        Self::builder().deleted(deleted).build()
    }

    /// Create an update request for alias changes
    pub fn alias_update(new_alias: String) -> Self {
        Self::builder().maybe_alias(Some(new_alias)).build()
    }
}

/// Database response for a deployment
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct DeploymentDBResponse {
    pub id: DeploymentId,
    pub model_name: String,
    pub alias: String,
    pub display_name: Option<String>,
    pub description: Option<String>,
    pub model_type: Option<ModelType>,
    pub capabilities: Option<Vec<String>>,
    pub created_by: UserId,
    /// Inference endpoint for regular models. None for composite models.
    pub hosted_on: Option<InferenceEndpointId>,
    pub status: ModelStatus,
    pub last_sync: Option<DateTime<Utc>>,
    pub deleted: bool,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub requests_per_second: Option<f32>,
    pub burst_size: Option<i32>,
    pub capacity: Option<i32>,
    pub batch_capacity: Option<i32>,
    /// Throughput in requests/second for batch capacity calculations
    pub throughput: Option<f32>,
    // Provider/downstream pricing
    pub provider_pricing: Option<ProviderPricing>,
    // Composite model fields
    /// Whether this is a composite model
    pub is_composite: bool,
    /// Load balancing strategy for composite models
    pub lb_strategy: LoadBalancingStrategy,
    /// Fallback configuration for composite models
    pub fallback_enabled: bool,
    pub fallback_on_rate_limit: bool,
    pub fallback_on_status: Vec<i32>,
    pub fallback_with_replacement: bool,
    pub fallback_max_attempts: Option<i32>,
    /// Whether to sanitize/filter sensitive data from model responses
    pub sanitize_responses: bool,
    /// Whether to mark provider as trusted in strict mode (bypasses sanitization)
    pub trusted: bool,
    /// Whether the open_responses adapter is enabled (converts /v1/responses to /v1/chat/completions)
    pub open_responses_adapter: bool,
    /// Per-model allowed batch completion windows (overrides global config when set)
    pub allowed_batch_completion_windows: Option<Vec<String>>,
    /// Catalog metadata (JSONB)
    pub metadata: serde_json::Value,
}

/// DB action for a traffic routing rule (used at the repository layer)
#[derive(Debug, Clone)]
pub enum TrafficRuleAction {
    Deny,
    Redirect(DeploymentId),
}

/// Row returned from model_traffic_rules table (with joined redirect target alias)
#[derive(Debug, Clone)]
pub struct TrafficRuleDBRow {
    pub id: Uuid,
    pub deployed_model_id: DeploymentId,
    pub api_key_purpose: String,
    pub action: String,
    pub redirect_target_id: Option<DeploymentId>,
    /// Populated via LEFT JOIN on deployed_models
    pub redirect_target_alias: Option<String>,
    pub created_at: DateTime<Utc>,
}