👌perf: 优化了分段回复和回复时at,引用都打开时的一些体验性问题

This commit is contained in:
Soulter
2025-02-15 19:29:34 +08:00
parent aff520e69a
commit 5c32a17787
4 changed files with 23 additions and 28 deletions
+19 -3
View File
@@ -7,14 +7,17 @@ from ..context import PipelineContext
from astrbot.core.platform.astr_message_event import AstrMessageEvent
from astrbot.core.message.message_event_result import MessageChain
from astrbot.core import logger
from astrbot.core.message.message_event_result import BaseMessageComponent, Plain
from astrbot.core.message.message_event_result import BaseMessageComponent
from astrbot.core.star.star_handler import star_handlers_registry, EventType
from astrbot.core.message.components import Plain, Reply, At
@register_stage
class RespondStage(Stage):
async def initialize(self, ctx: PipelineContext):
self.ctx = ctx
self.reply_with_mention = ctx.astrbot_config['platform_settings']['reply_with_mention']
self.reply_with_quote = ctx.astrbot_config['platform_settings']['reply_with_quote']
# 分段回复
self.enable_seg: bool = ctx.astrbot_config['platform_settings']['segmented_reply']['enable']
self.only_llm_result = ctx.astrbot_config['platform_settings']['segmented_reply']['only_llm_result']
@@ -60,11 +63,24 @@ class RespondStage(Stage):
await event._pre_send()
if self.enable_seg and ((self.only_llm_result and result.is_llm_result()) or not self.only_llm_result):
decorated_comps = []
if self.reply_with_mention:
for comp in result.chain:
if isinstance(comp, At):
decorated_comps.append(comp)
result.chain.remove(comp)
break
if self.reply_with_quote:
for comp in result.chain:
if isinstance(comp, Reply):
decorated_comps.append(comp)
result.chain.remove(comp)
break
# 分段回复
for comp in result.chain:
i = await self._calc_comp_interval(comp)
await asyncio.sleep(i)
await event.send(MessageChain([comp]))
await event.send(MessageChain([*decorated_comps, comp]))
else:
await event.send(result)
await event._post_send()
@@ -89,7 +89,7 @@ class ResultDecorateStage(Stage):
new_chain.append(comp)
continue
for seg in split_response:
if seg:
if seg.strip():
new_chain.append(Plain(seg))
else:
# 非 Plain 类型的消息段不分段
@@ -5,7 +5,7 @@ import botpy.types.message
from astrbot.core.utils.io import file_to_base64, download_image_by_url
from astrbot.api.event import AstrMessageEvent, MessageChain
from astrbot.api.platform import AstrBotMessage, PlatformMetadata
from astrbot.api.message_components import Plain, Image, Reply
from astrbot.api.message_components import Plain, Image
from botpy import Client
from botpy.http import Route
from astrbot.api import logger
@@ -33,19 +33,6 @@ class QQOfficialMessageEvent(AstrMessageEvent):
if not plain_text and not image_base64 and not image_path:
return
ref = None
for i in self.send_buffer.chain:
if isinstance(i, Reply):
try:
ref = self.message_obj.raw_message.message_reference
ref = botpy.types.message.Reference(
message_id=ref.message_id,
ignore_get_message_error=False
)
except BaseException as _:
pass
break
payload = {
'content': plain_text,
'msg_id': self.message_obj.message_id,
@@ -53,30 +40,22 @@ class QQOfficialMessageEvent(AstrMessageEvent):
match type(source):
case botpy.message.GroupMessage:
if ref:
payload['message_reference'] = ref
if image_base64:
media = await self.upload_group_and_c2c_image(image_base64, 1, group_openid=source.group_openid)
payload['media'] = media
payload['msg_type'] = 7
await self.bot.api.post_group_message(group_openid=source.group_openid, **payload)
case botpy.message.C2CMessage:
if ref:
payload['message_reference'] = ref
if image_base64:
media = await self.upload_group_and_c2c_image(image_base64, 1, openid=source.author.user_openid)
payload['media'] = media
payload['msg_type'] = 7
await self.bot.api.post_c2c_message(openid=source.author.user_openid, **payload)
case botpy.message.Message:
if ref:
payload['message_reference'] = ref
if image_path:
payload['file_image'] = image_path
await self.bot.api.post_message(channel_id=source.channel_id, **payload)
case botpy.message.DirectMessage:
if ref:
payload['message_reference'] = ref
if image_path:
payload['file_image'] = image_path
await self.bot.api.post_dms(guild_id=source.guild_id, **payload)
+2 -2
View File
@@ -75,10 +75,10 @@ class Main(star.Star):
@llm_tool("web_search")
async def search_from_search_engine(self, event: AstrMessageEvent, query: str) -> str:
'''Search the web for answers to the user's query
'''搜索网络以回答用户的问题。当用户需要搜索网络以获取即时性的信息时调用此工具。
Args:
query(string): A search query which will be used to fetch the most relevant snippets regarding the user's query
query(string): 和用户的问题最相关的搜索关键词,用于在 Google 上搜索。
'''
logger.info("web_searcher - search_from_search_engine: " + query)
results = []