@zhivex-ai/gateway 1.2.2 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/budget.js ADDED
@@ -0,0 +1,53 @@
1
+ import { GatewayError } from "./types.js";
2
+ export class GatewayBudgetError extends GatewayError {
3
+ constructor() { super("Gateway budget reservation denied or unavailable.", false); this.name = "GatewayBudgetError"; }
4
+ }
5
+ /** Process-local lifetime budgets. Use a new store/scope for a new accounting period. */
6
+ export const createGatewayBudgetStore = (options) => {
7
+ const capacity = options.maxScopes ?? 1000;
8
+ if (!Number.isFinite(options.limit) || options.limit < 0 || !options.currency.trim() || !Number.isSafeInteger(capacity) || capacity < 1 || capacity > 100000)
9
+ throw new GatewayError("Invalid gateway budget configuration.", false);
10
+ const entries = new Map();
11
+ return {
12
+ async reserve({ scope, amount, currency, signal }) {
13
+ signal?.throwIfAborted();
14
+ if (!scope.trim() || scope.length > 256 || currency !== options.currency || !Number.isFinite(amount) || amount <= 0)
15
+ throw new GatewayBudgetError();
16
+ let entry = entries.get(scope);
17
+ if (!entry) {
18
+ // Never evict spend state: doing so would reset a caller's budget.
19
+ if (entries.size >= capacity)
20
+ throw new GatewayBudgetError();
21
+ entry = { spent: 0, reserved: 0, uncertain: 0 };
22
+ entries.set(scope, entry);
23
+ }
24
+ if (entry.spent + entry.reserved + entry.uncertain + amount > options.limit)
25
+ throw new GatewayBudgetError();
26
+ entry.reserved += amount;
27
+ let settled = false;
28
+ return {
29
+ settle(actual) {
30
+ if (settled)
31
+ return;
32
+ if (actual !== null && (!Number.isFinite(actual) || actual < 0))
33
+ throw new GatewayBudgetError();
34
+ settled = true;
35
+ entry.reserved = Math.max(0, entry.reserved - amount);
36
+ if (actual === null)
37
+ entry.uncertain += amount;
38
+ else
39
+ entry.spent += actual;
40
+ },
41
+ cancel() { if (!settled) {
42
+ settled = true;
43
+ entry.reserved = Math.max(0, entry.reserved - amount);
44
+ } }
45
+ };
46
+ },
47
+ snapshot(scope) {
48
+ const entry = entries.get(scope) ?? { spent: 0, reserved: 0, uncertain: 0 };
49
+ return { ...entry, remaining: Math.max(0, options.limit - entry.spent - entry.reserved - entry.uncertain) };
50
+ }
51
+ };
52
+ };
53
+ //# sourceMappingURL=budget.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"budget.js","sourceRoot":"","sources":["../src/budget.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC1C,MAAM,OAAO,kBAAmB,SAAQ,YAAY;IAClD,gBAAgB,KAAK,CAAC,mDAAmD,EAAE,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC,CAAC,CAAC;CACvH;AAWD,yFAAyF;AACzF,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,OAAgE,EAA2E,EAAE;IACpL,MAAM,QAAQ,GAAG,OAAO,CAAC,SAAS,IAAI,IAAI,CAAC;IAC3C,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,CAAC,KAAK,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,QAAQ,CAAC,IAAI,QAAQ,GAAG,CAAC,IAAI,QAAQ,GAAG,MAAM;QAAE,MAAM,IAAI,YAAY,CAAC,uCAAuC,EAAE,KAAK,CAAC,CAAC;IACrO,MAAM,OAAO,GAAG,IAAI,GAAG,EAAkE,CAAC;IAC1F,OAAO;QACL,KAAK,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE;YAC/C,MAAM,EAAE,cAAc,EAAE,CAAC;YACzB,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,KAAK,CAAC,MAAM,GAAG,GAAG,IAAI,QAAQ,KAAK,OAAO,CAAC,QAAQ,IAAI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D,MAAM,KAAK,IAAI,CAAC,OAAO,EAAE,CAAC;oBAAC,OAAO,GAAG,IAAI,CAAC;oBAAC,KAAM,CAAC,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAM,CAAC,QAAQ,GAAG,MAAM,CAAC,CAAC;gBAAC,CAAC,CAAC,CAAC;aACxG,CAAC;QACJ,CAAC;QACD,QAAQ,CAAC,KAAK;YACZ,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,CAAC;YAC5E,OAAO,EAAE,GAAG,KAAK,EAAE,SAAS,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,QAAQ,GAAG,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC;QAC9G,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
@@ -0,0 +1,36 @@
1
+ import { GatewayError, type GatewayModelTarget } from "./types.js";
2
+ export type GatewayCircuitState = "closed" | "open" | "half-open";
3
+ export interface GatewayCircuitSnapshot {
4
+ state: GatewayCircuitState;
5
+ failures: number;
6
+ inFlight: number;
7
+ openUntil?: number;
8
+ }
9
+ export interface GatewayCircuitPermit {
10
+ end(outcome: "success" | "retryable-error" | "neutral", retryAfterMs?: number): void;
11
+ }
12
+ export interface GatewayCircuitBreaker {
13
+ acquire(target: GatewayModelTarget): GatewayCircuitPermit | undefined;
14
+ snapshot(target: GatewayModelTarget): GatewayCircuitSnapshot | undefined;
15
+ canAttempt(target: GatewayModelTarget): boolean;
16
+ }
17
+ export interface GatewayCircuitOptions {
18
+ failureThreshold?: number;
19
+ cooldownMs?: number;
20
+ maxCooldownMs?: number;
21
+ halfOpenMaxProbes?: number;
22
+ maxTargets?: number;
23
+ now?: () => number;
24
+ onStateChange?: (event: {
25
+ target: GatewayModelTarget;
26
+ from: GatewayCircuitState;
27
+ to: GatewayCircuitState;
28
+ at: number;
29
+ openUntil?: number;
30
+ }) => void | Promise<void>;
31
+ }
32
+ export declare class GatewayCircuitOpenError extends GatewayError {
33
+ constructor();
34
+ }
35
+ export declare const createGatewayCircuitBreaker: (options?: GatewayCircuitOptions) => GatewayCircuitBreaker;
36
+ //# sourceMappingURL=circuit-breaker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"circuit-breaker.d.ts","sourceRoot":"","sources":["../src/circuit-breaker.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,KAAK,kBAAkB,EAAE,MAAM,YAAY,CAAC;AACnE,MAAM,MAAM,mBAAmB,GAAG,QAAQ,GAAG,MAAM,GAAG,WAAW,CAAC;AAClE,MAAM,WAAW,sBAAsB;IAAG,KAAK,EAAE,mBAAmB,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAAE;AAC/H,MAAM,WAAW,oBAAoB;IAAG,GAAG,CAAC,OAAO,EAAE,SAAS,GAAG,iBAAiB,GAAG,SAAS,EAAE,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAAE;AAC/H,MAAM,WAAW,qBAAqB;IACpC,OAAO,CAAC,MAAM,EAAE,kBAAkB,GAAG,oBAAoB,GAAG,SAAS,CAAC;IACtE,QAAQ,CAAC,MAAM,EAAE,kBAAkB,GAAG,sBAAsB,GAAG,SAAS,CAAC;IACzE,UAAU,CAAC,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC;CACjD;AACD,MAAM,WAAW,qBAAqB;IACpC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE;QAAE,MAAM,EAAE,kBAAkB,CAAC;QAAC,IAAI,EAAE,mBAAmB,CAAC;QAAC,EAAE,EAAE,mBAAmB,CAAC;QAAC,EAAE,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACrK;AACD,qBAAa,uBAAwB,SAAQ,YAAY;IACvD,cAAqJ;CACtJ;AACD,eAAO,MAAM,2BAA2B,aAAa,qBAAqB,KAAQ,qBAuDjF,CAAC"}
@@ -0,0 +1,86 @@
1
+ import { targetKey } from "./target.js";
2
+ import { GatewayError } from "./types.js";
3
+ export class GatewayCircuitOpenError extends GatewayError {
4
+ constructor() { super("All eligible destination circuits are open or have exhausted probe capacity.", true); this.name = "GatewayCircuitOpenError"; }
5
+ }
6
+ export const createGatewayCircuitBreaker = (options = {}) => {
7
+ const threshold = options.failureThreshold ?? 5, cooldown = options.cooldownMs ?? 30_000, maxCooldown = options.maxCooldownMs ?? 60_000, probes = options.halfOpenMaxProbes ?? 1, capacity = options.maxTargets ?? 128;
8
+ for (const [value, max] of [[threshold, 1000], [cooldown, 86_400_000], [maxCooldown, 86_400_000], [probes, 100], [capacity, 10_000]]) {
9
+ if (!Number.isSafeInteger(value) || value < 1 || value > max)
10
+ throw new GatewayError("Invalid circuit breaker limits.", false);
11
+ }
12
+ if (cooldown > maxCooldown)
13
+ throw new GatewayError("cooldownMs cannot exceed maxCooldownMs.", false);
14
+ const now = options.now ?? Date.now;
15
+ const entries = new Map();
16
+ const key = targetKey;
17
+ const transition = (entry, target, to) => {
18
+ const from = entry.state;
19
+ entry.state = to;
20
+ entry.epoch++;
21
+ if (from === to || !options.onStateChange)
22
+ return;
23
+ const event = { target: { ...target }, from, to, at: now(), openUntil: entry.openUntil };
24
+ queueMicrotask(() => { try {
25
+ void Promise.resolve(options.onStateChange(event)).catch(() => undefined);
26
+ }
27
+ catch { /* Observer failure cannot change policy. */ } });
28
+ };
29
+ return {
30
+ canAttempt(target) {
31
+ const entry = entries.get(key(target));
32
+ return !entry || entry.state === "closed" || ((entry.state === "half-open" || now() >= entry.openUntil) && entry.inFlight < probes);
33
+ },
34
+ acquire(target) {
35
+ const at = now(), id = key(target);
36
+ let entry = entries.get(id);
37
+ if (!entry) {
38
+ if (entries.size >= capacity) {
39
+ const idle = [...entries].filter(([, x]) => x.inFlight === 0 && x.state === "closed").sort((a, b) => a[1].touched - b[1].touched)[0];
40
+ if (!idle)
41
+ return undefined;
42
+ entries.delete(idle[0]);
43
+ }
44
+ entry = { state: "closed", failures: 0, inFlight: 0, epoch: 0, touched: at };
45
+ entries.set(id, entry);
46
+ }
47
+ if (entry.state === "open") {
48
+ if (at < entry.openUntil)
49
+ return undefined;
50
+ transition(entry, target, "half-open");
51
+ }
52
+ if (entry.state === "half-open" && entry.inFlight >= probes)
53
+ return undefined;
54
+ entry.inFlight++;
55
+ entry.touched = at;
56
+ const epoch = entry.epoch;
57
+ let ended = false;
58
+ return { end(outcome, retryAfterMs) {
59
+ if (ended)
60
+ return;
61
+ ended = true;
62
+ entry.inFlight--;
63
+ entry.touched = now();
64
+ if (epoch !== entry.epoch)
65
+ return;
66
+ if (outcome === "success") {
67
+ entry.failures = 0;
68
+ entry.openUntil = undefined;
69
+ if (entry.state !== "closed")
70
+ transition(entry, target, "closed");
71
+ }
72
+ if (outcome === "retryable-error") {
73
+ entry.failures++;
74
+ if (entry.state === "half-open" || entry.failures >= threshold) {
75
+ const serverDelay = Number.isFinite(retryAfterMs) && retryAfterMs > 0 ? retryAfterMs : 0;
76
+ entry.openUntil = now() + Math.min(maxCooldown, Math.max(cooldown, serverDelay));
77
+ transition(entry, target, "open");
78
+ }
79
+ }
80
+ } };
81
+ },
82
+ snapshot(target) { const entry = entries.get(key(target)); if (!entry)
83
+ return undefined; const { state, failures, inFlight, openUntil } = entry; return { state, failures, inFlight, openUntil }; }
84
+ };
85
+ };
86
+ //# sourceMappingURL=circuit-breaker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"circuit-breaker.js","sourceRoot":"","sources":["../src/circuit-breaker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,YAAY,EAA2B,MAAM,YAAY,CAAC;AAkBnE,MAAM,OAAO,uBAAwB,SAAQ,YAAY;IACvD,gBAAgB,KAAK,CAAC,8EAA8E,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC,CAAC,CAAC;CACtJ;AACD,MAAM,CAAC,MAAM,2BAA2B,GAAG,CAAC,OAAO,GAA0B,EAAE,EAAyB,EAAE;IACxG,MAAM,SAAS,GAAG,OAAO,CAAC,gBAAgB,IAAI,CAAC,EAAE,QAAQ,GAAG,OAAO,CAAC,UAAU,IAAI,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,aAAa,IAAI,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,IAAI,CAAC,EAAE,QAAQ,GAAG,OAAO,CAAC,UAAU,IAAI,GAAG,CAAC;IACvN,KAAK,MAAM,CAAC,KAAK,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,IAAI,CAAC,EAAE,CAAC,QAAQ,EAAE,UAAU,CAAC,EAAE,CAAC,WAAW,EAAE,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC;QACrI,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAM,GAAG,CAAC,IAAI,KAAM,GAAG,GAAI;YAAE,MAAM,IAAI,YAAY,CAAC,iCAAiC,EAAE,KAAK,CAAC,CAAC;IACpI,CAAC;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AAC,QAAQ,EAAE,WAAW,CAAC,CAAC,CAAC;4BAClF,UAAU,CAAC,KAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;wBACrC,CAAC;oBACH,CAAC;gBACH,CAAC,EAAE,CAAC;QACN,CAAC;QACD,QAAQ,CAAC,MAAM,IAAI,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK;YAAE,OAAO,SAAS,CAAC,CAAC,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,KAAK,CAAC,CAAC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;KACpM,CAAC;AACJ,CAAC,CAAC"}
@@ -0,0 +1,17 @@
1
+ import { type GenerateResult, type LanguageModel, type ModelGenerateInput, type StreamEvent } from "@zhivex-ai/core";
2
+ import { type GatewayConfig, type GatewayModelTarget } from "./types.js";
3
+ export declare const cachedResults: WeakSet<GenerateResult>;
4
+ /** Provider dispatch, resource lifetimes and exact caching are shared by every routed operation. */
5
+ export declare const createGatewayExecutor: (config: GatewayConfig) => {
6
+ diagnostics: () => {
7
+ settlementFailures: number;
8
+ pendingSettlements: number;
9
+ pendingCacheWrites: number;
10
+ droppedCacheWrites: number;
11
+ inFlightCacheKeys: number;
12
+ };
13
+ flush: () => Promise<void>;
14
+ generate(model: LanguageModel, target: GatewayModelTarget, input: ModelGenerateInput, scope?: string, cacheScope?: string): Promise<GenerateResult>;
15
+ stream(model: LanguageModel, target: GatewayModelTarget, input: ModelGenerateInput, scope?: string): Promise<AsyncIterable<StreamEvent>>;
16
+ };
17
+ //# sourceMappingURL=execution.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"execution.d.ts","sourceRoot":"","sources":["../src/execution.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsD,KAAK,cAAc,EAAE,KAAK,aAAa,EAAE,KAAK,kBAAkB,EAAE,KAAK,WAAW,EAAmB,MAAM,iBAAiB,CAAC;AAC1L,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAKzE,eAAO,MAAM,aAAa,yBAAgC,CAAC;AAE3D,oGAAoG;AACpG,eAAO,MAAM,qBAAqB,WAAY,aAAa;;;;;;;;;oBAsEjC,aAAa,UAAU,kBAAkB,SAAS,kBAAkB,UAAU,MAAM,eAAe,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC;kBAwDrI,aAAa,UAAU,kBAAkB,SAAS,kBAAkB,UAAU,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,WAAW,CAAC,CAAC;CAcjJ,CAAC"}
@@ -0,0 +1,223 @@
1
+ import { calculateModelCost, createCachedGenerateMiddleware } from "@zhivex-ai/core";
2
+ import { GatewayBudgetError } from "./budget.js";
3
+ import { targetKey } from "./target.js";
4
+ import { operationControl } from "./operation.js";
5
+ export const cachedResults = new WeakSet();
6
+ const reportedTokens = (usage) => usage?.totalTokens !== undefined && Number.isSafeInteger(usage.totalTokens) && usage.totalTokens >= 0 ? usage.totalTokens : undefined;
7
+ /** Provider dispatch, resource lifetimes and exact caching are shared by every routed operation. */
8
+ export const createGatewayExecutor = (config) => {
9
+ const flights = new Map();
10
+ let settlementFailures = 0, droppedCacheWrites = 0;
11
+ const cacheWrites = new Set();
12
+ const settlements = new Set();
13
+ const safe = (action) => {
14
+ try {
15
+ const result = action();
16
+ if (result) {
17
+ const control = operationControl(undefined, config.resourceTimeoutMs ?? 1000);
18
+ const completion = control.wait(Promise.resolve(result)).catch(() => { settlementFailures++; }).finally(() => { control.dispose(); settlements.delete(completion); });
19
+ settlements.add(completion);
20
+ }
21
+ }
22
+ catch {
23
+ settlementFailures++;
24
+ }
25
+ };
26
+ const begin = async (target, input, scope) => {
27
+ const signal = input.abortSignal;
28
+ signal?.throwIfAborted();
29
+ let admission, reservation;
30
+ let started = false, ended = false;
31
+ const finish = async (usage) => {
32
+ if (ended)
33
+ return;
34
+ ended = true;
35
+ signal?.removeEventListener("abort", abort);
36
+ const acquired = admission;
37
+ admission = undefined;
38
+ if (acquired)
39
+ safe(() => acquired.release(reportedTokens(usage)));
40
+ if (reservation) {
41
+ let amount = null;
42
+ if (usage) {
43
+ try {
44
+ const cost = calculateModelCost({ catalog: config.modelCatalog, ...target, usage,
45
+ cacheAssumption: config.costAccounting?.cacheAssumption,
46
+ reasoningAccounting: config.costAccounting?.reasoningAccounting?.[target.provider] });
47
+ if (cost.currency === config.budget.currency && cost.status === "known")
48
+ amount = cost.amount;
49
+ }
50
+ catch { /* Unknown usage retains its reservation. */ }
51
+ }
52
+ const held = reservation;
53
+ reservation = undefined;
54
+ safe(() => started ? held.settle(amount) : held.cancel());
55
+ }
56
+ };
57
+ const abort = () => { void finish(); };
58
+ signal?.addEventListener("abort", abort, { once: true });
59
+ try {
60
+ if (config.admission)
61
+ admission = await config.admission.acquire({ target, signal,
62
+ tokens: input.maxTokens === undefined ? undefined : Math.ceil(JSON.stringify(input.messages).length / 4) + input.maxTokens });
63
+ if (signal?.aborted) {
64
+ const late = admission;
65
+ admission = undefined;
66
+ if (late)
67
+ safe(() => late.release());
68
+ signal.throwIfAborted();
69
+ }
70
+ if (config.budget) {
71
+ if (settlementFailures || settlements.size >= 1024 || !scope?.trim())
72
+ throw new GatewayBudgetError();
73
+ try {
74
+ reservation = await config.budget.store.reserve({ scope, currency: config.budget.currency, amount: config.budget.reserveAmount, signal });
75
+ }
76
+ catch {
77
+ throw new GatewayBudgetError();
78
+ }
79
+ }
80
+ if (signal?.aborted) {
81
+ const late = reservation;
82
+ reservation = undefined;
83
+ if (late)
84
+ safe(() => late.cancel());
85
+ signal.throwIfAborted();
86
+ }
87
+ return { start() { signal?.throwIfAborted(); started = true; }, finish };
88
+ }
89
+ catch (error) {
90
+ await finish();
91
+ throw error;
92
+ }
93
+ };
94
+ const generate = async (model, target, input, scope) => {
95
+ const lease = await begin(target, input, scope);
96
+ try {
97
+ lease.start();
98
+ const result = await model.generate(input);
99
+ await lease.finish(result.usage);
100
+ return result;
101
+ }
102
+ catch (error) {
103
+ await lease.finish();
104
+ throw error;
105
+ }
106
+ };
107
+ return {
108
+ diagnostics: () => ({ settlementFailures, pendingSettlements: settlements.size, pendingCacheWrites: cacheWrites.size, droppedCacheWrites, inFlightCacheKeys: flights.size }),
109
+ flush: async () => { await Promise.all([...settlements, ...cacheWrites]); },
110
+ async generate(model, target, input, scope, cacheScope) {
111
+ // Cache only pure text model steps; never replay tool calls, provider state or effects.
112
+ const eligible = config.cache && cacheScope?.trim() && !input.tools?.length && !input.providerOptions && input.messages.every(m => m.parts.every(p => p.type === "text"));
113
+ if (!eligible)
114
+ return generate(model, target, input, scope);
115
+ let key, hit = false;
116
+ const middleware = createCachedGenerateMiddleware({
117
+ scope: JSON.stringify([config.cache.scope, cacheScope, scope ?? null, targetKey(target)]),
118
+ cache: {
119
+ async get(id) {
120
+ key = id;
121
+ const control = operationControl(input.abortSignal, config.cache.timeoutMs ?? 50);
122
+ try {
123
+ const value = await control.wait(Promise.resolve(config.cache.store.get(id)));
124
+ if (value) {
125
+ hit = true;
126
+ return structuredClone(value);
127
+ }
128
+ }
129
+ catch {
130
+ input.abortSignal?.throwIfAborted(); /* Cache outages are misses. */
131
+ }
132
+ finally {
133
+ control.dispose();
134
+ }
135
+ return undefined;
136
+ },
137
+ set(id, value) {
138
+ // Stores may hang: writes must never delay a completed provider response.
139
+ if (value.finishReason === "tool-calls" || value.audio?.length || value.images?.length ||
140
+ value.message?.parts.some(part => part.type !== "text") ||
141
+ value.messages?.some(message => message.parts.some(part => part.type !== "text")))
142
+ return;
143
+ if (cacheWrites.size >= 256) {
144
+ droppedCacheWrites++;
145
+ return;
146
+ }
147
+ const control = operationControl(undefined, config.cache.timeoutMs ?? 50);
148
+ try {
149
+ const write = control.wait(Promise.resolve(config.cache.store.set(id, structuredClone(value))))
150
+ .catch(() => undefined).finally(() => { control.dispose(); cacheWrites.delete(write); });
151
+ cacheWrites.add(write);
152
+ }
153
+ catch {
154
+ control.dispose();
155
+ }
156
+ }
157
+ }
158
+ });
159
+ const result = await middleware.wrapGenerate({ model, input }, async () => {
160
+ input.abortSignal?.throwIfAborted();
161
+ if (!key)
162
+ return generate(model, target, input, scope);
163
+ let flight = flights.get(key);
164
+ if (!flight) {
165
+ if (flights.size >= 1024)
166
+ return generate(model, target, input, scope);
167
+ const controller = new AbortController();
168
+ const promise = generate(model, target, { ...input, abortSignal: controller.signal }, scope);
169
+ flight = { controller, promise, users: 0 };
170
+ flights.set(key, flight);
171
+ const id = key, current = flight;
172
+ void promise.finally(() => { if (flights.get(id) === current)
173
+ flights.delete(id); }).catch(() => undefined);
174
+ }
175
+ else
176
+ hit = true;
177
+ flight.users++;
178
+ const wait = operationControl(input.abortSignal);
179
+ try {
180
+ return structuredClone(await wait.wait(flight.promise));
181
+ }
182
+ finally {
183
+ wait.dispose();
184
+ flight.users--;
185
+ if (flight.users === 0) {
186
+ if (flights.get(key) === flight)
187
+ flights.delete(key);
188
+ flight.controller.abort();
189
+ }
190
+ }
191
+ });
192
+ input.abortSignal?.throwIfAborted();
193
+ if (hit)
194
+ cachedResults.add(result);
195
+ return result;
196
+ },
197
+ async stream(model, target, input, scope) {
198
+ const lease = await begin(target, input, scope);
199
+ try {
200
+ lease.start();
201
+ const source = await model.stream(input);
202
+ return (async function* () {
203
+ let usage;
204
+ try {
205
+ for await (const event of source) {
206
+ if (event.type === "finish")
207
+ usage = event.usage;
208
+ yield event;
209
+ }
210
+ }
211
+ finally {
212
+ await lease.finish(usage);
213
+ }
214
+ })();
215
+ }
216
+ catch (error) {
217
+ await lease.finish();
218
+ throw error;
219
+ }
220
+ }
221
+ };
222
+ };
223
+ //# sourceMappingURL=execution.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"execution.js","sourceRoot":"","sources":["../src/execution.ts"],"names":[],"mappings":"AAAA,OAAO,EAA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,CAAC;gBAAC,MAAM,KAAK,CAAC;YAAC,CAAC;QACxD,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,13 +1,36 @@
1
+ export { GatewayDeadlineError } from "./operation.js";
2
+ export { createGatewayAdmissionController, GatewayAdmissionError } from "./admission.js";
3
+ export type { GatewayAdmissionController, GatewayAdmissionLease, GatewayAdmissionOptions } from "./admission.js";
4
+ export { createGatewayBudgetStore, GatewayBudgetError } from "./budget.js";
5
+ export type { GatewayBudgetStore, GatewayBudgetReservation, GatewayBudgetSnapshot } from "./budget.js";
6
+ export { createGatewayRoutingPolicy } from "./adaptive-routing.js";
1
7
  import type { ZodTypeAny } from "zod";
8
+ export type { GatewayAdaptiveCandidate, GatewayAdaptiveRoutingPolicy, GatewayQualityProfile } from "./adaptive-routing.js";
9
+ export { createGatewayCircuitBreaker, GatewayCircuitOpenError } from "./circuit-breaker.js";
10
+ export type { GatewayCircuitBreaker, GatewayCircuitOptions, GatewayCircuitPermit, GatewayCircuitSnapshot, GatewayCircuitState } from "./circuit-breaker.js";
11
+ export { createGatewayMetrics } from "./metrics.js";
12
+ export type { GatewayMetricsHandle, GatewayMetricsOptions, GatewayMetricsSnapshot, GatewayMetricsStore, GatewayMetricOutcome } from "./metrics.js";
2
13
  import { type GatewayAgentRequest, type GatewayAgentResponse, type GatewayAgentStreamResult, type GatewayConfig, type GatewayGenerateObjectRequest, type GatewayObjectResponse, type GatewayRequest, type GatewayResponse, type GatewayStreamObjectResult, type GatewayStreamTextResult } from "./types.js";
3
14
  export { GatewayError } from "./types.js";
4
15
  export type { GatewayAgentRequest, GatewayAgentResponse, GatewayAgentStreamResult, GatewayAttempt, GatewayAttemptReasonCode, GatewayConfig, GatewayGenerateObjectRequest, GatewayImageAttachment, GatewayInputMessage, GatewayMessage, GatewayModelTarget, GatewayObjectResponse, GatewayProviderId, GatewayRequest, GatewayResponse, GatewayRouteDecisionReasonCode, GatewayRoutingMode, GatewayRoutingScoreContext, GatewayStreamObjectResult, GatewayStreamTextResult, GatewayTaskIntent, GatewayUnknownCostPolicy } from "./types.js";
5
16
  export declare const createGateway: (config: GatewayConfig) => {
6
- generate(request: GatewayRequest): Promise<GatewayResponse>;
7
- streamText(request: GatewayRequest): GatewayStreamTextResult;
8
- generateObject<TSchema extends ZodTypeAny>(request: GatewayGenerateObjectRequest<TSchema>): Promise<GatewayObjectResponse<TSchema>>;
9
- streamObject<TSchema extends ZodTypeAny>(request: GatewayGenerateObjectRequest<TSchema>): GatewayStreamObjectResult<TSchema>;
10
- runAgent(request: GatewayAgentRequest): Promise<GatewayAgentResponse>;
11
- streamAgent(request: GatewayAgentRequest): GatewayAgentStreamResult;
17
+ flushControls: () => Promise<void>;
18
+ flushObservers: () => Promise<void>;
19
+ diagnostics: () => {
20
+ settlementFailures: number;
21
+ pendingSettlements: number;
22
+ pendingCacheWrites: number;
23
+ droppedCacheWrites: number;
24
+ inFlightCacheKeys: number;
25
+ pendingObservers: number;
26
+ droppedObservers: number;
27
+ affinityEntries: number;
28
+ };
29
+ generate: (request: GatewayRequest) => Promise<GatewayResponse>;
30
+ generateObject: <TSchema extends ZodTypeAny>(request: GatewayGenerateObjectRequest<TSchema>) => Promise<GatewayObjectResponse<TSchema>>;
31
+ runAgent: (request: GatewayAgentRequest) => Promise<GatewayAgentResponse>;
32
+ streamText: (request: GatewayRequest) => GatewayStreamTextResult;
33
+ streamObject: <TSchema extends ZodTypeAny>(request: GatewayGenerateObjectRequest<TSchema>) => GatewayStreamObjectResult<TSchema>;
34
+ streamAgent: (request: GatewayAgentRequest) => GatewayAgentStreamResult;
12
35
  };
13
36
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA4BA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AAItC,OAAO,EAEL,KAAK,mBAAmB,EACxB,KAAK,oBAAoB,EAC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