新增 QQ Bot 渠道扩展,支持在 Yuxi 平台中集成 QQ 机器人渠道。 包含以下功能模块: - api_client: QQ API 客户端封装 - api_routes: API 路由管理 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - websocket: WebSocket 实时连接 - credentials: 凭证管理 - token: Token 管理 - outbound: 外发消息管理 - outbound_media: 媒体外发 - streaming: 流式消息处理 - streaming_media: 媒体流处理 - pairing: 用户配对与绑定 - security: 安全校验 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - session: 会话管理 - pipeline: 消息管道 - pipeline_stages: 管道阶段 - commands: 指令处理 - commands_builtin: 内置指令 - interaction: 交互处理 - approval: 审批流程 - ark: ARK 消息 - audio: 音频处理 - media: 媒体资源 - media_chunked: 分块媒体 - media_tags: 媒体标签 - message_queue: 消息队列 - delivery: 消息送达确认 - reconnect: 重连机制 - typing_keepalive: 输入状态保活 - group_activation: 群激活 - group_gating: 群门控 - group_history: 群历史 - known_users: 已知用户 - ref_index: 引用索引 - tools: Agent 工具集成 - types: 类型定义
123 lines
3.9 KiB
Python
123 lines
3.9 KiB
Python
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import logging
|
|
import os
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
MEDIA_SIZE_LIMITS = {
|
|
"image": 30 * 1024 * 1024,
|
|
"voice": 20 * 1024 * 1024,
|
|
"video": 100 * 1024 * 1024,
|
|
"file": 100 * 1024 * 1024,
|
|
}
|
|
|
|
MEDIA_EXTENSIONS = {
|
|
".jpg": "image", ".jpeg": "image", ".png": "image",
|
|
".gif": "image", ".webp": "image", ".bmp": "image",
|
|
".silk": "voice", ".wav": "voice", ".mp3": "voice",
|
|
".ogg": "voice", ".aac": "voice", ".flac": "voice",
|
|
".mp4": "video", ".mov": "video", ".avi": "video",
|
|
".mkv": "video", ".webm": "video",
|
|
}
|
|
|
|
DEFAULT_MEDIA_ROOT = Path.home() / ".forcepilot" / "qqbot" / "media"
|
|
|
|
|
|
class QQBotMedia:
|
|
def __init__(
|
|
self,
|
|
api_client: Any = None,
|
|
media_root: Path | None = None,
|
|
url_direct_upload: bool = True,
|
|
):
|
|
self._api_client = api_client
|
|
self._media_root = media_root or DEFAULT_MEDIA_ROOT
|
|
self._media_root.mkdir(parents=True, exist_ok=True)
|
|
self._url_direct_upload = url_direct_upload
|
|
self._upload_cache: dict[str, str] = {}
|
|
|
|
def detect_media_type(self, path_or_url: str) -> str:
|
|
ext = os.path.splitext(path_or_url.split("?")[0])[1].lower()
|
|
if ext in MEDIA_EXTENSIONS:
|
|
return MEDIA_EXTENSIONS[ext]
|
|
return "image"
|
|
|
|
def check_size_limit(self, file_path: str | Path, media_type: str) -> bool:
|
|
path = Path(file_path)
|
|
if not path.exists():
|
|
return False
|
|
|
|
limit = MEDIA_SIZE_LIMITS.get(media_type, 100 * 1024 * 1024)
|
|
size = path.stat().st_size
|
|
return size <= limit
|
|
|
|
def is_trusted_path(self, file_path: str | Path) -> bool:
|
|
path = Path(file_path).resolve()
|
|
try:
|
|
path.relative_to(self._media_root.resolve())
|
|
return True
|
|
except ValueError:
|
|
pass
|
|
|
|
for suffix in path.suffixes:
|
|
if suffix.lower() in MEDIA_EXTENSIONS:
|
|
return True
|
|
|
|
return False
|
|
|
|
async def upload_file(
|
|
self, file_path: str, chat_type: str, target_id: str
|
|
) -> dict | None:
|
|
path = Path(file_path)
|
|
if not path.exists():
|
|
logger.warning("Media file not found: %s", file_path)
|
|
return None
|
|
|
|
if not self.is_trusted_path(path):
|
|
logger.warning("Media path not trusted: %s", file_path)
|
|
return None
|
|
|
|
file_data = path.read_bytes()
|
|
file_hash = hashlib.sha256(file_data).hexdigest()[:16]
|
|
|
|
if file_hash in self._upload_cache:
|
|
return {"url": self._upload_cache[file_hash], "cache_hit": True}
|
|
|
|
media_type = self.detect_media_type(file_path)
|
|
if not self.check_size_limit(file_path, media_type):
|
|
logger.warning("Media file exceeds size limit: %s", file_path)
|
|
return None
|
|
|
|
if self._api_client:
|
|
from yuxi.channel.extensions.qqbot.api_routes import (
|
|
FILE_TYPE_FILE,
|
|
FILE_TYPE_IMAGE,
|
|
FILE_TYPE_VIDEO,
|
|
FILE_TYPE_VOICE,
|
|
)
|
|
from yuxi.channel.extensions.qqbot.types import QQBotChatType
|
|
|
|
file_type_map = {
|
|
"image": FILE_TYPE_IMAGE,
|
|
"voice": FILE_TYPE_VOICE,
|
|
"video": FILE_TYPE_VIDEO,
|
|
"file": FILE_TYPE_FILE,
|
|
}
|
|
qt = QQBotChatType.C2C if chat_type in ("c2c", "dm") else QQBotChatType.GROUP
|
|
ft = file_type_map.get(media_type, FILE_TYPE_FILE)
|
|
|
|
att = await self._api_client.upload_media(
|
|
target_id, file_data, path.name, ft, qt
|
|
)
|
|
if att and att.url:
|
|
self._upload_cache[file_hash] = att.url
|
|
return {"url": att.url, "cache_hit": False}
|
|
|
|
return None
|
|
|
|
def clear_upload_cache(self) -> None:
|
|
self._upload_cache.clear() |