@providerprotocol/ai 0.0.26 → 0.0.28
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 +1 -1
- package/dist/anthropic/index.js +38 -1
- package/dist/anthropic/index.js.map +1 -1
- package/dist/{chunk-6AZVUI6H.js → chunk-ILR2D5PN.js} +7 -1
- package/dist/chunk-ILR2D5PN.js.map +1 -0
- package/dist/{chunk-MKDLXV4O.js → chunk-NSE7QN3P.js} +1 -1
- package/dist/chunk-NSE7QN3P.js.map +1 -0
- package/dist/embedding-DtyOFIsS.d.ts +158 -0
- package/dist/google/index.d.ts +1 -1
- package/dist/google/index.js +41 -4
- package/dist/google/index.js.map +1 -1
- package/dist/http/index.d.ts +2 -2
- package/dist/index.d.ts +430 -669
- package/dist/index.js +627 -3
- package/dist/index.js.map +1 -1
- package/dist/llm-DgDEy9il.d.ts +3118 -0
- package/dist/ollama/index.d.ts +1 -1
- package/dist/ollama/index.js +2 -1
- package/dist/ollama/index.js.map +1 -1
- package/dist/openai/index.d.ts +1 -1
- package/dist/openai/index.js +70 -3
- package/dist/openai/index.js.map +1 -1
- package/dist/openrouter/index.d.ts +20 -2
- package/dist/openrouter/index.js +134 -13
- package/dist/openrouter/index.js.map +1 -1
- package/dist/proxy/index.d.ts +220 -3
- package/dist/proxy/index.js +817 -22
- package/dist/proxy/index.js.map +1 -1
- package/dist/{retry-DTfjXXPh.d.ts → retry-DXLQnTuU.d.ts} +1 -1
- package/dist/xai/index.d.ts +1 -1
- package/dist/xai/index.js +7 -3
- package/dist/xai/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-6AZVUI6H.js.map +0 -1
- package/dist/chunk-MKDLXV4O.js.map +0 -1
- package/dist/provider-x4RocsnK.d.ts +0 -1474
- package/dist/stream-ITNFNnO4.d.ts +0 -1080
|
@@ -0,0 +1,3118 @@
|
|
|
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
|
+
* Content block type constants.
|
|
12
|
+
*
|
|
13
|
+
* Use these constants instead of raw strings for type-safe content handling:
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { ContentBlockType } from 'upp';
|
|
18
|
+
*
|
|
19
|
+
* if (block.type === ContentBlockType.Text) {
|
|
20
|
+
* console.log(block.text);
|
|
21
|
+
* } else if (block.type === ContentBlockType.Image) {
|
|
22
|
+
* console.log(block.mimeType);
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
declare const ContentBlockType: {
|
|
27
|
+
/** Text content */
|
|
28
|
+
readonly Text: "text";
|
|
29
|
+
/** Reasoning/thinking content from extended thinking models */
|
|
30
|
+
readonly Reasoning: "reasoning";
|
|
31
|
+
/** Image content */
|
|
32
|
+
readonly Image: "image";
|
|
33
|
+
/** Document content (PDFs, text files) */
|
|
34
|
+
readonly Document: "document";
|
|
35
|
+
/** Audio content */
|
|
36
|
+
readonly Audio: "audio";
|
|
37
|
+
/** Video content */
|
|
38
|
+
readonly Video: "video";
|
|
39
|
+
/** Binary/arbitrary data content */
|
|
40
|
+
readonly Binary: "binary";
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Content block type discriminator union.
|
|
44
|
+
*
|
|
45
|
+
* This type is derived from {@link ContentBlockType} constants.
|
|
46
|
+
*/
|
|
47
|
+
type ContentBlockType = (typeof ContentBlockType)[keyof typeof ContentBlockType];
|
|
48
|
+
/**
|
|
49
|
+
* Image source type constants.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* import { ImageSourceType } from 'upp';
|
|
54
|
+
*
|
|
55
|
+
* if (source.type === ImageSourceType.Base64) {
|
|
56
|
+
* // Handle base64 encoded image
|
|
57
|
+
* } else if (source.type === ImageSourceType.Url) {
|
|
58
|
+
* // Handle URL reference
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
declare const ImageSourceType: {
|
|
63
|
+
/** Base64-encoded image data */
|
|
64
|
+
readonly Base64: "base64";
|
|
65
|
+
/** URL reference to image */
|
|
66
|
+
readonly Url: "url";
|
|
67
|
+
/** Raw bytes image data */
|
|
68
|
+
readonly Bytes: "bytes";
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Image source type discriminator union.
|
|
72
|
+
*
|
|
73
|
+
* This type is derived from {@link ImageSourceType} constants.
|
|
74
|
+
*/
|
|
75
|
+
type ImageSourceType = (typeof ImageSourceType)[keyof typeof ImageSourceType];
|
|
76
|
+
/**
|
|
77
|
+
* Image source variants for ImageBlock.
|
|
78
|
+
*
|
|
79
|
+
* Images can be provided as base64-encoded strings, URLs, or raw bytes.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* // Base64 encoded image
|
|
84
|
+
* const base64Source: ImageSource = {
|
|
85
|
+
* type: 'base64',
|
|
86
|
+
* data: 'iVBORw0KGgo...'
|
|
87
|
+
* };
|
|
88
|
+
*
|
|
89
|
+
* // URL reference
|
|
90
|
+
* const urlSource: ImageSource = {
|
|
91
|
+
* type: 'url',
|
|
92
|
+
* url: 'https://example.com/image.png'
|
|
93
|
+
* };
|
|
94
|
+
*
|
|
95
|
+
* // Raw bytes
|
|
96
|
+
* const bytesSource: ImageSource = {
|
|
97
|
+
* type: 'bytes',
|
|
98
|
+
* data: new Uint8Array([...])
|
|
99
|
+
* };
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
type ImageSource = {
|
|
103
|
+
type: 'base64';
|
|
104
|
+
data: string;
|
|
105
|
+
} | {
|
|
106
|
+
type: 'url';
|
|
107
|
+
url: string;
|
|
108
|
+
} | {
|
|
109
|
+
type: 'bytes';
|
|
110
|
+
data: Uint8Array;
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Document source type constants.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```typescript
|
|
117
|
+
* import { DocumentSourceType } from 'upp';
|
|
118
|
+
*
|
|
119
|
+
* if (source.type === DocumentSourceType.Base64) {
|
|
120
|
+
* // Handle base64 encoded document (PDF)
|
|
121
|
+
* } else if (source.type === DocumentSourceType.Url) {
|
|
122
|
+
* // Handle URL reference (PDF)
|
|
123
|
+
* } else if (source.type === DocumentSourceType.Text) {
|
|
124
|
+
* // Handle plain text document
|
|
125
|
+
* }
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
declare const DocumentSourceType: {
|
|
129
|
+
/** Base64-encoded document data (for PDFs) */
|
|
130
|
+
readonly Base64: "base64";
|
|
131
|
+
/** URL reference to document (for PDFs) */
|
|
132
|
+
readonly Url: "url";
|
|
133
|
+
/** Plain text document content */
|
|
134
|
+
readonly Text: "text";
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Document source type discriminator union.
|
|
138
|
+
*
|
|
139
|
+
* This type is derived from {@link DocumentSourceType} constants.
|
|
140
|
+
*/
|
|
141
|
+
type DocumentSourceType = (typeof DocumentSourceType)[keyof typeof DocumentSourceType];
|
|
142
|
+
/**
|
|
143
|
+
* Document source variants for DocumentBlock.
|
|
144
|
+
*
|
|
145
|
+
* Documents can be provided as base64-encoded data (PDFs), URLs (PDFs), or plain text.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* // Base64 encoded PDF
|
|
150
|
+
* const base64Source: DocumentSource = {
|
|
151
|
+
* type: 'base64',
|
|
152
|
+
* data: 'JVBERi0xLjQK...'
|
|
153
|
+
* };
|
|
154
|
+
*
|
|
155
|
+
* // URL reference to PDF
|
|
156
|
+
* const urlSource: DocumentSource = {
|
|
157
|
+
* type: 'url',
|
|
158
|
+
* url: 'https://example.com/document.pdf'
|
|
159
|
+
* };
|
|
160
|
+
*
|
|
161
|
+
* // Plain text document
|
|
162
|
+
* const textSource: DocumentSource = {
|
|
163
|
+
* type: 'text',
|
|
164
|
+
* data: 'This is the document content...'
|
|
165
|
+
* };
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
type DocumentSource = {
|
|
169
|
+
type: 'base64';
|
|
170
|
+
data: string;
|
|
171
|
+
} | {
|
|
172
|
+
type: 'url';
|
|
173
|
+
url: string;
|
|
174
|
+
} | {
|
|
175
|
+
type: 'text';
|
|
176
|
+
data: string;
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* Text content block.
|
|
180
|
+
*
|
|
181
|
+
* The most common content block type, containing plain text content.
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```typescript
|
|
185
|
+
* const textBlock: TextBlock = {
|
|
186
|
+
* type: 'text',
|
|
187
|
+
* text: 'Hello, world!'
|
|
188
|
+
* };
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
interface TextBlock {
|
|
192
|
+
/** Discriminator for text blocks */
|
|
193
|
+
type: 'text';
|
|
194
|
+
/** The text content */
|
|
195
|
+
text: string;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Reasoning content block.
|
|
199
|
+
*
|
|
200
|
+
* Contains model reasoning/thinking from extended thinking or chain-of-thought.
|
|
201
|
+
* This content represents the model's internal reasoning process.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ```typescript
|
|
205
|
+
* const reasoningBlock: ReasoningBlock = {
|
|
206
|
+
* type: 'reasoning',
|
|
207
|
+
* text: 'Let me think about this step by step...'
|
|
208
|
+
* };
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
interface ReasoningBlock {
|
|
212
|
+
/** Discriminator for reasoning blocks */
|
|
213
|
+
type: 'reasoning';
|
|
214
|
+
/** The reasoning/thinking text */
|
|
215
|
+
text: string;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Image content block.
|
|
219
|
+
*
|
|
220
|
+
* Contains an image with its source data and metadata.
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```typescript
|
|
224
|
+
* const imageBlock: ImageBlock = {
|
|
225
|
+
* type: 'image',
|
|
226
|
+
* source: { type: 'url', url: 'https://example.com/photo.jpg' },
|
|
227
|
+
* mimeType: 'image/jpeg',
|
|
228
|
+
* width: 1920,
|
|
229
|
+
* height: 1080
|
|
230
|
+
* };
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
233
|
+
interface ImageBlock {
|
|
234
|
+
/** Discriminator for image blocks */
|
|
235
|
+
type: 'image';
|
|
236
|
+
/** The image data source */
|
|
237
|
+
source: ImageSource;
|
|
238
|
+
/** MIME type of the image (e.g., 'image/png', 'image/jpeg') */
|
|
239
|
+
mimeType: string;
|
|
240
|
+
/** Image width in pixels */
|
|
241
|
+
width?: number;
|
|
242
|
+
/** Image height in pixels */
|
|
243
|
+
height?: number;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Document content block.
|
|
247
|
+
*
|
|
248
|
+
* Contains a document (PDF or plain text) with its source and metadata.
|
|
249
|
+
* Supports PDF documents via base64 encoding or URL, and plain text content.
|
|
250
|
+
*
|
|
251
|
+
* @example
|
|
252
|
+
* ```typescript
|
|
253
|
+
* // PDF document from base64
|
|
254
|
+
* const pdfBlock: DocumentBlock = {
|
|
255
|
+
* type: 'document',
|
|
256
|
+
* source: { type: 'base64', data: 'JVBERi0xLjQK...' },
|
|
257
|
+
* mimeType: 'application/pdf',
|
|
258
|
+
* title: 'Annual Report'
|
|
259
|
+
* };
|
|
260
|
+
*
|
|
261
|
+
* // Plain text document
|
|
262
|
+
* const textDoc: DocumentBlock = {
|
|
263
|
+
* type: 'document',
|
|
264
|
+
* source: { type: 'text', data: 'Document contents here...' },
|
|
265
|
+
* mimeType: 'text/plain'
|
|
266
|
+
* };
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
interface DocumentBlock {
|
|
270
|
+
/** Discriminator for document blocks */
|
|
271
|
+
type: 'document';
|
|
272
|
+
/** The document data source */
|
|
273
|
+
source: DocumentSource;
|
|
274
|
+
/** MIME type of the document ('application/pdf' or 'text/plain') */
|
|
275
|
+
mimeType: string;
|
|
276
|
+
/** Optional document title (used for citations) */
|
|
277
|
+
title?: string;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Audio content block.
|
|
281
|
+
*
|
|
282
|
+
* Contains audio data with its metadata.
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```typescript
|
|
286
|
+
* const audioBlock: AudioBlock = {
|
|
287
|
+
* type: 'audio',
|
|
288
|
+
* data: audioBytes,
|
|
289
|
+
* mimeType: 'audio/mp3',
|
|
290
|
+
* duration: 120.5
|
|
291
|
+
* };
|
|
292
|
+
* ```
|
|
293
|
+
*/
|
|
294
|
+
interface AudioBlock {
|
|
295
|
+
/** Discriminator for audio blocks */
|
|
296
|
+
type: 'audio';
|
|
297
|
+
/** Raw audio data */
|
|
298
|
+
data: Uint8Array;
|
|
299
|
+
/** MIME type of the audio (e.g., 'audio/mp3', 'audio/wav') */
|
|
300
|
+
mimeType: string;
|
|
301
|
+
/** Duration in seconds */
|
|
302
|
+
duration?: number;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Video content block.
|
|
306
|
+
*
|
|
307
|
+
* Contains video data with its metadata.
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
310
|
+
* ```typescript
|
|
311
|
+
* const videoBlock: VideoBlock = {
|
|
312
|
+
* type: 'video',
|
|
313
|
+
* data: videoBytes,
|
|
314
|
+
* mimeType: 'video/mp4',
|
|
315
|
+
* duration: 30,
|
|
316
|
+
* width: 1920,
|
|
317
|
+
* height: 1080
|
|
318
|
+
* };
|
|
319
|
+
* ```
|
|
320
|
+
*/
|
|
321
|
+
interface VideoBlock {
|
|
322
|
+
/** Discriminator for video blocks */
|
|
323
|
+
type: 'video';
|
|
324
|
+
/** Raw video data */
|
|
325
|
+
data: Uint8Array;
|
|
326
|
+
/** MIME type of the video (e.g., 'video/mp4', 'video/webm') */
|
|
327
|
+
mimeType: string;
|
|
328
|
+
/** Duration in seconds */
|
|
329
|
+
duration?: number;
|
|
330
|
+
/** Video width in pixels */
|
|
331
|
+
width?: number;
|
|
332
|
+
/** Video height in pixels */
|
|
333
|
+
height?: number;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Binary content block for arbitrary data.
|
|
337
|
+
*
|
|
338
|
+
* A generic block type for data that doesn't fit other categories.
|
|
339
|
+
*
|
|
340
|
+
* @example
|
|
341
|
+
* ```typescript
|
|
342
|
+
* const binaryBlock: BinaryBlock = {
|
|
343
|
+
* type: 'binary',
|
|
344
|
+
* data: pdfBytes,
|
|
345
|
+
* mimeType: 'application/pdf',
|
|
346
|
+
* metadata: { filename: 'document.pdf', pages: 10 }
|
|
347
|
+
* };
|
|
348
|
+
* ```
|
|
349
|
+
*/
|
|
350
|
+
interface BinaryBlock {
|
|
351
|
+
/** Discriminator for binary blocks */
|
|
352
|
+
type: 'binary';
|
|
353
|
+
/** Raw binary data */
|
|
354
|
+
data: Uint8Array;
|
|
355
|
+
/** MIME type of the data */
|
|
356
|
+
mimeType: string;
|
|
357
|
+
/** Additional metadata about the binary content */
|
|
358
|
+
metadata?: Record<string, unknown>;
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Union of all content block types.
|
|
362
|
+
*
|
|
363
|
+
* Used when a function or property can accept any type of content block.
|
|
364
|
+
*/
|
|
365
|
+
type ContentBlock = TextBlock | ReasoningBlock | ImageBlock | DocumentBlock | AudioBlock | VideoBlock | BinaryBlock;
|
|
366
|
+
/**
|
|
367
|
+
* Content types allowed in user messages.
|
|
368
|
+
*
|
|
369
|
+
* Users can send any type of content block including binary data.
|
|
370
|
+
*/
|
|
371
|
+
type UserContent = TextBlock | ImageBlock | DocumentBlock | AudioBlock | VideoBlock | BinaryBlock;
|
|
372
|
+
/**
|
|
373
|
+
* Content types allowed in assistant messages.
|
|
374
|
+
*
|
|
375
|
+
* Assistants can generate text and media but not arbitrary binary data.
|
|
376
|
+
*/
|
|
377
|
+
type AssistantContent = TextBlock | ReasoningBlock | ImageBlock | AudioBlock | VideoBlock;
|
|
378
|
+
/**
|
|
379
|
+
* Creates a text content block from a string.
|
|
380
|
+
*
|
|
381
|
+
* @param content - The text content
|
|
382
|
+
* @returns A TextBlock containing the provided text
|
|
383
|
+
*
|
|
384
|
+
* @example
|
|
385
|
+
* ```typescript
|
|
386
|
+
* const block = text('Hello, world!');
|
|
387
|
+
* // { type: 'text', text: 'Hello, world!' }
|
|
388
|
+
* ```
|
|
389
|
+
*/
|
|
390
|
+
declare function text(content: string): TextBlock;
|
|
391
|
+
/**
|
|
392
|
+
* Creates a reasoning content block from a string.
|
|
393
|
+
*
|
|
394
|
+
* @param content - The reasoning/thinking content
|
|
395
|
+
* @returns A ReasoningBlock containing the provided text
|
|
396
|
+
*
|
|
397
|
+
* @example
|
|
398
|
+
* ```typescript
|
|
399
|
+
* const block = reasoning('Let me think step by step...');
|
|
400
|
+
* // { type: 'reasoning', text: 'Let me think step by step...' }
|
|
401
|
+
* ```
|
|
402
|
+
*/
|
|
403
|
+
declare function reasoning(content: string): ReasoningBlock;
|
|
404
|
+
/**
|
|
405
|
+
* Type guard for TextBlock.
|
|
406
|
+
*
|
|
407
|
+
* @param block - The content block to check
|
|
408
|
+
* @returns True if the block is a TextBlock
|
|
409
|
+
*
|
|
410
|
+
* @example
|
|
411
|
+
* ```typescript
|
|
412
|
+
* if (isTextBlock(block)) {
|
|
413
|
+
* console.log(block.text);
|
|
414
|
+
* }
|
|
415
|
+
* ```
|
|
416
|
+
*/
|
|
417
|
+
declare function isTextBlock(block: ContentBlock): block is TextBlock;
|
|
418
|
+
/**
|
|
419
|
+
* Type guard for ReasoningBlock.
|
|
420
|
+
*
|
|
421
|
+
* @param block - The content block to check
|
|
422
|
+
* @returns True if the block is a ReasoningBlock
|
|
423
|
+
*
|
|
424
|
+
* @example
|
|
425
|
+
* ```typescript
|
|
426
|
+
* if (isReasoningBlock(block)) {
|
|
427
|
+
* console.log(block.text);
|
|
428
|
+
* }
|
|
429
|
+
* ```
|
|
430
|
+
*/
|
|
431
|
+
declare function isReasoningBlock(block: ContentBlock): block is ReasoningBlock;
|
|
432
|
+
/**
|
|
433
|
+
* Type guard for ImageBlock.
|
|
434
|
+
*
|
|
435
|
+
* @param block - The content block to check
|
|
436
|
+
* @returns True if the block is an ImageBlock
|
|
437
|
+
*
|
|
438
|
+
* @example
|
|
439
|
+
* ```typescript
|
|
440
|
+
* if (isImageBlock(block)) {
|
|
441
|
+
* console.log(block.mimeType, block.width, block.height);
|
|
442
|
+
* }
|
|
443
|
+
* ```
|
|
444
|
+
*/
|
|
445
|
+
declare function isImageBlock(block: ContentBlock): block is ImageBlock;
|
|
446
|
+
/**
|
|
447
|
+
* Type guard for DocumentBlock.
|
|
448
|
+
*
|
|
449
|
+
* @param block - The content block to check
|
|
450
|
+
* @returns True if the block is a DocumentBlock
|
|
451
|
+
*
|
|
452
|
+
* @example
|
|
453
|
+
* ```typescript
|
|
454
|
+
* if (isDocumentBlock(block)) {
|
|
455
|
+
* console.log(block.mimeType, block.title);
|
|
456
|
+
* }
|
|
457
|
+
* ```
|
|
458
|
+
*/
|
|
459
|
+
declare function isDocumentBlock(block: ContentBlock): block is DocumentBlock;
|
|
460
|
+
/**
|
|
461
|
+
* Type guard for AudioBlock.
|
|
462
|
+
*
|
|
463
|
+
* @param block - The content block to check
|
|
464
|
+
* @returns True if the block is an AudioBlock
|
|
465
|
+
*
|
|
466
|
+
* @example
|
|
467
|
+
* ```typescript
|
|
468
|
+
* if (isAudioBlock(block)) {
|
|
469
|
+
* console.log(block.mimeType, block.duration);
|
|
470
|
+
* }
|
|
471
|
+
* ```
|
|
472
|
+
*/
|
|
473
|
+
declare function isAudioBlock(block: ContentBlock): block is AudioBlock;
|
|
474
|
+
/**
|
|
475
|
+
* Type guard for VideoBlock.
|
|
476
|
+
*
|
|
477
|
+
* @param block - The content block to check
|
|
478
|
+
* @returns True if the block is a VideoBlock
|
|
479
|
+
*
|
|
480
|
+
* @example
|
|
481
|
+
* ```typescript
|
|
482
|
+
* if (isVideoBlock(block)) {
|
|
483
|
+
* console.log(block.mimeType, block.duration);
|
|
484
|
+
* }
|
|
485
|
+
* ```
|
|
486
|
+
*/
|
|
487
|
+
declare function isVideoBlock(block: ContentBlock): block is VideoBlock;
|
|
488
|
+
/**
|
|
489
|
+
* Type guard for BinaryBlock.
|
|
490
|
+
*
|
|
491
|
+
* @param block - The content block to check
|
|
492
|
+
* @returns True if the block is a BinaryBlock
|
|
493
|
+
*
|
|
494
|
+
* @example
|
|
495
|
+
* ```typescript
|
|
496
|
+
* if (isBinaryBlock(block)) {
|
|
497
|
+
* console.log(block.mimeType, block.metadata);
|
|
498
|
+
* }
|
|
499
|
+
* ```
|
|
500
|
+
*/
|
|
501
|
+
declare function isBinaryBlock(block: ContentBlock): block is BinaryBlock;
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* @fileoverview JSON Schema types for tool parameters and structured outputs.
|
|
505
|
+
*
|
|
506
|
+
* Provides TypeScript interfaces for defining JSON Schema objects used in
|
|
507
|
+
* LLM tool definitions and structured output specifications.
|
|
508
|
+
*
|
|
509
|
+
* @module types/schema
|
|
510
|
+
*/
|
|
511
|
+
/**
|
|
512
|
+
* Primitive and composite JSON Schema property types.
|
|
513
|
+
*
|
|
514
|
+
* These types correspond to the JSON Schema specification's allowed type values.
|
|
515
|
+
*/
|
|
516
|
+
type JSONSchemaPropertyType =
|
|
517
|
+
/** String values */
|
|
518
|
+
'string'
|
|
519
|
+
/** Floating point numbers */
|
|
520
|
+
| 'number'
|
|
521
|
+
/** Whole numbers */
|
|
522
|
+
| 'integer'
|
|
523
|
+
/** Boolean true/false values */
|
|
524
|
+
| 'boolean'
|
|
525
|
+
/** Ordered lists of values */
|
|
526
|
+
| 'array'
|
|
527
|
+
/** Key-value mappings */
|
|
528
|
+
| 'object'
|
|
529
|
+
/** Explicit null value */
|
|
530
|
+
| 'null';
|
|
531
|
+
/**
|
|
532
|
+
* JSON Schema property definition.
|
|
533
|
+
*
|
|
534
|
+
* Describes a single property within a JSON Schema object, including
|
|
535
|
+
* type constraints, validation rules, and nested structure definitions.
|
|
536
|
+
*
|
|
537
|
+
* @example
|
|
538
|
+
* ```typescript
|
|
539
|
+
* const nameProperty: JSONSchemaProperty = {
|
|
540
|
+
* type: 'string',
|
|
541
|
+
* description: 'User name',
|
|
542
|
+
* minLength: 1,
|
|
543
|
+
* maxLength: 100
|
|
544
|
+
* };
|
|
545
|
+
* ```
|
|
546
|
+
*
|
|
547
|
+
* @example
|
|
548
|
+
* ```typescript
|
|
549
|
+
* const tagsProperty: JSONSchemaProperty = {
|
|
550
|
+
* type: 'array',
|
|
551
|
+
* description: 'List of tags',
|
|
552
|
+
* items: { type: 'string' },
|
|
553
|
+
* minItems: 1,
|
|
554
|
+
* uniqueItems: true
|
|
555
|
+
* };
|
|
556
|
+
* ```
|
|
557
|
+
*/
|
|
558
|
+
interface JSONSchemaProperty {
|
|
559
|
+
/** The JSON type of this property */
|
|
560
|
+
type: JSONSchemaPropertyType;
|
|
561
|
+
/** Human-readable description for the LLM */
|
|
562
|
+
description?: string;
|
|
563
|
+
/** Allowed values (enumeration) */
|
|
564
|
+
enum?: unknown[];
|
|
565
|
+
/** Constant value this property must equal */
|
|
566
|
+
const?: unknown;
|
|
567
|
+
/** Default value if not provided */
|
|
568
|
+
default?: unknown;
|
|
569
|
+
/** Minimum string length (string type only) */
|
|
570
|
+
minLength?: number;
|
|
571
|
+
/** Maximum string length (string type only) */
|
|
572
|
+
maxLength?: number;
|
|
573
|
+
/** Regular expression pattern for validation (string type only) */
|
|
574
|
+
pattern?: string;
|
|
575
|
+
/** Semantic format hint (string type only) */
|
|
576
|
+
format?: 'email' | 'uri' | 'date' | 'date-time' | 'uuid';
|
|
577
|
+
/** Minimum value inclusive (number/integer types only) */
|
|
578
|
+
minimum?: number;
|
|
579
|
+
/** Maximum value inclusive (number/integer types only) */
|
|
580
|
+
maximum?: number;
|
|
581
|
+
/** Minimum value exclusive (number/integer types only) */
|
|
582
|
+
exclusiveMinimum?: number;
|
|
583
|
+
/** Maximum value exclusive (number/integer types only) */
|
|
584
|
+
exclusiveMaximum?: number;
|
|
585
|
+
/** Value must be divisible by this (number/integer types only) */
|
|
586
|
+
multipleOf?: number;
|
|
587
|
+
/** Schema for array elements (array type only) */
|
|
588
|
+
items?: JSONSchemaProperty;
|
|
589
|
+
/** Minimum array length (array type only) */
|
|
590
|
+
minItems?: number;
|
|
591
|
+
/** Maximum array length (array type only) */
|
|
592
|
+
maxItems?: number;
|
|
593
|
+
/** Whether array elements must be unique (array type only) */
|
|
594
|
+
uniqueItems?: boolean;
|
|
595
|
+
/** Nested property definitions (object type only) */
|
|
596
|
+
properties?: Record<string, JSONSchemaProperty>;
|
|
597
|
+
/** List of required property names (object type only) */
|
|
598
|
+
required?: string[];
|
|
599
|
+
/** Whether additional properties are allowed (object type only) */
|
|
600
|
+
additionalProperties?: boolean;
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Root JSON Schema for tool parameters or structured outputs.
|
|
604
|
+
*
|
|
605
|
+
* This is the top-level schema definition used when defining tool
|
|
606
|
+
* parameters or requesting structured output from an LLM.
|
|
607
|
+
*
|
|
608
|
+
* @example
|
|
609
|
+
* ```typescript
|
|
610
|
+
* const weatherToolSchema: JSONSchema = {
|
|
611
|
+
* type: 'object',
|
|
612
|
+
* description: 'Parameters for getting weather information',
|
|
613
|
+
* properties: {
|
|
614
|
+
* location: {
|
|
615
|
+
* type: 'string',
|
|
616
|
+
* description: 'City name or coordinates'
|
|
617
|
+
* },
|
|
618
|
+
* units: {
|
|
619
|
+
* type: 'string',
|
|
620
|
+
* enum: ['celsius', 'fahrenheit'],
|
|
621
|
+
* description: 'Temperature units'
|
|
622
|
+
* }
|
|
623
|
+
* },
|
|
624
|
+
* required: ['location']
|
|
625
|
+
* };
|
|
626
|
+
* ```
|
|
627
|
+
*/
|
|
628
|
+
interface JSONSchema {
|
|
629
|
+
/** Root schemas are always objects */
|
|
630
|
+
type: 'object';
|
|
631
|
+
/** Property definitions for the object */
|
|
632
|
+
properties: Record<string, JSONSchemaProperty>;
|
|
633
|
+
/** List of required property names */
|
|
634
|
+
required?: string[];
|
|
635
|
+
/** Whether additional properties are allowed beyond those defined */
|
|
636
|
+
additionalProperties?: boolean;
|
|
637
|
+
/** Human-readable description of the schema's purpose */
|
|
638
|
+
description?: string;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* @fileoverview Tool types for LLM function calling.
|
|
643
|
+
*
|
|
644
|
+
* Defines the interfaces for registering tools with LLMs, handling
|
|
645
|
+
* tool calls from the model, and managing tool execution strategies.
|
|
646
|
+
*
|
|
647
|
+
* @module types/tool
|
|
648
|
+
*/
|
|
649
|
+
|
|
650
|
+
/**
|
|
651
|
+
* Provider-namespaced metadata for tools.
|
|
652
|
+
*
|
|
653
|
+
* Each provider can attach its own metadata under its namespace,
|
|
654
|
+
* enabling provider-specific features like caching, strict mode, etc.
|
|
655
|
+
*
|
|
656
|
+
* @example
|
|
657
|
+
* ```typescript
|
|
658
|
+
* const metadata: ToolMetadata = {
|
|
659
|
+
* anthropic: { cache_control: { type: 'ephemeral' } },
|
|
660
|
+
* openrouter: { cache_control: { type: 'ephemeral', ttl: '1h' } }
|
|
661
|
+
* };
|
|
662
|
+
* ```
|
|
663
|
+
*/
|
|
664
|
+
interface ToolMetadata {
|
|
665
|
+
[provider: string]: Record<string, unknown> | undefined;
|
|
666
|
+
}
|
|
667
|
+
/**
|
|
668
|
+
* Tool call requested by the model.
|
|
669
|
+
*
|
|
670
|
+
* Represents a single function call request from the LLM, including
|
|
671
|
+
* the tool name and parsed arguments.
|
|
672
|
+
*
|
|
673
|
+
* @example
|
|
674
|
+
* ```typescript
|
|
675
|
+
* const toolCall: ToolCall = {
|
|
676
|
+
* toolCallId: 'call_abc123',
|
|
677
|
+
* toolName: 'get_weather',
|
|
678
|
+
* arguments: { location: 'San Francisco', units: 'celsius' }
|
|
679
|
+
* };
|
|
680
|
+
* ```
|
|
681
|
+
*/
|
|
682
|
+
interface ToolCall {
|
|
683
|
+
/** Unique identifier for this tool call, used to match results */
|
|
684
|
+
toolCallId: string;
|
|
685
|
+
/** Name of the tool being called */
|
|
686
|
+
toolName: string;
|
|
687
|
+
/** Parsed arguments for the tool call */
|
|
688
|
+
arguments: Record<string, unknown>;
|
|
689
|
+
}
|
|
690
|
+
/**
|
|
691
|
+
* Result of tool execution.
|
|
692
|
+
*
|
|
693
|
+
* Returned after executing a tool, containing the result data
|
|
694
|
+
* and whether an error occurred.
|
|
695
|
+
*
|
|
696
|
+
* @example
|
|
697
|
+
* ```typescript
|
|
698
|
+
* const result: ToolResult = {
|
|
699
|
+
* toolCallId: 'call_abc123',
|
|
700
|
+
* result: { temperature: 72, conditions: 'sunny' }
|
|
701
|
+
* };
|
|
702
|
+
*
|
|
703
|
+
* // Error result
|
|
704
|
+
* const errorResult: ToolResult = {
|
|
705
|
+
* toolCallId: 'call_abc123',
|
|
706
|
+
* result: 'Location not found',
|
|
707
|
+
* isError: true
|
|
708
|
+
* };
|
|
709
|
+
* ```
|
|
710
|
+
*/
|
|
711
|
+
interface ToolResult {
|
|
712
|
+
/** The tool call ID this result corresponds to */
|
|
713
|
+
toolCallId: string;
|
|
714
|
+
/** The result data (can be any serializable value) */
|
|
715
|
+
result: unknown;
|
|
716
|
+
/** Whether the tool execution resulted in an error */
|
|
717
|
+
isError?: boolean;
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Tool definition for LLM function calling.
|
|
721
|
+
*
|
|
722
|
+
* Defines a tool that can be called by the LLM, including its
|
|
723
|
+
* name, description, parameter schema, and execution function.
|
|
724
|
+
*
|
|
725
|
+
* @typeParam TParams - The type of parameters the tool accepts
|
|
726
|
+
* @typeParam TResult - The type of result the tool returns
|
|
727
|
+
*
|
|
728
|
+
* @example
|
|
729
|
+
* ```typescript
|
|
730
|
+
* const weatherTool: Tool<{ location: string }, WeatherData> = {
|
|
731
|
+
* name: 'get_weather',
|
|
732
|
+
* description: 'Get current weather for a location',
|
|
733
|
+
* parameters: {
|
|
734
|
+
* type: 'object',
|
|
735
|
+
* properties: {
|
|
736
|
+
* location: { type: 'string', description: 'City name' }
|
|
737
|
+
* },
|
|
738
|
+
* required: ['location']
|
|
739
|
+
* },
|
|
740
|
+
* run: async (params) => {
|
|
741
|
+
* return fetchWeather(params.location);
|
|
742
|
+
* }
|
|
743
|
+
* };
|
|
744
|
+
* ```
|
|
745
|
+
*/
|
|
746
|
+
interface Tool<TParams = unknown, TResult = unknown> {
|
|
747
|
+
/** Tool name (must be unique within an llm() instance) */
|
|
748
|
+
name: string;
|
|
749
|
+
/** Human-readable description for the model to understand when to use this tool */
|
|
750
|
+
description: string;
|
|
751
|
+
/** JSON Schema defining the tool's parameters */
|
|
752
|
+
parameters: JSONSchema;
|
|
753
|
+
/**
|
|
754
|
+
* Provider-specific metadata, namespaced by provider name.
|
|
755
|
+
*
|
|
756
|
+
* Used for provider-specific features like prompt caching:
|
|
757
|
+
* @example
|
|
758
|
+
* ```typescript
|
|
759
|
+
* const tool: Tool = {
|
|
760
|
+
* name: 'search_docs',
|
|
761
|
+
* description: 'Search documentation',
|
|
762
|
+
* parameters: {...},
|
|
763
|
+
* run: async (params) => {...},
|
|
764
|
+
* metadata: {
|
|
765
|
+
* anthropic: { cache_control: { type: 'ephemeral' } }
|
|
766
|
+
* }
|
|
767
|
+
* };
|
|
768
|
+
* ```
|
|
769
|
+
*/
|
|
770
|
+
metadata?: ToolMetadata;
|
|
771
|
+
/**
|
|
772
|
+
* Executes the tool with the provided parameters.
|
|
773
|
+
*
|
|
774
|
+
* @param params - The parameters passed by the model
|
|
775
|
+
* @returns The tool result, synchronously or as a Promise
|
|
776
|
+
*/
|
|
777
|
+
run(params: TParams): TResult | Promise<TResult>;
|
|
778
|
+
/**
|
|
779
|
+
* Optional approval handler for sensitive operations.
|
|
780
|
+
*
|
|
781
|
+
* If provided, this function is called before the tool executes.
|
|
782
|
+
* Return false to prevent execution.
|
|
783
|
+
*
|
|
784
|
+
* @param params - The parameters the tool would be called with
|
|
785
|
+
* @returns Whether to approve the execution
|
|
786
|
+
*/
|
|
787
|
+
approval?(params: TParams): boolean | Promise<boolean>;
|
|
788
|
+
}
|
|
789
|
+
/**
|
|
790
|
+
* Result from onBeforeCall hook indicating whether to proceed and optionally transformed params.
|
|
791
|
+
*/
|
|
792
|
+
interface BeforeCallResult {
|
|
793
|
+
/** Whether to proceed with tool execution */
|
|
794
|
+
proceed: boolean;
|
|
795
|
+
/** Transformed parameters to use instead of the original (optional) */
|
|
796
|
+
params?: unknown;
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
* Result from onAfterCall hook optionally containing a transformed result.
|
|
800
|
+
*/
|
|
801
|
+
interface AfterCallResult {
|
|
802
|
+
/** Transformed result to use instead of the original */
|
|
803
|
+
result: unknown;
|
|
804
|
+
}
|
|
805
|
+
/**
|
|
806
|
+
* Strategy for controlling tool execution behavior.
|
|
807
|
+
*
|
|
808
|
+
* Provides hooks for monitoring, controlling, and transforming the tool execution
|
|
809
|
+
* loop during LLM inference.
|
|
810
|
+
*
|
|
811
|
+
* @example
|
|
812
|
+
* ```typescript
|
|
813
|
+
* const strategy: ToolUseStrategy = {
|
|
814
|
+
* maxIterations: 5,
|
|
815
|
+
* onToolCall: (tool, params) => {
|
|
816
|
+
* console.log(`Calling ${tool.name} with`, params);
|
|
817
|
+
* },
|
|
818
|
+
* // Transform input parameters
|
|
819
|
+
* onBeforeCall: (tool, params) => {
|
|
820
|
+
* if (tool.name === 'search') {
|
|
821
|
+
* return { proceed: true, params: { ...params, limit: 10 } };
|
|
822
|
+
* }
|
|
823
|
+
* return true;
|
|
824
|
+
* },
|
|
825
|
+
* // Transform output results
|
|
826
|
+
* onAfterCall: (tool, params, result) => {
|
|
827
|
+
* if (tool.name === 'fetch_data') {
|
|
828
|
+
* return { result: sanitize(result) };
|
|
829
|
+
* }
|
|
830
|
+
* },
|
|
831
|
+
* onMaxIterations: (iterations) => {
|
|
832
|
+
* console.warn(`Reached max iterations: ${iterations}`);
|
|
833
|
+
* }
|
|
834
|
+
* };
|
|
835
|
+
* ```
|
|
836
|
+
*/
|
|
837
|
+
interface ToolUseStrategy {
|
|
838
|
+
/** Maximum number of tool execution rounds (default: 10) */
|
|
839
|
+
maxIterations?: number;
|
|
840
|
+
/**
|
|
841
|
+
* Called when the model requests a tool call.
|
|
842
|
+
*
|
|
843
|
+
* @param tool - The tool being called
|
|
844
|
+
* @param params - The parameters for the call
|
|
845
|
+
*/
|
|
846
|
+
onToolCall?(tool: Tool, params: unknown): void | Promise<void>;
|
|
847
|
+
/**
|
|
848
|
+
* Called before tool execution. Can skip execution or transform parameters.
|
|
849
|
+
*
|
|
850
|
+
* @param tool - The tool about to be executed
|
|
851
|
+
* @param params - The parameters for the call
|
|
852
|
+
* @returns One of:
|
|
853
|
+
* - `false` to skip execution
|
|
854
|
+
* - `true` to proceed with original params
|
|
855
|
+
* - `BeforeCallResult` object to control execution and optionally transform params
|
|
856
|
+
*/
|
|
857
|
+
onBeforeCall?(tool: Tool, params: unknown): boolean | BeforeCallResult | Promise<boolean | BeforeCallResult>;
|
|
858
|
+
/**
|
|
859
|
+
* Called after tool execution completes. Can transform the result.
|
|
860
|
+
*
|
|
861
|
+
* @param tool - The tool that was executed
|
|
862
|
+
* @param params - The parameters that were used
|
|
863
|
+
* @param result - The result from the tool
|
|
864
|
+
* @returns Void to use original result, or `AfterCallResult` to transform it
|
|
865
|
+
*/
|
|
866
|
+
onAfterCall?(tool: Tool, params: unknown, result: unknown): void | AfterCallResult | Promise<void | AfterCallResult>;
|
|
867
|
+
/**
|
|
868
|
+
* Called when a tool execution throws an error.
|
|
869
|
+
*
|
|
870
|
+
* @param tool - The tool that failed
|
|
871
|
+
* @param params - The parameters that were used
|
|
872
|
+
* @param error - The error that was thrown
|
|
873
|
+
*/
|
|
874
|
+
onError?(tool: Tool, params: unknown, error: Error): void | Promise<void>;
|
|
875
|
+
/**
|
|
876
|
+
* Called when the maximum iteration limit is reached.
|
|
877
|
+
*
|
|
878
|
+
* @param iterations - The number of iterations that were performed
|
|
879
|
+
*/
|
|
880
|
+
onMaxIterations?(iterations: number): void | Promise<void>;
|
|
881
|
+
}
|
|
882
|
+
/**
|
|
883
|
+
* Record of a completed tool execution.
|
|
884
|
+
*
|
|
885
|
+
* Contains all information about a tool call that was executed,
|
|
886
|
+
* including timing and result data.
|
|
887
|
+
*
|
|
888
|
+
* @example
|
|
889
|
+
* ```typescript
|
|
890
|
+
* const execution: ToolExecution = {
|
|
891
|
+
* toolName: 'get_weather',
|
|
892
|
+
* toolCallId: 'call_abc123',
|
|
893
|
+
* arguments: { location: 'San Francisco' },
|
|
894
|
+
* result: { temperature: 72 },
|
|
895
|
+
* isError: false,
|
|
896
|
+
* duration: 150,
|
|
897
|
+
* approved: true
|
|
898
|
+
* };
|
|
899
|
+
* ```
|
|
900
|
+
*/
|
|
901
|
+
interface ToolExecution {
|
|
902
|
+
/** Name of the tool that was called */
|
|
903
|
+
toolName: string;
|
|
904
|
+
/** Unique identifier for this tool call */
|
|
905
|
+
toolCallId: string;
|
|
906
|
+
/** Arguments that were passed to the tool */
|
|
907
|
+
arguments: Record<string, unknown>;
|
|
908
|
+
/** Result returned by the tool */
|
|
909
|
+
result: unknown;
|
|
910
|
+
/** Whether the tool execution resulted in an error */
|
|
911
|
+
isError: boolean;
|
|
912
|
+
/** Execution duration in milliseconds */
|
|
913
|
+
duration: number;
|
|
914
|
+
/** Whether approval was required and granted (undefined if no approval handler) */
|
|
915
|
+
approved?: boolean;
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
/**
|
|
919
|
+
* @fileoverview Message types for conversation history.
|
|
920
|
+
*
|
|
921
|
+
* Defines the message classes used to represent conversation turns
|
|
922
|
+
* between users and assistants, including support for multimodal
|
|
923
|
+
* content and tool calls.
|
|
924
|
+
*
|
|
925
|
+
* @module types/messages
|
|
926
|
+
*/
|
|
927
|
+
|
|
928
|
+
/**
|
|
929
|
+
* Message serialized to JSON format.
|
|
930
|
+
* Picks common fields from Message, converts timestamp to string.
|
|
931
|
+
*/
|
|
932
|
+
type MessageJSON = Pick<Message, 'id' | 'type' | 'metadata'> & {
|
|
933
|
+
timestamp: string;
|
|
934
|
+
content: ContentBlock[];
|
|
935
|
+
toolCalls?: ToolCall[];
|
|
936
|
+
results?: ToolResult[];
|
|
937
|
+
};
|
|
938
|
+
/**
|
|
939
|
+
* Message type discriminator union.
|
|
940
|
+
*
|
|
941
|
+
* This type is derived from {@link MessageRole} constants. The name `MessageType`
|
|
942
|
+
* is kept for backward compatibility; `MessageRole` works as both the const
|
|
943
|
+
* object and this type.
|
|
944
|
+
*/
|
|
945
|
+
type MessageType = (typeof MessageRole)[keyof typeof MessageRole];
|
|
946
|
+
/**
|
|
947
|
+
* Message role/type constants.
|
|
948
|
+
*
|
|
949
|
+
* Use these constants instead of raw strings for type-safe message handling:
|
|
950
|
+
*
|
|
951
|
+
* @example
|
|
952
|
+
* ```typescript
|
|
953
|
+
* import { MessageRole, isUserMessage } from 'upp';
|
|
954
|
+
*
|
|
955
|
+
* if (message.type === MessageRole.User) {
|
|
956
|
+
* console.log('User said:', message.text);
|
|
957
|
+
* } else if (message.type === MessageRole.Assistant) {
|
|
958
|
+
* console.log('Assistant replied:', message.text);
|
|
959
|
+
* }
|
|
960
|
+
* ```
|
|
961
|
+
*/
|
|
962
|
+
declare const MessageRole: {
|
|
963
|
+
/** User message */
|
|
964
|
+
readonly User: "user";
|
|
965
|
+
/** Assistant/model response */
|
|
966
|
+
readonly Assistant: "assistant";
|
|
967
|
+
/** Tool execution result */
|
|
968
|
+
readonly ToolResult: "tool_result";
|
|
969
|
+
};
|
|
970
|
+
/**
|
|
971
|
+
* Type alias for MessageType, allowing `MessageRole` to work as both const and type.
|
|
972
|
+
*/
|
|
973
|
+
type MessageRole = MessageType;
|
|
974
|
+
/**
|
|
975
|
+
* Provider-namespaced metadata for messages.
|
|
976
|
+
*
|
|
977
|
+
* Each provider can attach its own metadata under its namespace,
|
|
978
|
+
* preventing conflicts between different providers.
|
|
979
|
+
*
|
|
980
|
+
* @example
|
|
981
|
+
* ```typescript
|
|
982
|
+
* const metadata: MessageMetadata = {
|
|
983
|
+
* openai: { model: 'gpt-4', finishReason: 'stop' },
|
|
984
|
+
* anthropic: { model: 'claude-3', stopReason: 'end_turn' }
|
|
985
|
+
* };
|
|
986
|
+
* ```
|
|
987
|
+
*/
|
|
988
|
+
interface MessageMetadata {
|
|
989
|
+
[provider: string]: Record<string, unknown> | undefined;
|
|
990
|
+
}
|
|
991
|
+
/**
|
|
992
|
+
* Options for constructing messages.
|
|
993
|
+
*/
|
|
994
|
+
interface MessageOptions {
|
|
995
|
+
/** Custom message ID (auto-generated if not provided) */
|
|
996
|
+
id?: string;
|
|
997
|
+
/** Provider-specific metadata */
|
|
998
|
+
metadata?: MessageMetadata;
|
|
999
|
+
}
|
|
1000
|
+
/**
|
|
1001
|
+
* Abstract base class for all message types.
|
|
1002
|
+
*
|
|
1003
|
+
* Provides common functionality for user, assistant, and tool result
|
|
1004
|
+
* messages, including content accessors and metadata handling.
|
|
1005
|
+
*
|
|
1006
|
+
* @example
|
|
1007
|
+
* ```typescript
|
|
1008
|
+
* // Access text content from any message
|
|
1009
|
+
* const text = message.text;
|
|
1010
|
+
*
|
|
1011
|
+
* // Access images
|
|
1012
|
+
* const images = message.images;
|
|
1013
|
+
* ```
|
|
1014
|
+
*/
|
|
1015
|
+
declare abstract class Message {
|
|
1016
|
+
/** Unique message identifier */
|
|
1017
|
+
readonly id: string;
|
|
1018
|
+
/** Timestamp when the message was created */
|
|
1019
|
+
readonly timestamp: Date;
|
|
1020
|
+
/** Provider-specific metadata, namespaced by provider name */
|
|
1021
|
+
readonly metadata?: MessageMetadata;
|
|
1022
|
+
/** Message type discriminator (implemented by subclasses) */
|
|
1023
|
+
abstract readonly type: MessageType;
|
|
1024
|
+
/**
|
|
1025
|
+
* Returns the content blocks for this message.
|
|
1026
|
+
* Implemented by subclasses to provide type-specific content.
|
|
1027
|
+
*/
|
|
1028
|
+
protected abstract getContent(): ContentBlock[];
|
|
1029
|
+
/**
|
|
1030
|
+
* Creates a new message instance.
|
|
1031
|
+
*
|
|
1032
|
+
* @param options - Optional message ID and metadata
|
|
1033
|
+
*/
|
|
1034
|
+
constructor(options?: MessageOptions);
|
|
1035
|
+
/**
|
|
1036
|
+
* Concatenated text content from all text blocks.
|
|
1037
|
+
* Blocks are joined with double newlines.
|
|
1038
|
+
*/
|
|
1039
|
+
get text(): string;
|
|
1040
|
+
/**
|
|
1041
|
+
* All image content blocks in this message.
|
|
1042
|
+
*/
|
|
1043
|
+
get images(): ImageBlock[];
|
|
1044
|
+
/**
|
|
1045
|
+
* All document content blocks in this message.
|
|
1046
|
+
*/
|
|
1047
|
+
get documents(): DocumentBlock[];
|
|
1048
|
+
/**
|
|
1049
|
+
* All audio content blocks in this message.
|
|
1050
|
+
*/
|
|
1051
|
+
get audio(): AudioBlock[];
|
|
1052
|
+
/**
|
|
1053
|
+
* All video content blocks in this message.
|
|
1054
|
+
*/
|
|
1055
|
+
get video(): VideoBlock[];
|
|
1056
|
+
/**
|
|
1057
|
+
* All reasoning/thinking content blocks in this message.
|
|
1058
|
+
* Available when using extended thinking models.
|
|
1059
|
+
*/
|
|
1060
|
+
get reasoning(): ReasoningBlock[];
|
|
1061
|
+
}
|
|
1062
|
+
/**
|
|
1063
|
+
* User input message.
|
|
1064
|
+
*
|
|
1065
|
+
* Represents a message from the user, which can contain text and/or
|
|
1066
|
+
* multimodal content like images, audio, or video.
|
|
1067
|
+
*
|
|
1068
|
+
* @example
|
|
1069
|
+
* ```typescript
|
|
1070
|
+
* // Simple text message
|
|
1071
|
+
* const msg = new UserMessage('Hello, world!');
|
|
1072
|
+
*
|
|
1073
|
+
* // Multimodal message
|
|
1074
|
+
* const msg = new UserMessage([
|
|
1075
|
+
* { type: 'text', text: 'What is in this image?' },
|
|
1076
|
+
* { type: 'image', source: { type: 'url', url: '...' }, mimeType: 'image/png' }
|
|
1077
|
+
* ]);
|
|
1078
|
+
* ```
|
|
1079
|
+
*/
|
|
1080
|
+
declare class UserMessage extends Message {
|
|
1081
|
+
/** Message type discriminator */
|
|
1082
|
+
readonly type: "user";
|
|
1083
|
+
/** Content blocks in this message */
|
|
1084
|
+
readonly content: UserContent[];
|
|
1085
|
+
/**
|
|
1086
|
+
* Creates a new user message.
|
|
1087
|
+
*
|
|
1088
|
+
* @param content - String (converted to TextBlock) or array of content blocks
|
|
1089
|
+
* @param options - Optional message ID and metadata
|
|
1090
|
+
*/
|
|
1091
|
+
constructor(content: string | UserContent[], options?: MessageOptions);
|
|
1092
|
+
protected getContent(): ContentBlock[];
|
|
1093
|
+
}
|
|
1094
|
+
/**
|
|
1095
|
+
* Assistant response message.
|
|
1096
|
+
*
|
|
1097
|
+
* Represents a response from the AI assistant, which may contain
|
|
1098
|
+
* text, media content, and/or tool call requests.
|
|
1099
|
+
*
|
|
1100
|
+
* @example
|
|
1101
|
+
* ```typescript
|
|
1102
|
+
* // Simple text response
|
|
1103
|
+
* const msg = new AssistantMessage('Hello! How can I help?');
|
|
1104
|
+
*
|
|
1105
|
+
* // Response with tool calls
|
|
1106
|
+
* const msg = new AssistantMessage(
|
|
1107
|
+
* 'Let me check the weather...',
|
|
1108
|
+
* [{ toolCallId: 'call_1', toolName: 'get_weather', arguments: { location: 'NYC' } }]
|
|
1109
|
+
* );
|
|
1110
|
+
* ```
|
|
1111
|
+
*/
|
|
1112
|
+
declare class AssistantMessage extends Message {
|
|
1113
|
+
/** Message type discriminator */
|
|
1114
|
+
readonly type: "assistant";
|
|
1115
|
+
/** Content blocks in this message */
|
|
1116
|
+
readonly content: AssistantContent[];
|
|
1117
|
+
/** Tool calls requested by the model (if any) */
|
|
1118
|
+
readonly toolCalls?: ToolCall[];
|
|
1119
|
+
/**
|
|
1120
|
+
* Creates a new assistant message.
|
|
1121
|
+
*
|
|
1122
|
+
* @param content - String (converted to TextBlock) or array of content blocks
|
|
1123
|
+
* @param toolCalls - Tool calls requested by the model
|
|
1124
|
+
* @param options - Optional message ID and metadata
|
|
1125
|
+
*/
|
|
1126
|
+
constructor(content: string | AssistantContent[], toolCalls?: ToolCall[], options?: MessageOptions);
|
|
1127
|
+
protected getContent(): ContentBlock[];
|
|
1128
|
+
/**
|
|
1129
|
+
* Whether this message contains tool call requests.
|
|
1130
|
+
*/
|
|
1131
|
+
get hasToolCalls(): boolean;
|
|
1132
|
+
}
|
|
1133
|
+
/**
|
|
1134
|
+
* Tool execution result message.
|
|
1135
|
+
*
|
|
1136
|
+
* Contains the results of executing one or more tool calls,
|
|
1137
|
+
* sent back to the model for further processing.
|
|
1138
|
+
*
|
|
1139
|
+
* @example
|
|
1140
|
+
* ```typescript
|
|
1141
|
+
* const msg = new ToolResultMessage([
|
|
1142
|
+
* { toolCallId: 'call_1', result: { temperature: 72, conditions: 'sunny' } },
|
|
1143
|
+
* { toolCallId: 'call_2', result: 'File not found', isError: true }
|
|
1144
|
+
* ]);
|
|
1145
|
+
* ```
|
|
1146
|
+
*/
|
|
1147
|
+
declare class ToolResultMessage extends Message {
|
|
1148
|
+
/** Message type discriminator */
|
|
1149
|
+
readonly type: "tool_result";
|
|
1150
|
+
/** Results from tool executions */
|
|
1151
|
+
readonly results: ToolResult[];
|
|
1152
|
+
/**
|
|
1153
|
+
* Creates a new tool result message.
|
|
1154
|
+
*
|
|
1155
|
+
* @param results - Array of tool execution results
|
|
1156
|
+
* @param options - Optional message ID and metadata
|
|
1157
|
+
*/
|
|
1158
|
+
constructor(results: ToolResult[], options?: MessageOptions);
|
|
1159
|
+
protected getContent(): ContentBlock[];
|
|
1160
|
+
}
|
|
1161
|
+
/**
|
|
1162
|
+
* Type guard for UserMessage.
|
|
1163
|
+
*
|
|
1164
|
+
* @param msg - The message to check
|
|
1165
|
+
* @returns True if the message is a UserMessage
|
|
1166
|
+
*
|
|
1167
|
+
* @example
|
|
1168
|
+
* ```typescript
|
|
1169
|
+
* if (isUserMessage(msg)) {
|
|
1170
|
+
* console.log('User said:', msg.text);
|
|
1171
|
+
* }
|
|
1172
|
+
* ```
|
|
1173
|
+
*/
|
|
1174
|
+
declare function isUserMessage(msg: Message): msg is UserMessage;
|
|
1175
|
+
/**
|
|
1176
|
+
* Type guard for AssistantMessage.
|
|
1177
|
+
*
|
|
1178
|
+
* @param msg - The message to check
|
|
1179
|
+
* @returns True if the message is an AssistantMessage
|
|
1180
|
+
*
|
|
1181
|
+
* @example
|
|
1182
|
+
* ```typescript
|
|
1183
|
+
* if (isAssistantMessage(msg)) {
|
|
1184
|
+
* console.log('Assistant said:', msg.text);
|
|
1185
|
+
* if (msg.hasToolCalls) {
|
|
1186
|
+
* console.log('Tool calls:', msg.toolCalls);
|
|
1187
|
+
* }
|
|
1188
|
+
* }
|
|
1189
|
+
* ```
|
|
1190
|
+
*/
|
|
1191
|
+
declare function isAssistantMessage(msg: Message): msg is AssistantMessage;
|
|
1192
|
+
/**
|
|
1193
|
+
* Type guard for ToolResultMessage.
|
|
1194
|
+
*
|
|
1195
|
+
* @param msg - The message to check
|
|
1196
|
+
* @returns True if the message is a ToolResultMessage
|
|
1197
|
+
*
|
|
1198
|
+
* @example
|
|
1199
|
+
* ```typescript
|
|
1200
|
+
* if (isToolResultMessage(msg)) {
|
|
1201
|
+
* for (const result of msg.results) {
|
|
1202
|
+
* console.log(`Tool ${result.toolCallId}:`, result.result);
|
|
1203
|
+
* }
|
|
1204
|
+
* }
|
|
1205
|
+
* ```
|
|
1206
|
+
*/
|
|
1207
|
+
declare function isToolResultMessage(msg: Message): msg is ToolResultMessage;
|
|
1208
|
+
|
|
1209
|
+
/**
|
|
1210
|
+
* @fileoverview Turn types for inference results.
|
|
1211
|
+
*
|
|
1212
|
+
* A Turn represents the complete result of one inference call, including
|
|
1213
|
+
* all messages produced during tool execution loops, token usage, and
|
|
1214
|
+
* optional structured output data.
|
|
1215
|
+
*
|
|
1216
|
+
* @module types/turn
|
|
1217
|
+
*/
|
|
1218
|
+
|
|
1219
|
+
/**
|
|
1220
|
+
* Token usage information for an inference request.
|
|
1221
|
+
*
|
|
1222
|
+
* Tracks input and output tokens across all inference cycles,
|
|
1223
|
+
* with optional per-cycle breakdown and cache metrics.
|
|
1224
|
+
*
|
|
1225
|
+
* @example
|
|
1226
|
+
* ```typescript
|
|
1227
|
+
* const usage: TokenUsage = {
|
|
1228
|
+
* inputTokens: 150,
|
|
1229
|
+
* outputTokens: 50,
|
|
1230
|
+
* totalTokens: 200,
|
|
1231
|
+
* cacheReadTokens: 100,
|
|
1232
|
+
* cacheWriteTokens: 50,
|
|
1233
|
+
* cycles: [
|
|
1234
|
+
* { inputTokens: 100, outputTokens: 30, cacheReadTokens: 0, cacheWriteTokens: 50 },
|
|
1235
|
+
* { inputTokens: 50, outputTokens: 20, cacheReadTokens: 100, cacheWriteTokens: 0 }
|
|
1236
|
+
* ]
|
|
1237
|
+
* };
|
|
1238
|
+
* ```
|
|
1239
|
+
*/
|
|
1240
|
+
interface TokenUsage {
|
|
1241
|
+
/** Total input tokens across all cycles */
|
|
1242
|
+
inputTokens: number;
|
|
1243
|
+
/** Total output tokens across all cycles */
|
|
1244
|
+
outputTokens: number;
|
|
1245
|
+
/** Sum of input and output tokens */
|
|
1246
|
+
totalTokens: number;
|
|
1247
|
+
/**
|
|
1248
|
+
* Tokens read from cache (cache hits).
|
|
1249
|
+
* Returns 0 for providers that don't support or report cache metrics.
|
|
1250
|
+
*/
|
|
1251
|
+
cacheReadTokens: number;
|
|
1252
|
+
/**
|
|
1253
|
+
* Tokens written to cache (cache misses that were cached).
|
|
1254
|
+
* Only Anthropic reports this metric; returns 0 for other providers.
|
|
1255
|
+
*/
|
|
1256
|
+
cacheWriteTokens: number;
|
|
1257
|
+
/** Per-cycle token breakdown (if multiple cycles occurred) */
|
|
1258
|
+
cycles?: Array<{
|
|
1259
|
+
inputTokens: number;
|
|
1260
|
+
outputTokens: number;
|
|
1261
|
+
cacheReadTokens: number;
|
|
1262
|
+
cacheWriteTokens: number;
|
|
1263
|
+
}>;
|
|
1264
|
+
}
|
|
1265
|
+
/**
|
|
1266
|
+
* A Turn represents the complete result of one inference call.
|
|
1267
|
+
*
|
|
1268
|
+
* Includes all messages produced during tool execution loops,
|
|
1269
|
+
* the final assistant response, token usage, and optional
|
|
1270
|
+
* structured output data.
|
|
1271
|
+
*
|
|
1272
|
+
* @typeParam TData - Type of the structured output data
|
|
1273
|
+
*
|
|
1274
|
+
* @example
|
|
1275
|
+
* ```typescript
|
|
1276
|
+
* const turn = await instance.generate('Hello');
|
|
1277
|
+
* console.log(turn.response.text);
|
|
1278
|
+
* console.log(`Used ${turn.usage.totalTokens} tokens in ${turn.cycles} cycles`);
|
|
1279
|
+
*
|
|
1280
|
+
* // With structured output
|
|
1281
|
+
* interface WeatherData { temperature: number; conditions: string; }
|
|
1282
|
+
* const turn = await instance.generate<WeatherData>('Get weather');
|
|
1283
|
+
* console.log(turn.data?.temperature);
|
|
1284
|
+
* ```
|
|
1285
|
+
*/
|
|
1286
|
+
interface Turn<TData = unknown> {
|
|
1287
|
+
/**
|
|
1288
|
+
* All messages produced during this inference, in chronological order.
|
|
1289
|
+
* Includes UserMessage, AssistantMessage (may include toolCalls), and ToolResultMessage.
|
|
1290
|
+
*/
|
|
1291
|
+
readonly messages: Message[];
|
|
1292
|
+
/** The final assistant response (last AssistantMessage in the turn) */
|
|
1293
|
+
readonly response: AssistantMessage;
|
|
1294
|
+
/**
|
|
1295
|
+
* Tool executions that occurred during this turn.
|
|
1296
|
+
*
|
|
1297
|
+
* Execution order reflects completion timing, not call order.
|
|
1298
|
+
* Correlate with tool calls using toolCallId.
|
|
1299
|
+
*/
|
|
1300
|
+
readonly toolExecutions: ToolExecution[];
|
|
1301
|
+
/** Aggregate token usage for the entire turn */
|
|
1302
|
+
readonly usage: TokenUsage;
|
|
1303
|
+
/** Total number of inference cycles (1 + number of tool rounds) */
|
|
1304
|
+
readonly cycles: number;
|
|
1305
|
+
/**
|
|
1306
|
+
* Structured output data (if a structure schema was provided).
|
|
1307
|
+
* Type is inferred from the schema when using TypeScript.
|
|
1308
|
+
*/
|
|
1309
|
+
readonly data?: TData;
|
|
1310
|
+
}
|
|
1311
|
+
/**
|
|
1312
|
+
* Turn serialized to JSON format.
|
|
1313
|
+
* Messages are converted to MessageJSON, response is omitted (computed from messages).
|
|
1314
|
+
*
|
|
1315
|
+
* @remarks
|
|
1316
|
+
* This type is derived from {@link Turn} and should stay in sync with it.
|
|
1317
|
+
*/
|
|
1318
|
+
type TurnJSON = Omit<Turn, 'messages' | 'response'> & {
|
|
1319
|
+
messages: MessageJSON[];
|
|
1320
|
+
};
|
|
1321
|
+
/**
|
|
1322
|
+
* Creates a Turn from accumulated inference data.
|
|
1323
|
+
*
|
|
1324
|
+
* @typeParam TData - Type of the structured output data
|
|
1325
|
+
* @param messages - All messages produced during the inference
|
|
1326
|
+
* @param toolExecutions - Record of all tool executions
|
|
1327
|
+
* @param usage - Aggregate token usage
|
|
1328
|
+
* @param cycles - Number of inference cycles
|
|
1329
|
+
* @param data - Optional structured output data
|
|
1330
|
+
* @returns A complete Turn object
|
|
1331
|
+
* @throws Error if no assistant message is found in the messages
|
|
1332
|
+
*
|
|
1333
|
+
* @example
|
|
1334
|
+
* ```typescript
|
|
1335
|
+
* const turn = createTurn(
|
|
1336
|
+
* [userMsg, assistantMsg],
|
|
1337
|
+
* [],
|
|
1338
|
+
* { inputTokens: 100, outputTokens: 50, totalTokens: 150 },
|
|
1339
|
+
* 1
|
|
1340
|
+
* );
|
|
1341
|
+
* ```
|
|
1342
|
+
*/
|
|
1343
|
+
declare function createTurn<TData = unknown>(messages: Message[], toolExecutions: ToolExecution[], usage: TokenUsage, cycles: number, data?: TData): Turn<TData>;
|
|
1344
|
+
/**
|
|
1345
|
+
* Creates an empty TokenUsage object.
|
|
1346
|
+
*
|
|
1347
|
+
* @returns A TokenUsage with all values set to zero
|
|
1348
|
+
*
|
|
1349
|
+
* @example
|
|
1350
|
+
* ```typescript
|
|
1351
|
+
* const usage = emptyUsage();
|
|
1352
|
+
* // { inputTokens: 0, outputTokens: 0, totalTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0, cycles: [] }
|
|
1353
|
+
* ```
|
|
1354
|
+
*/
|
|
1355
|
+
declare function emptyUsage(): TokenUsage;
|
|
1356
|
+
/**
|
|
1357
|
+
* Aggregates token usage from multiple inference cycles.
|
|
1358
|
+
*
|
|
1359
|
+
* @param usages - Array of TokenUsage objects to aggregate
|
|
1360
|
+
* @returns Combined TokenUsage with per-cycle breakdown
|
|
1361
|
+
*
|
|
1362
|
+
* @example
|
|
1363
|
+
* ```typescript
|
|
1364
|
+
* const cycle1 = { inputTokens: 100, outputTokens: 30, totalTokens: 130, cacheReadTokens: 50, cacheWriteTokens: 0 };
|
|
1365
|
+
* const cycle2 = { inputTokens: 150, outputTokens: 40, totalTokens: 190, cacheReadTokens: 100, cacheWriteTokens: 0 };
|
|
1366
|
+
* const total = aggregateUsage([cycle1, cycle2]);
|
|
1367
|
+
* // { inputTokens: 250, outputTokens: 70, totalTokens: 320, cacheReadTokens: 150, cacheWriteTokens: 0, cycles: [...] }
|
|
1368
|
+
* ```
|
|
1369
|
+
*/
|
|
1370
|
+
declare function aggregateUsage(usages: TokenUsage[]): TokenUsage;
|
|
1371
|
+
|
|
1372
|
+
/**
|
|
1373
|
+
* @fileoverview Streaming types for real-time LLM responses.
|
|
1374
|
+
*
|
|
1375
|
+
* Defines the event types and interfaces for streaming LLM inference,
|
|
1376
|
+
* including text deltas, tool call deltas, and control events.
|
|
1377
|
+
*
|
|
1378
|
+
* @module types/stream
|
|
1379
|
+
*/
|
|
1380
|
+
|
|
1381
|
+
/**
|
|
1382
|
+
* Stream event type constants.
|
|
1383
|
+
*
|
|
1384
|
+
* Use these constants instead of raw strings for type-safe event handling:
|
|
1385
|
+
*
|
|
1386
|
+
* @example
|
|
1387
|
+
* ```typescript
|
|
1388
|
+
* import { StreamEventType } from 'upp';
|
|
1389
|
+
*
|
|
1390
|
+
* for await (const event of stream) {
|
|
1391
|
+
* if (event.type === StreamEventType.TextDelta) {
|
|
1392
|
+
* process.stdout.write(event.delta.text ?? '');
|
|
1393
|
+
* }
|
|
1394
|
+
* }
|
|
1395
|
+
* ```
|
|
1396
|
+
*/
|
|
1397
|
+
declare const StreamEventType: {
|
|
1398
|
+
/** Incremental text output */
|
|
1399
|
+
readonly TextDelta: "text_delta";
|
|
1400
|
+
/** Incremental reasoning/thinking output */
|
|
1401
|
+
readonly ReasoningDelta: "reasoning_delta";
|
|
1402
|
+
/** Incremental image data */
|
|
1403
|
+
readonly ImageDelta: "image_delta";
|
|
1404
|
+
/** Incremental audio data */
|
|
1405
|
+
readonly AudioDelta: "audio_delta";
|
|
1406
|
+
/** Incremental video data */
|
|
1407
|
+
readonly VideoDelta: "video_delta";
|
|
1408
|
+
/** Incremental tool call data (arguments being streamed) */
|
|
1409
|
+
readonly ToolCallDelta: "tool_call_delta";
|
|
1410
|
+
/** Tool execution has started (may be emitted after completion in some implementations) */
|
|
1411
|
+
readonly ToolExecutionStart: "tool_execution_start";
|
|
1412
|
+
/** Tool execution has completed */
|
|
1413
|
+
readonly ToolExecutionEnd: "tool_execution_end";
|
|
1414
|
+
/** Beginning of a message */
|
|
1415
|
+
readonly MessageStart: "message_start";
|
|
1416
|
+
/** End of a message */
|
|
1417
|
+
readonly MessageStop: "message_stop";
|
|
1418
|
+
/** Beginning of a content block */
|
|
1419
|
+
readonly ContentBlockStart: "content_block_start";
|
|
1420
|
+
/** End of a content block */
|
|
1421
|
+
readonly ContentBlockStop: "content_block_stop";
|
|
1422
|
+
};
|
|
1423
|
+
/**
|
|
1424
|
+
* Stream event type discriminator union.
|
|
1425
|
+
*
|
|
1426
|
+
* This type is derived from {@link StreamEventType} constants. Use `StreamEventType.TextDelta`
|
|
1427
|
+
* for constants or `type MyType = StreamEventType` for type annotations.
|
|
1428
|
+
*/
|
|
1429
|
+
type StreamEventType = (typeof StreamEventType)[keyof typeof StreamEventType];
|
|
1430
|
+
/**
|
|
1431
|
+
* Event delta data payload.
|
|
1432
|
+
*
|
|
1433
|
+
* Contains the type-specific data for a streaming event.
|
|
1434
|
+
* Different fields are populated depending on the event type.
|
|
1435
|
+
*/
|
|
1436
|
+
interface EventDelta {
|
|
1437
|
+
/** Incremental text content (for text_delta, reasoning_delta) */
|
|
1438
|
+
text?: string;
|
|
1439
|
+
/** Incremental binary data (for image_delta, audio_delta, video_delta) */
|
|
1440
|
+
data?: Uint8Array;
|
|
1441
|
+
/** Tool call identifier (for tool_call_delta, tool_execution_start/end) */
|
|
1442
|
+
toolCallId?: string;
|
|
1443
|
+
/** Tool name (for tool_call_delta, tool_execution_start/end) */
|
|
1444
|
+
toolName?: string;
|
|
1445
|
+
/** Incremental JSON arguments string (for tool_call_delta) */
|
|
1446
|
+
argumentsJson?: string;
|
|
1447
|
+
/** Tool execution result (for tool_execution_end) */
|
|
1448
|
+
result?: unknown;
|
|
1449
|
+
/** Whether tool execution resulted in an error (for tool_execution_end) */
|
|
1450
|
+
isError?: boolean;
|
|
1451
|
+
/** Timestamp in milliseconds (for tool_execution_start/end) */
|
|
1452
|
+
timestamp?: number;
|
|
1453
|
+
}
|
|
1454
|
+
/**
|
|
1455
|
+
* A single streaming event from the LLM.
|
|
1456
|
+
*
|
|
1457
|
+
* Events are emitted in order as the model generates output,
|
|
1458
|
+
* allowing for real-time display of responses.
|
|
1459
|
+
*
|
|
1460
|
+
* @example
|
|
1461
|
+
* ```typescript
|
|
1462
|
+
* import { StreamEventType } from 'upp';
|
|
1463
|
+
*
|
|
1464
|
+
* for await (const event of stream) {
|
|
1465
|
+
* if (event.type === StreamEventType.TextDelta) {
|
|
1466
|
+
* process.stdout.write(event.delta.text ?? '');
|
|
1467
|
+
* } else if (event.type === StreamEventType.ToolCallDelta) {
|
|
1468
|
+
* console.log('Tool:', event.delta.toolName);
|
|
1469
|
+
* }
|
|
1470
|
+
* }
|
|
1471
|
+
* ```
|
|
1472
|
+
*/
|
|
1473
|
+
interface StreamEvent {
|
|
1474
|
+
/** Event type discriminator */
|
|
1475
|
+
type: StreamEventType;
|
|
1476
|
+
/** Index of the content block this event belongs to */
|
|
1477
|
+
index: number;
|
|
1478
|
+
/** Event-specific data payload */
|
|
1479
|
+
delta: EventDelta;
|
|
1480
|
+
}
|
|
1481
|
+
/**
|
|
1482
|
+
* Stream result - an async iterable that also provides the final turn.
|
|
1483
|
+
*
|
|
1484
|
+
* Allows consuming streaming events while also awaiting the complete
|
|
1485
|
+
* Turn result after streaming finishes.
|
|
1486
|
+
*
|
|
1487
|
+
* @typeParam TData - Type of the structured output data
|
|
1488
|
+
*
|
|
1489
|
+
* @example
|
|
1490
|
+
* ```typescript
|
|
1491
|
+
* import { StreamEventType } from 'upp';
|
|
1492
|
+
*
|
|
1493
|
+
* const stream = instance.stream('Tell me a story');
|
|
1494
|
+
*
|
|
1495
|
+
* // Consume streaming events
|
|
1496
|
+
* for await (const event of stream) {
|
|
1497
|
+
* if (event.type === StreamEventType.TextDelta) {
|
|
1498
|
+
* process.stdout.write(event.delta.text ?? '');
|
|
1499
|
+
* }
|
|
1500
|
+
* }
|
|
1501
|
+
*
|
|
1502
|
+
* // Get the complete turn after streaming
|
|
1503
|
+
* const turn = await stream.turn;
|
|
1504
|
+
* console.log('\n\nTokens used:', turn.usage.totalTokens);
|
|
1505
|
+
* ```
|
|
1506
|
+
*/
|
|
1507
|
+
interface StreamResult<TData = unknown> extends AsyncIterable<StreamEvent> {
|
|
1508
|
+
/**
|
|
1509
|
+
* Promise that resolves to the complete Turn after streaming finishes.
|
|
1510
|
+
* Rejects if the stream is aborted or terminated early.
|
|
1511
|
+
*/
|
|
1512
|
+
readonly turn: Promise<Turn<TData>>;
|
|
1513
|
+
/**
|
|
1514
|
+
* Aborts the stream, stopping further events and cancelling the request.
|
|
1515
|
+
* This will cause {@link StreamResult.turn} to reject.
|
|
1516
|
+
*/
|
|
1517
|
+
abort(): void;
|
|
1518
|
+
}
|
|
1519
|
+
/**
|
|
1520
|
+
* Creates a StreamResult from an async generator and completion promise.
|
|
1521
|
+
*
|
|
1522
|
+
* @typeParam TData - Type of the structured output data
|
|
1523
|
+
* @param generator - Async generator that yields stream events
|
|
1524
|
+
* @param turnPromiseOrFactory - Promise or factory that resolves to the complete Turn
|
|
1525
|
+
* @param abortController - Controller for aborting the stream
|
|
1526
|
+
* @returns A StreamResult that can be iterated and awaited
|
|
1527
|
+
*
|
|
1528
|
+
* @example
|
|
1529
|
+
* ```typescript
|
|
1530
|
+
* const abortController = new AbortController();
|
|
1531
|
+
* const stream = createStreamResult(
|
|
1532
|
+
* eventGenerator(),
|
|
1533
|
+
* turnPromise,
|
|
1534
|
+
* abortController
|
|
1535
|
+
* );
|
|
1536
|
+
* ```
|
|
1537
|
+
*/
|
|
1538
|
+
declare function createStreamResult<TData = unknown>(generator: AsyncGenerator<StreamEvent, void, unknown>, turnPromiseOrFactory: Promise<Turn<TData>> | (() => Promise<Turn<TData>>), abortController: AbortController): StreamResult<TData>;
|
|
1539
|
+
/**
|
|
1540
|
+
* Creates a text delta stream event.
|
|
1541
|
+
*
|
|
1542
|
+
* @param text - The incremental text content
|
|
1543
|
+
* @param index - Content block index (default: 0)
|
|
1544
|
+
* @returns A text_delta StreamEvent
|
|
1545
|
+
*/
|
|
1546
|
+
declare function textDelta(text: string, index?: number): StreamEvent;
|
|
1547
|
+
/**
|
|
1548
|
+
* Creates a tool call delta stream event.
|
|
1549
|
+
*
|
|
1550
|
+
* @param toolCallId - Unique identifier for the tool call
|
|
1551
|
+
* @param toolName - Name of the tool being called
|
|
1552
|
+
* @param argumentsJson - Incremental JSON arguments string
|
|
1553
|
+
* @param index - Content block index (default: 0)
|
|
1554
|
+
* @returns A tool_call_delta StreamEvent
|
|
1555
|
+
*/
|
|
1556
|
+
declare function toolCallDelta(toolCallId: string, toolName: string, argumentsJson: string, index?: number): StreamEvent;
|
|
1557
|
+
/**
|
|
1558
|
+
* Creates a message start stream event.
|
|
1559
|
+
*
|
|
1560
|
+
* @returns A message_start StreamEvent
|
|
1561
|
+
*/
|
|
1562
|
+
declare function messageStart(): StreamEvent;
|
|
1563
|
+
/**
|
|
1564
|
+
* Creates a message stop stream event.
|
|
1565
|
+
*
|
|
1566
|
+
* @returns A message_stop StreamEvent
|
|
1567
|
+
*/
|
|
1568
|
+
declare function messageStop(): StreamEvent;
|
|
1569
|
+
/**
|
|
1570
|
+
* Creates a content block start stream event.
|
|
1571
|
+
*
|
|
1572
|
+
* @param index - The content block index starting
|
|
1573
|
+
* @returns A content_block_start StreamEvent
|
|
1574
|
+
*/
|
|
1575
|
+
declare function contentBlockStart(index: number): StreamEvent;
|
|
1576
|
+
/**
|
|
1577
|
+
* Creates a content block stop stream event.
|
|
1578
|
+
*
|
|
1579
|
+
* @param index - The content block index stopping
|
|
1580
|
+
* @returns A content_block_stop StreamEvent
|
|
1581
|
+
*/
|
|
1582
|
+
declare function contentBlockStop(index: number): StreamEvent;
|
|
1583
|
+
|
|
1584
|
+
/**
|
|
1585
|
+
* @fileoverview Error types for the Unified Provider Protocol.
|
|
1586
|
+
*
|
|
1587
|
+
* Provides normalized error codes and a unified error class for handling
|
|
1588
|
+
* errors across different AI providers in a consistent manner.
|
|
1589
|
+
*
|
|
1590
|
+
* @module types/errors
|
|
1591
|
+
*/
|
|
1592
|
+
/**
|
|
1593
|
+
* Error code constants for cross-provider error handling.
|
|
1594
|
+
*
|
|
1595
|
+
* Use these constants instead of raw strings for type-safe error handling:
|
|
1596
|
+
*
|
|
1597
|
+
* @example
|
|
1598
|
+
* ```typescript
|
|
1599
|
+
* import { ErrorCode } from 'upp';
|
|
1600
|
+
*
|
|
1601
|
+
* try {
|
|
1602
|
+
* await llm.generate('Hello');
|
|
1603
|
+
* } catch (error) {
|
|
1604
|
+
* if (error instanceof UPPError) {
|
|
1605
|
+
* switch (error.code) {
|
|
1606
|
+
* case ErrorCode.RateLimited:
|
|
1607
|
+
* await delay(error.retryAfter);
|
|
1608
|
+
* break;
|
|
1609
|
+
* case ErrorCode.AuthenticationFailed:
|
|
1610
|
+
* throw new Error('Invalid API key');
|
|
1611
|
+
* }
|
|
1612
|
+
* }
|
|
1613
|
+
* }
|
|
1614
|
+
* ```
|
|
1615
|
+
*/
|
|
1616
|
+
declare const ErrorCode: {
|
|
1617
|
+
/** API key is invalid or expired */
|
|
1618
|
+
readonly AuthenticationFailed: "AUTHENTICATION_FAILED";
|
|
1619
|
+
/** Rate limit exceeded, retry after delay */
|
|
1620
|
+
readonly RateLimited: "RATE_LIMITED";
|
|
1621
|
+
/** Input exceeds model's context window */
|
|
1622
|
+
readonly ContextLengthExceeded: "CONTEXT_LENGTH_EXCEEDED";
|
|
1623
|
+
/** Requested model does not exist */
|
|
1624
|
+
readonly ModelNotFound: "MODEL_NOT_FOUND";
|
|
1625
|
+
/** Request parameters are malformed */
|
|
1626
|
+
readonly InvalidRequest: "INVALID_REQUEST";
|
|
1627
|
+
/** Provider returned an unexpected response format */
|
|
1628
|
+
readonly InvalidResponse: "INVALID_RESPONSE";
|
|
1629
|
+
/** Content was blocked by safety filters */
|
|
1630
|
+
readonly ContentFiltered: "CONTENT_FILTERED";
|
|
1631
|
+
/** Account quota or credits exhausted */
|
|
1632
|
+
readonly QuotaExceeded: "QUOTA_EXCEEDED";
|
|
1633
|
+
/** Provider-specific error not covered by other codes */
|
|
1634
|
+
readonly ProviderError: "PROVIDER_ERROR";
|
|
1635
|
+
/** Network connectivity issue */
|
|
1636
|
+
readonly NetworkError: "NETWORK_ERROR";
|
|
1637
|
+
/** Request exceeded timeout limit */
|
|
1638
|
+
readonly Timeout: "TIMEOUT";
|
|
1639
|
+
/** Request was cancelled via AbortSignal */
|
|
1640
|
+
readonly Cancelled: "CANCELLED";
|
|
1641
|
+
};
|
|
1642
|
+
/**
|
|
1643
|
+
* Error code discriminator union.
|
|
1644
|
+
*
|
|
1645
|
+
* This type is derived from {@link ErrorCode} constants. Use `ErrorCode.RateLimited`
|
|
1646
|
+
* for constants or `type MyCode = ErrorCode` for type annotations.
|
|
1647
|
+
*/
|
|
1648
|
+
type ErrorCode = (typeof ErrorCode)[keyof typeof ErrorCode];
|
|
1649
|
+
/**
|
|
1650
|
+
* Modality type discriminator union.
|
|
1651
|
+
*
|
|
1652
|
+
* This type is derived from {@link ModalityType} constants. The name `Modality`
|
|
1653
|
+
* is kept for backward compatibility; `ModalityType` works as both the const
|
|
1654
|
+
* object and this type.
|
|
1655
|
+
*/
|
|
1656
|
+
type Modality = (typeof ModalityType)[keyof typeof ModalityType];
|
|
1657
|
+
/**
|
|
1658
|
+
* Modality type constants.
|
|
1659
|
+
*
|
|
1660
|
+
* Use these constants for type-safe modality handling:
|
|
1661
|
+
*
|
|
1662
|
+
* @example
|
|
1663
|
+
* ```typescript
|
|
1664
|
+
* import { ModalityType } from 'upp';
|
|
1665
|
+
*
|
|
1666
|
+
* if (provider.modality === ModalityType.LLM) {
|
|
1667
|
+
* // Handle LLM provider
|
|
1668
|
+
* }
|
|
1669
|
+
* ```
|
|
1670
|
+
*/
|
|
1671
|
+
declare const ModalityType: {
|
|
1672
|
+
/** Large language model for text generation */
|
|
1673
|
+
readonly LLM: "llm";
|
|
1674
|
+
/** Text/image embedding model */
|
|
1675
|
+
readonly Embedding: "embedding";
|
|
1676
|
+
/** Image generation model */
|
|
1677
|
+
readonly Image: "image";
|
|
1678
|
+
/** Audio processing/generation model */
|
|
1679
|
+
readonly Audio: "audio";
|
|
1680
|
+
/** Video processing/generation model */
|
|
1681
|
+
readonly Video: "video";
|
|
1682
|
+
};
|
|
1683
|
+
/**
|
|
1684
|
+
* Type alias for Modality, allowing `ModalityType` to work as both const and type.
|
|
1685
|
+
*/
|
|
1686
|
+
type ModalityType = Modality;
|
|
1687
|
+
/**
|
|
1688
|
+
* Unified Provider Protocol Error.
|
|
1689
|
+
*
|
|
1690
|
+
* All provider-specific errors are normalized to this type, providing
|
|
1691
|
+
* a consistent interface for error handling across different AI providers.
|
|
1692
|
+
*
|
|
1693
|
+
* @example
|
|
1694
|
+
* ```typescript
|
|
1695
|
+
* import { ErrorCode, ModalityType } from 'upp';
|
|
1696
|
+
*
|
|
1697
|
+
* throw new UPPError(
|
|
1698
|
+
* 'API key is invalid',
|
|
1699
|
+
* ErrorCode.AuthenticationFailed,
|
|
1700
|
+
* 'openai',
|
|
1701
|
+
* ModalityType.LLM,
|
|
1702
|
+
* 401
|
|
1703
|
+
* );
|
|
1704
|
+
* ```
|
|
1705
|
+
*
|
|
1706
|
+
* @example
|
|
1707
|
+
* ```typescript
|
|
1708
|
+
* import { ErrorCode, ModalityType } from 'upp';
|
|
1709
|
+
*
|
|
1710
|
+
* // Wrapping a provider error
|
|
1711
|
+
* try {
|
|
1712
|
+
* await openai.chat.completions.create({ ... });
|
|
1713
|
+
* } catch (err) {
|
|
1714
|
+
* throw new UPPError(
|
|
1715
|
+
* 'OpenAI request failed',
|
|
1716
|
+
* ErrorCode.ProviderError,
|
|
1717
|
+
* 'openai',
|
|
1718
|
+
* ModalityType.LLM,
|
|
1719
|
+
* err.status,
|
|
1720
|
+
* err
|
|
1721
|
+
* );
|
|
1722
|
+
* }
|
|
1723
|
+
* ```
|
|
1724
|
+
*/
|
|
1725
|
+
declare class UPPError extends Error {
|
|
1726
|
+
/** Normalized error code for programmatic handling */
|
|
1727
|
+
readonly code: ErrorCode;
|
|
1728
|
+
/** Name of the provider that generated the error */
|
|
1729
|
+
readonly provider: string;
|
|
1730
|
+
/** The modality that was being used when the error occurred */
|
|
1731
|
+
readonly modality: Modality;
|
|
1732
|
+
/** HTTP status code from the provider's response, if available */
|
|
1733
|
+
readonly statusCode?: number;
|
|
1734
|
+
/** The original error that caused this UPPError, if wrapping another error */
|
|
1735
|
+
readonly cause?: Error;
|
|
1736
|
+
/** Error class name, always 'UPPError' */
|
|
1737
|
+
readonly name = "UPPError";
|
|
1738
|
+
/**
|
|
1739
|
+
* Creates a new UPPError instance.
|
|
1740
|
+
*
|
|
1741
|
+
* @param message - Human-readable error description
|
|
1742
|
+
* @param code - Normalized error code for programmatic handling
|
|
1743
|
+
* @param provider - Name of the provider that generated the error
|
|
1744
|
+
* @param modality - The modality that was being used
|
|
1745
|
+
* @param statusCode - HTTP status code from the provider's response
|
|
1746
|
+
* @param cause - The original error being wrapped
|
|
1747
|
+
*/
|
|
1748
|
+
constructor(message: string, code: ErrorCode, provider: string, modality: Modality, statusCode?: number, cause?: Error);
|
|
1749
|
+
/**
|
|
1750
|
+
* Creates a string representation of the error.
|
|
1751
|
+
*
|
|
1752
|
+
* @returns Formatted error string including code, message, provider, and modality
|
|
1753
|
+
*/
|
|
1754
|
+
toString(): string;
|
|
1755
|
+
/**
|
|
1756
|
+
* Converts the error to a JSON-serializable object.
|
|
1757
|
+
*
|
|
1758
|
+
* @returns Plain object representation suitable for logging or transmission
|
|
1759
|
+
*/
|
|
1760
|
+
toJSON(): Record<string, unknown>;
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1763
|
+
/**
|
|
1764
|
+
* @fileoverview Image content handling for the Universal Provider Protocol.
|
|
1765
|
+
*
|
|
1766
|
+
* Provides a unified Image class for working with images across different sources
|
|
1767
|
+
* (file paths, URLs, raw bytes, base64). Supports conversion between formats and
|
|
1768
|
+
* integration with UPP message content blocks.
|
|
1769
|
+
*
|
|
1770
|
+
* @module core/media/Image
|
|
1771
|
+
*/
|
|
1772
|
+
|
|
1773
|
+
/**
|
|
1774
|
+
* Represents an image that can be used in UPP messages.
|
|
1775
|
+
*
|
|
1776
|
+
* Images can be created from various sources (files, URLs, bytes, base64) and
|
|
1777
|
+
* converted to different formats as needed by providers. The class provides
|
|
1778
|
+
* a unified interface regardless of the underlying source type.
|
|
1779
|
+
*
|
|
1780
|
+
* @example
|
|
1781
|
+
* ```typescript
|
|
1782
|
+
* // Load from file
|
|
1783
|
+
* const fileImage = await Image.fromPath('./photo.jpg');
|
|
1784
|
+
*
|
|
1785
|
+
* // Reference by URL
|
|
1786
|
+
* const urlImage = Image.fromUrl('https://example.com/image.png');
|
|
1787
|
+
*
|
|
1788
|
+
* // From raw bytes
|
|
1789
|
+
* const bytesImage = Image.fromBytes(uint8Array, 'image/png');
|
|
1790
|
+
*
|
|
1791
|
+
* // Use in a message
|
|
1792
|
+
* const message = new UserMessage([image.toBlock()]);
|
|
1793
|
+
* ```
|
|
1794
|
+
*/
|
|
1795
|
+
declare class Image {
|
|
1796
|
+
/** The underlying image source (bytes, base64, or URL) */
|
|
1797
|
+
readonly source: ImageSource;
|
|
1798
|
+
/** MIME type of the image (e.g., 'image/jpeg', 'image/png') */
|
|
1799
|
+
readonly mimeType: string;
|
|
1800
|
+
/** Image width in pixels, if known */
|
|
1801
|
+
readonly width?: number;
|
|
1802
|
+
/** Image height in pixels, if known */
|
|
1803
|
+
readonly height?: number;
|
|
1804
|
+
private constructor();
|
|
1805
|
+
/**
|
|
1806
|
+
* Whether this image has data loaded in memory.
|
|
1807
|
+
*
|
|
1808
|
+
* Returns `false` for URL-sourced images that reference external resources.
|
|
1809
|
+
* These must be fetched before their data can be accessed.
|
|
1810
|
+
*/
|
|
1811
|
+
get hasData(): boolean;
|
|
1812
|
+
/**
|
|
1813
|
+
* Converts the image to a base64-encoded string.
|
|
1814
|
+
*
|
|
1815
|
+
* @returns The image data as a base64 string
|
|
1816
|
+
* @throws {Error} When the source is a URL (data must be fetched first)
|
|
1817
|
+
*/
|
|
1818
|
+
toBase64(): string;
|
|
1819
|
+
/**
|
|
1820
|
+
* Converts the image to a data URL suitable for embedding in HTML or CSS.
|
|
1821
|
+
*
|
|
1822
|
+
* @returns A data URL in the format `data:{mimeType};base64,{data}`
|
|
1823
|
+
* @throws {Error} When the source is a URL (data must be fetched first)
|
|
1824
|
+
*/
|
|
1825
|
+
toDataUrl(): string;
|
|
1826
|
+
/**
|
|
1827
|
+
* Gets the image data as raw bytes.
|
|
1828
|
+
*
|
|
1829
|
+
* @returns The image data as a Uint8Array
|
|
1830
|
+
* @throws {Error} When the source is a URL (data must be fetched first)
|
|
1831
|
+
*/
|
|
1832
|
+
toBytes(): Uint8Array;
|
|
1833
|
+
/**
|
|
1834
|
+
* Gets the URL for URL-sourced images.
|
|
1835
|
+
*
|
|
1836
|
+
* @returns The image URL
|
|
1837
|
+
* @throws {Error} When the source is not a URL
|
|
1838
|
+
*/
|
|
1839
|
+
toUrl(): string;
|
|
1840
|
+
/**
|
|
1841
|
+
* Converts this Image to an ImageBlock for use in UPP messages.
|
|
1842
|
+
*
|
|
1843
|
+
* @returns An ImageBlock that can be included in message content arrays
|
|
1844
|
+
*/
|
|
1845
|
+
toBlock(): ImageBlock;
|
|
1846
|
+
/**
|
|
1847
|
+
* Creates an Image by reading a file from disk.
|
|
1848
|
+
*
|
|
1849
|
+
* The file is read into memory as bytes. MIME type is automatically
|
|
1850
|
+
* detected from the file extension.
|
|
1851
|
+
*
|
|
1852
|
+
* @param path - Path to the image file
|
|
1853
|
+
* @returns Promise resolving to an Image with the file contents
|
|
1854
|
+
*
|
|
1855
|
+
* @example
|
|
1856
|
+
* ```typescript
|
|
1857
|
+
* const image = await Image.fromPath('./photos/vacation.jpg');
|
|
1858
|
+
* ```
|
|
1859
|
+
*/
|
|
1860
|
+
static fromPath(path: string): Promise<Image>;
|
|
1861
|
+
/**
|
|
1862
|
+
* Creates an Image from a URL reference.
|
|
1863
|
+
*
|
|
1864
|
+
* The URL is stored as a reference and not fetched. Providers will handle
|
|
1865
|
+
* URL-to-data conversion if needed. MIME type is detected from the URL
|
|
1866
|
+
* path if not provided.
|
|
1867
|
+
*
|
|
1868
|
+
* @param url - URL pointing to the image
|
|
1869
|
+
* @param mimeType - Optional MIME type override
|
|
1870
|
+
* @returns An Image referencing the URL
|
|
1871
|
+
*
|
|
1872
|
+
* @example
|
|
1873
|
+
* ```typescript
|
|
1874
|
+
* const image = Image.fromUrl('https://example.com/logo.png');
|
|
1875
|
+
* ```
|
|
1876
|
+
*/
|
|
1877
|
+
static fromUrl(url: string, mimeType?: string): Image;
|
|
1878
|
+
/**
|
|
1879
|
+
* Creates an Image from raw byte data.
|
|
1880
|
+
*
|
|
1881
|
+
* @param data - The image data as a Uint8Array
|
|
1882
|
+
* @param mimeType - The MIME type of the image
|
|
1883
|
+
* @returns An Image containing the byte data
|
|
1884
|
+
*
|
|
1885
|
+
* @example
|
|
1886
|
+
* ```typescript
|
|
1887
|
+
* const image = Image.fromBytes(pngData, 'image/png');
|
|
1888
|
+
* ```
|
|
1889
|
+
*/
|
|
1890
|
+
static fromBytes(data: Uint8Array, mimeType: string): Image;
|
|
1891
|
+
/**
|
|
1892
|
+
* Creates an Image from a base64-encoded string.
|
|
1893
|
+
*
|
|
1894
|
+
* @param base64 - The base64-encoded image data (without data URL prefix)
|
|
1895
|
+
* @param mimeType - The MIME type of the image
|
|
1896
|
+
* @returns An Image containing the base64 data
|
|
1897
|
+
*
|
|
1898
|
+
* @example
|
|
1899
|
+
* ```typescript
|
|
1900
|
+
* const image = Image.fromBase64(base64String, 'image/jpeg');
|
|
1901
|
+
* ```
|
|
1902
|
+
*/
|
|
1903
|
+
static fromBase64(base64: string, mimeType: string): Image;
|
|
1904
|
+
/**
|
|
1905
|
+
* Creates an Image from an existing ImageBlock.
|
|
1906
|
+
*
|
|
1907
|
+
* Useful for converting content blocks received from providers back
|
|
1908
|
+
* into Image instances for further processing.
|
|
1909
|
+
*
|
|
1910
|
+
* @param block - An ImageBlock from message content
|
|
1911
|
+
* @returns An Image with the block's source and metadata
|
|
1912
|
+
*/
|
|
1913
|
+
static fromBlock(block: ImageBlock): Image;
|
|
1914
|
+
}
|
|
1915
|
+
|
|
1916
|
+
/**
|
|
1917
|
+
* @fileoverview Image generation types for the Universal Provider Protocol.
|
|
1918
|
+
*
|
|
1919
|
+
* Defines the interfaces for configuring and executing image generation operations,
|
|
1920
|
+
* including options, instances, requests, responses, streaming, and capabilities.
|
|
1921
|
+
*
|
|
1922
|
+
* @module types/image
|
|
1923
|
+
*/
|
|
1924
|
+
|
|
1925
|
+
/**
|
|
1926
|
+
* Structural type for image model input.
|
|
1927
|
+
* Uses structural typing to avoid generic variance issues with Provider generics.
|
|
1928
|
+
*
|
|
1929
|
+
* @remarks
|
|
1930
|
+
* This type mirrors {@link ModelReference} while keeping provider options
|
|
1931
|
+
* structurally compatible across providers.
|
|
1932
|
+
*
|
|
1933
|
+
* @see ModelReference
|
|
1934
|
+
*/
|
|
1935
|
+
interface ImageModelInput {
|
|
1936
|
+
readonly modelId: string;
|
|
1937
|
+
readonly provider: ProviderIdentity;
|
|
1938
|
+
/** Optional provider configuration merged into requests */
|
|
1939
|
+
readonly providerConfig?: Partial<ProviderConfig>;
|
|
1940
|
+
}
|
|
1941
|
+
/**
|
|
1942
|
+
* Options for creating an image instance with the image() function.
|
|
1943
|
+
*
|
|
1944
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
1945
|
+
*
|
|
1946
|
+
* @example
|
|
1947
|
+
* ```typescript
|
|
1948
|
+
* const options: ImageOptions<OpenAIImageParams> = {
|
|
1949
|
+
* model: openai('dall-e-3'),
|
|
1950
|
+
* config: { apiKey: process.env.OPENAI_API_KEY },
|
|
1951
|
+
* params: { size: '1024x1024', quality: 'hd' }
|
|
1952
|
+
* };
|
|
1953
|
+
* ```
|
|
1954
|
+
*/
|
|
1955
|
+
interface ImageOptions<TParams = unknown> {
|
|
1956
|
+
/** A model reference from a provider factory */
|
|
1957
|
+
model: ImageModelInput;
|
|
1958
|
+
/** Provider infrastructure configuration */
|
|
1959
|
+
config?: ProviderConfig;
|
|
1960
|
+
/** Provider-specific parameters (passed through unchanged) */
|
|
1961
|
+
params?: TParams;
|
|
1962
|
+
}
|
|
1963
|
+
/**
|
|
1964
|
+
* Options for image generation.
|
|
1965
|
+
*/
|
|
1966
|
+
interface ImageGenerateOptions {
|
|
1967
|
+
/** Abort signal for cancellation */
|
|
1968
|
+
signal?: AbortSignal;
|
|
1969
|
+
}
|
|
1970
|
+
/**
|
|
1971
|
+
* Input type for generate() - either a string prompt or object with prompt.
|
|
1972
|
+
*/
|
|
1973
|
+
type ImageInput = string | {
|
|
1974
|
+
prompt: string;
|
|
1975
|
+
};
|
|
1976
|
+
/**
|
|
1977
|
+
* Input for edit() operations.
|
|
1978
|
+
*/
|
|
1979
|
+
interface ImageEditInput {
|
|
1980
|
+
/** Base image to edit */
|
|
1981
|
+
image: Image;
|
|
1982
|
+
/** Mask indicating edit region (interpretation varies by provider) */
|
|
1983
|
+
mask?: Image;
|
|
1984
|
+
/** Edit instruction prompt */
|
|
1985
|
+
prompt: string;
|
|
1986
|
+
}
|
|
1987
|
+
/**
|
|
1988
|
+
* A single generated image with optional metadata.
|
|
1989
|
+
*/
|
|
1990
|
+
interface GeneratedImage {
|
|
1991
|
+
/** The generated image */
|
|
1992
|
+
image: Image;
|
|
1993
|
+
/** Provider-specific per-image metadata */
|
|
1994
|
+
metadata?: Record<string, unknown>;
|
|
1995
|
+
}
|
|
1996
|
+
/**
|
|
1997
|
+
* Usage statistics for image generation.
|
|
1998
|
+
* Fields are optional because providers report usage differently.
|
|
1999
|
+
*/
|
|
2000
|
+
interface ImageUsage {
|
|
2001
|
+
/** Number of images generated */
|
|
2002
|
+
imagesGenerated?: number;
|
|
2003
|
+
/** Input tokens consumed (token-based pricing) */
|
|
2004
|
+
inputTokens?: number;
|
|
2005
|
+
/** Output tokens consumed (token-based pricing) */
|
|
2006
|
+
outputTokens?: number;
|
|
2007
|
+
/** Provider-reported cost (credits, dollars, etc.) */
|
|
2008
|
+
cost?: number;
|
|
2009
|
+
}
|
|
2010
|
+
/**
|
|
2011
|
+
* Result from generate() or edit() calls.
|
|
2012
|
+
*/
|
|
2013
|
+
interface ImageResult {
|
|
2014
|
+
/** Generated images */
|
|
2015
|
+
images: GeneratedImage[];
|
|
2016
|
+
/** Provider-specific response metadata */
|
|
2017
|
+
metadata?: Record<string, unknown>;
|
|
2018
|
+
/** Usage/billing information */
|
|
2019
|
+
usage?: ImageUsage;
|
|
2020
|
+
}
|
|
2021
|
+
/**
|
|
2022
|
+
* Stream events for image generation.
|
|
2023
|
+
*/
|
|
2024
|
+
type ImageStreamEvent = {
|
|
2025
|
+
type: 'preview';
|
|
2026
|
+
image: Image;
|
|
2027
|
+
index: number;
|
|
2028
|
+
metadata?: Record<string, unknown>;
|
|
2029
|
+
} | {
|
|
2030
|
+
type: 'complete';
|
|
2031
|
+
image: GeneratedImage;
|
|
2032
|
+
index: number;
|
|
2033
|
+
};
|
|
2034
|
+
/**
|
|
2035
|
+
* Async iterable stream with final result accessor.
|
|
2036
|
+
* Returned when stream() is called.
|
|
2037
|
+
*/
|
|
2038
|
+
interface ImageStreamResult extends AsyncIterable<ImageStreamEvent> {
|
|
2039
|
+
/** Promise resolving to complete result after streaming */
|
|
2040
|
+
readonly result: Promise<ImageResult>;
|
|
2041
|
+
/** Abort the generation */
|
|
2042
|
+
abort(): void;
|
|
2043
|
+
}
|
|
2044
|
+
/**
|
|
2045
|
+
* Image generation capabilities.
|
|
2046
|
+
*/
|
|
2047
|
+
interface ImageCapabilities {
|
|
2048
|
+
/** Supports text-to-image generation */
|
|
2049
|
+
generate: boolean;
|
|
2050
|
+
/** Supports streaming with partial previews */
|
|
2051
|
+
streaming: boolean;
|
|
2052
|
+
/** Supports image editing/inpainting */
|
|
2053
|
+
edit: boolean;
|
|
2054
|
+
/** Maximum images per request (if known) */
|
|
2055
|
+
maxImages?: number;
|
|
2056
|
+
}
|
|
2057
|
+
/**
|
|
2058
|
+
* Image instance returned by the image() function.
|
|
2059
|
+
*
|
|
2060
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
2061
|
+
*
|
|
2062
|
+
* @example
|
|
2063
|
+
* ```typescript
|
|
2064
|
+
* const dalle = image({ model: openai('dall-e-3') });
|
|
2065
|
+
*
|
|
2066
|
+
* // Simple generation
|
|
2067
|
+
* const result = await dalle.generate('A sunset over mountains');
|
|
2068
|
+
*
|
|
2069
|
+
* // Streaming (if supported)
|
|
2070
|
+
* if (dalle.capabilities.streaming && dalle.stream) {
|
|
2071
|
+
* const stream = dalle.stream('A cyberpunk cityscape');
|
|
2072
|
+
* for await (const event of stream) {
|
|
2073
|
+
* // Handle preview/complete events
|
|
2074
|
+
* }
|
|
2075
|
+
* }
|
|
2076
|
+
* ```
|
|
2077
|
+
*/
|
|
2078
|
+
interface ImageInstance<TParams = unknown> {
|
|
2079
|
+
/**
|
|
2080
|
+
* Generate images from a text prompt.
|
|
2081
|
+
*
|
|
2082
|
+
* @param input - The prompt string or object with prompt
|
|
2083
|
+
* @param options - Optional generation options
|
|
2084
|
+
* @returns Promise resolving to the generated images
|
|
2085
|
+
*/
|
|
2086
|
+
generate(input: ImageInput, options?: ImageGenerateOptions): Promise<ImageResult>;
|
|
2087
|
+
/**
|
|
2088
|
+
* Generate with streaming progress (if supported).
|
|
2089
|
+
* Only available when capabilities.streaming is true.
|
|
2090
|
+
*
|
|
2091
|
+
* @param input - The prompt string or object with prompt
|
|
2092
|
+
* @returns ImageStreamResult with events and final result
|
|
2093
|
+
*/
|
|
2094
|
+
stream?(input: ImageInput): ImageStreamResult;
|
|
2095
|
+
/**
|
|
2096
|
+
* Edit an existing image (if supported).
|
|
2097
|
+
* Only available when capabilities.edit is true.
|
|
2098
|
+
*
|
|
2099
|
+
* @param input - Edit input with image, optional mask, and prompt
|
|
2100
|
+
* @returns Promise resolving to the edited images
|
|
2101
|
+
*/
|
|
2102
|
+
edit?(input: ImageEditInput): Promise<ImageResult>;
|
|
2103
|
+
/** The bound image model */
|
|
2104
|
+
readonly model: BoundImageModel<TParams>;
|
|
2105
|
+
/** Current parameters */
|
|
2106
|
+
readonly params: TParams | undefined;
|
|
2107
|
+
/** Model capabilities */
|
|
2108
|
+
readonly capabilities: ImageCapabilities;
|
|
2109
|
+
}
|
|
2110
|
+
/**
|
|
2111
|
+
* Request passed from image() core to providers for generation.
|
|
2112
|
+
* @internal
|
|
2113
|
+
*/
|
|
2114
|
+
interface ImageRequest<TParams = unknown> {
|
|
2115
|
+
/** Generation prompt */
|
|
2116
|
+
prompt: string;
|
|
2117
|
+
/** Provider-specific parameters (passed through unchanged) */
|
|
2118
|
+
params?: TParams;
|
|
2119
|
+
/** Provider infrastructure config */
|
|
2120
|
+
config: ProviderConfig;
|
|
2121
|
+
/** Abort signal for cancellation */
|
|
2122
|
+
signal?: AbortSignal;
|
|
2123
|
+
}
|
|
2124
|
+
/**
|
|
2125
|
+
* Request passed to providers for edit operations.
|
|
2126
|
+
* @internal
|
|
2127
|
+
*/
|
|
2128
|
+
interface ImageEditRequest<TParams = unknown> {
|
|
2129
|
+
/** Base image to edit */
|
|
2130
|
+
image: Image;
|
|
2131
|
+
/** Edit mask */
|
|
2132
|
+
mask?: Image;
|
|
2133
|
+
/** Edit instruction prompt */
|
|
2134
|
+
prompt: string;
|
|
2135
|
+
/** Provider-specific parameters */
|
|
2136
|
+
params?: TParams;
|
|
2137
|
+
/** Provider infrastructure config */
|
|
2138
|
+
config: ProviderConfig;
|
|
2139
|
+
/** Abort signal for cancellation */
|
|
2140
|
+
signal?: AbortSignal;
|
|
2141
|
+
}
|
|
2142
|
+
/**
|
|
2143
|
+
* Response from provider's generate or edit method.
|
|
2144
|
+
* @internal
|
|
2145
|
+
*/
|
|
2146
|
+
interface ImageResponse {
|
|
2147
|
+
/** Generated images */
|
|
2148
|
+
images: GeneratedImage[];
|
|
2149
|
+
/** Provider-specific response metadata */
|
|
2150
|
+
metadata?: Record<string, unknown>;
|
|
2151
|
+
/** Usage information */
|
|
2152
|
+
usage?: ImageUsage;
|
|
2153
|
+
}
|
|
2154
|
+
/**
|
|
2155
|
+
* Raw provider stream result.
|
|
2156
|
+
* An async iterable of ImageStreamEvent with a response promise.
|
|
2157
|
+
* @internal
|
|
2158
|
+
*/
|
|
2159
|
+
interface ImageProviderStreamResult extends AsyncIterable<ImageStreamEvent> {
|
|
2160
|
+
/** Promise resolving to the complete response */
|
|
2161
|
+
readonly response: Promise<ImageResponse>;
|
|
2162
|
+
}
|
|
2163
|
+
/**
|
|
2164
|
+
* Bound image model - full definition.
|
|
2165
|
+
*
|
|
2166
|
+
* Represents an image model bound to a specific provider and model ID,
|
|
2167
|
+
* ready to execute generation requests.
|
|
2168
|
+
*
|
|
2169
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
2170
|
+
*/
|
|
2171
|
+
interface BoundImageModel<TParams = unknown> {
|
|
2172
|
+
/** The model identifier */
|
|
2173
|
+
readonly modelId: string;
|
|
2174
|
+
/** Reference to the parent provider */
|
|
2175
|
+
readonly provider: ImageProvider<TParams>;
|
|
2176
|
+
/** Model capabilities */
|
|
2177
|
+
readonly capabilities: ImageCapabilities;
|
|
2178
|
+
/**
|
|
2179
|
+
* Generate images from a prompt.
|
|
2180
|
+
*
|
|
2181
|
+
* @param request - The generation request
|
|
2182
|
+
* @returns Promise resolving to the response
|
|
2183
|
+
*/
|
|
2184
|
+
generate(request: ImageRequest<TParams>): Promise<ImageResponse>;
|
|
2185
|
+
/**
|
|
2186
|
+
* Stream image generation (optional).
|
|
2187
|
+
*
|
|
2188
|
+
* @param request - The generation request
|
|
2189
|
+
* @returns Stream result with events and final response
|
|
2190
|
+
*/
|
|
2191
|
+
stream?(request: ImageRequest<TParams>): ImageProviderStreamResult;
|
|
2192
|
+
/**
|
|
2193
|
+
* Edit an image (optional).
|
|
2194
|
+
*
|
|
2195
|
+
* @param request - The edit request
|
|
2196
|
+
* @returns Promise resolving to the response
|
|
2197
|
+
*/
|
|
2198
|
+
edit?(request: ImageEditRequest<TParams>): Promise<ImageResponse>;
|
|
2199
|
+
}
|
|
2200
|
+
|
|
2201
|
+
/**
|
|
2202
|
+
* @fileoverview Provider types for AI service integrations.
|
|
2203
|
+
*
|
|
2204
|
+
* Defines the interfaces for provider factories, modality handlers,
|
|
2205
|
+
* and configuration options for connecting to various AI providers.
|
|
2206
|
+
*
|
|
2207
|
+
* @module types/provider
|
|
2208
|
+
*/
|
|
2209
|
+
|
|
2210
|
+
/**
|
|
2211
|
+
* API key strategy interface for managing multiple keys.
|
|
2212
|
+
*
|
|
2213
|
+
* Implement this interface to provide custom key rotation or
|
|
2214
|
+
* selection logic when working with multiple API keys.
|
|
2215
|
+
*
|
|
2216
|
+
* @example
|
|
2217
|
+
* ```typescript
|
|
2218
|
+
* class RoundRobinKeys implements KeyStrategy {
|
|
2219
|
+
* private keys: string[];
|
|
2220
|
+
* private index = 0;
|
|
2221
|
+
*
|
|
2222
|
+
* constructor(keys: string[]) {
|
|
2223
|
+
* this.keys = keys;
|
|
2224
|
+
* }
|
|
2225
|
+
*
|
|
2226
|
+
* getKey(): string {
|
|
2227
|
+
* const key = this.keys[this.index];
|
|
2228
|
+
* this.index = (this.index + 1) % this.keys.length;
|
|
2229
|
+
* return key;
|
|
2230
|
+
* }
|
|
2231
|
+
* }
|
|
2232
|
+
* ```
|
|
2233
|
+
*/
|
|
2234
|
+
interface KeyStrategy {
|
|
2235
|
+
/**
|
|
2236
|
+
* Gets the next API key to use for a request.
|
|
2237
|
+
*
|
|
2238
|
+
* @returns The API key string, or a Promise resolving to it
|
|
2239
|
+
*/
|
|
2240
|
+
getKey(): string | Promise<string>;
|
|
2241
|
+
}
|
|
2242
|
+
/**
|
|
2243
|
+
* Retry strategy interface for handling request failures.
|
|
2244
|
+
*
|
|
2245
|
+
* Implement this interface to provide custom retry logic for
|
|
2246
|
+
* handling rate limits, transient errors, and other failures.
|
|
2247
|
+
*
|
|
2248
|
+
* @example
|
|
2249
|
+
* ```typescript
|
|
2250
|
+
* class ExponentialBackoff implements RetryStrategy {
|
|
2251
|
+
* private maxAttempts = 5;
|
|
2252
|
+
* private baseDelay = 1000;
|
|
2253
|
+
*
|
|
2254
|
+
* onRetry(error: UPPError, attempt: number): number | null {
|
|
2255
|
+
* if (attempt > this.maxAttempts) return null;
|
|
2256
|
+
* if (error.code !== 'RATE_LIMITED') return null;
|
|
2257
|
+
* return this.baseDelay * Math.pow(2, attempt - 1);
|
|
2258
|
+
* }
|
|
2259
|
+
* }
|
|
2260
|
+
* ```
|
|
2261
|
+
*/
|
|
2262
|
+
interface RetryStrategy {
|
|
2263
|
+
/**
|
|
2264
|
+
* Called when a request fails with a retryable error.
|
|
2265
|
+
*
|
|
2266
|
+
* @param error - The error that occurred
|
|
2267
|
+
* @param attempt - The attempt number (1 = first retry)
|
|
2268
|
+
* @returns Delay in ms before retrying, or null to stop retrying
|
|
2269
|
+
*/
|
|
2270
|
+
onRetry(error: UPPError, attempt: number): number | null | Promise<number | null>;
|
|
2271
|
+
/**
|
|
2272
|
+
* Called before each request. Can be used to implement pre-emptive rate limiting.
|
|
2273
|
+
*
|
|
2274
|
+
* @returns Delay in ms to wait before making the request, or 0 to proceed immediately
|
|
2275
|
+
*/
|
|
2276
|
+
beforeRequest?(): number | Promise<number>;
|
|
2277
|
+
/**
|
|
2278
|
+
* Reset the strategy state (e.g., after a successful request).
|
|
2279
|
+
*/
|
|
2280
|
+
reset?(): void;
|
|
2281
|
+
}
|
|
2282
|
+
/**
|
|
2283
|
+
* Provider identity shape for structural typing.
|
|
2284
|
+
*
|
|
2285
|
+
* Used in model input types to accept any provider instance
|
|
2286
|
+
* without generic variance constraints.
|
|
2287
|
+
*/
|
|
2288
|
+
interface ProviderIdentity {
|
|
2289
|
+
/** Provider name (e.g., 'openai', 'anthropic') */
|
|
2290
|
+
readonly name: string;
|
|
2291
|
+
/** Provider version string */
|
|
2292
|
+
readonly version: string;
|
|
2293
|
+
}
|
|
2294
|
+
/**
|
|
2295
|
+
* Provider configuration for infrastructure and connection settings.
|
|
2296
|
+
*
|
|
2297
|
+
* These settings control how requests are made to the provider's API,
|
|
2298
|
+
* including authentication, timeouts, and retry behavior.
|
|
2299
|
+
*
|
|
2300
|
+
* @example
|
|
2301
|
+
* ```typescript
|
|
2302
|
+
* const config: ProviderConfig = {
|
|
2303
|
+
* apiKey: process.env.OPENAI_API_KEY,
|
|
2304
|
+
* timeout: 30000,
|
|
2305
|
+
* retryStrategy: new ExponentialBackoff()
|
|
2306
|
+
* };
|
|
2307
|
+
*
|
|
2308
|
+
* // Or with a key strategy for key rotation
|
|
2309
|
+
* const config: ProviderConfig = {
|
|
2310
|
+
* apiKey: new RoundRobinKeys(['sk-1', 'sk-2', 'sk-3']),
|
|
2311
|
+
* baseUrl: 'https://custom-proxy.example.com'
|
|
2312
|
+
* };
|
|
2313
|
+
* ```
|
|
2314
|
+
*/
|
|
2315
|
+
interface ProviderConfig {
|
|
2316
|
+
/**
|
|
2317
|
+
* API key for authentication.
|
|
2318
|
+
* Can be a string, async function, or KeyStrategy for advanced use cases.
|
|
2319
|
+
*/
|
|
2320
|
+
apiKey?: string | (() => string | Promise<string>) | KeyStrategy;
|
|
2321
|
+
/** Override the base API URL (for proxies, local models) */
|
|
2322
|
+
baseUrl?: string;
|
|
2323
|
+
/**
|
|
2324
|
+
* Request timeout in milliseconds.
|
|
2325
|
+
* Applied per attempt; total wall time can exceed this when retries are enabled.
|
|
2326
|
+
*/
|
|
2327
|
+
timeout?: number;
|
|
2328
|
+
/** Custom fetch implementation (for logging, caching, custom TLS) */
|
|
2329
|
+
fetch?: typeof fetch;
|
|
2330
|
+
/** API version override (provider-specific) */
|
|
2331
|
+
apiVersion?: string;
|
|
2332
|
+
/** Retry strategy for handling failures and rate limits */
|
|
2333
|
+
retryStrategy?: RetryStrategy;
|
|
2334
|
+
/**
|
|
2335
|
+
* Custom headers to include in API requests.
|
|
2336
|
+
*
|
|
2337
|
+
* Use this to pass provider-specific headers such as:
|
|
2338
|
+
* - Anthropic: `anthropic-beta` for beta features
|
|
2339
|
+
* - OpenAI: `OpenAI-Organization`, `OpenAI-Project`
|
|
2340
|
+
* - OpenRouter: `HTTP-Referer`, `X-Title` for attribution
|
|
2341
|
+
* - Ollama: Proxy authentication headers
|
|
2342
|
+
*
|
|
2343
|
+
* @example
|
|
2344
|
+
* ```typescript
|
|
2345
|
+
* const config: ProviderConfig = {
|
|
2346
|
+
* headers: { 'anthropic-beta': 'extended-cache-ttl-2025-04-11' }
|
|
2347
|
+
* };
|
|
2348
|
+
* ```
|
|
2349
|
+
*/
|
|
2350
|
+
headers?: Record<string, string | undefined>;
|
|
2351
|
+
/**
|
|
2352
|
+
* Maximum Retry-After delay in seconds when honoring server headers.
|
|
2353
|
+
* Defaults to 3600 seconds (1 hour).
|
|
2354
|
+
*/
|
|
2355
|
+
retryAfterMaxSeconds?: number;
|
|
2356
|
+
}
|
|
2357
|
+
/**
|
|
2358
|
+
* A reference to a model, created by a provider factory.
|
|
2359
|
+
*
|
|
2360
|
+
* Model references are lightweight objects that identify a model
|
|
2361
|
+
* and its provider, used as input to the llm() function.
|
|
2362
|
+
*
|
|
2363
|
+
* @typeParam TOptions - Provider-specific options type
|
|
2364
|
+
*
|
|
2365
|
+
* @example
|
|
2366
|
+
* ```typescript
|
|
2367
|
+
* const model = openai('gpt-4');
|
|
2368
|
+
* console.log(model.modelId); // 'gpt-4'
|
|
2369
|
+
* console.log(model.provider.name); // 'openai'
|
|
2370
|
+
* ```
|
|
2371
|
+
*/
|
|
2372
|
+
interface ModelReference<TOptions = unknown> {
|
|
2373
|
+
/** The model identifier (e.g., 'gpt-4', 'claude-3-opus') */
|
|
2374
|
+
readonly modelId: string;
|
|
2375
|
+
/** The provider that created this reference */
|
|
2376
|
+
readonly provider: Provider<TOptions>;
|
|
2377
|
+
/**
|
|
2378
|
+
* Optional provider-specific configuration that gets merged into request config.
|
|
2379
|
+
*
|
|
2380
|
+
* This allows providers to store options set at model reference creation time
|
|
2381
|
+
* (e.g., `anthropic('model', { betas: [...] })`) that should be applied to all requests.
|
|
2382
|
+
* The `llm()` factory will merge these into the request config, with explicit config
|
|
2383
|
+
* values taking precedence.
|
|
2384
|
+
*/
|
|
2385
|
+
readonly providerConfig?: Partial<ProviderConfig>;
|
|
2386
|
+
/**
|
|
2387
|
+
* The original options passed when creating this model reference.
|
|
2388
|
+
*
|
|
2389
|
+
* Used by providers with multiple LLM handlers (e.g., OpenAI with responses/completions APIs)
|
|
2390
|
+
* to resolve the correct handler at request time, avoiding race conditions from shared state.
|
|
2391
|
+
*/
|
|
2392
|
+
readonly options?: TOptions;
|
|
2393
|
+
}
|
|
2394
|
+
/**
|
|
2395
|
+
* LLM handler interface for providers.
|
|
2396
|
+
*
|
|
2397
|
+
* Implemented by providers to enable language model capabilities.
|
|
2398
|
+
*
|
|
2399
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
2400
|
+
* @internal
|
|
2401
|
+
*/
|
|
2402
|
+
interface LLMHandler<TParams = unknown> {
|
|
2403
|
+
/**
|
|
2404
|
+
* Binds a model ID to create an executable model instance.
|
|
2405
|
+
*
|
|
2406
|
+
* @param modelId - The model identifier to bind
|
|
2407
|
+
* @returns A bound LLM model ready for inference
|
|
2408
|
+
*/
|
|
2409
|
+
bind(modelId: string): BoundLLMModel<TParams>;
|
|
2410
|
+
/**
|
|
2411
|
+
* Sets the parent provider reference.
|
|
2412
|
+
* Called by createProvider() after the provider is constructed.
|
|
2413
|
+
*
|
|
2414
|
+
* @param provider - The parent provider
|
|
2415
|
+
* @internal
|
|
2416
|
+
*/
|
|
2417
|
+
_setProvider?(provider: LLMProvider<TParams>): void;
|
|
2418
|
+
}
|
|
2419
|
+
/**
|
|
2420
|
+
* Embedding handler interface for providers.
|
|
2421
|
+
*
|
|
2422
|
+
* Implemented by providers to enable embedding capabilities.
|
|
2423
|
+
*
|
|
2424
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
2425
|
+
* @internal
|
|
2426
|
+
*/
|
|
2427
|
+
interface EmbeddingHandler<TParams = unknown> {
|
|
2428
|
+
/** Supported input types for embeddings */
|
|
2429
|
+
readonly supportedInputs: ('text' | 'image')[];
|
|
2430
|
+
/**
|
|
2431
|
+
* Binds a model ID to create an executable embedding model.
|
|
2432
|
+
*
|
|
2433
|
+
* @param modelId - The model identifier to bind
|
|
2434
|
+
* @returns A bound embedding model ready for use
|
|
2435
|
+
*/
|
|
2436
|
+
bind(modelId: string): BoundEmbeddingModel<TParams>;
|
|
2437
|
+
/**
|
|
2438
|
+
* Sets the parent provider reference.
|
|
2439
|
+
*
|
|
2440
|
+
* @param provider - The parent provider
|
|
2441
|
+
* @internal
|
|
2442
|
+
*/
|
|
2443
|
+
_setProvider?(provider: EmbeddingProvider<TParams>): void;
|
|
2444
|
+
}
|
|
2445
|
+
/**
|
|
2446
|
+
* Image handler interface for providers.
|
|
2447
|
+
*
|
|
2448
|
+
* Implemented by providers to enable image generation capabilities.
|
|
2449
|
+
*
|
|
2450
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
2451
|
+
* @internal
|
|
2452
|
+
*/
|
|
2453
|
+
interface ImageHandler<TParams = unknown> {
|
|
2454
|
+
/**
|
|
2455
|
+
* Binds a model ID to create an executable image model.
|
|
2456
|
+
*
|
|
2457
|
+
* @param modelId - The model identifier to bind
|
|
2458
|
+
* @returns A bound image model ready for generation
|
|
2459
|
+
*/
|
|
2460
|
+
bind(modelId: string): BoundImageModel<TParams>;
|
|
2461
|
+
/**
|
|
2462
|
+
* Sets the parent provider reference.
|
|
2463
|
+
*
|
|
2464
|
+
* @param provider - The parent provider
|
|
2465
|
+
* @internal
|
|
2466
|
+
*/
|
|
2467
|
+
_setProvider?(provider: ImageProvider<TParams>): void;
|
|
2468
|
+
}
|
|
2469
|
+
/**
|
|
2470
|
+
* Bound embedding model interface.
|
|
2471
|
+
*
|
|
2472
|
+
* Represents an embedding model bound to a specific model ID,
|
|
2473
|
+
* ready to generate embeddings.
|
|
2474
|
+
*
|
|
2475
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
2476
|
+
*/
|
|
2477
|
+
interface BoundEmbeddingModel<TParams = unknown> {
|
|
2478
|
+
/** The model identifier */
|
|
2479
|
+
readonly modelId: string;
|
|
2480
|
+
/** Reference to the parent provider */
|
|
2481
|
+
readonly provider: EmbeddingProvider<TParams>;
|
|
2482
|
+
/** Maximum number of inputs per batch request */
|
|
2483
|
+
readonly maxBatchSize: number;
|
|
2484
|
+
/** Maximum length of input text in tokens */
|
|
2485
|
+
readonly maxInputLength: number;
|
|
2486
|
+
/** Output embedding dimensions */
|
|
2487
|
+
readonly dimensions: number;
|
|
2488
|
+
/**
|
|
2489
|
+
* Execute embedding request.
|
|
2490
|
+
*
|
|
2491
|
+
* @param request - The embedding request
|
|
2492
|
+
* @returns Promise resolving to embedding response
|
|
2493
|
+
*/
|
|
2494
|
+
embed(request: EmbeddingRequest<TParams>): Promise<EmbeddingResponse>;
|
|
2495
|
+
}
|
|
2496
|
+
/**
|
|
2497
|
+
* Request passed to provider's embed method.
|
|
2498
|
+
* @internal
|
|
2499
|
+
*/
|
|
2500
|
+
interface EmbeddingRequest<TParams = unknown> {
|
|
2501
|
+
/** Inputs to embed */
|
|
2502
|
+
inputs: EmbeddingInput[];
|
|
2503
|
+
/** Provider-specific parameters (passed through unchanged) */
|
|
2504
|
+
params?: TParams;
|
|
2505
|
+
/** Provider infrastructure config */
|
|
2506
|
+
config: ProviderConfig;
|
|
2507
|
+
/** Abort signal for cancellation */
|
|
2508
|
+
signal?: AbortSignal;
|
|
2509
|
+
}
|
|
2510
|
+
/**
|
|
2511
|
+
* Response from provider's embed method.
|
|
2512
|
+
* @internal
|
|
2513
|
+
*/
|
|
2514
|
+
interface EmbeddingResponse {
|
|
2515
|
+
/** Embedding vectors */
|
|
2516
|
+
embeddings: EmbeddingVector[];
|
|
2517
|
+
/** Aggregate usage */
|
|
2518
|
+
usage: EmbeddingUsage;
|
|
2519
|
+
/** Provider-specific response metadata */
|
|
2520
|
+
metadata?: Record<string, unknown>;
|
|
2521
|
+
}
|
|
2522
|
+
/**
|
|
2523
|
+
* Single vector from provider response.
|
|
2524
|
+
* @internal
|
|
2525
|
+
*/
|
|
2526
|
+
interface EmbeddingVector {
|
|
2527
|
+
/** The embedding vector (floats or base64 string) */
|
|
2528
|
+
vector: number[] | string;
|
|
2529
|
+
/** Index in input array */
|
|
2530
|
+
index: number;
|
|
2531
|
+
/** Token count for this input */
|
|
2532
|
+
tokens?: number;
|
|
2533
|
+
/** Provider-specific per-embedding metadata */
|
|
2534
|
+
metadata?: Record<string, unknown>;
|
|
2535
|
+
}
|
|
2536
|
+
/**
|
|
2537
|
+
* Usage statistics for embedding operations.
|
|
2538
|
+
*/
|
|
2539
|
+
interface EmbeddingUsage {
|
|
2540
|
+
/** Total tokens processed */
|
|
2541
|
+
totalTokens: number;
|
|
2542
|
+
}
|
|
2543
|
+
/**
|
|
2544
|
+
* Valid input types for embedding.
|
|
2545
|
+
*/
|
|
2546
|
+
type EmbeddingInput = string | {
|
|
2547
|
+
type: 'text';
|
|
2548
|
+
text: string;
|
|
2549
|
+
} | {
|
|
2550
|
+
type: 'image';
|
|
2551
|
+
source: unknown;
|
|
2552
|
+
mimeType: string;
|
|
2553
|
+
};
|
|
2554
|
+
/**
|
|
2555
|
+
* Bound image model interface.
|
|
2556
|
+
*
|
|
2557
|
+
* Represents an image generation model bound to a specific model ID.
|
|
2558
|
+
*
|
|
2559
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
2560
|
+
*/
|
|
2561
|
+
/**
|
|
2562
|
+
* Provider factory function with metadata and modality handlers.
|
|
2563
|
+
*
|
|
2564
|
+
* The Provider interface represents a callable function that creates
|
|
2565
|
+
* model references, along with metadata and modality-specific handlers.
|
|
2566
|
+
*
|
|
2567
|
+
* @typeParam TOptions - Provider-specific options passed to the factory
|
|
2568
|
+
*
|
|
2569
|
+
* @example
|
|
2570
|
+
* ```typescript
|
|
2571
|
+
* // Using a provider
|
|
2572
|
+
* const model = openai('gpt-4', { temperature: 0.7 });
|
|
2573
|
+
*
|
|
2574
|
+
* // Accessing provider metadata
|
|
2575
|
+
* console.log(openai.name); // 'openai'
|
|
2576
|
+
* console.log(openai.version); // '1.0.0'
|
|
2577
|
+
* ```
|
|
2578
|
+
*
|
|
2579
|
+
* @remarks
|
|
2580
|
+
* Providers are intended to be used with `llm()`, `embedding()`, or `image()`.
|
|
2581
|
+
* Direct handler access is not part of the public API.
|
|
2582
|
+
*/
|
|
2583
|
+
interface Provider<TOptions = unknown> extends ProviderIdentity {
|
|
2584
|
+
/**
|
|
2585
|
+
* Creates a model reference with optional provider-specific options.
|
|
2586
|
+
*
|
|
2587
|
+
* @param modelId - The model identifier
|
|
2588
|
+
* @param options - Provider-specific options
|
|
2589
|
+
* @returns A model reference for use with llm() or other functions
|
|
2590
|
+
*/
|
|
2591
|
+
(modelId: string, options?: TOptions): ModelReference<TOptions>;
|
|
2592
|
+
/** Provider name (e.g., 'openai', 'anthropic') */
|
|
2593
|
+
readonly name: string;
|
|
2594
|
+
/** Provider version string */
|
|
2595
|
+
readonly version: string;
|
|
2596
|
+
}
|
|
2597
|
+
/**
|
|
2598
|
+
* Provider with LLM modality support.
|
|
2599
|
+
*
|
|
2600
|
+
* Type alias for providers that support language model inference.
|
|
2601
|
+
*
|
|
2602
|
+
* @typeParam TParams - Model-specific parameters type
|
|
2603
|
+
* @typeParam TOptions - Provider-specific options type
|
|
2604
|
+
*/
|
|
2605
|
+
type LLMProvider<TParams = unknown, TOptions = unknown> = Provider<TOptions> & {
|
|
2606
|
+
/** @internal */
|
|
2607
|
+
readonly __params?: TParams;
|
|
2608
|
+
};
|
|
2609
|
+
/**
|
|
2610
|
+
* Provider with Embedding modality support.
|
|
2611
|
+
*
|
|
2612
|
+
* Type alias for providers that support embedding generation.
|
|
2613
|
+
*
|
|
2614
|
+
* @typeParam TParams - Model-specific parameters type
|
|
2615
|
+
* @typeParam TOptions - Provider-specific options type
|
|
2616
|
+
*/
|
|
2617
|
+
type EmbeddingProvider<TParams = unknown, TOptions = unknown> = Provider<TOptions> & {
|
|
2618
|
+
/** @internal */
|
|
2619
|
+
readonly __params?: TParams;
|
|
2620
|
+
};
|
|
2621
|
+
/**
|
|
2622
|
+
* Provider with Image modality support.
|
|
2623
|
+
*
|
|
2624
|
+
* Type alias for providers that support image generation.
|
|
2625
|
+
*
|
|
2626
|
+
* @typeParam TParams - Model-specific parameters type
|
|
2627
|
+
* @typeParam TOptions - Provider-specific options type
|
|
2628
|
+
*/
|
|
2629
|
+
type ImageProvider<TParams = unknown, TOptions = unknown> = Provider<TOptions> & {
|
|
2630
|
+
/** @internal */
|
|
2631
|
+
readonly __params?: TParams;
|
|
2632
|
+
};
|
|
2633
|
+
|
|
2634
|
+
/**
|
|
2635
|
+
* @fileoverview Thread class for managing conversation history.
|
|
2636
|
+
*
|
|
2637
|
+
* Provides a utility class for building and manipulating conversation
|
|
2638
|
+
* message sequences, with support for serialization and deserialization.
|
|
2639
|
+
*
|
|
2640
|
+
* @module types/thread
|
|
2641
|
+
*/
|
|
2642
|
+
|
|
2643
|
+
/**
|
|
2644
|
+
* Thread serialized to JSON format.
|
|
2645
|
+
* Picks id from Thread, converts dates to strings.
|
|
2646
|
+
*/
|
|
2647
|
+
type ThreadJSON = Pick<Thread, 'id'> & {
|
|
2648
|
+
messages: MessageJSON[];
|
|
2649
|
+
createdAt: string;
|
|
2650
|
+
updatedAt: string;
|
|
2651
|
+
};
|
|
2652
|
+
/**
|
|
2653
|
+
* Thread - A utility class for managing conversation history.
|
|
2654
|
+
*
|
|
2655
|
+
* Provides methods for building, manipulating, and persisting
|
|
2656
|
+
* conversation message sequences. This class is optional; users
|
|
2657
|
+
* can also manage their own `Message[]` arrays directly.
|
|
2658
|
+
*
|
|
2659
|
+
* @example
|
|
2660
|
+
* ```typescript
|
|
2661
|
+
* // Create a new thread and add messages
|
|
2662
|
+
* const thread = new Thread();
|
|
2663
|
+
* thread.user('Hello!');
|
|
2664
|
+
* thread.assistant('Hi there! How can I help?');
|
|
2665
|
+
*
|
|
2666
|
+
* // Use with LLM inference
|
|
2667
|
+
* const turn = await instance.generate(thread, 'What is 2+2?');
|
|
2668
|
+
* thread.append(turn);
|
|
2669
|
+
*
|
|
2670
|
+
* // Serialize for storage
|
|
2671
|
+
* const json = thread.toJSON();
|
|
2672
|
+
* localStorage.setItem('chat', JSON.stringify(json));
|
|
2673
|
+
*
|
|
2674
|
+
* // Restore from storage
|
|
2675
|
+
* const restored = Thread.fromJSON(JSON.parse(localStorage.getItem('chat')));
|
|
2676
|
+
* ```
|
|
2677
|
+
*/
|
|
2678
|
+
declare class Thread {
|
|
2679
|
+
/** Unique thread identifier */
|
|
2680
|
+
readonly id: string;
|
|
2681
|
+
/** Internal message storage */
|
|
2682
|
+
private _messages;
|
|
2683
|
+
/** Creation timestamp */
|
|
2684
|
+
private _createdAt;
|
|
2685
|
+
/** Last update timestamp */
|
|
2686
|
+
private _updatedAt;
|
|
2687
|
+
/**
|
|
2688
|
+
* Creates a new thread instance.
|
|
2689
|
+
*
|
|
2690
|
+
* @param messages - Optional initial messages to populate the thread
|
|
2691
|
+
*/
|
|
2692
|
+
constructor(messages?: Message[]);
|
|
2693
|
+
/**
|
|
2694
|
+
* All messages in the thread (readonly).
|
|
2695
|
+
*/
|
|
2696
|
+
get messages(): readonly Message[];
|
|
2697
|
+
/**
|
|
2698
|
+
* Number of messages in the thread.
|
|
2699
|
+
*/
|
|
2700
|
+
get length(): number;
|
|
2701
|
+
/**
|
|
2702
|
+
* Appends all messages from a Turn to the thread.
|
|
2703
|
+
*
|
|
2704
|
+
* @param turn - The Turn containing messages to append
|
|
2705
|
+
* @returns This thread instance for chaining
|
|
2706
|
+
*/
|
|
2707
|
+
append(turn: Turn): this;
|
|
2708
|
+
/**
|
|
2709
|
+
* Adds raw messages to the thread.
|
|
2710
|
+
*
|
|
2711
|
+
* @param messages - Messages to add
|
|
2712
|
+
* @returns This thread instance for chaining
|
|
2713
|
+
*/
|
|
2714
|
+
push(...messages: Message[]): this;
|
|
2715
|
+
/**
|
|
2716
|
+
* Adds a user message to the thread.
|
|
2717
|
+
*
|
|
2718
|
+
* @param content - String or array of content blocks
|
|
2719
|
+
* @returns This thread instance for chaining
|
|
2720
|
+
*
|
|
2721
|
+
* @example
|
|
2722
|
+
* ```typescript
|
|
2723
|
+
* thread.user('Hello, world!');
|
|
2724
|
+
* thread.user([
|
|
2725
|
+
* { type: 'text', text: 'Describe this image:' },
|
|
2726
|
+
* { type: 'image', source: { type: 'url', url: '...' }, mimeType: 'image/png' }
|
|
2727
|
+
* ]);
|
|
2728
|
+
* ```
|
|
2729
|
+
*/
|
|
2730
|
+
user(content: string | UserContent[]): this;
|
|
2731
|
+
/**
|
|
2732
|
+
* Adds an assistant message to the thread.
|
|
2733
|
+
*
|
|
2734
|
+
* @param content - String or array of content blocks
|
|
2735
|
+
* @returns This thread instance for chaining
|
|
2736
|
+
*
|
|
2737
|
+
* @example
|
|
2738
|
+
* ```typescript
|
|
2739
|
+
* thread.assistant('I can help with that!');
|
|
2740
|
+
* ```
|
|
2741
|
+
*/
|
|
2742
|
+
assistant(content: string | AssistantContent[]): this;
|
|
2743
|
+
/**
|
|
2744
|
+
* Filters messages by type.
|
|
2745
|
+
*
|
|
2746
|
+
* @param type - The message type to filter by
|
|
2747
|
+
* @returns Array of messages matching the type
|
|
2748
|
+
*
|
|
2749
|
+
* @example
|
|
2750
|
+
* ```typescript
|
|
2751
|
+
* const userMessages = thread.filter('user');
|
|
2752
|
+
* const assistantMessages = thread.filter('assistant');
|
|
2753
|
+
* ```
|
|
2754
|
+
*/
|
|
2755
|
+
filter(type: MessageType): Message[];
|
|
2756
|
+
/**
|
|
2757
|
+
* Returns the last N messages from the thread.
|
|
2758
|
+
*
|
|
2759
|
+
* @param count - Number of messages to return
|
|
2760
|
+
* @returns Array of the last N messages
|
|
2761
|
+
*
|
|
2762
|
+
* @example
|
|
2763
|
+
* ```typescript
|
|
2764
|
+
* const recent = thread.tail(5);
|
|
2765
|
+
* ```
|
|
2766
|
+
*/
|
|
2767
|
+
tail(count: number): Message[];
|
|
2768
|
+
/**
|
|
2769
|
+
* Creates a new thread with a subset of messages.
|
|
2770
|
+
*
|
|
2771
|
+
* @param start - Start index (inclusive)
|
|
2772
|
+
* @param end - End index (exclusive)
|
|
2773
|
+
* @returns New Thread containing the sliced messages
|
|
2774
|
+
*
|
|
2775
|
+
* @example
|
|
2776
|
+
* ```typescript
|
|
2777
|
+
* const subset = thread.slice(0, 10);
|
|
2778
|
+
* ```
|
|
2779
|
+
*/
|
|
2780
|
+
slice(start?: number, end?: number): Thread;
|
|
2781
|
+
/**
|
|
2782
|
+
* Removes all messages from the thread.
|
|
2783
|
+
*
|
|
2784
|
+
* @returns This thread instance for chaining
|
|
2785
|
+
*/
|
|
2786
|
+
clear(): this;
|
|
2787
|
+
/**
|
|
2788
|
+
* Converts the thread to a plain message array.
|
|
2789
|
+
*
|
|
2790
|
+
* @returns Copy of the internal message array
|
|
2791
|
+
*/
|
|
2792
|
+
toMessages(): Message[];
|
|
2793
|
+
/**
|
|
2794
|
+
* Serializes the thread to JSON format.
|
|
2795
|
+
*
|
|
2796
|
+
* @returns JSON-serializable representation of the thread
|
|
2797
|
+
*
|
|
2798
|
+
* @example
|
|
2799
|
+
* ```typescript
|
|
2800
|
+
* const json = thread.toJSON();
|
|
2801
|
+
* localStorage.setItem('thread', JSON.stringify(json));
|
|
2802
|
+
* ```
|
|
2803
|
+
*/
|
|
2804
|
+
toJSON(): ThreadJSON;
|
|
2805
|
+
/**
|
|
2806
|
+
* Deserializes a thread from JSON format.
|
|
2807
|
+
*
|
|
2808
|
+
* @param json - The JSON representation to deserialize
|
|
2809
|
+
* @returns Reconstructed Thread instance
|
|
2810
|
+
*
|
|
2811
|
+
* @example
|
|
2812
|
+
* ```typescript
|
|
2813
|
+
* const json = JSON.parse(localStorage.getItem('thread'));
|
|
2814
|
+
* const thread = Thread.fromJSON(json);
|
|
2815
|
+
* ```
|
|
2816
|
+
*/
|
|
2817
|
+
static fromJSON(json: ThreadJSON): Thread;
|
|
2818
|
+
/**
|
|
2819
|
+
* Enables iteration over messages with for...of loops.
|
|
2820
|
+
*
|
|
2821
|
+
* @returns Iterator over the thread's messages
|
|
2822
|
+
*
|
|
2823
|
+
* @example
|
|
2824
|
+
* ```typescript
|
|
2825
|
+
* for (const message of thread) {
|
|
2826
|
+
* console.log(message.text);
|
|
2827
|
+
* }
|
|
2828
|
+
* ```
|
|
2829
|
+
*/
|
|
2830
|
+
[Symbol.iterator](): Iterator<Message>;
|
|
2831
|
+
/**
|
|
2832
|
+
* Converts a message to JSON format.
|
|
2833
|
+
*/
|
|
2834
|
+
private messageToJSON;
|
|
2835
|
+
/**
|
|
2836
|
+
* Reconstructs a message from JSON format.
|
|
2837
|
+
*/
|
|
2838
|
+
private static messageFromJSON;
|
|
2839
|
+
}
|
|
2840
|
+
|
|
2841
|
+
/**
|
|
2842
|
+
* @fileoverview LLM types for language model inference.
|
|
2843
|
+
*
|
|
2844
|
+
* Defines the interfaces for configuring and executing LLM inference,
|
|
2845
|
+
* including options, instances, requests, responses, and capabilities.
|
|
2846
|
+
*
|
|
2847
|
+
* @module types/llm
|
|
2848
|
+
*/
|
|
2849
|
+
|
|
2850
|
+
/**
|
|
2851
|
+
* Structural type for model input that accepts any ModelReference.
|
|
2852
|
+
* Uses structural typing to avoid generic variance issues with Provider generics.
|
|
2853
|
+
* The nested types use `unknown` to accept any provider parameter types.
|
|
2854
|
+
*
|
|
2855
|
+
* @remarks
|
|
2856
|
+
* This type mirrors {@link ModelReference} while keeping provider options
|
|
2857
|
+
* structurally compatible across providers.
|
|
2858
|
+
*
|
|
2859
|
+
* @see ModelReference
|
|
2860
|
+
*/
|
|
2861
|
+
type ModelInput = {
|
|
2862
|
+
readonly modelId: string;
|
|
2863
|
+
readonly provider: ProviderIdentity;
|
|
2864
|
+
/**
|
|
2865
|
+
* Optional provider-specific configuration that gets merged into request config.
|
|
2866
|
+
* Set when creating a model reference with provider-specific options.
|
|
2867
|
+
*/
|
|
2868
|
+
readonly providerConfig?: Partial<ProviderConfig>;
|
|
2869
|
+
/**
|
|
2870
|
+
* The original options passed when creating this model reference.
|
|
2871
|
+
* Used by providers with multiple LLM handlers to resolve the correct handler.
|
|
2872
|
+
*/
|
|
2873
|
+
readonly options?: unknown;
|
|
2874
|
+
};
|
|
2875
|
+
/**
|
|
2876
|
+
* LLM capabilities declare what a provider's API supports.
|
|
2877
|
+
*
|
|
2878
|
+
* These are API-level capabilities, not individual model capabilities.
|
|
2879
|
+
* If a user attempts to use a feature with a model that doesn't support it,
|
|
2880
|
+
* the provider's API will return an error.
|
|
2881
|
+
*
|
|
2882
|
+
* Capabilities are static and do not vary per-request or per-model.
|
|
2883
|
+
*
|
|
2884
|
+
* @example
|
|
2885
|
+
* ```typescript
|
|
2886
|
+
* const capabilities: LLMCapabilities = {
|
|
2887
|
+
* streaming: true,
|
|
2888
|
+
* tools: true,
|
|
2889
|
+
* structuredOutput: true,
|
|
2890
|
+
* imageInput: true,
|
|
2891
|
+
* videoInput: false,
|
|
2892
|
+
* audioInput: false
|
|
2893
|
+
* };
|
|
2894
|
+
* ```
|
|
2895
|
+
*/
|
|
2896
|
+
interface LLMCapabilities {
|
|
2897
|
+
/** Provider API supports streaming responses */
|
|
2898
|
+
streaming: boolean;
|
|
2899
|
+
/** Provider API supports tool/function calling */
|
|
2900
|
+
tools: boolean;
|
|
2901
|
+
/** Provider API supports native structured output (JSON schema) */
|
|
2902
|
+
structuredOutput: boolean;
|
|
2903
|
+
/** Provider API supports image input in messages */
|
|
2904
|
+
imageInput: boolean;
|
|
2905
|
+
/** Provider API supports document input in messages (PDFs, text files) */
|
|
2906
|
+
documentInput: boolean;
|
|
2907
|
+
/** Provider API supports video input in messages */
|
|
2908
|
+
videoInput: boolean;
|
|
2909
|
+
/** Provider API supports audio input in messages */
|
|
2910
|
+
audioInput: boolean;
|
|
2911
|
+
/** Provider API supports image generation output (via image() or built-in tools) */
|
|
2912
|
+
imageOutput?: boolean;
|
|
2913
|
+
}
|
|
2914
|
+
/**
|
|
2915
|
+
* Valid input types for inference.
|
|
2916
|
+
*
|
|
2917
|
+
* Inference input can be a simple string, a Message object, or
|
|
2918
|
+
* a raw ContentBlock for multimodal input.
|
|
2919
|
+
*/
|
|
2920
|
+
type InferenceInput = string | Message | ContentBlock;
|
|
2921
|
+
/**
|
|
2922
|
+
* Options for creating an LLM instance with the llm() function.
|
|
2923
|
+
*
|
|
2924
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
2925
|
+
*
|
|
2926
|
+
* @example
|
|
2927
|
+
* ```typescript
|
|
2928
|
+
* const options: LLMOptions = {
|
|
2929
|
+
* model: openai('gpt-4'),
|
|
2930
|
+
* system: 'You are a helpful assistant.',
|
|
2931
|
+
* params: { temperature: 0.7, max_tokens: 1000 },
|
|
2932
|
+
* tools: [weatherTool, searchTool],
|
|
2933
|
+
* toolStrategy: { maxIterations: 5 }
|
|
2934
|
+
* };
|
|
2935
|
+
*
|
|
2936
|
+
* const instance = llm(options);
|
|
2937
|
+
* ```
|
|
2938
|
+
*/
|
|
2939
|
+
interface LLMOptions<TParams = unknown> {
|
|
2940
|
+
/** A model reference from a provider factory */
|
|
2941
|
+
model: ModelInput;
|
|
2942
|
+
/** Provider infrastructure configuration (optional - uses env vars if omitted) */
|
|
2943
|
+
config?: ProviderConfig;
|
|
2944
|
+
/** Model-specific parameters (temperature, max_tokens, etc.) */
|
|
2945
|
+
params?: TParams;
|
|
2946
|
+
/**
|
|
2947
|
+
* System prompt for all inferences.
|
|
2948
|
+
*
|
|
2949
|
+
* Can be a simple string or a provider-specific array format:
|
|
2950
|
+
* - Anthropic: `[{type: 'text', text: '...', cache_control?: {...}}]`
|
|
2951
|
+
* - Google: `[{text: '...'}, {text: '...'}]` (parts array)
|
|
2952
|
+
*
|
|
2953
|
+
* Array formats are passed through directly to the provider.
|
|
2954
|
+
*/
|
|
2955
|
+
system?: string | unknown[];
|
|
2956
|
+
/** Tools available to the model */
|
|
2957
|
+
tools?: Tool[];
|
|
2958
|
+
/** Tool execution strategy */
|
|
2959
|
+
toolStrategy?: ToolUseStrategy;
|
|
2960
|
+
/** Structured output schema (JSON Schema) */
|
|
2961
|
+
structure?: JSONSchema;
|
|
2962
|
+
}
|
|
2963
|
+
/**
|
|
2964
|
+
* LLM instance returned by the llm() function.
|
|
2965
|
+
*
|
|
2966
|
+
* Provides methods for generating responses and streaming output,
|
|
2967
|
+
* with access to the bound model and capabilities.
|
|
2968
|
+
*
|
|
2969
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
2970
|
+
*
|
|
2971
|
+
* @example
|
|
2972
|
+
* ```typescript
|
|
2973
|
+
* import { llm, openai, StreamEventType } from 'provider-protocol';
|
|
2974
|
+
*
|
|
2975
|
+
* const instance = llm({ model: openai('gpt-4') });
|
|
2976
|
+
*
|
|
2977
|
+
* // Simple generation
|
|
2978
|
+
* const turn = await instance.generate('Hello!');
|
|
2979
|
+
* console.log(turn.response.text);
|
|
2980
|
+
*
|
|
2981
|
+
* // Streaming
|
|
2982
|
+
* const stream = instance.stream('Tell me a story');
|
|
2983
|
+
* for await (const event of stream) {
|
|
2984
|
+
* if (event.type === StreamEventType.TextDelta) {
|
|
2985
|
+
* process.stdout.write(event.delta.text ?? '');
|
|
2986
|
+
* }
|
|
2987
|
+
* }
|
|
2988
|
+
* const finalTurn = await stream.turn;
|
|
2989
|
+
* ```
|
|
2990
|
+
*/
|
|
2991
|
+
interface LLMInstance<TParams = unknown> {
|
|
2992
|
+
/**
|
|
2993
|
+
* Executes inference and returns the complete Turn.
|
|
2994
|
+
*
|
|
2995
|
+
* Supports multiple calling patterns:
|
|
2996
|
+
* - Single input: `generate('Hello')`
|
|
2997
|
+
* - Multiple inputs: `generate('Context...', 'Question?')`
|
|
2998
|
+
* - With history: `generate(messages, 'Follow-up?')`
|
|
2999
|
+
* - With thread: `generate(thread, 'Next message')`
|
|
3000
|
+
*
|
|
3001
|
+
* @param historyOrInput - History (Message[] or Thread) or first input
|
|
3002
|
+
* @param input - Additional inputs to include in the request
|
|
3003
|
+
* @returns Promise resolving to the complete Turn
|
|
3004
|
+
*/
|
|
3005
|
+
generate(historyOrInput: Message[] | Thread | InferenceInput, ...input: InferenceInput[]): Promise<Turn>;
|
|
3006
|
+
/**
|
|
3007
|
+
* Executes streaming inference.
|
|
3008
|
+
*
|
|
3009
|
+
* Returns an async iterable of stream events that can also
|
|
3010
|
+
* be awaited for the final Turn.
|
|
3011
|
+
*
|
|
3012
|
+
* @param historyOrInput - History (Message[] or Thread) or first input
|
|
3013
|
+
* @param input - Additional inputs to include in the request
|
|
3014
|
+
* @returns StreamResult that yields events and resolves to Turn
|
|
3015
|
+
*/
|
|
3016
|
+
stream(historyOrInput: Message[] | Thread | InferenceInput, ...input: InferenceInput[]): StreamResult;
|
|
3017
|
+
/** The bound model instance */
|
|
3018
|
+
readonly model: BoundLLMModel<TParams>;
|
|
3019
|
+
/** Current system prompt (string or provider-specific array format) */
|
|
3020
|
+
readonly system: string | unknown[] | undefined;
|
|
3021
|
+
/** Current model parameters */
|
|
3022
|
+
readonly params: TParams | undefined;
|
|
3023
|
+
/** Provider API capabilities */
|
|
3024
|
+
readonly capabilities: LLMCapabilities;
|
|
3025
|
+
}
|
|
3026
|
+
/**
|
|
3027
|
+
* Request passed from the llm() core to providers.
|
|
3028
|
+
*
|
|
3029
|
+
* Contains all information needed by a provider to execute inference.
|
|
3030
|
+
* The config is required here because llm() resolves defaults before
|
|
3031
|
+
* passing to providers.
|
|
3032
|
+
*
|
|
3033
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
3034
|
+
* @internal
|
|
3035
|
+
*/
|
|
3036
|
+
interface LLMRequest<TParams = unknown> {
|
|
3037
|
+
/** All messages for this request (history + new input) */
|
|
3038
|
+
messages: Message[];
|
|
3039
|
+
/**
|
|
3040
|
+
* System prompt - string or provider-specific array format.
|
|
3041
|
+
* Arrays are passed through directly to the provider.
|
|
3042
|
+
*/
|
|
3043
|
+
system?: string | unknown[];
|
|
3044
|
+
/** Model-specific parameters (passed through unchanged) */
|
|
3045
|
+
params?: TParams;
|
|
3046
|
+
/** Tools available for this request */
|
|
3047
|
+
tools?: Tool[];
|
|
3048
|
+
/** Structured output schema (if requested) */
|
|
3049
|
+
structure?: JSONSchema;
|
|
3050
|
+
/** Provider infrastructure config (resolved by llm() core) */
|
|
3051
|
+
config: ProviderConfig;
|
|
3052
|
+
/** Abort signal for cancellation */
|
|
3053
|
+
signal?: AbortSignal;
|
|
3054
|
+
}
|
|
3055
|
+
/**
|
|
3056
|
+
* Raw provider response from a single inference cycle.
|
|
3057
|
+
*
|
|
3058
|
+
* Does not include tool loop handling - that's managed by llm() core.
|
|
3059
|
+
*
|
|
3060
|
+
* @internal
|
|
3061
|
+
*/
|
|
3062
|
+
interface LLMResponse {
|
|
3063
|
+
/** The assistant's response message */
|
|
3064
|
+
message: AssistantMessage;
|
|
3065
|
+
/** Token usage for this cycle */
|
|
3066
|
+
usage: TokenUsage;
|
|
3067
|
+
/** Stop reason from the provider */
|
|
3068
|
+
stopReason: string;
|
|
3069
|
+
/**
|
|
3070
|
+
* Structured output data extracted by the provider.
|
|
3071
|
+
* Present when a structure schema was requested and successfully extracted.
|
|
3072
|
+
*/
|
|
3073
|
+
data?: unknown;
|
|
3074
|
+
}
|
|
3075
|
+
/**
|
|
3076
|
+
* Raw provider stream result.
|
|
3077
|
+
*
|
|
3078
|
+
* An async iterable of stream events with a Promise that resolves
|
|
3079
|
+
* to the complete response after streaming finishes.
|
|
3080
|
+
*
|
|
3081
|
+
* @internal
|
|
3082
|
+
*/
|
|
3083
|
+
interface LLMStreamResult extends AsyncIterable<StreamEvent> {
|
|
3084
|
+
/** Promise resolving to the complete response */
|
|
3085
|
+
readonly response: Promise<LLMResponse>;
|
|
3086
|
+
}
|
|
3087
|
+
/**
|
|
3088
|
+
* Bound LLM model - full definition.
|
|
3089
|
+
*
|
|
3090
|
+
* Represents a model bound to a specific provider and model ID,
|
|
3091
|
+
* ready to execute inference requests.
|
|
3092
|
+
*
|
|
3093
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
3094
|
+
*/
|
|
3095
|
+
interface BoundLLMModel<TParams = unknown> {
|
|
3096
|
+
/** The model identifier */
|
|
3097
|
+
readonly modelId: string;
|
|
3098
|
+
/** Reference to the parent provider */
|
|
3099
|
+
readonly provider: LLMProvider<TParams>;
|
|
3100
|
+
/** Provider API capabilities */
|
|
3101
|
+
readonly capabilities: LLMCapabilities;
|
|
3102
|
+
/**
|
|
3103
|
+
* Executes a single non-streaming inference request.
|
|
3104
|
+
*
|
|
3105
|
+
* @param request - The inference request
|
|
3106
|
+
* @returns Promise resolving to the response
|
|
3107
|
+
*/
|
|
3108
|
+
complete(request: LLMRequest<TParams>): Promise<LLMResponse>;
|
|
3109
|
+
/**
|
|
3110
|
+
* Executes a single streaming inference request.
|
|
3111
|
+
*
|
|
3112
|
+
* @param request - The inference request
|
|
3113
|
+
* @returns Stream result with events and final response
|
|
3114
|
+
*/
|
|
3115
|
+
stream(request: LLMRequest<TParams>): LLMStreamResult;
|
|
3116
|
+
}
|
|
3117
|
+
|
|
3118
|
+
export { MessageRole as $, type AudioBlock as A, type BinaryBlock as B, type ContentBlock as C, type DocumentSource as D, type EmbeddingHandler as E, isBinaryBlock as F, type Tool as G, type ToolCall as H, type ImageOptions as I, type JSONSchema as J, type ToolResult as K, type LLMOptions as L, type ModelReference as M, type ToolMetadata as N, type ToolUseStrategy as O, type Provider as P, type BeforeCallResult as Q, type ReasoningBlock as R, type AfterCallResult as S, type TextBlock as T, UPPError as U, type VideoBlock as V, type ToolExecution as W, Message as X, UserMessage as Y, AssistantMessage as Z, ToolResultMessage as _, type LLMInstance as a, isUserMessage as a0, isAssistantMessage as a1, isToolResultMessage as a2, type MessageType as a3, type MessageMetadata as a4, type MessageOptions as a5, type Turn as a6, type TokenUsage as a7, createTurn as a8, emptyUsage as a9, type EmbeddingUsage as aA, type EmbeddingInput as aB, type LLMCapabilities as aC, type LLMRequest as aD, type LLMResponse as aE, type LLMStreamResult as aF, type BoundLLMModel as aG, type InferenceInput as aH, type ImageInput as aI, type ImageEditInput as aJ, type ImageGenerateOptions as aK, type GeneratedImage as aL, type ImageUsage as aM, type ImageResult as aN, type ImageStreamEvent as aO, type ImageStreamResult as aP, type ImageCapabilities as aQ, type ImageRequest as aR, type ImageEditRequest as aS, type ImageResponse as aT, type ImageProviderStreamResult as aU, type BoundImageModel as aV, type ImageModelInput as aW, type TurnJSON as aX, aggregateUsage as aa, Thread as ab, type ThreadJSON as ac, type MessageJSON as ad, type StreamEvent as ae, type EventDelta as af, type StreamResult as ag, StreamEventType as ah, createStreamResult as ai, textDelta as aj, toolCallDelta as ak, messageStart as al, messageStop as am, contentBlockStart as an, contentBlockStop as ao, type ProviderIdentity as ap, type ProviderConfig as aq, type KeyStrategy as ar, type RetryStrategy as as, type LLMProvider as at, type EmbeddingProvider as au, type ImageProvider as av, type BoundEmbeddingModel as aw, type EmbeddingRequest as ax, type EmbeddingResponse as ay, type EmbeddingVector as az, type ImageInstance as b, type LLMHandler as c, type ImageHandler as d, type DocumentBlock as e, Image as f, ErrorCode as g, ModalityType as h, type Modality as i, type JSONSchemaProperty as j, type JSONSchemaPropertyType as k, type ImageBlock as l, type ImageSource as m, type UserContent as n, type AssistantContent as o, ContentBlockType as p, ImageSourceType as q, DocumentSourceType as r, reasoning as s, text as t, isTextBlock as u, isReasoningBlock as v, isImageBlock as w, isDocumentBlock as x, isAudioBlock as y, isVideoBlock as z };
|