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};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ReplayRecordKind {
    Session,
    Turn,
    Summary,
}

impl Default for ReplayRecordKind {
    fn default() -> Self {
        Self::Session
    }
}

impl std::fmt::Display for ReplayRecordKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Session => write!(f, "session"),
            Self::Turn => write!(f, "turn"),
            Self::Summary => write!(f, "summary"),
        }
    }
}

impl ReplayRecordKind {
    pub fn from_str_opt(s: &str) -> Option<Self> {
        match s {
            "session" => Some(Self::Session),
            "turn" => Some(Self::Turn),
            "summary" => Some(Self::Summary),
            _ => None,
        }
    }

    pub fn as_str(&self) -> &str {
        match self {
            Self::Session => "session",
            Self::Turn => "turn",
            Self::Summary => "summary",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ReplayRecordStatus {
    Success,
    Error,
    Cancelled,
}

impl Default for ReplayRecordStatus {
    fn default() -> Self {
        Self::Success
    }
}

impl std::fmt::Display for ReplayRecordStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Success => write!(f, "success"),
            Self::Error => write!(f, "error"),
            Self::Cancelled => write!(f, "cancelled"),
        }
    }
}

impl ReplayRecordStatus {
    pub fn from_str_opt(s: &str) -> Option<Self> {
        match s {
            "success" => Some(Self::Success),
            "error" => Some(Self::Error),
            "cancelled" => Some(Self::Cancelled),
            _ => None,
        }
    }

    pub fn as_str(&self) -> &str {
        match self {
            Self::Success => "success",
            Self::Error => "error",
            Self::Cancelled => "cancelled",
        }
    }
}

/// Stable, replay-comparable projection of a journal record. Runtime journal records may carry additional payload fields, durations, telemetry, or provider-specific data. Replay verification compares this normalized shape so deterministic orchestration semantics are mechanically shared across runtimes.
#[derive(Debug, Clone, Default)]
pub struct ReplayJournalRecord {
    /// Journal record kind
    pub kind: ReplayRecordKind,
    /// Turn or session event type, when kind is not summary
    pub r#type: Option<String>,
    /// Stable harness session identifier
    pub session_id: Option<String>,
    /// Stable turn identifier within the session
    pub turn_id: Option<String>,
    /// Zero-based model loop iteration for turn records
    pub iteration: Option<i32>,
    /// Final semantic status for turn/session/summary records
    pub status: Option<ReplayRecordStatus>,
    /// Permission request identifier for permission request records
    pub request_id: Option<String>,
    /// Host tool name for tool execution/result records
    pub tool_name: Option<String>,
    /// Whether a permission or host tool operation succeeded
    pub success: Option<bool>,
    /// Stable error discriminator for failed records
    pub error_kind: Option<String>,
    /// Number of turns represented by a summary record
    pub turns: Option<i32>,
    /// Number of checkpoints represented by a summary record
    pub checkpoints: Option<i32>,
}

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

    /// Load ReplayJournalRecord 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 ReplayJournalRecord 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 ReplayJournalRecord 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 {
            kind: value
                .get("kind")
                .and_then(|v| v.as_str())
                .and_then(|s| ReplayRecordKind::from_str_opt(s))
                .unwrap_or(ReplayRecordKind::Session),
            r#type: value
                .get("type")
                .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()),
            iteration: value
                .get("iteration")
                .and_then(|v| v.as_i64())
                .map(|v| v as i32),
            status: value
                .get("status")
                .and_then(|v| v.as_str())
                .and_then(|s| ReplayRecordStatus::from_str_opt(s)),
            request_id: value
                .get("requestId")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            tool_name: value
                .get("toolName")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            success: value.get("success").and_then(|v| v.as_bool()),
            error_kind: value
                .get("errorKind")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            turns: value
                .get("turns")
                .and_then(|v| v.as_i64())
                .map(|v| v as i32),
            checkpoints: value
                .get("checkpoints")
                .and_then(|v| v.as_i64())
                .map(|v| v as i32),
        }
    }

    /// Serialize ReplayJournalRecord 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
        result.insert(
            "kind".to_string(),
            serde_json::Value::String(self.kind.to_string()),
        );
        if let Some(ref val) = self.r#type {
            result.insert("type".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.iteration {
            result.insert(
                "iteration".to_string(),
                serde_json::Value::Number(serde_json::Number::from(val)),
            );
        }
        if let Some(ref val) = self.status {
            result.insert(
                "status".to_string(),
                serde_json::Value::String(val.to_string()),
            );
        }
        if let Some(ref val) = self.request_id {
            result.insert(
                "requestId".to_string(),
                serde_json::Value::String(val.clone()),
            );
        }
        if let Some(ref val) = self.tool_name {
            result.insert(
                "toolName".to_string(),
                serde_json::Value::String(val.clone()),
            );
        }
        if let Some(val) = self.success {
            result.insert("success".to_string(), serde_json::Value::Bool(val));
        }
        if let Some(ref val) = self.error_kind {
            result.insert(
                "errorKind".to_string(),
                serde_json::Value::String(val.clone()),
            );
        }
        if let Some(val) = self.turns {
            result.insert(
                "turns".to_string(),
                serde_json::Value::Number(serde_json::Number::from(val)),
            );
        }
        if let Some(val) = self.checkpoints {
            result.insert(
                "checkpoints".to_string(),
                serde_json::Value::Number(serde_json::Number::from(val)),
            );
        }
        ctx.process_dict(serde_json::Value::Object(result))
    }

    /// Serialize ReplayJournalRecord 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 ReplayJournalRecord to a YAML string.
    pub fn to_yaml(&self, ctx: &SaveContext) -> Result<String, serde_yaml::Error> {
        serde_yaml::to_string(&self.to_value(ctx))
    }
}