@mate-academy/llm-gateway 3.0.0 → 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +163 -11
- package/dist/LLMService.constants.d.ts +553 -5
- package/dist/LLMService.constants.js.map +1 -1
- package/dist/LLMService.factory.js +1 -1
- package/dist/LLMService.factory.js.map +1 -1
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAI.constants.d.ts +176 -3
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAI.constants.js.map +1 -1
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAIService.factory.js.map +1 -1
- package/dist/providers/GoogleGenerativeAI/schemas/GoogleSchemaAdapter.d.ts +12 -0
- package/dist/providers/GoogleGenerativeAI/schemas/GoogleSchemaAdapter.js +72 -0
- package/dist/providers/GoogleGenerativeAI/schemas/GoogleSchemaAdapter.js.map +1 -0
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAIAssistance.service.js +1 -1
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAIAssistance.service.js.map +1 -1
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAICompletion.service.js +1 -1
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAICompletion.service.js.map +1 -1
- package/dist/providers/OpenAI/OpenAI.constants.d.ts +384 -3
- package/dist/providers/OpenAI/OpenAI.constants.js.map +1 -1
- package/dist/providers/OpenAI/OpenAIService.factory.js.map +1 -1
- package/dist/providers/OpenAI/schemas/OpenAISchemaAdapter.d.ts +14 -0
- package/dist/providers/OpenAI/schemas/OpenAISchemaAdapter.js +87 -0
- package/dist/providers/OpenAI/schemas/OpenAISchemaAdapter.js.map +1 -0
- package/dist/providers/OpenAI/services/OpenAIAssistance.service.js +1 -1
- package/dist/providers/OpenAI/services/OpenAIAssistance.service.js.map +1 -1
- package/dist/providers/OpenAI/services/OpenAICompletion.service.js +1 -1
- package/dist/providers/OpenAI/services/OpenAICompletion.service.js.map +1 -1
- package/dist/services/LLMAssistanceService.abstract.js.map +1 -1
- package/dist/services/LLMBaseService.abstract.d.ts +2 -2
- package/dist/services/LLMBaseService.abstract.js.map +1 -1
- package/dist/utilities/promptBuilder.d.ts +7 -2
- package/dist/utilities/promptBuilder.js +18 -2
- package/dist/utilities/promptBuilder.js.map +1 -1
- package/dist/utilities/schema/LLMSchema.d.ts +4 -20
- package/dist/utilities/schema/LLMSchema.js +8 -147
- package/dist/utilities/schema/LLMSchema.js.map +1 -1
- package/dist/utilities/schema/LLMSchemaInterface.d.ts +3 -7
- package/dist/utilities/schema/adapters/SchemaAdapterInterface.d.ts +12 -0
- package/dist/utilities/schema/adapters/SchemaAdapterInterface.js +3 -0
- package/dist/utilities/schema/adapters/SchemaAdapterInterface.js.map +1 -0
- package/dist/utilities/schema/adapters/SchemaAdapterRegistry.d.ts +24 -0
- package/dist/utilities/schema/adapters/SchemaAdapterRegistry.js +30 -0
- package/dist/utilities/schema/adapters/SchemaAdapterRegistry.js.map +1 -0
- package/package.json +1 -1
|
@@ -17,6 +17,10 @@
|
|
|
17
17
|
* This is a conditional section that will only appear if the value is truthy.
|
|
18
18
|
* Value can be used inside the section: {{valueAsCondition}}
|
|
19
19
|
* {{/valueAsCondition}}
|
|
20
|
+
*
|
|
21
|
+
* {{#!skipSection}}
|
|
22
|
+
* This negative conditional section will only appear if skipSection is falsy.
|
|
23
|
+
* {{/skipSection}}
|
|
20
24
|
* `),
|
|
21
25
|
* };
|
|
22
26
|
*
|
|
@@ -33,8 +37,8 @@
|
|
|
33
37
|
type RegularValue = string | number;
|
|
34
38
|
type ConditionalValue = RegularValue | boolean | undefined | null;
|
|
35
39
|
type ExtractAllVariables<T extends string> = T extends `${string}{{${infer Key}}}${infer Rest}` ? Key extends `${infer CleanKey}` ? CleanKey | ExtractAllVariables<Rest> : never : never;
|
|
36
|
-
type ExtractSectionVariables<T extends string> = T extends `${string}{{#${infer Key}}}${infer Rest}` ? Key | ExtractSectionVariables<Rest> : T extends `${string}{{/${infer Key}}}${infer Rest}` ? Key | ExtractSectionVariables<Rest> : never;
|
|
37
|
-
type ExtractRegularVariables<T extends string> = ExtractAllVariables<T> extends infer All ? All extends string ? All extends `#${string}` | `/${string}` ? never : All : never : never;
|
|
40
|
+
type ExtractSectionVariables<T extends string> = T extends `${string}{{#!${infer Key}}}${infer Rest}` ? Key | ExtractSectionVariables<Rest> : T extends `${string}{{#${infer Key}}}${infer Rest}` ? Key | ExtractSectionVariables<Rest> : T extends `${string}{{/${infer Key}}}${infer Rest}` ? Key | ExtractSectionVariables<Rest> : never;
|
|
41
|
+
type ExtractRegularVariables<T extends string> = ExtractAllVariables<T> extends infer All ? All extends string ? All extends `#!${string}` | `#${string}` | `/${string}` ? never : All : never : never;
|
|
38
42
|
type CreateVariableRecord<T extends string> = ExtractRegularVariables<T> extends never ? ExtractSectionVariables<T> extends never ? Record<string, never> : Record<ExtractSectionVariables<T>, RegularValue> : ExtractSectionVariables<T> extends never ? Record<ExtractRegularVariables<T>, RegularValue> : Omit<Record<ExtractRegularVariables<T>, RegularValue>, ExtractSectionVariables<T>> & Record<ExtractSectionVariables<T>, ConditionalValue>;
|
|
39
43
|
type HasVariables<T extends string> = ExtractAllVariables<T> extends never ? false : true;
|
|
40
44
|
/**
|
|
@@ -44,6 +48,7 @@ type HasVariables<T extends string> = ExtractAllVariables<T> extends never ? fal
|
|
|
44
48
|
* @param {T} template The template string with placeholders:
|
|
45
49
|
* - for dynamic values (e.g., `{{topicTitle}}`)
|
|
46
50
|
* - for conditional sections (e.g., `{{#section}}...{{/section}}`)
|
|
51
|
+
* - for negative conditional sections (e.g., `{{#!section}}...{{/section}}`)
|
|
47
52
|
* - for sections that use conditional variables as values (e.g., `{{#bonus}}...{{bonus}}...{{/bonus}}`)
|
|
48
53
|
* @returns A function that takes the dynamic values and returns the generated prompt.
|
|
49
54
|
*/
|
|
@@ -18,6 +18,10 @@
|
|
|
18
18
|
* This is a conditional section that will only appear if the value is truthy.
|
|
19
19
|
* Value can be used inside the section: {{valueAsCondition}}
|
|
20
20
|
* {{/valueAsCondition}}
|
|
21
|
+
*
|
|
22
|
+
* {{#!skipSection}}
|
|
23
|
+
* This negative conditional section will only appear if skipSection is falsy.
|
|
24
|
+
* {{/skipSection}}
|
|
21
25
|
* `),
|
|
22
26
|
* };
|
|
23
27
|
*
|
|
@@ -41,8 +45,18 @@ function promptBuilder(prompt, variables) {
|
|
|
41
45
|
// Replace all occurrences of the variable in the source string
|
|
42
46
|
Object.entries(variables).reduce((acc, [key, value]) => (acc.replace(new RegExp(`{{\\s*${key}\\s*}}`, 'g'), String(value))), source));
|
|
43
47
|
const processConditionalSections = (source) => {
|
|
44
|
-
// Process conditional sections
|
|
45
|
-
|
|
48
|
+
// Process negative conditional sections first ({{#!condition}})
|
|
49
|
+
let result = source.replace(/{{#!(\w+)}}([\s\S]*?){{\/\1}}/g, (_, sectionName, content) => {
|
|
50
|
+
const value = variables[sectionName];
|
|
51
|
+
if (!value) {
|
|
52
|
+
// Recursively process nested conditional sections and then replace variables
|
|
53
|
+
const processedContent = processConditionalSections(content);
|
|
54
|
+
return replaceVariables(processedContent);
|
|
55
|
+
}
|
|
56
|
+
return '';
|
|
57
|
+
});
|
|
58
|
+
// Then process positive conditional sections ({{#condition}})
|
|
59
|
+
result = result.replace(/{{#(\w+)}}([\s\S]*?){{\/\1}}/g, (_, sectionName, content) => {
|
|
46
60
|
const value = variables[sectionName];
|
|
47
61
|
if (value) {
|
|
48
62
|
// Recursively process nested conditional sections and then replace variables
|
|
@@ -51,6 +65,7 @@ function promptBuilder(prompt, variables) {
|
|
|
51
65
|
}
|
|
52
66
|
return '';
|
|
53
67
|
});
|
|
68
|
+
return result;
|
|
54
69
|
};
|
|
55
70
|
let result = prompt;
|
|
56
71
|
// Process conditional sections first (with recursive handling for nested sections)
|
|
@@ -66,6 +81,7 @@ function promptBuilder(prompt, variables) {
|
|
|
66
81
|
* @param {T} template The template string with placeholders:
|
|
67
82
|
* - for dynamic values (e.g., `{{topicTitle}}`)
|
|
68
83
|
* - for conditional sections (e.g., `{{#section}}...{{/section}}`)
|
|
84
|
+
* - for negative conditional sections (e.g., `{{#!section}}...{{/section}}`)
|
|
69
85
|
* - for sections that use conditional variables as values (e.g., `{{#bonus}}...{{bonus}}...{{/bonus}}`)
|
|
70
86
|
* @returns A function that takes the dynamic values and returns the generated prompt.
|
|
71
87
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promptBuilder.js","sourceRoot":"","sources":["../../src/utilities/promptBuilder.ts"],"names":[],"mappings":";AAAA
|
|
1
|
+
{"version":3,"file":"promptBuilder.js","sourceRoot":"","sources":["../../src/utilities/promptBuilder.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;;AAwIH,oDAQC;AA3ED,SAAS,aAAa,CACpB,MAAc,EACd,SAA2D;IAE3D,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,MAAM,gBAAgB,GAAG,CAAC,MAAc,EAAU,EAAE,CAAC;IACnD,+DAA+D;IAC/D,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CACtD,GAAG,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC,SAAS,GAAG,QAAQ,EAAE,GAAG,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAClE,EAAE,MAAM,CAAC,CACX,CAAC;IAEF,MAAM,0BAA0B,GAAG,CAAC,MAAc,EAAU,EAAE;QAC5D,gEAAgE;QAChE,IAAI,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,gCAAgC,EAAE,CAAC,CAAC,EAAE,WAAW,EAAE,OAAO,EAAE,EAAE;YACxF,MAAM,KAAK,GAAG,SAAS,CAAC,WAAW,CAAC,CAAC;YAErC,IAAI,CAAC,KAAK,EAAE,CAAC;gBACX,6EAA6E;gBAC7E,MAAM,gBAAgB,GAAG,0BAA0B,CAAC,OAAO,CAAC,CAAC;gBAC7D,OAAO,gBAAgB,CAAC,gBAAgB,CAAC,CAAC;YAC5C,CAAC;YAED,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC,CAAC;QAEH,8DAA8D;QAC9D,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,+BAA+B,EAAE,CAAC,CAAC,EAAE,WAAW,EAAE,OAAO,EAAE,EAAE;YACnF,MAAM,KAAK,GAAG,SAAS,CAAC,WAAW,CAAC,CAAC;YAErC,IAAI,KAAK,EAAE,CAAC;gBACV,6EAA6E;gBAC7E,MAAM,gBAAgB,GAAG,0BAA0B,CAAC,OAAO,CAAC,CAAC;gBAC7D,OAAO,gBAAgB,CAAC,gBAAgB,CAAC,CAAC;YAC5C,CAAC;YAED,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC,CAAC;QAEH,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IAEF,IAAI,MAAM,GAAG,MAAM,CAAC;IAEpB,mFAAmF;IACnF,MAAM,GAAG,0BAA0B,CAAC,MAAM,CAAC,CAAC;IAE5C,6CAA6C;IAC7C,MAAM,GAAG,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAElC,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,oBAAoB,CAAmB,QAAW;IAChE,MAAM,eAAe,GAAG,QAAQ,CAAC,IAAI,EAAO,CAAC;IAE7C,OAAO,CACL,GAAG,IAEqC,EAChC,EAAE,CAAC,aAAa,CAAC,eAAe,EAAE,GAAG,IAAI,CAAC,CAAC;AACvD,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type { LLMProviders } from '../../LLMService.typedefs';
|
|
2
|
+
import type { LLMSchemaInterface, ParseResult, InferSchema } from './LLMSchemaInterface';
|
|
2
3
|
/**
|
|
3
4
|
* Concrete implementation of schema builder using Zod as the underlying validator.
|
|
4
5
|
* This implementation can be replaced with another validator without changing the public API.
|
|
@@ -103,24 +104,7 @@ export declare class LLMSchema<T = any> implements LLMSchemaInterface<T> {
|
|
|
103
104
|
_parse(data: unknown): ParseResult<T>;
|
|
104
105
|
/**
|
|
105
106
|
* @internal
|
|
106
|
-
* Converts schema to
|
|
107
|
+
* Converts schema to provider-specific format using the adapter registry
|
|
107
108
|
*/
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* @internal
|
|
111
|
-
* Converts schema to Google Generative AI format
|
|
112
|
-
*/
|
|
113
|
-
_toGoogleSchema(): any;
|
|
114
|
-
/**
|
|
115
|
-
* @internal
|
|
116
|
-
* Converts JSON Schema to Google's Type-based format
|
|
117
|
-
*/
|
|
118
|
-
private _convertJsonSchemaToGoogleFormat;
|
|
119
|
-
/**
|
|
120
|
-
* @internal
|
|
121
|
-
* Normalizes JSON Schema for OpenAI structured_response:
|
|
122
|
-
* - For each object, sets required to all property keys.
|
|
123
|
-
* - Any property that was optional becomes nullable.
|
|
124
|
-
*/
|
|
125
|
-
private _normalizeForOpenAIStructuredResponse;
|
|
109
|
+
_toProviderSchema(provider: LLMProviders): any;
|
|
126
110
|
}
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.LLMSchema = void 0;
|
|
4
4
|
const zod_1 = require("zod");
|
|
5
5
|
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
6
|
-
const
|
|
6
|
+
const SchemaAdapterRegistry_1 = require("./adapters/SchemaAdapterRegistry");
|
|
7
7
|
/**
|
|
8
8
|
* Concrete implementation of schema builder using Zod as the underlying validator.
|
|
9
9
|
* This implementation can be replaced with another validator without changing the public API.
|
|
@@ -204,157 +204,18 @@ class LLMSchema {
|
|
|
204
204
|
}
|
|
205
205
|
/**
|
|
206
206
|
* @internal
|
|
207
|
-
* Converts schema to
|
|
207
|
+
* Converts schema to provider-specific format using the adapter registry
|
|
208
208
|
*/
|
|
209
|
-
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
$
|
|
213
|
-
}
|
|
214
|
-
return this._normalizeForOpenAIStructuredResponse(raw);
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* @internal
|
|
218
|
-
* Converts schema to Google Generative AI format
|
|
219
|
-
*/
|
|
220
|
-
_toGoogleSchema() {
|
|
209
|
+
_toProviderSchema(provider) {
|
|
210
|
+
const adapter = (0, SchemaAdapterRegistry_1.getSchemaAdapter)(provider);
|
|
211
|
+
if (!adapter) {
|
|
212
|
+
throw new Error(`Provider ${provider} does not support structured output`);
|
|
213
|
+
}
|
|
221
214
|
const jsonSchema = (0, zod_to_json_schema_1.zodToJsonSchema)(this.zodSchema, {
|
|
222
215
|
target: 'jsonSchema7',
|
|
223
216
|
$refStrategy: 'none',
|
|
224
217
|
});
|
|
225
|
-
return
|
|
226
|
-
}
|
|
227
|
-
/**
|
|
228
|
-
* @internal
|
|
229
|
-
* Converts JSON Schema to Google's Type-based format
|
|
230
|
-
*/
|
|
231
|
-
_convertJsonSchemaToGoogleFormat(jsonSchema) {
|
|
232
|
-
// Handle null type
|
|
233
|
-
if (jsonSchema.type === 'null') {
|
|
234
|
-
return { type: genai_1.Type.STRING, nullable: true };
|
|
235
|
-
}
|
|
236
|
-
// Handle arrays with anyOf (for unions)
|
|
237
|
-
if (jsonSchema.anyOf) {
|
|
238
|
-
// For now, we'll use the first type in the union
|
|
239
|
-
// This is a simplification - full union support would be more complex
|
|
240
|
-
return this._convertJsonSchemaToGoogleFormat(jsonSchema.anyOf[0]);
|
|
241
|
-
}
|
|
242
|
-
// Handle objects
|
|
243
|
-
if (jsonSchema.type === 'object') {
|
|
244
|
-
const properties = {};
|
|
245
|
-
const required = jsonSchema.required || [];
|
|
246
|
-
for (const [key, value] of Object.entries(jsonSchema.properties || {})) {
|
|
247
|
-
const convertedProperty = this._convertJsonSchemaToGoogleFormat(value);
|
|
248
|
-
properties[key] = convertedProperty;
|
|
249
|
-
}
|
|
250
|
-
return {
|
|
251
|
-
type: genai_1.Type.OBJECT,
|
|
252
|
-
properties,
|
|
253
|
-
required,
|
|
254
|
-
propertyOrdering: Object.keys(properties),
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
// Handle arrays
|
|
258
|
-
if (jsonSchema.type === 'array') {
|
|
259
|
-
return {
|
|
260
|
-
type: genai_1.Type.ARRAY,
|
|
261
|
-
items: this._convertJsonSchemaToGoogleFormat(jsonSchema.items || {}),
|
|
262
|
-
};
|
|
263
|
-
}
|
|
264
|
-
// Handle strings
|
|
265
|
-
if (jsonSchema.type === 'string') {
|
|
266
|
-
if (jsonSchema.enum) {
|
|
267
|
-
return {
|
|
268
|
-
type: genai_1.Type.STRING,
|
|
269
|
-
enum: jsonSchema.enum,
|
|
270
|
-
};
|
|
271
|
-
}
|
|
272
|
-
return { type: genai_1.Type.STRING };
|
|
273
|
-
}
|
|
274
|
-
// Handle numbers
|
|
275
|
-
if (jsonSchema.type === 'number' || jsonSchema.type === 'integer') {
|
|
276
|
-
return { type: genai_1.Type.NUMBER };
|
|
277
|
-
}
|
|
278
|
-
// Handle booleans
|
|
279
|
-
if (jsonSchema.type === 'boolean') {
|
|
280
|
-
return { type: genai_1.Type.BOOLEAN };
|
|
281
|
-
}
|
|
282
|
-
// Default fallback
|
|
283
|
-
return { type: genai_1.Type.STRING };
|
|
284
|
-
}
|
|
285
|
-
/**
|
|
286
|
-
* @internal
|
|
287
|
-
* Normalizes JSON Schema for OpenAI structured_response:
|
|
288
|
-
* - For each object, sets required to all property keys.
|
|
289
|
-
* - Any property that was optional becomes nullable.
|
|
290
|
-
*/
|
|
291
|
-
_normalizeForOpenAIStructuredResponse(schema) {
|
|
292
|
-
const copy = JSON.parse(JSON.stringify(schema));
|
|
293
|
-
const allowsNull = (node) => {
|
|
294
|
-
if (!node || typeof node !== 'object') {
|
|
295
|
-
return false;
|
|
296
|
-
}
|
|
297
|
-
if (node.nullable === true) {
|
|
298
|
-
return true;
|
|
299
|
-
}
|
|
300
|
-
if (node.type === 'null') {
|
|
301
|
-
return true;
|
|
302
|
-
}
|
|
303
|
-
if (Array.isArray(node.type) && node.type.includes('null')) {
|
|
304
|
-
return true;
|
|
305
|
-
}
|
|
306
|
-
const list = node.anyOf || node.oneOf;
|
|
307
|
-
return Array.isArray(list)
|
|
308
|
-
? list.some((node) => node?.type === 'null' || node?.nullable === true)
|
|
309
|
-
: false;
|
|
310
|
-
};
|
|
311
|
-
const makeNullable = (node) => {
|
|
312
|
-
if (!node
|
|
313
|
-
|| typeof node !== 'object'
|
|
314
|
-
|| allowsNull(node)) {
|
|
315
|
-
return;
|
|
316
|
-
}
|
|
317
|
-
if (node.anyOf) {
|
|
318
|
-
node.anyOf = [...node.anyOf, { type: 'null' }];
|
|
319
|
-
}
|
|
320
|
-
else if (node.oneOf) {
|
|
321
|
-
node.oneOf = [...node.oneOf, { type: 'null' }];
|
|
322
|
-
}
|
|
323
|
-
else {
|
|
324
|
-
node.nullable = true; // OpenAPI 3 nullable flag
|
|
325
|
-
}
|
|
326
|
-
};
|
|
327
|
-
const visit = (node) => {
|
|
328
|
-
if (!node || typeof node !== 'object') {
|
|
329
|
-
return;
|
|
330
|
-
}
|
|
331
|
-
// Handle composition
|
|
332
|
-
for (const key of ['anyOf', 'oneOf', 'allOf']) {
|
|
333
|
-
if (Array.isArray(node[key])) {
|
|
334
|
-
node[key].forEach(visit);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
// Recurse arrays
|
|
338
|
-
if (node.type === 'array' && node.items) {
|
|
339
|
-
visit(node.items);
|
|
340
|
-
}
|
|
341
|
-
// Objects: enforce required includes all properties
|
|
342
|
-
if (node.type === 'object' && node.properties) {
|
|
343
|
-
const props = node.properties;
|
|
344
|
-
const originalRequired = new Set(node.required || []);
|
|
345
|
-
const keys = Object.keys(props);
|
|
346
|
-
// Previously optional -> make nullable
|
|
347
|
-
for (const key of keys) {
|
|
348
|
-
if (!originalRequired.has(key)) {
|
|
349
|
-
makeNullable(props[key]);
|
|
350
|
-
}
|
|
351
|
-
visit(props[key]);
|
|
352
|
-
}
|
|
353
|
-
node.required = keys; // all properties required
|
|
354
|
-
}
|
|
355
|
-
};
|
|
356
|
-
visit(copy);
|
|
357
|
-
return copy;
|
|
218
|
+
return adapter.convertSchema(jsonSchema);
|
|
358
219
|
}
|
|
359
220
|
}
|
|
360
221
|
exports.LLMSchema = LLMSchema;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LLMSchema.js","sourceRoot":"","sources":["../../../src/utilities/schema/LLMSchema.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AACxB,2DAAqD;
|
|
1
|
+
{"version":3,"file":"LLMSchema.js","sourceRoot":"","sources":["../../../src/utilities/schema/LLMSchema.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AACxB,2DAAqD;AAGrD,4EAAoE;AAEpE;;;GAGG;AACH,MAAa,SAAS;IAED;IADnB,YACmB,SAAuB;QAAvB,cAAS,GAAT,SAAS,CAAc;IACvC,CAAC;IAEJ,4CAA4C;IAE5C;;OAEG;IACH,MAAM,CAAC,MAAM;QACX,OAAO,IAAI,SAAS,CAAC,OAAC,CAAC,MAAM,EAAE,CAAC,CAAC;IACnC,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,MAAM;QACX,OAAO,IAAI,SAAS,CAAC,OAAC,CAAC,MAAM,EAAE,CAAC,CAAC;IACnC,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,OAAO;QACZ,OAAO,IAAI,SAAS,CAAC,OAAC,CAAC,OAAO,EAAE,CAAC,CAAC;IACpC,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,MAAM,CACX,KAAQ;QAIR,MAAM,QAAQ,GAA8B,EAAE,CAAC;QAC/C,KAAK,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,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AS,CAAC,IAAI,CAAC,CAAC;QAC9C,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;YACnB,OAAO;gBACL,OAAO,EAAE,IAAI;gBACb,IAAI,EAAE,MAAM,CAAC,IAAI;aAClB,CAAC;QACJ,CAAC;QACD,OAAO;YACL,OAAO,EAAE,KAAK;YACd,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,OAAO;SAC5B,CAAC;IACJ,CAAC;IAED;;;OAGG;IACH,iBAAiB,CAAC,QAAsB;QACtC,MAAM,OAAO,GAAG,IAAA,wCAAgB,EAAC,QAAQ,CAAC,CAAC;QAC3C,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC,YAAY,QAAQ,qCAAqC,CAAC,CAAC;QAC7E,CAAC;QAED,MAAM,UAAU,GAAG,IAAA,oCAAe,EAAC,IAAI,CAAC,SAAS,EAAE;YACjD,MAAM,EAAE,aAAa;YACrB,YAAY,EAAE,MAAM;SACrB,CAAC,CAAC;QAEH,OAAO,OAAO,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;IAC3C,CAAC;CAEF;AAxPD,8BAwPC"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { LLMProviders } from '../../LLMService.typedefs';
|
|
1
2
|
/**
|
|
2
3
|
* Result of parsing data against a schema
|
|
3
4
|
*/
|
|
@@ -17,15 +18,10 @@ export interface LLMSchemaInterface<T = any> {
|
|
|
17
18
|
*/
|
|
18
19
|
_parse(data: unknown): ParseResult<T>;
|
|
19
20
|
/**
|
|
20
|
-
* Convert schema to
|
|
21
|
+
* Convert schema to provider-specific format using registered adapters
|
|
21
22
|
* @internal - Used internally by the package
|
|
22
23
|
*/
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Convert schema to Google Generative AI format
|
|
26
|
-
* @internal - Used internally by the package
|
|
27
|
-
*/
|
|
28
|
-
_toGoogleSchema(): any;
|
|
24
|
+
_toProviderSchema(provider: LLMProviders): any;
|
|
29
25
|
optional(): LLMSchemaInterface<T | undefined>;
|
|
30
26
|
nullable(): LLMSchemaInterface<T | null>;
|
|
31
27
|
default(value: any): LLMSchemaInterface<T>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for schema adapters that convert generic JSON Schema
|
|
3
|
+
* to provider-specific formats
|
|
4
|
+
*/
|
|
5
|
+
export interface SchemaAdapterInterface {
|
|
6
|
+
/**
|
|
7
|
+
* Converts a JSON Schema to provider-specific format
|
|
8
|
+
* @param jsonSchema - The JSON Schema to convert
|
|
9
|
+
* @returns The provider-specific schema format
|
|
10
|
+
*/
|
|
11
|
+
convertSchema(jsonSchema: any): any;
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaAdapterInterface.js","sourceRoot":"","sources":["../../../../src/utilities/schema/adapters/SchemaAdapterInterface.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { LLMProviders } from '../../../LLMService.typedefs';
|
|
2
|
+
import type { SchemaAdapterInterface } from './SchemaAdapterInterface';
|
|
3
|
+
import { OpenAISchemaAdapter } from '../../../providers/OpenAI/schemas/OpenAISchemaAdapter';
|
|
4
|
+
import { GoogleSchemaAdapter } from '../../../providers/GoogleGenerativeAI/schemas/GoogleSchemaAdapter';
|
|
5
|
+
/**
|
|
6
|
+
* Type-safe registry of schema adapters for all supported providers.
|
|
7
|
+
*
|
|
8
|
+
* When adding a new provider:
|
|
9
|
+
* 1. Add the provider to LLMProviders enum
|
|
10
|
+
* 2. Create the schema adapter implementing SchemaAdapterInterface
|
|
11
|
+
* 3. Add the adapter to this registry (TypeScript will enforce this)
|
|
12
|
+
* 4. Set to `null` if the provider doesn't support structured output
|
|
13
|
+
*
|
|
14
|
+
* This ensures compile-time safety and forces developers to handle all providers.
|
|
15
|
+
*/
|
|
16
|
+
export declare const SCHEMA_ADAPTER_REGISTRY: {
|
|
17
|
+
readonly OpenAI: OpenAISchemaAdapter;
|
|
18
|
+
readonly GoogleGenerativeAI: GoogleSchemaAdapter;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Type-safe helper to get a schema adapter for a provider.
|
|
22
|
+
* Returns null if the provider doesn't support structured output.
|
|
23
|
+
*/
|
|
24
|
+
export declare function getSchemaAdapter(provider: LLMProviders): SchemaAdapterInterface | null;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SCHEMA_ADAPTER_REGISTRY = void 0;
|
|
4
|
+
exports.getSchemaAdapter = getSchemaAdapter;
|
|
5
|
+
const LLMService_typedefs_1 = require("../../../LLMService.typedefs");
|
|
6
|
+
const OpenAISchemaAdapter_1 = require("../../../providers/OpenAI/schemas/OpenAISchemaAdapter");
|
|
7
|
+
const GoogleSchemaAdapter_1 = require("../../../providers/GoogleGenerativeAI/schemas/GoogleSchemaAdapter");
|
|
8
|
+
/**
|
|
9
|
+
* Type-safe registry of schema adapters for all supported providers.
|
|
10
|
+
*
|
|
11
|
+
* When adding a new provider:
|
|
12
|
+
* 1. Add the provider to LLMProviders enum
|
|
13
|
+
* 2. Create the schema adapter implementing SchemaAdapterInterface
|
|
14
|
+
* 3. Add the adapter to this registry (TypeScript will enforce this)
|
|
15
|
+
* 4. Set to `null` if the provider doesn't support structured output
|
|
16
|
+
*
|
|
17
|
+
* This ensures compile-time safety and forces developers to handle all providers.
|
|
18
|
+
*/
|
|
19
|
+
exports.SCHEMA_ADAPTER_REGISTRY = {
|
|
20
|
+
[LLMService_typedefs_1.LLMProviders.OpenAI]: new OpenAISchemaAdapter_1.OpenAISchemaAdapter(),
|
|
21
|
+
[LLMService_typedefs_1.LLMProviders.GoogleGenerativeAI]: new GoogleSchemaAdapter_1.GoogleSchemaAdapter(),
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Type-safe helper to get a schema adapter for a provider.
|
|
25
|
+
* Returns null if the provider doesn't support structured output.
|
|
26
|
+
*/
|
|
27
|
+
function getSchemaAdapter(provider) {
|
|
28
|
+
return exports.SCHEMA_ADAPTER_REGISTRY[provider];
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=SchemaAdapterRegistry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaAdapterRegistry.js","sourceRoot":"","sources":["../../../../src/utilities/schema/adapters/SchemaAdapterRegistry.ts"],"names":[],"mappings":";;;AAyBA,4CAEC;AA3BD,+DAAqD;AAErD,wFAAqF;AACrF,oGAAiG;AAEjG;;;;;;;;;;GAUG;AACU,QAAA,uBAAuB,GAAG;IACrC,CAAC,kCAAY,CAAC,MAAM,CAAC,EAAE,IAAI,yCAAmB,EAAE;IAChD,CAAC,kCAAY,CAAC,kBAAkB,CAAC,EAAE,IAAI,yCAAmB,EAAE;CACU,CAAC;AAEzE;;;GAGG;AACH,SAAgB,gBAAgB,CAAC,QAAsB;IACrD,OAAO,+BAAuB,CAAC,QAAQ,CAAC,CAAC;AAC3C,CAAC"}
|