agent-context 0.1.3

Multi-backend agent context manager with three-zone memory model
Documentation
use serde::{Deserialize, Serialize};

/// LLM 对话消息角色,对应 OpenAI/DeepSeek/智谱等 API 的标准消息角色。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, derive_more::Display)]
pub enum Role {
    /// 系统提示词
    #[display("system")]
    System,
    /// 用户消息
    #[display("user")]
    User,
    /// 模型回复
    #[display("assistant")]
    Assistant,
    /// 工具调用结果
    #[display("tool")]
    Tool,
}

impl Role {
    /// 返回角色对应的 JSON 字段值(小写字符串)。
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::System => "system",
            Self::User => "user",
            Self::Assistant => "assistant",
            Self::Tool => "tool",
        }
    }
}

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

    #[test]
    fn display() {
        assert_eq!(Role::System.to_string(), "system");
        assert_eq!(Role::User.to_string(), "user");
        assert_eq!(Role::Assistant.to_string(), "assistant");
        assert_eq!(Role::Tool.to_string(), "tool");
    }

    #[test]
    fn as_str_matches_display() {
        assert_eq!(Role::System.as_str(), Role::System.to_string());
    }

    #[test]
    fn serde_roundtrip() {
        let json = serde_json::to_string(&Role::Assistant).unwrap();
        assert_eq!(json, "\"Assistant\"");
        let role: Role = serde_json::from_str(&json).unwrap();
        assert_eq!(role, Role::Assistant);
    }
}