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-10-22 18:09:18 +08:00
|
|
|
|
import threading
|
2024-01-30 14:58:34 +08:00
|
|
|
|
import traceback
|
2024-11-16 12:40:57 +08:00
|
|
|
|
import enum
|
2024-11-16 16:45:13 +08:00
|
|
|
|
import sys
|
2024-11-17 20:43:40 +08:00
|
|
|
|
import os
|
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-10-15 00:07:40 +08:00
|
|
|
|
from ..config import settings as settings_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-10-11 22:27:53 +08:00
|
|
|
|
from ..persistence import mgr as persistencemgr
|
|
|
|
|
|
from ..api.http.controller import main as http_controller
|
2024-11-17 19:11:44 +08:00
|
|
|
|
from ..api.http.service import user as user_service
|
2025-02-22 14:49:05 +08:00
|
|
|
|
from ..discover import engine as discover_engine
|
2024-11-16 12:40:57 +08:00
|
|
|
|
from ..utils import logcache, ip
|
2024-10-22 18:09:18 +08:00
|
|
|
|
from . import taskmgr
|
2024-11-16 12:40:57 +08:00
|
|
|
|
from . import entities as core_entities
|
2025-02-25 12:56:00 +08:00
|
|
|
|
from .bootutils import config
|
2024-01-23 20:55:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Application:
|
2024-03-03 16:34:59 +08:00
|
|
|
|
"""运行时应用对象和上下文"""
|
|
|
|
|
|
|
2024-10-11 22:27:53 +08:00
|
|
|
|
event_loop: asyncio.AbstractEventLoop = None
|
|
|
|
|
|
|
2024-10-22 18:09:18 +08:00
|
|
|
|
# asyncio_tasks: list[asyncio.Task] = []
|
|
|
|
|
|
task_mgr: taskmgr.AsyncTaskManager = None
|
2024-10-11 22:27:53 +08:00
|
|
|
|
|
2025-02-22 14:49:05 +08:00
|
|
|
|
discover: discover_engine.ComponentDiscoveryEngine = None
|
|
|
|
|
|
|
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-10-15 00:07:40 +08:00
|
|
|
|
settings_mgr: settings_mgr.SettingsManager = 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-11-17 19:11:44 +08:00
|
|
|
|
instance_secret_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
|
|
|
|
|
|
|
2024-10-11 22:27:53 +08:00
|
|
|
|
persistence_mgr: persistencemgr.PersistenceManager = None
|
|
|
|
|
|
|
|
|
|
|
|
http_ctrl: http_controller.HTTPController = None
|
|
|
|
|
|
|
|
|
|
|
|
log_cache: logcache.LogCache = None
|
|
|
|
|
|
|
2024-11-17 19:11:44 +08:00
|
|
|
|
# ========= HTTP Services =========
|
|
|
|
|
|
|
|
|
|
|
|
user_service: user_service.UserService = None
|
|
|
|
|
|
|
2024-01-23 20:55:20 +08:00
|
|
|
|
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-30 14:58:34 +08:00
|
|
|
|
try:
|
2024-11-16 16:45:13 +08:00
|
|
|
|
await self.plugin_mgr.initialize_plugins()
|
2024-10-11 22:27:53 +08:00
|
|
|
|
# 后续可能会允许动态重启其他任务
|
|
|
|
|
|
# 故为了防止程序在非 Ctrl-C 情况下退出,这里创建一个不会结束的协程
|
|
|
|
|
|
async def never_ending():
|
|
|
|
|
|
while True:
|
|
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
|
|
|
2024-11-16 12:40:57 +08:00
|
|
|
|
self.task_mgr.create_task(self.platform_mgr.run(), name="platform-manager", scopes=[core_entities.LifecycleControlScope.APPLICATION, core_entities.LifecycleControlScope.PLATFORM])
|
|
|
|
|
|
self.task_mgr.create_task(self.ctrl.run(), name="query-controller", scopes=[core_entities.LifecycleControlScope.APPLICATION])
|
|
|
|
|
|
self.task_mgr.create_task(self.http_ctrl.run(), name="http-api-controller", scopes=[core_entities.LifecycleControlScope.APPLICATION])
|
|
|
|
|
|
self.task_mgr.create_task(never_ending(), name="never-ending-task", scopes=[core_entities.LifecycleControlScope.APPLICATION])
|
2024-10-22 18:09:18 +08:00
|
|
|
|
|
2024-11-16 12:40:57 +08:00
|
|
|
|
await self.print_web_access_info()
|
2024-10-22 18:09:18 +08:00
|
|
|
|
await self.task_mgr.wait_all()
|
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-11-15 20:03:49 +08:00
|
|
|
|
|
2024-11-16 12:40:57 +08:00
|
|
|
|
async def print_web_access_info(self):
|
|
|
|
|
|
"""打印访问 webui 的提示"""
|
2024-11-17 20:43:40 +08:00
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(os.path.join(".", "web/dist")):
|
|
|
|
|
|
self.logger.warning("WebUI 文件缺失,请根据文档获取:https://docs.langbot.app/webui/intro.html")
|
|
|
|
|
|
return
|
|
|
|
|
|
|
2024-12-23 21:35:16 +08:00
|
|
|
|
host_ip = "127.0.0.1"
|
2024-11-16 12:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
public_ip = await ip.get_myip()
|
|
|
|
|
|
|
|
|
|
|
|
port = self.system_cfg.data['http-api']['port']
|
|
|
|
|
|
|
|
|
|
|
|
tips = f"""
|
|
|
|
|
|
=======================================
|
2024-11-16 16:13:02 +08:00
|
|
|
|
✨ 您可通过以下方式访问管理面板
|
2024-11-16 12:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
🏠 本地地址:http://{host_ip}:{port}/
|
|
|
|
|
|
🌐 公网地址:http://{public_ip}:{port}/
|
|
|
|
|
|
|
|
|
|
|
|
📌 如果您在容器中运行此程序,请确保容器的 {port} 端口已对外暴露
|
|
|
|
|
|
🔗 若要使用公网地址访问,请阅读以下须知
|
|
|
|
|
|
1. 公网地址仅供参考,请以您的主机公网 IP 为准;
|
|
|
|
|
|
2. 要使用公网地址访问,请确保您的主机具有公网 IP,并且系统防火墙已放行 {port} 端口;
|
2024-11-16 16:13:02 +08:00
|
|
|
|
|
|
|
|
|
|
🤯 WebUI 仍处于 Beta 测试阶段,如有问题或建议请反馈到 https://github.com/RockChinQ/LangBot/issues
|
2024-11-16 12:40:57 +08:00
|
|
|
|
=======================================
|
|
|
|
|
|
""".strip()
|
|
|
|
|
|
for line in tips.split("\n"):
|
|
|
|
|
|
self.logger.info(line)
|
|
|
|
|
|
|
|
|
|
|
|
async def reload(
|
|
|
|
|
|
self,
|
|
|
|
|
|
scope: core_entities.LifecycleControlScope,
|
|
|
|
|
|
):
|
|
|
|
|
|
match scope:
|
|
|
|
|
|
case core_entities.LifecycleControlScope.PLATFORM.value:
|
|
|
|
|
|
self.logger.info("执行热重载 scope="+scope)
|
|
|
|
|
|
await self.platform_mgr.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
self.platform_mgr = im_mgr.PlatformManager(self)
|
|
|
|
|
|
|
|
|
|
|
|
await self.platform_mgr.initialize()
|
2024-11-15 20:03:49 +08:00
|
|
|
|
|
2024-11-16 12:40:57 +08:00
|
|
|
|
self.task_mgr.create_task(self.platform_mgr.run(), name="platform-manager", scopes=[core_entities.LifecycleControlScope.APPLICATION, core_entities.LifecycleControlScope.PLATFORM])
|
2024-11-16 16:45:13 +08:00
|
|
|
|
case core_entities.LifecycleControlScope.PLUGIN.value:
|
|
|
|
|
|
self.logger.info("执行热重载 scope="+scope)
|
|
|
|
|
|
await self.plugin_mgr.destroy_plugins()
|
|
|
|
|
|
|
|
|
|
|
|
# 删除 sys.module 中所有的 plugins/* 下的模块
|
|
|
|
|
|
for mod in list(sys.modules.keys()):
|
|
|
|
|
|
if mod.startswith("plugins."):
|
|
|
|
|
|
del sys.modules[mod]
|
|
|
|
|
|
|
|
|
|
|
|
self.plugin_mgr = plugin_mgr.PluginManager(self)
|
|
|
|
|
|
await self.plugin_mgr.initialize()
|
|
|
|
|
|
|
|
|
|
|
|
await self.plugin_mgr.initialize_plugins()
|
|
|
|
|
|
|
|
|
|
|
|
await self.plugin_mgr.load_plugins()
|
|
|
|
|
|
await self.plugin_mgr.initialize_plugins()
|
2025-01-04 23:07:10 +08:00
|
|
|
|
case core_entities.LifecycleControlScope.PROVIDER.value:
|
|
|
|
|
|
self.logger.info("执行热重载 scope="+scope)
|
|
|
|
|
|
|
2025-02-25 12:56:00 +08:00
|
|
|
|
latest_llm_model_config = await config.load_json_config("data/metadata/llm-models.json", "templates/metadata/llm-models.json")
|
2025-02-25 16:52:00 +08:00
|
|
|
|
self.llm_models_meta = latest_llm_model_config
|
2025-01-04 23:07:10 +08:00
|
|
|
|
llm_model_mgr_inst = llm_model_mgr.ModelManager(self)
|
|
|
|
|
|
await llm_model_mgr_inst.initialize()
|
|
|
|
|
|
self.model_mgr = llm_model_mgr_inst
|
|
|
|
|
|
|
|
|
|
|
|
llm_session_mgr_inst = llm_session_mgr.SessionManager(self)
|
|
|
|
|
|
await llm_session_mgr_inst.initialize()
|
|
|
|
|
|
self.sess_mgr = llm_session_mgr_inst
|
|
|
|
|
|
|
|
|
|
|
|
llm_prompt_mgr_inst = llm_prompt_mgr.PromptManager(self)
|
|
|
|
|
|
await llm_prompt_mgr_inst.initialize()
|
|
|
|
|
|
self.prompt_mgr = llm_prompt_mgr_inst
|
|
|
|
|
|
|
|
|
|
|
|
llm_tool_mgr_inst = llm_tool_mgr.ToolManager(self)
|
|
|
|
|
|
await llm_tool_mgr_inst.initialize()
|
|
|
|
|
|
self.tool_mgr = llm_tool_mgr_inst
|
|
|
|
|
|
|
|
|
|
|
|
runner_mgr_inst = runnermgr.RunnerManager(self)
|
|
|
|
|
|
await runner_mgr_inst.initialize()
|
|
|
|
|
|
self.runner_mgr = runner_mgr_inst
|
2024-11-16 12:40:57 +08:00
|
|
|
|
case _:
|
2025-01-04 23:07:10 +08:00
|
|
|
|
pass
|