From 97bb24c5b97338dbe7c45162a804a2f795f9ab38 Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Sat, 4 Jan 2025 23:07:10 +0800 Subject: [PATCH] feat: supports for provider reloading --- pkg/api/http/controller/groups/logs.py | 2 +- pkg/api/http/controller/groups/plugins.py | 12 +++++------ pkg/api/http/controller/groups/settings.py | 6 +++--- pkg/api/http/controller/groups/stats.py | 2 +- pkg/api/http/controller/groups/system.py | 8 ++++---- pkg/core/app.py | 24 +++++++++++++++++++++- pkg/core/entities.py | 1 + web/src/App.vue | 9 +++++++- 8 files changed, 47 insertions(+), 17 deletions(-) diff --git a/pkg/api/http/controller/groups/logs.py b/pkg/api/http/controller/groups/logs.py index 36aa0d75..4244d889 100644 --- a/pkg/api/http/controller/groups/logs.py +++ b/pkg/api/http/controller/groups/logs.py @@ -12,7 +12,7 @@ class LogsRouterGroup(group.RouterGroup): async def initialize(self) -> None: - @self.route('', methods=['GET']) + @self.route('', methods=['GET'], auth_type=group.AuthType.USER_TOKEN) async def _() -> str: start_page_number = int(quart.request.args.get('start_page_number', 0)) diff --git a/pkg/api/http/controller/groups/plugins.py b/pkg/api/http/controller/groups/plugins.py index 86ac7ec6..00951550 100644 --- a/pkg/api/http/controller/groups/plugins.py +++ b/pkg/api/http/controller/groups/plugins.py @@ -13,7 +13,7 @@ class PluginsRouterGroup(group.RouterGroup): async def initialize(self) -> None: - @self.route('', methods=['GET']) + @self.route('', methods=['GET'], auth_type=group.AuthType.USER_TOKEN) async def _() -> str: plugins = self.ap.plugin_mgr.plugins() @@ -23,14 +23,14 @@ async def _() -> str: 'plugins': plugins_data }) - @self.route('///toggle', methods=['PUT']) + @self.route('///toggle', methods=['PUT'], auth_type=group.AuthType.USER_TOKEN) async def _(author: str, plugin_name: str) -> str: data = await quart.request.json target_enabled = data.get('target_enabled') await self.ap.plugin_mgr.update_plugin_switch(plugin_name, target_enabled) return self.success() - @self.route('///update', methods=['POST']) + @self.route('///update', methods=['POST'], auth_type=group.AuthType.USER_TOKEN) async def _(author: str, plugin_name: str) -> str: ctx = taskmgr.TaskContext.new() wrapper = self.ap.task_mgr.create_user_task( @@ -44,7 +44,7 @@ async def _(author: str, plugin_name: str) -> str: 'task_id': wrapper.id }) - @self.route('//', methods=['DELETE']) + @self.route('//', methods=['DELETE'], auth_type=group.AuthType.USER_TOKEN) async def _(author: str, plugin_name: str) -> str: ctx = taskmgr.TaskContext.new() wrapper = self.ap.task_mgr.create_user_task( @@ -59,13 +59,13 @@ async def _(author: str, plugin_name: str) -> str: 'task_id': wrapper.id }) - @self.route('/reorder', methods=['PUT']) + @self.route('/reorder', methods=['PUT'], auth_type=group.AuthType.USER_TOKEN) async def _() -> str: data = await quart.request.json await self.ap.plugin_mgr.reorder_plugins(data.get('plugins')) return self.success() - @self.route('/install/github', methods=['POST']) + @self.route('/install/github', methods=['POST'], auth_type=group.AuthType.USER_TOKEN) async def _() -> str: data = await quart.request.json diff --git a/pkg/api/http/controller/groups/settings.py b/pkg/api/http/controller/groups/settings.py index 00693239..835d86ad 100644 --- a/pkg/api/http/controller/groups/settings.py +++ b/pkg/api/http/controller/groups/settings.py @@ -9,7 +9,7 @@ class SettingsRouterGroup(group.RouterGroup): async def initialize(self) -> None: - @self.route('', methods=['GET']) + @self.route('', methods=['GET'], auth_type=group.AuthType.USER_TOKEN) async def _() -> str: return self.success( data={ @@ -23,7 +23,7 @@ async def _() -> str: } ) - @self.route('/', methods=['GET']) + @self.route('/', methods=['GET'], auth_type=group.AuthType.USER_TOKEN) async def _(manager_name: str) -> str: manager = self.ap.settings_mgr.get_manager(manager_name) @@ -44,7 +44,7 @@ async def _(manager_name: str) -> str: } ) - @self.route('//data', methods=['PUT']) + @self.route('//data', methods=['PUT'], auth_type=group.AuthType.USER_TOKEN) async def _(manager_name: str) -> str: data = await quart.request.json manager = self.ap.settings_mgr.get_manager(manager_name) diff --git a/pkg/api/http/controller/groups/stats.py b/pkg/api/http/controller/groups/stats.py index 45326035..43d56f27 100644 --- a/pkg/api/http/controller/groups/stats.py +++ b/pkg/api/http/controller/groups/stats.py @@ -9,7 +9,7 @@ class StatsRouterGroup(group.RouterGroup): async def initialize(self) -> None: - @self.route('/basic', methods=['GET']) + @self.route('/basic', methods=['GET'], auth_type=group.AuthType.USER_TOKEN) async def _() -> str: conv_count = 0 diff --git a/pkg/api/http/controller/groups/system.py b/pkg/api/http/controller/groups/system.py index f074531a..71d0d8df 100644 --- a/pkg/api/http/controller/groups/system.py +++ b/pkg/api/http/controller/groups/system.py @@ -20,7 +20,7 @@ async def _() -> str: } ) - @self.route('/tasks', methods=['GET']) + @self.route('/tasks', methods=['GET'], auth_type=group.AuthType.USER_TOKEN) async def _() -> str: task_type = quart.request.args.get("type") @@ -31,7 +31,7 @@ async def _() -> str: data=self.ap.task_mgr.get_tasks_dict(task_type) ) - @self.route('/tasks/', methods=['GET']) + @self.route('/tasks/', methods=['GET'], auth_type=group.AuthType.USER_TOKEN) async def _(task_id: str) -> str: task = self.ap.task_mgr.get_task_by_id(int(task_id)) @@ -40,7 +40,7 @@ async def _(task_id: str) -> str: return self.success(data=task.to_dict()) - @self.route('/reload', methods=['POST']) + @self.route('/reload', methods=['POST'], auth_type=group.AuthType.USER_TOKEN) async def _() -> str: json_data = await quart.request.json @@ -51,7 +51,7 @@ async def _() -> str: ) return self.success() - @self.route('/_debug/exec', methods=['POST']) + @self.route('/_debug/exec', methods=['POST'], auth_type=group.AuthType.USER_TOKEN) async def _() -> str: if not constants.debug_mode: return self.http_status(403, 403, "Forbidden") diff --git a/pkg/core/app.py b/pkg/core/app.py index b5d7c809..60cd1d03 100644 --- a/pkg/core/app.py +++ b/pkg/core/app.py @@ -197,5 +197,27 @@ async def reload( await self.plugin_mgr.load_plugins() await self.plugin_mgr.initialize_plugins() + case core_entities.LifecycleControlScope.PROVIDER.value: + self.logger.info("执行热重载 scope="+scope) + + 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 case _: - pass + pass \ No newline at end of file diff --git a/pkg/core/entities.py b/pkg/core/entities.py index 712835b1..502fc73e 100644 --- a/pkg/core/entities.py +++ b/pkg/core/entities.py @@ -23,6 +23,7 @@ class LifecycleControlScope(enum.Enum): APPLICATION = "application" PLATFORM = "platform" PLUGIN = "plugin" + PROVIDER = "provider" class LauncherTypes(enum.Enum): diff --git a/web/src/App.vue b/web/src/App.vue index e1fb3b16..926ad3a7 100644 --- a/web/src/App.vue +++ b/web/src/App.vue @@ -79,6 +79,12 @@ 重载插件 + + + + 重载 LLM 管理器 + + @@ -169,7 +175,8 @@ function openDocs() { const reloadScopeLabel = { 'platform': "消息平台", - 'plugin': "插件" + 'plugin': "插件", + 'provider': "LLM 管理器" } function reload(scope) {