@anthonyhaussman/opencode-agy-auth 1.1.14-alpha.0 → 1.1.14-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -516,6 +516,69 @@ function normalizeErrorEnvelope(parsed) {
516
516
  }
517
517
  return isObject(parsed) ? parsed : null;
518
518
  }
519
+ var MAX_QUOTA_RESET_WAIT_MS = 72e5;
520
+ function findResetTimeForModel(summary, model) {
521
+ if (!summary) return null;
522
+ const normalize2 = (str) => str.toLowerCase().replace(/[^a-z0-9]/g, "");
523
+ const modelNorm = model ? normalize2(model) : "";
524
+ let targetGroups = summary.groups ?? [];
525
+ if (modelNorm && targetGroups.length > 0) {
526
+ const matched = targetGroups.filter((g) => {
527
+ const gName = normalize2(g.displayName ?? "");
528
+ return gName.includes(modelNorm) || modelNorm.includes(gName);
529
+ });
530
+ if (matched.length > 0) {
531
+ targetGroups = matched;
532
+ }
533
+ }
534
+ const allBuckets = [];
535
+ for (const group of targetGroups) {
536
+ if (group.buckets) {
537
+ allBuckets.push(...group.buckets);
538
+ }
539
+ }
540
+ if (allBuckets.length === 0 && summary.buckets) {
541
+ allBuckets.push(...summary.buckets);
542
+ }
543
+ const now = Date.now();
544
+ const validResetBuckets = allBuckets.filter((b) => {
545
+ if (!b.resetTime) return false;
546
+ const t = new Date(b.resetTime).getTime();
547
+ return !Number.isNaN(t) && t > now;
548
+ });
549
+ if (validResetBuckets.length === 0) {
550
+ return null;
551
+ }
552
+ const exhaustedFiveHour = validResetBuckets.find(
553
+ (b) => (b.remainingFraction === 0 || b.disabled) && b.window?.toUpperCase() === "FIVE_HOUR"
554
+ );
555
+ if (exhaustedFiveHour?.resetTime) return exhaustedFiveHour.resetTime;
556
+ const exhaustedAny = validResetBuckets.find((b) => b.remainingFraction === 0 || b.disabled);
557
+ if (exhaustedAny?.resetTime) return exhaustedAny.resetTime;
558
+ const fiveHour = validResetBuckets.find((b) => b.window?.toUpperCase() === "FIVE_HOUR");
559
+ if (fiveHour?.resetTime) return fiveHour.resetTime;
560
+ validResetBuckets.sort(
561
+ (a, b) => new Date(a.resetTime).getTime() - new Date(b.resetTime).getTime()
562
+ );
563
+ return validResetBuckets[0]?.resetTime ?? null;
564
+ }
565
+ async function resolveQuotaResetDelay(accessToken, projectId, model, userAgentModel) {
566
+ try {
567
+ const summary = await retrieveUserQuotaSummary(accessToken, projectId, userAgentModel);
568
+ if (!summary) return null;
569
+ const resetTime = findResetTimeForModel(summary, model);
570
+ if (!resetTime) return null;
571
+ const resetTimestamp = new Date(resetTime).getTime();
572
+ if (Number.isNaN(resetTimestamp)) return null;
573
+ const waitMs = resetTimestamp - Date.now() + 1e3;
574
+ if (waitMs <= 0 || waitMs > MAX_QUOTA_RESET_WAIT_MS) {
575
+ return null;
576
+ }
577
+ return { waitMs, resetTime };
578
+ } catch {
579
+ return null;
580
+ }
581
+ }
519
582
 
520
583
  // src/sdk/retry/helpers.ts
521
584
  var DEFAULT_MAX_ATTEMPTS = 3;
@@ -812,6 +875,7 @@ async function fetchWithRetry(input, init) {
812
875
  const throttleKey = buildRetryThrottleKey(input, retryInit);
813
876
  await waitForRetryCooldown(throttleKey, retryInit.signal);
814
877
  let attempt = 1;
878
+ let hasRetriedQuotaReset = false;
815
879
  const url2 = readRequestUrl(input);
816
880
  while (attempt <= DEFAULT_MAX_ATTEMPTS) {
817
881
  let response;
@@ -837,6 +901,25 @@ async function fetchWithRetry(input, init) {
837
901
  if (quotaContext.reason === "MODEL_CAPACITY_EXHAUSTED") {
838
902
  const cooldownMs = quotaContext.retryDelayMs ?? MODEL_CAPACITY_COOLDOWN_MS;
839
903
  setRetryCooldown(throttleKey, cooldownMs);
904
+ return response;
905
+ }
906
+ if (quotaContext.reason === "QUOTA_EXHAUSTED" && !hasRetriedQuotaReset && !retryInit.signal?.aborted) {
907
+ const body = typeof retryInit.body === "string" ? safeParseBody(retryInit.body) : null;
908
+ const project = readString(body?.project);
909
+ const model = readString(body?.model);
910
+ const token = extractAuthToken(retryInit.headers);
911
+ if (token && project) {
912
+ const resetInfo = await resolveQuotaResetDelay(token, project, model);
913
+ if (resetInfo && resetInfo.waitMs > 0 && resetInfo.waitMs <= MAX_QUOTA_RESET_WAIT_MS) {
914
+ hasRetriedQuotaReset = true;
915
+ setRetryCooldown(throttleKey, resetInfo.waitMs);
916
+ await wait(resetInfo.waitMs);
917
+ if (retryInit.signal?.aborted) {
918
+ return response;
919
+ }
920
+ continue;
921
+ }
922
+ }
840
923
  }
841
924
  return response;
842
925
  }
@@ -922,6 +1005,14 @@ function safeParseBody(body) {
922
1005
  function readString(value) {
923
1006
  return typeof value === "string" && value.trim() ? value : void 0;
924
1007
  }
1008
+ function extractAuthToken(headers) {
1009
+ if (!headers) return void 0;
1010
+ const h = new Headers(headers);
1011
+ const auth = h.get("authorization") || h.get("Authorization");
1012
+ if (!auth) return void 0;
1013
+ const match = auth.match(/^Bearer\s+(.+)$/i);
1014
+ return match?.[1]?.trim() || auth.trim();
1015
+ }
925
1016
 
926
1017
  // src/sdk/terminal-hyperlink.ts
927
1018
  var OSC8_OPEN = "\x1B]8;;";
@@ -18453,7 +18544,7 @@ function applyLatestSignature(contents, latestSig) {
18453
18544
  if (content && typeof content === "object" && Array.isArray(content.parts)) {
18454
18545
  for (const part of content.parts) {
18455
18546
  if (part && typeof part === "object" && part.functionCall) {
18456
- allFunctionCalls.push(part);
18547
+ allFunctionCalls.push(part.functionCall);
18457
18548
  }
18458
18549
  }
18459
18550
  }
@@ -19315,7 +19406,7 @@ function resolveThinkingConfigDefaults(provider) {
19315
19406
  const providerOptions = provider && typeof provider === "object" ? provider.options ?? void 0 : void 0;
19316
19407
  const providerThinkingConfig = providerOptions?.thinkingConfig;
19317
19408
  const modelThinkingConfigByModel = {};
19318
- for (const [modelId, model] of Object.entries(provider.models ?? {})) {
19409
+ for (const [modelId, model] of Object.entries(provider?.models ?? {})) {
19319
19410
  if (!model || typeof model !== "object") {
19320
19411
  continue;
19321
19412
  }