新增 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: 类型定义
50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
import re
|
|
from typing import Any
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
MEDIA_TAG_RE = re.compile(r"<(qqmedia|qqimg)>.*?</\1>")
|
|
|
|
|
|
class StreamingMediaHandler:
|
|
def __init__(self, streaming_controller: Any, outbound: Any):
|
|
self._streaming = streaming_controller
|
|
self._outbound = outbound
|
|
|
|
async def handle_media_interrupt(self, openid: str, text: str) -> tuple[str, bool]:
|
|
match = MEDIA_TAG_RE.search(text)
|
|
if not match:
|
|
return text, False
|
|
|
|
before = text[: match.start()]
|
|
tag_text = match.group(0)
|
|
after = text[match.end() :]
|
|
|
|
if before.strip():
|
|
try:
|
|
await self._streaming.flush(openid)
|
|
except Exception:
|
|
pass
|
|
|
|
await self._streaming.abort(openid)
|
|
|
|
if tag_text:
|
|
try:
|
|
from yuxi.channel.extensions.qqbot.media_tags import parse_media_tags
|
|
|
|
_, tags = parse_media_tags(tag_text)
|
|
for tag in tags:
|
|
await self._outbound.send_media(openid, tag.url, "auto")
|
|
except Exception as e:
|
|
logger.warning("Failed to send media in stream: %s", e)
|
|
|
|
return after, True
|
|
|
|
async def resume_after_media(self, openid: str, text: str) -> None:
|
|
if not text.strip():
|
|
return
|
|
stream_id = await self._streaming.start_streaming_session(openid)
|
|
await self._streaming.feed(openid, text) |