prompty 2.0.0-beta.1

Prompty is an asset class and format for LLM prompts
Documentation
// Code generated by Prompty 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::content_part::{ContentPart, ContentPartKind};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Role {
    System,
    User,
    Assistant,
    Developer,
    Tool,
}

impl Default for Role {
    fn default() -> Self {
        Self::System
    }
}

impl std::fmt::Display for Role {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::System => write!(f, "system"),
            Self::User => write!(f, "user"),
            Self::Assistant => write!(f, "assistant"),
            Self::Developer => write!(f, "developer"),
            Self::Tool => write!(f, "tool"),
        }
    }
}

impl Role {
    pub fn from_str_opt(s: &str) -> Option<Self> {
        match s {
            "system" => Some(Self::System),
            "user" => Some(Self::User),
            "assistant" => Some(Self::Assistant),
            "developer" => Some(Self::Developer),
            "tool" => Some(Self::Tool),
            _ => None,
        }
    }

    pub fn as_str(&self) -> &str {
        match self {
            Self::System => "system",
            Self::User => "user",
            Self::Assistant => "assistant",
            Self::Developer => "developer",
            Self::Tool => "tool",
        }
    }
}

/// A message in a conversation. Messages have a role and a list of content parts representing the different modalities of the message content.
#[derive(Debug, Clone, Default)]
pub struct Message {
    /// The role of the message sender
    pub role: Role,
    /// The content parts of the message
    pub parts: Vec<ContentPart>,
    /// Optional metadata associated with the message
    pub metadata: serde_json::Value,
}

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

    /// Load Message 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 Message 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 Message 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 {
            role: value
                .get("role")
                .and_then(|v| v.as_str())
                .and_then(|s| Role::from_str_opt(s))
                .unwrap_or(Role::User),
            parts: value
                .get("parts")
                .map(|v| Self::load_parts(v, ctx))
                .unwrap_or_default(),
            metadata: value
                .get("metadata")
                .cloned()
                .unwrap_or(serde_json::Value::Null),
        }
    }

    /// Serialize Message 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(
            "role".to_string(),
            serde_json::Value::String(self.role.to_string()),
        );
        if !self.parts.is_empty() {
            result.insert("parts".to_string(), Self::save_parts(&self.parts, ctx));
        }
        if !self.metadata.is_null() {
            result.insert("metadata".to_string(), self.metadata.clone());
        }
        ctx.process_dict(serde_json::Value::Object(result))
    }

    /// Serialize Message 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 Message 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_metadata_dict(&self) -> Option<&serde_json::Map<String, serde_json::Value>> {
        self.metadata.as_object()
    }

    /// Load a collection of ContentPart from a JSON value.
    /// Handles both array format `[{...}]`.
    fn load_parts(data: &serde_json::Value, ctx: &LoadContext) -> Vec<ContentPart> {
        match data {
            serde_json::Value::Array(arr) => arr
                .iter()
                .map(|v| ContentPart::load_from_value(v, ctx))
                .collect(),

            _ => Vec::new(),
        }
    }

    /// Save a collection of ContentPart to a JSON value.
    fn save_parts(items: &[ContentPart], ctx: &SaveContext) -> serde_json::Value {
        serde_json::Value::Array(
            items
                .iter()
                .map(|item| item.to_value(ctx))
                .collect::<Vec<_>>(),
        )
    }
    /// Create a Message with preset field values.
    pub fn assistant(text: impl Into<String>) -> Self {
        Message {
            role: Role::Assistant,
            parts: vec![ContentPart {
                kind: ContentPartKind::TextPart { value: text.into() },
                ..Default::default()
            }],
            ..Default::default()
        }
    }
    /// Create a Message with preset field values.
    pub fn system(text: impl Into<String>) -> Self {
        Message {
            role: Role::System,
            parts: vec![ContentPart {
                kind: ContentPartKind::TextPart { value: text.into() },
                ..Default::default()
            }],
            ..Default::default()
        }
    }
    /// Create a Message with preset field values.
    pub fn user(text: impl Into<String>) -> Self {
        Message {
            role: Role::User,
            parts: vec![ContentPart {
                kind: ContentPartKind::TextPart { value: text.into() },
                ..Default::default()
            }],
            ..Default::default()
        }
    }
}
/// Helpers for [`Message`]. Implement in a separate file.
pub trait MessageHelpers {
    /// Return plain string if all parts are text, else a list of content part dicts for wire serialization
    fn to_text_content(&self) -> serde_json::Value;
    /// Concatenate all TextPart values joined by newline
    fn text(&self) -> String;
}