Skip to main content

causa_kernel/context/material/state/
value.rs

1//! Controlled context material and independent request frames.
2
3use crate::context::block::ContextBlock;
4use serde::Serialize;
5
6use super::EditFailure;
7
8/// The ordered material selected for one model request.
9///
10/// This owned value is independent of the context that produced it. Invocation
11/// identity belongs to the model request rather than this material selection.
12#[derive(Debug, Clone)]
13pub struct ContextFrame {
14    /// Blocks presented to the model, in request order.
15    pub blocks: Vec<ContextBlock>,
16}
17
18/// Ordered material with atomic, identity-preserving edits.
19///
20/// A context has no execution identity or terminal state. Generic edits and
21/// imports allow local tool material without imposing execution pairing rules.
22#[derive(Debug, Clone, Default, Serialize)]
23pub struct Context {
24    pub(super) blocks: Vec<ContextBlock>,
25}
26
27impl Context {
28    /// Creates empty material without allocating an execution identity.
29    pub fn new() -> Self {
30        Self::default()
31    }
32
33    /// Imports blocks, validating local identity uniqueness and returning all
34    /// submitted material on failure.
35    pub fn from_blocks(blocks: Vec<ContextBlock>) -> Result<Self, EditFailure> {
36        let mut context = Self::new();
37        context.apply(Vec::new(), blocks)?;
38        Ok(context)
39    }
40
41    /// Borrows current material in its committed order.
42    pub fn blocks(&self) -> &[ContextBlock] {
43        &self.blocks
44    }
45
46    /// Transfers ownership of the current blocks without copying them.
47    pub fn into_blocks(self) -> Vec<ContextBlock> {
48        self.blocks
49    }
50
51    /// Copies current material into an independent request frame.
52    pub fn frame(&self) -> ContextFrame {
53        ContextFrame {
54            blocks: self.blocks.clone(),
55        }
56    }
57}