praisonai 0.2.0

Core library for PraisonAI - Agent, Tools, Workflows
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
//! Conditions Module for PraisonAI Rust SDK
//!
//! Provides condition protocols and implementations for workflow routing.
//! Enables unified condition evaluation across AgentFlow and AgentTeam.
//!
//! # Example
//!
//! ```rust,ignore
//! use praisonai::conditions::{ExpressionCondition, DictCondition};
//!
//! // Expression-based condition
//! let cond = ExpressionCondition::new("score > 80");
//! let result = cond.evaluate(&context);
//!
//! // Dictionary-based condition
//! let cond = DictCondition::new()
//!     .when("approved", vec!["review_task"])
//!     .when("rejected", vec!["revision_task"]);
//! ```

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

/// Protocol trait for condition implementations.
///
/// This defines the essential interface that any condition must provide.
/// It enables unified condition evaluation across AgentFlow (string-based)
/// and AgentTeam (dict-based) systems.
pub trait ConditionProtocol: Send + Sync {
    /// Evaluate the condition against the given context.
    ///
    /// # Arguments
    ///
    /// * `context` - Dictionary containing variables for evaluation.
    ///               May include workflow variables, previous outputs, etc.
    ///
    /// # Returns
    ///
    /// Boolean result of condition evaluation.
    /// Returns false on evaluation errors (fail-safe).
    fn evaluate(&self, context: &HashMap<String, serde_json::Value>) -> bool;
}

/// Extended protocol for conditions that support routing to targets.
///
/// This extends ConditionProtocol with the ability to return target
/// tasks/steps based on the condition evaluation.
pub trait RoutingConditionProtocol: ConditionProtocol {
    /// Get the target tasks/steps based on condition evaluation.
    ///
    /// # Arguments
    ///
    /// * `context` - Dictionary containing variables for evaluation.
    ///
    /// # Returns
    ///
    /// List of target task/step names to route to.
    /// Returns empty list if no match found.
    fn get_target(&self, context: &HashMap<String, serde_json::Value>) -> Vec<String>;
}

/// Simple expression-based condition.
///
/// Evaluates simple expressions like "score > 80" or "status == 'approved'".
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExpressionCondition {
    /// The expression to evaluate
    pub expression: String,
}

impl ExpressionCondition {
    /// Create a new expression condition.
    pub fn new(expression: impl Into<String>) -> Self {
        Self {
            expression: expression.into(),
        }
    }

    /// Parse and evaluate a simple comparison expression.
    fn evaluate_expression(&self, context: &HashMap<String, serde_json::Value>) -> bool {
        let expr = self.expression.trim();

        // Handle simple boolean variable
        if !expr.contains('>') && !expr.contains('<') && !expr.contains('=') && !expr.contains("!=") {
            return context
                .get(expr)
                .and_then(|v| v.as_bool())
                .unwrap_or(false);
        }

        // Parse comparison operators
        let operators = [">=", "<=", "!=", "==", ">", "<"];
        for op in operators {
            if let Some(pos) = expr.find(op) {
                let left = expr[..pos].trim();
                let right = expr[pos + op.len()..].trim();
                return self.compare(left, op, right, context);
            }
        }

        false
    }

    /// Compare two values with an operator.
    fn compare(
        &self,
        left: &str,
        op: &str,
        right: &str,
        context: &HashMap<String, serde_json::Value>,
    ) -> bool {
        // Get left value from context
        let left_val = context.get(left);

        // Parse right value (could be literal or variable)
        let right_val = if right.starts_with('\'') || right.starts_with('"') {
            // String literal
            let s = right.trim_matches(|c| c == '\'' || c == '"');
            Some(serde_json::json!(s))
        } else if let Ok(n) = right.parse::<f64>() {
            // Number literal
            Some(serde_json::json!(n))
        } else if right == "true" {
            Some(serde_json::json!(true))
        } else if right == "false" {
            Some(serde_json::json!(false))
        } else {
            // Variable reference
            context.get(right).cloned()
        };

        match (left_val, right_val) {
            (Some(l), Some(r)) => {
                // Try numeric comparison first
                if let (Some(ln), Some(rn)) = (l.as_f64(), r.as_f64()) {
                    return match op {
                        ">" => ln > rn,
                        "<" => ln < rn,
                        ">=" => ln >= rn,
                        "<=" => ln <= rn,
                        "==" => (ln - rn).abs() < f64::EPSILON,
                        "!=" => (ln - rn).abs() >= f64::EPSILON,
                        _ => false,
                    };
                }

                // String comparison
                if let (Some(ls), Some(rs)) = (l.as_str(), r.as_str()) {
                    return match op {
                        "==" => ls == rs,
                        "!=" => ls != rs,
                        ">" => ls > rs,
                        "<" => ls < rs,
                        ">=" => ls >= rs,
                        "<=" => ls <= rs,
                        _ => false,
                    };
                }

                // Boolean comparison
                if let (Some(lb), Some(rb)) = (l.as_bool(), r.as_bool()) {
                    return match op {
                        "==" => lb == rb,
                        "!=" => lb != rb,
                        _ => false,
                    };
                }

                // Generic equality
                match op {
                    "==" => l == &r,
                    "!=" => l != &r,
                    _ => false,
                }
            }
            _ => false,
        }
    }
}

impl ConditionProtocol for ExpressionCondition {
    fn evaluate(&self, context: &HashMap<String, serde_json::Value>) -> bool {
        self.evaluate_expression(context)
    }
}

/// Dictionary-based condition for routing.
///
/// Maps values to target tasks/steps.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DictCondition {
    /// Variable name to check
    pub variable: String,
    /// Mapping of values to target tasks
    pub routes: HashMap<String, Vec<String>>,
    /// Default targets if no match
    pub default: Vec<String>,
}

impl DictCondition {
    /// Create a new dict condition.
    pub fn new(variable: impl Into<String>) -> Self {
        Self {
            variable: variable.into(),
            routes: HashMap::new(),
            default: Vec::new(),
        }
    }

    /// Add a route for a value.
    pub fn when(mut self, value: impl Into<String>, targets: Vec<String>) -> Self {
        self.routes.insert(value.into(), targets);
        self
    }

    /// Set default targets.
    pub fn default_targets(mut self, targets: Vec<String>) -> Self {
        self.default = targets;
        self
    }
}

impl ConditionProtocol for DictCondition {
    fn evaluate(&self, context: &HashMap<String, serde_json::Value>) -> bool {
        if let Some(value) = context.get(&self.variable) {
            let key = match value {
                serde_json::Value::String(s) => s.clone(),
                serde_json::Value::Bool(b) => b.to_string(),
                serde_json::Value::Number(n) => n.to_string(),
                _ => return !self.default.is_empty(),
            };
            self.routes.contains_key(&key) || !self.default.is_empty()
        } else {
            !self.default.is_empty()
        }
    }
}

impl RoutingConditionProtocol for DictCondition {
    fn get_target(&self, context: &HashMap<String, serde_json::Value>) -> Vec<String> {
        if let Some(value) = context.get(&self.variable) {
            let key = match value {
                serde_json::Value::String(s) => s.clone(),
                serde_json::Value::Bool(b) => b.to_string(),
                serde_json::Value::Number(n) => n.to_string(),
                _ => return self.default.clone(),
            };
            self.routes.get(&key).cloned().unwrap_or_else(|| self.default.clone())
        } else {
            self.default.clone()
        }
    }
}

/// Closure-based condition.
///
/// Wraps a closure for custom condition logic.
pub struct ClosureCondition<F>
where
    F: Fn(&HashMap<String, serde_json::Value>) -> bool + Send + Sync,
{
    condition: F,
}

impl<F> ClosureCondition<F>
where
    F: Fn(&HashMap<String, serde_json::Value>) -> bool + Send + Sync,
{
    /// Create a new closure condition.
    pub fn new(condition: F) -> Self {
        Self { condition }
    }
}

impl<F> ConditionProtocol for ClosureCondition<F>
where
    F: Fn(&HashMap<String, serde_json::Value>) -> bool + Send + Sync,
{
    fn evaluate(&self, context: &HashMap<String, serde_json::Value>) -> bool {
        (self.condition)(context)
    }
}

/// Evaluate a condition expression against a context.
///
/// Convenience function for simple condition evaluation.
pub fn evaluate_condition(
    expression: &str,
    context: &HashMap<String, serde_json::Value>,
) -> bool {
    ExpressionCondition::new(expression).evaluate(context)
}

/// Builder for creating conditions.
pub struct If;

impl If {
    /// Create an expression condition.
    pub fn expr(expression: impl Into<String>) -> ExpressionCondition {
        ExpressionCondition::new(expression)
    }

    /// Create a dict condition.
    pub fn dict(variable: impl Into<String>) -> DictCondition {
        DictCondition::new(variable)
    }

    /// Create a closure condition.
    pub fn closure<F>(f: F) -> ClosureCondition<F>
    where
        F: Fn(&HashMap<String, serde_json::Value>) -> bool + Send + Sync,
    {
        ClosureCondition::new(f)
    }
}

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

    fn make_context(pairs: &[(&str, serde_json::Value)]) -> HashMap<String, serde_json::Value> {
        pairs.iter().map(|(k, v)| (k.to_string(), v.clone())).collect()
    }

    #[test]
    fn test_expression_condition_numeric_greater() {
        let cond = ExpressionCondition::new("score > 80");
        
        let ctx = make_context(&[("score", serde_json::json!(90))]);
        assert!(cond.evaluate(&ctx));

        let ctx = make_context(&[("score", serde_json::json!(70))]);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_expression_condition_numeric_equal() {
        let cond = ExpressionCondition::new("count == 5");
        
        let ctx = make_context(&[("count", serde_json::json!(5))]);
        assert!(cond.evaluate(&ctx));

        let ctx = make_context(&[("count", serde_json::json!(3))]);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_expression_condition_string_equal() {
        let cond = ExpressionCondition::new("status == 'approved'");
        
        let ctx = make_context(&[("status", serde_json::json!("approved"))]);
        assert!(cond.evaluate(&ctx));

        let ctx = make_context(&[("status", serde_json::json!("rejected"))]);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_expression_condition_not_equal() {
        let cond = ExpressionCondition::new("status != 'pending'");
        
        let ctx = make_context(&[("status", serde_json::json!("approved"))]);
        assert!(cond.evaluate(&ctx));

        let ctx = make_context(&[("status", serde_json::json!("pending"))]);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_expression_condition_boolean() {
        let cond = ExpressionCondition::new("is_valid");
        
        let ctx = make_context(&[("is_valid", serde_json::json!(true))]);
        assert!(cond.evaluate(&ctx));

        let ctx = make_context(&[("is_valid", serde_json::json!(false))]);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_expression_condition_missing_variable() {
        let cond = ExpressionCondition::new("score > 80");
        let ctx = make_context(&[]);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_dict_condition_basic() {
        let cond = DictCondition::new("decision")
            .when("approved", vec!["process_task".to_string()])
            .when("rejected", vec!["revision_task".to_string()]);

        let ctx = make_context(&[("decision", serde_json::json!("approved"))]);
        assert!(cond.evaluate(&ctx));
        assert_eq!(cond.get_target(&ctx), vec!["process_task"]);

        let ctx = make_context(&[("decision", serde_json::json!("rejected"))]);
        assert!(cond.evaluate(&ctx));
        assert_eq!(cond.get_target(&ctx), vec!["revision_task"]);
    }

    #[test]
    fn test_dict_condition_default() {
        let cond = DictCondition::new("decision")
            .when("approved", vec!["process_task".to_string()])
            .default_targets(vec!["fallback_task".to_string()]);

        let ctx = make_context(&[("decision", serde_json::json!("unknown"))]);
        assert!(cond.evaluate(&ctx));
        assert_eq!(cond.get_target(&ctx), vec!["fallback_task"]);
    }

    #[test]
    fn test_dict_condition_no_match_no_default() {
        let cond = DictCondition::new("decision")
            .when("approved", vec!["process_task".to_string()]);

        let ctx = make_context(&[("decision", serde_json::json!("unknown"))]);
        assert!(!cond.evaluate(&ctx));
        assert!(cond.get_target(&ctx).is_empty());
    }

    #[test]
    fn test_closure_condition() {
        let cond = ClosureCondition::new(|ctx| {
            ctx.get("score")
                .and_then(|v| v.as_f64())
                .map(|s| s > 80.0)
                .unwrap_or(false)
        });

        let ctx = make_context(&[("score", serde_json::json!(90))]);
        assert!(cond.evaluate(&ctx));

        let ctx = make_context(&[("score", serde_json::json!(70))]);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_evaluate_condition_function() {
        let ctx = make_context(&[("score", serde_json::json!(90))]);
        assert!(evaluate_condition("score > 80", &ctx));
        assert!(!evaluate_condition("score < 80", &ctx));
    }

    #[test]
    fn test_if_builder() {
        let expr_cond = If::expr("score > 80");
        let ctx = make_context(&[("score", serde_json::json!(90))]);
        assert!(expr_cond.evaluate(&ctx));

        let dict_cond = If::dict("status")
            .when("ok", vec!["proceed".to_string()]);
        let ctx = make_context(&[("status", serde_json::json!("ok"))]);
        assert!(dict_cond.evaluate(&ctx));
    }

    #[test]
    fn test_expression_condition_greater_equal() {
        let cond = ExpressionCondition::new("score >= 80");
        
        let ctx = make_context(&[("score", serde_json::json!(80))]);
        assert!(cond.evaluate(&ctx));

        let ctx = make_context(&[("score", serde_json::json!(79))]);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_expression_condition_less_than() {
        let cond = ExpressionCondition::new("score < 50");
        
        let ctx = make_context(&[("score", serde_json::json!(30))]);
        assert!(cond.evaluate(&ctx));

        let ctx = make_context(&[("score", serde_json::json!(60))]);
        assert!(!cond.evaluate(&ctx));
    }
}