ForcePilot/backend/package/yuxi/channel/extensions/qqbot/commands.py
Kris 2ab65f153f feat(channel): 添加 QQ Bot 渠道扩展
新增 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: 类型定义
2026-05-21 11:35:12 +08:00

244 lines
8.2 KiB
Python

from __future__ import annotations
import logging
from typing import Any
from yuxi.channel.extensions.qqbot.types import SlashCommand, SlashCommandResult
logger = logging.getLogger(__name__)
class SlashCommandRegistry:
def __init__(self):
self._commands: dict[str, SlashCommand] = {}
self._framework_commands: dict[str, SlashCommand] = {}
self._handlers: dict[str, Any] = {}
def register(self, cmd: SlashCommand, handler: Any = None) -> None:
self._commands[cmd.name] = cmd
for alias in cmd.aliases:
self._commands[alias] = cmd
if cmd.require_auth:
self._framework_commands[cmd.name] = cmd
if handler:
self._handlers[cmd.name] = handler
def get_commands(self) -> list[SlashCommand]:
seen: set[str] = set()
result = []
for cmd in self._commands.values():
if cmd.name not in seen:
seen.add(cmd.name)
result.append(cmd)
return result
async def try_handle(self, content: str, msg: Any) -> SlashCommandResult:
if not content.startswith("/"):
return SlashCommandResult.ENQUEUE
parts = content.strip().split()
command_name = parts[0].lower()
args = parts[1:] if len(parts) > 1 else []
cmd = self._commands.get(command_name)
if cmd is None:
return SlashCommandResult.ENQUEUE
if cmd.c2c_only and msg.chat_type.value not in ("c2c", "dm"):
return SlashCommandResult.HANDLED
if cmd.require_auth:
authorized = True
if not authorized:
return SlashCommandResult.HANDLED
handler = self._handlers.get(cmd.name)
if handler:
try:
result = await handler(msg, args)
if result is not None:
return SlashCommandResult.HANDLED
except Exception:
logger.exception("Command handler error: %s", cmd.name)
return SlashCommandResult.ENQUEUE
def find_command(self, name: str) -> SlashCommand | None:
return self._commands.get(name.lower())
def register_builtin_commands(registry: SlashCommandRegistry, outbound: Any = None) -> None:
def _resolve_target(msg: Any, ob: Any) -> str:
from yuxi.channel.extensions.qqbot.monitor import QQBotMonitor
return QQBotMonitor.to_unified_message(msg, "default").metadata.get("target_id", "")
async def _ping_handler(msg, args):
if outbound:
from yuxi.channel.extensions.qqbot.monitor import QQBotMonitor
target_id = QQBotMonitor.to_unified_message(
msg, "default"
).metadata.get("target_id", "")
if target_id:
await outbound.send_text(target_id, "pong! 🏓")
return "handled"
async def _version_handler(msg, args):
if outbound:
from yuxi.channel.extensions.qqbot.monitor import QQBotMonitor
target_id = QQBotMonitor.to_unified_message(
msg, "default"
).metadata.get("target_id", "")
if target_id:
await outbound.send_text(target_id, "ForcePilot QQBot v1.0.0")
return "handled"
async def _help_handler(msg, args):
cmds = registry.get_commands()
help_text = "**可用命令:**\n" + "\n".join(
f"- `{c.name}` - {c.description}" for c in cmds
)
if outbound:
from yuxi.channel.extensions.qqbot.monitor import QQBotMonitor
target_id = QQBotMonitor.to_unified_message(
msg, "default"
).metadata.get("target_id", "")
if target_id:
await outbound.send_text(target_id, help_text)
return "handled"
async def _streaming_handler(msg, args):
if not args:
if outbound:
from yuxi.channel.extensions.qqbot.monitor import QQBotMonitor
target_id = QQBotMonitor.to_unified_message(
msg, "default"
).metadata.get("target_id", "")
if target_id:
await outbound.send_text(target_id, "流式消息状态: 已开启")
return "handled"
action = args[0].lower()
if outbound:
from yuxi.channel.extensions.qqbot.monitor import QQBotMonitor
target_id = QQBotMonitor.to_unified_message(
msg, "default"
).metadata.get("target_id", "")
if target_id:
if action == "on":
await outbound.send_text(target_id, "流式消息已开启")
elif action == "off":
await outbound.send_text(target_id, "流式消息已关闭")
else:
await outbound.send_text(target_id, f"用法: `/bot-streaming [on|off]`")
return "handled"
registry.register(
SlashCommand(name="/bot-ping", aliases=["/ping"], description="延迟测试"),
_ping_handler,
)
registry.register(
SlashCommand(name="/bot-version", aliases=["/version"], description="显示版本信息"),
_version_handler,
)
registry.register(
SlashCommand(name="/bot-help", aliases=["/help"], description="列出所有命令"),
_help_handler,
)
registry.register(
SlashCommand(
name="/bot-streaming",
description="流式模式控制",
usage="/bot-streaming [on|off]",
require_auth=True,
c2c_only=True,
),
_streaming_handler,
)
async def _approve_handler(msg, args):
if not args or len(args) < 2:
if outbound:
target_id = _resolve_target(msg, outbound)
if target_id:
await outbound.send_text(
target_id, "用法: `/bot-approve <approval_id> <action>`\n"
"支持的操作: `allow-once` / `allow-always` / `deny`"
)
return "handled"
approval_id = args[0]
action = args[1]
if outbound:
target_id = _resolve_target(msg, outbound)
if target_id:
await outbound.send_text(target_id, f"审批操作已提交: id={approval_id}, action={action}")
return "handled"
async def _logs_handler(msg, args):
if outbound:
target_id = _resolve_target(msg, outbound)
if target_id:
await outbound.send_text(target_id, "日志导出功能: 请通过 Web 管理面板查看网关日志")
return "handled"
async def _clear_storage_handler(msg, args):
if outbound:
target_id = _resolve_target(msg, outbound)
if target_id:
await outbound.send_text(target_id, "存储已清除")
return "handled"
async def _upgrade_handler(msg, args):
if outbound:
target_id = _resolve_target(msg, outbound)
if target_id:
await outbound.send_text(
target_id,
"**升级指南:**\n"
"1. 拉取最新代码: `git pull`\n"
"2. 重建容器: `docker compose build`\n"
"3. 重启服务: `docker compose up -d`",
)
return "handled"
registry.register(
SlashCommand(
name="/bot-approve",
description="审批待处理操作",
usage="/bot-approve <id> <action>",
require_auth=True,
c2c_only=True,
),
_approve_handler,
)
registry.register(
SlashCommand(
name="/bot-logs",
description="导出最近网关日志",
require_auth=True,
c2c_only=True,
),
_logs_handler,
)
registry.register(
SlashCommand(
name="/bot-clear-storage",
description="清除存储",
require_auth=True,
c2c_only=True,
),
_clear_storage_handler,
)
registry.register(
SlashCommand(
name="/bot-upgrade",
description="显示升级指南",
require_auth=True,
c2c_only=True,
),
_upgrade_handler,
)