新增 Mattermost 渠道扩展,支持在 Yuxi 平台中集成 Mattermost 团队协作平台。 包含以下功能模块: - client: Mattermost API 客户端封装 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - websocket: WebSocket 实时连接 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - dedup: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - session: 会话管理 - interactions: 交互处理 - slash_commands: 斜杠指令 - actions: 动作处理 - approval: 审批流程 - delivery: 消息送达确认 - directory: 目录管理 - threading: 线程管理 - gating: 门控管理 - reconnect: 重连机制 - reactions: 表情反应 - media: 媒体资源处理 - model_picker: 模型选择 - types: 类型定义
43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
from __future__ import annotations
|
|
|
|
import time
|
|
from collections import OrderedDict
|
|
from typing import Literal
|
|
|
|
|
|
class ClaimableDedupe:
|
|
def __init__(self, ttl_ms: int = 300_000, max_size: int = 2000):
|
|
self.ttl_ms = ttl_ms
|
|
self.max_size = max_size
|
|
self._store: OrderedDict[str, float] = OrderedDict()
|
|
|
|
def claim(self, key: str) -> Literal["claimed", "duplicate"]:
|
|
self._evict_expired()
|
|
if key in self._store:
|
|
return "duplicate"
|
|
self._store[key] = time.monotonic() * 1000
|
|
while len(self._store) > self.max_size:
|
|
self._store.popitem(last=False)
|
|
return "claimed"
|
|
|
|
def commit(self, key: str) -> None:
|
|
pass
|
|
|
|
def release(self, key: str) -> None:
|
|
self._store.pop(key, None)
|
|
|
|
def _evict_expired(self) -> None:
|
|
now = time.monotonic() * 1000
|
|
expired = [k for k, v in self._store.items() if now - v > self.ttl_ms]
|
|
for k in expired:
|
|
self._store.pop(k, None)
|
|
|
|
def is_duplicate(self, key: str) -> bool:
|
|
return self.claim(key) == "duplicate"
|
|
|
|
def reset(self) -> None:
|
|
self._store.clear()
|
|
|
|
def __len__(self) -> int:
|
|
return len(self._store)
|