支持html转图传入视口宽度并在总结模板中透传配置

This commit is contained in:
liuwei
2026-04-23 11:27:37 +08:00
parent d83bf9bbb6
commit 3e93188efb
2 changed files with 54 additions and 5 deletions

View File

@@ -110,6 +110,13 @@ class MessageSummaryPlugin(MessagePluginInterface):
self._summary_image_template_path = str(
output_config.get("summary_image_template_path", "plugins/message_summary/templates/summary_card.html")
).strip()
# 模板截图视口参数:
# 1. 支持在插件配置中单独传入宽度/高度/缩放;
# 2. 默认保持历史值780/960/1.2),避免影响未配置场景;
# 3. 仅在 template 模式生图时使用markdown 模式不受影响。
self._template_viewport_width = int(output_config.get("template_viewport_width", 780))
self._template_viewport_height = int(output_config.get("template_viewport_height", 960))
self._template_device_scale_factor = float(output_config.get("template_device_scale_factor", 1.2))
self.llm_client = UnifiedLLMClient(api_config)
self._api_mode = self.llm_client.mode or self._api_mode
self._response_mode = self.llm_client.response_mode or self._response_mode
@@ -1489,7 +1496,16 @@ class MessageSummaryPlugin(MessagePluginInterface):
message_stats=message_stats,
metadata=metadata,
)
await asyncio.wait_for(html_to_image(html_content, str(output_path)), timeout=total_timeout)
await asyncio.wait_for(
html_to_image(
html_content,
str(output_path),
viewport_width=self._template_viewport_width,
viewport_height=self._template_viewport_height,
device_scale_factor=self._template_device_scale_factor,
),
timeout=total_timeout,
)
if not output_path.exists() or output_path.stat().st_size < 1024:
raise RuntimeError("模板截图输出文件异常")
return str(output_path.resolve())