Skip to main content

rustev_contract/
request.rs

1//! Request identity documents, `rustev.request/1` (spec 004, 3.4.1).
2//!
3//! A request's identity is what determines a backend computation: the full
4//! isolation scope, the actual backend, artifact and descriptor, the exact
5//! canonical projection bytes, and the bound output kind, required value
6//! kind and normalization. Decision id, step id, plan id, timing, retries
7//! and core-applied calibration are deliberately absent (3.4.3). The core
8//! computes it for a pending request; runtime and eval use that one
9//! function.
10
11use serde::{Deserialize, Serialize};
12
13use crate::definition::RequiredKind;
14use crate::descriptor::OutputKind;
15use crate::ids::{ArtifactId, DescriptorId, RequestId};
16use crate::limits::REQUEST_V1;
17use crate::plan::Normalization;
18use crate::schema;
19use crate::scope::Scope;
20
21#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
22#[serde(deny_unknown_fields)]
23pub struct RequestDoc {
24    pub schema: String,
25    pub scope: Scope,
26    /// The actual target's backend: the primary or an explicit fallback.
27    pub backend_id: String,
28    pub artifact: ArtifactId,
29    /// Commits to `input_limit.max_bytes` and `input_limit.on_excess`.
30    pub descriptor: DescriptorId,
31    /// The exact canonical projection bytes, which are UTF-8 JSON: operation,
32    /// task, question, ordered options, candidates, instance and values.
33    pub projection: String,
34    pub output: OutputKind,
35    pub requires: RequiredKind,
36    pub normalization: Normalization,
37}
38
39crate::document::document!(RequestDoc, schema::REQUEST, REQUEST_V1, RequestId);
40
41/// Extraction follows generation's operation envelope inside `projection`.
42/// It leaves all existing request documents and their identities unchanged.
43#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
44#[serde(tag = "operation", rename_all = "snake_case", deny_unknown_fields)]
45pub enum ExtractionRequestOperation {
46    Extract {
47        plan_id: crate::ids::PlanId,
48        request: crate::extraction_documents::ExtractRequest,
49    },
50}
51
52impl ExtractionRequestOperation {
53    /// The exact UTF-8 canonical bytes to store in `RequestDoc::projection`.
54    pub fn projection(&self) -> Result<String, crate::canonical::CanonicalError> {
55        // Canonical JSON is UTF-8 by construction.
56        String::from_utf8(crate::canonical::canonical_bytes(self)?).map_err(|e| {
57            crate::canonical::CanonicalError::Serialize {
58                message: e.to_string(),
59            }
60        })
61    }
62}