2024-01-26 15:51:49 +08:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
import os
|
|
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
|
|
|
|
|
|
|
|
from ...core import app
|
|
|
|
|
|
from . import strategy
|
|
|
|
|
|
from .strategies import image, forward
|
|
|
|
|
|
from .. import stage, entities, stagemgr
|
|
|
|
|
|
from ...core import entities as core_entities
|
|
|
|
|
|
from ...config import manager as cfg_mgr
|
2024-09-26 00:23:03 +08:00
|
|
|
|
from ...platform.types import message as platform_message
|
2024-01-26 15:51:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@stage.stage_class("LongTextProcessStage")
|
|
|
|
|
|
class LongTextProcessStage(stage.PipelineStage):
|
2024-03-03 16:34:59 +08:00
|
|
|
|
"""长消息处理阶段
|
2024-05-14 22:20:31 +08:00
|
|
|
|
|
|
|
|
|
|
改写:
|
|
|
|
|
|
- resp_message_chain
|
2024-03-03 16:34:59 +08:00
|
|
|
|
"""
|
2024-01-26 15:51:49 +08:00
|
|
|
|
|
|
|
|
|
|
strategy_impl: strategy.LongTextStrategy
|
|
|
|
|
|
|
|
|
|
|
|
async def initialize(self):
|
2024-02-06 21:26:03 +08:00
|
|
|
|
config = self.ap.platform_cfg.data['long-text-process']
|
|
|
|
|
|
if config['strategy'] == 'image':
|
|
|
|
|
|
use_font = config['font-path']
|
2024-01-26 15:51:49 +08:00
|
|
|
|
try:
|
|
|
|
|
|
# 检查是否存在
|
|
|
|
|
|
if not os.path.exists(use_font):
|
|
|
|
|
|
# 若是windows系统,使用微软雅黑
|
|
|
|
|
|
if os.name == "nt":
|
|
|
|
|
|
use_font = "C:/Windows/Fonts/msyh.ttc"
|
|
|
|
|
|
if not os.path.exists(use_font):
|
2024-05-18 18:52:45 +08:00
|
|
|
|
self.ap.logger.warn("未找到字体文件,且无法使用Windows自带字体,更换为转发消息组件以发送长消息,您可以在配置文件中调整相关设置。")
|
2024-01-26 15:51:49 +08:00
|
|
|
|
config['blob_message_strategy'] = "forward"
|
|
|
|
|
|
else:
|
|
|
|
|
|
self.ap.logger.info("使用Windows自带字体:" + use_font)
|
2024-02-06 21:26:03 +08:00
|
|
|
|
config['font-path'] = use_font
|
2024-01-26 15:51:49 +08:00
|
|
|
|
else:
|
2024-05-18 18:52:45 +08:00
|
|
|
|
self.ap.logger.warn("未找到字体文件,且无法使用系统自带字体,更换为转发消息组件以发送长消息,您可以在配置文件中调整相关设置。")
|
2024-02-06 21:26:03 +08:00
|
|
|
|
|
|
|
|
|
|
self.ap.platform_cfg.data['long-text-process']['strategy'] = "forward"
|
2024-01-26 15:51:49 +08:00
|
|
|
|
except:
|
|
|
|
|
|
traceback.print_exc()
|
2024-05-18 18:52:45 +08:00
|
|
|
|
self.ap.logger.error("加载字体文件失败({}),更换为转发消息组件以发送长消息,您可以在配置文件中调整相关设置。".format(use_font))
|
2024-02-06 21:26:03 +08:00
|
|
|
|
|
|
|
|
|
|
self.ap.platform_cfg.data['long-text-process']['strategy'] = "forward"
|
2024-03-08 20:31:22 +08:00
|
|
|
|
|
|
|
|
|
|
for strategy_cls in strategy.preregistered_strategies:
|
|
|
|
|
|
if strategy_cls.name == config['strategy']:
|
|
|
|
|
|
self.strategy_impl = strategy_cls(self.ap)
|
|
|
|
|
|
break
|
|
|
|
|
|
else:
|
|
|
|
|
|
raise ValueError(f"未找到名为 {config['strategy']} 的长消息处理策略")
|
|
|
|
|
|
|
2024-01-26 15:51:49 +08:00
|
|
|
|
await self.strategy_impl.initialize()
|
|
|
|
|
|
|
|
|
|
|
|
async def process(self, query: core_entities.Query, stage_inst_name: str) -> entities.StageProcessResult:
|
2024-03-31 14:38:15 +08:00
|
|
|
|
# 检查是否包含非 Plain 组件
|
|
|
|
|
|
contains_non_plain = False
|
|
|
|
|
|
|
2024-05-14 23:08:49 +08:00
|
|
|
|
for msg in query.resp_message_chain[-1]:
|
2024-09-26 00:23:03 +08:00
|
|
|
|
if not isinstance(msg, platform_message.Plain):
|
2024-03-31 14:38:15 +08:00
|
|
|
|
contains_non_plain = True
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
if contains_non_plain:
|
|
|
|
|
|
self.ap.logger.debug("消息中包含非 Plain 组件,跳过长消息处理。")
|
2024-05-14 23:08:49 +08:00
|
|
|
|
elif len(str(query.resp_message_chain[-1])) > self.ap.platform_cfg.data['long-text-process']['threshold']:
|
2024-09-26 00:23:03 +08:00
|
|
|
|
query.resp_message_chain[-1] = platform_message.MessageChain(await self.strategy_impl.process(str(query.resp_message_chain[-1]), query))
|
2024-03-31 14:38:15 +08:00
|
|
|
|
|
2024-01-26 15:51:49 +08:00
|
|
|
|
return entities.StageProcessResult(
|
|
|
|
|
|
result_type=entities.ResultType.CONTINUE,
|
|
|
|
|
|
new_query=query
|
2024-01-27 21:50:40 +08:00
|
|
|
|
)
|