2024-01-23 20:55:20 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
2024-01-26 15:51:49 +08:00
|
|
|
import asyncio
|
2024-01-30 14:58:34 +08:00
|
|
|
import traceback
|
2024-01-23 20:55:20 +08:00
|
|
|
|
2024-01-29 21:22:27 +08:00
|
|
|
from ..platform import manager as im_mgr
|
2024-01-28 19:20:10 +08:00
|
|
|
from ..provider.session import sessionmgr as llm_session_mgr
|
2024-03-12 16:04:11 +08:00
|
|
|
from ..provider.modelmgr import modelmgr as llm_model_mgr
|
2024-01-28 19:20:10 +08:00
|
|
|
from ..provider.sysprompt import sysprompt as llm_prompt_mgr
|
|
|
|
from ..provider.tools import toolmgr as llm_tool_mgr
|
2024-07-28 18:45:27 +08:00
|
|
|
from ..provider import runnermgr
|
2024-01-23 20:55:20 +08:00
|
|
|
from ..config import manager as config_mgr
|
2024-01-30 16:13:33 +08:00
|
|
|
from ..audit.center import v2 as center_mgr
|
2024-01-28 00:16:42 +08:00
|
|
|
from ..command import cmdmgr
|
2024-01-29 21:22:27 +08:00
|
|
|
from ..plugin import manager as plugin_mgr
|
2024-02-23 17:46:22 +08:00
|
|
|
from ..pipeline import pool
|
2024-02-23 17:20:57 +08:00
|
|
|
from ..pipeline import controller, stagemgr
|
2024-07-03 17:34:23 +08:00
|
|
|
from ..utils import version as version_mgr, proxy as proxy_mgr, announce as announce_mgr
|
2024-01-23 20:55:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Application:
|
2024-03-03 16:34:59 +08:00
|
|
|
"""运行时应用对象和上下文"""
|
|
|
|
|
2024-03-22 17:09:43 +08:00
|
|
|
platform_mgr: im_mgr.PlatformManager = None
|
2024-01-23 20:55:20 +08:00
|
|
|
|
2024-01-28 00:16:42 +08:00
|
|
|
cmd_mgr: cmdmgr.CommandManager = None
|
|
|
|
|
2024-01-27 00:06:38 +08:00
|
|
|
sess_mgr: llm_session_mgr.SessionManager = None
|
|
|
|
|
|
|
|
model_mgr: llm_model_mgr.ModelManager = None
|
|
|
|
|
|
|
|
prompt_mgr: llm_prompt_mgr.PromptManager = None
|
|
|
|
|
2024-01-27 21:50:40 +08:00
|
|
|
tool_mgr: llm_tool_mgr.ToolManager = None
|
|
|
|
|
2024-07-28 18:45:27 +08:00
|
|
|
runner_mgr: runnermgr.RunnerManager = None
|
|
|
|
|
2024-03-16 15:41:59 +08:00
|
|
|
# ======= 配置管理器 =======
|
|
|
|
|
2024-02-06 21:26:03 +08:00
|
|
|
command_cfg: config_mgr.ConfigManager = None
|
2024-01-23 20:55:20 +08:00
|
|
|
|
2024-02-06 21:26:03 +08:00
|
|
|
pipeline_cfg: config_mgr.ConfigManager = None
|
|
|
|
|
|
|
|
platform_cfg: config_mgr.ConfigManager = None
|
|
|
|
|
|
|
|
provider_cfg: config_mgr.ConfigManager = None
|
|
|
|
|
|
|
|
system_cfg: config_mgr.ConfigManager = None
|
2024-01-23 20:55:20 +08:00
|
|
|
|
2024-03-16 15:41:59 +08:00
|
|
|
# ======= 元数据配置管理器 =======
|
|
|
|
|
|
|
|
sensitive_meta: config_mgr.ConfigManager = None
|
|
|
|
|
|
|
|
adapter_qq_botpy_meta: config_mgr.ConfigManager = None
|
|
|
|
|
|
|
|
plugin_setting_meta: config_mgr.ConfigManager = None
|
|
|
|
|
2024-03-16 21:43:09 +08:00
|
|
|
llm_models_meta: config_mgr.ConfigManager = None
|
|
|
|
|
2024-03-16 15:41:59 +08:00
|
|
|
# =========================
|
|
|
|
|
2024-01-30 16:13:33 +08:00
|
|
|
ctr_mgr: center_mgr.V2CenterAPI = None
|
2024-01-23 20:55:20 +08:00
|
|
|
|
2024-01-29 21:22:27 +08:00
|
|
|
plugin_mgr: plugin_mgr.PluginManager = None
|
2024-01-23 20:55:20 +08:00
|
|
|
|
2024-01-26 15:51:49 +08:00
|
|
|
query_pool: pool.QueryPool = None
|
|
|
|
|
|
|
|
ctrl: controller.Controller = None
|
|
|
|
|
|
|
|
stage_mgr: stagemgr.StageManager = None
|
|
|
|
|
2024-01-29 21:22:27 +08:00
|
|
|
ver_mgr: version_mgr.VersionManager = None
|
|
|
|
|
2024-07-03 17:34:23 +08:00
|
|
|
ann_mgr: announce_mgr.AnnouncementManager = None
|
|
|
|
|
2024-01-29 21:22:27 +08:00
|
|
|
proxy_mgr: proxy_mgr.ProxyManager = None
|
|
|
|
|
2024-01-23 20:55:20 +08:00
|
|
|
logger: logging.Logger = None
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
2024-01-27 21:50:40 +08:00
|
|
|
async def initialize(self):
|
2024-01-29 21:22:27 +08:00
|
|
|
pass
|
2024-01-23 22:28:30 +08:00
|
|
|
|
2024-01-27 21:50:40 +08:00
|
|
|
async def run(self):
|
2024-01-29 21:41:20 +08:00
|
|
|
await self.plugin_mgr.initialize_plugins()
|
2024-01-27 21:50:40 +08:00
|
|
|
|
2024-02-07 20:03:46 +08:00
|
|
|
tasks = []
|
|
|
|
|
2024-01-30 14:58:34 +08:00
|
|
|
try:
|
2024-03-20 15:48:11 +08:00
|
|
|
|
2024-01-30 16:13:33 +08:00
|
|
|
tasks = [
|
2024-03-22 17:09:43 +08:00
|
|
|
asyncio.create_task(self.platform_mgr.run()),
|
2024-01-30 16:13:33 +08:00
|
|
|
asyncio.create_task(self.ctrl.run())
|
|
|
|
]
|
2024-02-11 23:35:05 +08:00
|
|
|
|
2024-03-03 16:34:59 +08:00
|
|
|
# 挂信号处理
|
2024-02-19 19:27:42 +08:00
|
|
|
|
|
|
|
import signal
|
|
|
|
|
|
|
|
def signal_handler(sig, frame):
|
2024-02-11 23:35:05 +08:00
|
|
|
for task in tasks:
|
|
|
|
task.cancel()
|
2024-02-19 19:27:42 +08:00
|
|
|
self.logger.info("程序退出.")
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
|
|
|
|
await asyncio.gather(*tasks, return_exceptions=True)
|
2024-01-30 14:58:34 +08:00
|
|
|
|
|
|
|
except asyncio.CancelledError:
|
2024-02-07 20:03:46 +08:00
|
|
|
pass
|
2024-01-30 14:58:34 +08:00
|
|
|
except Exception as e:
|
|
|
|
self.logger.error(f"应用运行致命异常: {e}")
|
|
|
|
self.logger.debug(f"Traceback: {traceback.format_exc()}")
|
2024-02-19 19:27:42 +08:00
|
|
|
|