@providerprotocol/ai 0.0.11 → 0.0.13

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 (104) hide show
  1. package/dist/anthropic/index.d.ts +51 -15
  2. package/dist/anthropic/index.js +54 -19
  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 +127 -15
  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 +1482 -198
  20. package/dist/index.js +233 -49
  21. package/dist/index.js.map +1 -1
  22. package/dist/ollama/index.d.ts +92 -20
  23. package/dist/ollama/index.js +17 -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 +57 -15
  27. package/dist/openai/index.js.map +1 -1
  28. package/dist/openrouter/index.d.ts +107 -51
  29. package/dist/openrouter/index.js +36 -8
  30. package/dist/openrouter/index.js.map +1 -1
  31. package/dist/provider-mKkz7Q9U.d.ts +488 -0
  32. package/dist/retry-Dh70lgr0.d.ts +508 -0
  33. package/dist/xai/index.d.ts +97 -22
  34. package/dist/xai/index.js +55 -19
  35. package/dist/xai/index.js.map +1 -1
  36. package/package.json +8 -12
  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
  45. package/src/anthropic/index.ts +0 -3
  46. package/src/core/image.ts +0 -188
  47. package/src/core/llm.ts +0 -650
  48. package/src/core/provider.ts +0 -92
  49. package/src/google/index.ts +0 -3
  50. package/src/http/errors.ts +0 -112
  51. package/src/http/fetch.ts +0 -210
  52. package/src/http/index.ts +0 -31
  53. package/src/http/keys.ts +0 -136
  54. package/src/http/retry.ts +0 -205
  55. package/src/http/sse.ts +0 -136
  56. package/src/index.ts +0 -32
  57. package/src/ollama/index.ts +0 -3
  58. package/src/openai/index.ts +0 -39
  59. package/src/openrouter/index.ts +0 -11
  60. package/src/providers/anthropic/index.ts +0 -17
  61. package/src/providers/anthropic/llm.ts +0 -196
  62. package/src/providers/anthropic/transform.ts +0 -434
  63. package/src/providers/anthropic/types.ts +0 -213
  64. package/src/providers/google/index.ts +0 -17
  65. package/src/providers/google/llm.ts +0 -203
  66. package/src/providers/google/transform.ts +0 -447
  67. package/src/providers/google/types.ts +0 -214
  68. package/src/providers/ollama/index.ts +0 -43
  69. package/src/providers/ollama/llm.ts +0 -272
  70. package/src/providers/ollama/transform.ts +0 -434
  71. package/src/providers/ollama/types.ts +0 -260
  72. package/src/providers/openai/index.ts +0 -186
  73. package/src/providers/openai/llm.completions.ts +0 -201
  74. package/src/providers/openai/llm.responses.ts +0 -211
  75. package/src/providers/openai/transform.completions.ts +0 -561
  76. package/src/providers/openai/transform.responses.ts +0 -708
  77. package/src/providers/openai/types.ts +0 -1249
  78. package/src/providers/openrouter/index.ts +0 -177
  79. package/src/providers/openrouter/llm.completions.ts +0 -201
  80. package/src/providers/openrouter/llm.responses.ts +0 -211
  81. package/src/providers/openrouter/transform.completions.ts +0 -538
  82. package/src/providers/openrouter/transform.responses.ts +0 -742
  83. package/src/providers/openrouter/types.ts +0 -717
  84. package/src/providers/xai/index.ts +0 -223
  85. package/src/providers/xai/llm.completions.ts +0 -201
  86. package/src/providers/xai/llm.messages.ts +0 -195
  87. package/src/providers/xai/llm.responses.ts +0 -211
  88. package/src/providers/xai/transform.completions.ts +0 -565
  89. package/src/providers/xai/transform.messages.ts +0 -448
  90. package/src/providers/xai/transform.responses.ts +0 -678
  91. package/src/providers/xai/types.ts +0 -938
  92. package/src/types/content.ts +0 -133
  93. package/src/types/errors.ts +0 -85
  94. package/src/types/index.ts +0 -105
  95. package/src/types/llm.ts +0 -211
  96. package/src/types/messages.ts +0 -205
  97. package/src/types/provider.ts +0 -195
  98. package/src/types/schema.ts +0 -58
  99. package/src/types/stream.ts +0 -188
  100. package/src/types/thread.ts +0 -226
  101. package/src/types/tool.ts +0 -88
  102. package/src/types/turn.ts +0 -118
  103. package/src/utils/id.ts +0 -28
  104. package/src/xai/index.ts +0 -41
@@ -0,0 +1,488 @@
1
+ /**
2
+ * @fileoverview Error types for the Unified Provider Protocol.
3
+ *
4
+ * Provides normalized error codes and a unified error class for handling
5
+ * errors across different AI providers in a consistent manner.
6
+ *
7
+ * @module types/errors
8
+ */
9
+ /**
10
+ * Normalized error codes for cross-provider error handling.
11
+ *
12
+ * These codes provide a consistent way to identify and handle errors
13
+ * regardless of which AI provider generated them.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * try {
18
+ * await llm.generate('Hello');
19
+ * } catch (error) {
20
+ * if (error instanceof UPPError) {
21
+ * switch (error.code) {
22
+ * case 'RATE_LIMITED':
23
+ * await delay(error.retryAfter);
24
+ * break;
25
+ * case 'AUTHENTICATION_FAILED':
26
+ * throw new Error('Invalid API key');
27
+ * }
28
+ * }
29
+ * }
30
+ * ```
31
+ */
32
+ type ErrorCode =
33
+ /** API key is invalid or expired */
34
+ 'AUTHENTICATION_FAILED'
35
+ /** Rate limit exceeded, retry after delay */
36
+ | 'RATE_LIMITED'
37
+ /** Input exceeds model's context window */
38
+ | 'CONTEXT_LENGTH_EXCEEDED'
39
+ /** Requested model does not exist */
40
+ | 'MODEL_NOT_FOUND'
41
+ /** Request parameters are malformed */
42
+ | 'INVALID_REQUEST'
43
+ /** Provider returned an unexpected response format */
44
+ | 'INVALID_RESPONSE'
45
+ /** Content was blocked by safety filters */
46
+ | 'CONTENT_FILTERED'
47
+ /** Account quota or credits exhausted */
48
+ | 'QUOTA_EXCEEDED'
49
+ /** Provider-specific error not covered by other codes */
50
+ | 'PROVIDER_ERROR'
51
+ /** Network connectivity issue */
52
+ | 'NETWORK_ERROR'
53
+ /** Request exceeded timeout limit */
54
+ | 'TIMEOUT'
55
+ /** Request was cancelled via AbortSignal */
56
+ | 'CANCELLED';
57
+ /**
58
+ * Modality types supported by UPP.
59
+ *
60
+ * Each modality represents a different type of AI capability that
61
+ * can be provided by a UPP-compatible provider.
62
+ */
63
+ type Modality =
64
+ /** Large language model for text generation */
65
+ 'llm'
66
+ /** Text/image embedding model */
67
+ | 'embedding'
68
+ /** Image generation model */
69
+ | 'image'
70
+ /** Audio processing/generation model */
71
+ | 'audio'
72
+ /** Video processing/generation model */
73
+ | 'video';
74
+ /**
75
+ * Unified Provider Protocol Error.
76
+ *
77
+ * All provider-specific errors are normalized to this type, providing
78
+ * a consistent interface for error handling across different AI providers.
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * throw new UPPError(
83
+ * 'API key is invalid',
84
+ * 'AUTHENTICATION_FAILED',
85
+ * 'openai',
86
+ * 'llm',
87
+ * 401
88
+ * );
89
+ * ```
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * // Wrapping a provider error
94
+ * try {
95
+ * await openai.chat.completions.create({ ... });
96
+ * } catch (err) {
97
+ * throw new UPPError(
98
+ * 'OpenAI request failed',
99
+ * 'PROVIDER_ERROR',
100
+ * 'openai',
101
+ * 'llm',
102
+ * err.status,
103
+ * err
104
+ * );
105
+ * }
106
+ * ```
107
+ */
108
+ declare class UPPError extends Error {
109
+ /** Normalized error code for programmatic handling */
110
+ readonly code: ErrorCode;
111
+ /** Name of the provider that generated the error */
112
+ readonly provider: string;
113
+ /** The modality that was being used when the error occurred */
114
+ readonly modality: Modality;
115
+ /** HTTP status code from the provider's response, if available */
116
+ readonly statusCode?: number;
117
+ /** The original error that caused this UPPError, if wrapping another error */
118
+ readonly cause?: Error;
119
+ /** Error class name, always 'UPPError' */
120
+ readonly name = "UPPError";
121
+ /**
122
+ * Creates a new UPPError instance.
123
+ *
124
+ * @param message - Human-readable error description
125
+ * @param code - Normalized error code for programmatic handling
126
+ * @param provider - Name of the provider that generated the error
127
+ * @param modality - The modality that was being used
128
+ * @param statusCode - HTTP status code from the provider's response
129
+ * @param cause - The original error being wrapped
130
+ */
131
+ constructor(message: string, code: ErrorCode, provider: string, modality: Modality, statusCode?: number, cause?: Error);
132
+ /**
133
+ * Creates a string representation of the error.
134
+ *
135
+ * @returns Formatted error string including code, message, provider, and modality
136
+ */
137
+ toString(): string;
138
+ /**
139
+ * Converts the error to a JSON-serializable object.
140
+ *
141
+ * @returns Plain object representation suitable for logging or transmission
142
+ */
143
+ toJSON(): Record<string, unknown>;
144
+ }
145
+
146
+ /**
147
+ * @fileoverview Provider types for AI service integrations.
148
+ *
149
+ * Defines the interfaces for provider factories, modality handlers,
150
+ * and configuration options for connecting to various AI providers.
151
+ *
152
+ * @module types/provider
153
+ */
154
+
155
+ /**
156
+ * API key strategy interface for managing multiple keys.
157
+ *
158
+ * Implement this interface to provide custom key rotation or
159
+ * selection logic when working with multiple API keys.
160
+ *
161
+ * @example
162
+ * ```typescript
163
+ * class RoundRobinKeys implements KeyStrategy {
164
+ * private keys: string[];
165
+ * private index = 0;
166
+ *
167
+ * constructor(keys: string[]) {
168
+ * this.keys = keys;
169
+ * }
170
+ *
171
+ * getKey(): string {
172
+ * const key = this.keys[this.index];
173
+ * this.index = (this.index + 1) % this.keys.length;
174
+ * return key;
175
+ * }
176
+ * }
177
+ * ```
178
+ */
179
+ interface KeyStrategy {
180
+ /**
181
+ * Gets the next API key to use for a request.
182
+ *
183
+ * @returns The API key string, or a Promise resolving to it
184
+ */
185
+ getKey(): string | Promise<string>;
186
+ }
187
+ /**
188
+ * Retry strategy interface for handling request failures.
189
+ *
190
+ * Implement this interface to provide custom retry logic for
191
+ * handling rate limits, transient errors, and other failures.
192
+ *
193
+ * @example
194
+ * ```typescript
195
+ * class ExponentialBackoff implements RetryStrategy {
196
+ * private maxAttempts = 5;
197
+ * private baseDelay = 1000;
198
+ *
199
+ * onRetry(error: UPPError, attempt: number): number | null {
200
+ * if (attempt > this.maxAttempts) return null;
201
+ * if (error.code !== 'RATE_LIMITED') return null;
202
+ * return this.baseDelay * Math.pow(2, attempt - 1);
203
+ * }
204
+ * }
205
+ * ```
206
+ */
207
+ interface RetryStrategy {
208
+ /**
209
+ * Called when a request fails with a retryable error.
210
+ *
211
+ * @param error - The error that occurred
212
+ * @param attempt - The attempt number (1 = first retry)
213
+ * @returns Delay in ms before retrying, or null to stop retrying
214
+ */
215
+ onRetry(error: UPPError, attempt: number): number | null | Promise<number | null>;
216
+ /**
217
+ * Called before each request. Can be used to implement pre-emptive rate limiting.
218
+ *
219
+ * @returns Delay in ms to wait before making the request, or 0 to proceed immediately
220
+ */
221
+ beforeRequest?(): number | Promise<number>;
222
+ /**
223
+ * Reset the strategy state (e.g., after a successful request).
224
+ */
225
+ reset?(): void;
226
+ }
227
+ /**
228
+ * Provider configuration for infrastructure and connection settings.
229
+ *
230
+ * These settings control how requests are made to the provider's API,
231
+ * including authentication, timeouts, and retry behavior.
232
+ *
233
+ * @example
234
+ * ```typescript
235
+ * const config: ProviderConfig = {
236
+ * apiKey: process.env.OPENAI_API_KEY,
237
+ * timeout: 30000,
238
+ * retryStrategy: new ExponentialBackoff()
239
+ * };
240
+ *
241
+ * // Or with a key strategy for key rotation
242
+ * const config: ProviderConfig = {
243
+ * apiKey: new RoundRobinKeys(['sk-1', 'sk-2', 'sk-3']),
244
+ * baseUrl: 'https://custom-proxy.example.com'
245
+ * };
246
+ * ```
247
+ */
248
+ interface ProviderConfig {
249
+ /**
250
+ * API key for authentication.
251
+ * Can be a string, async function, or KeyStrategy for advanced use cases.
252
+ */
253
+ apiKey?: string | (() => string | Promise<string>) | KeyStrategy;
254
+ /** Override the base API URL (for proxies, local models) */
255
+ baseUrl?: string;
256
+ /** Request timeout in milliseconds */
257
+ timeout?: number;
258
+ /** Custom fetch implementation (for logging, caching, custom TLS) */
259
+ fetch?: typeof fetch;
260
+ /** API version override (provider-specific) */
261
+ apiVersion?: string;
262
+ /** Retry strategy for handling failures and rate limits */
263
+ retryStrategy?: RetryStrategy;
264
+ }
265
+ /**
266
+ * A reference to a model, created by a provider factory.
267
+ *
268
+ * Model references are lightweight objects that identify a model
269
+ * and its provider, used as input to the llm() function.
270
+ *
271
+ * @typeParam TOptions - Provider-specific options type
272
+ *
273
+ * @example
274
+ * ```typescript
275
+ * const model = openai('gpt-4');
276
+ * console.log(model.modelId); // 'gpt-4'
277
+ * console.log(model.provider.name); // 'openai'
278
+ * ```
279
+ */
280
+ interface ModelReference<TOptions = unknown> {
281
+ /** The model identifier (e.g., 'gpt-4', 'claude-3-opus') */
282
+ readonly modelId: string;
283
+ /** The provider that created this reference */
284
+ readonly provider: Provider<TOptions>;
285
+ }
286
+ /**
287
+ * LLM handler interface for providers.
288
+ *
289
+ * Implemented by providers to enable language model capabilities.
290
+ *
291
+ * @typeParam TParams - Provider-specific parameter type
292
+ * @internal
293
+ */
294
+ interface LLMHandler<TParams = any> {
295
+ /**
296
+ * Binds a model ID to create an executable model instance.
297
+ *
298
+ * @param modelId - The model identifier to bind
299
+ * @returns A bound LLM model ready for inference
300
+ */
301
+ bind(modelId: string): BoundLLMModel<TParams>;
302
+ /**
303
+ * Sets the parent provider reference.
304
+ * Called by createProvider() after the provider is constructed.
305
+ *
306
+ * @param provider - The parent provider
307
+ * @internal
308
+ */
309
+ _setProvider?(provider: LLMProvider<TParams>): void;
310
+ }
311
+ /**
312
+ * Embedding handler interface for providers.
313
+ *
314
+ * Implemented by providers to enable embedding capabilities.
315
+ *
316
+ * @typeParam TParams - Provider-specific parameter type
317
+ * @internal
318
+ */
319
+ interface EmbeddingHandler<TParams = any> {
320
+ /** Supported input types for embeddings */
321
+ readonly supportedInputs: ('text' | 'image')[];
322
+ /**
323
+ * Binds a model ID to create an executable embedding model.
324
+ *
325
+ * @param modelId - The model identifier to bind
326
+ * @returns A bound embedding model ready for use
327
+ */
328
+ bind(modelId: string): BoundEmbeddingModel<TParams>;
329
+ /**
330
+ * Sets the parent provider reference.
331
+ *
332
+ * @param provider - The parent provider
333
+ * @internal
334
+ */
335
+ _setProvider?(provider: EmbeddingProvider<TParams>): void;
336
+ }
337
+ /**
338
+ * Image handler interface for providers.
339
+ *
340
+ * Implemented by providers to enable image generation capabilities.
341
+ *
342
+ * @typeParam TParams - Provider-specific parameter type
343
+ * @internal
344
+ */
345
+ interface ImageHandler<TParams = any> {
346
+ /**
347
+ * Binds a model ID to create an executable image model.
348
+ *
349
+ * @param modelId - The model identifier to bind
350
+ * @returns A bound image model ready for generation
351
+ */
352
+ bind(modelId: string): BoundImageModel<TParams>;
353
+ /**
354
+ * Sets the parent provider reference.
355
+ *
356
+ * @param provider - The parent provider
357
+ * @internal
358
+ */
359
+ _setProvider?(provider: ImageProvider<TParams>): void;
360
+ }
361
+ /**
362
+ * Bound LLM model interface (forward declaration).
363
+ *
364
+ * Full definition is in llm.ts to avoid circular dependencies.
365
+ *
366
+ * @typeParam TParams - Provider-specific parameter type
367
+ */
368
+ interface BoundLLMModel<TParams = any> {
369
+ /** The model identifier */
370
+ readonly modelId: string;
371
+ /** Reference to the parent provider */
372
+ readonly provider: LLMProvider<TParams>;
373
+ }
374
+ /**
375
+ * Bound embedding model interface.
376
+ *
377
+ * Represents an embedding model bound to a specific model ID,
378
+ * ready to generate embeddings.
379
+ *
380
+ * @typeParam TParams - Provider-specific parameter type
381
+ */
382
+ interface BoundEmbeddingModel<TParams = any> {
383
+ /** The model identifier */
384
+ readonly modelId: string;
385
+ /** Reference to the parent provider */
386
+ readonly provider: EmbeddingProvider<TParams>;
387
+ /** Maximum number of inputs per batch request */
388
+ readonly maxBatchSize: number;
389
+ /** Maximum length of input text in tokens */
390
+ readonly maxInputLength: number;
391
+ /** Output embedding dimensions */
392
+ readonly dimensions: number;
393
+ }
394
+ /**
395
+ * Bound image model interface.
396
+ *
397
+ * Represents an image generation model bound to a specific model ID.
398
+ *
399
+ * @typeParam TParams - Provider-specific parameter type
400
+ */
401
+ interface BoundImageModel<TParams = any> {
402
+ /** The model identifier */
403
+ readonly modelId: string;
404
+ /** Reference to the parent provider */
405
+ readonly provider: ImageProvider<TParams>;
406
+ }
407
+ /**
408
+ * Provider factory function with metadata and modality handlers.
409
+ *
410
+ * The Provider interface represents a callable function that creates
411
+ * model references, along with metadata and modality-specific handlers.
412
+ *
413
+ * @typeParam TOptions - Provider-specific options passed to the factory
414
+ *
415
+ * @example
416
+ * ```typescript
417
+ * // Using a provider
418
+ * const model = openai('gpt-4', { temperature: 0.7 });
419
+ *
420
+ * // Accessing provider metadata
421
+ * console.log(openai.name); // 'openai'
422
+ * console.log(openai.version); // '1.0.0'
423
+ *
424
+ * // Accessing modality handlers
425
+ * const llmHandler = openai.modalities.llm;
426
+ * ```
427
+ */
428
+ interface Provider<TOptions = unknown> {
429
+ /**
430
+ * Creates a model reference with optional provider-specific options.
431
+ *
432
+ * @param modelId - The model identifier
433
+ * @param options - Provider-specific options
434
+ * @returns A model reference for use with llm() or other functions
435
+ */
436
+ (modelId: string, options?: TOptions): ModelReference<TOptions>;
437
+ /** Provider name (e.g., 'openai', 'anthropic') */
438
+ readonly name: string;
439
+ /** Provider version string */
440
+ readonly version: string;
441
+ /** Supported modalities and their handlers */
442
+ readonly modalities: {
443
+ llm?: LLMHandler;
444
+ embedding?: EmbeddingHandler;
445
+ image?: ImageHandler;
446
+ };
447
+ }
448
+ /**
449
+ * Provider with LLM modality support.
450
+ *
451
+ * Type alias for providers that support language model inference.
452
+ *
453
+ * @typeParam TParams - Model-specific parameters type
454
+ * @typeParam TOptions - Provider-specific options type
455
+ */
456
+ type LLMProvider<TParams = any, TOptions = unknown> = Provider<TOptions> & {
457
+ readonly modalities: {
458
+ llm: LLMHandler<TParams>;
459
+ };
460
+ };
461
+ /**
462
+ * Provider with Embedding modality support.
463
+ *
464
+ * Type alias for providers that support embedding generation.
465
+ *
466
+ * @typeParam TParams - Model-specific parameters type
467
+ * @typeParam TOptions - Provider-specific options type
468
+ */
469
+ type EmbeddingProvider<TParams = any, TOptions = unknown> = Provider<TOptions> & {
470
+ readonly modalities: {
471
+ embedding: EmbeddingHandler<TParams>;
472
+ };
473
+ };
474
+ /**
475
+ * Provider with Image modality support.
476
+ *
477
+ * Type alias for providers that support image generation.
478
+ *
479
+ * @typeParam TParams - Model-specific parameters type
480
+ * @typeParam TOptions - Provider-specific options type
481
+ */
482
+ type ImageProvider<TParams = any, TOptions = unknown> = Provider<TOptions> & {
483
+ readonly modalities: {
484
+ image: ImageHandler<TParams>;
485
+ };
486
+ };
487
+
488
+ export { type BoundEmbeddingModel as B, type EmbeddingHandler as E, type ImageHandler as I, type KeyStrategy as K, type LLMProvider as L, type ModelReference as M, type ProviderConfig as P, type RetryStrategy as R, UPPError as U, type LLMHandler as a, type Provider as b, type ErrorCode as c, type Modality as d, type EmbeddingProvider as e, type ImageProvider as f, type BoundImageModel as g };