feat: settings 基础组件

This commit is contained in:
Junyan Qin 2024-10-15 00:07:40 +08:00
parent cd0a8fb24b
commit 7174742886
No known key found for this signature in database
GPG Key ID: 8AC0BEFE1743A015
9 changed files with 258 additions and 9 deletions

View File

@ -0,0 +1,41 @@
import quart
from .....core import app
from .. import group
@group.group_class('settings', '/api/v1/settings')
class SettingsRouterGroup(group.RouterGroup):
async def initialize(self) -> None:
@self.route('', methods=['GET'])
async def _() -> str:
return self.success(
data={
"managers": [
{
"name": m.name,
"description": m.description,
}
for m in self.ap.settings_mgr.get_manager_list()
]
}
)
@self.route('/<manager_name>', methods=['GET'])
async def _(manager_name: str) -> str:
manager = self.ap.settings_mgr.get_manager(manager_name)
return self.success(
data={
"manager": {
"name": manager.name,
"description": manager.description,
"schema": manager.schema,
"file": manager.file.config_file_name,
"data": manager.data
}
}
)

View File

@ -16,4 +16,4 @@ class SystemRouterGroup(group.RouterGroup):
"version": constants.semantic_version,
"debug": constants.debug_mode
}
)
)

View File

@ -5,7 +5,7 @@ import asyncio
import quart
from ....core import app
from .groups import logs, system
from .groups import logs, system, settings
from . import group

View File

@ -4,11 +4,19 @@ from . import model as file_model
from .impls import pymodule, json as json_file, yaml as yaml_file
managers: ConfigManager = []
class ConfigManager:
"""配置文件管理器"""
name: str = None
"""配置管理器名"""
description: str = None
"""配置管理器描述"""
schema: dict = None
"""配置文件 schema
需要符合 JSON Schema Draft 7 规范
"""
file: file_model.ConfigFile = None
"""配置文件实例"""

73
pkg/config/settings.py Normal file
View File

@ -0,0 +1,73 @@
from __future__ import annotations
from . import manager as config_manager
from ..core import app
class SettingsManager:
"""设置管理器
保存管理多个配置文件管理器
"""
ap: app.Application
managers: list[config_manager.ConfigManager] = []
"""配置文件管理器列表"""
def __init__(self, ap: app.Application) -> None:
self.ap = ap
self.managers = []
async def initialize(self) -> None:
pass
def register_manager(
self,
name: str,
description: str,
manager: config_manager.ConfigManager,
schema: dict=None,
) -> None:
"""注册配置管理器
Args:
name (str): 配置管理器名
description (str): 配置管理器描述
manager (ConfigManager): 配置管理器
schema (dict): 配置文件 schema符合 JSON Schema Draft 7 规范
"""
for m in self.managers:
if m.name == name:
raise ValueError(f'配置管理器名 {name} 已存在')
manager.name = name
manager.description = description
manager.schema = schema
self.managers.append(manager)
def get_manager(self, name: str) -> config_manager.ConfigManager:
"""获取配置管理器
Args:
name (str): 配置管理器名
Returns:
ConfigManager: 配置管理器
"""
for m in self.managers:
if m.name == name:
return m
raise ValueError(f'配置管理器 {name} 不存在')
def get_manager_list(self) -> list[config_manager.ConfigManager]:
"""获取配置管理器列表
Returns:
list[ConfigManager]: 配置管理器列表
"""
return self.managers

View File

@ -11,6 +11,7 @@ from ..provider.sysprompt import sysprompt as llm_prompt_mgr
from ..provider.tools import toolmgr as llm_tool_mgr
from ..provider import runnermgr
from ..config import manager as config_mgr
from ..config import settings as settings_mgr
from ..audit.center import v2 as center_mgr
from ..command import cmdmgr
from ..plugin import manager as plugin_mgr
@ -43,6 +44,8 @@ class Application:
runner_mgr: runnermgr.RunnerManager = None
settings_mgr: settings_mgr.SettingsManager = None
# ======= 配置管理器 =======
command_cfg: config_mgr.ConfigManager = None

View File

@ -2,6 +2,7 @@ from __future__ import annotations
from .. import stage, app
from ..bootutils import config
from ...config import settings as settings_mgr
@stage.stage_class("LoadConfigStage")
@ -12,12 +13,46 @@ class LoadConfigStage(stage.BootingStage):
async def run(self, ap: app.Application):
"""启动
"""
ap.settings_mgr = settings_mgr.SettingsManager(ap)
await ap.settings_mgr.initialize()
ap.command_cfg = await config.load_json_config("data/config/command.json", "templates/command.json", completion=False)
ap.pipeline_cfg = await config.load_json_config("data/config/pipeline.json", "templates/pipeline.json", completion=False)
ap.platform_cfg = await config.load_json_config("data/config/platform.json", "templates/platform.json", completion=False)
ap.provider_cfg = await config.load_json_config("data/config/provider.json", "templates/provider.json", completion=False)
ap.system_cfg = await config.load_json_config("data/config/system.json", "templates/system.json", completion=False)
ap.settings_mgr.register_manager(
name="command.json",
description="命令配置",
manager=ap.command_cfg
)
ap.settings_mgr.register_manager(
name="pipeline.json",
description="消息处理流水线配置",
manager=ap.pipeline_cfg
)
ap.settings_mgr.register_manager(
name="platform.json",
description="消息平台配置",
manager=ap.platform_cfg
)
ap.settings_mgr.register_manager(
name="provider.json",
description="大模型能力配置",
manager=ap.provider_cfg
)
ap.settings_mgr.register_manager(
name="system.json",
description="系统配置",
manager=ap.system_cfg
)
ap.plugin_setting_meta = await config.load_json_config("plugins/plugins.json", "templates/plugin-settings.json")
await ap.plugin_setting_meta.dump_config()

View File

@ -68,7 +68,7 @@ onUnmounted(() => {
margin: 1rem;
margin-top: 1rem;
height: 3rem;
border-radius: 1rem;
border-radius: 0.3rem;
}
.toolbar-component {
@ -80,7 +80,7 @@ onUnmounted(() => {
margin: 1rem;
margin-top: 1rem;
height: calc(100vh - 9.5rem);
border-radius: 1rem;
border-radius: 0.3rem;
}

View File

@ -1,13 +1,102 @@
<template>
<PageTitle title="设置" @refresh="refresh" />
<PageTitle title="设置" @refresh="refresh" />
<v-card id="settings-card">
<v-tabs id="settings-tabs" v-model="tab" show-arrows center-active>
<v-tab v-for="manager in managerList" :key="manager.name" :value="manager.name">{{ manager.name }}</v-tab>
</v-tabs>
<v-tabs-window id="settings-tab-window" v-model="tab">
<v-tabs-window-item v-for="manager in managerList" :key="manager.name" :value="manager.name"
class="config-tab-window">
<v-card class="config-tab-toolbar">
<v-tooltip :text="manager.schema == null ? '仅配置文件管理器提供了 JSON Schema 时支持可视化配置' : '切换编辑模式'" location="top">
<template v-slot:activator="{ props }">
<v-btn-toggle id="config-type-toggle" color="primary" v-model="configType" mandatory v-bind="props">
<v-btn class="config-type-toggle-btn" value="ui" :readonly="manager.schema == null">
<v-icon>mdi-view-dashboard-edit-outline</v-icon>
</v-btn>
<v-btn class="config-type-toggle-btn" value="json" :readonly="manager.schema == null">
<v-icon>mdi-code-json</v-icon>
</v-btn>
</v-btn-toggle>
</template>
</v-tooltip>
</v-card>
</v-tabs-window-item>
</v-tabs-window>
</v-card>
</template>
<script setup>
import PageTitle from '@/components/PageTitle.vue'
import { ref, getCurrentInstance, onMounted, onUnmounted } from 'vue'
const { proxy } = getCurrentInstance()
const managerList = ref([])
const tab = ref('')
const configType = ref('json') // ui or json
const refresh = () => {
proxy.$axios.get('/settings').then(response => {
managerList.value = response.data.data.managers
})
}
onMounted(async () => {
refresh()
})
</script>
<style scoped>
#settings-card {
margin: 1rem;
height: calc(100% - 5rem);
/* max-height: calc(100% - 5rem); */
display: flex;
flex-direction: column;
overflow: hidden;
/* background-color: #f0f0f0; */
}
</style>
#settings-tabs {
height: 3rem;
overflow: hidden;
}
#settings-tab-window {
height: calc(100vh - 9rem);
overflow: hidden;
/* background-color: aqua; */
}
.config-tab-window {
overflow: auto;
}
.config-tab-toolbar {
margin: 0.5rem;
height: 4rem;
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
}
#config-type-toggle {
margin: 0.5rem;
box-shadow: 0 0 0 2px #dddddd;
}
.config-type-toggle-btn {}
.config-tab-content {
margin: 0.2rem;
}
</style>