feat: 命令行退出方式

This commit is contained in:
RockChinQ 2024-02-11 23:35:05 +08:00
parent 836df87e18
commit abc19e78b8
5 changed files with 22 additions and 11 deletions

View File

@ -4,6 +4,8 @@ import logging
import asyncio import asyncio
import traceback import traceback
import aioconsole
from ..platform import manager as im_mgr from ..platform import manager as im_mgr
from ..provider.session import sessionmgr as llm_session_mgr from ..provider.session import sessionmgr as llm_session_mgr
from ..provider.requester import modelmgr as llm_model_mgr from ..provider.requester import modelmgr as llm_model_mgr
@ -72,11 +74,21 @@ class Application:
tasks = [] tasks = []
try: try:
tasks = [ tasks = [
asyncio.create_task(self.im_mgr.run()), asyncio.create_task(self.im_mgr.run()),
asyncio.create_task(self.ctrl.run()) asyncio.create_task(self.ctrl.run())
] ]
await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
async def interrupt(tasks):
await asyncio.sleep(1.5)
while await aioconsole.ainput("使用 exit 退出程序 > ") != 'exit':
pass
for task in tasks:
task.cancel()
await interrupt(tasks)
except asyncio.CancelledError: except asyncio.CancelledError:
pass pass

View File

@ -13,7 +13,8 @@ class RandomRespRule(rule_model.GroupRespondRule):
self, self,
message_text: str, message_text: str,
message_chain: mirai.MessageChain, message_chain: mirai.MessageChain,
rule_dict: dict rule_dict: dict,
query: core_entities.Query
) -> entities.RuleJudgeResult: ) -> entities.RuleJudgeResult:
random_rate = rule_dict['random'] random_rate = rule_dict['random']

View File

@ -13,7 +13,8 @@ class RegExpRule(rule_model.GroupRespondRule):
self, self,
message_text: str, message_text: str,
message_chain: mirai.MessageChain, message_chain: mirai.MessageChain,
rule_dict: dict rule_dict: dict,
query: core_entities.Query
) -> entities.RuleJudgeResult: ) -> entities.RuleJudgeResult:
regexps = rule_dict['regexp'] regexps = rule_dict['regexp']

View File

@ -78,13 +78,6 @@ class PlatformManager:
adapter=adapter adapter=adapter
) )
# nakuru不区分好友和陌生人故仅为yirimirai注册陌生人事件
# if self.ap.platform_cfg.data['platform-adapter'] == 'yiri-mirai':
# self.adapter.register_listener(
# StrangerMessage,
# on_stranger_message
# )
async def on_group_message(event: GroupMessage, adapter: msadapter.MessageSourceAdapter): async def on_group_message(event: GroupMessage, adapter: msadapter.MessageSourceAdapter):
event_ctx = await self.ap.plugin_mgr.emit_event( event_ctx = await self.ap.plugin_mgr.emit_event(
@ -187,7 +180,10 @@ class PlatformManager:
tasks = [] tasks = []
for adapter in self.adapters: for adapter in self.adapters:
tasks.append(adapter.run_async()) tasks.append(adapter.run_async())
await asyncio.gather(*tasks)
for task in tasks:
asyncio.create_task(task)
except Exception as e: except Exception as e:
self.ap.logger.error('平台适配器运行出错: ' + str(e)) self.ap.logger.error('平台适配器运行出错: ' + str(e))
self.ap.logger.debug(f"Traceback: {traceback.format_exc()}") self.ap.logger.debug(f"Traceback: {traceback.format_exc()}")

View File

@ -14,3 +14,4 @@ tiktoken
PyYaml PyYaml
aiohttp aiohttp
pydantic pydantic
aioconsole