From f51d345ad9e8bf7a28511a1f9f870691ff0eed93 Mon Sep 17 00:00:00 2001 From: Edric Li Date: Mon, 8 Sep 2025 15:39:14 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"feat:=20=E5=B0=86=E8=B4=B9=E7=94=A8?= =?UTF-8?q?=E4=BC=98=E5=85=88=E8=B0=83=E5=BA=A6=E9=80=BB=E8=BE=91=E9=9B=86?= =?UTF-8?q?=E6=88=90=E5=88=B0=20UnifiedClaudeScheduler"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 1a5b3b614961e889a0700809e3e86b08eccb5e19. --- src/services/unifiedClaudeScheduler.js | 115 ++++--------------------- 1 file changed, 15 insertions(+), 100 deletions(-) diff --git a/src/services/unifiedClaudeScheduler.js b/src/services/unifiedClaudeScheduler.js index 1f17a680..7bceb040 100644 --- a/src/services/unifiedClaudeScheduler.js +++ b/src/services/unifiedClaudeScheduler.js @@ -206,8 +206,8 @@ class UnifiedClaudeScheduler { } } - // 基于日费用排序账户(费用最少的优先) - const sortedAccounts = await this._sortAccountsByCost(availableAccounts) + // 按优先级和最后使用时间排序 + const sortedAccounts = this._sortAccountsByPriority(availableAccounts) // 选择第一个账户 const selectedAccount = sortedAccounts[0] @@ -482,104 +482,19 @@ class UnifiedClaudeScheduler { return availableAccounts } - // 💰 基于日费用排序账户(费用最少的优先,支持多种账户类型) - async _sortAccountsByCost(accounts) { - try { - // 并行获取所有账号的日费用 - const accountsWithCost = await Promise.all( - accounts.map(async (account) => { - try { - let dailyCost = 0 - - // 根据账户类型获取日费用 - if (account.accountType === 'claude-official') { - dailyCost = await redis.getAccountDailyCost(account.accountId || account.id) - } else if (account.accountType === 'claude-console') { - // Claude Console 账户也使用相同的费用存储机制 - dailyCost = await redis.getAccountDailyCost(account.accountId || account.id) - } else if (account.accountType === 'bedrock') { - // Bedrock 账户也使用相同的费用存储机制 - dailyCost = await redis.getAccountDailyCost(account.accountId || account.id) - } - - return { - ...account, - _dailyCost: dailyCost - } - } catch (error) { - logger.warn( - `Failed to get daily cost for account ${account.accountId || account.id}: ${error.message}` - ) - return { - ...account, - _dailyCost: Number.MAX_SAFE_INTEGER, // 获取费用失败时,设为最高值(最低优先级) - _costError: true - } - } - }) - ) - - // 按日费用排序(费用最少的优先) - const sortedAccounts = accountsWithCost.sort((a, b) => { - // 如果费用相同,按优先级排序 - if (Math.abs(a._dailyCost - b._dailyCost) < 0.000001) { - // 优先级相同时,按最后使用时间排序(最久未使用的优先) - if (a.priority === b.priority) { - const aLastUsed = new Date(a.lastUsedAt || 0).getTime() - const bLastUsed = new Date(b.lastUsedAt || 0).getTime() - return aLastUsed - bLastUsed - } - return a.priority - b.priority - } - return a._dailyCost - b._dailyCost - }) - - // 检查是否所有账号的费用获取都失败了 - const allAccountsHaveErrors = sortedAccounts.every((account) => account._costError) - - if (allAccountsHaveErrors) { - logger.warn( - '⚠️ All accounts failed to get daily cost, falling back to priority-based sorting' - ) - return accounts.sort((a, b) => { - // 首先按优先级排序 - if (a.priority !== b.priority) { - return a.priority - b.priority - } - // 优先级相同时,按最后使用时间排序 - const aLastUsed = new Date(a.lastUsedAt || 0).getTime() - const bLastUsed = new Date(b.lastUsedAt || 0).getTime() - return aLastUsed - bLastUsed - }) + // 🔢 按优先级和最后使用时间排序账户 + _sortAccountsByPriority(accounts) { + return accounts.sort((a, b) => { + // 首先按优先级排序(数字越小优先级越高) + if (a.priority !== b.priority) { + return a.priority - b.priority } - logger.debug('💰 Account cost ranking:') - sortedAccounts.forEach((account, index) => { - const costDisplay = account._costError ? 'ERROR' : `$${account._dailyCost.toFixed(4)}` - const accountDisplayName = account.name || account.accountId || account.id - logger.debug( - ` ${index + 1}. ${accountDisplayName} (${account.accountType}): ${costDisplay}` - ) - }) - - return sortedAccounts - } catch (error) { - logger.error( - '❌ Failed to sort accounts by cost, falling back to priority-based sorting:', - error - ) - // 回退到原有的按优先级排序策略 - return accounts.sort((a, b) => { - // 首先按优先级排序 - if (a.priority !== b.priority) { - return a.priority - b.priority - } - // 优先级相同时,按最后使用时间排序 - const aLastUsed = new Date(a.lastUsedAt || 0).getTime() - const bLastUsed = new Date(b.lastUsedAt || 0).getTime() - return aLastUsed - bLastUsed - }) - } + // 优先级相同时,按最后使用时间排序(最久未使用的优先) + const aLastUsed = new Date(a.lastUsedAt || 0).getTime() + const bLastUsed = new Date(b.lastUsedAt || 0).getTime() + return aLastUsed - bLastUsed + }) } // 🔍 检查账户是否可用 @@ -961,8 +876,8 @@ class UnifiedClaudeScheduler { throw new Error(`No available accounts in group ${group.name}`) } - // 使用基于费用的排序逻辑 - const sortedAccounts = await this._sortAccountsByCost(availableAccounts) + // 使用现有的优先级排序逻辑 + const sortedAccounts = this._sortAccountsByPriority(availableAccounts) // 选择第一个账户 const selectedAccount = sortedAccounts[0]