fix: 修复claude SSE捕获usage问题

This commit is contained in:
shaw
2025-10-12 23:05:48 +08:00
parent 6f6c274877
commit a67c34bee1
9 changed files with 43 additions and 42 deletions

View File

@@ -7614,7 +7614,10 @@ router.put('/openai-accounts/:id', authenticateAdmin, async (req, res) => {
updateData.subscriptionExpiresAt = updates.expiresAt
}
if (!hasOauthExpiry && Object.prototype.hasOwnProperty.call(updateData, 'subscriptionExpiresAt')) {
if (
!hasOauthExpiry &&
Object.prototype.hasOwnProperty.call(updateData, 'subscriptionExpiresAt')
) {
delete updateData.expiresAt
}

View File

@@ -146,9 +146,7 @@ async function createAccount(accountData) {
isActive: accountData.isActive !== false ? 'true' : 'false',
status: 'active',
schedulable: accountData.schedulable !== false ? 'true' : 'false',
subscriptionExpiresAt: normalizeSubscriptionExpiresAt(
accountData.subscriptionExpiresAt || ''
),
subscriptionExpiresAt: normalizeSubscriptionExpiresAt(accountData.subscriptionExpiresAt || ''),
createdAt: now,
updatedAt: now
}
@@ -243,9 +241,7 @@ async function updateAccount(accountId, updates) {
}
if (Object.prototype.hasOwnProperty.call(updates, 'subscriptionExpiresAt')) {
updates.subscriptionExpiresAt = normalizeSubscriptionExpiresAt(
updates.subscriptionExpiresAt
)
updates.subscriptionExpiresAt = normalizeSubscriptionExpiresAt(updates.subscriptionExpiresAt)
} else if (Object.prototype.hasOwnProperty.call(updates, 'expiresAt')) {
updates.subscriptionExpiresAt = normalizeSubscriptionExpiresAt(updates.expiresAt)
delete updates.expiresAt

View File

@@ -162,15 +162,15 @@ class BedrockAccountService {
accountType: account.accountType,
priority: account.priority,
schedulable: account.schedulable,
credentialType: account.credentialType,
createdAt: account.createdAt,
updatedAt: account.updatedAt,
type: 'bedrock',
hasCredentials: !!account.awsCredentials,
expiresAt: account.expiresAt || null,
subscriptionExpiresAt: account.subscriptionExpiresAt || null
})
}
credentialType: account.credentialType,
createdAt: account.createdAt,
updatedAt: account.updatedAt,
type: 'bedrock',
hasCredentials: !!account.awsCredentials,
expiresAt: account.expiresAt || null,
subscriptionExpiresAt: account.subscriptionExpiresAt || null
})
}
}
// 按优先级和名称排序

View File

@@ -183,14 +183,14 @@ class CcrAccountService {
schedulable: accountData.schedulable !== 'false', // 默认为true只有明确设置为false才不可调度
// 额度管理相关
dailyQuota: parseFloat(accountData.dailyQuota || '0'),
dailyUsage: parseFloat(accountData.dailyUsage || '0'),
lastResetDate: accountData.lastResetDate || '',
quotaResetTime: accountData.quotaResetTime || '00:00',
quotaStoppedAt: accountData.quotaStoppedAt || null,
expiresAt: accountData.expiresAt || null,
subscriptionExpiresAt: accountData.subscriptionExpiresAt || null
})
}
dailyUsage: parseFloat(accountData.dailyUsage || '0'),
lastResetDate: accountData.lastResetDate || '',
quotaResetTime: accountData.quotaResetTime || '00:00',
quotaStoppedAt: accountData.quotaStoppedAt || null,
expiresAt: accountData.expiresAt || null,
subscriptionExpiresAt: accountData.subscriptionExpiresAt || null
})
}
}
return accounts

View File

@@ -186,14 +186,14 @@ class ClaudeConsoleAccountService {
schedulable: accountData.schedulable !== 'false', // 默认为true只有明确设置为false才不可调度
// 额度管理相关
dailyQuota: parseFloat(accountData.dailyQuota || '0'),
dailyUsage: parseFloat(accountData.dailyUsage || '0'),
lastResetDate: accountData.lastResetDate || '',
quotaResetTime: accountData.quotaResetTime || '00:00',
quotaStoppedAt: accountData.quotaStoppedAt || null,
expiresAt: accountData.expiresAt || null,
subscriptionExpiresAt: accountData.subscriptionExpiresAt || null
})
}
dailyUsage: parseFloat(accountData.dailyUsage || '0'),
lastResetDate: accountData.lastResetDate || '',
quotaResetTime: accountData.quotaResetTime || '00:00',
quotaStoppedAt: accountData.quotaStoppedAt || null,
expiresAt: accountData.expiresAt || null,
subscriptionExpiresAt: accountData.subscriptionExpiresAt || null
})
}
}
return accounts

View File

@@ -487,9 +487,12 @@ class ClaudeConsoleRelayService {
// 解析SSE数据寻找usage信息
for (const line of lines) {
if (line.startsWith('data: ') && line.length > 6) {
if (line.startsWith('data:')) {
const jsonStr = line.slice(5).trimStart()
if (!jsonStr || jsonStr === '[DONE]') {
continue
}
try {
const jsonStr = line.slice(6)
const data = JSON.parse(jsonStr)
// 收集usage数据

View File

@@ -1487,9 +1487,12 @@ class ClaudeRelayService {
for (const line of lines) {
// 解析SSE数据寻找usage信息
if (line.startsWith('data: ') && line.length > 6) {
if (line.startsWith('data:')) {
const jsonStr = line.slice(5).trimStart()
if (!jsonStr || jsonStr === '[DONE]') {
continue
}
try {
const jsonStr = line.slice(6)
const data = JSON.parse(jsonStr)
// 收集来自不同事件的usage数据

View File

@@ -504,9 +504,7 @@ async function updateAccount(accountId, updates) {
updates.updatedAt = now
if (Object.prototype.hasOwnProperty.call(updates, 'subscriptionExpiresAt')) {
updates.subscriptionExpiresAt = normalizeSubscriptionExpiresAt(
updates.subscriptionExpiresAt
)
updates.subscriptionExpiresAt = normalizeSubscriptionExpiresAt(updates.subscriptionExpiresAt)
}
// 检查是否新增了 refresh token

View File

@@ -173,9 +173,7 @@ class OpenAIResponsesAccountService {
}
if (Object.prototype.hasOwnProperty.call(updates, 'subscriptionExpiresAt')) {
updates.subscriptionExpiresAt = normalizeSubscriptionExpiresAt(
updates.subscriptionExpiresAt
)
updates.subscriptionExpiresAt = normalizeSubscriptionExpiresAt(updates.subscriptionExpiresAt)
} else if (Object.prototype.hasOwnProperty.call(updates, 'expiresAt')) {
updates.subscriptionExpiresAt = normalizeSubscriptionExpiresAt(updates.expiresAt)
delete updates.expiresAt