agnt5-sdk-core 0.3.9

High-performance Rust core for AGNT5 SDK
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
//! Deterministic scorers for evaluating outputs.
//!
//! These scorers provide fast, deterministic evaluation of outputs
//! using string comparison, pattern matching, and similarity metrics.

use super::{ScorerInput, ScorerResult};
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Configuration for exact_match scorer
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ExactMatchConfig {
    /// Whether to compare case-sensitively (default: true)
    pub case_sensitive: Option<bool>,
}

/// Check if output exactly matches expected value.
///
/// # Arguments
/// * `input` - ScorerInput with output and expected values
/// * `config` - Configuration for case sensitivity
///
/// # Returns
/// ScorerResult with score 1.0 for match, 0.0 for mismatch
pub fn exact_match(input: &ScorerInput, config: &ExactMatchConfig) -> ScorerResult {
    let case_sensitive = config.case_sensitive.unwrap_or(true);

    let output_str = value_to_string(&input.output);
    let expected_str = input
        .expected
        .as_ref()
        .map(value_to_string)
        .unwrap_or_default();

    let matches = if case_sensitive {
        output_str == expected_str
    } else {
        output_str.to_lowercase() == expected_str.to_lowercase()
    };

    ScorerResult {
        score: if matches { 1.0 } else { 0.0 },
        passed: Some(matches),
        label: Some(if matches {
            "match".into()
        } else {
            "mismatch".into()
        }),
        explanation: None,
        metadata: None,
    }
}

/// Configuration for contains scorer
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ContainsConfig {
    /// Pattern to search for
    pub pattern: String,
    /// Whether to search case-sensitively (default: true)
    pub case_sensitive: Option<bool>,
}

/// Check if output contains a specific pattern.
///
/// # Arguments
/// * `input` - ScorerInput with output value
/// * `config` - Configuration with pattern and case sensitivity
///
/// # Returns
/// ScorerResult with score 1.0 if found, 0.0 if not found
pub fn contains(input: &ScorerInput, config: &ContainsConfig) -> ScorerResult {
    let case_sensitive = config.case_sensitive.unwrap_or(true);
    let output_str = value_to_string(&input.output);

    let found = if case_sensitive {
        output_str.contains(&config.pattern)
    } else {
        output_str
            .to_lowercase()
            .contains(&config.pattern.to_lowercase())
    };

    ScorerResult {
        score: if found { 1.0 } else { 0.0 },
        passed: Some(found),
        label: Some(if found {
            "found".into()
        } else {
            "not_found".into()
        }),
        explanation: None,
        metadata: None,
    }
}

/// Check if output is valid JSON.
///
/// If the output is already a JSON Value (not a string), it's considered valid.
/// If it's a string, attempts to parse it as JSON.
///
/// # Arguments
/// * `input` - ScorerInput with output value
///
/// # Returns
/// ScorerResult with score 1.0 if valid, 0.0 if invalid
pub fn json_valid(input: &ScorerInput) -> ScorerResult {
    let valid = match &input.output {
        Value::String(s) => serde_json::from_str::<Value>(s).is_ok(),
        _ => true, // Already a Value, so it's valid JSON
    };

    ScorerResult {
        score: if valid { 1.0 } else { 0.0 },
        passed: Some(valid),
        label: Some(if valid {
            "valid".into()
        } else {
            "invalid".into()
        }),
        explanation: None,
        metadata: None,
    }
}

/// Configuration for regex_match scorer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegexConfig {
    /// Regex pattern to match
    pub pattern: String,
}

/// Check if output matches a regex pattern.
///
/// # Arguments
/// * `input` - ScorerInput with output value
/// * `config` - Configuration with regex pattern
///
/// # Returns
/// ScorerResult with score 1.0 if matches, 0.0 if not
pub fn regex_match(input: &ScorerInput, config: &RegexConfig) -> ScorerResult {
    let output_str = value_to_string(&input.output);

    match regex::Regex::new(&config.pattern) {
        Ok(re) => {
            let matches = re.is_match(&output_str);
            ScorerResult {
                score: if matches { 1.0 } else { 0.0 },
                passed: Some(matches),
                label: Some(if matches {
                    "match".into()
                } else {
                    "no_match".into()
                }),
                explanation: None,
                metadata: None,
            }
        }
        Err(e) => ScorerResult {
            score: 0.0,
            passed: Some(false),
            label: Some("error".into()),
            explanation: Some(format!("Invalid regex: {}", e)),
            metadata: None,
        },
    }
}

/// Configuration for levenshtein scorer
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct LevenshteinConfig {
    /// Minimum similarity threshold (0.0-1.0, default: 0.0)
    pub threshold: Option<f64>,
}

/// Calculate Levenshtein similarity between output and expected.
///
/// Returns a similarity score between 0.0 and 1.0, where 1.0 means identical.
///
/// # Arguments
/// * `input` - ScorerInput with output and expected values
/// * `config` - Configuration with optional threshold
///
/// # Returns
/// ScorerResult with similarity score
pub fn levenshtein(input: &ScorerInput, config: &LevenshteinConfig) -> ScorerResult {
    let output_str = value_to_string(&input.output);
    let expected_str = input
        .expected
        .as_ref()
        .map(value_to_string)
        .unwrap_or_default();

    let distance = levenshtein_distance(&output_str, &expected_str);
    let max_len = output_str.len().max(expected_str.len());
    let similarity = if max_len == 0 {
        1.0
    } else {
        1.0 - (distance as f64 / max_len as f64)
    };

    let threshold = config.threshold.unwrap_or(0.0);
    let passed = similarity >= threshold;

    ScorerResult {
        score: similarity,
        passed: Some(passed),
        label: None,
        explanation: Some(format!(
            "Similarity: {:.2}%, Distance: {}",
            similarity * 100.0,
            distance
        )),
        metadata: None,
    }
}

/// Configuration for json_schema scorer.
///
/// Validates the output against a JSON Schema (Draft 2020-12 by default).
/// The output is parsed as JSON if it's a string; otherwise the JSON `Value`
/// is validated directly.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonSchemaConfig {
    /// The JSON Schema to validate against. Required.
    pub schema: Value,
}

/// Check whether the output validates against a JSON Schema.
///
/// String outputs are parsed as JSON first; anything that isn't valid JSON
/// fails with `parse_error`. Validation failures include a one-line summary
/// of the first error in `explanation` and the full error list in
/// `metadata.errors`.
pub fn json_schema(input: &ScorerInput, config: &JsonSchemaConfig) -> ScorerResult {
    // Parse the output: strings get JSON-decoded, everything else is the
    // existing `Value` (the caller already gave us structured JSON).
    let parsed: Value = match &input.output {
        Value::String(s) => match serde_json::from_str(s) {
            Ok(v) => v,
            Err(e) => {
                return ScorerResult {
                    score: 0.0,
                    passed: Some(false),
                    label: Some("parse_error".into()),
                    explanation: Some(format!("output is not valid JSON: {e}")),
                    metadata: None,
                };
            }
        },
        v => v.clone(),
    };

    // Compile the schema. A bad schema is a config error, not a sample
    // failure — report it distinctly so users notice.
    let validator = match jsonschema::validator_for(&config.schema) {
        Ok(v) => v,
        Err(e) => {
            return ScorerResult {
                score: 0.0,
                passed: Some(false),
                label: Some("config_error".into()),
                explanation: Some(format!("invalid schema: {e}")),
                metadata: None,
            };
        }
    };

    let errors: Vec<String> = validator
        .iter_errors(&parsed)
        .map(|e| format!("{}: {}", e.instance_path, e))
        .collect();

    if errors.is_empty() {
        ScorerResult {
            score: 1.0,
            passed: Some(true),
            label: Some("valid".into()),
            explanation: None,
            metadata: None,
        }
    } else {
        ScorerResult {
            score: 0.0,
            passed: Some(false),
            label: Some("invalid".into()),
            explanation: Some(errors[0].clone()),
            metadata: Some(serde_json::json!({ "errors": errors })),
        }
    }
}

/// Configuration for numeric_range scorer.
///
/// At least one of `min` / `max` must be set. `inclusive` controls
/// whether the bounds themselves are accepted (default: true).
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct NumericRangeConfig {
    pub min: Option<f64>,
    pub max: Option<f64>,
    /// When true (default), `min ≤ x ≤ max`. When false, `min < x < max`.
    pub inclusive: Option<bool>,
}

/// Check whether the output's numeric value falls within `[min, max]`.
///
/// Accepts numbers directly or numeric strings (e.g. `"42"` or `"3.14"`).
/// Non-numeric output fails with `parse_error`. Returns 1.0 inside the
/// range, 0.0 outside.
pub fn numeric_range(input: &ScorerInput, config: &NumericRangeConfig) -> ScorerResult {
    if config.min.is_none() && config.max.is_none() {
        return ScorerResult {
            score: 0.0,
            passed: Some(false),
            label: Some("config_error".into()),
            explanation: Some("numeric_range requires at least one of `min` or `max`".into()),
            metadata: None,
        };
    }

    let value = match &input.output {
        Value::Number(n) => n.as_f64(),
        Value::String(s) => s.trim().parse::<f64>().ok(),
        _ => None,
    };

    let Some(value) = value else {
        return ScorerResult {
            score: 0.0,
            passed: Some(false),
            label: Some("parse_error".into()),
            explanation: Some(format!(
                "output is not numeric: {}",
                serde_json::to_string(&input.output).unwrap_or_default()
            )),
            metadata: None,
        };
    };

    let inclusive = config.inclusive.unwrap_or(true);
    let above_min = match config.min {
        Some(min) if inclusive => value >= min,
        Some(min) => value > min,
        None => true,
    };
    let below_max = match config.max {
        Some(max) if inclusive => value <= max,
        Some(max) => value < max,
        None => true,
    };
    let in_range = above_min && below_max;

    ScorerResult {
        score: if in_range { 1.0 } else { 0.0 },
        passed: Some(in_range),
        label: Some(if in_range { "in_range" } else { "out_of_range" }.into()),
        explanation: Some(format!(
            "value={value}, min={:?}, max={:?}, inclusive={inclusive}",
            config.min, config.max
        )),
        metadata: None,
    }
}

/// Convert a JSON Value to a string for comparison
fn value_to_string(v: &Value) -> String {
    match v {
        Value::String(s) => s.clone(),
        Value::Null => String::new(),
        _ => v.to_string(),
    }
}

/// Calculate Levenshtein edit distance between two strings
fn levenshtein_distance(a: &str, b: &str) -> usize {
    let a_chars: Vec<char> = a.chars().collect();
    let b_chars: Vec<char> = b.chars().collect();
    let m = a_chars.len();
    let n = b_chars.len();

    if m == 0 {
        return n;
    }
    if n == 0 {
        return m;
    }

    // Use two rows instead of full matrix for space efficiency
    let mut prev_row: Vec<usize> = (0..=n).collect();
    let mut curr_row: Vec<usize> = vec![0; n + 1];

    for i in 1..=m {
        curr_row[0] = i;
        for j in 1..=n {
            let cost = if a_chars[i - 1] == b_chars[j - 1] {
                0
            } else {
                1
            };
            curr_row[j] = (prev_row[j] + 1)
                .min(curr_row[j - 1] + 1)
                .min(prev_row[j - 1] + cost);
        }
        std::mem::swap(&mut prev_row, &mut curr_row);
    }

    prev_row[n]
}

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

    #[test]
    fn test_exact_match() {
        // Exact match
        let input = ScorerInput::new(json!("hello")).with_expected(json!("hello"));
        let result = exact_match(&input, &ExactMatchConfig::default());
        assert_eq!(result.score, 1.0);
        assert_eq!(result.passed, Some(true));

        // Mismatch
        let input = ScorerInput::new(json!("hello")).with_expected(json!("world"));
        let result = exact_match(&input, &ExactMatchConfig::default());
        assert_eq!(result.score, 0.0);
        assert_eq!(result.passed, Some(false));

        // Case insensitive
        let input = ScorerInput::new(json!("HELLO")).with_expected(json!("hello"));
        let config = ExactMatchConfig {
            case_sensitive: Some(false),
        };
        let result = exact_match(&input, &config);
        assert_eq!(result.score, 1.0);
    }

    #[test]
    fn test_contains() {
        // Found
        let input = ScorerInput::new(json!("hello world"));
        let config = ContainsConfig {
            pattern: "world".into(),
            case_sensitive: Some(true),
        };
        let result = contains(&input, &config);
        assert_eq!(result.score, 1.0);

        // Not found
        let config = ContainsConfig {
            pattern: "foo".into(),
            case_sensitive: Some(true),
        };
        let result = contains(&input, &config);
        assert_eq!(result.score, 0.0);

        // Case insensitive
        let config = ContainsConfig {
            pattern: "WORLD".into(),
            case_sensitive: Some(false),
        };
        let result = contains(&input, &config);
        assert_eq!(result.score, 1.0);
    }

    #[test]
    fn test_json_valid() {
        // Valid JSON string
        let input = ScorerInput::new(json!(r#"{"key": "value"}"#));
        let result = json_valid(&input);
        assert_eq!(result.score, 1.0);

        // Invalid JSON string
        let input = ScorerInput::new(json!("{invalid}"));
        let result = json_valid(&input);
        assert_eq!(result.score, 0.0);

        // Already a JSON object
        let input = ScorerInput::new(json!({"key": "value"}));
        let result = json_valid(&input);
        assert_eq!(result.score, 1.0);
    }

    #[test]
    fn test_regex_match() {
        // Match
        let input = ScorerInput::new(json!("hello123world"));
        let config = RegexConfig {
            pattern: r"\d+".into(),
        };
        let result = regex_match(&input, &config);
        assert_eq!(result.score, 1.0);

        // No match
        let input = ScorerInput::new(json!("helloworld"));
        let result = regex_match(&input, &config);
        assert_eq!(result.score, 0.0);

        // Invalid regex
        let config = RegexConfig {
            pattern: r"[".into(),
        };
        let result = regex_match(&input, &config);
        assert_eq!(result.score, 0.0);
        assert_eq!(result.label, Some("error".into()));
    }

    #[test]
    fn test_levenshtein() {
        // Identical strings
        let input = ScorerInput::new(json!("hello")).with_expected(json!("hello"));
        let result = levenshtein(&input, &LevenshteinConfig::default());
        assert_eq!(result.score, 1.0);

        // Different strings
        let input = ScorerInput::new(json!("hello")).with_expected(json!("hallo"));
        let result = levenshtein(&input, &LevenshteinConfig::default());
        assert!(result.score > 0.7); // 4/5 = 0.8 similarity

        // With threshold
        let config = LevenshteinConfig {
            threshold: Some(0.9),
        };
        let result = levenshtein(&input, &config);
        assert_eq!(result.passed, Some(false)); // 0.8 < 0.9
    }

    #[test]
    fn test_json_schema_valid_object() {
        let schema = json!({
            "type": "object",
            "required": ["name"],
            "properties": {"name": {"type": "string"}}
        });
        let input = ScorerInput::new(json!({"name": "Ada"}));
        let result = json_schema(&input, &JsonSchemaConfig { schema });
        assert_eq!(result.score, 1.0);
        assert_eq!(result.label.as_deref(), Some("valid"));
    }

    #[test]
    fn test_json_schema_invalid_object() {
        let schema = json!({
            "type": "object",
            "required": ["name", "age"],
            "properties": {
                "name": {"type": "string"},
                "age": {"type": "integer", "minimum": 0}
            }
        });
        let input = ScorerInput::new(json!({"name": "Ada", "age": -5}));
        let result = json_schema(&input, &JsonSchemaConfig { schema });
        assert_eq!(result.score, 0.0);
        assert!(result.explanation.is_some());
    }

    #[test]
    fn test_json_schema_string_parses_json() {
        let schema = json!({"type": "array", "items": {"type": "integer"}});
        let input = ScorerInput::new(json!("[1, 2, 3]"));
        let result = json_schema(&input, &JsonSchemaConfig { schema });
        assert_eq!(result.score, 1.0);
    }

    #[test]
    fn test_json_schema_invalid_schema_is_config_error() {
        // Schemas must be objects/booleans, not strings.
        let input = ScorerInput::new(json!({"x": 1}));
        let result = json_schema(
            &input,
            &JsonSchemaConfig {
                schema: json!("not a schema"),
            },
        );
        assert_eq!(result.label.as_deref(), Some("config_error"));
    }

    #[test]
    fn test_numeric_range_basics() {
        let cfg = NumericRangeConfig {
            min: Some(0.0),
            max: Some(10.0),
            inclusive: None,
        };
        assert_eq!(numeric_range(&ScorerInput::new(json!(5)), &cfg).score, 1.0);
        assert_eq!(numeric_range(&ScorerInput::new(json!(0)), &cfg).score, 1.0);
        assert_eq!(numeric_range(&ScorerInput::new(json!(10)), &cfg).score, 1.0);
        assert_eq!(numeric_range(&ScorerInput::new(json!(-1)), &cfg).score, 0.0);
        assert_eq!(numeric_range(&ScorerInput::new(json!(11)), &cfg).score, 0.0);
    }

    #[test]
    fn test_numeric_range_exclusive() {
        let cfg = NumericRangeConfig {
            min: Some(0.0),
            max: Some(10.0),
            inclusive: Some(false),
        };
        assert_eq!(numeric_range(&ScorerInput::new(json!(0)), &cfg).score, 0.0);
        assert_eq!(numeric_range(&ScorerInput::new(json!(10)), &cfg).score, 0.0);
        assert_eq!(numeric_range(&ScorerInput::new(json!(5)), &cfg).score, 1.0);
    }

    #[test]
    fn test_numeric_range_one_sided() {
        // Lower bound only.
        let cfg = NumericRangeConfig {
            min: Some(100.0),
            max: None,
            inclusive: None,
        };
        assert_eq!(numeric_range(&ScorerInput::new(json!(99)), &cfg).score, 0.0);
        assert_eq!(
            numeric_range(&ScorerInput::new(json!(100)), &cfg).score,
            1.0
        );
        assert_eq!(
            numeric_range(&ScorerInput::new(json!(1_000_000)), &cfg).score,
            1.0
        );

        // Upper bound only.
        let cfg = NumericRangeConfig {
            min: None,
            max: Some(0.5),
            inclusive: None,
        };
        assert_eq!(
            numeric_range(&ScorerInput::new(json!(0.4)), &cfg).score,
            1.0
        );
        assert_eq!(
            numeric_range(&ScorerInput::new(json!(0.5)), &cfg).score,
            1.0
        );
        assert_eq!(
            numeric_range(&ScorerInput::new(json!(0.6)), &cfg).score,
            0.0
        );
    }

    #[test]
    fn test_numeric_range_requires_a_bound() {
        let cfg = NumericRangeConfig::default();
        let r = numeric_range(&ScorerInput::new(json!(1)), &cfg);
        assert_eq!(r.label.as_deref(), Some("config_error"));
    }

    #[test]
    fn test_levenshtein_distance() {
        assert_eq!(levenshtein_distance("", ""), 0);
        assert_eq!(levenshtein_distance("hello", "hello"), 0);
        assert_eq!(levenshtein_distance("hello", ""), 5);
        assert_eq!(levenshtein_distance("", "hello"), 5);
        assert_eq!(levenshtein_distance("hello", "hallo"), 1);
        assert_eq!(levenshtein_distance("kitten", "sitting"), 3);
    }
}