aof-core 0.4.0-beta

Core types, traits, and abstractions for AOF framework
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
// AOF Core - FlowBinding resource type
//
// FlowBinding ties together a Trigger, Context, and Flow to create
// a complete routing configuration. This enables:
// - Decoupled architecture (define each resource once, compose with bindings)
// - Multi-tenant deployments (same flow, different contexts)
// - Flexible routing (multiple bindings per trigger)

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// FlowBinding - Ties trigger + context + flow together
///
/// Example:
/// ```yaml
/// apiVersion: aof.dev/v1
/// kind: FlowBinding
/// metadata:
///   name: prod-k8s-binding
/// spec:
///   trigger: slack-prod-channel    # Reference to Trigger
///   context: prod                   # Reference to Context
///   flow: k8s-ops-flow             # Reference to Flow
///   match:
///     patterns: ["kubectl", "k8s"]
///     priority: 100
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FlowBinding {
    /// API version (e.g., "aof.dev/v1")
    #[serde(default = "default_api_version")]
    pub api_version: String,

    /// Resource kind, always "FlowBinding"
    #[serde(default = "default_binding_kind")]
    pub kind: String,

    /// Binding metadata
    pub metadata: FlowBindingMetadata,

    /// Binding specification
    pub spec: FlowBindingSpec,
}

fn default_api_version() -> String {
    "aof.dev/v1".to_string()
}

fn default_binding_kind() -> String {
    "FlowBinding".to_string()
}

/// FlowBinding metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FlowBindingMetadata {
    /// Binding name (unique identifier)
    pub name: String,

    /// Namespace
    #[serde(skip_serializing_if = "Option::is_none")]
    pub namespace: Option<String>,

    /// Labels for categorization
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub labels: HashMap<String, String>,

    /// Annotations for additional metadata
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub annotations: HashMap<String, String>,
}

/// FlowBinding specification
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct FlowBindingSpec {
    /// Reference to Trigger resource name
    pub trigger: String,

    /// Reference to Context resource name (optional - uses default if not specified)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context: Option<String>,

    /// Reference to Flow resource name
    pub flow: String,

    /// Optional: Agent reference (for simple single-agent flows)
    /// If specified, creates an implicit single-step flow
    #[serde(skip_serializing_if = "Option::is_none")]
    pub agent: Option<String>,

    /// Optional: Fleet reference (for multi-agent coordination)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fleet: Option<String>,

    /// Match configuration for this binding
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#match: Option<BindingMatch>,

    /// Whether this binding is enabled
    #[serde(default = "default_enabled")]
    pub enabled: bool,

    /// Additional configuration overrides
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub config: HashMap<String, serde_json::Value>,
}

fn default_enabled() -> bool {
    true
}

/// Match configuration for routing
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct BindingMatch {
    /// Message patterns to match (regex)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub patterns: Vec<String>,

    /// Priority for this binding (higher = more priority)
    #[serde(default)]
    pub priority: i32,

    /// Channel filter (override trigger's channels for this binding)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub channels: Vec<String>,

    /// User filter (override trigger's users for this binding)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub users: Vec<String>,

    /// Event filter (override trigger's events for this binding)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub events: Vec<String>,

    /// Required: Message must contain all of these
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub required_keywords: Vec<String>,

    /// Excluded: Message must not contain any of these
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub excluded_keywords: Vec<String>,
}

impl FlowBinding {
    /// Get the binding name
    pub fn name(&self) -> &str {
        &self.metadata.name
    }

    /// Get the trigger reference
    pub fn trigger_ref(&self) -> &str {
        &self.spec.trigger
    }

    /// Get the context reference (if specified)
    pub fn context_ref(&self) -> Option<&str> {
        self.spec.context.as_deref()
    }

    /// Get the flow reference
    pub fn flow_ref(&self) -> &str {
        &self.spec.flow
    }

    /// Get agent reference (for simple bindings)
    pub fn agent_ref(&self) -> Option<&str> {
        self.spec.agent.as_deref()
    }

    /// Get fleet reference
    pub fn fleet_ref(&self) -> Option<&str> {
        self.spec.fleet.as_deref()
    }

    /// Validate the binding configuration
    pub fn validate(&self) -> Result<(), String> {
        // Check name
        if self.metadata.name.is_empty() {
            return Err("FlowBinding name is required".to_string());
        }

        // Check trigger reference
        if self.spec.trigger.is_empty() {
            return Err("FlowBinding requires a trigger reference".to_string());
        }

        // Must have either flow, agent, or fleet
        if self.spec.flow.is_empty() && self.spec.agent.is_none() && self.spec.fleet.is_none() {
            return Err("FlowBinding requires flow, agent, or fleet reference".to_string());
        }

        Ok(())
    }

    /// Check if this binding matches the given message
    pub fn matches(&self, channel: Option<&str>, user: Option<&str>, text: Option<&str>) -> bool {
        if let Some(ref match_config) = self.spec.r#match {
            // Channel filter
            if !match_config.channels.is_empty() {
                if let Some(ch) = channel {
                    if !match_config.channels.iter().any(|c| c == ch) {
                        return false;
                    }
                } else {
                    return false;
                }
            }

            // User filter
            if !match_config.users.is_empty() {
                if let Some(u) = user {
                    if !match_config.users.iter().any(|allowed| allowed == u) {
                        return false;
                    }
                } else {
                    return false;
                }
            }

            // Pattern filter
            if !match_config.patterns.is_empty() {
                if let Some(t) = text {
                    let matches_pattern = match_config.patterns.iter().any(|p| {
                        if let Ok(re) = regex::Regex::new(p) {
                            re.is_match(t)
                        } else {
                            t.to_lowercase().contains(&p.to_lowercase())
                        }
                    });
                    if !matches_pattern {
                        return false;
                    }
                } else {
                    return false;
                }
            }

            // Required keywords
            if !match_config.required_keywords.is_empty() {
                if let Some(t) = text {
                    let text_lower = t.to_lowercase();
                    if !match_config.required_keywords.iter().all(|kw| text_lower.contains(&kw.to_lowercase())) {
                        return false;
                    }
                } else {
                    return false;
                }
            }

            // Excluded keywords
            if !match_config.excluded_keywords.is_empty() {
                if let Some(t) = text {
                    let text_lower = t.to_lowercase();
                    if match_config.excluded_keywords.iter().any(|kw| text_lower.contains(&kw.to_lowercase())) {
                        return false;
                    }
                }
            }
        }

        true
    }

    /// Calculate a match score for routing priority
    /// Higher score = more specific match = higher priority
    pub fn match_score(&self, channel: Option<&str>, user: Option<&str>, text: Option<&str>) -> i32 {
        if !self.matches(channel, user, text) {
            return i32::MIN;
        }

        let mut score = 0i32;

        if let Some(ref match_config) = self.spec.r#match {
            // Explicit priority
            score += match_config.priority;

            // Channel specificity
            if !match_config.channels.is_empty() && channel.is_some() {
                score += 100;
            }

            // User specificity
            if !match_config.users.is_empty() && user.is_some() {
                score += 80;
            }

            // Pattern specificity
            if !match_config.patterns.is_empty() && text.is_some() {
                score += 60;
            }

            // Required keywords add specificity
            if !match_config.required_keywords.is_empty() {
                score += 40 * match_config.required_keywords.len() as i32;
            }
        }

        // Base score for having a binding at all
        score += 10;

        score
    }
}

/// Result of binding resolution
#[derive(Debug, Clone)]
pub struct ResolvedBinding {
    /// The binding that matched
    pub binding: FlowBinding,

    /// Match score
    pub score: i32,

    /// Trigger name
    pub trigger_name: String,

    /// Context name (if specified)
    pub context_name: Option<String>,

    /// Flow name
    pub flow_name: String,
}

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

    #[test]
    fn test_parse_flow_binding() {
        let yaml = r#"
apiVersion: aof.dev/v1
kind: FlowBinding
metadata:
  name: prod-k8s-binding
  labels:
    environment: production
spec:
  trigger: slack-prod-channel
  context: prod
  flow: k8s-ops-flow
  match:
    patterns:
      - kubectl
      - k8s
    priority: 100
"#;

        let binding: FlowBinding = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(binding.metadata.name, "prod-k8s-binding");
        assert_eq!(binding.spec.trigger, "slack-prod-channel");
        assert_eq!(binding.spec.context, Some("prod".to_string()));
        assert_eq!(binding.spec.flow, "k8s-ops-flow");
        assert!(binding.validate().is_ok());
    }

    #[test]
    fn test_parse_simple_binding() {
        let yaml = r#"
apiVersion: aof.dev/v1
kind: FlowBinding
metadata:
  name: simple-binding
spec:
  trigger: telegram-oncall
  agent: incident-responder
  flow: incident-flow
"#;

        let binding: FlowBinding = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(binding.spec.agent, Some("incident-responder".to_string()));
        assert!(binding.validate().is_ok());
    }

    #[test]
    fn test_parse_binding_with_match() {
        let yaml = r#"
apiVersion: aof.dev/v1
kind: FlowBinding
metadata:
  name: k8s-only-binding
spec:
  trigger: slack-prod
  context: prod
  flow: k8s-ops-flow
  match:
    patterns:
      - "^kubectl"
      - "^k8s"
    channels:
      - production
    required_keywords:
      - pod
    excluded_keywords:
      - delete
    priority: 200
"#;

        let binding: FlowBinding = serde_yaml::from_str(yaml).unwrap();
        let match_config = binding.spec.r#match.as_ref().unwrap();
        assert_eq!(match_config.patterns.len(), 2);
        assert_eq!(match_config.channels.len(), 1);
        assert_eq!(match_config.required_keywords.len(), 1);
        assert_eq!(match_config.excluded_keywords.len(), 1);
        assert_eq!(match_config.priority, 200);
    }

    #[test]
    fn test_validation_errors() {
        // Empty name
        let yaml = r#"
apiVersion: aof.dev/v1
kind: FlowBinding
metadata:
  name: ""
spec:
  trigger: test
  flow: test
"#;
        let binding: FlowBinding = serde_yaml::from_str(yaml).unwrap();
        assert!(binding.validate().is_err());

        // Missing trigger
        let yaml2 = r#"
apiVersion: aof.dev/v1
kind: FlowBinding
metadata:
  name: test
spec:
  trigger: ""
  flow: test
"#;
        let binding2: FlowBinding = serde_yaml::from_str(yaml2).unwrap();
        assert!(binding2.validate().is_err());

        // Missing flow/agent/fleet
        let yaml3 = r#"
apiVersion: aof.dev/v1
kind: FlowBinding
metadata:
  name: test
spec:
  trigger: test
  flow: ""
"#;
        let binding3: FlowBinding = serde_yaml::from_str(yaml3).unwrap();
        assert!(binding3.validate().is_err());
    }

    #[test]
    fn test_matches() {
        let yaml = r#"
apiVersion: aof.dev/v1
kind: FlowBinding
metadata:
  name: test
spec:
  trigger: test
  flow: test-flow
  match:
    patterns:
      - kubectl
    channels:
      - production
    required_keywords:
      - pod
    excluded_keywords:
      - delete
"#;

        let binding: FlowBinding = serde_yaml::from_str(yaml).unwrap();

        // Matches
        assert!(binding.matches(Some("production"), None, Some("kubectl get pod")));

        // Wrong channel
        assert!(!binding.matches(Some("staging"), None, Some("kubectl get pod")));

        // Missing required keyword
        assert!(!binding.matches(Some("production"), None, Some("kubectl get deployment")));

        // Contains excluded keyword
        assert!(!binding.matches(Some("production"), None, Some("kubectl delete pod")));
    }

    #[test]
    fn test_match_score() {
        // Binding with high specificity
        let yaml1 = r#"
apiVersion: aof.dev/v1
kind: FlowBinding
metadata:
  name: specific
spec:
  trigger: test
  flow: test
  match:
    patterns: [kubectl]
    channels: [production]
    priority: 50
"#;

        // Binding with low specificity
        let yaml2 = r#"
apiVersion: aof.dev/v1
kind: FlowBinding
metadata:
  name: catchall
spec:
  trigger: test
  flow: test
"#;

        let specific: FlowBinding = serde_yaml::from_str(yaml1).unwrap();
        let catchall: FlowBinding = serde_yaml::from_str(yaml2).unwrap();

        let score1 = specific.match_score(Some("production"), None, Some("kubectl get pods"));
        let score2 = catchall.match_score(Some("production"), None, Some("kubectl get pods"));

        // More specific binding should have higher score
        assert!(score1 > score2);
    }

    #[test]
    fn test_disabled_binding() {
        let yaml = r#"
apiVersion: aof.dev/v1
kind: FlowBinding
metadata:
  name: disabled
spec:
  trigger: test
  flow: test
  enabled: false
"#;

        let binding: FlowBinding = serde_yaml::from_str(yaml).unwrap();
        assert!(!binding.spec.enabled);
    }
}