universal-llm-client 4.0.0 → 4.2.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/dist/ai-model.d.ts +20 -22
- package/dist/ai-model.d.ts.map +1 -1
- package/dist/ai-model.js +26 -23
- package/dist/ai-model.js.map +1 -1
- package/dist/client.d.ts +5 -5
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +17 -9
- package/dist/client.js.map +1 -1
- package/dist/http.d.ts +2 -0
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +1 -0
- package/dist/http.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/interfaces.d.ts +49 -11
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/interfaces.js +14 -0
- package/dist/interfaces.js.map +1 -1
- package/dist/providers/anthropic.d.ts +56 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +524 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/google.d.ts +5 -0
- package/dist/providers/google.d.ts.map +1 -1
- package/dist/providers/google.js +64 -8
- package/dist/providers/google.js.map +1 -1
- package/dist/providers/index.d.ts +1 -0
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +1 -0
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/ollama.d.ts.map +1 -1
- package/dist/providers/ollama.js +38 -11
- package/dist/providers/ollama.js.map +1 -1
- package/dist/providers/openai.d.ts.map +1 -1
- package/dist/providers/openai.js +9 -7
- package/dist/providers/openai.js.map +1 -1
- package/dist/router.d.ts +13 -33
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +33 -57
- package/dist/router.js.map +1 -1
- package/dist/stream-decoder.d.ts +29 -2
- package/dist/stream-decoder.d.ts.map +1 -1
- package/dist/stream-decoder.js +39 -11
- package/dist/stream-decoder.js.map +1 -1
- package/dist/structured-output.d.ts +107 -181
- package/dist/structured-output.d.ts.map +1 -1
- package/dist/structured-output.js +137 -192
- package/dist/structured-output.js.map +1 -1
- package/dist/zod-adapter.d.ts +44 -0
- package/dist/zod-adapter.d.ts.map +1 -0
- package/dist/zod-adapter.js +61 -0
- package/dist/zod-adapter.js.map +1 -0
- package/package.json +9 -1
- package/src/ai-model.ts +350 -0
- package/src/auditor.ts +213 -0
- package/src/client.ts +402 -0
- package/src/debug/debug-google-streaming.ts +97 -0
- package/src/debug/debug-tool-execution.ts +86 -0
- package/src/debug/test-lmstudio-tools.ts +155 -0
- package/src/demos/README.md +47 -0
- package/src/demos/basic/universal-llm-examples.ts +161 -0
- package/src/demos/mcp/astrid-memory-demo.ts +295 -0
- package/src/demos/mcp/astrid-persona-memory.ts +357 -0
- package/src/demos/mcp/mcp-mongodb-demo.ts +275 -0
- package/src/demos/mcp/simple-astrid-memory.ts +148 -0
- package/src/demos/mcp/simple-mcp-demo.ts +68 -0
- package/src/demos/mcp/working-mcp-demo.ts +62 -0
- package/src/demos/model-alias-demo.ts +0 -0
- package/src/demos/tools/RAG_MEMORY_INTEGRATION.md +267 -0
- package/src/demos/tools/astrid-memory-demo.ts +270 -0
- package/src/demos/tools/astrid-production-memory-clean.ts +785 -0
- package/src/demos/tools/astrid-production-memory.ts +558 -0
- package/src/demos/tools/basic-translation-test.ts +66 -0
- package/src/demos/tools/chromadb-similarity-tuning.ts +390 -0
- package/src/demos/tools/clean-multilingual-conversation.ts +209 -0
- package/src/demos/tools/clean-translation-test.ts +119 -0
- package/src/demos/tools/clean-universal-multilingual-test.ts +131 -0
- package/src/demos/tools/complete-rag-demo.ts +369 -0
- package/src/demos/tools/complete-tool-demo.ts +132 -0
- package/src/demos/tools/demo-tool-calling.ts +124 -0
- package/src/demos/tools/dynamic-language-switching-test.ts +251 -0
- package/src/demos/tools/hybrid-thinking-test.ts +154 -0
- package/src/demos/tools/memory-integration-test.ts +420 -0
- package/src/demos/tools/multilingual-memory-system.ts +802 -0
- package/src/demos/tools/ondemand-translation-demo.ts +655 -0
- package/src/demos/tools/production-tool-demo.ts +245 -0
- package/src/demos/tools/revolutionary-multilingual-test.ts +151 -0
- package/src/demos/tools/rigorous-language-analysis.ts +218 -0
- package/src/demos/tools/test-universal-memory-system.ts +126 -0
- package/src/demos/tools/translation-integration-guide.ts +346 -0
- package/src/demos/tools/universal-memory-system.ts +560 -0
- package/src/http.ts +247 -0
- package/src/index.ts +161 -0
- package/src/interfaces.ts +657 -0
- package/src/mcp.ts +345 -0
- package/src/providers/anthropic.ts +762 -0
- package/src/providers/google.ts +620 -0
- package/src/providers/index.ts +8 -0
- package/src/providers/ollama.ts +469 -0
- package/src/providers/openai.ts +392 -0
- package/src/router.ts +780 -0
- package/src/stream-decoder.ts +361 -0
- package/src/structured-output.ts +759 -0
- package/src/test-scripts/test-advanced-tools.ts +310 -0
- package/src/test-scripts/test-google-streaming-enhanced.ts +147 -0
- package/src/test-scripts/test-google-streaming.ts +63 -0
- package/src/test-scripts/test-google-system-prompt-comprehensive.ts +189 -0
- package/src/test-scripts/test-mcp-config.ts +28 -0
- package/src/test-scripts/test-mcp-connection.ts +29 -0
- package/src/test-scripts/test-system-message-positions.ts +163 -0
- package/src/test-scripts/test-system-prompt-improvement-demo.ts +83 -0
- package/src/test-scripts/test-tool-calling.ts +231 -0
- package/src/tests/ai-model.test.ts +1614 -0
- package/src/tests/auditor.test.ts +224 -0
- package/src/tests/http.test.ts +200 -0
- package/src/tests/interfaces.test.ts +117 -0
- package/src/tests/providers/google.test.ts +660 -0
- package/src/tests/providers/ollama.test.ts +954 -0
- package/src/tests/providers/openai.test.ts +1122 -0
- package/src/tests/router.test.ts +254 -0
- package/src/tests/stream-decoder.test.ts +179 -0
- package/src/tests/structured-output.test.ts +1450 -0
- package/src/tests/tools.test.ts +175 -0
- package/src/tools.ts +246 -0
- package/src/zod-adapter.ts +72 -0
|
@@ -2,32 +2,31 @@
|
|
|
2
2
|
* Structured Output Core Types
|
|
3
3
|
*
|
|
4
4
|
* Core types for structured output support in universal-llm-client.
|
|
5
|
-
*
|
|
5
|
+
* Zero-dependency — works with raw JSON Schema and optional validate functions.
|
|
6
|
+
*
|
|
7
|
+
* For Zod integration, use the `universal-llm-client/zod` entrypoint.
|
|
6
8
|
*
|
|
7
9
|
* @module structured-output
|
|
8
10
|
*/
|
|
9
|
-
import { z } from 'zod';
|
|
10
11
|
/**
|
|
11
12
|
* Custom error class for structured output validation failures.
|
|
12
13
|
*
|
|
13
14
|
* Thrown when:
|
|
14
15
|
* - JSON parsing of LLM response fails
|
|
15
|
-
* -
|
|
16
|
+
* - Schema validation fails
|
|
16
17
|
*
|
|
17
18
|
* Features:
|
|
18
19
|
* - `rawOutput` property containing the original LLM response
|
|
19
|
-
* - `cause` property for the underlying error
|
|
20
|
+
* - `cause` property for the underlying error
|
|
20
21
|
*
|
|
21
22
|
* @example
|
|
22
23
|
* ```typescript
|
|
23
24
|
* try {
|
|
24
|
-
* const result = await model.generateStructured(
|
|
25
|
+
* const result = await model.generateStructured(schema, messages);
|
|
25
26
|
* } catch (error) {
|
|
26
27
|
* if (error instanceof StructuredOutputError) {
|
|
27
28
|
* console.log('Raw LLM output:', error.rawOutput);
|
|
28
|
-
*
|
|
29
|
-
* console.log('Validation issues:', error.cause.issues);
|
|
30
|
-
* }
|
|
29
|
+
* console.log('Cause:', error.cause);
|
|
31
30
|
* }
|
|
32
31
|
* }
|
|
33
32
|
* ```
|
|
@@ -35,7 +34,7 @@ import { z } from 'zod';
|
|
|
35
34
|
export class StructuredOutputError extends Error {
|
|
36
35
|
/** The raw output from the LLM that failed validation */
|
|
37
36
|
rawOutput;
|
|
38
|
-
/** The underlying cause (e.g.,
|
|
37
|
+
/** The underlying cause (e.g., validation error) */
|
|
39
38
|
cause;
|
|
40
39
|
constructor(message, options) {
|
|
41
40
|
super(message);
|
|
@@ -52,39 +51,12 @@ export class StructuredOutputError extends Error {
|
|
|
52
51
|
// ============================================================================
|
|
53
52
|
/**
|
|
54
53
|
* Type guard to check if a structured output result is successful.
|
|
55
|
-
*
|
|
56
|
-
* @param result The result to check
|
|
57
|
-
* @returns true if the result is successful
|
|
58
|
-
*
|
|
59
|
-
* @example
|
|
60
|
-
* ```typescript
|
|
61
|
-
* const result = await model.tryParseStructured(UserSchema, messages);
|
|
62
|
-
*
|
|
63
|
-
* if (isStructuredOutputSuccess(result)) {
|
|
64
|
-
* console.log('User:', result.value.name);
|
|
65
|
-
* } else {
|
|
66
|
-
* console.log('Error:', result.error.message);
|
|
67
|
-
* }
|
|
68
|
-
* ```
|
|
69
54
|
*/
|
|
70
55
|
export function isStructuredOutputSuccess(result) {
|
|
71
56
|
return result.ok === true;
|
|
72
57
|
}
|
|
73
58
|
/**
|
|
74
59
|
* Type guard to check if a structured output result is a failure.
|
|
75
|
-
*
|
|
76
|
-
* @param result The result to check
|
|
77
|
-
* @returns true if the result is a failure
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* ```typescript
|
|
81
|
-
* const result = await model.tryParseStructured(UserSchema, messages);
|
|
82
|
-
*
|
|
83
|
-
* if (isStructuredOutputFailure(result)) {
|
|
84
|
-
* console.log('Error:', result.error.message);
|
|
85
|
-
* console.log('Raw:', result.rawOutput);
|
|
86
|
-
* }
|
|
87
|
-
* ```
|
|
88
60
|
*/
|
|
89
61
|
export function isStructuredOutputFailure(result) {
|
|
90
62
|
return result.ok === false;
|
|
@@ -92,36 +64,6 @@ export function isStructuredOutputFailure(result) {
|
|
|
92
64
|
// ============================================================================
|
|
93
65
|
// Schema Conversion Utilities
|
|
94
66
|
// ============================================================================
|
|
95
|
-
/**
|
|
96
|
-
* Convert a Zod schema to JSON Schema.
|
|
97
|
-
*
|
|
98
|
-
* Uses Zod 4's native `z.toJSONSchema()` for conversion.
|
|
99
|
-
* Handles all Zod types including objects, arrays, primitives, enums, and nested structures.
|
|
100
|
-
*
|
|
101
|
-
* @param schema The Zod schema to convert
|
|
102
|
-
* @returns JSON Schema representation
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```typescript
|
|
106
|
-
* const UserSchema = z.object({
|
|
107
|
-
* name: z.string(),
|
|
108
|
-
* age: z.number(),
|
|
109
|
-
* });
|
|
110
|
-
*
|
|
111
|
-
* const jsonSchema = zodToJsonSchema(UserSchema);
|
|
112
|
-
* // { type: 'object', properties: { name: { type: 'string' }, ... }, required: ['name', 'age'] }
|
|
113
|
-
* ```
|
|
114
|
-
*/
|
|
115
|
-
export function zodToJsonSchema(schema) {
|
|
116
|
-
const result = z.toJSONSchema(schema, {
|
|
117
|
-
target: 'draft-07',
|
|
118
|
-
unrepresentable: 'any',
|
|
119
|
-
});
|
|
120
|
-
// Cast to our JSONSchema type and clean up
|
|
121
|
-
const cleanResult = result;
|
|
122
|
-
delete cleanResult.$schema;
|
|
123
|
-
return cleanResult;
|
|
124
|
-
}
|
|
125
67
|
/**
|
|
126
68
|
* Normalize a raw JSON Schema.
|
|
127
69
|
*
|
|
@@ -136,21 +78,25 @@ export function normalizeJsonSchema(schema) {
|
|
|
136
78
|
return JSON.parse(JSON.stringify(schema));
|
|
137
79
|
}
|
|
138
80
|
/**
|
|
139
|
-
* Get the JSON Schema from
|
|
140
|
-
*
|
|
141
|
-
* Converts Zod schema to JSON Schema if necessary, or normalizes raw JSON Schema.
|
|
81
|
+
* Get the JSON Schema from a SchemaConfig or StructuredOutputOptions.
|
|
142
82
|
*
|
|
143
83
|
* @param options The structured output options
|
|
144
84
|
* @returns JSON Schema
|
|
145
85
|
*/
|
|
146
86
|
export function getJsonSchema(options) {
|
|
147
|
-
if (options.
|
|
148
|
-
return
|
|
87
|
+
if (options.schemaConfig) {
|
|
88
|
+
return normalizeJsonSchema(options.schemaConfig.jsonSchema);
|
|
149
89
|
}
|
|
150
90
|
if (options.jsonSchema) {
|
|
151
91
|
return normalizeJsonSchema(options.jsonSchema);
|
|
152
92
|
}
|
|
153
|
-
throw new Error('Either
|
|
93
|
+
throw new Error('Either schemaConfig or jsonSchema must be provided');
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Get the JSON Schema from a SchemaConfig directly.
|
|
97
|
+
*/
|
|
98
|
+
export function getJsonSchemaFromConfig(config) {
|
|
99
|
+
return normalizeJsonSchema(config.jsonSchema);
|
|
154
100
|
}
|
|
155
101
|
/**
|
|
156
102
|
* Features that some providers don't support.
|
|
@@ -176,18 +122,6 @@ const GOOGLE_UNSUPPORTED_FEATURES = [
|
|
|
176
122
|
* @param schema The JSON Schema to transform
|
|
177
123
|
* @param provider The target provider
|
|
178
124
|
* @returns Cleaned JSON Schema
|
|
179
|
-
*
|
|
180
|
-
* @example
|
|
181
|
-
* ```typescript
|
|
182
|
-
* const schema = {
|
|
183
|
-
* type: 'string',
|
|
184
|
-
* pattern: '^[a-z]+$',
|
|
185
|
-
* minLength: 1,
|
|
186
|
-
* };
|
|
187
|
-
*
|
|
188
|
-
* const cleaned = stripUnsupportedFeatures(schema, 'google');
|
|
189
|
-
* // { type: 'string' } - pattern and minLength removed
|
|
190
|
-
* ```
|
|
191
125
|
*/
|
|
192
126
|
export function stripUnsupportedFeatures(schema, provider) {
|
|
193
127
|
// Only Google needs transformation currently
|
|
@@ -208,12 +142,12 @@ export function stripUnsupportedFeatures(schema, provider) {
|
|
|
208
142
|
}
|
|
209
143
|
}
|
|
210
144
|
}
|
|
211
|
-
if (result
|
|
212
|
-
if (Array.isArray(result
|
|
213
|
-
result
|
|
145
|
+
if (result['items']) {
|
|
146
|
+
if (Array.isArray(result['items'])) {
|
|
147
|
+
result['items'] = result['items'].map(item => stripUnsupportedFeatures(item, provider));
|
|
214
148
|
}
|
|
215
149
|
else {
|
|
216
|
-
result
|
|
150
|
+
result['items'] = stripUnsupportedFeatures(result['items'], provider);
|
|
217
151
|
}
|
|
218
152
|
}
|
|
219
153
|
// Handle oneOf, anyOf, allOf
|
|
@@ -232,80 +166,114 @@ export function stripUnsupportedFeatures(schema, provider) {
|
|
|
232
166
|
* 1. Extracts/converts the JSON Schema from options
|
|
233
167
|
* 2. Applies provider-specific transformations (e.g., removing unsupported features for Google)
|
|
234
168
|
* 3. Adds name/description for LLM guidance
|
|
235
|
-
*
|
|
236
|
-
* @param provider The target provider
|
|
237
|
-
* @param options The structured output options
|
|
238
|
-
* @returns Provider-ready schema with name and description
|
|
239
|
-
*
|
|
240
|
-
* @example
|
|
241
|
-
* ```typescript
|
|
242
|
-
* const result = convertToProviderSchema('openai', {
|
|
243
|
-
* schema: z.object({ name: z.string() }),
|
|
244
|
-
* name: 'User',
|
|
245
|
-
* description: 'A user object',
|
|
246
|
-
* });
|
|
247
|
-
*
|
|
248
|
-
* // result.schema - JSON Schema
|
|
249
|
-
* // result.name - 'User'
|
|
250
|
-
* // result.description - 'A user object'
|
|
251
|
-
* ```
|
|
252
169
|
*/
|
|
253
170
|
export function convertToProviderSchema(provider, options) {
|
|
254
|
-
// Get the JSON Schema
|
|
171
|
+
// Get the JSON Schema
|
|
255
172
|
const jsonSchema = getJsonSchema(options);
|
|
256
173
|
// Apply provider-specific transformations
|
|
257
174
|
const schema = stripUnsupportedFeatures(jsonSchema, provider);
|
|
258
175
|
// Generate a default name if not provided (some providers require it)
|
|
259
|
-
const name = options.name ?? 'response';
|
|
176
|
+
const name = options.name ?? options.schemaConfig?.name ?? 'response';
|
|
260
177
|
return {
|
|
261
178
|
schema,
|
|
262
179
|
name,
|
|
263
|
-
description: options.description,
|
|
180
|
+
description: options.description ?? options.schemaConfig?.description,
|
|
264
181
|
};
|
|
265
182
|
}
|
|
266
183
|
// ============================================================================
|
|
267
184
|
// Validation Functions
|
|
268
185
|
// ============================================================================
|
|
186
|
+
/**
|
|
187
|
+
* Strip markdown code fences from raw LLM output.
|
|
188
|
+
*
|
|
189
|
+
* Some providers/models — notably Gemma variants via Ollama — wrap
|
|
190
|
+
* structured JSON output in ` ```json ... ``` ` fences even when a JSON
|
|
191
|
+
* schema is passed via the `format` field. This helper extracts the
|
|
192
|
+
* inner payload so it can be fed to `JSON.parse`.
|
|
193
|
+
*
|
|
194
|
+
* Behavior:
|
|
195
|
+
* - If the input contains no triple-backtick fences, the input is
|
|
196
|
+
* returned unchanged (zero risk of disturbing well-formed output).
|
|
197
|
+
* - Otherwise, the first fenced block is extracted and returned trimmed.
|
|
198
|
+
* Language tags (` ```json `, ` ```JSON `, etc.) are handled.
|
|
199
|
+
* - Callers should still treat the return value as untrusted — it may
|
|
200
|
+
* not be valid JSON (e.g. the model emitted prose, not JSON).
|
|
201
|
+
*
|
|
202
|
+
* This helper is idempotent: `stripJsonFences(stripJsonFences(x)) === stripJsonFences(x)`.
|
|
203
|
+
*
|
|
204
|
+
* @param rawOutput The raw LLM response text
|
|
205
|
+
* @returns Fence-stripped content if fences were present, otherwise `rawOutput` unchanged
|
|
206
|
+
*/
|
|
207
|
+
export function stripJsonFences(rawOutput) {
|
|
208
|
+
if (!rawOutput.includes('```'))
|
|
209
|
+
return rawOutput;
|
|
210
|
+
// Match the first fenced block, optionally with a language tag.
|
|
211
|
+
// The non-greedy `[\s\S]*?` keeps us from swallowing content after
|
|
212
|
+
// the first closing fence. Whitespace/newlines around the payload
|
|
213
|
+
// are tolerated so we accept both inline and multi-line fences.
|
|
214
|
+
const fenceMatch = rawOutput.match(/```(?:[a-zA-Z0-9_-]*)[ \t]*\n?([\s\S]*?)\n?[ \t]*```/);
|
|
215
|
+
if (fenceMatch && fenceMatch[1] !== undefined) {
|
|
216
|
+
return fenceMatch[1].trim();
|
|
217
|
+
}
|
|
218
|
+
return rawOutput;
|
|
219
|
+
}
|
|
269
220
|
/**
|
|
270
221
|
* Parse and validate structured output from raw LLM response text.
|
|
271
222
|
*
|
|
272
223
|
* This function:
|
|
273
|
-
* 1. Parses JSON from the raw output string
|
|
274
|
-
*
|
|
224
|
+
* 1. Parses JSON from the raw output string (with fallback fence stripping
|
|
225
|
+
* for models that wrap structured output in ` ```json ... ``` `)
|
|
226
|
+
* 2. Validates using the SchemaConfig's validate function (if provided)
|
|
275
227
|
* 3. Throws StructuredOutputError on failure
|
|
276
228
|
*
|
|
277
|
-
* @param
|
|
229
|
+
* @param config The schema configuration with optional validator
|
|
278
230
|
* @param rawOutput The raw string output from the LLM
|
|
279
231
|
* @returns The validated and typed data
|
|
280
|
-
* @throws StructuredOutputError if JSON parsing fails or
|
|
281
|
-
*
|
|
282
|
-
* @example
|
|
283
|
-
* ```typescript
|
|
284
|
-
* const schema = z.object({ name: z.string(), age: z.number() });
|
|
285
|
-
* const rawOutput = '{"name": "Alice", "age": 30}';
|
|
286
|
-
* const result = parseStructured(schema, rawOutput); // { name: "Alice", age: 30 }
|
|
287
|
-
* ```
|
|
232
|
+
* @throws StructuredOutputError if JSON parsing fails or validation fails
|
|
288
233
|
*/
|
|
289
|
-
export function parseStructured(
|
|
290
|
-
// Step 1: Parse JSON
|
|
234
|
+
export function parseStructured(config, rawOutput) {
|
|
235
|
+
// Step 1: Parse JSON. Try the raw output first — well-behaved providers
|
|
236
|
+
// return clean JSON and we don't want to pay any cost in the fast path.
|
|
237
|
+
// If that fails, fall back to stripping markdown code fences before
|
|
238
|
+
// re-trying; some models (e.g. Gemma via Ollama) wrap structured output
|
|
239
|
+
// in ` ```json ... ``` ` even when a JSON schema is requested.
|
|
291
240
|
let parsed;
|
|
241
|
+
let parseError;
|
|
292
242
|
try {
|
|
293
243
|
parsed = JSON.parse(rawOutput);
|
|
294
244
|
}
|
|
295
245
|
catch (error) {
|
|
296
|
-
|
|
297
|
-
const syntaxError = error instanceof SyntaxError
|
|
246
|
+
parseError = error instanceof SyntaxError
|
|
298
247
|
? error
|
|
299
248
|
: new SyntaxError(String(error));
|
|
300
|
-
|
|
249
|
+
const fenceStripped = stripJsonFences(rawOutput);
|
|
250
|
+
if (fenceStripped !== rawOutput) {
|
|
251
|
+
try {
|
|
252
|
+
parsed = JSON.parse(fenceStripped);
|
|
253
|
+
parseError = undefined;
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
// Fence stripping didn't help — fall through to throw the
|
|
257
|
+
// original parse error so error messages reflect what the
|
|
258
|
+
// model actually emitted, not the post-processed variant.
|
|
259
|
+
}
|
|
260
|
+
}
|
|
301
261
|
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
if (!result.success) {
|
|
305
|
-
// Schema validation failed - throw with ZodError as cause
|
|
306
|
-
throw new StructuredOutputError(`Validation failed: ${result.error.issues.map((e) => e.message).join(', ')}`, { rawOutput, cause: result.error });
|
|
262
|
+
if (parseError) {
|
|
263
|
+
throw new StructuredOutputError(`Failed to parse JSON: ${parseError.message}`, { rawOutput, cause: parseError });
|
|
307
264
|
}
|
|
308
|
-
|
|
265
|
+
// Step 2: Validate if validator is provided
|
|
266
|
+
if (config.validate) {
|
|
267
|
+
try {
|
|
268
|
+
return config.validate(parsed);
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
271
|
+
const validationError = error instanceof Error ? error : new Error(String(error));
|
|
272
|
+
throw new StructuredOutputError(`Validation failed: ${validationError.message}`, { rawOutput, cause: validationError });
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
// No validator — return as-is (unsafe cast, user chose to skip validation)
|
|
276
|
+
return parsed;
|
|
309
277
|
}
|
|
310
278
|
/**
|
|
311
279
|
* Try to parse and validate structured output, returning a result object.
|
|
@@ -314,31 +282,13 @@ export function parseStructured(schema, rawOutput) {
|
|
|
314
282
|
* on validation failure, it returns a result object with `ok: false` and
|
|
315
283
|
* the error details.
|
|
316
284
|
*
|
|
317
|
-
* @param
|
|
285
|
+
* @param config The schema configuration with optional validator
|
|
318
286
|
* @param rawOutput The raw string output from the LLM
|
|
319
287
|
* @returns A result object: `{ ok: true, value }` on success, `{ ok: false, error, rawOutput }` on failure
|
|
320
|
-
*
|
|
321
|
-
* @example
|
|
322
|
-
* ```typescript
|
|
323
|
-
* const schema = z.object({ name: z.string(), age: z.number() });
|
|
324
|
-
*
|
|
325
|
-
* // Success case
|
|
326
|
-
* const result1 = tryParseStructured(schema, '{"name": "Alice", "age": 30}');
|
|
327
|
-
* if (result1.ok) {
|
|
328
|
-
* console.log(result1.value.name); // "Alice"
|
|
329
|
-
* }
|
|
330
|
-
*
|
|
331
|
-
* // Failure case
|
|
332
|
-
* const result2 = tryParseStructured(schema, 'invalid json');
|
|
333
|
-
* if (!result2.ok) {
|
|
334
|
-
* console.log(result2.error.message); // Error message
|
|
335
|
-
* console.log(result2.rawOutput); // Original output
|
|
336
|
-
* }
|
|
337
|
-
* ```
|
|
338
288
|
*/
|
|
339
|
-
export function tryParseStructured(
|
|
289
|
+
export function tryParseStructured(config, rawOutput) {
|
|
340
290
|
try {
|
|
341
|
-
const value = parseStructured(
|
|
291
|
+
const value = parseStructured(config, rawOutput);
|
|
342
292
|
return { ok: true, value };
|
|
343
293
|
}
|
|
344
294
|
catch (error) {
|
|
@@ -354,31 +304,28 @@ export function tryParseStructured(schema, rawOutput) {
|
|
|
354
304
|
}
|
|
355
305
|
}
|
|
356
306
|
/**
|
|
357
|
-
* Validate already-parsed data
|
|
307
|
+
* Validate already-parsed data using a SchemaConfig's validator.
|
|
358
308
|
*
|
|
359
|
-
* This is useful when you have already parsed JSON and need to validate
|
|
360
|
-
* it against a schema, with optional raw output for error messages.
|
|
309
|
+
* This is useful when you have already parsed JSON and need to validate it.
|
|
361
310
|
*
|
|
362
|
-
* @param
|
|
311
|
+
* @param config The schema configuration with optional validator
|
|
363
312
|
* @param data The parsed data to validate
|
|
364
313
|
* @param rawOutput Optional raw output string for error messages
|
|
365
314
|
* @returns The validated and typed data
|
|
366
|
-
* @throws StructuredOutputError if
|
|
367
|
-
*
|
|
368
|
-
* @example
|
|
369
|
-
* ```typescript
|
|
370
|
-
* const schema = z.object({ name: z.string(), age: z.number() });
|
|
371
|
-
* const data = JSON.parse('{"name": "Alice", "age": 30}');
|
|
372
|
-
* const result = validateStructuredOutput(schema, data); // { name: "Alice", age: 30 }
|
|
373
|
-
* ```
|
|
315
|
+
* @throws StructuredOutputError if validation fails
|
|
374
316
|
*/
|
|
375
|
-
export function validateStructuredOutput(
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
317
|
+
export function validateStructuredOutput(config, data, rawOutput) {
|
|
318
|
+
if (config.validate) {
|
|
319
|
+
try {
|
|
320
|
+
return config.validate(data);
|
|
321
|
+
}
|
|
322
|
+
catch (error) {
|
|
323
|
+
const rawData = rawOutput ?? JSON.stringify(data);
|
|
324
|
+
const validationError = error instanceof Error ? error : new Error(String(error));
|
|
325
|
+
throw new StructuredOutputError(`Validation failed: ${validationError.message}`, { rawOutput: rawData, cause: validationError });
|
|
326
|
+
}
|
|
380
327
|
}
|
|
381
|
-
return
|
|
328
|
+
return data;
|
|
382
329
|
}
|
|
383
330
|
// ============================================================================
|
|
384
331
|
// Streaming JSON Parsing
|
|
@@ -392,32 +339,30 @@ export function validateStructuredOutput(schema, data, rawOutput) {
|
|
|
392
339
|
*/
|
|
393
340
|
export class StreamingJsonParser {
|
|
394
341
|
buffer = '';
|
|
395
|
-
|
|
396
|
-
constructor(
|
|
397
|
-
this.
|
|
342
|
+
validateFn;
|
|
343
|
+
constructor(config) {
|
|
344
|
+
this.validateFn = config.validate;
|
|
398
345
|
}
|
|
399
346
|
/**
|
|
400
347
|
* Feed a chunk of JSON text to the parser.
|
|
401
348
|
* Returns a validated partial object if the current buffer can be parsed
|
|
402
|
-
* as valid JSON that passes
|
|
403
|
-
*
|
|
404
|
-
* For partial objects, this attempts to:
|
|
405
|
-
* 1. Add closing braces/brackets to make valid JSON
|
|
406
|
-
* 2. Fill in missing required fields with null/empty values
|
|
407
|
-
* 3. Validate against schema
|
|
408
|
-
*
|
|
409
|
-
* @param chunk Text chunk from the stream
|
|
410
|
-
* @returns Validated partial object or undefined
|
|
349
|
+
* as valid JSON that passes validation, or undefined if not yet valid.
|
|
411
350
|
*/
|
|
412
351
|
feed(chunk) {
|
|
413
352
|
this.buffer += chunk;
|
|
414
353
|
// Try to parse as complete JSON first
|
|
415
354
|
try {
|
|
416
355
|
const parsed = JSON.parse(this.buffer);
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
356
|
+
if (this.validateFn) {
|
|
357
|
+
try {
|
|
358
|
+
const validated = this.validateFn(parsed);
|
|
359
|
+
return { partial: validated, complete: true };
|
|
360
|
+
}
|
|
361
|
+
catch {
|
|
362
|
+
// Validation failed on complete JSON — return parsed but not validated
|
|
363
|
+
}
|
|
420
364
|
}
|
|
365
|
+
return { partial: parsed, complete: true };
|
|
421
366
|
}
|
|
422
367
|
catch {
|
|
423
368
|
// Not yet valid complete JSON
|
|
@@ -486,10 +431,15 @@ export class StreamingJsonParser {
|
|
|
486
431
|
const candidate = this.buffer + closing;
|
|
487
432
|
try {
|
|
488
433
|
const parsed = JSON.parse(candidate);
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
434
|
+
if (this.validateFn) {
|
|
435
|
+
try {
|
|
436
|
+
return this.validateFn(parsed);
|
|
437
|
+
}
|
|
438
|
+
catch {
|
|
439
|
+
// Partial validation failed — that's expected for partials
|
|
440
|
+
}
|
|
492
441
|
}
|
|
442
|
+
return parsed;
|
|
493
443
|
}
|
|
494
444
|
catch {
|
|
495
445
|
// Silently fail for partial JSON
|
|
@@ -497,9 +447,4 @@ export class StreamingJsonParser {
|
|
|
497
447
|
return undefined;
|
|
498
448
|
}
|
|
499
449
|
}
|
|
500
|
-
// ============================================================================
|
|
501
|
-
// Re-exports for Convenience
|
|
502
|
-
// ============================================================================
|
|
503
|
-
// Re-export Zod for users who need it
|
|
504
|
-
export { z } from 'zod';
|
|
505
450
|
//# sourceMappingURL=structured-output.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"structured-output.js","sourceRoot":"","sources":["../src/structured-output.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"structured-output.js","sourceRoot":"","sources":["../src/structured-output.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAiIH;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,OAAO,qBAAsB,SAAQ,K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CAAC;iBACjC,IAAI,IAAI,KAAK,GAAG;gBAAE,YAAY,EAAE,CAAC;QAC1C,CAAC;QAED,yBAAyB;QACzB,IAAI,OAAO,GAAG,EAAE,CAAC;QACjB,OAAO,YAAY,GAAG,CAAC,EAAE,CAAC;YACtB,OAAO,IAAI,GAAG,CAAC;YACf,YAAY,EAAE,CAAC;QACnB,CAAC;QACD,OAAO,UAAU,GAAG,CAAC,EAAE,CAAC;YACpB,OAAO,IAAI,GAAG,CAAC;YACf,UAAU,EAAE,CAAC;QACjB,CAAC;QAED,2BAA2B;QAC3B,MAAM,SAAS,GAAG,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC;QACxC,IAAI,CAAC;YACD,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;YACrC,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;gBAClB,IAAI,CAAC;oBACD,OAAO,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;gBACnC,CAAC;gBAAC,MAAM,CAAC;oBACL,2DAA2D;gBAC/D,CAAC;YACL,CAAC;YACD,OAAO,MAAW,CAAC;QACvB,CAAC;QAAC,MAAM,CAAC;YACL,iCAAiC;QACrC,CAAC;QAED,OAAO,SAAS,CAAC;IACrB,CAAC;CACJ"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod Adapter for Universal LLM Client
|
|
3
|
+
*
|
|
4
|
+
* Optional entrypoint for projects that use Zod for schema validation.
|
|
5
|
+
* Import from 'universal-llm-client/zod' to use.
|
|
6
|
+
*
|
|
7
|
+
* @module universal-llm-client/zod
|
|
8
|
+
*/
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
import type { SchemaConfig } from './structured-output.js';
|
|
11
|
+
/**
|
|
12
|
+
* Create a SchemaConfig from a Zod schema.
|
|
13
|
+
*
|
|
14
|
+
* This bridges Zod's type-safe schema definitions to the library's
|
|
15
|
+
* generic SchemaConfig interface, using Zod 4's native `z.toJSONSchema()`.
|
|
16
|
+
*
|
|
17
|
+
* @template T The type inferred from the Zod schema
|
|
18
|
+
* @param schema The Zod schema
|
|
19
|
+
* @param options Optional name and description for LLM guidance
|
|
20
|
+
* @returns SchemaConfig ready for use with generateStructured, etc.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* import { fromZod } from 'universal-llm-client/zod';
|
|
25
|
+
* import { z } from 'zod';
|
|
26
|
+
*
|
|
27
|
+
* const UserSchema = z.object({
|
|
28
|
+
* name: z.string(),
|
|
29
|
+
* age: z.number(),
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* const config = fromZod(UserSchema, { name: 'User' });
|
|
33
|
+
*
|
|
34
|
+
* const user = await model.generateStructured(config, messages);
|
|
35
|
+
* // user.name: string, user.age: number (fully typed)
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare function fromZod<T>(schema: z.ZodType<T>, options?: {
|
|
39
|
+
name?: string;
|
|
40
|
+
description?: string;
|
|
41
|
+
}): SchemaConfig<T>;
|
|
42
|
+
export { z } from 'zod';
|
|
43
|
+
export type { SchemaConfig } from './structured-output.js';
|
|
44
|
+
//# sourceMappingURL=zod-adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-adapter.d.ts","sourceRoot":"","sources":["../src/zod-adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAE3D;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,OAAO,CAAC,CAAC,EACrB,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,CAAC,EAAE;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,GAClD,YAAY,CAAC,CAAC,CAAC,CAuBjB;AAGD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,YAAY,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod Adapter for Universal LLM Client
|
|
3
|
+
*
|
|
4
|
+
* Optional entrypoint for projects that use Zod for schema validation.
|
|
5
|
+
* Import from 'universal-llm-client/zod' to use.
|
|
6
|
+
*
|
|
7
|
+
* @module universal-llm-client/zod
|
|
8
|
+
*/
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
/**
|
|
11
|
+
* Create a SchemaConfig from a Zod schema.
|
|
12
|
+
*
|
|
13
|
+
* This bridges Zod's type-safe schema definitions to the library's
|
|
14
|
+
* generic SchemaConfig interface, using Zod 4's native `z.toJSONSchema()`.
|
|
15
|
+
*
|
|
16
|
+
* @template T The type inferred from the Zod schema
|
|
17
|
+
* @param schema The Zod schema
|
|
18
|
+
* @param options Optional name and description for LLM guidance
|
|
19
|
+
* @returns SchemaConfig ready for use with generateStructured, etc.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { fromZod } from 'universal-llm-client/zod';
|
|
24
|
+
* import { z } from 'zod';
|
|
25
|
+
*
|
|
26
|
+
* const UserSchema = z.object({
|
|
27
|
+
* name: z.string(),
|
|
28
|
+
* age: z.number(),
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* const config = fromZod(UserSchema, { name: 'User' });
|
|
32
|
+
*
|
|
33
|
+
* const user = await model.generateStructured(config, messages);
|
|
34
|
+
* // user.name: string, user.age: number (fully typed)
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export function fromZod(schema, options) {
|
|
38
|
+
// Convert Zod schema to JSON Schema using Zod 4's native method
|
|
39
|
+
const rawJsonSchema = z.toJSONSchema(schema, {
|
|
40
|
+
target: 'draft-07',
|
|
41
|
+
unrepresentable: 'any',
|
|
42
|
+
});
|
|
43
|
+
// Clean up — remove $schema since providers don't need it
|
|
44
|
+
const jsonSchema = { ...rawJsonSchema };
|
|
45
|
+
delete jsonSchema.$schema;
|
|
46
|
+
return {
|
|
47
|
+
jsonSchema: jsonSchema,
|
|
48
|
+
validate: (data) => {
|
|
49
|
+
const result = schema.safeParse(data);
|
|
50
|
+
if (!result.success) {
|
|
51
|
+
throw result.error;
|
|
52
|
+
}
|
|
53
|
+
return result.data;
|
|
54
|
+
},
|
|
55
|
+
name: options?.name,
|
|
56
|
+
description: options?.description,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
// Re-export z for convenience (users importing from /zod likely want it)
|
|
60
|
+
export { z } from 'zod';
|
|
61
|
+
//# sourceMappingURL=zod-adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-adapter.js","sourceRoot":"","sources":["../src/zod-adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,UAAU,OAAO,CACnB,MAAoB,EACpB,OAAiD;IAEjD,gEAAgE;IAChE,MAAM,aAAa,GAAG,CAAC,CAAC,YAAY,CAAC,MAAM,EAAE;QACzC,MAAM,EAAE,UAAU;QAClB,eAAe,EAAE,KAAK;KACzB,CAAC,CAAC;IAEH,0DAA0D;IAC1D,MAAM,UAAU,GAAG,EAAE,GAAG,aAAa,EAA6B,CAAC;IACnE,OAAO,UAAU,CAAC,OAAO,CAAC;IAE1B,OAAO;QACH,UAAU,EAAE,UAAyD;QACrE,QAAQ,EAAE,CAAC,IAAa,EAAK,EAAE;YAC3B,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;YACtC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;gBAClB,MAAM,MAAM,CAAC,KAAK,CAAC;YACvB,CAAC;YACD,OAAO,MAAM,CAAC,IAAI,CAAC;QACvB,CAAC;QACD,IAAI,EAAE,OAAO,EAAE,IAAI;QACnB,WAAW,EAAE,OAAO,EAAE,WAAW;KACpC,CAAC;AACN,CAAC;AAED,yEAAyE;AACzE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC"}
|