优化一下总结生成。加入重试机制

This commit is contained in:
liuwei
2025-09-22 10:43:25 +08:00
parent 6eb6b70591
commit 31c9fc64ad
2 changed files with 147 additions and 33 deletions

View File

@@ -158,14 +158,29 @@ class MessageSummaryPlugin(MessagePluginInterface):
summary, image_path = await self._generate_summary(chat_content, group_name)
if image_path:
# 图片生成成功,发送图片
await self.bot.send_image_message(group_id, Path(image_path))
self.LOG.info(f"成功发送图片总结到群 {group_id}")
return True
else:
client_msg_id, create_time, new_msg_id = await self.bot.send_text_message(group_id,
"❌ 生成总结失败,请稍后再试!")
self.revoke.add_message_to_revoke(group_id, client_msg_id, create_time, new_msg_id, 5)
return False
# 图片生成失败,发送文本消息
if summary and len(summary.strip()) > 0:
# 截断过长的文本
max_length = 2000
if len(summary) > max_length:
summary = summary[:max_length] + "\n\n... (内容过长,已截断)"
client_msg_id, create_time, new_msg_id = await self.bot.send_text_message(group_id, summary)
self.revoke.add_message_to_revoke(group_id, client_msg_id, create_time, new_msg_id, 30)
self.LOG.info(f"图片生成失败,已发送文本总结到群 {group_id}")
return True
else:
# 连文本内容都没有
client_msg_id, create_time, new_msg_id = await self.bot.send_text_message(group_id,
"❌ 生成总结失败,请稍后再试!")
self.revoke.add_message_to_revoke(group_id, client_msg_id, create_time, new_msg_id, 5)
return False
except Exception as e:
self.LOG.error(f"异步生成总结失败: {e}")
client_msg_id, create_time, new_msg_id = await self.bot.send_text_message(group_id,
@@ -243,11 +258,22 @@ class MessageSummaryPlugin(MessagePluginInterface):
timestamp = int(time.time())
output_path = f"summary_{timestamp}.png"
# 构建完整的输出路径
self.LOG.info(f"开始生成图片: {output_path}")
spath = await convert_md_str_to_image(answer, output_path)
self.LOG.info(f"成功生成图片: {spath}")
except Exception as e:
self.LOG.error(f"生成image失败:{e}", exc_info=True)
spath = None
self.LOG.error(f"生成图片失败: {e}", exc_info=True)
# 如果图片生成失败,尝试发送纯文本消息
try:
# 截断过长的文本,避免消息太长
max_length = 2000
if len(answer) > max_length:
answer = answer[:max_length] + "\n\n... (内容过长,已截断)"
self.LOG.info("图片生成失败,将发送文本消息作为备选方案")
spath = None # 设置为None让调用方知道需要发送文本
except Exception as fallback_error:
self.LOG.error(f"备选文本发送也失败: {fallback_error}")
spath = None
# 返回文本内容和图片路径
return answer, spath