ForcePilot/backend/package/yuxi/channel/extensions/tencent_sms/dedupe.py
Kris 944c9cdbb6 feat(channel): 添加腾讯短信渠道扩展
新增腾讯短信(Tencent SMS)渠道扩展,支持在 Yuxi 平台中集成腾讯云短信渠道。

包含以下功能模块:
- client: 腾讯云短信 API 客户端封装
- plugin: 渠道插件核心
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- webhook: Webhook 事件处理
- outbound: 外发消息管理
- streaming: 流式消息处理
- security: 安全校验
- dedupe: 消息去重
- delivery: 送达状态回调
- compliance: 合规管理
- frequency: 频率控制
- templates: 短信模板
- agent_prompt: Agent 提示词
- types: 类型定义
2026-05-21 11:50:32 +08:00

53 lines
1.5 KiB
Python

from __future__ import annotations
import time
from collections import OrderedDict
class TencentSmsDeduplicator:
def __init__(self, max_size: int = 10000, ttl_seconds: int = 600):
self._cache: OrderedDict[str, float] = OrderedDict()
self._max_size = max_size
self._ttl_seconds = ttl_seconds
def _evict_expired(self, now: float) -> None:
expired = [k for k, ts in self._cache.items() if now - ts > self._ttl_seconds]
for k in expired:
del self._cache[k]
def _evict_oldest(self) -> None:
while len(self._cache) > self._max_size:
self._cache.popitem(last=False)
def is_duplicate(self, dedupe_key: str) -> bool:
now = time.monotonic()
self._evict_expired(now)
if dedupe_key in self._cache:
return True
self._cache[dedupe_key] = now
self._evict_oldest()
return False
def check(self, dedupe_key: str) -> bool:
now = time.monotonic()
self._evict_expired(now)
return dedupe_key in self._cache
def mark_seen(self, dedupe_key: str) -> None:
now = time.monotonic()
self._evict_expired(now)
if dedupe_key not in self._cache:
self._cache[dedupe_key] = now
self._evict_oldest()
@property
def ttl_seconds(self) -> int:
return self._ttl_seconds
@property
def max_entries(self) -> int:
return self._max_size
def reset(self) -> None:
self._cache.clear()