mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-12 00:36:03 +00:00
feat: discovering plugins by manifests
This commit is contained in:
@@ -5,7 +5,7 @@ import traceback
|
||||
|
||||
from ..core import app, taskmgr
|
||||
from . import context, loader, events, installer, setting, models
|
||||
from .loaders import classic
|
||||
from .loaders import classic, manifest
|
||||
from .installers import github
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ class PluginManager:
|
||||
|
||||
ap: app.Application
|
||||
|
||||
loader: loader.PluginLoader
|
||||
loaders: list[loader.PluginLoader]
|
||||
|
||||
installer: installer.PluginInstaller
|
||||
|
||||
@@ -22,6 +22,8 @@ class PluginManager:
|
||||
|
||||
api_host: context.APIHost
|
||||
|
||||
plugin_containers: list[context.RuntimeContainer]
|
||||
|
||||
def plugins(
|
||||
self,
|
||||
enabled: bool=None,
|
||||
@@ -29,7 +31,7 @@ class PluginManager:
|
||||
) -> list[context.RuntimeContainer]:
|
||||
"""获取插件列表
|
||||
"""
|
||||
plugins = self.loader.plugins
|
||||
plugins = self.plugin_containers
|
||||
|
||||
if enabled is not None:
|
||||
plugins = [plugin for plugin in plugins if plugin.enabled == enabled]
|
||||
@@ -41,13 +43,18 @@ class PluginManager:
|
||||
|
||||
def __init__(self, ap: app.Application):
|
||||
self.ap = ap
|
||||
self.loader = classic.PluginLoader(ap)
|
||||
self.loaders = [
|
||||
classic.PluginLoader(ap),
|
||||
manifest.PluginManifestLoader(ap),
|
||||
]
|
||||
self.installer = github.GitHubRepoInstaller(ap)
|
||||
self.setting = setting.SettingManager(ap)
|
||||
self.api_host = context.APIHost(ap)
|
||||
self.plugin_containers = []
|
||||
|
||||
async def initialize(self):
|
||||
await self.loader.initialize()
|
||||
for loader in self.loaders:
|
||||
await loader.initialize()
|
||||
await self.installer.initialize()
|
||||
await self.setting.initialize()
|
||||
await self.api_host.initialize()
|
||||
@@ -55,14 +62,16 @@ class PluginManager:
|
||||
setattr(models, 'require_ver', self.api_host.require_ver)
|
||||
|
||||
async def load_plugins(self):
|
||||
await self.loader.load_plugins()
|
||||
for loader in self.loaders:
|
||||
await loader.load_plugins()
|
||||
self.plugin_containers.extend(loader.plugins)
|
||||
|
||||
await self.setting.sync_setting(self.loader.plugins)
|
||||
await self.setting.sync_setting(self.plugin_containers)
|
||||
|
||||
# 按优先级倒序
|
||||
self.loader.plugins.sort(key=lambda x: x.priority, reverse=True)
|
||||
self.plugin_containers.sort(key=lambda x: x.priority, reverse=True)
|
||||
|
||||
self.ap.logger.debug(f'优先级排序后的插件列表 {self.loader.plugins}')
|
||||
self.ap.logger.debug(f'优先级排序后的插件列表 {self.plugin_containers}')
|
||||
|
||||
async def initialize_plugin(self, plugin: context.RuntimeContainer):
|
||||
self.ap.logger.debug(f'初始化插件 {plugin.plugin_name}')
|
||||
@@ -147,7 +156,7 @@ class PluginManager:
|
||||
await self.ap.ctr_mgr.plugin.post_remove_record(
|
||||
{
|
||||
"name": plugin_name,
|
||||
"remote": plugin_container.plugin_source,
|
||||
"remote": plugin_container.plugin_repository,
|
||||
"author": plugin_container.plugin_author,
|
||||
"version": plugin_container.plugin_version
|
||||
}
|
||||
@@ -171,7 +180,7 @@ class PluginManager:
|
||||
await self.ap.ctr_mgr.plugin.post_update_record(
|
||||
plugin={
|
||||
"name": plugin_name,
|
||||
"remote": plugin_container.plugin_source,
|
||||
"remote": plugin_container.plugin_repository,
|
||||
"author": plugin_container.plugin_author,
|
||||
"version": plugin_container.plugin_version
|
||||
},
|
||||
@@ -238,7 +247,7 @@ class PluginManager:
|
||||
plugins_info: list[dict] = [
|
||||
{
|
||||
'name': plugin.plugin_name,
|
||||
'remote': plugin.plugin_source,
|
||||
'remote': plugin.plugin_repository,
|
||||
'version': plugin.plugin_version,
|
||||
'author': plugin.plugin_author
|
||||
} for plugin in emitted_plugins
|
||||
@@ -266,7 +275,7 @@ class PluginManager:
|
||||
|
||||
plugin.enabled = new_status
|
||||
|
||||
await self.setting.dump_container_setting(self.loader.plugins)
|
||||
await self.setting.dump_container_setting(self.plugin_containers)
|
||||
|
||||
break
|
||||
|
||||
@@ -280,11 +289,11 @@ class PluginManager:
|
||||
plugin_name = plugin.get('name')
|
||||
plugin_priority = plugin.get('priority')
|
||||
|
||||
for plugin in self.loader.plugins:
|
||||
for plugin in self.plugin_containers:
|
||||
if plugin.plugin_name == plugin_name:
|
||||
plugin.priority = plugin_priority
|
||||
break
|
||||
|
||||
self.loader.plugins.sort(key=lambda x: x.priority, reverse=True)
|
||||
self.plugin_containers.sort(key=lambda x: x.priority, reverse=True)
|
||||
|
||||
await self.setting.dump_container_setting(self.loader.plugins)
|
||||
await self.setting.dump_container_setting(self.plugin_containers)
|
||||
|
||||
Reference in New Issue
Block a user