新增BlueBubbles适配器全套核心工具类与服务,包含会话管理、消息去重、Webhook验证、缓存系统、账号配置解析、聊天消息处理等完整功能模块,支持iMessage消息收发、群管理、反应特效、语音合成等能力,提供完善的健康检查与配置校验流程。
48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
import time
|
|
from collections import defaultdict
|
|
from typing import Any
|
|
|
|
from yuxi.utils.logging_config import logger
|
|
|
|
|
|
class SecretContract:
|
|
_MAX_AUDIT_PER_KEY = 128
|
|
|
|
def __init__(self) -> None:
|
|
self._secrets: dict[str, str] = {}
|
|
self._audit_log: dict[str, list[dict[str, Any]]] = defaultdict(list)
|
|
|
|
def register(self, label: str, description: str) -> None:
|
|
self._secrets[label] = description
|
|
logger.info("[BlueBubbles] Secret registered: %s (%s)", label, description)
|
|
|
|
def audit(self, label: str, action: str, context: dict[str, Any] | None = None) -> None:
|
|
if label not in self._secrets:
|
|
logger.warning("[BlueBubbles] Secret audit for unregistered key: %s", label)
|
|
return
|
|
|
|
entry = {
|
|
"label": label,
|
|
"action": action,
|
|
"timestamp": time.time(),
|
|
"context": context or {},
|
|
}
|
|
log_entries = self._audit_log[label]
|
|
log_entries.append(entry)
|
|
if len(log_entries) > self._MAX_AUDIT_PER_KEY:
|
|
self._audit_log[label] = log_entries[-self._MAX_AUDIT_PER_KEY :]
|
|
|
|
logger.debug("[BlueBubbles] Secret audit: %s -> %s", label, action)
|
|
|
|
def list_secrets(self) -> list[dict[str, str]]:
|
|
return [{"label": k, "description": v} for k, v in self._secrets.items()]
|
|
|
|
def get_audit_log(self, label: str) -> list[dict[str, Any]]:
|
|
return list(self._audit_log.get(label, []))
|
|
|
|
def clear(self) -> None:
|
|
self._secrets.clear()
|
|
self._audit_log.clear()
|