@providerprotocol/ai 0.0.11 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/dist/anthropic/index.d.ts +51 -15
  2. package/dist/anthropic/index.js +54 -19
  3. package/dist/anthropic/index.js.map +1 -1
  4. package/dist/{chunk-SUNYWHTH.js → chunk-MOU4U3PO.js} +55 -3
  5. package/dist/chunk-MOU4U3PO.js.map +1 -0
  6. package/dist/{chunk-Y6Q7JCNP.js → chunk-MSR5P65T.js} +1 -1
  7. package/dist/chunk-MSR5P65T.js.map +1 -0
  8. package/dist/{chunk-W4BB4BG2.js → chunk-SVYROCLD.js} +31 -11
  9. package/dist/chunk-SVYROCLD.js.map +1 -0
  10. package/dist/chunk-U4JJC2YX.js +234 -0
  11. package/dist/chunk-U4JJC2YX.js.map +1 -0
  12. package/dist/{chunk-X5G4EHL7.js → chunk-Z7RBRCRN.js} +1 -1
  13. package/dist/chunk-Z7RBRCRN.js.map +1 -0
  14. package/dist/google/index.d.ts +376 -7
  15. package/dist/google/index.js +127 -15
  16. package/dist/google/index.js.map +1 -1
  17. package/dist/http/index.d.ts +222 -25
  18. package/dist/http/index.js +3 -3
  19. package/dist/index.d.ts +1482 -198
  20. package/dist/index.js +233 -49
  21. package/dist/index.js.map +1 -1
  22. package/dist/ollama/index.d.ts +92 -20
  23. package/dist/ollama/index.js +17 -7
  24. package/dist/ollama/index.js.map +1 -1
  25. package/dist/openai/index.d.ts +340 -61
  26. package/dist/openai/index.js +57 -15
  27. package/dist/openai/index.js.map +1 -1
  28. package/dist/openrouter/index.d.ts +107 -51
  29. package/dist/openrouter/index.js +36 -8
  30. package/dist/openrouter/index.js.map +1 -1
  31. package/dist/provider-mKkz7Q9U.d.ts +488 -0
  32. package/dist/retry-Dh70lgr0.d.ts +508 -0
  33. package/dist/xai/index.d.ts +97 -22
  34. package/dist/xai/index.js +55 -19
  35. package/dist/xai/index.js.map +1 -1
  36. package/package.json +8 -12
  37. package/dist/chunk-CUCRF5W6.js +0 -136
  38. package/dist/chunk-CUCRF5W6.js.map +0 -1
  39. package/dist/chunk-SUNYWHTH.js.map +0 -1
  40. package/dist/chunk-W4BB4BG2.js.map +0 -1
  41. package/dist/chunk-X5G4EHL7.js.map +0 -1
  42. package/dist/chunk-Y6Q7JCNP.js.map +0 -1
  43. package/dist/provider-CUJWjgNl.d.ts +0 -192
  44. package/dist/retry-I2661_rv.d.ts +0 -118
  45. package/src/anthropic/index.ts +0 -3
  46. package/src/core/image.ts +0 -188
  47. package/src/core/llm.ts +0 -650
  48. package/src/core/provider.ts +0 -92
  49. package/src/google/index.ts +0 -3
  50. package/src/http/errors.ts +0 -112
  51. package/src/http/fetch.ts +0 -210
  52. package/src/http/index.ts +0 -31
  53. package/src/http/keys.ts +0 -136
  54. package/src/http/retry.ts +0 -205
  55. package/src/http/sse.ts +0 -136
  56. package/src/index.ts +0 -32
  57. package/src/ollama/index.ts +0 -3
  58. package/src/openai/index.ts +0 -39
  59. package/src/openrouter/index.ts +0 -11
  60. package/src/providers/anthropic/index.ts +0 -17
  61. package/src/providers/anthropic/llm.ts +0 -196
  62. package/src/providers/anthropic/transform.ts +0 -434
  63. package/src/providers/anthropic/types.ts +0 -213
  64. package/src/providers/google/index.ts +0 -17
  65. package/src/providers/google/llm.ts +0 -203
  66. package/src/providers/google/transform.ts +0 -447
  67. package/src/providers/google/types.ts +0 -214
  68. package/src/providers/ollama/index.ts +0 -43
  69. package/src/providers/ollama/llm.ts +0 -272
  70. package/src/providers/ollama/transform.ts +0 -434
  71. package/src/providers/ollama/types.ts +0 -260
  72. package/src/providers/openai/index.ts +0 -186
  73. package/src/providers/openai/llm.completions.ts +0 -201
  74. package/src/providers/openai/llm.responses.ts +0 -211
  75. package/src/providers/openai/transform.completions.ts +0 -561
  76. package/src/providers/openai/transform.responses.ts +0 -708
  77. package/src/providers/openai/types.ts +0 -1249
  78. package/src/providers/openrouter/index.ts +0 -177
  79. package/src/providers/openrouter/llm.completions.ts +0 -201
  80. package/src/providers/openrouter/llm.responses.ts +0 -211
  81. package/src/providers/openrouter/transform.completions.ts +0 -538
  82. package/src/providers/openrouter/transform.responses.ts +0 -742
  83. package/src/providers/openrouter/types.ts +0 -717
  84. package/src/providers/xai/index.ts +0 -223
  85. package/src/providers/xai/llm.completions.ts +0 -201
  86. package/src/providers/xai/llm.messages.ts +0 -195
  87. package/src/providers/xai/llm.responses.ts +0 -211
  88. package/src/providers/xai/transform.completions.ts +0 -565
  89. package/src/providers/xai/transform.messages.ts +0 -448
  90. package/src/providers/xai/transform.responses.ts +0 -678
  91. package/src/providers/xai/types.ts +0 -938
  92. package/src/types/content.ts +0 -133
  93. package/src/types/errors.ts +0 -85
  94. package/src/types/index.ts +0 -105
  95. package/src/types/llm.ts +0 -211
  96. package/src/types/messages.ts +0 -205
  97. package/src/types/provider.ts +0 -195
  98. package/src/types/schema.ts +0 -58
  99. package/src/types/stream.ts +0 -188
  100. package/src/types/thread.ts +0 -226
  101. package/src/types/tool.ts +0 -88
  102. package/src/types/turn.ts +0 -118
  103. package/src/utils/id.ts +0 -28
  104. package/src/xai/index.ts +0 -41
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-CUJWjgNl.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-CUJWjgNl.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-I2661_rv.js';
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-mKkz7Q9U.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-mKkz7Q9U.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-Dh70lgr0.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 types
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
- * All content block types
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
- * Helper to create a text block
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 definition
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 call requested by the model
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 a llm() instance) */
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
- /** Tool execution function */
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
- /** Optional approval handler for sensitive operations */
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
- * Strategy for tool execution
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
- /** Called when the model requests a tool call */
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
- /** Called before tool execution, return false to skip */
185
- onBeforeCall?(tool: Tool, params: unknown): boolean | Promise<boolean>;
186
- /** Called after tool execution */
187
- onAfterCall?(tool: Tool, params: unknown, result: unknown): void | Promise<void>;
188
- /** Called on tool execution error */
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
- /** Called when max iterations reached */
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
- /** The tool that was called */
679
+ /** Name of the tool that was called */
198
680
  toolName: string;
199
- /** Tool call ID */
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,334 @@ 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 type discriminator
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
- * Each provider uses its own namespace
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 message construction
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
- * Base message class
233
- * All messages inherit from this
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
- /** Raw content - implemented by subclasses */
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
- * Convenience accessor for text content
249
- * Concatenates all text blocks with '\n\n'
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
- * Convenience accessor for image content blocks
778
+ * All image content blocks in this message.
254
779
  */
255
780
  get images(): ImageBlock[];
256
781
  /**
257
- * Convenience accessor for audio content blocks
782
+ * All audio content blocks in this message.
258
783
  */
259
784
  get audio(): AudioBlock[];
260
785
  /**
261
- * Convenience accessor for video content blocks
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
- * May contain text, media, and/or tool calls
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 - Message ID and metadata
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
- /** Check if this message requests tool execution */
856
+ /**
857
+ * Whether this message contains tool call requests.
858
+ */
294
859
  get hasToolCalls(): boolean;
295
860
  }
296
861
  /**
297
- * Result of tool execution (sent back to model)
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 - Message ID and metadata
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
- * Token usage information
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 },
963
+ * { inputTokens: 50, outputTokens: 20 }
964
+ * ]
965
+ * };
966
+ * ```
324
967
  */
325
968
  interface TokenUsage {
326
- /** Input tokens across all cycles */
969
+ /** Total input tokens across all cycles */
327
970
  inputTokens: number;
328
- /** Output tokens across all cycles */
971
+ /** Total output tokens across all cycles */
329
972
  outputTokens: number;
330
- /** Total tokens */
973
+ /** Sum of input and output tokens */
331
974
  totalTokens: number;
332
- /** Per-cycle breakdown (if available) */
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;
336
989
  }>;
337
990
  }
338
991
  /**
339
- * A Turn represents the complete result of one inference call,
340
- * including all messages produced during tool execution loops.
992
+ * A Turn represents the complete result of one inference call.
993
+ *
994
+ * Includes all messages produced during tool execution loops,
995
+ * the final assistant response, token usage, and optional
996
+ * structured output data.
997
+ *
998
+ * @typeParam TData - Type of the structured output data
999
+ *
1000
+ * @example
1001
+ * ```typescript
1002
+ * const turn = await instance.generate('Hello');
1003
+ * console.log(turn.response.text);
1004
+ * console.log(`Used ${turn.usage.totalTokens} tokens in ${turn.cycles} cycles`);
1005
+ *
1006
+ * // With structured output
1007
+ * interface WeatherData { temperature: number; conditions: string; }
1008
+ * const turn = await instance.generate<WeatherData>('Get weather');
1009
+ * console.log(turn.data?.temperature);
1010
+ * ```
341
1011
  */
342
1012
  interface Turn<TData = unknown> {
343
1013
  /**
344
1014
  * All messages produced during this inference, in chronological order.
345
- * Types: UserMessage, AssistantMessage (may include toolCalls), ToolResultMessage
1015
+ * Includes UserMessage, AssistantMessage (may include toolCalls), and ToolResultMessage.
346
1016
  */
347
1017
  readonly messages: Message[];
348
- /** The final assistant response (convenience accessor) */
1018
+ /** The final assistant response (last AssistantMessage in the turn) */
349
1019
  readonly response: AssistantMessage;
350
1020
  /** Tool executions that occurred during this turn */
351
1021
  readonly toolExecutions: ToolExecution[];
@@ -354,120 +1024,320 @@ interface Turn<TData = unknown> {
354
1024
  /** Total number of inference cycles (1 + number of tool rounds) */
355
1025
  readonly cycles: number;
356
1026
  /**
357
- * Structured output data (if structure was provided).
1027
+ * Structured output data (if a structure schema was provided).
358
1028
  * Type is inferred from the schema when using TypeScript.
359
1029
  */
360
1030
  readonly data?: TData;
361
1031
  }
362
1032
  /**
363
- * Create a Turn from accumulated data
1033
+ * Creates a Turn from accumulated inference data.
1034
+ *
1035
+ * @typeParam TData - Type of the structured output data
1036
+ * @param messages - All messages produced during the inference
1037
+ * @param toolExecutions - Record of all tool executions
1038
+ * @param usage - Aggregate token usage
1039
+ * @param cycles - Number of inference cycles
1040
+ * @param data - Optional structured output data
1041
+ * @returns A complete Turn object
1042
+ * @throws Error if no assistant message is found in the messages
1043
+ *
1044
+ * @example
1045
+ * ```typescript
1046
+ * const turn = createTurn(
1047
+ * [userMsg, assistantMsg],
1048
+ * [],
1049
+ * { inputTokens: 100, outputTokens: 50, totalTokens: 150 },
1050
+ * 1
1051
+ * );
1052
+ * ```
364
1053
  */
365
1054
  declare function createTurn<TData = unknown>(messages: Message[], toolExecutions: ToolExecution[], usage: TokenUsage, cycles: number, data?: TData): Turn<TData>;
366
1055
  /**
367
- * Create empty token usage
1056
+ * Creates an empty TokenUsage object.
1057
+ *
1058
+ * @returns A TokenUsage with all values set to zero
1059
+ *
1060
+ * @example
1061
+ * ```typescript
1062
+ * const usage = emptyUsage();
1063
+ * // { inputTokens: 0, outputTokens: 0, totalTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0, cycles: [] }
1064
+ * ```
368
1065
  */
369
1066
  declare function emptyUsage(): TokenUsage;
370
1067
  /**
371
- * Aggregate token usage from multiple cycles
1068
+ * Aggregates token usage from multiple inference cycles.
1069
+ *
1070
+ * @param usages - Array of TokenUsage objects to aggregate
1071
+ * @returns Combined TokenUsage with per-cycle breakdown
1072
+ *
1073
+ * @example
1074
+ * ```typescript
1075
+ * const cycle1 = { inputTokens: 100, outputTokens: 30, totalTokens: 130, cacheReadTokens: 50, cacheWriteTokens: 0 };
1076
+ * const cycle2 = { inputTokens: 150, outputTokens: 40, totalTokens: 190, cacheReadTokens: 100, cacheWriteTokens: 0 };
1077
+ * const total = aggregateUsage([cycle1, cycle2]);
1078
+ * // { inputTokens: 250, outputTokens: 70, totalTokens: 320, cacheReadTokens: 150, cacheWriteTokens: 0, cycles: [...] }
1079
+ * ```
372
1080
  */
373
1081
  declare function aggregateUsage(usages: TokenUsage[]): TokenUsage;
374
1082
 
375
1083
  /**
376
- * Stream event types
1084
+ * @fileoverview Streaming types for real-time LLM responses.
1085
+ *
1086
+ * Defines the event types and interfaces for streaming LLM inference,
1087
+ * including text deltas, tool call deltas, and control events.
1088
+ *
1089
+ * @module types/stream
377
1090
  */
378
- type StreamEventType = 'text_delta' | 'reasoning_delta' | 'image_delta' | 'audio_delta' | 'video_delta' | 'tool_call_delta' | 'tool_execution_start' | 'tool_execution_end' | 'message_start' | 'message_stop' | 'content_block_start' | 'content_block_stop';
1091
+
379
1092
  /**
380
- * Event delta data (type-specific)
1093
+ * Stream event type discriminators.
1094
+ *
1095
+ * Each event type represents a different kind of streaming update
1096
+ * from the LLM provider.
1097
+ */
1098
+ type StreamEventType =
1099
+ /** Incremental text output */
1100
+ 'text_delta'
1101
+ /** Incremental reasoning/thinking output */
1102
+ | 'reasoning_delta'
1103
+ /** Incremental image data */
1104
+ | 'image_delta'
1105
+ /** Incremental audio data */
1106
+ | 'audio_delta'
1107
+ /** Incremental video data */
1108
+ | 'video_delta'
1109
+ /** Incremental tool call data (arguments being streamed) */
1110
+ | 'tool_call_delta'
1111
+ /** Tool execution has started */
1112
+ | 'tool_execution_start'
1113
+ /** Tool execution has completed */
1114
+ | 'tool_execution_end'
1115
+ /** Beginning of a message */
1116
+ | 'message_start'
1117
+ /** End of a message */
1118
+ | 'message_stop'
1119
+ /** Beginning of a content block */
1120
+ | 'content_block_start'
1121
+ /** End of a content block */
1122
+ | 'content_block_stop';
1123
+ /**
1124
+ * Event delta data payload.
1125
+ *
1126
+ * Contains the type-specific data for a streaming event.
1127
+ * Different fields are populated depending on the event type.
381
1128
  */
382
1129
  interface EventDelta {
1130
+ /** Incremental text content (for text_delta, reasoning_delta) */
383
1131
  text?: string;
1132
+ /** Incremental binary data (for image_delta, audio_delta, video_delta) */
384
1133
  data?: Uint8Array;
1134
+ /** Tool call identifier (for tool_call_delta, tool_execution_start/end) */
385
1135
  toolCallId?: string;
1136
+ /** Tool name (for tool_call_delta, tool_execution_start/end) */
386
1137
  toolName?: string;
1138
+ /** Incremental JSON arguments string (for tool_call_delta) */
387
1139
  argumentsJson?: string;
388
1140
  /** Tool execution result (for tool_execution_end) */
389
1141
  result?: unknown;
390
- /** Whether tool execution errored (for tool_execution_end) */
1142
+ /** Whether tool execution resulted in an error (for tool_execution_end) */
391
1143
  isError?: boolean;
392
- /** Timestamp in ms (for tool_execution_start/end) */
1144
+ /** Timestamp in milliseconds (for tool_execution_start/end) */
393
1145
  timestamp?: number;
394
1146
  }
395
1147
  /**
396
- * A streaming event
1148
+ * A single streaming event from the LLM.
1149
+ *
1150
+ * Events are emitted in order as the model generates output,
1151
+ * allowing for real-time display of responses.
1152
+ *
1153
+ * @example
1154
+ * ```typescript
1155
+ * for await (const event of stream) {
1156
+ * if (event.type === 'text_delta') {
1157
+ * process.stdout.write(event.delta.text ?? '');
1158
+ * } else if (event.type === 'tool_call_delta') {
1159
+ * console.log('Tool:', event.delta.toolName);
1160
+ * }
1161
+ * }
1162
+ * ```
397
1163
  */
398
1164
  interface StreamEvent {
399
- /** Event type */
1165
+ /** Event type discriminator */
400
1166
  type: StreamEventType;
401
1167
  /** Index of the content block this event belongs to */
402
1168
  index: number;
403
- /** Event data (type-specific) */
1169
+ /** Event-specific data payload */
404
1170
  delta: EventDelta;
405
1171
  }
406
1172
  /**
407
- * Stream result - async iterable that also provides final turn
1173
+ * Stream result - an async iterable that also provides the final turn.
1174
+ *
1175
+ * Allows consuming streaming events while also awaiting the complete
1176
+ * Turn result after streaming finishes.
1177
+ *
1178
+ * @typeParam TData - Type of the structured output data
1179
+ *
1180
+ * @example
1181
+ * ```typescript
1182
+ * const stream = instance.stream('Tell me a story');
1183
+ *
1184
+ * // Consume streaming events
1185
+ * for await (const event of stream) {
1186
+ * if (event.type === 'text_delta') {
1187
+ * process.stdout.write(event.delta.text ?? '');
1188
+ * }
1189
+ * }
1190
+ *
1191
+ * // Get the complete turn after streaming
1192
+ * const turn = await stream.turn;
1193
+ * console.log('\n\nTokens used:', turn.usage.totalTokens);
1194
+ * ```
408
1195
  */
409
1196
  interface StreamResult<TData = unknown> extends AsyncIterable<StreamEvent> {
410
1197
  /**
411
- * Get the complete Turn after streaming finishes.
412
- * Resolves when the stream completes.
1198
+ * Promise that resolves to the complete Turn after streaming finishes.
413
1199
  */
414
1200
  readonly turn: Promise<Turn<TData>>;
415
- /** Abort the stream */
1201
+ /**
1202
+ * Aborts the stream, stopping further events and cancelling the request.
1203
+ */
416
1204
  abort(): void;
417
1205
  }
418
1206
  /**
419
- * Create a stream result from an async generator and completion promise
1207
+ * Creates a StreamResult from an async generator and completion promise.
1208
+ *
1209
+ * @typeParam TData - Type of the structured output data
1210
+ * @param generator - Async generator that yields stream events
1211
+ * @param turnPromise - Promise that resolves to the complete Turn
1212
+ * @param abortController - Controller for aborting the stream
1213
+ * @returns A StreamResult that can be iterated and awaited
1214
+ *
1215
+ * @example
1216
+ * ```typescript
1217
+ * const abortController = new AbortController();
1218
+ * const stream = createStreamResult(
1219
+ * eventGenerator(),
1220
+ * turnPromise,
1221
+ * abortController
1222
+ * );
1223
+ * ```
420
1224
  */
421
1225
  declare function createStreamResult<TData = unknown>(generator: AsyncGenerator<StreamEvent, void, unknown>, turnPromise: Promise<Turn<TData>>, abortController: AbortController): StreamResult<TData>;
422
1226
  /**
423
- * Create a text delta event
1227
+ * Creates a text delta stream event.
1228
+ *
1229
+ * @param text - The incremental text content
1230
+ * @param index - Content block index (default: 0)
1231
+ * @returns A text_delta StreamEvent
424
1232
  */
425
1233
  declare function textDelta(text: string, index?: number): StreamEvent;
426
1234
  /**
427
- * Create a tool call delta event
1235
+ * Creates a tool call delta stream event.
1236
+ *
1237
+ * @param toolCallId - Unique identifier for the tool call
1238
+ * @param toolName - Name of the tool being called
1239
+ * @param argumentsJson - Incremental JSON arguments string
1240
+ * @param index - Content block index (default: 0)
1241
+ * @returns A tool_call_delta StreamEvent
428
1242
  */
429
1243
  declare function toolCallDelta(toolCallId: string, toolName: string, argumentsJson: string, index?: number): StreamEvent;
430
1244
  /**
431
- * Create a message start event
1245
+ * Creates a message start stream event.
1246
+ *
1247
+ * @returns A message_start StreamEvent
432
1248
  */
433
1249
  declare function messageStart(): StreamEvent;
434
1250
  /**
435
- * Create a message stop event
1251
+ * Creates a message stop stream event.
1252
+ *
1253
+ * @returns A message_stop StreamEvent
436
1254
  */
437
1255
  declare function messageStop(): StreamEvent;
438
1256
  /**
439
- * Create a content block start event
1257
+ * Creates a content block start stream event.
1258
+ *
1259
+ * @param index - The content block index starting
1260
+ * @returns A content_block_start StreamEvent
440
1261
  */
441
1262
  declare function contentBlockStart(index: number): StreamEvent;
442
1263
  /**
443
- * Create a content block stop event
1264
+ * Creates a content block stop stream event.
1265
+ *
1266
+ * @param index - The content block index stopping
1267
+ * @returns A content_block_stop StreamEvent
444
1268
  */
445
1269
  declare function contentBlockStop(index: number): StreamEvent;
446
1270
 
447
1271
  /**
448
- * Serialized message format
1272
+ * @fileoverview Thread class for managing conversation history.
1273
+ *
1274
+ * Provides a utility class for building and manipulating conversation
1275
+ * message sequences, with support for serialization and deserialization.
1276
+ *
1277
+ * @module types/thread
1278
+ */
1279
+
1280
+ /**
1281
+ * Serialized message format for JSON storage.
1282
+ *
1283
+ * Used when persisting messages to storage or transmitting over the network.
449
1284
  */
450
1285
  interface MessageJSON {
1286
+ /** Unique message identifier */
451
1287
  id: string;
1288
+ /** Message type discriminator */
452
1289
  type: MessageType;
1290
+ /** Content blocks in the message */
453
1291
  content: ContentBlock[];
1292
+ /** Tool calls (for assistant messages) */
454
1293
  toolCalls?: ToolCall[];
1294
+ /** Tool results (for tool result messages) */
455
1295
  results?: ToolResult[];
1296
+ /** Provider-specific metadata */
456
1297
  metadata?: MessageMetadata;
1298
+ /** ISO timestamp string */
457
1299
  timestamp: string;
458
1300
  }
459
1301
  /**
460
- * Serialized thread format
1302
+ * Serialized thread format for JSON storage.
1303
+ *
1304
+ * Contains all data needed to reconstruct a Thread instance.
461
1305
  */
462
1306
  interface ThreadJSON {
1307
+ /** Unique thread identifier */
463
1308
  id: string;
1309
+ /** Serialized messages */
464
1310
  messages: MessageJSON[];
1311
+ /** ISO timestamp of thread creation */
465
1312
  createdAt: string;
1313
+ /** ISO timestamp of last update */
466
1314
  updatedAt: string;
467
1315
  }
468
1316
  /**
469
- * Thread - A utility class for managing conversation history
470
- * Users control their own history; Thread is optional
1317
+ * Thread - A utility class for managing conversation history.
1318
+ *
1319
+ * Provides methods for building, manipulating, and persisting
1320
+ * conversation message sequences. This class is optional; users
1321
+ * can also manage their own `Message[]` arrays directly.
1322
+ *
1323
+ * @example
1324
+ * ```typescript
1325
+ * // Create a new thread and add messages
1326
+ * const thread = new Thread();
1327
+ * thread.user('Hello!');
1328
+ * thread.assistant('Hi there! How can I help?');
1329
+ *
1330
+ * // Use with LLM inference
1331
+ * const turn = await instance.generate(thread, 'What is 2+2?');
1332
+ * thread.append(turn);
1333
+ *
1334
+ * // Serialize for storage
1335
+ * const json = thread.toJSON();
1336
+ * localStorage.setItem('chat', JSON.stringify(json));
1337
+ *
1338
+ * // Restore from storage
1339
+ * const restored = Thread.fromJSON(JSON.parse(localStorage.getItem('chat')));
1340
+ * ```
471
1341
  */
472
1342
  declare class Thread {
473
1343
  /** Unique thread identifier */
@@ -479,78 +1349,188 @@ declare class Thread {
479
1349
  /** Last update timestamp */
480
1350
  private _updatedAt;
481
1351
  /**
482
- * Create a new thread, optionally with initial messages
1352
+ * Creates a new thread instance.
1353
+ *
1354
+ * @param messages - Optional initial messages to populate the thread
483
1355
  */
484
1356
  constructor(messages?: Message[]);
485
- /** All messages in the thread (readonly) */
1357
+ /**
1358
+ * All messages in the thread (readonly).
1359
+ */
486
1360
  get messages(): readonly Message[];
487
- /** Number of messages */
1361
+ /**
1362
+ * Number of messages in the thread.
1363
+ */
488
1364
  get length(): number;
489
1365
  /**
490
- * Append messages from a turn
1366
+ * Appends all messages from a Turn to the thread.
1367
+ *
1368
+ * @param turn - The Turn containing messages to append
1369
+ * @returns This thread instance for chaining
491
1370
  */
492
1371
  append(turn: Turn): this;
493
1372
  /**
494
- * Add raw messages
1373
+ * Adds raw messages to the thread.
1374
+ *
1375
+ * @param messages - Messages to add
1376
+ * @returns This thread instance for chaining
495
1377
  */
496
1378
  push(...messages: Message[]): this;
497
1379
  /**
498
- * Add a user message
1380
+ * Adds a user message to the thread.
1381
+ *
1382
+ * @param content - String or array of content blocks
1383
+ * @returns This thread instance for chaining
1384
+ *
1385
+ * @example
1386
+ * ```typescript
1387
+ * thread.user('Hello, world!');
1388
+ * thread.user([
1389
+ * { type: 'text', text: 'Describe this image:' },
1390
+ * { type: 'image', source: { type: 'url', url: '...' }, mimeType: 'image/png' }
1391
+ * ]);
1392
+ * ```
499
1393
  */
500
1394
  user(content: string | UserContent[]): this;
501
1395
  /**
502
- * Add an assistant message
1396
+ * Adds an assistant message to the thread.
1397
+ *
1398
+ * @param content - String or array of content blocks
1399
+ * @returns This thread instance for chaining
1400
+ *
1401
+ * @example
1402
+ * ```typescript
1403
+ * thread.assistant('I can help with that!');
1404
+ * ```
503
1405
  */
504
1406
  assistant(content: string | AssistantContent[]): this;
505
1407
  /**
506
- * Get messages by type
1408
+ * Filters messages by type.
1409
+ *
1410
+ * @param type - The message type to filter by
1411
+ * @returns Array of messages matching the type
1412
+ *
1413
+ * @example
1414
+ * ```typescript
1415
+ * const userMessages = thread.filter('user');
1416
+ * const assistantMessages = thread.filter('assistant');
1417
+ * ```
507
1418
  */
508
1419
  filter(type: MessageType): Message[];
509
1420
  /**
510
- * Get the last N messages
1421
+ * Returns the last N messages from the thread.
1422
+ *
1423
+ * @param count - Number of messages to return
1424
+ * @returns Array of the last N messages
1425
+ *
1426
+ * @example
1427
+ * ```typescript
1428
+ * const recent = thread.tail(5);
1429
+ * ```
511
1430
  */
512
1431
  tail(count: number): Message[];
513
1432
  /**
514
- * Create a new thread with a subset of messages
1433
+ * Creates a new thread with a subset of messages.
1434
+ *
1435
+ * @param start - Start index (inclusive)
1436
+ * @param end - End index (exclusive)
1437
+ * @returns New Thread containing the sliced messages
1438
+ *
1439
+ * @example
1440
+ * ```typescript
1441
+ * const subset = thread.slice(0, 10);
1442
+ * ```
515
1443
  */
516
1444
  slice(start?: number, end?: number): Thread;
517
1445
  /**
518
- * Clear all messages
1446
+ * Removes all messages from the thread.
1447
+ *
1448
+ * @returns This thread instance for chaining
519
1449
  */
520
1450
  clear(): this;
521
1451
  /**
522
- * Convert to plain message array
1452
+ * Converts the thread to a plain message array.
1453
+ *
1454
+ * @returns Copy of the internal message array
523
1455
  */
524
1456
  toMessages(): Message[];
525
1457
  /**
526
- * Serialize to JSON
1458
+ * Serializes the thread to JSON format.
1459
+ *
1460
+ * @returns JSON-serializable representation of the thread
1461
+ *
1462
+ * @example
1463
+ * ```typescript
1464
+ * const json = thread.toJSON();
1465
+ * localStorage.setItem('thread', JSON.stringify(json));
1466
+ * ```
527
1467
  */
528
1468
  toJSON(): ThreadJSON;
529
1469
  /**
530
- * Deserialize from JSON
1470
+ * Deserializes a thread from JSON format.
1471
+ *
1472
+ * @param json - The JSON representation to deserialize
1473
+ * @returns Reconstructed Thread instance
1474
+ *
1475
+ * @example
1476
+ * ```typescript
1477
+ * const json = JSON.parse(localStorage.getItem('thread'));
1478
+ * const thread = Thread.fromJSON(json);
1479
+ * ```
531
1480
  */
532
1481
  static fromJSON(json: ThreadJSON): Thread;
533
1482
  /**
534
- * Iterate over messages
1483
+ * Enables iteration over messages with for...of loops.
1484
+ *
1485
+ * @returns Iterator over the thread's messages
1486
+ *
1487
+ * @example
1488
+ * ```typescript
1489
+ * for (const message of thread) {
1490
+ * console.log(message.text);
1491
+ * }
1492
+ * ```
535
1493
  */
536
1494
  [Symbol.iterator](): Iterator<Message>;
537
1495
  /**
538
- * Convert a message to JSON
1496
+ * Converts a message to JSON format.
539
1497
  */
540
1498
  private messageToJSON;
541
1499
  /**
542
- * Reconstruct a message from JSON
1500
+ * Reconstructs a message from JSON format.
543
1501
  */
544
1502
  private static messageFromJSON;
545
1503
  }
546
1504
 
547
1505
  /**
548
- * LLMCapabilities declares what the provider's API supports, not individual model capabilities.
1506
+ * @fileoverview LLM types for language model inference.
1507
+ *
1508
+ * Defines the interfaces for configuring and executing LLM inference,
1509
+ * including options, instances, requests, responses, and capabilities.
1510
+ *
1511
+ * @module types/llm
1512
+ */
1513
+
1514
+ /**
1515
+ * LLM capabilities declare what a provider's API supports.
1516
+ *
1517
+ * These are API-level capabilities, not individual model capabilities.
549
1518
  * 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—this is expected behavior.
1519
+ * the provider's API will return an error.
551
1520
  *
552
- * Capabilities are static - they are constant for the lifetime of the provider instance
553
- * and do not vary per-request or per-model.
1521
+ * Capabilities are static and do not vary per-request or per-model.
1522
+ *
1523
+ * @example
1524
+ * ```typescript
1525
+ * const capabilities: LLMCapabilities = {
1526
+ * streaming: true,
1527
+ * tools: true,
1528
+ * structuredOutput: true,
1529
+ * imageInput: true,
1530
+ * videoInput: false,
1531
+ * audioInput: false
1532
+ * };
1533
+ * ```
554
1534
  */
555
1535
  interface LLMCapabilities {
556
1536
  /** Provider API supports streaming responses */
@@ -559,19 +1539,37 @@ interface LLMCapabilities {
559
1539
  tools: boolean;
560
1540
  /** Provider API supports native structured output (JSON schema) */
561
1541
  structuredOutput: boolean;
562
- /** Provider API supports image input */
1542
+ /** Provider API supports image input in messages */
563
1543
  imageInput: boolean;
564
- /** Provider API supports video input */
1544
+ /** Provider API supports video input in messages */
565
1545
  videoInput: boolean;
566
- /** Provider API supports audio input */
1546
+ /** Provider API supports audio input in messages */
567
1547
  audioInput: boolean;
568
1548
  }
569
1549
  /**
570
- * Input types for inference
1550
+ * Valid input types for inference.
1551
+ *
1552
+ * Inference input can be a simple string, a Message object, or
1553
+ * a raw ContentBlock for multimodal input.
571
1554
  */
572
1555
  type InferenceInput = string | Message | ContentBlock;
573
1556
  /**
574
- * Options for llm() function
1557
+ * Options for creating an LLM instance with the llm() function.
1558
+ *
1559
+ * @typeParam TParams - Provider-specific parameter type
1560
+ *
1561
+ * @example
1562
+ * ```typescript
1563
+ * const options: LLMOptions = {
1564
+ * model: openai('gpt-4'),
1565
+ * system: 'You are a helpful assistant.',
1566
+ * params: { temperature: 0.7, max_tokens: 1000 },
1567
+ * tools: [weatherTool, searchTool],
1568
+ * toolStrategy: { maxIterations: 5 }
1569
+ * };
1570
+ *
1571
+ * const instance = llm(options);
1572
+ * ```
575
1573
  */
576
1574
  interface LLMOptions<TParams = unknown> {
577
1575
  /** A model reference from a provider factory */
@@ -580,8 +1578,16 @@ interface LLMOptions<TParams = unknown> {
580
1578
  config?: ProviderConfig;
581
1579
  /** Model-specific parameters (temperature, max_tokens, etc.) */
582
1580
  params?: TParams;
583
- /** System prompt for all inferences */
584
- system?: string;
1581
+ /**
1582
+ * System prompt for all inferences.
1583
+ *
1584
+ * Can be a simple string or a provider-specific array format:
1585
+ * - Anthropic: `[{type: 'text', text: '...', cache_control?: {...}}]`
1586
+ * - Google: `[{text: '...'}, {text: '...'}]` (parts array)
1587
+ *
1588
+ * Array formats are passed through directly to the provider.
1589
+ */
1590
+ system?: string | unknown[];
585
1591
  /** Tools available to the model */
586
1592
  tools?: Tool[];
587
1593
  /** Tool execution strategy */
@@ -590,54 +1596,84 @@ interface LLMOptions<TParams = unknown> {
590
1596
  structure?: JSONSchema;
591
1597
  }
592
1598
  /**
593
- * LLM instance returned by llm()
1599
+ * LLM instance returned by the llm() function.
1600
+ *
1601
+ * Provides methods for generating responses and streaming output,
1602
+ * with access to the bound model and capabilities.
1603
+ *
1604
+ * @typeParam TParams - Provider-specific parameter type
1605
+ *
1606
+ * @example
1607
+ * ```typescript
1608
+ * const instance = llm({ model: openai('gpt-4') });
1609
+ *
1610
+ * // Simple generation
1611
+ * const turn = await instance.generate('Hello!');
1612
+ * console.log(turn.response.text);
1613
+ *
1614
+ * // Streaming
1615
+ * const stream = instance.stream('Tell me a story');
1616
+ * for await (const event of stream) {
1617
+ * if (event.type === 'text_delta') {
1618
+ * process.stdout.write(event.delta.text ?? '');
1619
+ * }
1620
+ * }
1621
+ * const finalTurn = await stream.turn;
1622
+ * ```
594
1623
  */
595
1624
  interface LLMInstance<TParams = unknown> {
596
1625
  /**
597
- * Execute inference and return complete Turn
1626
+ * Executes inference and returns the complete Turn.
598
1627
  *
599
- * @overload No history - single input
600
- * generate(input: InferenceInput): Promise<Turn>
1628
+ * Supports multiple calling patterns:
1629
+ * - Single input: `generate('Hello')`
1630
+ * - Multiple inputs: `generate('Context...', 'Question?')`
1631
+ * - With history: `generate(messages, 'Follow-up?')`
1632
+ * - With thread: `generate(thread, 'Next message')`
601
1633
  *
602
- * @overload No history - multiple inputs
603
- * generate(...inputs: InferenceInput[]): Promise<Turn>
604
- *
605
- * @overload With history
606
- * generate(history: Message[] | Thread, ...inputs: InferenceInput[]): Promise<Turn>
1634
+ * @param historyOrInput - History (Message[] or Thread) or first input
1635
+ * @param input - Additional inputs to include in the request
1636
+ * @returns Promise resolving to the complete Turn
607
1637
  */
608
1638
  generate(historyOrInput: Message[] | Thread | InferenceInput, ...input: InferenceInput[]): Promise<Turn>;
609
1639
  /**
610
- * Execute streaming inference
611
- *
612
- * @overload No history - single input
613
- * stream(input: InferenceInput): StreamResult
1640
+ * Executes streaming inference.
614
1641
  *
615
- * @overload No history - multiple inputs
616
- * stream(...inputs: InferenceInput[]): StreamResult
1642
+ * Returns an async iterable of stream events that can also
1643
+ * be awaited for the final Turn.
617
1644
  *
618
- * @overload With history
619
- * stream(history: Message[] | Thread, ...inputs: InferenceInput[]): StreamResult
1645
+ * @param historyOrInput - History (Message[] or Thread) or first input
1646
+ * @param input - Additional inputs to include in the request
1647
+ * @returns StreamResult that yields events and resolves to Turn
620
1648
  */
621
1649
  stream(historyOrInput: Message[] | Thread | InferenceInput, ...input: InferenceInput[]): StreamResult;
622
- /** The bound model */
1650
+ /** The bound model instance */
623
1651
  readonly model: BoundLLMModel<TParams>;
624
- /** Current system prompt */
625
- readonly system: string | undefined;
626
- /** Current parameters */
1652
+ /** Current system prompt (string or provider-specific array format) */
1653
+ readonly system: string | unknown[] | undefined;
1654
+ /** Current model parameters */
627
1655
  readonly params: TParams | undefined;
628
1656
  /** Provider API capabilities */
629
1657
  readonly capabilities: LLMCapabilities;
630
1658
  }
631
1659
  /**
632
- * Request passed from llm() core to providers
633
- * Note: config is required here because llm() core resolves defaults
634
- * before passing to providers
1660
+ * Request passed from the llm() core to providers.
1661
+ *
1662
+ * Contains all information needed by a provider to execute inference.
1663
+ * The config is required here because llm() resolves defaults before
1664
+ * passing to providers.
1665
+ *
1666
+ * @typeParam TParams - Provider-specific parameter type
1667
+ * @internal
635
1668
  */
636
1669
  interface LLMRequest<TParams = unknown> {
637
1670
  /** All messages for this request (history + new input) */
638
1671
  messages: Message[];
639
- /** System prompt */
640
- system?: string;
1672
+ /**
1673
+ * System prompt - string or provider-specific array format.
1674
+ * Arrays are passed through directly to the provider.
1675
+ */
1676
+ system?: string | unknown[];
641
1677
  /** Model-specific parameters (passed through unchanged) */
642
1678
  params?: TParams;
643
1679
  /** Tools available for this request */
@@ -650,28 +1686,44 @@ interface LLMRequest<TParams = unknown> {
650
1686
  signal?: AbortSignal;
651
1687
  }
652
1688
  /**
653
- * Raw provider response (single cycle, no tool loop)
1689
+ * Raw provider response from a single inference cycle.
1690
+ *
1691
+ * Does not include tool loop handling - that's managed by llm() core.
1692
+ *
1693
+ * @internal
654
1694
  */
655
1695
  interface LLMResponse {
1696
+ /** The assistant's response message */
656
1697
  message: AssistantMessage;
1698
+ /** Token usage for this cycle */
657
1699
  usage: TokenUsage;
1700
+ /** Stop reason from the provider */
658
1701
  stopReason: string;
659
1702
  /**
660
1703
  * Structured output data extracted by the provider.
661
- * Present when a structure schema was requested and the provider
662
- * successfully extracted the data (via tool call or native JSON mode).
663
- * Providers handle their own extraction logic - core just uses this value.
1704
+ * Present when a structure schema was requested and successfully extracted.
664
1705
  */
665
1706
  data?: unknown;
666
1707
  }
667
1708
  /**
668
- * Raw provider stream result
1709
+ * Raw provider stream result.
1710
+ *
1711
+ * An async iterable of stream events with a Promise that resolves
1712
+ * to the complete response after streaming finishes.
1713
+ *
1714
+ * @internal
669
1715
  */
670
1716
  interface LLMStreamResult extends AsyncIterable<StreamEvent> {
1717
+ /** Promise resolving to the complete response */
671
1718
  readonly response: Promise<LLMResponse>;
672
1719
  }
673
1720
  /**
674
- * Bound LLM model - full definition
1721
+ * Bound LLM model - full definition.
1722
+ *
1723
+ * Represents a model bound to a specific provider and model ID,
1724
+ * ready to execute inference requests.
1725
+ *
1726
+ * @typeParam TParams - Provider-specific parameter type
675
1727
  */
676
1728
  interface BoundLLMModel<TParams = unknown> {
677
1729
  /** The model identifier */
@@ -680,131 +1732,363 @@ interface BoundLLMModel<TParams = unknown> {
680
1732
  readonly provider: LLMProvider<TParams>;
681
1733
  /** Provider API capabilities */
682
1734
  readonly capabilities: LLMCapabilities;
683
- /** Execute a single non-streaming inference request */
1735
+ /**
1736
+ * Executes a single non-streaming inference request.
1737
+ *
1738
+ * @param request - The inference request
1739
+ * @returns Promise resolving to the response
1740
+ */
684
1741
  complete(request: LLMRequest<TParams>): Promise<LLMResponse>;
685
- /** Execute a single streaming inference request */
1742
+ /**
1743
+ * Executes a single streaming inference request.
1744
+ *
1745
+ * @param request - The inference request
1746
+ * @returns Stream result with events and final response
1747
+ */
686
1748
  stream(request: LLMRequest<TParams>): LLMStreamResult;
687
1749
  }
688
1750
  /**
689
- * LLM Handler for providers
1751
+ * LLM Handler interface for providers.
1752
+ *
1753
+ * Implemented by providers to enable language model capabilities.
1754
+ *
1755
+ * @typeParam TParams - Provider-specific parameter type
690
1756
  */
691
1757
  interface LLMHandler<TParams = unknown> {
692
- /** Bind model ID to create executable model */
1758
+ /**
1759
+ * Binds a model ID to create an executable model instance.
1760
+ *
1761
+ * @param modelId - The model identifier to bind
1762
+ * @returns A bound LLM model ready for inference
1763
+ */
693
1764
  bind(modelId: string): BoundLLMModel<TParams>;
694
1765
  /**
695
- * Internal: Set the parent provider reference.
1766
+ * Sets the parent provider reference.
696
1767
  * Called by createProvider() after the provider is constructed.
697
- * This allows bind() to return models with the correct provider reference.
1768
+ *
1769
+ * @param provider - The parent provider
698
1770
  * @internal
699
1771
  */
700
1772
  _setProvider?(provider: LLMProvider<TParams>): void;
701
1773
  }
702
1774
 
703
1775
  /**
704
- * Create an LLM instance
1776
+ * @fileoverview LLM instance factory and streaming logic for the Universal Provider Protocol.
1777
+ *
1778
+ * This module provides the core functionality for creating and managing LLM instances,
1779
+ * including support for tool execution, streaming responses, and structured output.
1780
+ *
1781
+ * @module core/llm
1782
+ */
1783
+
1784
+ /**
1785
+ * Creates an LLM instance configured with the specified options.
1786
+ *
1787
+ * This is the primary factory function for creating LLM instances. It validates
1788
+ * provider capabilities, binds the model, and returns an instance with `generate`
1789
+ * and `stream` methods for inference.
1790
+ *
1791
+ * @typeParam TParams - Provider-specific parameter type for model configuration
1792
+ * @param options - Configuration options for the LLM instance
1793
+ * @returns A configured LLM instance ready for inference
1794
+ * @throws {UPPError} When the provider does not support the LLM modality
1795
+ * @throws {UPPError} When structured output is requested but not supported
1796
+ * @throws {UPPError} When tools are provided but not supported
1797
+ *
1798
+ * @example
1799
+ * ```typescript
1800
+ * import { llm } from 'upp';
1801
+ * import { anthropic } from 'upp/providers/anthropic';
1802
+ *
1803
+ * const assistant = llm({
1804
+ * model: anthropic('claude-sonnet-4-20250514'),
1805
+ * system: 'You are a helpful assistant.',
1806
+ * tools: [myTool],
1807
+ * });
1808
+ *
1809
+ * const turn = await assistant.generate('Hello, world!');
1810
+ * console.log(turn.text);
1811
+ * ```
705
1812
  */
706
1813
  declare function llm<TParams = unknown>(options: LLMOptions<TParams>): LLMInstance<TParams>;
707
1814
 
708
1815
  /**
709
- * Options for creating a provider
1816
+ * @fileoverview Base provider interface and factory for the Universal Provider Protocol.
1817
+ *
1818
+ * This module provides the foundation for creating AI providers that conform to the
1819
+ * UPP specification. Providers are callable functions that create model references
1820
+ * and expose modality handlers (LLM, embedding, image).
1821
+ *
1822
+ * @module core/provider
1823
+ */
1824
+
1825
+ /**
1826
+ * Configuration options for creating a new provider.
1827
+ *
1828
+ * @example
1829
+ * ```typescript
1830
+ * const options: CreateProviderOptions = {
1831
+ * name: 'my-provider',
1832
+ * version: '1.0.0',
1833
+ * modalities: {
1834
+ * llm: createLLMHandler(),
1835
+ * embedding: createEmbeddingHandler(),
1836
+ * },
1837
+ * };
1838
+ * ```
710
1839
  */
711
1840
  interface CreateProviderOptions {
1841
+ /** Unique identifier for the provider */
712
1842
  name: string;
1843
+ /** Semantic version string for the provider implementation */
713
1844
  version: string;
1845
+ /** Handlers for supported modalities (LLM, embedding, image generation) */
714
1846
  modalities: {
1847
+ /** Handler for language model completions */
715
1848
  llm?: LLMHandler$1;
1849
+ /** Handler for text embeddings */
716
1850
  embedding?: EmbeddingHandler;
1851
+ /** Handler for image generation */
717
1852
  image?: ImageHandler;
718
1853
  };
719
1854
  }
720
1855
  /**
721
- * Create a provider factory function
1856
+ * Creates a provider factory function with attached modality handlers.
1857
+ *
1858
+ * The returned provider is a callable function that creates model references
1859
+ * when invoked with a model ID. It also exposes `name`, `version`, and
1860
+ * `modalities` properties for introspection.
722
1861
  *
723
1862
  * @typeParam TOptions - Provider-specific options type (defaults to unknown)
724
- * @param options - Provider configuration
725
- * @returns Provider function with modalities attached
1863
+ * @param options - Provider configuration including name, version, and handlers
1864
+ * @returns A callable Provider with modalities attached
726
1865
  *
727
1866
  * @example
728
- * ```ts
729
- * // Basic provider without options
1867
+ * ```typescript
1868
+ * // Create a basic provider
730
1869
  * const anthropic = createProvider({
731
1870
  * name: 'anthropic',
732
1871
  * version: '1.0.0',
733
1872
  * modalities: { llm: createLLMHandler() },
734
1873
  * });
735
1874
  *
736
- * // Provider with custom options (typically needs custom factory)
737
- * interface MyProviderOptions { api?: 'v1' | 'v2' }
738
- * const myProvider = createProvider<MyProviderOptions>({
1875
+ * // Use the provider to create a model reference
1876
+ * const model = anthropic('claude-sonnet-4-20250514');
1877
+ *
1878
+ * // Provider with custom options type
1879
+ * interface MyOptions { apiVersion?: 'v1' | 'v2' }
1880
+ * const myProvider = createProvider<MyOptions>({
739
1881
  * name: 'my-provider',
740
1882
  * version: '1.0.0',
741
- * modalities: { llm: createLLMHandler() },
1883
+ * modalities: { llm: handler },
742
1884
  * });
743
1885
  * ```
744
1886
  */
745
1887
  declare function createProvider<TOptions = unknown>(options: CreateProviderOptions): Provider<TOptions>;
746
1888
 
747
1889
  /**
748
- * Image class for handling images in UPP
1890
+ * @fileoverview Image content handling for the Universal Provider Protocol.
1891
+ *
1892
+ * Provides a unified Image class for working with images across different sources
1893
+ * (file paths, URLs, raw bytes, base64). Supports conversion between formats and
1894
+ * integration with UPP message content blocks.
1895
+ *
1896
+ * @module core/image
1897
+ */
1898
+
1899
+ /**
1900
+ * Represents an image that can be used in UPP messages.
1901
+ *
1902
+ * Images can be created from various sources (files, URLs, bytes, base64) and
1903
+ * converted to different formats as needed by providers. The class provides
1904
+ * a unified interface regardless of the underlying source type.
1905
+ *
1906
+ * @example
1907
+ * ```typescript
1908
+ * // Load from file
1909
+ * const fileImage = await Image.fromPath('./photo.jpg');
1910
+ *
1911
+ * // Reference by URL
1912
+ * const urlImage = Image.fromUrl('https://example.com/image.png');
1913
+ *
1914
+ * // From raw bytes
1915
+ * const bytesImage = Image.fromBytes(uint8Array, 'image/png');
1916
+ *
1917
+ * // Use in a message
1918
+ * const message = new UserMessage([image.toBlock()]);
1919
+ * ```
749
1920
  */
750
1921
  declare class Image {
1922
+ /** The underlying image source (bytes, base64, or URL) */
751
1923
  readonly source: ImageSource;
1924
+ /** MIME type of the image (e.g., 'image/jpeg', 'image/png') */
752
1925
  readonly mimeType: string;
1926
+ /** Image width in pixels, if known */
753
1927
  readonly width?: number;
1928
+ /** Image height in pixels, if known */
754
1929
  readonly height?: number;
755
1930
  private constructor();
756
1931
  /**
757
- * Check if this image has data loaded (false for URL sources)
1932
+ * Whether this image has data loaded in memory.
1933
+ *
1934
+ * Returns `false` for URL-sourced images that reference external resources.
1935
+ * These must be fetched before their data can be accessed.
758
1936
  */
759
1937
  get hasData(): boolean;
760
1938
  /**
761
- * Convert to base64 string (throws if source is URL)
1939
+ * Converts the image to a base64-encoded string.
1940
+ *
1941
+ * @returns The image data as a base64 string
1942
+ * @throws {Error} When the source is a URL (data must be fetched first)
762
1943
  */
763
1944
  toBase64(): string;
764
1945
  /**
765
- * Convert to data URL (throws if source is URL)
1946
+ * Converts the image to a data URL suitable for embedding in HTML or CSS.
1947
+ *
1948
+ * @returns A data URL in the format `data:{mimeType};base64,{data}`
1949
+ * @throws {Error} When the source is a URL (data must be fetched first)
766
1950
  */
767
1951
  toDataUrl(): string;
768
1952
  /**
769
- * Get raw bytes (throws if source is URL)
1953
+ * Gets the image data as raw bytes.
1954
+ *
1955
+ * @returns The image data as a Uint8Array
1956
+ * @throws {Error} When the source is a URL (data must be fetched first)
770
1957
  */
771
1958
  toBytes(): Uint8Array;
772
1959
  /**
773
- * Get the URL (only for URL sources)
1960
+ * Gets the URL for URL-sourced images.
1961
+ *
1962
+ * @returns The image URL
1963
+ * @throws {Error} When the source is not a URL
774
1964
  */
775
1965
  toUrl(): string;
776
1966
  /**
777
- * Convert to ImageBlock for use in messages
1967
+ * Converts this Image to an ImageBlock for use in UPP messages.
1968
+ *
1969
+ * @returns An ImageBlock that can be included in message content arrays
778
1970
  */
779
1971
  toBlock(): ImageBlock;
780
1972
  /**
781
- * Create from file path (reads file into memory)
1973
+ * Creates an Image by reading a file from disk.
1974
+ *
1975
+ * The file is read into memory as bytes. MIME type is automatically
1976
+ * detected from the file extension.
1977
+ *
1978
+ * @param path - Path to the image file
1979
+ * @returns Promise resolving to an Image with the file contents
1980
+ *
1981
+ * @example
1982
+ * ```typescript
1983
+ * const image = await Image.fromPath('./photos/vacation.jpg');
1984
+ * ```
782
1985
  */
783
1986
  static fromPath(path: string): Promise<Image>;
784
1987
  /**
785
- * Create from URL reference (does not fetch - providers handle URL conversion)
1988
+ * Creates an Image from a URL reference.
1989
+ *
1990
+ * The URL is stored as a reference and not fetched. Providers will handle
1991
+ * URL-to-data conversion if needed. MIME type is detected from the URL
1992
+ * path if not provided.
1993
+ *
1994
+ * @param url - URL pointing to the image
1995
+ * @param mimeType - Optional MIME type override
1996
+ * @returns An Image referencing the URL
1997
+ *
1998
+ * @example
1999
+ * ```typescript
2000
+ * const image = Image.fromUrl('https://example.com/logo.png');
2001
+ * ```
786
2002
  */
787
2003
  static fromUrl(url: string, mimeType?: string): Image;
788
2004
  /**
789
- * Create from raw bytes
2005
+ * Creates an Image from raw byte data.
2006
+ *
2007
+ * @param data - The image data as a Uint8Array
2008
+ * @param mimeType - The MIME type of the image
2009
+ * @returns An Image containing the byte data
2010
+ *
2011
+ * @example
2012
+ * ```typescript
2013
+ * const image = Image.fromBytes(pngData, 'image/png');
2014
+ * ```
790
2015
  */
791
2016
  static fromBytes(data: Uint8Array, mimeType: string): Image;
792
2017
  /**
793
- * Create from base64 string
2018
+ * Creates an Image from a base64-encoded string.
2019
+ *
2020
+ * @param base64 - The base64-encoded image data (without data URL prefix)
2021
+ * @param mimeType - The MIME type of the image
2022
+ * @returns An Image containing the base64 data
2023
+ *
2024
+ * @example
2025
+ * ```typescript
2026
+ * const image = Image.fromBase64(base64String, 'image/jpeg');
2027
+ * ```
794
2028
  */
795
2029
  static fromBase64(base64: string, mimeType: string): Image;
796
2030
  /**
797
- * Create from an existing ImageBlock
2031
+ * Creates an Image from an existing ImageBlock.
2032
+ *
2033
+ * Useful for converting content blocks received from providers back
2034
+ * into Image instances for further processing.
2035
+ *
2036
+ * @param block - An ImageBlock from message content
2037
+ * @returns An Image with the block's source and metadata
798
2038
  */
799
2039
  static fromBlock(block: ImageBlock): Image;
800
2040
  }
801
2041
 
802
2042
  /**
803
- * UPP namespace object
804
- * Provides ai.llm(), ai.embedding(), ai.image() style access
2043
+ * @fileoverview Unified Provider Protocol (UPP) - A unified interface for AI model inference
2044
+ *
2045
+ * UPP provides a consistent API for interacting with multiple AI providers including
2046
+ * Anthropic, OpenAI, Google, Ollama, OpenRouter, and xAI. The library handles provider-specific
2047
+ * transformations, streaming, tool execution, and error handling.
2048
+ *
2049
+ * @example Basic usage
2050
+ * ```typescript
2051
+ * import { llm, anthropic } from '@providerprotocol/ai';
2052
+ *
2053
+ * const model = llm({
2054
+ * model: anthropic('claude-sonnet-4-20250514'),
2055
+ * params: { max_tokens: 1000 }
2056
+ * });
2057
+ *
2058
+ * const turn = await model.generate('Hello!');
2059
+ * console.log(turn.response.text);
2060
+ * ```
2061
+ *
2062
+ * @example Streaming
2063
+ * ```typescript
2064
+ * for await (const event of model.stream('Tell me a story')) {
2065
+ * if (event.type === 'text') {
2066
+ * process.stdout.write(event.delta.text);
2067
+ * }
2068
+ * }
2069
+ * ```
2070
+ *
2071
+ * @module @providerprotocol/ai
2072
+ * @packageDocumentation
2073
+ */
2074
+ /** LLM instance factory for creating model-bound inference functions */
2075
+
2076
+ /**
2077
+ * UPP namespace object providing alternative import style.
2078
+ *
2079
+ * @example
2080
+ * ```typescript
2081
+ * import { ai } from '@providerprotocol/ai';
2082
+ *
2083
+ * const model = ai.llm({
2084
+ * model: openai('gpt-4o'),
2085
+ * params: { max_tokens: 1000 }
2086
+ * });
2087
+ * ```
805
2088
  */
806
2089
  declare const ai: {
2090
+ /** LLM instance factory */
807
2091
  llm: typeof llm;
808
2092
  };
809
2093
 
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 };
2094
+ 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 };