ruvector-postgres 2.0.5

High-performance PostgreSQL vector database extension v2 - pgvector drop-in replacement with 230+ SQL functions, SIMD acceleration, Flash Attention, GNN layers, hybrid search, multi-tenancy, self-healing, and self-learning capabilities
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
// Agent Registry and Management
//
// Thread-safe registry for managing AI agents with capabilities and performance metrics.

use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// Type of AI agent
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AgentType {
    /// Language model (GPT, Claude, etc.)
    LLM,
    /// Embedding model
    Embedding,
    /// Specialized task agent
    Specialized,
    /// Vision model
    Vision,
    /// Audio model
    Audio,
    /// Multimodal agent
    Multimodal,
    /// Custom agent type
    Custom(String),
}

impl AgentType {
    /// Parse agent type from string
    pub fn from_str(s: &str) -> Self {
        match s.to_lowercase().as_str() {
            "llm" => AgentType::LLM,
            "embedding" => AgentType::Embedding,
            "specialized" => AgentType::Specialized,
            "vision" => AgentType::Vision,
            "audio" => AgentType::Audio,
            "multimodal" => AgentType::Multimodal,
            _ => AgentType::Custom(s.to_string()),
        }
    }

    /// Convert to string
    pub fn as_str(&self) -> &str {
        match self {
            AgentType::LLM => "llm",
            AgentType::Embedding => "embedding",
            AgentType::Specialized => "specialized",
            AgentType::Vision => "vision",
            AgentType::Audio => "audio",
            AgentType::Multimodal => "multimodal",
            AgentType::Custom(s) => s,
        }
    }
}

/// Cost model for agent usage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CostModel {
    /// Cost per request
    pub per_request: f32,
    /// Cost per token (if applicable)
    pub per_token: Option<f32>,
    /// Fixed monthly cost
    pub monthly_fixed: Option<f32>,
}

impl Default for CostModel {
    fn default() -> Self {
        Self {
            per_request: 0.0,
            per_token: None,
            monthly_fixed: None,
        }
    }
}

/// Performance metrics for an agent
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceMetrics {
    /// Average latency in milliseconds
    pub avg_latency_ms: f32,
    /// 95th percentile latency
    pub p95_latency_ms: f32,
    /// 99th percentile latency
    pub p99_latency_ms: f32,
    /// Quality score (0-1)
    pub quality_score: f32,
    /// Success rate (0-1)
    pub success_rate: f32,
    /// Total requests processed
    pub total_requests: u64,
}

impl Default for PerformanceMetrics {
    fn default() -> Self {
        Self {
            avg_latency_ms: 100.0,
            p95_latency_ms: 200.0,
            p99_latency_ms: 500.0,
            quality_score: 0.8,
            success_rate: 0.99,
            total_requests: 0,
        }
    }
}

/// AI Agent definition with capabilities and metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Agent {
    /// Unique agent name
    pub name: String,
    /// Agent type
    pub agent_type: AgentType,
    /// Capabilities (e.g., ["code_generation", "translation"])
    pub capabilities: Vec<String>,
    /// Cost model
    pub cost_model: CostModel,
    /// Performance metrics
    pub performance: PerformanceMetrics,
    /// Agent embedding for similarity matching (384-dim)
    pub embedding: Option<Vec<f32>>,
    /// Whether agent is currently active
    pub is_active: bool,
    /// Additional metadata
    pub metadata: serde_json::Value,
}

impl Agent {
    /// Create a new agent
    pub fn new(name: String, agent_type: AgentType, capabilities: Vec<String>) -> Self {
        Self {
            name,
            agent_type,
            capabilities,
            cost_model: CostModel::default(),
            performance: PerformanceMetrics::default(),
            embedding: None,
            is_active: true,
            metadata: serde_json::Value::Null,
        }
    }

    /// Check if agent has a specific capability
    pub fn has_capability(&self, capability: &str) -> bool {
        self.capabilities
            .iter()
            .any(|c| c.eq_ignore_ascii_case(capability))
    }

    /// Calculate total cost for a request
    pub fn calculate_cost(&self, token_count: Option<u32>) -> f32 {
        let mut cost = self.cost_model.per_request;

        if let (Some(tokens), Some(per_token)) = (token_count, self.cost_model.per_token) {
            cost += tokens as f32 * per_token;
        }

        cost
    }

    /// Update performance metrics with new observation
    pub fn update_metrics(&mut self, latency_ms: f32, success: bool, quality: Option<f32>) {
        let n = self.performance.total_requests as f32;
        let new_n = n + 1.0;

        // Update average latency with exponential moving average
        self.performance.avg_latency_ms =
            (self.performance.avg_latency_ms * n + latency_ms) / new_n;

        // Update success rate
        let prev_successes = (self.performance.success_rate * n) as u64;
        let new_successes = prev_successes + if success { 1 } else { 0 };
        self.performance.success_rate = new_successes as f32 / new_n;

        // Update quality score if provided
        if let Some(q) = quality {
            self.performance.quality_score = (self.performance.quality_score * n + q) / new_n;
        }

        self.performance.total_requests += 1;

        // Update percentiles (simplified approach)
        if latency_ms > self.performance.avg_latency_ms * 1.5 {
            self.performance.p95_latency_ms =
                (self.performance.p95_latency_ms * 0.95 + latency_ms * 0.05).max(latency_ms);
        }
        if latency_ms > self.performance.avg_latency_ms * 2.0 {
            self.performance.p99_latency_ms =
                (self.performance.p99_latency_ms * 0.99 + latency_ms * 0.01).max(latency_ms);
        }
    }
}

/// Thread-safe agent registry
pub struct AgentRegistry {
    /// Agents stored by name
    agents: Arc<DashMap<String, Agent>>,
}

impl AgentRegistry {
    /// Create a new agent registry
    pub fn new() -> Self {
        Self {
            agents: Arc::new(DashMap::new()),
        }
    }

    /// Register a new agent
    pub fn register(&self, agent: Agent) -> Result<(), String> {
        if self.agents.contains_key(&agent.name) {
            return Err(format!("Agent '{}' already exists", agent.name));
        }

        self.agents.insert(agent.name.clone(), agent);
        Ok(())
    }

    /// Update an existing agent
    pub fn update(&self, agent: Agent) -> Result<(), String> {
        if !self.agents.contains_key(&agent.name) {
            return Err(format!("Agent '{}' not found", agent.name));
        }

        self.agents.insert(agent.name.clone(), agent);
        Ok(())
    }

    /// Get an agent by name
    pub fn get(&self, name: &str) -> Option<Agent> {
        self.agents.get(name).map(|entry| entry.clone())
    }

    /// Remove an agent
    pub fn remove(&self, name: &str) -> Option<Agent> {
        self.agents.remove(name).map(|(_, agent)| agent)
    }

    /// List all active agents
    pub fn list_active(&self) -> Vec<Agent> {
        self.agents
            .iter()
            .filter(|entry| entry.is_active)
            .map(|entry| entry.clone())
            .collect()
    }

    /// List all agents
    pub fn list_all(&self) -> Vec<Agent> {
        self.agents.iter().map(|entry| entry.clone()).collect()
    }

    /// Find agents by capability
    pub fn find_by_capability(&self, capability: &str, k: usize) -> Vec<Agent> {
        let mut agents: Vec<Agent> = self
            .agents
            .iter()
            .filter(|entry| entry.is_active && entry.has_capability(capability))
            .map(|entry| entry.clone())
            .collect();

        // Sort by quality score (descending)
        agents.sort_by(|a, b| {
            b.performance
                .quality_score
                .partial_cmp(&a.performance.quality_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        agents.into_iter().take(k).collect()
    }

    /// Find agents by type
    pub fn find_by_type(&self, agent_type: &AgentType) -> Vec<Agent> {
        self.agents
            .iter()
            .filter(|entry| entry.is_active && &entry.agent_type == agent_type)
            .map(|entry| entry.clone())
            .collect()
    }

    /// Get agent count
    pub fn count(&self) -> usize {
        self.agents.len()
    }

    /// Get active agent count
    pub fn count_active(&self) -> usize {
        self.agents.iter().filter(|entry| entry.is_active).count()
    }

    /// Clear all agents
    pub fn clear(&self) {
        self.agents.clear();
    }
}

impl Default for AgentRegistry {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_agent_type_parsing() {
        assert_eq!(AgentType::from_str("llm"), AgentType::LLM);
        assert_eq!(AgentType::from_str("LLM"), AgentType::LLM);
        assert_eq!(AgentType::from_str("embedding"), AgentType::Embedding);
        assert_eq!(
            AgentType::from_str("custom"),
            AgentType::Custom("custom".to_string())
        );
    }

    #[test]
    fn test_agent_creation() {
        let agent = Agent::new(
            "gpt-4".to_string(),
            AgentType::LLM,
            vec!["code_generation".to_string(), "translation".to_string()],
        );

        assert_eq!(agent.name, "gpt-4");
        assert_eq!(agent.agent_type, AgentType::LLM);
        assert_eq!(agent.capabilities.len(), 2);
        assert!(agent.is_active);
    }

    #[test]
    fn test_agent_has_capability() {
        let agent = Agent::new(
            "test".to_string(),
            AgentType::LLM,
            vec!["code_generation".to_string()],
        );

        assert!(agent.has_capability("code_generation"));
        assert!(agent.has_capability("CODE_GENERATION"));
        assert!(!agent.has_capability("translation"));
    }

    #[test]
    fn test_agent_cost_calculation() {
        let mut agent = Agent::new("test".to_string(), AgentType::LLM, vec![]);
        agent.cost_model.per_request = 0.01;
        agent.cost_model.per_token = Some(0.0001);

        assert_eq!(agent.calculate_cost(None), 0.01);
        assert_eq!(agent.calculate_cost(Some(1000)), 0.11); // 0.01 + 1000 * 0.0001
    }

    #[test]
    fn test_agent_update_metrics() {
        let mut agent = Agent::new("test".to_string(), AgentType::LLM, vec![]);

        // Initial state
        assert_eq!(agent.performance.total_requests, 0);

        // Add first observation
        agent.update_metrics(100.0, true, Some(0.9));
        assert_eq!(agent.performance.total_requests, 1);
        assert_eq!(agent.performance.avg_latency_ms, 100.0);
        assert_eq!(agent.performance.success_rate, 1.0);
        assert_eq!(agent.performance.quality_score, 0.9);

        // Add second observation
        agent.update_metrics(200.0, true, Some(0.8));
        assert_eq!(agent.performance.total_requests, 2);
        assert_eq!(agent.performance.avg_latency_ms, 150.0);
        assert_eq!(agent.performance.success_rate, 1.0);
        assert!((agent.performance.quality_score - 0.85).abs() < 0.01);
    }

    #[test]
    fn test_registry_register() {
        let registry = AgentRegistry::new();
        let agent = Agent::new("test".to_string(), AgentType::LLM, vec![]);

        assert!(registry.register(agent.clone()).is_ok());
        assert_eq!(registry.count(), 1);

        // Duplicate registration should fail
        assert!(registry.register(agent).is_err());
    }

    #[test]
    fn test_registry_get() {
        let registry = AgentRegistry::new();
        let agent = Agent::new("test".to_string(), AgentType::LLM, vec![]);

        registry.register(agent.clone()).unwrap();

        let retrieved = registry.get("test").unwrap();
        assert_eq!(retrieved.name, "test");

        assert!(registry.get("nonexistent").is_none());
    }

    #[test]
    fn test_registry_remove() {
        let registry = AgentRegistry::new();
        let agent = Agent::new("test".to_string(), AgentType::LLM, vec![]);

        registry.register(agent).unwrap();
        assert_eq!(registry.count(), 1);

        let removed = registry.remove("test").unwrap();
        assert_eq!(removed.name, "test");
        assert_eq!(registry.count(), 0);
    }

    #[test]
    fn test_registry_list_active() {
        let registry = AgentRegistry::new();

        let mut agent1 = Agent::new("active".to_string(), AgentType::LLM, vec![]);
        agent1.is_active = true;

        let mut agent2 = Agent::new("inactive".to_string(), AgentType::LLM, vec![]);
        agent2.is_active = false;

        registry.register(agent1).unwrap();
        registry.register(agent2).unwrap();

        let active = registry.list_active();
        assert_eq!(active.len(), 1);
        assert_eq!(active[0].name, "active");
    }

    #[test]
    fn test_registry_find_by_capability() {
        let registry = AgentRegistry::new();

        let agent1 = Agent::new(
            "agent1".to_string(),
            AgentType::LLM,
            vec!["coding".to_string()],
        );
        let agent2 = Agent::new(
            "agent2".to_string(),
            AgentType::LLM,
            vec!["translation".to_string()],
        );
        let agent3 = Agent::new(
            "agent3".to_string(),
            AgentType::LLM,
            vec!["coding".to_string(), "translation".to_string()],
        );

        registry.register(agent1).unwrap();
        registry.register(agent2).unwrap();
        registry.register(agent3).unwrap();

        let coders = registry.find_by_capability("coding", 10);
        assert_eq!(coders.len(), 2);

        let translators = registry.find_by_capability("translation", 10);
        assert_eq!(translators.len(), 2);
    }

    #[test]
    fn test_registry_find_by_type() {
        let registry = AgentRegistry::new();

        registry
            .register(Agent::new("llm1".to_string(), AgentType::LLM, vec![]))
            .unwrap();
        registry
            .register(Agent::new("llm2".to_string(), AgentType::LLM, vec![]))
            .unwrap();
        registry
            .register(Agent::new(
                "embed1".to_string(),
                AgentType::Embedding,
                vec![],
            ))
            .unwrap();

        let llms = registry.find_by_type(&AgentType::LLM);
        assert_eq!(llms.len(), 2);

        let embeddings = registry.find_by_type(&AgentType::Embedding);
        assert_eq!(embeddings.len(), 1);
    }

    #[test]
    fn test_registry_clear() {
        let registry = AgentRegistry::new();
        registry
            .register(Agent::new("test".to_string(), AgentType::LLM, vec![]))
            .unwrap();

        assert_eq!(registry.count(), 1);
        registry.clear();
        assert_eq!(registry.count(), 0);
    }
}