ForcePilot/backend/package/yuxi/channel/extensions/qqbot/credentials.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

65 lines
2.4 KiB
Python

from __future__ import annotations
import json
import logging
import os
from pathlib import Path
logger = logging.getLogger(__name__)
CREDENTIAL_BACKUP_DIR = Path.home() / ".forcepilot" / "qqbot" / "credentials"
class CredentialBackup:
def __init__(self, backup_dir: Path | None = None):
self._backup_dir = backup_dir or CREDENTIAL_BACKUP_DIR
self._backup_dir.mkdir(parents=True, exist_ok=True)
def _backup_path(self, account_id: str) -> Path:
safe_id = account_id.replace("/", "_").replace("\\", "_")
return self._backup_dir / f"{safe_id}.json"
def save(self, account_id: str, app_id: str, client_secret: str) -> None:
path = self._backup_path(account_id)
data = {
"account_id": account_id,
"app_id": app_id,
"client_secret": client_secret,
}
try:
path.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
logger.debug("Credential backup saved for account '%s'", account_id)
except OSError as e:
logger.warning("Failed to save credential backup for '%s': %s", account_id, e)
def load(self, account_id: str) -> dict | None:
path = self._backup_path(account_id)
if not path.exists():
return None
try:
data = json.loads(path.read_text(encoding="utf-8"))
if data.get("app_id") and data.get("client_secret"):
logger.debug("Credential backup loaded for account '%s'", account_id)
return data
except (json.JSONDecodeError, OSError) as e:
logger.warning("Failed to load credential backup for '%s': %s", account_id, e)
return None
def exists(self, account_id: str) -> bool:
return self._backup_path(account_id).exists()
def clear(self, account_id: str) -> None:
path = self._backup_path(account_id)
try:
path.unlink(missing_ok=True)
logger.debug("Credential backup cleared for account '%s'", account_id)
except OSError as e:
logger.warning("Failed to clear credential backup for '%s': %s", account_id, e)
def clear_all(self) -> None:
for path in self._backup_dir.glob("*.json"):
try:
path.unlink()
except OSError:
pass
logger.debug("All credential backups cleared")