@providerprotocol/ai 0.0.20 → 0.0.21
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.
- package/dist/anthropic/index.d.ts +184 -14
- package/dist/anthropic/index.js +210 -82
- package/dist/anthropic/index.js.map +1 -1
- package/dist/{chunk-U3FZWV4U.js → chunk-EDENPF3E.js} +5 -2
- package/dist/{chunk-U3FZWV4U.js.map → chunk-EDENPF3E.js.map} +1 -1
- package/dist/{chunk-UMKWXGO3.js → chunk-M4BMM5IB.js} +86 -2
- package/dist/chunk-M4BMM5IB.js.map +1 -0
- package/dist/{chunk-P5IRTEM5.js → chunk-Y3GBJNA2.js} +2 -2
- package/dist/{chunk-U4JJC2YX.js → chunk-Z4ILICF5.js} +2 -2
- package/dist/chunk-Z4ILICF5.js.map +1 -0
- package/dist/google/index.d.ts +16 -19
- package/dist/google/index.js +14 -36
- package/dist/google/index.js.map +1 -1
- package/dist/http/index.d.ts +2 -2
- package/dist/http/index.js +3 -3
- package/dist/index.d.ts +101 -38
- package/dist/index.js +69 -43
- package/dist/index.js.map +1 -1
- package/dist/ollama/index.d.ts +14 -16
- package/dist/ollama/index.js +5 -7
- package/dist/ollama/index.js.map +1 -1
- package/dist/openai/index.d.ts +25 -133
- package/dist/openai/index.js +27 -81
- package/dist/openai/index.js.map +1 -1
- package/dist/openrouter/index.d.ts +28 -53
- package/dist/openrouter/index.js +20 -43
- package/dist/openrouter/index.js.map +1 -1
- package/dist/provider-DGQHYE6I.d.ts +1319 -0
- package/dist/proxy/index.d.ts +2 -3
- package/dist/proxy/index.js +5 -7
- package/dist/proxy/index.js.map +1 -1
- package/dist/{retry-DR7YRJDz.d.ts → retry-Pcs3hnbu.d.ts} +2 -2
- package/dist/{stream-DRHy6q1a.d.ts → stream-Di9acos2.d.ts} +1 -1
- package/dist/xai/index.d.ts +16 -88
- package/dist/xai/index.js +30 -58
- package/dist/xai/index.js.map +1 -1
- package/package.json +4 -1
- package/dist/chunk-MSR5P65T.js +0 -39
- package/dist/chunk-MSR5P65T.js.map +0 -1
- package/dist/chunk-U4JJC2YX.js.map +0 -1
- package/dist/chunk-UMKWXGO3.js.map +0 -1
- package/dist/content-DEl3z_W2.d.ts +0 -276
- package/dist/image-Dhq-Yuq4.d.ts +0 -456
- package/dist/provider-BBMBZuGn.d.ts +0 -570
- /package/dist/{chunk-P5IRTEM5.js.map → chunk-Y3GBJNA2.js.map} +0 -0
|
@@ -0,0 +1,1319 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Content block types for multimodal messages.
|
|
3
|
+
*
|
|
4
|
+
* Defines the various content block types that can be included in
|
|
5
|
+
* user and assistant messages, supporting text, images, audio, video,
|
|
6
|
+
* and arbitrary binary data.
|
|
7
|
+
*
|
|
8
|
+
* @module types/content
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Image source variants for ImageBlock.
|
|
12
|
+
*
|
|
13
|
+
* Images can be provided as base64-encoded strings, URLs, or raw bytes.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // Base64 encoded image
|
|
18
|
+
* const base64Source: ImageSource = {
|
|
19
|
+
* type: 'base64',
|
|
20
|
+
* data: 'iVBORw0KGgo...'
|
|
21
|
+
* };
|
|
22
|
+
*
|
|
23
|
+
* // URL reference
|
|
24
|
+
* const urlSource: ImageSource = {
|
|
25
|
+
* type: 'url',
|
|
26
|
+
* url: 'https://example.com/image.png'
|
|
27
|
+
* };
|
|
28
|
+
*
|
|
29
|
+
* // Raw bytes
|
|
30
|
+
* const bytesSource: ImageSource = {
|
|
31
|
+
* type: 'bytes',
|
|
32
|
+
* data: new Uint8Array([...])
|
|
33
|
+
* };
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
type ImageSource = {
|
|
37
|
+
type: 'base64';
|
|
38
|
+
data: string;
|
|
39
|
+
} | {
|
|
40
|
+
type: 'url';
|
|
41
|
+
url: string;
|
|
42
|
+
} | {
|
|
43
|
+
type: 'bytes';
|
|
44
|
+
data: Uint8Array;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Text content block.
|
|
48
|
+
*
|
|
49
|
+
* The most common content block type, containing plain text content.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* const textBlock: TextBlock = {
|
|
54
|
+
* type: 'text',
|
|
55
|
+
* text: 'Hello, world!'
|
|
56
|
+
* };
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
interface TextBlock {
|
|
60
|
+
/** Discriminator for text blocks */
|
|
61
|
+
type: 'text';
|
|
62
|
+
/** The text content */
|
|
63
|
+
text: string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Image content block.
|
|
67
|
+
*
|
|
68
|
+
* Contains an image with its source data and metadata.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* const imageBlock: ImageBlock = {
|
|
73
|
+
* type: 'image',
|
|
74
|
+
* source: { type: 'url', url: 'https://example.com/photo.jpg' },
|
|
75
|
+
* mimeType: 'image/jpeg',
|
|
76
|
+
* width: 1920,
|
|
77
|
+
* height: 1080
|
|
78
|
+
* };
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
interface ImageBlock {
|
|
82
|
+
/** Discriminator for image blocks */
|
|
83
|
+
type: 'image';
|
|
84
|
+
/** The image data source */
|
|
85
|
+
source: ImageSource;
|
|
86
|
+
/** MIME type of the image (e.g., 'image/png', 'image/jpeg') */
|
|
87
|
+
mimeType: string;
|
|
88
|
+
/** Image width in pixels */
|
|
89
|
+
width?: number;
|
|
90
|
+
/** Image height in pixels */
|
|
91
|
+
height?: number;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Audio content block.
|
|
95
|
+
*
|
|
96
|
+
* Contains audio data with its metadata.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* const audioBlock: AudioBlock = {
|
|
101
|
+
* type: 'audio',
|
|
102
|
+
* data: audioBytes,
|
|
103
|
+
* mimeType: 'audio/mp3',
|
|
104
|
+
* duration: 120.5
|
|
105
|
+
* };
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
interface AudioBlock {
|
|
109
|
+
/** Discriminator for audio blocks */
|
|
110
|
+
type: 'audio';
|
|
111
|
+
/** Raw audio data */
|
|
112
|
+
data: Uint8Array;
|
|
113
|
+
/** MIME type of the audio (e.g., 'audio/mp3', 'audio/wav') */
|
|
114
|
+
mimeType: string;
|
|
115
|
+
/** Duration in seconds */
|
|
116
|
+
duration?: number;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Video content block.
|
|
120
|
+
*
|
|
121
|
+
* Contains video data with its metadata.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```typescript
|
|
125
|
+
* const videoBlock: VideoBlock = {
|
|
126
|
+
* type: 'video',
|
|
127
|
+
* data: videoBytes,
|
|
128
|
+
* mimeType: 'video/mp4',
|
|
129
|
+
* duration: 30,
|
|
130
|
+
* width: 1920,
|
|
131
|
+
* height: 1080
|
|
132
|
+
* };
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
interface VideoBlock {
|
|
136
|
+
/** Discriminator for video blocks */
|
|
137
|
+
type: 'video';
|
|
138
|
+
/** Raw video data */
|
|
139
|
+
data: Uint8Array;
|
|
140
|
+
/** MIME type of the video (e.g., 'video/mp4', 'video/webm') */
|
|
141
|
+
mimeType: string;
|
|
142
|
+
/** Duration in seconds */
|
|
143
|
+
duration?: number;
|
|
144
|
+
/** Video width in pixels */
|
|
145
|
+
width?: number;
|
|
146
|
+
/** Video height in pixels */
|
|
147
|
+
height?: number;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Binary content block for arbitrary data.
|
|
151
|
+
*
|
|
152
|
+
* A generic block type for data that doesn't fit other categories.
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```typescript
|
|
156
|
+
* const binaryBlock: BinaryBlock = {
|
|
157
|
+
* type: 'binary',
|
|
158
|
+
* data: pdfBytes,
|
|
159
|
+
* mimeType: 'application/pdf',
|
|
160
|
+
* metadata: { filename: 'document.pdf', pages: 10 }
|
|
161
|
+
* };
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
interface BinaryBlock {
|
|
165
|
+
/** Discriminator for binary blocks */
|
|
166
|
+
type: 'binary';
|
|
167
|
+
/** Raw binary data */
|
|
168
|
+
data: Uint8Array;
|
|
169
|
+
/** MIME type of the data */
|
|
170
|
+
mimeType: string;
|
|
171
|
+
/** Additional metadata about the binary content */
|
|
172
|
+
metadata?: Record<string, unknown>;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Union of all content block types.
|
|
176
|
+
*
|
|
177
|
+
* Used when a function or property can accept any type of content block.
|
|
178
|
+
*/
|
|
179
|
+
type ContentBlock = TextBlock | ImageBlock | AudioBlock | VideoBlock | BinaryBlock;
|
|
180
|
+
/**
|
|
181
|
+
* Content types allowed in user messages.
|
|
182
|
+
*
|
|
183
|
+
* Users can send any type of content block including binary data.
|
|
184
|
+
*/
|
|
185
|
+
type UserContent = TextBlock | ImageBlock | AudioBlock | VideoBlock | BinaryBlock;
|
|
186
|
+
/**
|
|
187
|
+
* Content types allowed in assistant messages.
|
|
188
|
+
*
|
|
189
|
+
* Assistants can generate text and media but not arbitrary binary data.
|
|
190
|
+
*/
|
|
191
|
+
type AssistantContent = TextBlock | ImageBlock | AudioBlock | VideoBlock;
|
|
192
|
+
/**
|
|
193
|
+
* Creates a text content block from a string.
|
|
194
|
+
*
|
|
195
|
+
* @param content - The text content
|
|
196
|
+
* @returns A TextBlock containing the provided text
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```typescript
|
|
200
|
+
* const block = text('Hello, world!');
|
|
201
|
+
* // { type: 'text', text: 'Hello, world!' }
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
declare function text(content: string): TextBlock;
|
|
205
|
+
/**
|
|
206
|
+
* Type guard for TextBlock.
|
|
207
|
+
*
|
|
208
|
+
* @param block - The content block to check
|
|
209
|
+
* @returns True if the block is a TextBlock
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```typescript
|
|
213
|
+
* if (isTextBlock(block)) {
|
|
214
|
+
* console.log(block.text);
|
|
215
|
+
* }
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
declare function isTextBlock(block: ContentBlock): block is TextBlock;
|
|
219
|
+
/**
|
|
220
|
+
* Type guard for ImageBlock.
|
|
221
|
+
*
|
|
222
|
+
* @param block - The content block to check
|
|
223
|
+
* @returns True if the block is an ImageBlock
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```typescript
|
|
227
|
+
* if (isImageBlock(block)) {
|
|
228
|
+
* console.log(block.mimeType, block.width, block.height);
|
|
229
|
+
* }
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
declare function isImageBlock(block: ContentBlock): block is ImageBlock;
|
|
233
|
+
/**
|
|
234
|
+
* Type guard for AudioBlock.
|
|
235
|
+
*
|
|
236
|
+
* @param block - The content block to check
|
|
237
|
+
* @returns True if the block is an AudioBlock
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```typescript
|
|
241
|
+
* if (isAudioBlock(block)) {
|
|
242
|
+
* console.log(block.mimeType, block.duration);
|
|
243
|
+
* }
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
246
|
+
declare function isAudioBlock(block: ContentBlock): block is AudioBlock;
|
|
247
|
+
/**
|
|
248
|
+
* Type guard for VideoBlock.
|
|
249
|
+
*
|
|
250
|
+
* @param block - The content block to check
|
|
251
|
+
* @returns True if the block is a VideoBlock
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* ```typescript
|
|
255
|
+
* if (isVideoBlock(block)) {
|
|
256
|
+
* console.log(block.mimeType, block.duration);
|
|
257
|
+
* }
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
260
|
+
declare function isVideoBlock(block: ContentBlock): block is VideoBlock;
|
|
261
|
+
/**
|
|
262
|
+
* Type guard for BinaryBlock.
|
|
263
|
+
*
|
|
264
|
+
* @param block - The content block to check
|
|
265
|
+
* @returns True if the block is a BinaryBlock
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```typescript
|
|
269
|
+
* if (isBinaryBlock(block)) {
|
|
270
|
+
* console.log(block.mimeType, block.metadata);
|
|
271
|
+
* }
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
274
|
+
declare function isBinaryBlock(block: ContentBlock): block is BinaryBlock;
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* @fileoverview Error types for the Unified Provider Protocol.
|
|
278
|
+
*
|
|
279
|
+
* Provides normalized error codes and a unified error class for handling
|
|
280
|
+
* errors across different AI providers in a consistent manner.
|
|
281
|
+
*
|
|
282
|
+
* @module types/errors
|
|
283
|
+
*/
|
|
284
|
+
/**
|
|
285
|
+
* Normalized error codes for cross-provider error handling.
|
|
286
|
+
*
|
|
287
|
+
* These codes provide a consistent way to identify and handle errors
|
|
288
|
+
* regardless of which AI provider generated them.
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
* ```typescript
|
|
292
|
+
* try {
|
|
293
|
+
* await llm.generate('Hello');
|
|
294
|
+
* } catch (error) {
|
|
295
|
+
* if (error instanceof UPPError) {
|
|
296
|
+
* switch (error.code) {
|
|
297
|
+
* case 'RATE_LIMITED':
|
|
298
|
+
* await delay(error.retryAfter);
|
|
299
|
+
* break;
|
|
300
|
+
* case 'AUTHENTICATION_FAILED':
|
|
301
|
+
* throw new Error('Invalid API key');
|
|
302
|
+
* }
|
|
303
|
+
* }
|
|
304
|
+
* }
|
|
305
|
+
* ```
|
|
306
|
+
*/
|
|
307
|
+
type ErrorCode =
|
|
308
|
+
/** API key is invalid or expired */
|
|
309
|
+
'AUTHENTICATION_FAILED'
|
|
310
|
+
/** Rate limit exceeded, retry after delay */
|
|
311
|
+
| 'RATE_LIMITED'
|
|
312
|
+
/** Input exceeds model's context window */
|
|
313
|
+
| 'CONTEXT_LENGTH_EXCEEDED'
|
|
314
|
+
/** Requested model does not exist */
|
|
315
|
+
| 'MODEL_NOT_FOUND'
|
|
316
|
+
/** Request parameters are malformed */
|
|
317
|
+
| 'INVALID_REQUEST'
|
|
318
|
+
/** Provider returned an unexpected response format */
|
|
319
|
+
| 'INVALID_RESPONSE'
|
|
320
|
+
/** Content was blocked by safety filters */
|
|
321
|
+
| 'CONTENT_FILTERED'
|
|
322
|
+
/** Account quota or credits exhausted */
|
|
323
|
+
| 'QUOTA_EXCEEDED'
|
|
324
|
+
/** Provider-specific error not covered by other codes */
|
|
325
|
+
| 'PROVIDER_ERROR'
|
|
326
|
+
/** Network connectivity issue */
|
|
327
|
+
| 'NETWORK_ERROR'
|
|
328
|
+
/** Request exceeded timeout limit */
|
|
329
|
+
| 'TIMEOUT'
|
|
330
|
+
/** Request was cancelled via AbortSignal */
|
|
331
|
+
| 'CANCELLED';
|
|
332
|
+
/**
|
|
333
|
+
* Modality types supported by UPP.
|
|
334
|
+
*
|
|
335
|
+
* Each modality represents a different type of AI capability that
|
|
336
|
+
* can be provided by a UPP-compatible provider.
|
|
337
|
+
*/
|
|
338
|
+
type Modality =
|
|
339
|
+
/** Large language model for text generation */
|
|
340
|
+
'llm'
|
|
341
|
+
/** Text/image embedding model */
|
|
342
|
+
| 'embedding'
|
|
343
|
+
/** Image generation model */
|
|
344
|
+
| 'image'
|
|
345
|
+
/** Audio processing/generation model */
|
|
346
|
+
| 'audio'
|
|
347
|
+
/** Video processing/generation model */
|
|
348
|
+
| 'video';
|
|
349
|
+
/**
|
|
350
|
+
* Unified Provider Protocol Error.
|
|
351
|
+
*
|
|
352
|
+
* All provider-specific errors are normalized to this type, providing
|
|
353
|
+
* a consistent interface for error handling across different AI providers.
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* ```typescript
|
|
357
|
+
* throw new UPPError(
|
|
358
|
+
* 'API key is invalid',
|
|
359
|
+
* 'AUTHENTICATION_FAILED',
|
|
360
|
+
* 'openai',
|
|
361
|
+
* 'llm',
|
|
362
|
+
* 401
|
|
363
|
+
* );
|
|
364
|
+
* ```
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
* ```typescript
|
|
368
|
+
* // Wrapping a provider error
|
|
369
|
+
* try {
|
|
370
|
+
* await openai.chat.completions.create({ ... });
|
|
371
|
+
* } catch (err) {
|
|
372
|
+
* throw new UPPError(
|
|
373
|
+
* 'OpenAI request failed',
|
|
374
|
+
* 'PROVIDER_ERROR',
|
|
375
|
+
* 'openai',
|
|
376
|
+
* 'llm',
|
|
377
|
+
* err.status,
|
|
378
|
+
* err
|
|
379
|
+
* );
|
|
380
|
+
* }
|
|
381
|
+
* ```
|
|
382
|
+
*/
|
|
383
|
+
declare class UPPError extends Error {
|
|
384
|
+
/** Normalized error code for programmatic handling */
|
|
385
|
+
readonly code: ErrorCode;
|
|
386
|
+
/** Name of the provider that generated the error */
|
|
387
|
+
readonly provider: string;
|
|
388
|
+
/** The modality that was being used when the error occurred */
|
|
389
|
+
readonly modality: Modality;
|
|
390
|
+
/** HTTP status code from the provider's response, if available */
|
|
391
|
+
readonly statusCode?: number;
|
|
392
|
+
/** The original error that caused this UPPError, if wrapping another error */
|
|
393
|
+
readonly cause?: Error;
|
|
394
|
+
/** Error class name, always 'UPPError' */
|
|
395
|
+
readonly name = "UPPError";
|
|
396
|
+
/**
|
|
397
|
+
* Creates a new UPPError instance.
|
|
398
|
+
*
|
|
399
|
+
* @param message - Human-readable error description
|
|
400
|
+
* @param code - Normalized error code for programmatic handling
|
|
401
|
+
* @param provider - Name of the provider that generated the error
|
|
402
|
+
* @param modality - The modality that was being used
|
|
403
|
+
* @param statusCode - HTTP status code from the provider's response
|
|
404
|
+
* @param cause - The original error being wrapped
|
|
405
|
+
*/
|
|
406
|
+
constructor(message: string, code: ErrorCode, provider: string, modality: Modality, statusCode?: number, cause?: Error);
|
|
407
|
+
/**
|
|
408
|
+
* Creates a string representation of the error.
|
|
409
|
+
*
|
|
410
|
+
* @returns Formatted error string including code, message, provider, and modality
|
|
411
|
+
*/
|
|
412
|
+
toString(): string;
|
|
413
|
+
/**
|
|
414
|
+
* Converts the error to a JSON-serializable object.
|
|
415
|
+
*
|
|
416
|
+
* @returns Plain object representation suitable for logging or transmission
|
|
417
|
+
*/
|
|
418
|
+
toJSON(): Record<string, unknown>;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* @fileoverview Image content handling for the Universal Provider Protocol.
|
|
423
|
+
*
|
|
424
|
+
* Provides a unified Image class for working with images across different sources
|
|
425
|
+
* (file paths, URLs, raw bytes, base64). Supports conversion between formats and
|
|
426
|
+
* integration with UPP message content blocks.
|
|
427
|
+
*
|
|
428
|
+
* @module core/media/Image
|
|
429
|
+
*/
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Represents an image that can be used in UPP messages.
|
|
433
|
+
*
|
|
434
|
+
* Images can be created from various sources (files, URLs, bytes, base64) and
|
|
435
|
+
* converted to different formats as needed by providers. The class provides
|
|
436
|
+
* a unified interface regardless of the underlying source type.
|
|
437
|
+
*
|
|
438
|
+
* @example
|
|
439
|
+
* ```typescript
|
|
440
|
+
* // Load from file
|
|
441
|
+
* const fileImage = await Image.fromPath('./photo.jpg');
|
|
442
|
+
*
|
|
443
|
+
* // Reference by URL
|
|
444
|
+
* const urlImage = Image.fromUrl('https://example.com/image.png');
|
|
445
|
+
*
|
|
446
|
+
* // From raw bytes
|
|
447
|
+
* const bytesImage = Image.fromBytes(uint8Array, 'image/png');
|
|
448
|
+
*
|
|
449
|
+
* // Use in a message
|
|
450
|
+
* const message = new UserMessage([image.toBlock()]);
|
|
451
|
+
* ```
|
|
452
|
+
*/
|
|
453
|
+
declare class Image {
|
|
454
|
+
/** The underlying image source (bytes, base64, or URL) */
|
|
455
|
+
readonly source: ImageSource;
|
|
456
|
+
/** MIME type of the image (e.g., 'image/jpeg', 'image/png') */
|
|
457
|
+
readonly mimeType: string;
|
|
458
|
+
/** Image width in pixels, if known */
|
|
459
|
+
readonly width?: number;
|
|
460
|
+
/** Image height in pixels, if known */
|
|
461
|
+
readonly height?: number;
|
|
462
|
+
private constructor();
|
|
463
|
+
/**
|
|
464
|
+
* Whether this image has data loaded in memory.
|
|
465
|
+
*
|
|
466
|
+
* Returns `false` for URL-sourced images that reference external resources.
|
|
467
|
+
* These must be fetched before their data can be accessed.
|
|
468
|
+
*/
|
|
469
|
+
get hasData(): boolean;
|
|
470
|
+
/**
|
|
471
|
+
* Converts the image to a base64-encoded string.
|
|
472
|
+
*
|
|
473
|
+
* @returns The image data as a base64 string
|
|
474
|
+
* @throws {Error} When the source is a URL (data must be fetched first)
|
|
475
|
+
*/
|
|
476
|
+
toBase64(): string;
|
|
477
|
+
/**
|
|
478
|
+
* Converts the image to a data URL suitable for embedding in HTML or CSS.
|
|
479
|
+
*
|
|
480
|
+
* @returns A data URL in the format `data:{mimeType};base64,{data}`
|
|
481
|
+
* @throws {Error} When the source is a URL (data must be fetched first)
|
|
482
|
+
*/
|
|
483
|
+
toDataUrl(): string;
|
|
484
|
+
/**
|
|
485
|
+
* Gets the image data as raw bytes.
|
|
486
|
+
*
|
|
487
|
+
* @returns The image data as a Uint8Array
|
|
488
|
+
* @throws {Error} When the source is a URL (data must be fetched first)
|
|
489
|
+
*/
|
|
490
|
+
toBytes(): Uint8Array;
|
|
491
|
+
/**
|
|
492
|
+
* Gets the URL for URL-sourced images.
|
|
493
|
+
*
|
|
494
|
+
* @returns The image URL
|
|
495
|
+
* @throws {Error} When the source is not a URL
|
|
496
|
+
*/
|
|
497
|
+
toUrl(): string;
|
|
498
|
+
/**
|
|
499
|
+
* Converts this Image to an ImageBlock for use in UPP messages.
|
|
500
|
+
*
|
|
501
|
+
* @returns An ImageBlock that can be included in message content arrays
|
|
502
|
+
*/
|
|
503
|
+
toBlock(): ImageBlock;
|
|
504
|
+
/**
|
|
505
|
+
* Creates an Image by reading a file from disk.
|
|
506
|
+
*
|
|
507
|
+
* The file is read into memory as bytes. MIME type is automatically
|
|
508
|
+
* detected from the file extension.
|
|
509
|
+
*
|
|
510
|
+
* @param path - Path to the image file
|
|
511
|
+
* @returns Promise resolving to an Image with the file contents
|
|
512
|
+
*
|
|
513
|
+
* @example
|
|
514
|
+
* ```typescript
|
|
515
|
+
* const image = await Image.fromPath('./photos/vacation.jpg');
|
|
516
|
+
* ```
|
|
517
|
+
*/
|
|
518
|
+
static fromPath(path: string): Promise<Image>;
|
|
519
|
+
/**
|
|
520
|
+
* Creates an Image from a URL reference.
|
|
521
|
+
*
|
|
522
|
+
* The URL is stored as a reference and not fetched. Providers will handle
|
|
523
|
+
* URL-to-data conversion if needed. MIME type is detected from the URL
|
|
524
|
+
* path if not provided.
|
|
525
|
+
*
|
|
526
|
+
* @param url - URL pointing to the image
|
|
527
|
+
* @param mimeType - Optional MIME type override
|
|
528
|
+
* @returns An Image referencing the URL
|
|
529
|
+
*
|
|
530
|
+
* @example
|
|
531
|
+
* ```typescript
|
|
532
|
+
* const image = Image.fromUrl('https://example.com/logo.png');
|
|
533
|
+
* ```
|
|
534
|
+
*/
|
|
535
|
+
static fromUrl(url: string, mimeType?: string): Image;
|
|
536
|
+
/**
|
|
537
|
+
* Creates an Image from raw byte data.
|
|
538
|
+
*
|
|
539
|
+
* @param data - The image data as a Uint8Array
|
|
540
|
+
* @param mimeType - The MIME type of the image
|
|
541
|
+
* @returns An Image containing the byte data
|
|
542
|
+
*
|
|
543
|
+
* @example
|
|
544
|
+
* ```typescript
|
|
545
|
+
* const image = Image.fromBytes(pngData, 'image/png');
|
|
546
|
+
* ```
|
|
547
|
+
*/
|
|
548
|
+
static fromBytes(data: Uint8Array, mimeType: string): Image;
|
|
549
|
+
/**
|
|
550
|
+
* Creates an Image from a base64-encoded string.
|
|
551
|
+
*
|
|
552
|
+
* @param base64 - The base64-encoded image data (without data URL prefix)
|
|
553
|
+
* @param mimeType - The MIME type of the image
|
|
554
|
+
* @returns An Image containing the base64 data
|
|
555
|
+
*
|
|
556
|
+
* @example
|
|
557
|
+
* ```typescript
|
|
558
|
+
* const image = Image.fromBase64(base64String, 'image/jpeg');
|
|
559
|
+
* ```
|
|
560
|
+
*/
|
|
561
|
+
static fromBase64(base64: string, mimeType: string): Image;
|
|
562
|
+
/**
|
|
563
|
+
* Creates an Image from an existing ImageBlock.
|
|
564
|
+
*
|
|
565
|
+
* Useful for converting content blocks received from providers back
|
|
566
|
+
* into Image instances for further processing.
|
|
567
|
+
*
|
|
568
|
+
* @param block - An ImageBlock from message content
|
|
569
|
+
* @returns An Image with the block's source and metadata
|
|
570
|
+
*/
|
|
571
|
+
static fromBlock(block: ImageBlock): Image;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* @fileoverview Image generation types for the Universal Provider Protocol.
|
|
576
|
+
*
|
|
577
|
+
* Defines the interfaces for configuring and executing image generation operations,
|
|
578
|
+
* including options, instances, requests, responses, streaming, and capabilities.
|
|
579
|
+
*
|
|
580
|
+
* @module types/image
|
|
581
|
+
*/
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* Structural type for image model input.
|
|
585
|
+
* Uses structural typing to avoid generic variance issues with Provider generics.
|
|
586
|
+
*
|
|
587
|
+
* @remarks
|
|
588
|
+
* This type mirrors {@link ModelReference} while keeping provider options
|
|
589
|
+
* structurally compatible across providers.
|
|
590
|
+
*
|
|
591
|
+
* @see ModelReference
|
|
592
|
+
*/
|
|
593
|
+
interface ImageModelInput {
|
|
594
|
+
readonly modelId: string;
|
|
595
|
+
readonly provider: ProviderIdentity;
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* Options for creating an image instance with the image() function.
|
|
599
|
+
*
|
|
600
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
601
|
+
*
|
|
602
|
+
* @example
|
|
603
|
+
* ```typescript
|
|
604
|
+
* const options: ImageOptions<OpenAIImageParams> = {
|
|
605
|
+
* model: openai('dall-e-3'),
|
|
606
|
+
* config: { apiKey: process.env.OPENAI_API_KEY },
|
|
607
|
+
* params: { size: '1024x1024', quality: 'hd' }
|
|
608
|
+
* };
|
|
609
|
+
* ```
|
|
610
|
+
*/
|
|
611
|
+
interface ImageOptions<TParams = unknown> {
|
|
612
|
+
/** A model reference from a provider factory */
|
|
613
|
+
model: ImageModelInput;
|
|
614
|
+
/** Provider infrastructure configuration */
|
|
615
|
+
config?: ProviderConfig;
|
|
616
|
+
/** Provider-specific parameters (passed through unchanged) */
|
|
617
|
+
params?: TParams;
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* Options for image generation.
|
|
621
|
+
*/
|
|
622
|
+
interface ImageGenerateOptions {
|
|
623
|
+
/** Abort signal for cancellation */
|
|
624
|
+
signal?: AbortSignal;
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* Input type for generate() - either a string prompt or object with prompt.
|
|
628
|
+
*/
|
|
629
|
+
type ImageInput = string | {
|
|
630
|
+
prompt: string;
|
|
631
|
+
};
|
|
632
|
+
/**
|
|
633
|
+
* Input for edit() operations.
|
|
634
|
+
*/
|
|
635
|
+
interface ImageEditInput {
|
|
636
|
+
/** Base image to edit */
|
|
637
|
+
image: Image;
|
|
638
|
+
/** Mask indicating edit region (interpretation varies by provider) */
|
|
639
|
+
mask?: Image;
|
|
640
|
+
/** Edit instruction prompt */
|
|
641
|
+
prompt: string;
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* A single generated image with optional metadata.
|
|
645
|
+
*/
|
|
646
|
+
interface GeneratedImage {
|
|
647
|
+
/** The generated image */
|
|
648
|
+
image: Image;
|
|
649
|
+
/** Provider-specific per-image metadata */
|
|
650
|
+
metadata?: Record<string, unknown>;
|
|
651
|
+
}
|
|
652
|
+
/**
|
|
653
|
+
* Usage statistics for image generation.
|
|
654
|
+
* Fields are optional because providers report usage differently.
|
|
655
|
+
*/
|
|
656
|
+
interface ImageUsage {
|
|
657
|
+
/** Number of images generated */
|
|
658
|
+
imagesGenerated?: number;
|
|
659
|
+
/** Input tokens consumed (token-based pricing) */
|
|
660
|
+
inputTokens?: number;
|
|
661
|
+
/** Output tokens consumed (token-based pricing) */
|
|
662
|
+
outputTokens?: number;
|
|
663
|
+
/** Provider-reported cost (credits, dollars, etc.) */
|
|
664
|
+
cost?: number;
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* Result from generate() or edit() calls.
|
|
668
|
+
*/
|
|
669
|
+
interface ImageResult {
|
|
670
|
+
/** Generated images */
|
|
671
|
+
images: GeneratedImage[];
|
|
672
|
+
/** Provider-specific response metadata */
|
|
673
|
+
metadata?: Record<string, unknown>;
|
|
674
|
+
/** Usage/billing information */
|
|
675
|
+
usage?: ImageUsage;
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
678
|
+
* Stream events for image generation.
|
|
679
|
+
*/
|
|
680
|
+
type ImageStreamEvent = {
|
|
681
|
+
type: 'preview';
|
|
682
|
+
image: Image;
|
|
683
|
+
index: number;
|
|
684
|
+
metadata?: Record<string, unknown>;
|
|
685
|
+
} | {
|
|
686
|
+
type: 'complete';
|
|
687
|
+
image: GeneratedImage;
|
|
688
|
+
index: number;
|
|
689
|
+
};
|
|
690
|
+
/**
|
|
691
|
+
* Async iterable stream with final result accessor.
|
|
692
|
+
* Returned when stream() is called.
|
|
693
|
+
*/
|
|
694
|
+
interface ImageStreamResult extends AsyncIterable<ImageStreamEvent> {
|
|
695
|
+
/** Promise resolving to complete result after streaming */
|
|
696
|
+
readonly result: Promise<ImageResult>;
|
|
697
|
+
/** Abort the generation */
|
|
698
|
+
abort(): void;
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* Image generation capabilities.
|
|
702
|
+
*/
|
|
703
|
+
interface ImageCapabilities {
|
|
704
|
+
/** Supports text-to-image generation */
|
|
705
|
+
generate: boolean;
|
|
706
|
+
/** Supports streaming with partial previews */
|
|
707
|
+
streaming: boolean;
|
|
708
|
+
/** Supports image editing/inpainting */
|
|
709
|
+
edit: boolean;
|
|
710
|
+
/** Maximum images per request (if known) */
|
|
711
|
+
maxImages?: number;
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Image instance returned by the image() function.
|
|
715
|
+
*
|
|
716
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
717
|
+
*
|
|
718
|
+
* @example
|
|
719
|
+
* ```typescript
|
|
720
|
+
* const dalle = image({ model: openai('dall-e-3') });
|
|
721
|
+
*
|
|
722
|
+
* // Simple generation
|
|
723
|
+
* const result = await dalle.generate('A sunset over mountains');
|
|
724
|
+
*
|
|
725
|
+
* // Streaming (if supported)
|
|
726
|
+
* if (dalle.capabilities.streaming && dalle.stream) {
|
|
727
|
+
* const stream = dalle.stream('A cyberpunk cityscape');
|
|
728
|
+
* for await (const event of stream) {
|
|
729
|
+
* // Handle preview/complete events
|
|
730
|
+
* }
|
|
731
|
+
* }
|
|
732
|
+
* ```
|
|
733
|
+
*/
|
|
734
|
+
interface ImageInstance<TParams = unknown> {
|
|
735
|
+
/**
|
|
736
|
+
* Generate images from a text prompt.
|
|
737
|
+
*
|
|
738
|
+
* @param input - The prompt string or object with prompt
|
|
739
|
+
* @param options - Optional generation options
|
|
740
|
+
* @returns Promise resolving to the generated images
|
|
741
|
+
*/
|
|
742
|
+
generate(input: ImageInput, options?: ImageGenerateOptions): Promise<ImageResult>;
|
|
743
|
+
/**
|
|
744
|
+
* Generate with streaming progress (if supported).
|
|
745
|
+
* Only available when capabilities.streaming is true.
|
|
746
|
+
*
|
|
747
|
+
* @param input - The prompt string or object with prompt
|
|
748
|
+
* @returns ImageStreamResult with events and final result
|
|
749
|
+
*/
|
|
750
|
+
stream?(input: ImageInput): ImageStreamResult;
|
|
751
|
+
/**
|
|
752
|
+
* Edit an existing image (if supported).
|
|
753
|
+
* Only available when capabilities.edit is true.
|
|
754
|
+
*
|
|
755
|
+
* @param input - Edit input with image, optional mask, and prompt
|
|
756
|
+
* @returns Promise resolving to the edited images
|
|
757
|
+
*/
|
|
758
|
+
edit?(input: ImageEditInput): Promise<ImageResult>;
|
|
759
|
+
/** The bound image model */
|
|
760
|
+
readonly model: BoundImageModel<TParams>;
|
|
761
|
+
/** Current parameters */
|
|
762
|
+
readonly params: TParams | undefined;
|
|
763
|
+
/** Model capabilities */
|
|
764
|
+
readonly capabilities: ImageCapabilities;
|
|
765
|
+
}
|
|
766
|
+
/**
|
|
767
|
+
* Request passed from image() core to providers for generation.
|
|
768
|
+
* @internal
|
|
769
|
+
*/
|
|
770
|
+
interface ImageRequest<TParams = unknown> {
|
|
771
|
+
/** Generation prompt */
|
|
772
|
+
prompt: string;
|
|
773
|
+
/** Provider-specific parameters (passed through unchanged) */
|
|
774
|
+
params?: TParams;
|
|
775
|
+
/** Provider infrastructure config */
|
|
776
|
+
config: ProviderConfig;
|
|
777
|
+
/** Abort signal for cancellation */
|
|
778
|
+
signal?: AbortSignal;
|
|
779
|
+
}
|
|
780
|
+
/**
|
|
781
|
+
* Request passed to providers for edit operations.
|
|
782
|
+
* @internal
|
|
783
|
+
*/
|
|
784
|
+
interface ImageEditRequest<TParams = unknown> {
|
|
785
|
+
/** Base image to edit */
|
|
786
|
+
image: Image;
|
|
787
|
+
/** Edit mask */
|
|
788
|
+
mask?: Image;
|
|
789
|
+
/** Edit instruction prompt */
|
|
790
|
+
prompt: string;
|
|
791
|
+
/** Provider-specific parameters */
|
|
792
|
+
params?: TParams;
|
|
793
|
+
/** Provider infrastructure config */
|
|
794
|
+
config: ProviderConfig;
|
|
795
|
+
/** Abort signal for cancellation */
|
|
796
|
+
signal?: AbortSignal;
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
* Response from provider's generate or edit method.
|
|
800
|
+
* @internal
|
|
801
|
+
*/
|
|
802
|
+
interface ImageResponse {
|
|
803
|
+
/** Generated images */
|
|
804
|
+
images: GeneratedImage[];
|
|
805
|
+
/** Provider-specific response metadata */
|
|
806
|
+
metadata?: Record<string, unknown>;
|
|
807
|
+
/** Usage information */
|
|
808
|
+
usage?: ImageUsage;
|
|
809
|
+
}
|
|
810
|
+
/**
|
|
811
|
+
* Raw provider stream result.
|
|
812
|
+
* An async iterable of ImageStreamEvent with a response promise.
|
|
813
|
+
* @internal
|
|
814
|
+
*/
|
|
815
|
+
interface ImageProviderStreamResult extends AsyncIterable<ImageStreamEvent> {
|
|
816
|
+
/** Promise resolving to the complete response */
|
|
817
|
+
readonly response: Promise<ImageResponse>;
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
* Bound image model - full definition.
|
|
821
|
+
*
|
|
822
|
+
* Represents an image model bound to a specific provider and model ID,
|
|
823
|
+
* ready to execute generation requests.
|
|
824
|
+
*
|
|
825
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
826
|
+
*/
|
|
827
|
+
interface BoundImageModel<TParams = unknown> {
|
|
828
|
+
/** The model identifier */
|
|
829
|
+
readonly modelId: string;
|
|
830
|
+
/** Reference to the parent provider */
|
|
831
|
+
readonly provider: ImageProvider<TParams>;
|
|
832
|
+
/** Model capabilities */
|
|
833
|
+
readonly capabilities: ImageCapabilities;
|
|
834
|
+
/**
|
|
835
|
+
* Generate images from a prompt.
|
|
836
|
+
*
|
|
837
|
+
* @param request - The generation request
|
|
838
|
+
* @returns Promise resolving to the response
|
|
839
|
+
*/
|
|
840
|
+
generate(request: ImageRequest<TParams>): Promise<ImageResponse>;
|
|
841
|
+
/**
|
|
842
|
+
* Stream image generation (optional).
|
|
843
|
+
*
|
|
844
|
+
* @param request - The generation request
|
|
845
|
+
* @returns Stream result with events and final response
|
|
846
|
+
*/
|
|
847
|
+
stream?(request: ImageRequest<TParams>): ImageProviderStreamResult;
|
|
848
|
+
/**
|
|
849
|
+
* Edit an image (optional).
|
|
850
|
+
*
|
|
851
|
+
* @param request - The edit request
|
|
852
|
+
* @returns Promise resolving to the response
|
|
853
|
+
*/
|
|
854
|
+
edit?(request: ImageEditRequest<TParams>): Promise<ImageResponse>;
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Image Handler interface for providers.
|
|
858
|
+
*
|
|
859
|
+
* Implemented by providers to enable image generation capabilities.
|
|
860
|
+
*
|
|
861
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
862
|
+
*/
|
|
863
|
+
interface ImageHandler$1<TParams = unknown> {
|
|
864
|
+
/**
|
|
865
|
+
* Binds a model ID to create an executable image model.
|
|
866
|
+
*
|
|
867
|
+
* @param modelId - The model identifier to bind
|
|
868
|
+
* @returns A bound image model ready for generation
|
|
869
|
+
*/
|
|
870
|
+
bind(modelId: string): BoundImageModel<TParams>;
|
|
871
|
+
/**
|
|
872
|
+
* Sets the parent provider reference.
|
|
873
|
+
* Called by createProvider() after the provider is constructed.
|
|
874
|
+
*
|
|
875
|
+
* @param provider - The parent provider
|
|
876
|
+
* @internal
|
|
877
|
+
*/
|
|
878
|
+
_setProvider?(provider: ImageProvider<TParams>): void;
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
/**
|
|
882
|
+
* @fileoverview Provider types for AI service integrations.
|
|
883
|
+
*
|
|
884
|
+
* Defines the interfaces for provider factories, modality handlers,
|
|
885
|
+
* and configuration options for connecting to various AI providers.
|
|
886
|
+
*
|
|
887
|
+
* @module types/provider
|
|
888
|
+
*/
|
|
889
|
+
|
|
890
|
+
/**
|
|
891
|
+
* API key strategy interface for managing multiple keys.
|
|
892
|
+
*
|
|
893
|
+
* Implement this interface to provide custom key rotation or
|
|
894
|
+
* selection logic when working with multiple API keys.
|
|
895
|
+
*
|
|
896
|
+
* @example
|
|
897
|
+
* ```typescript
|
|
898
|
+
* class RoundRobinKeys implements KeyStrategy {
|
|
899
|
+
* private keys: string[];
|
|
900
|
+
* private index = 0;
|
|
901
|
+
*
|
|
902
|
+
* constructor(keys: string[]) {
|
|
903
|
+
* this.keys = keys;
|
|
904
|
+
* }
|
|
905
|
+
*
|
|
906
|
+
* getKey(): string {
|
|
907
|
+
* const key = this.keys[this.index];
|
|
908
|
+
* this.index = (this.index + 1) % this.keys.length;
|
|
909
|
+
* return key;
|
|
910
|
+
* }
|
|
911
|
+
* }
|
|
912
|
+
* ```
|
|
913
|
+
*/
|
|
914
|
+
interface KeyStrategy {
|
|
915
|
+
/**
|
|
916
|
+
* Gets the next API key to use for a request.
|
|
917
|
+
*
|
|
918
|
+
* @returns The API key string, or a Promise resolving to it
|
|
919
|
+
*/
|
|
920
|
+
getKey(): string | Promise<string>;
|
|
921
|
+
}
|
|
922
|
+
/**
|
|
923
|
+
* Retry strategy interface for handling request failures.
|
|
924
|
+
*
|
|
925
|
+
* Implement this interface to provide custom retry logic for
|
|
926
|
+
* handling rate limits, transient errors, and other failures.
|
|
927
|
+
*
|
|
928
|
+
* @example
|
|
929
|
+
* ```typescript
|
|
930
|
+
* class ExponentialBackoff implements RetryStrategy {
|
|
931
|
+
* private maxAttempts = 5;
|
|
932
|
+
* private baseDelay = 1000;
|
|
933
|
+
*
|
|
934
|
+
* onRetry(error: UPPError, attempt: number): number | null {
|
|
935
|
+
* if (attempt > this.maxAttempts) return null;
|
|
936
|
+
* if (error.code !== 'RATE_LIMITED') return null;
|
|
937
|
+
* return this.baseDelay * Math.pow(2, attempt - 1);
|
|
938
|
+
* }
|
|
939
|
+
* }
|
|
940
|
+
* ```
|
|
941
|
+
*/
|
|
942
|
+
interface RetryStrategy {
|
|
943
|
+
/**
|
|
944
|
+
* Called when a request fails with a retryable error.
|
|
945
|
+
*
|
|
946
|
+
* @param error - The error that occurred
|
|
947
|
+
* @param attempt - The attempt number (1 = first retry)
|
|
948
|
+
* @returns Delay in ms before retrying, or null to stop retrying
|
|
949
|
+
*/
|
|
950
|
+
onRetry(error: UPPError, attempt: number): number | null | Promise<number | null>;
|
|
951
|
+
/**
|
|
952
|
+
* Called before each request. Can be used to implement pre-emptive rate limiting.
|
|
953
|
+
*
|
|
954
|
+
* @returns Delay in ms to wait before making the request, or 0 to proceed immediately
|
|
955
|
+
*/
|
|
956
|
+
beforeRequest?(): number | Promise<number>;
|
|
957
|
+
/**
|
|
958
|
+
* Reset the strategy state (e.g., after a successful request).
|
|
959
|
+
*/
|
|
960
|
+
reset?(): void;
|
|
961
|
+
}
|
|
962
|
+
/**
|
|
963
|
+
* Provider identity shape for structural typing.
|
|
964
|
+
*
|
|
965
|
+
* Used in model input types to accept any provider instance
|
|
966
|
+
* without generic variance constraints.
|
|
967
|
+
*/
|
|
968
|
+
interface ProviderIdentity {
|
|
969
|
+
/** Provider name (e.g., 'openai', 'anthropic') */
|
|
970
|
+
readonly name: string;
|
|
971
|
+
/** Provider version string */
|
|
972
|
+
readonly version: string;
|
|
973
|
+
}
|
|
974
|
+
/**
|
|
975
|
+
* Provider configuration for infrastructure and connection settings.
|
|
976
|
+
*
|
|
977
|
+
* These settings control how requests are made to the provider's API,
|
|
978
|
+
* including authentication, timeouts, and retry behavior.
|
|
979
|
+
*
|
|
980
|
+
* @example
|
|
981
|
+
* ```typescript
|
|
982
|
+
* const config: ProviderConfig = {
|
|
983
|
+
* apiKey: process.env.OPENAI_API_KEY,
|
|
984
|
+
* timeout: 30000,
|
|
985
|
+
* retryStrategy: new ExponentialBackoff()
|
|
986
|
+
* };
|
|
987
|
+
*
|
|
988
|
+
* // Or with a key strategy for key rotation
|
|
989
|
+
* const config: ProviderConfig = {
|
|
990
|
+
* apiKey: new RoundRobinKeys(['sk-1', 'sk-2', 'sk-3']),
|
|
991
|
+
* baseUrl: 'https://custom-proxy.example.com'
|
|
992
|
+
* };
|
|
993
|
+
* ```
|
|
994
|
+
*/
|
|
995
|
+
interface ProviderConfig {
|
|
996
|
+
/**
|
|
997
|
+
* API key for authentication.
|
|
998
|
+
* Can be a string, async function, or KeyStrategy for advanced use cases.
|
|
999
|
+
*/
|
|
1000
|
+
apiKey?: string | (() => string | Promise<string>) | KeyStrategy;
|
|
1001
|
+
/** Override the base API URL (for proxies, local models) */
|
|
1002
|
+
baseUrl?: string;
|
|
1003
|
+
/** Request timeout in milliseconds */
|
|
1004
|
+
timeout?: number;
|
|
1005
|
+
/** Custom fetch implementation (for logging, caching, custom TLS) */
|
|
1006
|
+
fetch?: typeof fetch;
|
|
1007
|
+
/** API version override (provider-specific) */
|
|
1008
|
+
apiVersion?: string;
|
|
1009
|
+
/** Retry strategy for handling failures and rate limits */
|
|
1010
|
+
retryStrategy?: RetryStrategy;
|
|
1011
|
+
/**
|
|
1012
|
+
* Custom headers to include in API requests.
|
|
1013
|
+
*
|
|
1014
|
+
* Use this to pass provider-specific headers such as:
|
|
1015
|
+
* - Anthropic: `anthropic-beta` for beta features
|
|
1016
|
+
* - OpenAI: `OpenAI-Organization`, `OpenAI-Project`
|
|
1017
|
+
* - OpenRouter: `HTTP-Referer`, `X-Title` for attribution
|
|
1018
|
+
* - Ollama: Proxy authentication headers
|
|
1019
|
+
*
|
|
1020
|
+
* @example
|
|
1021
|
+
* ```typescript
|
|
1022
|
+
* const config: ProviderConfig = {
|
|
1023
|
+
* headers: { 'anthropic-beta': 'extended-cache-ttl-2025-04-11' }
|
|
1024
|
+
* };
|
|
1025
|
+
* ```
|
|
1026
|
+
*/
|
|
1027
|
+
headers?: Record<string, string | undefined>;
|
|
1028
|
+
}
|
|
1029
|
+
/**
|
|
1030
|
+
* A reference to a model, created by a provider factory.
|
|
1031
|
+
*
|
|
1032
|
+
* Model references are lightweight objects that identify a model
|
|
1033
|
+
* and its provider, used as input to the llm() function.
|
|
1034
|
+
*
|
|
1035
|
+
* @typeParam TOptions - Provider-specific options type
|
|
1036
|
+
*
|
|
1037
|
+
* @example
|
|
1038
|
+
* ```typescript
|
|
1039
|
+
* const model = openai('gpt-4');
|
|
1040
|
+
* console.log(model.modelId); // 'gpt-4'
|
|
1041
|
+
* console.log(model.provider.name); // 'openai'
|
|
1042
|
+
* ```
|
|
1043
|
+
*/
|
|
1044
|
+
interface ModelReference<TOptions = unknown> {
|
|
1045
|
+
/** The model identifier (e.g., 'gpt-4', 'claude-3-opus') */
|
|
1046
|
+
readonly modelId: string;
|
|
1047
|
+
/** The provider that created this reference */
|
|
1048
|
+
readonly provider: Provider<TOptions>;
|
|
1049
|
+
/**
|
|
1050
|
+
* Optional provider-specific configuration that gets merged into request config.
|
|
1051
|
+
*
|
|
1052
|
+
* This allows providers to store options set at model reference creation time
|
|
1053
|
+
* (e.g., `anthropic('model', { betas: [...] })`) that should be applied to all requests.
|
|
1054
|
+
* The `llm()` factory will merge these into the request config, with explicit config
|
|
1055
|
+
* values taking precedence.
|
|
1056
|
+
*/
|
|
1057
|
+
readonly providerConfig?: Partial<ProviderConfig>;
|
|
1058
|
+
/**
|
|
1059
|
+
* The original options passed when creating this model reference.
|
|
1060
|
+
*
|
|
1061
|
+
* Used by providers with multiple LLM handlers (e.g., OpenAI with responses/completions APIs)
|
|
1062
|
+
* to resolve the correct handler at request time, avoiding race conditions from shared state.
|
|
1063
|
+
*/
|
|
1064
|
+
readonly options?: TOptions;
|
|
1065
|
+
}
|
|
1066
|
+
/**
|
|
1067
|
+
* LLM handler interface for providers.
|
|
1068
|
+
*
|
|
1069
|
+
* Implemented by providers to enable language model capabilities.
|
|
1070
|
+
*
|
|
1071
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
1072
|
+
* @internal
|
|
1073
|
+
*/
|
|
1074
|
+
interface LLMHandler<TParams = unknown> {
|
|
1075
|
+
/**
|
|
1076
|
+
* Binds a model ID to create an executable model instance.
|
|
1077
|
+
*
|
|
1078
|
+
* @param modelId - The model identifier to bind
|
|
1079
|
+
* @returns A bound LLM model ready for inference
|
|
1080
|
+
*/
|
|
1081
|
+
bind(modelId: string): BoundLLMModel<TParams>;
|
|
1082
|
+
/**
|
|
1083
|
+
* Sets the parent provider reference.
|
|
1084
|
+
* Called by createProvider() after the provider is constructed.
|
|
1085
|
+
*
|
|
1086
|
+
* @param provider - The parent provider
|
|
1087
|
+
* @internal
|
|
1088
|
+
*/
|
|
1089
|
+
_setProvider?(provider: LLMProvider<TParams>): void;
|
|
1090
|
+
}
|
|
1091
|
+
/**
|
|
1092
|
+
* Embedding handler interface for providers.
|
|
1093
|
+
*
|
|
1094
|
+
* Implemented by providers to enable embedding capabilities.
|
|
1095
|
+
*
|
|
1096
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
1097
|
+
* @internal
|
|
1098
|
+
*/
|
|
1099
|
+
interface EmbeddingHandler<TParams = unknown> {
|
|
1100
|
+
/** Supported input types for embeddings */
|
|
1101
|
+
readonly supportedInputs: ('text' | 'image')[];
|
|
1102
|
+
/**
|
|
1103
|
+
* Binds a model ID to create an executable embedding model.
|
|
1104
|
+
*
|
|
1105
|
+
* @param modelId - The model identifier to bind
|
|
1106
|
+
* @returns A bound embedding model ready for use
|
|
1107
|
+
*/
|
|
1108
|
+
bind(modelId: string): BoundEmbeddingModel<TParams>;
|
|
1109
|
+
/**
|
|
1110
|
+
* Sets the parent provider reference.
|
|
1111
|
+
*
|
|
1112
|
+
* @param provider - The parent provider
|
|
1113
|
+
* @internal
|
|
1114
|
+
*/
|
|
1115
|
+
_setProvider?(provider: EmbeddingProvider<TParams>): void;
|
|
1116
|
+
}
|
|
1117
|
+
/**
|
|
1118
|
+
* Image handler interface for providers.
|
|
1119
|
+
*
|
|
1120
|
+
* Implemented by providers to enable image generation capabilities.
|
|
1121
|
+
*
|
|
1122
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
1123
|
+
* @internal
|
|
1124
|
+
*/
|
|
1125
|
+
interface ImageHandler<TParams = unknown> {
|
|
1126
|
+
/**
|
|
1127
|
+
* Binds a model ID to create an executable image model.
|
|
1128
|
+
*
|
|
1129
|
+
* @param modelId - The model identifier to bind
|
|
1130
|
+
* @returns A bound image model ready for generation
|
|
1131
|
+
*/
|
|
1132
|
+
bind(modelId: string): BoundImageModel<TParams>;
|
|
1133
|
+
/**
|
|
1134
|
+
* Sets the parent provider reference.
|
|
1135
|
+
*
|
|
1136
|
+
* @param provider - The parent provider
|
|
1137
|
+
* @internal
|
|
1138
|
+
*/
|
|
1139
|
+
_setProvider?(provider: ImageProvider<TParams>): void;
|
|
1140
|
+
}
|
|
1141
|
+
/**
|
|
1142
|
+
* Bound LLM model interface (forward declaration).
|
|
1143
|
+
*
|
|
1144
|
+
* Full definition is in llm.ts to avoid circular dependencies.
|
|
1145
|
+
*
|
|
1146
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
1147
|
+
*/
|
|
1148
|
+
interface BoundLLMModel<TParams = unknown> {
|
|
1149
|
+
/** The model identifier */
|
|
1150
|
+
readonly modelId: string;
|
|
1151
|
+
/** Reference to the parent provider */
|
|
1152
|
+
readonly provider: LLMProvider<TParams>;
|
|
1153
|
+
}
|
|
1154
|
+
/**
|
|
1155
|
+
* Bound embedding model interface.
|
|
1156
|
+
*
|
|
1157
|
+
* Represents an embedding model bound to a specific model ID,
|
|
1158
|
+
* ready to generate embeddings.
|
|
1159
|
+
*
|
|
1160
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
1161
|
+
*/
|
|
1162
|
+
interface BoundEmbeddingModel<TParams = unknown> {
|
|
1163
|
+
/** The model identifier */
|
|
1164
|
+
readonly modelId: string;
|
|
1165
|
+
/** Reference to the parent provider */
|
|
1166
|
+
readonly provider: EmbeddingProvider<TParams>;
|
|
1167
|
+
/** Maximum number of inputs per batch request */
|
|
1168
|
+
readonly maxBatchSize: number;
|
|
1169
|
+
/** Maximum length of input text in tokens */
|
|
1170
|
+
readonly maxInputLength: number;
|
|
1171
|
+
/** Output embedding dimensions */
|
|
1172
|
+
readonly dimensions: number;
|
|
1173
|
+
/**
|
|
1174
|
+
* Execute embedding request.
|
|
1175
|
+
*
|
|
1176
|
+
* @param request - The embedding request
|
|
1177
|
+
* @returns Promise resolving to embedding response
|
|
1178
|
+
*/
|
|
1179
|
+
embed(request: EmbeddingRequest<TParams>): Promise<EmbeddingResponse>;
|
|
1180
|
+
}
|
|
1181
|
+
/**
|
|
1182
|
+
* Request passed to provider's embed method.
|
|
1183
|
+
* @internal
|
|
1184
|
+
*/
|
|
1185
|
+
interface EmbeddingRequest<TParams = unknown> {
|
|
1186
|
+
/** Inputs to embed */
|
|
1187
|
+
inputs: EmbeddingInput[];
|
|
1188
|
+
/** Provider-specific parameters (passed through unchanged) */
|
|
1189
|
+
params?: TParams;
|
|
1190
|
+
/** Provider infrastructure config */
|
|
1191
|
+
config: ProviderConfig;
|
|
1192
|
+
/** Abort signal for cancellation */
|
|
1193
|
+
signal?: AbortSignal;
|
|
1194
|
+
}
|
|
1195
|
+
/**
|
|
1196
|
+
* Response from provider's embed method.
|
|
1197
|
+
* @internal
|
|
1198
|
+
*/
|
|
1199
|
+
interface EmbeddingResponse {
|
|
1200
|
+
/** Embedding vectors */
|
|
1201
|
+
embeddings: EmbeddingVector[];
|
|
1202
|
+
/** Aggregate usage */
|
|
1203
|
+
usage: EmbeddingUsage;
|
|
1204
|
+
/** Provider-specific response metadata */
|
|
1205
|
+
metadata?: Record<string, unknown>;
|
|
1206
|
+
}
|
|
1207
|
+
/**
|
|
1208
|
+
* Single vector from provider response.
|
|
1209
|
+
* @internal
|
|
1210
|
+
*/
|
|
1211
|
+
interface EmbeddingVector {
|
|
1212
|
+
/** The embedding vector (floats or base64 string) */
|
|
1213
|
+
vector: number[] | string;
|
|
1214
|
+
/** Index in input array */
|
|
1215
|
+
index: number;
|
|
1216
|
+
/** Token count for this input */
|
|
1217
|
+
tokens?: number;
|
|
1218
|
+
/** Provider-specific per-embedding metadata */
|
|
1219
|
+
metadata?: Record<string, unknown>;
|
|
1220
|
+
}
|
|
1221
|
+
/**
|
|
1222
|
+
* Usage statistics for embedding operations.
|
|
1223
|
+
*/
|
|
1224
|
+
interface EmbeddingUsage {
|
|
1225
|
+
/** Total tokens processed */
|
|
1226
|
+
totalTokens: number;
|
|
1227
|
+
}
|
|
1228
|
+
/**
|
|
1229
|
+
* Valid input types for embedding.
|
|
1230
|
+
*/
|
|
1231
|
+
type EmbeddingInput = string | {
|
|
1232
|
+
type: 'text';
|
|
1233
|
+
text: string;
|
|
1234
|
+
} | {
|
|
1235
|
+
type: 'image';
|
|
1236
|
+
source: unknown;
|
|
1237
|
+
mimeType: string;
|
|
1238
|
+
};
|
|
1239
|
+
/**
|
|
1240
|
+
* Bound image model interface.
|
|
1241
|
+
*
|
|
1242
|
+
* Represents an image generation model bound to a specific model ID.
|
|
1243
|
+
*
|
|
1244
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
1245
|
+
*/
|
|
1246
|
+
/**
|
|
1247
|
+
* Provider factory function with metadata and modality handlers.
|
|
1248
|
+
*
|
|
1249
|
+
* The Provider interface represents a callable function that creates
|
|
1250
|
+
* model references, along with metadata and modality-specific handlers.
|
|
1251
|
+
*
|
|
1252
|
+
* @typeParam TOptions - Provider-specific options passed to the factory
|
|
1253
|
+
*
|
|
1254
|
+
* @example
|
|
1255
|
+
* ```typescript
|
|
1256
|
+
* // Using a provider
|
|
1257
|
+
* const model = openai('gpt-4', { temperature: 0.7 });
|
|
1258
|
+
*
|
|
1259
|
+
* // Accessing provider metadata
|
|
1260
|
+
* console.log(openai.name); // 'openai'
|
|
1261
|
+
* console.log(openai.version); // '1.0.0'
|
|
1262
|
+
* ```
|
|
1263
|
+
*
|
|
1264
|
+
* @remarks
|
|
1265
|
+
* Providers are intended to be used with `llm()`, `embedding()`, or `image()`.
|
|
1266
|
+
* Direct handler access is not part of the public API.
|
|
1267
|
+
*/
|
|
1268
|
+
interface Provider<TOptions = unknown> extends ProviderIdentity {
|
|
1269
|
+
/**
|
|
1270
|
+
* Creates a model reference with optional provider-specific options.
|
|
1271
|
+
*
|
|
1272
|
+
* @param modelId - The model identifier
|
|
1273
|
+
* @param options - Provider-specific options
|
|
1274
|
+
* @returns A model reference for use with llm() or other functions
|
|
1275
|
+
*/
|
|
1276
|
+
(modelId: string, options?: TOptions): ModelReference<TOptions>;
|
|
1277
|
+
/** Provider name (e.g., 'openai', 'anthropic') */
|
|
1278
|
+
readonly name: string;
|
|
1279
|
+
/** Provider version string */
|
|
1280
|
+
readonly version: string;
|
|
1281
|
+
}
|
|
1282
|
+
/**
|
|
1283
|
+
* Provider with LLM modality support.
|
|
1284
|
+
*
|
|
1285
|
+
* Type alias for providers that support language model inference.
|
|
1286
|
+
*
|
|
1287
|
+
* @typeParam TParams - Model-specific parameters type
|
|
1288
|
+
* @typeParam TOptions - Provider-specific options type
|
|
1289
|
+
*/
|
|
1290
|
+
type LLMProvider<TParams = unknown, TOptions = unknown> = Provider<TOptions> & {
|
|
1291
|
+
/** @internal */
|
|
1292
|
+
readonly __params?: TParams;
|
|
1293
|
+
};
|
|
1294
|
+
/**
|
|
1295
|
+
* Provider with Embedding modality support.
|
|
1296
|
+
*
|
|
1297
|
+
* Type alias for providers that support embedding generation.
|
|
1298
|
+
*
|
|
1299
|
+
* @typeParam TParams - Model-specific parameters type
|
|
1300
|
+
* @typeParam TOptions - Provider-specific options type
|
|
1301
|
+
*/
|
|
1302
|
+
type EmbeddingProvider<TParams = unknown, TOptions = unknown> = Provider<TOptions> & {
|
|
1303
|
+
/** @internal */
|
|
1304
|
+
readonly __params?: TParams;
|
|
1305
|
+
};
|
|
1306
|
+
/**
|
|
1307
|
+
* Provider with Image modality support.
|
|
1308
|
+
*
|
|
1309
|
+
* Type alias for providers that support image generation.
|
|
1310
|
+
*
|
|
1311
|
+
* @typeParam TParams - Model-specific parameters type
|
|
1312
|
+
* @typeParam TOptions - Provider-specific options type
|
|
1313
|
+
*/
|
|
1314
|
+
type ImageProvider<TParams = unknown, TOptions = unknown> = Provider<TOptions> & {
|
|
1315
|
+
/** @internal */
|
|
1316
|
+
readonly __params?: TParams;
|
|
1317
|
+
};
|
|
1318
|
+
|
|
1319
|
+
export { type ImageHandler$1 as $, type AssistantContent as A, type BoundEmbeddingModel as B, type ContentBlock as C, type ImageInput as D, type EmbeddingInput as E, type ImageEditInput as F, type ImageGenerateOptions as G, type GeneratedImage as H, type ImageOptions as I, type ImageUsage as J, type KeyStrategy as K, type LLMProvider as L, type ModelReference as M, type ImageResult as N, type ImageStreamEvent as O, type ProviderIdentity as P, type ImageStreamResult as Q, type RetryStrategy as R, type ImageCapabilities as S, type TextBlock as T, type UserContent as U, type VideoBlock as V, type ImageRequest as W, type ImageEditRequest as X, type ImageResponse as Y, type ImageProviderStreamResult as Z, type BoundImageModel as _, type ProviderConfig as a, type ImageModelInput as a0, type EmbeddingUsage as b, type ImageInstance as c, type LLMHandler as d, type EmbeddingHandler as e, type ImageHandler as f, type Provider as g, Image as h, UPPError as i, type ErrorCode as j, type Modality as k, type ImageBlock as l, type AudioBlock as m, type BinaryBlock as n, type ImageSource as o, isTextBlock as p, isImageBlock as q, isAudioBlock as r, isVideoBlock as s, text as t, isBinaryBlock as u, type EmbeddingProvider as v, type ImageProvider as w, type EmbeddingRequest as x, type EmbeddingResponse as y, type EmbeddingVector as z };
|