@mate-academy/llm-gateway 3.0.1 → 3.0.4

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.
@@ -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 recursively to handle nested sections
45
- return source.replace(/{{#(\w+)}}([\s\S]*?){{\/\1}}/g, (_, sectionName, content) => {
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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;;AAqHH,oDAQC;AA3DD,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,qEAAqE;QACrE,OAAO,MAAM,CAAC,OAAO,CA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AAM,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,4 @@
1
- import type { LLMProviders } from '../../../LLMService.typedefs';
1
+ import { LLMProviders } from '../../../LLMService.typedefs';
2
2
  import type { SchemaAdapterInterface } from './SchemaAdapterInterface';
3
3
  import { OpenAISchemaAdapter } from '../../../providers/OpenAI/schemas/OpenAISchemaAdapter';
4
4
  import { GoogleSchemaAdapter } from '../../../providers/GoogleGenerativeAI/schemas/GoogleSchemaAdapter';
@@ -22,7 +22,3 @@ export declare const SCHEMA_ADAPTER_REGISTRY: {
22
22
  * Returns null if the provider doesn't support structured output.
23
23
  */
24
24
  export declare function getSchemaAdapter(provider: LLMProviders): SchemaAdapterInterface | null;
25
- /**
26
- * Type-safe helper to check if a provider supports structured output.
27
- */
28
- export declare function providerSupportsStructuredOutput(provider: LLMProviders): boolean;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SCHEMA_ADAPTER_REGISTRY = void 0;
4
4
  exports.getSchemaAdapter = getSchemaAdapter;
5
- exports.providerSupportsStructuredOutput = providerSupportsStructuredOutput;
5
+ const LLMService_typedefs_1 = require("../../../LLMService.typedefs");
6
6
  const OpenAISchemaAdapter_1 = require("../../../providers/OpenAI/schemas/OpenAISchemaAdapter");
7
7
  const GoogleSchemaAdapter_1 = require("../../../providers/GoogleGenerativeAI/schemas/GoogleSchemaAdapter");
8
8
  /**
@@ -17,8 +17,8 @@ const GoogleSchemaAdapter_1 = require("../../../providers/GoogleGenerativeAI/sch
17
17
  * This ensures compile-time safety and forces developers to handle all providers.
18
18
  */
19
19
  exports.SCHEMA_ADAPTER_REGISTRY = {
20
- OpenAI: new OpenAISchemaAdapter_1.OpenAISchemaAdapter(),
21
- GoogleGenerativeAI: new GoogleSchemaAdapter_1.GoogleSchemaAdapter(),
20
+ [LLMService_typedefs_1.LLMProviders.OpenAI]: new OpenAISchemaAdapter_1.OpenAISchemaAdapter(),
21
+ [LLMService_typedefs_1.LLMProviders.GoogleGenerativeAI]: new GoogleSchemaAdapter_1.GoogleSchemaAdapter(),
22
22
  };
23
23
  /**
24
24
  * Type-safe helper to get a schema adapter for a provider.
@@ -27,10 +27,4 @@ exports.SCHEMA_ADAPTER_REGISTRY = {
27
27
  function getSchemaAdapter(provider) {
28
28
  return exports.SCHEMA_ADAPTER_REGISTRY[provider];
29
29
  }
30
- /**
31
- * Type-safe helper to check if a provider supports structured output.
32
- */
33
- function providerSupportsStructuredOutput(provider) {
34
- return exports.SCHEMA_ADAPTER_REGISTRY[provider] !== null;
35
- }
36
30
  //# sourceMappingURL=SchemaAdapterRegistry.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"SchemaAdapterRegistry.js","sourceRoot":"","sources":["../../../../src/utilities/schema/adapters/SchemaAdapterRegistry.ts"],"names":[],"mappings":";;;AAyBA,4CAEC;AAKD,4EAEC;AAhCD,wFAAqF;AACrF,oGAAiG;AAEjG;;;;;;;;;;GAUG;AACU,QAAA,uBAAuB,GAAG;IACrC,MAAM,EAAE,IAAI,yCAAmB,EAAE;IACjC,kBAAkB,EAAE,IAAI,yCAAmB,EAAE;CACyB,CAAC;AAEzE;;;GAGG;AACH,SAAgB,gBAAgB,CAAC,QAAsB;IACrD,OAAO,+BAAuB,CAAC,QAAQ,CAAC,CAAC;AAC3C,CAAC;AAED;;GAEG;AACH,SAAgB,gCAAgC,CAAC,QAAsB;IACrE,OAAO,+BAAuB,CAAC,QAAQ,CAAC,KAAK,IAAI,CAAC;AACpD,CAAC"}
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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mate-academy/llm-gateway",
3
- "version": "3.0.1",
3
+ "version": "3.0.4",
4
4
  "description": "A gateway package for LLM services.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",