UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

49 lines • 2.74 kB
import { rendezvousOrder } from "@earendil-works/pi-ai/auth/pool/select"; import { attemptCandidates, noSlotsError } from "./failover.js"; import { orderAccounts, preferHeadroom } from "./selection-order.js"; /** Only headroom-induced moves are remembered; ordinary HRW membership changes keep their existing behavior. */ const headroomAffinities = new WeakMap(); /** The account a headroom skip moved this session to, so the stream and auxiliary requests read the same one. */ export function rememberedHeadroomAccount(repository, providerId, affinityKey) { return headroomAffinities.get(repository)?.get(`${providerId}\0${affinityKey}`); } export function rememberHeadroomAccount(repository, providerId, affinityKey, account) { const sessions = headroomAffinities.get(repository) ?? new Map(); sessions.set(`${providerId}\0${affinityKey}`, account); headroomAffinities.set(repository, sessions); } /** The strategy's order with the user's order applied, before the pin and before any headroom preference. */ export function orderRotationSlots(candidates, policy) { const ranked = policy.useAffinity ? rendezvousOrder(policy.affinityKey, candidates, policy.hasher) : candidates; return orderAccounts(candidates, ranked, policy.order, policy.strategy, policy.roundRobinOffset); } /** * The pin when present, else the first account of the affinity order with the user's order applied, * preferring an account with headroom when the policy carries limit readings. */ export function pickRotationSlot(candidates, policy) { const pinned = candidates.find((candidate) => candidate.pinned === true); if (pinned) return pinned; const ordered = orderRotationSlots(candidates, policy); const headroom = policy.headroom; if (!headroom) return ordered[0]; return preferHeadroom(ordered, headroom.providerId, headroom.limits, headroom.now, headroom.remembered)[0]; } /** * The account a session's next streamed request starts on, for callers that send a resolved key * outside the rotation stream (compaction, /btw, look-at, cache keepalive). It is the stream's own * first pick: the same candidates (unblocked accounts, else the rate-limited one due back first) and * the same order. When the stream would refuse, this throws the stream's refusal, so an auxiliary * request never authenticates as an account the stream would not use. */ export function selectSessionSlot(slots, policy, now) { if (slots.length === 0) return undefined; const candidates = attemptCandidates(slots, new Set(), now); if (candidates.length === 0) throw noSlotsError(slots, now); return pickRotationSlot(candidates, policy); } //# sourceMappingURL=session-slot.js.map