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>
354 lines
12 KiB
Python
354 lines
12 KiB
Python
import uuid
|
|
from dataclasses import dataclass, field
|
|
from datetime import datetime, timezone
|
|
from typing import TypedDict
|
|
|
|
from sqlmodel import JSON, Field, SQLModel, Text, UniqueConstraint
|
|
|
|
|
|
class PlatformStat(SQLModel, table=True):
|
|
"""This class represents the statistics of bot usage across different platforms.
|
|
|
|
Note: In astrbot v4, we moved `platform` table to here.
|
|
"""
|
|
|
|
__tablename__: str = "platform_stats"
|
|
|
|
id: int = Field(primary_key=True, sa_column_kwargs={"autoincrement": True})
|
|
timestamp: datetime = Field(nullable=False)
|
|
platform_id: str = Field(nullable=False)
|
|
platform_type: str = Field(nullable=False) # such as "aiocqhttp", "slack", etc.
|
|
count: int = Field(default=0, nullable=False)
|
|
|
|
__table_args__ = (
|
|
UniqueConstraint(
|
|
"timestamp",
|
|
"platform_id",
|
|
"platform_type",
|
|
name="uix_platform_stats",
|
|
),
|
|
)
|
|
|
|
|
|
class ConversationV2(SQLModel, table=True):
|
|
__tablename__: str = "conversations"
|
|
|
|
inner_conversation_id: int | None = Field(
|
|
default=None,
|
|
primary_key=True,
|
|
sa_column_kwargs={"autoincrement": True},
|
|
)
|
|
conversation_id: str = Field(
|
|
max_length=36,
|
|
nullable=False,
|
|
unique=True,
|
|
default_factory=lambda: str(uuid.uuid4()),
|
|
)
|
|
platform_id: str = Field(nullable=False)
|
|
user_id: str = Field(nullable=False)
|
|
content: list | None = Field(default=None, sa_type=JSON)
|
|
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
|
updated_at: datetime = Field(
|
|
default_factory=lambda: datetime.now(timezone.utc),
|
|
sa_column_kwargs={"onupdate": datetime.now(timezone.utc)},
|
|
)
|
|
title: str | None = Field(default=None, max_length=255)
|
|
persona_id: str | None = Field(default=None)
|
|
|
|
__table_args__ = (
|
|
UniqueConstraint(
|
|
"conversation_id",
|
|
name="uix_conversation_id",
|
|
),
|
|
)
|
|
|
|
|
|
class Persona(SQLModel, table=True):
|
|
"""Persona is a set of instructions for LLMs to follow.
|
|
|
|
It can be used to customize the behavior of LLMs.
|
|
"""
|
|
|
|
__tablename__: str = "personas"
|
|
|
|
id: int | None = Field(
|
|
primary_key=True,
|
|
sa_column_kwargs={"autoincrement": True},
|
|
default=None,
|
|
)
|
|
persona_id: str = Field(max_length=255, nullable=False)
|
|
system_prompt: str = Field(sa_type=Text, nullable=False)
|
|
begin_dialogs: list | None = Field(default=None, sa_type=JSON)
|
|
"""a list of strings, each representing a dialog to start with"""
|
|
tools: list | None = Field(default=None, sa_type=JSON)
|
|
"""None means use ALL tools for default, empty list means no tools, otherwise a list of tool names."""
|
|
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
|
updated_at: datetime = Field(
|
|
default_factory=lambda: datetime.now(timezone.utc),
|
|
sa_column_kwargs={"onupdate": datetime.now(timezone.utc)},
|
|
)
|
|
|
|
__table_args__ = (
|
|
UniqueConstraint(
|
|
"persona_id",
|
|
name="uix_persona_id",
|
|
),
|
|
)
|
|
|
|
|
|
class Preference(SQLModel, table=True):
|
|
"""This class represents preferences for bots."""
|
|
|
|
__tablename__: str = "preferences"
|
|
|
|
id: int | None = Field(
|
|
default=None,
|
|
primary_key=True,
|
|
sa_column_kwargs={"autoincrement": True},
|
|
)
|
|
scope: str = Field(nullable=False)
|
|
"""Scope of the preference, such as 'global', 'umo', 'plugin'."""
|
|
scope_id: str = Field(nullable=False)
|
|
"""ID of the scope, such as 'global', 'umo', 'plugin_name'."""
|
|
key: str = Field(nullable=False)
|
|
value: dict = Field(sa_type=JSON, nullable=False)
|
|
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
|
updated_at: datetime = Field(
|
|
default_factory=lambda: datetime.now(timezone.utc),
|
|
sa_column_kwargs={"onupdate": datetime.now(timezone.utc)},
|
|
)
|
|
|
|
__table_args__ = (
|
|
UniqueConstraint(
|
|
"scope",
|
|
"scope_id",
|
|
"key",
|
|
name="uix_preference_scope_scope_id_key",
|
|
),
|
|
)
|
|
|
|
|
|
class PlatformMessageHistory(SQLModel, table=True):
|
|
"""This class represents the message history for a specific platform.
|
|
|
|
It is used to store messages that are not LLM-generated, such as user messages
|
|
or platform-specific messages.
|
|
"""
|
|
|
|
__tablename__: str = "platform_message_history"
|
|
|
|
id: int | None = Field(
|
|
primary_key=True,
|
|
sa_column_kwargs={"autoincrement": True},
|
|
default=None,
|
|
)
|
|
platform_id: str = Field(nullable=False)
|
|
user_id: str = Field(nullable=False) # An id of group, user in platform
|
|
sender_id: str | None = Field(default=None) # ID of the sender in the platform
|
|
sender_name: str | None = Field(
|
|
default=None,
|
|
) # Name of the sender in the platform
|
|
content: dict = Field(sa_type=JSON, nullable=False) # a message chain list
|
|
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
|
updated_at: datetime = Field(
|
|
default_factory=lambda: datetime.now(timezone.utc),
|
|
sa_column_kwargs={"onupdate": datetime.now(timezone.utc)},
|
|
)
|
|
|
|
|
|
class PlatformSession(SQLModel, table=True):
|
|
"""Platform session table for managing user sessions across different platforms.
|
|
|
|
A session represents a chat window for a specific user on a specific platform.
|
|
Each session can have multiple conversations (对话) associated with it.
|
|
"""
|
|
|
|
__tablename__: str = "platform_sessions"
|
|
|
|
inner_id: int | None = Field(
|
|
primary_key=True,
|
|
sa_column_kwargs={"autoincrement": True},
|
|
default=None,
|
|
)
|
|
session_id: str = Field(
|
|
max_length=100,
|
|
nullable=False,
|
|
unique=True,
|
|
default_factory=lambda: str(uuid.uuid4()),
|
|
)
|
|
platform_id: str = Field(default="webchat", nullable=False)
|
|
"""Platform identifier (e.g., 'webchat', 'qq', 'discord')"""
|
|
creator: str = Field(nullable=False)
|
|
"""Username of the session creator"""
|
|
display_name: str | None = Field(default=None, max_length=255)
|
|
"""Display name for the session"""
|
|
is_group: int = Field(default=0, nullable=False)
|
|
"""0 for private chat, 1 for group chat (not implemented yet)"""
|
|
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
|
updated_at: datetime = Field(
|
|
default_factory=lambda: datetime.now(timezone.utc),
|
|
sa_column_kwargs={"onupdate": datetime.now(timezone.utc)},
|
|
)
|
|
|
|
__table_args__ = (
|
|
UniqueConstraint(
|
|
"session_id",
|
|
name="uix_platform_session_id",
|
|
),
|
|
)
|
|
|
|
|
|
class Attachment(SQLModel, table=True):
|
|
"""This class represents attachments for messages in AstrBot.
|
|
|
|
Attachments can be images, files, or other media types.
|
|
"""
|
|
|
|
__tablename__: str = "attachments"
|
|
|
|
inner_attachment_id: int | None = Field(
|
|
primary_key=True,
|
|
sa_column_kwargs={"autoincrement": True},
|
|
default=None,
|
|
)
|
|
attachment_id: str = Field(
|
|
max_length=36,
|
|
nullable=False,
|
|
unique=True,
|
|
default_factory=lambda: str(uuid.uuid4()),
|
|
)
|
|
path: str = Field(nullable=False) # Path to the file on disk
|
|
type: str = Field(nullable=False) # Type of the file (e.g., 'image', 'file')
|
|
mime_type: str = Field(nullable=False) # MIME type of the file
|
|
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
|
updated_at: datetime = Field(
|
|
default_factory=lambda: datetime.now(timezone.utc),
|
|
sa_column_kwargs={"onupdate": datetime.now(timezone.utc)},
|
|
)
|
|
|
|
__table_args__ = (
|
|
UniqueConstraint(
|
|
"attachment_id",
|
|
name="uix_attachment_id",
|
|
),
|
|
)
|
|
|
|
|
|
class CommandConfig(SQLModel, table=True):
|
|
"""Per-command configuration overrides for dashboard management."""
|
|
|
|
__tablename__ = "command_configs" # type: ignore
|
|
|
|
handler_full_name: str = Field(
|
|
primary_key=True,
|
|
max_length=512,
|
|
)
|
|
plugin_name: str = Field(nullable=False, max_length=255)
|
|
module_path: str = Field(nullable=False, max_length=255)
|
|
original_command: str = Field(nullable=False, max_length=255)
|
|
resolved_command: str | None = Field(default=None, max_length=255)
|
|
enabled: bool = Field(default=True, nullable=False)
|
|
keep_original_alias: bool = Field(default=False, nullable=False)
|
|
conflict_key: str | None = Field(default=None, max_length=255)
|
|
resolution_strategy: str | None = Field(default=None, max_length=64)
|
|
note: str | None = Field(default=None, sa_type=Text)
|
|
extra_data: dict | None = Field(default=None, sa_type=JSON)
|
|
auto_managed: bool = Field(default=False, nullable=False)
|
|
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
|
updated_at: datetime = Field(
|
|
default_factory=lambda: datetime.now(timezone.utc),
|
|
sa_column_kwargs={"onupdate": datetime.now(timezone.utc)},
|
|
)
|
|
|
|
|
|
class CommandConflict(SQLModel, table=True):
|
|
"""Conflict tracking for duplicated command names."""
|
|
|
|
__tablename__ = "command_conflicts" # type: ignore
|
|
|
|
id: int | None = Field(
|
|
default=None, primary_key=True, sa_column_kwargs={"autoincrement": True}
|
|
)
|
|
conflict_key: str = Field(nullable=False, max_length=255)
|
|
handler_full_name: str = Field(nullable=False, max_length=512)
|
|
plugin_name: str = Field(nullable=False, max_length=255)
|
|
status: str = Field(default="pending", max_length=32)
|
|
resolution: str | None = Field(default=None, max_length=64)
|
|
resolved_command: str | None = Field(default=None, max_length=255)
|
|
note: str | None = Field(default=None, sa_type=Text)
|
|
extra_data: dict | None = Field(default=None, sa_type=JSON)
|
|
auto_generated: bool = Field(default=False, nullable=False)
|
|
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
|
updated_at: datetime = Field(
|
|
default_factory=lambda: datetime.now(timezone.utc),
|
|
sa_column_kwargs={"onupdate": datetime.now(timezone.utc)},
|
|
)
|
|
|
|
__table_args__ = (
|
|
UniqueConstraint(
|
|
"conflict_key",
|
|
"handler_full_name",
|
|
name="uix_conflict_handler",
|
|
),
|
|
)
|
|
|
|
|
|
@dataclass
|
|
class Conversation:
|
|
"""LLM 对话类
|
|
|
|
对于 WebChat,history 存储了包括指令、回复、图片等在内的所有消息。
|
|
对于其他平台的聊天,不存储非 LLM 的回复(因为考虑到已经存储在各自的平台上)。
|
|
|
|
在 v4.0.0 版本及之后,WebChat 的历史记录被迁移至 `PlatformMessageHistory` 表中,
|
|
"""
|
|
|
|
platform_id: str
|
|
user_id: str
|
|
cid: str
|
|
"""对话 ID, 是 uuid 格式的字符串"""
|
|
history: str = ""
|
|
"""字符串格式的对话列表。"""
|
|
title: str | None = ""
|
|
persona_id: str | None = ""
|
|
created_at: int = 0
|
|
updated_at: int = 0
|
|
|
|
|
|
class Personality(TypedDict):
|
|
"""LLM 人格类。
|
|
|
|
在 v4.0.0 版本及之后,推荐使用上面的 Persona 类。并且, mood_imitation_dialogs 字段已被废弃。
|
|
"""
|
|
|
|
prompt: str
|
|
name: str
|
|
begin_dialogs: list[str]
|
|
mood_imitation_dialogs: list[str]
|
|
"""情感模拟对话预设。在 v4.0.0 版本及之后,已被废弃。"""
|
|
tools: list[str] | None
|
|
"""工具列表。None 表示使用所有工具,空列表表示不使用任何工具"""
|
|
|
|
# cache
|
|
_begin_dialogs_processed: list[dict]
|
|
_mood_imitation_dialogs_processed: str
|
|
|
|
|
|
# ====
|
|
# Deprecated, and will be removed in future versions.
|
|
# ====
|
|
|
|
|
|
@dataclass
|
|
class Platform:
|
|
"""平台使用统计数据"""
|
|
|
|
name: str
|
|
count: int
|
|
timestamp: int
|
|
|
|
|
|
@dataclass
|
|
class Stats:
|
|
platform: list[Platform] = field(default_factory=list)
|