@jeffusion/bungee-core 6.9.0 → 6.10.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.
@@ -1281,7 +1281,7 @@ function validStoredAccount(value) {
1281
1281
  if (typeof value !== "object" || value === null || Array.isArray(value))
1282
1282
  return false;
1283
1283
  const account = value;
1284
- return typeof account.id === "string" && account.id.length > 0 && account.id.length <= 128 && typeof account.label === "string" && account.label.length > 0 && account.label.length <= MAX_TEXT && ["active", "disabled", "reauth_required", "revoked"].includes(account.status) && Number.isSafeInteger(account.generation) && account.generation > 0 && (account.loginFence === undefined || Number.isSafeInteger(account.loginFence) && account.loginFence > 0) && (account.credentialValid === undefined || typeof account.credentialValid === "boolean") && (account.autoResetCredits === undefined || typeof account.autoResetCredits === "boolean") && (account.siwc === undefined || validSiwcMetadata(account.siwc)) && validAutoResetAttempts(account.autoResetAttempts) && Number.isFinite(account.createdAt) && Number.isFinite(account.updatedAt);
1284
+ return typeof account.id === "string" && account.id.length > 0 && account.id.length <= 128 && typeof account.label === "string" && account.label.length > 0 && account.label.length <= MAX_TEXT && ["active", "disabled", "reauth_required", "revoked"].includes(account.status) && Number.isSafeInteger(account.generation) && account.generation > 0 && (account.loginFence === undefined || Number.isSafeInteger(account.loginFence) && account.loginFence > 0) && (account.credentialValid === undefined || typeof account.credentialValid === "boolean") && (account.autoResetCredits === undefined || typeof account.autoResetCredits === "boolean") && (account.autoResetLeadMinutes === undefined || Number.isInteger(account.autoResetLeadMinutes) && account.autoResetLeadMinutes >= 1 && account.autoResetLeadMinutes <= 10080) && (account.autoResetOnLowBalance === undefined || typeof account.autoResetOnLowBalance === "boolean") && (account.autoResetRemainingPercent === undefined || Number.isInteger(account.autoResetRemainingPercent) && account.autoResetRemainingPercent >= 0 && account.autoResetRemainingPercent <= 100) && (account.siwc === undefined || validSiwcMetadata(account.siwc)) && validAutoResetAttempts(account.autoResetAttempts) && Number.isFinite(account.createdAt) && Number.isFinite(account.updatedAt);
1285
1285
  }
1286
1286
  function decode(value) {
1287
1287
  if (value === undefined)
@@ -1326,6 +1326,9 @@ function accountListItem(account) {
1326
1326
  expiresAt: account.expiresAt,
1327
1327
  identity: copyIdentity(account.identity),
1328
1328
  autoResetCredits: !account.siwc && account.autoResetCredits === true,
1329
+ autoResetLeadMinutes: account.autoResetLeadMinutes ?? 30,
1330
+ autoResetOnLowBalance: !account.siwc && account.autoResetOnLowBalance === true,
1331
+ autoResetRemainingPercent: account.autoResetRemainingPercent ?? 10,
1329
1332
  pendingAutoReset: !account.siwc && pending ? { ...pending } : undefined
1330
1333
  };
1331
1334
  }
@@ -1539,8 +1542,8 @@ class AccountStore {
1539
1542
  return copyAccount(account);
1540
1543
  });
1541
1544
  }
1542
- async setAutoResetCredits(id, enabled) {
1543
- if (typeof enabled !== "boolean")
1545
+ async setAutoResetCredits(id, enabled, options = {}) {
1546
+ if (enabled !== undefined && typeof enabled !== "boolean" || options.lowBalanceEnabled !== undefined && typeof options.lowBalanceEnabled !== "boolean" || options.leadMinutes !== undefined && (!Number.isInteger(options.leadMinutes) || options.leadMinutes < 1 || options.leadMinutes > 10080) || options.remainingPercent !== undefined && (!Number.isInteger(options.remainingPercent) || options.remainingPercent < 0 || options.remainingPercent > 100))
1544
1547
  throw new AccountControlError("invalid_input");
1545
1548
  return this.mutate((aggregate) => {
1546
1549
  const account = aggregate.accounts.find((item) => item.id === id);
@@ -1550,7 +1553,10 @@ class AccountStore {
1550
1553
  throw new AccountControlError("revoked");
1551
1554
  if (account.siwc)
1552
1555
  throw new AccountControlError("unsupported_operation");
1553
- account.autoResetCredits = enabled;
1556
+ account.autoResetCredits = enabled ?? account.autoResetCredits ?? false;
1557
+ account.autoResetLeadMinutes = options.leadMinutes ?? account.autoResetLeadMinutes ?? 30;
1558
+ account.autoResetOnLowBalance = options.lowBalanceEnabled ?? account.autoResetOnLowBalance ?? false;
1559
+ account.autoResetRemainingPercent = options.remainingPercent ?? account.autoResetRemainingPercent ?? 10;
1554
1560
  account.updatedAt = Date.now();
1555
1561
  return copyAccount(account);
1556
1562
  });
@@ -1558,7 +1564,7 @@ class AccountStore {
1558
1564
  async claimAutoReset(id, credit, now) {
1559
1565
  return this.mutate((aggregate) => {
1560
1566
  const account = aggregate.accounts.find((item) => item.id === id);
1561
- if (!account || account.siwc || !accountListItem(account).available || account.autoResetCredits !== true)
1567
+ if (!account || account.siwc || !accountListItem(account).available || !(account.autoResetCredits || account.autoResetOnLowBalance))
1562
1568
  return;
1563
1569
  const attempts = account.autoResetAttempts ?? [];
1564
1570
  if (attempts.some((item) => !item.completed || item.creditId === credit.id))
@@ -2554,8 +2560,17 @@ var defaultTimer = {
2554
2560
  },
2555
2561
  cancel: (handle) => clearTimeout(handle)
2556
2562
  };
2557
- function isAutoResetDue(credit, now) {
2558
- return credit.status === "available" && typeof credit.expiresAt === "number" && Number.isFinite(credit.expiresAt) && credit.expiresAt > now && credit.expiresAt - now <= AUTO_RESET_LEAD_MS;
2563
+ function isAutoResetDue(credit, now, leadMinutes = 30) {
2564
+ return credit.status === "available" && typeof credit.expiresAt === "number" && Number.isFinite(credit.expiresAt) && credit.expiresAt > now && credit.expiresAt - now <= leadMinutes * 60000;
2565
+ }
2566
+ function isLowBalance(usage, remainingPercent, now) {
2567
+ if (usage.state !== "fresh" || usage.error)
2568
+ return false;
2569
+ const windows = [usage.primary, usage.secondary].filter((window) => window !== undefined && Number.isFinite(window.windowSeconds) && window.windowSeconds > 18000);
2570
+ const longest = Math.max(...windows.map((window) => window.windowSeconds));
2571
+ const candidates = windows.filter((window) => window.windowSeconds === longest);
2572
+ const window = candidates[0];
2573
+ return candidates.length === 1 && window !== undefined && Number.isFinite(window.usedPercent) && window.usedPercent >= 0 && window.usedPercent <= 100 && window.resetAt > now && window.resetAt - now <= window.windowSeconds * 1000 && 100 - window.usedPercent <= remainingPercent;
2559
2574
  }
2560
2575
 
2561
2576
  class AutoResetScheduler {
@@ -2605,7 +2620,7 @@ class AutoResetScheduler {
2605
2620
  }
2606
2621
  async scan() {
2607
2622
  const accounts = await this.accounts.list();
2608
- const eligible = new Set(accounts.filter((account) => account.available && account.autoResetCredits && (!account.pendingAutoReset || this.accountRuns.has(account.id))).map((account) => account.id));
2623
+ const eligible = new Set(accounts.filter((account) => account.available && (account.autoResetCredits || account.autoResetOnLowBalance) && (!account.pendingAutoReset || this.accountRuns.has(account.id))).map((account) => account.id));
2609
2624
  for (const id of this.plans.keys())
2610
2625
  if (!eligible.has(id))
2611
2626
  this.cancelPlan(id);
@@ -2626,7 +2641,12 @@ class AutoResetScheduler {
2626
2641
  if ((snapshot.resetCredits.availableCount ?? 0) <= 0)
2627
2642
  continue;
2628
2643
  const credits = [...snapshot.resetCredits.credits ?? []].filter((credit) => credit.status === "available" && typeof credit.expiresAt === "number" && Number.isFinite(credit.expiresAt) && credit.expiresAt > this.now()).sort((a, b) => a.expiresAt - b.expiresAt);
2629
- const plan = { credits };
2644
+ const plan = {
2645
+ credits,
2646
+ leadMinutes: account.autoResetLeadMinutes,
2647
+ expiryEnabled: account.autoResetCredits,
2648
+ lowBalance: account.autoResetOnLowBalance && isLowBalance(snapshot.usage, account.autoResetRemainingPercent, this.now())
2649
+ };
2630
2650
  this.plans.set(account.id, plan);
2631
2651
  await this.executePlan(account.id, plan);
2632
2652
  } catch {}
@@ -2647,7 +2667,11 @@ class AutoResetScheduler {
2647
2667
  this.cancelPlan(accountRef);
2648
2668
  return;
2649
2669
  }
2650
- const dueAt = plan.credits[0].expiresAt - AUTO_RESET_LEAD_MS;
2670
+ if (!plan.expiryEnabled) {
2671
+ this.cancelPlan(accountRef);
2672
+ return;
2673
+ }
2674
+ const dueAt = plan.credits[0].expiresAt - plan.leadMinutes * 60000;
2651
2675
  plan.handle = this.timer.schedule(() => this.executePlan(accountRef, plan), Math.min(MAX_TIMER_DELAY_MS, Math.max(0, dueAt - this.now())));
2652
2676
  }
2653
2677
  executePlan(accountRef, plan) {
@@ -2659,32 +2683,51 @@ class AutoResetScheduler {
2659
2683
  return;
2660
2684
  try {
2661
2685
  const account = accountListItem(await this.accounts.get(accountRef));
2662
- if (!account.available || !account.autoResetCredits || account.pendingAutoReset) {
2686
+ if (!account.available || !(account.autoResetCredits || account.autoResetOnLowBalance) || account.pendingAutoReset) {
2663
2687
  this.cancelPlan(accountRef, plan);
2664
2688
  return;
2665
2689
  }
2690
+ plan.leadMinutes = account.autoResetLeadMinutes;
2691
+ plan.expiryEnabled = account.autoResetCredits;
2666
2692
  while (this.plans.get(accountRef) === plan && plan.credits.length > 0) {
2667
2693
  if (this.disposed || this.signal.aborted)
2668
2694
  return;
2669
2695
  const credit = plan.credits[0];
2670
- if (credit.expiresAt - AUTO_RESET_LEAD_MS > this.now())
2696
+ const latestConfig = accountListItem(await this.accounts.get(accountRef));
2697
+ plan.leadMinutes = latestConfig.autoResetLeadMinutes;
2698
+ plan.expiryEnabled = latestConfig.autoResetCredits;
2699
+ const balanceAttempt = plan.lowBalance && latestConfig.autoResetOnLowBalance;
2700
+ if (!balanceAttempt && (!plan.expiryEnabled || credit.expiresAt - plan.leadMinutes * 60000 > this.now()))
2671
2701
  break;
2672
2702
  plan.credits.shift();
2673
- if (!isAutoResetDue(credit, this.now()))
2703
+ if (balanceAttempt)
2704
+ plan.lowBalance = false;
2705
+ if (!balanceAttempt && !isAutoResetDue(credit, this.now(), plan.leadMinutes))
2674
2706
  continue;
2675
2707
  const attempt = await this.accounts.claimAutoReset(accountRef, credit, this.now());
2676
- if (!attempt)
2708
+ if (!attempt) {
2677
2709
  continue;
2710
+ }
2678
2711
  let deferred;
2679
2712
  try {
2680
2713
  const result = await this.usage.consume(accountRef, attempt.redeemRequestId, credit.id, this.signal, {
2681
2714
  canConsume: async (selected) => {
2682
- const latest = await this.accounts.get(accountRef);
2683
- if (this.disposed || this.signal.aborted || latest.autoResetCredits !== true || latest.status !== "active")
2715
+ const fresh = balanceAttempt ? await this.usage.get(accountRef, this.signal, { forceFresh: true }).catch(() => {
2716
+ return;
2717
+ }) : undefined;
2718
+ const latest = accountListItem(await this.accounts.get(accountRef));
2719
+ if (this.disposed || this.signal.aborted || !latest.available)
2720
+ return false;
2721
+ if (balanceAttempt) {
2722
+ const first = fresh?.resetCredits.state === "fresh" && (fresh.resetCredits.availableCount ?? 0) > 0 ? [...fresh.resetCredits.credits ?? []].filter((item) => item.status === "available" && Number.isFinite(item.expiresAt) && item.expiresAt > this.now()).sort((a, b) => a.expiresAt - b.expiresAt)[0] : undefined;
2723
+ if (latest.autoResetOnLowBalance && fresh !== undefined && first?.id === selected.id && isLowBalance(fresh.usage, latest.autoResetRemainingPercent, this.now()))
2724
+ return true;
2725
+ }
2726
+ if (!latest.autoResetCredits)
2684
2727
  return false;
2685
- if (selected.status === "available" && typeof selected.expiresAt === "number" && Number.isFinite(selected.expiresAt) && selected.expiresAt - AUTO_RESET_LEAD_MS > this.now())
2728
+ if (selected.status === "available" && typeof selected.expiresAt === "number" && Number.isFinite(selected.expiresAt) && selected.expiresAt - latest.autoResetLeadMinutes * 60000 > this.now())
2686
2729
  deferred = selected;
2687
- return isAutoResetDue(selected, this.now());
2730
+ return isAutoResetDue(selected, this.now(), latest.autoResetLeadMinutes);
2688
2731
  }
2689
2732
  });
2690
2733
  if (result.outcome === "reset_outcome_unknown") {
@@ -3202,10 +3245,14 @@ class ChatgptControl {
3202
3245
  }) },
3203
3246
  { path: "/accounts/auto-reset", methods: ["POST"], handler: "setAutoResetCredits", invoke: invoke(async (context) => {
3204
3247
  const body = await this.readJson(context);
3205
- exactKeys(body, ["accountRef", "enabled"]);
3206
- if (typeof body.enabled !== "boolean")
3248
+ exactKeys(body, ["accountRef", "enabled", "leadMinutes", "lowBalanceEnabled", "remainingPercent"]);
3249
+ if ("enabled" in body && typeof body.enabled !== "boolean" || "lowBalanceEnabled" in body && typeof body.lowBalanceEnabled !== "boolean" || "leadMinutes" in body && (typeof body.leadMinutes !== "number" || !Number.isInteger(body.leadMinutes) || body.leadMinutes < 1 || body.leadMinutes > 10080) || "remainingPercent" in body && (typeof body.remainingPercent !== "number" || !Number.isInteger(body.remainingPercent) || body.remainingPercent < 0 || body.remainingPercent > 100))
3207
3250
  throw new ControlError("invalid_input");
3208
- return jsonResponse({ account: accountListItem(await this.accounts.setAutoResetCredits(requiredText(body.accountRef, 128), body.enabled)) });
3251
+ return jsonResponse({ account: accountListItem(await this.accounts.setAutoResetCredits(requiredText(body.accountRef, 128), body.enabled, {
3252
+ leadMinutes: body.leadMinutes,
3253
+ lowBalanceEnabled: body.lowBalanceEnabled,
3254
+ remainingPercent: body.remainingPercent
3255
+ })) });
3209
3256
  }) },
3210
3257
  { path: "/accounts/draft", methods: ["POST"], handler: "createDraft", invoke: invoke((context) => this.draft(context, "codex")) },
3211
3258
  { path: "/accounts/siwc/draft", methods: ["POST"], handler: "createSiwcDraft", invoke: invoke((context) => this.draft(context, "siwc")) },
@@ -3381,4 +3428,4 @@ export {
3381
3428
  rpc
3382
3429
  };
3383
3430
 
3384
- //# debugId=ADBAC825AA911C0864756E2164756E21
3431
+ //# debugId=88F390C0C885560A64756E2164756E21