UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

74 lines (73 loc) • 2.97 kB
/** Accepted range for an explicitly configured OAuth account admission cap. */ export const MIN_MAX_INFLIGHT_PER_ACCOUNT = 1; export const MAX_MAX_INFLIGHT_PER_ACCOUNT = 20; /** * The single definition of "a usable admission cap": an integer inside the * accepted range. Anything else — `0`, `1.5`, `21`, `NaN` — means unlimited. * * `parseProxyConfig()` already drops invalid YAML values, but `ProxyRoutingConfig` * is an exported type, so a programmatic caller can hand `ModelRouter` a value * that never passed through it. Without this, `getMaxInflightPerAccount()` would * report a bound (`0`, `21`) that the admission path ignores as unlimited, and * the two would disagree about whether the account is capped. */ export function normalizeMaxInflightPerAccount(capacity) { return typeof capacity === "number" && Number.isInteger(capacity) && capacity >= MIN_MAX_INFLIGHT_PER_ACCOUNT && capacity <= MAX_MAX_INFLIGHT_PER_ACCOUNT ? capacity : undefined; } export class ModelRouter { mappings; passthrough; fallback; autoFallback; maxInflightPerAccount; constructor(config) { this.mappings = new Map(config.modelMappings.map((m) => [m.from, m])); this.passthrough = new Set(config.passthroughModels ?? []); this.fallback = config.fallbackChain; this.autoFallback = config.autoFallback === true; this.maxInflightPerAccount = normalizeMaxInflightPerAccount(config.maxInflightPerAccount); } resolve(requestedModel) { const mapping = this.mappings.get(requestedModel); if (mapping) { return { provider: mapping.provider, model: mapping.to }; } if (this.passthrough.has(requestedModel)) { return { provider: "anthropic", model: requestedModel }; } if (requestedModel.startsWith("gemini-")) { return { provider: "vertex", model: requestedModel }; } if (requestedModel.startsWith("claude-")) { return { provider: "anthropic", model: requestedModel }; } return { provider: null, model: requestedModel }; } isClaudeTarget(requestedModel) { return this.resolve(requestedModel).provider === "anthropic"; } getFallbackChain() { return this.fallback; } /** Whether translation-layer auto-provider fallback is explicitly enabled. */ isAutoFallbackEnabled() { return this.autoFallback; } /** Explicit per-account admission cap, or undefined when admission is unlimited. */ getMaxInflightPerAccount() { return this.maxInflightPerAccount; } /** Return the raw model mapping entries (used by /v1/models). */ getModelMappings() { return Array.from(this.mappings.values()); } /** Return models configured for passthrough (used by /v1/models). */ getPassthroughModels() { return Array.from(this.passthrough); } }