diff --git a/packages/reserve/README.md b/packages/reserve/README.md new file mode 100644 index 0000000..3e78799 --- /dev/null +++ b/packages/reserve/README.md @@ -0,0 +1,44 @@ +# @magpie-community/middleware-reserve + +Two routings for [magpie](https://usemagpie.ai) 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: + +```sh +magpie group set 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 + +```json +{ "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. + +```sh +bun test packages/reserve +``` + +中文:两种为账号留余量的路由。`reserve/ceiling` 按列出的顺序使用账号,每个账号用到额度的 `ceiling`%(默认 80)就换下一个,全部超过后用得最少的优先;`reserve/accounts-first` 订阅账号优先于按量计费的 API key,key 按权重排序。休息中的总是排最后。 diff --git a/packages/reserve/package.json b/packages/reserve/package.json new file mode 100644 index 0000000..e0d3a5f --- /dev/null +++ b/packages/reserve/package.json @@ -0,0 +1,14 @@ +{ + "name": "@magpie-community/middleware-reserve", + "version": "0.1.0", + "description": "Routings for magpie's gateway that keep something in reserve: fill accounts in order up to a ceiling, or spend subscriptions before pay-per-token keys", + "type": "module", + "files": ["reserve.gateway.js", "README.md"], + "keywords": ["magpie", "magpie-middleware", "magpie-routing", "routing", "load-balancing", "llm-gateway"], + "license": "MIT", + "repository": { "type": "git", "url": "git+https://github.com/magpie-community/plugins.git", "directory": "packages/reserve" }, + "magpie": { + "gateway": "./reserve.gateway.js", + "options": { "ceiling": 80 } + } +} diff --git a/packages/reserve/reserve.gateway.js b/packages/reserve/reserve.gateway.js new file mode 100644 index 0000000..135e782 --- /dev/null +++ b/packages/reserve/reserve.gateway.js @@ -0,0 +1,57 @@ +// Routings that keep something in reserve, for a provider's accounts and +// keys or a group's members. magpie offers each as reserve/ 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 + }, + ) + }, +} diff --git a/packages/reserve/reserve.test.mjs b/packages/reserve/reserve.test.mjs new file mode 100644 index 0000000..9d4e97d --- /dev/null +++ b/packages/reserve/reserve.test.mjs @@ -0,0 +1,22 @@ +import { expect, test } from "bun:test" +import * as mod from "./reserve.gateway.js" +import { checkStrategies, order } from "../../scripts/middleware.mjs" + +checkStrategies(import.meta.dir, mod) + +test("gives the two routings, and no hook it doesn't use", () => { + expect(Object.keys(mod.strategies).sort()).toEqual(["accounts-first", "ceiling"]) + expect(mod.onRequest).toBeUndefined() + expect(mod.onUsage).toBeUndefined() +}) + +test("every answer is an order of the seats, whatever they hold", () => { + for (const name of Object.keys(mod.strategies)) { + for (let n = 2; n < 9; n++) { + const seats = Array.from({ length: n }, (_, i) => ({ + account: i % 2 === 0, known: i % 3 !== 0, used: (i * 37) % 101, resting: i === 4, weight: (i % 3) + 1, tokens: i * 1000, + })) + expect(order(mod, name, "group/fast", seats, { ceiling: 50 }).failed).toBeUndefined() + } + } +}) diff --git a/packages/reserve/strategies.json b/packages/reserve/strategies.json new file mode 100644 index 0000000..398683b --- /dev/null +++ b/packages/reserve/strategies.json @@ -0,0 +1,100 @@ +[ + { + "name": "ceiling: the first account takes requests until it reaches the ceiling", + "strategy": "ceiling", + "seats": [ + { "key": "work", "account": true, "known": true, "used": 42 }, + { "key": "home", "account": true, "known": true, "used": 5 } + ], + "expect": ["work", "home"] + }, + { + "name": "ceiling: an account past the ceiling waits for the next", + "strategy": "ceiling", + "seats": [ + { "key": "work", "account": true, "known": true, "used": 83 }, + { "key": "home", "account": true, "known": true, "used": 5 }, + { "key": "spare", "account": true, "known": true, "used": 60 } + ], + "expect": ["home", "spare", "work"] + }, + { + "name": "ceiling: the options set the ceiling", + "strategy": "ceiling", + "options": { "ceiling": 50 }, + "seats": [ + { "key": "work", "account": true, "known": true, "used": 55 }, + { "key": "home", "account": true, "known": true, "used": 49.5 } + ], + "expect": ["home", "work"] + }, + { + "name": "ceiling: with every account past it, the least used goes first", + "strategy": "ceiling", + "seats": [ + { "key": "a", "account": true, "known": true, "used": 97 }, + { "key": "b", "account": true, "known": true, "used": 81 }, + { "key": "c", "account": true, "known": true, "used": 90 } + ], + "expect": ["b", "c", "a"] + }, + { + "name": "ceiling: one resting goes last, even below the ceiling", + "strategy": "ceiling", + "seats": [ + { "key": "a", "account": true, "known": true, "used": 10, "resting": true }, + { "key": "b", "account": true, "known": true, "used": 95 }, + { "key": "c", "account": true, "known": true, "used": 20 } + ], + "expect": ["c", "b", "a"] + }, + { + "name": "ceiling: an allowance not known counts as below it, in its place", + "strategy": "ceiling", + "seats": [ + { "key": "a", "account": true, "known": true, "used": 99 }, + { "key": "new", "account": true, "known": false }, + { "key": "key", "account": false } + ], + "expect": ["new", "key", "a"] + }, + { + "name": "ceiling: an option that isn't a share is 80", + "strategy": "ceiling", + "options": { "ceiling": "lots" }, + "seats": [ + { "key": "a", "account": true, "known": true, "used": 79 }, + { "key": "b", "account": true, "known": true, "used": 0 } + ], + "expect": ["a", "b"] + }, + { + "name": "accounts-first: subscriptions before keys, least used first", + "strategy": "accounts-first", + "seats": [ + { "key": "paid-key", "account": false, "tokens": 0 }, + { "key": "pro", "account": true, "known": true, "used": 70 }, + { "key": "max", "account": true, "known": true, "used": 20 } + ], + "expect": ["max", "pro", "paid-key"] + }, + { + "name": "accounts-first: keys by weight, then fewest tokens lately", + "strategy": "accounts-first", + "seats": [ + { "key": "k1", "weight": 1, "tokens": 10 }, + { "key": "k2", "weight": 3, "tokens": 900000 }, + { "key": "k3", "weight": 1, "tokens": 2 } + ], + "expect": ["k2", "k3", "k1"] + }, + { + "name": "accounts-first: a resting account goes after the keys", + "strategy": "accounts-first", + "seats": [ + { "key": "pro", "account": true, "known": true, "used": 1, "resting": true }, + { "key": "key", "account": false } + ], + "expect": ["key", "pro"] + } +]