universal-llm-client 3.0.0 → 4.0.0
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/CHANGELOG.md +103 -0
- package/LICENSE +1 -1
- package/README.md +181 -4
- package/dist/ai-model.d.ts +89 -0
- package/dist/ai-model.d.ts.map +1 -1
- package/dist/ai-model.js +96 -0
- package/dist/ai-model.js.map +1 -1
- package/dist/auditor.d.ts +5 -1
- package/dist/auditor.d.ts.map +1 -1
- package/dist/auditor.js +9 -0
- package/dist/auditor.js.map +1 -1
- package/dist/client.d.ts +14 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +52 -0
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -1
- package/dist/index.js.map +1 -1
- package/dist/interfaces.d.ts +143 -1
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/interfaces.js.map +1 -1
- package/dist/providers/google.d.ts.map +1 -1
- package/dist/providers/google.js +21 -0
- package/dist/providers/google.js.map +1 -1
- package/dist/providers/ollama.d.ts +13 -1
- package/dist/providers/ollama.d.ts.map +1 -1
- package/dist/providers/ollama.js +94 -25
- package/dist/providers/ollama.js.map +1 -1
- package/dist/providers/openai.d.ts +4 -0
- package/dist/providers/openai.d.ts.map +1 -1
- package/dist/providers/openai.js +51 -1
- package/dist/providers/openai.js.map +1 -1
- package/dist/router.d.ts +70 -0
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +343 -0
- package/dist/router.js.map +1 -1
- package/dist/structured-output.d.ts +467 -0
- package/dist/structured-output.d.ts.map +1 -0
- package/dist/structured-output.js +505 -0
- package/dist/structured-output.js.map +1 -0
- package/package.json +21 -4
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structured Output Core Types
|
|
3
|
+
*
|
|
4
|
+
* Core types for structured output support in universal-llm-client.
|
|
5
|
+
* Provides type-safe Zod schema integration and JSON Schema support.
|
|
6
|
+
*
|
|
7
|
+
* @module structured-output
|
|
8
|
+
*/
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
/**
|
|
11
|
+
* JSON Schema definition for structured output.
|
|
12
|
+
* This is a subset of JSON Schema focused on what providers need.
|
|
13
|
+
*/
|
|
14
|
+
export interface JSONSchema {
|
|
15
|
+
type?: string | string[];
|
|
16
|
+
properties?: Record<string, JSONSchema>;
|
|
17
|
+
items?: JSONSchema | {
|
|
18
|
+
type: string;
|
|
19
|
+
};
|
|
20
|
+
required?: string[];
|
|
21
|
+
additionalProperties?: boolean | JSONSchema;
|
|
22
|
+
enum?: (string | number | boolean | null)[];
|
|
23
|
+
const?: unknown;
|
|
24
|
+
oneOf?: JSONSchema[];
|
|
25
|
+
anyOf?: JSONSchema[];
|
|
26
|
+
allOf?: JSONSchema[];
|
|
27
|
+
not?: JSONSchema;
|
|
28
|
+
description?: string;
|
|
29
|
+
default?: unknown;
|
|
30
|
+
examples?: unknown[];
|
|
31
|
+
title?: string;
|
|
32
|
+
format?: string;
|
|
33
|
+
pattern?: string;
|
|
34
|
+
minLength?: number;
|
|
35
|
+
maxLength?: number;
|
|
36
|
+
minimum?: number;
|
|
37
|
+
maximum?: number;
|
|
38
|
+
exclusiveMinimum?: number;
|
|
39
|
+
exclusiveMaximum?: number;
|
|
40
|
+
minItems?: number;
|
|
41
|
+
maxItems?: number;
|
|
42
|
+
uniqueItems?: boolean;
|
|
43
|
+
minProperties?: number;
|
|
44
|
+
maxProperties?: number;
|
|
45
|
+
$ref?: string;
|
|
46
|
+
$id?: string;
|
|
47
|
+
$schema?: string;
|
|
48
|
+
definitions?: Record<string, JSONSchema>;
|
|
49
|
+
$defs?: Record<string, JSONSchema>;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Provider identifier for schema conversion.
|
|
53
|
+
*/
|
|
54
|
+
export type SchemaProvider = 'openai' | 'ollama' | 'google';
|
|
55
|
+
/**
|
|
56
|
+
* Result of converting a schema for a specific provider.
|
|
57
|
+
*/
|
|
58
|
+
export interface ProviderSchema {
|
|
59
|
+
/** The JSON Schema for the provider */
|
|
60
|
+
schema: JSONSchema;
|
|
61
|
+
/** Optional schema name (used by OpenAI) */
|
|
62
|
+
name?: string;
|
|
63
|
+
/** Optional schema description (used by OpenAI) */
|
|
64
|
+
description?: string;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Error options for StructuredOutputError
|
|
68
|
+
*/
|
|
69
|
+
export interface StructuredOutputErrorOptions {
|
|
70
|
+
/** The raw output from the LLM that failed validation */
|
|
71
|
+
rawOutput: string;
|
|
72
|
+
/** The underlying cause (e.g., ZodError) */
|
|
73
|
+
cause?: Error;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Custom error class for structured output validation failures.
|
|
77
|
+
*
|
|
78
|
+
* Thrown when:
|
|
79
|
+
* - JSON parsing of LLM response fails
|
|
80
|
+
* - Zod schema validation fails
|
|
81
|
+
*
|
|
82
|
+
* Features:
|
|
83
|
+
* - `rawOutput` property containing the original LLM response
|
|
84
|
+
* - `cause` property for the underlying error (ZodError, SyntaxError, etc.)
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* try {
|
|
89
|
+
* const result = await model.generateStructured(UserSchema, messages);
|
|
90
|
+
* } catch (error) {
|
|
91
|
+
* if (error instanceof StructuredOutputError) {
|
|
92
|
+
* console.log('Raw LLM output:', error.rawOutput);
|
|
93
|
+
* if (error.cause instanceof z.ZodError) {
|
|
94
|
+
* console.log('Validation issues:', error.cause.issues);
|
|
95
|
+
* }
|
|
96
|
+
* }
|
|
97
|
+
* }
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export declare class StructuredOutputError extends Error {
|
|
101
|
+
/** The raw output from the LLM that failed validation */
|
|
102
|
+
readonly rawOutput: string;
|
|
103
|
+
/** The underlying cause (e.g., ZodError for schema validation failures) */
|
|
104
|
+
readonly cause?: Error;
|
|
105
|
+
constructor(message: string, options: StructuredOutputErrorOptions);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Options for structured output generation.
|
|
109
|
+
*
|
|
110
|
+
* Accepts either:
|
|
111
|
+
* - A Zod schema (`schema`) for type-safe validation
|
|
112
|
+
* - A raw JSON Schema object (`jsonSchema`) for flexibility
|
|
113
|
+
*
|
|
114
|
+
* Optional `name` and `description` can be provided for LLM guidance
|
|
115
|
+
* (used by providers like OpenAI that support schema names).
|
|
116
|
+
*
|
|
117
|
+
* @template T The expected output type (inferred from Zod schema)
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* // Using Zod schema (recommended)
|
|
122
|
+
* const UserSchema = z.object({
|
|
123
|
+
* name: z.string(),
|
|
124
|
+
* age: z.number(),
|
|
125
|
+
* });
|
|
126
|
+
*
|
|
127
|
+
* const options: StructuredOutputOptions<User> = {
|
|
128
|
+
* schema: UserSchema,
|
|
129
|
+
* name: 'User',
|
|
130
|
+
* description: 'A user object',
|
|
131
|
+
* };
|
|
132
|
+
* ```
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```typescript
|
|
136
|
+
* // Using raw JSON Schema
|
|
137
|
+
* const options: StructuredOutputOptions<User> = {
|
|
138
|
+
* jsonSchema: {
|
|
139
|
+
* type: 'object',
|
|
140
|
+
* properties: {
|
|
141
|
+
* name: { type: 'string' },
|
|
142
|
+
* age: { type: 'number' },
|
|
143
|
+
* },
|
|
144
|
+
* required: ['name', 'age'],
|
|
145
|
+
* },
|
|
146
|
+
* };
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
export interface StructuredOutputOptions<T> {
|
|
150
|
+
/**
|
|
151
|
+
* Zod schema for structured output.
|
|
152
|
+
* Use this for type-safe validation with automatic type inference.
|
|
153
|
+
*/
|
|
154
|
+
schema?: z.ZodType<T>;
|
|
155
|
+
/**
|
|
156
|
+
* Raw JSON Schema for structured output.
|
|
157
|
+
* Use this when you have a pre-defined schema without Zod.
|
|
158
|
+
*/
|
|
159
|
+
jsonSchema?: JSONSchema;
|
|
160
|
+
/**
|
|
161
|
+
* Optional name for the schema.
|
|
162
|
+
* Used by providers like OpenAI for better LLM guidance.
|
|
163
|
+
*/
|
|
164
|
+
name?: string;
|
|
165
|
+
/**
|
|
166
|
+
* Optional description for the schema.
|
|
167
|
+
* Used by providers like OpenAI for better LLM guidance.
|
|
168
|
+
*/
|
|
169
|
+
description?: string;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Successful structured output result.
|
|
173
|
+
*
|
|
174
|
+
* @template T The output type
|
|
175
|
+
*/
|
|
176
|
+
export interface StructuredOutputSuccess<T> {
|
|
177
|
+
/** Indicates success */
|
|
178
|
+
readonly ok: true;
|
|
179
|
+
/** The validated output value */
|
|
180
|
+
readonly value: T;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Failed structured output result.
|
|
184
|
+
*
|
|
185
|
+
* @template _T The expected output type (unused but kept for type alignment with StructuredOutputSuccess)
|
|
186
|
+
*/
|
|
187
|
+
export interface StructuredOutputFailure<_T> {
|
|
188
|
+
/** Indicates failure */
|
|
189
|
+
readonly ok: false;
|
|
190
|
+
/** The error that occurred */
|
|
191
|
+
readonly error: StructuredOutputError;
|
|
192
|
+
/** The raw output from the LLM */
|
|
193
|
+
readonly rawOutput: string;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Result of structured output parsing.
|
|
197
|
+
*
|
|
198
|
+
* Discriminated union type that provides type-safe result handling:
|
|
199
|
+
* - `ok: true` → `{ value: T }`
|
|
200
|
+
* - `ok: false` → `{ error: StructuredOutputError, rawOutput: string }`
|
|
201
|
+
*
|
|
202
|
+
* @template T The output type
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```typescript
|
|
206
|
+
* const result = await model.tryParseStructured(UserSchema, messages);
|
|
207
|
+
*
|
|
208
|
+
* if (result.ok) {
|
|
209
|
+
* // TypeScript knows result.value is User
|
|
210
|
+
* console.log('User:', result.value.name);
|
|
211
|
+
* } else {
|
|
212
|
+
* // TypeScript knows result.error is StructuredOutputError
|
|
213
|
+
* console.log('Error:', result.error.message);
|
|
214
|
+
* console.log('Raw output:', result.rawOutput);
|
|
215
|
+
* }
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
export type StructuredOutputResult<T> = StructuredOutputSuccess<T> | StructuredOutputFailure<T>;
|
|
219
|
+
/**
|
|
220
|
+
* Type guard to check if a structured output result is successful.
|
|
221
|
+
*
|
|
222
|
+
* @param result The result to check
|
|
223
|
+
* @returns true if the result is successful
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```typescript
|
|
227
|
+
* const result = await model.tryParseStructured(UserSchema, messages);
|
|
228
|
+
*
|
|
229
|
+
* if (isStructuredOutputSuccess(result)) {
|
|
230
|
+
* console.log('User:', result.value.name);
|
|
231
|
+
* } else {
|
|
232
|
+
* console.log('Error:', result.error.message);
|
|
233
|
+
* }
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
export declare function isStructuredOutputSuccess<T>(result: StructuredOutputResult<T>): result is StructuredOutputSuccess<T>;
|
|
237
|
+
/**
|
|
238
|
+
* Type guard to check if a structured output result is a failure.
|
|
239
|
+
*
|
|
240
|
+
* @param result The result to check
|
|
241
|
+
* @returns true if the result is a failure
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```typescript
|
|
245
|
+
* const result = await model.tryParseStructured(UserSchema, messages);
|
|
246
|
+
*
|
|
247
|
+
* if (isStructuredOutputFailure(result)) {
|
|
248
|
+
* console.log('Error:', result.error.message);
|
|
249
|
+
* console.log('Raw:', result.rawOutput);
|
|
250
|
+
* }
|
|
251
|
+
* ```
|
|
252
|
+
*/
|
|
253
|
+
export declare function isStructuredOutputFailure<T>(result: StructuredOutputResult<T>): result is StructuredOutputFailure<T>;
|
|
254
|
+
/**
|
|
255
|
+
* Convert a Zod schema to JSON Schema.
|
|
256
|
+
*
|
|
257
|
+
* Uses Zod 4's native `z.toJSONSchema()` for conversion.
|
|
258
|
+
* Handles all Zod types including objects, arrays, primitives, enums, and nested structures.
|
|
259
|
+
*
|
|
260
|
+
* @param schema The Zod schema to convert
|
|
261
|
+
* @returns JSON Schema representation
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```typescript
|
|
265
|
+
* const UserSchema = z.object({
|
|
266
|
+
* name: z.string(),
|
|
267
|
+
* age: z.number(),
|
|
268
|
+
* });
|
|
269
|
+
*
|
|
270
|
+
* const jsonSchema = zodToJsonSchema(UserSchema);
|
|
271
|
+
* // { type: 'object', properties: { name: { type: 'string' }, ... }, required: ['name', 'age'] }
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
274
|
+
export declare function zodToJsonSchema<T>(schema: z.ZodType<T>): JSONSchema;
|
|
275
|
+
/**
|
|
276
|
+
* Normalize a raw JSON Schema.
|
|
277
|
+
*
|
|
278
|
+
* Currently passes through without modification.
|
|
279
|
+
* Future versions may add normalization for provider compatibility.
|
|
280
|
+
*
|
|
281
|
+
* @param schema The JSON Schema to normalize
|
|
282
|
+
* @returns Normalized JSON Schema
|
|
283
|
+
*/
|
|
284
|
+
export declare function normalizeJsonSchema(schema: JSONSchema): JSONSchema;
|
|
285
|
+
/**
|
|
286
|
+
* Get the JSON Schema from options.
|
|
287
|
+
*
|
|
288
|
+
* Converts Zod schema to JSON Schema if necessary, or normalizes raw JSON Schema.
|
|
289
|
+
*
|
|
290
|
+
* @param options The structured output options
|
|
291
|
+
* @returns JSON Schema
|
|
292
|
+
*/
|
|
293
|
+
export declare function getJsonSchema<T>(options: StructuredOutputOptions<T>): JSONSchema;
|
|
294
|
+
/**
|
|
295
|
+
* Strip unsupported features from a JSON Schema for a specific provider.
|
|
296
|
+
*
|
|
297
|
+
* Google/Gemini doesn't support certain JSON Schema features like pattern, min/max.
|
|
298
|
+
* This function removes those recursively.
|
|
299
|
+
*
|
|
300
|
+
* @param schema The JSON Schema to transform
|
|
301
|
+
* @param provider The target provider
|
|
302
|
+
* @returns Cleaned JSON Schema
|
|
303
|
+
*
|
|
304
|
+
* @example
|
|
305
|
+
* ```typescript
|
|
306
|
+
* const schema = {
|
|
307
|
+
* type: 'string',
|
|
308
|
+
* pattern: '^[a-z]+$',
|
|
309
|
+
* minLength: 1,
|
|
310
|
+
* };
|
|
311
|
+
*
|
|
312
|
+
* const cleaned = stripUnsupportedFeatures(schema, 'google');
|
|
313
|
+
* // { type: 'string' } - pattern and minLength removed
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
export declare function stripUnsupportedFeatures(schema: JSONSchema, provider: SchemaProvider): JSONSchema;
|
|
317
|
+
/**
|
|
318
|
+
* Convert structured output options to a provider-specific schema.
|
|
319
|
+
*
|
|
320
|
+
* This function:
|
|
321
|
+
* 1. Extracts/converts the JSON Schema from options
|
|
322
|
+
* 2. Applies provider-specific transformations (e.g., removing unsupported features for Google)
|
|
323
|
+
* 3. Adds name/description for LLM guidance
|
|
324
|
+
*
|
|
325
|
+
* @param provider The target provider
|
|
326
|
+
* @param options The structured output options
|
|
327
|
+
* @returns Provider-ready schema with name and description
|
|
328
|
+
*
|
|
329
|
+
* @example
|
|
330
|
+
* ```typescript
|
|
331
|
+
* const result = convertToProviderSchema('openai', {
|
|
332
|
+
* schema: z.object({ name: z.string() }),
|
|
333
|
+
* name: 'User',
|
|
334
|
+
* description: 'A user object',
|
|
335
|
+
* });
|
|
336
|
+
*
|
|
337
|
+
* // result.schema - JSON Schema
|
|
338
|
+
* // result.name - 'User'
|
|
339
|
+
* // result.description - 'A user object'
|
|
340
|
+
* ```
|
|
341
|
+
*/
|
|
342
|
+
export declare function convertToProviderSchema<T>(provider: SchemaProvider, options: StructuredOutputOptions<T>): ProviderSchema;
|
|
343
|
+
/**
|
|
344
|
+
* Parse and validate structured output from raw LLM response text.
|
|
345
|
+
*
|
|
346
|
+
* This function:
|
|
347
|
+
* 1. Parses JSON from the raw output string
|
|
348
|
+
* 2. Validates the parsed data against the Zod schema
|
|
349
|
+
* 3. Throws StructuredOutputError on failure
|
|
350
|
+
*
|
|
351
|
+
* @param schema The Zod schema to validate against
|
|
352
|
+
* @param rawOutput The raw string output from the LLM
|
|
353
|
+
* @returns The validated and typed data
|
|
354
|
+
* @throws StructuredOutputError if JSON parsing fails or schema validation fails
|
|
355
|
+
*
|
|
356
|
+
* @example
|
|
357
|
+
* ```typescript
|
|
358
|
+
* const schema = z.object({ name: z.string(), age: z.number() });
|
|
359
|
+
* const rawOutput = '{"name": "Alice", "age": 30}';
|
|
360
|
+
* const result = parseStructured(schema, rawOutput); // { name: "Alice", age: 30 }
|
|
361
|
+
* ```
|
|
362
|
+
*/
|
|
363
|
+
export declare function parseStructured<T>(schema: z.ZodType<T>, rawOutput: string): T;
|
|
364
|
+
/**
|
|
365
|
+
* Try to parse and validate structured output, returning a result object.
|
|
366
|
+
*
|
|
367
|
+
* This is the non-throwing variant of `parseStructured`. Instead of throwing
|
|
368
|
+
* on validation failure, it returns a result object with `ok: false` and
|
|
369
|
+
* the error details.
|
|
370
|
+
*
|
|
371
|
+
* @param schema The Zod schema to validate against
|
|
372
|
+
* @param rawOutput The raw string output from the LLM
|
|
373
|
+
* @returns A result object: `{ ok: true, value }` on success, `{ ok: false, error, rawOutput }` on failure
|
|
374
|
+
*
|
|
375
|
+
* @example
|
|
376
|
+
* ```typescript
|
|
377
|
+
* const schema = z.object({ name: z.string(), age: z.number() });
|
|
378
|
+
*
|
|
379
|
+
* // Success case
|
|
380
|
+
* const result1 = tryParseStructured(schema, '{"name": "Alice", "age": 30}');
|
|
381
|
+
* if (result1.ok) {
|
|
382
|
+
* console.log(result1.value.name); // "Alice"
|
|
383
|
+
* }
|
|
384
|
+
*
|
|
385
|
+
* // Failure case
|
|
386
|
+
* const result2 = tryParseStructured(schema, 'invalid json');
|
|
387
|
+
* if (!result2.ok) {
|
|
388
|
+
* console.log(result2.error.message); // Error message
|
|
389
|
+
* console.log(result2.rawOutput); // Original output
|
|
390
|
+
* }
|
|
391
|
+
* ```
|
|
392
|
+
*/
|
|
393
|
+
export declare function tryParseStructured<T>(schema: z.ZodType<T>, rawOutput: string): StructuredOutputResult<T>;
|
|
394
|
+
/**
|
|
395
|
+
* Validate already-parsed data against a Zod schema.
|
|
396
|
+
*
|
|
397
|
+
* This is useful when you have already parsed JSON and need to validate
|
|
398
|
+
* it against a schema, with optional raw output for error messages.
|
|
399
|
+
*
|
|
400
|
+
* @param schema The Zod schema to validate against
|
|
401
|
+
* @param data The parsed data to validate
|
|
402
|
+
* @param rawOutput Optional raw output string for error messages
|
|
403
|
+
* @returns The validated and typed data
|
|
404
|
+
* @throws StructuredOutputError if schema validation fails
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
* ```typescript
|
|
408
|
+
* const schema = z.object({ name: z.string(), age: z.number() });
|
|
409
|
+
* const data = JSON.parse('{"name": "Alice", "age": 30}');
|
|
410
|
+
* const result = validateStructuredOutput(schema, data); // { name: "Alice", age: 30 }
|
|
411
|
+
* ```
|
|
412
|
+
*/
|
|
413
|
+
export declare function validateStructuredOutput<T>(schema: z.ZodType<T>, data: unknown, rawOutput?: string): T;
|
|
414
|
+
/**
|
|
415
|
+
* Incremental JSON parser for streaming structured output.
|
|
416
|
+
*
|
|
417
|
+
* Allows parsing partial JSON as it streams in, returning validated partial
|
|
418
|
+
* objects when possible. Useful for structured output streaming where you
|
|
419
|
+
* want to see partial results before the complete JSON arrives.
|
|
420
|
+
*/
|
|
421
|
+
export declare class StreamingJsonParser<T> {
|
|
422
|
+
private buffer;
|
|
423
|
+
private schema;
|
|
424
|
+
constructor(schema: z.ZodType<T>);
|
|
425
|
+
/**
|
|
426
|
+
* Feed a chunk of JSON text to the parser.
|
|
427
|
+
* Returns a validated partial object if the current buffer can be parsed
|
|
428
|
+
* as valid JSON that passes the schema, or undefined if not yet valid.
|
|
429
|
+
*
|
|
430
|
+
* For partial objects, this attempts to:
|
|
431
|
+
* 1. Add closing braces/brackets to make valid JSON
|
|
432
|
+
* 2. Fill in missing required fields with null/empty values
|
|
433
|
+
* 3. Validate against schema
|
|
434
|
+
*
|
|
435
|
+
* @param chunk Text chunk from the stream
|
|
436
|
+
* @returns Validated partial object or undefined
|
|
437
|
+
*/
|
|
438
|
+
feed(chunk: string): {
|
|
439
|
+
partial: T | undefined;
|
|
440
|
+
complete: boolean;
|
|
441
|
+
};
|
|
442
|
+
/**
|
|
443
|
+
* Get the current buffer content.
|
|
444
|
+
*/
|
|
445
|
+
getBuffer(): string;
|
|
446
|
+
/**
|
|
447
|
+
* Reset the parser state.
|
|
448
|
+
*/
|
|
449
|
+
reset(): void;
|
|
450
|
+
/**
|
|
451
|
+
* Attempt to parse partial JSON by adding closing brackets.
|
|
452
|
+
*/
|
|
453
|
+
private tryParsePartial;
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Streaming structured output result.
|
|
457
|
+
* Each partial yield from the generator is a validated object (possibly partial).
|
|
458
|
+
* The final return value is the complete validated object.
|
|
459
|
+
*/
|
|
460
|
+
export interface StreamingStructuredResult<T> {
|
|
461
|
+
/** Whether this is partial (incomplete) data */
|
|
462
|
+
partial: boolean;
|
|
463
|
+
/** The validated partial or complete object */
|
|
464
|
+
value: T;
|
|
465
|
+
}
|
|
466
|
+
export { z } from 'zod';
|
|
467
|
+
//# sourceMappingURL=structured-output.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"structured-output.d.ts","sourceRoot":"","sources":["../src/structured-output.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAMxB;;;GAGG;AACH,MAAM,WAAW,UAAU;IACvB,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACzB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IACxC,KA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