@providerprotocol/ai 0.0.39 → 0.0.40

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.
Files changed (130) hide show
  1. package/README.md +269 -34
  2. package/dist/anthropic/index.d.ts +3 -3
  3. package/dist/anthropic/index.js +7 -5
  4. package/dist/anthropic/index.js.map +1 -1
  5. package/dist/cerebras/index.d.ts +3 -3
  6. package/dist/cerebras/index.js +7 -5
  7. package/dist/cerebras/index.js.map +1 -1
  8. package/dist/{chunk-WU4U6IHF.js → chunk-6QCV4WXF.js} +4 -13
  9. package/dist/chunk-6QCV4WXF.js.map +1 -0
  10. package/dist/{chunk-5XPRVUOK.js → chunk-AC3VHSZJ.js} +2 -2
  11. package/dist/{chunk-5XPRVUOK.js.map → chunk-AC3VHSZJ.js.map} +1 -1
  12. package/dist/{chunk-ZDYEDI2A.js → chunk-CWGTARDE.js} +2 -2
  13. package/dist/{chunk-KNBODIQU.js → chunk-DI47UY2H.js} +2 -2
  14. package/dist/{chunk-KNBODIQU.js.map → chunk-DI47UY2H.js.map} +1 -1
  15. package/dist/{chunk-IDZR4ROP.js → chunk-EHR3LIPS.js} +2 -2
  16. package/dist/{chunk-IDZR4ROP.js.map → chunk-EHR3LIPS.js.map} +1 -1
  17. package/dist/chunk-EY2LLDGY.js +94 -0
  18. package/dist/chunk-EY2LLDGY.js.map +1 -0
  19. package/dist/{chunk-MJI74VEJ.js → chunk-F5ENANMJ.js} +18 -2
  20. package/dist/chunk-F5ENANMJ.js.map +1 -0
  21. package/dist/chunk-IKJH5ZSJ.js +1 -0
  22. package/dist/chunk-IKJH5ZSJ.js.map +1 -0
  23. package/dist/{chunk-IIMTP3XC.js → chunk-KBI45OXI.js} +2 -2
  24. package/dist/{chunk-SAMIK4WZ.js → chunk-KVUOTFYZ.js} +2 -2
  25. package/dist/{chunk-U6M3MXNI.js → chunk-L6QWKFGE.js} +3 -2
  26. package/dist/chunk-L6QWKFGE.js.map +1 -0
  27. package/dist/{chunk-RDC5GYST.js → chunk-N4LAFGLX.js} +7 -7
  28. package/dist/{chunk-ZKNPQBIE.js → chunk-R3T2IYOU.js} +5 -3
  29. package/dist/{chunk-ZKNPQBIE.js.map → chunk-R3T2IYOU.js.map} +1 -1
  30. package/dist/chunk-U2G5PHHL.js +25 -0
  31. package/dist/chunk-U2G5PHHL.js.map +1 -0
  32. package/dist/{chunk-SBGZJVTJ.js → chunk-VQZPADW6.js} +100 -33
  33. package/dist/chunk-VQZPADW6.js.map +1 -0
  34. package/dist/{chunk-O32SBS6S.js → chunk-XTWBAL42.js} +2 -2
  35. package/dist/{chunk-O32SBS6S.js.map → chunk-XTWBAL42.js.map} +1 -1
  36. package/dist/{chunk-WNB5PSY6.js → chunk-ZMESKGUY.js} +2 -2
  37. package/dist/{chunk-7ULSRWDH.js → chunk-ZSZVWLGE.js} +2 -2
  38. package/dist/{embedding-iNQCeXfk.d.ts → embedding-ts1npsDg.d.ts} +1 -1
  39. package/dist/google/index.d.ts +38 -4
  40. package/dist/google/index.js +5 -4
  41. package/dist/google/index.js.map +1 -1
  42. package/dist/groq/index.d.ts +3 -3
  43. package/dist/groq/index.js +7 -5
  44. package/dist/groq/index.js.map +1 -1
  45. package/dist/http/index.d.ts +5 -5
  46. package/dist/http/index.js +19 -22
  47. package/dist/{image-stream-ARno6XlS.d.ts → image-stream-BPml2YZZ.d.ts} +1 -1
  48. package/dist/index.d.ts +8 -8
  49. package/dist/index.js +306 -112
  50. package/dist/index.js.map +1 -1
  51. package/dist/{llm-CZqlijjK.d.ts → llm-BWLaTzzY.d.ts} +75 -29
  52. package/dist/middleware/logging/index.d.ts +3 -3
  53. package/dist/middleware/logging/index.js +3 -0
  54. package/dist/middleware/logging/index.js.map +1 -1
  55. package/dist/middleware/parsed-object/index.d.ts +3 -3
  56. package/dist/middleware/parsed-object/index.js +5 -1
  57. package/dist/middleware/parsed-object/index.js.map +1 -1
  58. package/dist/middleware/persistence/index.d.ts +3 -3
  59. package/dist/middleware/persistence/index.js +3 -2
  60. package/dist/middleware/persistence/index.js.map +1 -1
  61. package/dist/middleware/pipeline/index.d.ts +195 -0
  62. package/dist/middleware/pipeline/index.js +61 -0
  63. package/dist/middleware/pipeline/index.js.map +1 -0
  64. package/dist/middleware/pubsub/index.d.ts +13 -11
  65. package/dist/middleware/pubsub/index.js +31 -5
  66. package/dist/middleware/pubsub/index.js.map +1 -1
  67. package/dist/middleware/pubsub/server/express/index.d.ts +3 -3
  68. package/dist/middleware/pubsub/server/express/index.js +2 -2
  69. package/dist/middleware/pubsub/server/fastify/index.d.ts +3 -3
  70. package/dist/middleware/pubsub/server/fastify/index.js +2 -2
  71. package/dist/middleware/pubsub/server/h3/index.d.ts +3 -3
  72. package/dist/middleware/pubsub/server/h3/index.js +2 -2
  73. package/dist/middleware/pubsub/server/index.d.ts +50 -9
  74. package/dist/middleware/pubsub/server/index.js +5 -5
  75. package/dist/middleware/pubsub/server/index.js.map +1 -1
  76. package/dist/middleware/pubsub/server/webapi/index.d.ts +3 -3
  77. package/dist/middleware/pubsub/server/webapi/index.js +2 -2
  78. package/dist/moonshot/index.d.ts +3 -3
  79. package/dist/moonshot/index.js +7 -5
  80. package/dist/moonshot/index.js.map +1 -1
  81. package/dist/ollama/index.d.ts +24 -4
  82. package/dist/ollama/index.js +5 -4
  83. package/dist/ollama/index.js.map +1 -1
  84. package/dist/openai/index.d.ts +65 -4
  85. package/dist/openai/index.js +7 -5
  86. package/dist/openai/index.js.map +1 -1
  87. package/dist/openrouter/index.d.ts +4 -4
  88. package/dist/openrouter/index.js +7 -5
  89. package/dist/openrouter/index.js.map +1 -1
  90. package/dist/proxy/index.d.ts +5 -5
  91. package/dist/proxy/index.js +16 -15
  92. package/dist/proxy/index.js.map +1 -1
  93. package/dist/proxy/server/express/index.d.ts +8 -9
  94. package/dist/proxy/server/express/index.js +4 -3
  95. package/dist/proxy/server/fastify/index.d.ts +8 -9
  96. package/dist/proxy/server/fastify/index.js +4 -3
  97. package/dist/proxy/server/h3/index.d.ts +8 -9
  98. package/dist/proxy/server/h3/index.js +4 -3
  99. package/dist/proxy/server/index.d.ts +5 -5
  100. package/dist/proxy/server/index.js +14 -13
  101. package/dist/proxy/server/webapi/index.d.ts +8 -9
  102. package/dist/proxy/server/webapi/index.js +4 -3
  103. package/dist/responses/index.d.ts +3 -3
  104. package/dist/responses/index.js +7 -5
  105. package/dist/responses/index.js.map +1 -1
  106. package/dist/retry-DVfdPLIB.d.ts +322 -0
  107. package/dist/{stream-DVVUIKpz.d.ts → stream-bBd_4Ipu.d.ts} +27 -4
  108. package/dist/{tool-D22EhP5F.d.ts → tool-BmAfKNBq.d.ts} +1 -1
  109. package/dist/{types-CyXF0J7C.d.ts → types-nTwlpyJE.d.ts} +13 -1
  110. package/dist/utils/index.d.ts +66 -2
  111. package/dist/utils/index.js +13 -0
  112. package/dist/xai/index.d.ts +3 -3
  113. package/dist/xai/index.js +7 -5
  114. package/dist/xai/index.js.map +1 -1
  115. package/package.json +6 -1
  116. package/dist/chunk-ARVM24K2.js +0 -128
  117. package/dist/chunk-ARVM24K2.js.map +0 -1
  118. package/dist/chunk-MJI74VEJ.js.map +0 -1
  119. package/dist/chunk-SBGZJVTJ.js.map +0 -1
  120. package/dist/chunk-U6M3MXNI.js.map +0 -1
  121. package/dist/chunk-WU4U6IHF.js.map +0 -1
  122. package/dist/chunk-Y5H7C5J4.js +0 -263
  123. package/dist/chunk-Y5H7C5J4.js.map +0 -1
  124. package/dist/retry-C1eJbEMV.d.ts +0 -531
  125. /package/dist/{chunk-ZDYEDI2A.js.map → chunk-CWGTARDE.js.map} +0 -0
  126. /package/dist/{chunk-IIMTP3XC.js.map → chunk-KBI45OXI.js.map} +0 -0
  127. /package/dist/{chunk-SAMIK4WZ.js.map → chunk-KVUOTFYZ.js.map} +0 -0
  128. /package/dist/{chunk-RDC5GYST.js.map → chunk-N4LAFGLX.js.map} +0 -0
  129. /package/dist/{chunk-WNB5PSY6.js.map → chunk-ZMESKGUY.js.map} +0 -0
  130. /package/dist/{chunk-7ULSRWDH.js.map → chunk-ZSZVWLGE.js.map} +0 -0
@@ -1,531 +0,0 @@
1
- import { K as KeyStrategy, c as ProviderConfig, f as Modality, R as RetryStrategy, U as UPPError } from './llm-CZqlijjK.js';
2
-
3
- /**
4
- * API key management strategies for load balancing and dynamic key selection.
5
- * @module http/keys
6
- */
7
-
8
- /**
9
- * Distributes API requests across multiple keys using round-robin selection.
10
- *
11
- * Each call to {@link getKey} returns the next key in sequence, cycling back to
12
- * the first key after reaching the end. This provides even distribution of requests
13
- * across all available keys, which is useful for:
14
- * - Spreading rate limits across multiple API keys
15
- * - Load balancing between different accounts
16
- * - Maximizing throughput when multiple keys are available
17
- *
18
- * @implements {KeyStrategy}
19
- *
20
- * @example
21
- * ```typescript
22
- * const keys = new RoundRobinKeys([
23
- * 'sk-key-1',
24
- * 'sk-key-2',
25
- * 'sk-key-3'
26
- * ]);
27
- *
28
- * keys.getKey(); // Returns 'sk-key-1'
29
- * keys.getKey(); // Returns 'sk-key-2'
30
- * keys.getKey(); // Returns 'sk-key-3'
31
- * keys.getKey(); // Returns 'sk-key-1' (cycles back)
32
- * ```
33
- */
34
- declare class RoundRobinKeys implements KeyStrategy {
35
- private keys;
36
- private index;
37
- /**
38
- * Creates a new RoundRobinKeys instance.
39
- *
40
- * @param keys - Array of API keys to rotate through
41
- * @throws {Error} When the keys array is empty
42
- */
43
- constructor(keys: string[]);
44
- /**
45
- * Returns the next key in the rotation sequence.
46
- *
47
- * @returns The next API key in round-robin order
48
- */
49
- getKey(): string;
50
- }
51
- /**
52
- * Selects API keys using weighted random probability.
53
- *
54
- * Each key is assigned a weight that determines its probability of being selected.
55
- * Higher weights mean higher selection probability. This is useful for:
56
- * - Preferring higher-tier API keys with better rate limits
57
- * - Gradually migrating traffic between old and new keys
58
- * - A/B testing different API accounts
59
- * - Directing more traffic to keys with higher quotas
60
- *
61
- * The selection probability for each key is: weight / totalWeight
62
- *
63
- * @implements {KeyStrategy}
64
- *
65
- * @example
66
- * ```typescript
67
- * const keys = new WeightedKeys([
68
- * { key: 'sk-premium', weight: 70 }, // 70% of requests
69
- * { key: 'sk-standard', weight: 20 }, // 20% of requests
70
- * { key: 'sk-backup', weight: 10 } // 10% of requests
71
- * ]);
72
- *
73
- * // Configure provider with weighted key selection
74
- * const provider = createOpenAI({
75
- * apiKey: keys
76
- * });
77
- * ```
78
- */
79
- declare class WeightedKeys implements KeyStrategy {
80
- private entries;
81
- private totalWeight;
82
- /**
83
- * Creates a new WeightedKeys instance.
84
- *
85
- * @param keys - Array of key-weight pairs defining selection probabilities
86
- * @throws {Error} When the keys array is empty
87
- */
88
- constructor(keys: Array<{
89
- key: string;
90
- weight: number;
91
- }>);
92
- /**
93
- * Returns a randomly selected key based on configured weights.
94
- *
95
- * @returns An API key selected with probability proportional to its weight
96
- */
97
- getKey(): string;
98
- }
99
- /**
100
- * Provides dynamic key selection using custom logic.
101
- *
102
- * This strategy delegates key selection to a user-provided function, enabling
103
- * advanced scenarios such as:
104
- * - Fetching keys from a secrets manager (AWS Secrets Manager, HashiCorp Vault)
105
- * - Rotating keys based on external state or configuration
106
- * - Selecting keys based on request context or time of day
107
- * - Implementing custom load balancing algorithms
108
- *
109
- * The selector function can be synchronous or asynchronous.
110
- *
111
- * @implements {KeyStrategy}
112
- *
113
- * @example
114
- * ```typescript
115
- * // Fetch key from environment based on current mode
116
- * const dynamicKey = new DynamicKey(() => {
117
- * return process.env.NODE_ENV === 'production'
118
- * ? process.env.PROD_API_KEY!
119
- * : process.env.DEV_API_KEY!;
120
- * });
121
- *
122
- * // Async key fetching from a secrets manager
123
- * const vaultKey = new DynamicKey(async () => {
124
- * const secret = await vault.read('secret/openai');
125
- * return secret.data.apiKey;
126
- * });
127
- *
128
- * // Time-based key rotation
129
- * const timedKey = new DynamicKey(() => {
130
- * const hour = new Date().getHours();
131
- * return hour < 12 ? morningKey : afternoonKey;
132
- * });
133
- * ```
134
- */
135
- declare class DynamicKey implements KeyStrategy {
136
- private selector;
137
- /**
138
- * Creates a new DynamicKey instance.
139
- *
140
- * @param selector - Function that returns an API key (sync or async)
141
- */
142
- constructor(selector: () => string | Promise<string>);
143
- /**
144
- * Invokes the selector function to retrieve the current key.
145
- *
146
- * @returns Promise resolving to the selected API key
147
- */
148
- getKey(): Promise<string>;
149
- }
150
- /**
151
- * Masks an API key for safe logging.
152
- * Shows first 4 and last 4 characters with ellipsis, or '***' for short keys.
153
- *
154
- * @param key - The API key to mask
155
- * @returns Masked key like "sk-ab...yz12" or "***" for short keys
156
- *
157
- * @example
158
- * ```typescript
159
- * maskApiKey('sk-abc123def456xyz789'); // 'sk-a...z789'
160
- * maskApiKey('short'); // '***'
161
- * ```
162
- */
163
- declare function maskApiKey(key: string): string;
164
- /**
165
- * Resolves an API key from provider configuration with multiple fallback options.
166
- *
167
- * This function handles various key specification methods in priority order:
168
- * 1. Direct string key in config.apiKey
169
- * 2. Function returning a key (sync or async) in config.apiKey
170
- * 3. KeyStrategy instance in config.apiKey (RoundRobinKeys, WeightedKeys, DynamicKey)
171
- * 4. Environment variable fallback (if envVar parameter is provided)
172
- *
173
- * @param config - Provider configuration containing the apiKey option
174
- * @param envVar - Optional environment variable name to check as fallback
175
- * @param provider - Provider identifier for error context (default: 'unknown')
176
- * @param modality - Request modality for error context (default: 'llm')
177
- * @returns The resolved API key string
178
- *
179
- * @throws {UPPError} AUTHENTICATION_FAILED - When no valid key is found
180
- *
181
- * @example
182
- * ```typescript
183
- * // Direct key in config
184
- * const key1 = await resolveApiKey({ apiKey: 'sk-...' }, 'OPENAI_API_KEY', 'openai');
185
- *
186
- * // Function-based key
187
- * const key2 = await resolveApiKey({ apiKey: () => getKeyFromVault() }, undefined, 'anthropic');
188
- *
189
- * // KeyStrategy instance
190
- * const key3 = await resolveApiKey({
191
- * apiKey: new RoundRobinKeys(['sk-1', 'sk-2', 'sk-3'])
192
- * }, 'OPENAI_API_KEY', 'openai');
193
- *
194
- * // Environment variable fallback
195
- * const key4 = await resolveApiKey({}, 'ANTHROPIC_API_KEY', 'anthropic');
196
- * ```
197
- */
198
- declare function resolveApiKey(config: ProviderConfig, envVar?: string, provider?: string, modality?: Modality): Promise<string>;
199
-
200
- /**
201
- * Retry strategies for handling transient failures in HTTP requests.
202
- * @module http/retry
203
- */
204
-
205
- /**
206
- * Implements exponential backoff with optional jitter for retry delays.
207
- *
208
- * The delay between retries doubles with each attempt, helping to:
209
- * - Avoid overwhelming servers during outages
210
- * - Reduce thundering herd effects when many clients retry simultaneously
211
- * - Give transient issues time to resolve
212
- *
213
- * Delay formula: min(baseDelay * 2^(attempt-1), maxDelay)
214
- * With jitter: delay * random(0.5, 1.0)
215
- *
216
- * Only retries on transient errors: RATE_LIMITED, NETWORK_ERROR, TIMEOUT, PROVIDER_ERROR
217
- *
218
- * @implements {RetryStrategy}
219
- *
220
- * @example
221
- * ```typescript
222
- * // Default configuration (3 retries, 1s base, 30s max, jitter enabled)
223
- * const retry = new ExponentialBackoff();
224
- *
225
- * // Custom configuration
226
- * const customRetry = new ExponentialBackoff({
227
- * maxAttempts: 5, // Up to 5 retry attempts
228
- * baseDelay: 500, // Start with 500ms delay
229
- * maxDelay: 60000, // Cap at 60 seconds
230
- * jitter: false // Disable random jitter
231
- * });
232
- *
233
- * // Use with provider
234
- * const provider = createOpenAI({
235
- * retryStrategy: customRetry
236
- * });
237
- * ```
238
- */
239
- declare class ExponentialBackoff implements RetryStrategy {
240
- private maxAttempts;
241
- private baseDelay;
242
- private maxDelay;
243
- private jitter;
244
- /**
245
- * Creates a new ExponentialBackoff instance.
246
- *
247
- * @param options - Configuration options
248
- * @param options.maxAttempts - Maximum number of retry attempts (default: 3)
249
- * @param options.baseDelay - Initial delay in milliseconds (default: 1000)
250
- * @param options.maxDelay - Maximum delay cap in milliseconds (default: 30000)
251
- * @param options.jitter - Whether to add random jitter to delays (default: true)
252
- */
253
- constructor(options?: {
254
- maxAttempts?: number;
255
- baseDelay?: number;
256
- maxDelay?: number;
257
- jitter?: boolean;
258
- });
259
- /**
260
- * Determines whether to retry and calculates the delay.
261
- *
262
- * @param error - The error that triggered the retry
263
- * @param attempt - Current attempt number (1-indexed)
264
- * @returns Delay in milliseconds before next retry, or null to stop retrying
265
- */
266
- onRetry(error: UPPError, attempt: number): number | null;
267
- /**
268
- * Checks if an error is eligible for retry.
269
- *
270
- * @param error - The error to evaluate
271
- * @returns True if the error is transient and retryable
272
- */
273
- private isRetryable;
274
- }
275
- /**
276
- * Implements linear backoff where delays increase proportionally with each attempt.
277
- *
278
- * Unlike exponential backoff, linear backoff increases delays at a constant rate:
279
- * - Attempt 1: delay * 1 (e.g., 1000ms)
280
- * - Attempt 2: delay * 2 (e.g., 2000ms)
281
- * - Attempt 3: delay * 3 (e.g., 3000ms)
282
- *
283
- * This strategy is simpler and more predictable than exponential backoff,
284
- * suitable for scenarios where gradual delay increase is preferred over
285
- * aggressive backoff.
286
- *
287
- * Only retries on transient errors: RATE_LIMITED, NETWORK_ERROR, TIMEOUT, PROVIDER_ERROR
288
- *
289
- * @implements {RetryStrategy}
290
- *
291
- * @example
292
- * ```typescript
293
- * // Default configuration (3 retries, 1s delay increment)
294
- * const retry = new LinearBackoff();
295
- *
296
- * // Custom configuration
297
- * const customRetry = new LinearBackoff({
298
- * maxAttempts: 4, // Up to 4 retry attempts
299
- * delay: 2000 // 2s, 4s, 6s, 8s delays
300
- * });
301
- *
302
- * // Use with provider
303
- * const provider = createAnthropic({
304
- * retryStrategy: customRetry
305
- * });
306
- * ```
307
- */
308
- declare class LinearBackoff implements RetryStrategy {
309
- private maxAttempts;
310
- private delay;
311
- /**
312
- * Creates a new LinearBackoff instance.
313
- *
314
- * @param options - Configuration options
315
- * @param options.maxAttempts - Maximum number of retry attempts (default: 3)
316
- * @param options.delay - Base delay multiplier in milliseconds (default: 1000)
317
- */
318
- constructor(options?: {
319
- maxAttempts?: number;
320
- delay?: number;
321
- });
322
- /**
323
- * Determines whether to retry and calculates the linear delay.
324
- *
325
- * @param error - The error that triggered the retry
326
- * @param attempt - Current attempt number (1-indexed)
327
- * @returns Delay in milliseconds (delay * attempt), or null to stop retrying
328
- */
329
- onRetry(error: UPPError, attempt: number): number | null;
330
- /**
331
- * Checks if an error is eligible for retry.
332
- *
333
- * @param error - The error to evaluate
334
- * @returns True if the error is transient and retryable
335
- */
336
- private isRetryable;
337
- }
338
- /**
339
- * Disables all retry behavior, failing immediately on any error.
340
- *
341
- * Use this strategy when:
342
- * - Retries are handled at a higher level in your application
343
- * - You want immediate failure feedback
344
- * - The operation is not idempotent
345
- * - Time sensitivity requires fast failure
346
- *
347
- * @implements {RetryStrategy}
348
- *
349
- * @example
350
- * ```typescript
351
- * // Disable retries for time-sensitive operations
352
- * const provider = createOpenAI({
353
- * retryStrategy: new NoRetry()
354
- * });
355
- * ```
356
- */
357
- declare class NoRetry implements RetryStrategy {
358
- /**
359
- * Always returns null to indicate no retry should be attempted.
360
- *
361
- * @returns Always returns null
362
- */
363
- onRetry(_error: UPPError, _attempt: number): null;
364
- }
365
- /**
366
- * Implements token bucket rate limiting with automatic refill.
367
- *
368
- * The token bucket algorithm provides smooth rate limiting by:
369
- * - Maintaining a bucket of tokens that replenish over time
370
- * - Consuming one token per request
371
- * - Delaying requests when the bucket is empty
372
- * - Allowing burst traffic up to the bucket capacity
373
- *
374
- * This is particularly useful for:
375
- * - Client-side rate limiting to avoid hitting API rate limits
376
- * - Smoothing request patterns to maintain consistent throughput
377
- * - Preventing accidental API abuse
378
- *
379
- * Unlike other retry strategies, TokenBucket implements {@link beforeRequest}
380
- * to proactively delay requests before they are made.
381
- *
382
- * @implements {RetryStrategy}
383
- *
384
- * @example
385
- * ```typescript
386
- * // Allow 10 requests burst, refill 1 token per second
387
- * const bucket = new TokenBucket({
388
- * maxTokens: 10, // Burst capacity
389
- * refillRate: 1, // Tokens per second
390
- * maxAttempts: 3 // Retry attempts on rate limit
391
- * });
392
- *
393
- * // Aggressive rate limiting: 5 req/s sustained
394
- * const strictBucket = new TokenBucket({
395
- * maxTokens: 5,
396
- * refillRate: 5
397
- * });
398
- *
399
- * // Use with provider
400
- * const provider = createOpenAI({
401
- * retryStrategy: bucket
402
- * });
403
- * ```
404
- */
405
- declare class TokenBucket implements RetryStrategy {
406
- private tokens;
407
- private maxTokens;
408
- private refillRate;
409
- private lastRefill;
410
- private maxAttempts;
411
- private lock;
412
- /**
413
- * Creates a new TokenBucket instance.
414
- *
415
- * @param options - Configuration options
416
- * @param options.maxTokens - Maximum bucket capacity (default: 10)
417
- * @param options.refillRate - Tokens added per second (default: 1)
418
- * @param options.maxAttempts - Maximum retry attempts on rate limit (default: 3)
419
- */
420
- constructor(options?: {
421
- maxTokens?: number;
422
- refillRate?: number;
423
- maxAttempts?: number;
424
- });
425
- /**
426
- * Called before each request to consume a token or calculate wait time.
427
- *
428
- * Refills the bucket based on elapsed time, then either:
429
- * - Returns 0 if a token is available (consumed immediately)
430
- * - Returns the wait time in milliseconds until the next token
431
- *
432
- * This method may allow tokens to go negative to reserve future capacity
433
- * and avoid concurrent callers oversubscribing the same refill.
434
- *
435
- * @returns Delay in milliseconds before the request can proceed
436
- */
437
- beforeRequest(): Promise<number>;
438
- /**
439
- * Handles retry logic for rate-limited requests.
440
- *
441
- * Only retries on RATE_LIMITED errors, waiting for bucket refill.
442
- *
443
- * @param error - The error that triggered the retry
444
- * @param attempt - Current attempt number (1-indexed)
445
- * @returns Delay in milliseconds (time for 2 tokens), or null to stop
446
- */
447
- onRetry(error: UPPError, attempt: number): number | null;
448
- /**
449
- * Resets the bucket to full capacity.
450
- *
451
- * Called automatically on successful requests to restore available tokens.
452
- */
453
- reset(): void;
454
- /**
455
- * Refills the bucket based on elapsed time since last refill.
456
- */
457
- private refill;
458
- private withLock;
459
- }
460
- /**
461
- * Respects server-provided Retry-After headers for optimal retry timing.
462
- *
463
- * When servers return a 429 (Too Many Requests) response, they often include
464
- * a Retry-After header indicating when the client should retry. This strategy
465
- * uses that information for precise retry timing.
466
- *
467
- * Benefits over fixed backoff strategies:
468
- * - Follows server recommendations for optimal retry timing
469
- * - Avoids retrying too early and wasting requests
470
- * - Adapts to dynamic rate limit windows
471
- *
472
- * If no Retry-After header is provided, falls back to a configurable delay.
473
- * Only retries on RATE_LIMITED errors.
474
- *
475
- * @implements {RetryStrategy}
476
- *
477
- * @example
478
- * ```typescript
479
- * // Use server-recommended retry timing
480
- * const retryAfter = new RetryAfterStrategy({
481
- * maxAttempts: 5, // Retry up to 5 times
482
- * fallbackDelay: 10000 // 10s fallback if no header
483
- * });
484
- *
485
- * // The doFetch function automatically calls setRetryAfter
486
- * // when a Retry-After header is present in the response
487
- *
488
- * const provider = createOpenAI({
489
- * retryStrategy: retryAfter
490
- * });
491
- * ```
492
- */
493
- declare class RetryAfterStrategy implements RetryStrategy {
494
- private maxAttempts;
495
- private fallbackDelay;
496
- private lastRetryAfter?;
497
- /**
498
- * Creates a new RetryAfterStrategy instance.
499
- *
500
- * @param options - Configuration options
501
- * @param options.maxAttempts - Maximum number of retry attempts (default: 3)
502
- * @param options.fallbackDelay - Delay in ms when no Retry-After header (default: 5000)
503
- */
504
- constructor(options?: {
505
- maxAttempts?: number;
506
- fallbackDelay?: number;
507
- });
508
- /**
509
- * Creates a request-scoped copy of this strategy.
510
- */
511
- fork(): RetryAfterStrategy;
512
- /**
513
- * Sets the retry delay from a Retry-After header value.
514
- *
515
- * Called by doFetch when a Retry-After header is present in the response.
516
- * The value is used for the next onRetry call and then cleared.
517
- *
518
- * @param seconds - The Retry-After value in seconds
519
- */
520
- setRetryAfter(seconds: number): void;
521
- /**
522
- * Determines retry delay using Retry-After header or fallback.
523
- *
524
- * @param error - The error that triggered the retry
525
- * @param attempt - Current attempt number (1-indexed)
526
- * @returns Delay from Retry-After header or fallback, null to stop
527
- */
528
- onRetry(error: UPPError, attempt: number): number | null;
529
- }
530
-
531
- export { DynamicKey as D, ExponentialBackoff as E, LinearBackoff as L, NoRetry as N, RoundRobinKeys as R, TokenBucket as T, WeightedKeys as W, RetryAfterStrategy as a, maskApiKey as m, resolveApiKey as r };