新增Slack适配器全套核心模块,包括消息处理流水线、会话管理、配置适配、权限控制等完整功能: 1. 新增语音、视觉相关的TTS和图像分析导出接口 2. 实现消息预处理、路由、线程上下文处理的完整流水线 3. 新增账号管理、缓存机制、房间上下文提取功能 4. 支持Webhook和Socket Mode两种事件接收方式 5. 实现权限白名单、审批配对、自动状态管理功能 6. 新增配置迁移、作用域校验、重连策略等辅助模块
49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
import time
|
|
from collections import OrderedDict
|
|
|
|
DEFAULT_CACHE_TTL_S = 3600.0
|
|
DEFAULT_MAX_ENTRIES = 2048
|
|
|
|
|
|
class SentMessageCache:
|
|
def __init__(self, max_entries: int = DEFAULT_MAX_ENTRIES, ttl_s: float = DEFAULT_CACHE_TTL_S):
|
|
self._cache: OrderedDict[str, tuple[str, float]] = OrderedDict()
|
|
self._max_entries = max_entries
|
|
self._ttl = ttl_s
|
|
|
|
def put(self, key: str, thread_ts: str) -> None:
|
|
self._evict_expired()
|
|
if key in self._cache:
|
|
self._cache.move_to_end(key)
|
|
elif len(self._cache) >= self._max_entries:
|
|
self._cache.popitem(last=False)
|
|
self._cache[key] = (thread_ts, time.monotonic())
|
|
|
|
def get(self, key: str) -> str | None:
|
|
self._evict_expired()
|
|
entry = self._cache.get(key)
|
|
if entry is None:
|
|
return None
|
|
ts, stored_at = entry
|
|
if time.monotonic() - stored_at > self._ttl:
|
|
self._cache.pop(key, None)
|
|
return None
|
|
self._cache.move_to_end(key)
|
|
return ts
|
|
|
|
def clear(self) -> None:
|
|
self._cache.clear()
|
|
|
|
def _evict_expired(self) -> None:
|
|
now = time.monotonic()
|
|
expired = [k for k, (_, t) in self._cache.items() if now - t > self._ttl]
|
|
for k in expired:
|
|
self._cache.pop(k, None)
|
|
|
|
@property
|
|
def size(self) -> int:
|
|
self._evict_expired()
|
|
return len(self._cache)
|