rag-module 0.6.7

Enterprise RAG module with chat context storage, vector search, session management, and model downloading. Rust implementation with Node.js compatibility.
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
//! Operation context service for parsing user requests and service detection

use anyhow::{Result, anyhow};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use regex::Regex;
use crate::config::service_registry::{ServiceRegistry, OperationConfig, ValidationRule};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParsedOperation {
    pub parsed_successfully: bool,
    pub service: String,
    pub operation: String,
    pub confidence: f32,
    pub resource_name: Option<String>,
    pub extracted_params: HashMap<String, String>,
    pub parsing_method: String,
    pub error_message: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationResult {
    pub is_valid: bool,
    pub error_messages: Vec<String>,
    pub validated_value: Option<String>,
}

pub struct OperationContextService {
    service_registry: ServiceRegistry,
    operation_mappings: HashMap<String, String>,
    service_patterns: HashMap<String, Vec<Regex>>,
}

impl OperationContextService {
    pub async fn new() -> Result<Self> {
        let service_registry = ServiceRegistry::default();
        let operation_mappings = Self::load_operation_mappings();
        let service_patterns = Self::load_service_patterns();
        
        Ok(Self {
            service_registry,
            operation_mappings,
            service_patterns,
        })
    }
    
    pub async fn initialize(&self) -> Result<()> {
        Ok(())
    }
    
    pub async fn shutdown(&self) -> Result<()> {
        Ok(())
    }

    pub fn parse_operation(&self, user_request: &str) -> ParsedOperation {
        let user_request = user_request.to_lowercase();
        
        // Try RAG-first approach (simplified version - would integrate with actual RAG module)
        if let Some(parsed) = self.try_rag_parsing(&user_request) {
            return parsed;
        }
        
        // Fallback to keyword-based parsing
        self.parse_with_keywords(&user_request)
    }

    fn try_rag_parsing(&self, user_request: &str) -> Option<ParsedOperation> {
        // This would integrate with the actual RAG module for semantic parsing
        // For now, return None to use keyword fallback
        None
    }

    fn parse_with_keywords(&self, user_request: &str) -> ParsedOperation {
        // Detect service
        if let Some((service, confidence)) = self.detect_service(user_request) {
            // Detect operation
            if let Some(operation) = self.detect_operation(user_request, &service) {
                // Extract resource name
                let resource_name = self.extract_resource_name(user_request, &service);
                
                // Extract parameters
                let extracted_params = self.extract_parameters(user_request, &service, &operation);
                
                return ParsedOperation {
                    parsed_successfully: true,
                    service: service.clone(),
                    operation: operation.clone(),
                    confidence,
                    resource_name,
                    extracted_params,
                    parsing_method: "keyword-based".to_string(),
                    error_message: None,
                };
            }
        }
        
        // Failed to parse
        ParsedOperation {
            parsed_successfully: false,
            service: String::new(),
            operation: String::new(),
            confidence: 0.0,
            resource_name: None,
            extracted_params: HashMap::new(),
            parsing_method: "failed".to_string(),
            error_message: Some("Could not parse user request".to_string()),
        }
    }

    fn detect_service(&self, user_request: &str) -> Option<(String, f32)> {
        let mut best_match = None;
        let mut best_confidence = 0.0;
        
        for (service, patterns) in &self.service_patterns {
            for pattern in patterns {
                if pattern.is_match(user_request) {
                    let confidence = 0.8; // Could be more sophisticated
                    if confidence > best_confidence {
                        best_match = Some(service.clone());
                        best_confidence = confidence;
                    }
                }
            }
        }
        
        best_match.map(|service| (service, best_confidence))
    }

    fn detect_operation(&self, user_request: &str, service: &str) -> Option<String> {
        // Get operations for this service
        let operations = self.service_registry.get_service_operations(service);
        
        for operation in operations {
            if let Some(keywords) = self.get_operation_keywords(operation) {
                for keyword in keywords {
                    if user_request.contains(&keyword) {
                        return Some(operation.to_string());
                    }
                }
            }
        }
        
        None
    }

    fn get_operation_keywords(&self, operation: &str) -> Option<Vec<String>> {
        match operation {
            "stop_instance" | "stop_db_instance" => Some(vec![
                "stop".to_string(), "halt".to_string(), "shutdown".to_string(), "terminate".to_string()
            ]),
            "start_instance" | "start_db_instance" => Some(vec![
                "start".to_string(), "boot".to_string(), "launch".to_string(), "run".to_string()
            ]),
            "delete_bucket" => Some(vec![
                "delete".to_string(), "remove".to_string(), "destroy".to_string()
            ]),
            _ => None,
        }
    }

    fn extract_resource_name(&self, user_request: &str, service: &str) -> Option<String> {
        // Simple regex patterns to extract resource names
        let patterns = match service {
            "aws_ec2" => vec![
                Regex::new(r"instance[:\s]+([a-zA-Z0-9\-_]+)").unwrap(),
                Regex::new(r"i-[0-9a-f]+").unwrap(),
            ],
            "aws_rds" => vec![
                Regex::new(r"database[:\s]+([a-zA-Z0-9\-_]+)").unwrap(),
                Regex::new(r"db[:\s]+([a-zA-Z0-9\-_]+)").unwrap(),
            ],
            "aws_s3" => vec![
                Regex::new(r"bucket[:\s]+([a-zA-Z0-9\-_\.]+)").unwrap(),
            ],
            _ => vec![],
        };
        
        for pattern in patterns {
            if let Some(captures) = pattern.captures(user_request) {
                if let Some(name) = captures.get(1) {
                    return Some(name.as_str().to_string());
                } else if let Some(name) = captures.get(0) {
                    return Some(name.as_str().to_string());
                }
            }
        }
        
        None
    }

    fn extract_parameters(&self, user_request: &str, service: &str, operation: &str) -> HashMap<String, String> {
        let mut params = HashMap::new();
        
        // Extract region
        if let Some(region) = self.extract_region(user_request) {
            params.insert("region".to_string(), region);
        }
        
        // Extract force parameter
        if user_request.contains("force") || user_request.contains("forcefully") {
            params.insert("force".to_string(), "true".to_string());
        }
        
        // Service-specific parameter extraction
        match service {
            "aws_rds" => {
                if user_request.contains("snapshot") {
                    if let Some(snapshot_id) = self.extract_snapshot_id(user_request) {
                        params.insert("snapshot_id".to_string(), snapshot_id);
                    }
                }
            },
            _ => {}
        }
        
        params
    }

    fn extract_region(&self, user_request: &str) -> Option<String> {
        let region_patterns = vec![
            Regex::new(r"in\s+(us-[a-z]+-\d+)").unwrap(),
            Regex::new(r"region[:\s]+(us-[a-z]+-\d+)").unwrap(),
            Regex::new(r"(eu-[a-z]+-\d+)").unwrap(),
            Regex::new(r"(ap-[a-z]+-\d+)").unwrap(),
        ];
        
        for pattern in region_patterns {
            if let Some(captures) = pattern.captures(user_request) {
                if let Some(region) = captures.get(1) {
                    return Some(region.as_str().to_string());
                }
            }
        }
        
        None
    }

    fn extract_snapshot_id(&self, user_request: &str) -> Option<String> {
        let snapshot_pattern = Regex::new(r"snapshot[:\s]+([a-zA-Z0-9\-_]+)").unwrap();
        
        if let Some(captures) = snapshot_pattern.captures(user_request) {
            if let Some(snapshot_id) = captures.get(1) {
                return Some(snapshot_id.as_str().to_string());
            }
        }
        
        None
    }

    pub fn get_operation_config(&self, service: &str, operation: &str) -> Option<&OperationConfig> {
        self.service_registry.get_operation_config(service, operation)
    }

    pub fn validate_parameter(
        &self,
        service: &str,
        operation: &str,
        param_name: &str,
        value: &str,
    ) -> ValidationResult {
        if let Some(config) = self.get_operation_config(service, operation) {
            if let Some(validation_rule) = config.validation_rules.get(param_name) {
                return self.apply_validation_rule(validation_rule, value);
            }
        }
        
        // Default validation - just check if not empty
        ValidationResult {
            is_valid: !value.trim().is_empty(),
            error_messages: if value.trim().is_empty() {
                vec!["Value cannot be empty".to_string()]
            } else {
                vec![]
            },
            validated_value: Some(value.to_string()),
        }
    }

    fn apply_validation_rule(&self, rule: &ValidationRule, value: &str) -> ValidationResult {
        let mut errors = Vec::new();
        
        // Check type
        match rule.rule_type.as_str() {
            "string" => {
                if let Some(min_len) = rule.min_length {
                    if value.len() < min_len {
                        errors.push(format!("Value must be at least {} characters long", min_len));
                    }
                }
                
                if let Some(max_len) = rule.max_length {
                    if value.len() > max_len {
                        errors.push(format!("Value must be no more than {} characters long", max_len));
                    }
                }
            },
            "boolean" => {
                if !matches!(value, "true" | "false" | "1" | "0" | "yes" | "no") {
                    errors.push("Value must be a boolean (true/false)".to_string());
                }
            },
            _ => {}
        }
        
        // Check pattern
        if let Some(pattern_str) = &rule.pattern {
            if let Ok(pattern) = Regex::new(pattern_str) {
                if !pattern.is_match(value) {
                    errors.push(format!("Value does not match required pattern: {}", pattern_str));
                }
            }
        }
        
        // Check enum values
        if let Some(enum_values) = &rule.enum_values {
            if !enum_values.contains(&value.to_string()) {
                errors.push(format!("Value must be one of: {}", enum_values.join(", ")));
            }
        }
        
        let is_valid = errors.is_empty();
        ValidationResult {
            is_valid,
            error_messages: errors,
            validated_value: if is_valid { Some(value.to_string()) } else { None },
        }
    }

    pub fn is_data_confidential(&self, service: &str, operation: &str, field_name: &str) -> bool {
        if let Some(config) = self.get_operation_config(service, operation) {
            config.confidential_data.contains(&field_name.to_string())
        } else {
            false
        }
    }

    pub fn is_data_non_confidential(&self, service: &str, operation: &str, field_name: &str) -> bool {
        if let Some(config) = self.get_operation_config(service, operation) {
            config.non_confidential_data.contains(&field_name.to_string())
        } else {
            false
        }
    }

    pub fn get_required_parameters(&self, service: &str, operation: &str) -> Vec<String> {
        if let Some(config) = self.get_operation_config(service, operation) {
            config.required_params.clone()
        } else {
            vec![]
        }
    }

    pub fn get_optional_parameters(&self, service: &str, operation: &str) -> Vec<String> {
        if let Some(config) = self.get_operation_config(service, operation) {
            config.optional_params.clone()
        } else {
            vec![]
        }
    }

    pub fn get_all_services(&self) -> Vec<String> {
        self.service_registry.get_all_services()
            .into_iter()
            .map(|s| s.to_string())
            .collect()
    }

    pub fn get_service_operations(&self, service: &str) -> Vec<String> {
        self.service_registry.get_service_operations(service)
            .into_iter()
            .map(|op| op.to_string())
            .collect()
    }

    fn load_operation_mappings() -> HashMap<String, String> {
        let mut mappings = HashMap::new();
        
        // Stop operations
        mappings.insert("halt".to_string(), "stop".to_string());
        mappings.insert("shutdown".to_string(), "stop".to_string());
        mappings.insert("terminate".to_string(), "stop".to_string());
        
        // Start operations
        mappings.insert("boot".to_string(), "start".to_string());
        mappings.insert("launch".to_string(), "start".to_string());
        mappings.insert("run".to_string(), "start".to_string());
        
        // Delete operations
        mappings.insert("remove".to_string(), "delete".to_string());
        mappings.insert("destroy".to_string(), "delete".to_string());
        
        mappings
    }

    fn load_service_patterns() -> HashMap<String, Vec<Regex>> {
        let mut patterns = HashMap::new();
        
        patterns.insert("aws_ec2".to_string(), vec![
            Regex::new(r"ec2|instance").unwrap(),
            Regex::new(r"virtual machine|vm").unwrap(),
            Regex::new(r"compute|server").unwrap(),
        ]);
        
        patterns.insert("aws_rds".to_string(), vec![
            Regex::new(r"rds|database|db").unwrap(),
            Regex::new(r"mysql|postgres|oracle").unwrap(),
            Regex::new(r"sql server").unwrap(),
        ]);
        
        patterns.insert("aws_s3".to_string(), vec![
            Regex::new(r"s3|bucket").unwrap(),
            Regex::new(r"storage|object").unwrap(),
            Regex::new(r"file|blob").unwrap(),
        ]);
        
        patterns
    }

    pub fn get_parsing_summary(&self, parsed: &ParsedOperation) -> String {
        if parsed.parsed_successfully {
            format!(
                "Successfully parsed '{}' operation for '{}' service (confidence: {:.1}%) using {} method",
                parsed.operation, parsed.service, parsed.confidence * 100.0, parsed.parsing_method
            )
        } else {
            format!(
                "Failed to parse request: {}",
                parsed.error_message.as_ref().unwrap_or(&"Unknown error".to_string())
            )
        }
    }
}