ForcePilot/backend/package/yuxi/channel/extensions/messenger/dedupe.py
Kris 895602f774 feat(channel): 添加 Facebook Messenger 渠道扩展
新增 Facebook Messenger 渠道扩展,支持在 Yuxi 平台中集成 Messenger 即时通讯渠道。

包含以下功能模块:
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- webhook: Webhook 事件处理
- outbound: 外发消息管理
- streaming: 流式消息处理
- pairing: 用户配对与绑定
- security: 安全校验
- dedupe: 消息去重
- monitor: 渠道状态监控
- status: 会话状态管理
- actions: 动作处理
- template: 消息模板
- quick_reply: 快捷回复
- private_reply: 私密回复
- handover: 转人工切换
- persona: 人设管理
- profile: 主页配置
- user: 用户信息
- insights: 数据洞察
- notification: 通知推送
- media: 媒体资源处理
- types: 类型定义
2026-05-21 11:25:47 +08:00

38 lines
989 B
Python

from collections import OrderedDict
import time
import logging
logger = logging.getLogger(__name__)
class MessengerMessageDeduplicator:
def __init__(self, max_size: int = 10000, ttl_seconds: int = 300):
self._cache: OrderedDict[str, float] = OrderedDict()
self._max_size = max_size
self._ttl_seconds = ttl_seconds
def is_duplicate(self, msg_id: str) -> bool:
if not msg_id:
return False
self._evict_expired()
if msg_id in self._cache:
logger.debug(f"messenger dedupe: duplicate {msg_id}")
return True
self._cache[msg_id] = time.monotonic()
if len(self._cache) > self._max_size:
self._cache.popitem(last=False)
return False
def _evict_expired(self):
now = time.monotonic()
cutoff = now - self._ttl_seconds
expired = [k for k, v in self._cache.items() if v < cutoff]
for k in expired:
del self._cache[k]