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

/// Payload for "llm_start" events — an LLM request is about to be sent.
#[derive(Debug, Clone, Default)]
pub struct LlmStartPayload {
    /// Provider identifier used for the request
    pub provider: Option<String>,
    /// Model or deployment identifier used for the request
    pub model_id: Option<String>,
    /// Number of messages sent to the provider
    pub message_count: Option<i32>,
    /// Retry attempt number, zero for the initial attempt
    pub attempt: Option<i32>,
}

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

    /// Load LlmStartPayload 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 LlmStartPayload 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 LlmStartPayload 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 {
            provider: value
                .get("provider")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            model_id: value
                .get("modelId")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            message_count: value
                .get("messageCount")
                .and_then(|v| v.as_i64())
                .map(|v| v as i32),
            attempt: value
                .get("attempt")
                .and_then(|v| v.as_i64())
                .map(|v| v as i32),
        }
    }

    /// Serialize LlmStartPayload 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.provider {
            result.insert(
                "provider".to_string(),
                serde_json::Value::String(val.clone()),
            );
        }
        if let Some(ref val) = self.model_id {
            result.insert(
                "modelId".to_string(),
                serde_json::Value::String(val.clone()),
            );
        }
        if let Some(val) = self.message_count {
            result.insert(
                "messageCount".to_string(),
                serde_json::Value::Number(serde_json::Number::from(val)),
            );
        }
        if let Some(val) = self.attempt {
            result.insert(
                "attempt".to_string(),
                serde_json::Value::Number(serde_json::Number::from(val)),
            );
        }
        ctx.process_dict(serde_json::Value::Object(result))
    }

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