@providerprotocol/ai 0.0.17 → 0.0.19
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 +294 -114
- package/dist/anthropic/index.d.ts +1 -1
- package/dist/anthropic/index.js +5 -3
- package/dist/anthropic/index.js.map +1 -1
- package/dist/{chunk-MOU4U3PO.js → chunk-5FEAOEXV.js} +4 -68
- package/dist/chunk-5FEAOEXV.js.map +1 -0
- package/dist/chunk-DZQHVGNV.js +71 -0
- package/dist/chunk-DZQHVGNV.js.map +1 -0
- package/dist/chunk-SKY2JLA7.js +59 -0
- package/dist/chunk-SKY2JLA7.js.map +1 -0
- package/dist/{chunk-SVYROCLD.js → chunk-UMKWXGO3.js} +1 -1
- package/dist/chunk-UMKWXGO3.js.map +1 -0
- package/dist/chunk-WAKD3OO5.js +224 -0
- package/dist/chunk-WAKD3OO5.js.map +1 -0
- package/dist/content-DEl3z_W2.d.ts +276 -0
- package/dist/google/index.d.ts +3 -1
- package/dist/google/index.js +122 -4
- package/dist/google/index.js.map +1 -1
- package/dist/http/index.d.ts +2 -2
- package/dist/http/index.js +2 -1
- package/dist/image-Dhq-Yuq4.d.ts +456 -0
- package/dist/index.d.ts +59 -1460
- package/dist/index.js +89 -267
- package/dist/index.js.map +1 -1
- package/dist/ollama/index.d.ts +1 -1
- package/dist/ollama/index.js +5 -3
- package/dist/ollama/index.js.map +1 -1
- package/dist/openai/index.d.ts +47 -20
- package/dist/openai/index.js +309 -4
- package/dist/openai/index.js.map +1 -1
- package/dist/openrouter/index.d.ts +1 -1
- package/dist/openrouter/index.js +5 -3
- package/dist/openrouter/index.js.map +1 -1
- package/dist/{provider-D5MO3-pS.d.ts → provider-BBMBZuGn.d.ts} +11 -11
- package/dist/proxy/index.d.ts +652 -0
- package/dist/proxy/index.js +565 -0
- package/dist/proxy/index.js.map +1 -0
- package/dist/{retry-DZ4Sqmxp.d.ts → retry-DR7YRJDz.d.ts} +1 -1
- package/dist/stream-DRHy6q1a.d.ts +1013 -0
- package/dist/xai/index.d.ts +29 -1
- package/dist/xai/index.js +118 -4
- package/dist/xai/index.js.map +1 -1
- package/package.json +6 -1
- package/dist/chunk-MOU4U3PO.js.map +0 -1
- package/dist/chunk-SVYROCLD.js.map +0 -1
|
@@ -0,0 +1,1013 @@
|
|
|
1
|
+
import { C as ContentBlock, I as ImageBlock, a as AudioBlock, V as VideoBlock, A as AssistantContent, U as UserContent } from './content-DEl3z_W2.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview JSON Schema types for tool parameters and structured outputs.
|
|
5
|
+
*
|
|
6
|
+
* Provides TypeScript interfaces for defining JSON Schema objects used in
|
|
7
|
+
* LLM tool definitions and structured output specifications.
|
|
8
|
+
*
|
|
9
|
+
* @module types/schema
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Primitive and composite JSON Schema property types.
|
|
13
|
+
*
|
|
14
|
+
* These types correspond to the JSON Schema specification's allowed type values.
|
|
15
|
+
*/
|
|
16
|
+
type JSONSchemaPropertyType =
|
|
17
|
+
/** String values */
|
|
18
|
+
'string'
|
|
19
|
+
/** Floating point numbers */
|
|
20
|
+
| 'number'
|
|
21
|
+
/** Whole numbers */
|
|
22
|
+
| 'integer'
|
|
23
|
+
/** Boolean true/false values */
|
|
24
|
+
| 'boolean'
|
|
25
|
+
/** Ordered lists of values */
|
|
26
|
+
| 'array'
|
|
27
|
+
/** Key-value mappings */
|
|
28
|
+
| 'object'
|
|
29
|
+
/** Explicit null value */
|
|
30
|
+
| 'null';
|
|
31
|
+
/**
|
|
32
|
+
* JSON Schema property definition.
|
|
33
|
+
*
|
|
34
|
+
* Describes a single property within a JSON Schema object, including
|
|
35
|
+
* type constraints, validation rules, and nested structure definitions.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const nameProperty: JSONSchemaProperty = {
|
|
40
|
+
* type: 'string',
|
|
41
|
+
* description: 'User name',
|
|
42
|
+
* minLength: 1,
|
|
43
|
+
* maxLength: 100
|
|
44
|
+
* };
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const tagsProperty: JSONSchemaProperty = {
|
|
50
|
+
* type: 'array',
|
|
51
|
+
* description: 'List of tags',
|
|
52
|
+
* items: { type: 'string' },
|
|
53
|
+
* minItems: 1,
|
|
54
|
+
* uniqueItems: true
|
|
55
|
+
* };
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
interface JSONSchemaProperty {
|
|
59
|
+
/** The JSON type of this property */
|
|
60
|
+
type: JSONSchemaPropertyType;
|
|
61
|
+
/** Human-readable description for the LLM */
|
|
62
|
+
description?: string;
|
|
63
|
+
/** Allowed values (enumeration) */
|
|
64
|
+
enum?: unknown[];
|
|
65
|
+
/** Constant value this property must equal */
|
|
66
|
+
const?: unknown;
|
|
67
|
+
/** Default value if not provided */
|
|
68
|
+
default?: unknown;
|
|
69
|
+
/** Minimum string length (string type only) */
|
|
70
|
+
minLength?: number;
|
|
71
|
+
/** Maximum string length (string type only) */
|
|
72
|
+
maxLength?: number;
|
|
73
|
+
/** Regular expression pattern for validation (string type only) */
|
|
74
|
+
pattern?: string;
|
|
75
|
+
/** Semantic format hint (string type only) */
|
|
76
|
+
format?: 'email' | 'uri' | 'date' | 'date-time' | 'uuid';
|
|
77
|
+
/** Minimum value inclusive (number/integer types only) */
|
|
78
|
+
minimum?: number;
|
|
79
|
+
/** Maximum value inclusive (number/integer types only) */
|
|
80
|
+
maximum?: number;
|
|
81
|
+
/** Minimum value exclusive (number/integer types only) */
|
|
82
|
+
exclusiveMinimum?: number;
|
|
83
|
+
/** Maximum value exclusive (number/integer types only) */
|
|
84
|
+
exclusiveMaximum?: number;
|
|
85
|
+
/** Value must be divisible by this (number/integer types only) */
|
|
86
|
+
multipleOf?: number;
|
|
87
|
+
/** Schema for array elements (array type only) */
|
|
88
|
+
items?: JSONSchemaProperty;
|
|
89
|
+
/** Minimum array length (array type only) */
|
|
90
|
+
minItems?: number;
|
|
91
|
+
/** Maximum array length (array type only) */
|
|
92
|
+
maxItems?: number;
|
|
93
|
+
/** Whether array elements must be unique (array type only) */
|
|
94
|
+
uniqueItems?: boolean;
|
|
95
|
+
/** Nested property definitions (object type only) */
|
|
96
|
+
properties?: Record<string, JSONSchemaProperty>;
|
|
97
|
+
/** List of required property names (object type only) */
|
|
98
|
+
required?: string[];
|
|
99
|
+
/** Whether additional properties are allowed (object type only) */
|
|
100
|
+
additionalProperties?: boolean;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Root JSON Schema for tool parameters or structured outputs.
|
|
104
|
+
*
|
|
105
|
+
* This is the top-level schema definition used when defining tool
|
|
106
|
+
* parameters or requesting structured output from an LLM.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```typescript
|
|
110
|
+
* const weatherToolSchema: JSONSchema = {
|
|
111
|
+
* type: 'object',
|
|
112
|
+
* description: 'Parameters for getting weather information',
|
|
113
|
+
* properties: {
|
|
114
|
+
* location: {
|
|
115
|
+
* type: 'string',
|
|
116
|
+
* description: 'City name or coordinates'
|
|
117
|
+
* },
|
|
118
|
+
* units: {
|
|
119
|
+
* type: 'string',
|
|
120
|
+
* enum: ['celsius', 'fahrenheit'],
|
|
121
|
+
* description: 'Temperature units'
|
|
122
|
+
* }
|
|
123
|
+
* },
|
|
124
|
+
* required: ['location']
|
|
125
|
+
* };
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
interface JSONSchema {
|
|
129
|
+
/** Root schemas are always objects */
|
|
130
|
+
type: 'object';
|
|
131
|
+
/** Property definitions for the object */
|
|
132
|
+
properties: Record<string, JSONSchemaProperty>;
|
|
133
|
+
/** List of required property names */
|
|
134
|
+
required?: string[];
|
|
135
|
+
/** Whether additional properties are allowed beyond those defined */
|
|
136
|
+
additionalProperties?: boolean;
|
|
137
|
+
/** Human-readable description of the schema's purpose */
|
|
138
|
+
description?: string;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @fileoverview Tool types for LLM function calling.
|
|
143
|
+
*
|
|
144
|
+
* Defines the interfaces for registering tools with LLMs, handling
|
|
145
|
+
* tool calls from the model, and managing tool execution strategies.
|
|
146
|
+
*
|
|
147
|
+
* @module types/tool
|
|
148
|
+
*/
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Provider-namespaced metadata for tools.
|
|
152
|
+
*
|
|
153
|
+
* Each provider can attach its own metadata under its namespace,
|
|
154
|
+
* enabling provider-specific features like caching, strict mode, etc.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* const metadata: ToolMetadata = {
|
|
159
|
+
* anthropic: { cache_control: { type: 'ephemeral' } },
|
|
160
|
+
* openrouter: { cache_control: { type: 'ephemeral', ttl: '1h' } }
|
|
161
|
+
* };
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
interface ToolMetadata {
|
|
165
|
+
[provider: string]: Record<string, unknown> | undefined;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Tool call requested by the model.
|
|
169
|
+
*
|
|
170
|
+
* Represents a single function call request from the LLM, including
|
|
171
|
+
* the tool name and parsed arguments.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```typescript
|
|
175
|
+
* const toolCall: ToolCall = {
|
|
176
|
+
* toolCallId: 'call_abc123',
|
|
177
|
+
* toolName: 'get_weather',
|
|
178
|
+
* arguments: { location: 'San Francisco', units: 'celsius' }
|
|
179
|
+
* };
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
interface ToolCall {
|
|
183
|
+
/** Unique identifier for this tool call, used to match results */
|
|
184
|
+
toolCallId: string;
|
|
185
|
+
/** Name of the tool being called */
|
|
186
|
+
toolName: string;
|
|
187
|
+
/** Parsed arguments for the tool call */
|
|
188
|
+
arguments: Record<string, unknown>;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Result of tool execution.
|
|
192
|
+
*
|
|
193
|
+
* Returned after executing a tool, containing the result data
|
|
194
|
+
* and whether an error occurred.
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```typescript
|
|
198
|
+
* const result: ToolResult = {
|
|
199
|
+
* toolCallId: 'call_abc123',
|
|
200
|
+
* result: { temperature: 72, conditions: 'sunny' }
|
|
201
|
+
* };
|
|
202
|
+
*
|
|
203
|
+
* // Error result
|
|
204
|
+
* const errorResult: ToolResult = {
|
|
205
|
+
* toolCallId: 'call_abc123',
|
|
206
|
+
* result: 'Location not found',
|
|
207
|
+
* isError: true
|
|
208
|
+
* };
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
interface ToolResult {
|
|
212
|
+
/** The tool call ID this result corresponds to */
|
|
213
|
+
toolCallId: string;
|
|
214
|
+
/** The result data (can be any serializable value) */
|
|
215
|
+
result: unknown;
|
|
216
|
+
/** Whether the tool execution resulted in an error */
|
|
217
|
+
isError?: boolean;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Tool definition for LLM function calling.
|
|
221
|
+
*
|
|
222
|
+
* Defines a tool that can be called by the LLM, including its
|
|
223
|
+
* name, description, parameter schema, and execution function.
|
|
224
|
+
*
|
|
225
|
+
* @typeParam TParams - The type of parameters the tool accepts
|
|
226
|
+
* @typeParam TResult - The type of result the tool returns
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* ```typescript
|
|
230
|
+
* const weatherTool: Tool<{ location: string }, WeatherData> = {
|
|
231
|
+
* name: 'get_weather',
|
|
232
|
+
* description: 'Get current weather for a location',
|
|
233
|
+
* parameters: {
|
|
234
|
+
* type: 'object',
|
|
235
|
+
* properties: {
|
|
236
|
+
* location: { type: 'string', description: 'City name' }
|
|
237
|
+
* },
|
|
238
|
+
* required: ['location']
|
|
239
|
+
* },
|
|
240
|
+
* run: async (params) => {
|
|
241
|
+
* return fetchWeather(params.location);
|
|
242
|
+
* }
|
|
243
|
+
* };
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
246
|
+
interface Tool<TParams = unknown, TResult = unknown> {
|
|
247
|
+
/** Tool name (must be unique within an llm() instance) */
|
|
248
|
+
name: string;
|
|
249
|
+
/** Human-readable description for the model to understand when to use this tool */
|
|
250
|
+
description: string;
|
|
251
|
+
/** JSON Schema defining the tool's parameters */
|
|
252
|
+
parameters: JSONSchema;
|
|
253
|
+
/**
|
|
254
|
+
* Provider-specific metadata, namespaced by provider name.
|
|
255
|
+
*
|
|
256
|
+
* Used for provider-specific features like prompt caching:
|
|
257
|
+
* @example
|
|
258
|
+
* ```typescript
|
|
259
|
+
* const tool: Tool = {
|
|
260
|
+
* name: 'search_docs',
|
|
261
|
+
* description: 'Search documentation',
|
|
262
|
+
* parameters: {...},
|
|
263
|
+
* run: async (params) => {...},
|
|
264
|
+
* metadata: {
|
|
265
|
+
* anthropic: { cache_control: { type: 'ephemeral' } }
|
|
266
|
+
* }
|
|
267
|
+
* };
|
|
268
|
+
* ```
|
|
269
|
+
*/
|
|
270
|
+
metadata?: ToolMetadata;
|
|
271
|
+
/**
|
|
272
|
+
* Executes the tool with the provided parameters.
|
|
273
|
+
*
|
|
274
|
+
* @param params - The parameters passed by the model
|
|
275
|
+
* @returns The tool result, synchronously or as a Promise
|
|
276
|
+
*/
|
|
277
|
+
run(params: TParams): TResult | Promise<TResult>;
|
|
278
|
+
/**
|
|
279
|
+
* Optional approval handler for sensitive operations.
|
|
280
|
+
*
|
|
281
|
+
* If provided, this function is called before the tool executes.
|
|
282
|
+
* Return false to prevent execution.
|
|
283
|
+
*
|
|
284
|
+
* @param params - The parameters the tool would be called with
|
|
285
|
+
* @returns Whether to approve the execution
|
|
286
|
+
*/
|
|
287
|
+
approval?(params: TParams): boolean | Promise<boolean>;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Result from onBeforeCall hook indicating whether to proceed and optionally transformed params.
|
|
291
|
+
*/
|
|
292
|
+
interface BeforeCallResult {
|
|
293
|
+
/** Whether to proceed with tool execution */
|
|
294
|
+
proceed: boolean;
|
|
295
|
+
/** Transformed parameters to use instead of the original (optional) */
|
|
296
|
+
params?: unknown;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Result from onAfterCall hook optionally containing a transformed result.
|
|
300
|
+
*/
|
|
301
|
+
interface AfterCallResult {
|
|
302
|
+
/** Transformed result to use instead of the original */
|
|
303
|
+
result: unknown;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Strategy for controlling tool execution behavior.
|
|
307
|
+
*
|
|
308
|
+
* Provides hooks for monitoring, controlling, and transforming the tool execution
|
|
309
|
+
* loop during LLM inference.
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* ```typescript
|
|
313
|
+
* const strategy: ToolUseStrategy = {
|
|
314
|
+
* maxIterations: 5,
|
|
315
|
+
* onToolCall: (tool, params) => {
|
|
316
|
+
* console.log(`Calling ${tool.name} with`, params);
|
|
317
|
+
* },
|
|
318
|
+
* // Transform input parameters
|
|
319
|
+
* onBeforeCall: (tool, params) => {
|
|
320
|
+
* if (tool.name === 'search') {
|
|
321
|
+
* return { proceed: true, params: { ...params, limit: 10 } };
|
|
322
|
+
* }
|
|
323
|
+
* return true;
|
|
324
|
+
* },
|
|
325
|
+
* // Transform output results
|
|
326
|
+
* onAfterCall: (tool, params, result) => {
|
|
327
|
+
* if (tool.name === 'fetch_data') {
|
|
328
|
+
* return { result: sanitize(result) };
|
|
329
|
+
* }
|
|
330
|
+
* },
|
|
331
|
+
* onMaxIterations: (iterations) => {
|
|
332
|
+
* console.warn(`Reached max iterations: ${iterations}`);
|
|
333
|
+
* }
|
|
334
|
+
* };
|
|
335
|
+
* ```
|
|
336
|
+
*/
|
|
337
|
+
interface ToolUseStrategy {
|
|
338
|
+
/** Maximum number of tool execution rounds (default: 10) */
|
|
339
|
+
maxIterations?: number;
|
|
340
|
+
/**
|
|
341
|
+
* Called when the model requests a tool call.
|
|
342
|
+
*
|
|
343
|
+
* @param tool - The tool being called
|
|
344
|
+
* @param params - The parameters for the call
|
|
345
|
+
*/
|
|
346
|
+
onToolCall?(tool: Tool, params: unknown): void | Promise<void>;
|
|
347
|
+
/**
|
|
348
|
+
* Called before tool execution. Can skip execution or transform parameters.
|
|
349
|
+
*
|
|
350
|
+
* @param tool - The tool about to be executed
|
|
351
|
+
* @param params - The parameters for the call
|
|
352
|
+
* @returns One of:
|
|
353
|
+
* - `false` to skip execution
|
|
354
|
+
* - `true` to proceed with original params
|
|
355
|
+
* - `BeforeCallResult` object to control execution and optionally transform params
|
|
356
|
+
*/
|
|
357
|
+
onBeforeCall?(tool: Tool, params: unknown): boolean | BeforeCallResult | Promise<boolean | BeforeCallResult>;
|
|
358
|
+
/**
|
|
359
|
+
* Called after tool execution completes. Can transform the result.
|
|
360
|
+
*
|
|
361
|
+
* @param tool - The tool that was executed
|
|
362
|
+
* @param params - The parameters that were used
|
|
363
|
+
* @param result - The result from the tool
|
|
364
|
+
* @returns Void to use original result, or `AfterCallResult` to transform it
|
|
365
|
+
*/
|
|
366
|
+
onAfterCall?(tool: Tool, params: unknown, result: unknown): void | AfterCallResult | Promise<void | AfterCallResult>;
|
|
367
|
+
/**
|
|
368
|
+
* Called when a tool execution throws an error.
|
|
369
|
+
*
|
|
370
|
+
* @param tool - The tool that failed
|
|
371
|
+
* @param params - The parameters that were used
|
|
372
|
+
* @param error - The error that was thrown
|
|
373
|
+
*/
|
|
374
|
+
onError?(tool: Tool, params: unknown, error: Error): void | Promise<void>;
|
|
375
|
+
/**
|
|
376
|
+
* Called when the maximum iteration limit is reached.
|
|
377
|
+
*
|
|
378
|
+
* @param iterations - The number of iterations that were performed
|
|
379
|
+
*/
|
|
380
|
+
onMaxIterations?(iterations: number): void | Promise<void>;
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Record of a completed tool execution.
|
|
384
|
+
*
|
|
385
|
+
* Contains all information about a tool call that was executed,
|
|
386
|
+
* including timing and result data.
|
|
387
|
+
*
|
|
388
|
+
* @example
|
|
389
|
+
* ```typescript
|
|
390
|
+
* const execution: ToolExecution = {
|
|
391
|
+
* toolName: 'get_weather',
|
|
392
|
+
* toolCallId: 'call_abc123',
|
|
393
|
+
* arguments: { location: 'San Francisco' },
|
|
394
|
+
* result: { temperature: 72 },
|
|
395
|
+
* isError: false,
|
|
396
|
+
* duration: 150,
|
|
397
|
+
* approved: true
|
|
398
|
+
* };
|
|
399
|
+
* ```
|
|
400
|
+
*/
|
|
401
|
+
interface ToolExecution {
|
|
402
|
+
/** Name of the tool that was called */
|
|
403
|
+
toolName: string;
|
|
404
|
+
/** Unique identifier for this tool call */
|
|
405
|
+
toolCallId: string;
|
|
406
|
+
/** Arguments that were passed to the tool */
|
|
407
|
+
arguments: Record<string, unknown>;
|
|
408
|
+
/** Result returned by the tool */
|
|
409
|
+
result: unknown;
|
|
410
|
+
/** Whether the tool execution resulted in an error */
|
|
411
|
+
isError: boolean;
|
|
412
|
+
/** Execution duration in milliseconds */
|
|
413
|
+
duration: number;
|
|
414
|
+
/** Whether approval was required and granted (undefined if no approval handler) */
|
|
415
|
+
approved?: boolean;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* @fileoverview Message types for conversation history.
|
|
420
|
+
*
|
|
421
|
+
* Defines the message classes used to represent conversation turns
|
|
422
|
+
* between users and assistants, including support for multimodal
|
|
423
|
+
* content and tool calls.
|
|
424
|
+
*
|
|
425
|
+
* @module types/messages
|
|
426
|
+
*/
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Message serialized to JSON format.
|
|
430
|
+
* Picks common fields from Message, converts timestamp to string.
|
|
431
|
+
*/
|
|
432
|
+
type MessageJSON = Pick<Message, 'id' | 'type' | 'metadata'> & {
|
|
433
|
+
timestamp: string;
|
|
434
|
+
content: ContentBlock[];
|
|
435
|
+
toolCalls?: ToolCall[];
|
|
436
|
+
results?: ToolResult[];
|
|
437
|
+
};
|
|
438
|
+
/**
|
|
439
|
+
* Message type discriminator.
|
|
440
|
+
*
|
|
441
|
+
* Used to distinguish between different message types in a conversation.
|
|
442
|
+
*/
|
|
443
|
+
type MessageType = 'user' | 'assistant' | 'tool_result';
|
|
444
|
+
/**
|
|
445
|
+
* Provider-namespaced metadata for messages.
|
|
446
|
+
*
|
|
447
|
+
* Each provider can attach its own metadata under its namespace,
|
|
448
|
+
* preventing conflicts between different providers.
|
|
449
|
+
*
|
|
450
|
+
* @example
|
|
451
|
+
* ```typescript
|
|
452
|
+
* const metadata: MessageMetadata = {
|
|
453
|
+
* openai: { model: 'gpt-4', finishReason: 'stop' },
|
|
454
|
+
* anthropic: { model: 'claude-3', stopReason: 'end_turn' }
|
|
455
|
+
* };
|
|
456
|
+
* ```
|
|
457
|
+
*/
|
|
458
|
+
interface MessageMetadata {
|
|
459
|
+
[provider: string]: Record<string, unknown> | undefined;
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Options for constructing messages.
|
|
463
|
+
*/
|
|
464
|
+
interface MessageOptions {
|
|
465
|
+
/** Custom message ID (auto-generated if not provided) */
|
|
466
|
+
id?: string;
|
|
467
|
+
/** Provider-specific metadata */
|
|
468
|
+
metadata?: MessageMetadata;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Abstract base class for all message types.
|
|
472
|
+
*
|
|
473
|
+
* Provides common functionality for user, assistant, and tool result
|
|
474
|
+
* messages, including content accessors and metadata handling.
|
|
475
|
+
*
|
|
476
|
+
* @example
|
|
477
|
+
* ```typescript
|
|
478
|
+
* // Access text content from any message
|
|
479
|
+
* const text = message.text;
|
|
480
|
+
*
|
|
481
|
+
* // Access images
|
|
482
|
+
* const images = message.images;
|
|
483
|
+
* ```
|
|
484
|
+
*/
|
|
485
|
+
declare abstract class Message {
|
|
486
|
+
/** Unique message identifier */
|
|
487
|
+
readonly id: string;
|
|
488
|
+
/** Timestamp when the message was created */
|
|
489
|
+
readonly timestamp: Date;
|
|
490
|
+
/** Provider-specific metadata, namespaced by provider name */
|
|
491
|
+
readonly metadata?: MessageMetadata;
|
|
492
|
+
/** Message type discriminator (implemented by subclasses) */
|
|
493
|
+
abstract readonly type: MessageType;
|
|
494
|
+
/**
|
|
495
|
+
* Returns the content blocks for this message.
|
|
496
|
+
* Implemented by subclasses to provide type-specific content.
|
|
497
|
+
*/
|
|
498
|
+
protected abstract getContent(): ContentBlock[];
|
|
499
|
+
/**
|
|
500
|
+
* Creates a new message instance.
|
|
501
|
+
*
|
|
502
|
+
* @param options - Optional message ID and metadata
|
|
503
|
+
*/
|
|
504
|
+
constructor(options?: MessageOptions);
|
|
505
|
+
/**
|
|
506
|
+
* Concatenated text content from all text blocks.
|
|
507
|
+
* Blocks are joined with double newlines.
|
|
508
|
+
*/
|
|
509
|
+
get text(): string;
|
|
510
|
+
/**
|
|
511
|
+
* All image content blocks in this message.
|
|
512
|
+
*/
|
|
513
|
+
get images(): ImageBlock[];
|
|
514
|
+
/**
|
|
515
|
+
* All audio content blocks in this message.
|
|
516
|
+
*/
|
|
517
|
+
get audio(): AudioBlock[];
|
|
518
|
+
/**
|
|
519
|
+
* All video content blocks in this message.
|
|
520
|
+
*/
|
|
521
|
+
get video(): VideoBlock[];
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* User input message.
|
|
525
|
+
*
|
|
526
|
+
* Represents a message from the user, which can contain text and/or
|
|
527
|
+
* multimodal content like images, audio, or video.
|
|
528
|
+
*
|
|
529
|
+
* @example
|
|
530
|
+
* ```typescript
|
|
531
|
+
* // Simple text message
|
|
532
|
+
* const msg = new UserMessage('Hello, world!');
|
|
533
|
+
*
|
|
534
|
+
* // Multimodal message
|
|
535
|
+
* const msg = new UserMessage([
|
|
536
|
+
* { type: 'text', text: 'What is in this image?' },
|
|
537
|
+
* { type: 'image', source: { type: 'url', url: '...' }, mimeType: 'image/png' }
|
|
538
|
+
* ]);
|
|
539
|
+
* ```
|
|
540
|
+
*/
|
|
541
|
+
declare class UserMessage extends Message {
|
|
542
|
+
/** Message type discriminator */
|
|
543
|
+
readonly type: "user";
|
|
544
|
+
/** Content blocks in this message */
|
|
545
|
+
readonly content: UserContent[];
|
|
546
|
+
/**
|
|
547
|
+
* Creates a new user message.
|
|
548
|
+
*
|
|
549
|
+
* @param content - String (converted to TextBlock) or array of content blocks
|
|
550
|
+
* @param options - Optional message ID and metadata
|
|
551
|
+
*/
|
|
552
|
+
constructor(content: string | UserContent[], options?: MessageOptions);
|
|
553
|
+
protected getContent(): ContentBlock[];
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Assistant response message.
|
|
557
|
+
*
|
|
558
|
+
* Represents a response from the AI assistant, which may contain
|
|
559
|
+
* text, media content, and/or tool call requests.
|
|
560
|
+
*
|
|
561
|
+
* @example
|
|
562
|
+
* ```typescript
|
|
563
|
+
* // Simple text response
|
|
564
|
+
* const msg = new AssistantMessage('Hello! How can I help?');
|
|
565
|
+
*
|
|
566
|
+
* // Response with tool calls
|
|
567
|
+
* const msg = new AssistantMessage(
|
|
568
|
+
* 'Let me check the weather...',
|
|
569
|
+
* [{ toolCallId: 'call_1', toolName: 'get_weather', arguments: { location: 'NYC' } }]
|
|
570
|
+
* );
|
|
571
|
+
* ```
|
|
572
|
+
*/
|
|
573
|
+
declare class AssistantMessage extends Message {
|
|
574
|
+
/** Message type discriminator */
|
|
575
|
+
readonly type: "assistant";
|
|
576
|
+
/** Content blocks in this message */
|
|
577
|
+
readonly content: AssistantContent[];
|
|
578
|
+
/** Tool calls requested by the model (if any) */
|
|
579
|
+
readonly toolCalls?: ToolCall[];
|
|
580
|
+
/**
|
|
581
|
+
* Creates a new assistant message.
|
|
582
|
+
*
|
|
583
|
+
* @param content - String (converted to TextBlock) or array of content blocks
|
|
584
|
+
* @param toolCalls - Tool calls requested by the model
|
|
585
|
+
* @param options - Optional message ID and metadata
|
|
586
|
+
*/
|
|
587
|
+
constructor(content: string | AssistantContent[], toolCalls?: ToolCall[], options?: MessageOptions);
|
|
588
|
+
protected getContent(): ContentBlock[];
|
|
589
|
+
/**
|
|
590
|
+
* Whether this message contains tool call requests.
|
|
591
|
+
*/
|
|
592
|
+
get hasToolCalls(): boolean;
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Tool execution result message.
|
|
596
|
+
*
|
|
597
|
+
* Contains the results of executing one or more tool calls,
|
|
598
|
+
* sent back to the model for further processing.
|
|
599
|
+
*
|
|
600
|
+
* @example
|
|
601
|
+
* ```typescript
|
|
602
|
+
* const msg = new ToolResultMessage([
|
|
603
|
+
* { toolCallId: 'call_1', result: { temperature: 72, conditions: 'sunny' } },
|
|
604
|
+
* { toolCallId: 'call_2', result: 'File not found', isError: true }
|
|
605
|
+
* ]);
|
|
606
|
+
* ```
|
|
607
|
+
*/
|
|
608
|
+
declare class ToolResultMessage extends Message {
|
|
609
|
+
/** Message type discriminator */
|
|
610
|
+
readonly type: "tool_result";
|
|
611
|
+
/** Results from tool executions */
|
|
612
|
+
readonly results: ToolResult[];
|
|
613
|
+
/**
|
|
614
|
+
* Creates a new tool result message.
|
|
615
|
+
*
|
|
616
|
+
* @param results - Array of tool execution results
|
|
617
|
+
* @param options - Optional message ID and metadata
|
|
618
|
+
*/
|
|
619
|
+
constructor(results: ToolResult[], options?: MessageOptions);
|
|
620
|
+
protected getContent(): ContentBlock[];
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* Type guard for UserMessage.
|
|
624
|
+
*
|
|
625
|
+
* @param msg - The message to check
|
|
626
|
+
* @returns True if the message is a UserMessage
|
|
627
|
+
*
|
|
628
|
+
* @example
|
|
629
|
+
* ```typescript
|
|
630
|
+
* if (isUserMessage(msg)) {
|
|
631
|
+
* console.log('User said:', msg.text);
|
|
632
|
+
* }
|
|
633
|
+
* ```
|
|
634
|
+
*/
|
|
635
|
+
declare function isUserMessage(msg: Message): msg is UserMessage;
|
|
636
|
+
/**
|
|
637
|
+
* Type guard for AssistantMessage.
|
|
638
|
+
*
|
|
639
|
+
* @param msg - The message to check
|
|
640
|
+
* @returns True if the message is an AssistantMessage
|
|
641
|
+
*
|
|
642
|
+
* @example
|
|
643
|
+
* ```typescript
|
|
644
|
+
* if (isAssistantMessage(msg)) {
|
|
645
|
+
* console.log('Assistant said:', msg.text);
|
|
646
|
+
* if (msg.hasToolCalls) {
|
|
647
|
+
* console.log('Tool calls:', msg.toolCalls);
|
|
648
|
+
* }
|
|
649
|
+
* }
|
|
650
|
+
* ```
|
|
651
|
+
*/
|
|
652
|
+
declare function isAssistantMessage(msg: Message): msg is AssistantMessage;
|
|
653
|
+
/**
|
|
654
|
+
* Type guard for ToolResultMessage.
|
|
655
|
+
*
|
|
656
|
+
* @param msg - The message to check
|
|
657
|
+
* @returns True if the message is a ToolResultMessage
|
|
658
|
+
*
|
|
659
|
+
* @example
|
|
660
|
+
* ```typescript
|
|
661
|
+
* if (isToolResultMessage(msg)) {
|
|
662
|
+
* for (const result of msg.results) {
|
|
663
|
+
* console.log(`Tool ${result.toolCallId}:`, result.result);
|
|
664
|
+
* }
|
|
665
|
+
* }
|
|
666
|
+
* ```
|
|
667
|
+
*/
|
|
668
|
+
declare function isToolResultMessage(msg: Message): msg is ToolResultMessage;
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* @fileoverview Turn types for inference results.
|
|
672
|
+
*
|
|
673
|
+
* A Turn represents the complete result of one inference call, including
|
|
674
|
+
* all messages produced during tool execution loops, token usage, and
|
|
675
|
+
* optional structured output data.
|
|
676
|
+
*
|
|
677
|
+
* @module types/turn
|
|
678
|
+
*/
|
|
679
|
+
|
|
680
|
+
/**
|
|
681
|
+
* Token usage information for an inference request.
|
|
682
|
+
*
|
|
683
|
+
* Tracks input and output tokens across all inference cycles,
|
|
684
|
+
* with optional per-cycle breakdown and cache metrics.
|
|
685
|
+
*
|
|
686
|
+
* @example
|
|
687
|
+
* ```typescript
|
|
688
|
+
* const usage: TokenUsage = {
|
|
689
|
+
* inputTokens: 150,
|
|
690
|
+
* outputTokens: 50,
|
|
691
|
+
* totalTokens: 200,
|
|
692
|
+
* cacheReadTokens: 100,
|
|
693
|
+
* cacheWriteTokens: 50,
|
|
694
|
+
* cycles: [
|
|
695
|
+
* { inputTokens: 100, outputTokens: 30, cacheReadTokens: 0, cacheWriteTokens: 50 },
|
|
696
|
+
* { inputTokens: 50, outputTokens: 20, cacheReadTokens: 100, cacheWriteTokens: 0 }
|
|
697
|
+
* ]
|
|
698
|
+
* };
|
|
699
|
+
* ```
|
|
700
|
+
*/
|
|
701
|
+
interface TokenUsage {
|
|
702
|
+
/** Total input tokens across all cycles */
|
|
703
|
+
inputTokens: number;
|
|
704
|
+
/** Total output tokens across all cycles */
|
|
705
|
+
outputTokens: number;
|
|
706
|
+
/** Sum of input and output tokens */
|
|
707
|
+
totalTokens: number;
|
|
708
|
+
/**
|
|
709
|
+
* Tokens read from cache (cache hits).
|
|
710
|
+
* Returns 0 for providers that don't support or report cache metrics.
|
|
711
|
+
*/
|
|
712
|
+
cacheReadTokens: number;
|
|
713
|
+
/**
|
|
714
|
+
* Tokens written to cache (cache misses that were cached).
|
|
715
|
+
* Only Anthropic reports this metric; returns 0 for other providers.
|
|
716
|
+
*/
|
|
717
|
+
cacheWriteTokens: number;
|
|
718
|
+
/** Per-cycle token breakdown (if multiple cycles occurred) */
|
|
719
|
+
cycles?: Array<{
|
|
720
|
+
inputTokens: number;
|
|
721
|
+
outputTokens: number;
|
|
722
|
+
cacheReadTokens: number;
|
|
723
|
+
cacheWriteTokens: number;
|
|
724
|
+
}>;
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* A Turn represents the complete result of one inference call.
|
|
728
|
+
*
|
|
729
|
+
* Includes all messages produced during tool execution loops,
|
|
730
|
+
* the final assistant response, token usage, and optional
|
|
731
|
+
* structured output data.
|
|
732
|
+
*
|
|
733
|
+
* @typeParam TData - Type of the structured output data
|
|
734
|
+
*
|
|
735
|
+
* @example
|
|
736
|
+
* ```typescript
|
|
737
|
+
* const turn = await instance.generate('Hello');
|
|
738
|
+
* console.log(turn.response.text);
|
|
739
|
+
* console.log(`Used ${turn.usage.totalTokens} tokens in ${turn.cycles} cycles`);
|
|
740
|
+
*
|
|
741
|
+
* // With structured output
|
|
742
|
+
* interface WeatherData { temperature: number; conditions: string; }
|
|
743
|
+
* const turn = await instance.generate<WeatherData>('Get weather');
|
|
744
|
+
* console.log(turn.data?.temperature);
|
|
745
|
+
* ```
|
|
746
|
+
*/
|
|
747
|
+
interface Turn<TData = unknown> {
|
|
748
|
+
/**
|
|
749
|
+
* All messages produced during this inference, in chronological order.
|
|
750
|
+
* Includes UserMessage, AssistantMessage (may include toolCalls), and ToolResultMessage.
|
|
751
|
+
*/
|
|
752
|
+
readonly messages: Message[];
|
|
753
|
+
/** The final assistant response (last AssistantMessage in the turn) */
|
|
754
|
+
readonly response: AssistantMessage;
|
|
755
|
+
/** Tool executions that occurred during this turn */
|
|
756
|
+
readonly toolExecutions: ToolExecution[];
|
|
757
|
+
/** Aggregate token usage for the entire turn */
|
|
758
|
+
readonly usage: TokenUsage;
|
|
759
|
+
/** Total number of inference cycles (1 + number of tool rounds) */
|
|
760
|
+
readonly cycles: number;
|
|
761
|
+
/**
|
|
762
|
+
* Structured output data (if a structure schema was provided).
|
|
763
|
+
* Type is inferred from the schema when using TypeScript.
|
|
764
|
+
*/
|
|
765
|
+
readonly data?: TData;
|
|
766
|
+
}
|
|
767
|
+
/**
|
|
768
|
+
* Turn serialized to JSON format.
|
|
769
|
+
* Messages are converted to MessageJSON, response is omitted (computed from messages).
|
|
770
|
+
*/
|
|
771
|
+
type TurnJSON = Omit<Turn, 'messages' | 'response'> & {
|
|
772
|
+
messages: MessageJSON[];
|
|
773
|
+
};
|
|
774
|
+
/**
|
|
775
|
+
* Creates a Turn from accumulated inference data.
|
|
776
|
+
*
|
|
777
|
+
* @typeParam TData - Type of the structured output data
|
|
778
|
+
* @param messages - All messages produced during the inference
|
|
779
|
+
* @param toolExecutions - Record of all tool executions
|
|
780
|
+
* @param usage - Aggregate token usage
|
|
781
|
+
* @param cycles - Number of inference cycles
|
|
782
|
+
* @param data - Optional structured output data
|
|
783
|
+
* @returns A complete Turn object
|
|
784
|
+
* @throws Error if no assistant message is found in the messages
|
|
785
|
+
*
|
|
786
|
+
* @example
|
|
787
|
+
* ```typescript
|
|
788
|
+
* const turn = createTurn(
|
|
789
|
+
* [userMsg, assistantMsg],
|
|
790
|
+
* [],
|
|
791
|
+
* { inputTokens: 100, outputTokens: 50, totalTokens: 150 },
|
|
792
|
+
* 1
|
|
793
|
+
* );
|
|
794
|
+
* ```
|
|
795
|
+
*/
|
|
796
|
+
declare function createTurn<TData = unknown>(messages: Message[], toolExecutions: ToolExecution[], usage: TokenUsage, cycles: number, data?: TData): Turn<TData>;
|
|
797
|
+
/**
|
|
798
|
+
* Creates an empty TokenUsage object.
|
|
799
|
+
*
|
|
800
|
+
* @returns A TokenUsage with all values set to zero
|
|
801
|
+
*
|
|
802
|
+
* @example
|
|
803
|
+
* ```typescript
|
|
804
|
+
* const usage = emptyUsage();
|
|
805
|
+
* // { inputTokens: 0, outputTokens: 0, totalTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0, cycles: [] }
|
|
806
|
+
* ```
|
|
807
|
+
*/
|
|
808
|
+
declare function emptyUsage(): TokenUsage;
|
|
809
|
+
/**
|
|
810
|
+
* Aggregates token usage from multiple inference cycles.
|
|
811
|
+
*
|
|
812
|
+
* @param usages - Array of TokenUsage objects to aggregate
|
|
813
|
+
* @returns Combined TokenUsage with per-cycle breakdown
|
|
814
|
+
*
|
|
815
|
+
* @example
|
|
816
|
+
* ```typescript
|
|
817
|
+
* const cycle1 = { inputTokens: 100, outputTokens: 30, totalTokens: 130, cacheReadTokens: 50, cacheWriteTokens: 0 };
|
|
818
|
+
* const cycle2 = { inputTokens: 150, outputTokens: 40, totalTokens: 190, cacheReadTokens: 100, cacheWriteTokens: 0 };
|
|
819
|
+
* const total = aggregateUsage([cycle1, cycle2]);
|
|
820
|
+
* // { inputTokens: 250, outputTokens: 70, totalTokens: 320, cacheReadTokens: 150, cacheWriteTokens: 0, cycles: [...] }
|
|
821
|
+
* ```
|
|
822
|
+
*/
|
|
823
|
+
declare function aggregateUsage(usages: TokenUsage[]): TokenUsage;
|
|
824
|
+
|
|
825
|
+
/**
|
|
826
|
+
* @fileoverview Streaming types for real-time LLM responses.
|
|
827
|
+
*
|
|
828
|
+
* Defines the event types and interfaces for streaming LLM inference,
|
|
829
|
+
* including text deltas, tool call deltas, and control events.
|
|
830
|
+
*
|
|
831
|
+
* @module types/stream
|
|
832
|
+
*/
|
|
833
|
+
|
|
834
|
+
/**
|
|
835
|
+
* Stream event type discriminators.
|
|
836
|
+
*
|
|
837
|
+
* Each event type represents a different kind of streaming update
|
|
838
|
+
* from the LLM provider.
|
|
839
|
+
*/
|
|
840
|
+
type StreamEventType =
|
|
841
|
+
/** Incremental text output */
|
|
842
|
+
'text_delta'
|
|
843
|
+
/** Incremental reasoning/thinking output */
|
|
844
|
+
| 'reasoning_delta'
|
|
845
|
+
/** Incremental image data */
|
|
846
|
+
| 'image_delta'
|
|
847
|
+
/** Incremental audio data */
|
|
848
|
+
| 'audio_delta'
|
|
849
|
+
/** Incremental video data */
|
|
850
|
+
| 'video_delta'
|
|
851
|
+
/** Incremental tool call data (arguments being streamed) */
|
|
852
|
+
| 'tool_call_delta'
|
|
853
|
+
/** Tool execution has started */
|
|
854
|
+
| 'tool_execution_start'
|
|
855
|
+
/** Tool execution has completed */
|
|
856
|
+
| 'tool_execution_end'
|
|
857
|
+
/** Beginning of a message */
|
|
858
|
+
| 'message_start'
|
|
859
|
+
/** End of a message */
|
|
860
|
+
| 'message_stop'
|
|
861
|
+
/** Beginning of a content block */
|
|
862
|
+
| 'content_block_start'
|
|
863
|
+
/** End of a content block */
|
|
864
|
+
| 'content_block_stop';
|
|
865
|
+
/**
|
|
866
|
+
* Event delta data payload.
|
|
867
|
+
*
|
|
868
|
+
* Contains the type-specific data for a streaming event.
|
|
869
|
+
* Different fields are populated depending on the event type.
|
|
870
|
+
*/
|
|
871
|
+
interface EventDelta {
|
|
872
|
+
/** Incremental text content (for text_delta, reasoning_delta) */
|
|
873
|
+
text?: string;
|
|
874
|
+
/** Incremental binary data (for image_delta, audio_delta, video_delta) */
|
|
875
|
+
data?: Uint8Array;
|
|
876
|
+
/** Tool call identifier (for tool_call_delta, tool_execution_start/end) */
|
|
877
|
+
toolCallId?: string;
|
|
878
|
+
/** Tool name (for tool_call_delta, tool_execution_start/end) */
|
|
879
|
+
toolName?: string;
|
|
880
|
+
/** Incremental JSON arguments string (for tool_call_delta) */
|
|
881
|
+
argumentsJson?: string;
|
|
882
|
+
/** Tool execution result (for tool_execution_end) */
|
|
883
|
+
result?: unknown;
|
|
884
|
+
/** Whether tool execution resulted in an error (for tool_execution_end) */
|
|
885
|
+
isError?: boolean;
|
|
886
|
+
/** Timestamp in milliseconds (for tool_execution_start/end) */
|
|
887
|
+
timestamp?: number;
|
|
888
|
+
}
|
|
889
|
+
/**
|
|
890
|
+
* A single streaming event from the LLM.
|
|
891
|
+
*
|
|
892
|
+
* Events are emitted in order as the model generates output,
|
|
893
|
+
* allowing for real-time display of responses.
|
|
894
|
+
*
|
|
895
|
+
* @example
|
|
896
|
+
* ```typescript
|
|
897
|
+
* for await (const event of stream) {
|
|
898
|
+
* if (event.type === 'text_delta') {
|
|
899
|
+
* process.stdout.write(event.delta.text ?? '');
|
|
900
|
+
* } else if (event.type === 'tool_call_delta') {
|
|
901
|
+
* console.log('Tool:', event.delta.toolName);
|
|
902
|
+
* }
|
|
903
|
+
* }
|
|
904
|
+
* ```
|
|
905
|
+
*/
|
|
906
|
+
interface StreamEvent {
|
|
907
|
+
/** Event type discriminator */
|
|
908
|
+
type: StreamEventType;
|
|
909
|
+
/** Index of the content block this event belongs to */
|
|
910
|
+
index: number;
|
|
911
|
+
/** Event-specific data payload */
|
|
912
|
+
delta: EventDelta;
|
|
913
|
+
}
|
|
914
|
+
/**
|
|
915
|
+
* Stream result - an async iterable that also provides the final turn.
|
|
916
|
+
*
|
|
917
|
+
* Allows consuming streaming events while also awaiting the complete
|
|
918
|
+
* Turn result after streaming finishes.
|
|
919
|
+
*
|
|
920
|
+
* @typeParam TData - Type of the structured output data
|
|
921
|
+
*
|
|
922
|
+
* @example
|
|
923
|
+
* ```typescript
|
|
924
|
+
* const stream = instance.stream('Tell me a story');
|
|
925
|
+
*
|
|
926
|
+
* // Consume streaming events
|
|
927
|
+
* for await (const event of stream) {
|
|
928
|
+
* if (event.type === 'text_delta') {
|
|
929
|
+
* process.stdout.write(event.delta.text ?? '');
|
|
930
|
+
* }
|
|
931
|
+
* }
|
|
932
|
+
*
|
|
933
|
+
* // Get the complete turn after streaming
|
|
934
|
+
* const turn = await stream.turn;
|
|
935
|
+
* console.log('\n\nTokens used:', turn.usage.totalTokens);
|
|
936
|
+
* ```
|
|
937
|
+
*/
|
|
938
|
+
interface StreamResult<TData = unknown> extends AsyncIterable<StreamEvent> {
|
|
939
|
+
/**
|
|
940
|
+
* Promise that resolves to the complete Turn after streaming finishes.
|
|
941
|
+
*/
|
|
942
|
+
readonly turn: Promise<Turn<TData>>;
|
|
943
|
+
/**
|
|
944
|
+
* Aborts the stream, stopping further events and cancelling the request.
|
|
945
|
+
*/
|
|
946
|
+
abort(): void;
|
|
947
|
+
}
|
|
948
|
+
/**
|
|
949
|
+
* Creates a StreamResult from an async generator and completion promise.
|
|
950
|
+
*
|
|
951
|
+
* @typeParam TData - Type of the structured output data
|
|
952
|
+
* @param generator - Async generator that yields stream events
|
|
953
|
+
* @param turnPromise - Promise that resolves to the complete Turn
|
|
954
|
+
* @param abortController - Controller for aborting the stream
|
|
955
|
+
* @returns A StreamResult that can be iterated and awaited
|
|
956
|
+
*
|
|
957
|
+
* @example
|
|
958
|
+
* ```typescript
|
|
959
|
+
* const abortController = new AbortController();
|
|
960
|
+
* const stream = createStreamResult(
|
|
961
|
+
* eventGenerator(),
|
|
962
|
+
* turnPromise,
|
|
963
|
+
* abortController
|
|
964
|
+
* );
|
|
965
|
+
* ```
|
|
966
|
+
*/
|
|
967
|
+
declare function createStreamResult<TData = unknown>(generator: AsyncGenerator<StreamEvent, void, unknown>, turnPromise: Promise<Turn<TData>>, abortController: AbortController): StreamResult<TData>;
|
|
968
|
+
/**
|
|
969
|
+
* Creates a text delta stream event.
|
|
970
|
+
*
|
|
971
|
+
* @param text - The incremental text content
|
|
972
|
+
* @param index - Content block index (default: 0)
|
|
973
|
+
* @returns A text_delta StreamEvent
|
|
974
|
+
*/
|
|
975
|
+
declare function textDelta(text: string, index?: number): StreamEvent;
|
|
976
|
+
/**
|
|
977
|
+
* Creates a tool call delta stream event.
|
|
978
|
+
*
|
|
979
|
+
* @param toolCallId - Unique identifier for the tool call
|
|
980
|
+
* @param toolName - Name of the tool being called
|
|
981
|
+
* @param argumentsJson - Incremental JSON arguments string
|
|
982
|
+
* @param index - Content block index (default: 0)
|
|
983
|
+
* @returns A tool_call_delta StreamEvent
|
|
984
|
+
*/
|
|
985
|
+
declare function toolCallDelta(toolCallId: string, toolName: string, argumentsJson: string, index?: number): StreamEvent;
|
|
986
|
+
/**
|
|
987
|
+
* Creates a message start stream event.
|
|
988
|
+
*
|
|
989
|
+
* @returns A message_start StreamEvent
|
|
990
|
+
*/
|
|
991
|
+
declare function messageStart(): StreamEvent;
|
|
992
|
+
/**
|
|
993
|
+
* Creates a message stop stream event.
|
|
994
|
+
*
|
|
995
|
+
* @returns A message_stop StreamEvent
|
|
996
|
+
*/
|
|
997
|
+
declare function messageStop(): StreamEvent;
|
|
998
|
+
/**
|
|
999
|
+
* Creates a content block start stream event.
|
|
1000
|
+
*
|
|
1001
|
+
* @param index - The content block index starting
|
|
1002
|
+
* @returns A content_block_start StreamEvent
|
|
1003
|
+
*/
|
|
1004
|
+
declare function contentBlockStart(index: number): StreamEvent;
|
|
1005
|
+
/**
|
|
1006
|
+
* Creates a content block stop stream event.
|
|
1007
|
+
*
|
|
1008
|
+
* @param index - The content block index stopping
|
|
1009
|
+
* @returns A content_block_stop StreamEvent
|
|
1010
|
+
*/
|
|
1011
|
+
declare function contentBlockStop(index: number): StreamEvent;
|
|
1012
|
+
|
|
1013
|
+
export { AssistantMessage as A, type BeforeCallResult as B, messageStart as C, messageStop as D, type EventDelta as E, contentBlockStart as F, contentBlockStop as G, type TurnJSON as H, type JSONSchema as J, Message as M, type StreamResult as S, type Turn as T, UserMessage as U, type MessageType as a, type MessageJSON as b, type Tool as c, type ToolUseStrategy as d, type TokenUsage as e, type StreamEvent as f, type JSONSchemaProperty as g, type JSONSchemaPropertyType as h, type ToolCall as i, type ToolResult as j, type ToolMetadata as k, type AfterCallResult as l, type ToolExecution as m, ToolResultMessage as n, isUserMessage as o, isAssistantMessage as p, isToolResultMessage as q, type MessageMetadata as r, type MessageOptions as s, createTurn as t, emptyUsage as u, aggregateUsage as v, type StreamEventType as w, createStreamResult as x, textDelta as y, toolCallDelta as z };
|