fix(provider-pool): 修复提供者使用计数和健康状态更新逻辑
更新提供者使用计数时增加最后使用时间记录 修复流式请求和一元请求成功后的使用计数更新 移除不必要的模型映射和配置 调整服务选择时跳过使用计数的逻辑
This commit is contained in:
parent
98086d8027
commit
f851ac8bb2
6 changed files with 29 additions and 12 deletions
|
|
@ -35,9 +35,7 @@ const FULL_MODEL_MAPPING = {
|
|||
"claude-sonnet-4-5": "CLAUDE_SONNET_4_5_20250929_V1_0",
|
||||
"claude-sonnet-4-5-20250929": "CLAUDE_SONNET_4_5_20250929_V1_0",
|
||||
"claude-sonnet-4-20250514": "CLAUDE_SONNET_4_20250514_V1_0",
|
||||
"claude-3-7-sonnet-20250219": "CLAUDE_3_7_SONNET_20250219_V1_0",
|
||||
"amazonq-claude-sonnet-4-20250514": "CLAUDE_SONNET_4_20250514_V1_0",
|
||||
"amazonq-claude-3-7-sonnet-20250219": "CLAUDE_3_7_SONNET_20250219_V1_0"
|
||||
"claude-3-7-sonnet-20250219": "CLAUDE_3_7_SONNET_20250219_V1_0"
|
||||
};
|
||||
|
||||
// 只保留 KIRO_MODELS 中存在的模型映射
|
||||
|
|
|
|||
|
|
@ -233,9 +233,17 @@ export async function handleStreamRequest(res, service, model, requestBody, from
|
|||
// console.log(`data: ${JSON.stringify(getOpenAIStreamChunkStop(model))}\n`);
|
||||
}
|
||||
|
||||
// 流式请求成功完成,统计使用次数,错误次数重置为0
|
||||
if (providerPoolManager && pooluuid) {
|
||||
console.log(`[Provider Pool] Increasing usage count for ${toProvider} (${pooluuid}) after successful stream request`);
|
||||
providerPoolManager.markProviderHealthy(toProvider, {
|
||||
uuid: pooluuid
|
||||
});
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('\n[Server] Error during stream processing:', error.stack);
|
||||
if (providerPoolManager) {
|
||||
if (providerPoolManager && pooluuid) {
|
||||
console.log(`[Provider Pool] Marking ${toProvider} as unhealthy due to stream error`);
|
||||
// 如果是号池模式,并且请求处理失败,则标记当前使用的提供者为不健康
|
||||
providerPoolManager.markProviderUnhealthy(toProvider, {
|
||||
|
|
@ -279,9 +287,18 @@ export async function handleUnaryRequest(res, service, model, requestBody, fromP
|
|||
await handleUnifiedResponse(res, JSON.stringify(clientResponse), false);
|
||||
await logConversation('output', responseText, PROMPT_LOG_MODE, PROMPT_LOG_FILENAME);
|
||||
// fs.writeFile('oldResponse'+Date.now()+'.json', JSON.stringify(clientResponse));
|
||||
|
||||
// 一元请求成功完成,统计使用次数,错误次数重置为0
|
||||
if (providerPoolManager && pooluuid) {
|
||||
console.log(`[Provider Pool] Increasing usage count for ${toProvider} (${pooluuid}) after successful unary request`);
|
||||
providerPoolManager.markProviderHealthy(toProvider, {
|
||||
uuid: pooluuid
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('\n[Server] Error during unary processing:', error.stack);
|
||||
if (providerPoolManager) {
|
||||
if (providerPoolManager && pooluuid) {
|
||||
console.log(`[Provider Pool] Marking ${toProvider} as unhealthy due to stream error`);
|
||||
// 如果是号池模式,并且请求处理失败,则标记当前使用的提供者为不健康
|
||||
providerPoolManager.markProviderUnhealthy(toProvider, {
|
||||
uuid: pooluuid
|
||||
|
|
@ -398,7 +415,7 @@ export async function handleContentGenerationRequest(req, res, service, endpoint
|
|||
// 注意:这里使用 skipUsageCount: true,因为初次选择时已经增加了 usageCount
|
||||
if (providerPoolManager && CONFIG.providerPools && CONFIG.providerPools[CONFIG.MODEL_PROVIDER]) {
|
||||
const { getApiService } = await import('./service-manager.js');
|
||||
service = await getApiService(CONFIG, model, { skipUsageCount: true });
|
||||
service = await getApiService(CONFIG, model);
|
||||
console.log(`[Content Generation] Re-selected service adapter based on model: ${model}`);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -505,7 +505,7 @@ export async function handleOllamaChat(req, res, apiService, currentConfig, prov
|
|||
|
||||
if (detectedProvider !== currentConfig.MODEL_PROVIDER && providerPoolManager) {
|
||||
// Select provider from pool
|
||||
const providerConfig = providerPoolManager.selectProvider(detectedProvider, modelName);
|
||||
const providerConfig = providerPoolManager.selectProvider(detectedProvider, modelName, { skipUsageCount: true });
|
||||
if (providerConfig) {
|
||||
actualConfig = {
|
||||
...currentConfig,
|
||||
|
|
@ -601,7 +601,7 @@ export async function handleOllamaGenerate(req, res, apiService, currentConfig,
|
|||
|
||||
if (detectedProvider !== currentConfig.MODEL_PROVIDER && providerPoolManager) {
|
||||
// Select provider from pool
|
||||
const providerConfig = providerPoolManager.selectProvider(detectedProvider, modelName);
|
||||
const providerConfig = providerPoolManager.selectProvider(detectedProvider, modelName, { skipUsageCount: true });
|
||||
if (providerConfig) {
|
||||
actualConfig = {
|
||||
...currentConfig,
|
||||
|
|
|
|||
|
|
@ -23,13 +23,12 @@ export const PROVIDER_MODELS = {
|
|||
'claude-custom': [],
|
||||
'claude-kiro-oauth': [
|
||||
'claude-opus-4-5',
|
||||
'claude-opus-4-5-20251101',
|
||||
'claude-haiku-4-5',
|
||||
'claude-sonnet-4-5',
|
||||
'claude-sonnet-4-5-20250929',
|
||||
'claude-sonnet-4-20250514',
|
||||
'claude-3-7-sonnet-20250219',
|
||||
'amazonq-claude-sonnet-4-20250514',
|
||||
'amazonq-claude-3-7-sonnet-20250219'
|
||||
'claude-3-7-sonnet-20250219'
|
||||
],
|
||||
'openai-custom': [],
|
||||
'openaiResponses-custom': [],
|
||||
|
|
|
|||
|
|
@ -231,6 +231,9 @@ export class ProviderPoolManager {
|
|||
// 只有在明确要求重置使用计数时才重置
|
||||
if (resetUsageCount) {
|
||||
provider.config.usageCount = 0;
|
||||
}else{
|
||||
provider.config.usageCount++;
|
||||
provider.config.lastUsed = new Date().toISOString();
|
||||
}
|
||||
this._log('info', `Marked provider as healthy: ${provider.config.uuid} for type ${providerType}${resetUsageCount ? ' (usage count reset)' : ''}`);
|
||||
|
||||
|
|
|
|||
|
|
@ -220,7 +220,7 @@ export async function getApiService(config, requestedModel = null, options = {})
|
|||
let serviceConfig = config;
|
||||
if (providerPoolManager && config.providerPools && config.providerPools[config.MODEL_PROVIDER]) {
|
||||
// 如果有号池管理器,并且当前模型提供者类型有对应的号池,则从号池中选择一个提供者配置
|
||||
const selectedProviderConfig = providerPoolManager.selectProvider(config.MODEL_PROVIDER, requestedModel, options);
|
||||
const selectedProviderConfig = providerPoolManager.selectProvider(config.MODEL_PROVIDER, requestedModel, { skipUsageCount: true });
|
||||
if (selectedProviderConfig) {
|
||||
// 合并选中的提供者配置到当前请求的 config 中
|
||||
serviceConfig = deepmerge(config, selectedProviderConfig);
|
||||
|
|
|
|||
Loading…
Reference in a new issue