jules-core 0.1.0

Core abstractions and traits for Jules-SDK
Documentation
//! Message module.

use serde::{Deserialize, Serialize};

/// The role of the entity that generated a message.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Role {
    /// System messages provide instructions and context to the model.
    System,
    /// User messages are the prompts provided by the user.
    #[default]
    User,
    /// Assistant messages are the responses generated by the model.
    Assistant,
    /// Tool messages provide the results of a tool call.
    Tool,
}

/// A single message in a conversation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Message {
    role: Role,
    content: String,
    #[cfg(feature = "tools")]
    #[serde(skip_serializing_if = "Option::is_none")]
    tool_calls: Option<Vec<crate::tool::ToolCall>>,
    #[cfg(feature = "tools")]
    #[serde(skip_serializing_if = "Option::is_none")]
    tool_call_id: Option<String>,
}

impl Message {
    /// Creates a new [`Message`] with the specified role and content.
    #[must_use]
    pub fn new(role: Role, content: impl Into<String>) -> Self {
        Self {
            role,
            content: content.into(),
            #[cfg(feature = "tools")]
            tool_calls: None,
            #[cfg(feature = "tools")]
            tool_call_id: None,
        }
    }

    /// Adds tool calls to this message.
    #[cfg(feature = "tools")]
    #[must_use]
    pub fn with_tool_calls(mut self, tool_calls: Vec<crate::tool::ToolCall>) -> Self {
        self.tool_calls = Some(tool_calls);
        self
    }

    /// Sets the tool call ID for this message.
    #[cfg(feature = "tools")]
    #[must_use]
    pub fn with_tool_call_id(mut self, tool_call_id: impl Into<String>) -> Self {
        self.tool_call_id = Some(tool_call_id.into());
        self
    }

    /// Returns the tool calls associated with this message, if any.
    #[cfg(feature = "tools")]
    #[must_use]
    pub fn tool_calls(&self) -> Option<&[crate::tool::ToolCall]> {
        self.tool_calls.as_deref()
    }

    /// Returns the tool call ID associated with this message, if any.
    #[cfg(feature = "tools")]
    #[must_use]
    pub fn tool_call_id(&self) -> Option<&str> {
        self.tool_call_id.as_deref()
    }

    /// Returns the role of the message.
    #[must_use]
    pub fn role(&self) -> &Role {
        &self.role
    }

    /// Returns the content of the message.
    #[must_use]
    pub fn content(&self) -> &str {
        &self.content
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_message_creation() {
        let msg = Message::new(Role::User, "Hello");
        assert_eq!(*msg.role(), Role::User);
        assert_eq!(msg.content(), "Hello");
    }
}