feat: return 400 error when no available provider in pool
This commit is contained in:
parent
402278240c
commit
369aa59286
2 changed files with 38 additions and 4 deletions
|
|
@ -402,9 +402,24 @@ export async function handleContentGenerationRequest(req, res, service, endpoint
|
||||||
|
|
||||||
// 2.5. 如果使用了提供商池,根据模型重新选择提供商
|
// 2.5. 如果使用了提供商池,根据模型重新选择提供商
|
||||||
if (providerPoolManager && CONFIG.providerPools && CONFIG.providerPools[CONFIG.MODEL_PROVIDER]) {
|
if (providerPoolManager && CONFIG.providerPools && CONFIG.providerPools[CONFIG.MODEL_PROVIDER]) {
|
||||||
const { getApiService } = await import('./service-manager.js');
|
const { getApiService, NoAvailableProviderError } = await import('./service-manager.js');
|
||||||
service = await getApiService(CONFIG, model);
|
try {
|
||||||
console.log(`[Content Generation] Re-selected service adapter based on model: ${model}`);
|
service = await getApiService(CONFIG, model);
|
||||||
|
console.log(`[Content Generation] Re-selected service adapter based on model: ${model}`);
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof NoAvailableProviderError) {
|
||||||
|
// 号池无可用账号,返回 400 错误
|
||||||
|
res.writeHead(400, { 'Content-Type': 'application/json' });
|
||||||
|
res.end(JSON.stringify({
|
||||||
|
error: {
|
||||||
|
type: 'no_available_provider',
|
||||||
|
message: error.message
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Apply system prompt from file if configured.
|
// 3. Apply system prompt from file if configured.
|
||||||
|
|
|
||||||
|
|
@ -56,11 +56,28 @@ export async function initApiService(config) {
|
||||||
return serviceInstances; // Return the collection of initialized service instances
|
return serviceInstances; // Return the collection of initialized service instances
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom error class for no available provider in pool
|
||||||
|
*/
|
||||||
|
export class NoAvailableProviderError extends Error {
|
||||||
|
constructor(providerType, requestedModel = null) {
|
||||||
|
const message = requestedModel
|
||||||
|
? `号池无可用账号: ${providerType} (model: ${requestedModel})`
|
||||||
|
: `号池无可用账号: ${providerType}`;
|
||||||
|
super(message);
|
||||||
|
this.name = 'NoAvailableProviderError';
|
||||||
|
this.providerType = providerType;
|
||||||
|
this.requestedModel = requestedModel;
|
||||||
|
this.statusCode = 400;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get API service adapter, considering provider pools
|
* Get API service adapter, considering provider pools
|
||||||
* @param {Object} config - The current request configuration
|
* @param {Object} config - The current request configuration
|
||||||
* @param {string} [requestedModel] - Optional. The model name to filter providers by.
|
* @param {string} [requestedModel] - Optional. The model name to filter providers by.
|
||||||
* @returns {Promise<Object>} The API service adapter
|
* @returns {Promise<Object>} The API service adapter
|
||||||
|
* @throws {NoAvailableProviderError} If no healthy provider is available in the pool
|
||||||
*/
|
*/
|
||||||
export async function getApiService(config, requestedModel = null) {
|
export async function getApiService(config, requestedModel = null) {
|
||||||
let serviceConfig = config;
|
let serviceConfig = config;
|
||||||
|
|
@ -74,7 +91,9 @@ export async function getApiService(config, requestedModel = null) {
|
||||||
config.uuid = serviceConfig.uuid;
|
config.uuid = serviceConfig.uuid;
|
||||||
console.log(`[API Service] Using pooled configuration for ${config.MODEL_PROVIDER}: ${serviceConfig.uuid}${requestedModel ? ` (model: ${requestedModel})` : ''}`);
|
console.log(`[API Service] Using pooled configuration for ${config.MODEL_PROVIDER}: ${serviceConfig.uuid}${requestedModel ? ` (model: ${requestedModel})` : ''}`);
|
||||||
} else {
|
} else {
|
||||||
console.warn(`[API Service] No healthy provider found in pool for ${config.MODEL_PROVIDER}${requestedModel ? ` supporting model: ${requestedModel}` : ''}. Falling back to main config.`);
|
// 号池没有可用账号,抛出错误
|
||||||
|
console.error(`[API Service] 号池无可用账号: ${config.MODEL_PROVIDER}${requestedModel ? ` (model: ${requestedModel})` : ''}`);
|
||||||
|
throw new NoAvailableProviderError(config.MODEL_PROVIDER, requestedModel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return getServiceAdapter(serviceConfig);
|
return getServiceAdapter(serviceConfig);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue