ForcePilot/backend/package/yuxi/channels/adapters/matrix/cli.py
Kris 2063145ce1 feat(matrix): 新增完整的Matrix适配器工具集
新增了20+个Matrix相关工具模块,包含线程处理、自动加入、消息去重、配置管理、安全策略、媒体处理、命令解析等功能,完善Matrix适配器基础能力
2026-05-12 00:45:54 +08:00

145 lines
4.3 KiB
Python

from __future__ import annotations
from typing import Any
def get_cli_commands() -> dict[str, dict[str, Any]]:
return {
"matrix": {
"description": "Matrix channel management commands",
"subcommands": {
"account": {
"description": "Manage Matrix accounts",
"subcommands": {
"add": {
"description": "Add a new Matrix account",
"args": ["<homeserver>", "<user_id>", "[access_token]"],
},
"list": {
"description": "List configured Matrix accounts",
"args": [],
},
"remove": {
"description": "Remove a Matrix account",
"args": ["<account_id>"],
},
"select": {
"description": "Select default Matrix account",
"args": ["<account_id>"],
},
},
},
"status": {
"description": "Show Matrix connection status",
"args": [],
},
"rooms": {
"description": "List joined Matrix rooms",
"args": [],
},
"join": {
"description": "Join a Matrix room",
"args": ["<room_id_or_alias>"],
},
"leave": {
"description": "Leave a Matrix room",
"args": ["<room_id>"],
},
"probe": {
"description": "Probe Matrix homeserver health",
"args": [],
},
"doctor": {
"description": "Run diagnostics and repair",
"args": [],
},
"wizard": {
"description": "Start interactive setup wizard",
"args": [],
},
"sync-token": {
"description": "Manage sync token",
"subcommands": {
"show": {"description": "Show current sync token", "args": []},
"clear": {"description": "Clear stored sync token", "args": []},
},
},
},
}
}
def format_cli_help() -> str:
return """
Matrix CLI Commands:
=====================
yuxi matrix account add <homeserver> <user_id> [access_token]
Add a new Matrix account
yuxi matrix account list
List all configured accounts
yuxi matrix account remove <account_id>
Remove an account
yuxi matrix account select <account_id>
Set default account
yuxi matrix status
Show connection status
yuxi matrix rooms
List joined rooms
yuxi matrix join <room_id_or_alias>
Join a room
yuxi matrix leave <room_id>
Leave a room
yuxi matrix probe
Run health probe
yuxi matrix doctor
Run diagnostic checks
yuxi matrix wizard
Start setup wizard
yuxi matrix sync-token show
Show stored sync token
yuxi matrix sync-token clear
Clear stored sync token
""".strip()
class MatrixCLI:
def __init__(self):
self._commands = get_cli_commands()
def dispatch(self, args: list[str]) -> dict[str, Any]:
if not args:
return {"status": "error", "message": "No command specified"}
if args[0] != "matrix":
return {"status": "error", "message": f"Unknown command: {args[0]}"}
if len(args) < 2:
return {"status": "ok", "help": format_cli_help()}
subcommand = args[1]
subcmds = self._commands["matrix"]["subcommands"]
if subcommand not in subcmds:
return {"status": "error", "message": f"Unknown matrix subcommand: {subcommand}"}
return {
"status": "ok",
"command": f"matrix {subcommand}",
"description": subcmds[subcommand]["description"],
"args": args[2:] if len(args) > 2 else [],
"expected_args": subcmds[subcommand].get("args", []),
}