Files
yetone b3a3e609ed middleware-reserve 0.1.0: routings that fill to a ceiling, or accounts before keys
A routing-strategy gateway plugin (magpie.gateway, exports strategies):
reserve/ceiling fills accounts in listed order up to `ceiling` percent of
their allowance (80 by default), then the next; past it everywhere, the
least used first. reserve/accounts-first puts subscriptions before API
keys, accounts least used first, keys by weight then recent tokens.
Resting seats go last in both. One file, no imports, for moejs.

Run:
- bun test packages/reserve: 12 tests (10 strategies.json cases through
  checkStrategies, exactly two strategies and no hooks, every answer a
  permutation for 2-8 seats). Breaking the resting rule by hand turned
  its case red.
- Sandbox magpie from yetone/magpie origin/main b03031738: `magpie plugin`
  lists "routings reserve/accounts-first, reserve/ceiling". A stub
  OpenAI-compatible provider on 127.0.0.1:3593 recorded the key of each
  request; the provider had key-a, key-b, key-c (weight 5), listed in that
  order. routing=reserve/accounts-first: 3 of 3 went to key-c. With key-c
  answering 429: key-c 429 once, then key-a, key-b, key-a (key-c resting,
  last; equal weights by fewer tokens). routing=reserve/ceiling: 4 of 4
  to key-a (keys' allowance unknown, so listed order).
Not tried: the over-ceiling tier with real subscription accounts; it is
covered by strategies.json only.
2026-10-16 02:57:43 +08:00
..

@magpie-community/middleware-reserve

Two routings for magpie that keep something in reserve. Pick one for a provider's accounts or keys, or for a routing group's members, and magpie's gateway tries them in the order it gives.

Install it from magpie's Plugins › Discover, or with magpie plugin add @magpie-community/middleware-reserve. While it is on, every routing picker offers reserve/ceiling and reserve/accounts-first. From the command line, a group takes one with:

magpie group set <id> routing=reserve/ceiling

Routings

reserve/ceiling fills the accounts in the order listed, but keeps the last share of each one's allowance.

  • The first account takes requests until it has used ceiling percent of its allowance, then the next one takes over.
  • Once every account is past the ceiling, the least used goes first.
  • A seat whose allowance isn't known (an API key, or an account not read yet) counts as below the ceiling.
  • A seat resting after a failure goes last.

Use it to keep, say, a fifth of your main account's week for when you're at its own app, while the gateway uses the rest.

reserve/accounts-first puts subscriptions before API keys, so what you have paid for already goes first. A key that bills by the token is used only when no account can take the request.

  • Accounts go least used first.
  • Keys go by their weight, highest first, then by what they were sent lately.
  • Seats resting after a failure go last.

Options

{ "ceiling": 80 }

ceiling is the percent of an account's allowance past which reserve/ceiling moves on, from 1 to 100. It is 80 when none is set. Set it under Plugins › Installed › Options, or with magpie plugin options reserve '{"ceiling": 70}'.

Test

strategies.json holds the cases: seats in, the order wanted out. reserve.test.mjs runs them with checkStrategies() from scripts/middleware.mjs, and checks that each answer is a permutation of the seats.

bun test packages/reserve

中文:两种为账号留余量的路由。reserve/ceiling 按列出的顺序使用账号,每个账号用到额度的 ceiling%(默认 80)就换下一个,全部超过后用得最少的优先;reserve/accounts-first 订阅账号优先于按量计费的 API key,key 按权重排序。休息中的总是排最后。