fix(gemini): 修复认证初始化顺序和令牌刷新逻辑
调整 initializeAuth 方法中凭证加载和令牌刷新检查的顺序,确保在评估令牌过期前已加载凭证。添加 isTokenExpiringSoon 方法用于主动检查令牌过期状态,防止使用即将过期的令牌。同时修复 Antigravity 服务中模型别名转换的逻辑错误。
This commit is contained in:
parent
2680651e3a
commit
82c667ac5d
3 changed files with 29 additions and 14 deletions
2
VERSION
2
VERSION
|
|
@ -1 +1 @@
|
|||
2.11.5.1
|
||||
2.11.5.2
|
||||
|
|
|
|||
|
|
@ -783,7 +783,12 @@ export class AntigravityApiService {
|
|||
}
|
||||
|
||||
async initializeAuth(forceRefresh = false) {
|
||||
// 检查是否需要刷新 Token
|
||||
const credPath = this.oauthCredsFilePath || path.join(os.homedir(), CREDENTIALS_DIR, CREDENTIALS_FILE);
|
||||
|
||||
// 首先执行基础凭证加载
|
||||
await this.loadCredentials();
|
||||
|
||||
// 检查是否需要刷新 Token(在加载凭证后重新评估)
|
||||
const needsRefresh = forceRefresh || this.isTokenExpiringSoon();
|
||||
|
||||
if (this.authClient.credentials.access_token && !needsRefresh) {
|
||||
|
|
@ -791,11 +796,6 @@ export class AntigravityApiService {
|
|||
return;
|
||||
}
|
||||
|
||||
const credPath = this.oauthCredsFilePath || path.join(os.homedir(), CREDENTIALS_DIR, CREDENTIALS_FILE);
|
||||
|
||||
// 首先执行基础凭证加载
|
||||
await this.loadCredentials();
|
||||
|
||||
// 只有在明确要求刷新,或者 AccessToken 确实缺失时,才执行刷新/认证
|
||||
// 注意:在 V2 架构下,此方法主要由 PoolManager 的后台队列调用
|
||||
if (needsRefresh || !this.authClient.credentials.access_token) {
|
||||
|
|
@ -1484,8 +1484,12 @@ export class AntigravityApiService {
|
|||
|
||||
// 遍历模型数据,提取配额信息
|
||||
for (const [modelId, modelData] of Object.entries(modelsData)) {
|
||||
const aliasName = modelName2Alias(modelId);
|
||||
if (aliasName == null || aliasName === '') continue; // 跳过不支持的模型
|
||||
// 参考 fetchAvailableModels 的逻辑修复 modelName2Alias 不存在的问题
|
||||
if (!modelId || (!ANTIGRAVITY_MODELS.includes(modelId) && !modelId.startsWith('claude-'))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const aliasName = modelId.startsWith('claude-') ? `gemini-${modelId}` : modelId;
|
||||
|
||||
const modelInfo = {
|
||||
remaining: 0,
|
||||
|
|
|
|||
|
|
@ -351,17 +351,17 @@ export class GeminiApiService {
|
|||
}
|
||||
|
||||
async initializeAuth(forceRefresh = false) {
|
||||
// 检查是否需要刷新 Token
|
||||
const needsRefresh = forceRefresh
|
||||
// 首先执行基础凭证加载
|
||||
await this.loadCredentials();
|
||||
|
||||
// 检查是否需要刷新 Token(加载凭证后评估)
|
||||
const needsRefresh = forceRefresh || this.isTokenExpiringSoon();
|
||||
|
||||
if (this.authClient.credentials.access_token && !needsRefresh) {
|
||||
// Token 有效且不需要刷新
|
||||
return;
|
||||
}
|
||||
|
||||
// 首先执行基础凭证加载
|
||||
await this.loadCredentials();
|
||||
|
||||
// 只有在明确要求刷新,或者 AccessToken 确实缺失时,才执行刷新/认证
|
||||
// 注意:在 V2 架构下,此方法主要由 PoolManager 的后台队列调用
|
||||
if (needsRefresh || !this.authClient.credentials.access_token) {
|
||||
|
|
@ -811,6 +811,17 @@ export class GeminiApiService {
|
|||
}
|
||||
}
|
||||
|
||||
isTokenExpiringSoon() {
|
||||
if (!this.authClient.credentials.expiry_date) {
|
||||
return false;
|
||||
}
|
||||
const currentTime = Date.now();
|
||||
const expiryTime = this.authClient.credentials.expiry_date;
|
||||
const REFRESH_SKEW = 3000; // 50分钟
|
||||
const refreshSkewMs = REFRESH_SKEW * 1000;
|
||||
return expiryTime <= (currentTime + refreshSkewMs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存凭证到文件
|
||||
* @param {string} filePath - 凭证文件路径
|
||||
|
|
|
|||
Loading…
Reference in a new issue