(groupId: string, apiKey: string)
| 486 | } |
| 487 | |
| 488 | async function fetchMinimaxUsageLimits(groupId: string, apiKey: string): Promise<{ |
| 489 | planType?: string |
| 490 | allowed?: boolean |
| 491 | limitReached?: boolean |
| 492 | limits?: { primary?: RateLimitWindowSummary } |
| 493 | }> { |
| 494 | const url = `${MINIMAX_USAGE_URL}?GroupId=${encodeURIComponent(groupId)}` |
| 495 | const response = await globalThis.fetch(url, { |
| 496 | headers: { |
| 497 | Authorization: `Bearer ${apiKey}`, |
| 498 | Referer: "https://platform.minimax.io/user-center/payment/coding-plan", |
| 499 | Accept: "application/json", |
| 500 | }, |
| 501 | }) |
| 502 | |
| 503 | if (!response.ok) { |
| 504 | if (response.status === 401 || response.status === 403) { |
| 505 | throw new Error("Authorization failed. Please check your MiniMax API key.") |
| 506 | } |
| 507 | throw new Error(`Failed to fetch MiniMax usage limits: ${response.statusText}`) |
| 508 | } |
| 509 | |
| 510 | const payload = (await response.json().catch(() => null)) as any |
| 511 | if (!payload || payload.base_resp?.status_code !== 0) { |
| 512 | const errorMsg = payload?.base_resp?.status_msg || "Unknown error" |
| 513 | throw new Error(`MiniMax API error: ${errorMsg}`) |
| 514 | } |
| 515 | |
| 516 | const modelRemains = Array.isArray(payload.model_remains) ? payload.model_remains : [] |
| 517 | if (modelRemains.length === 0) { |
| 518 | return {} |
| 519 | } |
| 520 | |
| 521 | const model = modelRemains[0] |
| 522 | const totalCount = typeof model.current_interval_total_count === "number" ? model.current_interval_total_count : 0 |
| 523 | const remainingCount = typeof model.current_interval_usage_count === "number" ? model.current_interval_usage_count : 0 |
| 524 | const remainsTime = typeof model.remains_time === "number" ? model.remains_time : 0 |
| 525 | const modelName = typeof model.model_name === "string" ? model.model_name : "MiniMax" |
| 526 | |
| 527 | const usedCount = totalCount > 0 ? totalCount - remainingCount : 0 |
| 528 | const remainingPercent = totalCount > 0 ? (remainingCount / totalCount) * 100 : 100 |
| 529 | const usedPercent = totalCount > 0 ? (usedCount / totalCount) * 100 : 0 |
| 530 | |
| 531 | const limitReached = usedCount >= totalCount && totalCount > 0 |
| 532 | |
| 533 | const planType = `${modelName}\n- ${remainingCount.toLocaleString()} / ${totalCount.toLocaleString()} requests remaining (${remainingPercent.toFixed(1)}%)\n- Time remaining: ${formatMilliseconds(remainsTime)}` |
| 534 | |
| 535 | const resetsAt = remainsTime > 0 ? Math.floor((Date.now() + remainsTime) / 1000) : undefined |
| 536 | |
| 537 | return { |
| 538 | planType, |
| 539 | allowed: !limitReached, |
| 540 | limitReached, |
| 541 | limits: { |
| 542 | primary: { |
| 543 | usedPercent: remainingPercent, |
| 544 | resetsAt, |
| 545 | label: "Request quota", |
no test coverage detected