objectiveai 0.1.4

ObjectiveAI SDK, definitions, 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
579
580
581
//! Function types and client-side compilation.

use serde::{Deserialize, Serialize};
use std::sync::LazyLock;

/// A Function definition, either remote (GitHub-hosted) or inline.
///
/// Functions are composable scoring pipelines that transform structured input
/// into scores. Use [`compile_tasks`](Self::compile_tasks) and
/// [`compile_output`](Self::compile_output) to preview how expressions resolve
/// for given inputs.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Function {
    /// A GitHub-hosted function with metadata (description, schema, changelog, etc.).
    Remote(RemoteFunction),
    /// An inline function definition without metadata.
    Inline(InlineFunction),
}

impl Function {
    /// Compiles task expressions to show the final tasks for a given input.
    ///
    /// Evaluates all JMESPath expressions in the function's tasks using the
    /// provided input data. Tasks with `skip` expressions that evaluate to true
    /// return `None`. Tasks with `map` fields produce multiple task instances.
    ///
    /// # Returns
    ///
    /// A vector where each element corresponds to a task definition:
    /// - `None` if the task was skipped
    /// - `Some(CompiledTask::One(...))` for non-mapped tasks
    /// - `Some(CompiledTask::Many(...))` for mapped tasks
    pub fn compile_tasks(
        self,
        input: &super::expression::Input,
    ) -> Result<
        Vec<Option<super::CompiledTask>>,
        super::expression::ExpressionError,
    > {
        static EMPTY_TASKS: LazyLock<
            Vec<Option<super::expression::TaskOutput>>,
        > = LazyLock::new(|| Vec::new());

        // extract input_maps expression and task expressions
        let (input_maps_expr, task_exprs) = match self {
            Function::Remote(RemoteFunction::Scalar {
                input_maps,
                tasks,
                ..
            }) => (input_maps, tasks),
            Function::Remote(RemoteFunction::Vector {
                input_maps,
                tasks,
                ..
            }) => (input_maps, tasks),
            Function::Inline(InlineFunction::Scalar {
                input_maps,
                tasks,
                ..
            }) => (input_maps, tasks),
            Function::Inline(InlineFunction::Vector {
                input_maps,
                tasks,
                ..
            }) => (input_maps, tasks),
        };

        // prepare params for compiling expressions
        let mut params =
            super::expression::Params::Ref(super::expression::ParamsRef {
                input,
                tasks: &EMPTY_TASKS,
                map: None,
            });

        // compile input_maps
        let input_maps = if let Some(input_maps_expr) = input_maps_expr {
            Some(input_maps_expr.compile(&params)?)
        } else {
            None
        };

        // compile tasks
        let mut tasks = Vec::with_capacity(task_exprs.len());
        for mut task_expr in task_exprs {
            tasks.push(
                if let Some(skip_expr) = task_expr.take_skip()
                    && skip_expr.compile_one::<bool>(&params)?
                {
                    // None if task is skipped
                    None
                } else if let Some(input_map_index) = task_expr.input_map() {
                    // for map tasks, map input to multiple instances of the task
                    if let Some(input_maps) = &input_maps
                        && let Some(input_map) =
                            input_maps.get(input_map_index as usize)
                    {
                        // compile task for each map input
                        let mut map_tasks = Vec::with_capacity(input_map.len());
                        for input in input_map {
                            // set map input
                            match &mut params {
                                super::expression::Params::Ref(params_ref) => {
                                    params_ref.map = Some(input);
                                }
                                _ => unreachable!(),
                            }
                            // compile task with map input
                            map_tasks.push(task_expr.clone().compile(&params)?);
                            // reset map input
                            match &mut params {
                                super::expression::Params::Ref(params_ref) => {
                                    params_ref.map = None;
                                }
                                _ => unreachable!(),
                            }
                        }
                        Some(super::CompiledTask::Many(map_tasks))
                    } else {
                        // no map found is treated as empty map
                        Some(super::CompiledTask::Many(Vec::new()))
                    }
                } else {
                    // compile single task
                    Some(super::CompiledTask::One(task_expr.compile(&params)?))
                },
            );
        }

        // compiled tasks
        Ok(tasks)
    }

    /// Computes the final output given input and task outputs.
    ///
    /// Evaluates the function's output expression using the provided input data
    /// and task results. Also validates that the output meets constraints:
    /// - Scalar functions: output must be in [0, 1]
    /// - Vector functions: output must sum to approximately 1
    pub fn compile_output(
        self,
        input: &super::expression::Input,
        task_outputs: &[Option<super::expression::TaskOutput>],
    ) -> Result<
        super::expression::CompiledFunctionOutput,
        super::expression::ExpressionError,
    > {
        #[derive(Clone, Copy)]
        enum FunctionType {
            Scalar,
            Vector,
        }
        static EMPTY_TASKS: LazyLock<
            Vec<Option<super::expression::TaskOutput>>,
        > = LazyLock::new(|| Vec::new());

        // prepare params for compiling output_length expression
        let mut params =
            super::expression::Params::Ref(super::expression::ParamsRef {
                input,
                tasks: &EMPTY_TASKS,
                map: None,
            });

        // extract output expression and output_length
        let (function_type, output_expr, output_length) = match self {
            Function::Remote(RemoteFunction::Scalar { output, .. }) => {
                (FunctionType::Scalar, output, None)
            }
            Function::Remote(RemoteFunction::Vector {
                output,
                output_length,
                ..
            }) => (
                FunctionType::Vector,
                output,
                Some(output_length.compile_one(&params)?),
            ),
            Function::Inline(InlineFunction::Scalar { output, .. }) => {
                (FunctionType::Scalar, output, None)
            }
            Function::Inline(InlineFunction::Vector { output, .. }) => {
                (FunctionType::Vector, output, None)
            }
        };

        // prepare params for compiling output expression
        match &mut params {
            super::expression::Params::Ref(params_ref) => {
                params_ref.tasks = task_outputs;
            }
            _ => unreachable!(),
        }

        // compile output
        let output = output_expr
            .compile_one::<super::expression::FunctionOutput>(&params)?;

        // validate output
        let valid = match (function_type, &output, output_length) {
            (
                FunctionType::Scalar,
                &super::expression::FunctionOutput::Scalar(scalar),
                _,
            ) => {
                scalar >= rust_decimal::Decimal::ZERO
                    && scalar <= rust_decimal::Decimal::ONE
            }
            (
                FunctionType::Vector,
                super::expression::FunctionOutput::Vector(vector),
                Some(length),
            ) => {
                let sum = vector.iter().sum::<rust_decimal::Decimal>();
                vector.len() == length as usize
                    && sum >= rust_decimal::dec!(0.99)
                    && sum <= rust_decimal::dec!(1.01)
            }
            (
                FunctionType::Vector,
                super::expression::FunctionOutput::Vector(vector),
                None,
            ) => {
                let sum = vector.iter().sum::<rust_decimal::Decimal>();
                sum >= rust_decimal::dec!(0.99)
                    && sum <= rust_decimal::dec!(1.01)
            }
            _ => false,
        };

        // compiled output
        Ok(super::expression::CompiledFunctionOutput { output, valid })
    }

    /// Returns the function's description, if available.
    pub fn description(&self) -> Option<&str> {
        match self {
            Function::Remote(remote_function) => {
                Some(remote_function.description())
            }
            Function::Inline(_) => None,
        }
    }

    /// Returns the function's changelog, if available.
    pub fn changelog(&self) -> Option<&str> {
        match self {
            Function::Remote(remote_function) => remote_function.changelog(),
            Function::Inline(_) => None,
        }
    }

    /// Returns the function's input schema, if available.
    pub fn input_schema(&self) -> Option<&super::expression::InputSchema> {
        match self {
            Function::Remote(remote_function) => {
                Some(remote_function.input_schema())
            }
            Function::Inline(_) => None,
        }
    }

    /// Returns the function's input maps, if defined.
    pub fn input_maps(&self) -> Option<&super::expression::InputMaps> {
        match self {
            Function::Remote(remote_function) => remote_function.input_maps(),
            Function::Inline(inline_function) => inline_function.input_maps(),
        }
    }

    /// Returns the function's tasks.
    pub fn tasks(&self) -> &[super::TaskExpression] {
        match self {
            Function::Remote(remote_function) => remote_function.tasks(),
            Function::Inline(inline_function) => inline_function.tasks(),
        }
    }

    /// Returns the function's output expression.
    pub fn output(&self) -> &super::expression::Expression {
        match self {
            Function::Remote(remote_function) => remote_function.output(),
            Function::Inline(inline_function) => inline_function.output(),
        }
    }

    /// Returns the function's expected output length expression, if defined.
    pub fn output_length(
        &self,
    ) -> Option<&super::expression::WithExpression<u64>> {
        match self {
            Function::Remote(remote_function) => {
                remote_function.output_length()
            }
            Function::Inline(_) => None,
        }
    }

    /// Returns the function's input_split expression, if defined.
    pub fn input_split(
        &self,
    ) -> Option<&super::expression::WithExpression<Vec<super::expression::Input>>>
    {
        match self {
            Function::Remote(remote_function) => remote_function.input_split(),
            Function::Inline(inline_function) => inline_function.input_split(),
        }
    }

    /// Returns the function's input_merge expression, if defined.
    pub fn input_merge(
        &self,
    ) -> Option<&super::expression::WithExpression<super::expression::Input>>
    {
        match self {
            Function::Remote(remote_function) => remote_function.input_merge(),
            Function::Inline(inline_function) => inline_function.input_merge(),
        }
    }
}

/// A GitHub-hosted function with full metadata.
///
/// Remote functions are stored as `function.json` in GitHub repositories and
/// referenced by `owner/repository`. They include documentation fields that
/// inline functions lack.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum RemoteFunction {
    /// Produces a single score in [0, 1].
    #[serde(rename = "scalar.function")]
    Scalar {
        /// Human-readable description of what the function does.
        description: String,
        /// Version history and changes for this function.
        #[serde(skip_serializing_if = "Option::is_none")]
        changelog: Option<String>,
        /// JSON Schema defining the expected input structure.
        input_schema: super::expression::InputSchema,
        /// Expressions that transform input into a 2D array for mapped tasks.
        /// Each sub-array can be referenced by tasks via their `map` index.
        /// Receives: `input`.
        #[serde(skip_serializing_if = "Option::is_none")]
        input_maps: Option<super::expression::InputMaps>,
        /// The list of tasks to execute. Tasks with a `map` index are expanded
        /// into multiple instances, one per element in the referenced sub-array.
        /// Each instance is compiled with `map` set to that element's value.
        /// Receives: `input`, `map` (if mapped).
        tasks: Vec<super::TaskExpression>,
        /// Expression computing the final score from task results.
        /// Receives: `input`, `tasks`.
        output: super::expression::Expression,
    },
    /// Produces a vector of scores that sums to 1.
    #[serde(rename = "vector.function")]
    Vector {
        /// Human-readable description of what the function does.
        description: String,
        /// Version history and changes for this function.
        #[serde(skip_serializing_if = "Option::is_none")]
        changelog: Option<String>,
        /// JSON Schema defining the expected input structure.
        input_schema: super::expression::InputSchema,
        /// Expressions that transform input into a 2D array for mapped tasks.
        /// Each sub-array can be referenced by tasks via their `map` index.
        /// Receives: `input`.
        #[serde(skip_serializing_if = "Option::is_none")]
        input_maps: Option<super::expression::InputMaps>,
        /// The list of tasks to execute. Tasks with a `map` index are expanded
        /// into multiple instances, one per element in the referenced sub-array.
        /// Each instance is compiled with `map` set to that element's value.
        /// Receives: `input`, `map` (if mapped).
        tasks: Vec<super::TaskExpression>,
        /// Expression computing the final score vector from task results.
        /// Receives: `input`, `tasks`.
        output: super::expression::Expression,
        /// Expression computing the expected output vector length.
        /// Receives: `input`.
        output_length: super::expression::WithExpression<u64>,
        /// Expression transforming input into an input array of the output_length
        /// When the Function is executed with any input from the array,
        /// The output_length should be 1.
        /// Receives: `input`.
        input_split:
            super::expression::WithExpression<Vec<super::expression::Input>>,
        /// Expression transforming an array of inputs computed by `input_split`
        /// into a single Input object for the Function.
        /// Receives: `input` (as an array).
        input_merge:
            super::expression::WithExpression<super::expression::Input>,
    },
}

impl RemoteFunction {
    /// Returns the function's description.
    pub fn description(&self) -> &str {
        match self {
            RemoteFunction::Scalar { description, .. } => description,
            RemoteFunction::Vector { description, .. } => description,
        }
    }

    /// Returns the function's changelog, if present.
    pub fn changelog(&self) -> Option<&str> {
        match self {
            RemoteFunction::Scalar { changelog, .. } => changelog.as_deref(),
            RemoteFunction::Vector { changelog, .. } => changelog.as_deref(),
        }
    }

    /// Returns the function's input schema.
    pub fn input_schema(&self) -> &super::expression::InputSchema {
        match self {
            RemoteFunction::Scalar { input_schema, .. } => input_schema,
            RemoteFunction::Vector { input_schema, .. } => input_schema,
        }
    }

    /// Returns the function's input maps, if defined.
    pub fn input_maps(&self) -> Option<&super::expression::InputMaps> {
        match self {
            RemoteFunction::Scalar { input_maps, .. } => input_maps.as_ref(),
            RemoteFunction::Vector { input_maps, .. } => input_maps.as_ref(),
        }
    }

    /// Returns the function's tasks.
    pub fn tasks(&self) -> &[super::TaskExpression] {
        match self {
            RemoteFunction::Scalar { tasks, .. } => tasks,
            RemoteFunction::Vector { tasks, .. } => tasks,
        }
    }

    /// Returns the function's output expression.
    pub fn output(&self) -> &super::expression::Expression {
        match self {
            RemoteFunction::Scalar { output, .. } => output,
            RemoteFunction::Vector { output, .. } => output,
        }
    }

    /// Returns the function's expected output length, if defined (vector functions only).
    pub fn output_length(
        &self,
    ) -> Option<&super::expression::WithExpression<u64>> {
        match self {
            RemoteFunction::Scalar { .. } => None,
            RemoteFunction::Vector { output_length, .. } => Some(output_length),
        }
    }

    /// Returns the function's input_split expression, if defined (vector functions only).
    pub fn input_split(
        &self,
    ) -> Option<&super::expression::WithExpression<Vec<super::expression::Input>>>
    {
        match self {
            RemoteFunction::Scalar { .. } => None,
            RemoteFunction::Vector { input_split, .. } => Some(input_split),
        }
    }

    /// Returns the function's input_merge expression, if defined (vector functions only).
    pub fn input_merge(
        &self,
    ) -> Option<&super::expression::WithExpression<super::expression::Input>>
    {
        match self {
            RemoteFunction::Scalar { .. } => None,
            RemoteFunction::Vector { input_merge, .. } => Some(input_merge),
        }
    }
}

/// An inline function definition without metadata.
///
/// Used when embedding function logic directly in requests rather than
/// referencing a GitHub-hosted function. Lacks description, changelog,
/// and input schema fields.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum InlineFunction {
    /// Produces a single score in [0, 1].
    #[serde(rename = "scalar.function")]
    Scalar {
        /// Expressions that transform input into a 2D array for mapped tasks.
        /// Each sub-array can be referenced by tasks via their `map` index.
        /// Receives: `input`.
        #[serde(skip_serializing_if = "Option::is_none")]
        input_maps: Option<super::expression::InputMaps>,
        /// The list of tasks to execute. Tasks with a `map` index are expanded
        /// into multiple instances, one per element in the referenced sub-array.
        /// Each instance is compiled with `map` set to that element's value.
        /// Receives: `input`, `map` (if mapped).
        tasks: Vec<super::TaskExpression>,
        /// Expression computing the final score from task results.
        /// Receives: `input`, `tasks`.
        output: super::expression::Expression,
    },
    /// Produces a vector of scores that sums to 1.
    #[serde(rename = "vector.function")]
    Vector {
        /// Expressions that transform input into a 2D array for mapped tasks.
        /// Each sub-array can be referenced by tasks via their `map` index.
        /// Receives: `input`.
        #[serde(skip_serializing_if = "Option::is_none")]
        input_maps: Option<super::expression::InputMaps>,
        /// The list of tasks to execute. Tasks with a `map` index are expanded
        /// into multiple instances, one per element in the referenced sub-array.
        /// Each instance is compiled with `map` set to that element's value.
        /// Receives: `input`, `map` (if mapped).
        tasks: Vec<super::TaskExpression>,
        /// Expression computing the final score vector from task results.
        /// Receives: `input`, `tasks`.
        output: super::expression::Expression,
        /// Expression transforming input into an input array of the output_length
        /// When the Function is executed with any input from the array,
        /// The output_length should be 1.
        /// Receives: `input`.
        /// Only required if the request uses a strategy that needs input splitting.
        input_split: Option<
            super::expression::WithExpression<Vec<super::expression::Input>>,
        >,
        /// Expression transforming an array of inputs computed by `input_split`
        /// into a single Input object for the Function.
        /// Receives: `input` (as an array).
        /// Only required if the request uses a strategy that needs input splitting.
        input_merge:
            Option<super::expression::WithExpression<super::expression::Input>>,
    },
}

impl InlineFunction {
    /// Returns the function's input maps, if defined.
    pub fn input_maps(&self) -> Option<&super::expression::InputMaps> {
        match self {
            InlineFunction::Scalar { input_maps, .. } => input_maps.as_ref(),
            InlineFunction::Vector { input_maps, .. } => input_maps.as_ref(),
        }
    }

    /// Returns the function's tasks.
    pub fn tasks(&self) -> &[super::TaskExpression] {
        match self {
            InlineFunction::Scalar { tasks, .. } => tasks,
            InlineFunction::Vector { tasks, .. } => tasks,
        }
    }

    /// Returns the function's output expression.
    pub fn output(&self) -> &super::expression::Expression {
        match self {
            InlineFunction::Scalar { output, .. } => output,
            InlineFunction::Vector { output, .. } => output,
        }
    }

    /// Returns the function's input_split expression, if defined (vector functions only).
    pub fn input_split(
        &self,
    ) -> Option<&super::expression::WithExpression<Vec<super::expression::Input>>>
    {
        match self {
            InlineFunction::Scalar { .. } => None,
            InlineFunction::Vector { input_split, .. } => input_split.as_ref(),
        }
    }

    /// Returns the function's input_merge expression, if defined (vector functions only).
    pub fn input_merge(
        &self,
    ) -> Option<&super::expression::WithExpression<super::expression::Input>>
    {
        match self {
            InlineFunction::Scalar { .. } => None,
            InlineFunction::Vector { input_merge, .. } => input_merge.as_ref(),
        }
    }
}