refactor(配置管理): 重构提供商池配置文件路径处理逻辑
移除默认的provider_pools.json路径,改为可选配置 更新相关UI提示和配置加载逻辑 简化API管理初始化参数
This commit is contained in:
parent
8a782c49f0
commit
0d9e01d137
7 changed files with 21 additions and 24 deletions
|
|
@ -4,6 +4,7 @@ import {
|
|||
API_ACTIONS,
|
||||
ENDPOINT_TYPE
|
||||
} from './common.js';
|
||||
import { getProviderPoolManager } from './service-manager.js';
|
||||
|
||||
/**
|
||||
* Handle API authentication and routing
|
||||
|
|
@ -58,18 +59,16 @@ export async function handleAPIRequests(method, path, req, res, currentConfig, a
|
|||
|
||||
/**
|
||||
* Initialize API management features
|
||||
* @param {Object} config - The server configuration
|
||||
* @param {Object} services - The initialized services
|
||||
* @param {Object} providerPoolManager - The provider pool manager instance
|
||||
* @returns {Function} - The heartbeat and token refresh function
|
||||
*/
|
||||
export function initializeAPIManagement(config, services, providerPoolManager) {
|
||||
export function initializeAPIManagement(services) {
|
||||
return async function heartbeatAndRefreshToken() {
|
||||
console.log(`[Heartbeat] Server is running. Current time: ${new Date().toLocaleString()}`);
|
||||
console.log(`[Heartbeat] Server is running. Current time: ${new Date().toLocaleString()}`, Object.keys(services));
|
||||
// 循环遍历所有已初始化的服务适配器,并尝试刷新令牌
|
||||
if (providerPoolManager) {
|
||||
await providerPoolManager.performHealthChecks(); // 定期执行健康检查
|
||||
}
|
||||
// if (getProviderPoolManager()) {
|
||||
// await getProviderPoolManager().performHealthChecks(); // 定期执行健康检查
|
||||
// }
|
||||
for (const providerKey in services) {
|
||||
const serviceAdapter = services[providerKey];
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -111,10 +111,7 @@ import { createRequestHandler } from './request-handler.js';
|
|||
*/
|
||||
|
||||
import 'dotenv/config'; // Import dotenv and configure it
|
||||
import deepmerge from 'deepmerge';
|
||||
import './converters/register-converters.js'; // 注册所有转换器
|
||||
|
||||
// 导入各个模块功能
|
||||
import { getProviderPoolManager } from './service-manager.js';
|
||||
|
||||
// --- Server Initialization ---
|
||||
|
|
@ -129,7 +126,7 @@ async function startServer() {
|
|||
initializeUIManagement(CONFIG);
|
||||
|
||||
// Initialize API management and get heartbeat function
|
||||
const heartbeatAndRefreshToken = initializeAPIManagement(CONFIG, services, getProviderPoolManager());
|
||||
const heartbeatAndRefreshToken = initializeAPIManagement(services);
|
||||
|
||||
// Create request handler
|
||||
const requestHandlerInstance = createRequestHandler(CONFIG, getProviderPoolManager());
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ export async function initializeConfig(args = process.argv.slice(2), configFileP
|
|||
REQUEST_BASE_DELAY: 1000,
|
||||
CRON_NEAR_MINUTES: 15,
|
||||
CRON_REFRESH_TOKEN: false,
|
||||
PROVIDER_POOLS_FILE_PATH: 'provider_pools.json', // 新增号池配置文件路径
|
||||
PROVIDER_POOLS_FILE_PATH: null, // 新增号池配置文件路径
|
||||
MAX_ERROR_COUNT: 3 // 提供商最大错误次数
|
||||
};
|
||||
console.log('[Config] Using default configuration.');
|
||||
|
|
@ -270,9 +270,9 @@ export async function initializeConfig(args = process.argv.slice(2), configFileP
|
|||
currentConfig.SYSTEM_PROMPT_CONTENT = await getSystemPromptFileContent(currentConfig.SYSTEM_PROMPT_FILE_PATH);
|
||||
|
||||
// 加载号池配置
|
||||
if (!currentConfig.PROVIDER_POOLS_FILE_PATH) {
|
||||
currentConfig.PROVIDER_POOLS_FILE_PATH = 'provider_pools.json';
|
||||
}
|
||||
// if (!currentConfig.PROVIDER_POOLS_FILE_PATH) {
|
||||
// currentConfig.PROVIDER_POOLS_FILE_PATH = 'provider_pools.json';
|
||||
// }
|
||||
if (currentConfig.PROVIDER_POOLS_FILE_PATH) {
|
||||
try {
|
||||
const poolsData = await pfs.readFile(currentConfig.PROVIDER_POOLS_FILE_PATH, 'utf8');
|
||||
|
|
|
|||
|
|
@ -609,11 +609,12 @@ export async function handleUIApiRequests(method, pathParam, req, res, currentCo
|
|||
// Get provider pools summary
|
||||
if (method === 'GET' && pathParam === '/api/providers') {
|
||||
let providerPools = {};
|
||||
const filePath = currentConfig.PROVIDER_POOLS_FILE_PATH || 'provider_pools.json';
|
||||
try {
|
||||
if (providerPoolManager && providerPoolManager.providerPools) {
|
||||
providerPools = providerPoolManager.providerPools;
|
||||
} else if (currentConfig.PROVIDER_POOLS_FILE_PATH && existsSync(currentConfig.PROVIDER_POOLS_FILE_PATH)) {
|
||||
const poolsData = JSON.parse(readFileSync(currentConfig.PROVIDER_POOLS_FILE_PATH, 'utf-8'));
|
||||
} else if (filePath && existsSync(filePath)) {
|
||||
const poolsData = JSON.parse(readFileSync(filePath, 'utf-8'));
|
||||
providerPools = poolsData;
|
||||
}
|
||||
} catch (error) {
|
||||
|
|
@ -630,12 +631,12 @@ export async function handleUIApiRequests(method, pathParam, req, res, currentCo
|
|||
if (method === 'GET' && providerTypeMatch) {
|
||||
const providerType = decodeURIComponent(providerTypeMatch[1]);
|
||||
let providerPools = {};
|
||||
|
||||
const filePath = currentConfig.PROVIDER_POOLS_FILE_PATH || 'provider_pools.json';
|
||||
try {
|
||||
if (providerPoolManager && providerPoolManager.providerPools) {
|
||||
providerPools = providerPoolManager.providerPools;
|
||||
} else if (currentConfig.PROVIDER_POOLS_FILE_PATH && existsSync(currentConfig.PROVIDER_POOLS_FILE_PATH)) {
|
||||
const poolsData = JSON.parse(readFileSync(currentConfig.PROVIDER_POOLS_FILE_PATH, 'utf-8'));
|
||||
} else if (filePath && existsSync(filePath)) {
|
||||
const poolsData = JSON.parse(readFileSync(filePath, 'utf-8'));
|
||||
providerPools = poolsData;
|
||||
}
|
||||
} catch (error) {
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ async function loadConfiguration() {
|
|||
if (requestBaseDelayEl) requestBaseDelayEl.value = data.REQUEST_BASE_DELAY || 1000;
|
||||
if (cronNearMinutesEl) cronNearMinutesEl.value = data.CRON_NEAR_MINUTES || 1;
|
||||
if (cronRefreshTokenEl) cronRefreshTokenEl.checked = data.CRON_REFRESH_TOKEN || false;
|
||||
if (providerPoolsFilePathEl) providerPoolsFilePathEl.value = data.PROVIDER_POOLS_FILE_PATH || 'provider_pools.json';
|
||||
if (providerPoolsFilePathEl) providerPoolsFilePathEl.value = data.PROVIDER_POOLS_FILE_PATH;
|
||||
if (maxErrorCountEl) maxErrorCountEl.value = data.MAX_ERROR_COUNT || 3;
|
||||
|
||||
// 触发提供商配置显示
|
||||
|
|
|
|||
|
|
@ -667,8 +667,8 @@ function showAddProviderForm(providerType) {
|
|||
<div class="form-group">
|
||||
<label>健康检查</label>
|
||||
<select id="newCheckHealth">
|
||||
<option value="true">启用</option>
|
||||
<option value="false">禁用</option>
|
||||
<option value="true">启用</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -614,7 +614,7 @@
|
|||
<input type="number" id="cronNearMinutes" class="form-control" min="1" max="60" value="1">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="cronNearMinutes">启用OAuth令牌自动刷新</label>
|
||||
<label for="cronNearMinutes">启用OAuth令牌自动刷新(需重启服务)</label>
|
||||
<label class="toggle-switch">
|
||||
<input type="checkbox" id="cronRefreshToken">
|
||||
<span class="toggle-slider"></span>
|
||||
|
|
@ -624,7 +624,7 @@
|
|||
|
||||
<div class="form-group pool-section">
|
||||
<label for="providerPoolsFilePath">提供商池配置文件路径</label>
|
||||
<input type="text" id="providerPoolsFilePath" class="form-control" value="provider_pools.json" placeholder="例如: provider_pools.json">
|
||||
<input type="text" id="providerPoolsFilePath" class="form-control" value="" placeholder="例如: provider_pools.json">
|
||||
<small class="form-text">配置了提供商池后,默认使用提供商池的配置,提供商池配置失效降级到默认配置</small>
|
||||
</div>
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue