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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
//! JMESPath runtime with custom functions for expression evaluation.
//!
//! Extends the standard JMESPath runtime with additional functions:
//! - `add(a, b)` - Addition
//! - `subtract(a, b)` - Subtraction
//! - `multiply(a, b)` - Multiplication
//! - `divide(a, b)` - Division (returns null if dividing by zero)
//! - `mod(a, b)` - Modulo (returns null if dividing by zero)
//! - `json_parse(s)` - Parse a JSON string
//! - `is_null(v)` - Check if a value is null
//! - `if(cond, then, else)` - Conditional expression
use crate::chat;
use jmespath::{
Context, ErrorReason, JmespathError, Rcvar, RuntimeError, Variable,
ast::Ast,
};
use std::{collections::BTreeMap, sync::LazyLock};
/// Global JMESPath runtime instance with custom functions.
pub static JMESPATH_RUNTIME: LazyLock<jmespath::Runtime> = LazyLock::new(
|| {
use jmespath::{
Context, ErrorReason, JmespathError, Rcvar, Runtime, RuntimeError,
Variable,
functions::{ArgumentType, CustomFunction, Signature},
};
use serde_json::Number;
use std::rc::Rc;
// convert arg
fn arg_as_number(
arg: &Rcvar,
ctx: &Context,
position: usize,
) -> Result<f64, JmespathError> {
arg.as_number().ok_or_else(|| {
JmespathError::new(
ctx.expression,
ctx.offset,
ErrorReason::Runtime(RuntimeError::InvalidType {
expected: "number".to_string(),
actual: arg.get_type().to_string(),
position,
}),
)
})
}
fn arg_as_string(
arg: &Rcvar,
ctx: &Context,
position: usize,
) -> Result<String, JmespathError> {
arg.as_string().cloned().ok_or_else(|| {
JmespathError::new(
ctx.expression,
ctx.offset,
ErrorReason::Runtime(RuntimeError::InvalidType {
expected: "string".to_string(),
actual: arg.get_type().to_string(),
position,
}),
)
})
}
fn arg_as_bool(
arg: &Rcvar,
ctx: &Context,
position: usize,
) -> Result<bool, JmespathError> {
arg.as_boolean().ok_or_else(|| {
JmespathError::new(
ctx.expression,
ctx.offset,
ErrorReason::Runtime(RuntimeError::InvalidType {
expected: "boolean".to_string(),
actual: arg.get_type().to_string(),
position,
}),
)
})
}
fn arg_as_array<'var>(
arg: &'var Rcvar,
ctx: &Context,
position: usize,
) -> Result<&'var Vec<Rcvar>, JmespathError> {
arg.as_array().ok_or_else(|| {
JmespathError::new(
ctx.expression,
ctx.offset,
ErrorReason::Runtime(RuntimeError::InvalidType {
expected: "array".to_string(),
actual: arg.get_type().to_string(),
position,
}),
)
})
}
// extract arg
fn any_arg(
args: &[Rcvar],
ctx: &Context,
position: usize,
expect_args_len: usize,
) -> Result<Rcvar, JmespathError> {
args.get(position)
.ok_or_else(|| {
JmespathError::new(
ctx.expression,
ctx.offset,
ErrorReason::Runtime(
RuntimeError::NotEnoughArguments {
expected: expect_args_len,
actual: args.len(),
},
),
)
})
.cloned()
}
fn number_arg(
args: &[Rcvar],
ctx: &Context,
position: usize,
expect_args_len: usize,
) -> Result<f64, JmespathError> {
let arg = any_arg(args, ctx, position, expect_args_len)?;
arg_as_number(&arg, ctx, position)
}
fn nullable_number_arg(
args: &[Rcvar],
ctx: &Context,
position: usize,
expect_args_len: usize,
) -> Result<Option<f64>, JmespathError> {
let arg = any_arg(args, ctx, position, expect_args_len)?;
if arg.is_null() {
Ok(None)
} else {
Ok(Some(arg_as_number(&arg, ctx, position)?))
}
}
fn string_arg(
args: &[Rcvar],
ctx: &Context,
position: usize,
expect_args_len: usize,
) -> Result<String, JmespathError> {
let arg = any_arg(args, ctx, position, expect_args_len)?;
arg_as_string(&arg, ctx, position)
}
fn nullable_string_arg(
args: &[Rcvar],
ctx: &Context,
position: usize,
expect_args_len: usize,
) -> Result<Option<String>, JmespathError> {
let arg = any_arg(args, ctx, position, expect_args_len)?;
if arg.is_null() {
Ok(None)
} else {
Ok(Some(arg_as_string(&arg, ctx, position)?))
}
}
fn bool_arg(
args: &[Rcvar],
ctx: &Context,
position: usize,
expect_args_len: usize,
) -> Result<bool, JmespathError> {
let arg = any_arg(args, ctx, position, expect_args_len)?;
arg_as_bool(&arg, ctx, position)
}
fn nullable_bool_arg(
args: &[Rcvar],
ctx: &Context,
position: usize,
expect_args_len: usize,
) -> Result<Option<bool>, JmespathError> {
let arg = any_arg(args, ctx, position, expect_args_len)?;
if arg.is_null() {
Ok(None)
} else {
Ok(Some(arg_as_bool(&arg, ctx, position)?))
}
}
fn array_arg(
args: &[Rcvar],
ctx: &Context,
position: usize,
expect_args_len: usize,
) -> Result<Vec<Rcvar>, JmespathError> {
let arg = any_arg(args, ctx, position, expect_args_len)?;
let array = arg_as_array(&arg, ctx, position)?;
Ok(array.clone())
}
fn number_array_arg(
args: &[Rcvar],
ctx: &Context,
position: usize,
expect_args_len: usize,
) -> Result<Vec<f64>, JmespathError> {
let arg = any_arg(args, ctx, position, expect_args_len)?;
let array = arg_as_array(&arg, ctx, position)?;
let mut numbers = Vec::with_capacity(array.len());
for item in array.iter() {
let number = arg_as_number(item, ctx, position)?;
numbers.push(number);
}
Ok(numbers)
}
// return value
fn rcvar_f64(n: f64) -> Rcvar {
Rc::new(Variable::Number(
Number::from_f64(n).unwrap_or(Number::from_f64(0.0).unwrap()),
))
}
fn rcvar_f64_u64(n: f64) -> Rcvar {
Rc::new(Variable::Number(Number::from(n.round() as u64)))
}
let mut runtime = Runtime::new();
// https://jmespath.org/specification.html
runtime.register_builtin_functions();
// basic math
runtime.register_function(
"add",
Box::new(CustomFunction::new(
Signature::new(
vec![ArgumentType::Number, ArgumentType::Number],
None,
),
Box::new(|args: &[Rcvar], ctx: &mut Context| {
let a = number_arg(args, ctx, 0, 2)?;
let b = number_arg(args, ctx, 1, 2)?;
Ok(rcvar_f64(a + b))
}),
)),
);
runtime.register_function(
"subtract",
Box::new(CustomFunction::new(
Signature::new(
vec![ArgumentType::Number, ArgumentType::Number],
None,
),
Box::new(|args: &[Rcvar], ctx: &mut Context| {
let a = number_arg(args, ctx, 0, 2)?;
let b = number_arg(args, ctx, 1, 2)?;
Ok(rcvar_f64(a - b))
}),
)),
);
runtime.register_function(
"multiply",
Box::new(CustomFunction::new(
Signature::new(
vec![ArgumentType::Number, ArgumentType::Number],
None,
),
Box::new(|args: &[Rcvar], ctx: &mut Context| {
let a = number_arg(args, ctx, 0, 2)?;
let b = number_arg(args, ctx, 1, 2)?;
Ok(rcvar_f64(a * b))
}),
)),
);
runtime.register_function(
"divide",
Box::new(CustomFunction::new(
Signature::new(
vec![ArgumentType::Number, ArgumentType::Number],
None,
),
Box::new(|args: &[Rcvar], ctx: &mut Context| {
let a = number_arg(args, ctx, 0, 2)?;
let b = number_arg(args, ctx, 1, 2)?;
if b == 0.0 {
Ok(Rc::new(Variable::Null))
} else {
Ok(rcvar_f64(a / b))
}
}),
)),
);
runtime.register_function(
"mod",
Box::new(CustomFunction::new(
Signature::new(
vec![ArgumentType::Number, ArgumentType::Number],
None,
),
Box::new(|args: &[Rcvar], ctx: &mut Context| {
let a = number_arg(args, ctx, 0, 2)?;
let b = number_arg(args, ctx, 1, 2)?;
if b == 0.0 {
Ok(Rc::new(Variable::Null))
} else {
Ok(rcvar_f64(a % b))
}
}),
)),
);
// parse JSON into native JMESPath variable
runtime.register_function(
"json_parse",
Box::new(CustomFunction::new(
Signature::new(vec![ArgumentType::String], None),
Box::new(|args: &[Rcvar], ctx: &mut Context| {
let string = string_arg(args, ctx, 0, 1)?;
let variable: jmespath::Variable =
serde_json::from_str(&string)
.unwrap_or(jmespath::Variable::Null);
Ok(Rc::new(variable))
}),
)),
);
// check if value is null
runtime.register_function(
"is_null",
Box::new(CustomFunction::new(
Signature::new(vec![ArgumentType::Any], None),
Box::new(|args: &[Rcvar], ctx: &mut Context| {
let value = any_arg(args, ctx, 0, 1)?;
Ok(Rc::new(Variable::Bool(value.is_null())))
}),
)),
);
// conditional expression
// if(cond, then, else)
runtime.register_function(
"if",
Box::new(CustomFunction::new(
Signature::new(
vec![
ArgumentType::Any,
ArgumentType::Any,
ArgumentType::Any,
],
None,
),
Box::new(|args: &[Rcvar], ctx: &mut Context| {
let condition = any_arg(args, ctx, 0, 3)?;
let then_branch = any_arg(args, ctx, 1, 3)?;
let else_branch = any_arg(args, ctx, 2, 3)?;
if condition.is_truthy() {
Ok(then_branch)
} else {
Ok(else_branch)
}
}),
)),
);
// input_value_switch function
// selects output based on the type of the input value
// evaluates exprefs if necessary
runtime.register_function(
"input_value_switch",
Box::new(CustomFunction::new(
Signature::new(
vec![
ArgumentType::Any, // Input Value
ArgumentType::Any, // Object
ArgumentType::Any, // Array
ArgumentType::Any, // String
ArgumentType::Any, // Integer
ArgumentType::Any, // Number
ArgumentType::Any, // Boolean
ArgumentType::Any, // Image
ArgumentType::Any, // Audio
ArgumentType::Any, // Video
ArgumentType::Any, // File
],
None,
),
Box::new(|args: &[Rcvar], ctx: &mut Context| {
// first arg is input value
let input_value = any_arg(args, ctx, 0, 11)?;
// output_value depends on the type of input_value
let output_value = match serde_json::from_value::<
super::Input,
>(
serde_json::to_value(&input_value)
.unwrap_or(serde_json::Value::Null),
) {
Ok(super::Input::Object(_)) => Ok(
any_arg(args, ctx, 1, 11)?,
),
Ok(super::Input::Array(_)) => Ok(
any_arg(args, ctx, 2, 11)?,
),
Ok(super::Input::String(_)) => Ok(
any_arg(args, ctx, 3, 11)?,
),
Ok(super::Input::Integer(_)) => Ok(
any_arg(args, ctx, 4, 11)?,
),
Ok(super::Input::Number(_)) => Ok(
any_arg(args, ctx, 5, 11)?,
),
Ok(super::Input::Boolean(_)) => Ok(
any_arg(args, ctx, 6, 11)?,
),
Ok(super::Input::RichContentPart(
chat::completions::request::RichContentPart::ImageUrl {
..
}
)) => Ok(any_arg(args, ctx, 7, 11)?),
Ok(super::Input::RichContentPart(
chat::completions::request::RichContentPart::InputAudio {
..
}
)) => Ok(any_arg(args, ctx, 8, 11)?),
Ok(super::Input::RichContentPart(
chat::completions::request::RichContentPart::InputVideo {
..
}
)) => Ok(any_arg(args, ctx, 9, 11)?),
Ok(super::Input::RichContentPart(
chat::completions::request::RichContentPart::VideoUrl {
..
}
)) => Ok(any_arg(args, ctx, 9, 11)?),
Ok(super::Input::RichContentPart(
chat::completions::request::RichContentPart::File {
..
}
)) => Ok(any_arg(args, ctx, 10, 11)?),
Ok(super::Input::RichContentPart(
chat::completions::request::RichContentPart::Text {
..
}
)) => Ok(any_arg(args, ctx, 1, 11)?),
Err(_) => Err(JmespathError::new(
ctx.expression,
ctx.offset,
ErrorReason::Runtime(RuntimeError::InvalidType {
expected: "valid Input type".to_string(),
actual: input_value.get_type().to_string(),
position: 0,
}),
)),
}?;
if let Some(ast) = output_value.as_expref() {
interpret(&input_value, &ast, ctx)
} else {
Ok(output_value)
}
}),
)),
);
// zips a 2D array and maps each column with an expref
// if sub-arrays are of different lengths, fills missing values with null
runtime.register_function(
"zip_map",
Box::new(CustomFunction::new(
Signature::new(
vec![
ArgumentType::Expref,
ArgumentType::TypedArray(Box::new(ArgumentType::Array)),
],
None,
),
Box::new(|args: &[Rcvar], ctx: &mut Context| {
let expref = args[0].as_expref().unwrap();
let input_array = args[1].as_array().unwrap();
let mut output_array = Vec::with_capacity(
input_array
.iter()
.map(|v| v.as_array().unwrap().len())
.max()
.unwrap_or_default(),
);
for i in 0..output_array.capacity() {
let mut column = Vec::with_capacity(input_array.len());
for row in input_array.iter() {
let row_array = row.as_array().unwrap();
if let Some(value) = row_array.get(i) {
column.push(value.clone());
} else {
column.push(Rc::new(Variable::Null));
}
}
output_array.push(interpret(
&Rc::new(Variable::Array(column)),
&expref,
ctx,
)?);
}
Ok(Rc::new(Variable::Array(output_array)))
}),
)),
);
// L1 normalization of a number array
runtime.register_function(
"l1_normalize",
Box::new(CustomFunction::new(
Signature::new(
vec![ArgumentType::TypedArray(Box::new(
ArgumentType::Number,
))],
None,
),
Box::new(|args: &[Rcvar], ctx: &mut Context| {
let numbers = number_array_arg(args, ctx, 0, 1)?;
let sum: f64 = numbers.iter().map(|n| n.abs()).sum();
if numbers.len() == 0 {
Ok(Rc::new(Variable::Array(Vec::new())))
} else if sum == 0.0 {
Ok(Rc::new(Variable::Array(
numbers
.iter()
.map(|_| {
Rc::new(Variable::Number(
Number::from_f64(
1.0 / numbers.len() as f64,
)
.unwrap(),
))
})
.collect(),
)))
} else {
Ok(Rc::new(Variable::Array(
numbers
.iter()
.map(|n| rcvar_f64(n / sum))
.collect(),
)))
}
}),
)),
);
runtime
},
);
/// Copied from jmespath::interpreter
/// https://github.com/jmespath/jmespath.rs/pull/60
/// Interprets the given data using an AST node.
fn interpret(
data: &Rcvar,
node: &Ast,
ctx: &mut Context<'_>,
) -> Result<Rcvar, JmespathError> {
match *node {
Ast::Field { ref name, .. } => Ok(data.get_field(name)),
Ast::Subexpr {
ref lhs, ref rhs, ..
} => {
let left_result = interpret(data, lhs, ctx)?;
interpret(&left_result, rhs, ctx)
}
Ast::Identity { .. } => Ok(data.clone()),
Ast::Literal { ref value, .. } => Ok(value.clone()),
Ast::Index { idx, .. } => {
if idx >= 0 {
Ok(data.get_index(idx as usize))
} else {
Ok(data.get_negative_index((-idx) as usize))
}
}
Ast::Or {
ref lhs, ref rhs, ..
} => {
let left = interpret(data, lhs, ctx)?;
if left.is_truthy() {
Ok(left)
} else {
interpret(data, rhs, ctx)
}
}
Ast::And {
ref lhs, ref rhs, ..
} => {
let left = interpret(data, lhs, ctx)?;
if !left.is_truthy() {
Ok(left)
} else {
interpret(data, rhs, ctx)
}
}
Ast::Not { ref node, .. } => {
let result = interpret(data, node, ctx)?;
Ok(Rcvar::new(Variable::Bool(!result.is_truthy())))
}
// Returns the resut of RHS if cond yields truthy value.
Ast::Condition {
ref predicate,
ref then,
..
} => {
let cond_result = interpret(data, predicate, ctx)?;
if cond_result.is_truthy() {
interpret(data, then, ctx)
} else {
Ok(Rcvar::new(Variable::Null))
}
}
Ast::Comparison {
ref comparator,
ref lhs,
ref rhs,
..
} => {
let left = interpret(data, lhs, ctx)?;
let right = interpret(data, rhs, ctx)?;
Ok(left
.compare(comparator, &right)
.map_or(Rcvar::new(Variable::Null), |result| {
Rcvar::new(Variable::Bool(result))
}))
}
// Converts an object into a JSON array of its values.
Ast::ObjectValues { ref node, .. } => {
let subject = interpret(data, node, ctx)?;
match *subject {
Variable::Object(ref v) => Ok(Rcvar::new(Variable::Array(
v.values().cloned().collect::<Vec<Rcvar>>(),
))),
_ => Ok(Rcvar::new(Variable::Null)),
}
}
// Passes the results of lhs into rhs if lhs yields an array and
// each node of lhs that passes through rhs yields a non-null value.
Ast::Projection {
ref lhs, ref rhs, ..
} => match interpret(data, lhs, ctx)?.as_array() {
None => Ok(Rcvar::new(Variable::Null)),
Some(left) => {
let mut collected = vec![];
for element in left {
let current = interpret(element, rhs, ctx)?;
if !current.is_null() {
collected.push(current);
}
}
Ok(Rcvar::new(Variable::Array(collected)))
}
},
Ast::Flatten { ref node, .. } => match interpret(data, node, ctx)?
.as_array()
{
None => Ok(Rcvar::new(Variable::Null)),
Some(a) => {
let mut collected: Vec<Rcvar> = vec![];
for element in a {
match element.as_array() {
Some(array) => collected.extend(array.iter().cloned()),
_ => collected.push(element.clone()),
}
}
Ok(Rcvar::new(Variable::Array(collected)))
}
},
Ast::MultiList { ref elements, .. } => {
if data.is_null() {
Ok(Rcvar::new(Variable::Null))
} else {
let mut collected = vec![];
for node in elements {
collected.push(interpret(data, node, ctx)?);
}
Ok(Rcvar::new(Variable::Array(collected)))
}
}
Ast::MultiHash { ref elements, .. } => {
if data.is_null() {
Ok(Rcvar::new(Variable::Null))
} else {
let mut collected = BTreeMap::new();
for kvp in elements {
let value = interpret(data, &kvp.value, ctx)?;
collected.insert(kvp.key.clone(), value);
}
Ok(Rcvar::new(Variable::Object(collected)))
}
}
Ast::Function {
ref name,
ref args,
offset,
} => {
let mut fn_args: Vec<Rcvar> = vec![];
for arg in args {
fn_args.push(interpret(data, arg, ctx)?);
}
// Reset the offset so that it points to the function being evaluated.
ctx.offset = offset;
match ctx.runtime.get_function(name) {
Some(f) => f.evaluate(&fn_args, ctx),
None => {
let reason = ErrorReason::Runtime(
RuntimeError::UnknownFunction(name.to_owned()),
);
Err(JmespathError::from_ctx(ctx, reason))
}
}
}
Ast::Expref { ref ast, .. } => {
Ok(Rcvar::new(Variable::Expref(*ast.clone())))
}
Ast::Slice {
start,
stop,
step,
offset,
} => {
if step == 0 {
ctx.offset = offset;
let reason = ErrorReason::Runtime(RuntimeError::InvalidSlice);
Err(JmespathError::from_ctx(ctx, reason))
} else {
match data.slice(start, stop, step) {
Some(array) => Ok(Rcvar::new(Variable::Array(array))),
None => Ok(Rcvar::new(Variable::Null)),
}
}
}
}
}