prompty 2.0.0-alpha.11

Prompty is an asset class and format for LLM prompts
Documentation
// Code generated by AgentSchema 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::super::model::token_usage::TokenUsage;

use super::trace_time::TraceTime;

/// A single trace span capturing one pipeline stage or function invocation. Spans nest via the `__frames` field to form a tree representing the full execution (§3.6.1).
#[derive(Debug, Clone, Default)]
pub struct TraceSpan {
    /// The name of this span (typically the function signature)
    pub name: String,
    /// Timing information for this span
    pub __time: TraceTime,
    /// Fully-qualified function signature that produced this span
    pub signature: Option<String>,
    /// Serialized input parameters (redacted per §3.4)
    pub inputs: serde_json::Value,
    /// Serialized return value or error information (redacted per §3.4)
    pub output: Option<serde_json::Value>,
    /// Error message if the span ended with an exception
    pub error: Option<String>,
    /// Aggregated token usage hoisted from child spans (§3.5)
    pub __usage: Option<TokenUsage>,
    /// Additional span attributes (e.g., OpenTelemetry GenAI attributes)
    pub attributes: serde_json::Value,
    /// Nested child spans forming the execution tree (recursive; each element is a TraceSpan)
    pub __frames: Option<Vec<serde_json::Value>>,
}

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

    /// Load TraceSpan 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 TraceSpan 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 TraceSpan 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 {
            name: value.get("name").and_then(|v| v.as_str()).unwrap_or_default().to_string(),
            __time: value.get("__time").filter(|v| v.is_object() || v.is_array() || v.is_string()).map(|v| TraceTime::load_from_value(v, ctx)).unwrap_or_default(),
            signature: value.get("signature").and_then(|v| v.as_str()).map(|s| s.to_string()),
            inputs: value.get("inputs").cloned().unwrap_or(serde_json::Value::Null),
            output: value.get("output").cloned(),
            error: value.get("error").and_then(|v| v.as_str()).map(|s| s.to_string()),
            __usage: value.get("__usage").filter(|v| v.is_object() || v.is_array() || v.is_string()).map(|v| TokenUsage::load_from_value(v, ctx)),
            attributes: value.get("attributes").cloned().unwrap_or(serde_json::Value::Null),
            __frames: value.get("__frames").and_then(|v| v.as_array()).map(|arr| arr.to_vec()),
        }
    }

    /// Serialize TraceSpan 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 !self.name.is_empty() {
            result.insert("name".to_string(), serde_json::Value::String(self.name.clone()));
        }
        {
            let nested = self.__time.to_value(ctx);
            if !nested.is_null() {
                result.insert("__time".to_string(), nested);
            }
        }
        if let Some(ref val) = self.signature {
            result.insert("signature".to_string(), serde_json::Value::String(val.clone()));
        }
        if !self.inputs.is_null() {
            result.insert("inputs".to_string(), self.inputs.clone());
        }
        if let Some(ref val) = self.output {
            result.insert("output".to_string(), val.clone());
        }
        if let Some(ref val) = self.error {
            result.insert("error".to_string(), serde_json::Value::String(val.clone()));
        }
        if let Some(ref val) = self.__usage {
            let nested = val.to_value(ctx);
            if !nested.is_null() {
                result.insert("__usage".to_string(), nested);
            }
        }
        if !self.attributes.is_null() {
            result.insert("attributes".to_string(), self.attributes.clone());
        }
        if let Some(ref items) = self.__frames {
            result.insert("__frames".to_string(), serde_json::to_value(items).unwrap_or(serde_json::Value::Null));
        }
        ctx.process_dict(serde_json::Value::Object(result))
    }

    /// Serialize TraceSpan 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 TraceSpan 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_inputs_dict(&self) -> Option<&serde_json::Map<String, serde_json::Value>> {
        self.inputs.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_attributes_dict(&self) -> Option<&serde_json::Map<String, serde_json::Value>> {
        self.attributes.as_object()
    }

}