ForcePilot/backend/package/yuxi/channels/models.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

392 lines
11 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from __future__ import annotations
from datetime import datetime
from enum import StrEnum
from typing import Any, Literal
from pydantic import BaseModel, ConfigDict, Field
from yuxi.utils.datetime_utils import utc_now_naive
class ThreadType(StrEnum):
NATIVE = "native"
TOPIC = "topic"
REPLY_CHAIN = "reply_chain"
SIMULATED = "simulated"
DIRECT = "direct"
GROUP = "group"
CHANNEL = "channel"
class SessionScope(StrEnum):
DIRECT = "dm"
GROUP = "group"
GROUP_SENDER = "group_sender"
TOPIC = "topic"
TOPIC_SENDER = "topic_sender"
THREAD = "thread"
class ThreadContext(BaseModel):
thread_id: str
thread_type: ThreadType = ThreadType.DIRECT
parent_id: str | None = None
root_message_id: str | None = None
participants: list[str] = []
created_at: datetime | None = None
metadata: dict[str, Any] = {}
class HistoricalMessage(BaseModel):
message_id: str
sender_id: str
sender_name: str
content: str
timestamp: datetime
is_from_bot: bool = False
reply_to_id: str | None = None
class FetchOptions(BaseModel):
max_messages: int = 50
max_chars: int = 4000
include_bot_messages: bool = True
before_message_id: str | None = None
after_message_id: str | None = None
class MessageType(StrEnum):
TEXT = "text"
IMAGE = "image"
FILE = "file"
AUDIO = "audio"
VIDEO = "video"
LOCATION = "location"
STICKER = "sticker"
CARD = "card"
POLL = "poll"
COMMAND = "command"
class ChannelType(StrEnum):
WEBCHAT = "webchat"
TELEGRAM = "telegram"
SLACK = "slack"
DISCORD = "discord"
NOSTR = "nostr"
DINGDING = "dingding"
FEISHU = "feishu"
WHATSAPP = "whatsapp"
LINE = "line"
WECHAT = "wechat"
QQ_BOT = "qq_bot"
WECHAT_WORK = "wechat_work"
ALIBABA = "alibaba"
VK = "vk"
ZALO = "zalo"
ZALO_OA = "zalo_oa"
ZALO_USER = "zalo_user"
SIGNAL = "signal"
IMO = "imo"
MS_TEAMS = "ms_teams"
GOOGLE_CHAT = "google_chat"
ROCKETCHAT = "rocketchat"
MATRIX = "matrix"
MATTERMOST = "mattermost"
SKYPE = "skype"
KIK = "kik"
THREADS = "threads"
TWITTER = "twitter"
SMS = "sms"
EMAIL = "email"
SYNOLOGYCHAT = "synologychat"
BLUEBUBBLES = "bluebubbles"
IMESSAGE = "imessage"
NEXTCLOUDTALK = "nextcloud-talk"
IRC = "irc"
TWITCH = "twitch"
URBIT = "urbit"
YUANBAO = "yuanbao"
class ChatType(StrEnum):
DIRECT = "direct"
CHANNEL = "channel"
GROUP = "group"
THREAD = "thread"
FORUM = "forum"
GUILD_CHANNEL = "guild_channel"
SPACE = "space"
CHAT_ROOM = "chat_room"
class EventType(StrEnum):
MESSAGE_RECEIVED = "message.received"
MESSAGE_UPDATED = "message.updated"
MESSAGE_DELETED = "message.deleted"
MESSAGES_DELETED = "messages.deleted"
BOT_ADDED = "bot.added"
BOT_REMOVED = "bot.removed"
MEMBER_JOINED = "member.joined"
MEMBER_LEFT = "member.left"
MEMBER_ADDED = "member.added"
MEMBER_REMOVED = "member.removed"
MEMBER_UPDATED = "member.updated"
CARD_ACTION = "card.action"
REACTION_ADDED = "reaction.added"
REACTION_REMOVED = "reaction.removed"
BOT_MENU = "bot.menu"
TYPING = "typing"
READ_RECEIPT = "read_receipt"
SYSTEM_EVENT = "system.event"
ROLE_CREATED = "role.created"
ROLE_DELETED = "role.deleted"
ROLE_UPDATED = "role.updated"
CHANNEL_CREATED = "channel.created"
CHANNEL_UPDATED = "channel.updated"
CHANNEL_DELETED = "channel.deleted"
INTERACTION = "interaction"
class RejectReason(StrEnum):
UNAUTHORIZED = "unauthorized"
RATE_LIMITED = "rate_limited"
SIZE_EXCEEDED = "size_exceeded"
UNSUPPORTED_TYPE = "unsupported_type"
POLICY_DENIED = "policy_denied"
INVALID_PAYLOAD = "invalid_payload"
TIMEOUT = "timeout"
INTERNAL_ERROR = "internal_error"
BLOCKED_USER = "blocked_user"
BLOCKED_GROUP = "blocked_group"
class Attachment(BaseModel):
type: str = "file"
url: str | None = None
filename: str | None = None
size_bytes: int | None = None
mime_type: str | None = None
file_id: str | None = None
metadata: dict[str, Any] = {}
class MentionsInfo(BaseModel):
mentioned_user_ids: list[str] = []
is_bot_mentioned: bool = False
raw_text: str | None = None
class ChannelIdentity(BaseModel):
channel_id: str
channel_type: ChannelType
channel_user_id: str
channel_chat_id: str
channel_message_id: str | None = None
class ChannelMessage(BaseModel):
identity: ChannelIdentity
event_type: EventType = EventType.MESSAGE_RECEIVED
message_type: MessageType = MessageType.TEXT
chat_type: ChatType = ChatType.DIRECT
content: str
attachments: list[Attachment] = []
mentions: MentionsInfo | None = None
extracted_urls: list[str] = []
reply_to_message_id: str | None = None
metadata: dict[str, Any] = {}
timestamp: datetime = Field(default_factory=utc_now_naive)
class ChannelResponse(BaseModel):
identity: ChannelIdentity
message_type: MessageType = MessageType.TEXT
content: str
attachments: list[Attachment] = []
reply_to_message_id: str | None = None
metadata: dict[str, Any] = {}
timestamp: datetime = Field(default_factory=utc_now_naive)
class AgentRequest(BaseModel):
user_id: str
thread_id: str
agent_id: str | None = None
agent_config_id: int
message: ChannelMessage
context: dict[str, Any] = {}
class AgentResult(BaseModel):
response_text: str
response_type: Literal["text", "stream", "error", "interrupt"] = "text"
attachments: list[Attachment] = []
agent_state: dict[str, Any] | None = None
metadata: dict[str, Any] = {}
class DeliveryResult(BaseModel):
success: bool
message_id: str | None = None
error: str | None = None
auth_expired: bool = False
metadata: dict[str, Any] = {}
class ChannelStatus(StrEnum):
DISCONNECTED = "disconnected"
CONNECTING = "connecting"
CONNECTED = "connected"
RECONNECTING = "reconnecting"
ERROR = "error"
DISABLED = "disabled"
class HealthStatus(BaseModel):
status: Literal["healthy", "degraded", "unhealthy"]
latency_ms: float | None = None
last_error: str | None = None
last_connected_at: datetime | None = None
metadata: dict[str, Any] = {}
class TokenStatus(BaseModel):
"""Token / 密钥状态 — 对齐清单 5.5.3"""
source: str = ""
status: str = "unknown"
last_verified_at: float = 0.0
class ChannelAccountSnapshot(BaseModel):
"""渠道账户运行状态快照(所有适配器统一格式)
统一了 QQBot/WhatsApp/微信/飞书四份方案中的状态字段。
适配器可通过继承扩展渠道专属字段。
字段按语义分组:
- 标识account_id, name
- 配置状态configured, enabled
- 连接状态linked, running, connected, status_state, health_state
- 时间线8 个 last_* 时间戳
- 错误与重连last_error, last_disconnect, reconnect_attempts
- 安全策略dm_policy, group_policy, allow_from_count
- 负载busy, active_runs, chain_stopped
- Token/密钥token_status, bot_token_status, app_token_status 等
- 网络配置webhook_path, webhook_url, base_url, port 等
- 探测/审计probe, last_probe_at, audit
- Bot/应用信息application, bot, profile, public_key
"""
account_id: str = ""
name: str = ""
configured: bool = False
enabled: bool = True
linked: bool = False
running: bool = False
connected: bool = False
status_state: str = "not-configured"
health_state: str = "stopped"
last_start_at: float | None = None
last_stop_at: float | None = None
last_connected_at_s: float | None = None
last_message_at: float | None = None
last_event_at: float | None = None
last_inbound_at: float | None = None
last_outbound_at: float | None = None
last_transport_activity_at: float | None = None
last_error: str | None = None
last_disconnect: dict | None = None
reconnect_attempts: int = 0
dm_policy: str = "pairing"
group_policy: str = "allowlist"
allow_from_count: int = 0
busy: bool = False
active_runs: int = 0
chain_stopped: bool = False
token_source: str = ""
token_status: TokenStatus | None = None
bot_token_source: str = ""
bot_token_status: TokenStatus | None = None
app_token_source: str = ""
app_token_status: TokenStatus | None = None
signing_secret_source: str = ""
signing_secret_status: TokenStatus | None = None
user_token_status: TokenStatus | None = None
credential_source: str = ""
secret_source: str = ""
webhook_path: str = ""
webhook_url: str = ""
base_url: str = ""
cli_path: str = ""
db_path: str = ""
port: int = 0
probe: dict | None = None
last_probe_at: float = 0.0
audit: dict | None = None
application: dict | None = None
bot: dict | None = None
public_key: str = ""
profile: dict | None = None
model_config = ConfigDict(extra="allow")
class MessageActionRequest(BaseModel):
action: str
message_id: str
params: dict[str, Any] = {}
class MessageActionResult(BaseModel):
success: bool
message: str = ""
data: dict[str, Any] | None = None
def build_snapshot_from_adapter(adapter) -> ChannelAccountSnapshot:
"""从适配器实例构建通用快照
使用 getattr 安全读取,兼容适配器未定义某个字段的场景。
适配器可以覆写此函数或直接继承 ChannelAccountSnapshot。
用法:
snapshot = build_snapshot_from_adapter(self)
snapshot.reconnect_attempts += 1
"""
status = getattr(adapter, "_status", None)
status_str = str(status.value) if hasattr(status, "value") else str(status or "stopped")
return ChannelAccountSnapshot(
account_id=getattr(adapter, "account_id", ""),
name=getattr(adapter, "account_name", ""),
configured=getattr(adapter, "_token_mgr", None) is not None,
enabled=getattr(adapter, "_enabled", True),
linked=getattr(adapter, "_linked", False),
running=getattr(adapter, "_running", False),
connected=getattr(adapter, "_connected", False),
status_state=getattr(adapter, "_status_state", "not-configured"),
health_state=status_str,
last_connected_at_s=getattr(adapter, "_last_connected_at", None),
last_message_at=getattr(adapter, "_last_message_at", None),
last_error=getattr(adapter, "_last_error", None),
reconnect_attempts=getattr(adapter, "_reconnect_attempts", 0),
dm_policy=str(getattr(adapter, "dm_policy", "allowlist")),
group_policy=str(getattr(adapter, "group_policy", "allowlist")),
busy=getattr(adapter, "_busy", False),
active_runs=getattr(adapter, "_active_runs", 0),
)