Files
LangBot/pkg/pipeline/process/handlers/chat.py

97 lines
3.6 KiB
Python
Raw Normal View History

from __future__ import annotations
import typing
2024-02-01 18:11:47 +08:00
import traceback
from .. import handler
from ... import entities
from ....core import entities as core_entities
from ....provider import runner as runner_module
2024-01-30 21:45:17 +08:00
from ....plugin import events
from ....platform.types import message as platform_message
from ....utils import importutil
from ....provider import runners
importutil.import_modules_in_pkg(runners)
class ChatMessageHandler(handler.MessageHandler):
async def handle(
self,
query: core_entities.Query,
) -> typing.AsyncGenerator[entities.StageProcessResult, None]:
"""处理"""
# 调API
# 生成器
2024-01-30 21:45:17 +08:00
# 触发插件事件
event_class = (
events.PersonNormalMessageReceived
if query.launcher_type == core_entities.LauncherTypes.PERSON
else events.GroupNormalMessageReceived
)
2024-01-30 21:45:17 +08:00
event_ctx = await self.ap.plugin_mgr.emit_event(
event=event_class(
launcher_type=query.launcher_type.value,
launcher_id=query.launcher_id,
sender_id=query.sender_id,
text_message=str(query.message_chain),
query=query,
2024-01-28 18:21:43 +08:00
)
)
2024-01-30 21:45:17 +08:00
if event_ctx.is_prevented_default():
if event_ctx.event.reply is not None:
mc = platform_message.MessageChain(event_ctx.event.reply)
query.resp_messages.append(mc)
2024-01-27 21:50:40 +08:00
2025-05-10 18:04:58 +08:00
yield entities.StageProcessResult(result_type=entities.ResultType.CONTINUE, new_query=query)
2024-01-30 21:45:17 +08:00
else:
2025-05-10 18:04:58 +08:00
yield entities.StageProcessResult(result_type=entities.ResultType.INTERRUPT, new_query=query)
2024-01-30 21:45:17 +08:00
else:
if event_ctx.event.alter is not None:
# if isinstance(event_ctx.event, str): # 现在暂时不考虑多模态alter
query.user_message.content = event_ctx.event.alter
2024-01-30 21:45:17 +08:00
2024-01-31 00:02:19 +08:00
text_length = 0
2024-02-01 16:43:44 +08:00
try:
for r in runner_module.preregistered_runners:
if r.name == query.pipeline_config['ai']['runner']['runner']:
runner = r(self.ap, query.pipeline_config)
break
else:
2025-05-10 18:04:58 +08:00
raise ValueError(f'未找到请求运行器: {query.pipeline_config["ai"]["runner"]["runner"]}')
2024-07-28 18:45:27 +08:00
async for result in runner.run(query):
2024-02-01 16:43:44 +08:00
query.resp_messages.append(result)
2024-02-01 16:35:00 +08:00
2025-05-10 18:04:58 +08:00
self.ap.logger.info(f'对话({query.query_id})响应: {self.cut_str(result.readable_str())}')
2024-02-01 16:43:44 +08:00
if result.content is not None:
text_length += len(result.content)
2024-01-31 00:02:19 +08:00
2025-05-10 18:04:58 +08:00
yield entities.StageProcessResult(result_type=entities.ResultType.CONTINUE, new_query=query)
query.session.using_conversation.messages.append(query.user_message)
query.session.using_conversation.messages.extend(query.resp_messages)
2024-02-01 18:11:47 +08:00
except Exception as e:
2025-05-10 18:04:58 +08:00
self.ap.logger.error(f'对话({query.query_id})请求失败: {type(e).__name__} {str(e)}')
2025-05-10 18:04:58 +08:00
hide_exception_info = query.pipeline_config['output']['misc']['hide-exception']
2024-02-01 18:11:47 +08:00
yield entities.StageProcessResult(
result_type=entities.ResultType.INTERRUPT,
new_query=query,
user_notice='请求失败' if hide_exception_info else f'{e}',
2024-02-01 18:11:47 +08:00
error_notice=f'{e}',
debug_notice=traceback.format_exc(),
2024-02-01 18:11:47 +08:00
)
2024-02-01 16:43:44 +08:00
finally:
2025-05-10 16:17:01 +08:00
# TODO statistics
2025-05-10 17:16:57 +08:00
pass