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

58 lines
2.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Routings that keep something in reserve, for a provider's accounts and
// keys or a group's members. magpie offers each as reserve/<name> in every
// routing picker while the plugin is on.
//
// A strategy is given the pool's name, its seats and this plugin's options,
// and returns the order to try the seats in, as indexes. Each seat has key,
// account (a subscription, not an API key), known (its allowance is known),
// used (0–100 of it), soon, pace, weight, resting and tokens. It runs in
// magpie's gateway on moejs: one file, no imports, 20 ms.
// ceiling is the share of an account's allowance past which it waits for
// the others: the options' ceiling, 80 when none is set.
function ceilingOf(options) {
const c = Number(options && options.ceiling)
return Number.isFinite(c) && c > 0 && c <= 100 ? c : 80
}
// byTier sorts the indexes by tier, then by more, keeping the listed order
// among those alike.
function byTier(seats, tier, more) {
return seats
.map((_, i) => i)
.sort((a, b) => tier(seats[a]) - tier(seats[b]) || (more ? more(seats[a], seats[b]) : 0) || a - b)
}
export const strategies = {
// Fill in the order listed, keeping each account's last share: the first
// one takes requests until it has used `ceiling` percent, then the next.
// Once every account is past it, the least used goes first. One resting
// after a failure goes last. A seat whose allowance isn't known (an API
// key, an account not read yet) counts as below the ceiling.
ceiling(pool, seats, options) {
const c = ceilingOf(options)
const over = (s) => s.known && s.used >= c
return byTier(
seats,
(s) => (s.resting ? 2 : over(s) ? 1 : 0),
(a, b) => (over(a) && over(b) ? a.used - b.used : 0),
)
},
// Subscriptions before API keys: what is paid for already goes first,
// and a key that bills by the token only when no account can take the
// request. Accounts go least used first; keys by their weight, then by
// what they were sent lately. Resting seats go last.
"accounts-first"(pool, seats) {
return byTier(
seats,
(s) => (s.resting ? 2 : s.account ? 0 : 1),
(a, b) => {
if (a.account && b.account) return (a.known ? a.used : 0) - (b.known ? b.used : 0)
if (!a.account && !b.account) return (b.weight || 1) - (a.weight || 1) || a.tokens - b.tokens
return 0
},
)
},
}