@providerprotocol/ai 0.0.34 → 0.0.36
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/README.md +546 -3
- package/dist/anthropic/index.d.ts +2 -1
- package/dist/anthropic/index.js +151 -145
- package/dist/anthropic/index.js.map +1 -1
- package/dist/cerebras/index.d.ts +392 -0
- package/dist/cerebras/index.js +648 -0
- package/dist/cerebras/index.js.map +1 -0
- package/dist/chunk-2YXFLRQ6.js +49 -0
- package/dist/chunk-2YXFLRQ6.js.map +1 -0
- package/dist/chunk-4OGB7JZA.js +157 -0
- package/dist/chunk-4OGB7JZA.js.map +1 -0
- package/dist/chunk-4RX4VQCB.js +31 -0
- package/dist/chunk-4RX4VQCB.js.map +1 -0
- package/dist/chunk-5IWHCXKN.js +30 -0
- package/dist/chunk-5IWHCXKN.js.map +1 -0
- package/dist/{chunk-3C7O2RNO.js → chunk-A2IM7PGT.js} +6 -4
- package/dist/{chunk-3C7O2RNO.js.map → chunk-A2IM7PGT.js.map} +1 -1
- package/dist/{chunk-3D6XGGVG.js → chunk-ARVM24K2.js} +2 -2
- package/dist/{chunk-4J6OFUKX.js → chunk-AY55T37A.js} +70 -162
- package/dist/chunk-AY55T37A.js.map +1 -0
- package/dist/{chunk-ILR2D5PN.js → chunk-BRP5XJ6Q.js} +2 -86
- package/dist/chunk-BRP5XJ6Q.js.map +1 -0
- package/dist/chunk-C4JP64VW.js +298 -0
- package/dist/chunk-C4JP64VW.js.map +1 -0
- package/dist/chunk-COS4ON4G.js +111 -0
- package/dist/chunk-COS4ON4G.js.map +1 -0
- package/dist/chunk-CRP6Y7NF.js +31 -0
- package/dist/chunk-CRP6Y7NF.js.map +1 -0
- package/dist/chunk-EPB3GQNL.js +118 -0
- package/dist/chunk-EPB3GQNL.js.map +1 -0
- package/dist/chunk-ETBFOLQN.js +34 -0
- package/dist/chunk-ETBFOLQN.js.map +1 -0
- package/dist/chunk-I53CI6ZZ.js +142 -0
- package/dist/chunk-I53CI6ZZ.js.map +1 -0
- package/dist/chunk-IDZOVWP3.js +29 -0
- package/dist/chunk-IDZOVWP3.js.map +1 -0
- package/dist/chunk-JA3UZALR.js +88 -0
- package/dist/chunk-JA3UZALR.js.map +1 -0
- package/dist/{chunk-WAKD3OO5.js → chunk-N5DX5JW3.js} +31 -31
- package/dist/chunk-N5DX5JW3.js.map +1 -0
- package/dist/chunk-OIEWDFQU.js +97 -0
- package/dist/chunk-OIEWDFQU.js.map +1 -0
- package/dist/{chunk-TOJCZMVU.js → chunk-PMK5LZ5Z.js} +40 -40
- package/dist/chunk-PMK5LZ5Z.js.map +1 -0
- package/dist/{chunk-6S222DHN.js → chunk-RJGTRQ47.js} +20 -1
- package/dist/chunk-RJGTRQ47.js.map +1 -0
- package/dist/chunk-UFFJDYCE.js +94 -0
- package/dist/chunk-UFFJDYCE.js.map +1 -0
- package/dist/chunk-VGKZIGVI.js +222 -0
- package/dist/chunk-VGKZIGVI.js.map +1 -0
- package/dist/{chunk-KUPF5KHT.js → chunk-Y5H7C5J4.js} +2 -2
- package/dist/{embedding-D2BYIehX.d.ts → embedding-BXA72PlJ.d.ts} +1 -1
- package/dist/google/index.d.ts +2 -1
- package/dist/google/index.js +202 -199
- package/dist/google/index.js.map +1 -1
- package/dist/groq/index.d.ts +410 -0
- package/dist/groq/index.js +649 -0
- package/dist/groq/index.js.map +1 -0
- package/dist/http/index.d.ts +3 -2
- package/dist/http/index.js +5 -4
- package/dist/image-stream-CCgwB7ve.d.ts +11 -0
- package/dist/index.d.ts +8 -118
- package/dist/index.js +520 -769
- package/dist/index.js.map +1 -1
- package/dist/{llm-BQJZj3cD.d.ts → llm-ByUFPcFH.d.ts} +12 -1632
- package/dist/middleware/logging/index.d.ts +76 -0
- package/dist/middleware/logging/index.js +74 -0
- package/dist/middleware/logging/index.js.map +1 -0
- package/dist/middleware/parsed-object/index.d.ts +45 -0
- package/dist/middleware/parsed-object/index.js +73 -0
- package/dist/middleware/parsed-object/index.js.map +1 -0
- package/dist/middleware/pubsub/index.d.ts +97 -0
- package/dist/middleware/pubsub/index.js +160 -0
- package/dist/middleware/pubsub/index.js.map +1 -0
- package/dist/middleware/pubsub/server/express/index.d.ts +66 -0
- package/dist/middleware/pubsub/server/express/index.js +11 -0
- package/dist/middleware/pubsub/server/express/index.js.map +1 -0
- package/dist/middleware/pubsub/server/fastify/index.d.ts +67 -0
- package/dist/middleware/pubsub/server/fastify/index.js +11 -0
- package/dist/middleware/pubsub/server/fastify/index.js.map +1 -0
- package/dist/middleware/pubsub/server/h3/index.d.ts +70 -0
- package/dist/middleware/pubsub/server/h3/index.js +11 -0
- package/dist/middleware/pubsub/server/h3/index.js.map +1 -0
- package/dist/middleware/pubsub/server/index.d.ts +78 -0
- package/dist/middleware/pubsub/server/index.js +34 -0
- package/dist/middleware/pubsub/server/index.js.map +1 -0
- package/dist/middleware/pubsub/server/webapi/index.d.ts +63 -0
- package/dist/middleware/pubsub/server/webapi/index.js +11 -0
- package/dist/middleware/pubsub/server/webapi/index.js.map +1 -0
- package/dist/ollama/index.d.ts +2 -1
- package/dist/ollama/index.js +48 -45
- package/dist/ollama/index.js.map +1 -1
- package/dist/openai/index.d.ts +2 -1
- package/dist/openai/index.js +319 -313
- package/dist/openai/index.js.map +1 -1
- package/dist/openrouter/index.d.ts +2 -1
- package/dist/openrouter/index.js +381 -385
- package/dist/openrouter/index.js.map +1 -1
- package/dist/proxy/index.d.ts +10 -914
- package/dist/proxy/index.js +275 -1007
- package/dist/proxy/index.js.map +1 -1
- package/dist/proxy/server/express/index.d.ts +161 -0
- package/dist/proxy/server/express/index.js +24 -0
- package/dist/proxy/server/express/index.js.map +1 -0
- package/dist/proxy/server/fastify/index.d.ts +162 -0
- package/dist/proxy/server/fastify/index.js +24 -0
- package/dist/proxy/server/fastify/index.js.map +1 -0
- package/dist/proxy/server/h3/index.d.ts +189 -0
- package/dist/proxy/server/h3/index.js +28 -0
- package/dist/proxy/server/h3/index.js.map +1 -0
- package/dist/proxy/server/index.d.ts +151 -0
- package/dist/proxy/server/index.js +48 -0
- package/dist/proxy/server/index.js.map +1 -0
- package/dist/proxy/server/webapi/index.d.ts +278 -0
- package/dist/proxy/server/webapi/index.js +32 -0
- package/dist/proxy/server/webapi/index.js.map +1 -0
- package/dist/responses/index.d.ts +650 -0
- package/dist/responses/index.js +930 -0
- package/dist/responses/index.js.map +1 -0
- package/dist/{retry-8Ch-WWgX.d.ts → retry-BDMo4AVu.d.ts} +1 -1
- package/dist/stream-S7nwQRqM.d.ts +1643 -0
- package/dist/types-CE4B7pno.d.ts +96 -0
- package/dist/utils/index.d.ts +53 -0
- package/dist/utils/index.js +7 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/xai/index.d.ts +2 -1
- package/dist/xai/index.js +310 -310
- package/dist/xai/index.js.map +1 -1
- package/package.json +82 -4
- package/dist/chunk-4J6OFUKX.js.map +0 -1
- package/dist/chunk-6S222DHN.js.map +0 -1
- package/dist/chunk-ILR2D5PN.js.map +0 -1
- package/dist/chunk-TOJCZMVU.js.map +0 -1
- package/dist/chunk-WAKD3OO5.js.map +0 -1
- /package/dist/{chunk-3D6XGGVG.js.map → chunk-ARVM24K2.js.map} +0 -0
- /package/dist/{chunk-KUPF5KHT.js.map → chunk-Y5H7C5J4.js.map} +0 -0
|
@@ -1,1633 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* @fileoverview Content block types for multimodal messages.
|
|
3
|
-
*
|
|
4
|
-
* Defines the various content block types that can be included in
|
|
5
|
-
* user and assistant messages, supporting text, images, audio, video,
|
|
6
|
-
* and arbitrary binary data.
|
|
7
|
-
*
|
|
8
|
-
* @module types/content
|
|
9
|
-
*/
|
|
10
|
-
/**
|
|
11
|
-
* Content block type constants.
|
|
12
|
-
*
|
|
13
|
-
* Use these constants instead of raw strings for type-safe content handling:
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* import { ContentBlockType } from 'upp';
|
|
18
|
-
*
|
|
19
|
-
* if (block.type === ContentBlockType.Text) {
|
|
20
|
-
* console.log(block.text);
|
|
21
|
-
* } else if (block.type === ContentBlockType.Image) {
|
|
22
|
-
* console.log(block.mimeType);
|
|
23
|
-
* }
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
declare const ContentBlockType: {
|
|
27
|
-
/** Text content */
|
|
28
|
-
readonly Text: "text";
|
|
29
|
-
/** Reasoning/thinking content from extended thinking models */
|
|
30
|
-
readonly Reasoning: "reasoning";
|
|
31
|
-
/** Image content */
|
|
32
|
-
readonly Image: "image";
|
|
33
|
-
/** Document content (PDFs, text files) */
|
|
34
|
-
readonly Document: "document";
|
|
35
|
-
/** Audio content */
|
|
36
|
-
readonly Audio: "audio";
|
|
37
|
-
/** Video content */
|
|
38
|
-
readonly Video: "video";
|
|
39
|
-
/** Binary/arbitrary data content */
|
|
40
|
-
readonly Binary: "binary";
|
|
41
|
-
};
|
|
42
|
-
/**
|
|
43
|
-
* Content block type discriminator union.
|
|
44
|
-
*
|
|
45
|
-
* This type is derived from {@link ContentBlockType} constants.
|
|
46
|
-
*/
|
|
47
|
-
type ContentBlockType = (typeof ContentBlockType)[keyof typeof ContentBlockType];
|
|
48
|
-
/**
|
|
49
|
-
* Image source type constants.
|
|
50
|
-
*
|
|
51
|
-
* @example
|
|
52
|
-
* ```typescript
|
|
53
|
-
* import { ImageSourceType } from 'upp';
|
|
54
|
-
*
|
|
55
|
-
* if (source.type === ImageSourceType.Base64) {
|
|
56
|
-
* // Handle base64 encoded image
|
|
57
|
-
* } else if (source.type === ImageSourceType.Url) {
|
|
58
|
-
* // Handle URL reference
|
|
59
|
-
* }
|
|
60
|
-
* ```
|
|
61
|
-
*/
|
|
62
|
-
declare const ImageSourceType: {
|
|
63
|
-
/** Base64-encoded image data */
|
|
64
|
-
readonly Base64: "base64";
|
|
65
|
-
/** URL reference to image */
|
|
66
|
-
readonly Url: "url";
|
|
67
|
-
/** Raw bytes image data */
|
|
68
|
-
readonly Bytes: "bytes";
|
|
69
|
-
};
|
|
70
|
-
/**
|
|
71
|
-
* Image source type discriminator union.
|
|
72
|
-
*
|
|
73
|
-
* This type is derived from {@link ImageSourceType} constants.
|
|
74
|
-
*/
|
|
75
|
-
type ImageSourceType = (typeof ImageSourceType)[keyof typeof ImageSourceType];
|
|
76
|
-
/**
|
|
77
|
-
* Image source variants for ImageBlock.
|
|
78
|
-
*
|
|
79
|
-
* Images can be provided as base64-encoded strings, URLs, or raw bytes.
|
|
80
|
-
*
|
|
81
|
-
* @example
|
|
82
|
-
* ```typescript
|
|
83
|
-
* // Base64 encoded image
|
|
84
|
-
* const base64Source: ImageSource = {
|
|
85
|
-
* type: 'base64',
|
|
86
|
-
* data: 'iVBORw0KGgo...'
|
|
87
|
-
* };
|
|
88
|
-
*
|
|
89
|
-
* // URL reference
|
|
90
|
-
* const urlSource: ImageSource = {
|
|
91
|
-
* type: 'url',
|
|
92
|
-
* url: 'https://example.com/image.png'
|
|
93
|
-
* };
|
|
94
|
-
*
|
|
95
|
-
* // Raw bytes
|
|
96
|
-
* const bytesSource: ImageSource = {
|
|
97
|
-
* type: 'bytes',
|
|
98
|
-
* data: new Uint8Array([...])
|
|
99
|
-
* };
|
|
100
|
-
* ```
|
|
101
|
-
*/
|
|
102
|
-
type ImageSource = {
|
|
103
|
-
type: 'base64';
|
|
104
|
-
data: string;
|
|
105
|
-
} | {
|
|
106
|
-
type: 'url';
|
|
107
|
-
url: string;
|
|
108
|
-
} | {
|
|
109
|
-
type: 'bytes';
|
|
110
|
-
data: Uint8Array;
|
|
111
|
-
};
|
|
112
|
-
/**
|
|
113
|
-
* Document source type constants.
|
|
114
|
-
*
|
|
115
|
-
* @example
|
|
116
|
-
* ```typescript
|
|
117
|
-
* import { DocumentSourceType } from 'upp';
|
|
118
|
-
*
|
|
119
|
-
* if (source.type === DocumentSourceType.Base64) {
|
|
120
|
-
* // Handle base64 encoded document (PDF)
|
|
121
|
-
* } else if (source.type === DocumentSourceType.Url) {
|
|
122
|
-
* // Handle URL reference (PDF)
|
|
123
|
-
* } else if (source.type === DocumentSourceType.Text) {
|
|
124
|
-
* // Handle plain text document
|
|
125
|
-
* }
|
|
126
|
-
* ```
|
|
127
|
-
*/
|
|
128
|
-
declare const DocumentSourceType: {
|
|
129
|
-
/** Base64-encoded document data (for PDFs) */
|
|
130
|
-
readonly Base64: "base64";
|
|
131
|
-
/** URL reference to document (for PDFs) */
|
|
132
|
-
readonly Url: "url";
|
|
133
|
-
/** Plain text document content */
|
|
134
|
-
readonly Text: "text";
|
|
135
|
-
};
|
|
136
|
-
/**
|
|
137
|
-
* Document source type discriminator union.
|
|
138
|
-
*
|
|
139
|
-
* This type is derived from {@link DocumentSourceType} constants.
|
|
140
|
-
*/
|
|
141
|
-
type DocumentSourceType = (typeof DocumentSourceType)[keyof typeof DocumentSourceType];
|
|
142
|
-
/**
|
|
143
|
-
* Document source variants for DocumentBlock.
|
|
144
|
-
*
|
|
145
|
-
* Documents can be provided as base64-encoded data (PDFs), URLs (PDFs), or plain text.
|
|
146
|
-
*
|
|
147
|
-
* @example
|
|
148
|
-
* ```typescript
|
|
149
|
-
* // Base64 encoded PDF
|
|
150
|
-
* const base64Source: DocumentSource = {
|
|
151
|
-
* type: 'base64',
|
|
152
|
-
* data: 'JVBERi0xLjQK...'
|
|
153
|
-
* };
|
|
154
|
-
*
|
|
155
|
-
* // URL reference to PDF
|
|
156
|
-
* const urlSource: DocumentSource = {
|
|
157
|
-
* type: 'url',
|
|
158
|
-
* url: 'https://example.com/document.pdf'
|
|
159
|
-
* };
|
|
160
|
-
*
|
|
161
|
-
* // Plain text document
|
|
162
|
-
* const textSource: DocumentSource = {
|
|
163
|
-
* type: 'text',
|
|
164
|
-
* data: 'This is the document content...'
|
|
165
|
-
* };
|
|
166
|
-
* ```
|
|
167
|
-
*/
|
|
168
|
-
type DocumentSource = {
|
|
169
|
-
type: 'base64';
|
|
170
|
-
data: string;
|
|
171
|
-
} | {
|
|
172
|
-
type: 'url';
|
|
173
|
-
url: string;
|
|
174
|
-
} | {
|
|
175
|
-
type: 'text';
|
|
176
|
-
data: string;
|
|
177
|
-
};
|
|
178
|
-
/**
|
|
179
|
-
* Text content block.
|
|
180
|
-
*
|
|
181
|
-
* The most common content block type, containing plain text content.
|
|
182
|
-
*
|
|
183
|
-
* @example
|
|
184
|
-
* ```typescript
|
|
185
|
-
* const textBlock: TextBlock = {
|
|
186
|
-
* type: 'text',
|
|
187
|
-
* text: 'Hello, world!'
|
|
188
|
-
* };
|
|
189
|
-
* ```
|
|
190
|
-
*/
|
|
191
|
-
interface TextBlock {
|
|
192
|
-
/** Discriminator for text blocks */
|
|
193
|
-
type: 'text';
|
|
194
|
-
/** The text content */
|
|
195
|
-
text: string;
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Reasoning content block.
|
|
199
|
-
*
|
|
200
|
-
* Contains model reasoning/thinking from extended thinking or chain-of-thought.
|
|
201
|
-
* This content represents the model's internal reasoning process.
|
|
202
|
-
*
|
|
203
|
-
* @example
|
|
204
|
-
* ```typescript
|
|
205
|
-
* const reasoningBlock: ReasoningBlock = {
|
|
206
|
-
* type: 'reasoning',
|
|
207
|
-
* text: 'Let me think about this step by step...'
|
|
208
|
-
* };
|
|
209
|
-
* ```
|
|
210
|
-
*/
|
|
211
|
-
interface ReasoningBlock {
|
|
212
|
-
/** Discriminator for reasoning blocks */
|
|
213
|
-
type: 'reasoning';
|
|
214
|
-
/** The reasoning/thinking text */
|
|
215
|
-
text: string;
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* Image content block.
|
|
219
|
-
*
|
|
220
|
-
* Contains an image with its source data and metadata.
|
|
221
|
-
*
|
|
222
|
-
* @example
|
|
223
|
-
* ```typescript
|
|
224
|
-
* const imageBlock: ImageBlock = {
|
|
225
|
-
* type: 'image',
|
|
226
|
-
* source: { type: 'url', url: 'https://example.com/photo.jpg' },
|
|
227
|
-
* mimeType: 'image/jpeg',
|
|
228
|
-
* width: 1920,
|
|
229
|
-
* height: 1080
|
|
230
|
-
* };
|
|
231
|
-
* ```
|
|
232
|
-
*/
|
|
233
|
-
interface ImageBlock {
|
|
234
|
-
/** Discriminator for image blocks */
|
|
235
|
-
type: 'image';
|
|
236
|
-
/** The image data source */
|
|
237
|
-
source: ImageSource;
|
|
238
|
-
/** MIME type of the image (e.g., 'image/png', 'image/jpeg') */
|
|
239
|
-
mimeType: string;
|
|
240
|
-
/** Image width in pixels */
|
|
241
|
-
width?: number;
|
|
242
|
-
/** Image height in pixels */
|
|
243
|
-
height?: number;
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Document content block.
|
|
247
|
-
*
|
|
248
|
-
* Contains a document (PDF or plain text) with its source and metadata.
|
|
249
|
-
* Supports PDF documents via base64 encoding or URL, and plain text content.
|
|
250
|
-
*
|
|
251
|
-
* @example
|
|
252
|
-
* ```typescript
|
|
253
|
-
* // PDF document from base64
|
|
254
|
-
* const pdfBlock: DocumentBlock = {
|
|
255
|
-
* type: 'document',
|
|
256
|
-
* source: { type: 'base64', data: 'JVBERi0xLjQK...' },
|
|
257
|
-
* mimeType: 'application/pdf',
|
|
258
|
-
* title: 'Annual Report'
|
|
259
|
-
* };
|
|
260
|
-
*
|
|
261
|
-
* // Plain text document
|
|
262
|
-
* const textDoc: DocumentBlock = {
|
|
263
|
-
* type: 'document',
|
|
264
|
-
* source: { type: 'text', data: 'Document contents here...' },
|
|
265
|
-
* mimeType: 'text/plain'
|
|
266
|
-
* };
|
|
267
|
-
* ```
|
|
268
|
-
*/
|
|
269
|
-
interface DocumentBlock {
|
|
270
|
-
/** Discriminator for document blocks */
|
|
271
|
-
type: 'document';
|
|
272
|
-
/** The document data source */
|
|
273
|
-
source: DocumentSource;
|
|
274
|
-
/** MIME type of the document ('application/pdf' or 'text/plain') */
|
|
275
|
-
mimeType: string;
|
|
276
|
-
/** Optional document title (used for citations) */
|
|
277
|
-
title?: string;
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Audio content block.
|
|
281
|
-
*
|
|
282
|
-
* Contains audio data with its metadata.
|
|
283
|
-
*
|
|
284
|
-
* @example
|
|
285
|
-
* ```typescript
|
|
286
|
-
* const audioBlock: AudioBlock = {
|
|
287
|
-
* type: 'audio',
|
|
288
|
-
* data: audioBytes,
|
|
289
|
-
* mimeType: 'audio/mp3',
|
|
290
|
-
* duration: 120.5
|
|
291
|
-
* };
|
|
292
|
-
* ```
|
|
293
|
-
*/
|
|
294
|
-
interface AudioBlock {
|
|
295
|
-
/** Discriminator for audio blocks */
|
|
296
|
-
type: 'audio';
|
|
297
|
-
/** Raw audio data */
|
|
298
|
-
data: Uint8Array;
|
|
299
|
-
/** MIME type of the audio (e.g., 'audio/mp3', 'audio/wav') */
|
|
300
|
-
mimeType: string;
|
|
301
|
-
/** Duration in seconds */
|
|
302
|
-
duration?: number;
|
|
303
|
-
}
|
|
304
|
-
/**
|
|
305
|
-
* Video content block.
|
|
306
|
-
*
|
|
307
|
-
* Contains video data with its metadata.
|
|
308
|
-
*
|
|
309
|
-
* @example
|
|
310
|
-
* ```typescript
|
|
311
|
-
* const videoBlock: VideoBlock = {
|
|
312
|
-
* type: 'video',
|
|
313
|
-
* data: videoBytes,
|
|
314
|
-
* mimeType: 'video/mp4',
|
|
315
|
-
* duration: 30,
|
|
316
|
-
* width: 1920,
|
|
317
|
-
* height: 1080
|
|
318
|
-
* };
|
|
319
|
-
* ```
|
|
320
|
-
*/
|
|
321
|
-
interface VideoBlock {
|
|
322
|
-
/** Discriminator for video blocks */
|
|
323
|
-
type: 'video';
|
|
324
|
-
/** Raw video data */
|
|
325
|
-
data: Uint8Array;
|
|
326
|
-
/** MIME type of the video (e.g., 'video/mp4', 'video/webm') */
|
|
327
|
-
mimeType: string;
|
|
328
|
-
/** Duration in seconds */
|
|
329
|
-
duration?: number;
|
|
330
|
-
/** Video width in pixels */
|
|
331
|
-
width?: number;
|
|
332
|
-
/** Video height in pixels */
|
|
333
|
-
height?: number;
|
|
334
|
-
}
|
|
335
|
-
/**
|
|
336
|
-
* Binary content block for arbitrary data.
|
|
337
|
-
*
|
|
338
|
-
* A generic block type for data that doesn't fit other categories.
|
|
339
|
-
*
|
|
340
|
-
* @example
|
|
341
|
-
* ```typescript
|
|
342
|
-
* const binaryBlock: BinaryBlock = {
|
|
343
|
-
* type: 'binary',
|
|
344
|
-
* data: pdfBytes,
|
|
345
|
-
* mimeType: 'application/pdf',
|
|
346
|
-
* metadata: { filename: 'document.pdf', pages: 10 }
|
|
347
|
-
* };
|
|
348
|
-
* ```
|
|
349
|
-
*/
|
|
350
|
-
interface BinaryBlock {
|
|
351
|
-
/** Discriminator for binary blocks */
|
|
352
|
-
type: 'binary';
|
|
353
|
-
/** Raw binary data */
|
|
354
|
-
data: Uint8Array;
|
|
355
|
-
/** MIME type of the data */
|
|
356
|
-
mimeType: string;
|
|
357
|
-
/** Additional metadata about the binary content */
|
|
358
|
-
metadata?: Record<string, unknown>;
|
|
359
|
-
}
|
|
360
|
-
/**
|
|
361
|
-
* Union of all content block types.
|
|
362
|
-
*
|
|
363
|
-
* Used when a function or property can accept any type of content block.
|
|
364
|
-
*/
|
|
365
|
-
type ContentBlock = TextBlock | ReasoningBlock | ImageBlock | DocumentBlock | AudioBlock | VideoBlock | BinaryBlock;
|
|
366
|
-
/**
|
|
367
|
-
* Content types allowed in user messages.
|
|
368
|
-
*
|
|
369
|
-
* Users can send any type of content block including binary data.
|
|
370
|
-
*/
|
|
371
|
-
type UserContent = TextBlock | ImageBlock | DocumentBlock | AudioBlock | VideoBlock | BinaryBlock;
|
|
372
|
-
/**
|
|
373
|
-
* Content types allowed in assistant messages.
|
|
374
|
-
*
|
|
375
|
-
* Assistants can generate text and media but not arbitrary binary data.
|
|
376
|
-
*/
|
|
377
|
-
type AssistantContent = TextBlock | ReasoningBlock | ImageBlock | AudioBlock | VideoBlock;
|
|
378
|
-
/**
|
|
379
|
-
* Creates a text content block from a string.
|
|
380
|
-
*
|
|
381
|
-
* @param content - The text content
|
|
382
|
-
* @returns A TextBlock containing the provided text
|
|
383
|
-
*
|
|
384
|
-
* @example
|
|
385
|
-
* ```typescript
|
|
386
|
-
* const block = text('Hello, world!');
|
|
387
|
-
* // { type: 'text', text: 'Hello, world!' }
|
|
388
|
-
* ```
|
|
389
|
-
*/
|
|
390
|
-
declare function text(content: string): TextBlock;
|
|
391
|
-
/**
|
|
392
|
-
* Creates a reasoning content block from a string.
|
|
393
|
-
*
|
|
394
|
-
* @param content - The reasoning/thinking content
|
|
395
|
-
* @returns A ReasoningBlock containing the provided text
|
|
396
|
-
*
|
|
397
|
-
* @example
|
|
398
|
-
* ```typescript
|
|
399
|
-
* const block = reasoning('Let me think step by step...');
|
|
400
|
-
* // { type: 'reasoning', text: 'Let me think step by step...' }
|
|
401
|
-
* ```
|
|
402
|
-
*/
|
|
403
|
-
declare function reasoning(content: string): ReasoningBlock;
|
|
404
|
-
/**
|
|
405
|
-
* Type guard for TextBlock.
|
|
406
|
-
*
|
|
407
|
-
* @param block - The content block to check
|
|
408
|
-
* @returns True if the block is a TextBlock
|
|
409
|
-
*
|
|
410
|
-
* @example
|
|
411
|
-
* ```typescript
|
|
412
|
-
* if (isTextBlock(block)) {
|
|
413
|
-
* console.log(block.text);
|
|
414
|
-
* }
|
|
415
|
-
* ```
|
|
416
|
-
*/
|
|
417
|
-
declare function isTextBlock(block: ContentBlock): block is TextBlock;
|
|
418
|
-
/**
|
|
419
|
-
* Type guard for ReasoningBlock.
|
|
420
|
-
*
|
|
421
|
-
* @param block - The content block to check
|
|
422
|
-
* @returns True if the block is a ReasoningBlock
|
|
423
|
-
*
|
|
424
|
-
* @example
|
|
425
|
-
* ```typescript
|
|
426
|
-
* if (isReasoningBlock(block)) {
|
|
427
|
-
* console.log(block.text);
|
|
428
|
-
* }
|
|
429
|
-
* ```
|
|
430
|
-
*/
|
|
431
|
-
declare function isReasoningBlock(block: ContentBlock): block is ReasoningBlock;
|
|
432
|
-
/**
|
|
433
|
-
* Type guard for ImageBlock.
|
|
434
|
-
*
|
|
435
|
-
* @param block - The content block to check
|
|
436
|
-
* @returns True if the block is an ImageBlock
|
|
437
|
-
*
|
|
438
|
-
* @example
|
|
439
|
-
* ```typescript
|
|
440
|
-
* if (isImageBlock(block)) {
|
|
441
|
-
* console.log(block.mimeType, block.width, block.height);
|
|
442
|
-
* }
|
|
443
|
-
* ```
|
|
444
|
-
*/
|
|
445
|
-
declare function isImageBlock(block: ContentBlock): block is ImageBlock;
|
|
446
|
-
/**
|
|
447
|
-
* Type guard for DocumentBlock.
|
|
448
|
-
*
|
|
449
|
-
* @param block - The content block to check
|
|
450
|
-
* @returns True if the block is a DocumentBlock
|
|
451
|
-
*
|
|
452
|
-
* @example
|
|
453
|
-
* ```typescript
|
|
454
|
-
* if (isDocumentBlock(block)) {
|
|
455
|
-
* console.log(block.mimeType, block.title);
|
|
456
|
-
* }
|
|
457
|
-
* ```
|
|
458
|
-
*/
|
|
459
|
-
declare function isDocumentBlock(block: ContentBlock): block is DocumentBlock;
|
|
460
|
-
/**
|
|
461
|
-
* Type guard for AudioBlock.
|
|
462
|
-
*
|
|
463
|
-
* @param block - The content block to check
|
|
464
|
-
* @returns True if the block is an AudioBlock
|
|
465
|
-
*
|
|
466
|
-
* @example
|
|
467
|
-
* ```typescript
|
|
468
|
-
* if (isAudioBlock(block)) {
|
|
469
|
-
* console.log(block.mimeType, block.duration);
|
|
470
|
-
* }
|
|
471
|
-
* ```
|
|
472
|
-
*/
|
|
473
|
-
declare function isAudioBlock(block: ContentBlock): block is AudioBlock;
|
|
474
|
-
/**
|
|
475
|
-
* Type guard for VideoBlock.
|
|
476
|
-
*
|
|
477
|
-
* @param block - The content block to check
|
|
478
|
-
* @returns True if the block is a VideoBlock
|
|
479
|
-
*
|
|
480
|
-
* @example
|
|
481
|
-
* ```typescript
|
|
482
|
-
* if (isVideoBlock(block)) {
|
|
483
|
-
* console.log(block.mimeType, block.duration);
|
|
484
|
-
* }
|
|
485
|
-
* ```
|
|
486
|
-
*/
|
|
487
|
-
declare function isVideoBlock(block: ContentBlock): block is VideoBlock;
|
|
488
|
-
/**
|
|
489
|
-
* Type guard for BinaryBlock.
|
|
490
|
-
*
|
|
491
|
-
* @param block - The content block to check
|
|
492
|
-
* @returns True if the block is a BinaryBlock
|
|
493
|
-
*
|
|
494
|
-
* @example
|
|
495
|
-
* ```typescript
|
|
496
|
-
* if (isBinaryBlock(block)) {
|
|
497
|
-
* console.log(block.mimeType, block.metadata);
|
|
498
|
-
* }
|
|
499
|
-
* ```
|
|
500
|
-
*/
|
|
501
|
-
declare function isBinaryBlock(block: ContentBlock): block is BinaryBlock;
|
|
502
|
-
|
|
503
|
-
/**
|
|
504
|
-
* @fileoverview JSON Schema types for tool parameters and structured outputs.
|
|
505
|
-
*
|
|
506
|
-
* Provides TypeScript interfaces for defining JSON Schema objects used in
|
|
507
|
-
* LLM tool definitions and structured output specifications.
|
|
508
|
-
*
|
|
509
|
-
* @module types/schema
|
|
510
|
-
*/
|
|
511
|
-
/**
|
|
512
|
-
* Primitive and composite JSON Schema property types.
|
|
513
|
-
*
|
|
514
|
-
* These types correspond to the JSON Schema specification's allowed type values.
|
|
515
|
-
*/
|
|
516
|
-
type JSONSchemaPropertyType =
|
|
517
|
-
/** String values */
|
|
518
|
-
'string'
|
|
519
|
-
/** Floating point numbers */
|
|
520
|
-
| 'number'
|
|
521
|
-
/** Whole numbers */
|
|
522
|
-
| 'integer'
|
|
523
|
-
/** Boolean true/false values */
|
|
524
|
-
| 'boolean'
|
|
525
|
-
/** Ordered lists of values */
|
|
526
|
-
| 'array'
|
|
527
|
-
/** Key-value mappings */
|
|
528
|
-
| 'object'
|
|
529
|
-
/** Explicit null value */
|
|
530
|
-
| 'null';
|
|
531
|
-
/**
|
|
532
|
-
* JSON Schema property definition.
|
|
533
|
-
*
|
|
534
|
-
* Describes a single property within a JSON Schema object, including
|
|
535
|
-
* type constraints, validation rules, and nested structure definitions.
|
|
536
|
-
*
|
|
537
|
-
* @example
|
|
538
|
-
* ```typescript
|
|
539
|
-
* const nameProperty: JSONSchemaProperty = {
|
|
540
|
-
* type: 'string',
|
|
541
|
-
* description: 'User name',
|
|
542
|
-
* minLength: 1,
|
|
543
|
-
* maxLength: 100
|
|
544
|
-
* };
|
|
545
|
-
* ```
|
|
546
|
-
*
|
|
547
|
-
* @example
|
|
548
|
-
* ```typescript
|
|
549
|
-
* const tagsProperty: JSONSchemaProperty = {
|
|
550
|
-
* type: 'array',
|
|
551
|
-
* description: 'List of tags',
|
|
552
|
-
* items: { type: 'string' },
|
|
553
|
-
* minItems: 1,
|
|
554
|
-
* uniqueItems: true
|
|
555
|
-
* };
|
|
556
|
-
* ```
|
|
557
|
-
*/
|
|
558
|
-
interface JSONSchemaProperty {
|
|
559
|
-
/** The JSON type of this property */
|
|
560
|
-
type: JSONSchemaPropertyType;
|
|
561
|
-
/** Human-readable description for the LLM */
|
|
562
|
-
description?: string;
|
|
563
|
-
/** Allowed values (enumeration) */
|
|
564
|
-
enum?: unknown[];
|
|
565
|
-
/** Constant value this property must equal */
|
|
566
|
-
const?: unknown;
|
|
567
|
-
/** Default value if not provided */
|
|
568
|
-
default?: unknown;
|
|
569
|
-
/** Minimum string length (string type only) */
|
|
570
|
-
minLength?: number;
|
|
571
|
-
/** Maximum string length (string type only) */
|
|
572
|
-
maxLength?: number;
|
|
573
|
-
/** Regular expression pattern for validation (string type only) */
|
|
574
|
-
pattern?: string;
|
|
575
|
-
/** Semantic format hint (string type only) */
|
|
576
|
-
format?: 'email' | 'uri' | 'date' | 'date-time' | 'uuid';
|
|
577
|
-
/** Minimum value inclusive (number/integer types only) */
|
|
578
|
-
minimum?: number;
|
|
579
|
-
/** Maximum value inclusive (number/integer types only) */
|
|
580
|
-
maximum?: number;
|
|
581
|
-
/** Minimum value exclusive (number/integer types only) */
|
|
582
|
-
exclusiveMinimum?: number;
|
|
583
|
-
/** Maximum value exclusive (number/integer types only) */
|
|
584
|
-
exclusiveMaximum?: number;
|
|
585
|
-
/** Value must be divisible by this (number/integer types only) */
|
|
586
|
-
multipleOf?: number;
|
|
587
|
-
/** Schema for array elements (array type only) */
|
|
588
|
-
items?: JSONSchemaProperty;
|
|
589
|
-
/** Minimum array length (array type only) */
|
|
590
|
-
minItems?: number;
|
|
591
|
-
/** Maximum array length (array type only) */
|
|
592
|
-
maxItems?: number;
|
|
593
|
-
/** Whether array elements must be unique (array type only) */
|
|
594
|
-
uniqueItems?: boolean;
|
|
595
|
-
/** Nested property definitions (object type only) */
|
|
596
|
-
properties?: Record<string, JSONSchemaProperty>;
|
|
597
|
-
/** List of required property names (object type only) */
|
|
598
|
-
required?: string[];
|
|
599
|
-
/** Whether additional properties are allowed (object type only) */
|
|
600
|
-
additionalProperties?: boolean;
|
|
601
|
-
}
|
|
602
|
-
/**
|
|
603
|
-
* Root JSON Schema for tool parameters or structured outputs.
|
|
604
|
-
*
|
|
605
|
-
* This is the top-level schema definition used when defining tool
|
|
606
|
-
* parameters or requesting structured output from an LLM.
|
|
607
|
-
*
|
|
608
|
-
* @example
|
|
609
|
-
* ```typescript
|
|
610
|
-
* const weatherToolSchema: JSONSchema = {
|
|
611
|
-
* type: 'object',
|
|
612
|
-
* description: 'Parameters for getting weather information',
|
|
613
|
-
* properties: {
|
|
614
|
-
* location: {
|
|
615
|
-
* type: 'string',
|
|
616
|
-
* description: 'City name or coordinates'
|
|
617
|
-
* },
|
|
618
|
-
* units: {
|
|
619
|
-
* type: 'string',
|
|
620
|
-
* enum: ['celsius', 'fahrenheit'],
|
|
621
|
-
* description: 'Temperature units'
|
|
622
|
-
* }
|
|
623
|
-
* },
|
|
624
|
-
* required: ['location']
|
|
625
|
-
* };
|
|
626
|
-
* ```
|
|
627
|
-
*/
|
|
628
|
-
interface JSONSchema {
|
|
629
|
-
/** Root schemas are always objects */
|
|
630
|
-
type: 'object';
|
|
631
|
-
/** Property definitions for the object */
|
|
632
|
-
properties: Record<string, JSONSchemaProperty>;
|
|
633
|
-
/** List of required property names */
|
|
634
|
-
required?: string[];
|
|
635
|
-
/** Whether additional properties are allowed beyond those defined */
|
|
636
|
-
additionalProperties?: boolean;
|
|
637
|
-
/** Human-readable description of the schema's purpose */
|
|
638
|
-
description?: string;
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
/**
|
|
642
|
-
* @fileoverview Tool types for LLM function calling.
|
|
643
|
-
*
|
|
644
|
-
* Defines the interfaces for registering tools with LLMs, handling
|
|
645
|
-
* tool calls from the model, and managing tool execution strategies.
|
|
646
|
-
*
|
|
647
|
-
* @module types/tool
|
|
648
|
-
*/
|
|
649
|
-
|
|
650
|
-
/**
|
|
651
|
-
* Provider-namespaced metadata for tools.
|
|
652
|
-
*
|
|
653
|
-
* Each provider can attach its own metadata under its namespace,
|
|
654
|
-
* enabling provider-specific features like caching, strict mode, etc.
|
|
655
|
-
*
|
|
656
|
-
* @example
|
|
657
|
-
* ```typescript
|
|
658
|
-
* const metadata: ToolMetadata = {
|
|
659
|
-
* anthropic: { cache_control: { type: 'ephemeral' } },
|
|
660
|
-
* openrouter: { cache_control: { type: 'ephemeral', ttl: '1h' } }
|
|
661
|
-
* };
|
|
662
|
-
* ```
|
|
663
|
-
*/
|
|
664
|
-
interface ToolMetadata {
|
|
665
|
-
[provider: string]: Record<string, unknown> | undefined;
|
|
666
|
-
}
|
|
667
|
-
/**
|
|
668
|
-
* Tool call requested by the model.
|
|
669
|
-
*
|
|
670
|
-
* Represents a single function call request from the LLM, including
|
|
671
|
-
* the tool name and parsed arguments.
|
|
672
|
-
*
|
|
673
|
-
* @example
|
|
674
|
-
* ```typescript
|
|
675
|
-
* const toolCall: ToolCall = {
|
|
676
|
-
* toolCallId: 'call_abc123',
|
|
677
|
-
* toolName: 'get_weather',
|
|
678
|
-
* arguments: { location: 'San Francisco', units: 'celsius' }
|
|
679
|
-
* };
|
|
680
|
-
* ```
|
|
681
|
-
*/
|
|
682
|
-
interface ToolCall {
|
|
683
|
-
/** Unique identifier for this tool call, used to match results */
|
|
684
|
-
toolCallId: string;
|
|
685
|
-
/** Name of the tool being called */
|
|
686
|
-
toolName: string;
|
|
687
|
-
/** Parsed arguments for the tool call */
|
|
688
|
-
arguments: Record<string, unknown>;
|
|
689
|
-
}
|
|
690
|
-
/**
|
|
691
|
-
* Result of tool execution.
|
|
692
|
-
*
|
|
693
|
-
* Returned after executing a tool, containing the result data
|
|
694
|
-
* and whether an error occurred.
|
|
695
|
-
*
|
|
696
|
-
* @example
|
|
697
|
-
* ```typescript
|
|
698
|
-
* const result: ToolResult = {
|
|
699
|
-
* toolCallId: 'call_abc123',
|
|
700
|
-
* result: { temperature: 72, conditions: 'sunny' }
|
|
701
|
-
* };
|
|
702
|
-
*
|
|
703
|
-
* // Error result
|
|
704
|
-
* const errorResult: ToolResult = {
|
|
705
|
-
* toolCallId: 'call_abc123',
|
|
706
|
-
* result: 'Location not found',
|
|
707
|
-
* isError: true
|
|
708
|
-
* };
|
|
709
|
-
* ```
|
|
710
|
-
*/
|
|
711
|
-
interface ToolResult {
|
|
712
|
-
/** The tool call ID this result corresponds to */
|
|
713
|
-
toolCallId: string;
|
|
714
|
-
/** The result data (can be any serializable value) */
|
|
715
|
-
result: unknown;
|
|
716
|
-
/** Whether the tool execution resulted in an error */
|
|
717
|
-
isError?: boolean;
|
|
718
|
-
}
|
|
719
|
-
/**
|
|
720
|
-
* Tool definition for LLM function calling.
|
|
721
|
-
*
|
|
722
|
-
* Defines a tool that can be called by the LLM, including its
|
|
723
|
-
* name, description, parameter schema, and execution function.
|
|
724
|
-
*
|
|
725
|
-
* @typeParam TParams - The type of parameters the tool accepts
|
|
726
|
-
* @typeParam TResult - The type of result the tool returns
|
|
727
|
-
*
|
|
728
|
-
* @example
|
|
729
|
-
* ```typescript
|
|
730
|
-
* const weatherTool: Tool<{ location: string }, WeatherData> = {
|
|
731
|
-
* name: 'get_weather',
|
|
732
|
-
* description: 'Get current weather for a location',
|
|
733
|
-
* parameters: {
|
|
734
|
-
* type: 'object',
|
|
735
|
-
* properties: {
|
|
736
|
-
* location: { type: 'string', description: 'City name' }
|
|
737
|
-
* },
|
|
738
|
-
* required: ['location']
|
|
739
|
-
* },
|
|
740
|
-
* run: async (params) => {
|
|
741
|
-
* return fetchWeather(params.location);
|
|
742
|
-
* }
|
|
743
|
-
* };
|
|
744
|
-
* ```
|
|
745
|
-
*/
|
|
746
|
-
interface Tool<TParams = unknown, TResult = unknown> {
|
|
747
|
-
/** Tool name (must be unique within an llm() instance) */
|
|
748
|
-
name: string;
|
|
749
|
-
/** Human-readable description for the model to understand when to use this tool */
|
|
750
|
-
description: string;
|
|
751
|
-
/** JSON Schema defining the tool's parameters */
|
|
752
|
-
parameters: JSONSchema;
|
|
753
|
-
/**
|
|
754
|
-
* Provider-specific metadata, namespaced by provider name.
|
|
755
|
-
*
|
|
756
|
-
* Used for provider-specific features like prompt caching:
|
|
757
|
-
* @example
|
|
758
|
-
* ```typescript
|
|
759
|
-
* const tool: Tool = {
|
|
760
|
-
* name: 'search_docs',
|
|
761
|
-
* description: 'Search documentation',
|
|
762
|
-
* parameters: {...},
|
|
763
|
-
* run: async (params) => {...},
|
|
764
|
-
* metadata: {
|
|
765
|
-
* anthropic: { cache_control: { type: 'ephemeral' } }
|
|
766
|
-
* }
|
|
767
|
-
* };
|
|
768
|
-
* ```
|
|
769
|
-
*/
|
|
770
|
-
metadata?: ToolMetadata;
|
|
771
|
-
/**
|
|
772
|
-
* Executes the tool with the provided parameters.
|
|
773
|
-
*
|
|
774
|
-
* @param params - The parameters passed by the model
|
|
775
|
-
* @returns The tool result, synchronously or as a Promise
|
|
776
|
-
*/
|
|
777
|
-
run(params: TParams): TResult | Promise<TResult>;
|
|
778
|
-
/**
|
|
779
|
-
* Optional approval handler for sensitive operations.
|
|
780
|
-
*
|
|
781
|
-
* If provided, this function is called before the tool executes.
|
|
782
|
-
* Return false to prevent execution.
|
|
783
|
-
*
|
|
784
|
-
* @param params - The parameters the tool would be called with
|
|
785
|
-
* @returns Whether to approve the execution
|
|
786
|
-
*/
|
|
787
|
-
approval?(params: TParams): boolean | Promise<boolean>;
|
|
788
|
-
}
|
|
789
|
-
/**
|
|
790
|
-
* Result from onBeforeCall hook indicating whether to proceed and optionally transformed params.
|
|
791
|
-
*/
|
|
792
|
-
interface BeforeCallResult {
|
|
793
|
-
/** Whether to proceed with tool execution */
|
|
794
|
-
proceed: boolean;
|
|
795
|
-
/** Transformed parameters to use instead of the original (optional) */
|
|
796
|
-
params?: unknown;
|
|
797
|
-
}
|
|
798
|
-
/**
|
|
799
|
-
* Result from onAfterCall hook optionally containing a transformed result.
|
|
800
|
-
*/
|
|
801
|
-
interface AfterCallResult {
|
|
802
|
-
/** Transformed result to use instead of the original */
|
|
803
|
-
result: unknown;
|
|
804
|
-
}
|
|
805
|
-
/**
|
|
806
|
-
* Strategy for controlling tool execution behavior.
|
|
807
|
-
*
|
|
808
|
-
* Provides hooks for monitoring, controlling, and transforming the tool execution
|
|
809
|
-
* loop during LLM inference.
|
|
810
|
-
*
|
|
811
|
-
* @example
|
|
812
|
-
* ```typescript
|
|
813
|
-
* const strategy: ToolUseStrategy = {
|
|
814
|
-
* maxIterations: 5,
|
|
815
|
-
* onToolCall: (tool, params) => {
|
|
816
|
-
* console.log(`Calling ${tool.name} with`, params);
|
|
817
|
-
* },
|
|
818
|
-
* // Transform input parameters
|
|
819
|
-
* onBeforeCall: (tool, params) => {
|
|
820
|
-
* if (tool.name === 'search') {
|
|
821
|
-
* return { proceed: true, params: { ...params, limit: 10 } };
|
|
822
|
-
* }
|
|
823
|
-
* return true;
|
|
824
|
-
* },
|
|
825
|
-
* // Transform output results
|
|
826
|
-
* onAfterCall: (tool, params, result) => {
|
|
827
|
-
* if (tool.name === 'fetch_data') {
|
|
828
|
-
* return { result: sanitize(result) };
|
|
829
|
-
* }
|
|
830
|
-
* },
|
|
831
|
-
* onMaxIterations: (iterations) => {
|
|
832
|
-
* console.warn(`Reached max iterations: ${iterations}`);
|
|
833
|
-
* }
|
|
834
|
-
* };
|
|
835
|
-
* ```
|
|
836
|
-
*/
|
|
837
|
-
interface ToolUseStrategy {
|
|
838
|
-
/** Maximum number of tool execution rounds (default: 10) */
|
|
839
|
-
maxIterations?: number;
|
|
840
|
-
/**
|
|
841
|
-
* Called when the model requests a tool call.
|
|
842
|
-
*
|
|
843
|
-
* @param tool - The tool being called
|
|
844
|
-
* @param params - The parameters for the call
|
|
845
|
-
*/
|
|
846
|
-
onToolCall?(tool: Tool, params: unknown): void | Promise<void>;
|
|
847
|
-
/**
|
|
848
|
-
* Called before tool execution. Can skip execution or transform parameters.
|
|
849
|
-
*
|
|
850
|
-
* @param tool - The tool about to be executed
|
|
851
|
-
* @param params - The parameters for the call
|
|
852
|
-
* @returns One of:
|
|
853
|
-
* - `false` to skip execution
|
|
854
|
-
* - `true` to proceed with original params
|
|
855
|
-
* - `BeforeCallResult` object to control execution and optionally transform params
|
|
856
|
-
*/
|
|
857
|
-
onBeforeCall?(tool: Tool, params: unknown): boolean | BeforeCallResult | Promise<boolean | BeforeCallResult>;
|
|
858
|
-
/**
|
|
859
|
-
* Called after tool execution completes. Can transform the result.
|
|
860
|
-
*
|
|
861
|
-
* @param tool - The tool that was executed
|
|
862
|
-
* @param params - The parameters that were used
|
|
863
|
-
* @param result - The result from the tool
|
|
864
|
-
* @returns Void to use original result, or `AfterCallResult` to transform it
|
|
865
|
-
*/
|
|
866
|
-
onAfterCall?(tool: Tool, params: unknown, result: unknown): void | AfterCallResult | Promise<void | AfterCallResult>;
|
|
867
|
-
/**
|
|
868
|
-
* Called when a tool execution throws an error.
|
|
869
|
-
*
|
|
870
|
-
* @param tool - The tool that failed
|
|
871
|
-
* @param params - The parameters that were used
|
|
872
|
-
* @param error - The error that was thrown
|
|
873
|
-
*/
|
|
874
|
-
onError?(tool: Tool, params: unknown, error: Error): void | Promise<void>;
|
|
875
|
-
/**
|
|
876
|
-
* Called when the maximum iteration limit is reached.
|
|
877
|
-
*
|
|
878
|
-
* @param iterations - The number of iterations that were performed
|
|
879
|
-
*/
|
|
880
|
-
onMaxIterations?(iterations: number): void | Promise<void>;
|
|
881
|
-
}
|
|
882
|
-
/**
|
|
883
|
-
* Record of a completed tool execution.
|
|
884
|
-
*
|
|
885
|
-
* Contains all information about a tool call that was executed,
|
|
886
|
-
* including timing and result data.
|
|
887
|
-
*
|
|
888
|
-
* @example
|
|
889
|
-
* ```typescript
|
|
890
|
-
* const execution: ToolExecution = {
|
|
891
|
-
* toolName: 'get_weather',
|
|
892
|
-
* toolCallId: 'call_abc123',
|
|
893
|
-
* arguments: { location: 'San Francisco' },
|
|
894
|
-
* result: { temperature: 72 },
|
|
895
|
-
* isError: false,
|
|
896
|
-
* duration: 150,
|
|
897
|
-
* approved: true
|
|
898
|
-
* };
|
|
899
|
-
* ```
|
|
900
|
-
*/
|
|
901
|
-
interface ToolExecution {
|
|
902
|
-
/** Name of the tool that was called */
|
|
903
|
-
toolName: string;
|
|
904
|
-
/** Unique identifier for this tool call */
|
|
905
|
-
toolCallId: string;
|
|
906
|
-
/** Arguments that were passed to the tool */
|
|
907
|
-
arguments: Record<string, unknown>;
|
|
908
|
-
/** Result returned by the tool */
|
|
909
|
-
result: unknown;
|
|
910
|
-
/** Whether the tool execution resulted in an error */
|
|
911
|
-
isError: boolean;
|
|
912
|
-
/** Execution duration in milliseconds */
|
|
913
|
-
duration: number;
|
|
914
|
-
/** Whether approval was required and granted (undefined if no approval handler) */
|
|
915
|
-
approved?: boolean;
|
|
916
|
-
}
|
|
917
|
-
|
|
918
|
-
/**
|
|
919
|
-
* @fileoverview Message types for conversation history.
|
|
920
|
-
*
|
|
921
|
-
* Defines the message classes used to represent conversation turns
|
|
922
|
-
* between users and assistants, including support for multimodal
|
|
923
|
-
* content and tool calls.
|
|
924
|
-
*
|
|
925
|
-
* @module types/messages
|
|
926
|
-
*/
|
|
927
|
-
|
|
928
|
-
/**
|
|
929
|
-
* Message serialized to JSON format.
|
|
930
|
-
* Picks common fields from Message, converts timestamp to string.
|
|
931
|
-
*/
|
|
932
|
-
type MessageJSON = Pick<Message, 'id' | 'type' | 'metadata'> & {
|
|
933
|
-
timestamp: string;
|
|
934
|
-
content: ContentBlock[];
|
|
935
|
-
toolCalls?: ToolCall[];
|
|
936
|
-
results?: ToolResult[];
|
|
937
|
-
};
|
|
938
|
-
/**
|
|
939
|
-
* Message type discriminator union.
|
|
940
|
-
*
|
|
941
|
-
* This type is derived from {@link MessageRole} constants. The name `MessageType`
|
|
942
|
-
* is kept for backward compatibility; `MessageRole` works as both the const
|
|
943
|
-
* object and this type.
|
|
944
|
-
*/
|
|
945
|
-
type MessageType = (typeof MessageRole)[keyof typeof MessageRole];
|
|
946
|
-
/**
|
|
947
|
-
* Message role/type constants.
|
|
948
|
-
*
|
|
949
|
-
* Use these constants instead of raw strings for type-safe message handling:
|
|
950
|
-
*
|
|
951
|
-
* @example
|
|
952
|
-
* ```typescript
|
|
953
|
-
* import { MessageRole, isUserMessage } from 'upp';
|
|
954
|
-
*
|
|
955
|
-
* if (message.type === MessageRole.User) {
|
|
956
|
-
* console.log('User said:', message.text);
|
|
957
|
-
* } else if (message.type === MessageRole.Assistant) {
|
|
958
|
-
* console.log('Assistant replied:', message.text);
|
|
959
|
-
* }
|
|
960
|
-
* ```
|
|
961
|
-
*/
|
|
962
|
-
declare const MessageRole: {
|
|
963
|
-
/** User message */
|
|
964
|
-
readonly User: "user";
|
|
965
|
-
/** Assistant/model response */
|
|
966
|
-
readonly Assistant: "assistant";
|
|
967
|
-
/** Tool execution result */
|
|
968
|
-
readonly ToolResult: "tool_result";
|
|
969
|
-
};
|
|
970
|
-
/**
|
|
971
|
-
* Type alias for MessageType, allowing `MessageRole` to work as both const and type.
|
|
972
|
-
*/
|
|
973
|
-
type MessageRole = MessageType;
|
|
974
|
-
/**
|
|
975
|
-
* Provider-namespaced metadata for messages.
|
|
976
|
-
*
|
|
977
|
-
* Each provider can attach its own metadata under its namespace,
|
|
978
|
-
* preventing conflicts between different providers.
|
|
979
|
-
*
|
|
980
|
-
* @example
|
|
981
|
-
* ```typescript
|
|
982
|
-
* const metadata: MessageMetadata = {
|
|
983
|
-
* openai: { model: 'gpt-4', finishReason: 'stop' },
|
|
984
|
-
* anthropic: { model: 'claude-3', stopReason: 'end_turn' }
|
|
985
|
-
* };
|
|
986
|
-
* ```
|
|
987
|
-
*/
|
|
988
|
-
interface MessageMetadata {
|
|
989
|
-
[provider: string]: Record<string, unknown> | undefined;
|
|
990
|
-
}
|
|
991
|
-
/**
|
|
992
|
-
* Options for constructing messages.
|
|
993
|
-
*/
|
|
994
|
-
interface MessageOptions {
|
|
995
|
-
/** Custom message ID (auto-generated if not provided) */
|
|
996
|
-
id?: string;
|
|
997
|
-
/** Provider-specific metadata */
|
|
998
|
-
metadata?: MessageMetadata;
|
|
999
|
-
}
|
|
1000
|
-
/**
|
|
1001
|
-
* Abstract base class for all message types.
|
|
1002
|
-
*
|
|
1003
|
-
* Provides common functionality for user, assistant, and tool result
|
|
1004
|
-
* messages, including content accessors and metadata handling.
|
|
1005
|
-
*
|
|
1006
|
-
* @example
|
|
1007
|
-
* ```typescript
|
|
1008
|
-
* // Access text content from any message
|
|
1009
|
-
* const text = message.text;
|
|
1010
|
-
*
|
|
1011
|
-
* // Access images
|
|
1012
|
-
* const images = message.images;
|
|
1013
|
-
* ```
|
|
1014
|
-
*/
|
|
1015
|
-
declare abstract class Message {
|
|
1016
|
-
/** Unique message identifier */
|
|
1017
|
-
readonly id: string;
|
|
1018
|
-
/** Timestamp when the message was created */
|
|
1019
|
-
readonly timestamp: Date;
|
|
1020
|
-
/** Provider-specific metadata, namespaced by provider name */
|
|
1021
|
-
readonly metadata?: MessageMetadata;
|
|
1022
|
-
/** Message type discriminator (implemented by subclasses) */
|
|
1023
|
-
abstract readonly type: MessageType;
|
|
1024
|
-
/**
|
|
1025
|
-
* Returns the content blocks for this message.
|
|
1026
|
-
* Implemented by subclasses to provide type-specific content.
|
|
1027
|
-
*/
|
|
1028
|
-
protected abstract getContent(): ContentBlock[];
|
|
1029
|
-
/**
|
|
1030
|
-
* Creates a new message instance.
|
|
1031
|
-
*
|
|
1032
|
-
* @param options - Optional message ID and metadata
|
|
1033
|
-
*/
|
|
1034
|
-
constructor(options?: MessageOptions);
|
|
1035
|
-
/**
|
|
1036
|
-
* Concatenated text content from all text blocks.
|
|
1037
|
-
* Blocks are joined with double newlines.
|
|
1038
|
-
*/
|
|
1039
|
-
get text(): string;
|
|
1040
|
-
/**
|
|
1041
|
-
* All image content blocks in this message.
|
|
1042
|
-
*/
|
|
1043
|
-
get images(): ImageBlock[];
|
|
1044
|
-
/**
|
|
1045
|
-
* All document content blocks in this message.
|
|
1046
|
-
*/
|
|
1047
|
-
get documents(): DocumentBlock[];
|
|
1048
|
-
/**
|
|
1049
|
-
* All audio content blocks in this message.
|
|
1050
|
-
*/
|
|
1051
|
-
get audio(): AudioBlock[];
|
|
1052
|
-
/**
|
|
1053
|
-
* All video content blocks in this message.
|
|
1054
|
-
*/
|
|
1055
|
-
get video(): VideoBlock[];
|
|
1056
|
-
/**
|
|
1057
|
-
* All reasoning/thinking content blocks in this message.
|
|
1058
|
-
* Available when using extended thinking models.
|
|
1059
|
-
*/
|
|
1060
|
-
get reasoning(): ReasoningBlock[];
|
|
1061
|
-
}
|
|
1062
|
-
/**
|
|
1063
|
-
* User input message.
|
|
1064
|
-
*
|
|
1065
|
-
* Represents a message from the user, which can contain text and/or
|
|
1066
|
-
* multimodal content like images, audio, or video.
|
|
1067
|
-
*
|
|
1068
|
-
* @example
|
|
1069
|
-
* ```typescript
|
|
1070
|
-
* // Simple text message
|
|
1071
|
-
* const msg = new UserMessage('Hello, world!');
|
|
1072
|
-
*
|
|
1073
|
-
* // Multimodal message
|
|
1074
|
-
* const msg = new UserMessage([
|
|
1075
|
-
* { type: 'text', text: 'What is in this image?' },
|
|
1076
|
-
* { type: 'image', source: { type: 'url', url: '...' }, mimeType: 'image/png' }
|
|
1077
|
-
* ]);
|
|
1078
|
-
* ```
|
|
1079
|
-
*/
|
|
1080
|
-
declare class UserMessage extends Message {
|
|
1081
|
-
/** Message type discriminator */
|
|
1082
|
-
readonly type: "user";
|
|
1083
|
-
/** Content blocks in this message */
|
|
1084
|
-
readonly content: UserContent[];
|
|
1085
|
-
/**
|
|
1086
|
-
* Creates a new user message.
|
|
1087
|
-
*
|
|
1088
|
-
* @param content - String (converted to TextBlock) or array of content blocks
|
|
1089
|
-
* @param options - Optional message ID and metadata
|
|
1090
|
-
*/
|
|
1091
|
-
constructor(content: string | UserContent[], options?: MessageOptions);
|
|
1092
|
-
protected getContent(): ContentBlock[];
|
|
1093
|
-
}
|
|
1094
|
-
/**
|
|
1095
|
-
* Assistant response message.
|
|
1096
|
-
*
|
|
1097
|
-
* Represents a response from the AI assistant, which may contain
|
|
1098
|
-
* text, media content, and/or tool call requests.
|
|
1099
|
-
*
|
|
1100
|
-
* @example
|
|
1101
|
-
* ```typescript
|
|
1102
|
-
* // Simple text response
|
|
1103
|
-
* const msg = new AssistantMessage('Hello! How can I help?');
|
|
1104
|
-
*
|
|
1105
|
-
* // Response with tool calls
|
|
1106
|
-
* const msg = new AssistantMessage(
|
|
1107
|
-
* 'Let me check the weather...',
|
|
1108
|
-
* [{ toolCallId: 'call_1', toolName: 'get_weather', arguments: { location: 'NYC' } }]
|
|
1109
|
-
* );
|
|
1110
|
-
* ```
|
|
1111
|
-
*/
|
|
1112
|
-
declare class AssistantMessage extends Message {
|
|
1113
|
-
/** Message type discriminator */
|
|
1114
|
-
readonly type: "assistant";
|
|
1115
|
-
/** Content blocks in this message */
|
|
1116
|
-
readonly content: AssistantContent[];
|
|
1117
|
-
/** Tool calls requested by the model (if any) */
|
|
1118
|
-
readonly toolCalls?: ToolCall[];
|
|
1119
|
-
/**
|
|
1120
|
-
* Creates a new assistant message.
|
|
1121
|
-
*
|
|
1122
|
-
* @param content - String (converted to TextBlock) or array of content blocks
|
|
1123
|
-
* @param toolCalls - Tool calls requested by the model
|
|
1124
|
-
* @param options - Optional message ID and metadata
|
|
1125
|
-
*/
|
|
1126
|
-
constructor(content: string | AssistantContent[], toolCalls?: ToolCall[], options?: MessageOptions);
|
|
1127
|
-
protected getContent(): ContentBlock[];
|
|
1128
|
-
/**
|
|
1129
|
-
* Whether this message contains tool call requests.
|
|
1130
|
-
*/
|
|
1131
|
-
get hasToolCalls(): boolean;
|
|
1132
|
-
}
|
|
1133
|
-
/**
|
|
1134
|
-
* Tool execution result message.
|
|
1135
|
-
*
|
|
1136
|
-
* Contains the results of executing one or more tool calls,
|
|
1137
|
-
* sent back to the model for further processing.
|
|
1138
|
-
*
|
|
1139
|
-
* @example
|
|
1140
|
-
* ```typescript
|
|
1141
|
-
* const msg = new ToolResultMessage([
|
|
1142
|
-
* { toolCallId: 'call_1', result: { temperature: 72, conditions: 'sunny' } },
|
|
1143
|
-
* { toolCallId: 'call_2', result: 'File not found', isError: true }
|
|
1144
|
-
* ]);
|
|
1145
|
-
* ```
|
|
1146
|
-
*/
|
|
1147
|
-
declare class ToolResultMessage extends Message {
|
|
1148
|
-
/** Message type discriminator */
|
|
1149
|
-
readonly type: "tool_result";
|
|
1150
|
-
/** Results from tool executions */
|
|
1151
|
-
readonly results: ToolResult[];
|
|
1152
|
-
/**
|
|
1153
|
-
* Creates a new tool result message.
|
|
1154
|
-
*
|
|
1155
|
-
* @param results - Array of tool execution results
|
|
1156
|
-
* @param options - Optional message ID and metadata
|
|
1157
|
-
*/
|
|
1158
|
-
constructor(results: ToolResult[], options?: MessageOptions);
|
|
1159
|
-
protected getContent(): ContentBlock[];
|
|
1160
|
-
}
|
|
1161
|
-
/**
|
|
1162
|
-
* Type guard for UserMessage.
|
|
1163
|
-
*
|
|
1164
|
-
* @param msg - The message to check
|
|
1165
|
-
* @returns True if the message is a UserMessage
|
|
1166
|
-
*
|
|
1167
|
-
* @example
|
|
1168
|
-
* ```typescript
|
|
1169
|
-
* if (isUserMessage(msg)) {
|
|
1170
|
-
* console.log('User said:', msg.text);
|
|
1171
|
-
* }
|
|
1172
|
-
* ```
|
|
1173
|
-
*/
|
|
1174
|
-
declare function isUserMessage(msg: Message): msg is UserMessage;
|
|
1175
|
-
/**
|
|
1176
|
-
* Type guard for AssistantMessage.
|
|
1177
|
-
*
|
|
1178
|
-
* @param msg - The message to check
|
|
1179
|
-
* @returns True if the message is an AssistantMessage
|
|
1180
|
-
*
|
|
1181
|
-
* @example
|
|
1182
|
-
* ```typescript
|
|
1183
|
-
* if (isAssistantMessage(msg)) {
|
|
1184
|
-
* console.log('Assistant said:', msg.text);
|
|
1185
|
-
* if (msg.hasToolCalls) {
|
|
1186
|
-
* console.log('Tool calls:', msg.toolCalls);
|
|
1187
|
-
* }
|
|
1188
|
-
* }
|
|
1189
|
-
* ```
|
|
1190
|
-
*/
|
|
1191
|
-
declare function isAssistantMessage(msg: Message): msg is AssistantMessage;
|
|
1192
|
-
/**
|
|
1193
|
-
* Type guard for ToolResultMessage.
|
|
1194
|
-
*
|
|
1195
|
-
* @param msg - The message to check
|
|
1196
|
-
* @returns True if the message is a ToolResultMessage
|
|
1197
|
-
*
|
|
1198
|
-
* @example
|
|
1199
|
-
* ```typescript
|
|
1200
|
-
* if (isToolResultMessage(msg)) {
|
|
1201
|
-
* for (const result of msg.results) {
|
|
1202
|
-
* console.log(`Tool ${result.toolCallId}:`, result.result);
|
|
1203
|
-
* }
|
|
1204
|
-
* }
|
|
1205
|
-
* ```
|
|
1206
|
-
*/
|
|
1207
|
-
declare function isToolResultMessage(msg: Message): msg is ToolResultMessage;
|
|
1208
|
-
|
|
1209
|
-
/**
|
|
1210
|
-
* @fileoverview Turn types for inference results.
|
|
1211
|
-
*
|
|
1212
|
-
* A Turn represents the complete result of one inference call, including
|
|
1213
|
-
* all messages produced during tool execution loops, token usage, and
|
|
1214
|
-
* optional structured output data.
|
|
1215
|
-
*
|
|
1216
|
-
* @module types/turn
|
|
1217
|
-
*/
|
|
1218
|
-
|
|
1219
|
-
/**
|
|
1220
|
-
* Token usage information for an inference request.
|
|
1221
|
-
*
|
|
1222
|
-
* Tracks input and output tokens across all inference cycles,
|
|
1223
|
-
* with optional per-cycle breakdown and cache metrics.
|
|
1224
|
-
*
|
|
1225
|
-
* @example
|
|
1226
|
-
* ```typescript
|
|
1227
|
-
* const usage: TokenUsage = {
|
|
1228
|
-
* inputTokens: 150,
|
|
1229
|
-
* outputTokens: 50,
|
|
1230
|
-
* totalTokens: 200,
|
|
1231
|
-
* cacheReadTokens: 100,
|
|
1232
|
-
* cacheWriteTokens: 50,
|
|
1233
|
-
* cycles: [
|
|
1234
|
-
* { inputTokens: 100, outputTokens: 30, cacheReadTokens: 0, cacheWriteTokens: 50 },
|
|
1235
|
-
* { inputTokens: 50, outputTokens: 20, cacheReadTokens: 100, cacheWriteTokens: 0 }
|
|
1236
|
-
* ]
|
|
1237
|
-
* };
|
|
1238
|
-
* ```
|
|
1239
|
-
*/
|
|
1240
|
-
interface TokenUsage {
|
|
1241
|
-
/** Total input tokens across all cycles */
|
|
1242
|
-
inputTokens: number;
|
|
1243
|
-
/** Total output tokens across all cycles */
|
|
1244
|
-
outputTokens: number;
|
|
1245
|
-
/** Sum of input and output tokens */
|
|
1246
|
-
totalTokens: number;
|
|
1247
|
-
/**
|
|
1248
|
-
* Tokens read from cache (cache hits).
|
|
1249
|
-
* Returns 0 for providers that don't support or report cache metrics.
|
|
1250
|
-
*/
|
|
1251
|
-
cacheReadTokens: number;
|
|
1252
|
-
/**
|
|
1253
|
-
* Tokens written to cache (cache misses that were cached).
|
|
1254
|
-
* Only Anthropic reports this metric; returns 0 for other providers.
|
|
1255
|
-
*/
|
|
1256
|
-
cacheWriteTokens: number;
|
|
1257
|
-
/** Per-cycle token breakdown (if multiple cycles occurred) */
|
|
1258
|
-
cycles?: Array<{
|
|
1259
|
-
inputTokens: number;
|
|
1260
|
-
outputTokens: number;
|
|
1261
|
-
cacheReadTokens: number;
|
|
1262
|
-
cacheWriteTokens: number;
|
|
1263
|
-
}>;
|
|
1264
|
-
}
|
|
1265
|
-
/**
|
|
1266
|
-
* A Turn represents the complete result of one inference call.
|
|
1267
|
-
*
|
|
1268
|
-
* Includes all messages produced during tool execution loops,
|
|
1269
|
-
* the final assistant response, token usage, and optional
|
|
1270
|
-
* structured output data.
|
|
1271
|
-
*
|
|
1272
|
-
* @typeParam TData - Type of the structured output data
|
|
1273
|
-
*
|
|
1274
|
-
* @example
|
|
1275
|
-
* ```typescript
|
|
1276
|
-
* const turn = await instance.generate('Hello');
|
|
1277
|
-
* console.log(turn.response.text);
|
|
1278
|
-
* console.log(`Used ${turn.usage.totalTokens} tokens in ${turn.cycles} cycles`);
|
|
1279
|
-
*
|
|
1280
|
-
* // With structured output
|
|
1281
|
-
* interface WeatherData { temperature: number; conditions: string; }
|
|
1282
|
-
* const turn = await instance.generate<WeatherData>('Get weather');
|
|
1283
|
-
* console.log(turn.data?.temperature);
|
|
1284
|
-
* ```
|
|
1285
|
-
*/
|
|
1286
|
-
interface Turn<TData = unknown> {
|
|
1287
|
-
/**
|
|
1288
|
-
* All messages produced during this inference, in chronological order.
|
|
1289
|
-
* Includes UserMessage, AssistantMessage (may include toolCalls), and ToolResultMessage.
|
|
1290
|
-
*/
|
|
1291
|
-
readonly messages: Message[];
|
|
1292
|
-
/** The final assistant response (last AssistantMessage in the turn) */
|
|
1293
|
-
readonly response: AssistantMessage;
|
|
1294
|
-
/**
|
|
1295
|
-
* Tool executions that occurred during this turn.
|
|
1296
|
-
*
|
|
1297
|
-
* Execution order reflects completion timing, not call order.
|
|
1298
|
-
* Correlate with tool calls using toolCallId.
|
|
1299
|
-
*/
|
|
1300
|
-
readonly toolExecutions: ToolExecution[];
|
|
1301
|
-
/** Aggregate token usage for the entire turn */
|
|
1302
|
-
readonly usage: TokenUsage;
|
|
1303
|
-
/** Total number of inference cycles (1 + number of tool rounds) */
|
|
1304
|
-
readonly cycles: number;
|
|
1305
|
-
/**
|
|
1306
|
-
* Structured output data (if a structure schema was provided).
|
|
1307
|
-
* Type is inferred from the schema when using TypeScript.
|
|
1308
|
-
*/
|
|
1309
|
-
readonly data?: TData;
|
|
1310
|
-
}
|
|
1311
|
-
/**
|
|
1312
|
-
* Turn serialized to JSON format.
|
|
1313
|
-
* Messages are converted to MessageJSON, response is omitted (computed from messages).
|
|
1314
|
-
*
|
|
1315
|
-
* @remarks
|
|
1316
|
-
* This type is derived from {@link Turn} and should stay in sync with it.
|
|
1317
|
-
*/
|
|
1318
|
-
type TurnJSON = Omit<Turn, 'messages' | 'response'> & {
|
|
1319
|
-
messages: MessageJSON[];
|
|
1320
|
-
};
|
|
1321
|
-
/**
|
|
1322
|
-
* Creates a Turn from accumulated inference data.
|
|
1323
|
-
*
|
|
1324
|
-
* @typeParam TData - Type of the structured output data
|
|
1325
|
-
* @param messages - All messages produced during the inference
|
|
1326
|
-
* @param toolExecutions - Record of all tool executions
|
|
1327
|
-
* @param usage - Aggregate token usage
|
|
1328
|
-
* @param cycles - Number of inference cycles
|
|
1329
|
-
* @param data - Optional structured output data
|
|
1330
|
-
* @returns A complete Turn object
|
|
1331
|
-
* @throws Error if no assistant message is found in the messages
|
|
1332
|
-
*
|
|
1333
|
-
* @example
|
|
1334
|
-
* ```typescript
|
|
1335
|
-
* const turn = createTurn(
|
|
1336
|
-
* [userMsg, assistantMsg],
|
|
1337
|
-
* [],
|
|
1338
|
-
* { inputTokens: 100, outputTokens: 50, totalTokens: 150 },
|
|
1339
|
-
* 1
|
|
1340
|
-
* );
|
|
1341
|
-
* ```
|
|
1342
|
-
*/
|
|
1343
|
-
declare function createTurn<TData = unknown>(messages: Message[], toolExecutions: ToolExecution[], usage: TokenUsage, cycles: number, data?: TData): Turn<TData>;
|
|
1344
|
-
/**
|
|
1345
|
-
* Creates an empty TokenUsage object.
|
|
1346
|
-
*
|
|
1347
|
-
* @returns A TokenUsage with all values set to zero
|
|
1348
|
-
*
|
|
1349
|
-
* @example
|
|
1350
|
-
* ```typescript
|
|
1351
|
-
* const usage = emptyUsage();
|
|
1352
|
-
* // { inputTokens: 0, outputTokens: 0, totalTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0, cycles: [] }
|
|
1353
|
-
* ```
|
|
1354
|
-
*/
|
|
1355
|
-
declare function emptyUsage(): TokenUsage;
|
|
1356
|
-
/**
|
|
1357
|
-
* Aggregates token usage from multiple inference cycles.
|
|
1358
|
-
*
|
|
1359
|
-
* @param usages - Array of TokenUsage objects to aggregate
|
|
1360
|
-
* @returns Combined TokenUsage with per-cycle breakdown
|
|
1361
|
-
*
|
|
1362
|
-
* @example
|
|
1363
|
-
* ```typescript
|
|
1364
|
-
* const cycle1 = { inputTokens: 100, outputTokens: 30, totalTokens: 130, cacheReadTokens: 50, cacheWriteTokens: 0 };
|
|
1365
|
-
* const cycle2 = { inputTokens: 150, outputTokens: 40, totalTokens: 190, cacheReadTokens: 100, cacheWriteTokens: 0 };
|
|
1366
|
-
* const total = aggregateUsage([cycle1, cycle2]);
|
|
1367
|
-
* // { inputTokens: 250, outputTokens: 70, totalTokens: 320, cacheReadTokens: 150, cacheWriteTokens: 0, cycles: [...] }
|
|
1368
|
-
* ```
|
|
1369
|
-
*/
|
|
1370
|
-
declare function aggregateUsage(usages: TokenUsage[]): TokenUsage;
|
|
1371
|
-
|
|
1372
|
-
/**
|
|
1373
|
-
* @fileoverview Streaming types for real-time LLM responses.
|
|
1374
|
-
*
|
|
1375
|
-
* Defines the event types and interfaces for streaming LLM inference,
|
|
1376
|
-
* including text deltas, tool call deltas, and control events.
|
|
1377
|
-
*
|
|
1378
|
-
* @module types/stream
|
|
1379
|
-
*/
|
|
1380
|
-
|
|
1381
|
-
/**
|
|
1382
|
-
* Stream event type constants.
|
|
1383
|
-
*
|
|
1384
|
-
* Use these constants instead of raw strings for type-safe event handling:
|
|
1385
|
-
*
|
|
1386
|
-
* @example
|
|
1387
|
-
* ```typescript
|
|
1388
|
-
* import { StreamEventType } from 'upp';
|
|
1389
|
-
*
|
|
1390
|
-
* for await (const event of stream) {
|
|
1391
|
-
* if (event.type === StreamEventType.TextDelta) {
|
|
1392
|
-
* process.stdout.write(event.delta.text ?? '');
|
|
1393
|
-
* }
|
|
1394
|
-
* }
|
|
1395
|
-
* ```
|
|
1396
|
-
*/
|
|
1397
|
-
declare const StreamEventType: {
|
|
1398
|
-
/** Incremental text output */
|
|
1399
|
-
readonly TextDelta: "text_delta";
|
|
1400
|
-
/** Incremental reasoning/thinking output */
|
|
1401
|
-
readonly ReasoningDelta: "reasoning_delta";
|
|
1402
|
-
/** Incremental image data */
|
|
1403
|
-
readonly ImageDelta: "image_delta";
|
|
1404
|
-
/** Incremental audio data */
|
|
1405
|
-
readonly AudioDelta: "audio_delta";
|
|
1406
|
-
/** Incremental video data */
|
|
1407
|
-
readonly VideoDelta: "video_delta";
|
|
1408
|
-
/** Incremental tool call data (arguments being streamed) */
|
|
1409
|
-
readonly ToolCallDelta: "tool_call_delta";
|
|
1410
|
-
/** Incremental structured object data (for structured output responses) */
|
|
1411
|
-
readonly ObjectDelta: "object_delta";
|
|
1412
|
-
/** Tool execution has started (may be emitted after completion in some implementations) */
|
|
1413
|
-
readonly ToolExecutionStart: "tool_execution_start";
|
|
1414
|
-
/** Tool execution has completed */
|
|
1415
|
-
readonly ToolExecutionEnd: "tool_execution_end";
|
|
1416
|
-
/** Beginning of a message */
|
|
1417
|
-
readonly MessageStart: "message_start";
|
|
1418
|
-
/** End of a message */
|
|
1419
|
-
readonly MessageStop: "message_stop";
|
|
1420
|
-
/** Beginning of a content block */
|
|
1421
|
-
readonly ContentBlockStart: "content_block_start";
|
|
1422
|
-
/** End of a content block */
|
|
1423
|
-
readonly ContentBlockStop: "content_block_stop";
|
|
1424
|
-
};
|
|
1425
|
-
/**
|
|
1426
|
-
* Stream event type discriminator union.
|
|
1427
|
-
*
|
|
1428
|
-
* This type is derived from {@link StreamEventType} constants. Use `StreamEventType.TextDelta`
|
|
1429
|
-
* for constants or `type MyType = StreamEventType` for type annotations.
|
|
1430
|
-
*/
|
|
1431
|
-
type StreamEventType = (typeof StreamEventType)[keyof typeof StreamEventType];
|
|
1432
|
-
/**
|
|
1433
|
-
* Event delta data payload.
|
|
1434
|
-
*
|
|
1435
|
-
* Contains the type-specific data for a streaming event.
|
|
1436
|
-
* Different fields are populated depending on the event type:
|
|
1437
|
-
*
|
|
1438
|
-
* | Event Type | Fields |
|
|
1439
|
-
* |------------|--------|
|
|
1440
|
-
* | `text_delta` | `text` |
|
|
1441
|
-
* | `reasoning_delta` | `text` |
|
|
1442
|
-
* | `object_delta` | `text` |
|
|
1443
|
-
* | `image_delta` | `data` |
|
|
1444
|
-
* | `audio_delta` | `data` |
|
|
1445
|
-
* | `video_delta` | `data` |
|
|
1446
|
-
* | `tool_call_delta` | `toolCallId`, `toolName`, `argumentsJson` |
|
|
1447
|
-
* | `tool_execution_start` | `toolCallId`, `toolName`, `timestamp` |
|
|
1448
|
-
* | `tool_execution_end` | `toolCallId`, `toolName`, `result`, `isError`, `timestamp` |
|
|
1449
|
-
* | `message_start` | (none) |
|
|
1450
|
-
* | `message_stop` | (none) |
|
|
1451
|
-
* | `content_block_start` | (none) |
|
|
1452
|
-
* | `content_block_stop` | (none) |
|
|
1453
|
-
*/
|
|
1454
|
-
interface EventDelta {
|
|
1455
|
-
/** Incremental text content (text_delta, reasoning_delta, object_delta) */
|
|
1456
|
-
text?: string;
|
|
1457
|
-
/** Incremental binary data (image_delta, audio_delta, video_delta) */
|
|
1458
|
-
data?: Uint8Array;
|
|
1459
|
-
/** Tool call identifier (tool_call_delta, tool_execution_start/end) */
|
|
1460
|
-
toolCallId?: string;
|
|
1461
|
-
/** Tool name (tool_call_delta, tool_execution_start/end) */
|
|
1462
|
-
toolName?: string;
|
|
1463
|
-
/** Incremental JSON arguments string (tool_call_delta) */
|
|
1464
|
-
argumentsJson?: string;
|
|
1465
|
-
/** Tool execution result (tool_execution_end) */
|
|
1466
|
-
result?: unknown;
|
|
1467
|
-
/** Whether tool execution resulted in an error (tool_execution_end) */
|
|
1468
|
-
isError?: boolean;
|
|
1469
|
-
/** Timestamp in milliseconds (tool_execution_start/end) */
|
|
1470
|
-
timestamp?: number;
|
|
1471
|
-
}
|
|
1472
|
-
/**
|
|
1473
|
-
* A single streaming event from the LLM.
|
|
1474
|
-
*
|
|
1475
|
-
* Events are emitted in order as the model generates output,
|
|
1476
|
-
* allowing for real-time display of responses.
|
|
1477
|
-
*
|
|
1478
|
-
* @example
|
|
1479
|
-
* ```typescript
|
|
1480
|
-
* import { StreamEventType } from 'upp';
|
|
1481
|
-
*
|
|
1482
|
-
* for await (const event of stream) {
|
|
1483
|
-
* if (event.type === StreamEventType.TextDelta) {
|
|
1484
|
-
* process.stdout.write(event.delta.text ?? '');
|
|
1485
|
-
* } else if (event.type === StreamEventType.ToolCallDelta) {
|
|
1486
|
-
* console.log('Tool:', event.delta.toolName);
|
|
1487
|
-
* }
|
|
1488
|
-
* }
|
|
1489
|
-
* ```
|
|
1490
|
-
*/
|
|
1491
|
-
interface StreamEvent {
|
|
1492
|
-
/** Event type discriminator */
|
|
1493
|
-
type: StreamEventType;
|
|
1494
|
-
/** Index of the content block this event belongs to */
|
|
1495
|
-
index: number;
|
|
1496
|
-
/** Event-specific data payload */
|
|
1497
|
-
delta: EventDelta;
|
|
1498
|
-
}
|
|
1499
|
-
/**
|
|
1500
|
-
* Stream result - an async iterable that also provides the final turn.
|
|
1501
|
-
*
|
|
1502
|
-
* Allows consuming streaming events while also awaiting the complete
|
|
1503
|
-
* Turn result after streaming finishes.
|
|
1504
|
-
*
|
|
1505
|
-
* @typeParam TData - Type of the structured output data
|
|
1506
|
-
*
|
|
1507
|
-
* @example
|
|
1508
|
-
* ```typescript
|
|
1509
|
-
* import { StreamEventType } from 'upp';
|
|
1510
|
-
*
|
|
1511
|
-
* const stream = instance.stream('Tell me a story');
|
|
1512
|
-
*
|
|
1513
|
-
* // Consume streaming events
|
|
1514
|
-
* for await (const event of stream) {
|
|
1515
|
-
* if (event.type === StreamEventType.TextDelta) {
|
|
1516
|
-
* process.stdout.write(event.delta.text ?? '');
|
|
1517
|
-
* }
|
|
1518
|
-
* }
|
|
1519
|
-
*
|
|
1520
|
-
* // Get the complete turn after streaming
|
|
1521
|
-
* const turn = await stream.turn;
|
|
1522
|
-
* console.log('\n\nTokens used:', turn.usage.totalTokens);
|
|
1523
|
-
* ```
|
|
1524
|
-
*/
|
|
1525
|
-
interface StreamResult<TData = unknown> extends AsyncIterable<StreamEvent> {
|
|
1526
|
-
/**
|
|
1527
|
-
* Promise that resolves to the complete Turn after streaming finishes.
|
|
1528
|
-
* Rejects if the stream is aborted or terminated early.
|
|
1529
|
-
*/
|
|
1530
|
-
readonly turn: Promise<Turn<TData>>;
|
|
1531
|
-
/**
|
|
1532
|
-
* Aborts the stream, stopping further events and cancelling the request.
|
|
1533
|
-
* This will cause {@link StreamResult.turn} to reject.
|
|
1534
|
-
*/
|
|
1535
|
-
abort(): void;
|
|
1536
|
-
}
|
|
1537
|
-
/**
|
|
1538
|
-
* Creates a StreamResult from an async generator and completion promise.
|
|
1539
|
-
*
|
|
1540
|
-
* @typeParam TData - Type of the structured output data
|
|
1541
|
-
* @param generator - Async generator that yields stream events
|
|
1542
|
-
* @param turnPromiseOrFactory - Promise or factory that resolves to the complete Turn
|
|
1543
|
-
* @param abortController - Controller for aborting the stream
|
|
1544
|
-
* @returns A StreamResult that can be iterated and awaited
|
|
1545
|
-
*
|
|
1546
|
-
* @example
|
|
1547
|
-
* ```typescript
|
|
1548
|
-
* const abortController = new AbortController();
|
|
1549
|
-
* const stream = createStreamResult(
|
|
1550
|
-
* eventGenerator(),
|
|
1551
|
-
* turnPromise,
|
|
1552
|
-
* abortController
|
|
1553
|
-
* );
|
|
1554
|
-
* ```
|
|
1555
|
-
*/
|
|
1556
|
-
declare function createStreamResult<TData = unknown>(generator: AsyncGenerator<StreamEvent, void, unknown>, turnPromiseOrFactory: Promise<Turn<TData>> | (() => Promise<Turn<TData>>), abortController: AbortController): StreamResult<TData>;
|
|
1557
|
-
/**
|
|
1558
|
-
* Creates a text delta stream event.
|
|
1559
|
-
*
|
|
1560
|
-
* @param text - The incremental text content
|
|
1561
|
-
* @param index - Content block index (default: 0)
|
|
1562
|
-
* @returns A text_delta StreamEvent
|
|
1563
|
-
*/
|
|
1564
|
-
declare function textDelta(text: string, index?: number): StreamEvent;
|
|
1565
|
-
/**
|
|
1566
|
-
* Creates a tool call delta stream event.
|
|
1567
|
-
*
|
|
1568
|
-
* @param toolCallId - Unique identifier for the tool call
|
|
1569
|
-
* @param toolName - Name of the tool being called
|
|
1570
|
-
* @param argumentsJson - Incremental JSON arguments string
|
|
1571
|
-
* @param index - Content block index (default: 0)
|
|
1572
|
-
* @returns A tool_call_delta StreamEvent
|
|
1573
|
-
*/
|
|
1574
|
-
declare function toolCallDelta(toolCallId: string, toolName: string, argumentsJson: string, index?: number): StreamEvent;
|
|
1575
|
-
/**
|
|
1576
|
-
* Creates an object delta stream event for structured output responses.
|
|
1577
|
-
*
|
|
1578
|
-
* @param text - The incremental text content
|
|
1579
|
-
* @param index - Content block index (default: 0)
|
|
1580
|
-
* @returns An object_delta StreamEvent
|
|
1581
|
-
*/
|
|
1582
|
-
declare function objectDelta(text: string, index?: number): StreamEvent;
|
|
1583
|
-
/**
|
|
1584
|
-
* Creates a message start stream event.
|
|
1585
|
-
*
|
|
1586
|
-
* @returns A message_start StreamEvent
|
|
1587
|
-
*/
|
|
1588
|
-
declare function messageStart(): StreamEvent;
|
|
1589
|
-
/**
|
|
1590
|
-
* Creates a message stop stream event.
|
|
1591
|
-
*
|
|
1592
|
-
* @returns A message_stop StreamEvent
|
|
1593
|
-
*/
|
|
1594
|
-
declare function messageStop(): StreamEvent;
|
|
1595
|
-
/**
|
|
1596
|
-
* Creates a content block start stream event.
|
|
1597
|
-
*
|
|
1598
|
-
* @param index - The content block index starting
|
|
1599
|
-
* @returns A content_block_start StreamEvent
|
|
1600
|
-
*/
|
|
1601
|
-
declare function contentBlockStart(index: number): StreamEvent;
|
|
1602
|
-
/**
|
|
1603
|
-
* Creates a content block stop stream event.
|
|
1604
|
-
*
|
|
1605
|
-
* @param index - The content block index stopping
|
|
1606
|
-
* @returns A content_block_stop StreamEvent
|
|
1607
|
-
*/
|
|
1608
|
-
declare function contentBlockStop(index: number): StreamEvent;
|
|
1609
|
-
/**
|
|
1610
|
-
* Creates a tool execution start stream event.
|
|
1611
|
-
*
|
|
1612
|
-
* @param toolCallId - Unique identifier for the tool call
|
|
1613
|
-
* @param toolName - Name of the tool being executed
|
|
1614
|
-
* @param timestamp - Start timestamp in milliseconds
|
|
1615
|
-
* @param index - Content block index (default: 0)
|
|
1616
|
-
* @returns A tool_execution_start StreamEvent
|
|
1617
|
-
*/
|
|
1618
|
-
declare function toolExecutionStart(toolCallId: string, toolName: string, timestamp: number, index?: number): StreamEvent;
|
|
1619
|
-
/**
|
|
1620
|
-
* Creates a tool execution end stream event.
|
|
1621
|
-
*
|
|
1622
|
-
* @param toolCallId - Unique identifier for the tool call
|
|
1623
|
-
* @param toolName - Name of the tool that was executed
|
|
1624
|
-
* @param result - The result from the tool execution
|
|
1625
|
-
* @param isError - Whether the execution resulted in an error
|
|
1626
|
-
* @param timestamp - End timestamp in milliseconds
|
|
1627
|
-
* @param index - Content block index (default: 0)
|
|
1628
|
-
* @returns A tool_execution_end StreamEvent
|
|
1629
|
-
*/
|
|
1630
|
-
declare function toolExecutionEnd(toolCallId: string, toolName: string, result: unknown, isError: boolean, timestamp: number, index?: number): StreamEvent;
|
|
1
|
+
import { I as ImageSource, c as ImageBlock, S as StreamEvent, d as Tool, M as Message, T as Turn, U as UserContent, A as AssistantContent, e as MessageType, a as MessageJSON, f as ToolUseStrategy, J as JSONSchema, g as AssistantMessage, h as TokenUsage, C as ContentBlock, i as StreamResult } from './stream-S7nwQRqM.js';
|
|
1631
2
|
|
|
1632
3
|
/**
|
|
1633
4
|
* @fileoverview Error types for the Unified Provider Protocol.
|
|
@@ -1964,7 +335,7 @@ declare class Image {
|
|
|
1964
335
|
/**
|
|
1965
336
|
* @fileoverview Middleware types for the Universal Provider Protocol.
|
|
1966
337
|
*
|
|
1967
|
-
* Defines the interfaces for composable middleware that can
|
|
338
|
+
* Defines the interfaces for composable middleware that can
|
|
1968
339
|
* transform requests, responses, and stream events across all modalities.
|
|
1969
340
|
*
|
|
1970
341
|
* @module types/middleware
|
|
@@ -2103,6 +474,15 @@ interface Middleware {
|
|
|
2103
474
|
* @param ctx - The middleware context
|
|
2104
475
|
*/
|
|
2105
476
|
onError?(error: Error, ctx: MiddlewareContext): void | Promise<void>;
|
|
477
|
+
/**
|
|
478
|
+
* Called when a request is cancelled (for example, when a stream is aborted
|
|
479
|
+
* or a client disconnects).
|
|
480
|
+
* Called for all middleware that have this hook, regardless of order.
|
|
481
|
+
*
|
|
482
|
+
* @param error - The cancellation error
|
|
483
|
+
* @param ctx - The middleware context
|
|
484
|
+
*/
|
|
485
|
+
onAbort?(error: Error, ctx: MiddlewareContext): void | Promise<void>;
|
|
2106
486
|
/**
|
|
2107
487
|
* Called before provider execution. Can modify the request.
|
|
2108
488
|
*
|
|
@@ -3382,4 +1762,4 @@ interface BoundLLMModel<TParams = unknown> {
|
|
|
3382
1762
|
stream(request: LLMRequest<TParams>): LLMStreamResult;
|
|
3383
1763
|
}
|
|
3384
1764
|
|
|
3385
|
-
export {
|
|
1765
|
+
export { type MiddlewareModality as $, type BoundLLMModel as A, type BoundEmbeddingModel as B, type InferenceInput as C, type ImageInput as D, type EmbeddingInput as E, type ImageEditInput as F, type ImageGenerateOptions as G, type GeneratedImage as H, type ImageStreamResult as I, type ImageUsage as J, type KeyStrategy as K, type LLMOptions as L, type Middleware as M, type ImageResult as N, type ImageStreamEvent as O, type ProviderIdentity as P, type ImageCapabilities as Q, type RetryStrategy as R, type ImageRequest as S, Thread as T, UPPError as U, type ImageEditRequest as V, type ImageResponse as W, type BoundImageModel as X, type ImageModelInput as Y, type MiddlewareContext as Z, type StreamContext as _, type ImageProviderStreamResult as a, type AnyRequest as a0, type AnyResponse as a1, type ProviderConfig as b, type EmbeddingUsage as c, type Provider as d, type Modality as e, ErrorCode as f, type ModelReference as g, type LLMInstance as h, type ImageOptions as i, type ImageInstance as j, type LLMHandler as k, type EmbeddingHandler as l, type ImageHandler as m, Image as n, ModalityType as o, type ThreadJSON as p, type LLMProvider as q, type EmbeddingProvider as r, type ImageProvider as s, type EmbeddingRequest as t, type EmbeddingResponse as u, type EmbeddingVector as v, type LLMCapabilities as w, type LLMRequest as x, type LLMResponse as y, type LLMStreamResult as z };
|