mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-22 16:43:35 +00:00
fix: 修复session窗口计算问题
This commit is contained in:
@@ -110,11 +110,43 @@ 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) {
|
||||
const lastUsed = new Date(account.lastUsedAt);
|
||||
const now = new Date();
|
||||
const windowStart = claudeAccountService._calculateSessionWindowStart(lastUsed);
|
||||
const windowEnd = claudeAccountService._calculateSessionWindowEnd(windowStart);
|
||||
|
||||
if (now < windowEnd) {
|
||||
stats.canRecover++;
|
||||
console.log(` 可恢复窗口: ✅ ${windowStart.toLocaleString()} - ${windowEnd.toLocaleString()}`);
|
||||
} else {
|
||||
stats.expired++;
|
||||
console.log(` 可恢复窗口: ❌ 已过期 (${windowStart.toLocaleString()} - ${windowEnd.toLocaleString()})`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (account.lastUsedAt) {
|
||||
@@ -124,18 +156,6 @@ const commands = {
|
||||
const minutesAgo = Math.round((now - lastUsed) / (1000 * 60));
|
||||
|
||||
console.log(` 最后使用: ${lastUsed.toLocaleString()} (${minutesAgo}分钟前)`);
|
||||
|
||||
// 计算基于lastUsedAt的窗口
|
||||
const windowStart = claudeAccountService._calculateSessionWindowStart(lastUsed);
|
||||
const windowEnd = claudeAccountService._calculateSessionWindowEnd(windowStart);
|
||||
|
||||
if (now < windowEnd) {
|
||||
stats.canRecover++;
|
||||
console.log(` 可恢复窗口: ✅ ${windowStart.toLocaleString()} - ${windowEnd.toLocaleString()}`);
|
||||
} else {
|
||||
stats.expired++;
|
||||
console.log(` 可恢复窗口: ❌ 已过期 (${windowStart.toLocaleString()} - ${windowEnd.toLocaleString()})`);
|
||||
}
|
||||
} else {
|
||||
console.log(` 最后使用: ❌ 无记录`);
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user