Files
mai-bot/src/common/utils/utils_session.py
DrSmoothl 0c508995dd feat: enhance session ID calculation and plugin management
- Updated `calculate_session_id` method in `SessionUtils` to include optional `account_id` and `scope` parameters for more granular session ID generation.
- Added new environment variables in `plugin_runtime` for external plugin dependencies and global configuration snapshots.
- Introduced methods in `RuntimeComponentManagerProtocol` for loading and reloading plugins globally, accommodating external dependencies.
- Enhanced `PluginRunnerSupervisor` to manage external available plugin IDs during plugin reloads.
- Implemented dependency extraction and management in `PluginRuntimeManager` to handle cross-supervisor dependencies.
- Added tests for session ID calculation and message registration in `ChatManager` to ensure correct behavior with new parameters.
2026-03-24 12:14:58 +08:00

43 lines
1.3 KiB
Python
Raw 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.
from typing import Optional
import hashlib
class SessionUtils:
@staticmethod
def calculate_session_id(
platform: str,
*,
user_id: Optional[str] = None,
group_id: Optional[str] = None,
account_id: Optional[str] = None,
scope: Optional[str] = None,
) -> str:
"""计算session_id
Args:
platform: 平台名称
user_id: 用户ID如果是私聊
group_id: 群ID如果是群聊
account_id: 当前平台账号 ID可选
scope: 当前路由作用域,可选
Returns:
str: 计算得到的会话ID
Raises:
ValueError: 当 user_id 和 group_id 都未提供时抛出
"""
if not user_id and not group_id:
raise ValueError("UserID 或 GroupID 必须提供其一")
route_components = []
if account_id:
route_components.append(f"account:{account_id}")
if scope:
route_components.append(f"scope:{scope}")
if group_id:
components = [platform, *route_components, group_id]
else:
components = [platform, *route_components, user_id, "private"]
return hashlib.md5("_".join(components).encode()).hexdigest()