Files
MoviePilot/app/foundation/identity.py
DDSRem 2de976f6b3 Merge pull request #7050 from DDSRem-Dev/codex-comment-migration
docs: 统一全项目中文注释和结构化契约说明
2026-10-17 19:26:29 +08:00

48 lines
1.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
提供内部用户标识判断与用户记忆隔离键生成。
"""
import hashlib
from typing import Optional, Union
# 后台任务会话使用的内部占位用户ID。
# 它只用于在 agent/memory/session 侧标识“系统触发的任务”,
# 不能直接作为真实消息接收人下发到 Telegram/企业微信 等通知渠道。
SYSTEM_INTERNAL_USER_ID = "system"
def is_internal_user_id(userid: Optional[Union[str, int]]) -> bool:
"""
判断是否为系统内部占位用户ID。
"""
return (
isinstance(userid, str)
and userid.strip().lower() == SYSTEM_INTERNAL_USER_ID
)
def normalize_internal_user_id(
userid: Optional[Union[str, int]]
) -> Optional[Union[str, int]]:
"""
将系统内部占位用户ID归一化为 None,避免被通知渠道误认为真实接收人。
"""
if is_internal_user_id(userid):
return None
return userid
def build_user_memory_key(userid: Optional[Union[str, int]]) -> Optional[str]:
"""
为用户记忆目录生成不可逆且稳定的隔离键。
用户 ID 可能来自消息渠道,不能直接拼接进文件路径;系统内部用户不拥有
用户级记忆,返回 None 让调用方只使用公共记忆。
"""
if userid is None or is_internal_user_id(userid):
return None
normalized = str(userid).strip()
if not normalized:
return None
return hashlib.sha256(normalized.encode("utf-8")).hexdigest()