@providerprotocol/ai 0.0.18 → 0.0.20

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 (39) hide show
  1. package/README.md +364 -111
  2. package/dist/anthropic/index.d.ts +1 -1
  3. package/dist/anthropic/index.js +6 -6
  4. package/dist/chunk-P5IRTEM5.js +120 -0
  5. package/dist/chunk-P5IRTEM5.js.map +1 -0
  6. package/dist/{chunk-5FEAOEXV.js → chunk-U3FZWV4U.js} +53 -102
  7. package/dist/chunk-U3FZWV4U.js.map +1 -0
  8. package/dist/chunk-WAKD3OO5.js +224 -0
  9. package/dist/chunk-WAKD3OO5.js.map +1 -0
  10. package/dist/content-DEl3z_W2.d.ts +276 -0
  11. package/dist/google/index.d.ts +3 -1
  12. package/dist/google/index.js +123 -7
  13. package/dist/google/index.js.map +1 -1
  14. package/dist/http/index.d.ts +2 -2
  15. package/dist/http/index.js +4 -3
  16. package/dist/image-Dhq-Yuq4.d.ts +456 -0
  17. package/dist/index.d.ts +55 -163
  18. package/dist/index.js +81 -213
  19. package/dist/index.js.map +1 -1
  20. package/dist/ollama/index.d.ts +1 -1
  21. package/dist/ollama/index.js +6 -6
  22. package/dist/openai/index.d.ts +47 -20
  23. package/dist/openai/index.js +310 -7
  24. package/dist/openai/index.js.map +1 -1
  25. package/dist/openrouter/index.d.ts +1 -1
  26. package/dist/openrouter/index.js +6 -6
  27. package/dist/{provider-D5MO3-pS.d.ts → provider-BBMBZuGn.d.ts} +11 -11
  28. package/dist/proxy/index.d.ts +310 -86
  29. package/dist/proxy/index.js +33 -59
  30. package/dist/proxy/index.js.map +1 -1
  31. package/dist/{retry-DZ4Sqmxp.d.ts → retry-DR7YRJDz.d.ts} +1 -1
  32. package/dist/{stream-BjyVzBxV.d.ts → stream-DRHy6q1a.d.ts} +2 -275
  33. package/dist/xai/index.d.ts +29 -1
  34. package/dist/xai/index.js +119 -7
  35. package/dist/xai/index.js.map +1 -1
  36. package/package.json +1 -1
  37. package/dist/chunk-5FEAOEXV.js.map +0 -1
  38. package/dist/chunk-DZQHVGNV.js +0 -71
  39. package/dist/chunk-DZQHVGNV.js.map +0 -1
@@ -0,0 +1,456 @@
1
+ import { P as ProviderConfig, h as ImageProvider } from './provider-BBMBZuGn.js';
2
+ import { b as ImageSource, I as ImageBlock } from './content-DEl3z_W2.js';
3
+
4
+ /**
5
+ * @fileoverview Image content handling for the Universal Provider Protocol.
6
+ *
7
+ * Provides a unified Image class for working with images across different sources
8
+ * (file paths, URLs, raw bytes, base64). Supports conversion between formats and
9
+ * integration with UPP message content blocks.
10
+ *
11
+ * @module core/media/Image
12
+ */
13
+
14
+ /**
15
+ * Represents an image that can be used in UPP messages.
16
+ *
17
+ * Images can be created from various sources (files, URLs, bytes, base64) and
18
+ * converted to different formats as needed by providers. The class provides
19
+ * a unified interface regardless of the underlying source type.
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * // Load from file
24
+ * const fileImage = await Image.fromPath('./photo.jpg');
25
+ *
26
+ * // Reference by URL
27
+ * const urlImage = Image.fromUrl('https://example.com/image.png');
28
+ *
29
+ * // From raw bytes
30
+ * const bytesImage = Image.fromBytes(uint8Array, 'image/png');
31
+ *
32
+ * // Use in a message
33
+ * const message = new UserMessage([image.toBlock()]);
34
+ * ```
35
+ */
36
+ declare class Image {
37
+ /** The underlying image source (bytes, base64, or URL) */
38
+ readonly source: ImageSource;
39
+ /** MIME type of the image (e.g., 'image/jpeg', 'image/png') */
40
+ readonly mimeType: string;
41
+ /** Image width in pixels, if known */
42
+ readonly width?: number;
43
+ /** Image height in pixels, if known */
44
+ readonly height?: number;
45
+ private constructor();
46
+ /**
47
+ * Whether this image has data loaded in memory.
48
+ *
49
+ * Returns `false` for URL-sourced images that reference external resources.
50
+ * These must be fetched before their data can be accessed.
51
+ */
52
+ get hasData(): boolean;
53
+ /**
54
+ * Converts the image to a base64-encoded string.
55
+ *
56
+ * @returns The image data as a base64 string
57
+ * @throws {Error} When the source is a URL (data must be fetched first)
58
+ */
59
+ toBase64(): string;
60
+ /**
61
+ * Converts the image to a data URL suitable for embedding in HTML or CSS.
62
+ *
63
+ * @returns A data URL in the format `data:{mimeType};base64,{data}`
64
+ * @throws {Error} When the source is a URL (data must be fetched first)
65
+ */
66
+ toDataUrl(): string;
67
+ /**
68
+ * Gets the image data as raw bytes.
69
+ *
70
+ * @returns The image data as a Uint8Array
71
+ * @throws {Error} When the source is a URL (data must be fetched first)
72
+ */
73
+ toBytes(): Uint8Array;
74
+ /**
75
+ * Gets the URL for URL-sourced images.
76
+ *
77
+ * @returns The image URL
78
+ * @throws {Error} When the source is not a URL
79
+ */
80
+ toUrl(): string;
81
+ /**
82
+ * Converts this Image to an ImageBlock for use in UPP messages.
83
+ *
84
+ * @returns An ImageBlock that can be included in message content arrays
85
+ */
86
+ toBlock(): ImageBlock;
87
+ /**
88
+ * Creates an Image by reading a file from disk.
89
+ *
90
+ * The file is read into memory as bytes. MIME type is automatically
91
+ * detected from the file extension.
92
+ *
93
+ * @param path - Path to the image file
94
+ * @returns Promise resolving to an Image with the file contents
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * const image = await Image.fromPath('./photos/vacation.jpg');
99
+ * ```
100
+ */
101
+ static fromPath(path: string): Promise<Image>;
102
+ /**
103
+ * Creates an Image from a URL reference.
104
+ *
105
+ * The URL is stored as a reference and not fetched. Providers will handle
106
+ * URL-to-data conversion if needed. MIME type is detected from the URL
107
+ * path if not provided.
108
+ *
109
+ * @param url - URL pointing to the image
110
+ * @param mimeType - Optional MIME type override
111
+ * @returns An Image referencing the URL
112
+ *
113
+ * @example
114
+ * ```typescript
115
+ * const image = Image.fromUrl('https://example.com/logo.png');
116
+ * ```
117
+ */
118
+ static fromUrl(url: string, mimeType?: string): Image;
119
+ /**
120
+ * Creates an Image from raw byte data.
121
+ *
122
+ * @param data - The image data as a Uint8Array
123
+ * @param mimeType - The MIME type of the image
124
+ * @returns An Image containing the byte data
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * const image = Image.fromBytes(pngData, 'image/png');
129
+ * ```
130
+ */
131
+ static fromBytes(data: Uint8Array, mimeType: string): Image;
132
+ /**
133
+ * Creates an Image from a base64-encoded string.
134
+ *
135
+ * @param base64 - The base64-encoded image data (without data URL prefix)
136
+ * @param mimeType - The MIME type of the image
137
+ * @returns An Image containing the base64 data
138
+ *
139
+ * @example
140
+ * ```typescript
141
+ * const image = Image.fromBase64(base64String, 'image/jpeg');
142
+ * ```
143
+ */
144
+ static fromBase64(base64: string, mimeType: string): Image;
145
+ /**
146
+ * Creates an Image from an existing ImageBlock.
147
+ *
148
+ * Useful for converting content blocks received from providers back
149
+ * into Image instances for further processing.
150
+ *
151
+ * @param block - An ImageBlock from message content
152
+ * @returns An Image with the block's source and metadata
153
+ */
154
+ static fromBlock(block: ImageBlock): Image;
155
+ }
156
+
157
+ /**
158
+ * @fileoverview Image generation types for the Universal Provider Protocol.
159
+ *
160
+ * Defines the interfaces for configuring and executing image generation operations,
161
+ * including options, instances, requests, responses, streaming, and capabilities.
162
+ *
163
+ * @module types/image
164
+ */
165
+
166
+ /**
167
+ * Structural type for image model input.
168
+ * Uses structural typing to avoid generic variance issues with Provider generics.
169
+ */
170
+ interface ImageModelInput {
171
+ readonly modelId: string;
172
+ readonly provider: {
173
+ readonly name: string;
174
+ readonly version: string;
175
+ readonly modalities: {
176
+ image?: unknown;
177
+ };
178
+ };
179
+ }
180
+ /**
181
+ * Options for creating an image instance with the image() function.
182
+ *
183
+ * @typeParam TParams - Provider-specific parameter type
184
+ *
185
+ * @example
186
+ * ```typescript
187
+ * const options: ImageOptions<OpenAIImageParams> = {
188
+ * model: openai('dall-e-3'),
189
+ * config: { apiKey: process.env.OPENAI_API_KEY },
190
+ * params: { size: '1024x1024', quality: 'hd' }
191
+ * };
192
+ * ```
193
+ */
194
+ interface ImageOptions<TParams = unknown> {
195
+ /** A model reference from a provider factory */
196
+ model: ImageModelInput;
197
+ /** Provider infrastructure configuration */
198
+ config?: ProviderConfig;
199
+ /** Provider-specific parameters (passed through unchanged) */
200
+ params?: TParams;
201
+ }
202
+ /**
203
+ * Input type for generate() - either a string prompt or object with prompt.
204
+ */
205
+ type ImageInput = string | {
206
+ prompt: string;
207
+ };
208
+ /**
209
+ * Input for edit() operations.
210
+ */
211
+ interface ImageEditInput {
212
+ /** Base image to edit */
213
+ image: Image;
214
+ /** Mask indicating edit region (interpretation varies by provider) */
215
+ mask?: Image;
216
+ /** Edit instruction prompt */
217
+ prompt: string;
218
+ }
219
+ /**
220
+ * A single generated image with optional metadata.
221
+ */
222
+ interface GeneratedImage {
223
+ /** The generated image */
224
+ image: Image;
225
+ /** Provider-specific per-image metadata */
226
+ metadata?: Record<string, unknown>;
227
+ }
228
+ /**
229
+ * Usage statistics for image generation.
230
+ * Fields are optional because providers report usage differently.
231
+ */
232
+ interface ImageUsage {
233
+ /** Number of images generated */
234
+ imagesGenerated?: number;
235
+ /** Input tokens consumed (token-based pricing) */
236
+ inputTokens?: number;
237
+ /** Output tokens consumed (token-based pricing) */
238
+ outputTokens?: number;
239
+ /** Provider-reported cost (credits, dollars, etc.) */
240
+ cost?: number;
241
+ }
242
+ /**
243
+ * Result from generate() or edit() calls.
244
+ */
245
+ interface ImageResult {
246
+ /** Generated images */
247
+ images: GeneratedImage[];
248
+ /** Provider-specific response metadata */
249
+ metadata?: Record<string, unknown>;
250
+ /** Usage/billing information */
251
+ usage?: ImageUsage;
252
+ }
253
+ /**
254
+ * Stream events for image generation.
255
+ */
256
+ type ImageStreamEvent = {
257
+ type: 'preview';
258
+ image: Image;
259
+ index: number;
260
+ metadata?: Record<string, unknown>;
261
+ } | {
262
+ type: 'complete';
263
+ image: GeneratedImage;
264
+ index: number;
265
+ };
266
+ /**
267
+ * Async iterable stream with final result accessor.
268
+ * Returned when stream() is called.
269
+ */
270
+ interface ImageStreamResult extends AsyncIterable<ImageStreamEvent> {
271
+ /** Promise resolving to complete result after streaming */
272
+ readonly result: Promise<ImageResult>;
273
+ /** Abort the generation */
274
+ abort(): void;
275
+ }
276
+ /**
277
+ * Image generation capabilities.
278
+ */
279
+ interface ImageCapabilities {
280
+ /** Supports text-to-image generation */
281
+ generate: boolean;
282
+ /** Supports streaming with partial previews */
283
+ streaming: boolean;
284
+ /** Supports image editing/inpainting */
285
+ edit: boolean;
286
+ /** Maximum images per request (if known) */
287
+ maxImages?: number;
288
+ }
289
+ /**
290
+ * Image instance returned by the image() function.
291
+ *
292
+ * @typeParam TParams - Provider-specific parameter type
293
+ *
294
+ * @example
295
+ * ```typescript
296
+ * const dalle = image({ model: openai('dall-e-3') });
297
+ *
298
+ * // Simple generation
299
+ * const result = await dalle.generate('A sunset over mountains');
300
+ *
301
+ * // Streaming (if supported)
302
+ * if (dalle.capabilities.streaming && dalle.stream) {
303
+ * const stream = dalle.stream('A cyberpunk cityscape');
304
+ * for await (const event of stream) {
305
+ * // Handle preview/complete events
306
+ * }
307
+ * }
308
+ * ```
309
+ */
310
+ interface ImageInstance<TParams = unknown> {
311
+ /**
312
+ * Generate images from a text prompt.
313
+ *
314
+ * @param input - The prompt string or object with prompt
315
+ * @returns Promise resolving to the generated images
316
+ */
317
+ generate(input: ImageInput): Promise<ImageResult>;
318
+ /**
319
+ * Generate with streaming progress (if supported).
320
+ * Only available when capabilities.streaming is true.
321
+ *
322
+ * @param input - The prompt string or object with prompt
323
+ * @returns ImageStreamResult with events and final result
324
+ */
325
+ stream?(input: ImageInput): ImageStreamResult;
326
+ /**
327
+ * Edit an existing image (if supported).
328
+ * Only available when capabilities.edit is true.
329
+ *
330
+ * @param input - Edit input with image, optional mask, and prompt
331
+ * @returns Promise resolving to the edited images
332
+ */
333
+ edit?(input: ImageEditInput): Promise<ImageResult>;
334
+ /** The bound image model */
335
+ readonly model: BoundImageModel<TParams>;
336
+ /** Current parameters */
337
+ readonly params: TParams | undefined;
338
+ /** Model capabilities */
339
+ readonly capabilities: ImageCapabilities;
340
+ }
341
+ /**
342
+ * Request passed from image() core to providers for generation.
343
+ * @internal
344
+ */
345
+ interface ImageRequest<TParams = unknown> {
346
+ /** Generation prompt */
347
+ prompt: string;
348
+ /** Provider-specific parameters (passed through unchanged) */
349
+ params?: TParams;
350
+ /** Provider infrastructure config */
351
+ config: ProviderConfig;
352
+ /** Abort signal for cancellation */
353
+ signal?: AbortSignal;
354
+ }
355
+ /**
356
+ * Request passed to providers for edit operations.
357
+ * @internal
358
+ */
359
+ interface ImageEditRequest<TParams = unknown> {
360
+ /** Base image to edit */
361
+ image: Image;
362
+ /** Edit mask */
363
+ mask?: Image;
364
+ /** Edit instruction prompt */
365
+ prompt: string;
366
+ /** Provider-specific parameters */
367
+ params?: TParams;
368
+ /** Provider infrastructure config */
369
+ config: ProviderConfig;
370
+ /** Abort signal for cancellation */
371
+ signal?: AbortSignal;
372
+ }
373
+ /**
374
+ * Response from provider's generate or edit method.
375
+ * @internal
376
+ */
377
+ interface ImageResponse {
378
+ /** Generated images */
379
+ images: GeneratedImage[];
380
+ /** Provider-specific response metadata */
381
+ metadata?: Record<string, unknown>;
382
+ /** Usage information */
383
+ usage?: ImageUsage;
384
+ }
385
+ /**
386
+ * Raw provider stream result.
387
+ * An async iterable of ImageStreamEvent with a response promise.
388
+ * @internal
389
+ */
390
+ interface ImageProviderStreamResult extends AsyncIterable<ImageStreamEvent> {
391
+ /** Promise resolving to the complete response */
392
+ readonly response: Promise<ImageResponse>;
393
+ }
394
+ /**
395
+ * Bound image model - full definition.
396
+ *
397
+ * Represents an image model bound to a specific provider and model ID,
398
+ * ready to execute generation requests.
399
+ *
400
+ * @typeParam TParams - Provider-specific parameter type
401
+ */
402
+ interface BoundImageModel<TParams = unknown> {
403
+ /** The model identifier */
404
+ readonly modelId: string;
405
+ /** Reference to the parent provider */
406
+ readonly provider: ImageProvider<TParams>;
407
+ /** Model capabilities */
408
+ readonly capabilities: ImageCapabilities;
409
+ /**
410
+ * Generate images from a prompt.
411
+ *
412
+ * @param request - The generation request
413
+ * @returns Promise resolving to the response
414
+ */
415
+ generate(request: ImageRequest<TParams>): Promise<ImageResponse>;
416
+ /**
417
+ * Stream image generation (optional).
418
+ *
419
+ * @param request - The generation request
420
+ * @returns Stream result with events and final response
421
+ */
422
+ stream?(request: ImageRequest<TParams>): ImageProviderStreamResult;
423
+ /**
424
+ * Edit an image (optional).
425
+ *
426
+ * @param request - The edit request
427
+ * @returns Promise resolving to the response
428
+ */
429
+ edit?(request: ImageEditRequest<TParams>): Promise<ImageResponse>;
430
+ }
431
+ /**
432
+ * Image Handler interface for providers.
433
+ *
434
+ * Implemented by providers to enable image generation capabilities.
435
+ *
436
+ * @typeParam TParams - Provider-specific parameter type
437
+ */
438
+ interface ImageHandler<TParams = unknown> {
439
+ /**
440
+ * Binds a model ID to create an executable image model.
441
+ *
442
+ * @param modelId - The model identifier to bind
443
+ * @returns A bound image model ready for generation
444
+ */
445
+ bind(modelId: string): BoundImageModel<TParams>;
446
+ /**
447
+ * Sets the parent provider reference.
448
+ * Called by createProvider() after the provider is constructed.
449
+ *
450
+ * @param provider - The parent provider
451
+ * @internal
452
+ */
453
+ _setProvider?(provider: ImageProvider<TParams>): void;
454
+ }
455
+
456
+ export { type BoundImageModel as B, type GeneratedImage as G, type ImageOptions as I, type ImageInstance as a, Image as b, type ImageInput as c, type ImageEditInput as d, type ImageUsage as e, type ImageResult as f, type ImageStreamEvent as g, type ImageStreamResult as h, type ImageCapabilities as i, type ImageRequest as j, type ImageEditRequest as k, type ImageResponse as l, type ImageProviderStreamResult as m, type ImageHandler as n, type ImageModelInput as o };