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 compact, replay-oriented record of one harness-side action or observation.
#[derive(Debug, Clone, Default)]
pub struct TrajectoryEvent {
    /// Stable trajectory event identifier
    pub id: Option<String>,
    /// Stable session identifier
    pub session_id: Option<String>,
    /// Associated turn identifier, when available
    pub turn_id: Option<String>,
    /// Associated tool call identifier, when available
    pub tool_call_id: Option<String>,
    /// Zero-based turn index in the session
    pub turn_index: Option<i32>,
    /// Host-defined trajectory event category
    pub event_type: String,
    /// Sanitized event data
    pub data: serde_json::Value,
    /// ISO 8601 UTC timestamp when the trajectory event was recorded
    pub created_at: Option<String>,
    /// Redaction state for sensitive trajectory fields
    pub redaction: Option<RedactionMetadata>,
}

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

    /// Load TrajectoryEvent 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 TrajectoryEvent 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 TrajectoryEvent 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()),
            tool_call_id: value
                .get("toolCallId")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            turn_index: value
                .get("turnIndex")
                .and_then(|v| v.as_i64())
                .map(|v| v as i32),
            event_type: value
                .get("eventType")
                .and_then(|v| v.as_str())
                .unwrap_or_default()
                .to_string(),
            data: value
                .get("data")
                .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 TrajectoryEvent 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(ref val) = self.tool_call_id {
            result.insert(
                "toolCallId".to_string(),
                serde_json::Value::String(val.clone()),
            );
        }
        if let Some(val) = self.turn_index {
            result.insert(
                "turnIndex".to_string(),
                serde_json::Value::Number(serde_json::Number::from(val)),
            );
        }
        if !self.event_type.is_empty() {
            result.insert(
                "eventType".to_string(),
                serde_json::Value::String(self.event_type.clone()),
            );
        }
        if !self.data.is_null() {
            result.insert("data".to_string(), self.data.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 TrajectoryEvent 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 TrajectoryEvent 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_data_dict(&self) -> Option<&serde_json::Map<String, serde_json::Value>> {
        self.data.as_object()
    }
}