57 lines
1.9 KiB
Python
57 lines
1.9 KiB
Python
from datetime import datetime
|
|
from typing import Optional
|
|
from pydantic import Field
|
|
from beanie import Document
|
|
import arrow
|
|
|
|
|
|
class GLMRole(Document):
|
|
"""GLM角色配置模型"""
|
|
|
|
# 基本信息
|
|
name: str = Field(..., description="角色名称")
|
|
description: str = Field(..., description="角色描述")
|
|
skills: str = Field(default="", description="技能列表(多行文本字符串)")
|
|
restrictions: str = Field(default="", description="限制列表(多行文本字符串)")
|
|
|
|
# 状态管理
|
|
is_active: bool = Field(default=True, description="是否激活")
|
|
is_default: bool = Field(default=False, description="是否为默认角色")
|
|
|
|
# 时间戳
|
|
created_at: datetime = Field(default_factory=lambda: arrow.now().datetime)
|
|
updated_at: datetime = Field(default_factory=lambda: arrow.now().datetime)
|
|
|
|
class Settings:
|
|
name = "glm_roles" # MongoDB集合名称
|
|
indexes = [
|
|
[("is_active", 1)], # 按激活状态查询
|
|
[("is_default", 1)], # 按默认状态查询
|
|
[("created_at", -1)], # 按创建时间排序
|
|
]
|
|
|
|
def update_timestamp(self):
|
|
"""更新更新时间戳"""
|
|
self.updated_at = arrow.now().datetime
|
|
|
|
@classmethod
|
|
def from_role_config(cls, role_config: dict) -> "GLMRole":
|
|
"""从角色配置字典创建GLMRole实例"""
|
|
return cls(
|
|
name=role_config.get("name", ""),
|
|
description=role_config.get("description", ""),
|
|
skills=role_config.get("skills", ""),
|
|
restrictions=role_config.get("restrictions", ""),
|
|
is_active=True,
|
|
is_default=True,
|
|
)
|
|
|
|
def to_role_config_dict(self) -> dict:
|
|
"""转换为角色配置字典"""
|
|
return {
|
|
"name": self.name,
|
|
"description": self.description,
|
|
"skills": self.skills,
|
|
"restrictions": self.restrictions,
|
|
}
|