2024-01-27 00:06:38 +08:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from ...core import app, entities as core_entities
|
|
|
|
|
from . import handler
|
|
|
|
|
from .handlers import chat, command
|
|
|
|
|
from .. import entities
|
|
|
|
|
from .. import stage, entities, stagemgr
|
|
|
|
|
from ...core import entities as core_entities
|
|
|
|
|
from ...config import manager as cfg_mgr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@stage.stage_class("MessageProcessor")
|
|
|
|
|
class Processor(stage.PipelineStage):
|
2024-05-14 22:20:31 +08:00
|
|
|
|
"""请求实际处理阶段
|
|
|
|
|
|
|
|
|
|
通过命令处理器和聊天处理器处理消息。
|
|
|
|
|
|
|
|
|
|
改写:
|
|
|
|
|
- resp_messages
|
|
|
|
|
"""
|
2024-01-27 00:06:38 +08:00
|
|
|
|
|
|
|
|
|
cmd_handler: handler.MessageHandler
|
|
|
|
|
|
|
|
|
|
chat_handler: handler.MessageHandler
|
|
|
|
|
|
|
|
|
|
async def initialize(self):
|
|
|
|
|
self.cmd_handler = command.CommandHandler(self.ap)
|
|
|
|
|
self.chat_handler = chat.ChatMessageHandler(self.ap)
|
|
|
|
|
|
|
|
|
|
await self.cmd_handler.initialize()
|
|
|
|
|
await self.chat_handler.initialize()
|
|
|
|
|
|
|
|
|
|
async def process(
|
|
|
|
|
self,
|
|
|
|
|
query: core_entities.Query,
|
|
|
|
|
stage_inst_name: str,
|
|
|
|
|
) -> entities.StageProcessResult:
|
|
|
|
|
"""处理
|
|
|
|
|
"""
|
|
|
|
|
message_text = str(query.message_chain).strip()
|
|
|
|
|
|
2024-02-16 14:11:22 +08:00
|
|
|
|
self.ap.logger.info(f"处理 {query.launcher_type.value}_{query.launcher_id} 的请求({query.query_id}): {message_text}")
|
|
|
|
|
|
2024-02-20 22:56:42 +08:00
|
|
|
|
async def generator():
|
|
|
|
|
if message_text.startswith('!') or message_text.startswith('!'):
|
|
|
|
|
async for result in self.cmd_handler.handle(query):
|
|
|
|
|
yield result
|
|
|
|
|
else:
|
|
|
|
|
async for result in self.chat_handler.handle(query):
|
|
|
|
|
yield result
|
|
|
|
|
|
|
|
|
|
return generator()
|