ForcePilot/backend/package/yuxi/channel/extensions/feishu/sequential.py
Kris 5e91bb9985 feat(feishu): 新增飞书渠道完整插件实现
新增飞书渠道插件,包含基础配置、事件处理、消息收发、工具调用、权限管理等完整功能模块,支持WebSocket长连接、卡片消息、流式响应、群管理、审批通知等能力
2026-05-21 10:46:42 +08:00

50 lines
1.3 KiB
Python

from __future__ import annotations
import asyncio
import logging
from collections import defaultdict
logger = logging.getLogger(__name__)
class SequentialQueue:
def __init__(self):
self._queues: dict[str, asyncio.Queue] = defaultdict(asyncio.Queue)
self._processing: dict[str, bool] = defaultdict(lambda: False)
self._lock = asyncio.Lock()
async def process(self, key: str, handler, item) -> None:
queue = self._queues[key]
await queue.put(item)
async with self._lock:
if self._processing[key]:
return
self._processing[key] = True
try:
while True:
entry = await queue.get()
try:
await handler(entry)
except Exception:
logger.exception("Error processing sequential item for key %s", key)
if queue.empty():
break
finally:
async with self._lock:
self._processing[key] = False
def get_sequential_key(msg: dict, account_id: str) -> str:
chat_id = ""
group = msg.get("group")
if group:
chat_id = group.get("id", "")
else:
sender = msg.get("sender", {})
chat_id = sender.get("id", "unknown")
return f"{account_id}:{chat_id}"