rune-chain-core 0.1.1

Core traits and types for the rune-chain LLM orchestration framework
Documentation
use serde::{Deserialize, Serialize};

/// The originator of a [`Message`] in a conversation turn.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Role {
    /// Instructions that frame the model's behaviour for the whole conversation.
    System,
    /// A turn sent by the end user or caller.
    Human,
    /// A turn generated by the AI model.
    Ai,
    /// A synthetic turn injected by a tool or function-call result.
    Tool,
}

impl std::fmt::Display for Role {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Role::System => write!(f, "system"),
            Role::Human => write!(f, "human"),
            Role::Ai => write!(f, "ai"),
            Role::Tool => write!(f, "tool"),
        }
    }
}

/// A single turn in a conversation, carrying a [`Role`] and text content.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Message {
    /// Who produced this message.
    pub role: Role,
    /// The text of the message.
    pub content: String,
}

impl Message {
    /// Create a new [`Message`] with an explicit role and content.
    ///
    /// # Example
    ///
    /// ```rust
    /// use rune_chain_core::{Message, Role};
    ///
    /// let msg = Message::new(Role::Human, "Hello!");
    /// assert_eq!(msg.role, Role::Human);
    /// assert_eq!(msg.content, "Hello!");
    /// ```
    pub fn new(role: Role, content: impl Into<String>) -> Self {
        Self {
            role,
            content: content.into(),
        }
    }

    /// Shorthand for a [`Role::System`] message.
    ///
    /// # Example
    ///
    /// ```rust
    /// use rune_chain_core::{Message, Role};
    ///
    /// let msg = Message::system("You are a helpful assistant.");
    /// assert_eq!(msg.role, Role::System);
    /// ```
    pub fn system(content: impl Into<String>) -> Self {
        Self::new(Role::System, content)
    }

    /// Shorthand for a [`Role::Human`] message.
    ///
    /// # Example
    ///
    /// ```rust
    /// use rune_chain_core::{Message, Role};
    ///
    /// let msg = Message::human("What is 2 + 2?");
    /// assert_eq!(msg.role, Role::Human);
    /// ```
    pub fn human(content: impl Into<String>) -> Self {
        Self::new(Role::Human, content)
    }

    /// Shorthand for a [`Role::Ai`] message.
    ///
    /// # Example
    ///
    /// ```rust
    /// use rune_chain_core::{Message, Role};
    ///
    /// let msg = Message::ai("The answer is 4.");
    /// assert_eq!(msg.role, Role::Ai);
    /// ```
    pub fn ai(content: impl Into<String>) -> Self {
        Self::new(Role::Ai, content)
    }
}