mockforge-intelligence 0.3.143

AI-powered behavior, response generation, and behavioral cloning for MockForge
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
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
//! Database-backed implementation of OrgControlsAccessor
//!
//! This module provides a PostgreSQL implementation of OrgControlsAccessor
//! for storing and retrieving org-level AI controls from the database.
//!
//! Requires the `database` feature to be enabled.

#[cfg(feature = "database")]
use crate::ai_studio::budget_manager::AiFeature;
#[cfg(feature = "database")]
use crate::ai_studio::org_controls::{
    BudgetCheckResult, OrgAiControlsConfig, OrgBudgetConfig, OrgControlsAccessor,
    OrgRateLimitConfig, RateLimitCheckResult,
};
#[cfg(feature = "database")]
use async_trait::async_trait;
#[cfg(feature = "database")]
use chrono::{DateTime, Utc};
#[cfg(feature = "database")]
use mockforge_foundation::Result;
#[cfg(feature = "database")]
use serde_json::Value;
#[cfg(feature = "database")]
use sqlx::{PgPool, Row};
#[cfg(feature = "database")]
use std::collections::HashMap;
#[cfg(feature = "database")]
use uuid::Uuid;

/// Database-backed org controls accessor
#[cfg(feature = "database")]
pub struct DbOrgControls {
    /// PostgreSQL connection pool
    pool: PgPool,
}

#[cfg(feature = "database")]
impl DbOrgControls {
    /// Create a new database-backed org controls accessor
    pub fn new(pool: PgPool) -> Self {
        Self { pool }
    }
}

#[cfg(feature = "database")]
#[async_trait]
impl OrgControlsAccessor for DbOrgControls {
    /// Load org controls configuration from database
    async fn load_org_config(
        &self,
        org_id: &str,
        workspace_id: Option<&str>,
    ) -> Result<Option<OrgAiControlsConfig>> {
        let org_uuid = Uuid::parse_str(org_id).map_err(|e| {
            mockforge_foundation::Error::validation(format!("Invalid org_id: {}", e))
        })?;
        let workspace_uuid = workspace_id.and_then(|w| Uuid::parse_str(w).ok());

        // Load budget config
        let budget = if let Some(ws_uuid) = workspace_uuid {
            sqlx::query_as::<_, BudgetRow>(
                "SELECT * FROM org_ai_budgets WHERE org_id = $1 AND workspace_id = $2",
            )
            .bind(org_uuid)
            .bind(ws_uuid)
            .fetch_optional(&self.pool)
            .await
            .map_err(|e| {
                mockforge_foundation::Error::internal(format!("Failed to load budget: {}", e))
            })?
        } else {
            sqlx::query_as::<_, BudgetRow>(
                "SELECT * FROM org_ai_budgets WHERE org_id = $1 AND workspace_id IS NULL",
            )
            .bind(org_uuid)
            .fetch_optional(&self.pool)
            .await
            .map_err(|e| {
                mockforge_foundation::Error::internal(format!("Failed to load budget: {}", e))
            })?
        };

        // Load rate limit config
        let rate_limit = if let Some(ws_uuid) = workspace_uuid {
            sqlx::query_as::<_, RateLimitRow>(
                "SELECT * FROM org_ai_rate_limits WHERE org_id = $1 AND workspace_id = $2",
            )
            .bind(org_uuid)
            .bind(ws_uuid)
            .fetch_optional(&self.pool)
            .await
            .map_err(|e| {
                mockforge_foundation::Error::internal(format!("Failed to load rate limit: {}", e))
            })?
        } else {
            sqlx::query_as::<_, RateLimitRow>(
                "SELECT * FROM org_ai_rate_limits WHERE org_id = $1 AND workspace_id IS NULL",
            )
            .bind(org_uuid)
            .fetch_optional(&self.pool)
            .await
            .map_err(|e| {
                mockforge_foundation::Error::internal(format!("Failed to load rate limit: {}", e))
            })?
        };

        // Load feature toggles
        let toggles_query = if let Some(ws_uuid) = workspace_uuid {
            sqlx::query_as::<_, FeatureToggleRow>(
                "SELECT * FROM org_ai_feature_toggles WHERE org_id = $1 AND workspace_id = $2",
            )
            .bind(org_uuid)
            .bind(ws_uuid)
            .fetch_all(&self.pool)
            .await
        } else {
            sqlx::query_as::<_, FeatureToggleRow>(
                "SELECT * FROM org_ai_feature_toggles WHERE org_id = $1 AND workspace_id IS NULL",
            )
            .bind(org_uuid)
            .fetch_all(&self.pool)
            .await
        };

        let toggles = toggles_query.map_err(|e| {
            mockforge_foundation::Error::internal(format!("Failed to load feature toggles: {}", e))
        })?;

        // If no config found, return None
        if budget.is_none() && rate_limit.is_none() && toggles.is_empty() {
            return Ok(None);
        }

        // Build config from database rows
        let budget_config = budget
            .map(|b| OrgBudgetConfig {
                max_tokens_per_period: b.max_tokens_per_period as u64,
                period_type: b.period_type,
                max_calls_per_period: b.max_calls_per_period as u64,
            })
            .unwrap_or_default();

        let rate_limit_config = rate_limit
            .map(|r| OrgRateLimitConfig {
                rate_limit_per_minute: r.rate_limit_per_minute as u64,
                rate_limit_per_hour: r.rate_limit_per_hour.map(|v| v as u64),
                rate_limit_per_day: r.rate_limit_per_day.map(|v| v as u64),
            })
            .unwrap_or_default();

        let mut feature_toggles = HashMap::new();
        for toggle in toggles {
            feature_toggles.insert(toggle.feature_name, toggle.enabled);
        }

        Ok(Some(OrgAiControlsConfig {
            budgets: budget_config,
            rate_limits: rate_limit_config,
            feature_toggles,
        }))
    }

    /// Check if budget allows the request
    async fn check_budget(
        &self,
        org_id: &str,
        workspace_id: Option<&str>,
        estimated_tokens: u64,
    ) -> Result<BudgetCheckResult> {
        let org_uuid = Uuid::parse_str(org_id).map_err(|e| {
            mockforge_foundation::Error::validation(format!("Invalid org_id: {}", e))
        })?;
        let workspace_uuid = workspace_id.and_then(|w| Uuid::parse_str(w).ok());

        // Get current budget
        let budget = if let Some(ws_uuid) = workspace_uuid {
            sqlx::query_as::<_, BudgetRow>(
                "SELECT * FROM org_ai_budgets WHERE org_id = $1 AND workspace_id = $2",
            )
            .bind(org_uuid)
            .bind(ws_uuid)
            .fetch_optional(&self.pool)
            .await
        } else {
            sqlx::query_as::<_, BudgetRow>(
                "SELECT * FROM org_ai_budgets WHERE org_id = $1 AND workspace_id IS NULL",
            )
            .bind(org_uuid)
            .fetch_optional(&self.pool)
            .await
        };

        let budget = budget.map_err(|e| {
            mockforge_foundation::Error::internal(format!("Failed to check budget: {}", e))
        })?;

        if let Some(b) = budget {
            // Check if period has expired and reset if needed
            let now = Utc::now();
            let period_start = b.period_start;
            let period_expired = match b.period_type.as_str() {
                "day" => (now - period_start).num_days() >= 1,
                "week" => (now - period_start).num_weeks() >= 1,
                "month" => (now - period_start).num_days() >= 30,
                "year" => (now - period_start).num_days() >= 365,
                _ => false,
            };

            let (current_tokens, current_calls, period_start_actual) = if period_expired {
                // Reset period
                let new_period_start = now;
                sqlx::query(
                    "UPDATE org_ai_budgets SET current_tokens_used = 0, current_calls_used = 0, period_start = $1 WHERE id = $2"
                )
                .bind(new_period_start)
                .bind(b.id)
                .execute(&self.pool)
                .await
                .map_err(|e| mockforge_foundation::Error::internal(format!("Failed to reset budget period: {}", e)))?;
                (0u64, 0u64, Some(new_period_start))
            } else {
                (b.current_tokens_used as u64, b.current_calls_used as u64, Some(period_start))
            };

            // Check limits
            let tokens_allowed =
                current_tokens + estimated_tokens <= b.max_tokens_per_period as u64;
            let calls_allowed = current_calls < b.max_calls_per_period as u64;
            let allowed = tokens_allowed && calls_allowed;

            Ok(BudgetCheckResult {
                allowed,
                current_tokens,
                max_tokens: b.max_tokens_per_period as u64,
                current_calls,
                max_calls: b.max_calls_per_period as u64,
                period_start: period_start_actual,
                reason: if !allowed {
                    Some(if !tokens_allowed {
                        "Token budget exceeded".to_string()
                    } else {
                        "Call limit exceeded".to_string()
                    })
                } else {
                    None
                },
            })
        } else {
            // No budget configured - allow
            Ok(BudgetCheckResult {
                allowed: true,
                current_tokens: 0,
                max_tokens: 0,
                current_calls: 0,
                max_calls: 0,
                period_start: None,
                reason: None,
            })
        }
    }

    /// Check if rate limit allows the request
    async fn check_rate_limit(
        &self,
        org_id: &str,
        workspace_id: Option<&str>,
    ) -> Result<RateLimitCheckResult> {
        let org_uuid = Uuid::parse_str(org_id).map_err(|e| {
            mockforge_foundation::Error::validation(format!("Invalid org_id: {}", e))
        })?;
        let workspace_uuid = workspace_id.and_then(|w| Uuid::parse_str(w).ok());

        // Load rate limit configuration
        let rate_limit_config = if let Some(ws_uuid) = workspace_uuid {
            sqlx::query_as::<_, RateLimitRow>(
                "SELECT * FROM org_ai_rate_limits WHERE org_id = $1 AND workspace_id = $2",
            )
            .bind(org_uuid)
            .bind(ws_uuid)
            .fetch_optional(&self.pool)
            .await
            .map_err(|e| {
                mockforge_foundation::Error::internal(format!("Failed to load rate limit: {}", e))
            })?
        } else {
            sqlx::query_as::<_, RateLimitRow>(
                "SELECT * FROM org_ai_rate_limits WHERE org_id = $1 AND workspace_id IS NULL",
            )
            .bind(org_uuid)
            .fetch_optional(&self.pool)
            .await
            .map_err(|e| {
                mockforge_foundation::Error::internal(format!("Failed to load rate limit: {}", e))
            })?
        };

        // If no rate limit config, allow the request
        let config = match rate_limit_config {
            Some(row) => row,
            None => {
                return Ok(RateLimitCheckResult {
                    allowed: true,
                    current_requests: 0,
                    max_requests: 1000, // Default high limit
                    window_type: "minute".to_string(),
                    retry_after: None,
                    reason: None,
                });
            }
        };

        // Determine which rate limit to use (prefer minute, then hour, then day)
        let (max_requests, window_type, window_seconds) = if config.rate_limit_per_minute > 0 {
            (config.rate_limit_per_minute as u64, "minute".to_string(), 60)
        } else if let Some(per_hour) = config.rate_limit_per_hour {
            if per_hour > 0 {
                (per_hour as u64, "hour".to_string(), 3600)
            } else if let Some(per_day) = config.rate_limit_per_day {
                if per_day > 0 {
                    (per_day as u64, "day".to_string(), 86400)
                } else {
                    (1000, "minute".to_string(), 60) // Default fallback
                }
            } else {
                (1000, "minute".to_string(), 60) // Default fallback
            }
        } else if let Some(per_day) = config.rate_limit_per_day {
            if per_day > 0 {
                (per_day as u64, "day".to_string(), 86400)
            } else {
                (1000, "minute".to_string(), 60) // Default fallback
            }
        } else {
            (1000, "minute".to_string(), 60) // Default fallback
        };

        // Calculate current window start time
        let now = Utc::now();
        let window_start = now.timestamp() / window_seconds * window_seconds;
        let window_start_dt =
            DateTime::<Utc>::from_timestamp(window_start, 0).ok_or_else(|| {
                mockforge_foundation::Error::internal("Invalid timestamp".to_string())
            })?;

        // Count requests in current window
        // We'll use a usage tracking table or create one if it doesn't exist
        // For now, we'll track in org_ai_usage table with a window_start column
        let current_requests: i64 = if let Some(ws_uuid) = workspace_uuid {
            sqlx::query_scalar::<_, i64>(
                r#"
                SELECT COUNT(*)
                FROM org_ai_usage
                WHERE org_id = $1
                  AND workspace_id = $2
                  AND created_at >= $3
                "#,
            )
            .bind(org_uuid)
            .bind(ws_uuid)
            .bind(window_start_dt)
            .fetch_one(&self.pool)
            .await
            .unwrap_or(0)
        } else {
            sqlx::query_scalar::<_, i64>(
                r#"
                SELECT COUNT(*)
                FROM org_ai_usage
                WHERE org_id = $1
                  AND workspace_id IS NULL
                  AND created_at >= $3
                "#,
            )
            .bind(org_uuid)
            .bind(window_start_dt)
            .fetch_one(&self.pool)
            .await
            .unwrap_or(0)
        };

        let current_requests = current_requests as u64;

        // Check if rate limit is exceeded
        if current_requests >= max_requests {
            // Calculate retry after (next window start)
            let next_window_start = window_start + window_seconds;
            let retry_after =
                DateTime::<Utc>::from_timestamp(next_window_start, 0).ok_or_else(|| {
                    mockforge_foundation::Error::internal("Invalid timestamp".to_string())
                })?;

            Ok(RateLimitCheckResult {
                allowed: false,
                current_requests,
                max_requests,
                window_type: window_type.clone(),
                retry_after: Some(retry_after),
                reason: Some(format!(
                    "Rate limit exceeded: {}/{} requests in current {} window",
                    current_requests, max_requests, window_type
                )),
            })
        } else {
            Ok(RateLimitCheckResult {
                allowed: true,
                current_requests,
                max_requests,
                window_type: window_type.clone(),
                retry_after: None,
                reason: None,
            })
        }
    }

    /// Check if a feature is enabled
    async fn is_feature_enabled(
        &self,
        org_id: &str,
        workspace_id: Option<&str>,
        feature: &str,
    ) -> Result<bool> {
        let org_uuid = Uuid::parse_str(org_id).map_err(|e| {
            mockforge_foundation::Error::validation(format!("Invalid org_id: {}", e))
        })?;
        let workspace_uuid = workspace_id.and_then(|w| Uuid::parse_str(w).ok());

        let result = if let Some(ws_uuid) = workspace_uuid {
            sqlx::query(
                "SELECT enabled FROM org_ai_feature_toggles WHERE org_id = $1 AND workspace_id = $2 AND feature_name = $3"
            )
            .bind(org_uuid)
            .bind(ws_uuid)
            .bind(feature)
            .fetch_optional(&self.pool)
            .await
        } else {
            sqlx::query(
                "SELECT enabled FROM org_ai_feature_toggles WHERE org_id = $1 AND workspace_id IS NULL AND feature_name = $2"
            )
            .bind(org_uuid)
            .bind(feature)
            .fetch_optional(&self.pool)
            .await
        };

        match result {
            Ok(Some(row)) => Ok(row.get::<bool, _>("enabled")),
            Ok(None) => Ok(true), // Default to enabled if not configured
            Err(e) => Err(mockforge_foundation::Error::internal(format!(
                "Failed to check feature: {}",
                e
            ))),
        }
    }

    /// Record usage for audit
    async fn record_usage(
        &self,
        org_id: &str,
        workspace_id: Option<&str>,
        user_id: Option<&str>,
        feature: AiFeature,
        tokens: u64,
        cost_usd: f64,
        metadata: Option<Value>,
    ) -> Result<()> {
        let org_uuid = Uuid::parse_str(org_id).map_err(|e| {
            mockforge_foundation::Error::validation(format!("Invalid org_id: {}", e))
        })?;
        let workspace_uuid = workspace_id.and_then(|w| Uuid::parse_str(w).ok());
        let user_uuid = user_id.and_then(|u| Uuid::parse_str(u).ok());

        let feature_name = match feature {
            AiFeature::MockAi => "mock_generation",
            AiFeature::ContractDiff => "contract_diff",
            AiFeature::PersonaGeneration => "persona_generation",
            AiFeature::DebugAnalysis => "debug_analysis",
            AiFeature::GenerativeSchema => "generative_schema",
            AiFeature::VoiceInterface => "voice_interface",
            AiFeature::GeneralChat => "free_form_generation",
        };

        // Insert usage log
        sqlx::query(
            "INSERT INTO org_ai_usage_logs (org_id, workspace_id, user_id, feature_name, tokens_used, cost_usd, metadata)
             VALUES ($1, $2, $3, $4, $5, $6, $7)"
        )
        .bind(org_uuid)
        .bind(workspace_uuid)
        .bind(user_uuid)
        .bind(feature_name)
        .bind(tokens as i64)
        .bind(cost_usd)
        .bind(metadata.unwrap_or_else(|| serde_json::json!({})))
        .execute(&self.pool)
        .await
        .map_err(|e| mockforge_foundation::Error::internal(format!("Failed to record usage: {}", e)))?;

        // Update budget counters
        if let Some(ws_uuid) = workspace_uuid {
            sqlx::query(
                "UPDATE org_ai_budgets
                 SET current_tokens_used = current_tokens_used + $1,
                     current_calls_used = current_calls_used + 1,
                     updated_at = NOW()
                 WHERE org_id = $2 AND workspace_id = $3",
            )
            .bind(tokens as i64)
            .bind(org_uuid)
            .bind(ws_uuid)
            .execute(&self.pool)
            .await
            .map_err(|e| {
                mockforge_foundation::Error::internal(format!("Failed to update budget: {}", e))
            })?;
        } else {
            sqlx::query(
                "UPDATE org_ai_budgets
                 SET current_tokens_used = current_tokens_used + $1,
                     current_calls_used = current_calls_used + 1,
                     updated_at = NOW()
                 WHERE org_id = $2 AND workspace_id IS NULL",
            )
            .bind(tokens as i64)
            .bind(org_uuid)
            .execute(&self.pool)
            .await
            .map_err(|e| {
                mockforge_foundation::Error::internal(format!("Failed to update budget: {}", e))
            })?;
        }

        Ok(())
    }
}

/// Database row for org_ai_budgets table
#[cfg(feature = "database")]
#[derive(sqlx::FromRow)]
#[allow(dead_code)]
struct BudgetRow {
    id: Uuid,
    org_id: Uuid,
    workspace_id: Option<Uuid>,
    max_tokens_per_period: i64,
    period_type: String,
    max_calls_per_period: i64,
    current_tokens_used: i64,
    current_calls_used: i64,
    period_start: DateTime<Utc>,
}

/// Database row for org_ai_rate_limits table
#[cfg(feature = "database")]
#[derive(sqlx::FromRow)]
#[allow(dead_code)]
struct RateLimitRow {
    id: Uuid,
    org_id: Uuid,
    workspace_id: Option<Uuid>,
    rate_limit_per_minute: i32,
    rate_limit_per_hour: Option<i32>,
    rate_limit_per_day: Option<i32>,
}

/// Database row for org_ai_feature_toggles table
#[cfg(feature = "database")]
#[derive(sqlx::FromRow)]
#[allow(dead_code)]
struct FeatureToggleRow {
    id: Uuid,
    org_id: Uuid,
    workspace_id: Option<Uuid>,
    feature_name: String,
    enabled: bool,
}