prompty 2.0.0-beta.3

Prompty is an asset class and format for LLM prompts
Documentation
// <auto-generated by typra-emitter>
// Code generated by Typra emitter; DO NOT EDIT.

#![allow(
    unused_imports,
    dead_code,
    non_camel_case_types,
    unused_variables,
    clippy::all
)]

use super::super::context::{LoadContext, SaveContext};

use super::redaction_metadata::RedactionMetadata;

/// A persisted handoff point for a harness session.
#[derive(Debug, Clone, Default)]
pub struct Checkpoint {
    /// Stable checkpoint identifier
    pub id: Option<String>,
    /// Stable session identifier
    pub session_id: Option<String>,
    /// Associated turn identifier, when the checkpoint was created inside a turn
    pub turn_id: Option<String>,
    /// Monotonic checkpoint number within the session
    pub checkpoint_number: Option<i32>,
    /// Short checkpoint title
    pub title: String,
    /// Short human-readable overview
    pub overview: Option<String>,
    /// Portable checkpoint state needed to resume or hand off the session
    pub state: serde_json::Value,
    /// Optional host-authored summary or handoff note
    pub summary: Option<String>,
    /// Host-defined checkpoint metadata
    pub metadata: serde_json::Value,
    /// ISO 8601 UTC timestamp when the checkpoint was created
    pub created_at: Option<String>,
    /// Redaction state for sensitive checkpoint fields
    pub redaction: Option<RedactionMetadata>,
}

impl Checkpoint {
    /// Create a new Checkpoint with default values.
    pub fn new() -> Self {
        Self::default()
    }

    /// Load Checkpoint from a JSON string.
    pub fn from_json(json: &str, ctx: &LoadContext) -> Result<Self, serde_json::Error> {
        let value: serde_json::Value = serde_json::from_str(json)?;
        Ok(Self::load_from_value(&value, ctx))
    }

    /// Load Checkpoint from a YAML string.
    pub fn from_yaml(yaml: &str, ctx: &LoadContext) -> Result<Self, serde_yaml::Error> {
        let value: serde_json::Value = serde_yaml::from_str(yaml)?;
        Ok(Self::load_from_value(&value, ctx))
    }

    /// Load Checkpoint from a `serde_json::Value`.
    ///
    /// Calls `ctx.process_input` before field extraction.
    pub fn load_from_value(value: &serde_json::Value, ctx: &LoadContext) -> Self {
        let value = ctx.process_input(value.clone());
        Self {
            id: value
                .get("id")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            session_id: value
                .get("sessionId")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            turn_id: value
                .get("turnId")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            checkpoint_number: value
                .get("checkpointNumber")
                .and_then(|v| v.as_i64())
                .map(|v| v as i32),
            title: value
                .get("title")
                .and_then(|v| v.as_str())
                .unwrap_or_default()
                .to_string(),
            overview: value
                .get("overview")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            state: value
                .get("state")
                .cloned()
                .unwrap_or(serde_json::Value::Null),
            summary: value
                .get("summary")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            metadata: value
                .get("metadata")
                .cloned()
                .unwrap_or(serde_json::Value::Null),
            created_at: value
                .get("createdAt")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            redaction: value
                .get("redaction")
                .filter(|v| v.is_object() || v.is_array() || v.is_string())
                .map(|v| RedactionMetadata::load_from_value(v, ctx)),
        }
    }

    /// Serialize Checkpoint to a `serde_json::Value`.
    ///
    /// Calls `ctx.process_dict` after serialization.
    pub fn to_value(&self, ctx: &SaveContext) -> serde_json::Value {
        let mut result = serde_json::Map::new();
        // Write base fields
        if let Some(ref val) = self.id {
            result.insert("id".to_string(), serde_json::Value::String(val.clone()));
        }
        if let Some(ref val) = self.session_id {
            result.insert(
                "sessionId".to_string(),
                serde_json::Value::String(val.clone()),
            );
        }
        if let Some(ref val) = self.turn_id {
            result.insert("turnId".to_string(), serde_json::Value::String(val.clone()));
        }
        if let Some(val) = self.checkpoint_number {
            result.insert(
                "checkpointNumber".to_string(),
                serde_json::Value::Number(serde_json::Number::from(val)),
            );
        }
        if !self.title.is_empty() {
            result.insert(
                "title".to_string(),
                serde_json::Value::String(self.title.clone()),
            );
        }
        if let Some(ref val) = self.overview {
            result.insert(
                "overview".to_string(),
                serde_json::Value::String(val.clone()),
            );
        }
        if !self.state.is_null() {
            result.insert("state".to_string(), self.state.clone());
        }
        if let Some(ref val) = self.summary {
            result.insert(
                "summary".to_string(),
                serde_json::Value::String(val.clone()),
            );
        }
        if !self.metadata.is_null() {
            result.insert("metadata".to_string(), self.metadata.clone());
        }
        if let Some(ref val) = self.created_at {
            result.insert(
                "createdAt".to_string(),
                serde_json::Value::String(val.clone()),
            );
        }
        if let Some(ref val) = self.redaction {
            let nested = val.to_value(ctx);
            if !nested.is_null() {
                result.insert("redaction".to_string(), nested);
            }
        }
        ctx.process_dict(serde_json::Value::Object(result))
    }

    /// Serialize Checkpoint to a JSON string.
    pub fn to_json(&self, ctx: &SaveContext) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(&self.to_value(ctx))
    }

    /// Serialize Checkpoint to a YAML string.
    pub fn to_yaml(&self, ctx: &SaveContext) -> Result<String, serde_yaml::Error> {
        serde_yaml::to_string(&self.to_value(ctx))
    }
    /// Returns typed reference to the map if the field is an object.
    /// Returns `None` if the field is null or not an object.
    pub fn as_state_dict(&self) -> Option<&serde_json::Map<String, serde_json::Value>> {
        self.state.as_object()
    }

    /// Returns typed reference to the map if the field is an object.
    /// Returns `None` if the field is null or not an object.
    pub fn as_metadata_dict(&self) -> Option<&serde_json::Map<String, serde_json::Value>> {
        self.metadata.as_object()
    }
}