调整生成图片的逻辑

This commit is contained in:
liuwei
2025-05-16 09:01:38 +08:00
parent 6b4cd5eb4d
commit 40568fe038

View File

@@ -1,7 +1,9 @@
import asyncio
import json
import os
import re
import threading
import time
from pathlib import Path
from typing import Dict, Any, Tuple, Optional, List
@@ -216,9 +218,27 @@ class MessageSummaryPlugin(MessagePluginInterface):
tokens_info = f"\n\n【tokens】输入: {prompt_tokens} 生成: {completion_tokens} 总: {total_tokens}"
answer += tokens_info
try:
spath = await convert_md_str_to_image(answer, "output.png")
# 使用唯一文件名并指定完整路径
timestamp = int(time.time())
output_filename = f"summary_{group_id}_{timestamp}.png"
output_dir = Path(os.path.dirname(os.path.abspath(__file__)), "outputs")
# 确保输出目录存在
os.makedirs(output_dir, exist_ok=True)
# 构建完整的输出路径
output_path = os.path.join(output_dir, output_filename)
# 检查磁盘空间
if self._check_disk_space(output_dir):
spath = await convert_md_str_to_image(answer, output_path)
self.LOG.info(f"成功生成图片: {spath}")
else:
self.LOG.error("磁盘空间不足,无法生成图片")
spath = None
except Exception as e:
self.LOG.error(f"生成image失败:{e}")
self.LOG.error(f"生成image失败:{e}", exc_info=True)
spath = None
# 返回文本内容和图片路径
return answer, spath
@@ -233,3 +253,22 @@ class MessageSummaryPlugin(MessagePluginInterface):
except Exception as e:
self.LOG.error(f"处理总结时出现未知错误: {e}")
return f"生成总结时出现未知错误: {str(e)}", None
def _check_disk_space(self, path, min_space_mb=100):
"""
检查指定路径的可用磁盘空间
:param path: 要检查的路径
:param min_space_mb: 最小所需空间MB
:return: 如果有足够空间则返回True否则返回False
"""
try:
import shutil
# 获取路径所在磁盘的可用空间(字节)
free_space = shutil.disk_usage(path).free
# 转换为MB
free_space_mb = free_space / (1024 * 1024)
return free_space_mb >= min_space_mb
except Exception as e:
self.LOG.error(f"检查磁盘空间时出错: {e}")
# 出错时假设空间足够
return True