prompty 2.0.0-alpha.9

Prompty is an asset class and format for LLM prompts
Documentation
// Code generated by AgentSchema emitter; DO NOT EDIT.
// Prompty Context

/// Callback type for pre-processing input data before parsing.
pub type PreProcessFn = Box<dyn Fn(serde_json::Value) -> serde_json::Value + Send + Sync>;

/// Callback type for post-processing the result after instantiation.
pub type PostProcessFn = Box<dyn Fn(serde_json::Value) -> serde_json::Value + Send + Sync>;

/// Callback type for pre-processing an object before serialization.
pub type PreSaveFn = Box<dyn Fn(serde_json::Value) -> serde_json::Value + Send + Sync>;

/// Callback type for post-processing a dictionary after serialization.
pub type PostSaveFn = Box<dyn Fn(serde_json::Value) -> serde_json::Value + Send + Sync>;

/// Context for customizing the loading process of agent definitions.
///
/// Provides hooks for pre-processing input data before parsing and
/// post-processing output data after instantiation.
#[derive(Default)]
pub struct LoadContext {
    /// Optional callback to transform input data before parsing.
    pub pre_process: Option<PreProcessFn>,
    /// Optional callback to transform the result after instantiation.
    pub post_process: Option<PostProcessFn>,
}

impl std::fmt::Debug for LoadContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LoadContext")
            .field("pre_process", &self.pre_process.as_ref().map(|_| "..."))
            .field("post_process", &self.post_process.as_ref().map(|_| "..."))
            .finish()
    }
}

impl LoadContext {
    /// Create a new empty LoadContext.
    pub fn new() -> Self {
        Self::default()
    }

    /// Apply pre-processing to input data if a pre_process callback is set.
    ///
    /// # Arguments
    /// * `data` - The raw input value to process.
    ///
    /// # Returns
    /// The processed value, or the original if no callback is set.
    pub fn process_input(&self, data: serde_json::Value) -> serde_json::Value {
        if let Some(ref f) = self.pre_process {
            f(data)
        } else {
            data
        }
    }

    /// Apply post-processing to the result if a post_process callback is set.
    ///
    /// # Arguments
    /// * `result` - The instantiated value to process.
    ///
    /// # Returns
    /// The processed result, or the original if no callback is set.
    pub fn process_output(&self, result: serde_json::Value) -> serde_json::Value {
        if let Some(ref f) = self.post_process {
            f(result)
        } else {
            result
        }
    }
}

/// Context for customizing the serialization process of agent definitions.
///
/// Provides hooks for pre-processing the object before serialization and
/// post-processing the dictionary after serialization.
pub struct SaveContext {
    /// Optional callback to transform the object before serialization.
    pub pre_save: Option<PreSaveFn>,
    /// Optional callback to transform the dictionary after serialization.
    pub post_save: Option<PostSaveFn>,
    /// Output format for collections: "object" (name as key) or "array" (list of dicts).
    /// Defaults to "object".
    pub collection_format: String,
    /// Use shorthand scalar representation when possible.
    /// Defaults to true.
    pub use_shorthand: bool,
}

impl std::fmt::Debug for SaveContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SaveContext")
            .field("pre_save", &self.pre_save.as_ref().map(|_| "..."))
            .field("post_save", &self.post_save.as_ref().map(|_| "..."))
            .field("collection_format", &self.collection_format)
            .field("use_shorthand", &self.use_shorthand)
            .finish()
    }
}

impl Default for SaveContext {
    fn default() -> Self {
        Self {
            pre_save: None,
            post_save: None,
            collection_format: "object".to_string(),
            use_shorthand: true,
        }
    }
}

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

    /// Apply pre-processing to the object if a pre_save callback is set.
    ///
    /// # Arguments
    /// * `obj` - The value to process before serialization.
    ///
    /// # Returns
    /// The processed value, or the original if no callback is set.
    pub fn process_object(&self, obj: serde_json::Value) -> serde_json::Value {
        if let Some(ref f) = self.pre_save {
            f(obj)
        } else {
            obj
        }
    }

    /// Apply post-processing to the dictionary if a post_save callback is set.
    ///
    /// # Arguments
    /// * `data` - The serialized value to process.
    ///
    /// # Returns
    /// The processed value, or the original if no callback is set.
    pub fn process_dict(&self, data: serde_json::Value) -> serde_json::Value {
        if let Some(ref f) = self.post_save {
            f(data)
        } else {
            data
        }
    }

    /// Convert a value to a YAML string.
    pub fn to_yaml(&self, data: &serde_json::Value) -> Result<String, serde_yaml::Error> {
        serde_yaml::to_string(data)
    }

    /// Convert a value to a JSON string.
    pub fn to_json(
        &self,
        data: &serde_json::Value,
        indent: bool,
    ) -> Result<String, serde_json::Error> {
        if indent {
            serde_json::to_string_pretty(data)
        } else {
            serde_json::to_string(data)
        }
    }
}