71 lines
2.5 KiB
Python
71 lines
2.5 KiB
Python
from typing import Dict, Any, List, Tuple
|
|
from utils.logger import session_logger
|
|
|
|
class RoleValidator:
|
|
"""角色配置验证服务,确保角色定义满足要求"""
|
|
@staticmethod
|
|
def validate_role_config(
|
|
role_key: str, config: Dict[str, Any]
|
|
) -> Tuple[bool, List[str]]:
|
|
"""
|
|
验证角色配置
|
|
Returns:
|
|
bool: 是否验证通过
|
|
list: 错误消息列表
|
|
"""
|
|
errors = []
|
|
|
|
bool_fields = ["competitive_llm_mode"]
|
|
for field in bool_fields:
|
|
if field in config and not isinstance(config[field], bool):
|
|
errors.append(f"{field} 字段必须是布尔值")
|
|
|
|
if "volcano_model_id" not in config:
|
|
pass
|
|
if "minimax_voice_id" not in config:
|
|
pass
|
|
|
|
if errors:
|
|
session_logger.warning(
|
|
"system", "role_validator", f"角色 {role_key} 验证失败: {', '.join(errors)}"
|
|
)
|
|
return False, errors
|
|
return True, []
|
|
|
|
@staticmethod
|
|
def validate_role_create(role_data: Dict[str, Any]) -> Tuple[bool, List[str]]:
|
|
"""
|
|
验证创建角色的数据
|
|
"""
|
|
role_key = role_data.get("role_key", "")
|
|
config = {
|
|
"name": role_data.get("name"),
|
|
"content": role_data.get("content"),
|
|
"description": role_data.get("description"),
|
|
"default_language": role_data.get("default_language"),
|
|
"competitive_llm_mode": role_data.get("competitive_llm_mode"),
|
|
"volcano_model_id": role_data.get("volcano_model_id"),
|
|
"minimax_voice_id": role_data.get("minimax_voice_id"),
|
|
"url": role_data.get("url"),
|
|
"homophones": role_data.get("homophones"),
|
|
}
|
|
|
|
languages = role_data.get("languages", [])
|
|
if languages:
|
|
multilingual = {}
|
|
for lang in languages:
|
|
lang_code = lang.get("language_code")
|
|
multilingual[lang_code] = {
|
|
"name": lang.get("name"),
|
|
"content": lang.get("content"),
|
|
"minimax_voice_id": lang.get("minimax_voice_id"),
|
|
"url": lang.get("url")
|
|
}
|
|
if multilingual:
|
|
config["multilingual"] = multilingual
|
|
|
|
config = {k: v for k, v in config.items() if v is not None}
|
|
return RoleValidator.validate_role_config(role_key, config)
|
|
|
|
role_validator = RoleValidator()
|