@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
48 lines • 2.19 kB
JavaScript
import { applyAccountOrder } from "@earendil-works/pi-ai/auth/pool/select";
export function parseAccountStrategy(value, field) {
switch (value) {
case undefined:
case "cache-affine":
case "fill-first":
case "round-robin":
return value;
default:
throw new Error(`${field} must be "cache-affine", "fill-first", or "round-robin"`);
}
}
/** All strategies share the user's stored order; only cache-affine uses HRW as its base. */
export function orderAccounts(accounts, ranked, accountOrder, strategy = "cache-affine", offset = 0) {
switch (strategy) {
case "cache-affine":
return applyAccountOrder(accountOrder, ranked);
case "fill-first":
return applyAccountOrder(accountOrder, accounts);
case "round-robin": {
const ordered = applyAccountOrder(accountOrder, accounts);
if (ordered.length === 0)
return ordered;
const start = offset % ordered.length;
return [...ordered.slice(start), ...ordered.slice(0, start)];
}
default: {
const unsupported = strategy;
throw new Error(`Unknown account strategy: ${unsupported}`);
}
}
}
/**
* Reported usage is a preference, not a block. Preserve the normal order when
* no account has headroom, and ignore windows that have already reset.
*/
export function preferHeadroom(ordered, provider, limits, now, preferredAccount) {
const preferred = ordered.find((account) => account.name === preferredAccount);
const normal = preferred === undefined ? ordered : [preferred, ...ordered.filter((account) => account !== preferred)];
const nearLimit = new Set(limits
.filter((limit) => limit.provider === provider &&
limit.windows.some((window) => window.usedPercent >= 95 &&
(window.resetsAt === undefined || !(Date.parse(window.resetsAt) <= now))))
.map((limit) => limit.account));
const withHeadroom = normal.filter((account) => !nearLimit.has(account.name));
return withHeadroom.length > 0 ? withHeadroom : normal;
}
//# sourceMappingURL=selection-order.js.map