fix:修复normal_chat的planner合并后的一些问题

This commit is contained in:
SengokuCola
2025-06-03 00:03:27 +08:00
parent 880e8ea71d
commit bc2c7c5c1e
6 changed files with 69 additions and 60 deletions

View File

@@ -135,11 +135,11 @@ class ActionManager:
cycle_timers: dict, cycle_timers: dict,
thinking_id: str, thinking_id: str,
observations: List[Observation], observations: List[Observation],
expressor: DefaultExpressor,
replyer: DefaultReplyer,
chat_stream: ChatStream, chat_stream: ChatStream,
log_prefix: str, log_prefix: str,
shutting_down: bool = False, shutting_down: bool = False,
expressor: DefaultExpressor = None,
replyer: DefaultReplyer = None,
) -> Optional[BaseAction]: ) -> Optional[BaseAction]:
""" """
创建动作处理器实例 创建动作处理器实例

View File

@@ -182,26 +182,33 @@ class PluginAction(BaseAction):
Returns: Returns:
bool: 是否发送成功 bool: 是否发送成功
""" """
try: expressor = self._services.get("expressor")
expressor = self._services.get("expressor") chat_stream = self._services.get("chat_stream")
chat_stream = self._services.get("chat_stream")
if not expressor or not chat_stream: if not expressor or not chat_stream:
logger.error(f"{self.log_prefix} 无法发送消息:缺少必要的内部服务") logger.error(f"{self.log_prefix} 无法发送消息:缺少必要的内部服务")
return False return False
# 构造简化的动作数据 # 构造简化的动作数据
reply_data = {"text": text, "target": target or "", "emojis": []} reply_data = {"text": text, "target": target or "", "emojis": []}
# 获取锚定消息(如果有) # 获取锚定消息(如果有)
observations = self._services.get("observations", []) observations = self._services.get("observations", [])
chatting_observation: ChattingObservation = next( # 查找 ChattingObservation 实例
obs for obs in observations if isinstance(obs, ChattingObservation) chatting_observation = None
for obs in observations:
if isinstance(obs, ChattingObservation):
chatting_observation = obs
break
if not chatting_observation:
logger.warning(f"{self.log_prefix} 未找到 ChattingObservation 实例,创建占位符")
anchor_message = await create_empty_anchor_message(
chat_stream.platform, chat_stream.group_info, chat_stream
) )
else:
anchor_message = chatting_observation.search_message_by_text(reply_data["target"]) anchor_message = chatting_observation.search_message_by_text(reply_data["target"])
# 如果没有找到锚点消息,创建一个占位符
if not anchor_message: if not anchor_message:
logger.info(f"{self.log_prefix} 未找到锚点消息,创建占位符") logger.info(f"{self.log_prefix} 未找到锚点消息,创建占位符")
anchor_message = await create_empty_anchor_message( anchor_message = await create_empty_anchor_message(
@@ -210,19 +217,16 @@ class PluginAction(BaseAction):
else: else:
anchor_message.update_chat_stream(chat_stream) anchor_message.update_chat_stream(chat_stream)
# 调用内部方法发送消息 # 调用内部方法发送消息
success, _ = await expressor.deal_reply( success, _ = await expressor.deal_reply(
cycle_timers=self.cycle_timers, cycle_timers=self.cycle_timers,
action_data=reply_data, action_data=reply_data,
anchor_message=anchor_message, anchor_message=anchor_message,
reasoning=self.reasoning, reasoning=self.reasoning,
thinking_id=self.thinking_id, thinking_id=self.thinking_id,
) )
return success return success
except Exception as e:
logger.error(f"{self.log_prefix} 发送消息时出错: {e}")
return False
def get_chat_type(self) -> str: def get_chat_type(self) -> str:
"""获取当前聊天类型 """获取当前聊天类型

View File

@@ -24,6 +24,7 @@ from src.chat.focus_chat.planners.action_manager import ActionManager
from src.chat.normal_chat.normal_chat_planner import NormalChatPlanner from src.chat.normal_chat.normal_chat_planner import NormalChatPlanner
from src.chat.normal_chat.normal_chat_action_modifier import NormalChatActionModifier from src.chat.normal_chat.normal_chat_action_modifier import NormalChatActionModifier
from src.chat.normal_chat.normal_chat_expressor import NormalChatExpressor from src.chat.normal_chat.normal_chat_expressor import NormalChatExpressor
from src.chat.focus_chat.replyer.default_replyer import DefaultReplyer
logger = get_logger("normal_chat") logger = get_logger("normal_chat")
@@ -77,6 +78,9 @@ class NormalChat:
# 初始化Normal Chat专用表达器 # 初始化Normal Chat专用表达器
self.expressor = NormalChatExpressor(self.chat_stream, self.stream_name) self.expressor = NormalChatExpressor(self.chat_stream, self.stream_name)
self.replyer = DefaultReplyer(chat_id=self.stream_id)
self.replyer.chat_stream = self.chat_stream
self._initialized = True self._initialized = True
logger.debug(f"[{self.stream_name}] NormalChat 初始化完成 (异步部分)。") logger.debug(f"[{self.stream_name}] NormalChat 初始化完成 (异步部分)。")
@@ -653,6 +657,7 @@ class NormalChat:
thinking_id=thinking_id, thinking_id=thinking_id,
observations=[], # normal_chat不使用observations observations=[], # normal_chat不使用observations
expressor=self.expressor, # 使用normal_chat专用的expressor expressor=self.expressor, # 使用normal_chat专用的expressor
replyer=self.replyer,
chat_stream=self.chat_stream, chat_stream=self.chat_stream,
log_prefix=self.stream_name, log_prefix=self.stream_name,
shutting_down=self._disabled, shutting_down=self._disabled,

View File

@@ -128,38 +128,38 @@ class ImageManager:
return f"[表情包,含义看起来是:{cached_description}]" return f"[表情包,含义看起来是:{cached_description}]"
# 根据配置决定是否保存图片 # 根据配置决定是否保存图片
if global_config.emoji.save_emoji: # if global_config.emoji.save_emoji:
# 生成文件名和路径 # 生成文件名和路径
logger.debug(f"保存表情包: {image_hash}") logger.debug(f"保存表情包: {image_hash}")
current_timestamp = time.time() current_timestamp = time.time()
filename = f"{int(current_timestamp)}_{image_hash[:8]}.{image_format}" filename = f"{int(current_timestamp)}_{image_hash[:8]}.{image_format}"
emoji_dir = os.path.join(self.IMAGE_DIR, "emoji") emoji_dir = os.path.join(self.IMAGE_DIR, "emoji")
os.makedirs(emoji_dir, exist_ok=True) os.makedirs(emoji_dir, exist_ok=True)
file_path = os.path.join(emoji_dir, filename) file_path = os.path.join(emoji_dir, filename)
try:
# 保存文件
with open(file_path, "wb") as f:
f.write(image_bytes)
# 保存到数据库 (Images表)
try: try:
# 保存文件 img_obj = Images.get((Images.emoji_hash == image_hash) & (Images.type == "emoji"))
with open(file_path, "wb") as f: img_obj.path = file_path
f.write(image_bytes) img_obj.description = description
img_obj.timestamp = current_timestamp
# 保存到数据库 (Images表) img_obj.save()
try: except Images.DoesNotExist:
img_obj = Images.get((Images.emoji_hash == image_hash) & (Images.type == "emoji")) Images.create(
img_obj.path = file_path emoji_hash=image_hash,
img_obj.description = description path=file_path,
img_obj.timestamp = current_timestamp type="emoji",
img_obj.save() description=description,
except Images.DoesNotExist: timestamp=current_timestamp,
Images.create( )
emoji_hash=image_hash, # logger.debug(f"保存表情包元数据: {file_path}")
path=file_path, except Exception as e:
type="emoji", logger.error(f"保存表情包文件或元数据失败: {str(e)}")
description=description,
timestamp=current_timestamp,
)
# logger.debug(f"保存表情包元数据: {file_path}")
except Exception as e:
logger.error(f"保存表情包文件或元数据失败: {str(e)}")
# 保存描述到数据库 (ImageDescriptions表) # 保存描述到数据库 (ImageDescriptions表)
self._save_description_to_db(image_hash, description, "emoji") self._save_description_to_db(image_hash, description, "emoji")

View File

@@ -22,7 +22,7 @@ class MuteAction(PluginAction):
"当有人发了擦边,或者色情内容时使用", "当有人发了擦边,或者色情内容时使用",
"当有人要求禁言自己时使用", "当有人要求禁言自己时使用",
] ]
default = False # 默认动作,是否手动添加到使用集 default = True # 默认动作,是否手动添加到使用集
associated_types = ["command", "text"] associated_types = ["command", "text"]
# associated_types = ["text"] # associated_types = ["text"]

View File

@@ -1,5 +1,5 @@
[inner] [inner]
version = "2.10.0" version = "2.11.0"
#----以下是给开发人员阅读的,如果你只是部署了麦麦,不需要阅读---- #----以下是给开发人员阅读的,如果你只是部署了麦麦,不需要阅读----
#如果你想要修改配置文件请在修改后将version的值进行变更 #如果你想要修改配置文件请在修改后将version的值进行变更