Spaces:
Sleeping
Sleeping
File size: 19,626 Bytes
ceb3821 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 | /**
* 用量查询服务
* 用于处理各个提供商的授权文件用量查询
*/
import { getProviderPoolManager } from './service-manager.js';
import { serviceInstances } from '../providers/adapter.js';
import { MODEL_PROVIDER } from '../utils/common.js';
/**
* 用量查询服务类
* 提供统一的接口来查询各提供商的用量信息
*/
export class UsageService {
constructor() {
this.providerHandlers = {
[MODEL_PROVIDER.KIRO_API]: this.getKiroUsage.bind(this),
[MODEL_PROVIDER.GEMINI_CLI]: this.getGeminiUsage.bind(this),
[MODEL_PROVIDER.ANTIGRAVITY]: this.getAntigravityUsage.bind(this),
};
}
/**
* 获取指定提供商的用量信息
* @param {string} providerType - 提供商类型
* @param {string} [uuid] - 可选的提供商实例 UUID
* @returns {Promise<Object>} 用量信息
*/
async getUsage(providerType, uuid = null) {
const handler = this.providerHandlers[providerType];
if (!handler) {
throw new Error(`不支持的提供商类型: ${providerType}`);
}
return handler(uuid);
}
/**
* 获取所有提供商的用量信息
* @returns {Promise<Object>} 所有提供商的用量信息
*/
async getAllUsage() {
const results = {};
const poolManager = getProviderPoolManager();
for (const [providerType, handler] of Object.entries(this.providerHandlers)) {
try {
// 检查是否有号池配置
if (poolManager) {
const pools = poolManager.getProviderPools(providerType);
if (pools && pools.length > 0) {
results[providerType] = [];
for (const pool of pools) {
try {
const usage = await handler(pool.uuid);
results[providerType].push({
uuid: pool.uuid,
usage
});
} catch (error) {
results[providerType].push({
uuid: pool.uuid,
error: error.message
});
}
}
}
}
// 如果没有号池配置,尝试获取单个实例的用量
if (!results[providerType] || results[providerType].length === 0) {
const usage = await handler(null);
results[providerType] = [{ uuid: 'default', usage }];
}
} catch (error) {
results[providerType] = [{ uuid: 'default', error: error.message }];
}
}
return results;
}
/**
* 获取 Kiro 提供商的用量信息
* @param {string} [uuid] - 可选的提供商实例 UUID
* @returns {Promise<Object>} Kiro 用量信息
*/
async getKiroUsage(uuid = null) {
const providerKey = uuid ? MODEL_PROVIDER.KIRO_API + uuid : MODEL_PROVIDER.KIRO_API;
const adapter = serviceInstances[providerKey];
if (!adapter) {
throw new Error(`Kiro 服务实例未找到: ${providerKey}`);
}
// 使用适配器的 getUsageLimits 方法
if (typeof adapter.getUsageLimits === 'function') {
return adapter.getUsageLimits();
}
// 兼容直接访问 kiroApiService 的情况
if (adapter.kiroApiService && typeof adapter.kiroApiService.getUsageLimits === 'function') {
return adapter.kiroApiService.getUsageLimits();
}
throw new Error(`Kiro 服务实例不支持用量查询: ${providerKey}`);
}
/**
* 获取 Gemini CLI 提供商的用量信息
* @param {string} [uuid] - 可选的提供商实例 UUID
* @returns {Promise<Object>} Gemini 用量信息
*/
async getGeminiUsage(uuid = null) {
const providerKey = uuid ? MODEL_PROVIDER.GEMINI_CLI + uuid : MODEL_PROVIDER.GEMINI_CLI;
const adapter = serviceInstances[providerKey];
if (!adapter) {
throw new Error(`Gemini CLI 服务实例未找到: ${providerKey}`);
}
// 使用适配器的 getUsageLimits 方法
if (typeof adapter.getUsageLimits === 'function') {
return adapter.getUsageLimits();
}
// 兼容直接访问 geminiApiService 的情况
if (adapter.geminiApiService && typeof adapter.geminiApiService.getUsageLimits === 'function') {
return adapter.geminiApiService.getUsageLimits();
}
throw new Error(`Gemini CLI 服务实例不支持用量查询: ${providerKey}`);
}
/**
* 获取 Antigravity 提供商的用量信息
* @param {string} [uuid] - 可选的提供商实例 UUID
* @returns {Promise<Object>} Antigravity 用量信息
*/
async getAntigravityUsage(uuid = null) {
const providerKey = uuid ? MODEL_PROVIDER.ANTIGRAVITY + uuid : MODEL_PROVIDER.ANTIGRAVITY;
const adapter = serviceInstances[providerKey];
if (!adapter) {
throw new Error(`Antigravity 服务实例未找到: ${providerKey}`);
}
// 使用适配器的 getUsageLimits 方法
if (typeof adapter.getUsageLimits === 'function') {
return adapter.getUsageLimits();
}
// 兼容直接访问 antigravityApiService 的情况
if (adapter.antigravityApiService && typeof adapter.antigravityApiService.getUsageLimits === 'function') {
return adapter.antigravityApiService.getUsageLimits();
}
throw new Error(`Antigravity 服务实例不支持用量查询: ${providerKey}`);
}
/**
* 获取支持用量查询的提供商列表
* @returns {Array<string>} 支持的提供商类型列表
*/
getSupportedProviders() {
return Object.keys(this.providerHandlers);
}
}
// 导出单例实例
export const usageService = new UsageService();
/**
* 格式化 Kiro 用量信息为易读格式
* @param {Object} usageData - 原始用量数据
* @returns {Object} 格式化后的用量信息
*/
export function formatKiroUsage(usageData) {
if (!usageData) {
return null;
}
const result = {
// 基本信息
daysUntilReset: usageData.daysUntilReset,
nextDateReset: usageData.nextDateReset ? new Date(usageData.nextDateReset * 1000).toISOString() : null,
// 订阅信息
subscription: null,
// 用户信息
user: null,
// 用量明细
usageBreakdown: []
};
// 解析订阅信息
if (usageData.subscriptionInfo) {
result.subscription = {
title: usageData.subscriptionInfo.subscriptionTitle,
type: usageData.subscriptionInfo.type,
upgradeCapability: usageData.subscriptionInfo.upgradeCapability,
overageCapability: usageData.subscriptionInfo.overageCapability
};
}
// 解析用户信息
if (usageData.userInfo) {
result.user = {
email: usageData.userInfo.email,
userId: usageData.userInfo.userId
};
}
// 解析用量明细
if (usageData.usageBreakdownList && Array.isArray(usageData.usageBreakdownList)) {
for (const breakdown of usageData.usageBreakdownList) {
const item = {
resourceType: breakdown.resourceType,
displayName: breakdown.displayName,
displayNamePlural: breakdown.displayNamePlural,
unit: breakdown.unit,
currency: breakdown.currency,
// 当前用量
currentUsage: breakdown.currentUsageWithPrecision ?? breakdown.currentUsage,
usageLimit: breakdown.usageLimitWithPrecision ?? breakdown.usageLimit,
// 超额信息
currentOverages: breakdown.currentOveragesWithPrecision ?? breakdown.currentOverages,
overageCap: breakdown.overageCapWithPrecision ?? breakdown.overageCap,
overageRate: breakdown.overageRate,
overageCharges: breakdown.overageCharges,
// 下次重置时间
nextDateReset: breakdown.nextDateReset ? new Date(breakdown.nextDateReset * 1000).toISOString() : null,
// 免费试用信息
freeTrial: null,
// 奖励信息
bonuses: []
};
// 解析免费试用信息
if (breakdown.freeTrialInfo) {
item.freeTrial = {
status: breakdown.freeTrialInfo.freeTrialStatus,
currentUsage: breakdown.freeTrialInfo.currentUsageWithPrecision ?? breakdown.freeTrialInfo.currentUsage,
usageLimit: breakdown.freeTrialInfo.usageLimitWithPrecision ?? breakdown.freeTrialInfo.usageLimit,
expiresAt: breakdown.freeTrialInfo.freeTrialExpiry
? new Date(breakdown.freeTrialInfo.freeTrialExpiry * 1000).toISOString()
: null
};
}
// 解析奖励信息
if (breakdown.bonuses && Array.isArray(breakdown.bonuses)) {
for (const bonus of breakdown.bonuses) {
item.bonuses.push({
code: bonus.bonusCode,
displayName: bonus.displayName,
description: bonus.description,
status: bonus.status,
currentUsage: bonus.currentUsage,
usageLimit: bonus.usageLimit,
redeemedAt: bonus.redeemedAt ? new Date(bonus.redeemedAt * 1000).toISOString() : null,
expiresAt: bonus.expiresAt ? new Date(bonus.expiresAt * 1000).toISOString() : null
});
}
}
result.usageBreakdown.push(item);
}
}
return result;
}
/**
* 格式化 Gemini 用量信息为易读格式(映射到 Kiro 数据结构)
* @param {Object} usageData - 原始用量数据
* @returns {Object} 格式化后的用量信息
*/
export function formatGeminiUsage(usageData) {
if (!usageData) {
return null;
}
const TZ_OFFSET = 8 * 60 * 60 * 1000; // Beijing timezone offset
/**
* 将 UTC 时间转换为北京时间
* @param {string} utcString - UTC 时间字符串
* @returns {string} 北京时间字符串
*/
function utcToBeijing(utcString) {
try {
if (!utcString) return '--';
const utcDate = new Date(utcString);
const beijingTime = new Date(utcDate.getTime() + TZ_OFFSET);
return beijingTime
.toLocaleString('zh-CN', {
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit'
})
.replace(/\//g, '-');
} catch (e) {
return '--';
}
}
const result = {
// 基本信息 - 映射到 Kiro 结构
daysUntilReset: null,
nextDateReset: null,
// 订阅信息
subscription: {
title: 'Gemini CLI OAuth',
type: 'gemini-cli-oauth',
upgradeCapability: null,
overageCapability: null
},
// 用户信息
user: {
email: null,
userId: null
},
// 用量明细
usageBreakdown: []
};
// 解析配额信息
if (usageData.quotaInfo) {
result.subscription.title = usageData.quotaInfo.currentTier || 'Gemini CLI OAuth';
if (usageData.quotaInfo.quotaResetTime) {
result.nextDateReset = usageData.quotaInfo.quotaResetTime;
// 计算距离重置的天数
const resetDate = new Date(usageData.quotaInfo.quotaResetTime);
const now = new Date();
const diffTime = resetDate.getTime() - now.getTime();
result.daysUntilReset = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
}
}
// 解析模型配额信息
if (usageData.models && typeof usageData.models === 'object') {
for (const [modelName, modelInfo] of Object.entries(usageData.models)) {
// Gemini 返回的数据结构:{ remaining, resetTime, resetTimeRaw }
// remaining 是 0-1 之间的比例值,表示剩余配额百分比
const remainingPercent = typeof modelInfo.remaining === 'number' ? modelInfo.remaining : 1;
const usedPercent = 1 - remainingPercent;
const item = {
resourceType: 'MODEL_USAGE',
displayName: modelInfo.displayName || modelName,
displayNamePlural: modelInfo.displayName || modelName,
unit: 'quota',
currency: null,
// 当前用量 - Gemini 返回的是剩余比例,转换为已用比例(百分比形式)
currentUsage: Math.round(usedPercent * 100),
usageLimit: 100, // 以百分比表示,总量为 100%
// 超额信息
currentOverages: 0,
overageCap: 0,
overageRate: null,
overageCharges: 0,
// 下次重置时间
nextDateReset: modelInfo.resetTimeRaw ? new Date(modelInfo.resetTimeRaw).toISOString() :
(modelInfo.resetTime ? new Date(modelInfo.resetTime).toISOString() : null),
// 免费试用信息
freeTrial: null,
// 奖励信息
bonuses: [],
// 额外的 Gemini 特有信息
modelName: modelName,
inputTokenLimit: modelInfo.inputTokenLimit || 0,
outputTokenLimit: modelInfo.outputTokenLimit || 0,
remaining: remainingPercent,
remainingPercent: Math.round(remainingPercent * 100), // 剩余百分比
resetTime: (modelInfo.resetTimeRaw || modelInfo.resetTime) ?
utcToBeijing(modelInfo.resetTimeRaw || modelInfo.resetTime) : '--',
resetTimeRaw: modelInfo.resetTimeRaw || modelInfo.resetTime || null
};
result.usageBreakdown.push(item);
}
}
return result;
}
/**
* 格式化 Antigravity 用量信息为易读格式(映射到 Kiro 数据结构)
* @param {Object} usageData - 原始用量数据
* @returns {Object} 格式化后的用量信息
*/
export function formatAntigravityUsage(usageData) {
if (!usageData) {
return null;
}
const TZ_OFFSET = 8 * 60 * 60 * 1000; // Beijing timezone offset
/**
* 将 UTC 时间转换为北京时间
* @param {string} utcString - UTC 时间字符串
* @returns {string} 北京时间字符串
*/
function utcToBeijing(utcString) {
try {
if (!utcString) return '--';
const utcDate = new Date(utcString);
const beijingTime = new Date(utcDate.getTime() + TZ_OFFSET);
return beijingTime
.toLocaleString('zh-CN', {
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit'
})
.replace(/\//g, '-');
} catch (e) {
return '--';
}
}
const result = {
// 基本信息 - 映射到 Kiro 结构
daysUntilReset: null,
nextDateReset: null,
// 订阅信息
subscription: {
title: 'Gemini Antigravity',
type: 'gemini-antigravity',
upgradeCapability: null,
overageCapability: null
},
// 用户信息
user: {
email: null,
userId: null
},
// 用量明细
usageBreakdown: []
};
// 解析配额信息
if (usageData.quotaInfo) {
result.subscription.title = usageData.quotaInfo.currentTier || 'Gemini Antigravity';
if (usageData.quotaInfo.quotaResetTime) {
result.nextDateReset = usageData.quotaInfo.quotaResetTime;
// 计算距离重置的天数
const resetDate = new Date(usageData.quotaInfo.quotaResetTime);
const now = new Date();
const diffTime = resetDate.getTime() - now.getTime();
result.daysUntilReset = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
}
}
// 解析模型配额信息
if (usageData.models && typeof usageData.models === 'object') {
for (const [modelName, modelInfo] of Object.entries(usageData.models)) {
// Antigravity 返回的数据结构:{ remaining, resetTime, resetTimeRaw }
// remaining 是 0-1 之间的比例值,表示剩余配额百分比
const remainingPercent = typeof modelInfo.remaining === 'number' ? modelInfo.remaining : 1;
const usedPercent = 1 - remainingPercent;
const item = {
resourceType: 'MODEL_USAGE',
displayName: modelInfo.displayName || modelName,
displayNamePlural: modelInfo.displayName || modelName,
unit: 'quota',
currency: null,
// 当前用量 - Antigravity 返回的是剩余比例,转换为已用比例(百分比形式)
currentUsage: usedPercent * 100,
usageLimit: 100, // 以百分比表示,总量为 100%
// 超额信息
currentOverages: 0,
overageCap: 0,
overageRate: null,
overageCharges: 0,
// 下次重置时间
nextDateReset: modelInfo.resetTimeRaw ? new Date(modelInfo.resetTimeRaw).toISOString() :
(modelInfo.resetTime ? new Date(modelInfo.resetTime).toISOString() : null),
// 免费试用信息
freeTrial: null,
// 奖励信息
bonuses: [],
// 额外的 Antigravity 特有信息
modelName: modelName,
inputTokenLimit: modelInfo.inputTokenLimit || 0,
outputTokenLimit: modelInfo.outputTokenLimit || 0,
remaining: remainingPercent,
remainingPercent: remainingPercent * 100, // 剩余百分比
resetTime: (modelInfo.resetTimeRaw || modelInfo.resetTime) ?
utcToBeijing(modelInfo.resetTimeRaw || modelInfo.resetTime) : '--',
resetTimeRaw: modelInfo.resetTimeRaw || modelInfo.resetTime || null
};
result.usageBreakdown.push(item);
}
}
return result;
} |