agent-context 0.1.0

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

/// LLM 对话消息角色,对应 OpenAI/DeepSeek/智谱等 API 的标准消息角色。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Role {
    /// 系统提示词
    System,
    /// 用户消息
    User,
    /// 模型回复
    Assistant,
    /// 工具调用结果
    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",
        }
    }
}

impl fmt::Display for Role {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

#[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);
    }
}