方便消息格式检查的property

pull/1496/head
UnCLAS-Prommer 2026-02-14 23:52:58 +08:00
parent dc36542403
commit 6db889580d
No known key found for this signature in database
2 changed files with 74 additions and 15 deletions

View File

@ -13,6 +13,12 @@ logger = get_logger("base_message_component_model")
class BaseMessageComponentModel(ABC):
@property
@abstractmethod
def format_name(self) -> str:
"""消息组件的格式名称,用于标识该组件的类型"""
raise NotImplementedError
@abstractmethod
async def to_seg(self) -> Seg:
"""将消息组件转换为 maim_message.Seg 对象"""
@ -34,6 +40,11 @@ class ByteComponent:
class TextComponent(BaseMessageComponentModel):
"""文本组件,包含一个文本消息的内容"""
@property
def format_name(self) -> str:
return "text"
def __init__(self, text: str):
self.text = text
assert isinstance(text, str), "TextComponent 的 text 必须是字符串类型"
@ -44,6 +55,11 @@ class TextComponent(BaseMessageComponentModel):
class ImageComponent(BaseMessageComponentModel, ByteComponent):
"""图片组件,包含一个图片消息的二进制数据和一个唯一标识该图片消息的 hash 值"""
@property
def format_name(self) -> str:
return "image"
async def load_image_binary(self):
if not self.binary_data:
raise NotImplementedError
@ -56,6 +72,11 @@ class ImageComponent(BaseMessageComponentModel, ByteComponent):
class EmojiComponent(BaseMessageComponentModel, ByteComponent):
"""表情组件,包含一个表情消息的二进制数据和一个唯一标识该表情消息的 hash 值"""
@property
def format_name(self) -> str:
return "emoji"
async def load_emoji_binary(self) -> None:
"""
加载表情的二进制数据如果 binary_data 为空则通过 emoji_hash 从表情管理器加载
@ -85,6 +106,11 @@ class EmojiComponent(BaseMessageComponentModel, ByteComponent):
class VoiceComponent(BaseMessageComponentModel, ByteComponent):
"""语音组件,包含一个语音消息的二进制数据和一个唯一标识该语音消息的 hash 值"""
@property
def format_name(self) -> str:
return "voice"
async def load_voice_binary(self) -> None:
if not self.binary_data:
from src.common.utils.utils_file import FileUtils
@ -103,6 +129,11 @@ class VoiceComponent(BaseMessageComponentModel, ByteComponent):
class AtComponent(BaseMessageComponentModel):
"""@组件,包含一个被@的用户的ID用于表示该组件是一个@某人的消息片段"""
@property
def format_name(self) -> str:
return "at"
def __init__(self, target_user_id: str) -> None:
self.target_user_id = target_user_id
"""目标用户ID"""
@ -114,6 +145,11 @@ class AtComponent(BaseMessageComponentModel):
class ReplyComponent(BaseMessageComponentModel):
"""回复组件,包含一个回复消息的 ID用于表示该组件是对哪条消息的回复"""
@property
def format_name(self) -> str:
return "reply"
def __init__(self, target_message_id: str) -> None:
assert isinstance(target_message_id, str), "ReplyComponent 的 target_message_id 必须是字符串类型"
self.target_message_id = target_message_id
@ -125,6 +161,11 @@ class ReplyComponent(BaseMessageComponentModel):
class ForwardNodeComponent(BaseMessageComponentModel):
"""转发节点消息组件,包含一个转发节点的消息,所有组件按照消息顺序排列"""
@property
def format_name(self) -> str:
return "forward_node"
def __init__(self, forward_components: List["ForwardComponent"]):
self.forward_components = forward_components
"""节点的消息组件列表,按照消息顺序排列"""
@ -165,6 +206,11 @@ StandardMessageComponents = Union[
class ForwardComponent(BaseMessageComponentModel):
"""转发组件,包含一个转发消息中的一个节点的信息,包括发送者信息和该节点的消息内容"""
@property
def format_name(self) -> str:
return "forward"
def __init__(
self,
user_nickname: str,

View File

@ -18,15 +18,16 @@ class ExampleConfig(ConfigBase):
class BotConfig(ConfigBase):
"""机器人配置类"""
platform: str = ""
"""平台"""
qq_account: int = 0
"""QQ账号"""
platforms: list[str] = Field(default_factory=lambda: [])
"""其他平台"""
nickname: str = "麦麦"
"""机器人昵称"""
@ -49,11 +50,13 @@ class PersonalityConfig(ConfigBase):
multiple_probability: float = 0.3
"""每次构建回复时,从 multiple_reply_style 中随机替换 reply_style 的概率0.0-1.0"""
plan_style: str = """1.思考**所有**的可用的action中的**每个动作**是否符合当下条件,如果动作使用条件符合聊天内容就使用
2.如果相同的action已经被执行请不要重复执行该action
3.如果有人对你感到厌烦请减少回复
4.如果有人在追问你或者话题没有说完请你继续回复
5.请分析哪些对话是和你说的哪些是其他人之间的互动不要误认为其他人之间的互动是和你说的"""
plan_style: str = (
"1.思考**所有**的可用的action中的**每个动作**是否符合当下条件,如果动作使用条件符合聊天内容就使用"
"2.如果相同的action已经被执行请不要重复执行该action"
"3.如果有人对你感到厌烦,请减少回复"
"4.如果有人在追问你,或者话题没有说完,请你继续回复"
"5.请分析哪些对话是和你说的,哪些是其他人之间的互动,不要误认为其他人之间的互动是和你说的"
)
"""_wrap_麦麦的说话规则和行为规则"""
visual_style: str = "请用中文描述这张图片的内容。如果有文字请把文字描述概括出来请留意其主题直观感受输出为一段平文本最多30字请注意不要分点就输出一段文本"
@ -142,6 +145,12 @@ class ChatConfig(ConfigBase):
class MessageReceiveConfig(ConfigBase):
"""消息接收配置类"""
image_parse_threshold: int = 5
"""
当消息中图片数量不超过此阈值时启用图片解析功能将图片内容解析为文本后再进行处理
当消息中图片数量超过此阈值时为了避免过度解析导致的性能问题将跳过图片解析直接进行处理
"""
ban_words: set[str] = Field(default_factory=lambda: set())
"""过滤词列表"""
@ -497,10 +506,11 @@ class ExtraPromptItem(ConfigBase):
class ExperimentalConfig(ConfigBase):
"""实验功能配置类"""
private_plan_style: str = """
1.思考**所有**的可用的action中的**每个动作**是否符合当下条件如果动作使用条件符合聊天内容就使用
2.如果相同的内容已经被执行请不要重复执行
3.某句话如果已经被回复过不要重复回复"""
private_plan_style: str = (
"1.思考**所有**的可用的action中的**每个动作**是否符合当下条件,如果动作使用条件符合聊天内容就使用"
"2.如果相同的内容已经被执行,请不要重复执行"
"3.某句话如果已经被回复过,不要重复回复"
)
"""_wrap_私聊说话规则行为风格实验性功能"""
chat_prompts: list[ExtraPromptItem] = Field(default_factory=lambda: [])
@ -512,9 +522,10 @@ class ExperimentalConfig(ConfigBase):
class MaimMessageConfig(ConfigBase):
"""maim_message配置类"""
ws_server_host: str = "127.0.0.1"
"""旧版基于WS的服务器主机地址"""
ws_server_port: int = 8080
"""旧版基于WS的服务器端口号"""
@ -628,6 +639,7 @@ class DreamConfig(ConfigBase):
raise ValueError(f"first_delay_seconds 不能为负数,当前值: {self.first_delay_seconds}")
return super().model_post_init(context)
class WebUIConfig(ConfigBase):
"""WebUI配置类"""
@ -655,13 +667,14 @@ class WebUIConfig(ConfigBase):
enable_paragraph_content: bool = False
"""是否在知识图谱中加载段落完整内容需要加载embedding store会占用额外内存"""
class DatabaseConfig(ConfigBase):
"""数据库配置类"""
save_binary_data: bool = False
"""
是否将消息中的二进制数据保存为独立文件
若启用消息中的语音等二进制数据将会保存为独立文件并在消息中以特殊标记替代启用会导致数据文件夹体积增大但可以实现二次识别等功能
若禁用则消息中的二进制将会在识别后删除并在消息中使用识别结果替代无法二次识别
该配置项仅影响新存储的消息已有消息不会受到影响
"""
"""