@mcp-abap-adt/llm-agent 22.2.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 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- 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 +7 -7
- package/dist/llm/base-llm-provider.d.ts.map +1 -1
- package/dist/llm/base-llm-provider.js +12 -12
- package/dist/llm/base-llm-provider.js.map +1 -1
- 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 +9 -6
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export * from './external-results.js';
|
|
|
5
5
|
export { assertClientDescriptors } from './interfaces/assert-client-descriptors.js';
|
|
6
6
|
export * from './interfaces/index.js';
|
|
7
7
|
export { BaseLLMProvider, type LLMProvider } from './llm/base-llm-provider.js';
|
|
8
|
-
export {
|
|
8
|
+
export { findThrottled, gateFor, isThrottledError, preserveThrottled, pruneQuotaGates, QuotaGate, quotaGateCount, resetQuotaGates, runWithThrottleRetry, type ThrottledError, type ThrottleRetryOptions, } from './llm/throttle.js';
|
|
9
9
|
export { NeedsDecompositionError } from './needs-decomposition-error.js';
|
|
10
10
|
export * from './rag/index.js';
|
|
11
11
|
export * from './types.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,mBAAmB,EACnB,iBAAiB,EACjB,kBAAkB,EAClB,SAAS,EACT,aAAa,GACd,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AACzE,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,OAAO,EAAE,uBAAuB,EAAE,MAAM,2CAA2C,CAAC;AACpF,cAAc,uBAAuB,CAAC;AACtC,OAAO,EAAE,eAAe,EAAE,KAAK,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAC/E,OAAO,EACL,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,mBAAmB,EACnB,iBAAiB,EACjB,kBAAkB,EAClB,SAAS,EACT,aAAa,GACd,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AACzE,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,OAAO,EAAE,uBAAuB,EAAE,MAAM,2CAA2C,CAAC;AACpF,cAAc,uBAAuB,CAAC;AACtC,OAAO,EAAE,eAAe,EAAE,KAAK,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAC/E,OAAO,EACL,aAAa,EACb,OAAO,EACP,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,EACf,SAAS,EACT,cAAc,EACd,eAAe,EACf,oBAAoB,EACpB,KAAK,cAAc,EACnB,KAAK,oBAAoB,GAC1B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AACzE,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAI3B,OAAO,EAAE,kBAAkB,EAAE,MAAM,oC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1B,kBAAkB,GACnB,MAAM,0CAA0C,CAAC;AAClD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAC;AACxE,OAAO,EACL,cAAc,EACd,KAAK,wBAAwB,EAC7B,wBAAwB,EACxB,KAAK,0BAA0B,EAC/B,qBAAqB,EACrB,eAAe,GAChB,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAC3D,OAAO,EACL,KAAK,oBAAoB,EACzB,iBAAiB,EACjB,iBAAiB,EACjB,kBAAkB,EAClB,aAAa,EACb,SAAS,GACV,MAAM,gCAAgC,CAAC;AACxC,OAAO,EACL,qBAAqB,EACrB,eAAe,GAChB,MAAM,uBAAuB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -6,7 +6,7 @@ export * from './external-results.js';
|
|
|
6
6
|
export { assertClientDescriptors } from './interfaces/assert-client-descriptors.js';
|
|
7
7
|
export * from './interfaces/index.js';
|
|
8
8
|
export { BaseLLMProvider } from './llm/base-llm-provider.js';
|
|
9
|
-
export {
|
|
9
|
+
export { findThrottled, gateFor, isThrottledError, preserveThrottled, pruneQuotaGates, QuotaGate, quotaGateCount, resetQuotaGates, runWithThrottleRetry, } from './llm/throttle.js';
|
|
10
10
|
export { NeedsDecompositionError } from './needs-decomposition-error.js';
|
|
11
11
|
export * from './rag/index.js';
|
|
12
12
|
export * from './types.js';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,2CAA2C;AAC3C,OAAO,EACL,mBAAmB,EACnB,iBAAiB,EACjB,kBAAkB,EAClB,SAAS,EACT,aAAa,GACd,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AACzE,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,OAAO,E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OAAO,EACL,sBAAsB,GAKvB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EACL,oBAAoB,GAErB,MAAM,wCAAwC,CAAC;AAGhD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAC;AACxE,OAAO,EACL,gBAAgB,EAChB,oBAAoB,GAGrB,MAAM,gCAAgC,CAAC;AAGxC,OAAO,EAAE,uBAAuB,EAAE,MAAM,wCAAwC,CAAC;AACjF,oBAAoB;AACpB,OAAO,EAAE,2BAA2B,EAAE,MAAM,6CAA6C,CAAC;AAC1F,OAAO,EAAE,wBAAwB,EAAE,MAAM,yCAAyC,CAAC;AACnF,OAAO,EACL,qBAAqB,EACrB,sBAAsB,GACvB,MAAM,yCAAyC,CAAC;AACjD,aAAa;AACb,OAAO,EACL,cAAc,GAGf,MAAM,iCAAiC,CAAC;AACzC,OAAO,EACL,sBAAsB,EACtB,0BAA0B,EAC1B,kBAAkB,GACnB,MAAM,0CAA0C,CAAC;AAClD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAC;AACxE,OAAO,EACL,cAAc,EAEd,wBAAwB,EAExB,qBAAqB,EACrB,eAAe,GAChB,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAC3D,OAAO,EAEL,iBAAiB,EACjB,iBAAiB,EACjB,kBAAkB,EAClB,aAAa,EACb,SAAS,GACV,MAAM,gCAAgC,CAAC;AACxC,OAAO,EACL,qBAAqB,EACrB,eAAe,GAChB,MAAM,uBAAuB,CAAC"}
|
|
@@ -56,6 +56,7 @@ export type { ISubAgent, ISubAgentInput, ISubAgentResult, SubAgentCapabilities,
|
|
|
56
56
|
export type { ISubAgentContextBuilder, SubAgentContextRequest, SubAgentContextResult, } from './subagent-context.js';
|
|
57
57
|
export type { ITaskFormalizer, ITaskSpec } from './task-spec.js';
|
|
58
58
|
export { renderTaskSpec } from './task-spec.js';
|
|
59
|
+
export { DEFAULT_THROTTLE_POLICY, DefaultThrottleStrategy, type IThrottleStrategy, type ThrottleContext, type ThrottleDecision, type ThrottlePolicy, } from './throttle-strategy.js';
|
|
59
60
|
export { type IToolCatalogReporter, isToolCatalogReporter, type ToolCatalogStatus, } from './tool-catalog.js';
|
|
60
61
|
export type { IToolLoopContextStrategy, JsonValue, SerializableStrategyState, ToolLoopContextBase, ToolLoopContextStrategyDeps, ToolLoopContextStrategyFactory, ToolRound, } from './tool-loop-context-strategy.js';
|
|
61
62
|
export { defaultToolRecordKey, type IToolRecordKey, type ToolKeyContext, toolNameFromRecord, } from './tool-record-key.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/interfaces/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,kBAAkB,EACvB,iBAAiB,EACjB,KAAK,kBAAkB,EACvB,KAAK,UAAU,GAChB,MAAM,sBAAsB,CAAC;AAC9B,YAAY,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACtE,YAAY,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AACnE,YAAY,EACV,WAAW,EACX,gBAAgB,EAChB,mBAAmB,GACpB,MAAM,cAAc,CAAC;AACtB,YAAY,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAC5D,YAAY,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACrD,YAAY,EACV,aAAa,EACb,mBAAmB,EACnB,kBAAkB,EAClB,mBAAmB,EACnB,iBAAiB,EACjB,iBAAiB,EACjB,IAAI,EACJ,QAAQ,EACR,UAAU,EACV,uBAAuB,GACxB,MAAM,kBAAkB,CAAC;AAC1B,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACvD,YAAY,EACV,YAAY,EACZ,aAAa,EACb,cAAc,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EAAE,MAAM,gBAAgB,CAAC;AAChD,YAAY,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC"}
|
package/dist/interfaces/index.js
CHANGED
|
@@ -5,6 +5,7 @@ export { isReadinessReporter, } from './readiness-reporter.js';
|
|
|
5
5
|
export { CatalogCasError, SkillsIncompatibleError } from './skills-rag.js';
|
|
6
6
|
export { TokenLedger } from './stepper.js';
|
|
7
7
|
export { renderTaskSpec } from './task-spec.js';
|
|
8
|
+
export { DEFAULT_THROTTLE_POLICY, DefaultThrottleStrategy, } from './throttle-strategy.js';
|
|
8
9
|
export { isToolCatalogReporter, } from './tool-catalog.js';
|
|
9
10
|
export { defaultToolRecordKey, toolNameFromRecord, } from './tool-record-key.js';
|
|
10
11
|
export { AssemblerError, ClassifierError, isMcpUnavailable, LlmError, MCP_UNAVAILABLE_CODES, McpError, RagError, SkillError, SmartAgentError, } from './types.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/interfaces/index.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,iBAAiB,GAGlB,MAAM,sBAAsB,CAAC;AAiD9B,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAiG9D,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAE/D,OAAO,EAEL,mBAAmB,GACpB,MAAM,yBAAyB,CAAC;AAkDjC,OAAO,EAAE,eAAe,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AA0B3E,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAiB3C,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EAEL,qBAAqB,GAEtB,MAAM,mBAAmB,CAAC;AAU3B,OAAO,EACL,oBAAoB,EAGpB,kBAAkB,GACnB,MAAM,sBAAsB,CAAC;AA+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when a server throttles it.
|
|
3
|
+
*
|
|
4
|
+
* The seam lives here, beside the other strategies, rather than next to the
|
|
5
|
+
* implementation that ships with it: a consumer programs against this package,
|
|
6
|
+
* and a contract kept in the implementation file is a contract nobody finds.
|
|
7
|
+
*
|
|
8
|
+
* The policy is the numbers; the strategy turns them into a decision. The
|
|
9
|
+
* shared pause per quota is NOT part of it — that is an invariant the runner
|
|
10
|
+
* keeps either way, because one caller declining to wait is its own business
|
|
11
|
+
* and one caller declining to mark the quota closed is everyone's.
|
|
12
|
+
*/
|
|
13
|
+
export interface ThrottlePolicy {
|
|
14
|
+
/** Total attempts INCLUDING the first. 1 waits out no throttling of its own. */
|
|
15
|
+
maxAttempts: number;
|
|
16
|
+
/** Give up once the accumulated waiting would exceed this. */
|
|
17
|
+
maxTotalWaitMs: number;
|
|
18
|
+
/** First backoff step when the server names no time of its own. */
|
|
19
|
+
baseDelayMs: number;
|
|
20
|
+
/** Ceiling for one computed step, before jitter. */
|
|
21
|
+
maxDelayMs: number;
|
|
22
|
+
/**
|
|
23
|
+
* How the numbers above turn into a decision. Omit for the default.
|
|
24
|
+
*
|
|
25
|
+
* There is no switch to turn throttle handling off, because there is no case
|
|
26
|
+
* where sending another request into a quota the server has just closed is
|
|
27
|
+
* the better answer — that is correctness, not preference. A consumer who
|
|
28
|
+
* wants different mechanics supplies them here, and the shared pause, which
|
|
29
|
+
* is the part other callers depend on, is kept either way.
|
|
30
|
+
*/
|
|
31
|
+
strategy?: IThrottleStrategy;
|
|
32
|
+
}
|
|
33
|
+
/** What the strategy is told about the 429 that just came back. */
|
|
34
|
+
export interface ThrottleContext {
|
|
35
|
+
/** 1-based: the attempt that was just refused. */
|
|
36
|
+
attempt: number;
|
|
37
|
+
/** What the server asked us to wait, in seconds, if it said. */
|
|
38
|
+
retryAfterSeconds?: number;
|
|
39
|
+
/** Waiting already spent on this call, the shared pause included. */
|
|
40
|
+
waitedMs: number;
|
|
41
|
+
/** The numbers in force, so a strategy need not close over them. */
|
|
42
|
+
policy: ThrottlePolicy;
|
|
43
|
+
}
|
|
44
|
+
export interface ThrottleDecision {
|
|
45
|
+
/**
|
|
46
|
+
* How long this quota is closed.
|
|
47
|
+
*
|
|
48
|
+
* Taken by the shared pause whether or not THIS caller retries: the quota is
|
|
49
|
+
* shut regardless of who has budget left, and a caller that gives up with the
|
|
50
|
+
* gate open sends everyone else straight back into the limit.
|
|
51
|
+
*/
|
|
52
|
+
waitMs: number;
|
|
53
|
+
/** Whether this caller waits it out and tries again. */
|
|
54
|
+
retry: boolean;
|
|
55
|
+
/** When not retrying, which cap ended it. */
|
|
56
|
+
reason?: 'attempts' | 'budget';
|
|
57
|
+
}
|
|
58
|
+
export interface IThrottleStrategy {
|
|
59
|
+
readonly name: string;
|
|
60
|
+
decide(ctx: ThrottleContext): ThrottleDecision;
|
|
61
|
+
}
|
|
62
|
+
export declare const DEFAULT_THROTTLE_POLICY: Omit<ThrottlePolicy, 'strategy'>;
|
|
63
|
+
/**
|
|
64
|
+
* What SAP AI Core documents: the server's own `Retry-After` when it sends one,
|
|
65
|
+
* otherwise exponential backoff with full jitter, capped by attempts and by
|
|
66
|
+
* total waiting.
|
|
67
|
+
*/
|
|
68
|
+
export declare class DefaultThrottleStrategy implements IThrottleStrategy {
|
|
69
|
+
readonly name = "default-throttle";
|
|
70
|
+
decide({ attempt, retryAfterSeconds, waitedMs, policy, }: ThrottleContext): ThrottleDecision;
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=throttle-strategy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle-strategy.d.ts","sourceRoot":"","sources":["../../src/interfaces/throttle-strategy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,MAAM,WAAW,cAAc;IAC7B,gFAAgF;IAChF,WAAW,EAAE,MAAM,CAAC;IACpB,8DAA8D;IAC9D,cAAc,EAAE,MAAM,CAAC;IACvB,mEAAmE;IACnE,WAAW,EAAE,MAAM,CAAC;IACpB,oDAAoD;IACpD,UAAU,EAAE,MAAM,CAAC;IACnB;;;;;;;;OAQG;IACH,QAAQ,CAAC,EAAE,iBAAiB,CAAC;CAC9B;AAED,mEAAmE;AACnE,MAAM,WAAW,eAAe;IAC9B,kDAAkD;IAClD,OAAO,EAAE,MAAM,CAAC;IAChB,gEAAgE;IAChE,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,qEAAqE;IACrE,QAAQ,EAAE,MAAM,CAAC;IACjB,oEAAoE;IACpE,MAAM,EAAE,cAAc,CAAC;CACxB;AAED,MAAM,WAAW,gBAAgB;IAC/B;;;;;;OAMG;IACH,MAAM,EAAE,MAAM,CAAC;IACf,wDAAwD;IACxD,KAAK,EAAE,OAAO,CAAC;IACf,6CAA6C;IAC7C,MAAM,CAAC,EAAE,UAAU,GAAG,QAAQ,CAAC;CAChC;AAED,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,MAAM,CAAC,GAAG,EAAE,eAAe,GAAG,gBAAgB,CAAC;CAChD;AAED,eAAO,MAAM,uBAAuB,EAAE,IAAI,CAAC,cAAc,EAAE,UAAU,CAKpE,CAAC;AAEF;;;;GAIG;AACH,qBAAa,uBAAwB,YAAW,iBAAiB;IAC/D,QAAQ,CAAC,IAAI,sBAAsB;IAEnC,MAAM,CAAC,EACL,OAAO,EACP,iBAAiB,EACjB,QAAQ,EACR,MAAM,GACP,EAAE,eAAe,GAAG,gBAAgB;CAkBtC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a provider does when a server throttles it.
|
|
3
|
+
*
|
|
4
|
+
* The seam lives here, beside the other strategies, rather than next to the
|
|
5
|
+
* implementation that ships with it: a consumer programs against this package,
|
|
6
|
+
* and a contract kept in the implementation file is a contract nobody finds.
|
|
7
|
+
*
|
|
8
|
+
* The policy is the numbers; the strategy turns them into a decision. The
|
|
9
|
+
* shared pause per quota is NOT part of it — that is an invariant the runner
|
|
10
|
+
* keeps either way, because one caller declining to wait is its own business
|
|
11
|
+
* and one caller declining to mark the quota closed is everyone's.
|
|
12
|
+
*/
|
|
13
|
+
export const DEFAULT_THROTTLE_POLICY = {
|
|
14
|
+
maxAttempts: 5,
|
|
15
|
+
maxTotalWaitMs: 60_000,
|
|
16
|
+
baseDelayMs: 1_000,
|
|
17
|
+
maxDelayMs: 20_000,
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* What SAP AI Core documents: the server's own `Retry-After` when it sends one,
|
|
21
|
+
* otherwise exponential backoff with full jitter, capped by attempts and by
|
|
22
|
+
* total waiting.
|
|
23
|
+
*/
|
|
24
|
+
export class DefaultThrottleStrategy {
|
|
25
|
+
name = 'default-throttle';
|
|
26
|
+
decide({ attempt, retryAfterSeconds, waitedMs, policy, }) {
|
|
27
|
+
const served = retryAfterSeconds !== undefined && Number.isFinite(retryAfterSeconds);
|
|
28
|
+
// The server's own number wins. A computed guess is for when it stays
|
|
29
|
+
// silent — it is an estimate of something the server already knows.
|
|
30
|
+
const waitMs = served
|
|
31
|
+
? retryAfterSeconds * 1000
|
|
32
|
+
: jittered(policy.baseDelayMs * 2 ** (attempt - 1), policy.maxDelayMs);
|
|
33
|
+
if (attempt >= policy.maxAttempts) {
|
|
34
|
+
return { waitMs, retry: false, reason: 'attempts' };
|
|
35
|
+
}
|
|
36
|
+
if (waitedMs + waitMs > policy.maxTotalWaitMs) {
|
|
37
|
+
return { waitMs, retry: false, reason: 'budget' };
|
|
38
|
+
}
|
|
39
|
+
return { waitMs, retry: true };
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Full jitter: a random point in [0, computed], not computed ± a nudge.
|
|
44
|
+
*
|
|
45
|
+
* Scaling a shared delay by a factor near 1 keeps the herd together; drawing
|
|
46
|
+
* from the whole interval is what actually spreads it.
|
|
47
|
+
*/
|
|
48
|
+
function jittered(computed, maxDelayMs) {
|
|
49
|
+
return Math.random() * Math.min(computed, maxDelayMs);
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=throttle-strategy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle-strategy.js","sourceRoot":"","sources":["../../src/interfaces/throttle-strategy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAuDH,MAAM,CAAC,MAAM,uBAAuB,GAAqC;IACvE,WAAW,EAAE,CAAC;IACd,cAAc,EAAE,MAAM;IACtB,WAAW,EAAE,KAAK;IAClB,UAAU,EAAE,MAAM;CACnB,CAAC;AAEF;;;;GAIG;AACH,MAAM,OAAO,uBAAuB;IACzB,IAAI,GAAG,kBAAkB,CAAC;IAEnC,MAAM,CAAC,EACL,OAAO,EACP,iBAAiB,EACjB,QAAQ,EACR,MAAM,GACU;QAChB,MAAM,MAAM,GACV,iBAAiB,KAAK,SAAS,IAAI,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAAC,CAAC;QAExE,sEAAsE;QACtE,oEAAoE;QACpE,MAAM,MAAM,GAAG,MAAM;YACnB,CAAC,CAAE,iBAA4B,GAAG,IAAI;YACtC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,WAAW,GAAG,CAAC,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC;QAEzE,IAAI,OAAO,IAAI,MAAM,CAAC,WAAW,EAAE,CAAC;YAClC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC;QACtD,CAAC;QACD,IAAI,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC,cAAc,EAAE,CAAC;YAC9C,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC;QACpD,CAAC;QACD,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;IACjC,CAAC;CACF;AAED;;;;;GAKG;AACH,SAAS,QAAQ,CAAC,QAAgB,EAAE,UAAkB;IACpD,OAAO,IAAI,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;AACxD,CAAC"}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Base interface for LLM providers
|
|
3
3
|
*/
|
|
4
4
|
import type { IModelInfo, LLMCallOptions, LLMProviderConfig, LLMResponse, Message } from '@mcp-abap-adt/llm-agent';
|
|
5
|
-
import { type
|
|
5
|
+
import { type ThrottleRetryOptions } from './throttle.js';
|
|
6
6
|
export interface LLMProvider {
|
|
7
7
|
/**
|
|
8
8
|
* Send a chat message and get response
|
|
@@ -43,7 +43,7 @@ export declare abstract class BaseLLMProvider<C extends LLMProviderConfig = LLMP
|
|
|
43
43
|
* the account's secret. Override to add a provider's own account fields
|
|
44
44
|
* (organization, project, resource group) — never the raw secret.
|
|
45
45
|
*/
|
|
46
|
-
protected
|
|
46
|
+
protected quotaScope(): string;
|
|
47
47
|
/**
|
|
48
48
|
* Reduce spellings of one endpoint to one key.
|
|
49
49
|
*
|
|
@@ -74,9 +74,9 @@ export declare abstract class BaseLLMProvider<C extends LLMProviderConfig = LLMP
|
|
|
74
74
|
* model's 429 would pause another's, and two overrides would share a gate
|
|
75
75
|
* neither of them belongs to.
|
|
76
76
|
*/
|
|
77
|
-
protected
|
|
77
|
+
protected quotaKey(model?: string): string;
|
|
78
78
|
/** Is this "too many requests" rather than a real failure? */
|
|
79
|
-
protected
|
|
79
|
+
protected isThrottled(error: unknown): boolean;
|
|
80
80
|
/**
|
|
81
81
|
* How long the server asked us to wait.
|
|
82
82
|
*
|
|
@@ -90,13 +90,13 @@ export declare abstract class BaseLLMProvider<C extends LLMProviderConfig = LLMP
|
|
|
90
90
|
* Providers rethrow their transport error as a readable one. Call this on the
|
|
91
91
|
* way out so the consumer still reads a fact, not a substring.
|
|
92
92
|
*/
|
|
93
|
-
protected
|
|
93
|
+
protected preserveThrottled<E extends Error>(original: unknown, wrapped: E): E;
|
|
94
94
|
/** Wrap one provider call in the shared policy. */
|
|
95
|
-
protected
|
|
95
|
+
protected withThrottleRetry<T>(fn: () => Promise<T>, extra?: {
|
|
96
96
|
/** The model this call actually uses, when it overrides the configured one. */
|
|
97
97
|
model?: string;
|
|
98
98
|
signal?: AbortSignal;
|
|
99
|
-
onRetry?:
|
|
99
|
+
onRetry?: ThrottleRetryOptions['onRetry'];
|
|
100
100
|
}): Promise<T>;
|
|
101
101
|
}
|
|
102
102
|
//# sourceMappingURL=base-llm-provider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base-llm-provider.d.ts","sourceRoot":"","sources":["../../src/llm/base-llm-provider.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,KAAK,EACV,UAAU,EACV,cAAc,EACd,iBAAiB,EACjB,WAAW,EACX,OAAO,EACR,MAAM,yBAAyB,CAAC;AACjC,OAAO,
|
|
1
|
+
{"version":3,"file":"base-llm-provider.d.ts","sourceRoot":"","sources":["../../src/llm/base-llm-provider.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,KAAK,EACV,UAAU,EACV,cAAc,EACd,iBAAiB,EACjB,WAAW,EACX,OAAO,EACR,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAGL,KAAK,oBAAoB,EAC1B,MAAM,eAAe,CAAC;AAEvB,MAAM,WAAW,WAAW;IAC1B;;OAEG;IACH,IAAI,CACF,QAAQ,EAAE,OAAO,EAAE,EACnB,KAAK,CAAC,EAAE,OAAO,EAAE,EACjB,OAAO,CAAC,EAAE,cAAc,GACvB,OAAO,CAAC,WAAW,CAAC,CAAC;IAExB;;OAEG;IACH,UAAU,CACR,QAAQ,EAAE,OAAO,EAAE,EACnB,KAAK,CAAC,EAAE,OAAO,EAAE,EACjB,OAAO,CAAC,EAAE,cAAc,GACvB,aAAa,CAAC,WAAW,CAAC,CAAC;IAE9B;;OAEG;IAC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;;;OAKG;IACH,SAAS,CAAC,iBAAiB,CAAC,CAAC,SAAS,KAAK,EACzC,QAAQ,EAAE,OAAO,EACjB,OAAO,EAAE,CAAC,GACT,CAAC;IAIJ,mDAAmD;IACnD,SAAS,CAAC,iBAAiB,CAAC,CAAC,EAC3B,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,KAAK,CAAC,EAAE;QACN,+EAA+E;QAC/E,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,MAAM,CAAC,EAAE,WAAW,CAAC;QACrB,OAAO,CAAC,EAAE,oBAAoB,CAAC,SAAS,CAAC,CAAC;KAC3C,GACA,OAAO,CAAC,CAAC,CAAC;CAUd"}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Base interface for LLM providers
|
|
3
3
|
*/
|
|
4
4
|
import { createHash } from 'node:crypto';
|
|
5
|
-
import {
|
|
5
|
+
import { preserveThrottled, runWithThrottleRetry, } from './throttle.js';
|
|
6
6
|
export class BaseLLMProvider {
|
|
7
7
|
config;
|
|
8
8
|
constructor(config) {
|
|
@@ -36,7 +36,7 @@ export class BaseLLMProvider {
|
|
|
36
36
|
* the account's secret. Override to add a provider's own account fields
|
|
37
37
|
* (organization, project, resource group) — never the raw secret.
|
|
38
38
|
*/
|
|
39
|
-
|
|
39
|
+
quotaScope() {
|
|
40
40
|
return [
|
|
41
41
|
this.canonicalEndpoint(this.quotaEndpoint()),
|
|
42
42
|
this.credentialFingerprint(this.config.apiKey),
|
|
@@ -89,11 +89,11 @@ export class BaseLLMProvider {
|
|
|
89
89
|
* model's 429 would pause another's, and two overrides would share a gate
|
|
90
90
|
* neither of them belongs to.
|
|
91
91
|
*/
|
|
92
|
-
|
|
93
|
-
return `${this.constructor.name}:${this.
|
|
92
|
+
quotaKey(model) {
|
|
93
|
+
return `${this.constructor.name}:${this.quotaScope()}:${model ?? this.config.model ?? 'default'}`;
|
|
94
94
|
}
|
|
95
95
|
/** Is this "too many requests" rather than a real failure? */
|
|
96
|
-
|
|
96
|
+
isThrottled(error) {
|
|
97
97
|
return httpStatusOf(error) === 429;
|
|
98
98
|
}
|
|
99
99
|
/**
|
|
@@ -120,15 +120,15 @@ export class BaseLLMProvider {
|
|
|
120
120
|
* Providers rethrow their transport error as a readable one. Call this on the
|
|
121
121
|
* way out so the consumer still reads a fact, not a substring.
|
|
122
122
|
*/
|
|
123
|
-
|
|
124
|
-
return
|
|
123
|
+
preserveThrottled(original, wrapped) {
|
|
124
|
+
return preserveThrottled(original, wrapped);
|
|
125
125
|
}
|
|
126
126
|
/** Wrap one provider call in the shared policy. */
|
|
127
|
-
|
|
128
|
-
return
|
|
129
|
-
key: this.
|
|
130
|
-
policy: this.config.
|
|
131
|
-
|
|
127
|
+
withThrottleRetry(fn, extra) {
|
|
128
|
+
return runWithThrottleRetry(fn, {
|
|
129
|
+
key: this.quotaKey(extra?.model),
|
|
130
|
+
policy: this.config.whenThrottled,
|
|
131
|
+
isThrottled: (e) => this.isThrottled(e),
|
|
132
132
|
retryAfterSeconds: (e) => this.retryAfterSeconds(e),
|
|
133
133
|
signal: extra?.signal,
|
|
134
134
|
onRetry: extra?.onRetry,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base-llm-provider.js","sourceRoot":"","sources":["../../src/llm/base-llm-provider.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAQzC,OAAO,EACL,iBAAiB,
|
|
1
|
+
{"version":3,"file":"base-llm-provider.js","sourceRoot":"","sources":["../../src/llm/base-llm-provider.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAQzC,OAAO,EACL,iBAAiB,EACjB,oBAAoB,GAErB,MAAM,eAAe,CAAC;AAgCvB,MAAM,OAAgB,eAAe;IAI1B,MAAM,CAAI;IAEnB,YAAY,MAAS;QACnB,IAAI,CAAC,MAAM,GAAG,MAAM,C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AG,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QAClC,OAAO,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IAC/D,CAAC;IACD,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAkC,CAAC,EAAE,CAAC;QACxE,IAAI,CAAC,CAAC,WAAW,EAAE,KAAK,IAAI;YAC1B,OAAO,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IACjE,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -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,oCAAoC,CAAC;AAK5C,oFAAoF;AACpF,MAAM,WAAW,cAAe,SAAQ,KAAK;IAC3C,SAAS,EAAE,IAAI,CAAC;IAChB,gDAAgD;IAChD,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,qDAAqD;IACrD,QAAQ,EAAE,MAAM,CAAC;IACjB;;;;;OAKG;IACH,MAAM,CAAC,EAAE,UAAU,GAAG,QAAQ,GAAG,MAAM,CAAC;CACzC;AAED,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI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AC,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;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EACL,uBAAuB,EACvB,uBAAuB,GAExB,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAErE,MAAM,eAAe,GAAG,IAAI,uBAAuB,EAAE,CAAC;AAkBtD,MAAM,UAAU,gBAAgB,CAAC,CAAU;IACzC,OAAO,CACL,OAAO,CAAC,KAAK,QAAQ;QACrB,CAAC,KAAK,IAAI;QACT,CAA6B,CAAC,SAAS,KAAK,IAAI,CAClD,CAAC;AACJ,CAA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,KAAK,SAAS;QAAE,CAAC,CAAC,MAAM,GAAG,MAAM,CAAC;IAC5C,OAAO,CAAC,CAAC;AACX,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
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
|
-
import type { RateLimitPolicy } from './llm/rate-limit.js';
|
|
6
6
|
export interface Message {
|
|
7
7
|
/**
|
|
8
8
|
* Message role
|
|
@@ -79,12 +79,15 @@ export interface LLMProviderConfig {
|
|
|
79
79
|
temperature?: number;
|
|
80
80
|
maxTokens?: number;
|
|
81
81
|
/**
|
|
82
|
-
*
|
|
83
|
-
* with jitter, honour `Retry-After`, give up after 5
|
|
84
|
-
*
|
|
85
|
-
*
|
|
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`.
|
|
86
89
|
*/
|
|
87
|
-
|
|
90
|
+
whenThrottled?: Partial<ThrottlePolicy>;
|
|
88
91
|
}
|
|
89
92
|
export interface LLMCallOptions {
|
|
90
93
|
/** Per-request model override. */
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mCAAmC,CA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