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

67 lines
2.2 KiB
Python

from __future__ import annotations
import logging
from yuxi.channel.extensions.feishu.types import FeishuGroupSessionScope
logger = logging.getLogger(__name__)
class FeishuSessionAdapter:
def resolve_session(self, msg: object):
from yuxi.channel.message.models import PeerKind
from yuxi.channel.protocols import SessionResolution
if hasattr(msg, "sender") and hasattr(msg.sender, "kind"):
if msg.sender.kind == PeerKind.DIRECT:
return SessionResolution(
kind="direct",
conversation_id=getattr(msg.sender, "id", "unknown"),
)
group = getattr(msg, "group", None)
if group:
chat_id = getattr(group, "id", "unknown")
return SessionResolution(
kind="group",
conversation_id=chat_id,
)
return SessionResolution(kind="group", conversation_id="unknown")
def build_session_key(
self,
chat_id: str,
*,
sender_id: str | None = None,
thread_id: str | None = None,
scope: str = "group",
) -> str:
if scope == FeishuGroupSessionScope.GROUP_SENDER and sender_id:
return f"{chat_id}:sender:{sender_id}"
elif scope == FeishuGroupSessionScope.GROUP_TOPIC and thread_id:
return f"{chat_id}:topic:{thread_id}"
elif scope == FeishuGroupSessionScope.GROUP_TOPIC_SENDER and thread_id and sender_id:
return f"{chat_id}:topic:{thread_id}:sender:{sender_id}"
else:
return chat_id
def extract_thread_id(self, msg: object) -> str | None:
if hasattr(msg, "thread_id"):
return msg.thread_id
if hasattr(msg, "raw_payload"):
raw = msg.raw_payload
if isinstance(raw, dict):
return raw.get("thread_id")
return None
def resolve_reply_transport(self, msg: object, thread_id: str | None):
from yuxi.channel.protocols import ReplyTransport
transport = ReplyTransport()
if thread_id:
transport.thread_id = thread_id
if hasattr(msg, "reply_to_id"):
transport.reply_to_id = msg.reply_to_id
return transport