brainwires-agents 0.10.0

Agent orchestration, coordination, and lifecycle management for the Brainwires Agent 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
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
//! Result Composition
//!
//! Combines outputs from multiple subtasks into a final result
//! based on the composition function specified during decomposition.

use std::collections::HashMap;

use serde_json::Value;

use super::decomposition::CompositionFunction;
use super::error::{CompositionError, MdapResult};
use super::microagent::SubtaskOutput;

/// Result composer for combining subtask outputs
pub struct Composer {
    /// Optional custom composition handlers
    custom_handlers: HashMap<String, Box<dyn CompositionHandler>>,
}

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

impl Composer {
    /// Create a new composer
    pub fn new() -> Self {
        Self {
            custom_handlers: HashMap::new(),
        }
    }

    /// Register a custom composition handler
    pub fn register_handler(
        &mut self,
        name: impl Into<String>,
        handler: Box<dyn CompositionHandler>,
    ) {
        self.custom_handlers.insert(name.into(), handler);
    }

    /// Compose results according to the composition function
    pub fn compose(
        &self,
        results: &[SubtaskOutput],
        function: &CompositionFunction,
    ) -> MdapResult<Value> {
        if results.is_empty() {
            return Err(
                CompositionError::MissingResult("No results to compose".to_string()).into(),
            );
        }

        match function {
            CompositionFunction::Identity => Ok(results[0].value.clone()),

            CompositionFunction::Concatenate => self.concatenate(results),

            CompositionFunction::Sequence => self.sequence(results),

            CompositionFunction::ObjectMerge => self.object_merge(results),

            CompositionFunction::LastOnly => Ok(results
                .last()
                .expect("checked non-empty above")
                .value
                .clone()),

            CompositionFunction::Custom(description) => self.custom_compose(results, description),

            CompositionFunction::Reduce { operation } => self.reduce(results, operation),
        }
    }

    /// Concatenate all results as strings
    fn concatenate(&self, results: &[SubtaskOutput]) -> MdapResult<Value> {
        let mut combined = String::new();

        for result in results {
            match &result.value {
                Value::String(s) => {
                    if !combined.is_empty() {
                        combined.push('\n');
                    }
                    combined.push_str(s);
                }
                Value::Array(arr) => {
                    for item in arr {
                        if !combined.is_empty() {
                            combined.push('\n');
                        }
                        combined.push_str(&item.to_string());
                    }
                }
                other => {
                    if !combined.is_empty() {
                        combined.push('\n');
                    }
                    combined.push_str(&other.to_string());
                }
            }
        }

        Ok(Value::String(combined))
    }

    /// Combine results as a sequence (array)
    fn sequence(&self, results: &[SubtaskOutput]) -> MdapResult<Value> {
        let items: Vec<Value> = results.iter().map(|r| r.value.clone()).collect();
        Ok(Value::Array(items))
    }

    /// Merge results into an object with subtask IDs as keys
    fn object_merge(&self, results: &[SubtaskOutput]) -> MdapResult<Value> {
        let mut map = serde_json::Map::new();

        for result in results {
            // If the value is already an object, merge its keys
            if let Value::Object(obj) = &result.value {
                for (k, v) in obj {
                    map.insert(k.clone(), v.clone());
                }
            } else {
                // Use subtask ID as key
                map.insert(result.subtask_id.clone(), result.value.clone());
            }
        }

        Ok(Value::Object(map))
    }

    /// Apply a reduce operation
    fn reduce(&self, results: &[SubtaskOutput], operation: &str) -> MdapResult<Value> {
        let op = operation.to_lowercase();

        match op.as_str() {
            "sum" | "add" => self.reduce_sum(results),
            "multiply" | "product" => self.reduce_product(results),
            "max" => self.reduce_max(results),
            "min" => self.reduce_min(results),
            "and" | "all" => self.reduce_and(results),
            "or" | "any" => self.reduce_or(results),
            "concat" | "join" => self.concatenate(results),
            _ => Err(CompositionError::FunctionNotFound {
                function: format!("reduce:{}", operation),
            }
            .into()),
        }
    }

    /// Sum numeric values
    fn reduce_sum(&self, results: &[SubtaskOutput]) -> MdapResult<Value> {
        let mut sum = 0.0f64;

        for result in results {
            let val = self.extract_number(&result.value)?;
            sum += val;
        }

        Ok(Value::Number(
            serde_json::Number::from_f64(sum)
                .ok_or_else(|| CompositionError::ExecutionFailed("Invalid number".to_string()))?,
        ))
    }

    /// Multiply numeric values
    fn reduce_product(&self, results: &[SubtaskOutput]) -> MdapResult<Value> {
        let mut product = 1.0f64;

        for result in results {
            let val = self.extract_number(&result.value)?;
            product *= val;
        }

        Ok(Value::Number(
            serde_json::Number::from_f64(product)
                .ok_or_else(|| CompositionError::ExecutionFailed("Invalid number".to_string()))?,
        ))
    }

    /// Find maximum value
    fn reduce_max(&self, results: &[SubtaskOutput]) -> MdapResult<Value> {
        let mut max = f64::NEG_INFINITY;

        for result in results {
            let val = self.extract_number(&result.value)?;
            if val > max {
                max = val;
            }
        }

        Ok(Value::Number(
            serde_json::Number::from_f64(max)
                .ok_or_else(|| CompositionError::ExecutionFailed("Invalid number".to_string()))?,
        ))
    }

    /// Find minimum value
    fn reduce_min(&self, results: &[SubtaskOutput]) -> MdapResult<Value> {
        let mut min = f64::INFINITY;

        for result in results {
            let val = self.extract_number(&result.value)?;
            if val < min {
                min = val;
            }
        }

        Ok(Value::Number(
            serde_json::Number::from_f64(min)
                .ok_or_else(|| CompositionError::ExecutionFailed("Invalid number".to_string()))?,
        ))
    }

    /// Logical AND of boolean values
    fn reduce_and(&self, results: &[SubtaskOutput]) -> MdapResult<Value> {
        for result in results {
            let val = self.extract_bool(&result.value)?;
            if !val {
                return Ok(Value::Bool(false));
            }
        }
        Ok(Value::Bool(true))
    }

    /// Logical OR of boolean values
    fn reduce_or(&self, results: &[SubtaskOutput]) -> MdapResult<Value> {
        for result in results {
            let val = self.extract_bool(&result.value)?;
            if val {
                return Ok(Value::Bool(true));
            }
        }
        Ok(Value::Bool(false))
    }

    /// Extract number from a value
    fn extract_number(&self, value: &Value) -> MdapResult<f64> {
        match value {
            Value::Number(n) => n.as_f64().ok_or_else(|| {
                CompositionError::IncompatibleTypes {
                    type_a: "number".to_string(),
                    type_b: "invalid float".to_string(),
                }
                .into()
            }),
            Value::String(s) => s.parse::<f64>().map_err(|_| {
                CompositionError::IncompatibleTypes {
                    type_a: "number".to_string(),
                    type_b: "string".to_string(),
                }
                .into()
            }),
            _ => Err(CompositionError::IncompatibleTypes {
                type_a: "number".to_string(),
                type_b: format!("{:?}", value),
            }
            .into()),
        }
    }

    /// Extract boolean from a value
    fn extract_bool(&self, value: &Value) -> MdapResult<bool> {
        match value {
            Value::Bool(b) => Ok(*b),
            Value::String(s) => match s.to_lowercase().as_str() {
                "true" | "yes" | "1" => Ok(true),
                "false" | "no" | "0" => Ok(false),
                _ => Err(CompositionError::IncompatibleTypes {
                    type_a: "bool".to_string(),
                    type_b: "string".to_string(),
                }
                .into()),
            },
            Value::Number(n) => Ok(n.as_f64().map(|f| f != 0.0).unwrap_or(false)),
            _ => Err(CompositionError::IncompatibleTypes {
                type_a: "bool".to_string(),
                type_b: format!("{:?}", value),
            }
            .into()),
        }
    }

    /// Handle custom composition
    fn custom_compose(&self, results: &[SubtaskOutput], description: &str) -> MdapResult<Value> {
        // Check for registered handler
        if let Some(handler) = self.custom_handlers.get(description) {
            return handler.compose(results);
        }

        // Default: just concatenate with the description as context
        let mut composed = serde_json::Map::new();
        composed.insert(
            "composition".to_string(),
            Value::String(description.to_string()),
        );
        composed.insert(
            "results".to_string(),
            Value::Array(results.iter().map(|r| r.value.clone()).collect()),
        );

        Ok(Value::Object(composed))
    }
}

/// Trait for custom composition handlers
pub trait CompositionHandler: Send + Sync {
    /// Compose subtask outputs into a final result.
    fn compose(&self, results: &[SubtaskOutput]) -> MdapResult<Value>;
}

/// Trait for composing decomposition results
pub trait ResultComposer: Send + Sync {
    /// Compose results from all subtasks according to the decomposition
    fn compose(
        &self,
        decomposition: &super::decomposition::DecompositionResult,
        results: &std::collections::HashMap<String, SubtaskOutput>,
    ) -> MdapResult<Value>;
}

/// Standard result composer implementation
pub struct StandardComposer;

impl ResultComposer for StandardComposer {
    fn compose(
        &self,
        decomposition: &super::decomposition::DecompositionResult,
        results: &std::collections::HashMap<String, SubtaskOutput>,
    ) -> MdapResult<Value> {
        // Collect outputs in subtask order
        let outputs: Vec<SubtaskOutput> = decomposition
            .subtasks
            .iter()
            .filter_map(|subtask| results.get(&subtask.id).cloned())
            .collect();

        if outputs.is_empty() {
            return Err(
                CompositionError::MissingResult("No results to compose".to_string()).into(),
            );
        }

        let composer = Composer::new();
        composer.compose(&outputs, &decomposition.composition_function)
    }
}

/// Builder for composition with validation
pub struct CompositionBuilder {
    results: Vec<SubtaskOutput>,
    function: CompositionFunction,
    validate: bool,
}

impl CompositionBuilder {
    /// Create a new composition builder with the given function.
    pub fn new(function: CompositionFunction) -> Self {
        Self {
            results: Vec::new(),
            function,
            validate: true,
        }
    }

    /// Add a single subtask output to the composition.
    pub fn add_result(mut self, result: SubtaskOutput) -> Self {
        self.results.push(result);
        self
    }

    /// Add multiple subtask outputs to the composition.
    pub fn add_results(mut self, results: Vec<SubtaskOutput>) -> Self {
        self.results.extend(results);
        self
    }

    /// Disable input validation before composing.
    pub fn skip_validation(mut self) -> Self {
        self.validate = false;
        self
    }

    /// Execute the composition and return the combined result.
    pub fn compose(self) -> MdapResult<Value> {
        if self.validate {
            self.validate_inputs()?;
        }

        let composer = Composer::new();
        composer.compose(&self.results, &self.function)
    }

    fn validate_inputs(&self) -> MdapResult<()> {
        if self.results.is_empty() {
            return Err(CompositionError::MissingResult("No results provided".to_string()).into());
        }

        // Check for type consistency in reduce operations
        if let CompositionFunction::Reduce { operation } = &self.function {
            let op = operation.to_lowercase();
            if op == "sum" || op == "multiply" || op == "max" || op == "min" {
                // All values should be numeric
                for result in &self.results {
                    if !(result.value.is_number()
                        || result.value.is_string()
                            && result
                                .value
                                .as_str()
                                .map(|s| s.parse::<f64>().is_ok())
                                .unwrap_or(false))
                    {
                        return Err(CompositionError::IncompatibleTypes {
                            type_a: "number".to_string(),
                            type_b: format!("{:?}", result.value),
                        }
                        .into());
                    }
                }
            }
        }

        Ok(())
    }
}

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

    fn make_output(id: &str, value: Value) -> SubtaskOutput {
        SubtaskOutput {
            subtask_id: id.to_string(),
            value,
            next_state: None,
        }
    }

    #[test]
    fn test_identity_composition() {
        let composer = Composer::new();
        let results = vec![make_output("a", Value::String("hello".to_string()))];

        let result = composer
            .compose(&results, &CompositionFunction::Identity)
            .unwrap();
        assert_eq!(result, Value::String("hello".to_string()));
    }

    #[test]
    fn test_concatenate_strings() {
        let composer = Composer::new();
        let results = vec![
            make_output("a", Value::String("hello".to_string())),
            make_output("b", Value::String("world".to_string())),
        ];

        let result = composer
            .compose(&results, &CompositionFunction::Concatenate)
            .unwrap();
        assert_eq!(result, Value::String("hello\nworld".to_string()));
    }

    #[test]
    fn test_sequence() {
        let composer = Composer::new();
        let results = vec![
            make_output("a", serde_json::json!(1)),
            make_output("b", serde_json::json!(2)),
            make_output("c", serde_json::json!(3)),
        ];

        let result = composer
            .compose(&results, &CompositionFunction::Sequence)
            .unwrap();
        assert_eq!(result, serde_json::json!([1, 2, 3]));
    }

    #[test]
    fn test_object_merge() {
        let composer = Composer::new();
        let results = vec![
            make_output("a", serde_json::json!({"x": 1})),
            make_output("b", serde_json::json!({"y": 2})),
        ];

        let result = composer
            .compose(&results, &CompositionFunction::ObjectMerge)
            .unwrap();
        assert_eq!(result, serde_json::json!({"x": 1, "y": 2}));
    }

    #[test]
    fn test_last_only() {
        let composer = Composer::new();
        let results = vec![
            make_output("a", serde_json::json!(1)),
            make_output("b", serde_json::json!(2)),
            make_output("c", serde_json::json!(3)),
        ];

        let result = composer
            .compose(&results, &CompositionFunction::LastOnly)
            .unwrap();
        assert_eq!(result, serde_json::json!(3));
    }

    #[test]
    fn test_reduce_sum() {
        let composer = Composer::new();
        let results = vec![
            make_output("a", serde_json::json!(10)),
            make_output("b", serde_json::json!(20)),
            make_output("c", serde_json::json!(30)),
        ];

        let result = composer
            .compose(
                &results,
                &CompositionFunction::Reduce {
                    operation: "sum".to_string(),
                },
            )
            .unwrap();
        assert_eq!(result, serde_json::json!(60.0));
    }

    #[test]
    fn test_reduce_product() {
        let composer = Composer::new();
        let results = vec![
            make_output("a", serde_json::json!(2)),
            make_output("b", serde_json::json!(3)),
            make_output("c", serde_json::json!(4)),
        ];

        let result = composer
            .compose(
                &results,
                &CompositionFunction::Reduce {
                    operation: "product".to_string(),
                },
            )
            .unwrap();
        assert_eq!(result, serde_json::json!(24.0));
    }

    #[test]
    fn test_reduce_max() {
        let composer = Composer::new();
        let results = vec![
            make_output("a", serde_json::json!(10)),
            make_output("b", serde_json::json!(50)),
            make_output("c", serde_json::json!(30)),
        ];

        let result = composer
            .compose(
                &results,
                &CompositionFunction::Reduce {
                    operation: "max".to_string(),
                },
            )
            .unwrap();
        assert_eq!(result, serde_json::json!(50.0));
    }

    #[test]
    fn test_reduce_and() {
        let composer = Composer::new();

        let all_true = vec![
            make_output("a", Value::Bool(true)),
            make_output("b", Value::Bool(true)),
        ];
        let result = composer
            .compose(
                &all_true,
                &CompositionFunction::Reduce {
                    operation: "and".to_string(),
                },
            )
            .unwrap();
        assert_eq!(result, Value::Bool(true));

        let some_false = vec![
            make_output("a", Value::Bool(true)),
            make_output("b", Value::Bool(false)),
        ];
        let result = composer
            .compose(
                &some_false,
                &CompositionFunction::Reduce {
                    operation: "and".to_string(),
                },
            )
            .unwrap();
        assert_eq!(result, Value::Bool(false));
    }

    #[test]
    fn test_reduce_or() {
        let composer = Composer::new();

        let all_false = vec![
            make_output("a", Value::Bool(false)),
            make_output("b", Value::Bool(false)),
        ];
        let result = composer
            .compose(
                &all_false,
                &CompositionFunction::Reduce {
                    operation: "or".to_string(),
                },
            )
            .unwrap();
        assert_eq!(result, Value::Bool(false));

        let some_true = vec![
            make_output("a", Value::Bool(false)),
            make_output("b", Value::Bool(true)),
        ];
        let result = composer
            .compose(
                &some_true,
                &CompositionFunction::Reduce {
                    operation: "or".to_string(),
                },
            )
            .unwrap();
        assert_eq!(result, Value::Bool(true));
    }

    #[test]
    fn test_composition_builder() {
        let result = CompositionBuilder::new(CompositionFunction::Sequence)
            .add_result(make_output("a", serde_json::json!(1)))
            .add_result(make_output("b", serde_json::json!(2)))
            .compose()
            .unwrap();

        assert_eq!(result, serde_json::json!([1, 2]));
    }

    #[test]
    fn test_composition_builder_validation() {
        let result = CompositionBuilder::new(CompositionFunction::Reduce {
            operation: "sum".to_string(),
        })
        .add_result(make_output("a", Value::String("not a number".to_string())))
        .compose();

        assert!(result.is_err());
    }

    #[test]
    fn test_empty_results() {
        let composer = Composer::new();
        let results: Vec<SubtaskOutput> = vec![];

        let result = composer.compose(&results, &CompositionFunction::Identity);
        assert!(result.is_err());
    }

    #[test]
    fn test_custom_composition() {
        let composer = Composer::new();
        let results = vec![
            make_output("a", serde_json::json!(1)),
            make_output("b", serde_json::json!(2)),
        ];

        let result = composer
            .compose(
                &results,
                &CompositionFunction::Custom("test composition".to_string()),
            )
            .unwrap();

        assert!(result.is_object());
        assert_eq!(result["composition"], "test composition");
    }
}