合并转发构建与发送

pull/1235/head
UnCLAS-Prommer 2025-09-13 11:11:31 +08:00
parent bfe4943b18
commit 27e4d2b803
No known key found for this signature in database
4 changed files with 268 additions and 127 deletions

View File

@ -368,37 +368,37 @@ class DefaultReplyer:
return f"{expression_habits_title}\n{expression_habits_block}", selected_ids
async def build_memory_block(self, chat_history: List[DatabaseMessages], target: str) -> str:
"""构建记忆块
# async def build_memory_block(self, chat_history: List[DatabaseMessages], target: str) -> str:
# """构建记忆块
Args:
chat_history: 聊天历史记录
target: 目标消息内容
# Args:
# chat_history: 聊天历史记录
# target: 目标消息内容
Returns:
str: 记忆信息字符串
"""
# Returns:
# str: 记忆信息字符串
# """
if not global_config.memory.enable_memory:
return ""
# if not global_config.memory.enable_memory:
# return ""
instant_memory = None
# instant_memory = None
running_memories = await self.memory_activator.activate_memory_with_chat_history(
target_message=target, chat_history=chat_history
)
if not running_memories:
return ""
# running_memories = await self.memory_activator.activate_memory_with_chat_history(
# target_message=target, chat_history=chat_history
# )
# if not running_memories:
# return ""
memory_str = "以下是当前在聊天中,你回忆起的记忆:\n"
for running_memory in running_memories:
keywords, content = running_memory
memory_str += f"- {keywords}{content}\n"
# memory_str = "以下是当前在聊天中,你回忆起的记忆:\n"
# for running_memory in running_memories:
# keywords, content = running_memory
# memory_str += f"- {keywords}{content}\n"
if instant_memory:
memory_str += f"- {instant_memory}\n"
# if instant_memory:
# memory_str += f"- {instant_memory}\n"
return memory_str
# return memory_str
async def build_tool_info(self, chat_history: str, sender: str, target: str, enable_tool: bool = True) -> str:
"""构建工具信息块

View File

@ -1,4 +1,4 @@
from typing import Optional, TYPE_CHECKING, List, Tuple, Union, Dict
from typing import Optional, TYPE_CHECKING, List, Tuple, Union, Dict, Any
from dataclasses import dataclass, field
from enum import Enum
@ -50,10 +50,65 @@ class ReplyContentType(Enum):
return self.value
@dataclass
class ForwardNode(BaseDataModel):
user_id: Optional[str] = None
user_nickname: Optional[str] = None
content: Union[List["ReplyContent"], str] = field(default_factory=list)
@classmethod
def construct_as_id_reference(cls, message_id: str) -> "ForwardNode":
return cls(user_id="", user_nickname="", content=message_id)
@classmethod
def construct_as_created_node(
cls, user_id: str, user_nickname: str, content: List["ReplyContent"]
) -> "ForwardNode":
return cls(user_id=user_id, user_nickname=user_nickname, content=content)
@dataclass
class ReplyContent(BaseDataModel):
content_type: ReplyContentType | str
content: Union[str, Dict, List["ReplyContent"]] # 支持嵌套的 ReplyContent
content: Union[str, Dict, List[ForwardNode], List["ReplyContent"]] # 支持嵌套的 ReplyContent
@classmethod
def construct_as_text(cls, text: str):
return cls(content_type=ReplyContentType.TEXT, content=text)
@classmethod
def construct_as_image(cls, image_base64: str):
return cls(content_type=ReplyContentType.IMAGE, content=image_base64)
@classmethod
def construct_as_voice(cls, voice_base64: str):
return cls(content_type=ReplyContentType.VOICE, content=voice_base64)
@classmethod
def construct_as_emoji(cls, emoji_str: str):
return cls(content_type=ReplyContentType.EMOJI, content=emoji_str)
@classmethod
def construct_as_command(cls, command_arg: Dict):
return cls(content_type=ReplyContentType.COMMAND, content=command_arg)
@classmethod
def construct_as_hybrid(cls, hybrid_content: List[Tuple[ReplyContentType | str, str]]):
hybrid_content_list: List[ReplyContent] = []
for content_type, content in hybrid_content:
assert content_type not in [
ReplyContentType.HYBRID,
ReplyContentType.FORWARD,
ReplyContentType.VOICE,
ReplyContentType.COMMAND,
], "混合内容的每个项不能是混合、转发、语音或命令类型"
assert isinstance(content, str), "混合内容的每个项必须是字符串"
hybrid_content_list.append(ReplyContent(content_type=content_type, content=content))
return cls(content_type=ReplyContentType.HYBRID, content=hybrid_content_list)
@classmethod
def construct_as_forward(cls, forward_nodes: List[ForwardNode]):
return cls(content_type=ReplyContentType.FORWARD, content=forward_nodes)
def __post_init__(self):
if isinstance(self.content_type, ReplyContentType):
@ -82,36 +137,70 @@ class ReplySetModel(BaseDataModel):
return len(self.reply_data)
def add_text_content(self, text: str):
"""添加文本内容"""
"""
添加文本内容
Args:
text: 文本内容
"""
self.reply_data.append(ReplyContent(content_type=ReplyContentType.TEXT, content=text))
def add_image_content(self, image_base64: str):
"""添加图片内容base64编码的图片数据"""
"""
添加图片内容base64编码的图片数据
Args:
image_base64: base64编码的图片数据
"""
self.reply_data.append(ReplyContent(content_type=ReplyContentType.IMAGE, content=image_base64))
def add_voice_content(self, voice_base64: str):
"""添加语音内容base64编码的音频数据"""
"""
添加语音内容base64编码的音频数据
Args:
voice_base64: base64编码的音频数据
"""
self.reply_data.append(ReplyContent(content_type=ReplyContentType.VOICE, content=voice_base64))
def add_hybrid_content(self, hybrid_content: List[Tuple[ReplyContentType, str]]):
def add_hybrid_content_by_raw(self, hybrid_content: List[Tuple[ReplyContentType | str, str]]):
"""
添加混合型内容可以包含多种类型的内容
实际解析时只关注最外层没有递归嵌套处理
添加混合型内容可以包含text, image, emoji的任意组合
Args:
hybrid_content: 元组 (类型, 消息内容) 构成的列表[(ReplyContentType.TEXT, "Hello"), (ReplyContentType.IMAGE, "<base64")]
"""
hybrid_content_list: List[ReplyContent] = []
for content_type, content in hybrid_content:
assert content_type not in [
ReplyContentType.HYBRID,
ReplyContentType.FORWARD,
ReplyContentType.VOICE,
ReplyContentType.COMMAND,
], "混合内容的每个项不能是混合、转发、语音或命令类型"
assert isinstance(content, str), "混合内容的每个项必须是字符串"
self.reply_data.append(ReplyContent(content_type=content_type, content=content))
hybrid_content_list.append(ReplyContent(content_type=content_type, content=content))
def add_custom_content(self, content_type: str, content: str):
"""添加自定义类型的内容"""
self.reply_data.append(ReplyContent(content_type=ReplyContentType.HYBRID, content=hybrid_content_list))
def add_hybrid_content(self, hybrid_content: List[ReplyContent]):
"""
添加混合型内容使用已经构造好的 ReplyContent 列表
Args:
hybrid_content: ReplyContent 构成的列表[ReplyContent(ReplyContentType.TEXT, "Hello"), ReplyContent(ReplyContentType.IMAGE, "<base64")]
"""
for content in hybrid_content:
assert content.content_type not in [
ReplyContentType.HYBRID,
ReplyContentType.FORWARD,
ReplyContentType.VOICE,
ReplyContentType.COMMAND,
], "混合内容的每个项不能是混合、转发、语音或命令类型"
assert isinstance(content.content, str), "混合内容的每个项必须是字符串"
self.reply_data.append(ReplyContent(content_type=ReplyContentType.HYBRID, content=hybrid_content))
def add_custom_content(self, content_type: str, content: Any):
"""
添加自定义类型的内容"""
self.reply_data.append(ReplyContent(content_type=content_type, content=content))
def add_forward_content(self, forward_content: List[Tuple[ReplyContentType, Union[str, ReplyContent]]]):
def add_forward_content(self, forward_content: List[ForwardNode]):
"""添加转发内容可以是字符串或ReplyContent嵌套的转发内容需要自己构造放入"""
for content_type, content in forward_content:
if isinstance(content, ReplyContent):
self.reply_data.append(content)
else:
assert isinstance(content, str), "转发内容的每个data必须是字符串或ReplyContent"
self.reply_data.append(ReplyContent(content_type=content_type, content=content))
self.reply_data.append(ReplyContent(content_type=ReplyContentType.FORWARD, content=forward_content))

View File

@ -0,0 +1,57 @@
# 有关转发消息和其他消息的构建类型说明
```mermaid
graph LR;
direction TB;
A[ReplySet] --- B[ReplyContent];
A --- C["ReplyContent"];
A --- K["ReplyContent"];
A --- L["ReplyContent"];
A --- N["ReplyContent"];
A --- D[...];
B --- E["Text (in str)"];
B --- F["Image (in base64)"];
C --- G["Voice (in base64)"];
B --- I["Emoji (in base64)"];
subgraph "可行内容(以下的任意组合)";
subgraph "转发消息(Forward)"
M["List[ForwardNode]"]
end
subgraph "混合消息(Hybrid)"
J["List[ReplyContent] (要求只能包含普通消息)"]
end
subgraph "命令消息(Command)"
H["Command (in Dict)"]
end
subgraph "语音消息"
G
end
subgraph "普通消息"
E
F
I
end
end
N --- H
K --- J
L --- M
subgraph ForwardNodes
O["ForwardNode"]
P["ForwardNode"]
Q["ForwardNode"]
end
M --- O
M --- P
M --- Q
subgraph "内容 (message_id引用法)"
P --- U["content: str, 引用已有消息的有效ID"];
end
subgraph "内容 (生成法)"
O --- R["user_id: str"];
O --- S["user_nickname: str"];
O --- T["content: List[ReplyContent], 为这个转发节点的消息内容"];
end
```
另外,自定义消息类型我们在这里不做讨论。
以上列出了所有可能的ReplySet构建方式下面我们来解释一下各个类型的含义。

View File

@ -21,7 +21,7 @@
import traceback
import time
from typing import Optional, Union, Dict, List, TYPE_CHECKING
from typing import Optional, Union, Dict, List, TYPE_CHECKING, Tuple
from src.common.logger import get_logger
from src.common.data_models.message_data_model import ReplyContentType
@ -29,11 +29,11 @@ from src.config.config import global_config
from src.chat.message_receive.chat_stream import get_chat_manager
from src.chat.message_receive.uni_message_sender import UniversalMessageSender
from src.chat.message_receive.message import MessageSending, MessageRecv
from maim_message import Seg, UserInfo
from maim_message import Seg, UserInfo, MessageBase, BaseMessageInfo
if TYPE_CHECKING:
from src.common.data_models.database_data_model import DatabaseMessages
from src.common.data_models.message_data_model import ReplySetModel
from src.common.data_models.message_data_model import ReplySetModel, ReplyContent, ForwardNode
logger = get_logger("send_api")
@ -367,89 +367,84 @@ async def custom_reply_set_to_stream(
flag: bool = True
for reply_content in reply_set.reply_data:
status: bool = False
content_type = reply_content.content_type
message_data = reply_content.content
if content_type == ReplyContentType.TEXT:
status = await _send_to_target(
message_segment=Seg(type="text", data=message_data), # type: ignore
stream_id=stream_id,
display_message=display_message,
typing=typing,
reply_message=reply_message,
set_reply=set_reply,
storage_message=storage_message,
show_log=show_log,
)
elif content_type in [
ReplyContentType.IMAGE,
ReplyContentType.EMOJI,
ReplyContentType.COMMAND,
ReplyContentType.VOICE,
]:
message_segment: Seg
if ReplyContentType == ReplyContentType.IMAGE:
message_segment = Seg(type="image", data=message_data) # type: ignore
elif ReplyContentType == ReplyContentType.EMOJI:
message_segment = Seg(type="emoji", data=message_data) # type: ignore
elif ReplyContentType == ReplyContentType.COMMAND:
message_segment = Seg(type="command", data=message_data) # type: ignore
elif ReplyContentType == ReplyContentType.VOICE:
message_segment = Seg(type="voice", data=message_data) # type: ignore
status = await _send_to_target(
message_segment=message_segment,
stream_id=stream_id,
display_message=display_message,
typing=False,
reply_message=reply_message,
set_reply=set_reply,
storage_message=storage_message,
show_log=show_log,
)
elif content_type == ReplyContentType.HYBRID:
assert isinstance(message_data, list), "混合类型内容必须是列表"
sub_seg_list: List[Seg] = []
for sub_content in message_data:
sub_content_type = sub_content.content_type
sub_content_data = sub_content.content
if sub_content_type == ReplyContentType.TEXT:
sub_seg_list.append(Seg(type="text", data=sub_content_data)) # type: ignore
elif sub_content_type == ReplyContentType.IMAGE:
sub_seg_list.append(Seg(type="image", data=sub_content_data)) # type: ignore
elif sub_content_type == ReplyContentType.EMOJI:
sub_seg_list.append(Seg(type="emoji", data=sub_content_data)) # type: ignore
else:
logger.warning(f"[SendAPI] 混合类型中不支持的子内容类型: {repr(sub_content_type)}")
continue
status = await _send_to_target(
message_segment=Seg(type="seglist", data=sub_seg_list), # type: ignore
stream_id=stream_id,
display_message=display_message,
typing=typing,
reply_message=reply_message,
set_reply=set_reply,
storage_message=storage_message,
show_log=show_log,
)
elif content_type == ReplyContentType.FORWARD:
assert isinstance(message_data, list), "转发类型内容必须是列表"
# TODO: 完成转发消息的发送机制
else:
message_type_in_str = (
content_type.value if isinstance(content_type, ReplyContentType) else str(content_type)
)
return await _send_to_target(
message_segment=Seg(type=message_type_in_str, data=message_data), # type: ignore
stream_id=stream_id,
display_message=display_message,
typing=typing,
reply_message=reply_message,
set_reply=set_reply,
storage_message=storage_message,
show_log=show_log,
)
message_seg, need_typing = _parse_content_to_seg(reply_content)
status = await _send_to_target(
message_segment=message_seg,
stream_id=stream_id,
display_message=display_message,
typing=bool(need_typing and typing),
reply_message=reply_message,
set_reply=set_reply,
storage_message=storage_message,
show_log=show_log,
)
if not status:
flag = False
logger.error(f"[SendAPI] 发送{repr(content_type)}消息失败,消息内容:{str(message_data)[:100]}")
logger.error(
f"[SendAPI] 发送{repr(reply_content.content_type)}消息失败,消息内容:{str(reply_content.content)[:100]}"
)
return flag
def _parse_content_to_seg(reply_content: "ReplyContent") -> Tuple[Seg, bool]:
"""
ReplyContent 转换为 Seg 结构 (Forward 中仅递归一次)
Args:
reply_content: ReplyContent 对象
Returns:
Tuple[Seg, bool]: 转换后的 Seg 结构和是否需要typing的标志
"""
content_type = reply_content.content_type
if content_type == ReplyContentType.TEXT:
text_data: str = reply_content.content # type: ignore
return Seg(type="text", data=text_data), True
elif content_type == ReplyContentType.IMAGE:
return Seg(type="image", data=reply_content.content), False # type: ignore
elif content_type == ReplyContentType.EMOJI:
return Seg(type="emoji", data=reply_content.content), False # type: ignore
elif content_type == ReplyContentType.COMMAND:
return Seg(type="command", data=reply_content.content), False # type: ignore
elif content_type == ReplyContentType.VOICE:
return Seg(type="voice", data=reply_content.content), False # type: ignore
elif content_type == ReplyContentType.HYBRID:
hybrid_message_list_data: List[ReplyContent] = reply_content.content # type: ignore
assert isinstance(hybrid_message_list_data, list), "混合类型内容必须是列表"
sub_seg_list: List[Seg] = []
for sub_content in hybrid_message_list_data:
sub_content_type = sub_content.content_type
sub_content_data = sub_content.content
if sub_content_type == ReplyContentType.TEXT:
sub_seg_list.append(Seg(type="text", data=sub_content_data)) # type: ignore
elif sub_content_type == ReplyContentType.IMAGE:
sub_seg_list.append(Seg(type="image", data=sub_content_data)) # type: ignore
elif sub_content_type == ReplyContentType.EMOJI:
sub_seg_list.append(Seg(type="emoji", data=sub_content_data)) # type: ignore
else:
logger.warning(f"[SendAPI] 混合类型中不支持的子内容类型: {repr(sub_content_type)}")
continue
return Seg(type="seglist", data=sub_seg_list), True
elif content_type == ReplyContentType.FORWARD:
forward_message_list_data: List["ForwardNode"] = reply_content.content # type: ignore
assert isinstance(forward_message_list_data, list), "转发类型内容必须是列表"
forward_message_list: List[MessageBase] = []
for forward_node in forward_message_list_data:
message_segment = Seg(type="id", data=forward_node.content) # type: ignore
user_info: Optional[UserInfo] = None
if forward_node.user_id and forward_node.user_nickname:
assert isinstance(forward_node.content, list), "转发节点内容必须是列表"
user_info = UserInfo(user_id=forward_node.user_id, user_nickname=forward_node.user_nickname)
single_node_content: List[Seg] = []
for sub_content in forward_node.content:
if sub_content.content_type != ReplyContentType.FORWARD:
sub_seg, _ = _parse_content_to_seg(sub_content)
single_node_content.append(sub_seg)
message_segment = Seg(type="seglist", data=single_node_content)
forward_message_list.append(
MessageBase(message_segment=message_segment, message_info=BaseMessageInfo(user_info=user_info))
)
return Seg(type="forward", data=forward_message_list), False # type: ignore
else:
message_type_in_str = content_type.value if isinstance(content_type, ReplyContentType) else str(content_type)
return Seg(type=message_type_in_str, data=reply_content.content), True # type: ignore