@promptbook/utils 0.50.0-13 → 0.50.0-15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/esm/index.es.js +188 -72
  2. package/esm/index.es.js.map +1 -1
  3. package/esm/typings/_packages/utils.index.d.ts +9 -2
  4. package/esm/typings/conversion/utils/extractParametersFromPromptTemplate.d.ts +13 -0
  5. package/esm/typings/conversion/utils/extractParametersFromPromptTemplate.test.d.ts +1 -0
  6. package/esm/typings/conversion/utils/extractVariables.d.ts +4 -3
  7. package/esm/typings/types/PromptbookJson/PromptTemplateJson.d.ts +1 -1
  8. package/esm/typings/utils/extractParameters.d.ts +1 -3
  9. package/esm/typings/utils/sets/difference.d.ts +4 -0
  10. package/esm/typings/utils/sets/difference.test.d.ts +1 -0
  11. package/esm/typings/utils/sets/intersection.d.ts +4 -0
  12. package/esm/typings/utils/sets/intersection.test.d.ts +1 -0
  13. package/esm/typings/utils/sets/union.d.ts +4 -0
  14. package/esm/typings/utils/sets/union.test.d.ts +1 -0
  15. package/package.json +1 -1
  16. package/umd/index.umd.js +193 -71
  17. package/umd/index.umd.js.map +1 -1
  18. package/umd/typings/_packages/utils.index.d.ts +9 -2
  19. package/umd/typings/conversion/utils/extractParametersFromPromptTemplate.d.ts +13 -0
  20. package/umd/typings/conversion/utils/extractParametersFromPromptTemplate.test.d.ts +1 -0
  21. package/umd/typings/conversion/utils/extractVariables.d.ts +4 -3
  22. package/umd/typings/types/PromptbookJson/PromptTemplateJson.d.ts +1 -1
  23. package/umd/typings/utils/extractParameters.d.ts +1 -3
  24. package/umd/typings/utils/sets/difference.d.ts +4 -0
  25. package/umd/typings/utils/sets/difference.test.d.ts +1 -0
  26. package/umd/typings/utils/sets/intersection.d.ts +4 -0
  27. package/umd/typings/utils/sets/intersection.test.d.ts +1 -0
  28. package/umd/typings/utils/sets/union.d.ts +4 -0
  29. package/umd/typings/utils/sets/union.test.d.ts +1 -0
package/umd/index.umd.js CHANGED
@@ -109,37 +109,6 @@
109
109
  */
110
110
  var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
111
111
 
112
- /**
113
- * Parses the template and returns the list of all parameter names
114
- *
115
- * @param template the template with parameters in {curly} braces
116
- * @returns the list of parameter names
117
- *
118
- * @private within the library
119
- */
120
- function extractParameters(template) {
121
- var e_1, _a;
122
- var matches = template.matchAll(/{\w+}/g);
123
- var parameterNames = [];
124
- try {
125
- for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
126
- var match = matches_1_1.value;
127
- var parameterName = match[0].slice(1, -1);
128
- if (!parameterNames.includes(parameterName)) {
129
- parameterNames.push(parameterName);
130
- }
131
- }
132
- }
133
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
134
- finally {
135
- try {
136
- if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
137
- }
138
- finally { if (e_1) throw e_1.error; }
139
- }
140
- return parameterNames;
141
- }
142
-
143
112
  /**
144
113
  * Computes the deepness of the markdown structure.
145
114
  *
@@ -405,10 +374,75 @@
405
374
  return spacetrim.spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
406
375
  }
407
376
 
377
+ /**
378
+ * Creates a new set with all elements that are present in either set
379
+ */
380
+ function union() {
381
+ var e_1, _a, e_2, _b;
382
+ var sets = [];
383
+ for (var _i = 0; _i < arguments.length; _i++) {
384
+ sets[_i] = arguments[_i];
385
+ }
386
+ var union = new Set();
387
+ try {
388
+ for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
389
+ var set = sets_1_1.value;
390
+ try {
391
+ for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
392
+ var item = _d.value;
393
+ union.add(item);
394
+ }
395
+ }
396
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
397
+ finally {
398
+ try {
399
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
400
+ }
401
+ finally { if (e_2) throw e_2.error; }
402
+ }
403
+ }
404
+ }
405
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
406
+ finally {
407
+ try {
408
+ if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
409
+ }
410
+ finally { if (e_1) throw e_1.error; }
411
+ }
412
+ return union;
413
+ }
414
+
408
415
  /**
409
416
  * The version of the Promptbook library
410
417
  */
411
- var PROMPTBOOK_VERSION = '0.50.0-12';
418
+ var PROMPTBOOK_VERSION = '0.50.0-14';
419
+
420
+ /**
421
+ * Parses the template and returns the list of all parameter names
422
+ *
423
+ * @param template the template with parameters in {curly} braces
424
+ * @returns the list of parameter names
425
+ */
426
+ function extractParameters(template) {
427
+ var e_1, _a;
428
+ var matches = template.matchAll(/{\w+}/g);
429
+ var parameterNames = new Set();
430
+ try {
431
+ for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
432
+ var match = matches_1_1.value;
433
+ var parameterName = match[0].slice(1, -1);
434
+ parameterNames.add(parameterName);
435
+ }
436
+ }
437
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
438
+ finally {
439
+ try {
440
+ if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
441
+ }
442
+ finally { if (e_1) throw e_1.error; }
443
+ }
444
+ return parameterNames;
445
+ }
412
446
 
413
447
  /**
414
448
  * Parses the given script and returns the list of all used variables that are not defined in the script
@@ -416,11 +450,9 @@
416
450
  * @param script from which to extract the variables
417
451
  * @returns the list of variable names
418
452
  * @throws {PromptbookSyntaxError} if the script is invalid
419
- *
420
- * @private within the promptbookStringToJson
421
453
  */
422
454
  function extractVariables(script) {
423
- var variables = [];
455
+ var variables = new Set();
424
456
  script = "(()=>{".concat(script, "})()");
425
457
  try {
426
458
  for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
@@ -444,7 +476,7 @@
444
476
  script = "const ".concat(undefinedName, " = ()=>'';") + script;
445
477
  }
446
478
  else {
447
- variables.push(undefinedName);
479
+ variables.add(undefinedName);
448
480
  script = "const ".concat(undefinedName, " = '';") + script;
449
481
  }
450
482
  }
@@ -457,6 +489,53 @@
457
489
  }
458
490
  return variables;
459
491
  }
492
+ /**
493
+ * TODO: [🔣] Support for multiple languages - python, java,...
494
+ */
495
+
496
+ /**
497
+ * Parses the prompt template and returns the set of all used parameters
498
+ *
499
+ * @param promptTemplate the template with used parameters
500
+ * @returns the set of parameter names
501
+ * @throws {PromptbookSyntaxError} if the script is invalid
502
+ */
503
+ function extractParametersFromPromptTemplate(promptTemplate) {
504
+ var e_1, _a, e_2, _b;
505
+ var parameterNames = new Set();
506
+ try {
507
+ for (var _c = __values(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameters(promptTemplate.title)), false), __read(extractParameters(promptTemplate.description || '')), false), __read(extractParameters(promptTemplate.content)), false)), _d = _c.next(); !_d.done; _d = _c.next()) {
508
+ var parameterName = _d.value;
509
+ parameterNames.add(parameterName);
510
+ }
511
+ }
512
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
513
+ finally {
514
+ try {
515
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
516
+ }
517
+ finally { if (e_1) throw e_1.error; }
518
+ }
519
+ if (promptTemplate.executionType === 'SCRIPT') {
520
+ try {
521
+ for (var _e = __values(extractVariables(promptTemplate.content)), _f = _e.next(); !_f.done; _f = _e.next()) {
522
+ var parameterName = _f.value;
523
+ parameterNames.add(parameterName);
524
+ }
525
+ }
526
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
527
+ finally {
528
+ try {
529
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
530
+ }
531
+ finally { if (e_2) throw e_2.error; }
532
+ }
533
+ }
534
+ return parameterNames;
535
+ }
536
+ /**
537
+ * TODO: [🔣] If script require contentLanguage
538
+ */
460
539
 
461
540
  /**
462
541
  * Execution type describes the way how the block is executed
@@ -474,7 +553,7 @@
474
553
  /**
475
554
  * Units of text measurement
476
555
  */
477
- var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'PARAGRAPHS', 'LINES', 'PAGES'];
556
+ var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'LINES', 'PARAGRAPHS', 'PAGES'];
478
557
  /**
479
558
  * TODO: [💝] Unite object for expecting amount and format - remove expectFormat
480
559
  * TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
@@ -934,11 +1013,11 @@
934
1013
  finally { if (e_1) throw e_1.error; }
935
1014
  }
936
1015
  var _loop_1 = function (section) {
937
- var e_3, _e, e_4, _f, e_5, _g;
1016
+ var e_3, _e;
938
1017
  // TODO: Parse prompt template description (the content out of the codeblock and lists)
939
1018
  var templateModelRequirements = __assign({}, defaultModelRequirements);
940
1019
  var listItems_3 = extractAllListItemsFromMarkdown(section.content);
941
- var dependentParameterNames = [];
1020
+ var dependentParameterNames = new Set();
942
1021
  var executionType = 'PROMPT_TEMPLATE';
943
1022
  var jokers = [];
944
1023
  var postprocessing = [];
@@ -952,7 +1031,7 @@
952
1031
  switch (command.type) {
953
1032
  case 'JOKER':
954
1033
  jokers.push(command.parameterName);
955
- dependentParameterNames.push(command.parameterName);
1034
+ dependentParameterNames.add(command.parameterName);
956
1035
  break;
957
1036
  case 'EXECUTE':
958
1037
  if (isExecutionTypeChanged) {
@@ -1006,7 +1085,7 @@
1006
1085
  }
1007
1086
  finally { if (e_3) throw e_3.error; }
1008
1087
  }
1009
- var _h = extractOneBlockFromMarkdown(section.content), language = _h.language, content = _h.content;
1088
+ var _f = extractOneBlockFromMarkdown(section.content), language = _f.language, content = _f.content;
1010
1089
  if (executionType === 'SCRIPT') {
1011
1090
  if (!language) {
1012
1091
  throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
@@ -1045,40 +1124,12 @@
1045
1124
  if (Object.keys(postprocessing).length === 0) {
1046
1125
  postprocessing = undefined;
1047
1126
  }
1048
- try {
1049
- for (var _j = (e_4 = void 0, __values(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameters(section.title)), false), __read(extractParameters(description_1 || '')), false), __read(extractParameters(content)), false))), _k = _j.next(); !_k.done; _k = _j.next()) {
1050
- var parameterName = _k.value;
1051
- dependentParameterNames.push(parameterName);
1052
- }
1053
- }
1054
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
1055
- finally {
1056
- try {
1057
- if (_k && !_k.done && (_f = _j.return)) _f.call(_j);
1058
- }
1059
- finally { if (e_4) throw e_4.error; }
1060
- }
1061
- if (executionType === 'SCRIPT') {
1062
- try {
1063
- for (var _l = (e_5 = void 0, __values(extractVariables(content))), _m = _l.next(); !_m.done; _m = _l.next()) {
1064
- var parameterName = _m.value;
1065
- dependentParameterNames.push(parameterName);
1066
- }
1067
- }
1068
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
1069
- finally {
1070
- try {
1071
- if (_m && !_m.done && (_g = _l.return)) _g.call(_l);
1072
- }
1073
- finally { if (e_5) throw e_5.error; }
1074
- }
1075
- }
1076
- dependentParameterNames = __spreadArray([], __read(new Set(dependentParameterNames)), false);
1127
+ dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, executionType: executionType, content: content })));
1077
1128
  promptbookJson.promptTemplates.push({
1078
1129
  name: titleToName(section.title),
1079
1130
  title: section.title,
1080
1131
  description: description_1,
1081
- dependentParameterNames: dependentParameterNames,
1132
+ dependentParameterNames: Array.from(dependentParameterNames),
1082
1133
  executionType: executionType,
1083
1134
  jokers: jokers,
1084
1135
  postprocessing: postprocessing,
@@ -2516,6 +2567,71 @@
2516
2567
  return text;
2517
2568
  }
2518
2569
 
2570
+ /**
2571
+ * Create difference set of two sets.
2572
+ */
2573
+ function difference(a, b, isEqual) {
2574
+ var e_1, _a;
2575
+ if (isEqual === void 0) { isEqual = function (a, b) { return a === b; }; }
2576
+ var diff = new Set();
2577
+ var _loop_1 = function (itemA) {
2578
+ if (!Array.from(b).some(function (itemB) { return isEqual(itemA, itemB); })) {
2579
+ diff.add(itemA);
2580
+ }
2581
+ };
2582
+ try {
2583
+ for (var _b = __values(Array.from(a)), _c = _b.next(); !_c.done; _c = _b.next()) {
2584
+ var itemA = _c.value;
2585
+ _loop_1(itemA);
2586
+ }
2587
+ }
2588
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2589
+ finally {
2590
+ try {
2591
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
2592
+ }
2593
+ finally { if (e_1) throw e_1.error; }
2594
+ }
2595
+ return diff;
2596
+ }
2597
+
2598
+ /**
2599
+ * Creates a new set with all elements that are present in all sets
2600
+ */
2601
+ function intersection() {
2602
+ var e_1, _a;
2603
+ var sets = [];
2604
+ for (var _i = 0; _i < arguments.length; _i++) {
2605
+ sets[_i] = arguments[_i];
2606
+ }
2607
+ var intersection = new Set();
2608
+ if (sets[0]) {
2609
+ try {
2610
+ for (var _b = __values(Array.from(sets[0])), _c = _b.next(); !_c.done; _c = _b.next()) {
2611
+ var item = _c.value;
2612
+ var isPresentInAllSets = true;
2613
+ for (var i = 1; i < sets.length; i++) {
2614
+ if (sets[i] !== undefined && !sets[i].has(item)) {
2615
+ isPresentInAllSets = false;
2616
+ break;
2617
+ }
2618
+ }
2619
+ if (isPresentInAllSets) {
2620
+ intersection.add(item);
2621
+ }
2622
+ }
2623
+ }
2624
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2625
+ finally {
2626
+ try {
2627
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
2628
+ }
2629
+ finally { if (e_1) throw e_1.error; }
2630
+ }
2631
+ }
2632
+ return intersection;
2633
+ }
2634
+
2519
2635
  /**
2520
2636
  * Function trimCodeBlock will trim starting and ending code block from the string if it is present.
2521
2637
  *
@@ -2650,11 +2766,16 @@
2650
2766
  exports.countSentences = countSentences;
2651
2767
  exports.countWords = countWords;
2652
2768
  exports.decapitalize = decapitalize;
2769
+ exports.difference = difference;
2653
2770
  exports.executionReportJsonToString = executionReportJsonToString;
2654
2771
  exports.extractAllBlocksFromMarkdown = extractAllBlocksFromMarkdown;
2655
2772
  exports.extractAllListItemsFromMarkdown = extractAllListItemsFromMarkdown;
2656
2773
  exports.extractBlock = extractBlock;
2657
2774
  exports.extractOneBlockFromMarkdown = extractOneBlockFromMarkdown;
2775
+ exports.extractParameters = extractParameters;
2776
+ exports.extractParametersFromPromptTemplate = extractParametersFromPromptTemplate;
2777
+ exports.extractVariables = extractVariables;
2778
+ exports.intersection = intersection;
2658
2779
  exports.isPassingExpectations = isPassingExpectations;
2659
2780
  exports.isValidJsonString = isValidJsonString;
2660
2781
  exports.isValidKeyword = isValidKeyword;
@@ -2684,6 +2805,7 @@
2684
2805
  exports.titleToName = titleToName;
2685
2806
  exports.trimCodeBlock = trimCodeBlock;
2686
2807
  exports.trimEndOfCodeBlock = trimEndOfCodeBlock;
2808
+ exports.union = union;
2687
2809
  exports.unwrapResult = unwrapResult;
2688
2810
 
2689
2811
  Object.defineProperty(exports, '__esModule', { value: true });