refactor: centralize llm backend configuration
This commit is contained in:
@@ -10,6 +10,7 @@ import psutil
|
||||
from collections import deque
|
||||
import gzip
|
||||
import json
|
||||
import yaml
|
||||
|
||||
# 创建系统信息蓝图
|
||||
system_bp = Blueprint('system', __name__)
|
||||
@@ -156,6 +157,52 @@ def get_current_user_info():
|
||||
return jsonify(result)
|
||||
|
||||
|
||||
@system_bp.route('/api/system/config/raw', methods=['GET'])
|
||||
@login_required
|
||||
def get_system_config_raw():
|
||||
try:
|
||||
server = current_app.dashboard_server
|
||||
config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'config.yaml'))
|
||||
with open(config_path, 'r', encoding='utf-8') as f:
|
||||
config_text = f.read()
|
||||
robot_config = getattr(getattr(server, "robot", None), "config", None)
|
||||
llm_config = getattr(robot_config, "llm", {}) if robot_config else {}
|
||||
llm_backends = (llm_config or {}).get("backends", {})
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"data": config_text,
|
||||
"path": config_path,
|
||||
"llm_backends": list((llm_backends or {}).keys()),
|
||||
})
|
||||
except Exception as e:
|
||||
logger.error(f"读取系统配置失败: {e}")
|
||||
return jsonify({"success": False, "message": str(e)}), 500
|
||||
|
||||
|
||||
@system_bp.route('/api/system/config/update', methods=['POST'])
|
||||
@login_required
|
||||
def update_system_config():
|
||||
try:
|
||||
server = current_app.dashboard_server
|
||||
data = request.get_json() or {}
|
||||
config_text = data.get("config_text")
|
||||
if config_text is None:
|
||||
return jsonify({"success": False, "message": "缺少配置内容"}), 400
|
||||
|
||||
yaml.safe_load(config_text)
|
||||
config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'config.yaml'))
|
||||
with open(config_path, 'w', encoding='utf-8') as f:
|
||||
f.write(config_text)
|
||||
|
||||
if getattr(server, "robot", None) and getattr(server.robot, "config", None):
|
||||
server.robot.config.reload()
|
||||
|
||||
return jsonify({"success": True, "message": "全局配置已保存"})
|
||||
except Exception as e:
|
||||
logger.error(f"保存系统配置失败: {e}")
|
||||
return jsonify({"success": False, "message": str(e)}), 500
|
||||
|
||||
|
||||
@system_bp.route('/api/restart_service', methods=['POST'])
|
||||
@login_required
|
||||
def restart_service():
|
||||
|
||||
@@ -29,6 +29,29 @@
|
||||
<iframe ref="monitorFrame" src="{{ src_url }}" frameborder="0"></iframe>
|
||||
</div>
|
||||
</el-card>
|
||||
|
||||
<el-card class="workspace-card" shadow="hover">
|
||||
<div slot="header" class="workspace-header">
|
||||
<div>
|
||||
<h3>全局配置</h3>
|
||||
<p>集中维护 `config.yaml`,其中 `llm.backends` 用于统一管理所有模型后端。</p>
|
||||
</div>
|
||||
<div class="page-hero-actions">
|
||||
<el-button size="mini" plain @click="loadSystemConfig">刷新配置</el-button>
|
||||
<el-button size="mini" type="success" @click="saveSystemConfig">保存配置</el-button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="config-meta" v-if="configPath">
|
||||
<span>配置文件:{% raw %}{{ configPath }}{% endraw %}</span>
|
||||
<span v-if="llmBackends.length">LLM 后端:{% raw %}{{ llmBackends.join(', ') }}{% endraw %}</span>
|
||||
</div>
|
||||
<el-input
|
||||
type="textarea"
|
||||
v-model="systemConfigText"
|
||||
:rows="18"
|
||||
placeholder="请输入 YAML 格式的全局配置">
|
||||
</el-input>
|
||||
</el-card>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
@@ -42,11 +65,15 @@
|
||||
currentView: '14',
|
||||
showTimeRangeSelector: false,
|
||||
frameUrl: '{{ src_url }}',
|
||||
restarting: false
|
||||
restarting: false,
|
||||
systemConfigText: '',
|
||||
configPath: '',
|
||||
llmBackends: []
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
this.currentView = '14';
|
||||
this.loadSystemConfig();
|
||||
},
|
||||
methods: {
|
||||
reloadIframe() {
|
||||
@@ -81,6 +108,35 @@
|
||||
} finally {
|
||||
this.restarting = false;
|
||||
}
|
||||
},
|
||||
async loadSystemConfig() {
|
||||
try {
|
||||
const response = await axios.get('/api/system/config/raw');
|
||||
if (response.data.success) {
|
||||
this.systemConfigText = response.data.data || '';
|
||||
this.configPath = response.data.path || '';
|
||||
this.llmBackends = response.data.llm_backends || [];
|
||||
} else {
|
||||
this.$message.error(response.data.message || '读取全局配置失败');
|
||||
}
|
||||
} catch (error) {
|
||||
this.$message.error(error.response?.data?.message || '读取全局配置失败');
|
||||
}
|
||||
},
|
||||
async saveSystemConfig() {
|
||||
try {
|
||||
const response = await axios.post('/api/system/config/update', {
|
||||
config_text: this.systemConfigText
|
||||
});
|
||||
if (response.data.success) {
|
||||
this.$message.success(response.data.message || '保存成功');
|
||||
this.loadSystemConfig();
|
||||
} else {
|
||||
this.$message.error(response.data.message || '保存失败');
|
||||
}
|
||||
} catch (error) {
|
||||
this.$message.error(error.response?.data?.message || '保存失败');
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -108,5 +164,7 @@
|
||||
.iframe-shell-card .el-card__body { height: calc(100% - 73px); }
|
||||
.iframe-shell { height: 100%; border-radius: 18px; overflow: hidden; border: 1px solid rgba(148,163,184,0.12); background: rgba(248,250,252,0.82); }
|
||||
.iframe-shell iframe { width: 100%; height: 100%; border: none; display: block; background: #fff; }
|
||||
.workspace-card .el-card__body { display: flex; flex-direction: column; gap: 12px; }
|
||||
.config-meta { display: flex; justify-content: space-between; gap: 12px; color: #64748b; font-size: 12px; }
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user