80b89fd2ea
move mcp management to plugin managemanet page * feat: 新增命令配置数据库模型 * feat: 实现核心命令管理系统 * feat: 将命令管理集成到 Star 框架 * feat: 新增命令管理后台 API * feat: 新增命令管理界面页面 * feat: 新增命令管理国际化支持 * test: 新增命令管理相关测试 * refactor(command): 移除指令重命名时的别名功能 * fix(command): 修正指令冲突检测逻辑 * fix(command): 排除已禁用指令的冲突检测 - 只有 `effective_command` 存在且 `enabled` 为 `True` 的指令才会被纳入冲突检测范围。 * feat(command): 优化指令冲突显示与提示 - 【功能】新增指令冲突警告提示,当检测到冲突时显示详细信息及解决方案。 - 【优化】调整指令列表排序逻辑,将冲突指令优先显示并分组。 - 【样式】为冲突指令行添加专属高亮样式,提升视觉识别度。 - 【国际化】更新英文和中文多语言文件,增加指令冲突警告相关的翻译文本。 * chore(command-page): 禁用命令表格部分列的排序功能 * style(command-page): 调整命令页面表格样式和图标大小 * refactor(command): 优化指令页面布局并更新冲突警告 - 【布局优化】重新组织指令管理页面布局,将筛选器移至顶部独立行 - 【信息展示】将搜索栏与总指令数、已禁用指令数合并显示,提升页面空间利用率 - 【视觉更新】更新指令冲突警告样式 * style: UI 细节 * refactor(command): 调整指令管理中的成员权限显示与筛选 - 更新指令筛选逻辑,当选择“所有人”权限筛选时,将同时包含 `everyone` 和 `member` 权限的指令。 * feat(command-management): 新增指令层级管理与UI展示 - 【后端】 - `CommandDescriptor` 新增 `parent_group_handler` 和 `sub_commands` 字段,支持指令层级结构定义。 - `list_commands` 函数重构,实现指令的层级收集与构建,将子指令正确挂载到其父指令组下。 - 新增 `_collect_all_descriptors` 和 `_find_parent_group_handler` 辅助函数,用于全面收集指令并定位父指令组。 - `_build_descriptor` 优化指令类型判断逻辑,明确区分普通指令、指令组和子指令。 - `_descriptor_to_dict` 递归处理子指令,确保 API 返回完整的指令层级数据。 - 【前端】 - 指令管理页面 (`CommandPage.vue`) 增加指令类型筛选器,并支持指令组的展开/折叠功能。 - 表格展示优化,为指令组和子指令添加不同的样式和缩进,提升层级结构的视觉可读性。 - 指令详情对话框新增指令类型、所属指令组和子指令列表的展示。 - 更新 `CommandItem` 接口,以适配后端提供的层级数据结构。 - 【i18n】 - 新增指令类型(指令、指令组、子指令)的国际化文本。 - 更新指令管理相关 UI 文本,包括表格头部、详情对话框字段和筛选器选项。 * style(command): 优化指令组子指令数量显示UI * refactor(command): 修改指令列表排序逻辑 * style(command-page): 优化命令列表UI * feat(command): 添加系统插件指令过滤与冲突处理 * refactor(command): 更新指令数展示逻辑 * style(command): 更新空状态描述 * feat(extension): 添加插件指令冲突检测与提示 - 在插件安装或启用后,自动检测并提示指令冲突。 - 当检测到指令冲突时,显示警告对话框,告知用户冲突数量及可能的影响。 * refactor(command): 移除指令表格内部加载指示器 * style(extension): 文案修改 * refactor(command): 模块化指令管理面板前端代码 * refactor(commandPanel): 重命名指令模块目录为 commandPanel * style(commandPanel): 微调指令面板UI * fix(command): 确保新命令配置的事务提交 * fix(sidebar): 补全新增侧边栏项后的侧边栏位追加逻辑 * refactor(commands): 重构/help指令以动态显示实际命令并补充部分命令描述 * style(builtin_commands): 补充命令描述 * refactor(commandPanel): 移除未使用的 filterState 常量 * perf(dashboard): 删除多余的CommandPage.vue文件(已被模块化引用) * perf(command): 优化命令冲突计数逻辑 * perf(command): 优化指令管理辅助函数和配置绑定逻辑 * perf(db): 优化重构command相关数据库操作 * refactor(sidebar): 提取侧边栏项目解析逻辑到工具函数复用 * refactor: move mcp and command page to extension page * refactor: remove unused imports in component panel * fix: update terminology for handler management in extension localization --------- Co-authored-by: Soulter <905617992@qq.com>
465 lines
13 KiB
Python
465 lines
13 KiB
Python
import abc
|
|
import datetime
|
|
import typing as T
|
|
from contextlib import asynccontextmanager
|
|
from dataclasses import dataclass
|
|
|
|
from deprecated import deprecated
|
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
|
|
|
from astrbot.core.db.po import (
|
|
Attachment,
|
|
CommandConfig,
|
|
CommandConflict,
|
|
ConversationV2,
|
|
Persona,
|
|
PlatformMessageHistory,
|
|
PlatformSession,
|
|
PlatformStat,
|
|
Preference,
|
|
Stats,
|
|
)
|
|
|
|
|
|
@dataclass
|
|
class BaseDatabase(abc.ABC):
|
|
"""数据库基类"""
|
|
|
|
DATABASE_URL = ""
|
|
|
|
def __init__(self) -> None:
|
|
self.engine = create_async_engine(
|
|
self.DATABASE_URL,
|
|
echo=False,
|
|
future=True,
|
|
)
|
|
self.AsyncSessionLocal = async_sessionmaker(
|
|
self.engine,
|
|
class_=AsyncSession,
|
|
expire_on_commit=False,
|
|
)
|
|
|
|
async def initialize(self):
|
|
"""初始化数据库连接"""
|
|
|
|
@asynccontextmanager
|
|
async def get_db(self) -> T.AsyncGenerator[AsyncSession, None]:
|
|
"""Get a database session."""
|
|
if not self.inited:
|
|
await self.initialize()
|
|
self.inited = True
|
|
async with self.AsyncSessionLocal() as session:
|
|
yield session
|
|
|
|
@deprecated(version="4.0.0", reason="Use get_platform_stats instead")
|
|
@abc.abstractmethod
|
|
def get_base_stats(self, offset_sec: int = 86400) -> Stats:
|
|
"""获取基础统计数据"""
|
|
raise NotImplementedError
|
|
|
|
@deprecated(version="4.0.0", reason="Use get_platform_stats instead")
|
|
@abc.abstractmethod
|
|
def get_total_message_count(self) -> int:
|
|
"""获取总消息数"""
|
|
raise NotImplementedError
|
|
|
|
@deprecated(version="4.0.0", reason="Use get_platform_stats instead")
|
|
@abc.abstractmethod
|
|
def get_grouped_base_stats(self, offset_sec: int = 86400) -> Stats:
|
|
"""获取基础统计数据(合并)"""
|
|
raise NotImplementedError
|
|
|
|
# New methods in v4.0.0
|
|
|
|
@abc.abstractmethod
|
|
async def insert_platform_stats(
|
|
self,
|
|
platform_id: str,
|
|
platform_type: str,
|
|
count: int = 1,
|
|
timestamp: datetime.datetime | None = None,
|
|
) -> None:
|
|
"""Insert a new platform statistic record."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def count_platform_stats(self) -> int:
|
|
"""Count the number of platform statistics records."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def get_platform_stats(self, offset_sec: int = 86400) -> list[PlatformStat]:
|
|
"""Get platform statistics within the specified offset in seconds and group by platform_id."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def get_conversations(
|
|
self,
|
|
user_id: str | None = None,
|
|
platform_id: str | None = None,
|
|
) -> list[ConversationV2]:
|
|
"""Get all conversations for a specific user and platform_id(optional).
|
|
|
|
content is not included in the result.
|
|
"""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def get_conversation_by_id(self, cid: str) -> ConversationV2:
|
|
"""Get a specific conversation by its ID."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def get_all_conversations(
|
|
self,
|
|
page: int = 1,
|
|
page_size: int = 20,
|
|
) -> list[ConversationV2]:
|
|
"""Get all conversations with pagination."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def get_filtered_conversations(
|
|
self,
|
|
page: int = 1,
|
|
page_size: int = 20,
|
|
platform_ids: list[str] | None = None,
|
|
search_query: str = "",
|
|
**kwargs,
|
|
) -> tuple[list[ConversationV2], int]:
|
|
"""Get conversations filtered by platform IDs and search query."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def create_conversation(
|
|
self,
|
|
user_id: str,
|
|
platform_id: str,
|
|
content: list[dict] | None = None,
|
|
title: str | None = None,
|
|
persona_id: str | None = None,
|
|
cid: str | None = None,
|
|
created_at: datetime.datetime | None = None,
|
|
updated_at: datetime.datetime | None = None,
|
|
) -> ConversationV2:
|
|
"""Create a new conversation."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def update_conversation(
|
|
self,
|
|
cid: str,
|
|
title: str | None = None,
|
|
persona_id: str | None = None,
|
|
content: list[dict] | None = None,
|
|
) -> None:
|
|
"""Update a conversation's history."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def delete_conversation(self, cid: str) -> None:
|
|
"""Delete a conversation by its ID."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def delete_conversations_by_user_id(self, user_id: str) -> None:
|
|
"""Delete all conversations for a specific user."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def insert_platform_message_history(
|
|
self,
|
|
platform_id: str,
|
|
user_id: str,
|
|
content: dict,
|
|
sender_id: str | None = None,
|
|
sender_name: str | None = None,
|
|
) -> PlatformMessageHistory:
|
|
"""Insert a new platform message history record."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def delete_platform_message_offset(
|
|
self,
|
|
platform_id: str,
|
|
user_id: str,
|
|
offset_sec: int = 86400,
|
|
) -> None:
|
|
"""Delete platform message history records newer than the specified offset."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def get_platform_message_history(
|
|
self,
|
|
platform_id: str,
|
|
user_id: str,
|
|
page: int = 1,
|
|
page_size: int = 20,
|
|
) -> list[PlatformMessageHistory]:
|
|
"""Get platform message history for a specific user."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def get_platform_message_history_by_id(
|
|
self,
|
|
message_id: int,
|
|
) -> PlatformMessageHistory | None:
|
|
"""Get a platform message history record by its ID."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def insert_attachment(
|
|
self,
|
|
path: str,
|
|
type: str,
|
|
mime_type: str,
|
|
):
|
|
"""Insert a new attachment record."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def get_attachment_by_id(self, attachment_id: str) -> Attachment:
|
|
"""Get an attachment by its ID."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def get_attachments(self, attachment_ids: list[str]) -> list[Attachment]:
|
|
"""Get multiple attachments by their IDs."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def delete_attachment(self, attachment_id: str) -> bool:
|
|
"""Delete an attachment by its ID.
|
|
|
|
Returns True if the attachment was deleted, False if it was not found.
|
|
"""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def delete_attachments(self, attachment_ids: list[str]) -> int:
|
|
"""Delete multiple attachments by their IDs.
|
|
|
|
Returns the number of attachments deleted.
|
|
"""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def insert_persona(
|
|
self,
|
|
persona_id: str,
|
|
system_prompt: str,
|
|
begin_dialogs: list[str] | None = None,
|
|
tools: list[str] | None = None,
|
|
) -> Persona:
|
|
"""Insert a new persona record."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def get_persona_by_id(self, persona_id: str) -> Persona:
|
|
"""Get a persona by its ID."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def get_personas(self) -> list[Persona]:
|
|
"""Get all personas for a specific bot."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def update_persona(
|
|
self,
|
|
persona_id: str,
|
|
system_prompt: str | None = None,
|
|
begin_dialogs: list[str] | None = None,
|
|
tools: list[str] | None = None,
|
|
) -> Persona | None:
|
|
"""Update a persona's system prompt or begin dialogs."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def delete_persona(self, persona_id: str) -> None:
|
|
"""Delete a persona by its ID."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def insert_preference_or_update(
|
|
self,
|
|
scope: str,
|
|
scope_id: str,
|
|
key: str,
|
|
value: dict,
|
|
) -> Preference:
|
|
"""Insert a new preference record."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def get_preference(self, scope: str, scope_id: str, key: str) -> Preference:
|
|
"""Get a preference by scope ID and key."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def get_preferences(
|
|
self,
|
|
scope: str,
|
|
scope_id: str | None = None,
|
|
key: str | None = None,
|
|
) -> list[Preference]:
|
|
"""Get all preferences for a specific scope ID or key."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def remove_preference(self, scope: str, scope_id: str, key: str) -> None:
|
|
"""Remove a preference by scope ID and key."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def clear_preferences(self, scope: str, scope_id: str) -> None:
|
|
"""Clear all preferences for a specific scope ID."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def get_command_configs(self) -> list[CommandConfig]:
|
|
"""Get all stored command configurations."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def get_command_config(self, handler_full_name: str) -> CommandConfig | None:
|
|
"""Fetch a single command configuration by handler."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def upsert_command_config(
|
|
self,
|
|
handler_full_name: str,
|
|
plugin_name: str,
|
|
module_path: str,
|
|
original_command: str,
|
|
*,
|
|
resolved_command: str | None = None,
|
|
enabled: bool | None = None,
|
|
keep_original_alias: bool | None = None,
|
|
conflict_key: str | None = None,
|
|
resolution_strategy: str | None = None,
|
|
note: str | None = None,
|
|
extra_data: dict | None = None,
|
|
auto_managed: bool | None = None,
|
|
) -> CommandConfig:
|
|
"""Create or update a command configuration."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def delete_command_config(self, handler_full_name: str) -> None:
|
|
"""Delete a single command configuration."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def delete_command_configs(self, handler_full_names: list[str]) -> None:
|
|
"""Bulk delete command configurations."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def list_command_conflicts(
|
|
self,
|
|
status: str | None = None,
|
|
) -> list[CommandConflict]:
|
|
"""List recorded command conflict entries."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def upsert_command_conflict(
|
|
self,
|
|
conflict_key: str,
|
|
handler_full_name: str,
|
|
plugin_name: str,
|
|
*,
|
|
status: str | None = None,
|
|
resolution: str | None = None,
|
|
resolved_command: str | None = None,
|
|
note: str | None = None,
|
|
extra_data: dict | None = None,
|
|
auto_generated: bool | None = None,
|
|
) -> CommandConflict:
|
|
"""Create or update a conflict record."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def delete_command_conflicts(self, ids: list[int]) -> None:
|
|
"""Delete conflict records."""
|
|
...
|
|
|
|
# @abc.abstractmethod
|
|
# async def insert_llm_message(
|
|
# self,
|
|
# cid: str,
|
|
# role: str,
|
|
# content: list,
|
|
# tool_calls: list = None,
|
|
# tool_call_id: str = None,
|
|
# parent_id: str = None,
|
|
# ) -> LLMMessage:
|
|
# """Insert a new LLM message into the conversation."""
|
|
# ...
|
|
|
|
# @abc.abstractmethod
|
|
# async def get_llm_messages(self, cid: str) -> list[LLMMessage]:
|
|
# """Get all LLM messages for a specific conversation."""
|
|
# ...
|
|
|
|
@abc.abstractmethod
|
|
async def get_session_conversations(
|
|
self,
|
|
page: int = 1,
|
|
page_size: int = 20,
|
|
search_query: str | None = None,
|
|
platform: str | None = None,
|
|
) -> tuple[list[dict], int]:
|
|
"""Get paginated session conversations with joined conversation and persona details, support search and platform filter."""
|
|
...
|
|
|
|
# ====
|
|
# Platform Session Management
|
|
# ====
|
|
|
|
@abc.abstractmethod
|
|
async def create_platform_session(
|
|
self,
|
|
creator: str,
|
|
platform_id: str = "webchat",
|
|
session_id: str | None = None,
|
|
display_name: str | None = None,
|
|
is_group: int = 0,
|
|
) -> PlatformSession:
|
|
"""Create a new Platform session."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def get_platform_session_by_id(
|
|
self, session_id: str
|
|
) -> PlatformSession | None:
|
|
"""Get a Platform session by its ID."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def get_platform_sessions_by_creator(
|
|
self,
|
|
creator: str,
|
|
platform_id: str | None = None,
|
|
page: int = 1,
|
|
page_size: int = 20,
|
|
) -> list[PlatformSession]:
|
|
"""Get all Platform sessions for a specific creator (username) and optionally platform."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def update_platform_session(
|
|
self,
|
|
session_id: str,
|
|
display_name: str | None = None,
|
|
) -> None:
|
|
"""Update a Platform session's updated_at timestamp and optionally display_name."""
|
|
...
|
|
|
|
@abc.abstractmethod
|
|
async def delete_platform_session(self, session_id: str) -> None:
|
|
"""Delete a Platform session by its ID."""
|
|
...
|