@kapybara/zerosub 1.18.3 → 1.19.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/server/routing.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { accountPeak } from "../shared/format";
2
- import { isAzureAccount, PACE_GATE_PERCENT, type Family, type PaceView, type Usage, type UsageWindow } from "../shared/model";
2
+ import { isAzureAccount, isBedrockAccount, PACE_GATE_PERCENT, type Family, type PaceView, type Usage, type UsageWindow } from "../shared/model";
3
3
  import { FIVE_HOUR_MINUTES, WEEK_MINUTES } from "./codex";
4
4
  import {
5
5
  accountsOf,
@@ -86,13 +86,13 @@ export function canStandIn(account: StoredAccount, now: number, model?: string |
86
86
  }
87
87
 
88
88
  /**
89
- * A paid API account that stands in for a subscription (an Azure OpenAI account), marked so when it
90
- * was added. Like any API account it's billed per use, so routing never puts agents on it by itself
89
+ * A paid API account that stands in for a subscription (an Azure OpenAI account under ChatGPT, an AWS
90
+ * Bedrock account under Claude), marked so when it was added. Like any API account it's billed per use, so routing never puts agents on it by itself
91
91
  * unless `paidApiFallback` is on, and then only as a last resort: once no subscription account of the
92
92
  * family can stand in (see `mostAvailable`). It can always be picked by hand, or made the default.
93
93
  */
94
94
  export function isLastResort(account: StoredAccount): boolean {
95
- return isAzureAccount(account);
95
+ return isAzureAccount(account) || isBedrockAccount(account);
96
96
  }
97
97
 
98
98
  /**
@@ -393,6 +393,16 @@ export function chooseAccount(state: StoredState, input: RouteInput): RouteDecis
393
393
  const bound = findAccount(state, binding?.accountId);
394
394
  let account = bound && bound.family === input.family ? bound : undefined;
395
395
  let bind: Binding | null = null;
396
+ // A paid stand-in (Bedrock) that ZeroSub bound the agent to by itself (a ChatGPT chat continued on
397
+ // Claude while every subscription was out) only holds it until a subscription account can take it:
398
+ // one with room, or, with `autoRedeem`, a limited one with a banked reset to spend. Then the agent
399
+ // routes as an unbound one would, and alignment takes it off the bill. One the user put there stays.
400
+ if (
401
+ account && binding?.source === "auto" && input.portable && isLastResort(account) &&
402
+ (accounts.some((other) => canStandIn(other, input.now, input.model)) || (input.autoRedeem && withBankedReset(state, input.family, input.usageOf, input.now)))
403
+ ) {
404
+ account = undefined;
405
+ }
396
406
 
397
407
  // The account named for this very agent outranks the pick, which waits for an agent that names
398
408
  // none. One that can't take the agent is passed over, and the agent routes as usual.
@@ -422,7 +432,11 @@ export function chooseAccount(state: StoredState, input: RouteInput): RouteDecis
422
432
  if (account && isLastResort(account) && input.autoRedeem) {
423
433
  account = withBankedReset(state, input.family, input.usageOf, input.now) ?? account;
424
434
  }
425
- if (account) bind = { accountId: account.id, source: "balance", at, selectionReason: nearReset ? "weekly_reset" : "headroom" };
435
+ // An agent that can move isn't bound to a paid stand-in (Bedrock): it's only there until a
436
+ // subscription account has room. A thread that can't move (Azure's) is, as it lives there.
437
+ if (account && !(input.portable && isLastResort(account))) {
438
+ bind = { accountId: account.id, source: "balance", at, selectionReason: nearReset ? "weekly_reset" : "headroom" };
439
+ }
426
440
  }
427
441
  if (!account) {
428
442
  // An existing Codex thread ZeroSub never routed (imported from the user's own CLI, or started
@@ -443,8 +457,9 @@ export function chooseAccount(state: StoredState, input: RouteInput): RouteDecis
443
457
  // A paid stand-in is only kept while no subscription account could take over from it.
444
458
  const keeps = (stand: StoredAccount) =>
445
459
  !isLastResort(stand) || !accountsOf(state, input.family).some((other) => other.id !== account!.id && canStandIn(other, input.now, input.model));
460
+ const stays = (stand: StoredAccount) => canStandIn(stand, input.now, input.model) || (isLastResort(stand) && isUsable(stand, input.now, input.model));
446
461
  let alternative =
447
- staying && staying.family === input.family && staying.id !== account.id && isUsable(staying, input.now, input.model) && keeps(staying)
462
+ staying && staying.family === input.family && staying.id !== account.id && stays(staying) && keeps(staying)
448
463
  ? staying
449
464
  : mostAvailable(state, input.family, input.usageOf, input.now, new Set([account.id]), input.paidFallback, pacing, input.model);
450
465
  // Before a paid stand-in, a limited subscription with a usable banked reset: the account itself
@@ -457,11 +472,12 @@ export function chooseAccount(state: StoredState, input: RouteInput): RouteDecis
457
472
  skipped = { account, why };
458
473
  account = alternative;
459
474
  // A thread that can't move lives where it starts. Otherwise a disabled account keeps its
460
- // agents' bindings, so they return when it's enabled again; a limit re-pins them.
475
+ // agents' bindings, so they return when it's enabled again; a limit re-pins them, except onto
476
+ // a paid stand-in (Bedrock), which they leave again once a subscription account has room.
461
477
  bind =
462
478
  !input.portable
463
479
  ? { accountId: alternative.id, source: "thread", at }
464
- : why === "disabled"
480
+ : why === "disabled" || isLastResort(alternative)
465
481
  ? null
466
482
  : { accountId: alternative.id, source: "auto", at };
467
483
  }