mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-02 12:05:54 +00:00
feat: applying plugin config to plugin instance
This commit is contained in:
@@ -66,6 +66,22 @@ class PluginsRouterGroup(group.RouterGroup):
|
||||
return self.success(data={
|
||||
'task_id': wrapper.id
|
||||
})
|
||||
|
||||
@self.route('/<author>/<plugin_name>/config', methods=['GET', 'PUT'], auth_type=group.AuthType.USER_TOKEN)
|
||||
async def _(author: str, plugin_name: str) -> quart.Response:
|
||||
plugin = self.ap.plugin_mgr.get_plugin(author, plugin_name)
|
||||
if plugin is None:
|
||||
return self.http_status(404, -1, 'plugin not found')
|
||||
if quart.request.method == 'GET':
|
||||
return self.success(data={
|
||||
'config': plugin.plugin_config
|
||||
})
|
||||
elif quart.request.method == 'PUT':
|
||||
data = await quart.request.json
|
||||
|
||||
await self.ap.plugin_mgr.set_plugin_config(plugin, data)
|
||||
|
||||
return self.success(data={})
|
||||
|
||||
@self.route('/reorder', methods=['PUT'], auth_type=group.AuthType.USER_TOKEN)
|
||||
async def _() -> str:
|
||||
|
||||
@@ -87,9 +87,13 @@ class BasePlugin(metaclass=abc.ABCMeta):
|
||||
ap: app.Application
|
||||
"""应用程序对象"""
|
||||
|
||||
config: dict
|
||||
"""插件配置"""
|
||||
|
||||
def __init__(self, host: APIHost):
|
||||
"""初始化阶段被调用"""
|
||||
self.host = host
|
||||
self.config = {}
|
||||
|
||||
async def initialize(self):
|
||||
"""初始化阶段被调用"""
|
||||
|
||||
@@ -138,6 +138,7 @@ class PluginManager:
|
||||
async def initialize_plugin(self, plugin: context.RuntimeContainer):
|
||||
self.ap.logger.debug(f'初始化插件 {plugin.plugin_name}')
|
||||
plugin.plugin_inst = plugin.plugin_class(self.api_host)
|
||||
plugin.plugin_inst.config = plugin.plugin_config
|
||||
plugin.plugin_inst.ap = self.ap
|
||||
plugin.plugin_inst.host = self.api_host
|
||||
await plugin.plugin_inst.initialize()
|
||||
@@ -359,3 +360,10 @@ class PluginManager:
|
||||
self.plugin_containers.sort(key=lambda x: x.priority, reverse=True)
|
||||
|
||||
await self.dump_plugin_container_setting(self.plugin_containers)
|
||||
|
||||
async def set_plugin_config(self, plugin_container: context.RuntimeContainer, new_config: dict):
|
||||
plugin_container.plugin_config = new_config
|
||||
|
||||
plugin_container.plugin_inst.config = new_config
|
||||
|
||||
await self.dump_plugin_container_setting(plugin_container)
|
||||
|
||||
Reference in New Issue
Block a user