ForcePilot/backend/package/yuxi/channels/mixins.py
Kris ede29b1809 refactor(channel): 完成频道模块大重构与功能扩展
本次提交对频道模块进行了全面重构并新增多项核心功能:
1.  优化适配器状态获取逻辑,修复状态返回空值问题
2.  新增4种频道异常类型,完善错误处理体系
3.  大幅精简Mixin类,移除冗余的抽象方法定义
4.  重构适配器注册系统,统一注册入口并新增内置适配器加载方法
5.  扩展插件系统,新增更多元数据配置项支持
6.  新增线程类型、会话范围等模型定义,扩展事件类型枚举
7.  优化用户映射逻辑,使用PostgreSQL upsert避免重复创建
8.  新增历史消息注入模块,支持多格式历史格式化与缓存管理
9.  新增线程能力配置与各平台预置适配配置
10. 新增线程绑定管理器,支持多类型线程绑定生命周期管理
11. 重构__init__.py,整理导出模块与类型
12. 扩展基础适配器类,新增凭证解析、状态存储等核心方法
13. 重写消息路由器,支持按频道加载策略、安全校验与多命令处理
14. 新增/history、/context、/summary等交互命令实现
15. 优化消息记录与统计逻辑,完善路由调度链路
2026-05-13 16:41:11 +08:00

86 lines
2.6 KiB
Python

from __future__ import annotations
from typing import Any
from yuxi.channels.models import ChannelResponse, DeliveryResult
class OutboundMixin:
"""出站消息能力 Mixin — 提供 send_text/send_media_url 等默认实现"""
async def send_text(self, chat_id: str, content: str) -> DeliveryResult:
identity = self._build_simple_identity(chat_id)
response = ChannelResponse(identity=identity, content=content)
return await self.send(response)
async def send_media_url(self, chat_id: str, media_url: str, media_type: str = "image") -> DeliveryResult:
identity = self._build_simple_identity(chat_id)
response = ChannelResponse(identity=identity, content=media_url, message_type=media_type) # type: ignore[arg-type]
return await self.send(response)
def _build_simple_identity(self, chat_id: str) -> Any:
from yuxi.channels.models import ChannelIdentity
return ChannelIdentity(
channel_id=getattr(self, "channel_id", ""),
channel_type=getattr(self, "channel_type", None),
channel_user_id="",
channel_chat_id=chat_id,
)
def sanitize_text(self, text: str) -> str:
return text
def chunker(self, text: str, limit: int) -> list[str]:
if len(text) <= limit:
return [text]
chunks = []
for i in range(0, len(text), limit):
chunks.append(text[i : i + limit])
return chunks
class ReactionMixin:
"""表情回应能力 Mixin"""
async def remove_reaction(self, chat_id: str, msg_id: str, emoji: str) -> DeliveryResult:
raise NotImplementedError
class PinMixin:
"""消息置顶/取消置顶能力 Mixin"""
async def pin_message(self, chat_id: str, msg_id: str) -> DeliveryResult:
raise NotImplementedError
async def unpin_message(self, chat_id: str, msg_id: str) -> DeliveryResult:
raise NotImplementedError
async def list_pins(self, chat_id: str) -> list[dict]:
raise NotImplementedError
class UserInfoMixin:
"""用户信息获取能力 Mixin"""
async def get_user_info(self, channel_user_id: str) -> dict[str, Any]:
raise NotImplementedError
async def download_media(self, file_id: str) -> bytes:
raise NotImplementedError
class PollMixin:
"""投票能力 Mixin"""
async def create_poll(self, chat_id: str, question: str, options: list[str]) -> DeliveryResult:
raise NotImplementedError
class StreamingMixin:
"""流式输出能力 Mixin"""
@property
def block_streaming(self) -> bool:
return False