objectiveai-sdk 2.0.5

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
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
734
735
736
//! Task types for Function definitions.
//!
//! Tasks are the building blocks of Functions. Each task either calls another
//! Function or runs a Vector Completion. Tasks can be conditionally skipped
//! or mapped over arrays of inputs.
//!
//! # Output Expressions
//!
//! Each task has an `output` expression that transforms its raw result into a
//! [`TaskOutputOwned`](super::expression::TaskOutputOwned). The expression receives
//! an `output` parameter that is one of four variants:
//!
//! - `Scalar(Decimal)` - a single score
//! - `Vector(Vec<Decimal>)` - a vector of scores
//! - `Vectors(Vec<Vec<Decimal>>)` - multiple vectors (from mapped tasks)
//! - `Err(Value)` - an error
//!
//! The expression must return a `TaskOutputOwned` valid for the parent function's type:
//! - **Scalar functions**: must return `Scalar(value)` where value is in [0, 1]
//! - **Vector functions**: must return `Vector(values)` where values sum to ~1 and match the expected length
//!
//! # Output Aggregation
//!
//! The function's final output is computed as a **weighted average** of all task outputs
//! using profile weights. If a function has only one task, that task's output becomes
//! the function's output directly (with weight 1.0).

use crate::agent;
use serde::{Deserialize, Serialize};
use schemars::JsonSchema;

/// A task definition with expressions (pre-compilation).
///
/// Task expressions contain dynamic fields (JMESPath or Starlark) that are
/// resolved against input data during compilation. Use [`compile`](Self::compile)
/// to produce a concrete [`Task`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, arbitrary::Arbitrary)]
#[serde(tag = "type")]
#[schemars(rename = "functions.TaskExpression")]
pub enum TaskExpression {
    #[schemars(title = "ScalarFunction")]
    #[serde(rename = "scalar.function")]
    ScalarFunction(ScalarFunctionTaskExpression),
    #[schemars(title = "VectorFunction")]
    #[serde(rename = "vector.function")]
    VectorFunction(VectorFunctionTaskExpression),
    #[schemars(title = "VectorCompletion")]
    #[serde(rename = "vector.completion")]
    VectorCompletion(VectorCompletionTaskExpression),
    #[schemars(title = "PlaceholderScalarFunction")]
    #[serde(rename = "placeholder.scalar.function")]
    PlaceholderScalarFunction(PlaceholderScalarFunctionTaskExpression),
    #[schemars(title = "PlaceholderVectorFunction")]
    #[serde(rename = "placeholder.vector.function")]
    PlaceholderVectorFunction(PlaceholderVectorFunctionTaskExpression),
}

impl TaskExpression {
    pub fn url(&self) -> Option<String> {
        match self {
            TaskExpression::ScalarFunction(task) => Some(task.url()),
            TaskExpression::VectorFunction(task) => Some(task.url()),
            TaskExpression::VectorCompletion(_) => None,
            TaskExpression::PlaceholderScalarFunction(_) => None,
            TaskExpression::PlaceholderVectorFunction(_) => None,
        }
    }

    /// Takes and returns the skip expression, if present.
    pub fn take_skip(&mut self) -> Option<super::expression::Expression> {
        match self {
            TaskExpression::ScalarFunction(task) => task.skip.take(),
            TaskExpression::VectorFunction(task) => task.skip.take(),
            TaskExpression::VectorCompletion(task) => task.skip.take(),
            TaskExpression::PlaceholderScalarFunction(task) => task.skip.take(),
            TaskExpression::PlaceholderVectorFunction(task) => task.skip.take(),
        }
    }

    /// Returns the map expression, if this is a mapped task.
    pub fn map(&self) -> Option<&super::expression::Expression> {
        match self {
            TaskExpression::ScalarFunction(task) => task.map.as_ref(),
            TaskExpression::VectorFunction(task) => task.map.as_ref(),
            TaskExpression::VectorCompletion(task) => task.map.as_ref(),
            TaskExpression::PlaceholderScalarFunction(task) => {
                task.map.as_ref()
            }
            TaskExpression::PlaceholderVectorFunction(task) => {
                task.map.as_ref()
            }
        }
    }

    /// Compiles the expression into a concrete [`Task`].
    pub fn compile(
        self,
        params: &super::expression::Params,
    ) -> Result<Task, super::expression::ExpressionError> {
        match self {
            TaskExpression::ScalarFunction(task) => {
                task.compile(params).map(Task::ScalarFunction)
            }
            TaskExpression::VectorFunction(task) => {
                task.compile(params).map(Task::VectorFunction)
            }
            TaskExpression::VectorCompletion(task) => {
                task.compile(params).map(Task::VectorCompletion)
            }
            TaskExpression::PlaceholderScalarFunction(task) => {
                task.compile(params).map(Task::PlaceholderScalarFunction)
            }
            TaskExpression::PlaceholderVectorFunction(task) => {
                task.compile(params).map(Task::PlaceholderVectorFunction)
            }
        }
    }
}

/// A compiled task ready for execution.
///
/// Produced by compiling a [`TaskExpression`] against input data. All
/// expressions have been resolved to concrete values.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type")]
#[schemars(rename = "functions.Task")]
pub enum Task {
    /// Calls a scalar function (produces a single score).
    #[schemars(title = "ScalarFunction")]
    #[serde(rename = "scalar.function")]
    ScalarFunction(ScalarFunctionTask),
    /// Calls a vector function (produces a vector of scores).
    #[schemars(title = "VectorFunction")]
    #[serde(rename = "vector.function")]
    VectorFunction(VectorFunctionTask),
    /// Runs a vector completion.
    #[schemars(title = "VectorCompletion")]
    #[serde(rename = "vector.completion")]
    VectorCompletion(VectorCompletionTask),
    /// Placeholder scalar function (always outputs 0.5).
    #[schemars(title = "PlaceholderScalarFunction")]
    #[serde(rename = "placeholder.scalar.function")]
    PlaceholderScalarFunction(PlaceholderScalarFunctionTask),
    /// Placeholder vector function (always outputs equalized vector).
    #[schemars(title = "PlaceholderVectorFunction")]
    #[serde(rename = "placeholder.vector.function")]
    PlaceholderVectorFunction(PlaceholderVectorFunctionTask),
}

impl Task {
    pub fn compile_output(
        &self,
        input: &super::expression::InputValue,
        raw_output: super::expression::TaskOutput,
    ) -> Result<
        super::expression::TaskOutputOwned,
        super::expression::ExpressionError,
    > {
        match self {
            Task::ScalarFunction(task) => {
                task.compile_output(input, raw_output)
            }
            Task::VectorFunction(task) => {
                task.compile_output(input, raw_output)
            }
            Task::VectorCompletion(task) => {
                task.compile_output(input, raw_output)
            }
            Task::PlaceholderScalarFunction(task) => {
                task.compile_output(input, raw_output)
            }
            Task::PlaceholderVectorFunction(task) => {
                task.compile_output(input, raw_output)
            }
        }
    }
}

/// Expression for a task that calls a scalar function (pre-compilation).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, arbitrary::Arbitrary)]
#[schemars(rename = "functions.ScalarFunctionTaskExpression")]
pub struct ScalarFunctionTaskExpression {
    #[serde(flatten)]
    #[schemars(schema_with = "crate::flatten_schema::<crate::RemotePath>")]
    pub path: crate::RemotePath,

    /// If this expression evaluates to true, skip the task. Receives: `input`.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(extend("omitempty" = true))]
    pub skip: Option<super::expression::Expression>,

    /// Expression that evaluates to the number of mapped task instances.
    /// Each instance receives `map` as an integer index (0-based).
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(extend("omitempty" = true))]
    pub map: Option<super::expression::Expression>,

    /// Expression for the input to pass to the function.
    /// Receives: `input`, `map` (if mapped).
    pub input:
        super::expression::WithExpression<super::expression::InputValueExpression>,

    /// Expression to transform the task result into a valid function output.
    ///
    /// Receives `output` which is one of 4 variants:
    /// - `Scalar(Decimal)` - a single score
    /// - `Vector(Vec<Decimal>)` - a vector of scores
    /// - `Vectors(Vec<Vec<Decimal>>)` - multiple vectors (from mapped tasks)
    /// - `Err(Value)` - an error
    ///
    /// The expression must return a `TaskOutputOwned` that is valid for the parent function's type:
    /// - For scalar functions: must return `Scalar(value)` where value is in [0, 1]
    /// - For vector functions: must return `Vector(values)` where values sum to ~1 and match the expected length
    ///
    /// The function's final output is computed as a weighted average of all task outputs using
    /// profile weights. If a function has only one task, that task's output becomes the function's
    /// output directly.
    pub output: super::expression::Expression,
}

impl ScalarFunctionTaskExpression {
    pub fn url(&self) -> String {
        self.path.url()
    }

    /// Compiles the expression into a concrete [`ScalarFunctionTask`].
    pub fn compile(
        self,
        params: &super::expression::Params,
    ) -> Result<ScalarFunctionTask, super::expression::ExpressionError> {
        let input = self.input.compile_one(params)?.compile(params)?;
        Ok(ScalarFunctionTask {
            path: self.path,
            input,
            output: self.output,
        })
    }
}

/// A compiled scalar function task ready for execution.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "functions.ScalarFunctionTask")]
pub struct ScalarFunctionTask {
    #[serde(flatten)]
    #[schemars(schema_with = "crate::flatten_schema::<crate::RemotePath>")]
    pub path: crate::RemotePath,
    /// The resolved input to pass to the function.
    pub input: super::expression::InputValue,
    /// Expression to transform the task result into a valid function output.
    ///
    /// Receives `output` as the nested function's result (Scalar or Vector).
    /// Must return a `TaskOutputOwned` valid for the parent function's type (scalar or vector).
    /// See [`ScalarFunctionTaskExpression::output`] for full documentation.
    pub output: super::expression::Expression,
}

impl ScalarFunctionTask {
    pub fn url(&self) -> String {
        self.path.url()
    }

    pub fn compile_output(
        &self,
        input: &super::expression::InputValue,
        raw_output: super::expression::TaskOutput,
    ) -> Result<
        super::expression::TaskOutputOwned,
        super::expression::ExpressionError,
    > {
        let params =
            super::expression::Params::Ref(super::expression::ParamsRef {
                input,
                output: Some(raw_output),
                map: None,
                tasks_min: None,
                tasks_max: None,
                depth: None,
                name: None,
                spec: None,
            });
        let compiled_output = self.output.compile_one(&params)?;
        Ok(compiled_output)
    }
}

/// Expression for a task that calls a vector function (pre-compilation).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, arbitrary::Arbitrary)]
#[schemars(rename = "functions.VectorFunctionTaskExpression")]
pub struct VectorFunctionTaskExpression {
    #[serde(flatten)]
    #[schemars(schema_with = "crate::flatten_schema::<crate::RemotePath>")]
    pub path: crate::RemotePath,

    /// If this expression evaluates to true, skip the task. Receives: `input`.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(extend("omitempty" = true))]
    pub skip: Option<super::expression::Expression>,

    /// Expression that evaluates to the number of mapped task instances.
    /// Each instance receives `map` as an integer index (0-based).
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(extend("omitempty" = true))]
    pub map: Option<super::expression::Expression>,

    /// Expression for the input to pass to the function.
    /// Receives: `input`, `map` (if mapped).
    pub input:
        super::expression::WithExpression<super::expression::InputValueExpression>,

    /// Expression to transform the task result into a valid function output.
    ///
    /// Receives `output` which is one of 4 variants:
    /// - `Scalar(Decimal)` - a single score
    /// - `Vector(Vec<Decimal>)` - a vector of scores
    /// - `Vectors(Vec<Vec<Decimal>>)` - multiple vectors (from mapped tasks)
    /// - `Err(Value)` - an error
    ///
    /// The expression must return a `TaskOutputOwned` that is valid for the parent function's type:
    /// - For scalar functions: must return `Scalar(value)` where value is in [0, 1]
    /// - For vector functions: must return `Vector(values)` where values sum to ~1 and match the expected length
    ///
    /// The function's final output is computed as a weighted average of all task outputs using
    /// profile weights. If a function has only one task, that task's output becomes the function's
    /// output directly.
    pub output: super::expression::Expression,
}

impl VectorFunctionTaskExpression {
    pub fn url(&self) -> String {
        self.path.url()
    }

    /// Compiles the expression into a concrete [`VectorFunctionTask`].
    pub fn compile(
        self,
        params: &super::expression::Params,
    ) -> Result<VectorFunctionTask, super::expression::ExpressionError> {
        let input = self.input.compile_one(params)?.compile(params)?;
        Ok(VectorFunctionTask {
            path: self.path,
            input,
            output: self.output,
        })
    }
}

/// A compiled vector function task ready for execution.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "functions.VectorFunctionTask")]
pub struct VectorFunctionTask {
    #[serde(flatten)]
    #[schemars(schema_with = "crate::flatten_schema::<crate::RemotePath>")]
    pub path: crate::RemotePath,
    /// The resolved input to pass to the function.
    pub input: super::expression::InputValue,
    /// Expression to transform the task result into a valid function output.
    ///
    /// Receives `output` as the nested function's result (Scalar or Vector).
    /// Must return a `TaskOutputOwned` valid for the parent function's type (scalar or vector).
    /// See [`VectorFunctionTaskExpression::output`] for full documentation.
    pub output: super::expression::Expression,
}

impl VectorFunctionTask {
    pub fn url(&self) -> String {
        self.path.url()
    }

    pub fn compile_output(
        &self,
        input: &super::expression::InputValue,
        raw_output: super::expression::TaskOutput,
    ) -> Result<
        super::expression::TaskOutputOwned,
        super::expression::ExpressionError,
    > {
        let params =
            super::expression::Params::Ref(super::expression::ParamsRef {
                input,
                output: Some(raw_output),
                map: None,
                tasks_min: None,
                tasks_max: None,
                depth: None,
                name: None,
                spec: None,
            });
        let compiled_output = self.output.compile_one(&params)?;
        Ok(compiled_output)
    }
}

/// Expression for a task that runs a vector completion (pre-compilation).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, arbitrary::Arbitrary)]
#[schemars(rename = "functions.VectorCompletionTaskExpression")]
pub struct VectorCompletionTaskExpression {
    /// If this expression evaluates to true, skip the task. Receives: `input`.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(extend("omitempty" = true))]
    pub skip: Option<super::expression::Expression>,

    /// Expression that evaluates to the number of mapped task instances.
    /// Each instance receives `map` as an integer index (0-based).
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(extend("omitempty" = true))]
    pub map: Option<super::expression::Expression>,

    /// Expression for the conversation messages (the prompt).
    /// Receives: `input`, `map` (if mapped).
    pub messages: super::expression::WithExpression<
        Vec<
            super::expression::WithExpression<
                agent::completions::message::MessageExpression,
            >,
        >,
    >,
    /// Expression for the possible responses the LLMs can vote for.
    /// Receives: `input`, `map` (if mapped).
    pub responses: super::expression::WithExpression<
        Vec<
            super::expression::WithExpression<
                agent::completions::message::RichContentExpression,
            >,
        >,
    >,

    /// Expression to transform the task result into a valid function output.
    ///
    /// Receives `output` as the task's raw result (typically `Vector(scores)`).
    ///
    /// The expression must return a `TaskOutputOwned` that is valid for the parent function's type:
    /// - For scalar functions: must return `Scalar(value)` where value is in [0, 1]
    /// - For vector functions: must return `Vector(values)` where values sum to ~1 and match the expected length
    ///
    /// The function's final output is computed as a weighted average of all task outputs using
    /// profile weights. If a function has only one task, that task's output becomes the function's
    /// output directly.
    pub output: super::expression::Expression,
}

impl VectorCompletionTaskExpression {
    /// Compiles the expression into a concrete [`VectorCompletionTask`].
    pub fn compile(
        self,
        params: &super::expression::Params,
    ) -> Result<VectorCompletionTask, super::expression::ExpressionError> {
        // compile messages
        let messages = self.messages.compile_one(params)?;
        let mut compiled_messages = Vec::with_capacity(messages.len());
        for message in messages {
            match message.compile_one_or_many(params)? {
                super::expression::OneOrMany::One(one_message) => {
                    compiled_messages.push(one_message.compile(params)?);
                }
                super::expression::OneOrMany::Many(many_messages) => {
                    for message in many_messages {
                        compiled_messages.push(message.compile(params)?);
                    }
                }
            }
        }

        // compile responses
        let responses = self.responses.compile_one(params)?;
        let mut compiled_responses = Vec::with_capacity(responses.len());
        for response in responses {
            match response.compile_one_or_many(params)? {
                super::expression::OneOrMany::One(one_response) => {
                    compiled_responses.push(one_response.compile(params)?);
                }
                super::expression::OneOrMany::Many(many_responses) => {
                    for response in many_responses {
                        compiled_responses.push(response.compile(params)?);
                    }
                }
            }
        }

        Ok(VectorCompletionTask {
            messages: compiled_messages,
            responses: compiled_responses,
            output: self.output,
        })
    }
}

/// A compiled vector completion task ready for execution.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "functions.VectorCompletionTask")]
pub struct VectorCompletionTask {
    /// The resolved conversation messages.
    pub messages: Vec<agent::completions::message::Message>,
    /// The resolved response options the LLMs can vote for.
    pub responses: Vec<agent::completions::message::RichContent>,
    /// Expression to transform the task result into a valid function output.
    ///
    /// Receives `output` as the task's raw result (typically `Vector(scores)`).
    /// Must return a `TaskOutputOwned` valid for the parent function's type (scalar or vector).
    /// See [`VectorCompletionTaskExpression::output`] for full documentation.
    pub output: super::expression::Expression,
}

impl VectorCompletionTask {
    pub fn compile_output(
        &self,
        input: &super::expression::InputValue,
        raw_output: super::expression::TaskOutput,
    ) -> Result<
        super::expression::TaskOutputOwned,
        super::expression::ExpressionError,
    > {
        let params =
            super::expression::Params::Ref(super::expression::ParamsRef {
                input,
                output: Some(raw_output),
                map: None,
                tasks_min: None,
                tasks_max: None,
                depth: None,
                name: None,
                spec: None,
            });
        let compiled_output = self.output.compile_one(&params)?;
        Ok(compiled_output)
    }
}

/// Expression for a placeholder scalar function task (pre-compilation).
///
/// Like [`ScalarFunctionTaskExpression`] but without owner/repository/commit.
/// Always produces a fixed output of 0.5.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, arbitrary::Arbitrary)]
#[schemars(rename = "functions.PlaceholderScalarFunctionTaskExpression")]
pub struct PlaceholderScalarFunctionTaskExpression {
    /// JSON Schema defining the expected input structure.
    pub input_schema: super::expression::InputSchema,

    /// If this expression evaluates to true, skip the task. Receives: `input`.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(extend("omitempty" = true))]
    pub skip: Option<super::expression::Expression>,

    /// Expression that evaluates to the number of mapped task instances.
    /// Each instance receives `map` as an integer index (0-based).
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(extend("omitempty" = true))]
    pub map: Option<super::expression::Expression>,

    /// Expression for the input to pass to the placeholder function.
    /// Receives: `input`, `map` (if mapped).
    pub input:
        super::expression::WithExpression<super::expression::InputValueExpression>,

    /// Expression to transform the fixed 0.5 output.
    /// Receives: `input`, `output` as `Scalar(0.5)`.
    pub output: super::expression::Expression,
}

impl PlaceholderScalarFunctionTaskExpression {
    pub fn compile(
        self,
        params: &super::expression::Params,
    ) -> Result<PlaceholderScalarFunctionTask, super::expression::ExpressionError>
    {
        let input = self.input.compile_one(params)?.compile(params)?;
        Ok(PlaceholderScalarFunctionTask {
            input_schema: self.input_schema,
            input,
            output: self.output,
        })
    }
}

/// A compiled placeholder scalar function task.
///
/// Always produces `Scalar(0.5)` before the output expression
/// is applied.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "functions.PlaceholderScalarFunctionTask")]
pub struct PlaceholderScalarFunctionTask {
    /// JSON Schema defining the expected input structure.
    pub input_schema: super::expression::InputSchema,
    /// The resolved input.
    pub input: super::expression::InputValue,
    /// Expression to transform the fixed 0.5 output.
    pub output: super::expression::Expression,
}

impl PlaceholderScalarFunctionTask {
    pub fn compile_output(
        &self,
        input: &super::expression::InputValue,
        raw_output: super::expression::TaskOutput,
    ) -> Result<
        super::expression::TaskOutputOwned,
        super::expression::ExpressionError,
    > {
        let params =
            super::expression::Params::Ref(super::expression::ParamsRef {
                input,
                output: Some(raw_output),
                map: None,
                tasks_min: None,
                tasks_max: None,
                depth: None,
                name: None,
                spec: None,
            });
        let compiled_output = self.output.compile_one(&params)?;
        Ok(compiled_output)
    }
}

/// Expression for a placeholder vector function task (pre-compilation).
///
/// Like [`VectorFunctionTaskExpression`] but without owner/repository/commit.
/// Always produces an equalized vector of length `output_length`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, arbitrary::Arbitrary)]
#[schemars(rename = "functions.PlaceholderVectorFunctionTaskExpression")]
pub struct PlaceholderVectorFunctionTaskExpression {
    /// JSON Schema defining the expected input structure.
    pub input_schema: super::expression::InputSchema,

    /// Expression computing the expected output vector length.
    /// Receives: `input`.
    pub output_length: super::expression::Expression,

    /// Expression transforming input into sub-inputs for swiss system.
    /// Receives: `input`.
    pub input_split: super::expression::Expression,

    /// Expression merging sub-inputs back into one input.
    /// Receives: `input` (as an array).
    pub input_merge: super::expression::Expression,

    /// If this expression evaluates to true, skip the task. Receives: `input`.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(extend("omitempty" = true))]
    pub skip: Option<super::expression::Expression>,

    /// Expression that evaluates to the number of mapped task instances.
    /// Each instance receives `map` as an integer index (0-based).
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(extend("omitempty" = true))]
    pub map: Option<super::expression::Expression>,

    /// Expression for the input to pass to the placeholder function.
    /// Receives: `input`, `map` (if mapped).
    pub input:
        super::expression::WithExpression<super::expression::InputValueExpression>,

    /// Expression to transform the equalized vector output.
    /// Receives: `input`, `output` as `Vector(equalized)`.
    pub output: super::expression::Expression,
}

impl PlaceholderVectorFunctionTaskExpression {
    pub fn compile(
        self,
        params: &super::expression::Params,
    ) -> Result<PlaceholderVectorFunctionTask, super::expression::ExpressionError>
    {
        let input = self.input.compile_one(params)?.compile(params)?;
        Ok(PlaceholderVectorFunctionTask {
            input_schema: self.input_schema,
            output_length: self.output_length,
            input_split: self.input_split,
            input_merge: self.input_merge,
            input,
            output: self.output,
        })
    }
}

/// A compiled placeholder vector function task.
///
/// Always produces `Vector(vec![1/N; output_length])` before
/// the output expression is applied.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "functions.PlaceholderVectorFunctionTask")]
pub struct PlaceholderVectorFunctionTask {
    /// JSON Schema defining the expected input structure.
    pub input_schema: super::expression::InputSchema,
    /// Expression computing the expected output vector length.
    pub output_length: super::expression::Expression,
    /// Expression transforming input into sub-inputs for swiss system.
    pub input_split: super::expression::Expression,
    /// Expression merging sub-inputs back into one input.
    pub input_merge: super::expression::Expression,
    /// The resolved input.
    pub input: super::expression::InputValue,
    /// Expression to transform the equalized vector output.
    pub output: super::expression::Expression,
}

impl PlaceholderVectorFunctionTask {
    pub fn compile_output(
        &self,
        input: &super::expression::InputValue,
        raw_output: super::expression::TaskOutput,
    ) -> Result<
        super::expression::TaskOutputOwned,
        super::expression::ExpressionError,
    > {
        let params =
            super::expression::Params::Ref(super::expression::ParamsRef {
                input,
                output: Some(raw_output),
                map: None,
                tasks_min: None,
                tasks_max: None,
                depth: None,
                name: None,
                spec: None,
            });
        let compiled_output = self.output.compile_one(&params)?;
        Ok(compiled_output)
    }
}

/// The result of compiling a task expression.
///
/// Tasks without a `map` field compile to a single task. Tasks with a `map`
/// expression are expanded into multiple tasks, one per integer index from
/// 0 to the evaluated count.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(untagged)]
#[schemars(rename = "functions.CompiledTask")]
pub enum CompiledTask {
    /// A single task (no mapping).
    #[schemars(title = "One")]
    One(Task),
    /// Multiple task instances from mapped execution.
    #[schemars(title = "Many")]
    Many(Vec<Task>),
}