@providerprotocol/ai 0.0.12 → 0.0.14

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 (44) hide show
  1. package/dist/anthropic/index.d.ts +51 -15
  2. package/dist/anthropic/index.js +80 -29
  3. package/dist/anthropic/index.js.map +1 -1
  4. package/dist/{chunk-SUNYWHTH.js → chunk-MOU4U3PO.js} +55 -3
  5. package/dist/chunk-MOU4U3PO.js.map +1 -0
  6. package/dist/{chunk-Y6Q7JCNP.js → chunk-MSR5P65T.js} +1 -1
  7. package/dist/chunk-MSR5P65T.js.map +1 -0
  8. package/dist/{chunk-W4BB4BG2.js → chunk-SVYROCLD.js} +31 -11
  9. package/dist/chunk-SVYROCLD.js.map +1 -0
  10. package/dist/chunk-U4JJC2YX.js +234 -0
  11. package/dist/chunk-U4JJC2YX.js.map +1 -0
  12. package/dist/{chunk-X5G4EHL7.js → chunk-Z7RBRCRN.js} +1 -1
  13. package/dist/chunk-Z7RBRCRN.js.map +1 -0
  14. package/dist/google/index.d.ts +376 -7
  15. package/dist/google/index.js +149 -21
  16. package/dist/google/index.js.map +1 -1
  17. package/dist/http/index.d.ts +222 -25
  18. package/dist/http/index.js +3 -3
  19. package/dist/index.d.ts +1484 -198
  20. package/dist/index.js +233 -47
  21. package/dist/index.js.map +1 -1
  22. package/dist/ollama/index.d.ts +92 -20
  23. package/dist/ollama/index.js +31 -7
  24. package/dist/ollama/index.js.map +1 -1
  25. package/dist/openai/index.d.ts +340 -61
  26. package/dist/openai/index.js +105 -31
  27. package/dist/openai/index.js.map +1 -1
  28. package/dist/openrouter/index.d.ts +107 -51
  29. package/dist/openrouter/index.js +84 -24
  30. package/dist/openrouter/index.js.map +1 -1
  31. package/dist/provider-Bi0nyNhA.d.ts +505 -0
  32. package/dist/retry-BatS2hjD.d.ts +508 -0
  33. package/dist/xai/index.d.ts +97 -22
  34. package/dist/xai/index.js +129 -45
  35. package/dist/xai/index.js.map +1 -1
  36. package/package.json +8 -3
  37. package/dist/chunk-CUCRF5W6.js +0 -136
  38. package/dist/chunk-CUCRF5W6.js.map +0 -1
  39. package/dist/chunk-SUNYWHTH.js.map +0 -1
  40. package/dist/chunk-W4BB4BG2.js.map +0 -1
  41. package/dist/chunk-X5G4EHL7.js.map +0 -1
  42. package/dist/chunk-Y6Q7JCNP.js.map +0 -1
  43. package/dist/provider-CUJWjgNl.d.ts +0 -192
  44. package/dist/retry-I2661_rv.d.ts +0 -118
@@ -0,0 +1,508 @@
1
+ import { K as KeyStrategy, P as ProviderConfig, d as Modality, R as RetryStrategy, U as UPPError } from './provider-Bi0nyNhA.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
+ * Resolves an API key from provider configuration with multiple fallback options.
152
+ *
153
+ * This function handles various key specification methods in priority order:
154
+ * 1. Direct string key in config.apiKey
155
+ * 2. Function returning a key (sync or async) in config.apiKey
156
+ * 3. KeyStrategy instance in config.apiKey (RoundRobinKeys, WeightedKeys, DynamicKey)
157
+ * 4. Environment variable fallback (if envVar parameter is provided)
158
+ *
159
+ * @param config - Provider configuration containing the apiKey option
160
+ * @param envVar - Optional environment variable name to check as fallback
161
+ * @param provider - Provider identifier for error context (default: 'unknown')
162
+ * @param modality - Request modality for error context (default: 'llm')
163
+ * @returns The resolved API key string
164
+ *
165
+ * @throws {UPPError} AUTHENTICATION_FAILED - When no valid key is found
166
+ *
167
+ * @example
168
+ * ```typescript
169
+ * // Direct key in config
170
+ * const key1 = await resolveApiKey({ apiKey: 'sk-...' }, 'OPENAI_API_KEY', 'openai');
171
+ *
172
+ * // Function-based key
173
+ * const key2 = await resolveApiKey({ apiKey: () => getKeyFromVault() }, undefined, 'anthropic');
174
+ *
175
+ * // KeyStrategy instance
176
+ * const key3 = await resolveApiKey({
177
+ * apiKey: new RoundRobinKeys(['sk-1', 'sk-2', 'sk-3'])
178
+ * }, 'OPENAI_API_KEY', 'openai');
179
+ *
180
+ * // Environment variable fallback
181
+ * const key4 = await resolveApiKey({}, 'ANTHROPIC_API_KEY', 'anthropic');
182
+ * ```
183
+ */
184
+ declare function resolveApiKey(config: ProviderConfig, envVar?: string, provider?: string, modality?: Modality): Promise<string>;
185
+
186
+ /**
187
+ * Retry strategies for handling transient failures in HTTP requests.
188
+ * @module http/retry
189
+ */
190
+
191
+ /**
192
+ * Implements exponential backoff with optional jitter for retry delays.
193
+ *
194
+ * The delay between retries doubles with each attempt, helping to:
195
+ * - Avoid overwhelming servers during outages
196
+ * - Reduce thundering herd effects when many clients retry simultaneously
197
+ * - Give transient issues time to resolve
198
+ *
199
+ * Delay formula: min(baseDelay * 2^(attempt-1), maxDelay)
200
+ * With jitter: delay * random(0.5, 1.0)
201
+ *
202
+ * Only retries on transient errors: RATE_LIMITED, NETWORK_ERROR, TIMEOUT, PROVIDER_ERROR
203
+ *
204
+ * @implements {RetryStrategy}
205
+ *
206
+ * @example
207
+ * ```typescript
208
+ * // Default configuration (3 retries, 1s base, 30s max, jitter enabled)
209
+ * const retry = new ExponentialBackoff();
210
+ *
211
+ * // Custom configuration
212
+ * const customRetry = new ExponentialBackoff({
213
+ * maxAttempts: 5, // Up to 5 retry attempts
214
+ * baseDelay: 500, // Start with 500ms delay
215
+ * maxDelay: 60000, // Cap at 60 seconds
216
+ * jitter: false // Disable random jitter
217
+ * });
218
+ *
219
+ * // Use with provider
220
+ * const provider = createOpenAI({
221
+ * retryStrategy: customRetry
222
+ * });
223
+ * ```
224
+ */
225
+ declare class ExponentialBackoff implements RetryStrategy {
226
+ private maxAttempts;
227
+ private baseDelay;
228
+ private maxDelay;
229
+ private jitter;
230
+ /**
231
+ * Creates a new ExponentialBackoff instance.
232
+ *
233
+ * @param options - Configuration options
234
+ * @param options.maxAttempts - Maximum number of retry attempts (default: 3)
235
+ * @param options.baseDelay - Initial delay in milliseconds (default: 1000)
236
+ * @param options.maxDelay - Maximum delay cap in milliseconds (default: 30000)
237
+ * @param options.jitter - Whether to add random jitter to delays (default: true)
238
+ */
239
+ constructor(options?: {
240
+ maxAttempts?: number;
241
+ baseDelay?: number;
242
+ maxDelay?: number;
243
+ jitter?: boolean;
244
+ });
245
+ /**
246
+ * Determines whether to retry and calculates the delay.
247
+ *
248
+ * @param error - The error that triggered the retry
249
+ * @param attempt - Current attempt number (1-indexed)
250
+ * @returns Delay in milliseconds before next retry, or null to stop retrying
251
+ */
252
+ onRetry(error: UPPError, attempt: number): number | null;
253
+ /**
254
+ * Checks if an error is eligible for retry.
255
+ *
256
+ * @param error - The error to evaluate
257
+ * @returns True if the error is transient and retryable
258
+ */
259
+ private isRetryable;
260
+ }
261
+ /**
262
+ * Implements linear backoff where delays increase proportionally with each attempt.
263
+ *
264
+ * Unlike exponential backoff, linear backoff increases delays at a constant rate:
265
+ * - Attempt 1: delay * 1 (e.g., 1000ms)
266
+ * - Attempt 2: delay * 2 (e.g., 2000ms)
267
+ * - Attempt 3: delay * 3 (e.g., 3000ms)
268
+ *
269
+ * This strategy is simpler and more predictable than exponential backoff,
270
+ * suitable for scenarios where gradual delay increase is preferred over
271
+ * aggressive backoff.
272
+ *
273
+ * Only retries on transient errors: RATE_LIMITED, NETWORK_ERROR, TIMEOUT, PROVIDER_ERROR
274
+ *
275
+ * @implements {RetryStrategy}
276
+ *
277
+ * @example
278
+ * ```typescript
279
+ * // Default configuration (3 retries, 1s delay increment)
280
+ * const retry = new LinearBackoff();
281
+ *
282
+ * // Custom configuration
283
+ * const customRetry = new LinearBackoff({
284
+ * maxAttempts: 4, // Up to 4 retry attempts
285
+ * delay: 2000 // 2s, 4s, 6s, 8s delays
286
+ * });
287
+ *
288
+ * // Use with provider
289
+ * const provider = createAnthropic({
290
+ * retryStrategy: customRetry
291
+ * });
292
+ * ```
293
+ */
294
+ declare class LinearBackoff implements RetryStrategy {
295
+ private maxAttempts;
296
+ private delay;
297
+ /**
298
+ * Creates a new LinearBackoff instance.
299
+ *
300
+ * @param options - Configuration options
301
+ * @param options.maxAttempts - Maximum number of retry attempts (default: 3)
302
+ * @param options.delay - Base delay multiplier in milliseconds (default: 1000)
303
+ */
304
+ constructor(options?: {
305
+ maxAttempts?: number;
306
+ delay?: number;
307
+ });
308
+ /**
309
+ * Determines whether to retry and calculates the linear delay.
310
+ *
311
+ * @param error - The error that triggered the retry
312
+ * @param attempt - Current attempt number (1-indexed)
313
+ * @returns Delay in milliseconds (delay * attempt), or null to stop retrying
314
+ */
315
+ onRetry(error: UPPError, attempt: number): number | null;
316
+ /**
317
+ * Checks if an error is eligible for retry.
318
+ *
319
+ * @param error - The error to evaluate
320
+ * @returns True if the error is transient and retryable
321
+ */
322
+ private isRetryable;
323
+ }
324
+ /**
325
+ * Disables all retry behavior, failing immediately on any error.
326
+ *
327
+ * Use this strategy when:
328
+ * - Retries are handled at a higher level in your application
329
+ * - You want immediate failure feedback
330
+ * - The operation is not idempotent
331
+ * - Time sensitivity requires fast failure
332
+ *
333
+ * @implements {RetryStrategy}
334
+ *
335
+ * @example
336
+ * ```typescript
337
+ * // Disable retries for time-sensitive operations
338
+ * const provider = createOpenAI({
339
+ * retryStrategy: new NoRetry()
340
+ * });
341
+ * ```
342
+ */
343
+ declare class NoRetry implements RetryStrategy {
344
+ /**
345
+ * Always returns null to indicate no retry should be attempted.
346
+ *
347
+ * @returns Always returns null
348
+ */
349
+ onRetry(): null;
350
+ }
351
+ /**
352
+ * Implements token bucket rate limiting with automatic refill.
353
+ *
354
+ * The token bucket algorithm provides smooth rate limiting by:
355
+ * - Maintaining a bucket of tokens that replenish over time
356
+ * - Consuming one token per request
357
+ * - Delaying requests when the bucket is empty
358
+ * - Allowing burst traffic up to the bucket capacity
359
+ *
360
+ * This is particularly useful for:
361
+ * - Client-side rate limiting to avoid hitting API rate limits
362
+ * - Smoothing request patterns to maintain consistent throughput
363
+ * - Preventing accidental API abuse
364
+ *
365
+ * Unlike other retry strategies, TokenBucket implements {@link beforeRequest}
366
+ * to proactively delay requests before they are made.
367
+ *
368
+ * @implements {RetryStrategy}
369
+ *
370
+ * @example
371
+ * ```typescript
372
+ * // Allow 10 requests burst, refill 1 token per second
373
+ * const bucket = new TokenBucket({
374
+ * maxTokens: 10, // Burst capacity
375
+ * refillRate: 1, // Tokens per second
376
+ * maxAttempts: 3 // Retry attempts on rate limit
377
+ * });
378
+ *
379
+ * // Aggressive rate limiting: 5 req/s sustained
380
+ * const strictBucket = new TokenBucket({
381
+ * maxTokens: 5,
382
+ * refillRate: 5
383
+ * });
384
+ *
385
+ * // Use with provider
386
+ * const provider = createOpenAI({
387
+ * retryStrategy: bucket
388
+ * });
389
+ * ```
390
+ */
391
+ declare class TokenBucket implements RetryStrategy {
392
+ private tokens;
393
+ private maxTokens;
394
+ private refillRate;
395
+ private lastRefill;
396
+ private maxAttempts;
397
+ /**
398
+ * Creates a new TokenBucket instance.
399
+ *
400
+ * @param options - Configuration options
401
+ * @param options.maxTokens - Maximum bucket capacity (default: 10)
402
+ * @param options.refillRate - Tokens added per second (default: 1)
403
+ * @param options.maxAttempts - Maximum retry attempts on rate limit (default: 3)
404
+ */
405
+ constructor(options?: {
406
+ maxTokens?: number;
407
+ refillRate?: number;
408
+ maxAttempts?: number;
409
+ });
410
+ /**
411
+ * Called before each request to consume a token or calculate wait time.
412
+ *
413
+ * Refills the bucket based on elapsed time, then either:
414
+ * - Returns 0 if a token is available (consumed immediately)
415
+ * - Returns the wait time in milliseconds until the next token
416
+ *
417
+ * @returns Delay in milliseconds before the request can proceed
418
+ */
419
+ beforeRequest(): number;
420
+ /**
421
+ * Handles retry logic for rate-limited requests.
422
+ *
423
+ * Only retries on RATE_LIMITED errors, waiting for bucket refill.
424
+ *
425
+ * @param error - The error that triggered the retry
426
+ * @param attempt - Current attempt number (1-indexed)
427
+ * @returns Delay in milliseconds (time for 2 tokens), or null to stop
428
+ */
429
+ onRetry(error: UPPError, attempt: number): number | null;
430
+ /**
431
+ * Resets the bucket to full capacity.
432
+ *
433
+ * Called automatically on successful requests to restore available tokens.
434
+ */
435
+ reset(): void;
436
+ /**
437
+ * Refills the bucket based on elapsed time since last refill.
438
+ */
439
+ private refill;
440
+ }
441
+ /**
442
+ * Respects server-provided Retry-After headers for optimal retry timing.
443
+ *
444
+ * When servers return a 429 (Too Many Requests) response, they often include
445
+ * a Retry-After header indicating when the client should retry. This strategy
446
+ * uses that information for precise retry timing.
447
+ *
448
+ * Benefits over fixed backoff strategies:
449
+ * - Follows server recommendations for optimal retry timing
450
+ * - Avoids retrying too early and wasting requests
451
+ * - Adapts to dynamic rate limit windows
452
+ *
453
+ * If no Retry-After header is provided, falls back to a configurable delay.
454
+ * Only retries on RATE_LIMITED errors.
455
+ *
456
+ * @implements {RetryStrategy}
457
+ *
458
+ * @example
459
+ * ```typescript
460
+ * // Use server-recommended retry timing
461
+ * const retryAfter = new RetryAfterStrategy({
462
+ * maxAttempts: 5, // Retry up to 5 times
463
+ * fallbackDelay: 10000 // 10s fallback if no header
464
+ * });
465
+ *
466
+ * // The doFetch function automatically calls setRetryAfter
467
+ * // when a Retry-After header is present in the response
468
+ *
469
+ * const provider = createOpenAI({
470
+ * retryStrategy: retryAfter
471
+ * });
472
+ * ```
473
+ */
474
+ declare class RetryAfterStrategy implements RetryStrategy {
475
+ private maxAttempts;
476
+ private fallbackDelay;
477
+ private lastRetryAfter?;
478
+ /**
479
+ * Creates a new RetryAfterStrategy instance.
480
+ *
481
+ * @param options - Configuration options
482
+ * @param options.maxAttempts - Maximum number of retry attempts (default: 3)
483
+ * @param options.fallbackDelay - Delay in ms when no Retry-After header (default: 5000)
484
+ */
485
+ constructor(options?: {
486
+ maxAttempts?: number;
487
+ fallbackDelay?: number;
488
+ });
489
+ /**
490
+ * Sets the retry delay from a Retry-After header value.
491
+ *
492
+ * Called by doFetch when a Retry-After header is present in the response.
493
+ * The value is used for the next onRetry call and then cleared.
494
+ *
495
+ * @param seconds - The Retry-After value in seconds
496
+ */
497
+ setRetryAfter(seconds: number): void;
498
+ /**
499
+ * Determines retry delay using Retry-After header or fallback.
500
+ *
501
+ * @param error - The error that triggered the retry
502
+ * @param attempt - Current attempt number (1-indexed)
503
+ * @returns Delay from Retry-After header or fallback, null to stop
504
+ */
505
+ onRetry(error: UPPError, attempt: number): number | null;
506
+ }
507
+
508
+ 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, resolveApiKey as r };
@@ -1,8 +1,23 @@
1
- import { b as Provider, M as ModelReference, a as LLMHandler } from '../provider-CUJWjgNl.js';
1
+ import { b as Provider, M as ModelReference, a as LLMHandler } from '../provider-Bi0nyNhA.js';
2
2
 
3
3
  /**
4
- * xAI Chat Completions API parameters (OpenAI-compatible)
5
- * These are passed through to the /v1/chat/completions endpoint
4
+ * xAI Chat Completions API parameters (OpenAI-compatible).
5
+ *
6
+ * These parameters are passed through to the xAI `/v1/chat/completions` endpoint.
7
+ * The API is fully compatible with OpenAI's Chat Completions API, allowing seamless
8
+ * migration between providers.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const params: XAICompletionsParams = {
13
+ * max_tokens: 1000,
14
+ * temperature: 0.7,
15
+ * reasoning_effort: 'high', // Grok 3 Mini only
16
+ * };
17
+ * ```
18
+ *
19
+ * @see {@link XAIResponsesParams} for Responses API parameters
20
+ * @see {@link XAIMessagesParams} for Messages API parameters
6
21
  */
7
22
  interface XAICompletionsParams {
8
23
  /** Maximum number of tokens to generate */
@@ -51,8 +66,24 @@ interface XAICompletionsParams {
51
66
  search_parameters?: XAISearchParameters;
52
67
  }
53
68
  /**
54
- * xAI Responses API parameters (OpenAI Responses-compatible)
55
- * These are passed through to the /v1/responses endpoint
69
+ * xAI Responses API parameters (OpenAI Responses-compatible).
70
+ *
71
+ * These parameters are passed through to the xAI `/v1/responses` endpoint.
72
+ * The Responses API provides stateful conversation support with features like
73
+ * `previous_response_id` for continuing conversations across requests.
74
+ *
75
+ * @example
76
+ * ```typescript
77
+ * const params: XAIResponsesParams = {
78
+ * max_output_tokens: 1000,
79
+ * temperature: 0.7,
80
+ * store: true, // Enable stateful storage
81
+ * previous_response_id: 'resp_123...', // Continue previous conversation
82
+ * };
83
+ * ```
84
+ *
85
+ * @see {@link XAICompletionsParams} for Chat Completions API parameters
86
+ * @see {@link XAIMessagesParams} for Messages API parameters
56
87
  */
57
88
  interface XAIResponsesParams {
58
89
  /** Maximum output tokens */
@@ -88,8 +119,23 @@ interface XAIResponsesParams {
88
119
  search_parameters?: XAISearchParameters;
89
120
  }
90
121
  /**
91
- * xAI Messages API parameters (Anthropic-compatible)
92
- * These are passed through to the /v1/messages endpoint
122
+ * xAI Messages API parameters (Anthropic-compatible).
123
+ *
124
+ * These parameters are passed through to the xAI `/v1/messages` endpoint.
125
+ * The API is compatible with Anthropic's Messages API, allowing developers
126
+ * migrating from Anthropic to use familiar patterns.
127
+ *
128
+ * @example
129
+ * ```typescript
130
+ * const params: XAIMessagesParams = {
131
+ * max_tokens: 1000,
132
+ * temperature: 0.7,
133
+ * thinking: { type: 'enabled', budget_tokens: 500 }, // Extended thinking
134
+ * };
135
+ * ```
136
+ *
137
+ * @see {@link XAICompletionsParams} for Chat Completions API parameters
138
+ * @see {@link XAIResponsesParams} for Responses API parameters
93
139
  */
94
140
  interface XAIMessagesParams {
95
141
  /** Maximum number of tokens to generate */
@@ -113,32 +159,42 @@ interface XAIMessagesParams {
113
159
  };
114
160
  }
115
161
  /**
116
- * API mode for xAI provider
162
+ * API mode selector for the xAI provider.
163
+ *
164
+ * xAI supports three distinct API modes, each with different capabilities:
165
+ * - `completions`: OpenAI Chat Completions compatible (recommended, default)
166
+ * - `responses`: OpenAI Responses compatible with stateful conversations
167
+ * - `messages`: Anthropic Messages compatible for easy migration
117
168
  */
118
169
  type XAIAPIMode = 'completions' | 'responses' | 'messages';
119
170
  /**
120
- * Model options when creating a model reference
171
+ * Options for configuring an xAI model reference.
121
172
  */
122
173
  interface XAIModelOptions {
123
- /** Which API to use */
174
+ /** The API mode to use for this model */
124
175
  api?: XAIAPIMode;
125
176
  }
126
177
  /**
127
- * Model reference with xAI-specific options
178
+ * A reference to an xAI model with optional configuration.
128
179
  */
129
180
  interface XAIModelReference {
181
+ /** The xAI model identifier (e.g., 'grok-4', 'grok-3-mini') */
130
182
  modelId: string;
183
+ /** Optional model-specific options */
131
184
  options?: XAIModelOptions;
132
185
  }
133
186
  /**
134
- * xAI provider configuration
187
+ * Configuration options for the xAI provider.
135
188
  */
136
189
  interface XAIConfig {
137
- /** Which API to use: 'completions', 'responses', or 'messages' */
190
+ /** The API mode to use (defaults to 'completions') */
138
191
  api?: XAIAPIMode;
139
192
  }
140
193
  /**
141
- * Live Search parameters (deprecated)
194
+ * Live Search parameters for real-time web search integration.
195
+ *
196
+ * @deprecated Live Search API will be removed on December 15, 2025.
197
+ * Use the Agent Tools API with `web_search` tool instead.
142
198
  */
143
199
  interface XAISearchParameters {
144
200
  /** Search mode */
@@ -153,13 +209,26 @@ interface XAISearchParameters {
153
209
  max_search_results?: number;
154
210
  }
155
211
  /**
156
- * Server-side agentic tools
212
+ * Server-side agentic tool configuration.
213
+ *
214
+ * These tools run on xAI's servers and provide capabilities like
215
+ * web search, X (Twitter) search, and code execution.
216
+ *
217
+ * @example
218
+ * ```typescript
219
+ * const tool: XAIAgentTool = { type: 'web_search' };
220
+ * ```
157
221
  */
158
222
  interface XAIAgentTool {
223
+ /** The type of server-side tool to enable */
159
224
  type: 'web_search' | 'x_search' | 'code_execution';
160
225
  }
161
226
  /**
162
- * Response format
227
+ * Specifies the output format for structured responses.
228
+ *
229
+ * - `{ type: 'text' }`: Plain text output (default)
230
+ * - `{ type: 'json_object' }`: JSON output with flexible schema
231
+ * - `{ type: 'json_schema', ... }`: JSON output with strict schema validation
163
232
  */
164
233
  type XAIResponseFormat = {
165
234
  type: 'text';
@@ -175,17 +244,23 @@ type XAIResponseFormat = {
175
244
  };
176
245
  };
177
246
 
178
- /** Union type for modalities interface */
247
+ /**
248
+ * Union type for LLM parameters across all xAI API modes.
249
+ * This type enables the provider to handle parameters from any of the three APIs.
250
+ */
179
251
  type XAILLMParamsUnion = XAICompletionsParams | XAIResponsesParams | XAIMessagesParams;
180
252
  /**
181
- * xAI provider options
253
+ * Configuration options for creating xAI model references.
182
254
  */
183
255
  interface XAIProviderOptions {
184
256
  /**
185
- * Which API to use:
186
- * - 'completions': Chat Completions API (OpenAI-compatible, default)
187
- * - 'responses': Responses API (OpenAI Responses-compatible, stateful)
188
- * - 'messages': Messages API (Anthropic-compatible)
257
+ * The API mode to use for this model.
258
+ *
259
+ * - `'completions'`: Chat Completions API (OpenAI-compatible, default, recommended)
260
+ * - `'responses'`: Responses API (OpenAI Responses-compatible, supports stateful conversations)
261
+ * - `'messages'`: Messages API (Anthropic-compatible, for easy migration from Anthropic)
262
+ *
263
+ * @default 'completions'
189
264
  */
190
265
  api?: XAIAPIMode;
191
266
  }