refactor: move all internal commands from astrbot plugin to default_command plugin (#3960)

* refactor: move all internal commands from astrbot plugin to default_command plugin

* ruff check

* feat: add config

* ruff check
This commit is contained in:
Soulter
2025-12-08 22:17:32 +08:00
committed by GitHub
parent 2c36649874
commit aa6d07afcc
23 changed files with 286 additions and 253 deletions
+6
View File
@@ -165,6 +165,7 @@ DEFAULT_CONFIG = {
"kb_fusion_top_k": 20, # 知识库检索融合阶段返回结果数量 "kb_fusion_top_k": 20, # 知识库检索融合阶段返回结果数量
"kb_final_top_k": 5, # 知识库检索最终返回结果数量 "kb_final_top_k": 5, # 知识库检索最终返回结果数量
"kb_agentic_mode": False, "kb_agentic_mode": False,
"disable_builtin_commands": False,
} }
@@ -2669,6 +2670,11 @@ CONFIG_METADATA_3 = {
"description": "只 @ 机器人是否触发等待", "description": "只 @ 机器人是否触发等待",
"type": "bool", "type": "bool",
}, },
"disable_builtin_commands": {
"description": "禁用自带指令",
"type": "bool",
"hint": "禁用所有 AstrBot 的自带指令,如 help, provider, model 等。",
},
}, },
}, },
"whitelist": { "whitelist": {
@@ -50,6 +50,9 @@ class WakingCheckStage(Stage):
"ignore_at_all", "ignore_at_all",
False, False,
) )
self.disable_builtin_commands = self.ctx.astrbot_config.get(
"disable_builtin_commands", False
)
async def process( async def process(
self, self,
@@ -131,6 +134,13 @@ class WakingCheckStage(Stage):
EventType.AdapterMessageEvent, EventType.AdapterMessageEvent,
plugins_name=event.plugins_name, plugins_name=event.plugins_name,
): ):
if (
self.disable_builtin_commands
and handler.handler_module_path == "packages.builtin_commands.main"
):
logger.debug("skipping builtin command")
continue
# filter 需满足 AND 逻辑关系 # filter 需满足 AND 逻辑关系
passed = True passed = True
permission_not_pass = False permission_not_pass = False
@@ -221,6 +221,10 @@
}, },
"wake_prefix": { "wake_prefix": {
"description": "Wake Word" "description": "Wake Word"
},
"disable_builtin_commands": {
"description": "Disable Built-in Commands",
"hint": "Disable all built-in AstrBot commands such as help, provider, model, etc."
} }
}, },
"whitelist": { "whitelist": {
@@ -229,6 +229,10 @@
}, },
"wake_prefix": { "wake_prefix": {
"description": "唤醒词" "description": "唤醒词"
},
"disable_builtin_commands": {
"description": "禁用自带指令",
"hint": "禁用所有 AstrBot 自带指令,如 help, provider, model 等"
} }
}, },
"whitelist": { "whitelist": {
+10 -226
View File
@@ -6,21 +6,6 @@ from astrbot.api.message_components import Image, Plain
from astrbot.api.provider import LLMResponse, ProviderRequest from astrbot.api.provider import LLMResponse, ProviderRequest
from astrbot.core import logger from astrbot.core import logger
from .commands import (
AdminCommands,
AlterCmdCommands,
ConversationCommands,
HelpCommand,
LLMCommands,
PersonaCommands,
PluginCommands,
ProviderCommands,
SetUnsetCommands,
SIDCommand,
T2ICommand,
ToolCommands,
TTSCommand,
)
from .long_term_memory import LongTermMemory from .long_term_memory import LongTermMemory
from .process_llm_request import ProcessLLMRequest from .process_llm_request import ProcessLLMRequest
@@ -34,19 +19,6 @@ class Main(star.Star):
except BaseException as e: except BaseException as e:
logger.error(f"聊天增强 err: {e}") logger.error(f"聊天增强 err: {e}")
self.help_c = HelpCommand(self.context)
self.llm_c = LLMCommands(self.context)
self.tool_c = ToolCommands(self.context)
self.plugin_c = PluginCommands(self.context)
self.admin_c = AdminCommands(self.context)
self.conversation_c = ConversationCommands(self.context, self.ltm)
self.provider_c = ProviderCommands(self.context)
self.persona_c = PersonaCommands(self.context)
self.alter_cmd_c = AlterCmdCommands(self.context)
self.setunset_c = SetUnsetCommands(self.context)
self.t2i_c = T2ICommand(self.context)
self.tts_c = TTSCommand(self.context)
self.sid_c = SIDCommand(self.context)
self.proc_llm_req = ProcessLLMRequest(self.context) self.proc_llm_req = ProcessLLMRequest(self.context)
def ltm_enabled(self, event: AstrMessageEvent): def ltm_enabled(self, event: AstrMessageEvent):
@@ -55,199 +27,6 @@ class Main(star.Star):
] ]
return ltmse["group_icl_enable"] or ltmse["active_reply"]["enable"] return ltmse["group_icl_enable"] or ltmse["active_reply"]["enable"]
@filter.command("help")
async def help(self, event: AstrMessageEvent):
"""查看帮助"""
await self.help_c.help(event)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("llm")
async def llm(self, event: AstrMessageEvent):
"""开启/关闭 LLM"""
await self.llm_c.llm(event)
@filter.command_group("tool")
def tool(self):
pass
@tool.command("ls")
async def tool_ls(self, event: AstrMessageEvent):
"""查看函数工具列表"""
await self.tool_c.tool_ls(event)
@tool.command("on")
async def tool_on(self, event: AstrMessageEvent, tool_name: str):
"""启用一个函数工具"""
await self.tool_c.tool_on(event, tool_name)
@tool.command("off")
async def tool_off(self, event: AstrMessageEvent, tool_name: str):
"""停用一个函数工具"""
await self.tool_c.tool_off(event, tool_name)
@tool.command("off_all")
async def tool_all_off(self, event: AstrMessageEvent):
"""停用所有函数工具"""
await self.tool_c.tool_all_off(event)
@filter.command_group("plugin")
def plugin(self):
pass
@plugin.command("ls")
async def plugin_ls(self, event: AstrMessageEvent):
"""获取已经安装的插件列表。"""
await self.plugin_c.plugin_ls(event)
@filter.permission_type(filter.PermissionType.ADMIN)
@plugin.command("off")
async def plugin_off(self, event: AstrMessageEvent, plugin_name: str = ""):
"""禁用插件"""
await self.plugin_c.plugin_off(event, plugin_name)
@filter.permission_type(filter.PermissionType.ADMIN)
@plugin.command("on")
async def plugin_on(self, event: AstrMessageEvent, plugin_name: str = ""):
"""启用插件"""
await self.plugin_c.plugin_on(event, plugin_name)
@filter.permission_type(filter.PermissionType.ADMIN)
@plugin.command("get")
async def plugin_get(self, event: AstrMessageEvent, plugin_repo: str = ""):
"""安装插件"""
await self.plugin_c.plugin_get(event, plugin_repo)
@plugin.command("help")
async def plugin_help(self, event: AstrMessageEvent, plugin_name: str = ""):
"""获取插件帮助"""
await self.plugin_c.plugin_help(event, plugin_name)
@filter.command("t2i")
async def t2i(self, event: AstrMessageEvent):
"""开关文本转图片"""
await self.t2i_c.t2i(event)
@filter.command("tts")
async def tts(self, event: AstrMessageEvent):
"""开关文本转语音(会话级别)"""
await self.tts_c.tts(event)
@filter.command("sid")
async def sid(self, event: AstrMessageEvent):
"""获取会话 ID 和 管理员 ID"""
await self.sid_c.sid(event)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("op")
async def op(self, event: AstrMessageEvent, admin_id: str = ""):
"""授权管理员。op <admin_id>"""
await self.admin_c.op(event, admin_id)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("deop")
async def deop(self, event: AstrMessageEvent, admin_id: str):
"""取消授权管理员。deop <admin_id>"""
await self.admin_c.deop(event, admin_id)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("wl")
async def wl(self, event: AstrMessageEvent, sid: str = ""):
"""添加白名单。wl <sid>"""
await self.admin_c.wl(event, sid)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("dwl")
async def dwl(self, event: AstrMessageEvent, sid: str):
"""删除白名单。dwl <sid>"""
await self.admin_c.dwl(event, sid)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("provider")
async def provider(
self,
event: AstrMessageEvent,
idx: str | int | None = None,
idx2: int | None = None,
):
"""查看或者切换 LLM Provider"""
await self.provider_c.provider(event, idx, idx2)
@filter.command("reset")
async def reset(self, message: AstrMessageEvent):
"""重置 LLM 会话"""
await self.conversation_c.reset(message)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("model")
async def model_ls(
self,
message: AstrMessageEvent,
idx_or_name: int | str | None = None,
):
"""查看或者切换模型"""
await self.provider_c.model_ls(message, idx_or_name)
@filter.command("history")
async def his(self, message: AstrMessageEvent, page: int = 1):
"""查看对话记录"""
await self.conversation_c.his(message, page)
@filter.command("ls")
async def convs(self, message: AstrMessageEvent, page: int = 1):
"""查看对话列表"""
await self.conversation_c.convs(message, page)
@filter.command("new")
async def new_conv(self, message: AstrMessageEvent):
"""创建新对话"""
await self.conversation_c.new_conv(message)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("groupnew")
async def groupnew_conv(self, message: AstrMessageEvent, sid: str):
"""创建新群聊对话"""
await self.conversation_c.groupnew_conv(message, sid)
@filter.command("switch")
async def switch_conv(self, message: AstrMessageEvent, index: int | None = None):
"""通过 /ls 前面的序号切换对话"""
await self.conversation_c.switch_conv(message, index)
@filter.command("rename")
async def rename_conv(self, message: AstrMessageEvent, new_name: str):
"""重命名对话"""
await self.conversation_c.rename_conv(message, new_name)
@filter.command("del")
async def del_conv(self, message: AstrMessageEvent):
"""删除当前对话"""
await self.conversation_c.del_conv(message)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("key")
async def key(self, message: AstrMessageEvent, index: int | None = None):
"""查看或者切换 Key"""
await self.provider_c.key(message, index)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("persona")
async def persona(self, message: AstrMessageEvent):
"""查看或者切换 Persona"""
await self.persona_c.persona(message)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("dashboard_update")
async def update_dashboard(self, event: AstrMessageEvent):
await self.admin_c.update_dashboard(event)
@filter.command("set")
async def set_variable(self, event: AstrMessageEvent, key: str, value: str):
await self.setunset_c.set_variable(event, key, value)
@filter.command("unset")
async def unset_variable(self, event: AstrMessageEvent, key: str):
await self.setunset_c.unset_variable(event, key)
@filter.platform_adapter_type(filter.PlatformAdapterType.ALL) @filter.platform_adapter_type(filter.PlatformAdapterType.ALL)
async def on_message(self, event: AstrMessageEvent): async def on_message(self, event: AstrMessageEvent):
"""群聊记忆增强""" """群聊记忆增强"""
@@ -337,8 +116,13 @@ class Main(star.Star):
except Exception as e: except Exception as e:
logger.error(f"ltm: {e}") logger.error(f"ltm: {e}")
@filter.permission_type(filter.PermissionType.ADMIN) @filter.after_message_sent()
@filter.command("alter_cmd", alias={"alter"}) async def after_message_sent(self, event: AstrMessageEvent):
async def alter_cmd(self, event: AstrMessageEvent): """消息发送后处理"""
"""修改命令权限""" if self.ltm and self.ltm_enabled(event):
await self.alter_cmd_c.alter_cmd(event) try:
clean_session = event.get_extra("_clean_ltm_session", False)
if clean_session:
await self.ltm.remove_session(event)
except Exception as e:
logger.error(f"ltm: {e}")
+2 -2
View File
@@ -1,4 +1,4 @@
name: astrbot name: astrbot
desc: AstrBot 基础指令结合 + 拓展功能 desc: AstrBot 自带插件,包含人格注入、思考内容注入、群聊上下文感知等功能的实现,禁用后将无法使用这些功能
author: Soulter author: Soulter
version: 4.0.0 version: 4.1.0
@@ -1,24 +1,23 @@
import datetime import datetime
from astrbot.api import logger, sp, star from astrbot.api import sp, star
from astrbot.api.event import AstrMessageEvent, MessageEventResult from astrbot.api.event import AstrMessageEvent, MessageEventResult
from astrbot.core.platform.astr_message_event import MessageSession from astrbot.core.platform.astr_message_event import MessageSession
from astrbot.core.platform.message_type import MessageType from astrbot.core.platform.message_type import MessageType
from ..long_term_memory import LongTermMemory
from .utils.rst_scene import RstScene from .utils.rst_scene import RstScene
THIRD_PARTY_AGENT_RUNNER_KEY = { THIRD_PARTY_AGENT_RUNNER_KEY = {
"dify": "dify_conversation_id", "dify": "dify_conversation_id",
"coze": "coze_conversation_id", "coze": "coze_conversation_id",
"dashscope": "dashscope_conversation_id",
} }
THIRD_PARTY_AGENT_RUNNER_STR = ", ".join(THIRD_PARTY_AGENT_RUNNER_KEY.keys()) THIRD_PARTY_AGENT_RUNNER_STR = ", ".join(THIRD_PARTY_AGENT_RUNNER_KEY.keys())
class ConversationCommands: class ConversationCommands:
def __init__(self, context: star.Context, ltm: LongTermMemory | None = None): def __init__(self, context: star.Context):
self.context = context self.context = context
self.ltm = ltm
async def _get_current_persona_id(self, session_id): async def _get_current_persona_id(self, session_id):
curr = await self.context.conversation_manager.get_curr_conversation_id( curr = await self.context.conversation_manager.get_curr_conversation_id(
@@ -30,16 +29,10 @@ class ConversationCommands:
session_id, session_id,
curr, curr,
) )
if not conv:
return None
return conv.persona_id return conv.persona_id
def ltm_enabled(self, event: AstrMessageEvent):
if not self.ltm:
return False
ltmse = self.context.get_config(umo=event.unified_msg_origin)[
"provider_ltm_settings"
]
return ltmse["group_icl_enable"] or ltmse["active_reply"]["enable"]
async def reset(self, message: AstrMessageEvent): async def reset(self, message: AstrMessageEvent):
"""重置 LLM 会话""" """重置 LLM 会话"""
umo = message.unified_msg_origin umo = message.unified_msg_origin
@@ -99,10 +92,9 @@ class ConversationCommands:
[], [],
) )
ret = "清除会话 LLM 聊天历史成功" ret = "清除聊天历史成功"
if self.ltm and self.ltm_enabled(message):
cnt = await self.ltm.remove_session(event=message) message.set_extra("_clean_ltm_session", True)
ret += f"\n聊天增强: 已清除 {cnt} 条聊天记录。"
message.set_result(MessageEventResult().message(ret)) message.set_result(MessageEventResult().message(ret))
@@ -244,12 +236,7 @@ class ConversationCommands:
persona_id=cpersona, persona_id=cpersona,
) )
# 长期记忆 message.set_extra("_clean_ltm_session", True)
if self.ltm and self.ltm_enabled(message):
try:
await self.ltm.remove_session(event=message)
except Exception as e:
logger.error(f"清理聊天增强记录失败: {e}")
message.set_result( message.set_result(
MessageEventResult().message(f"切换到新对话: 新对话({cid[:4]})。"), MessageEventResult().message(f"切换到新对话: 新对话({cid[:4]})。"),
@@ -375,7 +362,5 @@ class ConversationCommands:
) )
ret = "删除当前对话成功。不再处于对话状态,使用 /switch 序号 切换到其他对话或 /new 创建。" ret = "删除当前对话成功。不再处于对话状态,使用 /switch 序号 切换到其他对话或 /new 创建。"
if self.ltm and self.ltm_enabled(message): message.set_extra("_clean_ltm_session", True)
cnt = await self.ltm.remove_session(event=message)
ret += f"\n聊天增强: 已清除 {cnt} 条聊天记录。"
message.set_result(MessageEventResult().message(ret)) message.set_result(MessageEventResult().message(ret))
+236
View File
@@ -0,0 +1,236 @@
from astrbot.api import star
from astrbot.api.event import AstrMessageEvent, filter
from .commands import (
AdminCommands,
AlterCmdCommands,
ConversationCommands,
HelpCommand,
LLMCommands,
PersonaCommands,
PluginCommands,
ProviderCommands,
SetUnsetCommands,
SIDCommand,
T2ICommand,
ToolCommands,
TTSCommand,
)
class Main(star.Star):
def __init__(self, context: star.Context) -> None:
self.context = context
self.help_c = HelpCommand(self.context)
self.llm_c = LLMCommands(self.context)
self.tool_c = ToolCommands(self.context)
self.plugin_c = PluginCommands(self.context)
self.admin_c = AdminCommands(self.context)
self.conversation_c = ConversationCommands(self.context)
self.provider_c = ProviderCommands(self.context)
self.persona_c = PersonaCommands(self.context)
self.alter_cmd_c = AlterCmdCommands(self.context)
self.setunset_c = SetUnsetCommands(self.context)
self.t2i_c = T2ICommand(self.context)
self.tts_c = TTSCommand(self.context)
self.sid_c = SIDCommand(self.context)
@filter.command("help")
async def help(self, event: AstrMessageEvent):
"""查看帮助"""
await self.help_c.help(event)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("llm")
async def llm(self, event: AstrMessageEvent):
"""开启/关闭 LLM"""
await self.llm_c.llm(event)
@filter.command_group("tool")
def tool(self):
pass
@tool.command("ls")
async def tool_ls(self, event: AstrMessageEvent):
"""查看函数工具列表"""
await self.tool_c.tool_ls(event)
@tool.command("on")
async def tool_on(self, event: AstrMessageEvent, tool_name: str):
"""启用一个函数工具"""
await self.tool_c.tool_on(event, tool_name)
@tool.command("off")
async def tool_off(self, event: AstrMessageEvent, tool_name: str):
"""停用一个函数工具"""
await self.tool_c.tool_off(event, tool_name)
@tool.command("off_all")
async def tool_all_off(self, event: AstrMessageEvent):
"""停用所有函数工具"""
await self.tool_c.tool_all_off(event)
@filter.command_group("plugin")
def plugin(self):
pass
@plugin.command("ls")
async def plugin_ls(self, event: AstrMessageEvent):
"""获取已经安装的插件列表。"""
await self.plugin_c.plugin_ls(event)
@filter.permission_type(filter.PermissionType.ADMIN)
@plugin.command("off")
async def plugin_off(self, event: AstrMessageEvent, plugin_name: str = ""):
"""禁用插件"""
await self.plugin_c.plugin_off(event, plugin_name)
@filter.permission_type(filter.PermissionType.ADMIN)
@plugin.command("on")
async def plugin_on(self, event: AstrMessageEvent, plugin_name: str = ""):
"""启用插件"""
await self.plugin_c.plugin_on(event, plugin_name)
@filter.permission_type(filter.PermissionType.ADMIN)
@plugin.command("get")
async def plugin_get(self, event: AstrMessageEvent, plugin_repo: str = ""):
"""安装插件"""
await self.plugin_c.plugin_get(event, plugin_repo)
@plugin.command("help")
async def plugin_help(self, event: AstrMessageEvent, plugin_name: str = ""):
"""获取插件帮助"""
await self.plugin_c.plugin_help(event, plugin_name)
@filter.command("t2i")
async def t2i(self, event: AstrMessageEvent):
"""开关文本转图片"""
await self.t2i_c.t2i(event)
@filter.command("tts")
async def tts(self, event: AstrMessageEvent):
"""开关文本转语音(会话级别)"""
await self.tts_c.tts(event)
@filter.command("sid")
async def sid(self, event: AstrMessageEvent):
"""获取会话 ID 和 管理员 ID"""
await self.sid_c.sid(event)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("op")
async def op(self, event: AstrMessageEvent, admin_id: str = ""):
"""授权管理员。op <admin_id>"""
await self.admin_c.op(event, admin_id)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("deop")
async def deop(self, event: AstrMessageEvent, admin_id: str):
"""取消授权管理员。deop <admin_id>"""
await self.admin_c.deop(event, admin_id)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("wl")
async def wl(self, event: AstrMessageEvent, sid: str = ""):
"""添加白名单。wl <sid>"""
await self.admin_c.wl(event, sid)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("dwl")
async def dwl(self, event: AstrMessageEvent, sid: str):
"""删除白名单。dwl <sid>"""
await self.admin_c.dwl(event, sid)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("provider")
async def provider(
self,
event: AstrMessageEvent,
idx: str | int | None = None,
idx2: int | None = None,
):
"""查看或者切换 LLM Provider"""
await self.provider_c.provider(event, idx, idx2)
@filter.command("reset")
async def reset(self, message: AstrMessageEvent):
"""重置 LLM 会话"""
await self.conversation_c.reset(message)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("model")
async def model_ls(
self,
message: AstrMessageEvent,
idx_or_name: int | str | None = None,
):
"""查看或者切换模型"""
await self.provider_c.model_ls(message, idx_or_name)
@filter.command("history")
async def his(self, message: AstrMessageEvent, page: int = 1):
"""查看对话记录"""
await self.conversation_c.his(message, page)
@filter.command("ls")
async def convs(self, message: AstrMessageEvent, page: int = 1):
"""查看对话列表"""
await self.conversation_c.convs(message, page)
@filter.command("new")
async def new_conv(self, message: AstrMessageEvent):
"""创建新对话"""
await self.conversation_c.new_conv(message)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("groupnew")
async def groupnew_conv(self, message: AstrMessageEvent, sid: str):
"""创建新群聊对话"""
await self.conversation_c.groupnew_conv(message, sid)
@filter.command("switch")
async def switch_conv(self, message: AstrMessageEvent, index: int | None = None):
"""通过 /ls 前面的序号切换对话"""
await self.conversation_c.switch_conv(message, index)
@filter.command("rename")
async def rename_conv(self, message: AstrMessageEvent, new_name: str):
"""重命名对话"""
await self.conversation_c.rename_conv(message, new_name)
@filter.command("del")
async def del_conv(self, message: AstrMessageEvent):
"""删除当前对话"""
await self.conversation_c.del_conv(message)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("key")
async def key(self, message: AstrMessageEvent, index: int | None = None):
"""查看或者切换 Key"""
await self.provider_c.key(message, index)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("persona")
async def persona(self, message: AstrMessageEvent):
"""查看或者切换 Persona"""
await self.persona_c.persona(message)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("dashboard_update")
async def update_dashboard(self, event: AstrMessageEvent):
await self.admin_c.update_dashboard(event)
@filter.command("set")
async def set_variable(self, event: AstrMessageEvent, key: str, value: str):
await self.setunset_c.set_variable(event, key, value)
@filter.command("unset")
async def unset_variable(self, event: AstrMessageEvent, key: str):
await self.setunset_c.unset_variable(event, key)
@filter.permission_type(filter.PermissionType.ADMIN)
@filter.command("alter_cmd", alias={"alter"})
async def alter_cmd(self, event: AstrMessageEvent):
"""修改命令权限"""
await self.alter_cmd_c.alter_cmd(event)
+4
View File
@@ -0,0 +1,4 @@
name: builtin_commands
desc: AstrBot 自带指令,提供常用的对话管理、工具使用、插件管理等功能。
author: Soulter
version: 0.0.1