mofa-foundation 0.1.1

MoFA Foundation - Core building blocks and utilities
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
577
578
//! Concrete Reducer Implementations
//!
//! This module provides built-in reducers for state management in workflows.
//! Reducers determine how state updates are merged with existing values.

use async_trait::async_trait;
use mofa_kernel::workflow::{Reducer, ReducerType, StateUpdate};
use serde_json::Value;

use mofa_kernel::agent::error::{AgentError, AgentResult};

/// Overwrite reducer - replaces the current value with the update
///
/// This is the default reducer behavior. The new value completely
/// replaces any existing value.
///
/// # Example
///
/// ```rust,ignore
/// // Before: { "result": "old" }
/// // Update: { "result": "new" }
/// // After:  { "result": "new" }
/// ```
#[derive(Debug, Clone, Default)]
pub struct OverwriteReducer;

#[async_trait]
impl Reducer for OverwriteReducer {
    async fn reduce(&self, _current: Option<&Value>, update: &Value) -> AgentResult<Value> {
        Ok(update.clone())
    }

    fn name(&self) -> &str {
        "overwrite"
    }

    fn reducer_type(&self) -> ReducerType {
        ReducerType::Overwrite
    }
}

/// Append reducer - appends the update to a list
///
/// If the current value doesn't exist or isn't an array, creates a new array.
/// The update value is appended to the array.
///
/// # Example
///
/// ```rust,ignore
/// // Before: { "messages": ["hello"] }
/// // Update: { "messages": "world" }
/// // After:  { "messages": ["hello", "world"] }
/// ```
#[derive(Debug, Clone, Default)]
pub struct AppendReducer;

#[async_trait]
impl Reducer for AppendReducer {
    async fn reduce(&self, current: Option<&Value>, update: &Value) -> AgentResult<Value> {
        let mut arr = match current {
            Some(Value::Array(a)) => a.clone(),
            _ => Vec::new(),
        };
        arr.push(update.clone());
        Ok(Value::Array(arr))
    }

    fn name(&self) -> &str {
        "append"
    }

    fn reducer_type(&self) -> ReducerType {
        ReducerType::Append
    }
}

/// Extend reducer - extends a list with items from another list
///
/// If the update is an array, all items are added to the current array.
/// If the current value doesn't exist or isn't an array, creates a new array.
///
/// # Example
///
/// ```rust,ignore
/// // Before: { "items": [1, 2] }
/// // Update: { "items": [3, 4, 5] }
/// // After:  { "items": [1, 2, 3, 4, 5] }
/// ```
#[derive(Debug, Clone, Default)]
pub struct ExtendReducer;

#[async_trait]
impl Reducer for ExtendReducer {
    async fn reduce(&self, current: Option<&Value>, update: &Value) -> AgentResult<Value> {
        let mut arr = match current {
            Some(Value::Array(a)) => a.clone(),
            _ => Vec::new(),
        };

        match update {
            Value::Array(items) => {
                arr.extend(items.iter().cloned());
            }
            other => {
                arr.push(other.clone());
            }
        }

        Ok(Value::Array(arr))
    }

    fn name(&self) -> &str {
        "extend"
    }

    fn reducer_type(&self) -> ReducerType {
        ReducerType::Extend
    }
}

/// Merge reducer - merges the update into the current object
///
/// Performs a shallow or deep merge of two objects.
///
/// # Example
///
/// ```rust,ignore
/// // Before: { "config": { "a": 1, "b": 2 } }
/// // Update: { "config": { "b": 3, "c": 4 } }
/// // After (shallow): { "config": { "a": 1, "b": 3, "c": 4 } }
/// ```
#[derive(Debug, Clone)]
pub struct MergeReducer {
    /// Whether to perform deep merge on nested objects
    pub deep: bool,
}

impl Default for MergeReducer {
    fn default() -> Self {
        Self { deep: false }
    }
}

impl MergeReducer {
    /// Create a shallow merge reducer
    pub fn shallow() -> Self {
        Self { deep: false }
    }

    /// Create a deep merge reducer
    pub fn deep() -> Self {
        Self { deep: true }
    }
}

#[async_trait]
impl Reducer for MergeReducer {
    async fn reduce(&self, current: Option<&Value>, update: &Value) -> AgentResult<Value> {
        match (current, update) {
            (Some(Value::Object(current_map)), Value::Object(update_map)) => {
                let mut result = current_map.clone();

                for (key, value) in update_map {
                    // If deep merge and both values are objects, recurse
                    if self.deep {
                        if let (Some(Value::Object(existing)), Value::Object(new_obj)) =
                            (result.get(key), value)
                        {
                            let merged = merge_objects_deep(existing.clone(), new_obj.clone());
                            result.insert(key.clone(), Value::Object(merged));
                            continue;
                        }
                    }
                    result.insert(key.clone(), value.clone());
                }

                Ok(Value::Object(result))
            }
            (None, Value::Object(update_map)) => Ok(Value::Object(update_map.clone())),
            (Some(current), _) => Ok(current.clone()),
            (None, update) => Ok(update.clone()),
        }
    }

    fn name(&self) -> &str {
        if self.deep { "merge_deep" } else { "merge" }
    }

    fn reducer_type(&self) -> ReducerType {
        ReducerType::Merge { deep: self.deep }
    }
}

/// Helper function for deep object merging
fn merge_objects_deep(
    mut base: serde_json::Map<String, Value>,
    update: serde_json::Map<String, Value>,
) -> serde_json::Map<String, Value> {
    for (key, value) in update {
        match (base.get(&key), value) {
            (Some(Value::Object(base_obj)), Value::Object(update_obj)) => {
                let merged = merge_objects_deep(base_obj.clone(), update_obj);
                base.insert(key, Value::Object(merged));
            }
            (_, value) => {
                base.insert(key, value);
            }
        }
    }
    base
}

/// LastN reducer - keeps only the last N items in a list
///
/// Useful for maintaining a sliding window of values.
///
/// # Example
///
/// ```rust,ignore
/// // With n=3:
/// // Before: { "history": [1, 2, 3, 4, 5] }
/// // Update: { "history": 6 }
/// // After:  { "history": [4, 5, 6] }
/// ```
#[derive(Debug, Clone)]
pub struct LastNReducer {
    /// Maximum number of items to keep
    pub n: usize,
}

impl LastNReducer {
    /// Create a new LastN reducer
    pub fn new(n: usize) -> Self {
        Self { n }
    }
}

#[async_trait]
impl Reducer for LastNReducer {
    async fn reduce(&self, current: Option<&Value>, update: &Value) -> AgentResult<Value> {
        let mut arr = match current {
            Some(Value::Array(a)) => a.clone(),
            _ => Vec::new(),
        };

        // Append the new value
        match update {
            Value::Array(items) => {
                arr.extend(items.iter().cloned());
            }
            other => {
                arr.push(other.clone());
            }
        }

        // Keep only last n items
        if arr.len() > self.n {
            let start = arr.len() - self.n;
            arr = arr.split_off(start);
        }

        Ok(Value::Array(arr))
    }

    fn name(&self) -> &str {
        "last_n"
    }

    fn reducer_type(&self) -> ReducerType {
        ReducerType::LastN { n: self.n }
    }
}

/// First reducer - keeps the first non-null value
///
/// Once a value is set, subsequent updates are ignored.
///
/// # Example
///
/// ```rust,ignore
/// // Before: null
/// // Update: "first"
/// // After:  "first"
/// // Update: "second"
/// // After:  "first" (unchanged)
/// ```
#[derive(Debug, Clone, Default)]
pub struct FirstReducer;

#[async_trait]
impl Reducer for FirstReducer {
    async fn reduce(&self, current: Option<&Value>, update: &Value) -> AgentResult<Value> {
        match current {
            Some(value) if !value.is_null() => Ok(value.clone()),
            _ => Ok(update.clone()),
        }
    }

    fn name(&self) -> &str {
        "first"
    }

    fn reducer_type(&self) -> ReducerType {
        ReducerType::First
    }
}

/// Last reducer - keeps the last non-null value
///
/// Always takes the most recent non-null value.
///
/// # Example
///
/// ```rust,ignore
/// // Before: "first"
/// // Update: "second"
/// // After:  "second"
/// // Update: null
/// // After:  "second" (unchanged because update is null)
/// ```
#[derive(Debug, Clone, Default)]
pub struct LastReducer;

#[async_trait]
impl Reducer for LastReducer {
    async fn reduce(&self, _current: Option<&Value>, update: &Value) -> AgentResult<Value> {
        // If update is null, keep current
        if update.is_null() {
            // This is a bit tricky - we need to return the current value
            // But if current is None, we return null
            // For now, let's just return the update (null)
            Ok(update.clone())
        } else {
            Ok(update.clone())
        }
    }

    fn name(&self) -> &str {
        "last"
    }

    fn reducer_type(&self) -> ReducerType {
        ReducerType::Last
    }
}

/// Custom reducer using a closure
///
/// Allows defining custom merge logic with a function.
pub struct CustomReducer<F>
where
    F: Fn(Option<&Value>, &Value) -> AgentResult<Value> + Send + Sync,
{
    name: String,
    func: F,
}

impl<F> CustomReducer<F>
where
    F: Fn(Option<&Value>, &Value) -> AgentResult<Value> + Send + Sync,
{
    /// Create a new custom reducer
    pub fn new(name: impl Into<String>, func: F) -> Self {
        Self {
            name: name.into(),
            func,
        }
    }
}

#[async_trait]
impl<F> Reducer for CustomReducer<F>
where
    F: Fn(Option<&Value>, &Value) -> AgentResult<Value> + Send + Sync,
{
    async fn reduce(&self, current: Option<&Value>, update: &Value) -> AgentResult<Value> {
        (self.func)(current, update)
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn reducer_type(&self) -> ReducerType {
        ReducerType::Custom(self.name.clone())
    }
}

/// Create a reducer from a ReducerType
pub fn create_reducer(reducer_type: &ReducerType) -> AgentResult<Box<dyn Reducer>> {
    match reducer_type {
        ReducerType::Overwrite => Ok(Box::new(OverwriteReducer)),
        ReducerType::Append => Ok(Box::new(AppendReducer)),
        ReducerType::Extend => Ok(Box::new(ExtendReducer)),
        ReducerType::Merge { deep } => Ok(Box::new(MergeReducer { deep: *deep })),
        ReducerType::LastN { n } => Ok(Box::new(LastNReducer::new(*n))),
        ReducerType::First => Ok(Box::new(FirstReducer)),
        ReducerType::Last => Ok(Box::new(LastReducer)),
        ReducerType::Custom(name) => Err(AgentError::Internal(format!(
            "Cannot create reducer for unknown custom type: {}",
            name
        ))),
    }
}

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

    #[tokio::test]
    async fn test_overwrite_reducer() {
        let reducer = OverwriteReducer;

        let result = reducer
            .reduce(Some(&json!("old")), &json!("new"))
            .await
            .unwrap();
        assert_eq!(result, json!("new"));

        let result = reducer.reduce(None, &json!("value")).await.unwrap();
        assert_eq!(result, json!("value"));
    }

    #[tokio::test]
    async fn test_append_reducer() {
        let reducer = AppendReducer;

        let result = reducer
            .reduce(Some(&json!(["a", "b"])), &json!("c"))
            .await
            .unwrap();
        assert_eq!(result, json!(["a", "b", "c"]));

        let result = reducer.reduce(None, &json!("first")).await.unwrap();
        assert_eq!(result, json!(["first"]));
    }

    #[tokio::test]
    async fn test_extend_reducer() {
        let reducer = ExtendReducer;

        let result = reducer
            .reduce(Some(&json!([1, 2])), &json!([3, 4]))
            .await
            .unwrap();
        assert_eq!(result, json!([1, 2, 3, 4]));

        // Single item extends
        let result = reducer.reduce(Some(&json!([1])), &json!(2)).await.unwrap();
        assert_eq!(result, json!([1, 2]));
    }

    #[tokio::test]
    async fn test_merge_reducer_shallow() {
        let reducer = MergeReducer::shallow();

        let current = json!({"a": 1, "b": 2});
        let update = json!({"b": 3, "c": 4});
        let result = reducer.reduce(Some(&current), &update).await.unwrap();

        assert_eq!(result["a"], 1);
        assert_eq!(result["b"], 3);
        assert_eq!(result["c"], 4);
    }

    #[tokio::test]
    async fn test_merge_reducer_deep() {
        let reducer = MergeReducer::deep();

        let current = json!({
            "config": {
                "a": 1,
                "b": { "x": 1, "y": 2 }
            }
        });
        let update = json!({
            "config": {
                "b": { "y": 3, "z": 4 },
                "c": 5
            }
        });
        let result = reducer.reduce(Some(&current), &update).await.unwrap();

        // Deep merge should preserve nested "x"
        assert_eq!(result["config"]["a"], 1);
        assert_eq!(result["config"]["b"]["x"], 1);
        assert_eq!(result["config"]["b"]["y"], 3);
        assert_eq!(result["config"]["b"]["z"], 4);
        assert_eq!(result["config"]["c"], 5);
    }

    #[tokio::test]
    async fn test_last_n_reducer() {
        let reducer = LastNReducer::new(3);

        let result = reducer
            .reduce(Some(&json!([1, 2, 3, 4])), &json!(5))
            .await
            .unwrap();
        assert_eq!(result, json!([3, 4, 5]));

        // Adding array extends and keeps last N
        let result = reducer
            .reduce(Some(&json!([1, 2])), &json!([3, 4, 5, 6]))
            .await
            .unwrap();
        assert_eq!(result, json!([4, 5, 6]));
    }

    #[tokio::test]
    async fn test_first_reducer() {
        let reducer = FirstReducer;

        // First value
        let result = reducer.reduce(None, &json!("first")).await.unwrap();
        assert_eq!(result, json!("first"));

        // Subsequent updates ignored
        let result = reducer
            .reduce(Some(&json!("first")), &json!("second"))
            .await
            .unwrap();
        assert_eq!(result, json!("first"));

        // Null current accepts update
        let result = reducer
            .reduce(Some(&json!(null)), &json!("value"))
            .await
            .unwrap();
        assert_eq!(result, json!("value"));
    }

    #[tokio::test]
    async fn test_last_reducer() {
        let reducer = LastReducer;

        // Takes last non-null value
        let result = reducer
            .reduce(Some(&json!("first")), &json!("second"))
            .await
            .unwrap();
        assert_eq!(result, json!("second"));

        // Non-null update replaces
        let result = reducer
            .reduce(Some(&json!("old")), &json!("new"))
            .await
            .unwrap();
        assert_eq!(result, json!("new"));
    }

    #[tokio::test]
    async fn test_custom_reducer() {
        let reducer = CustomReducer::new("sum", |current, update| {
            let curr = current.and_then(|v| v.as_i64()).unwrap_or(0);
            let upd = update.as_i64().unwrap_or(0);
            Ok(json!(curr + upd))
        });

        let result = reducer.reduce(Some(&json!(10)), &json!(5)).await.unwrap();
        assert_eq!(result, json!(15));

        assert_eq!(reducer.name(), "sum");
    }

    #[test]
    fn test_create_reducer() {
        let r = create_reducer(&ReducerType::Overwrite).unwrap();
        assert_eq!(r.name(), "overwrite");

        let r = create_reducer(&ReducerType::Append).unwrap();
        assert_eq!(r.name(), "append");

        let r = create_reducer(&ReducerType::LastN { n: 5 }).unwrap();
        assert_eq!(r.name(), "last_n");
    }
}