@providerprotocol/ai 0.0.27 → 0.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/dist/anthropic/index.d.ts +1 -1
  2. package/dist/anthropic/index.js +38 -1
  3. package/dist/anthropic/index.js.map +1 -1
  4. package/dist/{chunk-6AZVUI6H.js → chunk-ILR2D5PN.js} +7 -1
  5. package/dist/chunk-ILR2D5PN.js.map +1 -0
  6. package/dist/{chunk-MKDLXV4O.js → chunk-NSE7QN3P.js} +1 -1
  7. package/dist/chunk-NSE7QN3P.js.map +1 -0
  8. package/dist/embedding-DtyOFIsS.d.ts +158 -0
  9. package/dist/google/index.d.ts +1 -1
  10. package/dist/google/index.js +41 -4
  11. package/dist/google/index.js.map +1 -1
  12. package/dist/http/index.d.ts +2 -2
  13. package/dist/index.d.ts +430 -514
  14. package/dist/index.js +627 -3
  15. package/dist/index.js.map +1 -1
  16. package/dist/llm-DgDEy9il.d.ts +3118 -0
  17. package/dist/ollama/index.d.ts +1 -1
  18. package/dist/ollama/index.js +2 -1
  19. package/dist/ollama/index.js.map +1 -1
  20. package/dist/openai/index.d.ts +1 -1
  21. package/dist/openai/index.js +70 -3
  22. package/dist/openai/index.js.map +1 -1
  23. package/dist/openrouter/index.d.ts +20 -2
  24. package/dist/openrouter/index.js +134 -13
  25. package/dist/openrouter/index.js.map +1 -1
  26. package/dist/proxy/index.d.ts +2 -2
  27. package/dist/proxy/index.js +3 -2
  28. package/dist/proxy/index.js.map +1 -1
  29. package/dist/{retry-BhX8mIrL.d.ts → retry-DXLQnTuU.d.ts} +1 -1
  30. package/dist/xai/index.d.ts +1 -1
  31. package/dist/xai/index.js +7 -3
  32. package/dist/xai/index.js.map +1 -1
  33. package/package.json +1 -1
  34. package/dist/chunk-6AZVUI6H.js.map +0 -1
  35. package/dist/chunk-MKDLXV4O.js.map +0 -1
  36. package/dist/embedding-CK5oa38O.d.ts +0 -1235
  37. package/dist/provider-6-mJYOOl.d.ts +0 -1474
@@ -1,1235 +0,0 @@
1
- import { C as ContentBlock, k as ImageBlock, l as AudioBlock, V as VideoBlock, R as ReasoningBlock, A as AssistantContent, U as UserContent, P as ProviderIdentity, a as ProviderConfig, N as EmbeddingInput, J as EmbeddingUsage, D as BoundEmbeddingModel } from './provider-6-mJYOOl.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 union.
440
- *
441
- * This type is derived from {@link MessageRole} constants. The name `MessageType`
442
- * is kept for backward compatibility; `MessageRole` works as both the const
443
- * object and this type.
444
- */
445
- type MessageType = (typeof MessageRole)[keyof typeof MessageRole];
446
- /**
447
- * Message role/type constants.
448
- *
449
- * Use these constants instead of raw strings for type-safe message handling:
450
- *
451
- * @example
452
- * ```typescript
453
- * import { MessageRole, isUserMessage } from 'upp';
454
- *
455
- * if (message.type === MessageRole.User) {
456
- * console.log('User said:', message.text);
457
- * } else if (message.type === MessageRole.Assistant) {
458
- * console.log('Assistant replied:', message.text);
459
- * }
460
- * ```
461
- */
462
- declare const MessageRole: {
463
- /** User message */
464
- readonly User: "user";
465
- /** Assistant/model response */
466
- readonly Assistant: "assistant";
467
- /** Tool execution result */
468
- readonly ToolResult: "tool_result";
469
- };
470
- /**
471
- * Type alias for MessageType, allowing `MessageRole` to work as both const and type.
472
- */
473
- type MessageRole = MessageType;
474
- /**
475
- * Provider-namespaced metadata for messages.
476
- *
477
- * Each provider can attach its own metadata under its namespace,
478
- * preventing conflicts between different providers.
479
- *
480
- * @example
481
- * ```typescript
482
- * const metadata: MessageMetadata = {
483
- * openai: { model: 'gpt-4', finishReason: 'stop' },
484
- * anthropic: { model: 'claude-3', stopReason: 'end_turn' }
485
- * };
486
- * ```
487
- */
488
- interface MessageMetadata {
489
- [provider: string]: Record<string, unknown> | undefined;
490
- }
491
- /**
492
- * Options for constructing messages.
493
- */
494
- interface MessageOptions {
495
- /** Custom message ID (auto-generated if not provided) */
496
- id?: string;
497
- /** Provider-specific metadata */
498
- metadata?: MessageMetadata;
499
- }
500
- /**
501
- * Abstract base class for all message types.
502
- *
503
- * Provides common functionality for user, assistant, and tool result
504
- * messages, including content accessors and metadata handling.
505
- *
506
- * @example
507
- * ```typescript
508
- * // Access text content from any message
509
- * const text = message.text;
510
- *
511
- * // Access images
512
- * const images = message.images;
513
- * ```
514
- */
515
- declare abstract class Message {
516
- /** Unique message identifier */
517
- readonly id: string;
518
- /** Timestamp when the message was created */
519
- readonly timestamp: Date;
520
- /** Provider-specific metadata, namespaced by provider name */
521
- readonly metadata?: MessageMetadata;
522
- /** Message type discriminator (implemented by subclasses) */
523
- abstract readonly type: MessageType;
524
- /**
525
- * Returns the content blocks for this message.
526
- * Implemented by subclasses to provide type-specific content.
527
- */
528
- protected abstract getContent(): ContentBlock[];
529
- /**
530
- * Creates a new message instance.
531
- *
532
- * @param options - Optional message ID and metadata
533
- */
534
- constructor(options?: MessageOptions);
535
- /**
536
- * Concatenated text content from all text blocks.
537
- * Blocks are joined with double newlines.
538
- */
539
- get text(): string;
540
- /**
541
- * All image content blocks in this message.
542
- */
543
- get images(): ImageBlock[];
544
- /**
545
- * All audio content blocks in this message.
546
- */
547
- get audio(): AudioBlock[];
548
- /**
549
- * All video content blocks in this message.
550
- */
551
- get video(): VideoBlock[];
552
- /**
553
- * All reasoning/thinking content blocks in this message.
554
- * Available when using extended thinking models.
555
- */
556
- get reasoning(): ReasoningBlock[];
557
- }
558
- /**
559
- * User input message.
560
- *
561
- * Represents a message from the user, which can contain text and/or
562
- * multimodal content like images, audio, or video.
563
- *
564
- * @example
565
- * ```typescript
566
- * // Simple text message
567
- * const msg = new UserMessage('Hello, world!');
568
- *
569
- * // Multimodal message
570
- * const msg = new UserMessage([
571
- * { type: 'text', text: 'What is in this image?' },
572
- * { type: 'image', source: { type: 'url', url: '...' }, mimeType: 'image/png' }
573
- * ]);
574
- * ```
575
- */
576
- declare class UserMessage extends Message {
577
- /** Message type discriminator */
578
- readonly type: "user";
579
- /** Content blocks in this message */
580
- readonly content: UserContent[];
581
- /**
582
- * Creates a new user message.
583
- *
584
- * @param content - String (converted to TextBlock) or array of content blocks
585
- * @param options - Optional message ID and metadata
586
- */
587
- constructor(content: string | UserContent[], options?: MessageOptions);
588
- protected getContent(): ContentBlock[];
589
- }
590
- /**
591
- * Assistant response message.
592
- *
593
- * Represents a response from the AI assistant, which may contain
594
- * text, media content, and/or tool call requests.
595
- *
596
- * @example
597
- * ```typescript
598
- * // Simple text response
599
- * const msg = new AssistantMessage('Hello! How can I help?');
600
- *
601
- * // Response with tool calls
602
- * const msg = new AssistantMessage(
603
- * 'Let me check the weather...',
604
- * [{ toolCallId: 'call_1', toolName: 'get_weather', arguments: { location: 'NYC' } }]
605
- * );
606
- * ```
607
- */
608
- declare class AssistantMessage extends Message {
609
- /** Message type discriminator */
610
- readonly type: "assistant";
611
- /** Content blocks in this message */
612
- readonly content: AssistantContent[];
613
- /** Tool calls requested by the model (if any) */
614
- readonly toolCalls?: ToolCall[];
615
- /**
616
- * Creates a new assistant message.
617
- *
618
- * @param content - String (converted to TextBlock) or array of content blocks
619
- * @param toolCalls - Tool calls requested by the model
620
- * @param options - Optional message ID and metadata
621
- */
622
- constructor(content: string | AssistantContent[], toolCalls?: ToolCall[], options?: MessageOptions);
623
- protected getContent(): ContentBlock[];
624
- /**
625
- * Whether this message contains tool call requests.
626
- */
627
- get hasToolCalls(): boolean;
628
- }
629
- /**
630
- * Tool execution result message.
631
- *
632
- * Contains the results of executing one or more tool calls,
633
- * sent back to the model for further processing.
634
- *
635
- * @example
636
- * ```typescript
637
- * const msg = new ToolResultMessage([
638
- * { toolCallId: 'call_1', result: { temperature: 72, conditions: 'sunny' } },
639
- * { toolCallId: 'call_2', result: 'File not found', isError: true }
640
- * ]);
641
- * ```
642
- */
643
- declare class ToolResultMessage extends Message {
644
- /** Message type discriminator */
645
- readonly type: "tool_result";
646
- /** Results from tool executions */
647
- readonly results: ToolResult[];
648
- /**
649
- * Creates a new tool result message.
650
- *
651
- * @param results - Array of tool execution results
652
- * @param options - Optional message ID and metadata
653
- */
654
- constructor(results: ToolResult[], options?: MessageOptions);
655
- protected getContent(): ContentBlock[];
656
- }
657
- /**
658
- * Type guard for UserMessage.
659
- *
660
- * @param msg - The message to check
661
- * @returns True if the message is a UserMessage
662
- *
663
- * @example
664
- * ```typescript
665
- * if (isUserMessage(msg)) {
666
- * console.log('User said:', msg.text);
667
- * }
668
- * ```
669
- */
670
- declare function isUserMessage(msg: Message): msg is UserMessage;
671
- /**
672
- * Type guard for AssistantMessage.
673
- *
674
- * @param msg - The message to check
675
- * @returns True if the message is an AssistantMessage
676
- *
677
- * @example
678
- * ```typescript
679
- * if (isAssistantMessage(msg)) {
680
- * console.log('Assistant said:', msg.text);
681
- * if (msg.hasToolCalls) {
682
- * console.log('Tool calls:', msg.toolCalls);
683
- * }
684
- * }
685
- * ```
686
- */
687
- declare function isAssistantMessage(msg: Message): msg is AssistantMessage;
688
- /**
689
- * Type guard for ToolResultMessage.
690
- *
691
- * @param msg - The message to check
692
- * @returns True if the message is a ToolResultMessage
693
- *
694
- * @example
695
- * ```typescript
696
- * if (isToolResultMessage(msg)) {
697
- * for (const result of msg.results) {
698
- * console.log(`Tool ${result.toolCallId}:`, result.result);
699
- * }
700
- * }
701
- * ```
702
- */
703
- declare function isToolResultMessage(msg: Message): msg is ToolResultMessage;
704
-
705
- /**
706
- * @fileoverview Turn types for inference results.
707
- *
708
- * A Turn represents the complete result of one inference call, including
709
- * all messages produced during tool execution loops, token usage, and
710
- * optional structured output data.
711
- *
712
- * @module types/turn
713
- */
714
-
715
- /**
716
- * Token usage information for an inference request.
717
- *
718
- * Tracks input and output tokens across all inference cycles,
719
- * with optional per-cycle breakdown and cache metrics.
720
- *
721
- * @example
722
- * ```typescript
723
- * const usage: TokenUsage = {
724
- * inputTokens: 150,
725
- * outputTokens: 50,
726
- * totalTokens: 200,
727
- * cacheReadTokens: 100,
728
- * cacheWriteTokens: 50,
729
- * cycles: [
730
- * { inputTokens: 100, outputTokens: 30, cacheReadTokens: 0, cacheWriteTokens: 50 },
731
- * { inputTokens: 50, outputTokens: 20, cacheReadTokens: 100, cacheWriteTokens: 0 }
732
- * ]
733
- * };
734
- * ```
735
- */
736
- interface TokenUsage {
737
- /** Total input tokens across all cycles */
738
- inputTokens: number;
739
- /** Total output tokens across all cycles */
740
- outputTokens: number;
741
- /** Sum of input and output tokens */
742
- totalTokens: number;
743
- /**
744
- * Tokens read from cache (cache hits).
745
- * Returns 0 for providers that don't support or report cache metrics.
746
- */
747
- cacheReadTokens: number;
748
- /**
749
- * Tokens written to cache (cache misses that were cached).
750
- * Only Anthropic reports this metric; returns 0 for other providers.
751
- */
752
- cacheWriteTokens: number;
753
- /** Per-cycle token breakdown (if multiple cycles occurred) */
754
- cycles?: Array<{
755
- inputTokens: number;
756
- outputTokens: number;
757
- cacheReadTokens: number;
758
- cacheWriteTokens: number;
759
- }>;
760
- }
761
- /**
762
- * A Turn represents the complete result of one inference call.
763
- *
764
- * Includes all messages produced during tool execution loops,
765
- * the final assistant response, token usage, and optional
766
- * structured output data.
767
- *
768
- * @typeParam TData - Type of the structured output data
769
- *
770
- * @example
771
- * ```typescript
772
- * const turn = await instance.generate('Hello');
773
- * console.log(turn.response.text);
774
- * console.log(`Used ${turn.usage.totalTokens} tokens in ${turn.cycles} cycles`);
775
- *
776
- * // With structured output
777
- * interface WeatherData { temperature: number; conditions: string; }
778
- * const turn = await instance.generate<WeatherData>('Get weather');
779
- * console.log(turn.data?.temperature);
780
- * ```
781
- */
782
- interface Turn<TData = unknown> {
783
- /**
784
- * All messages produced during this inference, in chronological order.
785
- * Includes UserMessage, AssistantMessage (may include toolCalls), and ToolResultMessage.
786
- */
787
- readonly messages: Message[];
788
- /** The final assistant response (last AssistantMessage in the turn) */
789
- readonly response: AssistantMessage;
790
- /**
791
- * Tool executions that occurred during this turn.
792
- *
793
- * Execution order reflects completion timing, not call order.
794
- * Correlate with tool calls using toolCallId.
795
- */
796
- readonly toolExecutions: ToolExecution[];
797
- /** Aggregate token usage for the entire turn */
798
- readonly usage: TokenUsage;
799
- /** Total number of inference cycles (1 + number of tool rounds) */
800
- readonly cycles: number;
801
- /**
802
- * Structured output data (if a structure schema was provided).
803
- * Type is inferred from the schema when using TypeScript.
804
- */
805
- readonly data?: TData;
806
- }
807
- /**
808
- * Turn serialized to JSON format.
809
- * Messages are converted to MessageJSON, response is omitted (computed from messages).
810
- *
811
- * @remarks
812
- * This type is derived from {@link Turn} and should stay in sync with it.
813
- */
814
- type TurnJSON = Omit<Turn, 'messages' | 'response'> & {
815
- messages: MessageJSON[];
816
- };
817
- /**
818
- * Creates a Turn from accumulated inference data.
819
- *
820
- * @typeParam TData - Type of the structured output data
821
- * @param messages - All messages produced during the inference
822
- * @param toolExecutions - Record of all tool executions
823
- * @param usage - Aggregate token usage
824
- * @param cycles - Number of inference cycles
825
- * @param data - Optional structured output data
826
- * @returns A complete Turn object
827
- * @throws Error if no assistant message is found in the messages
828
- *
829
- * @example
830
- * ```typescript
831
- * const turn = createTurn(
832
- * [userMsg, assistantMsg],
833
- * [],
834
- * { inputTokens: 100, outputTokens: 50, totalTokens: 150 },
835
- * 1
836
- * );
837
- * ```
838
- */
839
- declare function createTurn<TData = unknown>(messages: Message[], toolExecutions: ToolExecution[], usage: TokenUsage, cycles: number, data?: TData): Turn<TData>;
840
- /**
841
- * Creates an empty TokenUsage object.
842
- *
843
- * @returns A TokenUsage with all values set to zero
844
- *
845
- * @example
846
- * ```typescript
847
- * const usage = emptyUsage();
848
- * // { inputTokens: 0, outputTokens: 0, totalTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0, cycles: [] }
849
- * ```
850
- */
851
- declare function emptyUsage(): TokenUsage;
852
- /**
853
- * Aggregates token usage from multiple inference cycles.
854
- *
855
- * @param usages - Array of TokenUsage objects to aggregate
856
- * @returns Combined TokenUsage with per-cycle breakdown
857
- *
858
- * @example
859
- * ```typescript
860
- * const cycle1 = { inputTokens: 100, outputTokens: 30, totalTokens: 130, cacheReadTokens: 50, cacheWriteTokens: 0 };
861
- * const cycle2 = { inputTokens: 150, outputTokens: 40, totalTokens: 190, cacheReadTokens: 100, cacheWriteTokens: 0 };
862
- * const total = aggregateUsage([cycle1, cycle2]);
863
- * // { inputTokens: 250, outputTokens: 70, totalTokens: 320, cacheReadTokens: 150, cacheWriteTokens: 0, cycles: [...] }
864
- * ```
865
- */
866
- declare function aggregateUsage(usages: TokenUsage[]): TokenUsage;
867
-
868
- /**
869
- * @fileoverview Streaming types for real-time LLM responses.
870
- *
871
- * Defines the event types and interfaces for streaming LLM inference,
872
- * including text deltas, tool call deltas, and control events.
873
- *
874
- * @module types/stream
875
- */
876
-
877
- /**
878
- * Stream event type constants.
879
- *
880
- * Use these constants instead of raw strings for type-safe event handling:
881
- *
882
- * @example
883
- * ```typescript
884
- * import { StreamEventType } from 'upp';
885
- *
886
- * for await (const event of stream) {
887
- * if (event.type === StreamEventType.TextDelta) {
888
- * process.stdout.write(event.delta.text ?? '');
889
- * }
890
- * }
891
- * ```
892
- */
893
- declare const StreamEventType: {
894
- /** Incremental text output */
895
- readonly TextDelta: "text_delta";
896
- /** Incremental reasoning/thinking output */
897
- readonly ReasoningDelta: "reasoning_delta";
898
- /** Incremental image data */
899
- readonly ImageDelta: "image_delta";
900
- /** Incremental audio data */
901
- readonly AudioDelta: "audio_delta";
902
- /** Incremental video data */
903
- readonly VideoDelta: "video_delta";
904
- /** Incremental tool call data (arguments being streamed) */
905
- readonly ToolCallDelta: "tool_call_delta";
906
- /** Tool execution has started (may be emitted after completion in some implementations) */
907
- readonly ToolExecutionStart: "tool_execution_start";
908
- /** Tool execution has completed */
909
- readonly ToolExecutionEnd: "tool_execution_end";
910
- /** Beginning of a message */
911
- readonly MessageStart: "message_start";
912
- /** End of a message */
913
- readonly MessageStop: "message_stop";
914
- /** Beginning of a content block */
915
- readonly ContentBlockStart: "content_block_start";
916
- /** End of a content block */
917
- readonly ContentBlockStop: "content_block_stop";
918
- };
919
- /**
920
- * Stream event type discriminator union.
921
- *
922
- * This type is derived from {@link StreamEventType} constants. Use `StreamEventType.TextDelta`
923
- * for constants or `type MyType = StreamEventType` for type annotations.
924
- */
925
- type StreamEventType = (typeof StreamEventType)[keyof typeof StreamEventType];
926
- /**
927
- * Event delta data payload.
928
- *
929
- * Contains the type-specific data for a streaming event.
930
- * Different fields are populated depending on the event type.
931
- */
932
- interface EventDelta {
933
- /** Incremental text content (for text_delta, reasoning_delta) */
934
- text?: string;
935
- /** Incremental binary data (for image_delta, audio_delta, video_delta) */
936
- data?: Uint8Array;
937
- /** Tool call identifier (for tool_call_delta, tool_execution_start/end) */
938
- toolCallId?: string;
939
- /** Tool name (for tool_call_delta, tool_execution_start/end) */
940
- toolName?: string;
941
- /** Incremental JSON arguments string (for tool_call_delta) */
942
- argumentsJson?: string;
943
- /** Tool execution result (for tool_execution_end) */
944
- result?: unknown;
945
- /** Whether tool execution resulted in an error (for tool_execution_end) */
946
- isError?: boolean;
947
- /** Timestamp in milliseconds (for tool_execution_start/end) */
948
- timestamp?: number;
949
- }
950
- /**
951
- * A single streaming event from the LLM.
952
- *
953
- * Events are emitted in order as the model generates output,
954
- * allowing for real-time display of responses.
955
- *
956
- * @example
957
- * ```typescript
958
- * import { StreamEventType } from 'upp';
959
- *
960
- * for await (const event of stream) {
961
- * if (event.type === StreamEventType.TextDelta) {
962
- * process.stdout.write(event.delta.text ?? '');
963
- * } else if (event.type === StreamEventType.ToolCallDelta) {
964
- * console.log('Tool:', event.delta.toolName);
965
- * }
966
- * }
967
- * ```
968
- */
969
- interface StreamEvent {
970
- /** Event type discriminator */
971
- type: StreamEventType;
972
- /** Index of the content block this event belongs to */
973
- index: number;
974
- /** Event-specific data payload */
975
- delta: EventDelta;
976
- }
977
- /**
978
- * Stream result - an async iterable that also provides the final turn.
979
- *
980
- * Allows consuming streaming events while also awaiting the complete
981
- * Turn result after streaming finishes.
982
- *
983
- * @typeParam TData - Type of the structured output data
984
- *
985
- * @example
986
- * ```typescript
987
- * import { StreamEventType } from 'upp';
988
- *
989
- * const stream = instance.stream('Tell me a story');
990
- *
991
- * // Consume streaming events
992
- * for await (const event of stream) {
993
- * if (event.type === StreamEventType.TextDelta) {
994
- * process.stdout.write(event.delta.text ?? '');
995
- * }
996
- * }
997
- *
998
- * // Get the complete turn after streaming
999
- * const turn = await stream.turn;
1000
- * console.log('\n\nTokens used:', turn.usage.totalTokens);
1001
- * ```
1002
- */
1003
- interface StreamResult<TData = unknown> extends AsyncIterable<StreamEvent> {
1004
- /**
1005
- * Promise that resolves to the complete Turn after streaming finishes.
1006
- * Rejects if the stream is aborted or terminated early.
1007
- */
1008
- readonly turn: Promise<Turn<TData>>;
1009
- /**
1010
- * Aborts the stream, stopping further events and cancelling the request.
1011
- * This will cause {@link StreamResult.turn} to reject.
1012
- */
1013
- abort(): void;
1014
- }
1015
- /**
1016
- * Creates a StreamResult from an async generator and completion promise.
1017
- *
1018
- * @typeParam TData - Type of the structured output data
1019
- * @param generator - Async generator that yields stream events
1020
- * @param turnPromiseOrFactory - Promise or factory that resolves to the complete Turn
1021
- * @param abortController - Controller for aborting the stream
1022
- * @returns A StreamResult that can be iterated and awaited
1023
- *
1024
- * @example
1025
- * ```typescript
1026
- * const abortController = new AbortController();
1027
- * const stream = createStreamResult(
1028
- * eventGenerator(),
1029
- * turnPromise,
1030
- * abortController
1031
- * );
1032
- * ```
1033
- */
1034
- declare function createStreamResult<TData = unknown>(generator: AsyncGenerator<StreamEvent, void, unknown>, turnPromiseOrFactory: Promise<Turn<TData>> | (() => Promise<Turn<TData>>), abortController: AbortController): StreamResult<TData>;
1035
- /**
1036
- * Creates a text delta stream event.
1037
- *
1038
- * @param text - The incremental text content
1039
- * @param index - Content block index (default: 0)
1040
- * @returns A text_delta StreamEvent
1041
- */
1042
- declare function textDelta(text: string, index?: number): StreamEvent;
1043
- /**
1044
- * Creates a tool call delta stream event.
1045
- *
1046
- * @param toolCallId - Unique identifier for the tool call
1047
- * @param toolName - Name of the tool being called
1048
- * @param argumentsJson - Incremental JSON arguments string
1049
- * @param index - Content block index (default: 0)
1050
- * @returns A tool_call_delta StreamEvent
1051
- */
1052
- declare function toolCallDelta(toolCallId: string, toolName: string, argumentsJson: string, index?: number): StreamEvent;
1053
- /**
1054
- * Creates a message start stream event.
1055
- *
1056
- * @returns A message_start StreamEvent
1057
- */
1058
- declare function messageStart(): StreamEvent;
1059
- /**
1060
- * Creates a message stop stream event.
1061
- *
1062
- * @returns A message_stop StreamEvent
1063
- */
1064
- declare function messageStop(): StreamEvent;
1065
- /**
1066
- * Creates a content block start stream event.
1067
- *
1068
- * @param index - The content block index starting
1069
- * @returns A content_block_start StreamEvent
1070
- */
1071
- declare function contentBlockStart(index: number): StreamEvent;
1072
- /**
1073
- * Creates a content block stop stream event.
1074
- *
1075
- * @param index - The content block index stopping
1076
- * @returns A content_block_stop StreamEvent
1077
- */
1078
- declare function contentBlockStop(index: number): StreamEvent;
1079
-
1080
- /**
1081
- * @fileoverview Embedding types for vector embedding generation.
1082
- *
1083
- * Defines the interfaces for configuring and executing embedding operations,
1084
- * including options, instances, requests, responses, and streaming progress.
1085
- *
1086
- * @module types/embedding
1087
- */
1088
-
1089
- /**
1090
- * Structural type for embedding model input.
1091
- * Uses structural typing to avoid generic variance issues with Provider generics.
1092
- *
1093
- * @remarks
1094
- * This type mirrors {@link ModelReference} while keeping provider options
1095
- * structurally compatible across providers.
1096
- *
1097
- * @see ModelReference
1098
- */
1099
- interface EmbeddingModelInput {
1100
- readonly modelId: string;
1101
- readonly provider: ProviderIdentity;
1102
- /** Optional provider configuration merged into requests */
1103
- readonly providerConfig?: Partial<ProviderConfig>;
1104
- }
1105
- /**
1106
- * Options for creating an embedding instance with the embedding() function.
1107
- *
1108
- * @typeParam TParams - Provider-specific parameter type
1109
- *
1110
- * @example
1111
- * ```typescript
1112
- * const options: EmbeddingOptions<OpenAIEmbedParams> = {
1113
- * model: openai('text-embedding-3-large'),
1114
- * config: { apiKey: process.env.OPENAI_API_KEY },
1115
- * params: { dimensions: 1536 }
1116
- * };
1117
- * ```
1118
- */
1119
- interface EmbeddingOptions<TParams = unknown> {
1120
- /** A model reference from a provider factory */
1121
- model: EmbeddingModelInput;
1122
- /** Provider infrastructure configuration */
1123
- config?: ProviderConfig;
1124
- /** Provider-specific parameters (passed through unchanged) */
1125
- params?: TParams;
1126
- }
1127
- /**
1128
- * Options for embed() calls.
1129
- */
1130
- interface EmbedOptions {
1131
- /**
1132
- * Enable chunked processing with progress for large input sets.
1133
- * When true, returns EmbeddingStream instead of Promise.
1134
- */
1135
- chunked?: boolean;
1136
- /** Inputs per batch when chunked (default: provider max) */
1137
- batchSize?: number;
1138
- /** Concurrent batch limit when chunked (default: 1) */
1139
- concurrency?: number;
1140
- /** Abort signal for cancellation */
1141
- signal?: AbortSignal;
1142
- }
1143
- /**
1144
- * Single embedding vector result.
1145
- */
1146
- interface Embedding {
1147
- /** The embedding vector */
1148
- vector: number[];
1149
- /** Vector dimensionality */
1150
- dimensions: number;
1151
- /** Index corresponding to input array position */
1152
- index: number;
1153
- /** Token count for this input (if provider reports) */
1154
- tokens?: number;
1155
- /** Provider-specific per-embedding metadata */
1156
- metadata?: Record<string, unknown>;
1157
- }
1158
- /**
1159
- * Result from embed() call.
1160
- */
1161
- interface EmbeddingResult {
1162
- /** Embeddings in same order as inputs */
1163
- embeddings: Embedding[];
1164
- /** Usage statistics */
1165
- usage: EmbeddingUsage;
1166
- /** Provider-specific response metadata */
1167
- metadata?: Record<string, unknown>;
1168
- }
1169
- /**
1170
- * Progress update when using chunked mode.
1171
- */
1172
- interface EmbeddingProgress {
1173
- /** Embeddings from the latest batch */
1174
- embeddings: Embedding[];
1175
- /** Total embeddings completed so far */
1176
- completed: number;
1177
- /** Total number of inputs */
1178
- total: number;
1179
- /** Percentage complete (0-100) */
1180
- percent: number;
1181
- }
1182
- /**
1183
- * Async iterable stream with final result accessor.
1184
- * Returned when embed() is called with { chunked: true }.
1185
- */
1186
- interface EmbeddingStream extends AsyncIterable<EmbeddingProgress> {
1187
- /** Promise resolving to complete result after iteration */
1188
- readonly result: Promise<EmbeddingResult>;
1189
- /** Abort the operation */
1190
- abort(): void;
1191
- }
1192
- /**
1193
- * Embedding instance returned by the embedding() function.
1194
- *
1195
- * @typeParam TParams - Provider-specific parameter type
1196
- *
1197
- * @example
1198
- * ```typescript
1199
- * const embedder = embedding({ model: openai('text-embedding-3-large') });
1200
- *
1201
- * // Single input
1202
- * const result = await embedder.embed('Hello world');
1203
- *
1204
- * // Batch input
1205
- * const batch = await embedder.embed(['doc1', 'doc2', 'doc3']);
1206
- *
1207
- * // Large-scale with progress
1208
- * const stream = embedder.embed(documents, { chunked: true });
1209
- * for await (const progress of stream) {
1210
- * console.log(`${progress.percent}% complete`);
1211
- * }
1212
- * ```
1213
- */
1214
- interface EmbeddingInstance<TParams = unknown> {
1215
- /**
1216
- * Generate embeddings for one or more inputs.
1217
- *
1218
- * @param input - Single input or array of inputs
1219
- * @param options - Optional embed options
1220
- * @returns Promise<EmbeddingResult> or EmbeddingStream if chunked
1221
- */
1222
- embed(input: EmbeddingInput | EmbeddingInput[], options?: EmbedOptions & {
1223
- chunked?: false;
1224
- }): Promise<EmbeddingResult>;
1225
- embed(input: EmbeddingInput[], options: EmbedOptions & {
1226
- chunked: true;
1227
- }): EmbeddingStream;
1228
- embed(input: EmbeddingInput | EmbeddingInput[], options?: EmbedOptions): Promise<EmbeddingResult> | EmbeddingStream;
1229
- /** The bound embedding model */
1230
- readonly model: BoundEmbeddingModel<TParams>;
1231
- /** Current parameters */
1232
- readonly params: TParams | undefined;
1233
- }
1234
-
1235
- export { AssistantMessage as A, type BeforeCallResult as B, createStreamResult as C, textDelta as D, type EmbeddingOptions as E, toolCallDelta as F, messageStart as G, messageStop as H, contentBlockStart as I, type JSONSchema as J, contentBlockStop as K, type EmbedOptions as L, Message as M, type Embedding as N, type EmbeddingResult as O, type EmbeddingProgress as P, type EmbeddingStream as Q, type EmbeddingModelInput as R, type StreamResult as S, type Turn as T, UserMessage as U, type TurnJSON as V, 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 EmbeddingInstance as g, type JSONSchemaProperty as h, type JSONSchemaPropertyType as i, type ToolCall as j, type ToolResult as k, type ToolMetadata as l, type AfterCallResult as m, type ToolExecution as n, ToolResultMessage as o, MessageRole as p, isUserMessage as q, isAssistantMessage as r, isToolResultMessage as s, type MessageMetadata as t, type MessageOptions as u, createTurn as v, emptyUsage as w, aggregateUsage as x, type EventDelta as y, StreamEventType as z };