@mcp-abap-adt/llm-agent 22.1.0 → 23.0.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/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/interfaces/index.d.ts +1 -0
- package/dist/interfaces/index.d.ts.map +1 -1
- package/dist/interfaces/index.js +1 -0
- package/dist/interfaces/index.js.map +1 -1
- package/dist/interfaces/throttle-strategy.d.ts +72 -0
- package/dist/interfaces/throttle-strategy.d.ts.map +1 -0
- package/dist/interfaces/throttle-strategy.js +51 -0
- package/dist/interfaces/throttle-strategy.js.map +1 -0
- package/dist/llm/base-llm-provider.d.ts +69 -0
- package/dist/llm/base-llm-provider.d.ts.map +1 -1
- package/dist/llm/base-llm-provider.js +160 -0
- package/dist/llm/base-llm-provider.js.map +1 -1
- package/dist/llm/rate-limit.d.ts +165 -0
- package/dist/llm/rate-limit.d.ts.map +1 -0
- package/dist/llm/rate-limit.js +394 -0
- package/dist/llm/rate-limit.js.map +1 -0
- package/dist/llm/throttle.d.ts +160 -0
- package/dist/llm/throttle.d.ts.map +1 -0
- package/dist/llm/throttle.js +385 -0
- package/dist/llm/throttle.js.map +1 -0
- package/dist/types.d.ts +11 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared rate-limit handling for every LLM provider.
|
|
3
|
+
*
|
|
4
|
+
* None of the providers handled HTTP 429 (issue #282). Each one let the status
|
|
5
|
+
* collapse into an error message, so consumers were reduced to matching the
|
|
6
|
+
* substring "429" in prose to decide whether to retry.
|
|
7
|
+
*
|
|
8
|
+
* The policy lives here rather than in a provider because the providers differ
|
|
9
|
+
* in SDK but not in the shape of the problem: a shared quota, a documented
|
|
10
|
+
* `Retry-After`, and a herd of concurrent callers that will all rediscover the
|
|
11
|
+
* same limit unless something holds them back together.
|
|
12
|
+
*
|
|
13
|
+
* SAP AI Core states the rules this implements
|
|
14
|
+
* (help.sap.com, Rate Limit Management):
|
|
15
|
+
*
|
|
16
|
+
* Do not retry immediately after a 429. Use exponential backoff with jitter
|
|
17
|
+
* to prevent thundering-herd behavior. If the response includes a
|
|
18
|
+
* `Retry-After` header, use its value as the wait interval. Limit retries by
|
|
19
|
+
* using a maximum cap (for example, 5 retries or 60 seconds total wait).
|
|
20
|
+
*
|
|
21
|
+
* The same rules fit Anthropic and OpenAI, which return 429 with `Retry-After`
|
|
22
|
+
* as ordinary HTTP. Ollama is local and never rate-limited; it simply never
|
|
23
|
+
* enters this path.
|
|
24
|
+
*/
|
|
25
|
+
import { type ThrottlePolicy } from '../interfaces/throttle-strategy.js';
|
|
26
|
+
/** An error annotated by this module, so consumers read a fact instead of prose. */
|
|
27
|
+
export interface ThrottledError extends Error {
|
|
28
|
+
throttled: true;
|
|
29
|
+
/** What the server asked for, when it asked. */
|
|
30
|
+
retryAfterSeconds?: number;
|
|
31
|
+
/** How many attempts were spent before giving up. */
|
|
32
|
+
attempts: number;
|
|
33
|
+
/**
|
|
34
|
+
* Which cap ended it, when it ended here. `attempts` and `budget` are fixed
|
|
35
|
+
* by opposite settings, so a consumer that cannot tell them apart cannot act
|
|
36
|
+
* on either. `gate` means the pause was already longer than the budget and no
|
|
37
|
+
* request was sent at all.
|
|
38
|
+
*/
|
|
39
|
+
reason?: 'attempts' | 'budget' | 'gate';
|
|
40
|
+
}
|
|
41
|
+
export declare function isThrottledError(e: unknown): e is ThrottledError;
|
|
42
|
+
/**
|
|
43
|
+
* One shared pause per quota.
|
|
44
|
+
*
|
|
45
|
+
* A rate limit belongs to the quota, not to the request that happened to
|
|
46
|
+
* discover it. When one call is told to wait, every other call against the same
|
|
47
|
+
* quota is already over the limit too — letting each find that out for itself
|
|
48
|
+
* means N more rejected requests and N more penalties, which is how a limit
|
|
49
|
+
* that should last one window lasts several.
|
|
50
|
+
*
|
|
51
|
+
* Keyed because limits are per model: one model being throttled says nothing
|
|
52
|
+
* about another.
|
|
53
|
+
*/
|
|
54
|
+
export declare class QuotaGate {
|
|
55
|
+
private notBefore;
|
|
56
|
+
/** Hold every caller until the named time. Never shortens an existing hold. */
|
|
57
|
+
penalise(ms: number, now?: number): void;
|
|
58
|
+
/** Milliseconds still to wait, or 0. */
|
|
59
|
+
remaining(now?: number): number;
|
|
60
|
+
clear(): void;
|
|
61
|
+
/**
|
|
62
|
+
* Wait out any active hold.
|
|
63
|
+
*
|
|
64
|
+
* Re-checked after every wake, because the hold is not fixed at the moment
|
|
65
|
+
* this caller started waiting: another caller's 429 extends it, and a sleeper
|
|
66
|
+
* timed against the old deadline would wake early and spend a request on a
|
|
67
|
+
* quota the server has since said is still closed.
|
|
68
|
+
*
|
|
69
|
+
* The wait is jittered per caller: without that, everyone released by one
|
|
70
|
+
* penalty wakes in the same millisecond and rebuilds the herd the penalty was
|
|
71
|
+
* meant to break up.
|
|
72
|
+
*/
|
|
73
|
+
waitUntilOpen(signal?: AbortSignal, maxWaitMs?: number): Promise<void>;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Keys are open-ended — a per-request model override mints a new one — so the
|
|
77
|
+
* registry is pruned rather than left to grow for the life of the process.
|
|
78
|
+
* Only entries that are both open and long idle are dropped: a held gate is
|
|
79
|
+
* doing its job, and a recently used one may still be someone's in-flight call.
|
|
80
|
+
*/
|
|
81
|
+
export declare const GATE_IDLE_TTL_MS: number;
|
|
82
|
+
/**
|
|
83
|
+
* How many quotas the registry keeps. Enforced by evicting gates that are both
|
|
84
|
+
* open and unheld, so a process with more than this many pauses or in-flight
|
|
85
|
+
* calls at once can exceed it — those gates are load-bearing. Evicting an idle
|
|
86
|
+
* one costs nothing: it carries no deadline, and the next call mints an
|
|
87
|
+
* equivalent.
|
|
88
|
+
*/
|
|
89
|
+
export declare const GATE_LIMIT = 500;
|
|
90
|
+
/** The gate for one quota. Shared across every provider instance in the process. */
|
|
91
|
+
export declare function gateFor(key: string): QuotaGate;
|
|
92
|
+
/**
|
|
93
|
+
* Hold a gate for the length of one call, and keep it in the registry meanwhile.
|
|
94
|
+
*
|
|
95
|
+
* Being open is not the same as being unused. A request is in flight for as
|
|
96
|
+
* long as it takes the server to answer, and until that answer arrives the gate
|
|
97
|
+
* it belongs to has no pause on it. Evicted in that window, the call would go on
|
|
98
|
+
* to penalise an object the registry no longer knows: the pause would be real
|
|
99
|
+
* and invisible, and the next caller would be handed a fresh open gate and sent
|
|
100
|
+
* straight into a quota that had just closed. Worse than no gate at all, since
|
|
101
|
+
* the penalty is paid and nothing is bought with it.
|
|
102
|
+
*
|
|
103
|
+
* Release in a `finally`. Releasing twice is harmless.
|
|
104
|
+
*/
|
|
105
|
+
export declare function leaseQuotaGate(key: string): {
|
|
106
|
+
gate: QuotaGate;
|
|
107
|
+
release: () => void;
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Reclaim gates that are open and long idle. Exported so a long-lived host can
|
|
111
|
+
* run it on a timer, and so the reclaim can be tested at a chosen `now` without
|
|
112
|
+
* waiting ten minutes. It frees only what is certainly stale; the size bound is
|
|
113
|
+
* enforced separately, by `reclaimGates`.
|
|
114
|
+
*/
|
|
115
|
+
export declare function pruneQuotaGates(now?: number): void;
|
|
116
|
+
/** How many quotas are currently tracked. Diagnostics and tests. */
|
|
117
|
+
export declare function quotaGateCount(): number;
|
|
118
|
+
/** Test seam — drops every gate. */
|
|
119
|
+
export declare function resetQuotaGates(): void;
|
|
120
|
+
export interface ThrottleRetryOptions {
|
|
121
|
+
/** Identifies the quota being spent. Limits are per model, so include it. */
|
|
122
|
+
key: string;
|
|
123
|
+
policy?: Partial<ThrottlePolicy>;
|
|
124
|
+
/** Does this error mean "too many requests"? */
|
|
125
|
+
isThrottled: (error: unknown) => boolean;
|
|
126
|
+
/** What the server asked us to wait, in seconds, if it said. */
|
|
127
|
+
retryAfterSeconds?: (error: unknown) => number | undefined;
|
|
128
|
+
signal?: AbortSignal;
|
|
129
|
+
onRetry?: (info: {
|
|
130
|
+
attempt: number;
|
|
131
|
+
delayMs: number;
|
|
132
|
+
retryAfterSeconds?: number;
|
|
133
|
+
}) => void;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Run `fn`, respecting the shared pause and retrying a rate limit.
|
|
137
|
+
*
|
|
138
|
+
* Any error that is not a rate limit is rethrown untouched and immediately —
|
|
139
|
+
* this is not a general-purpose retry, and turning a genuine failure into four
|
|
140
|
+
* more of them would be worse than the failure.
|
|
141
|
+
*/
|
|
142
|
+
export declare function runWithThrottleRetry<T>(fn: () => Promise<T>, opts: ThrottleRetryOptions): Promise<T>;
|
|
143
|
+
/**
|
|
144
|
+
* The rate-limit marker on an error or anywhere in its `cause` chain.
|
|
145
|
+
*
|
|
146
|
+
* Layers above the provider rewrap errors — the ILlm adapter turns anything
|
|
147
|
+
* thrown into an `LlmError` — so the marker is rarely still on the outermost
|
|
148
|
+
* object by the time a retry decorator inspects it. Bounded by depth and a
|
|
149
|
+
* visited set, like `extractStatusCode`, so a cyclic chain cannot hang.
|
|
150
|
+
*/
|
|
151
|
+
export declare function findThrottled(err: unknown): ThrottledError | undefined;
|
|
152
|
+
/**
|
|
153
|
+
* Carry the rate-limit facts onto a provider's own error.
|
|
154
|
+
*
|
|
155
|
+
* Every provider catches its transport error and rethrows something friendlier.
|
|
156
|
+
* That is fine for a message and fatal for a fact: the 429 would be lost at the
|
|
157
|
+
* exact boundary where the consumer needs it. This moves the facts across.
|
|
158
|
+
*/
|
|
159
|
+
export declare function preserveThrottled<E extends Error>(original: unknown, wrapped: E): E;
|
|
160
|
+
//# sourceMappingURL=throttle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle.d.ts","sourceRoot":"","sources":["../../src/llm/throttle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAGL,KAAK,cAAc,EACpB,MAAM,o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BAAgB,eAAe,IAAI,IAAI,CAEtC;AAED,MAAM,WAAW,oBAAoB;IACnC,6EAA6E;IAC7E,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,CAAC;IACjC,gDAAgD;IAChD,WAAW,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC;IACzC,gEAAgE;IAChE,iBAAiB,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,GAAG,SAAS,CAAC;IAC3D,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE;QACf,OAAO,EAAE,MAAM,CAAC;QAChB,OAAO,EAAE,MAAM,CAAC;QAChB,iBAAiB,CAAC,EAAE,MAAM,CAAC;KAC5B,KAAK,IAAI,CAAC;CACZ;AAED;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CAAC,CAAC,EAC1C,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,IAAI,EAAE,oBAAoB,GACzB,OAAO,CAAC,CAAC,CAAC,CAQZ;AA2ED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,OAAO,GAAG,cAAc,GAAG,SAAS,CAWtE;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,KAAK,EAC/C,QAAQ,EAAE,OAAO,EACjB,OAAO,EAAE,CAAC,GACT,CAAC,CAaH"}
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared rate-limit handling for every LLM provider.
|
|
3
|
+
*
|
|
4
|
+
* None of the providers handled HTTP 429 (issue #282). Each one let the status
|
|
5
|
+
* collapse into an error message, so consumers were reduced to matching the
|
|
6
|
+
* substring "429" in prose to decide whether to retry.
|
|
7
|
+
*
|
|
8
|
+
* The policy lives here rather than in a provider because the providers differ
|
|
9
|
+
* in SDK but not in the shape of the problem: a shared quota, a documented
|
|
10
|
+
* `Retry-After`, and a herd of concurrent callers that will all rediscover the
|
|
11
|
+
* same limit unless something holds them back together.
|
|
12
|
+
*
|
|
13
|
+
* SAP AI Core states the rules this implements
|
|
14
|
+
* (help.sap.com, Rate Limit Management):
|
|
15
|
+
*
|
|
16
|
+
* Do not retry immediately after a 429. Use exponential backoff with jitter
|
|
17
|
+
* to prevent thundering-herd behavior. If the response includes a
|
|
18
|
+
* `Retry-After` header, use its value as the wait interval. Limit retries by
|
|
19
|
+
* using a maximum cap (for example, 5 retries or 60 seconds total wait).
|
|
20
|
+
*
|
|
21
|
+
* The same rules fit Anthropic and OpenAI, which return 429 with `Retry-After`
|
|
22
|
+
* as ordinary HTTP. Ollama is local and never rate-limited; it simply never
|
|
23
|
+
* enters this path.
|
|
24
|
+
*/
|
|
25
|
+
import { DEFAULT_THROTTLE_POLICY, DefaultThrottleStrategy, } from '../interfaces/throttle-strategy.js';
|
|
26
|
+
import { DefaultWaitStrategy } from '../interfaces/wait-strategy.js';
|
|
27
|
+
const defaultStrategy = new DefaultThrottleStrategy();
|
|
28
|
+
export function isThrottledError(e) {
|
|
29
|
+
return (typeof e === 'object' &&
|
|
30
|
+
e !== null &&
|
|
31
|
+
e.throttled === true);
|
|
32
|
+
}
|
|
33
|
+
const waiter = new DefaultWaitStrategy();
|
|
34
|
+
/**
|
|
35
|
+
* Sleep, or throw if the signal aborts.
|
|
36
|
+
*
|
|
37
|
+
* Delegated to the shared wait strategy rather than hand-rolled: the listener
|
|
38
|
+
* bookkeeping is where a sleep like this goes wrong. One added per backoff and
|
|
39
|
+
* never removed accumulates on a request- or session-scoped signal until Node
|
|
40
|
+
* warns about the leak — a defect this repository has already fixed once, in
|
|
41
|
+
* the retry decorators. Fixed in one place, it stays fixed for every waiter.
|
|
42
|
+
*/
|
|
43
|
+
const sleep = async (ms, signal) => {
|
|
44
|
+
const aborted = () => signal?.reason ?? new Error('Aborted');
|
|
45
|
+
if (signal?.aborted)
|
|
46
|
+
throw aborted();
|
|
47
|
+
if ((await waiter.wait(ms, signal)) === 'aborted')
|
|
48
|
+
throw aborted();
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Per-caller spread on waking, so everyone released by one penalty does not
|
|
52
|
+
* wake in the same millisecond and rebuild the herd the penalty broke up.
|
|
53
|
+
*
|
|
54
|
+
* It is added to a wait, never to a budget: a wait capped at exactly the budget
|
|
55
|
+
* simply loses the spread, which costs nothing — a caller whose budget ends
|
|
56
|
+
* with the pause is leaving either way.
|
|
57
|
+
*/
|
|
58
|
+
const WAKE_SPREAD_MS = 250;
|
|
59
|
+
/**
|
|
60
|
+
* One shared pause per quota.
|
|
61
|
+
*
|
|
62
|
+
* A rate limit belongs to the quota, not to the request that happened to
|
|
63
|
+
* discover it. When one call is told to wait, every other call against the same
|
|
64
|
+
* quota is already over the limit too — letting each find that out for itself
|
|
65
|
+
* means N more rejected requests and N more penalties, which is how a limit
|
|
66
|
+
* that should last one window lasts several.
|
|
67
|
+
*
|
|
68
|
+
* Keyed because limits are per model: one model being throttled says nothing
|
|
69
|
+
* about another.
|
|
70
|
+
*/
|
|
71
|
+
export class QuotaGate {
|
|
72
|
+
notBefore = 0;
|
|
73
|
+
/** Hold every caller until the named time. Never shortens an existing hold. */
|
|
74
|
+
penalise(ms, now = Date.now()) {
|
|
75
|
+
this.notBefore = Math.max(this.notBefore, now + ms);
|
|
76
|
+
}
|
|
77
|
+
/** Milliseconds still to wait, or 0. */
|
|
78
|
+
remaining(now = Date.now()) {
|
|
79
|
+
return Math.max(0, this.notBefore - now);
|
|
80
|
+
}
|
|
81
|
+
clear() {
|
|
82
|
+
this.notBefore = 0;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Wait out any active hold.
|
|
86
|
+
*
|
|
87
|
+
* Re-checked after every wake, because the hold is not fixed at the moment
|
|
88
|
+
* this caller started waiting: another caller's 429 extends it, and a sleeper
|
|
89
|
+
* timed against the old deadline would wake early and spend a request on a
|
|
90
|
+
* quota the server has since said is still closed.
|
|
91
|
+
*
|
|
92
|
+
* The wait is jittered per caller: without that, everyone released by one
|
|
93
|
+
* penalty wakes in the same millisecond and rebuilds the herd the penalty was
|
|
94
|
+
* meant to break up.
|
|
95
|
+
*/
|
|
96
|
+
async waitUntilOpen(signal, maxWaitMs = Number.POSITIVE_INFINITY) {
|
|
97
|
+
const deadline = Date.now() + maxWaitMs;
|
|
98
|
+
for (;;) {
|
|
99
|
+
const left = this.remaining();
|
|
100
|
+
if (left <= 0)
|
|
101
|
+
return;
|
|
102
|
+
const budget = deadline - Date.now();
|
|
103
|
+
// Out of the caller's budget with the gate still shut. It is told, not
|
|
104
|
+
// held: `remaining()` still reports the hold, and the caller decides.
|
|
105
|
+
if (budget <= 0)
|
|
106
|
+
return;
|
|
107
|
+
// A timer that lands a hair early is simply waited out again on the next
|
|
108
|
+
// turn, which is why the cap needs no padding to be safe.
|
|
109
|
+
await sleep(Math.min(left + Math.random() * WAKE_SPREAD_MS, budget), signal);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const gates = new Map();
|
|
114
|
+
/**
|
|
115
|
+
* Keys are open-ended — a per-request model override mints a new one — so the
|
|
116
|
+
* registry is pruned rather than left to grow for the life of the process.
|
|
117
|
+
* Only entries that are both open and long idle are dropped: a held gate is
|
|
118
|
+
* doing its job, and a recently used one may still be someone's in-flight call.
|
|
119
|
+
*/
|
|
120
|
+
export const GATE_IDLE_TTL_MS = 10 * 60_000;
|
|
121
|
+
/**
|
|
122
|
+
* How many quotas the registry keeps. Enforced by evicting gates that are both
|
|
123
|
+
* open and unheld, so a process with more than this many pauses or in-flight
|
|
124
|
+
* calls at once can exceed it — those gates are load-bearing. Evicting an idle
|
|
125
|
+
* one costs nothing: it carries no deadline, and the next call mints an
|
|
126
|
+
* equivalent.
|
|
127
|
+
*/
|
|
128
|
+
export const GATE_LIMIT = 500;
|
|
129
|
+
/** The gate for one quota. Shared across every provider instance in the process. */
|
|
130
|
+
export function gateFor(key) {
|
|
131
|
+
const now = Date.now();
|
|
132
|
+
const existing = gates.get(key);
|
|
133
|
+
if (existing) {
|
|
134
|
+
existing.lastUsed = now;
|
|
135
|
+
return existing.gate;
|
|
136
|
+
}
|
|
137
|
+
if (gates.size >= GATE_LIMIT)
|
|
138
|
+
reclaimGates(now);
|
|
139
|
+
const entry = {
|
|
140
|
+
gate: new QuotaGate(),
|
|
141
|
+
lastUsed: now,
|
|
142
|
+
leases: 0,
|
|
143
|
+
};
|
|
144
|
+
gates.set(key, entry);
|
|
145
|
+
return entry.gate;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Hold a gate for the length of one call, and keep it in the registry meanwhile.
|
|
149
|
+
*
|
|
150
|
+
* Being open is not the same as being unused. A request is in flight for as
|
|
151
|
+
* long as it takes the server to answer, and until that answer arrives the gate
|
|
152
|
+
* it belongs to has no pause on it. Evicted in that window, the call would go on
|
|
153
|
+
* to penalise an object the registry no longer knows: the pause would be real
|
|
154
|
+
* and invisible, and the next caller would be handed a fresh open gate and sent
|
|
155
|
+
* straight into a quota that had just closed. Worse than no gate at all, since
|
|
156
|
+
* the penalty is paid and nothing is bought with it.
|
|
157
|
+
*
|
|
158
|
+
* Release in a `finally`. Releasing twice is harmless.
|
|
159
|
+
*/
|
|
160
|
+
export function leaseQuotaGate(key) {
|
|
161
|
+
const gate = gateFor(key);
|
|
162
|
+
// By identity, not by key: `resetQuotaGates` may have replaced the entry
|
|
163
|
+
// by the time this is released, and that entry's count is not ours to touch.
|
|
164
|
+
const entry = gates.get(key);
|
|
165
|
+
if (entry)
|
|
166
|
+
entry.leases += 1;
|
|
167
|
+
let released = false;
|
|
168
|
+
return {
|
|
169
|
+
gate,
|
|
170
|
+
release: () => {
|
|
171
|
+
if (released || !entry)
|
|
172
|
+
return;
|
|
173
|
+
released = true;
|
|
174
|
+
entry.leases -= 1;
|
|
175
|
+
entry.lastUsed = Date.now();
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Reclaim gates that are open and long idle. Exported so a long-lived host can
|
|
181
|
+
* run it on a timer, and so the reclaim can be tested at a chosen `now` without
|
|
182
|
+
* waiting ten minutes. It frees only what is certainly stale; the size bound is
|
|
183
|
+
* enforced separately, by `reclaimGates`.
|
|
184
|
+
*/
|
|
185
|
+
export function pruneQuotaGates(now = Date.now()) {
|
|
186
|
+
for (const [key, entry] of gates) {
|
|
187
|
+
if (isEvictable(entry, now) && now - entry.lastUsed > GATE_IDLE_TTL_MS) {
|
|
188
|
+
gates.delete(key);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Safe to forget: nobody is waiting behind it, and no call is holding it.
|
|
194
|
+
*
|
|
195
|
+
* An open, unleased gate carries no state at all — the next call mints an
|
|
196
|
+
* equivalent one and loses nothing.
|
|
197
|
+
*/
|
|
198
|
+
function isEvictable(entry, now) {
|
|
199
|
+
return entry.leases === 0 && entry.gate.remaining(now) === 0;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Hold the registry at `GATE_LIMIT`.
|
|
203
|
+
*
|
|
204
|
+
* Age alone is not a bound: a burst of per-request model overrides mints keys
|
|
205
|
+
* far faster than the ten-minute idle rule retires them, and the map grows for
|
|
206
|
+
* as long as the burst lasts. So once the stale entries are gone, the least
|
|
207
|
+
* recently used OPEN gates go too, oldest first, until the registry is back
|
|
208
|
+
* under the limit.
|
|
209
|
+
*
|
|
210
|
+
* Only idle gates are evicted: not one holding a pause, which IS the pause, and
|
|
211
|
+
* not one a call is still holding, which may be about to become a pause.
|
|
212
|
+
*/
|
|
213
|
+
function reclaimGates(now) {
|
|
214
|
+
pruneQuotaGates(now);
|
|
215
|
+
if (gates.size < GATE_LIMIT)
|
|
216
|
+
return;
|
|
217
|
+
const evictable = [...gates]
|
|
218
|
+
.filter(([, entry]) => isEvictable(entry, now))
|
|
219
|
+
.sort((a, b) => a[1].lastUsed - b[1].lastUsed);
|
|
220
|
+
for (const [key] of evictable) {
|
|
221
|
+
if (gates.size < GATE_LIMIT)
|
|
222
|
+
return;
|
|
223
|
+
gates.delete(key);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
/** How many quotas are currently tracked. Diagnostics and tests. */
|
|
227
|
+
export function quotaGateCount() {
|
|
228
|
+
return gates.size;
|
|
229
|
+
}
|
|
230
|
+
/** Test seam — drops every gate. */
|
|
231
|
+
export function resetQuotaGates() {
|
|
232
|
+
gates.clear();
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Run `fn`, respecting the shared pause and retrying a rate limit.
|
|
236
|
+
*
|
|
237
|
+
* Any error that is not a rate limit is rethrown untouched and immediately —
|
|
238
|
+
* this is not a general-purpose retry, and turning a genuine failure into four
|
|
239
|
+
* more of them would be worse than the failure.
|
|
240
|
+
*/
|
|
241
|
+
export async function runWithThrottleRetry(fn, opts) {
|
|
242
|
+
const policy = { ...DEFAULT_THROTTLE_POLICY, ...opts.policy };
|
|
243
|
+
const { gate, release } = leaseQuotaGate(opts.key);
|
|
244
|
+
try {
|
|
245
|
+
return await attemptWithGate(fn, opts, policy, gate);
|
|
246
|
+
}
|
|
247
|
+
finally {
|
|
248
|
+
release();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
async function attemptWithGate(fn, opts, policy, gate) {
|
|
252
|
+
let attempt = 0;
|
|
253
|
+
let waited = 0;
|
|
254
|
+
for (;;) {
|
|
255
|
+
// Waiting at the gate is waiting. Uncounted it was free, so a caller with a
|
|
256
|
+
// ten-millisecond budget could sit out a minute-long pause another caller
|
|
257
|
+
// had earned, its declared budget saying nothing about the time it spent.
|
|
258
|
+
// Decided before the wait, never during it: a hold longer than what is left
|
|
259
|
+
// of the budget is refused outright rather than half-served.
|
|
260
|
+
const hold = gate.remaining();
|
|
261
|
+
if (hold > 0) {
|
|
262
|
+
const budgetLeft = policy.maxTotalWaitMs - waited;
|
|
263
|
+
if (hold > budgetLeft)
|
|
264
|
+
throw outOfBudgetAtGate(hold, attempt);
|
|
265
|
+
const before = Date.now();
|
|
266
|
+
// Capped as well as pre-checked: a pause that fits when the wait starts
|
|
267
|
+
// can be extended past the budget by someone else's 429 while it runs.
|
|
268
|
+
// The cap is the budget exactly. Padding it to protect the wake spread
|
|
269
|
+
// would make the budget the one thing it must not be — approximate.
|
|
270
|
+
await gate.waitUntilOpen(opts.signal, budgetLeft);
|
|
271
|
+
waited += Date.now() - before;
|
|
272
|
+
const stillShut = gate.remaining();
|
|
273
|
+
if (stillShut > 0)
|
|
274
|
+
throw outOfBudgetAtGate(stillShut, attempt);
|
|
275
|
+
}
|
|
276
|
+
try {
|
|
277
|
+
return await fn();
|
|
278
|
+
}
|
|
279
|
+
catch (error) {
|
|
280
|
+
if (!opts.isThrottled(error))
|
|
281
|
+
throw error;
|
|
282
|
+
attempt += 1;
|
|
283
|
+
const retryAfter = opts.retryAfterSeconds?.(error);
|
|
284
|
+
const decision = (policy.strategy ?? defaultStrategy).decide({
|
|
285
|
+
attempt,
|
|
286
|
+
retryAfterSeconds: retryAfter,
|
|
287
|
+
waitedMs: waited,
|
|
288
|
+
policy,
|
|
289
|
+
});
|
|
290
|
+
// Hold everyone else too, and hold them whether or not THIS caller is
|
|
291
|
+
// going on. The pause describes the quota, not one request: giving up
|
|
292
|
+
// with the gate open sends every other caller straight back into a limit
|
|
293
|
+
// the server just said was closed. That is why the decision carries a
|
|
294
|
+
// wait in both cases, rather than being retry-or-nothing.
|
|
295
|
+
//
|
|
296
|
+
// The gate is given the interval alone. This caller then serves its own
|
|
297
|
+
// backoff there, on the next turn of the loop, rather than sleeping
|
|
298
|
+
// beside it: two mechanisms waiting out one pause double-count it, and
|
|
299
|
+
// the leftover milliseconds between them decide outcomes at the margin.
|
|
300
|
+
// The per-caller spread that keeps the released callers from waking
|
|
301
|
+
// together lives in `waitUntilOpen`, where the waiting is done.
|
|
302
|
+
gate.penalise(decision.waitMs);
|
|
303
|
+
if (!decision.retry) {
|
|
304
|
+
throw annotate(error, attempt, retryAfter, decision.reason);
|
|
305
|
+
}
|
|
306
|
+
opts.onRetry?.({
|
|
307
|
+
attempt,
|
|
308
|
+
delayMs: decision.waitMs,
|
|
309
|
+
retryAfterSeconds: retryAfter,
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
const MAX_CAUSE_DEPTH = 5;
|
|
315
|
+
/**
|
|
316
|
+
* The rate-limit marker on an error or anywhere in its `cause` chain.
|
|
317
|
+
*
|
|
318
|
+
* Layers above the provider rewrap errors — the ILlm adapter turns anything
|
|
319
|
+
* thrown into an `LlmError` — so the marker is rarely still on the outermost
|
|
320
|
+
* object by the time a retry decorator inspects it. Bounded by depth and a
|
|
321
|
+
* visited set, like `extractStatusCode`, so a cyclic chain cannot hang.
|
|
322
|
+
*/
|
|
323
|
+
export function findThrottled(err) {
|
|
324
|
+
const visited = new Set();
|
|
325
|
+
let cur = err;
|
|
326
|
+
for (let depth = 0; depth < MAX_CAUSE_DEPTH; depth++) {
|
|
327
|
+
if (typeof cur !== 'object' || cur === null || visited.has(cur))
|
|
328
|
+
return undefined;
|
|
329
|
+
visited.add(cur);
|
|
330
|
+
if (isThrottledError(cur))
|
|
331
|
+
return cur;
|
|
332
|
+
cur = cur.cause;
|
|
333
|
+
}
|
|
334
|
+
return undefined;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Carry the rate-limit facts onto a provider's own error.
|
|
338
|
+
*
|
|
339
|
+
* Every provider catches its transport error and rethrows something friendlier.
|
|
340
|
+
* That is fine for a message and fatal for a fact: the 429 would be lost at the
|
|
341
|
+
* exact boundary where the consumer needs it. This moves the facts across.
|
|
342
|
+
*/
|
|
343
|
+
export function preserveThrottled(original, wrapped) {
|
|
344
|
+
if (!isThrottledError(original))
|
|
345
|
+
return wrapped;
|
|
346
|
+
const w = wrapped;
|
|
347
|
+
w.throttled = true;
|
|
348
|
+
w.attempts = original.attempts;
|
|
349
|
+
if (original.retryAfterSeconds !== undefined) {
|
|
350
|
+
w.retryAfterSeconds = original.retryAfterSeconds;
|
|
351
|
+
}
|
|
352
|
+
// Every fact, not a chosen few. Every provider wraps its transport error, so
|
|
353
|
+
// a fact left behind here is a fact no consumer ever sees — which is how the
|
|
354
|
+
// reason for giving up was lost the moment it was added.
|
|
355
|
+
if (original.reason !== undefined)
|
|
356
|
+
w.reason = original.reason;
|
|
357
|
+
return w;
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* The shared pause outlasts what this caller said it would wait.
|
|
361
|
+
*
|
|
362
|
+
* There is no server error to annotate here — the request was never sent,
|
|
363
|
+
* because sending it into a quota known to be closed is the one thing the gate
|
|
364
|
+
* exists to prevent. It is still a rate limit, and says so, carrying what is
|
|
365
|
+
* left of the pause so the consumer can decide.
|
|
366
|
+
*/
|
|
367
|
+
function outOfBudgetAtGate(remainingMs, attempts) {
|
|
368
|
+
const e = new Error('rate limited: the pause on this quota outlasts the configured wait budget');
|
|
369
|
+
e.throttled = true;
|
|
370
|
+
e.attempts = attempts;
|
|
371
|
+
e.retryAfterSeconds = remainingMs / 1000;
|
|
372
|
+
e.reason = 'gate';
|
|
373
|
+
return e;
|
|
374
|
+
}
|
|
375
|
+
function annotate(error, attempts, retryAfterSeconds, reason) {
|
|
376
|
+
const e = (error instanceof Error ? error : new Error(String(error)));
|
|
377
|
+
e.throttled = true;
|
|
378
|
+
e.attempts = attempts;
|
|
379
|
+
if (retryAfterSeconds !== undefined)
|
|
380
|
+
e.retryAfterSeconds = retryAfterSeconds;
|
|
381
|
+
if (reason !== undefined)
|
|
382
|
+
e.reason = reason;
|
|
383
|
+
return e;
|
|
384
|
+
}
|
|
385
|
+
//# sourceMappingURL=throttle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle.js","sourceRoot":"","sources":["../../src/llm/throttle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;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,MAAiC;IAEjC,MAAM,CAAC,GAAG,CACR,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CACxC,CAAC;IACpB,CAAC,CAAC,SAAS,GAAG,IAAI,CAAC;IACnB,CAAC,CAAC,QAAQ,GAAG,QAAQ,CAAC;IACtB,IAAI,iBAAiB,KAAK,SAAS;QAAE,CAAC,CAAC,iBAAiB,GAAG,iBAAiB,CAAC;IAC7E,IAAI,MAAM,KAAK,SAAS;QAAE,CAAC,CAAC,MAAM,GAAG,MAAM,CAAC;IAC5C,OAAO,CAAC,CAAC;AACX,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Core types for LLM Proxy
|
|
3
3
|
*/
|
|
4
|
+
import type { ThrottlePolicy } from './interfaces/throttle-strategy.js';
|
|
4
5
|
import type { LlmUsage } from './interfaces/types.js';
|
|
5
6
|
export interface Message {
|
|
6
7
|
/**
|
|
@@ -77,6 +78,16 @@ export interface LLMProviderConfig {
|
|
|
77
78
|
model?: string;
|
|
78
79
|
temperature?: number;
|
|
79
80
|
maxTokens?: number;
|
|
81
|
+
/**
|
|
82
|
+
* What this provider does when the server throttles it. Omit for the shared
|
|
83
|
+
* default: back off with jitter, honour `Retry-After`, give up after 5
|
|
84
|
+
* attempts or 60 seconds of waiting.
|
|
85
|
+
*
|
|
86
|
+
* There is no switch for turning it off — sending another request into a
|
|
87
|
+
* quota the server has just closed is never the better answer. Different
|
|
88
|
+
* mechanics go through `strategy`.
|
|
89
|
+
*/
|
|
90
|
+
whenThrottled?: Partial<ThrottlePolicy>;
|
|
80
91
|
}
|
|
81
92
|
export interface LLMCallOptions {
|
|
82
93
|
/** Per-request model override. */
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","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