新增飞书机器人适配器全套功能,包括: - 基础适配器入口与工具导出 - 消息格式化、卡片渲染、回复调度逻辑 - 会话ID生成、模型覆盖策略 - 消息发送缓存、顺序队列管理 - 飞书签名验证、加解密webhook请求 - 审批权限校验、机器人菜单事件处理 - 文档评论、钉消息、语音转码处理 - 静态/动态目录管理、子代理生命周期管理 - 各类工具集:聊天、云盘、文档、知识库API封装
45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
import time
|
|
from collections import OrderedDict
|
|
|
|
|
|
class ChatNameCache:
|
|
DEFAULT_TTL_S = 1800
|
|
DEFAULT_MAX_ENTRIES = 500
|
|
|
|
def __init__(self, ttl_s: int = DEFAULT_TTL_S, max_entries: int = DEFAULT_MAX_ENTRIES):
|
|
self._ttl_s = ttl_s
|
|
self._max_entries = max_entries
|
|
self._cache: OrderedDict[str, tuple[str, float]] = OrderedDict()
|
|
|
|
@staticmethod
|
|
def _make_key(account_id: str, chat_id: str) -> str:
|
|
return f"{account_id}:{chat_id}"
|
|
|
|
def get(self, account_id: str, chat_id: str) -> str | None:
|
|
key = self._make_key(account_id, chat_id)
|
|
entry = self._cache.get(key)
|
|
if entry is None:
|
|
return None
|
|
name, ts = entry
|
|
if time.monotonic() - ts > self._ttl_s:
|
|
self._cache.pop(key, None)
|
|
return None
|
|
self._cache.move_to_end(key)
|
|
return name
|
|
|
|
def set(self, account_id: str, chat_id: str, name: str) -> None:
|
|
key = self._make_key(account_id, chat_id)
|
|
self._cache[key] = (name, time.monotonic())
|
|
self._cache.move_to_end(key)
|
|
if len(self._cache) > self._max_entries:
|
|
self._cache.popitem(last=False)
|
|
|
|
def invalidate(self, account_id: str, chat_id: str) -> None:
|
|
key = self._make_key(account_id, chat_id)
|
|
self._cache.pop(key, None)
|
|
|
|
def clear(self) -> None:
|
|
self._cache.clear()
|