ForcePilot/backend/package/yuxi/channels/adapters/twitch/outbound_cache.py
Kris 18d1ea2aac refactor(twitch): 重构Twitch适配器,新增Helix API支持与功能优化
本次提交对Twitch适配器进行了全面升级与优化:
1.  修复UTF8截断逻辑,避免越界访问
2.  重构群聊策略配置,标准化mention相关规则
3.  新增消息缓存管理器,支持通过消息ID查询已发送消息
4.  更新配置schema,新增prefer_helix_send开关和deprecated策略自动转换
5.  新增CLEARMSG和ROOMSTATE IRC消息解析,补充事件订阅支持
6.  优化令牌刷新逻辑,增加重试机制与退避策略
7.  新增Helix API聊天消息发送、删除和公告功能
8.  扩展事件订阅类型,新增直播状态、频道更新等系统事件
9.  新增reply、delete_message、announcement等动作支持,完善操作能力
10. 重构流式发送逻辑,新增进度指示器和配置项
11. 优化重连策略,增加指数退避与计数重置
2026-05-13 16:16:02 +08:00

35 lines
1.1 KiB
Python

from __future__ import annotations
import time
from collections import OrderedDict
from typing import Any
class OutboundCacheManager:
def __init__(self, max_size: int = 500):
self._cache: OrderedDict[str, dict[str, Any]] = OrderedDict()
self._max_size = max_size
def record(self, target: str, content: str, message_id: str | None = None) -> str:
entry = {
"channel": target,
"content": content,
"timestamp": time.time(),
}
cache_key = message_id or f"{target}:{len(self._cache)}"
if message_id:
entry["message_id"] = message_id
self._cache[cache_key] = entry
while len(self._cache) > self._max_size:
self._cache.popitem(last=False)
return cache_key
def get_all(self) -> list[dict[str, Any]]:
return list(self._cache.values())
def find_by_message_id(self, message_id: str) -> dict[str, Any] | None:
for entry in self._cache.values():
if entry.get("message_id") == message_id:
return entry
return None