@providerprotocol/ai 0.0.38 → 0.0.39

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 (83) hide show
  1. package/README.md +94 -3
  2. package/dist/anthropic/index.d.ts +3 -2
  3. package/dist/cerebras/index.d.ts +3 -2
  4. package/dist/chunk-3Q5VELKG.js +124 -0
  5. package/dist/chunk-3Q5VELKG.js.map +1 -0
  6. package/dist/{chunk-LTEMH3CI.js → chunk-5XPRVUOK.js} +6 -4
  7. package/dist/{chunk-LTEMH3CI.js.map → chunk-5XPRVUOK.js.map} +1 -1
  8. package/dist/chunk-7ULSRWDH.js +83 -0
  9. package/dist/chunk-7ULSRWDH.js.map +1 -0
  10. package/dist/{chunk-YQLR3XOA.js → chunk-BIBMNP7Y.js} +1 -75
  11. package/dist/chunk-BIBMNP7Y.js.map +1 -0
  12. package/dist/{chunk-7GTWHZY2.js → chunk-IDZR4ROP.js} +5 -3
  13. package/dist/{chunk-7GTWHZY2.js.map → chunk-IDZR4ROP.js.map} +1 -1
  14. package/dist/{chunk-4RX4VQCB.js → chunk-IIMTP3XC.js} +2 -2
  15. package/dist/{chunk-ZRVNAET3.js → chunk-KNBODIQU.js} +6 -3
  16. package/dist/chunk-KNBODIQU.js.map +1 -0
  17. package/dist/{chunk-FYSZFIZS.js → chunk-O32SBS6S.js} +5 -3
  18. package/dist/{chunk-FYSZFIZS.js.map → chunk-O32SBS6S.js.map} +1 -1
  19. package/dist/{chunk-BDXH6NQS.js → chunk-RDC5GYST.js} +5 -5
  20. package/dist/{chunk-5IWHCXKN.js → chunk-SAMIK4WZ.js} +2 -2
  21. package/dist/{chunk-EPB3GQNL.js → chunk-U6M3MXNI.js} +11 -2
  22. package/dist/chunk-U6M3MXNI.js.map +1 -0
  23. package/dist/{chunk-2YXFLRQ6.js → chunk-WNB5PSY6.js} +2 -2
  24. package/dist/{chunk-CRP6Y7NF.js → chunk-ZDYEDI2A.js} +2 -2
  25. package/dist/{embedding-CwZ1ZNWv.d.ts → embedding-iNQCeXfk.d.ts} +1 -1
  26. package/dist/google/index.d.ts +3 -2
  27. package/dist/groq/index.d.ts +3 -2
  28. package/dist/http/index.d.ts +4 -3
  29. package/dist/{image-stream-CeQHtjxS.d.ts → image-stream-ARno6XlS.d.ts} +1 -1
  30. package/dist/index.d.ts +8 -7
  31. package/dist/index.js +11 -2
  32. package/dist/index.js.map +1 -1
  33. package/dist/{llm-DS_-l71X.d.ts → llm-CZqlijjK.d.ts} +16 -9
  34. package/dist/middleware/logging/index.d.ts +3 -2
  35. package/dist/middleware/parsed-object/index.d.ts +3 -2
  36. package/dist/middleware/persistence/index.d.ts +3 -2
  37. package/dist/middleware/pubsub/index.d.ts +5 -4
  38. package/dist/middleware/pubsub/index.js +49 -3
  39. package/dist/middleware/pubsub/index.js.map +1 -1
  40. package/dist/middleware/pubsub/server/express/index.d.ts +3 -2
  41. package/dist/middleware/pubsub/server/express/index.js +2 -2
  42. package/dist/middleware/pubsub/server/fastify/index.d.ts +3 -2
  43. package/dist/middleware/pubsub/server/fastify/index.js +2 -2
  44. package/dist/middleware/pubsub/server/h3/index.d.ts +3 -2
  45. package/dist/middleware/pubsub/server/h3/index.js +2 -2
  46. package/dist/middleware/pubsub/server/index.d.ts +3 -2
  47. package/dist/middleware/pubsub/server/index.js +5 -5
  48. package/dist/middleware/pubsub/server/webapi/index.d.ts +3 -2
  49. package/dist/middleware/pubsub/server/webapi/index.js +2 -2
  50. package/dist/moonshot/index.d.ts +3 -2
  51. package/dist/ollama/index.d.ts +3 -2
  52. package/dist/openai/index.d.ts +3 -2
  53. package/dist/openrouter/index.d.ts +3 -2
  54. package/dist/proxy/index.d.ts +5 -4
  55. package/dist/proxy/index.js +10 -8
  56. package/dist/proxy/index.js.map +1 -1
  57. package/dist/proxy/server/express/index.d.ts +5 -4
  58. package/dist/proxy/server/express/index.js +3 -2
  59. package/dist/proxy/server/fastify/index.d.ts +5 -4
  60. package/dist/proxy/server/fastify/index.js +3 -2
  61. package/dist/proxy/server/h3/index.d.ts +19 -17
  62. package/dist/proxy/server/h3/index.js +3 -2
  63. package/dist/proxy/server/index.d.ts +5 -4
  64. package/dist/proxy/server/index.js +7 -6
  65. package/dist/proxy/server/webapi/index.d.ts +5 -4
  66. package/dist/proxy/server/webapi/index.js +3 -2
  67. package/dist/responses/index.d.ts +3 -2
  68. package/dist/{retry-CgoBNa51.d.ts → retry-C1eJbEMV.d.ts} +1 -1
  69. package/dist/{stream-sXhBtWjl.d.ts → stream-DVVUIKpz.d.ts} +3 -416
  70. package/dist/tool-D22EhP5F.d.ts +507 -0
  71. package/dist/{types-Cr4F0tVy.d.ts → types-CyXF0J7C.d.ts} +16 -3
  72. package/dist/utils/index.d.ts +65 -1
  73. package/dist/utils/index.js +15 -1
  74. package/dist/xai/index.d.ts +3 -2
  75. package/package.json +15 -3
  76. package/dist/chunk-EPB3GQNL.js.map +0 -1
  77. package/dist/chunk-YQLR3XOA.js.map +0 -1
  78. package/dist/chunk-ZRVNAET3.js.map +0 -1
  79. /package/dist/{chunk-4RX4VQCB.js.map → chunk-IIMTP3XC.js.map} +0 -0
  80. /package/dist/{chunk-BDXH6NQS.js.map → chunk-RDC5GYST.js.map} +0 -0
  81. /package/dist/{chunk-5IWHCXKN.js.map → chunk-SAMIK4WZ.js.map} +0 -0
  82. /package/dist/{chunk-2YXFLRQ6.js.map → chunk-WNB5PSY6.js.map} +0 -0
  83. /package/dist/{chunk-CRP6Y7NF.js.map → chunk-ZDYEDI2A.js.map} +0 -0
@@ -0,0 +1,507 @@
1
+ /**
2
+ * @fileoverview JSON Schema types for tool parameters and structured outputs.
3
+ *
4
+ * Provides TypeScript interfaces for defining JSON Schema objects used in
5
+ * LLM tool definitions and structured output specifications.
6
+ *
7
+ * @module types/schema
8
+ */
9
+ /**
10
+ * Primitive and composite JSON Schema property types.
11
+ *
12
+ * These types correspond to the JSON Schema specification's allowed type values.
13
+ */
14
+ type JSONSchemaPropertyType =
15
+ /** String values */
16
+ 'string'
17
+ /** Floating point numbers */
18
+ | 'number'
19
+ /** Whole numbers */
20
+ | 'integer'
21
+ /** Boolean true/false values */
22
+ | 'boolean'
23
+ /** Ordered lists of values */
24
+ | 'array'
25
+ /** Key-value mappings */
26
+ | 'object'
27
+ /** Explicit null value */
28
+ | 'null';
29
+ /**
30
+ * JSON Schema property definition.
31
+ *
32
+ * Describes a single property within a JSON Schema object, including
33
+ * type constraints, validation rules, and nested structure definitions.
34
+ *
35
+ * @example
36
+ * ```typescript
37
+ * const nameProperty: JSONSchemaProperty = {
38
+ * type: 'string',
39
+ * description: 'User name',
40
+ * minLength: 1,
41
+ * maxLength: 100
42
+ * };
43
+ * ```
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * const tagsProperty: JSONSchemaProperty = {
48
+ * type: 'array',
49
+ * description: 'List of tags',
50
+ * items: { type: 'string' },
51
+ * minItems: 1,
52
+ * uniqueItems: true
53
+ * };
54
+ * ```
55
+ */
56
+ interface JSONSchemaProperty {
57
+ /** The JSON type of this property */
58
+ type: JSONSchemaPropertyType;
59
+ /** Human-readable description for the LLM */
60
+ description?: string;
61
+ /** Allowed values (enumeration) */
62
+ enum?: unknown[];
63
+ /** Constant value this property must equal */
64
+ const?: unknown;
65
+ /** Default value if not provided */
66
+ default?: unknown;
67
+ /** Minimum string length (string type only) */
68
+ minLength?: number;
69
+ /** Maximum string length (string type only) */
70
+ maxLength?: number;
71
+ /** Regular expression pattern for validation (string type only) */
72
+ pattern?: string;
73
+ /** Semantic format hint (string type only) */
74
+ format?: 'email' | 'uri' | 'date' | 'date-time' | 'uuid';
75
+ /** Minimum value inclusive (number/integer types only) */
76
+ minimum?: number;
77
+ /** Maximum value inclusive (number/integer types only) */
78
+ maximum?: number;
79
+ /** Minimum value exclusive (number/integer types only) */
80
+ exclusiveMinimum?: number;
81
+ /** Maximum value exclusive (number/integer types only) */
82
+ exclusiveMaximum?: number;
83
+ /** Value must be divisible by this (number/integer types only) */
84
+ multipleOf?: number;
85
+ /** Schema for array elements (array type only) */
86
+ items?: JSONSchemaProperty;
87
+ /** Minimum array length (array type only) */
88
+ minItems?: number;
89
+ /** Maximum array length (array type only) */
90
+ maxItems?: number;
91
+ /** Whether array elements must be unique (array type only) */
92
+ uniqueItems?: boolean;
93
+ /** Nested property definitions (object type only) */
94
+ properties?: Record<string, JSONSchemaProperty>;
95
+ /** List of required property names (object type only) */
96
+ required?: string[];
97
+ /** Whether additional properties are allowed (object type only) */
98
+ additionalProperties?: boolean;
99
+ }
100
+ /**
101
+ * Root JSON Schema for tool parameters or structured outputs.
102
+ *
103
+ * This is the top-level schema definition used when defining tool
104
+ * parameters or requesting structured output from an LLM.
105
+ *
106
+ * @example
107
+ * ```typescript
108
+ * const weatherToolSchema: JSONSchema = {
109
+ * type: 'object',
110
+ * description: 'Parameters for getting weather information',
111
+ * properties: {
112
+ * location: {
113
+ * type: 'string',
114
+ * description: 'City name or coordinates'
115
+ * },
116
+ * units: {
117
+ * type: 'string',
118
+ * enum: ['celsius', 'fahrenheit'],
119
+ * description: 'Temperature units'
120
+ * }
121
+ * },
122
+ * required: ['location']
123
+ * };
124
+ * ```
125
+ */
126
+ interface JSONSchema {
127
+ /** Root schemas are always objects */
128
+ type: 'object';
129
+ /** Property definitions for the object */
130
+ properties: Record<string, JSONSchemaProperty>;
131
+ /** List of required property names */
132
+ required?: string[];
133
+ /** Whether additional properties are allowed beyond those defined */
134
+ additionalProperties?: boolean;
135
+ /** Human-readable description of the schema's purpose */
136
+ description?: string;
137
+ }
138
+ /**
139
+ * Minimal interface for Zod v3 schema detection.
140
+ * Zod v3 schemas have a `_def` property containing schema definition.
141
+ */
142
+ interface ZodV3Like {
143
+ _def: unknown;
144
+ parse: (data: unknown) => unknown;
145
+ }
146
+ /**
147
+ * Minimal interface for Zod v4 schema detection.
148
+ * Zod v4 schemas have a `_zod` property in addition to `_def`.
149
+ */
150
+ interface ZodV4Like extends ZodV3Like {
151
+ _zod: unknown;
152
+ }
153
+ /**
154
+ * Union type representing any Zod-like schema.
155
+ * Allows accepting Zod schemas without requiring Zod as a direct dependency.
156
+ */
157
+ type ZodLike = ZodV3Like | ZodV4Like;
158
+ /**
159
+ * Structure input type that accepts either JSON Schema or Zod schemas.
160
+ *
161
+ * When a Zod schema is provided, it will be automatically converted
162
+ * to JSON Schema before being sent to the provider.
163
+ *
164
+ * @example
165
+ * ```typescript
166
+ * import { z } from 'zod';
167
+ *
168
+ * // Using Zod schema directly
169
+ * const model = llm({
170
+ * model: anthropic('claude-sonnet-4-20250514'),
171
+ * structure: z.object({
172
+ * name: z.string(),
173
+ * age: z.number(),
174
+ * }),
175
+ * });
176
+ *
177
+ * // Using JSON Schema
178
+ * const model = llm({
179
+ * model: anthropic('claude-sonnet-4-20250514'),
180
+ * structure: {
181
+ * type: 'object',
182
+ * properties: {
183
+ * name: { type: 'string' },
184
+ * age: { type: 'number' },
185
+ * },
186
+ * required: ['name', 'age'],
187
+ * },
188
+ * });
189
+ * ```
190
+ */
191
+ type Structure = JSONSchema | ZodLike;
192
+
193
+ /**
194
+ * @fileoverview Tool types for LLM function calling.
195
+ *
196
+ * Defines the interfaces for registering tools with LLMs, handling
197
+ * tool calls from the model, and managing tool execution strategies.
198
+ *
199
+ * @module types/tool
200
+ */
201
+
202
+ /**
203
+ * Provider-namespaced metadata for tools.
204
+ *
205
+ * Each provider can attach its own metadata under its namespace,
206
+ * enabling provider-specific features like caching, strict mode, etc.
207
+ *
208
+ * @example
209
+ * ```typescript
210
+ * const metadata: ToolMetadata = {
211
+ * anthropic: { cache_control: { type: 'ephemeral' } },
212
+ * openrouter: { cache_control: { type: 'ephemeral', ttl: '1h' } }
213
+ * };
214
+ * ```
215
+ */
216
+ interface ToolMetadata {
217
+ [provider: string]: Record<string, unknown> | undefined;
218
+ }
219
+ /**
220
+ * Tool call requested by the model.
221
+ *
222
+ * Represents a single function call request from the LLM, including
223
+ * the tool name and parsed arguments.
224
+ *
225
+ * @example
226
+ * ```typescript
227
+ * const toolCall: ToolCall = {
228
+ * toolCallId: 'call_abc123',
229
+ * toolName: 'get_weather',
230
+ * arguments: { location: 'San Francisco', units: 'celsius' }
231
+ * };
232
+ * ```
233
+ */
234
+ interface ToolCall {
235
+ /** Unique identifier for this tool call, used to match results */
236
+ toolCallId: string;
237
+ /** Name of the tool being called */
238
+ toolName: string;
239
+ /** Parsed arguments for the tool call */
240
+ arguments: Record<string, unknown>;
241
+ }
242
+ /**
243
+ * Result of tool execution.
244
+ *
245
+ * Returned after executing a tool, containing the result data
246
+ * and whether an error occurred.
247
+ *
248
+ * @example
249
+ * ```typescript
250
+ * const result: ToolResult = {
251
+ * toolCallId: 'call_abc123',
252
+ * result: { temperature: 72, conditions: 'sunny' }
253
+ * };
254
+ *
255
+ * // Error result
256
+ * const errorResult: ToolResult = {
257
+ * toolCallId: 'call_abc123',
258
+ * result: 'Location not found',
259
+ * isError: true
260
+ * };
261
+ * ```
262
+ */
263
+ interface ToolResult {
264
+ /** The tool call ID this result corresponds to */
265
+ toolCallId: string;
266
+ /** The result data (can be any serializable value) */
267
+ result: unknown;
268
+ /** Whether the tool execution resulted in an error */
269
+ isError?: boolean;
270
+ }
271
+ /**
272
+ * Tool definition for LLM function calling.
273
+ *
274
+ * Defines a tool that can be called by the LLM, including its
275
+ * name, description, parameter schema, and execution function.
276
+ *
277
+ * @typeParam TParams - The type of parameters the tool accepts
278
+ * @typeParam TResult - The type of result the tool returns
279
+ *
280
+ * @example
281
+ * ```typescript
282
+ * const weatherTool: Tool<{ location: string }, WeatherData> = {
283
+ * name: 'get_weather',
284
+ * description: 'Get current weather for a location',
285
+ * parameters: {
286
+ * type: 'object',
287
+ * properties: {
288
+ * location: { type: 'string', description: 'City name' }
289
+ * },
290
+ * required: ['location']
291
+ * },
292
+ * run: async (params) => {
293
+ * return fetchWeather(params.location);
294
+ * }
295
+ * };
296
+ * ```
297
+ */
298
+ interface Tool<TParams = unknown, TResult = unknown> {
299
+ /** Tool name (must be unique within an llm() instance) */
300
+ name: string;
301
+ /** Human-readable description for the model to understand when to use this tool */
302
+ description: string;
303
+ /** JSON Schema defining the tool's parameters */
304
+ parameters: JSONSchema;
305
+ /**
306
+ * Provider-specific metadata, namespaced by provider name.
307
+ *
308
+ * Used for provider-specific features like prompt caching:
309
+ * @example
310
+ * ```typescript
311
+ * const tool: Tool = {
312
+ * name: 'search_docs',
313
+ * description: 'Search documentation',
314
+ * parameters: {...},
315
+ * run: async (params) => {...},
316
+ * metadata: {
317
+ * anthropic: { cache_control: { type: 'ephemeral' } }
318
+ * }
319
+ * };
320
+ * ```
321
+ */
322
+ metadata?: ToolMetadata;
323
+ /**
324
+ * Executes the tool with the provided parameters.
325
+ *
326
+ * @param params - The parameters passed by the model
327
+ * @returns The tool result, synchronously or as a Promise
328
+ */
329
+ run(params: TParams): TResult | Promise<TResult>;
330
+ /**
331
+ * Optional approval handler for sensitive operations.
332
+ *
333
+ * If provided, this function is called before the tool executes.
334
+ * Return false to prevent execution.
335
+ *
336
+ * @param params - The parameters the tool would be called with
337
+ * @returns Whether to approve the execution
338
+ */
339
+ approval?(params: TParams): boolean | Promise<boolean>;
340
+ }
341
+ /**
342
+ * Tool input type that accepts Zod schemas for parameters.
343
+ *
344
+ * This is the user-facing type for defining tools. Zod schemas are
345
+ * automatically converted to JSON Schema before being sent to providers.
346
+ *
347
+ * @typeParam TParams - The type of parameters the tool accepts
348
+ * @typeParam TResult - The type of result the tool returns
349
+ *
350
+ * @example
351
+ * ```typescript
352
+ * import { z } from 'zod';
353
+ *
354
+ * const tool: ToolInput<{ location: string }> = {
355
+ * name: 'get_weather',
356
+ * description: 'Get weather for a location',
357
+ * parameters: z.object({
358
+ * location: z.string().describe('City name'),
359
+ * }),
360
+ * run: async (params) => fetchWeather(params.location),
361
+ * };
362
+ * ```
363
+ */
364
+ interface ToolInput<TParams = unknown, TResult = unknown> {
365
+ /** Tool name (must be unique within an llm() instance) */
366
+ name: string;
367
+ /** Human-readable description for the model to understand when to use this tool */
368
+ description: string;
369
+ /** JSON Schema or Zod schema defining the tool's parameters */
370
+ parameters: Structure;
371
+ /** Provider-specific metadata, namespaced by provider name */
372
+ metadata?: ToolMetadata;
373
+ /** Executes the tool with the provided parameters */
374
+ run(params: TParams): TResult | Promise<TResult>;
375
+ /** Optional approval handler for sensitive operations */
376
+ approval?(params: TParams): boolean | Promise<boolean>;
377
+ }
378
+ /**
379
+ * Result from onBeforeCall hook indicating whether to proceed and optionally transformed params.
380
+ */
381
+ interface BeforeCallResult {
382
+ /** Whether to proceed with tool execution */
383
+ proceed: boolean;
384
+ /** Transformed parameters to use instead of the original (optional) */
385
+ params?: unknown;
386
+ }
387
+ /**
388
+ * Result from onAfterCall hook optionally containing a transformed result.
389
+ */
390
+ interface AfterCallResult {
391
+ /** Transformed result to use instead of the original */
392
+ result: unknown;
393
+ }
394
+ /**
395
+ * Strategy for controlling tool execution behavior.
396
+ *
397
+ * Provides hooks for monitoring, controlling, and transforming the tool execution
398
+ * loop during LLM inference.
399
+ *
400
+ * @example
401
+ * ```typescript
402
+ * const strategy: ToolUseStrategy = {
403
+ * maxIterations: 5,
404
+ * onToolCall: (tool, params) => {
405
+ * console.log(`Calling ${tool.name} with`, params);
406
+ * },
407
+ * // Transform input parameters
408
+ * onBeforeCall: (tool, params) => {
409
+ * if (tool.name === 'search') {
410
+ * return { proceed: true, params: { ...params, limit: 10 } };
411
+ * }
412
+ * return true;
413
+ * },
414
+ * // Transform output results
415
+ * onAfterCall: (tool, params, result) => {
416
+ * if (tool.name === 'fetch_data') {
417
+ * return { result: sanitize(result) };
418
+ * }
419
+ * },
420
+ * onMaxIterations: (iterations) => {
421
+ * console.warn(`Reached max iterations: ${iterations}`);
422
+ * }
423
+ * };
424
+ * ```
425
+ */
426
+ interface ToolUseStrategy {
427
+ /** Maximum number of tool execution rounds (default: 10) */
428
+ maxIterations?: number;
429
+ /**
430
+ * Called when the model requests a tool call.
431
+ *
432
+ * @param tool - The tool being called
433
+ * @param params - The parameters for the call
434
+ */
435
+ onToolCall?(tool: Tool, params: unknown): void | Promise<void>;
436
+ /**
437
+ * Called before tool execution. Can skip execution or transform parameters.
438
+ *
439
+ * @param tool - The tool about to be executed
440
+ * @param params - The parameters for the call
441
+ * @returns One of:
442
+ * - `false` to skip execution
443
+ * - `true` to proceed with original params
444
+ * - `BeforeCallResult` object to control execution and optionally transform params
445
+ */
446
+ onBeforeCall?(tool: Tool, params: unknown): boolean | BeforeCallResult | Promise<boolean | BeforeCallResult>;
447
+ /**
448
+ * Called after tool execution completes. Can transform the result.
449
+ *
450
+ * @param tool - The tool that was executed
451
+ * @param params - The parameters that were used
452
+ * @param result - The result from the tool
453
+ * @returns Void to use original result, or `AfterCallResult` to transform it
454
+ */
455
+ onAfterCall?(tool: Tool, params: unknown, result: unknown): void | AfterCallResult | Promise<void | AfterCallResult>;
456
+ /**
457
+ * Called when a tool execution throws an error.
458
+ *
459
+ * @param tool - The tool that failed
460
+ * @param params - The parameters that were used
461
+ * @param error - The error that was thrown
462
+ */
463
+ onError?(tool: Tool, params: unknown, error: Error): void | Promise<void>;
464
+ /**
465
+ * Called when the maximum iteration limit is reached.
466
+ *
467
+ * @param iterations - The number of iterations that were performed
468
+ */
469
+ onMaxIterations?(iterations: number): void | Promise<void>;
470
+ }
471
+ /**
472
+ * Record of a completed tool execution.
473
+ *
474
+ * Contains all information about a tool call that was executed,
475
+ * including timing and result data.
476
+ *
477
+ * @example
478
+ * ```typescript
479
+ * const execution: ToolExecution = {
480
+ * toolName: 'get_weather',
481
+ * toolCallId: 'call_abc123',
482
+ * arguments: { location: 'San Francisco' },
483
+ * result: { temperature: 72 },
484
+ * isError: false,
485
+ * duration: 150,
486
+ * approved: true
487
+ * };
488
+ * ```
489
+ */
490
+ interface ToolExecution {
491
+ /** Name of the tool that was called */
492
+ toolName: string;
493
+ /** Unique identifier for this tool call */
494
+ toolCallId: string;
495
+ /** Arguments that were passed to the tool */
496
+ arguments: Record<string, unknown>;
497
+ /** Result returned by the tool */
498
+ result: unknown;
499
+ /** Whether the tool execution resulted in an error */
500
+ isError: boolean;
501
+ /** Execution duration in milliseconds */
502
+ duration: number;
503
+ /** Whether approval was required and granted (undefined if no approval handler) */
504
+ approved?: boolean;
505
+ }
506
+
507
+ export type { AfterCallResult as A, BeforeCallResult as B, JSONSchema as J, Structure as S, ToolInput as T, ZodLike as Z, ZodV4Like as a, Tool as b, ToolCall as c, ToolResult as d, ToolExecution as e, ToolUseStrategy as f, JSONSchemaProperty as g, JSONSchemaPropertyType as h, ZodV3Like as i, ToolMetadata as j };
@@ -1,4 +1,4 @@
1
- import { S as StreamEvent } from './stream-sXhBtWjl.js';
1
+ import { S as StreamEvent } from './stream-DVVUIKpz.js';
2
2
 
3
3
  /**
4
4
  * @fileoverview Pub-sub middleware types for stream resumption.
@@ -35,6 +35,12 @@ type CompletionCallback = () => void;
35
35
  * Unsubscribe function returned by subscribe.
36
36
  */
37
37
  type Unsubscribe = () => void;
38
+ /**
39
+ * Final data callback when stream completes with data.
40
+ *
41
+ * @param data - The final data (typically serialized Turn)
42
+ */
43
+ type FinalDataCallback = (data: unknown) => void;
38
44
  /**
39
45
  * Storage adapter interface for pub-sub middleware.
40
46
  *
@@ -57,12 +63,19 @@ interface PubSubAdapter {
57
63
  getEvents(streamId: string): Promise<StreamEvent[]>;
58
64
  /**
59
65
  * Subscribes to live events (creates lazily if needed).
66
+ *
67
+ * @param onFinalData - Optional callback for final data (Turn) before completion
60
68
  */
61
- subscribe(streamId: string, onEvent: SubscriptionCallback, onComplete: CompletionCallback): Unsubscribe;
69
+ subscribe(streamId: string, onEvent: SubscriptionCallback, onComplete: CompletionCallback, onFinalData?: FinalDataCallback): Unsubscribe;
62
70
  /**
63
71
  * Publishes event to all subscribers.
64
72
  */
65
73
  publish(streamId: string, event: StreamEvent): void;
74
+ /**
75
+ * Sets final data to be sent to subscribers before completion.
76
+ * Typically used to send the serialized Turn.
77
+ */
78
+ setFinalData(streamId: string, data: unknown): void;
66
79
  /**
67
80
  * Notifies subscribers and removes stream from storage.
68
81
  */
@@ -93,4 +106,4 @@ interface MemoryAdapterOptions {
93
106
  maxStreams?: number;
94
107
  }
95
108
 
96
- export type { CompletionCallback as C, MemoryAdapterOptions as M, PubSubAdapter as P, StoredStream as S, Unsubscribe as U, PubSubOptions as a, SubscriptionCallback as b };
109
+ export type { CompletionCallback as C, FinalDataCallback as F, MemoryAdapterOptions as M, PubSubAdapter as P, StoredStream as S, Unsubscribe as U, PubSubOptions as a, SubscriptionCallback as b };
@@ -1,3 +1,5 @@
1
+ import { Z as ZodLike, a as ZodV4Like, J as JSONSchema, T as ToolInput, b as Tool } from '../tool-D22EhP5F.js';
2
+
1
3
  /**
2
4
  * @fileoverview Partial JSON parser for streaming LLM responses.
3
5
  *
@@ -50,4 +52,66 @@ interface PartialParseResult<T = unknown> {
50
52
  */
51
53
  declare function parsePartialJson<T = unknown>(json: string): PartialParseResult<T>;
52
54
 
53
- export { type PartialParseResult, parsePartialJson };
55
+ /**
56
+ * @fileoverview Zod schema utilities for structured output.
57
+ *
58
+ * Provides detection and conversion utilities for Zod schemas,
59
+ * supporting both Zod v3 and v4 schemas.
60
+ *
61
+ * @module utils/zod
62
+ */
63
+
64
+ /**
65
+ * Checks if a value is a Zod schema (v3 or v4).
66
+ *
67
+ * @param value - Value to check
68
+ * @returns True if the value appears to be a Zod schema
69
+ */
70
+ declare function isZodSchema(value: unknown): value is ZodLike;
71
+ /**
72
+ * Checks if a Zod schema is v4 or later.
73
+ *
74
+ * @param schema - Zod schema to check
75
+ * @returns True if the schema is Zod v4+
76
+ */
77
+ declare function isZodV4(schema: ZodLike): schema is ZodV4Like;
78
+ /**
79
+ * Converts a Zod schema to JSON Schema.
80
+ *
81
+ * For Zod v4+, uses native `z.toJSONSchema()`.
82
+ * For Zod v3, uses `zod-to-json-schema` package.
83
+ *
84
+ * @param schema - Zod schema to convert
85
+ * @returns JSON Schema representation
86
+ * @throws Error if required dependencies are not installed
87
+ */
88
+ declare function zodToJSONSchema(schema: ZodLike): Promise<JSONSchema>;
89
+ /**
90
+ * Synchronous version of zodToJSONSchema.
91
+ * Requires modules to be cached via prior async load.
92
+ *
93
+ * @param schema - Zod schema to convert
94
+ * @returns JSON Schema representation
95
+ * @throws Error if modules not loaded or dependencies missing
96
+ */
97
+ declare function zodToJSONSchemaSync(schema: ZodLike): JSONSchema;
98
+ /**
99
+ * Resolves a structure parameter that may be JSONSchema or Zod schema.
100
+ * Validates that the result is an object schema.
101
+ *
102
+ * @param structure - JSONSchema or Zod schema (must be object type)
103
+ * @returns Resolved JSONSchema
104
+ * @throws Error if schema is not an object type
105
+ */
106
+ declare function resolveStructure(structure: JSONSchema | ZodLike): JSONSchema;
107
+ /**
108
+ * Resolves an array of tools, converting any Zod parameters to JSONSchema.
109
+ * Validates that each tool's parameters is an object schema.
110
+ *
111
+ * @param tools - Array of tools with Structure parameters
112
+ * @returns Array of tools with resolved JSONSchema parameters
113
+ * @throws Error if any tool parameters is not an object schema
114
+ */
115
+ declare function resolveTools(tools: ToolInput[]): Tool[];
116
+
117
+ export { type PartialParseResult, isZodSchema, isZodV4, parsePartialJson, resolveStructure, resolveTools, zodToJSONSchema, zodToJSONSchemaSync };
@@ -1,7 +1,21 @@
1
1
  import {
2
2
  parsePartialJson
3
3
  } from "../chunk-I53CI6ZZ.js";
4
+ import {
5
+ isZodSchema,
6
+ isZodV4,
7
+ resolveStructure,
8
+ resolveTools,
9
+ zodToJSONSchema,
10
+ zodToJSONSchemaSync
11
+ } from "../chunk-3Q5VELKG.js";
4
12
  export {
5
- parsePartialJson
13
+ isZodSchema,
14
+ isZodV4,
15
+ parsePartialJson,
16
+ resolveStructure,
17
+ resolveTools,
18
+ zodToJSONSchema,
19
+ zodToJSONSchemaSync
6
20
  };
7
21
  //# sourceMappingURL=index.js.map
@@ -1,5 +1,6 @@
1
- import { e as Provider } from '../llm-DS_-l71X.js';
2
- import '../stream-sXhBtWjl.js';
1
+ import { e as Provider } from '../llm-CZqlijjK.js';
2
+ import '../stream-DVVUIKpz.js';
3
+ import '../tool-D22EhP5F.js';
3
4
 
4
5
  /**
5
6
  * xAI Chat Completions API parameters (OpenAI-compatible).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@providerprotocol/ai",
3
- "version": "0.0.38",
3
+ "version": "0.0.39",
4
4
  "description": "UPP: Unified Provider Protocol for AI inference",
5
5
  "license": "MIT",
6
6
  "homepage": "https://providerprotocol.org",
@@ -184,10 +184,22 @@
184
184
  "eslint-plugin-import": "^2.32.0",
185
185
  "tsup": "^8.5.1",
186
186
  "typedoc": "^0.28.16",
187
- "typedoc-plugin-markdown": "^4.9.0"
187
+ "typedoc-plugin-markdown": "^4.9.0",
188
+ "zod": "^4.3.6",
189
+ "zod-to-json-schema": "^3.25.1"
188
190
  },
189
191
  "peerDependencies": {
190
- "typescript": "^5.9.3"
192
+ "typescript": "^5.9.3",
193
+ "zod": "^3.23.0 || ^4.0.0",
194
+ "zod-to-json-schema": "^3.25.0"
195
+ },
196
+ "peerDependenciesMeta": {
197
+ "zod": {
198
+ "optional": true
199
+ },
200
+ "zod-to-json-schema": {
201
+ "optional": true
202
+ }
191
203
  },
192
204
  "keywords": [
193
205
  "ai",