fix: 修复session窗口计算问题

This commit is contained in:
KevinLiao
2025-07-31 09:15:33 +08:00
parent 0d98467688
commit d93ce8713d
2 changed files with 40 additions and 16 deletions

View File

@@ -110,22 +110,32 @@ const commands = {
const windowStart = new Date(account.sessionWindowStart);
const windowEnd = new Date(account.sessionWindowEnd);
const now = new Date();
const isActive = now < windowEnd;
console.log(` 会话窗口: ${windowStart.toLocaleString()} - ${windowEnd.toLocaleString()}`);
console.log(` 窗口状态: ${now < windowEnd ? '✅ 活跃' : '❌ 已过期'}`);
console.log(` 窗口状态: ${isActive ? '✅ 活跃' : '❌ 已过期'}`);
// 只有在窗口已过期时才显示可恢复窗口
if (!isActive && account.lastUsedAt) {
const lastUsed = new Date(account.lastUsedAt);
const recoveredWindowStart = claudeAccountService._calculateSessionWindowStart(lastUsed);
const recoveredWindowEnd = claudeAccountService._calculateSessionWindowEnd(recoveredWindowStart);
if (now < recoveredWindowEnd) {
stats.canRecover++;
console.log(` 可恢复窗口: ✅ ${recoveredWindowStart.toLocaleString()} - ${recoveredWindowEnd.toLocaleString()}`);
} else {
stats.expired++;
console.log(` 可恢复窗口: ❌ 已过期 (${recoveredWindowStart.toLocaleString()} - ${recoveredWindowEnd.toLocaleString()})`);
}
}
} else {
console.log(` 会话窗口: ❌ 无`);
}
// 没有会话窗口时,检查是否有可恢复的窗口
if (account.lastUsedAt) {
stats.hasLastUsed++;
const lastUsed = new Date(account.lastUsedAt);
const now = new Date();
const minutesAgo = Math.round((now - lastUsed) / (1000 * 60));
console.log(` 最后使用: ${lastUsed.toLocaleString()} (${minutesAgo}分钟前)`);
// 计算基于lastUsedAt的窗口
const windowStart = claudeAccountService._calculateSessionWindowStart(lastUsed);
const windowEnd = claudeAccountService._calculateSessionWindowEnd(windowStart);
@@ -136,6 +146,16 @@ const commands = {
stats.expired++;
console.log(` 可恢复窗口: ❌ 已过期 (${windowStart.toLocaleString()} - ${windowEnd.toLocaleString()})`);
}
}
}
if (account.lastUsedAt) {
stats.hasLastUsed++;
const lastUsed = new Date(account.lastUsedAt);
const now = new Date();
const minutesAgo = Math.round((now - lastUsed) / (1000 * 60));
console.log(` 最后使用: ${lastUsed.toLocaleString()} (${minutesAgo}分钟前)`);
} else {
console.log(` 最后使用: ❌ 无记录`);
}

View File

@@ -861,14 +861,18 @@ class ClaudeAccountService {
if (accountData.sessionWindowStart && accountData.sessionWindowEnd) {
const windowEnd = new Date(accountData.sessionWindowEnd).getTime();
// 如果当前时间在窗口内,不需要更新
// 如果当前时间在窗口内,只更新最后请求时间
if (currentTime < windowEnd) {
accountData.lastRequestTime = now.toISOString();
return accountData;
}
// 窗口已过期,记录日志
const windowStart = new Date(accountData.sessionWindowStart);
logger.info(`⏰ Session window expired for account ${accountData.name} (${accountId}): ${windowStart.toISOString()} - ${new Date(windowEnd).toISOString()}`);
}
// 计算新的会话窗口
// 基于当前时间计算新的会话窗口
const windowStart = this._calculateSessionWindowStart(now);
const windowEnd = this._calculateSessionWindowEnd(windowStart);
@@ -877,7 +881,7 @@ class ClaudeAccountService {
accountData.sessionWindowEnd = windowEnd.toISOString();
accountData.lastRequestTime = now.toISOString();
logger.info(`🕐 Updated session window for account ${accountData.name} (${accountId}): ${windowStart.toISOString()} - ${windowEnd.toISOString()}`);
logger.info(`🕐 Created new session window for account ${accountData.name} (${accountId}): ${windowStart.toISOString()} - ${windowEnd.toISOString()}`);
return accountData;
} catch (error) {