@providerprotocol/ai 0.0.38 → 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 (136) hide show
  1. package/README.md +363 -37
  2. package/dist/anthropic/index.d.ts +3 -2
  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 -2
  6. package/dist/cerebras/index.js +7 -5
  7. package/dist/cerebras/index.js.map +1 -1
  8. package/dist/chunk-3Q5VELKG.js +124 -0
  9. package/dist/chunk-3Q5VELKG.js.map +1 -0
  10. package/dist/{chunk-WU4U6IHF.js → chunk-6QCV4WXF.js} +4 -13
  11. package/dist/chunk-6QCV4WXF.js.map +1 -0
  12. package/dist/{chunk-LTEMH3CI.js → chunk-AC3VHSZJ.js} +6 -4
  13. package/dist/{chunk-LTEMH3CI.js.map → chunk-AC3VHSZJ.js.map} +1 -1
  14. package/dist/{chunk-YQLR3XOA.js → chunk-BIBMNP7Y.js} +1 -75
  15. package/dist/chunk-BIBMNP7Y.js.map +1 -0
  16. package/dist/{chunk-CRP6Y7NF.js → chunk-CWGTARDE.js} +2 -2
  17. package/dist/{chunk-ZRVNAET3.js → chunk-DI47UY2H.js} +6 -3
  18. package/dist/chunk-DI47UY2H.js.map +1 -0
  19. package/dist/{chunk-7GTWHZY2.js → chunk-EHR3LIPS.js} +5 -3
  20. package/dist/{chunk-7GTWHZY2.js.map → chunk-EHR3LIPS.js.map} +1 -1
  21. package/dist/chunk-EY2LLDGY.js +94 -0
  22. package/dist/chunk-EY2LLDGY.js.map +1 -0
  23. package/dist/{chunk-MJI74VEJ.js → chunk-F5ENANMJ.js} +18 -2
  24. package/dist/chunk-F5ENANMJ.js.map +1 -0
  25. package/dist/chunk-IKJH5ZSJ.js +1 -0
  26. package/dist/chunk-IKJH5ZSJ.js.map +1 -0
  27. package/dist/{chunk-4RX4VQCB.js → chunk-KBI45OXI.js} +2 -2
  28. package/dist/{chunk-5IWHCXKN.js → chunk-KVUOTFYZ.js} +2 -2
  29. package/dist/{chunk-EPB3GQNL.js → chunk-L6QWKFGE.js} +13 -3
  30. package/dist/chunk-L6QWKFGE.js.map +1 -0
  31. package/dist/{chunk-BDXH6NQS.js → chunk-N4LAFGLX.js} +7 -7
  32. package/dist/{chunk-ZKNPQBIE.js → chunk-R3T2IYOU.js} +5 -3
  33. package/dist/{chunk-ZKNPQBIE.js.map → chunk-R3T2IYOU.js.map} +1 -1
  34. package/dist/chunk-U2G5PHHL.js +25 -0
  35. package/dist/chunk-U2G5PHHL.js.map +1 -0
  36. package/dist/{chunk-SBGZJVTJ.js → chunk-VQZPADW6.js} +100 -33
  37. package/dist/chunk-VQZPADW6.js.map +1 -0
  38. package/dist/{chunk-FYSZFIZS.js → chunk-XTWBAL42.js} +5 -3
  39. package/dist/{chunk-FYSZFIZS.js.map → chunk-XTWBAL42.js.map} +1 -1
  40. package/dist/{chunk-2YXFLRQ6.js → chunk-ZMESKGUY.js} +2 -2
  41. package/dist/chunk-ZSZVWLGE.js +83 -0
  42. package/dist/chunk-ZSZVWLGE.js.map +1 -0
  43. package/dist/{embedding-CwZ1ZNWv.d.ts → embedding-ts1npsDg.d.ts} +1 -1
  44. package/dist/google/index.d.ts +38 -3
  45. package/dist/google/index.js +5 -4
  46. package/dist/google/index.js.map +1 -1
  47. package/dist/groq/index.d.ts +3 -2
  48. package/dist/groq/index.js +7 -5
  49. package/dist/groq/index.js.map +1 -1
  50. package/dist/http/index.d.ts +5 -4
  51. package/dist/http/index.js +19 -22
  52. package/dist/{image-stream-CeQHtjxS.d.ts → image-stream-BPml2YZZ.d.ts} +1 -1
  53. package/dist/index.d.ts +8 -7
  54. package/dist/index.js +316 -113
  55. package/dist/index.js.map +1 -1
  56. package/dist/{llm-DS_-l71X.d.ts → llm-BWLaTzzY.d.ts} +89 -36
  57. package/dist/middleware/logging/index.d.ts +3 -2
  58. package/dist/middleware/logging/index.js +3 -0
  59. package/dist/middleware/logging/index.js.map +1 -1
  60. package/dist/middleware/parsed-object/index.d.ts +3 -2
  61. package/dist/middleware/parsed-object/index.js +5 -1
  62. package/dist/middleware/parsed-object/index.js.map +1 -1
  63. package/dist/middleware/persistence/index.d.ts +3 -2
  64. package/dist/middleware/persistence/index.js +3 -2
  65. package/dist/middleware/persistence/index.js.map +1 -1
  66. package/dist/middleware/pipeline/index.d.ts +195 -0
  67. package/dist/middleware/pipeline/index.js +61 -0
  68. package/dist/middleware/pipeline/index.js.map +1 -0
  69. package/dist/middleware/pubsub/index.d.ts +13 -10
  70. package/dist/middleware/pubsub/index.js +78 -6
  71. package/dist/middleware/pubsub/index.js.map +1 -1
  72. package/dist/middleware/pubsub/server/express/index.d.ts +3 -2
  73. package/dist/middleware/pubsub/server/express/index.js +2 -2
  74. package/dist/middleware/pubsub/server/fastify/index.d.ts +3 -2
  75. package/dist/middleware/pubsub/server/fastify/index.js +2 -2
  76. package/dist/middleware/pubsub/server/h3/index.d.ts +3 -2
  77. package/dist/middleware/pubsub/server/h3/index.js +2 -2
  78. package/dist/middleware/pubsub/server/index.d.ts +50 -8
  79. package/dist/middleware/pubsub/server/index.js +5 -5
  80. package/dist/middleware/pubsub/server/index.js.map +1 -1
  81. package/dist/middleware/pubsub/server/webapi/index.d.ts +3 -2
  82. package/dist/middleware/pubsub/server/webapi/index.js +2 -2
  83. package/dist/moonshot/index.d.ts +3 -2
  84. package/dist/moonshot/index.js +7 -5
  85. package/dist/moonshot/index.js.map +1 -1
  86. package/dist/ollama/index.d.ts +24 -3
  87. package/dist/ollama/index.js +5 -4
  88. package/dist/ollama/index.js.map +1 -1
  89. package/dist/openai/index.d.ts +65 -3
  90. package/dist/openai/index.js +7 -5
  91. package/dist/openai/index.js.map +1 -1
  92. package/dist/openrouter/index.d.ts +4 -3
  93. package/dist/openrouter/index.js +7 -5
  94. package/dist/openrouter/index.js.map +1 -1
  95. package/dist/proxy/index.d.ts +5 -4
  96. package/dist/proxy/index.js +20 -17
  97. package/dist/proxy/index.js.map +1 -1
  98. package/dist/proxy/server/express/index.d.ts +8 -8
  99. package/dist/proxy/server/express/index.js +5 -3
  100. package/dist/proxy/server/fastify/index.d.ts +8 -8
  101. package/dist/proxy/server/fastify/index.js +5 -3
  102. package/dist/proxy/server/h3/index.d.ts +22 -21
  103. package/dist/proxy/server/h3/index.js +5 -3
  104. package/dist/proxy/server/index.d.ts +5 -4
  105. package/dist/proxy/server/index.js +15 -13
  106. package/dist/proxy/server/webapi/index.d.ts +8 -8
  107. package/dist/proxy/server/webapi/index.js +5 -3
  108. package/dist/responses/index.d.ts +3 -2
  109. package/dist/responses/index.js +7 -5
  110. package/dist/responses/index.js.map +1 -1
  111. package/dist/retry-DVfdPLIB.d.ts +322 -0
  112. package/dist/{stream-sXhBtWjl.d.ts → stream-bBd_4Ipu.d.ts} +29 -419
  113. package/dist/tool-BmAfKNBq.d.ts +507 -0
  114. package/dist/{types-Cr4F0tVy.d.ts → types-nTwlpyJE.d.ts} +28 -3
  115. package/dist/utils/index.d.ts +129 -1
  116. package/dist/utils/index.js +28 -1
  117. package/dist/xai/index.d.ts +3 -2
  118. package/dist/xai/index.js +7 -5
  119. package/dist/xai/index.js.map +1 -1
  120. package/package.json +20 -3
  121. package/dist/chunk-ARVM24K2.js +0 -128
  122. package/dist/chunk-ARVM24K2.js.map +0 -1
  123. package/dist/chunk-EPB3GQNL.js.map +0 -1
  124. package/dist/chunk-MJI74VEJ.js.map +0 -1
  125. package/dist/chunk-SBGZJVTJ.js.map +0 -1
  126. package/dist/chunk-WU4U6IHF.js.map +0 -1
  127. package/dist/chunk-Y5H7C5J4.js +0 -263
  128. package/dist/chunk-Y5H7C5J4.js.map +0 -1
  129. package/dist/chunk-YQLR3XOA.js.map +0 -1
  130. package/dist/chunk-ZRVNAET3.js.map +0 -1
  131. package/dist/retry-CgoBNa51.d.ts +0 -531
  132. /package/dist/{chunk-CRP6Y7NF.js.map → chunk-CWGTARDE.js.map} +0 -0
  133. /package/dist/{chunk-4RX4VQCB.js.map → chunk-KBI45OXI.js.map} +0 -0
  134. /package/dist/{chunk-5IWHCXKN.js.map → chunk-KVUOTFYZ.js.map} +0 -0
  135. /package/dist/{chunk-BDXH6NQS.js.map → chunk-N4LAFGLX.js.map} +0 -0
  136. /package/dist/{chunk-2YXFLRQ6.js.map → chunk-ZMESKGUY.js.map} +0 -0
@@ -0,0 +1,322 @@
1
+ import { K as KeyStrategy, c as ProviderConfig, f as Modality, R as RetryStrategyFactory } from './llm-BWLaTzzY.js';
2
+
3
+ /**
4
+ * API key management strategies for load balancing and dynamic key selection.
5
+ * @module http/keys
6
+ */
7
+
8
+ /**
9
+ * Creates a key strategy that distributes API requests across multiple keys
10
+ * using round-robin selection.
11
+ *
12
+ * Each call to `getKey()` returns the next key in sequence, cycling back to
13
+ * the first key after reaching the end. This provides even distribution of requests
14
+ * across all available keys, which is useful for:
15
+ * - Spreading rate limits across multiple API keys
16
+ * - Load balancing between different accounts
17
+ * - Maximizing throughput when multiple keys are available
18
+ *
19
+ * @param keys - Array of API keys to rotate through
20
+ * @returns A {@link KeyStrategy} that cycles through the provided keys
21
+ * @throws {Error} When the keys array is empty
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * const keys = roundRobinKeys([
26
+ * 'sk-key-1',
27
+ * 'sk-key-2',
28
+ * 'sk-key-3'
29
+ * ]);
30
+ *
31
+ * keys.getKey(); // Returns 'sk-key-1'
32
+ * keys.getKey(); // Returns 'sk-key-2'
33
+ * keys.getKey(); // Returns 'sk-key-3'
34
+ * keys.getKey(); // Returns 'sk-key-1' (cycles back)
35
+ * ```
36
+ */
37
+ declare function roundRobinKeys(keys: string[]): KeyStrategy;
38
+ /**
39
+ * Creates a key strategy that selects API keys using weighted random probability.
40
+ *
41
+ * Each key is assigned a weight that determines its probability of being selected.
42
+ * Higher weights mean higher selection probability. This is useful for:
43
+ * - Preferring higher-tier API keys with better rate limits
44
+ * - Gradually migrating traffic between old and new keys
45
+ * - A/B testing different API accounts
46
+ * - Directing more traffic to keys with higher quotas
47
+ *
48
+ * The selection probability for each key is: weight / totalWeight
49
+ *
50
+ * @param keys - Array of key-weight pairs defining selection probabilities
51
+ * @returns A {@link KeyStrategy} that selects keys by weighted probability
52
+ * @throws {Error} When the keys array is empty
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * const keys = weightedKeys([
57
+ * { key: 'sk-premium', weight: 70 }, // 70% of requests
58
+ * { key: 'sk-standard', weight: 20 }, // 20% of requests
59
+ * { key: 'sk-backup', weight: 10 } // 10% of requests
60
+ * ]);
61
+ *
62
+ * // Configure provider with weighted key selection
63
+ * const provider = createOpenAI({
64
+ * apiKey: keys
65
+ * });
66
+ * ```
67
+ */
68
+ declare function weightedKeys(keys: Array<{
69
+ key: string;
70
+ weight: number;
71
+ }>): KeyStrategy;
72
+ /**
73
+ * Creates a key strategy that delegates key selection to a user-provided function,
74
+ * enabling advanced scenarios such as:
75
+ * - Fetching keys from a secrets manager (AWS Secrets Manager, HashiCorp Vault)
76
+ * - Rotating keys based on external state or configuration
77
+ * - Selecting keys based on request context or time of day
78
+ * - Implementing custom load balancing algorithms
79
+ *
80
+ * The selector function can be synchronous or asynchronous.
81
+ *
82
+ * @param selector - Function that returns an API key (sync or async)
83
+ * @returns A {@link KeyStrategy} that delegates to the selector function
84
+ *
85
+ * @example
86
+ * ```typescript
87
+ * // Fetch key from environment based on current mode
88
+ * const envKey = dynamicKey(() => {
89
+ * return process.env.NODE_ENV === 'production'
90
+ * ? process.env.PROD_API_KEY!
91
+ * : process.env.DEV_API_KEY!;
92
+ * });
93
+ *
94
+ * // Async key fetching from a secrets manager
95
+ * const vaultKey = dynamicKey(async () => {
96
+ * const secret = await vault.read('secret/openai');
97
+ * return secret.data.apiKey;
98
+ * });
99
+ *
100
+ * // Time-based key rotation
101
+ * const timedKey = dynamicKey(() => {
102
+ * const hour = new Date().getHours();
103
+ * return hour < 12 ? morningKey : afternoonKey;
104
+ * });
105
+ * ```
106
+ */
107
+ declare function dynamicKey(selector: () => string | Promise<string>): KeyStrategy;
108
+ /**
109
+ * Masks an API key for safe logging.
110
+ * Shows first 4 and last 4 characters with ellipsis, or '***' for short keys.
111
+ *
112
+ * @param key - The API key to mask
113
+ * @returns Masked key like "sk-ab...yz12" or "***" for short keys
114
+ *
115
+ * @example
116
+ * ```typescript
117
+ * maskApiKey('sk-abc123def456xyz789'); // 'sk-a...z789'
118
+ * maskApiKey('short'); // '***'
119
+ * ```
120
+ */
121
+ declare function maskApiKey(key: string): string;
122
+ /**
123
+ * Resolves an API key from provider configuration with multiple fallback options.
124
+ *
125
+ * This function handles various key specification methods in priority order:
126
+ * 1. Direct string key in config.apiKey
127
+ * 2. Function returning a key (sync or async) in config.apiKey
128
+ * 3. KeyStrategy instance in config.apiKey (roundRobinKeys, weightedKeys, dynamicKey)
129
+ * 4. Environment variable fallback (if envVar parameter is provided)
130
+ *
131
+ * @param config - Provider configuration containing the apiKey option
132
+ * @param envVar - Optional environment variable name to check as fallback
133
+ * @param provider - Provider identifier for error context (default: 'unknown')
134
+ * @param modality - Request modality for error context (default: 'llm')
135
+ * @returns The resolved API key string
136
+ *
137
+ * @throws {UPPError} AUTHENTICATION_FAILED - When no valid key is found
138
+ *
139
+ * @example
140
+ * ```typescript
141
+ * // Direct key in config
142
+ * const key1 = await resolveApiKey({ apiKey: 'sk-...' }, 'OPENAI_API_KEY', 'openai');
143
+ *
144
+ * // Function-based key
145
+ * const key2 = await resolveApiKey({ apiKey: () => getKeyFromVault() }, undefined, 'anthropic');
146
+ *
147
+ * // KeyStrategy instance
148
+ * const key3 = await resolveApiKey({
149
+ * apiKey: roundRobinKeys(['sk-1', 'sk-2', 'sk-3'])
150
+ * }, 'OPENAI_API_KEY', 'openai');
151
+ *
152
+ * // Environment variable fallback
153
+ * const key4 = await resolveApiKey({}, 'ANTHROPIC_API_KEY', 'anthropic');
154
+ * ```
155
+ */
156
+ declare function resolveApiKey(config: ProviderConfig, envVar?: string, provider?: string, modality?: Modality): Promise<string>;
157
+
158
+ /**
159
+ * Retry strategy factories for handling transient failures in HTTP requests.
160
+ *
161
+ * All strategies use the factory pattern to ensure each request gets an
162
+ * isolated instance, preventing state sharing between concurrent requests.
163
+ *
164
+ * @module http/retry
165
+ */
166
+
167
+ /**
168
+ * Options for exponential backoff retry strategy.
169
+ */
170
+ interface ExponentialBackoffOptions {
171
+ /** Maximum number of retry attempts (default: 3) */
172
+ maxAttempts?: number;
173
+ /** Initial delay in milliseconds (default: 1000) */
174
+ baseDelay?: number;
175
+ /** Maximum delay cap in milliseconds (default: 30000) */
176
+ maxDelay?: number;
177
+ /** Whether to add random jitter to delays (default: true) */
178
+ jitter?: boolean;
179
+ }
180
+ /**
181
+ * Creates an exponential backoff retry strategy.
182
+ *
183
+ * The delay between retries doubles with each attempt, helping to:
184
+ * - Avoid overwhelming servers during outages
185
+ * - Reduce thundering herd effects when many clients retry simultaneously
186
+ * - Give transient issues time to resolve
187
+ *
188
+ * Delay formula: min(baseDelay * 2^(attempt-1), maxDelay)
189
+ * With jitter: delay * random(0.5, 1.0)
190
+ *
191
+ * Only retries on transient errors: RATE_LIMITED, NETWORK_ERROR, TIMEOUT, PROVIDER_ERROR
192
+ *
193
+ * @param options - Configuration options
194
+ * @returns A factory that creates fresh strategy instances per request
195
+ *
196
+ * @example
197
+ * ```typescript
198
+ * // Default configuration (3 retries, 1s base, 30s max, jitter enabled)
199
+ * const retry = exponentialBackoff();
200
+ *
201
+ * // Custom configuration
202
+ * const customRetry = exponentialBackoff({
203
+ * maxAttempts: 5, // Up to 5 retry attempts
204
+ * baseDelay: 500, // Start with 500ms delay
205
+ * maxDelay: 60000, // Cap at 60 seconds
206
+ * jitter: false // Disable random jitter
207
+ * });
208
+ *
209
+ * // Use with provider
210
+ * const provider = createOpenAI({
211
+ * retryStrategy: customRetry
212
+ * });
213
+ * ```
214
+ */
215
+ declare function exponentialBackoff(options?: ExponentialBackoffOptions): RetryStrategyFactory;
216
+ /**
217
+ * Options for linear backoff retry strategy.
218
+ */
219
+ interface LinearBackoffOptions {
220
+ /** Maximum number of retry attempts (default: 3) */
221
+ maxAttempts?: number;
222
+ /** Base delay multiplier in milliseconds (default: 1000) */
223
+ delay?: number;
224
+ }
225
+ /**
226
+ * Creates a linear backoff retry strategy.
227
+ *
228
+ * Unlike exponential backoff, linear backoff increases delays at a constant rate:
229
+ * - Attempt 1: delay * 1 (e.g., 1000ms)
230
+ * - Attempt 2: delay * 2 (e.g., 2000ms)
231
+ * - Attempt 3: delay * 3 (e.g., 3000ms)
232
+ *
233
+ * This strategy is simpler and more predictable than exponential backoff,
234
+ * suitable for scenarios where gradual delay increase is preferred over
235
+ * aggressive backoff.
236
+ *
237
+ * Only retries on transient errors: RATE_LIMITED, NETWORK_ERROR, TIMEOUT, PROVIDER_ERROR
238
+ *
239
+ * @param options - Configuration options
240
+ * @returns A factory that creates fresh strategy instances per request
241
+ *
242
+ * @example
243
+ * ```typescript
244
+ * // Default configuration (3 retries, 1s delay increment)
245
+ * const retry = linearBackoff();
246
+ *
247
+ * // Custom configuration
248
+ * const customRetry = linearBackoff({
249
+ * maxAttempts: 4, // Up to 4 retry attempts
250
+ * delay: 2000 // 2s, 4s, 6s, 8s delays
251
+ * });
252
+ *
253
+ * // Use with provider
254
+ * const provider = createAnthropic({
255
+ * retryStrategy: customRetry
256
+ * });
257
+ * ```
258
+ */
259
+ declare function linearBackoff(options?: LinearBackoffOptions): RetryStrategyFactory;
260
+ /**
261
+ * Creates a no-retry strategy that fails immediately on any error.
262
+ *
263
+ * Use this strategy when:
264
+ * - Retries are handled at a higher level in your application
265
+ * - You want immediate failure feedback
266
+ * - The operation is not idempotent
267
+ * - Time sensitivity requires fast failure
268
+ *
269
+ * @returns A factory that creates no-retry strategy instances
270
+ *
271
+ * @example
272
+ * ```typescript
273
+ * // Disable retries for time-sensitive operations
274
+ * const provider = createOpenAI({
275
+ * retryStrategy: noRetry()
276
+ * });
277
+ * ```
278
+ */
279
+ declare function noRetry(): RetryStrategyFactory;
280
+ /**
281
+ * Options for retry-after strategy.
282
+ */
283
+ interface RetryAfterStrategyOptions {
284
+ /** Maximum number of retry attempts (default: 3) */
285
+ maxAttempts?: number;
286
+ /** Delay in ms when no Retry-After header is present (default: 5000) */
287
+ fallbackDelay?: number;
288
+ }
289
+ /**
290
+ * Creates a retry strategy that respects server-provided Retry-After headers.
291
+ *
292
+ * When servers return a 429 (Too Many Requests) response, they often include
293
+ * a Retry-After header indicating when the client should retry. This strategy
294
+ * uses that information for precise retry timing.
295
+ *
296
+ * Benefits over fixed backoff strategies:
297
+ * - Follows server recommendations for optimal retry timing
298
+ * - Avoids retrying too early and wasting requests
299
+ * - Adapts to dynamic rate limit windows
300
+ *
301
+ * If no Retry-After header is provided, falls back to a configurable delay.
302
+ * Only retries on RATE_LIMITED errors.
303
+ *
304
+ * @param options - Configuration options
305
+ * @returns A factory that creates fresh strategy instances per request
306
+ *
307
+ * @example
308
+ * ```typescript
309
+ * // Use server-recommended retry timing
310
+ * const retryAfter = retryAfterStrategy({
311
+ * maxAttempts: 5, // Retry up to 5 times
312
+ * fallbackDelay: 10000 // 10s fallback if no header
313
+ * });
314
+ *
315
+ * const provider = createOpenAI({
316
+ * retryStrategy: retryAfter
317
+ * });
318
+ * ```
319
+ */
320
+ declare function retryAfterStrategy(options?: RetryAfterStrategyOptions): RetryStrategyFactory;
321
+
322
+ export { type ExponentialBackoffOptions as E, type LinearBackoffOptions as L, type RetryAfterStrategyOptions as R, roundRobinKeys as a, retryAfterStrategy as b, dynamicKey as d, exponentialBackoff as e, linearBackoff as l, maskApiKey as m, noRetry as n, resolveApiKey as r, weightedKeys as w };