Merge pull request #1068 from A0000Xz/dev
添加了回报消息更新数据库message_id;让generator插件接口能够自定义使用错字或者切句;一些小的修复
This commit is contained in:
@@ -131,6 +131,12 @@ class ChatBot:
|
||||
message = MessageRecv(message_data)
|
||||
group_info = message.message_info.group_info
|
||||
user_info = message.message_info.user_info
|
||||
if message.message_info.additional_config:
|
||||
sent_message = message.message_info.additional_config.get("echo", False)
|
||||
if sent_message: # 这一段只是为了在一切处理前劫持上报的自身消息,用于更新message_id,需要ada支持上报事件,实际测试中不会对正常使用造成任何问题
|
||||
await MessageStorage.update_message(message)
|
||||
return
|
||||
|
||||
get_chat_manager().register_message(message)
|
||||
|
||||
# 创建聊天流
|
||||
@@ -197,4 +203,4 @@ class ChatBot:
|
||||
|
||||
|
||||
# 创建全局ChatBot实例
|
||||
chat_bot = ChatBot()
|
||||
chat_bot = ChatBot()
|
||||
@@ -103,3 +103,32 @@ class MessageStorage:
|
||||
|
||||
|
||||
# 如果需要其他存储相关的函数,可以在这里添加
|
||||
@staticmethod
|
||||
async def update_message(message: MessageRecv) -> None: # 用于实时更新数据库的自身发送消息ID,目前能处理text,reply,image和emoji
|
||||
"""更新最新一条匹配消息的message_id"""
|
||||
try:
|
||||
if message.message_segment.type == "notify":
|
||||
mmc_message_id = message.message_segment.data.get("echo")
|
||||
qq_message_id = message.message_segment.data.get("actual_id")
|
||||
else:
|
||||
logger.info(f"更新消息ID错误,seg类型为{message.message_segment.type}")
|
||||
return
|
||||
if not qq_message_id:
|
||||
logger.info("消息不存在message_id,无法更新")
|
||||
return
|
||||
# 查询最新一条匹配消息
|
||||
matched_message = Messages.select().where(
|
||||
(Messages.message_id == mmc_message_id)
|
||||
).order_by(Messages.time.desc()).first()
|
||||
|
||||
if matched_message:
|
||||
# 更新找到的消息记录
|
||||
Messages.update(message_id=qq_message_id).where(
|
||||
Messages.id == matched_message.id
|
||||
).execute()
|
||||
logger.info(f"更新消息ID成功: {matched_message.message_id} -> {qq_message_id}")
|
||||
else:
|
||||
logger.debug("未找到匹配的消息")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"更新消息ID失败: {e}")
|
||||
@@ -162,6 +162,8 @@ class DefaultReplyer:
|
||||
async def generate_reply_with_context(
|
||||
self,
|
||||
reply_data: Dict[str, Any],
|
||||
enable_splitter: bool=True,
|
||||
enable_chinese_typo: bool=True
|
||||
) -> Tuple[bool, Optional[List[str]]]:
|
||||
"""
|
||||
回复器 (Replier): 核心逻辑,负责生成回复文本。
|
||||
@@ -191,7 +193,7 @@ class DefaultReplyer:
|
||||
logger.error(f"{self.log_prefix}LLM 生成失败: {llm_e}")
|
||||
return False, None # LLM 调用失败则无法生成回复
|
||||
|
||||
processed_response = process_llm_response(content)
|
||||
processed_response = process_llm_response(content,enable_splitter,enable_chinese_typo)
|
||||
|
||||
# 5. 处理 LLM 响应
|
||||
if not content:
|
||||
@@ -216,6 +218,8 @@ class DefaultReplyer:
|
||||
async def rewrite_reply_with_context(
|
||||
self,
|
||||
reply_data: Dict[str, Any],
|
||||
enable_splitter: bool=True,
|
||||
enable_chinese_typo: bool=True
|
||||
) -> Tuple[bool, Optional[List[str]]]:
|
||||
"""
|
||||
表达器 (Expressor): 核心逻辑,负责生成回复文本。
|
||||
@@ -252,7 +256,7 @@ class DefaultReplyer:
|
||||
logger.error(f"{self.log_prefix}LLM 生成失败: {llm_e}")
|
||||
return False, None # LLM 调用失败则无法生成回复
|
||||
|
||||
processed_response = process_llm_response(content)
|
||||
processed_response = process_llm_response(content,enable_splitter,enable_chinese_typo)
|
||||
|
||||
# 5. 处理 LLM 响应
|
||||
if not content:
|
||||
|
||||
@@ -321,7 +321,7 @@ def random_remove_punctuation(text: str) -> str:
|
||||
return result
|
||||
|
||||
|
||||
def process_llm_response(text: str) -> list[str]:
|
||||
def process_llm_response(text: str, enable_splitter: bool=True, enable_chinese_typo: bool=True) -> list[str]:
|
||||
if not global_config.response_post_process.enable_response_post_process:
|
||||
return [text]
|
||||
|
||||
@@ -359,14 +359,14 @@ def process_llm_response(text: str) -> list[str]:
|
||||
word_replace_rate=global_config.chinese_typo.word_replace_rate,
|
||||
)
|
||||
|
||||
if global_config.response_splitter.enable:
|
||||
if global_config.response_splitter.enable and enable_splitter:
|
||||
split_sentences = split_into_sentences_w_remove_punctuation(cleaned_text)
|
||||
else:
|
||||
split_sentences = [cleaned_text]
|
||||
|
||||
sentences = []
|
||||
for sentence in split_sentences:
|
||||
if global_config.chinese_typo.enable:
|
||||
if global_config.chinese_typo.enable and enable_chinese_typo:
|
||||
typoed_text, typo_corrections = typo_generator.create_typo_sentence(sentence)
|
||||
sentences.append(typoed_text)
|
||||
if typo_corrections:
|
||||
|
||||
@@ -73,6 +73,8 @@ async def generate_reply(
|
||||
chat_stream=None,
|
||||
action_data: Dict[str, Any] = None,
|
||||
chat_id: str = None,
|
||||
enable_splitter: bool=True,
|
||||
enable_chinese_typo: bool=True
|
||||
) -> Tuple[bool, List[Tuple[str, Any]]]:
|
||||
"""生成回复
|
||||
|
||||
@@ -80,6 +82,8 @@ async def generate_reply(
|
||||
chat_stream: 聊天流对象(优先)
|
||||
action_data: 动作数据
|
||||
chat_id: 聊天ID(备用)
|
||||
enable_splitter: 是否启用消息分割器
|
||||
enable_chinese_typo: 是否启用错字生成器
|
||||
|
||||
Returns:
|
||||
Tuple[bool, List[Tuple[str, Any]]]: (是否成功, 回复集合)
|
||||
@@ -96,6 +100,8 @@ async def generate_reply(
|
||||
# 调用回复器生成回复
|
||||
success, reply_set = await replyer.generate_reply_with_context(
|
||||
reply_data=action_data or {},
|
||||
enable_splitter=enable_splitter,
|
||||
enable_chinese_typo=enable_chinese_typo
|
||||
)
|
||||
|
||||
if success:
|
||||
@@ -114,6 +120,8 @@ async def rewrite_reply(
|
||||
chat_stream=None,
|
||||
reply_data: Dict[str, Any] = None,
|
||||
chat_id: str = None,
|
||||
enable_splitter: bool=True,
|
||||
enable_chinese_typo: bool=True
|
||||
) -> Tuple[bool, List[Tuple[str, Any]]]:
|
||||
"""重写回复
|
||||
|
||||
@@ -121,6 +129,8 @@ async def rewrite_reply(
|
||||
chat_stream: 聊天流对象(优先)
|
||||
reply_data: 回复数据
|
||||
chat_id: 聊天ID(备用)
|
||||
enable_splitter: 是否启用消息分割器
|
||||
enable_chinese_typo: 是否启用错字生成器
|
||||
|
||||
Returns:
|
||||
Tuple[bool, List[Tuple[str, Any]]]: (是否成功, 回复集合)
|
||||
@@ -137,6 +147,8 @@ async def rewrite_reply(
|
||||
# 调用回复器重写回复
|
||||
success, reply_set = await replyer.rewrite_reply_with_context(
|
||||
reply_data=reply_data or {},
|
||||
enable_splitter=enable_splitter,
|
||||
enable_chinese_typo=enable_chinese_typo
|
||||
)
|
||||
|
||||
if success:
|
||||
|
||||
@@ -108,6 +108,8 @@ class BaseAction(ABC):
|
||||
# print(self.chat_stream.group_info)
|
||||
if self.chat_stream.group_info:
|
||||
self.is_group = True
|
||||
self.user_id = str(self.chat_stream.user_info.user_id)
|
||||
self.user_nickname = getattr(self.chat_stream.user_info, "user_nickname", None)
|
||||
self.group_id = str(self.chat_stream.group_info.group_id)
|
||||
self.group_name = getattr(self.chat_stream.group_info, "group_name", None)
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user