@providerprotocol/ai 0.0.12 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/anthropic/index.d.ts +51 -15
- package/dist/anthropic/index.js +80 -29
- package/dist/anthropic/index.js.map +1 -1
- package/dist/{chunk-SUNYWHTH.js → chunk-MOU4U3PO.js} +55 -3
- package/dist/chunk-MOU4U3PO.js.map +1 -0
- package/dist/{chunk-Y6Q7JCNP.js → chunk-MSR5P65T.js} +1 -1
- package/dist/chunk-MSR5P65T.js.map +1 -0
- package/dist/{chunk-W4BB4BG2.js → chunk-SVYROCLD.js} +31 -11
- package/dist/chunk-SVYROCLD.js.map +1 -0
- package/dist/chunk-U4JJC2YX.js +234 -0
- package/dist/chunk-U4JJC2YX.js.map +1 -0
- package/dist/{chunk-X5G4EHL7.js → chunk-Z7RBRCRN.js} +1 -1
- package/dist/chunk-Z7RBRCRN.js.map +1 -0
- package/dist/google/index.d.ts +376 -7
- package/dist/google/index.js +149 -21
- package/dist/google/index.js.map +1 -1
- package/dist/http/index.d.ts +222 -25
- package/dist/http/index.js +3 -3
- package/dist/index.d.ts +1484 -198
- package/dist/index.js +233 -47
- package/dist/index.js.map +1 -1
- package/dist/ollama/index.d.ts +92 -20
- package/dist/ollama/index.js +31 -7
- package/dist/ollama/index.js.map +1 -1
- package/dist/openai/index.d.ts +340 -61
- package/dist/openai/index.js +105 -31
- package/dist/openai/index.js.map +1 -1
- package/dist/openrouter/index.d.ts +107 -51
- package/dist/openrouter/index.js +84 -24
- package/dist/openrouter/index.js.map +1 -1
- package/dist/provider-Bi0nyNhA.d.ts +505 -0
- package/dist/retry-BatS2hjD.d.ts +508 -0
- package/dist/xai/index.d.ts +97 -22
- package/dist/xai/index.js +129 -45
- package/dist/xai/index.js.map +1 -1
- package/package.json +8 -3
- package/dist/chunk-CUCRF5W6.js +0 -136
- package/dist/chunk-CUCRF5W6.js.map +0 -1
- package/dist/chunk-SUNYWHTH.js.map +0 -1
- package/dist/chunk-W4BB4BG2.js.map +0 -1
- package/dist/chunk-X5G4EHL7.js.map +0 -1
- package/dist/chunk-Y6Q7JCNP.js.map +0 -1
- package/dist/provider-CUJWjgNl.d.ts +0 -192
- package/dist/retry-I2661_rv.d.ts +0 -118
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,41 @@
|
|
|
1
|
-
import { M as ModelReference, P as ProviderConfig, L as LLMProvider, a as LLMHandler$1, E as EmbeddingHandler, I as ImageHandler, b as Provider } from './provider-
|
|
2
|
-
export { B as BoundEmbeddingModel, g as BoundImageModel, e as EmbeddingProvider, c as ErrorCode, f as ImageProvider, K as KeyStrategy, d as Modality, R as RetryStrategy, U as UPPError } from './provider-
|
|
3
|
-
export { D as DynamicKey, E as ExponentialBackoff, L as LinearBackoff, N as NoRetry, a as RetryAfterStrategy, R as RoundRobinKeys, T as TokenBucket, W as WeightedKeys } from './retry-
|
|
1
|
+
import { M as ModelReference, P as ProviderConfig, L as LLMProvider, a as LLMHandler$1, E as EmbeddingHandler, I as ImageHandler, b as Provider } from './provider-Bi0nyNhA.js';
|
|
2
|
+
export { B as BoundEmbeddingModel, g as BoundImageModel, e as EmbeddingProvider, c as ErrorCode, f as ImageProvider, K as KeyStrategy, d as Modality, R as RetryStrategy, U as UPPError } from './provider-Bi0nyNhA.js';
|
|
3
|
+
export { D as DynamicKey, E as ExponentialBackoff, L as LinearBackoff, N as NoRetry, a as RetryAfterStrategy, R as RoundRobinKeys, T as TokenBucket, W as WeightedKeys } from './retry-BatS2hjD.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Content block types for messages
|
|
6
|
+
* @fileoverview Content block types for multimodal messages.
|
|
7
|
+
*
|
|
8
|
+
* Defines the various content block types that can be included in
|
|
9
|
+
* user and assistant messages, supporting text, images, audio, video,
|
|
10
|
+
* and arbitrary binary data.
|
|
11
|
+
*
|
|
12
|
+
* @module types/content
|
|
7
13
|
*/
|
|
8
14
|
/**
|
|
9
|
-
* Image source
|
|
15
|
+
* Image source variants for ImageBlock.
|
|
16
|
+
*
|
|
17
|
+
* Images can be provided as base64-encoded strings, URLs, or raw bytes.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* // Base64 encoded image
|
|
22
|
+
* const base64Source: ImageSource = {
|
|
23
|
+
* type: 'base64',
|
|
24
|
+
* data: 'iVBORw0KGgo...'
|
|
25
|
+
* };
|
|
26
|
+
*
|
|
27
|
+
* // URL reference
|
|
28
|
+
* const urlSource: ImageSource = {
|
|
29
|
+
* type: 'url',
|
|
30
|
+
* url: 'https://example.com/image.png'
|
|
31
|
+
* };
|
|
32
|
+
*
|
|
33
|
+
* // Raw bytes
|
|
34
|
+
* const bytesSource: ImageSource = {
|
|
35
|
+
* type: 'bytes',
|
|
36
|
+
* data: new Uint8Array([...])
|
|
37
|
+
* };
|
|
38
|
+
* ```
|
|
10
39
|
*/
|
|
11
40
|
type ImageSource = {
|
|
12
41
|
type: 'base64';
|
|
@@ -19,186 +48,639 @@ type ImageSource = {
|
|
|
19
48
|
data: Uint8Array;
|
|
20
49
|
};
|
|
21
50
|
/**
|
|
22
|
-
* Text content block
|
|
51
|
+
* Text content block.
|
|
52
|
+
*
|
|
53
|
+
* The most common content block type, containing plain text content.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const textBlock: TextBlock = {
|
|
58
|
+
* type: 'text',
|
|
59
|
+
* text: 'Hello, world!'
|
|
60
|
+
* };
|
|
61
|
+
* ```
|
|
23
62
|
*/
|
|
24
63
|
interface TextBlock {
|
|
64
|
+
/** Discriminator for text blocks */
|
|
25
65
|
type: 'text';
|
|
66
|
+
/** The text content */
|
|
26
67
|
text: string;
|
|
27
68
|
}
|
|
28
69
|
/**
|
|
29
|
-
* Image content block
|
|
70
|
+
* Image content block.
|
|
71
|
+
*
|
|
72
|
+
* Contains an image with its source data and metadata.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const imageBlock: ImageBlock = {
|
|
77
|
+
* type: 'image',
|
|
78
|
+
* source: { type: 'url', url: 'https://example.com/photo.jpg' },
|
|
79
|
+
* mimeType: 'image/jpeg',
|
|
80
|
+
* width: 1920,
|
|
81
|
+
* height: 1080
|
|
82
|
+
* };
|
|
83
|
+
* ```
|
|
30
84
|
*/
|
|
31
85
|
interface ImageBlock {
|
|
86
|
+
/** Discriminator for image blocks */
|
|
32
87
|
type: 'image';
|
|
88
|
+
/** The image data source */
|
|
33
89
|
source: ImageSource;
|
|
90
|
+
/** MIME type of the image (e.g., 'image/png', 'image/jpeg') */
|
|
34
91
|
mimeType: string;
|
|
92
|
+
/** Image width in pixels */
|
|
35
93
|
width?: number;
|
|
94
|
+
/** Image height in pixels */
|
|
36
95
|
height?: number;
|
|
37
96
|
}
|
|
38
97
|
/**
|
|
39
|
-
* Audio content block
|
|
98
|
+
* Audio content block.
|
|
99
|
+
*
|
|
100
|
+
* Contains audio data with its metadata.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* const audioBlock: AudioBlock = {
|
|
105
|
+
* type: 'audio',
|
|
106
|
+
* data: audioBytes,
|
|
107
|
+
* mimeType: 'audio/mp3',
|
|
108
|
+
* duration: 120.5
|
|
109
|
+
* };
|
|
110
|
+
* ```
|
|
40
111
|
*/
|
|
41
112
|
interface AudioBlock {
|
|
113
|
+
/** Discriminator for audio blocks */
|
|
42
114
|
type: 'audio';
|
|
115
|
+
/** Raw audio data */
|
|
43
116
|
data: Uint8Array;
|
|
117
|
+
/** MIME type of the audio (e.g., 'audio/mp3', 'audio/wav') */
|
|
44
118
|
mimeType: string;
|
|
119
|
+
/** Duration in seconds */
|
|
45
120
|
duration?: number;
|
|
46
121
|
}
|
|
47
122
|
/**
|
|
48
|
-
* Video content block
|
|
123
|
+
* Video content block.
|
|
124
|
+
*
|
|
125
|
+
* Contains video data with its metadata.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* const videoBlock: VideoBlock = {
|
|
130
|
+
* type: 'video',
|
|
131
|
+
* data: videoBytes,
|
|
132
|
+
* mimeType: 'video/mp4',
|
|
133
|
+
* duration: 30,
|
|
134
|
+
* width: 1920,
|
|
135
|
+
* height: 1080
|
|
136
|
+
* };
|
|
137
|
+
* ```
|
|
49
138
|
*/
|
|
50
139
|
interface VideoBlock {
|
|
140
|
+
/** Discriminator for video blocks */
|
|
51
141
|
type: 'video';
|
|
142
|
+
/** Raw video data */
|
|
52
143
|
data: Uint8Array;
|
|
144
|
+
/** MIME type of the video (e.g., 'video/mp4', 'video/webm') */
|
|
53
145
|
mimeType: string;
|
|
146
|
+
/** Duration in seconds */
|
|
54
147
|
duration?: number;
|
|
148
|
+
/** Video width in pixels */
|
|
55
149
|
width?: number;
|
|
150
|
+
/** Video height in pixels */
|
|
56
151
|
height?: number;
|
|
57
152
|
}
|
|
58
153
|
/**
|
|
59
|
-
* Binary content block for arbitrary data
|
|
154
|
+
* Binary content block for arbitrary data.
|
|
155
|
+
*
|
|
156
|
+
* A generic block type for data that doesn't fit other categories.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* const binaryBlock: BinaryBlock = {
|
|
161
|
+
* type: 'binary',
|
|
162
|
+
* data: pdfBytes,
|
|
163
|
+
* mimeType: 'application/pdf',
|
|
164
|
+
* metadata: { filename: 'document.pdf', pages: 10 }
|
|
165
|
+
* };
|
|
166
|
+
* ```
|
|
60
167
|
*/
|
|
61
168
|
interface BinaryBlock {
|
|
169
|
+
/** Discriminator for binary blocks */
|
|
62
170
|
type: 'binary';
|
|
171
|
+
/** Raw binary data */
|
|
63
172
|
data: Uint8Array;
|
|
173
|
+
/** MIME type of the data */
|
|
64
174
|
mimeType: string;
|
|
175
|
+
/** Additional metadata about the binary content */
|
|
65
176
|
metadata?: Record<string, unknown>;
|
|
66
177
|
}
|
|
67
178
|
/**
|
|
68
|
-
*
|
|
179
|
+
* Union of all content block types.
|
|
180
|
+
*
|
|
181
|
+
* Used when a function or property can accept any type of content block.
|
|
69
182
|
*/
|
|
70
183
|
type ContentBlock = TextBlock | ImageBlock | AudioBlock | VideoBlock | BinaryBlock;
|
|
71
184
|
/**
|
|
72
|
-
* Content types allowed in user messages
|
|
185
|
+
* Content types allowed in user messages.
|
|
186
|
+
*
|
|
187
|
+
* Users can send any type of content block including binary data.
|
|
73
188
|
*/
|
|
74
189
|
type UserContent = TextBlock | ImageBlock | AudioBlock | VideoBlock | BinaryBlock;
|
|
75
190
|
/**
|
|
76
|
-
* Content types allowed in assistant messages
|
|
191
|
+
* Content types allowed in assistant messages.
|
|
192
|
+
*
|
|
193
|
+
* Assistants can generate text and media but not arbitrary binary data.
|
|
77
194
|
*/
|
|
78
195
|
type AssistantContent = TextBlock | ImageBlock | AudioBlock | VideoBlock;
|
|
79
196
|
/**
|
|
80
|
-
*
|
|
197
|
+
* Creates a text content block from a string.
|
|
198
|
+
*
|
|
199
|
+
* @param content - The text content
|
|
200
|
+
* @returns A TextBlock containing the provided text
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```typescript
|
|
204
|
+
* const block = text('Hello, world!');
|
|
205
|
+
* // { type: 'text', text: 'Hello, world!' }
|
|
206
|
+
* ```
|
|
81
207
|
*/
|
|
82
208
|
declare function text(content: string): TextBlock;
|
|
83
209
|
/**
|
|
84
|
-
* Type guard for TextBlock
|
|
210
|
+
* Type guard for TextBlock.
|
|
211
|
+
*
|
|
212
|
+
* @param block - The content block to check
|
|
213
|
+
* @returns True if the block is a TextBlock
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```typescript
|
|
217
|
+
* if (isTextBlock(block)) {
|
|
218
|
+
* console.log(block.text);
|
|
219
|
+
* }
|
|
220
|
+
* ```
|
|
85
221
|
*/
|
|
86
222
|
declare function isTextBlock(block: ContentBlock): block is TextBlock;
|
|
87
223
|
/**
|
|
88
|
-
* Type guard for ImageBlock
|
|
224
|
+
* Type guard for ImageBlock.
|
|
225
|
+
*
|
|
226
|
+
* @param block - The content block to check
|
|
227
|
+
* @returns True if the block is an ImageBlock
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* ```typescript
|
|
231
|
+
* if (isImageBlock(block)) {
|
|
232
|
+
* console.log(block.mimeType, block.width, block.height);
|
|
233
|
+
* }
|
|
234
|
+
* ```
|
|
89
235
|
*/
|
|
90
236
|
declare function isImageBlock(block: ContentBlock): block is ImageBlock;
|
|
91
237
|
/**
|
|
92
|
-
* Type guard for AudioBlock
|
|
238
|
+
* Type guard for AudioBlock.
|
|
239
|
+
*
|
|
240
|
+
* @param block - The content block to check
|
|
241
|
+
* @returns True if the block is an AudioBlock
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```typescript
|
|
245
|
+
* if (isAudioBlock(block)) {
|
|
246
|
+
* console.log(block.mimeType, block.duration);
|
|
247
|
+
* }
|
|
248
|
+
* ```
|
|
93
249
|
*/
|
|
94
250
|
declare function isAudioBlock(block: ContentBlock): block is AudioBlock;
|
|
95
251
|
/**
|
|
96
|
-
* Type guard for VideoBlock
|
|
252
|
+
* Type guard for VideoBlock.
|
|
253
|
+
*
|
|
254
|
+
* @param block - The content block to check
|
|
255
|
+
* @returns True if the block is a VideoBlock
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```typescript
|
|
259
|
+
* if (isVideoBlock(block)) {
|
|
260
|
+
* console.log(block.mimeType, block.duration);
|
|
261
|
+
* }
|
|
262
|
+
* ```
|
|
97
263
|
*/
|
|
98
264
|
declare function isVideoBlock(block: ContentBlock): block is VideoBlock;
|
|
99
265
|
/**
|
|
100
|
-
* Type guard for BinaryBlock
|
|
266
|
+
* Type guard for BinaryBlock.
|
|
267
|
+
*
|
|
268
|
+
* @param block - The content block to check
|
|
269
|
+
* @returns True if the block is a BinaryBlock
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* ```typescript
|
|
273
|
+
* if (isBinaryBlock(block)) {
|
|
274
|
+
* console.log(block.mimeType, block.metadata);
|
|
275
|
+
* }
|
|
276
|
+
* ```
|
|
101
277
|
*/
|
|
102
278
|
declare function isBinaryBlock(block: ContentBlock): block is BinaryBlock;
|
|
103
279
|
|
|
104
280
|
/**
|
|
105
|
-
* JSON Schema types for tool parameters and structured outputs
|
|
281
|
+
* @fileoverview JSON Schema types for tool parameters and structured outputs.
|
|
282
|
+
*
|
|
283
|
+
* Provides TypeScript interfaces for defining JSON Schema objects used in
|
|
284
|
+
* LLM tool definitions and structured output specifications.
|
|
285
|
+
*
|
|
286
|
+
* @module types/schema
|
|
106
287
|
*/
|
|
107
|
-
type JSONSchemaPropertyType = 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'object' | 'null';
|
|
108
288
|
/**
|
|
109
|
-
* JSON Schema property
|
|
289
|
+
* Primitive and composite JSON Schema property types.
|
|
290
|
+
*
|
|
291
|
+
* These types correspond to the JSON Schema specification's allowed type values.
|
|
292
|
+
*/
|
|
293
|
+
type JSONSchemaPropertyType =
|
|
294
|
+
/** String values */
|
|
295
|
+
'string'
|
|
296
|
+
/** Floating point numbers */
|
|
297
|
+
| 'number'
|
|
298
|
+
/** Whole numbers */
|
|
299
|
+
| 'integer'
|
|
300
|
+
/** Boolean true/false values */
|
|
301
|
+
| 'boolean'
|
|
302
|
+
/** Ordered lists of values */
|
|
303
|
+
| 'array'
|
|
304
|
+
/** Key-value mappings */
|
|
305
|
+
| 'object'
|
|
306
|
+
/** Explicit null value */
|
|
307
|
+
| 'null';
|
|
308
|
+
/**
|
|
309
|
+
* JSON Schema property definition.
|
|
310
|
+
*
|
|
311
|
+
* Describes a single property within a JSON Schema object, including
|
|
312
|
+
* type constraints, validation rules, and nested structure definitions.
|
|
313
|
+
*
|
|
314
|
+
* @example
|
|
315
|
+
* ```typescript
|
|
316
|
+
* const nameProperty: JSONSchemaProperty = {
|
|
317
|
+
* type: 'string',
|
|
318
|
+
* description: 'User name',
|
|
319
|
+
* minLength: 1,
|
|
320
|
+
* maxLength: 100
|
|
321
|
+
* };
|
|
322
|
+
* ```
|
|
323
|
+
*
|
|
324
|
+
* @example
|
|
325
|
+
* ```typescript
|
|
326
|
+
* const tagsProperty: JSONSchemaProperty = {
|
|
327
|
+
* type: 'array',
|
|
328
|
+
* description: 'List of tags',
|
|
329
|
+
* items: { type: 'string' },
|
|
330
|
+
* minItems: 1,
|
|
331
|
+
* uniqueItems: true
|
|
332
|
+
* };
|
|
333
|
+
* ```
|
|
110
334
|
*/
|
|
111
335
|
interface JSONSchemaProperty {
|
|
336
|
+
/** The JSON type of this property */
|
|
112
337
|
type: JSONSchemaPropertyType;
|
|
338
|
+
/** Human-readable description for the LLM */
|
|
113
339
|
description?: string;
|
|
340
|
+
/** Allowed values (enumeration) */
|
|
114
341
|
enum?: unknown[];
|
|
342
|
+
/** Constant value this property must equal */
|
|
115
343
|
const?: unknown;
|
|
344
|
+
/** Default value if not provided */
|
|
116
345
|
default?: unknown;
|
|
346
|
+
/** Minimum string length (string type only) */
|
|
117
347
|
minLength?: number;
|
|
348
|
+
/** Maximum string length (string type only) */
|
|
118
349
|
maxLength?: number;
|
|
350
|
+
/** Regular expression pattern for validation (string type only) */
|
|
119
351
|
pattern?: string;
|
|
352
|
+
/** Semantic format hint (string type only) */
|
|
120
353
|
format?: 'email' | 'uri' | 'date' | 'date-time' | 'uuid';
|
|
354
|
+
/** Minimum value inclusive (number/integer types only) */
|
|
121
355
|
minimum?: number;
|
|
356
|
+
/** Maximum value inclusive (number/integer types only) */
|
|
122
357
|
maximum?: number;
|
|
358
|
+
/** Minimum value exclusive (number/integer types only) */
|
|
123
359
|
exclusiveMinimum?: number;
|
|
360
|
+
/** Maximum value exclusive (number/integer types only) */
|
|
124
361
|
exclusiveMaximum?: number;
|
|
362
|
+
/** Value must be divisible by this (number/integer types only) */
|
|
125
363
|
multipleOf?: number;
|
|
364
|
+
/** Schema for array elements (array type only) */
|
|
126
365
|
items?: JSONSchemaProperty;
|
|
366
|
+
/** Minimum array length (array type only) */
|
|
127
367
|
minItems?: number;
|
|
368
|
+
/** Maximum array length (array type only) */
|
|
128
369
|
maxItems?: number;
|
|
370
|
+
/** Whether array elements must be unique (array type only) */
|
|
129
371
|
uniqueItems?: boolean;
|
|
372
|
+
/** Nested property definitions (object type only) */
|
|
130
373
|
properties?: Record<string, JSONSchemaProperty>;
|
|
374
|
+
/** List of required property names (object type only) */
|
|
131
375
|
required?: string[];
|
|
376
|
+
/** Whether additional properties are allowed (object type only) */
|
|
132
377
|
additionalProperties?: boolean;
|
|
133
378
|
}
|
|
134
379
|
/**
|
|
135
|
-
* JSON Schema for tool parameters or structured outputs
|
|
380
|
+
* Root JSON Schema for tool parameters or structured outputs.
|
|
381
|
+
*
|
|
382
|
+
* This is the top-level schema definition used when defining tool
|
|
383
|
+
* parameters or requesting structured output from an LLM.
|
|
384
|
+
*
|
|
385
|
+
* @example
|
|
386
|
+
* ```typescript
|
|
387
|
+
* const weatherToolSchema: JSONSchema = {
|
|
388
|
+
* type: 'object',
|
|
389
|
+
* description: 'Parameters for getting weather information',
|
|
390
|
+
* properties: {
|
|
391
|
+
* location: {
|
|
392
|
+
* type: 'string',
|
|
393
|
+
* description: 'City name or coordinates'
|
|
394
|
+
* },
|
|
395
|
+
* units: {
|
|
396
|
+
* type: 'string',
|
|
397
|
+
* enum: ['celsius', 'fahrenheit'],
|
|
398
|
+
* description: 'Temperature units'
|
|
399
|
+
* }
|
|
400
|
+
* },
|
|
401
|
+
* required: ['location']
|
|
402
|
+
* };
|
|
403
|
+
* ```
|
|
136
404
|
*/
|
|
137
405
|
interface JSONSchema {
|
|
406
|
+
/** Root schemas are always objects */
|
|
138
407
|
type: 'object';
|
|
408
|
+
/** Property definitions for the object */
|
|
139
409
|
properties: Record<string, JSONSchemaProperty>;
|
|
410
|
+
/** List of required property names */
|
|
140
411
|
required?: string[];
|
|
412
|
+
/** Whether additional properties are allowed beyond those defined */
|
|
141
413
|
additionalProperties?: boolean;
|
|
414
|
+
/** Human-readable description of the schema's purpose */
|
|
142
415
|
description?: string;
|
|
143
416
|
}
|
|
144
417
|
|
|
145
418
|
/**
|
|
146
|
-
* Tool
|
|
419
|
+
* @fileoverview Tool types for LLM function calling.
|
|
420
|
+
*
|
|
421
|
+
* Defines the interfaces for registering tools with LLMs, handling
|
|
422
|
+
* tool calls from the model, and managing tool execution strategies.
|
|
423
|
+
*
|
|
424
|
+
* @module types/tool
|
|
425
|
+
*/
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Provider-namespaced metadata for tools.
|
|
429
|
+
*
|
|
430
|
+
* Each provider can attach its own metadata under its namespace,
|
|
431
|
+
* enabling provider-specific features like caching, strict mode, etc.
|
|
432
|
+
*
|
|
433
|
+
* @example
|
|
434
|
+
* ```typescript
|
|
435
|
+
* const metadata: ToolMetadata = {
|
|
436
|
+
* anthropic: { cache_control: { type: 'ephemeral' } },
|
|
437
|
+
* openrouter: { cache_control: { type: 'ephemeral', ttl: '1h' } }
|
|
438
|
+
* };
|
|
439
|
+
* ```
|
|
440
|
+
*/
|
|
441
|
+
interface ToolMetadata {
|
|
442
|
+
[provider: string]: Record<string, unknown> | undefined;
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Tool call requested by the model.
|
|
446
|
+
*
|
|
447
|
+
* Represents a single function call request from the LLM, including
|
|
448
|
+
* the tool name and parsed arguments.
|
|
449
|
+
*
|
|
450
|
+
* @example
|
|
451
|
+
* ```typescript
|
|
452
|
+
* const toolCall: ToolCall = {
|
|
453
|
+
* toolCallId: 'call_abc123',
|
|
454
|
+
* toolName: 'get_weather',
|
|
455
|
+
* arguments: { location: 'San Francisco', units: 'celsius' }
|
|
456
|
+
* };
|
|
457
|
+
* ```
|
|
147
458
|
*/
|
|
148
459
|
interface ToolCall {
|
|
460
|
+
/** Unique identifier for this tool call, used to match results */
|
|
149
461
|
toolCallId: string;
|
|
462
|
+
/** Name of the tool being called */
|
|
150
463
|
toolName: string;
|
|
464
|
+
/** Parsed arguments for the tool call */
|
|
151
465
|
arguments: Record<string, unknown>;
|
|
152
466
|
}
|
|
153
467
|
/**
|
|
154
|
-
* Result of tool execution
|
|
468
|
+
* Result of tool execution.
|
|
469
|
+
*
|
|
470
|
+
* Returned after executing a tool, containing the result data
|
|
471
|
+
* and whether an error occurred.
|
|
472
|
+
*
|
|
473
|
+
* @example
|
|
474
|
+
* ```typescript
|
|
475
|
+
* const result: ToolResult = {
|
|
476
|
+
* toolCallId: 'call_abc123',
|
|
477
|
+
* result: { temperature: 72, conditions: 'sunny' }
|
|
478
|
+
* };
|
|
479
|
+
*
|
|
480
|
+
* // Error result
|
|
481
|
+
* const errorResult: ToolResult = {
|
|
482
|
+
* toolCallId: 'call_abc123',
|
|
483
|
+
* result: 'Location not found',
|
|
484
|
+
* isError: true
|
|
485
|
+
* };
|
|
486
|
+
* ```
|
|
155
487
|
*/
|
|
156
488
|
interface ToolResult {
|
|
489
|
+
/** The tool call ID this result corresponds to */
|
|
157
490
|
toolCallId: string;
|
|
491
|
+
/** The result data (can be any serializable value) */
|
|
158
492
|
result: unknown;
|
|
493
|
+
/** Whether the tool execution resulted in an error */
|
|
159
494
|
isError?: boolean;
|
|
160
495
|
}
|
|
161
496
|
/**
|
|
162
|
-
* Tool definition
|
|
497
|
+
* Tool definition for LLM function calling.
|
|
498
|
+
*
|
|
499
|
+
* Defines a tool that can be called by the LLM, including its
|
|
500
|
+
* name, description, parameter schema, and execution function.
|
|
501
|
+
*
|
|
502
|
+
* @typeParam TParams - The type of parameters the tool accepts
|
|
503
|
+
* @typeParam TResult - The type of result the tool returns
|
|
504
|
+
*
|
|
505
|
+
* @example
|
|
506
|
+
* ```typescript
|
|
507
|
+
* const weatherTool: Tool<{ location: string }, WeatherData> = {
|
|
508
|
+
* name: 'get_weather',
|
|
509
|
+
* description: 'Get current weather for a location',
|
|
510
|
+
* parameters: {
|
|
511
|
+
* type: 'object',
|
|
512
|
+
* properties: {
|
|
513
|
+
* location: { type: 'string', description: 'City name' }
|
|
514
|
+
* },
|
|
515
|
+
* required: ['location']
|
|
516
|
+
* },
|
|
517
|
+
* run: async (params) => {
|
|
518
|
+
* return fetchWeather(params.location);
|
|
519
|
+
* }
|
|
520
|
+
* };
|
|
521
|
+
* ```
|
|
163
522
|
*/
|
|
164
523
|
interface Tool<TParams = unknown, TResult = unknown> {
|
|
165
|
-
/** Tool name (must be unique within
|
|
524
|
+
/** Tool name (must be unique within an llm() instance) */
|
|
166
525
|
name: string;
|
|
167
|
-
/** Human-readable description for the model */
|
|
526
|
+
/** Human-readable description for the model to understand when to use this tool */
|
|
168
527
|
description: string;
|
|
169
|
-
/** JSON Schema defining parameters */
|
|
528
|
+
/** JSON Schema defining the tool's parameters */
|
|
170
529
|
parameters: JSONSchema;
|
|
171
|
-
/**
|
|
530
|
+
/**
|
|
531
|
+
* Provider-specific metadata, namespaced by provider name.
|
|
532
|
+
*
|
|
533
|
+
* Used for provider-specific features like prompt caching:
|
|
534
|
+
* @example
|
|
535
|
+
* ```typescript
|
|
536
|
+
* const tool: Tool = {
|
|
537
|
+
* name: 'search_docs',
|
|
538
|
+
* description: 'Search documentation',
|
|
539
|
+
* parameters: {...},
|
|
540
|
+
* run: async (params) => {...},
|
|
541
|
+
* metadata: {
|
|
542
|
+
* anthropic: { cache_control: { type: 'ephemeral' } }
|
|
543
|
+
* }
|
|
544
|
+
* };
|
|
545
|
+
* ```
|
|
546
|
+
*/
|
|
547
|
+
metadata?: ToolMetadata;
|
|
548
|
+
/**
|
|
549
|
+
* Executes the tool with the provided parameters.
|
|
550
|
+
*
|
|
551
|
+
* @param params - The parameters passed by the model
|
|
552
|
+
* @returns The tool result, synchronously or as a Promise
|
|
553
|
+
*/
|
|
172
554
|
run(params: TParams): TResult | Promise<TResult>;
|
|
173
|
-
/**
|
|
555
|
+
/**
|
|
556
|
+
* Optional approval handler for sensitive operations.
|
|
557
|
+
*
|
|
558
|
+
* If provided, this function is called before the tool executes.
|
|
559
|
+
* Return false to prevent execution.
|
|
560
|
+
*
|
|
561
|
+
* @param params - The parameters the tool would be called with
|
|
562
|
+
* @returns Whether to approve the execution
|
|
563
|
+
*/
|
|
174
564
|
approval?(params: TParams): boolean | Promise<boolean>;
|
|
175
565
|
}
|
|
176
566
|
/**
|
|
177
|
-
*
|
|
567
|
+
* Result from onBeforeCall hook indicating whether to proceed and optionally transformed params.
|
|
568
|
+
*/
|
|
569
|
+
interface BeforeCallResult {
|
|
570
|
+
/** Whether to proceed with tool execution */
|
|
571
|
+
proceed: boolean;
|
|
572
|
+
/** Transformed parameters to use instead of the original (optional) */
|
|
573
|
+
params?: unknown;
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* Result from onAfterCall hook optionally containing a transformed result.
|
|
577
|
+
*/
|
|
578
|
+
interface AfterCallResult {
|
|
579
|
+
/** Transformed result to use instead of the original */
|
|
580
|
+
result: unknown;
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Strategy for controlling tool execution behavior.
|
|
584
|
+
*
|
|
585
|
+
* Provides hooks for monitoring, controlling, and transforming the tool execution
|
|
586
|
+
* loop during LLM inference.
|
|
587
|
+
*
|
|
588
|
+
* @example
|
|
589
|
+
* ```typescript
|
|
590
|
+
* const strategy: ToolUseStrategy = {
|
|
591
|
+
* maxIterations: 5,
|
|
592
|
+
* onToolCall: (tool, params) => {
|
|
593
|
+
* console.log(`Calling ${tool.name} with`, params);
|
|
594
|
+
* },
|
|
595
|
+
* // Transform input parameters
|
|
596
|
+
* onBeforeCall: (tool, params) => {
|
|
597
|
+
* if (tool.name === 'search') {
|
|
598
|
+
* return { proceed: true, params: { ...params, limit: 10 } };
|
|
599
|
+
* }
|
|
600
|
+
* return true;
|
|
601
|
+
* },
|
|
602
|
+
* // Transform output results
|
|
603
|
+
* onAfterCall: (tool, params, result) => {
|
|
604
|
+
* if (tool.name === 'fetch_data') {
|
|
605
|
+
* return { result: sanitize(result) };
|
|
606
|
+
* }
|
|
607
|
+
* },
|
|
608
|
+
* onMaxIterations: (iterations) => {
|
|
609
|
+
* console.warn(`Reached max iterations: ${iterations}`);
|
|
610
|
+
* }
|
|
611
|
+
* };
|
|
612
|
+
* ```
|
|
178
613
|
*/
|
|
179
614
|
interface ToolUseStrategy {
|
|
180
|
-
/** Maximum tool execution rounds (default: 10) */
|
|
615
|
+
/** Maximum number of tool execution rounds (default: 10) */
|
|
181
616
|
maxIterations?: number;
|
|
182
|
-
/**
|
|
617
|
+
/**
|
|
618
|
+
* Called when the model requests a tool call.
|
|
619
|
+
*
|
|
620
|
+
* @param tool - The tool being called
|
|
621
|
+
* @param params - The parameters for the call
|
|
622
|
+
*/
|
|
183
623
|
onToolCall?(tool: Tool, params: unknown): void | Promise<void>;
|
|
184
|
-
/**
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
624
|
+
/**
|
|
625
|
+
* Called before tool execution. Can skip execution or transform parameters.
|
|
626
|
+
*
|
|
627
|
+
* @param tool - The tool about to be executed
|
|
628
|
+
* @param params - The parameters for the call
|
|
629
|
+
* @returns One of:
|
|
630
|
+
* - `false` to skip execution
|
|
631
|
+
* - `true` to proceed with original params
|
|
632
|
+
* - `BeforeCallResult` object to control execution and optionally transform params
|
|
633
|
+
*/
|
|
634
|
+
onBeforeCall?(tool: Tool, params: unknown): boolean | BeforeCallResult | Promise<boolean | BeforeCallResult>;
|
|
635
|
+
/**
|
|
636
|
+
* Called after tool execution completes. Can transform the result.
|
|
637
|
+
*
|
|
638
|
+
* @param tool - The tool that was executed
|
|
639
|
+
* @param params - The parameters that were used
|
|
640
|
+
* @param result - The result from the tool
|
|
641
|
+
* @returns Void to use original result, or `AfterCallResult` to transform it
|
|
642
|
+
*/
|
|
643
|
+
onAfterCall?(tool: Tool, params: unknown, result: unknown): void | AfterCallResult | Promise<void | AfterCallResult>;
|
|
644
|
+
/**
|
|
645
|
+
* Called when a tool execution throws an error.
|
|
646
|
+
*
|
|
647
|
+
* @param tool - The tool that failed
|
|
648
|
+
* @param params - The parameters that were used
|
|
649
|
+
* @param error - The error that was thrown
|
|
650
|
+
*/
|
|
189
651
|
onError?(tool: Tool, params: unknown, error: Error): void | Promise<void>;
|
|
190
|
-
/**
|
|
652
|
+
/**
|
|
653
|
+
* Called when the maximum iteration limit is reached.
|
|
654
|
+
*
|
|
655
|
+
* @param iterations - The number of iterations that were performed
|
|
656
|
+
*/
|
|
191
657
|
onMaxIterations?(iterations: number): void | Promise<void>;
|
|
192
658
|
}
|
|
193
659
|
/**
|
|
194
|
-
* Record of a tool execution
|
|
660
|
+
* Record of a completed tool execution.
|
|
661
|
+
*
|
|
662
|
+
* Contains all information about a tool call that was executed,
|
|
663
|
+
* including timing and result data.
|
|
664
|
+
*
|
|
665
|
+
* @example
|
|
666
|
+
* ```typescript
|
|
667
|
+
* const execution: ToolExecution = {
|
|
668
|
+
* toolName: 'get_weather',
|
|
669
|
+
* toolCallId: 'call_abc123',
|
|
670
|
+
* arguments: { location: 'San Francisco' },
|
|
671
|
+
* result: { temperature: 72 },
|
|
672
|
+
* isError: false,
|
|
673
|
+
* duration: 150,
|
|
674
|
+
* approved: true
|
|
675
|
+
* };
|
|
676
|
+
* ```
|
|
195
677
|
*/
|
|
196
678
|
interface ToolExecution {
|
|
197
|
-
/**
|
|
679
|
+
/** Name of the tool that was called */
|
|
198
680
|
toolName: string;
|
|
199
|
-
/**
|
|
681
|
+
/** Unique identifier for this tool call */
|
|
200
682
|
toolCallId: string;
|
|
201
|
-
/** Arguments passed to the tool */
|
|
683
|
+
/** Arguments that were passed to the tool */
|
|
202
684
|
arguments: Record<string, unknown>;
|
|
203
685
|
/** Result returned by the tool */
|
|
204
686
|
result: unknown;
|
|
@@ -206,146 +688,336 @@ interface ToolExecution {
|
|
|
206
688
|
isError: boolean;
|
|
207
689
|
/** Execution duration in milliseconds */
|
|
208
690
|
duration: number;
|
|
209
|
-
/** Whether approval was required and granted */
|
|
691
|
+
/** Whether approval was required and granted (undefined if no approval handler) */
|
|
210
692
|
approved?: boolean;
|
|
211
693
|
}
|
|
212
694
|
|
|
213
695
|
/**
|
|
214
|
-
* Message
|
|
696
|
+
* @fileoverview Message types for conversation history.
|
|
697
|
+
*
|
|
698
|
+
* Defines the message classes used to represent conversation turns
|
|
699
|
+
* between users and assistants, including support for multimodal
|
|
700
|
+
* content and tool calls.
|
|
701
|
+
*
|
|
702
|
+
* @module types/messages
|
|
703
|
+
*/
|
|
704
|
+
|
|
705
|
+
/**
|
|
706
|
+
* Message type discriminator.
|
|
707
|
+
*
|
|
708
|
+
* Used to distinguish between different message types in a conversation.
|
|
215
709
|
*/
|
|
216
710
|
type MessageType = 'user' | 'assistant' | 'tool_result';
|
|
217
711
|
/**
|
|
218
|
-
* Provider-namespaced metadata
|
|
219
|
-
*
|
|
712
|
+
* Provider-namespaced metadata for messages.
|
|
713
|
+
*
|
|
714
|
+
* Each provider can attach its own metadata under its namespace,
|
|
715
|
+
* preventing conflicts between different providers.
|
|
716
|
+
*
|
|
717
|
+
* @example
|
|
718
|
+
* ```typescript
|
|
719
|
+
* const metadata: MessageMetadata = {
|
|
720
|
+
* openai: { model: 'gpt-4', finishReason: 'stop' },
|
|
721
|
+
* anthropic: { model: 'claude-3', stopReason: 'end_turn' }
|
|
722
|
+
* };
|
|
723
|
+
* ```
|
|
220
724
|
*/
|
|
221
725
|
interface MessageMetadata {
|
|
222
726
|
[provider: string]: Record<string, unknown> | undefined;
|
|
223
727
|
}
|
|
224
728
|
/**
|
|
225
|
-
* Options for
|
|
729
|
+
* Options for constructing messages.
|
|
226
730
|
*/
|
|
227
731
|
interface MessageOptions {
|
|
732
|
+
/** Custom message ID (auto-generated if not provided) */
|
|
228
733
|
id?: string;
|
|
734
|
+
/** Provider-specific metadata */
|
|
229
735
|
metadata?: MessageMetadata;
|
|
230
736
|
}
|
|
231
737
|
/**
|
|
232
|
-
*
|
|
233
|
-
*
|
|
738
|
+
* Abstract base class for all message types.
|
|
739
|
+
*
|
|
740
|
+
* Provides common functionality for user, assistant, and tool result
|
|
741
|
+
* messages, including content accessors and metadata handling.
|
|
742
|
+
*
|
|
743
|
+
* @example
|
|
744
|
+
* ```typescript
|
|
745
|
+
* // Access text content from any message
|
|
746
|
+
* const text = message.text;
|
|
747
|
+
*
|
|
748
|
+
* // Access images
|
|
749
|
+
* const images = message.images;
|
|
750
|
+
* ```
|
|
234
751
|
*/
|
|
235
752
|
declare abstract class Message {
|
|
236
753
|
/** Unique message identifier */
|
|
237
754
|
readonly id: string;
|
|
238
|
-
/** Timestamp */
|
|
755
|
+
/** Timestamp when the message was created */
|
|
239
756
|
readonly timestamp: Date;
|
|
240
|
-
/** Provider-specific metadata, namespaced by provider */
|
|
757
|
+
/** Provider-specific metadata, namespaced by provider name */
|
|
241
758
|
readonly metadata?: MessageMetadata;
|
|
242
|
-
/** Message type discriminator */
|
|
759
|
+
/** Message type discriminator (implemented by subclasses) */
|
|
243
760
|
abstract readonly type: MessageType;
|
|
244
|
-
/**
|
|
761
|
+
/**
|
|
762
|
+
* Returns the content blocks for this message.
|
|
763
|
+
* Implemented by subclasses to provide type-specific content.
|
|
764
|
+
*/
|
|
245
765
|
protected abstract getContent(): ContentBlock[];
|
|
766
|
+
/**
|
|
767
|
+
* Creates a new message instance.
|
|
768
|
+
*
|
|
769
|
+
* @param options - Optional message ID and metadata
|
|
770
|
+
*/
|
|
246
771
|
constructor(options?: MessageOptions);
|
|
247
772
|
/**
|
|
248
|
-
*
|
|
249
|
-
*
|
|
773
|
+
* Concatenated text content from all text blocks.
|
|
774
|
+
* Blocks are joined with double newlines.
|
|
250
775
|
*/
|
|
251
776
|
get text(): string;
|
|
252
777
|
/**
|
|
253
|
-
*
|
|
778
|
+
* All image content blocks in this message.
|
|
254
779
|
*/
|
|
255
780
|
get images(): ImageBlock[];
|
|
256
781
|
/**
|
|
257
|
-
*
|
|
782
|
+
* All audio content blocks in this message.
|
|
258
783
|
*/
|
|
259
784
|
get audio(): AudioBlock[];
|
|
260
785
|
/**
|
|
261
|
-
*
|
|
786
|
+
* All video content blocks in this message.
|
|
262
787
|
*/
|
|
263
788
|
get video(): VideoBlock[];
|
|
264
789
|
}
|
|
265
790
|
/**
|
|
266
|
-
* User input message
|
|
791
|
+
* User input message.
|
|
792
|
+
*
|
|
793
|
+
* Represents a message from the user, which can contain text and/or
|
|
794
|
+
* multimodal content like images, audio, or video.
|
|
795
|
+
*
|
|
796
|
+
* @example
|
|
797
|
+
* ```typescript
|
|
798
|
+
* // Simple text message
|
|
799
|
+
* const msg = new UserMessage('Hello, world!');
|
|
800
|
+
*
|
|
801
|
+
* // Multimodal message
|
|
802
|
+
* const msg = new UserMessage([
|
|
803
|
+
* { type: 'text', text: 'What is in this image?' },
|
|
804
|
+
* { type: 'image', source: { type: 'url', url: '...' }, mimeType: 'image/png' }
|
|
805
|
+
* ]);
|
|
806
|
+
* ```
|
|
267
807
|
*/
|
|
268
808
|
declare class UserMessage extends Message {
|
|
809
|
+
/** Message type discriminator */
|
|
269
810
|
readonly type: "user";
|
|
811
|
+
/** Content blocks in this message */
|
|
270
812
|
readonly content: UserContent[];
|
|
271
813
|
/**
|
|
814
|
+
* Creates a new user message.
|
|
815
|
+
*
|
|
272
816
|
* @param content - String (converted to TextBlock) or array of content blocks
|
|
817
|
+
* @param options - Optional message ID and metadata
|
|
273
818
|
*/
|
|
274
819
|
constructor(content: string | UserContent[], options?: MessageOptions);
|
|
275
820
|
protected getContent(): ContentBlock[];
|
|
276
821
|
}
|
|
277
822
|
/**
|
|
278
|
-
* Assistant response message
|
|
279
|
-
*
|
|
823
|
+
* Assistant response message.
|
|
824
|
+
*
|
|
825
|
+
* Represents a response from the AI assistant, which may contain
|
|
826
|
+
* text, media content, and/or tool call requests.
|
|
827
|
+
*
|
|
828
|
+
* @example
|
|
829
|
+
* ```typescript
|
|
830
|
+
* // Simple text response
|
|
831
|
+
* const msg = new AssistantMessage('Hello! How can I help?');
|
|
832
|
+
*
|
|
833
|
+
* // Response with tool calls
|
|
834
|
+
* const msg = new AssistantMessage(
|
|
835
|
+
* 'Let me check the weather...',
|
|
836
|
+
* [{ toolCallId: 'call_1', toolName: 'get_weather', arguments: { location: 'NYC' } }]
|
|
837
|
+
* );
|
|
838
|
+
* ```
|
|
280
839
|
*/
|
|
281
840
|
declare class AssistantMessage extends Message {
|
|
841
|
+
/** Message type discriminator */
|
|
282
842
|
readonly type: "assistant";
|
|
843
|
+
/** Content blocks in this message */
|
|
283
844
|
readonly content: AssistantContent[];
|
|
284
845
|
/** Tool calls requested by the model (if any) */
|
|
285
846
|
readonly toolCalls?: ToolCall[];
|
|
286
847
|
/**
|
|
848
|
+
* Creates a new assistant message.
|
|
849
|
+
*
|
|
287
850
|
* @param content - String (converted to TextBlock) or array of content blocks
|
|
288
851
|
* @param toolCalls - Tool calls requested by the model
|
|
289
|
-
* @param options -
|
|
852
|
+
* @param options - Optional message ID and metadata
|
|
290
853
|
*/
|
|
291
854
|
constructor(content: string | AssistantContent[], toolCalls?: ToolCall[], options?: MessageOptions);
|
|
292
855
|
protected getContent(): ContentBlock[];
|
|
293
|
-
/**
|
|
856
|
+
/**
|
|
857
|
+
* Whether this message contains tool call requests.
|
|
858
|
+
*/
|
|
294
859
|
get hasToolCalls(): boolean;
|
|
295
860
|
}
|
|
296
861
|
/**
|
|
297
|
-
*
|
|
862
|
+
* Tool execution result message.
|
|
863
|
+
*
|
|
864
|
+
* Contains the results of executing one or more tool calls,
|
|
865
|
+
* sent back to the model for further processing.
|
|
866
|
+
*
|
|
867
|
+
* @example
|
|
868
|
+
* ```typescript
|
|
869
|
+
* const msg = new ToolResultMessage([
|
|
870
|
+
* { toolCallId: 'call_1', result: { temperature: 72, conditions: 'sunny' } },
|
|
871
|
+
* { toolCallId: 'call_2', result: 'File not found', isError: true }
|
|
872
|
+
* ]);
|
|
873
|
+
* ```
|
|
298
874
|
*/
|
|
299
875
|
declare class ToolResultMessage extends Message {
|
|
876
|
+
/** Message type discriminator */
|
|
300
877
|
readonly type: "tool_result";
|
|
878
|
+
/** Results from tool executions */
|
|
301
879
|
readonly results: ToolResult[];
|
|
302
880
|
/**
|
|
881
|
+
* Creates a new tool result message.
|
|
882
|
+
*
|
|
303
883
|
* @param results - Array of tool execution results
|
|
304
|
-
* @param options -
|
|
884
|
+
* @param options - Optional message ID and metadata
|
|
305
885
|
*/
|
|
306
886
|
constructor(results: ToolResult[], options?: MessageOptions);
|
|
307
887
|
protected getContent(): ContentBlock[];
|
|
308
888
|
}
|
|
309
889
|
/**
|
|
310
|
-
* Type guard for UserMessage
|
|
890
|
+
* Type guard for UserMessage.
|
|
891
|
+
*
|
|
892
|
+
* @param msg - The message to check
|
|
893
|
+
* @returns True if the message is a UserMessage
|
|
894
|
+
*
|
|
895
|
+
* @example
|
|
896
|
+
* ```typescript
|
|
897
|
+
* if (isUserMessage(msg)) {
|
|
898
|
+
* console.log('User said:', msg.text);
|
|
899
|
+
* }
|
|
900
|
+
* ```
|
|
311
901
|
*/
|
|
312
902
|
declare function isUserMessage(msg: Message): msg is UserMessage;
|
|
313
903
|
/**
|
|
314
|
-
* Type guard for AssistantMessage
|
|
904
|
+
* Type guard for AssistantMessage.
|
|
905
|
+
*
|
|
906
|
+
* @param msg - The message to check
|
|
907
|
+
* @returns True if the message is an AssistantMessage
|
|
908
|
+
*
|
|
909
|
+
* @example
|
|
910
|
+
* ```typescript
|
|
911
|
+
* if (isAssistantMessage(msg)) {
|
|
912
|
+
* console.log('Assistant said:', msg.text);
|
|
913
|
+
* if (msg.hasToolCalls) {
|
|
914
|
+
* console.log('Tool calls:', msg.toolCalls);
|
|
915
|
+
* }
|
|
916
|
+
* }
|
|
917
|
+
* ```
|
|
315
918
|
*/
|
|
316
919
|
declare function isAssistantMessage(msg: Message): msg is AssistantMessage;
|
|
317
920
|
/**
|
|
318
|
-
* Type guard for ToolResultMessage
|
|
921
|
+
* Type guard for ToolResultMessage.
|
|
922
|
+
*
|
|
923
|
+
* @param msg - The message to check
|
|
924
|
+
* @returns True if the message is a ToolResultMessage
|
|
925
|
+
*
|
|
926
|
+
* @example
|
|
927
|
+
* ```typescript
|
|
928
|
+
* if (isToolResultMessage(msg)) {
|
|
929
|
+
* for (const result of msg.results) {
|
|
930
|
+
* console.log(`Tool ${result.toolCallId}:`, result.result);
|
|
931
|
+
* }
|
|
932
|
+
* }
|
|
933
|
+
* ```
|
|
319
934
|
*/
|
|
320
935
|
declare function isToolResultMessage(msg: Message): msg is ToolResultMessage;
|
|
321
936
|
|
|
322
937
|
/**
|
|
323
|
-
*
|
|
938
|
+
* @fileoverview Turn types for inference results.
|
|
939
|
+
*
|
|
940
|
+
* A Turn represents the complete result of one inference call, including
|
|
941
|
+
* all messages produced during tool execution loops, token usage, and
|
|
942
|
+
* optional structured output data.
|
|
943
|
+
*
|
|
944
|
+
* @module types/turn
|
|
945
|
+
*/
|
|
946
|
+
|
|
947
|
+
/**
|
|
948
|
+
* Token usage information for an inference request.
|
|
949
|
+
*
|
|
950
|
+
* Tracks input and output tokens across all inference cycles,
|
|
951
|
+
* with optional per-cycle breakdown and cache metrics.
|
|
952
|
+
*
|
|
953
|
+
* @example
|
|
954
|
+
* ```typescript
|
|
955
|
+
* const usage: TokenUsage = {
|
|
956
|
+
* inputTokens: 150,
|
|
957
|
+
* outputTokens: 50,
|
|
958
|
+
* totalTokens: 200,
|
|
959
|
+
* cacheReadTokens: 100,
|
|
960
|
+
* cacheWriteTokens: 50,
|
|
961
|
+
* cycles: [
|
|
962
|
+
* { inputTokens: 100, outputTokens: 30, cacheReadTokens: 0, cacheWriteTokens: 50 },
|
|
963
|
+
* { inputTokens: 50, outputTokens: 20, cacheReadTokens: 100, cacheWriteTokens: 0 }
|
|
964
|
+
* ]
|
|
965
|
+
* };
|
|
966
|
+
* ```
|
|
324
967
|
*/
|
|
325
968
|
interface TokenUsage {
|
|
326
|
-
/**
|
|
969
|
+
/** Total input tokens across all cycles */
|
|
327
970
|
inputTokens: number;
|
|
328
|
-
/**
|
|
971
|
+
/** Total output tokens across all cycles */
|
|
329
972
|
outputTokens: number;
|
|
330
|
-
/**
|
|
973
|
+
/** Sum of input and output tokens */
|
|
331
974
|
totalTokens: number;
|
|
332
|
-
/**
|
|
975
|
+
/**
|
|
976
|
+
* Tokens read from cache (cache hits).
|
|
977
|
+
* Returns 0 for providers that don't support or report cache metrics.
|
|
978
|
+
*/
|
|
979
|
+
cacheReadTokens: number;
|
|
980
|
+
/**
|
|
981
|
+
* Tokens written to cache (cache misses that were cached).
|
|
982
|
+
* Only Anthropic reports this metric; returns 0 for other providers.
|
|
983
|
+
*/
|
|
984
|
+
cacheWriteTokens: number;
|
|
985
|
+
/** Per-cycle token breakdown (if multiple cycles occurred) */
|
|
333
986
|
cycles?: Array<{
|
|
334
987
|
inputTokens: number;
|
|
335
988
|
outputTokens: number;
|
|
989
|
+
cacheReadTokens: number;
|
|
990
|
+
cacheWriteTokens: number;
|
|
336
991
|
}>;
|
|
337
992
|
}
|
|
338
993
|
/**
|
|
339
|
-
* A Turn represents the complete result of one inference call
|
|
340
|
-
*
|
|
994
|
+
* A Turn represents the complete result of one inference call.
|
|
995
|
+
*
|
|
996
|
+
* Includes all messages produced during tool execution loops,
|
|
997
|
+
* the final assistant response, token usage, and optional
|
|
998
|
+
* structured output data.
|
|
999
|
+
*
|
|
1000
|
+
* @typeParam TData - Type of the structured output data
|
|
1001
|
+
*
|
|
1002
|
+
* @example
|
|
1003
|
+
* ```typescript
|
|
1004
|
+
* const turn = await instance.generate('Hello');
|
|
1005
|
+
* console.log(turn.response.text);
|
|
1006
|
+
* console.log(`Used ${turn.usage.totalTokens} tokens in ${turn.cycles} cycles`);
|
|
1007
|
+
*
|
|
1008
|
+
* // With structured output
|
|
1009
|
+
* interface WeatherData { temperature: number; conditions: string; }
|
|
1010
|
+
* const turn = await instance.generate<WeatherData>('Get weather');
|
|
1011
|
+
* console.log(turn.data?.temperature);
|
|
1012
|
+
* ```
|
|
341
1013
|
*/
|
|
342
1014
|
interface Turn<TData = unknown> {
|
|
343
1015
|
/**
|
|
344
1016
|
* All messages produced during this inference, in chronological order.
|
|
345
|
-
*
|
|
1017
|
+
* Includes UserMessage, AssistantMessage (may include toolCalls), and ToolResultMessage.
|
|
346
1018
|
*/
|
|
347
1019
|
readonly messages: Message[];
|
|
348
|
-
/** The final assistant response (
|
|
1020
|
+
/** The final assistant response (last AssistantMessage in the turn) */
|
|
349
1021
|
readonly response: AssistantMessage;
|
|
350
1022
|
/** Tool executions that occurred during this turn */
|
|
351
1023
|
readonly toolExecutions: ToolExecution[];
|
|
@@ -354,120 +1026,320 @@ interface Turn<TData = unknown> {
|
|
|
354
1026
|
/** Total number of inference cycles (1 + number of tool rounds) */
|
|
355
1027
|
readonly cycles: number;
|
|
356
1028
|
/**
|
|
357
|
-
* Structured output data (if structure was provided).
|
|
1029
|
+
* Structured output data (if a structure schema was provided).
|
|
358
1030
|
* Type is inferred from the schema when using TypeScript.
|
|
359
1031
|
*/
|
|
360
1032
|
readonly data?: TData;
|
|
361
1033
|
}
|
|
362
1034
|
/**
|
|
363
|
-
*
|
|
1035
|
+
* Creates a Turn from accumulated inference data.
|
|
1036
|
+
*
|
|
1037
|
+
* @typeParam TData - Type of the structured output data
|
|
1038
|
+
* @param messages - All messages produced during the inference
|
|
1039
|
+
* @param toolExecutions - Record of all tool executions
|
|
1040
|
+
* @param usage - Aggregate token usage
|
|
1041
|
+
* @param cycles - Number of inference cycles
|
|
1042
|
+
* @param data - Optional structured output data
|
|
1043
|
+
* @returns A complete Turn object
|
|
1044
|
+
* @throws Error if no assistant message is found in the messages
|
|
1045
|
+
*
|
|
1046
|
+
* @example
|
|
1047
|
+
* ```typescript
|
|
1048
|
+
* const turn = createTurn(
|
|
1049
|
+
* [userMsg, assistantMsg],
|
|
1050
|
+
* [],
|
|
1051
|
+
* { inputTokens: 100, outputTokens: 50, totalTokens: 150 },
|
|
1052
|
+
* 1
|
|
1053
|
+
* );
|
|
1054
|
+
* ```
|
|
364
1055
|
*/
|
|
365
1056
|
declare function createTurn<TData = unknown>(messages: Message[], toolExecutions: ToolExecution[], usage: TokenUsage, cycles: number, data?: TData): Turn<TData>;
|
|
366
1057
|
/**
|
|
367
|
-
*
|
|
1058
|
+
* Creates an empty TokenUsage object.
|
|
1059
|
+
*
|
|
1060
|
+
* @returns A TokenUsage with all values set to zero
|
|
1061
|
+
*
|
|
1062
|
+
* @example
|
|
1063
|
+
* ```typescript
|
|
1064
|
+
* const usage = emptyUsage();
|
|
1065
|
+
* // { inputTokens: 0, outputTokens: 0, totalTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0, cycles: [] }
|
|
1066
|
+
* ```
|
|
368
1067
|
*/
|
|
369
1068
|
declare function emptyUsage(): TokenUsage;
|
|
370
1069
|
/**
|
|
371
|
-
*
|
|
1070
|
+
* Aggregates token usage from multiple inference cycles.
|
|
1071
|
+
*
|
|
1072
|
+
* @param usages - Array of TokenUsage objects to aggregate
|
|
1073
|
+
* @returns Combined TokenUsage with per-cycle breakdown
|
|
1074
|
+
*
|
|
1075
|
+
* @example
|
|
1076
|
+
* ```typescript
|
|
1077
|
+
* const cycle1 = { inputTokens: 100, outputTokens: 30, totalTokens: 130, cacheReadTokens: 50, cacheWriteTokens: 0 };
|
|
1078
|
+
* const cycle2 = { inputTokens: 150, outputTokens: 40, totalTokens: 190, cacheReadTokens: 100, cacheWriteTokens: 0 };
|
|
1079
|
+
* const total = aggregateUsage([cycle1, cycle2]);
|
|
1080
|
+
* // { inputTokens: 250, outputTokens: 70, totalTokens: 320, cacheReadTokens: 150, cacheWriteTokens: 0, cycles: [...] }
|
|
1081
|
+
* ```
|
|
372
1082
|
*/
|
|
373
1083
|
declare function aggregateUsage(usages: TokenUsage[]): TokenUsage;
|
|
374
1084
|
|
|
375
1085
|
/**
|
|
376
|
-
*
|
|
1086
|
+
* @fileoverview Streaming types for real-time LLM responses.
|
|
1087
|
+
*
|
|
1088
|
+
* Defines the event types and interfaces for streaming LLM inference,
|
|
1089
|
+
* including text deltas, tool call deltas, and control events.
|
|
1090
|
+
*
|
|
1091
|
+
* @module types/stream
|
|
377
1092
|
*/
|
|
378
|
-
|
|
1093
|
+
|
|
379
1094
|
/**
|
|
380
|
-
*
|
|
1095
|
+
* Stream event type discriminators.
|
|
1096
|
+
*
|
|
1097
|
+
* Each event type represents a different kind of streaming update
|
|
1098
|
+
* from the LLM provider.
|
|
1099
|
+
*/
|
|
1100
|
+
type StreamEventType =
|
|
1101
|
+
/** Incremental text output */
|
|
1102
|
+
'text_delta'
|
|
1103
|
+
/** Incremental reasoning/thinking output */
|
|
1104
|
+
| 'reasoning_delta'
|
|
1105
|
+
/** Incremental image data */
|
|
1106
|
+
| 'image_delta'
|
|
1107
|
+
/** Incremental audio data */
|
|
1108
|
+
| 'audio_delta'
|
|
1109
|
+
/** Incremental video data */
|
|
1110
|
+
| 'video_delta'
|
|
1111
|
+
/** Incremental tool call data (arguments being streamed) */
|
|
1112
|
+
| 'tool_call_delta'
|
|
1113
|
+
/** Tool execution has started */
|
|
1114
|
+
| 'tool_execution_start'
|
|
1115
|
+
/** Tool execution has completed */
|
|
1116
|
+
| 'tool_execution_end'
|
|
1117
|
+
/** Beginning of a message */
|
|
1118
|
+
| 'message_start'
|
|
1119
|
+
/** End of a message */
|
|
1120
|
+
| 'message_stop'
|
|
1121
|
+
/** Beginning of a content block */
|
|
1122
|
+
| 'content_block_start'
|
|
1123
|
+
/** End of a content block */
|
|
1124
|
+
| 'content_block_stop';
|
|
1125
|
+
/**
|
|
1126
|
+
* Event delta data payload.
|
|
1127
|
+
*
|
|
1128
|
+
* Contains the type-specific data for a streaming event.
|
|
1129
|
+
* Different fields are populated depending on the event type.
|
|
381
1130
|
*/
|
|
382
1131
|
interface EventDelta {
|
|
1132
|
+
/** Incremental text content (for text_delta, reasoning_delta) */
|
|
383
1133
|
text?: string;
|
|
1134
|
+
/** Incremental binary data (for image_delta, audio_delta, video_delta) */
|
|
384
1135
|
data?: Uint8Array;
|
|
1136
|
+
/** Tool call identifier (for tool_call_delta, tool_execution_start/end) */
|
|
385
1137
|
toolCallId?: string;
|
|
1138
|
+
/** Tool name (for tool_call_delta, tool_execution_start/end) */
|
|
386
1139
|
toolName?: string;
|
|
1140
|
+
/** Incremental JSON arguments string (for tool_call_delta) */
|
|
387
1141
|
argumentsJson?: string;
|
|
388
1142
|
/** Tool execution result (for tool_execution_end) */
|
|
389
1143
|
result?: unknown;
|
|
390
|
-
/** Whether tool execution
|
|
1144
|
+
/** Whether tool execution resulted in an error (for tool_execution_end) */
|
|
391
1145
|
isError?: boolean;
|
|
392
|
-
/** Timestamp in
|
|
1146
|
+
/** Timestamp in milliseconds (for tool_execution_start/end) */
|
|
393
1147
|
timestamp?: number;
|
|
394
1148
|
}
|
|
395
1149
|
/**
|
|
396
|
-
* A streaming event
|
|
1150
|
+
* A single streaming event from the LLM.
|
|
1151
|
+
*
|
|
1152
|
+
* Events are emitted in order as the model generates output,
|
|
1153
|
+
* allowing for real-time display of responses.
|
|
1154
|
+
*
|
|
1155
|
+
* @example
|
|
1156
|
+
* ```typescript
|
|
1157
|
+
* for await (const event of stream) {
|
|
1158
|
+
* if (event.type === 'text_delta') {
|
|
1159
|
+
* process.stdout.write(event.delta.text ?? '');
|
|
1160
|
+
* } else if (event.type === 'tool_call_delta') {
|
|
1161
|
+
* console.log('Tool:', event.delta.toolName);
|
|
1162
|
+
* }
|
|
1163
|
+
* }
|
|
1164
|
+
* ```
|
|
397
1165
|
*/
|
|
398
1166
|
interface StreamEvent {
|
|
399
|
-
/** Event type */
|
|
1167
|
+
/** Event type discriminator */
|
|
400
1168
|
type: StreamEventType;
|
|
401
1169
|
/** Index of the content block this event belongs to */
|
|
402
1170
|
index: number;
|
|
403
|
-
/** Event data
|
|
1171
|
+
/** Event-specific data payload */
|
|
404
1172
|
delta: EventDelta;
|
|
405
1173
|
}
|
|
406
1174
|
/**
|
|
407
|
-
* Stream result - async iterable that also provides final turn
|
|
1175
|
+
* Stream result - an async iterable that also provides the final turn.
|
|
1176
|
+
*
|
|
1177
|
+
* Allows consuming streaming events while also awaiting the complete
|
|
1178
|
+
* Turn result after streaming finishes.
|
|
1179
|
+
*
|
|
1180
|
+
* @typeParam TData - Type of the structured output data
|
|
1181
|
+
*
|
|
1182
|
+
* @example
|
|
1183
|
+
* ```typescript
|
|
1184
|
+
* const stream = instance.stream('Tell me a story');
|
|
1185
|
+
*
|
|
1186
|
+
* // Consume streaming events
|
|
1187
|
+
* for await (const event of stream) {
|
|
1188
|
+
* if (event.type === 'text_delta') {
|
|
1189
|
+
* process.stdout.write(event.delta.text ?? '');
|
|
1190
|
+
* }
|
|
1191
|
+
* }
|
|
1192
|
+
*
|
|
1193
|
+
* // Get the complete turn after streaming
|
|
1194
|
+
* const turn = await stream.turn;
|
|
1195
|
+
* console.log('\n\nTokens used:', turn.usage.totalTokens);
|
|
1196
|
+
* ```
|
|
408
1197
|
*/
|
|
409
1198
|
interface StreamResult<TData = unknown> extends AsyncIterable<StreamEvent> {
|
|
410
1199
|
/**
|
|
411
|
-
*
|
|
412
|
-
* Resolves when the stream completes.
|
|
1200
|
+
* Promise that resolves to the complete Turn after streaming finishes.
|
|
413
1201
|
*/
|
|
414
1202
|
readonly turn: Promise<Turn<TData>>;
|
|
415
|
-
/**
|
|
1203
|
+
/**
|
|
1204
|
+
* Aborts the stream, stopping further events and cancelling the request.
|
|
1205
|
+
*/
|
|
416
1206
|
abort(): void;
|
|
417
1207
|
}
|
|
418
1208
|
/**
|
|
419
|
-
*
|
|
1209
|
+
* Creates a StreamResult from an async generator and completion promise.
|
|
1210
|
+
*
|
|
1211
|
+
* @typeParam TData - Type of the structured output data
|
|
1212
|
+
* @param generator - Async generator that yields stream events
|
|
1213
|
+
* @param turnPromise - Promise that resolves to the complete Turn
|
|
1214
|
+
* @param abortController - Controller for aborting the stream
|
|
1215
|
+
* @returns A StreamResult that can be iterated and awaited
|
|
1216
|
+
*
|
|
1217
|
+
* @example
|
|
1218
|
+
* ```typescript
|
|
1219
|
+
* const abortController = new AbortController();
|
|
1220
|
+
* const stream = createStreamResult(
|
|
1221
|
+
* eventGenerator(),
|
|
1222
|
+
* turnPromise,
|
|
1223
|
+
* abortController
|
|
1224
|
+
* );
|
|
1225
|
+
* ```
|
|
420
1226
|
*/
|
|
421
1227
|
declare function createStreamResult<TData = unknown>(generator: AsyncGenerator<StreamEvent, void, unknown>, turnPromise: Promise<Turn<TData>>, abortController: AbortController): StreamResult<TData>;
|
|
422
1228
|
/**
|
|
423
|
-
*
|
|
1229
|
+
* Creates a text delta stream event.
|
|
1230
|
+
*
|
|
1231
|
+
* @param text - The incremental text content
|
|
1232
|
+
* @param index - Content block index (default: 0)
|
|
1233
|
+
* @returns A text_delta StreamEvent
|
|
424
1234
|
*/
|
|
425
1235
|
declare function textDelta(text: string, index?: number): StreamEvent;
|
|
426
1236
|
/**
|
|
427
|
-
*
|
|
1237
|
+
* Creates a tool call delta stream event.
|
|
1238
|
+
*
|
|
1239
|
+
* @param toolCallId - Unique identifier for the tool call
|
|
1240
|
+
* @param toolName - Name of the tool being called
|
|
1241
|
+
* @param argumentsJson - Incremental JSON arguments string
|
|
1242
|
+
* @param index - Content block index (default: 0)
|
|
1243
|
+
* @returns A tool_call_delta StreamEvent
|
|
428
1244
|
*/
|
|
429
1245
|
declare function toolCallDelta(toolCallId: string, toolName: string, argumentsJson: string, index?: number): StreamEvent;
|
|
430
1246
|
/**
|
|
431
|
-
*
|
|
1247
|
+
* Creates a message start stream event.
|
|
1248
|
+
*
|
|
1249
|
+
* @returns A message_start StreamEvent
|
|
432
1250
|
*/
|
|
433
1251
|
declare function messageStart(): StreamEvent;
|
|
434
1252
|
/**
|
|
435
|
-
*
|
|
1253
|
+
* Creates a message stop stream event.
|
|
1254
|
+
*
|
|
1255
|
+
* @returns A message_stop StreamEvent
|
|
436
1256
|
*/
|
|
437
1257
|
declare function messageStop(): StreamEvent;
|
|
438
1258
|
/**
|
|
439
|
-
*
|
|
1259
|
+
* Creates a content block start stream event.
|
|
1260
|
+
*
|
|
1261
|
+
* @param index - The content block index starting
|
|
1262
|
+
* @returns A content_block_start StreamEvent
|
|
440
1263
|
*/
|
|
441
1264
|
declare function contentBlockStart(index: number): StreamEvent;
|
|
442
1265
|
/**
|
|
443
|
-
*
|
|
1266
|
+
* Creates a content block stop stream event.
|
|
1267
|
+
*
|
|
1268
|
+
* @param index - The content block index stopping
|
|
1269
|
+
* @returns A content_block_stop StreamEvent
|
|
444
1270
|
*/
|
|
445
1271
|
declare function contentBlockStop(index: number): StreamEvent;
|
|
446
1272
|
|
|
447
1273
|
/**
|
|
448
|
-
*
|
|
1274
|
+
* @fileoverview Thread class for managing conversation history.
|
|
1275
|
+
*
|
|
1276
|
+
* Provides a utility class for building and manipulating conversation
|
|
1277
|
+
* message sequences, with support for serialization and deserialization.
|
|
1278
|
+
*
|
|
1279
|
+
* @module types/thread
|
|
1280
|
+
*/
|
|
1281
|
+
|
|
1282
|
+
/**
|
|
1283
|
+
* Serialized message format for JSON storage.
|
|
1284
|
+
*
|
|
1285
|
+
* Used when persisting messages to storage or transmitting over the network.
|
|
449
1286
|
*/
|
|
450
1287
|
interface MessageJSON {
|
|
1288
|
+
/** Unique message identifier */
|
|
451
1289
|
id: string;
|
|
1290
|
+
/** Message type discriminator */
|
|
452
1291
|
type: MessageType;
|
|
1292
|
+
/** Content blocks in the message */
|
|
453
1293
|
content: ContentBlock[];
|
|
1294
|
+
/** Tool calls (for assistant messages) */
|
|
454
1295
|
toolCalls?: ToolCall[];
|
|
1296
|
+
/** Tool results (for tool result messages) */
|
|
455
1297
|
results?: ToolResult[];
|
|
1298
|
+
/** Provider-specific metadata */
|
|
456
1299
|
metadata?: MessageMetadata;
|
|
1300
|
+
/** ISO timestamp string */
|
|
457
1301
|
timestamp: string;
|
|
458
1302
|
}
|
|
459
1303
|
/**
|
|
460
|
-
* Serialized thread format
|
|
1304
|
+
* Serialized thread format for JSON storage.
|
|
1305
|
+
*
|
|
1306
|
+
* Contains all data needed to reconstruct a Thread instance.
|
|
461
1307
|
*/
|
|
462
1308
|
interface ThreadJSON {
|
|
1309
|
+
/** Unique thread identifier */
|
|
463
1310
|
id: string;
|
|
1311
|
+
/** Serialized messages */
|
|
464
1312
|
messages: MessageJSON[];
|
|
1313
|
+
/** ISO timestamp of thread creation */
|
|
465
1314
|
createdAt: string;
|
|
1315
|
+
/** ISO timestamp of last update */
|
|
466
1316
|
updatedAt: string;
|
|
467
1317
|
}
|
|
468
1318
|
/**
|
|
469
|
-
* Thread - A utility class for managing conversation history
|
|
470
|
-
*
|
|
1319
|
+
* Thread - A utility class for managing conversation history.
|
|
1320
|
+
*
|
|
1321
|
+
* Provides methods for building, manipulating, and persisting
|
|
1322
|
+
* conversation message sequences. This class is optional; users
|
|
1323
|
+
* can also manage their own `Message[]` arrays directly.
|
|
1324
|
+
*
|
|
1325
|
+
* @example
|
|
1326
|
+
* ```typescript
|
|
1327
|
+
* // Create a new thread and add messages
|
|
1328
|
+
* const thread = new Thread();
|
|
1329
|
+
* thread.user('Hello!');
|
|
1330
|
+
* thread.assistant('Hi there! How can I help?');
|
|
1331
|
+
*
|
|
1332
|
+
* // Use with LLM inference
|
|
1333
|
+
* const turn = await instance.generate(thread, 'What is 2+2?');
|
|
1334
|
+
* thread.append(turn);
|
|
1335
|
+
*
|
|
1336
|
+
* // Serialize for storage
|
|
1337
|
+
* const json = thread.toJSON();
|
|
1338
|
+
* localStorage.setItem('chat', JSON.stringify(json));
|
|
1339
|
+
*
|
|
1340
|
+
* // Restore from storage
|
|
1341
|
+
* const restored = Thread.fromJSON(JSON.parse(localStorage.getItem('chat')));
|
|
1342
|
+
* ```
|
|
471
1343
|
*/
|
|
472
1344
|
declare class Thread {
|
|
473
1345
|
/** Unique thread identifier */
|
|
@@ -479,78 +1351,188 @@ declare class Thread {
|
|
|
479
1351
|
/** Last update timestamp */
|
|
480
1352
|
private _updatedAt;
|
|
481
1353
|
/**
|
|
482
|
-
*
|
|
1354
|
+
* Creates a new thread instance.
|
|
1355
|
+
*
|
|
1356
|
+
* @param messages - Optional initial messages to populate the thread
|
|
483
1357
|
*/
|
|
484
1358
|
constructor(messages?: Message[]);
|
|
485
|
-
/**
|
|
1359
|
+
/**
|
|
1360
|
+
* All messages in the thread (readonly).
|
|
1361
|
+
*/
|
|
486
1362
|
get messages(): readonly Message[];
|
|
487
|
-
/**
|
|
1363
|
+
/**
|
|
1364
|
+
* Number of messages in the thread.
|
|
1365
|
+
*/
|
|
488
1366
|
get length(): number;
|
|
489
1367
|
/**
|
|
490
|
-
*
|
|
1368
|
+
* Appends all messages from a Turn to the thread.
|
|
1369
|
+
*
|
|
1370
|
+
* @param turn - The Turn containing messages to append
|
|
1371
|
+
* @returns This thread instance for chaining
|
|
491
1372
|
*/
|
|
492
1373
|
append(turn: Turn): this;
|
|
493
1374
|
/**
|
|
494
|
-
*
|
|
1375
|
+
* Adds raw messages to the thread.
|
|
1376
|
+
*
|
|
1377
|
+
* @param messages - Messages to add
|
|
1378
|
+
* @returns This thread instance for chaining
|
|
495
1379
|
*/
|
|
496
1380
|
push(...messages: Message[]): this;
|
|
497
1381
|
/**
|
|
498
|
-
*
|
|
1382
|
+
* Adds a user message to the thread.
|
|
1383
|
+
*
|
|
1384
|
+
* @param content - String or array of content blocks
|
|
1385
|
+
* @returns This thread instance for chaining
|
|
1386
|
+
*
|
|
1387
|
+
* @example
|
|
1388
|
+
* ```typescript
|
|
1389
|
+
* thread.user('Hello, world!');
|
|
1390
|
+
* thread.user([
|
|
1391
|
+
* { type: 'text', text: 'Describe this image:' },
|
|
1392
|
+
* { type: 'image', source: { type: 'url', url: '...' }, mimeType: 'image/png' }
|
|
1393
|
+
* ]);
|
|
1394
|
+
* ```
|
|
499
1395
|
*/
|
|
500
1396
|
user(content: string | UserContent[]): this;
|
|
501
1397
|
/**
|
|
502
|
-
*
|
|
1398
|
+
* Adds an assistant message to the thread.
|
|
1399
|
+
*
|
|
1400
|
+
* @param content - String or array of content blocks
|
|
1401
|
+
* @returns This thread instance for chaining
|
|
1402
|
+
*
|
|
1403
|
+
* @example
|
|
1404
|
+
* ```typescript
|
|
1405
|
+
* thread.assistant('I can help with that!');
|
|
1406
|
+
* ```
|
|
503
1407
|
*/
|
|
504
1408
|
assistant(content: string | AssistantContent[]): this;
|
|
505
1409
|
/**
|
|
506
|
-
*
|
|
1410
|
+
* Filters messages by type.
|
|
1411
|
+
*
|
|
1412
|
+
* @param type - The message type to filter by
|
|
1413
|
+
* @returns Array of messages matching the type
|
|
1414
|
+
*
|
|
1415
|
+
* @example
|
|
1416
|
+
* ```typescript
|
|
1417
|
+
* const userMessages = thread.filter('user');
|
|
1418
|
+
* const assistantMessages = thread.filter('assistant');
|
|
1419
|
+
* ```
|
|
507
1420
|
*/
|
|
508
1421
|
filter(type: MessageType): Message[];
|
|
509
1422
|
/**
|
|
510
|
-
*
|
|
1423
|
+
* Returns the last N messages from the thread.
|
|
1424
|
+
*
|
|
1425
|
+
* @param count - Number of messages to return
|
|
1426
|
+
* @returns Array of the last N messages
|
|
1427
|
+
*
|
|
1428
|
+
* @example
|
|
1429
|
+
* ```typescript
|
|
1430
|
+
* const recent = thread.tail(5);
|
|
1431
|
+
* ```
|
|
511
1432
|
*/
|
|
512
1433
|
tail(count: number): Message[];
|
|
513
1434
|
/**
|
|
514
|
-
*
|
|
1435
|
+
* Creates a new thread with a subset of messages.
|
|
1436
|
+
*
|
|
1437
|
+
* @param start - Start index (inclusive)
|
|
1438
|
+
* @param end - End index (exclusive)
|
|
1439
|
+
* @returns New Thread containing the sliced messages
|
|
1440
|
+
*
|
|
1441
|
+
* @example
|
|
1442
|
+
* ```typescript
|
|
1443
|
+
* const subset = thread.slice(0, 10);
|
|
1444
|
+
* ```
|
|
515
1445
|
*/
|
|
516
1446
|
slice(start?: number, end?: number): Thread;
|
|
517
1447
|
/**
|
|
518
|
-
*
|
|
1448
|
+
* Removes all messages from the thread.
|
|
1449
|
+
*
|
|
1450
|
+
* @returns This thread instance for chaining
|
|
519
1451
|
*/
|
|
520
1452
|
clear(): this;
|
|
521
1453
|
/**
|
|
522
|
-
*
|
|
1454
|
+
* Converts the thread to a plain message array.
|
|
1455
|
+
*
|
|
1456
|
+
* @returns Copy of the internal message array
|
|
523
1457
|
*/
|
|
524
1458
|
toMessages(): Message[];
|
|
525
1459
|
/**
|
|
526
|
-
*
|
|
1460
|
+
* Serializes the thread to JSON format.
|
|
1461
|
+
*
|
|
1462
|
+
* @returns JSON-serializable representation of the thread
|
|
1463
|
+
*
|
|
1464
|
+
* @example
|
|
1465
|
+
* ```typescript
|
|
1466
|
+
* const json = thread.toJSON();
|
|
1467
|
+
* localStorage.setItem('thread', JSON.stringify(json));
|
|
1468
|
+
* ```
|
|
527
1469
|
*/
|
|
528
1470
|
toJSON(): ThreadJSON;
|
|
529
1471
|
/**
|
|
530
|
-
*
|
|
1472
|
+
* Deserializes a thread from JSON format.
|
|
1473
|
+
*
|
|
1474
|
+
* @param json - The JSON representation to deserialize
|
|
1475
|
+
* @returns Reconstructed Thread instance
|
|
1476
|
+
*
|
|
1477
|
+
* @example
|
|
1478
|
+
* ```typescript
|
|
1479
|
+
* const json = JSON.parse(localStorage.getItem('thread'));
|
|
1480
|
+
* const thread = Thread.fromJSON(json);
|
|
1481
|
+
* ```
|
|
531
1482
|
*/
|
|
532
1483
|
static fromJSON(json: ThreadJSON): Thread;
|
|
533
1484
|
/**
|
|
534
|
-
*
|
|
1485
|
+
* Enables iteration over messages with for...of loops.
|
|
1486
|
+
*
|
|
1487
|
+
* @returns Iterator over the thread's messages
|
|
1488
|
+
*
|
|
1489
|
+
* @example
|
|
1490
|
+
* ```typescript
|
|
1491
|
+
* for (const message of thread) {
|
|
1492
|
+
* console.log(message.text);
|
|
1493
|
+
* }
|
|
1494
|
+
* ```
|
|
535
1495
|
*/
|
|
536
1496
|
[Symbol.iterator](): Iterator<Message>;
|
|
537
1497
|
/**
|
|
538
|
-
*
|
|
1498
|
+
* Converts a message to JSON format.
|
|
539
1499
|
*/
|
|
540
1500
|
private messageToJSON;
|
|
541
1501
|
/**
|
|
542
|
-
*
|
|
1502
|
+
* Reconstructs a message from JSON format.
|
|
543
1503
|
*/
|
|
544
1504
|
private static messageFromJSON;
|
|
545
1505
|
}
|
|
546
1506
|
|
|
547
1507
|
/**
|
|
548
|
-
*
|
|
1508
|
+
* @fileoverview LLM types for language model inference.
|
|
1509
|
+
*
|
|
1510
|
+
* Defines the interfaces for configuring and executing LLM inference,
|
|
1511
|
+
* including options, instances, requests, responses, and capabilities.
|
|
1512
|
+
*
|
|
1513
|
+
* @module types/llm
|
|
1514
|
+
*/
|
|
1515
|
+
|
|
1516
|
+
/**
|
|
1517
|
+
* LLM capabilities declare what a provider's API supports.
|
|
1518
|
+
*
|
|
1519
|
+
* These are API-level capabilities, not individual model capabilities.
|
|
549
1520
|
* If a user attempts to use a feature with a model that doesn't support it,
|
|
550
|
-
* the provider's API will return an error
|
|
1521
|
+
* the provider's API will return an error.
|
|
551
1522
|
*
|
|
552
|
-
* Capabilities are static
|
|
553
|
-
*
|
|
1523
|
+
* Capabilities are static and do not vary per-request or per-model.
|
|
1524
|
+
*
|
|
1525
|
+
* @example
|
|
1526
|
+
* ```typescript
|
|
1527
|
+
* const capabilities: LLMCapabilities = {
|
|
1528
|
+
* streaming: true,
|
|
1529
|
+
* tools: true,
|
|
1530
|
+
* structuredOutput: true,
|
|
1531
|
+
* imageInput: true,
|
|
1532
|
+
* videoInput: false,
|
|
1533
|
+
* audioInput: false
|
|
1534
|
+
* };
|
|
1535
|
+
* ```
|
|
554
1536
|
*/
|
|
555
1537
|
interface LLMCapabilities {
|
|
556
1538
|
/** Provider API supports streaming responses */
|
|
@@ -559,19 +1541,37 @@ interface LLMCapabilities {
|
|
|
559
1541
|
tools: boolean;
|
|
560
1542
|
/** Provider API supports native structured output (JSON schema) */
|
|
561
1543
|
structuredOutput: boolean;
|
|
562
|
-
/** Provider API supports image input */
|
|
1544
|
+
/** Provider API supports image input in messages */
|
|
563
1545
|
imageInput: boolean;
|
|
564
|
-
/** Provider API supports video input */
|
|
1546
|
+
/** Provider API supports video input in messages */
|
|
565
1547
|
videoInput: boolean;
|
|
566
|
-
/** Provider API supports audio input */
|
|
1548
|
+
/** Provider API supports audio input in messages */
|
|
567
1549
|
audioInput: boolean;
|
|
568
1550
|
}
|
|
569
1551
|
/**
|
|
570
|
-
*
|
|
1552
|
+
* Valid input types for inference.
|
|
1553
|
+
*
|
|
1554
|
+
* Inference input can be a simple string, a Message object, or
|
|
1555
|
+
* a raw ContentBlock for multimodal input.
|
|
571
1556
|
*/
|
|
572
1557
|
type InferenceInput = string | Message | ContentBlock;
|
|
573
1558
|
/**
|
|
574
|
-
* Options for llm() function
|
|
1559
|
+
* Options for creating an LLM instance with the llm() function.
|
|
1560
|
+
*
|
|
1561
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
1562
|
+
*
|
|
1563
|
+
* @example
|
|
1564
|
+
* ```typescript
|
|
1565
|
+
* const options: LLMOptions = {
|
|
1566
|
+
* model: openai('gpt-4'),
|
|
1567
|
+
* system: 'You are a helpful assistant.',
|
|
1568
|
+
* params: { temperature: 0.7, max_tokens: 1000 },
|
|
1569
|
+
* tools: [weatherTool, searchTool],
|
|
1570
|
+
* toolStrategy: { maxIterations: 5 }
|
|
1571
|
+
* };
|
|
1572
|
+
*
|
|
1573
|
+
* const instance = llm(options);
|
|
1574
|
+
* ```
|
|
575
1575
|
*/
|
|
576
1576
|
interface LLMOptions<TParams = unknown> {
|
|
577
1577
|
/** A model reference from a provider factory */
|
|
@@ -580,8 +1580,16 @@ interface LLMOptions<TParams = unknown> {
|
|
|
580
1580
|
config?: ProviderConfig;
|
|
581
1581
|
/** Model-specific parameters (temperature, max_tokens, etc.) */
|
|
582
1582
|
params?: TParams;
|
|
583
|
-
/**
|
|
584
|
-
|
|
1583
|
+
/**
|
|
1584
|
+
* System prompt for all inferences.
|
|
1585
|
+
*
|
|
1586
|
+
* Can be a simple string or a provider-specific array format:
|
|
1587
|
+
* - Anthropic: `[{type: 'text', text: '...', cache_control?: {...}}]`
|
|
1588
|
+
* - Google: `[{text: '...'}, {text: '...'}]` (parts array)
|
|
1589
|
+
*
|
|
1590
|
+
* Array formats are passed through directly to the provider.
|
|
1591
|
+
*/
|
|
1592
|
+
system?: string | unknown[];
|
|
585
1593
|
/** Tools available to the model */
|
|
586
1594
|
tools?: Tool[];
|
|
587
1595
|
/** Tool execution strategy */
|
|
@@ -590,54 +1598,84 @@ interface LLMOptions<TParams = unknown> {
|
|
|
590
1598
|
structure?: JSONSchema;
|
|
591
1599
|
}
|
|
592
1600
|
/**
|
|
593
|
-
* LLM instance returned by llm()
|
|
1601
|
+
* LLM instance returned by the llm() function.
|
|
1602
|
+
*
|
|
1603
|
+
* Provides methods for generating responses and streaming output,
|
|
1604
|
+
* with access to the bound model and capabilities.
|
|
1605
|
+
*
|
|
1606
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
1607
|
+
*
|
|
1608
|
+
* @example
|
|
1609
|
+
* ```typescript
|
|
1610
|
+
* const instance = llm({ model: openai('gpt-4') });
|
|
1611
|
+
*
|
|
1612
|
+
* // Simple generation
|
|
1613
|
+
* const turn = await instance.generate('Hello!');
|
|
1614
|
+
* console.log(turn.response.text);
|
|
1615
|
+
*
|
|
1616
|
+
* // Streaming
|
|
1617
|
+
* const stream = instance.stream('Tell me a story');
|
|
1618
|
+
* for await (const event of stream) {
|
|
1619
|
+
* if (event.type === 'text_delta') {
|
|
1620
|
+
* process.stdout.write(event.delta.text ?? '');
|
|
1621
|
+
* }
|
|
1622
|
+
* }
|
|
1623
|
+
* const finalTurn = await stream.turn;
|
|
1624
|
+
* ```
|
|
594
1625
|
*/
|
|
595
1626
|
interface LLMInstance<TParams = unknown> {
|
|
596
1627
|
/**
|
|
597
|
-
*
|
|
1628
|
+
* Executes inference and returns the complete Turn.
|
|
598
1629
|
*
|
|
599
|
-
*
|
|
600
|
-
*
|
|
1630
|
+
* Supports multiple calling patterns:
|
|
1631
|
+
* - Single input: `generate('Hello')`
|
|
1632
|
+
* - Multiple inputs: `generate('Context...', 'Question?')`
|
|
1633
|
+
* - With history: `generate(messages, 'Follow-up?')`
|
|
1634
|
+
* - With thread: `generate(thread, 'Next message')`
|
|
601
1635
|
*
|
|
602
|
-
* @
|
|
603
|
-
*
|
|
604
|
-
*
|
|
605
|
-
* @overload With history
|
|
606
|
-
* generate(history: Message[] | Thread, ...inputs: InferenceInput[]): Promise<Turn>
|
|
1636
|
+
* @param historyOrInput - History (Message[] or Thread) or first input
|
|
1637
|
+
* @param input - Additional inputs to include in the request
|
|
1638
|
+
* @returns Promise resolving to the complete Turn
|
|
607
1639
|
*/
|
|
608
1640
|
generate(historyOrInput: Message[] | Thread | InferenceInput, ...input: InferenceInput[]): Promise<Turn>;
|
|
609
1641
|
/**
|
|
610
|
-
*
|
|
611
|
-
*
|
|
612
|
-
* @overload No history - single input
|
|
613
|
-
* stream(input: InferenceInput): StreamResult
|
|
1642
|
+
* Executes streaming inference.
|
|
614
1643
|
*
|
|
615
|
-
*
|
|
616
|
-
*
|
|
1644
|
+
* Returns an async iterable of stream events that can also
|
|
1645
|
+
* be awaited for the final Turn.
|
|
617
1646
|
*
|
|
618
|
-
* @
|
|
619
|
-
*
|
|
1647
|
+
* @param historyOrInput - History (Message[] or Thread) or first input
|
|
1648
|
+
* @param input - Additional inputs to include in the request
|
|
1649
|
+
* @returns StreamResult that yields events and resolves to Turn
|
|
620
1650
|
*/
|
|
621
1651
|
stream(historyOrInput: Message[] | Thread | InferenceInput, ...input: InferenceInput[]): StreamResult;
|
|
622
|
-
/** The bound model */
|
|
1652
|
+
/** The bound model instance */
|
|
623
1653
|
readonly model: BoundLLMModel<TParams>;
|
|
624
|
-
/** Current system prompt */
|
|
625
|
-
readonly system: string | undefined;
|
|
626
|
-
/** Current parameters */
|
|
1654
|
+
/** Current system prompt (string or provider-specific array format) */
|
|
1655
|
+
readonly system: string | unknown[] | undefined;
|
|
1656
|
+
/** Current model parameters */
|
|
627
1657
|
readonly params: TParams | undefined;
|
|
628
1658
|
/** Provider API capabilities */
|
|
629
1659
|
readonly capabilities: LLMCapabilities;
|
|
630
1660
|
}
|
|
631
1661
|
/**
|
|
632
|
-
* Request passed from llm() core to providers
|
|
633
|
-
*
|
|
634
|
-
*
|
|
1662
|
+
* Request passed from the llm() core to providers.
|
|
1663
|
+
*
|
|
1664
|
+
* Contains all information needed by a provider to execute inference.
|
|
1665
|
+
* The config is required here because llm() resolves defaults before
|
|
1666
|
+
* passing to providers.
|
|
1667
|
+
*
|
|
1668
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
1669
|
+
* @internal
|
|
635
1670
|
*/
|
|
636
1671
|
interface LLMRequest<TParams = unknown> {
|
|
637
1672
|
/** All messages for this request (history + new input) */
|
|
638
1673
|
messages: Message[];
|
|
639
|
-
/**
|
|
640
|
-
|
|
1674
|
+
/**
|
|
1675
|
+
* System prompt - string or provider-specific array format.
|
|
1676
|
+
* Arrays are passed through directly to the provider.
|
|
1677
|
+
*/
|
|
1678
|
+
system?: string | unknown[];
|
|
641
1679
|
/** Model-specific parameters (passed through unchanged) */
|
|
642
1680
|
params?: TParams;
|
|
643
1681
|
/** Tools available for this request */
|
|
@@ -650,28 +1688,44 @@ interface LLMRequest<TParams = unknown> {
|
|
|
650
1688
|
signal?: AbortSignal;
|
|
651
1689
|
}
|
|
652
1690
|
/**
|
|
653
|
-
* Raw provider response
|
|
1691
|
+
* Raw provider response from a single inference cycle.
|
|
1692
|
+
*
|
|
1693
|
+
* Does not include tool loop handling - that's managed by llm() core.
|
|
1694
|
+
*
|
|
1695
|
+
* @internal
|
|
654
1696
|
*/
|
|
655
1697
|
interface LLMResponse {
|
|
1698
|
+
/** The assistant's response message */
|
|
656
1699
|
message: AssistantMessage;
|
|
1700
|
+
/** Token usage for this cycle */
|
|
657
1701
|
usage: TokenUsage;
|
|
1702
|
+
/** Stop reason from the provider */
|
|
658
1703
|
stopReason: string;
|
|
659
1704
|
/**
|
|
660
1705
|
* Structured output data extracted by the provider.
|
|
661
|
-
* Present when a structure schema was requested and
|
|
662
|
-
* successfully extracted the data (via tool call or native JSON mode).
|
|
663
|
-
* Providers handle their own extraction logic - core just uses this value.
|
|
1706
|
+
* Present when a structure schema was requested and successfully extracted.
|
|
664
1707
|
*/
|
|
665
1708
|
data?: unknown;
|
|
666
1709
|
}
|
|
667
1710
|
/**
|
|
668
|
-
* Raw provider stream result
|
|
1711
|
+
* Raw provider stream result.
|
|
1712
|
+
*
|
|
1713
|
+
* An async iterable of stream events with a Promise that resolves
|
|
1714
|
+
* to the complete response after streaming finishes.
|
|
1715
|
+
*
|
|
1716
|
+
* @internal
|
|
669
1717
|
*/
|
|
670
1718
|
interface LLMStreamResult extends AsyncIterable<StreamEvent> {
|
|
1719
|
+
/** Promise resolving to the complete response */
|
|
671
1720
|
readonly response: Promise<LLMResponse>;
|
|
672
1721
|
}
|
|
673
1722
|
/**
|
|
674
|
-
* Bound LLM model - full definition
|
|
1723
|
+
* Bound LLM model - full definition.
|
|
1724
|
+
*
|
|
1725
|
+
* Represents a model bound to a specific provider and model ID,
|
|
1726
|
+
* ready to execute inference requests.
|
|
1727
|
+
*
|
|
1728
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
675
1729
|
*/
|
|
676
1730
|
interface BoundLLMModel<TParams = unknown> {
|
|
677
1731
|
/** The model identifier */
|
|
@@ -680,131 +1734,363 @@ interface BoundLLMModel<TParams = unknown> {
|
|
|
680
1734
|
readonly provider: LLMProvider<TParams>;
|
|
681
1735
|
/** Provider API capabilities */
|
|
682
1736
|
readonly capabilities: LLMCapabilities;
|
|
683
|
-
/**
|
|
1737
|
+
/**
|
|
1738
|
+
* Executes a single non-streaming inference request.
|
|
1739
|
+
*
|
|
1740
|
+
* @param request - The inference request
|
|
1741
|
+
* @returns Promise resolving to the response
|
|
1742
|
+
*/
|
|
684
1743
|
complete(request: LLMRequest<TParams>): Promise<LLMResponse>;
|
|
685
|
-
/**
|
|
1744
|
+
/**
|
|
1745
|
+
* Executes a single streaming inference request.
|
|
1746
|
+
*
|
|
1747
|
+
* @param request - The inference request
|
|
1748
|
+
* @returns Stream result with events and final response
|
|
1749
|
+
*/
|
|
686
1750
|
stream(request: LLMRequest<TParams>): LLMStreamResult;
|
|
687
1751
|
}
|
|
688
1752
|
/**
|
|
689
|
-
* LLM Handler for providers
|
|
1753
|
+
* LLM Handler interface for providers.
|
|
1754
|
+
*
|
|
1755
|
+
* Implemented by providers to enable language model capabilities.
|
|
1756
|
+
*
|
|
1757
|
+
* @typeParam TParams - Provider-specific parameter type
|
|
690
1758
|
*/
|
|
691
1759
|
interface LLMHandler<TParams = unknown> {
|
|
692
|
-
/**
|
|
1760
|
+
/**
|
|
1761
|
+
* Binds a model ID to create an executable model instance.
|
|
1762
|
+
*
|
|
1763
|
+
* @param modelId - The model identifier to bind
|
|
1764
|
+
* @returns A bound LLM model ready for inference
|
|
1765
|
+
*/
|
|
693
1766
|
bind(modelId: string): BoundLLMModel<TParams>;
|
|
694
1767
|
/**
|
|
695
|
-
*
|
|
1768
|
+
* Sets the parent provider reference.
|
|
696
1769
|
* Called by createProvider() after the provider is constructed.
|
|
697
|
-
*
|
|
1770
|
+
*
|
|
1771
|
+
* @param provider - The parent provider
|
|
698
1772
|
* @internal
|
|
699
1773
|
*/
|
|
700
1774
|
_setProvider?(provider: LLMProvider<TParams>): void;
|
|
701
1775
|
}
|
|
702
1776
|
|
|
703
1777
|
/**
|
|
704
|
-
*
|
|
1778
|
+
* @fileoverview LLM instance factory and streaming logic for the Universal Provider Protocol.
|
|
1779
|
+
*
|
|
1780
|
+
* This module provides the core functionality for creating and managing LLM instances,
|
|
1781
|
+
* including support for tool execution, streaming responses, and structured output.
|
|
1782
|
+
*
|
|
1783
|
+
* @module core/llm
|
|
1784
|
+
*/
|
|
1785
|
+
|
|
1786
|
+
/**
|
|
1787
|
+
* Creates an LLM instance configured with the specified options.
|
|
1788
|
+
*
|
|
1789
|
+
* This is the primary factory function for creating LLM instances. It validates
|
|
1790
|
+
* provider capabilities, binds the model, and returns an instance with `generate`
|
|
1791
|
+
* and `stream` methods for inference.
|
|
1792
|
+
*
|
|
1793
|
+
* @typeParam TParams - Provider-specific parameter type for model configuration
|
|
1794
|
+
* @param options - Configuration options for the LLM instance
|
|
1795
|
+
* @returns A configured LLM instance ready for inference
|
|
1796
|
+
* @throws {UPPError} When the provider does not support the LLM modality
|
|
1797
|
+
* @throws {UPPError} When structured output is requested but not supported
|
|
1798
|
+
* @throws {UPPError} When tools are provided but not supported
|
|
1799
|
+
*
|
|
1800
|
+
* @example
|
|
1801
|
+
* ```typescript
|
|
1802
|
+
* import { llm } from 'upp';
|
|
1803
|
+
* import { anthropic } from 'upp/providers/anthropic';
|
|
1804
|
+
*
|
|
1805
|
+
* const assistant = llm({
|
|
1806
|
+
* model: anthropic('claude-sonnet-4-20250514'),
|
|
1807
|
+
* system: 'You are a helpful assistant.',
|
|
1808
|
+
* tools: [myTool],
|
|
1809
|
+
* });
|
|
1810
|
+
*
|
|
1811
|
+
* const turn = await assistant.generate('Hello, world!');
|
|
1812
|
+
* console.log(turn.text);
|
|
1813
|
+
* ```
|
|
705
1814
|
*/
|
|
706
1815
|
declare function llm<TParams = unknown>(options: LLMOptions<TParams>): LLMInstance<TParams>;
|
|
707
1816
|
|
|
708
1817
|
/**
|
|
709
|
-
*
|
|
1818
|
+
* @fileoverview Base provider interface and factory for the Universal Provider Protocol.
|
|
1819
|
+
*
|
|
1820
|
+
* This module provides the foundation for creating AI providers that conform to the
|
|
1821
|
+
* UPP specification. Providers are callable functions that create model references
|
|
1822
|
+
* and expose modality handlers (LLM, embedding, image).
|
|
1823
|
+
*
|
|
1824
|
+
* @module core/provider
|
|
1825
|
+
*/
|
|
1826
|
+
|
|
1827
|
+
/**
|
|
1828
|
+
* Configuration options for creating a new provider.
|
|
1829
|
+
*
|
|
1830
|
+
* @example
|
|
1831
|
+
* ```typescript
|
|
1832
|
+
* const options: CreateProviderOptions = {
|
|
1833
|
+
* name: 'my-provider',
|
|
1834
|
+
* version: '1.0.0',
|
|
1835
|
+
* modalities: {
|
|
1836
|
+
* llm: createLLMHandler(),
|
|
1837
|
+
* embedding: createEmbeddingHandler(),
|
|
1838
|
+
* },
|
|
1839
|
+
* };
|
|
1840
|
+
* ```
|
|
710
1841
|
*/
|
|
711
1842
|
interface CreateProviderOptions {
|
|
1843
|
+
/** Unique identifier for the provider */
|
|
712
1844
|
name: string;
|
|
1845
|
+
/** Semantic version string for the provider implementation */
|
|
713
1846
|
version: string;
|
|
1847
|
+
/** Handlers for supported modalities (LLM, embedding, image generation) */
|
|
714
1848
|
modalities: {
|
|
1849
|
+
/** Handler for language model completions */
|
|
715
1850
|
llm?: LLMHandler$1;
|
|
1851
|
+
/** Handler for text embeddings */
|
|
716
1852
|
embedding?: EmbeddingHandler;
|
|
1853
|
+
/** Handler for image generation */
|
|
717
1854
|
image?: ImageHandler;
|
|
718
1855
|
};
|
|
719
1856
|
}
|
|
720
1857
|
/**
|
|
721
|
-
*
|
|
1858
|
+
* Creates a provider factory function with attached modality handlers.
|
|
1859
|
+
*
|
|
1860
|
+
* The returned provider is a callable function that creates model references
|
|
1861
|
+
* when invoked with a model ID. It also exposes `name`, `version`, and
|
|
1862
|
+
* `modalities` properties for introspection.
|
|
722
1863
|
*
|
|
723
1864
|
* @typeParam TOptions - Provider-specific options type (defaults to unknown)
|
|
724
|
-
* @param options - Provider configuration
|
|
725
|
-
* @returns Provider
|
|
1865
|
+
* @param options - Provider configuration including name, version, and handlers
|
|
1866
|
+
* @returns A callable Provider with modalities attached
|
|
726
1867
|
*
|
|
727
1868
|
* @example
|
|
728
|
-
* ```
|
|
729
|
-
* //
|
|
1869
|
+
* ```typescript
|
|
1870
|
+
* // Create a basic provider
|
|
730
1871
|
* const anthropic = createProvider({
|
|
731
1872
|
* name: 'anthropic',
|
|
732
1873
|
* version: '1.0.0',
|
|
733
1874
|
* modalities: { llm: createLLMHandler() },
|
|
734
1875
|
* });
|
|
735
1876
|
*
|
|
736
|
-
* //
|
|
737
|
-
*
|
|
738
|
-
*
|
|
1877
|
+
* // Use the provider to create a model reference
|
|
1878
|
+
* const model = anthropic('claude-sonnet-4-20250514');
|
|
1879
|
+
*
|
|
1880
|
+
* // Provider with custom options type
|
|
1881
|
+
* interface MyOptions { apiVersion?: 'v1' | 'v2' }
|
|
1882
|
+
* const myProvider = createProvider<MyOptions>({
|
|
739
1883
|
* name: 'my-provider',
|
|
740
1884
|
* version: '1.0.0',
|
|
741
|
-
* modalities: { llm:
|
|
1885
|
+
* modalities: { llm: handler },
|
|
742
1886
|
* });
|
|
743
1887
|
* ```
|
|
744
1888
|
*/
|
|
745
1889
|
declare function createProvider<TOptions = unknown>(options: CreateProviderOptions): Provider<TOptions>;
|
|
746
1890
|
|
|
747
1891
|
/**
|
|
748
|
-
* Image
|
|
1892
|
+
* @fileoverview Image content handling for the Universal Provider Protocol.
|
|
1893
|
+
*
|
|
1894
|
+
* Provides a unified Image class for working with images across different sources
|
|
1895
|
+
* (file paths, URLs, raw bytes, base64). Supports conversion between formats and
|
|
1896
|
+
* integration with UPP message content blocks.
|
|
1897
|
+
*
|
|
1898
|
+
* @module core/image
|
|
1899
|
+
*/
|
|
1900
|
+
|
|
1901
|
+
/**
|
|
1902
|
+
* Represents an image that can be used in UPP messages.
|
|
1903
|
+
*
|
|
1904
|
+
* Images can be created from various sources (files, URLs, bytes, base64) and
|
|
1905
|
+
* converted to different formats as needed by providers. The class provides
|
|
1906
|
+
* a unified interface regardless of the underlying source type.
|
|
1907
|
+
*
|
|
1908
|
+
* @example
|
|
1909
|
+
* ```typescript
|
|
1910
|
+
* // Load from file
|
|
1911
|
+
* const fileImage = await Image.fromPath('./photo.jpg');
|
|
1912
|
+
*
|
|
1913
|
+
* // Reference by URL
|
|
1914
|
+
* const urlImage = Image.fromUrl('https://example.com/image.png');
|
|
1915
|
+
*
|
|
1916
|
+
* // From raw bytes
|
|
1917
|
+
* const bytesImage = Image.fromBytes(uint8Array, 'image/png');
|
|
1918
|
+
*
|
|
1919
|
+
* // Use in a message
|
|
1920
|
+
* const message = new UserMessage([image.toBlock()]);
|
|
1921
|
+
* ```
|
|
749
1922
|
*/
|
|
750
1923
|
declare class Image {
|
|
1924
|
+
/** The underlying image source (bytes, base64, or URL) */
|
|
751
1925
|
readonly source: ImageSource;
|
|
1926
|
+
/** MIME type of the image (e.g., 'image/jpeg', 'image/png') */
|
|
752
1927
|
readonly mimeType: string;
|
|
1928
|
+
/** Image width in pixels, if known */
|
|
753
1929
|
readonly width?: number;
|
|
1930
|
+
/** Image height in pixels, if known */
|
|
754
1931
|
readonly height?: number;
|
|
755
1932
|
private constructor();
|
|
756
1933
|
/**
|
|
757
|
-
*
|
|
1934
|
+
* Whether this image has data loaded in memory.
|
|
1935
|
+
*
|
|
1936
|
+
* Returns `false` for URL-sourced images that reference external resources.
|
|
1937
|
+
* These must be fetched before their data can be accessed.
|
|
758
1938
|
*/
|
|
759
1939
|
get hasData(): boolean;
|
|
760
1940
|
/**
|
|
761
|
-
*
|
|
1941
|
+
* Converts the image to a base64-encoded string.
|
|
1942
|
+
*
|
|
1943
|
+
* @returns The image data as a base64 string
|
|
1944
|
+
* @throws {Error} When the source is a URL (data must be fetched first)
|
|
762
1945
|
*/
|
|
763
1946
|
toBase64(): string;
|
|
764
1947
|
/**
|
|
765
|
-
*
|
|
1948
|
+
* Converts the image to a data URL suitable for embedding in HTML or CSS.
|
|
1949
|
+
*
|
|
1950
|
+
* @returns A data URL in the format `data:{mimeType};base64,{data}`
|
|
1951
|
+
* @throws {Error} When the source is a URL (data must be fetched first)
|
|
766
1952
|
*/
|
|
767
1953
|
toDataUrl(): string;
|
|
768
1954
|
/**
|
|
769
|
-
*
|
|
1955
|
+
* Gets the image data as raw bytes.
|
|
1956
|
+
*
|
|
1957
|
+
* @returns The image data as a Uint8Array
|
|
1958
|
+
* @throws {Error} When the source is a URL (data must be fetched first)
|
|
770
1959
|
*/
|
|
771
1960
|
toBytes(): Uint8Array;
|
|
772
1961
|
/**
|
|
773
|
-
*
|
|
1962
|
+
* Gets the URL for URL-sourced images.
|
|
1963
|
+
*
|
|
1964
|
+
* @returns The image URL
|
|
1965
|
+
* @throws {Error} When the source is not a URL
|
|
774
1966
|
*/
|
|
775
1967
|
toUrl(): string;
|
|
776
1968
|
/**
|
|
777
|
-
*
|
|
1969
|
+
* Converts this Image to an ImageBlock for use in UPP messages.
|
|
1970
|
+
*
|
|
1971
|
+
* @returns An ImageBlock that can be included in message content arrays
|
|
778
1972
|
*/
|
|
779
1973
|
toBlock(): ImageBlock;
|
|
780
1974
|
/**
|
|
781
|
-
*
|
|
1975
|
+
* Creates an Image by reading a file from disk.
|
|
1976
|
+
*
|
|
1977
|
+
* The file is read into memory as bytes. MIME type is automatically
|
|
1978
|
+
* detected from the file extension.
|
|
1979
|
+
*
|
|
1980
|
+
* @param path - Path to the image file
|
|
1981
|
+
* @returns Promise resolving to an Image with the file contents
|
|
1982
|
+
*
|
|
1983
|
+
* @example
|
|
1984
|
+
* ```typescript
|
|
1985
|
+
* const image = await Image.fromPath('./photos/vacation.jpg');
|
|
1986
|
+
* ```
|
|
782
1987
|
*/
|
|
783
1988
|
static fromPath(path: string): Promise<Image>;
|
|
784
1989
|
/**
|
|
785
|
-
*
|
|
1990
|
+
* Creates an Image from a URL reference.
|
|
1991
|
+
*
|
|
1992
|
+
* The URL is stored as a reference and not fetched. Providers will handle
|
|
1993
|
+
* URL-to-data conversion if needed. MIME type is detected from the URL
|
|
1994
|
+
* path if not provided.
|
|
1995
|
+
*
|
|
1996
|
+
* @param url - URL pointing to the image
|
|
1997
|
+
* @param mimeType - Optional MIME type override
|
|
1998
|
+
* @returns An Image referencing the URL
|
|
1999
|
+
*
|
|
2000
|
+
* @example
|
|
2001
|
+
* ```typescript
|
|
2002
|
+
* const image = Image.fromUrl('https://example.com/logo.png');
|
|
2003
|
+
* ```
|
|
786
2004
|
*/
|
|
787
2005
|
static fromUrl(url: string, mimeType?: string): Image;
|
|
788
2006
|
/**
|
|
789
|
-
*
|
|
2007
|
+
* Creates an Image from raw byte data.
|
|
2008
|
+
*
|
|
2009
|
+
* @param data - The image data as a Uint8Array
|
|
2010
|
+
* @param mimeType - The MIME type of the image
|
|
2011
|
+
* @returns An Image containing the byte data
|
|
2012
|
+
*
|
|
2013
|
+
* @example
|
|
2014
|
+
* ```typescript
|
|
2015
|
+
* const image = Image.fromBytes(pngData, 'image/png');
|
|
2016
|
+
* ```
|
|
790
2017
|
*/
|
|
791
2018
|
static fromBytes(data: Uint8Array, mimeType: string): Image;
|
|
792
2019
|
/**
|
|
793
|
-
*
|
|
2020
|
+
* Creates an Image from a base64-encoded string.
|
|
2021
|
+
*
|
|
2022
|
+
* @param base64 - The base64-encoded image data (without data URL prefix)
|
|
2023
|
+
* @param mimeType - The MIME type of the image
|
|
2024
|
+
* @returns An Image containing the base64 data
|
|
2025
|
+
*
|
|
2026
|
+
* @example
|
|
2027
|
+
* ```typescript
|
|
2028
|
+
* const image = Image.fromBase64(base64String, 'image/jpeg');
|
|
2029
|
+
* ```
|
|
794
2030
|
*/
|
|
795
2031
|
static fromBase64(base64: string, mimeType: string): Image;
|
|
796
2032
|
/**
|
|
797
|
-
*
|
|
2033
|
+
* Creates an Image from an existing ImageBlock.
|
|
2034
|
+
*
|
|
2035
|
+
* Useful for converting content blocks received from providers back
|
|
2036
|
+
* into Image instances for further processing.
|
|
2037
|
+
*
|
|
2038
|
+
* @param block - An ImageBlock from message content
|
|
2039
|
+
* @returns An Image with the block's source and metadata
|
|
798
2040
|
*/
|
|
799
2041
|
static fromBlock(block: ImageBlock): Image;
|
|
800
2042
|
}
|
|
801
2043
|
|
|
802
2044
|
/**
|
|
803
|
-
* UPP
|
|
804
|
-
*
|
|
2045
|
+
* @fileoverview Unified Provider Protocol (UPP) - A unified interface for AI model inference
|
|
2046
|
+
*
|
|
2047
|
+
* UPP provides a consistent API for interacting with multiple AI providers including
|
|
2048
|
+
* Anthropic, OpenAI, Google, Ollama, OpenRouter, and xAI. The library handles provider-specific
|
|
2049
|
+
* transformations, streaming, tool execution, and error handling.
|
|
2050
|
+
*
|
|
2051
|
+
* @example Basic usage
|
|
2052
|
+
* ```typescript
|
|
2053
|
+
* import { llm, anthropic } from '@providerprotocol/ai';
|
|
2054
|
+
*
|
|
2055
|
+
* const model = llm({
|
|
2056
|
+
* model: anthropic('claude-sonnet-4-20250514'),
|
|
2057
|
+
* params: { max_tokens: 1000 }
|
|
2058
|
+
* });
|
|
2059
|
+
*
|
|
2060
|
+
* const turn = await model.generate('Hello!');
|
|
2061
|
+
* console.log(turn.response.text);
|
|
2062
|
+
* ```
|
|
2063
|
+
*
|
|
2064
|
+
* @example Streaming
|
|
2065
|
+
* ```typescript
|
|
2066
|
+
* for await (const event of model.stream('Tell me a story')) {
|
|
2067
|
+
* if (event.type === 'text') {
|
|
2068
|
+
* process.stdout.write(event.delta.text);
|
|
2069
|
+
* }
|
|
2070
|
+
* }
|
|
2071
|
+
* ```
|
|
2072
|
+
*
|
|
2073
|
+
* @module @providerprotocol/ai
|
|
2074
|
+
* @packageDocumentation
|
|
2075
|
+
*/
|
|
2076
|
+
/** LLM instance factory for creating model-bound inference functions */
|
|
2077
|
+
|
|
2078
|
+
/**
|
|
2079
|
+
* UPP namespace object providing alternative import style.
|
|
2080
|
+
*
|
|
2081
|
+
* @example
|
|
2082
|
+
* ```typescript
|
|
2083
|
+
* import { ai } from '@providerprotocol/ai';
|
|
2084
|
+
*
|
|
2085
|
+
* const model = ai.llm({
|
|
2086
|
+
* model: openai('gpt-4o'),
|
|
2087
|
+
* params: { max_tokens: 1000 }
|
|
2088
|
+
* });
|
|
2089
|
+
* ```
|
|
805
2090
|
*/
|
|
806
2091
|
declare const ai: {
|
|
2092
|
+
/** LLM instance factory */
|
|
807
2093
|
llm: typeof llm;
|
|
808
2094
|
};
|
|
809
2095
|
|
|
810
|
-
export { type AssistantContent, AssistantMessage, type AudioBlock, type BinaryBlock, type BoundLLMModel, type ContentBlock, EmbeddingHandler, type EventDelta, Image, type ImageBlock, ImageHandler, type ImageSource, type InferenceInput, type JSONSchema, type JSONSchemaProperty, type JSONSchemaPropertyType, type LLMCapabilities, type LLMHandler, type LLMInstance, type LLMOptions, LLMProvider, type LLMRequest, type LLMResponse, type LLMStreamResult, Message, type MessageJSON, type MessageMetadata, type MessageOptions, type MessageType, ModelReference, Provider, ProviderConfig, type StreamEvent, type StreamEventType, type StreamResult, type TextBlock, Thread, type ThreadJSON, type TokenUsage, type Tool, type ToolCall, type ToolExecution, type ToolResult, ToolResultMessage, type ToolUseStrategy, type Turn, type UserContent, UserMessage, type VideoBlock, aggregateUsage, ai, contentBlockStart, contentBlockStop, createProvider, createStreamResult, createTurn, emptyUsage, isAssistantMessage, isAudioBlock, isBinaryBlock, isImageBlock, isTextBlock, isToolResultMessage, isUserMessage, isVideoBlock, llm, messageStart, messageStop, text, textDelta, toolCallDelta };
|
|
2096
|
+
export { type AfterCallResult, type AssistantContent, AssistantMessage, type AudioBlock, type BeforeCallResult, type BinaryBlock, type BoundLLMModel, type ContentBlock, EmbeddingHandler, type EventDelta, Image, type ImageBlock, ImageHandler, type ImageSource, type InferenceInput, type JSONSchema, type JSONSchemaProperty, type JSONSchemaPropertyType, type LLMCapabilities, type LLMHandler, type LLMInstance, type LLMOptions, LLMProvider, type LLMRequest, type LLMResponse, type LLMStreamResult, Message, type MessageJSON, type MessageMetadata, type MessageOptions, type MessageType, ModelReference, Provider, ProviderConfig, type StreamEvent, type StreamEventType, type StreamResult, type TextBlock, Thread, type ThreadJSON, type TokenUsage, type Tool, type ToolCall, type ToolExecution, type ToolMetadata, type ToolResult, ToolResultMessage, type ToolUseStrategy, type Turn, type UserContent, UserMessage, type VideoBlock, aggregateUsage, ai, contentBlockStart, contentBlockStop, createProvider, createStreamResult, createTurn, emptyUsage, isAssistantMessage, isAudioBlock, isBinaryBlock, isImageBlock, isTextBlock, isToolResultMessage, isUserMessage, isVideoBlock, llm, messageStart, messageStop, text, textDelta, toolCallDelta };
|