@promptbook/core 0.61.0-14 → 0.61.0-16

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 (87) hide show
  1. package/esm/index.es.js +882 -614
  2. package/esm/index.es.js.map +1 -1
  3. package/esm/typings/promptbook-collection/index.d.ts +12 -15
  4. package/esm/typings/src/_packages/core.index.d.ts +2 -1
  5. package/esm/typings/src/_packages/types.index.d.ts +2 -3
  6. package/esm/typings/src/_packages/utils.index.d.ts +4 -3
  7. package/esm/typings/src/commands/KNOWLEDGE/KnowledgeCommand.d.ts +2 -3
  8. package/esm/typings/src/commands/_common/types/CommandParser.d.ts +28 -3
  9. package/esm/typings/src/config.d.ts +26 -1
  10. package/esm/typings/src/config.test.d.ts +4 -0
  11. package/esm/typings/src/conversion/utils/extractParametersFromPromptTemplate.d.ts +2 -2
  12. package/esm/typings/src/conversion/utils/stringifyPipelineJson.d.ts +1 -1
  13. package/esm/typings/src/conversion/validation/validatePipeline.d.ts +3 -0
  14. package/esm/typings/src/execution/PipelineExecutor.d.ts +5 -5
  15. package/esm/typings/src/execution/PromptResultUsage.d.ts +3 -3
  16. package/esm/typings/src/execution/ScriptExecutionTools.d.ts +2 -3
  17. package/esm/typings/src/execution/createPipelineExecutor.d.ts +16 -3
  18. package/esm/typings/src/formats/_common/FormatDefinition.d.ts +1 -1
  19. package/esm/typings/src/knowledge/prepare-knowledge/_common/Scraper.d.ts +27 -0
  20. package/esm/typings/src/knowledge/prepare-knowledge/_common/prepareKnowledgePieces.d.ts +1 -1
  21. package/esm/typings/src/knowledge/prepare-knowledge/markdown/prepareKnowledgeFromMarkdown.d.ts +1 -1
  22. package/esm/typings/src/knowledge/prepare-knowledge/pdf/prepareKnowledgeFromPdf.d.ts +1 -1
  23. package/esm/typings/src/llm-providers/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +1 -1
  24. package/esm/typings/src/llm-providers/azure-openai/AzureOpenAiExecutionTools.d.ts +2 -2
  25. package/esm/typings/src/llm-providers/mocked/MockedEchoLlmExecutionTools.d.ts +2 -2
  26. package/esm/typings/src/llm-providers/mocked/MockedFackedLlmExecutionTools.d.ts +3 -3
  27. package/esm/typings/src/llm-providers/openai/OpenAiExecutionTools.d.ts +3 -3
  28. package/esm/typings/src/prepare/isPipelinePrepared.d.ts +9 -0
  29. package/esm/typings/src/prepare/preparePipeline.d.ts +1 -0
  30. package/esm/typings/src/types/PipelineJson/LlmTemplateJson.d.ts +2 -1
  31. package/esm/typings/src/types/PipelineJson/PipelineJson.d.ts +1 -0
  32. package/esm/typings/src/types/PipelineJson/PromptDialogJson.d.ts +3 -0
  33. package/esm/typings/src/types/PipelineJson/PromptTemplateJson.d.ts +4 -0
  34. package/esm/typings/src/types/PipelineJson/PromptTemplateJsonCommon.d.ts +5 -3
  35. package/esm/typings/src/types/Prompt.d.ts +7 -10
  36. package/esm/typings/src/types/typeAliases.d.ts +44 -4
  37. package/esm/typings/src/utils/deepFreeze.d.ts +10 -1
  38. package/esm/typings/src/utils/extractParameters.d.ts +2 -2
  39. package/esm/typings/src/{execution/utils → utils}/replaceParameters.d.ts +2 -4
  40. package/esm/typings/src/utils/sets/difference.d.ts +3 -0
  41. package/package.json +1 -1
  42. package/umd/index.umd.js +882 -613
  43. package/umd/index.umd.js.map +1 -1
  44. package/umd/typings/promptbook-collection/index.d.ts +12 -15
  45. package/umd/typings/src/_packages/core.index.d.ts +2 -1
  46. package/umd/typings/src/_packages/types.index.d.ts +2 -3
  47. package/umd/typings/src/_packages/utils.index.d.ts +4 -3
  48. package/umd/typings/src/commands/KNOWLEDGE/KnowledgeCommand.d.ts +2 -3
  49. package/umd/typings/src/commands/_common/types/CommandParser.d.ts +28 -3
  50. package/umd/typings/src/config.d.ts +26 -1
  51. package/umd/typings/src/config.test.d.ts +4 -0
  52. package/umd/typings/src/conversion/utils/extractParametersFromPromptTemplate.d.ts +2 -2
  53. package/umd/typings/src/conversion/utils/stringifyPipelineJson.d.ts +1 -1
  54. package/umd/typings/src/conversion/validation/validatePipeline.d.ts +3 -0
  55. package/umd/typings/src/execution/PipelineExecutor.d.ts +5 -5
  56. package/umd/typings/src/execution/PromptResultUsage.d.ts +3 -3
  57. package/umd/typings/src/execution/ScriptExecutionTools.d.ts +2 -3
  58. package/umd/typings/src/execution/createPipelineExecutor.d.ts +16 -3
  59. package/umd/typings/src/formats/_common/FormatDefinition.d.ts +1 -1
  60. package/umd/typings/src/knowledge/prepare-knowledge/_common/Scraper.d.ts +27 -0
  61. package/umd/typings/src/knowledge/prepare-knowledge/_common/prepareKnowledgePieces.d.ts +1 -1
  62. package/umd/typings/src/knowledge/prepare-knowledge/markdown/prepareKnowledgeFromMarkdown.d.ts +1 -1
  63. package/umd/typings/src/knowledge/prepare-knowledge/pdf/prepareKnowledgeFromPdf.d.ts +1 -1
  64. package/umd/typings/src/llm-providers/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +1 -1
  65. package/umd/typings/src/llm-providers/azure-openai/AzureOpenAiExecutionTools.d.ts +2 -2
  66. package/umd/typings/src/llm-providers/mocked/MockedEchoLlmExecutionTools.d.ts +2 -2
  67. package/umd/typings/src/llm-providers/mocked/MockedFackedLlmExecutionTools.d.ts +3 -3
  68. package/umd/typings/src/llm-providers/openai/OpenAiExecutionTools.d.ts +3 -3
  69. package/umd/typings/src/prepare/isPipelinePrepared.d.ts +9 -0
  70. package/umd/typings/src/prepare/isPipelinePrepared.test.d.ts +1 -0
  71. package/umd/typings/src/prepare/preparePipeline.d.ts +1 -0
  72. package/umd/typings/src/types/PipelineJson/LlmTemplateJson.d.ts +2 -1
  73. package/umd/typings/src/types/PipelineJson/PipelineJson.d.ts +1 -0
  74. package/umd/typings/src/types/PipelineJson/PromptDialogJson.d.ts +3 -0
  75. package/umd/typings/src/types/PipelineJson/PromptTemplateJson.d.ts +4 -0
  76. package/umd/typings/src/types/PipelineJson/PromptTemplateJsonCommon.d.ts +5 -3
  77. package/umd/typings/src/types/Prompt.d.ts +7 -10
  78. package/umd/typings/src/types/typeAliases.d.ts +44 -4
  79. package/umd/typings/src/utils/deepFreeze.d.ts +10 -1
  80. package/umd/typings/src/utils/extractParameters.d.ts +2 -2
  81. package/umd/typings/src/{execution/utils → utils}/replaceParameters.d.ts +2 -4
  82. package/umd/typings/src/utils/replaceParameters.test.d.ts +1 -0
  83. package/umd/typings/src/utils/sets/difference.d.ts +3 -0
  84. package/esm/typings/src/types/Parameters.d.ts +0 -14
  85. package/umd/typings/src/types/Parameters.d.ts +0 -14
  86. /package/esm/typings/src/{execution/utils/replaceParameters.test.d.ts → prepare/isPipelinePrepared.test.d.ts} +0 -0
  87. /package/{umd/typings/src/execution → esm/typings/src}/utils/replaceParameters.test.d.ts +0 -0
package/esm/index.es.js CHANGED
@@ -251,7 +251,7 @@ function pipelineJsonToString(pipelineJson) {
251
251
  /* Note: Not using:> name, */
252
252
  title_1 = promptTemplate.title, description_1 = promptTemplate.description,
253
253
  /* Note: dependentParameterNames, */
254
- jokers = promptTemplate.jokers, blockType = promptTemplate.blockType, content = promptTemplate.content, postprocessing = promptTemplate.postprocessing, expectations = promptTemplate.expectations, expectFormat = promptTemplate.expectFormat, resultingParameterName = promptTemplate.resultingParameterName;
254
+ jokers = promptTemplate.jokerParameterNames, blockType = promptTemplate.blockType, content = promptTemplate.content, postprocessing = promptTemplate.postprocessingFunctionNames, expectations = promptTemplate.expectations, expectFormat = promptTemplate.expectFormat, resultingParameterName = promptTemplate.resultingParameterName;
255
255
  pipelineString += '\n\n';
256
256
  pipelineString += "## ".concat(title_1);
257
257
  if (description_1) {
@@ -395,7 +395,7 @@ function promptTemplateParameterJsonToString(promptTemplateParameterJson) {
395
395
  *
396
396
  * @returns The same object as the input, but deeply frozen
397
397
  *
398
- * Note: This function mutates the object
398
+ * Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
399
399
  */
400
400
  function deepFreeze(objectValue) {
401
401
  var e_1, _a;
@@ -418,11 +418,42 @@ function deepFreeze(objectValue) {
418
418
  }
419
419
  return Object.freeze(objectValue);
420
420
  }
421
+ /**
422
+ * @@@
423
+ * @@@
424
+ *
425
+ * @returns The same object as the input, but deeply frozen
426
+ *
427
+ * Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
428
+ */
429
+ function deepFreezeWithSameType(objectValue) {
430
+ return deepFreeze(objectValue);
431
+ }
421
432
  /**
422
433
  * TODO: [🔼] Export from `@promptbook/utils`
423
434
  * TODO: [🧠] Is there a way how to meaningfully test this utility
424
435
  */
425
436
 
437
+ /**
438
+ * Returns the same value that is passed as argument.
439
+ * No side effects.
440
+ *
441
+ * Note: It can be usefull for:
442
+ *
443
+ * 1) Leveling indentation
444
+ * 2) Putting always-true or always-false conditions without getting eslint errors
445
+ *
446
+ * @param value any values
447
+ * @returns the same values
448
+ * @private within the repository
449
+ */
450
+ function just(value) {
451
+ if (value === undefined) {
452
+ return undefined;
453
+ }
454
+ return value;
455
+ }
456
+
426
457
  /**
427
458
  * The maximum number of iterations for a loops
428
459
  */
@@ -431,18 +462,30 @@ var LOOP_LIMIT = 1000;
431
462
  * The maximum number of (LLM) tasks running in parallel
432
463
  */
433
464
  var MAX_PARALLEL_COUNT = 5;
465
+ /**
466
+ * The maximum number of attempts to execute LLM task before giving up
467
+ */
468
+ var MAX_EXECUTION_ATTEMPTS = 3;
434
469
  /**
435
470
  * The names of the parameters that are reserved for special purposes
436
471
  */
437
472
  var RESERVED_PARAMETER_NAMES = deepFreeze([
438
473
  'context',
474
+ 'currentDate',
439
475
  // <- TODO: Add more like 'date', 'modelName',...
440
476
  // <- TODO: Add [emoji] + instructions ACRY when adding new reserved parameter
441
477
  ]);
478
+ /**
479
+ * Nonce which is used for replacing things in strings
480
+ */
481
+ var REPLACING_NONCE = 'u$k42k%!V2zo34w7Fu#@QUHYPW';
442
482
  /*
443
483
  TODO: !!! Just testing false-negative detection of [🟡][🟢][🔵][⚪] leak
444
484
  */
445
485
  // [🟡][🟢][🔵][⚪]
486
+ /**
487
+ * TODO: [🔼] Export all to core
488
+ */
446
489
 
447
490
  /**
448
491
  * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
@@ -645,7 +688,7 @@ function isValidPipelineUrl(url) {
645
688
  */
646
689
  function validatePipeline(pipeline) {
647
690
  // TODO: [🧠] Maybe test if promptbook is a promise and make specific error case for that
648
- var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
691
+ var e_1, _a, e_2, _b, e_3, _c, e_4, _d, e_5, _e;
649
692
  if (pipeline.pipelineUrl !== undefined && !isValidPipelineUrl(pipeline.pipelineUrl)) {
650
693
  // <- Note: [🚲]
651
694
  throw new PipelineLogicError("Invalid promptbook URL \"".concat(pipeline.pipelineUrl, "\""));
@@ -682,19 +725,19 @@ function validatePipeline(pipeline) {
682
725
  };
683
726
  try {
684
727
  // Note: Check each parameter individually
685
- for (var _e = __values(pipeline.parameters), _f = _e.next(); !_f.done; _f = _e.next()) {
686
- var parameter = _f.value;
728
+ for (var _f = __values(pipeline.parameters), _g = _f.next(); !_g.done; _g = _f.next()) {
729
+ var parameter = _g.value;
687
730
  _loop_1(parameter);
688
731
  }
689
732
  }
690
733
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
691
734
  finally {
692
735
  try {
693
- if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
736
+ if (_g && !_g.done && (_a = _f.return)) _a.call(_f);
694
737
  }
695
738
  finally { if (e_1) throw e_1.error; }
696
739
  }
697
- // Note: Check each template individually
740
+ // Note: All input parameters are defined - so that they can be used as result of some template
698
741
  var definedParameters = new Set(pipeline.parameters.filter(function (_a) {
699
742
  var isInput = _a.isInput;
700
743
  return isInput;
@@ -703,23 +746,27 @@ function validatePipeline(pipeline) {
703
746
  return name;
704
747
  }));
705
748
  try {
706
- for (var _g = __values(pipeline.promptTemplates), _h = _g.next(); !_h.done; _h = _g.next()) {
707
- var template = _h.value;
749
+ // Note: Checking each template individually
750
+ for (var _h = __values(pipeline.promptTemplates), _j = _h.next(); !_j.done; _j = _h.next()) {
751
+ var template = _j.value;
708
752
  if (definedParameters.has(template.resultingParameterName)) {
709
753
  throw new PipelineLogicError("Parameter {".concat(template.resultingParameterName, "} is defined multiple times"));
710
754
  }
755
+ if (RESERVED_PARAMETER_NAMES.includes(template.resultingParameterName)) {
756
+ throw new PipelineLogicError("Parameter name {".concat(template.resultingParameterName, "} is reserved, please use fifferent name"));
757
+ }
711
758
  definedParameters.add(template.resultingParameterName);
712
759
  if (template.blockType === 'PROMPT_TEMPLATE' && template.modelRequirements.modelVariant === undefined) {
713
760
  throw new PipelineLogicError(spaceTrim$1("\n\n You must specify MODEL VARIANT in the prompt template \"".concat(template.title, "\"\n\n For example:\n - MODEL VARIANT Chat\n - MODEL NAME `gpt-4-1106-preview`\n\n ")));
714
761
  }
715
- if (template.jokers && template.jokers.length > 0) {
762
+ if (template.jokerParameterNames && template.jokerParameterNames.length > 0) {
716
763
  if (!template.expectFormat &&
717
764
  !template.expectations /* <- TODO: Require at least 1 -> min <- expectation to use jokers */) {
718
765
  throw new PipelineLogicError("Joker parameters are used for {".concat(template.resultingParameterName, "} but no expectations are defined"));
719
766
  }
720
767
  try {
721
- for (var _j = (e_3 = void 0, __values(template.jokers)), _k = _j.next(); !_k.done; _k = _j.next()) {
722
- var joker = _k.value;
768
+ for (var _k = (e_3 = void 0, __values(template.jokerParameterNames)), _l = _k.next(); !_l.done; _l = _k.next()) {
769
+ var joker = _l.value;
723
770
  if (!template.dependentParameterNames.includes(joker)) {
724
771
  throw new PipelineLogicError("Parameter {".concat(joker, "} is used for {").concat(template.resultingParameterName, "} as joker but not in dependentParameterNames"));
725
772
  }
@@ -728,15 +775,15 @@ function validatePipeline(pipeline) {
728
775
  catch (e_3_1) { e_3 = { error: e_3_1 }; }
729
776
  finally {
730
777
  try {
731
- if (_k && !_k.done && (_c = _j.return)) _c.call(_j);
778
+ if (_l && !_l.done && (_c = _k.return)) _c.call(_k);
732
779
  }
733
780
  finally { if (e_3) throw e_3.error; }
734
781
  }
735
782
  }
736
783
  if (template.expectations) {
737
784
  try {
738
- for (var _l = (e_4 = void 0, __values(Object.entries(template.expectations))), _m = _l.next(); !_m.done; _m = _l.next()) {
739
- var _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], min = _p.min, max = _p.max;
785
+ for (var _m = (e_4 = void 0, __values(Object.entries(template.expectations))), _o = _m.next(); !_o.done; _o = _m.next()) {
786
+ var _p = __read(_o.value, 2), unit = _p[0], _q = _p[1], min = _q.min, max = _q.max;
740
787
  if (min !== undefined && max !== undefined && min > max) {
741
788
  throw new PipelineLogicError("Min expectation (=".concat(min, ") of ").concat(unit, " is higher than max expectation (=").concat(max, ")"));
742
789
  }
@@ -751,7 +798,7 @@ function validatePipeline(pipeline) {
751
798
  catch (e_4_1) { e_4 = { error: e_4_1 }; }
752
799
  finally {
753
800
  try {
754
- if (_m && !_m.done && (_d = _l.return)) _d.call(_l);
801
+ if (_o && !_o.done && (_d = _m.return)) _d.call(_m);
755
802
  }
756
803
  finally { if (e_4) throw e_4.error; }
757
804
  }
@@ -761,7 +808,7 @@ function validatePipeline(pipeline) {
761
808
  catch (e_2_1) { e_2 = { error: e_2_1 }; }
762
809
  finally {
763
810
  try {
764
- if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
811
+ if (_j && !_j.done && (_b = _h.return)) _b.call(_h);
765
812
  }
766
813
  finally { if (e_2) throw e_2.error; }
767
814
  }
@@ -775,7 +822,22 @@ function validatePipeline(pipeline) {
775
822
  var name = _a.name;
776
823
  return name;
777
824
  });
825
+ try {
826
+ // Note: All reserved parameters are resolved
827
+ for (var RESERVED_PARAMETER_NAMES_1 = __values(RESERVED_PARAMETER_NAMES), RESERVED_PARAMETER_NAMES_1_1 = RESERVED_PARAMETER_NAMES_1.next(); !RESERVED_PARAMETER_NAMES_1_1.done; RESERVED_PARAMETER_NAMES_1_1 = RESERVED_PARAMETER_NAMES_1.next()) {
828
+ var reservedParameterName = RESERVED_PARAMETER_NAMES_1_1.value;
829
+ resovedParameters = __spreadArray(__spreadArray([], __read(resovedParameters), false), [reservedParameterName], false);
830
+ }
831
+ }
832
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
833
+ finally {
834
+ try {
835
+ if (RESERVED_PARAMETER_NAMES_1_1 && !RESERVED_PARAMETER_NAMES_1_1.done && (_e = RESERVED_PARAMETER_NAMES_1.return)) _e.call(RESERVED_PARAMETER_NAMES_1);
836
+ }
837
+ finally { if (e_5) throw e_5.error; }
838
+ }
778
839
  var unresovedTemplates = __spreadArray([], __read(pipeline.promptTemplates), false);
840
+ // <- TODO: [🧠][🥜]
779
841
  var loopLimit = LOOP_LIMIT;
780
842
  var _loop_2 = function () {
781
843
  if (loopLimit-- < 0) {
@@ -820,6 +882,9 @@ function validatePipeline(pipeline) {
820
882
  /**
821
883
  * TODO: [🧠][🐣] !!!! Validate that all samples match expectations
822
884
  * TODO: [🧠][🐣] !!!! Validate that knowledge is valid (non-void)
885
+ * TODO: [🧠][🐣] !!!! Validate that persona can be used only with CHAT variant
886
+ * TODO: !!!! Validate that parameter with reserved name not used RESERVED_PARAMETER_NAMES
887
+ * TODO: !!!! Validate that reserved parameter is not used as joker
823
888
  * TODO: [🧠] !!! Validationg not only logic itself but imports around - files and websites and rerefenced pipelines exists
824
889
  * TODO: [🛠] Actions, instruments (and maybe knowledge) => Functions and tools
825
890
  */
@@ -1359,7 +1424,7 @@ function forEachAsync(array, options, callbackfunction) {
1359
1424
  });
1360
1425
  }
1361
1426
 
1362
- var PipelineCollection = [{title:"Prepare Knowledge from Markdown",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.61.0-13",parameters:[{name:"content",description:"Markdown document content",isInput:true,isOutput:false},{name:"knowledge",description:"The knowledge JSON object",isInput:false,isOutput:true}],promptTemplates:[{name:"knowledge",title:"Knowledge",dependentParameterNames:["content"],blockType:"PROMPT_TEMPLATE",personaName:null,modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, extract the important knowledge from the document.\n\n# Rules\n\n- Make pieces of information concise, clear, and easy to understand\n- One piece of information should be approximately 1 paragraph\n- Divide the paragraphs by markdown horizontal lines ---\n- Omit irrelevant information\n- Group redundant information\n- Write just extracted information, nothing else\n\n# The document\n\nTake information from this document:\n\n> {content}",resultingParameterName:"knowledge"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[{id:1,promptbookVersion:"0.61.0-13",modelUsage:{price:{value:0},input:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}},output:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}}}}],sourceFile:"./promptbook-collection/prepare-knowledge-from-markdown.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-keywords.ptbk.md",promptbookVersion:"0.61.0-13",parameters:[{name:"content",description:"The content",isInput:true,isOutput:false},{name:"keywords",description:"Keywords separated by comma",isInput:false,isOutput:true}],promptTemplates:[{name:"knowledge",title:"Knowledge",dependentParameterNames:["content"],blockType:"PROMPT_TEMPLATE",personaName:null,modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, detect the important keywords in the document.\n\n# Rules\n\n- Write just keywords separated by comma\n\n# The document\n\nTake information from this document:\n\n> {content}",resultingParameterName:"keywords"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[{id:1,promptbookVersion:"0.61.0-13",modelUsage:{price:{value:0},input:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}},output:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}}}}],sourceFile:"./promptbook-collection/prepare-knowledge-keywords.ptbk.md"},{title:"Prepare Title",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-title.ptbk.md",promptbookVersion:"0.61.0-13",parameters:[{name:"content",description:"The content",isInput:true,isOutput:false},{name:"title",description:"The title of the document",isInput:false,isOutput:true}],promptTemplates:[{name:"knowledge",title:"Knowledge",dependentParameterNames:["content"],blockType:"PROMPT_TEMPLATE",expectations:{words:{min:1,max:8}},personaName:null,modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced content creator, write best title for the document.\n\n# Rules\n\n- Write just title, nothing else\n- Title should be concise and clear\n- Write maximum 5 words for the title\n\n# The document\n\n> {content}",resultingParameterName:"title"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[{id:1,promptbookVersion:"0.61.0-13",modelUsage:{price:{value:0},input:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}},output:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}}}}],sourceFile:"./promptbook-collection/prepare-knowledge-title.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-persona.ptbk.md",promptbookVersion:"0.61.0-13",parameters:[{name:"availableModelNames",description:"List of available model names separated by comma (,)",isInput:true,isOutput:false},{name:"personaDescription",description:"Description of the persona",isInput:true,isOutput:false},{name:"modelRequirements",description:"Specific requirements for the model",isInput:false,isOutput:true}],promptTemplates:[{name:"make-model-requirements",title:"Make modelRequirements",dependentParameterNames:["availableModelNames","personaDescription"],blockType:"PROMPT_TEMPLATE",expectFormat:"JSON",personaName:null,modelRequirements:{modelVariant:"CHAT",modelName:"gpt-4-turbo"},content:"You are experienced AI engineer, you need to create virtual assistant.\nWrite\n\n## Sample\n\n```json\n{\n\"modelName\": \"gpt-4o\",\n\"systemMessage\": \"You are experienced AI engineer and helpfull assistant.\",\n\"temperature\": 0.7\n}\n```\n\n## Instructions\n\n### Option `modelName`\n\nPick from the following models:\n\n- {availableModelNames}\n\n### Option `systemMessage`\n\nThe system message is used to communicate instructions or provide context to the model at the beginning of a conversation. It is displayed in a different format compared to user messages, helping the model understand its role in the conversation. The system message typically guides the model's behavior, sets the tone, or specifies desired output from the model. By utilizing the system message effectively, users can steer the model towards generating more accurate and relevant responses.\n\nFor example:\n\n> You are an experienced AI engineer and helpful assistant.\n\n> You are a friendly and knowledgeable chatbot.\n\n### Option `temperature`\n\nThe sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.\n\nYou can pick a value between 0 and 2. For example:\n\n- `0.1`: Low temperature, extremely conservative and deterministic\n- `0.5`: Medium temperature, balanced between conservative and creative\n- `1.0`: High temperature, creative and bit random\n- `1.5`: Very high temperature, extremely creative and often chaotic and unpredictable\n- `2.0`: Maximum temperature, completely random and unpredictable, for some extreme creative use cases\n\n# The assistant\n\nTake this description of the persona:\n\n> {personaDescription}",resultingParameterName:"modelRequirements"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[{id:1,promptbookVersion:"0.61.0-13",modelUsage:{price:{value:0},input:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}},output:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}}}}],sourceFile:"./promptbook-collection/prepare-persona.ptbk.md"}];
1427
+ var PipelineCollection = [{title:"Prepare Knowledge from Markdown",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.61.0-15",parameters:[{name:"content",description:"Markdown document content",isInput:true,isOutput:false},{name:"knowledge",description:"The knowledge JSON object",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, extract the important knowledge from the document.\n\n# Rules\n\n- Make pieces of information concise, clear, and easy to understand\n- One piece of information should be approximately 1 paragraph\n- Divide the paragraphs by markdown horizontal lines ---\n- Omit irrelevant information\n- Group redundant information\n- Write just extracted information, nothing else\n\n# The document\n\nTake information from this document:\n\n> {content}",dependentParameterNames:["content"],resultingParameterName:"knowledge"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[{id:1,promptbookVersion:"0.61.0-15",modelUsage:{price:{value:0},input:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}},output:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}}}}],sourceFile:"./promptbook-collection/prepare-knowledge-from-markdown.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-keywords.ptbk.md",promptbookVersion:"0.61.0-15",parameters:[{name:"content",description:"The content",isInput:true,isOutput:false},{name:"keywords",description:"Keywords separated by comma",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, detect the important keywords in the document.\n\n# Rules\n\n- Write just keywords separated by comma\n\n# The document\n\nTake information from this document:\n\n> {content}",dependentParameterNames:["content"],resultingParameterName:"keywords"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[{id:1,promptbookVersion:"0.61.0-15",modelUsage:{price:{value:0},input:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}},output:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}}}}],sourceFile:"./promptbook-collection/prepare-knowledge-keywords.ptbk.md"},{title:"Prepare Title",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-title.ptbk.md",promptbookVersion:"0.61.0-15",parameters:[{name:"content",description:"The content",isInput:true,isOutput:false},{name:"title",description:"The title of the document",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced content creator, write best title for the document.\n\n# Rules\n\n- Write just title, nothing else\n- Title should be concise and clear\n- Write maximum 5 words for the title\n\n# The document\n\n> {content}",expectations:{words:{min:1,max:8}},dependentParameterNames:["content"],resultingParameterName:"title"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[{id:1,promptbookVersion:"0.61.0-15",modelUsage:{price:{value:0},input:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}},output:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}}}}],sourceFile:"./promptbook-collection/prepare-knowledge-title.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-persona.ptbk.md",promptbookVersion:"0.61.0-15",parameters:[{name:"availableModelNames",description:"List of available model names separated by comma (,)",isInput:true,isOutput:false},{name:"personaDescription",description:"Description of the persona",isInput:true,isOutput:false},{name:"modelRequirements",description:"Specific requirements for the model",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"make-model-requirements",title:"Make modelRequirements",modelRequirements:{modelVariant:"CHAT",modelName:"gpt-4-turbo"},content:"You are experienced AI engineer, you need to create virtual assistant.\nWrite\n\n## Sample\n\n```json\n{\n\"modelName\": \"gpt-4o\",\n\"systemMessage\": \"You are experienced AI engineer and helpfull assistant.\",\n\"temperature\": 0.7\n}\n```\n\n## Instructions\n\n### Option `modelName`\n\nPick from the following models:\n\n- {availableModelNames}\n\n### Option `systemMessage`\n\nThe system message is used to communicate instructions or provide context to the model at the beginning of a conversation. It is displayed in a different format compared to user messages, helping the model understand its role in the conversation. The system message typically guides the model's behavior, sets the tone, or specifies desired output from the model. By utilizing the system message effectively, users can steer the model towards generating more accurate and relevant responses.\n\nFor example:\n\n> You are an experienced AI engineer and helpful assistant.\n\n> You are a friendly and knowledgeable chatbot.\n\n### Option `temperature`\n\nThe sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.\n\nYou can pick a value between 0 and 2. For example:\n\n- `0.1`: Low temperature, extremely conservative and deterministic\n- `0.5`: Medium temperature, balanced between conservative and creative\n- `1.0`: High temperature, creative and bit random\n- `1.5`: Very high temperature, extremely creative and often chaotic and unpredictable\n- `2.0`: Maximum temperature, completely random and unpredictable, for some extreme creative use cases\n\n# The assistant\n\nTake this description of the persona:\n\n> {personaDescription}",expectFormat:"JSON",dependentParameterNames:["availableModelNames","personaDescription"],resultingParameterName:"modelRequirements"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[{id:1,promptbookVersion:"0.61.0-15",modelUsage:{price:{value:0},input:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}},output:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}}}}],sourceFile:"./promptbook-collection/prepare-persona.ptbk.md"}];
1363
1428
 
1364
1429
  var defaultDiacriticsRemovalMap = [
1365
1430
  {
@@ -1739,6 +1804,212 @@ function assertsExecutionSuccessful(executionResult) {
1739
1804
  * TODO: [🧠] Can this return type be better typed than void
1740
1805
  */
1741
1806
 
1807
+ /**
1808
+ * Create difference set of two sets.
1809
+ *
1810
+ * @deprecated use new javascript set methods instead @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
1811
+ */
1812
+ function difference(a, b, isEqual) {
1813
+ var e_1, _a;
1814
+ if (isEqual === void 0) { isEqual = function (a, b) { return a === b; }; }
1815
+ var diff = new Set();
1816
+ var _loop_1 = function (itemA) {
1817
+ if (!Array.from(b).some(function (itemB) { return isEqual(itemA, itemB); })) {
1818
+ diff.add(itemA);
1819
+ }
1820
+ };
1821
+ try {
1822
+ for (var _b = __values(Array.from(a)), _c = _b.next(); !_c.done; _c = _b.next()) {
1823
+ var itemA = _c.value;
1824
+ _loop_1(itemA);
1825
+ }
1826
+ }
1827
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1828
+ finally {
1829
+ try {
1830
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1831
+ }
1832
+ finally { if (e_1) throw e_1.error; }
1833
+ }
1834
+ return diff;
1835
+ }
1836
+ /**
1837
+ * TODO: [🧠][💯] Maybe also implement symmetricDifference
1838
+ */
1839
+
1840
+ /**
1841
+ * Parses the template and returns the list of all parameter names
1842
+ *
1843
+ * @param template the template with parameters in {curly} braces
1844
+ * @returns the list of parameter names
1845
+ */
1846
+ function extractParameters(template) {
1847
+ var e_1, _a;
1848
+ var matches = template.matchAll(/{\w+}/g);
1849
+ var parameterNames = new Set();
1850
+ try {
1851
+ for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
1852
+ var match = matches_1_1.value;
1853
+ var parameterName = match[0].slice(1, -1);
1854
+ parameterNames.add(parameterName);
1855
+ }
1856
+ }
1857
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1858
+ finally {
1859
+ try {
1860
+ if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
1861
+ }
1862
+ finally { if (e_1) throw e_1.error; }
1863
+ }
1864
+ return parameterNames;
1865
+ }
1866
+
1867
+ /**
1868
+ * Parses the given script and returns the list of all used variables that are not defined in the script
1869
+ *
1870
+ * @param script from which to extract the variables
1871
+ * @returns the list of variable names
1872
+ * @throws {ParsingError} if the script is invalid
1873
+ */
1874
+ function extractVariables(script) {
1875
+ var variables = new Set();
1876
+ script = "(()=>{".concat(script, "})()");
1877
+ try {
1878
+ for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
1879
+ try {
1880
+ eval(script);
1881
+ }
1882
+ catch (error) {
1883
+ if (!(error instanceof ReferenceError)) {
1884
+ throw error;
1885
+ }
1886
+ var undefinedName = error.message.split(' ')[0];
1887
+ /*
1888
+ Note: Parsing the error
1889
+ [ReferenceError: thing is not defined]
1890
+ */
1891
+ if (!undefinedName) {
1892
+ throw error;
1893
+ }
1894
+ if (script.includes(undefinedName + '(')) {
1895
+ script = "const ".concat(undefinedName, " = ()=>'';") + script;
1896
+ }
1897
+ else {
1898
+ variables.add(undefinedName);
1899
+ script = "const ".concat(undefinedName, " = '';") + script;
1900
+ }
1901
+ }
1902
+ }
1903
+ catch (error) {
1904
+ if (!(error instanceof Error)) {
1905
+ throw error;
1906
+ }
1907
+ throw new ParsingError(spaceTrim$1(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
1908
+ }
1909
+ return variables;
1910
+ }
1911
+ /**
1912
+ * TODO: [🔣] Support for multiple languages - python, java,...
1913
+ */
1914
+
1915
+ /**
1916
+ * Parses the prompt template and returns the set of all used parameters
1917
+ *
1918
+ * @param promptTemplate the template with used parameters
1919
+ * @returns the set of parameter names
1920
+ * @throws {ParsingError} if the script is invalid
1921
+ */
1922
+ function extractParametersFromPromptTemplate(promptTemplate) {
1923
+ var e_1, _a, e_2, _b, e_3, _c;
1924
+ var title = promptTemplate.title, description = promptTemplate.description, blockType = promptTemplate.blockType, content = promptTemplate.content, jokerParameterNames = promptTemplate.jokerParameterNames;
1925
+ var parameterNames = new Set();
1926
+ try {
1927
+ for (var _d = __values(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameters(title)), false), __read(extractParameters(description || '')), false), __read(extractParameters(content)), false)), _e = _d.next(); !_e.done; _e = _d.next()) {
1928
+ var parameterName = _e.value;
1929
+ parameterNames.add(parameterName);
1930
+ }
1931
+ }
1932
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1933
+ finally {
1934
+ try {
1935
+ if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
1936
+ }
1937
+ finally { if (e_1) throw e_1.error; }
1938
+ }
1939
+ if (blockType === 'SCRIPT') {
1940
+ try {
1941
+ for (var _f = __values(extractVariables(content)), _g = _f.next(); !_g.done; _g = _f.next()) {
1942
+ var parameterName = _g.value;
1943
+ parameterNames.add(parameterName);
1944
+ }
1945
+ }
1946
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1947
+ finally {
1948
+ try {
1949
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
1950
+ }
1951
+ finally { if (e_2) throw e_2.error; }
1952
+ }
1953
+ }
1954
+ try {
1955
+ for (var _h = __values(jokerParameterNames || []), _j = _h.next(); !_j.done; _j = _h.next()) {
1956
+ var jokerName = _j.value;
1957
+ parameterNames.add(jokerName);
1958
+ }
1959
+ }
1960
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
1961
+ finally {
1962
+ try {
1963
+ if (_j && !_j.done && (_c = _h.return)) _c.call(_h);
1964
+ }
1965
+ finally { if (e_3) throw e_3.error; }
1966
+ }
1967
+ return parameterNames;
1968
+ }
1969
+ /**
1970
+ * TODO: [🔣] If script require contentLanguage
1971
+ */
1972
+
1973
+ /**
1974
+ * Creates a new set with all elements that are present in either set
1975
+ *
1976
+ * @deprecated use new javascript set methods instead @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
1977
+ */
1978
+ function union() {
1979
+ var e_1, _a, e_2, _b;
1980
+ var sets = [];
1981
+ for (var _i = 0; _i < arguments.length; _i++) {
1982
+ sets[_i] = arguments[_i];
1983
+ }
1984
+ var union = new Set();
1985
+ try {
1986
+ for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
1987
+ var set = sets_1_1.value;
1988
+ try {
1989
+ for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
1990
+ var item = _d.value;
1991
+ union.add(item);
1992
+ }
1993
+ }
1994
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1995
+ finally {
1996
+ try {
1997
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
1998
+ }
1999
+ finally { if (e_2) throw e_2.error; }
2000
+ }
2001
+ }
2002
+ }
2003
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2004
+ finally {
2005
+ try {
2006
+ if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
2007
+ }
2008
+ finally { if (e_1) throw e_1.error; }
2009
+ }
2010
+ return union;
2011
+ }
2012
+
1742
2013
  /**
1743
2014
  * This error occurs when some expectation is not met in the execution of the pipeline
1744
2015
  *
@@ -2011,6 +2282,28 @@ function joinLlmExecutionTools() {
2011
2282
  return new (MultipleLlmExecutionTools.bind.apply(MultipleLlmExecutionTools, __spreadArray([void 0], __read(llmExecutionTools), false)))();
2012
2283
  }
2013
2284
 
2285
+ /**
2286
+ * Determine if the pipeline is fully prepared
2287
+ */
2288
+ function isPipelinePrepared(pipeline) {
2289
+ // Note: Ignoring `pipeline.preparations` @@@
2290
+ // Note: Ignoring `pipeline.knowledgePieces` @@@
2291
+ if (!pipeline.personas.every(function (persona) { return persona.modelRequirements !== undefined; })) {
2292
+ return false;
2293
+ }
2294
+ if (!pipeline.knowledgeSources.every(function (knowledgeSource) { return knowledgeSource.preparationIds !== undefined; })) {
2295
+ return false;
2296
+ }
2297
+ // TODO: !!!!! Is context in each template
2298
+ // TODO: !!!!! Are samples prepared
2299
+ // TODO: !!!!! Are templates prepared
2300
+ return true;
2301
+ }
2302
+ /**
2303
+ * TODO: [🔼] Export via core or utils
2304
+ * TODO: [🧊] Pipeline can be partially prepared, this should return true ONLY if fully prepared
2305
+ */
2306
+
2014
2307
  /**
2015
2308
  * Takes an item or an array of items and returns an array of items
2016
2309
  *
@@ -2031,9 +2324,98 @@ function arrayableToArray(input) {
2031
2324
  }
2032
2325
 
2033
2326
  /**
2034
- * The version of the Promptbook library
2327
+ * Just marks a place of place where should be something implemented
2328
+ * No side effects.
2329
+ *
2330
+ * Note: It can be usefull suppressing eslint errors of unused variables
2331
+ *
2332
+ * @param value any values
2333
+ * @returns void
2334
+ * @private within the repository
2035
2335
  */
2036
- var PROMPTBOOK_VERSION = '0.61.0-13';
2336
+ function TODO_USE() {
2337
+ var value = [];
2338
+ for (var _i = 0; _i < arguments.length; _i++) {
2339
+ value[_i] = arguments[_i];
2340
+ }
2341
+ }
2342
+
2343
+ /**
2344
+ * This error type indicates that some limit was reached
2345
+ */
2346
+ var LimitReachedError = /** @class */ (function (_super) {
2347
+ __extends(LimitReachedError, _super);
2348
+ function LimitReachedError(message) {
2349
+ var _this = _super.call(this, message) || this;
2350
+ _this.name = 'LimitReachedError';
2351
+ Object.setPrototypeOf(_this, LimitReachedError.prototype);
2352
+ return _this;
2353
+ }
2354
+ return LimitReachedError;
2355
+ }(Error));
2356
+
2357
+ /**
2358
+ * Replaces parameters in template with values from parameters object
2359
+ *
2360
+ * @param template the template with parameters in {curly} braces
2361
+ * @param parameters the object with parameters
2362
+ * @returns the template with replaced parameters
2363
+ * @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
2364
+ */
2365
+ function replaceParameters(template, parameters) {
2366
+ var replacedTemplate = template;
2367
+ var match;
2368
+ var loopLimit = LOOP_LIMIT;
2369
+ var _loop_1 = function () {
2370
+ if (loopLimit-- < 0) {
2371
+ throw new LimitReachedError('Loop limit reached during parameters replacement in `replaceParameters`');
2372
+ }
2373
+ var precol = match.groups.precol;
2374
+ var parameterName = match.groups.parameterName;
2375
+ if (parameterName === '') {
2376
+ return "continue";
2377
+ }
2378
+ if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
2379
+ throw new PipelineExecutionError('Parameter is already opened or not closed');
2380
+ }
2381
+ if (parameters[parameterName] === undefined) {
2382
+ throw new PipelineExecutionError("Parameter {".concat(parameterName, "} is not defined"));
2383
+ }
2384
+ var parameterValue = parameters[parameterName];
2385
+ if (parameterValue === undefined) {
2386
+ throw new PipelineExecutionError("Parameter {".concat(parameterName, "} is not defined"));
2387
+ }
2388
+ parameterValue = parameterValue.toString();
2389
+ if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
2390
+ parameterValue = parameterValue
2391
+ .split('\n')
2392
+ .map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
2393
+ .join('\n');
2394
+ }
2395
+ replacedTemplate =
2396
+ replacedTemplate.substring(0, match.index + precol.length) +
2397
+ parameterValue +
2398
+ replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
2399
+ };
2400
+ while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
2401
+ .exec(replacedTemplate))) {
2402
+ _loop_1();
2403
+ }
2404
+ // [💫] Check if there are parameters that are not closed properly
2405
+ if (/{\w+$/.test(replacedTemplate)) {
2406
+ throw new PipelineExecutionError('Parameter is not closed');
2407
+ }
2408
+ // [💫] Check if there are parameters that are not opened properly
2409
+ if (/^\w+}/.test(replacedTemplate)) {
2410
+ throw new PipelineExecutionError('Parameter is not opened');
2411
+ }
2412
+ return replacedTemplate;
2413
+ }
2414
+
2415
+ /**
2416
+ * The version of the Promptbook library
2417
+ */
2418
+ var PROMPTBOOK_VERSION = '0.61.0-15';
2037
2419
  // TODO: !!!! List here all the versions and annotate + put into script
2038
2420
 
2039
2421
  /**
@@ -2167,80 +2549,6 @@ function isPassingExpectations(expectations, value) {
2167
2549
  * TODO: [💝] Unite object for expecting amount and format
2168
2550
  */
2169
2551
 
2170
- /**
2171
- * This error type indicates that some limit was reached
2172
- */
2173
- var LimitReachedError = /** @class */ (function (_super) {
2174
- __extends(LimitReachedError, _super);
2175
- function LimitReachedError(message) {
2176
- var _this = _super.call(this, message) || this;
2177
- _this.name = 'LimitReachedError';
2178
- Object.setPrototypeOf(_this, LimitReachedError.prototype);
2179
- return _this;
2180
- }
2181
- return LimitReachedError;
2182
- }(Error));
2183
-
2184
- /**
2185
- * Replaces parameters in template with values from parameters object
2186
- *
2187
- * @param template the template with parameters in {curly} braces
2188
- * @param parameters the object with parameters
2189
- * @returns the template with replaced parameters
2190
- * @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
2191
- *
2192
- * @private within the createPipelineExecutor
2193
- */
2194
- function replaceParameters(template, parameters) {
2195
- var replacedTemplate = template;
2196
- var match;
2197
- var loopLimit = LOOP_LIMIT;
2198
- var _loop_1 = function () {
2199
- if (loopLimit-- < 0) {
2200
- throw new LimitReachedError('Loop limit reached during parameters replacement in `replaceParameters`');
2201
- }
2202
- var precol = match.groups.precol;
2203
- var parameterName = match.groups.parameterName;
2204
- if (parameterName === '') {
2205
- return "continue";
2206
- }
2207
- if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
2208
- throw new PipelineExecutionError('Parameter is already opened or not closed');
2209
- }
2210
- if (parameters[parameterName] === undefined) {
2211
- throw new PipelineExecutionError("Parameter {".concat(parameterName, "} is not defined"));
2212
- }
2213
- var parameterValue = parameters[parameterName];
2214
- if (parameterValue === undefined) {
2215
- throw new PipelineExecutionError("Parameter {".concat(parameterName, "} is not defined"));
2216
- }
2217
- parameterValue = parameterValue.toString();
2218
- if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
2219
- parameterValue = parameterValue
2220
- .split('\n')
2221
- .map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
2222
- .join('\n');
2223
- }
2224
- replacedTemplate =
2225
- replacedTemplate.substring(0, match.index + precol.length) +
2226
- parameterValue +
2227
- replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
2228
- };
2229
- while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
2230
- .exec(replacedTemplate))) {
2231
- _loop_1();
2232
- }
2233
- // [💫] Check if there are parameters that are not closed properly
2234
- if (/{\w+$/.test(replacedTemplate)) {
2235
- throw new PipelineExecutionError('Parameter is not closed');
2236
- }
2237
- // [💫] Check if there are parameters that are not opened properly
2238
- if (/^\w+}/.test(replacedTemplate)) {
2239
- throw new PipelineExecutionError('Parameter is not opened');
2240
- }
2241
- return replacedTemplate;
2242
- }
2243
-
2244
2552
  /**
2245
2553
  * Creates executor function from pipeline and execution tools.
2246
2554
  *
@@ -2249,18 +2557,73 @@ function replaceParameters(template, parameters) {
2249
2557
  */
2250
2558
  function createPipelineExecutor(options) {
2251
2559
  var _this = this;
2252
- var pipeline = options.pipeline, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
2253
- var _b = settings.maxExecutionAttempts, maxExecutionAttempts = _b === void 0 ? 3 : _b;
2254
- validatePipeline(pipeline);
2560
+ var rawPipeline = options.pipeline, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
2561
+ var _b = settings.maxExecutionAttempts, maxExecutionAttempts = _b === void 0 ? MAX_EXECUTION_ATTEMPTS : _b, _c = settings.maxParallelCount, maxParallelCount = _c === void 0 ? MAX_PARALLEL_COUNT : _c, _d = settings.isVerbose, isVerbose = _d === void 0 ? false : _d;
2562
+ validatePipeline(rawPipeline);
2255
2563
  var llmTools = joinLlmExecutionTools.apply(void 0, __spreadArray([], __read(arrayableToArray(tools.llm)), false));
2564
+ var pipeline;
2565
+ if (isPipelinePrepared(rawPipeline)) {
2566
+ pipeline = rawPipeline;
2567
+ }
2568
+ else {
2569
+ console.warn(spaceTrim$1("\n Pipeline is not prepared\n\n It will be prepared ad-hoc before the first execution\n But it is recommended to prepare the pipeline during collection preparation\n\n @see more at https://ptbk.io/prepare-pipeline\n "));
2570
+ }
2256
2571
  var pipelineExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
2572
+ function getContextForTemplate(// <- TODO: [🧠][🥜]
2573
+ template) {
2574
+ return __awaiter(this, void 0, void 0, function () {
2575
+ return __generator(this, function (_a) {
2576
+ // TODO: !!!!!! Implement Better - use real index and keyword search
2577
+ TODO_USE(template);
2578
+ return [2 /*return*/, pipeline.knowledgePieces.map(function (_a) {
2579
+ var content = _a.content;
2580
+ return "- ".concat(content);
2581
+ }).join('\n')];
2582
+ });
2583
+ });
2584
+ }
2585
+ function getReservedParametersForTemplate(template) {
2586
+ return __awaiter(this, void 0, void 0, function () {
2587
+ var context, currentDate, reservedParameters, RESERVED_PARAMETER_NAMES_1, RESERVED_PARAMETER_NAMES_1_1, parameterName;
2588
+ var e_3, _a;
2589
+ return __generator(this, function (_b) {
2590
+ switch (_b.label) {
2591
+ case 0: return [4 /*yield*/, getContextForTemplate(template)];
2592
+ case 1:
2593
+ context = _b.sent();
2594
+ currentDate = new Date().toISOString();
2595
+ reservedParameters = {
2596
+ context: context,
2597
+ currentDate: currentDate,
2598
+ };
2599
+ try {
2600
+ // Note: Doublecheck that ALL reserved parameters are defined:
2601
+ for (RESERVED_PARAMETER_NAMES_1 = __values(RESERVED_PARAMETER_NAMES), RESERVED_PARAMETER_NAMES_1_1 = RESERVED_PARAMETER_NAMES_1.next(); !RESERVED_PARAMETER_NAMES_1_1.done; RESERVED_PARAMETER_NAMES_1_1 = RESERVED_PARAMETER_NAMES_1.next()) {
2602
+ parameterName = RESERVED_PARAMETER_NAMES_1_1.value;
2603
+ if (reservedParameters[parameterName] === undefined) {
2604
+ throw new UnexpectedError("Reserved parameter {".concat(parameterName, "} is not defined"));
2605
+ }
2606
+ }
2607
+ }
2608
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
2609
+ finally {
2610
+ try {
2611
+ if (RESERVED_PARAMETER_NAMES_1_1 && !RESERVED_PARAMETER_NAMES_1_1.done && (_a = RESERVED_PARAMETER_NAMES_1.return)) _a.call(RESERVED_PARAMETER_NAMES_1);
2612
+ }
2613
+ finally { if (e_3) throw e_3.error; }
2614
+ }
2615
+ return [2 /*return*/, reservedParameters];
2616
+ }
2617
+ });
2618
+ });
2619
+ }
2257
2620
  function executeSingleTemplate(currentTemplate) {
2258
2621
  return __awaiter(this, void 0, void 0, function () {
2259
- var name, title, priority, prompt, chatResult, completionResult, embeddingResult, result, resultString, expectError, scriptPipelineExecutionErrors, maxAttempts, jokers, attempt, isJokerAttempt, joker, _a, _b, _c, _d, scriptTools, error_2, e_2_1, _e, _f, functionName, postprocessingError, _g, _h, scriptTools, error_3, e_3_1, e_4_1, error_4;
2260
- var e_2, _j, e_4, _k, e_3, _l, _m;
2622
+ var name, title, priority, usedParameterNames, dependentParameterNames, definedParameters, _a, _b, _c, definedParameterNames, parameters, _d, _e, parameterName, prompt, chatResult, completionResult, embeddingResult, result, resultString, expectError, scriptPipelineExecutionErrors, maxAttempts, jokerParameterNames, attempt, isJokerAttempt, jokerParameterName, _f, _g, _h, _j, scriptTools, error_2, e_4_1, _k, _l, functionName, postprocessingError, _m, _o, scriptTools, error_3, e_5_1, e_6_1, error_4;
2623
+ var e_7, _p, e_4, _q, e_6, _r, e_5, _s, _t;
2261
2624
  var _this = this;
2262
- return __generator(this, function (_o) {
2263
- switch (_o.label) {
2625
+ return __generator(this, function (_u) {
2626
+ switch (_u.label) {
2264
2627
  case 0:
2265
2628
  name = "pipeline-executor-frame-".concat(currentTemplate.name);
2266
2629
  title = currentTemplate.title;
@@ -2277,64 +2640,108 @@ function createPipelineExecutor(options) {
2277
2640
  // <- [3]
2278
2641
  })];
2279
2642
  case 1:
2280
- _o.sent();
2281
- _o.label = 2;
2643
+ _u.sent();
2644
+ _u.label = 2;
2282
2645
  case 2:
2646
+ usedParameterNames = extractParametersFromPromptTemplate(currentTemplate);
2647
+ dependentParameterNames = new Set(currentTemplate.dependentParameterNames);
2648
+ if (union(difference(usedParameterNames, dependentParameterNames), difference(dependentParameterNames, usedParameterNames)).size !== 0) {
2649
+ throw new UnexpectedError(spaceTrim$1("\n Dependent parameters are not consistent used parameters:\n\n Dependent parameters:\n ".concat(Array.from(dependentParameterNames).join(', '), "\n\n Used parameters:\n ").concat(Array.from(usedParameterNames).join(', '), "\n\n ")));
2650
+ }
2651
+ _b = (_a = Object).freeze;
2652
+ _c = [{}];
2653
+ return [4 /*yield*/, getReservedParametersForTemplate(currentTemplate)];
2654
+ case 3:
2655
+ definedParameters = _b.apply(_a, [__assign.apply(void 0, [__assign.apply(void 0, _c.concat([(_u.sent())])), parametersToPass])]);
2656
+ definedParameterNames = new Set(Object.keys(definedParameters));
2657
+ parameters = {};
2658
+ try {
2659
+ // Note: [2] Check that all used parameters are defined and removing unused parameters for this template
2660
+ for (_d = __values(Array.from(union(definedParameterNames, usedParameterNames, dependentParameterNames))), _e = _d.next(); !_e.done; _e = _d.next()) {
2661
+ parameterName = _e.value;
2662
+ // Situation: Parameter is defined and used
2663
+ if (definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
2664
+ parameters[parameterName] = definedParameters[parameterName];
2665
+ }
2666
+ // Situation: Parameter is defined but NOT used
2667
+ else if (definedParameterNames.has(parameterName) && !usedParameterNames.has(parameterName)) {
2668
+ // Do not pass this parameter to prompt
2669
+ }
2670
+ // Situation: Parameter is NOT defined BUT used
2671
+ else if (!definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
2672
+ // Houston, we have a problem
2673
+ // Note: Checking part is also done in `validatePipeline`, but it’s good to doublecheck
2674
+ throw new UnexpectedError(spaceTrim$1("\n Parameter {".concat(parameterName, "} is NOT defined\n BUT used in template \"").concat(currentTemplate.title || currentTemplate.name, "\"\n\n This should be catched in `validatePipeline`\n\n ")));
2675
+ }
2676
+ }
2677
+ }
2678
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
2679
+ finally {
2680
+ try {
2681
+ if (_e && !_e.done && (_p = _d.return)) _p.call(_d);
2682
+ }
2683
+ finally { if (e_7) throw e_7.error; }
2684
+ }
2685
+ // Note: Now we can freeze `parameters` because we are sure that all and only used parameters are defined
2686
+ Object.freeze(parameters);
2283
2687
  result = null;
2284
2688
  resultString = null;
2285
2689
  expectError = null;
2286
2690
  maxAttempts = currentTemplate.blockType === 'PROMPT_DIALOG' ? Infinity : maxExecutionAttempts;
2287
- jokers = currentTemplate.jokers || [];
2288
- attempt = -jokers.length;
2289
- _o.label = 3;
2290
- case 3:
2291
- if (!(attempt < maxAttempts)) return [3 /*break*/, 51];
2691
+ jokerParameterNames = currentTemplate.jokerParameterNames || [];
2692
+ attempt = -jokerParameterNames.length;
2693
+ _u.label = 4;
2694
+ case 4:
2695
+ if (!(attempt < maxAttempts)) return [3 /*break*/, 52];
2292
2696
  isJokerAttempt = attempt < 0;
2293
- joker = jokers[jokers.length + attempt];
2294
- if (isJokerAttempt && !joker) {
2697
+ jokerParameterName = jokerParameterNames[jokerParameterNames.length + attempt];
2698
+ if (isJokerAttempt && !jokerParameterName) {
2295
2699
  throw new UnexpectedError("Joker not found in attempt ".concat(attempt));
2296
2700
  }
2297
2701
  result = null;
2298
2702
  resultString = null;
2299
2703
  expectError = null;
2300
2704
  if (isJokerAttempt) {
2301
- if (typeof parametersToPass[joker] === 'undefined') {
2302
- throw new PipelineExecutionError("Joker parameter {".concat(joker, "} not defined"));
2705
+ if (parameters[jokerParameterName] === undefined) {
2706
+ throw new PipelineExecutionError("Joker parameter {".concat(jokerParameterName, "} not defined"));
2707
+ // <- TODO: This is maybe `PipelineLogicError` which should be detected in `validatePipeline` and here just thrown as `UnexpectedError`
2708
+ }
2709
+ else {
2710
+ resultString = parameters[jokerParameterName];
2303
2711
  }
2304
- resultString = parametersToPass[joker];
2305
- }
2306
- _o.label = 4;
2307
- case 4:
2308
- _o.trys.push([4, 47, 48, 49]);
2309
- if (!!isJokerAttempt) return [3 /*break*/, 29];
2310
- _a = currentTemplate.blockType;
2311
- switch (_a) {
2312
- case 'SIMPLE_TEMPLATE': return [3 /*break*/, 5];
2313
- case 'PROMPT_TEMPLATE': return [3 /*break*/, 6];
2314
- case 'SCRIPT': return [3 /*break*/, 15];
2315
- case 'PROMPT_DIALOG': return [3 /*break*/, 26];
2316
2712
  }
2317
- return [3 /*break*/, 28];
2713
+ _u.label = 5;
2318
2714
  case 5:
2319
- resultString = replaceParameters(currentTemplate.content, parametersToPass);
2715
+ _u.trys.push([5, 48, 49, 50]);
2716
+ if (!!isJokerAttempt) return [3 /*break*/, 30];
2717
+ _f = currentTemplate.blockType;
2718
+ switch (_f) {
2719
+ case 'SIMPLE_TEMPLATE': return [3 /*break*/, 6];
2720
+ case 'PROMPT_TEMPLATE': return [3 /*break*/, 7];
2721
+ case 'SCRIPT': return [3 /*break*/, 16];
2722
+ case 'PROMPT_DIALOG': return [3 /*break*/, 27];
2723
+ }
2320
2724
  return [3 /*break*/, 29];
2321
2725
  case 6:
2726
+ resultString = replaceParameters(currentTemplate.content, parameters);
2727
+ return [3 /*break*/, 30];
2728
+ case 7:
2322
2729
  prompt = {
2323
2730
  title: currentTemplate.title,
2324
2731
  pipelineUrl: "".concat(pipeline.pipelineUrl
2325
2732
  ? pipeline.pipelineUrl
2326
2733
  : 'anonymous' /* <- TODO: [🧠] How to deal with anonymous pipelines, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
2327
- parameters: parametersToPass,
2328
- content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
2329
- // <- TODO: !!!!! Apply {context} and knowledges
2330
- // <- TODO: !!!!! Apply samples
2734
+ parameters: parameters,
2735
+ content: currentTemplate.content,
2331
2736
  modelRequirements: currentTemplate.modelRequirements,
2332
- // <- TODO: !!!!! Apply persona
2333
- expectations: currentTemplate.expectations,
2737
+ expectations: __assign(__assign({}, (pipeline.personas.find(function (_a) {
2738
+ var name = _a.name;
2739
+ return name === currentTemplate.personaName;
2740
+ }) || {})), currentTemplate.expectations),
2334
2741
  expectFormat: currentTemplate.expectFormat,
2335
- postprocessing: (currentTemplate.postprocessing || []).map(function (functionName) { return function (result) { return __awaiter(_this, void 0, void 0, function () {
2336
- var errors, _a, _b, scriptTools, error_5, e_5_1;
2337
- var e_5, _c;
2742
+ postprocessing: (currentTemplate.postprocessingFunctionNames || []).map(function (functionName) { return function (result) { return __awaiter(_this, void 0, void 0, function () {
2743
+ var errors, _a, _b, scriptTools, error_5, e_8_1;
2744
+ var e_8, _c;
2338
2745
  return __generator(this, function (_d) {
2339
2746
  switch (_d.label) {
2340
2747
  case 0:
@@ -2355,7 +2762,7 @@ function createPipelineExecutor(options) {
2355
2762
  script: "".concat(functionName, "(result)"),
2356
2763
  parameters: {
2357
2764
  result: result || '',
2358
- // Note: No ...parametersToPass, because working with result only
2765
+ // Note: No ...parametersForTemplate, because working with result only
2359
2766
  },
2360
2767
  })];
2361
2768
  case 4: return [2 /*return*/, _d.sent()];
@@ -2364,6 +2771,9 @@ function createPipelineExecutor(options) {
2364
2771
  if (!(error_5 instanceof Error)) {
2365
2772
  throw error_5;
2366
2773
  }
2774
+ if (error_5 instanceof UnexpectedError) {
2775
+ throw error_5;
2776
+ }
2367
2777
  errors.push(error_5);
2368
2778
  return [3 /*break*/, 6];
2369
2779
  case 6:
@@ -2371,14 +2781,14 @@ function createPipelineExecutor(options) {
2371
2781
  return [3 /*break*/, 2];
2372
2782
  case 7: return [3 /*break*/, 10];
2373
2783
  case 8:
2374
- e_5_1 = _d.sent();
2375
- e_5 = { error: e_5_1 };
2784
+ e_8_1 = _d.sent();
2785
+ e_8 = { error: e_8_1 };
2376
2786
  return [3 /*break*/, 10];
2377
2787
  case 9:
2378
2788
  try {
2379
2789
  if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
2380
2790
  }
2381
- finally { if (e_5) throw e_5.error; }
2791
+ finally { if (e_8) throw e_8.error; }
2382
2792
  return [7 /*endfinally*/];
2383
2793
  case 10:
2384
2794
  if (errors.length === 0) {
@@ -2394,35 +2804,35 @@ function createPipelineExecutor(options) {
2394
2804
  });
2395
2805
  }); }; }),
2396
2806
  };
2397
- _b = currentTemplate.modelRequirements.modelVariant;
2398
- switch (_b) {
2399
- case 'CHAT': return [3 /*break*/, 7];
2400
- case 'COMPLETION': return [3 /*break*/, 9];
2401
- case 'EMBEDDING': return [3 /*break*/, 11];
2807
+ _g = currentTemplate.modelRequirements.modelVariant;
2808
+ switch (_g) {
2809
+ case 'CHAT': return [3 /*break*/, 8];
2810
+ case 'COMPLETION': return [3 /*break*/, 10];
2811
+ case 'EMBEDDING': return [3 /*break*/, 12];
2402
2812
  }
2403
- return [3 /*break*/, 13];
2404
- case 7: return [4 /*yield*/, llmTools.callChatModel(prompt)];
2405
- case 8:
2406
- chatResult = _o.sent();
2813
+ return [3 /*break*/, 14];
2814
+ case 8: return [4 /*yield*/, llmTools.callChatModel(deepFreeze(prompt))];
2815
+ case 9:
2816
+ chatResult = _u.sent();
2407
2817
  // TODO: [🍬] Destroy chatThread
2408
2818
  result = chatResult;
2409
2819
  resultString = chatResult.content;
2410
- return [3 /*break*/, 14];
2411
- case 9: return [4 /*yield*/, llmTools.callCompletionModel(prompt)];
2412
- case 10:
2413
- completionResult = _o.sent();
2820
+ return [3 /*break*/, 15];
2821
+ case 10: return [4 /*yield*/, llmTools.callCompletionModel(deepFreeze(prompt))];
2822
+ case 11:
2823
+ completionResult = _u.sent();
2414
2824
  result = completionResult;
2415
2825
  resultString = completionResult.content;
2416
- return [3 /*break*/, 14];
2417
- case 11: return [4 /*yield*/, llmTools.callEmbeddingModel(prompt)];
2418
- case 12:
2419
- embeddingResult = _o.sent();
2826
+ return [3 /*break*/, 15];
2827
+ case 12: return [4 /*yield*/, llmTools.callEmbeddingModel(deepFreeze(prompt))];
2828
+ case 13:
2829
+ embeddingResult = _u.sent();
2420
2830
  result = embeddingResult;
2421
2831
  resultString = embeddingResult.content.join(',');
2422
- return [3 /*break*/, 14];
2423
- case 13: throw new PipelineExecutionError("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
2424
- case 14: return [3 /*break*/, 29];
2425
- case 15:
2832
+ return [3 /*break*/, 15];
2833
+ case 14: throw new PipelineExecutionError("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
2834
+ case 15: return [3 /*break*/, 30];
2835
+ case 16:
2426
2836
  if (arrayableToArray(tools.script).length === 0) {
2427
2837
  throw new PipelineExecutionError('No script execution tools are available');
2428
2838
  }
@@ -2431,49 +2841,52 @@ function createPipelineExecutor(options) {
2431
2841
  }
2432
2842
  // TODO: DRY [1]
2433
2843
  scriptPipelineExecutionErrors = [];
2434
- _o.label = 16;
2435
- case 16:
2436
- _o.trys.push([16, 23, 24, 25]);
2437
- _c = (e_2 = void 0, __values(arrayableToArray(tools.script))), _d = _c.next();
2438
- _o.label = 17;
2844
+ _u.label = 17;
2439
2845
  case 17:
2440
- if (!!_d.done) return [3 /*break*/, 22];
2441
- scriptTools = _d.value;
2442
- _o.label = 18;
2846
+ _u.trys.push([17, 24, 25, 26]);
2847
+ _h = (e_4 = void 0, __values(arrayableToArray(tools.script))), _j = _h.next();
2848
+ _u.label = 18;
2443
2849
  case 18:
2444
- _o.trys.push([18, 20, , 21]);
2445
- return [4 /*yield*/, scriptTools.execute({
2850
+ if (!!_j.done) return [3 /*break*/, 23];
2851
+ scriptTools = _j.value;
2852
+ _u.label = 19;
2853
+ case 19:
2854
+ _u.trys.push([19, 21, , 22]);
2855
+ return [4 /*yield*/, scriptTools.execute(deepFreeze({
2446
2856
  scriptLanguage: currentTemplate.contentLanguage,
2447
2857
  script: currentTemplate.content,
2448
- parameters: parametersToPass,
2449
- })];
2450
- case 19:
2451
- resultString = _o.sent();
2452
- return [3 /*break*/, 22];
2858
+ parameters: parameters,
2859
+ }))];
2453
2860
  case 20:
2454
- error_2 = _o.sent();
2861
+ resultString = _u.sent();
2862
+ return [3 /*break*/, 23];
2863
+ case 21:
2864
+ error_2 = _u.sent();
2455
2865
  if (!(error_2 instanceof Error)) {
2456
2866
  throw error_2;
2457
2867
  }
2868
+ if (error_2 instanceof UnexpectedError) {
2869
+ throw error_2;
2870
+ }
2458
2871
  scriptPipelineExecutionErrors.push(error_2);
2459
- return [3 /*break*/, 21];
2460
- case 21:
2461
- _d = _c.next();
2462
- return [3 /*break*/, 17];
2463
- case 22: return [3 /*break*/, 25];
2464
- case 23:
2465
- e_2_1 = _o.sent();
2466
- e_2 = { error: e_2_1 };
2467
- return [3 /*break*/, 25];
2872
+ return [3 /*break*/, 22];
2873
+ case 22:
2874
+ _j = _h.next();
2875
+ return [3 /*break*/, 18];
2876
+ case 23: return [3 /*break*/, 26];
2468
2877
  case 24:
2878
+ e_4_1 = _u.sent();
2879
+ e_4 = { error: e_4_1 };
2880
+ return [3 /*break*/, 26];
2881
+ case 25:
2469
2882
  try {
2470
- if (_d && !_d.done && (_j = _c.return)) _j.call(_c);
2883
+ if (_j && !_j.done && (_q = _h.return)) _q.call(_h);
2471
2884
  }
2472
- finally { if (e_2) throw e_2.error; }
2885
+ finally { if (e_4) throw e_4.error; }
2473
2886
  return [7 /*endfinally*/];
2474
- case 25:
2887
+ case 26:
2475
2888
  if (resultString !== null) {
2476
- return [3 /*break*/, 29];
2889
+ return [3 /*break*/, 30];
2477
2890
  }
2478
2891
  if (scriptPipelineExecutionErrors.length === 1) {
2479
2892
  throw scriptPipelineExecutionErrors[0];
@@ -2483,101 +2896,104 @@ function createPipelineExecutor(options) {
2483
2896
  .map(function (error) { return '- ' + error.message; })
2484
2897
  .join('\n\n')), "\n "); }));
2485
2898
  }
2486
- case 26:
2899
+ case 27:
2487
2900
  if (tools.userInterface === undefined) {
2488
2901
  throw new PipelineExecutionError('User interface tools are not available');
2489
2902
  }
2490
- return [4 /*yield*/, tools.userInterface.promptDialog({
2903
+ return [4 /*yield*/, tools.userInterface.promptDialog(deepFreeze({
2491
2904
  promptTitle: currentTemplate.title,
2492
- promptMessage: replaceParameters(currentTemplate.description || '', parametersToPass),
2493
- defaultValue: replaceParameters(currentTemplate.content, parametersToPass),
2905
+ promptMessage: replaceParameters(currentTemplate.description || '', parameters),
2906
+ defaultValue: replaceParameters(currentTemplate.content, parameters),
2494
2907
  // TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
2495
2908
  placeholder: undefined,
2496
2909
  priority: priority,
2497
- })];
2498
- case 27:
2910
+ }))];
2911
+ case 28:
2499
2912
  // TODO: [🌹] When making next attempt for `PROMPT DIALOG`, preserve the previous user input
2500
- resultString = _o.sent();
2501
- return [3 /*break*/, 29];
2502
- case 28: throw new PipelineExecutionError("Unknown execution type \"".concat(currentTemplate.blockType, "\""));
2503
- case 29:
2504
- if (!(!isJokerAttempt && currentTemplate.postprocessing)) return [3 /*break*/, 46];
2505
- _o.label = 30;
2913
+ resultString = _u.sent();
2914
+ return [3 /*break*/, 30];
2915
+ case 29: throw new PipelineExecutionError("Unknown execution type \"".concat(currentTemplate.blockType, "\""));
2506
2916
  case 30:
2507
- _o.trys.push([30, 44, 45, 46]);
2508
- _e = (e_4 = void 0, __values(currentTemplate.postprocessing)), _f = _e.next();
2509
- _o.label = 31;
2917
+ if (!(!isJokerAttempt && currentTemplate.postprocessingFunctionNames)) return [3 /*break*/, 47];
2918
+ _u.label = 31;
2510
2919
  case 31:
2511
- if (!!_f.done) return [3 /*break*/, 43];
2512
- functionName = _f.value;
2920
+ _u.trys.push([31, 45, 46, 47]);
2921
+ _k = (e_6 = void 0, __values(currentTemplate.postprocessingFunctionNames)), _l = _k.next();
2922
+ _u.label = 32;
2923
+ case 32:
2924
+ if (!!_l.done) return [3 /*break*/, 44];
2925
+ functionName = _l.value;
2513
2926
  // TODO: DRY [1]
2514
2927
  scriptPipelineExecutionErrors = [];
2515
2928
  postprocessingError = null;
2516
- _o.label = 32;
2517
- case 32:
2518
- _o.trys.push([32, 39, 40, 41]);
2519
- _g = (e_3 = void 0, __values(arrayableToArray(tools.script))), _h = _g.next();
2520
- _o.label = 33;
2929
+ _u.label = 33;
2521
2930
  case 33:
2522
- if (!!_h.done) return [3 /*break*/, 38];
2523
- scriptTools = _h.value;
2524
- _o.label = 34;
2931
+ _u.trys.push([33, 40, 41, 42]);
2932
+ _m = (e_5 = void 0, __values(arrayableToArray(tools.script))), _o = _m.next();
2933
+ _u.label = 34;
2525
2934
  case 34:
2526
- _o.trys.push([34, 36, , 37]);
2935
+ if (!!_o.done) return [3 /*break*/, 39];
2936
+ scriptTools = _o.value;
2937
+ _u.label = 35;
2938
+ case 35:
2939
+ _u.trys.push([35, 37, , 38]);
2527
2940
  return [4 /*yield*/, scriptTools.execute({
2528
2941
  scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
2529
2942
  script: "".concat(functionName, "(resultString)"),
2530
2943
  parameters: {
2531
2944
  resultString: resultString || '',
2532
- // Note: No ...parametersToPass, because working with result only
2945
+ // Note: No ...parametersForTemplate, because working with result only
2533
2946
  },
2534
2947
  })];
2535
- case 35:
2536
- resultString = _o.sent();
2537
- postprocessingError = null;
2538
- return [3 /*break*/, 38];
2539
2948
  case 36:
2540
- error_3 = _o.sent();
2949
+ resultString = _u.sent();
2950
+ postprocessingError = null;
2951
+ return [3 /*break*/, 39];
2952
+ case 37:
2953
+ error_3 = _u.sent();
2541
2954
  if (!(error_3 instanceof Error)) {
2542
2955
  throw error_3;
2543
2956
  }
2957
+ if (error_3 instanceof UnexpectedError) {
2958
+ throw error_3;
2959
+ }
2544
2960
  postprocessingError = error_3;
2545
2961
  scriptPipelineExecutionErrors.push(error_3);
2546
- return [3 /*break*/, 37];
2547
- case 37:
2548
- _h = _g.next();
2549
- return [3 /*break*/, 33];
2550
- case 38: return [3 /*break*/, 41];
2551
- case 39:
2552
- e_3_1 = _o.sent();
2553
- e_3 = { error: e_3_1 };
2554
- return [3 /*break*/, 41];
2962
+ return [3 /*break*/, 38];
2963
+ case 38:
2964
+ _o = _m.next();
2965
+ return [3 /*break*/, 34];
2966
+ case 39: return [3 /*break*/, 42];
2555
2967
  case 40:
2968
+ e_5_1 = _u.sent();
2969
+ e_5 = { error: e_5_1 };
2970
+ return [3 /*break*/, 42];
2971
+ case 41:
2556
2972
  try {
2557
- if (_h && !_h.done && (_l = _g.return)) _l.call(_g);
2973
+ if (_o && !_o.done && (_s = _m.return)) _s.call(_m);
2558
2974
  }
2559
- finally { if (e_3) throw e_3.error; }
2975
+ finally { if (e_5) throw e_5.error; }
2560
2976
  return [7 /*endfinally*/];
2561
- case 41:
2977
+ case 42:
2562
2978
  if (postprocessingError) {
2563
2979
  throw postprocessingError;
2564
2980
  }
2565
- _o.label = 42;
2566
- case 42:
2567
- _f = _e.next();
2568
- return [3 /*break*/, 31];
2569
- case 43: return [3 /*break*/, 46];
2570
- case 44:
2571
- e_4_1 = _o.sent();
2572
- e_4 = { error: e_4_1 };
2573
- return [3 /*break*/, 46];
2981
+ _u.label = 43;
2982
+ case 43:
2983
+ _l = _k.next();
2984
+ return [3 /*break*/, 32];
2985
+ case 44: return [3 /*break*/, 47];
2574
2986
  case 45:
2987
+ e_6_1 = _u.sent();
2988
+ e_6 = { error: e_6_1 };
2989
+ return [3 /*break*/, 47];
2990
+ case 46:
2575
2991
  try {
2576
- if (_f && !_f.done && (_k = _e.return)) _k.call(_e);
2992
+ if (_l && !_l.done && (_r = _k.return)) _r.call(_k);
2577
2993
  }
2578
- finally { if (e_4) throw e_4.error; }
2994
+ finally { if (e_6) throw e_6.error; }
2579
2995
  return [7 /*endfinally*/];
2580
- case 46:
2996
+ case 47:
2581
2997
  // TODO: [💝] Unite object for expecting amount and format
2582
2998
  if (currentTemplate.expectFormat) {
2583
2999
  if (currentTemplate.expectFormat === 'JSON') {
@@ -2590,15 +3006,18 @@ function createPipelineExecutor(options) {
2590
3006
  if (currentTemplate.expectations) {
2591
3007
  checkExpectations(currentTemplate.expectations, resultString || '');
2592
3008
  }
2593
- return [3 /*break*/, 51];
2594
- case 47:
2595
- error_4 = _o.sent();
3009
+ return [3 /*break*/, 52];
3010
+ case 48:
3011
+ error_4 = _u.sent();
2596
3012
  if (!(error_4 instanceof ExpectError)) {
2597
3013
  throw error_4;
2598
3014
  }
3015
+ if (error_4 instanceof UnexpectedError) {
3016
+ throw error_4;
3017
+ }
2599
3018
  expectError = error_4;
2600
- return [3 /*break*/, 49];
2601
- case 48:
3019
+ return [3 /*break*/, 50];
3020
+ case 49:
2602
3021
  if (!isJokerAttempt &&
2603
3022
  currentTemplate.blockType === 'PROMPT_TEMPLATE' &&
2604
3023
  prompt
@@ -2620,15 +3039,15 @@ function createPipelineExecutor(options) {
2620
3039
  });
2621
3040
  }
2622
3041
  return [7 /*endfinally*/];
2623
- case 49:
3042
+ case 50:
2624
3043
  if (expectError !== null && attempt === maxAttempts - 1) {
2625
3044
  throw new PipelineExecutionError(spaceTrim$1(function (block) { return "\n LLM execution failed ".concat(maxExecutionAttempts, "x\n\n ---\n Last error ").concat((expectError === null || expectError === void 0 ? void 0 : expectError.name) || '', ":\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n\n Last result:\n ").concat(resultString, "\n ---\n "); }));
2626
3045
  }
2627
- _o.label = 50;
2628
- case 50:
2629
- attempt++;
2630
- return [3 /*break*/, 3];
3046
+ _u.label = 51;
2631
3047
  case 51:
3048
+ attempt++;
3049
+ return [3 /*break*/, 4];
3050
+ case 52:
2632
3051
  if (resultString === null) {
2633
3052
  throw new UnexpectedError('Something went wrong and prompt result is null');
2634
3053
  }
@@ -2644,18 +3063,27 @@ function createPipelineExecutor(options) {
2644
3063
  // <- [3]
2645
3064
  });
2646
3065
  }
2647
- parametersToPass = __assign(__assign({}, parametersToPass), (_m = {}, _m[currentTemplate.resultingParameterName] = resultString /* <- Note: Not need to detect parameter collision here because pipeline checks logic consistency during construction */, _m));
3066
+ parametersToPass = Object.freeze(__assign(__assign({}, parametersToPass), (_t = {}, _t[currentTemplate.resultingParameterName] = resultString /* <- Note: Not need to detect parameter collision here because pipeline checks logic consistency during construction */, _t)));
2648
3067
  return [2 /*return*/];
2649
3068
  }
2650
3069
  });
2651
3070
  });
2652
3071
  }
2653
- var parametersToPass, executionReport, resovedParameters_1, unresovedTemplates, resolving_1, loopLimit, _loop_1, error_1, usage_1, _a, _b, parameter, usage;
2654
- var e_1, _c;
2655
- return __generator(this, function (_d) {
2656
- switch (_d.label) {
3072
+ var executionReport, _a, _b, parameter, parametersToPass, resovedParameters_1, unresovedTemplates, resolving_1, loopLimit, _loop_1, error_1, usage_1, outputParameters, errors, _c, _d, parameter, usage;
3073
+ var e_1, _e, e_2, _f;
3074
+ return __generator(this, function (_g) {
3075
+ switch (_g.label) {
2657
3076
  case 0:
2658
- parametersToPass = inputParameters;
3077
+ if (!(pipeline === undefined)) return [3 /*break*/, 2];
3078
+ return [4 /*yield*/, preparePipeline(rawPipeline, {
3079
+ llmTools: llmTools,
3080
+ isVerbose: isVerbose,
3081
+ maxParallelCount: maxParallelCount,
3082
+ })];
3083
+ case 1:
3084
+ pipeline = _g.sent();
3085
+ _g.label = 2;
3086
+ case 2:
2659
3087
  executionReport = {
2660
3088
  pipelineUrl: pipeline.pipelineUrl,
2661
3089
  title: pipeline.title,
@@ -2664,9 +3092,35 @@ function createPipelineExecutor(options) {
2664
3092
  description: pipeline.description,
2665
3093
  promptExecutions: [],
2666
3094
  };
2667
- _d.label = 1;
2668
- case 1:
2669
- _d.trys.push([1, 6, , 7]);
3095
+ try {
3096
+ // Note: Check that all input input parameters are defined
3097
+ for (_a = __values(pipeline.parameters), _b = _a.next(); !_b.done; _b = _a.next()) {
3098
+ parameter = _b.value;
3099
+ if (parameter.isInput && inputParameters[parameter.name] === undefined) {
3100
+ return [2 /*return*/, deepFreezeWithSameType({
3101
+ isSuccessful: false,
3102
+ errors: [
3103
+ new PipelineExecutionError("Parameter {".concat(parameter.name, "} is required as an input parameter")),
3104
+ // <- TODO: !!!!! Test this error
3105
+ ],
3106
+ executionReport: executionReport,
3107
+ outputParameters: {},
3108
+ usage: ZERO_USAGE,
3109
+ })];
3110
+ }
3111
+ }
3112
+ }
3113
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
3114
+ finally {
3115
+ try {
3116
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
3117
+ }
3118
+ finally { if (e_1) throw e_1.error; }
3119
+ }
3120
+ parametersToPass = inputParameters;
3121
+ _g.label = 3;
3122
+ case 3:
3123
+ _g.trys.push([3, 8, , 9]);
2670
3124
  resovedParameters_1 = pipeline.parameters
2671
3125
  .filter(function (_a) {
2672
3126
  var isInput = _a.isInput;
@@ -2681,8 +3135,8 @@ function createPipelineExecutor(options) {
2681
3135
  loopLimit = LOOP_LIMIT;
2682
3136
  _loop_1 = function () {
2683
3137
  var currentTemplate, work_1;
2684
- return __generator(this, function (_e) {
2685
- switch (_e.label) {
3138
+ return __generator(this, function (_h) {
3139
+ switch (_h.label) {
2686
3140
  case 0:
2687
3141
  if (loopLimit-- < 0) {
2688
3142
  // Note: Really UnexpectedError not LimitReachedError - this should be catched during validatePipeline
@@ -2697,7 +3151,7 @@ function createPipelineExecutor(options) {
2697
3151
  if (!!currentTemplate) return [3 /*break*/, 3];
2698
3152
  /* [5] */ return [4 /*yield*/, Promise.race(resolving_1)];
2699
3153
  case 2:
2700
- /* [5] */ _e.sent();
3154
+ /* [5] */ _h.sent();
2701
3155
  return [3 /*break*/, 4];
2702
3156
  case 3:
2703
3157
  unresovedTemplates = unresovedTemplates.filter(function (template) { return template !== currentTemplate; });
@@ -2709,24 +3163,24 @@ function createPipelineExecutor(options) {
2709
3163
  resolving_1 = resolving_1.filter(function (w) { return w !== work_1; });
2710
3164
  });
2711
3165
  resolving_1.push(work_1);
2712
- _e.label = 4;
3166
+ _h.label = 4;
2713
3167
  case 4: return [2 /*return*/];
2714
3168
  }
2715
3169
  });
2716
3170
  };
2717
- _d.label = 2;
2718
- case 2:
2719
- if (!(unresovedTemplates.length > 0)) return [3 /*break*/, 4];
3171
+ _g.label = 4;
3172
+ case 4:
3173
+ if (!(unresovedTemplates.length > 0)) return [3 /*break*/, 6];
2720
3174
  return [5 /*yield**/, _loop_1()];
2721
- case 3:
2722
- _d.sent();
2723
- return [3 /*break*/, 2];
2724
- case 4: return [4 /*yield*/, Promise.all(resolving_1)];
2725
3175
  case 5:
2726
- _d.sent();
2727
- return [3 /*break*/, 7];
2728
- case 6:
2729
- error_1 = _d.sent();
3176
+ _g.sent();
3177
+ return [3 /*break*/, 4];
3178
+ case 6: return [4 /*yield*/, Promise.all(resolving_1)];
3179
+ case 7:
3180
+ _g.sent();
3181
+ return [3 /*break*/, 9];
3182
+ case 8:
3183
+ error_1 = _g.sent();
2730
3184
  if (!(error_1 instanceof Error)) {
2731
3185
  throw error_1;
2732
3186
  }
@@ -2734,73 +3188,63 @@ function createPipelineExecutor(options) {
2734
3188
  var result = _a.result;
2735
3189
  return (result === null || result === void 0 ? void 0 : result.usage) || ZERO_USAGE;
2736
3190
  })), false));
2737
- return [2 /*return*/, {
3191
+ return [2 /*return*/, deepFreezeWithSameType({
2738
3192
  isSuccessful: false,
2739
3193
  errors: [error_1],
2740
3194
  usage: usage_1,
2741
3195
  executionReport: executionReport,
2742
3196
  outputParameters: parametersToPass,
2743
- }];
2744
- case 7:
3197
+ })];
3198
+ case 9:
3199
+ outputParameters = {};
3200
+ errors = [];
2745
3201
  try {
2746
3202
  // Note: Filter ONLY output parameters
2747
- for (_a = __values(pipeline.parameters), _b = _a.next(); !_b.done; _b = _a.next()) {
2748
- parameter = _b.value;
2749
- if (parameter.isOutput) {
3203
+ for (_c = __values(pipeline.parameters.filter(function (_a) {
3204
+ var isOutput = _a.isOutput;
3205
+ return isOutput;
3206
+ })), _d = _c.next(); !_d.done; _d = _c.next()) {
3207
+ parameter = _d.value;
3208
+ if (parametersToPass[parameter.name] === undefined) {
3209
+ errors.push(new PipelineExecutionError("Parameter {".concat(parameter.name, "} is required as an output parameter but not set in the pipeline")));
2750
3210
  continue;
2751
3211
  }
2752
- delete parametersToPass[parameter.name];
3212
+ outputParameters[parameter.name] = parametersToPass[parameter.name] || '';
2753
3213
  }
2754
3214
  }
2755
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
3215
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
2756
3216
  finally {
2757
3217
  try {
2758
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
3218
+ if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
2759
3219
  }
2760
- finally { if (e_1) throw e_1.error; }
3220
+ finally { if (e_2) throw e_2.error; }
2761
3221
  }
2762
3222
  usage = addUsage.apply(void 0, __spreadArray([], __read(executionReport.promptExecutions.map(function (_a) {
2763
3223
  var result = _a.result;
2764
3224
  return (result === null || result === void 0 ? void 0 : result.usage) || ZERO_USAGE;
2765
3225
  })), false));
2766
- return [2 /*return*/, {
3226
+ return [2 /*return*/, deepFreezeWithSameType({
2767
3227
  isSuccessful: true,
2768
- errors: [],
3228
+ errors: errors,
2769
3229
  usage: usage,
2770
3230
  executionReport: executionReport,
2771
- outputParameters: parametersToPass,
2772
- }];
3231
+ outputParameters: outputParameters,
3232
+ })];
2773
3233
  }
2774
3234
  });
2775
3235
  }); };
2776
3236
  return pipelineExecutor;
2777
3237
  }
2778
3238
  /**
2779
- * TODO: [🪂] Pass maxParallelCount here
3239
+ * TODO: Use isVerbose here (not only pass to `preparePipeline`)
3240
+ * TODO: [🪂] Use maxParallelCount here (not only pass to `preparePipeline`)
2780
3241
  * TODO: [♈] Probbably move expectations from templates to parameters
2781
3242
  * TODO: [🧠] When not meet expectations in PROMPT_DIALOG, make some way to tell the user
2782
3243
  * TODO: [👧] Strongly type the executors to avoid need of remove nullables whtn noUncheckedIndexedAccess in tsconfig.json
2783
3244
  * Note: CreatePipelineExecutorOptions are just connected to PipelineExecutor so do not extract to types folder
2784
3245
  * TODO: [🧠][3] transparent = (report intermediate parameters) / opaque execution = (report only output parameters) progress reporting mode
2785
3246
  * TODO: [🛠] Actions, instruments (and maybe knowledge) => Functions and tools
2786
- */
2787
-
2788
- /**
2789
- * Just marks a place of place where should be something implemented
2790
- * No side effects.
2791
- *
2792
- * Note: It can be usefull suppressing eslint errors of unused variables
2793
- *
2794
- * @param value any values
2795
- * @returns void
2796
- * @private within the repository
2797
- */
2798
- function TODO_USE() {
2799
- var value = [];
2800
- for (var _i = 0; _i < arguments.length; _i++) {
2801
- value[_i] = arguments[_i];
2802
- }
2803
- }
3247
+ */
2804
3248
 
2805
3249
  /**
2806
3250
  * @@@
@@ -2929,7 +3373,7 @@ function prepareKnowledgeFromMarkdown(content /* <- TODO: [🖖] (?maybe not) Al
2929
3373
  });
2930
3374
  }
2931
3375
  /**
2932
- * TODO: [🔼] !!! Export via `@promptbook/markdown`
3376
+ * TODO: [🐝][🔼] !!! Export via `@promptbook/markdown`
2933
3377
  * TODO: [🪂] Do it in parallel 11:11
2934
3378
  * Note: No need to aggregate usage here, it is done by intercepting the llmTools
2935
3379
  */
@@ -2952,7 +3396,7 @@ function prepareKnowledgePieces(knowledgeSources, options) {
2952
3396
  var partialPieces, pieces;
2953
3397
  return __generator(this, function (_a) {
2954
3398
  switch (_a.label) {
2955
- case 0: return [4 /*yield*/, prepareKnowledgeFromMarkdown(knowledgeSource.source, // <- TODO: [🐝] !!!!! Unhardcode markdown, detect which type it is
3399
+ case 0: return [4 /*yield*/, prepareKnowledgeFromMarkdown(knowledgeSource.source, // <- TODO: [🐝] !!! Unhardcode markdown, detect which type it is
2956
3400
  options)];
2957
3401
  case 1:
2958
3402
  partialPieces = _a.sent();
@@ -2990,7 +3434,7 @@ TODO: [🧊] This is how it can look in future
2990
3434
  > ):
2991
3435
  */
2992
3436
  /**
2993
- * TODO: [🔼] !!! Export via `@promptbook/core`
3437
+ * TODO: [🐝][🔼] !!! Export via `@promptbook/core`
2994
3438
  * TODO: [🧊] In future one preparation can take data from previous preparation and save tokens and time
2995
3439
  * Put `knowledgePieces` into `PrepareKnowledgeOptions`
2996
3440
  * TODO: [🪂] More than max things can run in parallel by acident [1,[2a,2b,_],[3a,3b,_]]
@@ -3116,12 +3560,15 @@ function preparePipeline(pipeline, options) {
3116
3560
  partialknowledgePiecesPrepared = _b.sent();
3117
3561
  knowledgePiecesPrepared = partialknowledgePiecesPrepared.map(function (piece) { return (__assign(__assign({}, piece), { preparationIds: [/* TODO: [🧊] -> */ currentPreparation.id] })); });
3118
3562
  // ----- /Knowledge preparation -----
3563
+ // TODO: !!!!! Add context to each template (if missing)
3564
+ // TODO: !!!!! Apply samples to each template (if missing)
3119
3565
  return [2 /*return*/, __assign(__assign({}, pipeline), { knowledgeSources: knowledgeSourcesPrepared, knowledgePieces: knowledgePiecesPrepared, personas: preparedPersonas, preparations: preparations })];
3120
3566
  }
3121
3567
  });
3122
3568
  });
3123
3569
  }
3124
3570
  /**
3571
+ * TODO: !!!!! Index the samples and maybe templates
3125
3572
  * TODO: [🔼] !!! Export via `@promptbook/core`
3126
3573
  * TODO: Write tests for `preparePipeline`
3127
3574
  * TODO: [🏏] Leverage the batch API and build queues @see https://platform.openai.com/docs/guides/batch
@@ -3177,7 +3624,7 @@ var knowledgeCommandParser = {
3177
3624
  /**
3178
3625
  * Link to discussion
3179
3626
  */
3180
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/41',
3627
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/41',
3181
3628
  /**
3182
3629
  * Example usages of the KNOWLEDGE command
3183
3630
  */
@@ -3214,8 +3661,9 @@ var knowledgeCommandParser = {
3214
3661
  /**
3215
3662
  * Note: Prototype of [🍧] (remove this comment after full implementation)
3216
3663
  */
3217
- applyToPipelineJson: function (pipelineJson, personaCommand) {
3664
+ applyToPipelineJson: function (personaCommand, subjects) {
3218
3665
  var source = personaCommand.source;
3666
+ var pipelineJson = subjects.pipelineJson;
3219
3667
  var name = titleToName(source);
3220
3668
  pipelineJson.knowledgeSources.push({
3221
3669
  name: name,
@@ -3250,7 +3698,7 @@ var personaCommandParser = {
3250
3698
  /**
3251
3699
  * Link to discussion
3252
3700
  */
3253
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/22',
3701
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/22',
3254
3702
  /**
3255
3703
  * Example usages of the PERSONA command
3256
3704
  */
@@ -3278,8 +3726,15 @@ var personaCommandParser = {
3278
3726
  /**
3279
3727
  * Note: Prototype of [🍧] (remove this comment after full implementation)
3280
3728
  */
3281
- applyToPipelineJson: function (pipelineJson, personaCommand) {
3729
+ applyToPipelineJson: function (personaCommand, subjects) {
3282
3730
  var personaName = personaCommand.personaName, personaDescription = personaCommand.personaDescription;
3731
+ var pipelineJson = subjects.pipelineJson, templateJson = subjects.templateJson;
3732
+ if (templateJson !== null) {
3733
+ if (templateJson.blockType !== 'PROMPT_TEMPLATE') {
3734
+ throw new ParsingError("PERSONA command can be used only in PROMPT_TEMPLATE block");
3735
+ }
3736
+ templateJson.personaName = personaName;
3737
+ }
3283
3738
  var persona = pipelineJson.personas.find(function (persona) { return persona.name === personaName; });
3284
3739
  if (persona === undefined) {
3285
3740
  pipelineJson.personas.push({
@@ -3421,7 +3876,7 @@ var blockCommandParser = {
3421
3876
  /**
3422
3877
  * Link to discussion
3423
3878
  */
3424
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/64',
3879
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/64',
3425
3880
  /**
3426
3881
  * Example usages of the BLOCK command
3427
3882
  */
@@ -3449,6 +3904,7 @@ var blockCommandParser = {
3449
3904
  //---
3450
3905
  /* <- TODO: [🧠] Maybe dynamic */
3451
3906
  ],
3907
+ // TODO: [♓️] order: -10 /* <- Note: Putting before other commands */
3452
3908
  /**
3453
3909
  * Parses the BLOCK command
3454
3910
  */
@@ -3565,7 +4021,7 @@ var expectCommandParser = {
3565
4021
  /**
3566
4022
  * Link to discussion
3567
4023
  */
3568
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/30',
4024
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/30',
3569
4025
  /**
3570
4026
  * Example usages of the EXPECT command
3571
4027
  */
@@ -3683,7 +4139,7 @@ var jokerCommandParser = {
3683
4139
  /**
3684
4140
  * Link to discussion
3685
4141
  */
3686
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/66',
4142
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/66',
3687
4143
  /**
3688
4144
  * Example usages of the JOKER command
3689
4145
  */
@@ -3726,7 +4182,11 @@ var modelCommandParser = {
3726
4182
  /**
3727
4183
  * BOILERPLATE command can be used in:
3728
4184
  */
3729
- usagePlaces: ['PIPELINE_HEAD', 'PIPELINE_TEMPLATE'],
4185
+ usagePlaces: [
4186
+ 'PIPELINE_HEAD',
4187
+ // <- TODO: [🧠][❔] Should there be possibility to set MODEL for entire pipeline?
4188
+ 'PIPELINE_TEMPLATE',
4189
+ ],
3730
4190
  /**
3731
4191
  * Description of the MODEL command
3732
4192
  */
@@ -3734,7 +4194,7 @@ var modelCommandParser = {
3734
4194
  /**
3735
4195
  * Link to discussion
3736
4196
  */
3737
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/67',
4197
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/67',
3738
4198
  /**
3739
4199
  * Example usages of the MODEL command
3740
4200
  */
@@ -3814,7 +4274,7 @@ var parameterCommandParser = {
3814
4274
  /**
3815
4275
  * Link to discussion
3816
4276
  */
3817
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/68',
4277
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/68',
3818
4278
  /**
3819
4279
  * Example usages of the PARAMETER command
3820
4280
  */
@@ -3878,7 +4338,7 @@ var postprocessCommandParser = {
3878
4338
  /**
3879
4339
  * Link to discussion
3880
4340
  */
3881
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/31',
4341
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/31',
3882
4342
  /**
3883
4343
  * Example usages of the POSTPROCESS command
3884
4344
  */
@@ -3933,7 +4393,7 @@ var promptbookVersionCommandParser = {
3933
4393
  /**
3934
4394
  * Link to discussion
3935
4395
  */
3936
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/69',
4396
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/69',
3937
4397
  /**
3938
4398
  * Example usages of the PROMPTBOOK_VERSION command
3939
4399
  */
@@ -3986,7 +4446,7 @@ var urlCommandParser = {
3986
4446
  /**
3987
4447
  * Link to discussion
3988
4448
  */
3989
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/70',
4449
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/70',
3990
4450
  /**
3991
4451
  * Example usages of the URL command
3992
4452
  */
@@ -4056,7 +4516,7 @@ var actionCommandParser = {
4056
4516
  /**
4057
4517
  * Link to discussion
4058
4518
  */
4059
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/72',
4519
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/72',
4060
4520
  /**
4061
4521
  * Example usages of the ACTION command
4062
4522
  */
@@ -4095,7 +4555,7 @@ var instrumentCommandParser = {
4095
4555
  /**
4096
4556
  * Link to discussion
4097
4557
  */
4098
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/71',
4558
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/71',
4099
4559
  /**
4100
4560
  * Example usages of the INSTRUMENT command
4101
4561
  */
@@ -4138,7 +4598,7 @@ var boilerplateCommandParser = {
4138
4598
  /**
4139
4599
  * Link to discussion
4140
4600
  */
4141
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/@@',
4601
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/@@',
4142
4602
  /**
4143
4603
  * Example usages of the BOILERPLATE command
4144
4604
  */
@@ -4272,9 +4732,9 @@ function parseCommand(raw, usagePlace) {
4272
4732
  */
4273
4733
  function getSupportedCommandsMessage() {
4274
4734
  return COMMANDS.flatMap(function (_a) {
4275
- var name = _a.name, aliasNames = _a.aliasNames, description = _a.description, discussionUrl = _a.discussionUrl;
4735
+ var name = _a.name, aliasNames = _a.aliasNames, description = _a.description, documentationUrl = _a.documentationUrl;
4276
4736
  return __spreadArray([
4277
- "- **".concat(name, "** ").concat(description, ", see [discussion](").concat(discussionUrl, ")")
4737
+ "- **".concat(name, "** ").concat(description, ", see [discussion](").concat(documentationUrl, ")")
4278
4738
  ], __read((aliasNames || []).map(function (aliasName) { return " - **".concat(aliasName, "** Alias for **").concat(name, "**"); })), false);
4279
4739
  }).join('\n');
4280
4740
  }
@@ -4629,195 +5089,6 @@ function removeContentComments(content) {
4629
5089
  return spaceTrim$1(content.replace(/<!--(.*?)-->/gs, ''));
4630
5090
  }
4631
5091
 
4632
- /**
4633
- * Create difference set of two sets.
4634
- *
4635
- * @deprecated use new javascript set methods instead @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
4636
- */
4637
- function difference(a, b, isEqual) {
4638
- var e_1, _a;
4639
- if (isEqual === void 0) { isEqual = function (a, b) { return a === b; }; }
4640
- var diff = new Set();
4641
- var _loop_1 = function (itemA) {
4642
- if (!Array.from(b).some(function (itemB) { return isEqual(itemA, itemB); })) {
4643
- diff.add(itemA);
4644
- }
4645
- };
4646
- try {
4647
- for (var _b = __values(Array.from(a)), _c = _b.next(); !_c.done; _c = _b.next()) {
4648
- var itemA = _c.value;
4649
- _loop_1(itemA);
4650
- }
4651
- }
4652
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
4653
- finally {
4654
- try {
4655
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
4656
- }
4657
- finally { if (e_1) throw e_1.error; }
4658
- }
4659
- return diff;
4660
- }
4661
-
4662
- /**
4663
- * Creates a new set with all elements that are present in either set
4664
- *
4665
- * @deprecated use new javascript set methods instead @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
4666
- */
4667
- function union() {
4668
- var e_1, _a, e_2, _b;
4669
- var sets = [];
4670
- for (var _i = 0; _i < arguments.length; _i++) {
4671
- sets[_i] = arguments[_i];
4672
- }
4673
- var union = new Set();
4674
- try {
4675
- for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
4676
- var set = sets_1_1.value;
4677
- try {
4678
- for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
4679
- var item = _d.value;
4680
- union.add(item);
4681
- }
4682
- }
4683
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
4684
- finally {
4685
- try {
4686
- if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
4687
- }
4688
- finally { if (e_2) throw e_2.error; }
4689
- }
4690
- }
4691
- }
4692
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
4693
- finally {
4694
- try {
4695
- if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
4696
- }
4697
- finally { if (e_1) throw e_1.error; }
4698
- }
4699
- return union;
4700
- }
4701
-
4702
- /**
4703
- * Parses the template and returns the list of all parameter names
4704
- *
4705
- * @param template the template with parameters in {curly} braces
4706
- * @returns the list of parameter names
4707
- */
4708
- function extractParameters(template) {
4709
- var e_1, _a;
4710
- var matches = template.matchAll(/{\w+}/g);
4711
- var parameterNames = new Set();
4712
- try {
4713
- for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
4714
- var match = matches_1_1.value;
4715
- var parameterName = match[0].slice(1, -1);
4716
- parameterNames.add(parameterName);
4717
- }
4718
- }
4719
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
4720
- finally {
4721
- try {
4722
- if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
4723
- }
4724
- finally { if (e_1) throw e_1.error; }
4725
- }
4726
- return parameterNames;
4727
- }
4728
-
4729
- /**
4730
- * Parses the given script and returns the list of all used variables that are not defined in the script
4731
- *
4732
- * @param script from which to extract the variables
4733
- * @returns the list of variable names
4734
- * @throws {ParsingError} if the script is invalid
4735
- */
4736
- function extractVariables(script) {
4737
- var variables = new Set();
4738
- script = "(()=>{".concat(script, "})()");
4739
- try {
4740
- for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
4741
- try {
4742
- eval(script);
4743
- }
4744
- catch (error) {
4745
- if (!(error instanceof ReferenceError)) {
4746
- throw error;
4747
- }
4748
- var undefinedName = error.message.split(' ')[0];
4749
- /*
4750
- Note: Parsing the error
4751
- [ReferenceError: thing is not defined]
4752
- */
4753
- if (!undefinedName) {
4754
- throw error;
4755
- }
4756
- if (script.includes(undefinedName + '(')) {
4757
- script = "const ".concat(undefinedName, " = ()=>'';") + script;
4758
- }
4759
- else {
4760
- variables.add(undefinedName);
4761
- script = "const ".concat(undefinedName, " = '';") + script;
4762
- }
4763
- }
4764
- }
4765
- catch (error) {
4766
- if (!(error instanceof Error)) {
4767
- throw error;
4768
- }
4769
- throw new ParsingError(spaceTrim$1(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
4770
- }
4771
- return variables;
4772
- }
4773
- /**
4774
- * TODO: [🔣] Support for multiple languages - python, java,...
4775
- */
4776
-
4777
- /**
4778
- * Parses the prompt template and returns the set of all used parameters
4779
- *
4780
- * @param promptTemplate the template with used parameters
4781
- * @returns the set of parameter names
4782
- * @throws {ParsingError} if the script is invalid
4783
- */
4784
- function extractParametersFromPromptTemplate(promptTemplate) {
4785
- var e_1, _a, e_2, _b;
4786
- var parameterNames = new Set();
4787
- try {
4788
- 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()) {
4789
- var parameterName = _d.value;
4790
- parameterNames.add(parameterName);
4791
- }
4792
- }
4793
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
4794
- finally {
4795
- try {
4796
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
4797
- }
4798
- finally { if (e_1) throw e_1.error; }
4799
- }
4800
- if (promptTemplate.blockType === 'SCRIPT') {
4801
- try {
4802
- for (var _e = __values(extractVariables(promptTemplate.content)), _f = _e.next(); !_f.done; _f = _e.next()) {
4803
- var parameterName = _f.value;
4804
- parameterNames.add(parameterName);
4805
- }
4806
- }
4807
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
4808
- finally {
4809
- try {
4810
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
4811
- }
4812
- finally { if (e_2) throw e_2.error; }
4813
- }
4814
- }
4815
- return parameterNames;
4816
- }
4817
- /**
4818
- * TODO: [🔣] If script require contentLanguage
4819
- */
4820
-
4821
5092
  /**
4822
5093
  * Compile pipeline from string (markdown) format to JSON format synchronously
4823
5094
  *
@@ -4927,7 +5198,7 @@ function pipelineStringToJsonSync(pipelineString) {
4927
5198
  pipelineJson.pipelineUrl = command.pipelineUrl.href;
4928
5199
  break;
4929
5200
  case 'KNOWLEDGE':
4930
- knowledgeCommandParser.applyToPipelineJson(pipelineJson, command);
5201
+ knowledgeCommandParser.applyToPipelineJson(command, { pipelineJson: pipelineJson, templateJson: null });
4931
5202
  break;
4932
5203
  case 'ACTION':
4933
5204
  console.error(new NotYetImplementedError('Actions are not implemented yet'));
@@ -4936,7 +5207,7 @@ function pipelineStringToJsonSync(pipelineString) {
4936
5207
  console.error(new NotYetImplementedError('Instruments are not implemented yet'));
4937
5208
  break;
4938
5209
  case 'PERSONA':
4939
- personaCommandParser.applyToPipelineJson(pipelineJson, command);
5210
+ personaCommandParser.applyToPipelineJson(command, { pipelineJson: pipelineJson, templateJson: null });
4940
5211
  // <- Note: Prototype of [🍧] (remove this comment after full implementation)
4941
5212
  break;
4942
5213
  case 'BOILERPLATE':
@@ -4960,13 +5231,6 @@ function pipelineStringToJsonSync(pipelineString) {
4960
5231
  // TODO: Parse prompt template description (the content out of the codeblock and lists)
4961
5232
  var templateModelRequirements = __assign({}, defaultModelRequirements);
4962
5233
  var listItems_3 = extractAllListItemsFromMarkdown(section.content);
4963
- var dependentParameterNames = new Set();
4964
- var blockType = 'PROMPT_TEMPLATE';
4965
- var jokers = [];
4966
- var postprocessing = [];
4967
- var expectAmount = {};
4968
- var expectFormat = undefined;
4969
- var isBlockTypeSet = false;
4970
5234
  var lastLine = section.content.split('\n').pop();
4971
5235
  var resultingParameterNameMatch = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
4972
5236
  var resultingParameterName = null;
@@ -4987,10 +5251,39 @@ function pipelineStringToJsonSync(pipelineString) {
4987
5251
  .join('\n')), "\n "); }));
4988
5252
  };
4989
5253
  var _e = extractOneBlockFromMarkdown(section.content), language = _e.language, content = _e.content;
5254
+ // TODO: [🎾][1] DRY description
5255
+ var description_1 = section.content;
5256
+ // Note: Remove codeblocks - TODO: [🎾]
5257
+ description_1 = description_1.split(/^```.*^```/gms).join('');
5258
+ description_1 = description_1.split(/^>.*$/gm).join('');
5259
+ //Note: Remove lists and return statement - TODO: [🎾]
5260
+ description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
5261
+ description_1 = spaceTrim$1(description_1);
5262
+ if (description_1 === '') {
5263
+ description_1 = undefined;
5264
+ }
5265
+ var templateJson = {
5266
+ blockType: 'PROMPT_TEMPLATE',
5267
+ name: titleToName(section.title),
5268
+ title: section.title,
5269
+ description: description_1,
5270
+ modelRequirements: templateModelRequirements,
5271
+ content: content,
5272
+ };
5273
+ /**
5274
+ * This is nessesary because block type can be
5275
+ * - Set zero times, so anticipate 'PROMPT_TEMPLATE'
5276
+ * - Set one time
5277
+ * - Set more times - throw error
5278
+ *
5279
+ * Note: [2]
5280
+ */
5281
+ var isBlockTypeSet = false;
4990
5282
  try {
4991
5283
  for (var listItems_2 = (e_3 = void 0, __values(listItems_3)), listItems_2_1 = listItems_2.next(); !listItems_2_1.done; listItems_2_1 = listItems_2.next()) {
4992
5284
  var listItem = listItems_2_1.value;
4993
5285
  var command = parseCommand(listItem, 'PIPELINE_TEMPLATE');
5286
+ // TODO [🍧][♓️] List commands and before apply order them
4994
5287
  switch (command.type) {
4995
5288
  // TODO: [🍧] Use here applyToPipelineJson and remove switch statement
4996
5289
  case 'BLOCK':
@@ -5008,9 +5301,12 @@ function pipelineStringToJsonSync(pipelineString) {
5008
5301
  return "continue-templates";
5009
5302
  }
5010
5303
  if (command.blockType === 'KNOWLEDGE') {
5011
- knowledgeCommandParser.applyToPipelineJson(pipelineJson, {
5304
+ knowledgeCommandParser.applyToPipelineJson({
5012
5305
  type: 'KNOWLEDGE',
5013
- source: content, // <- TODO: [🐝] !!!! Work with KNOWLEDGE which not referring to the source file/wweb, but its content itself
5306
+ source: content, // <- TODO: [🐝] !!! Work with KNOWLEDGE which not referring to the source file or website, but its content itself
5307
+ }, {
5308
+ pipelineJson: pipelineJson,
5309
+ templateJson: templateJson,
5014
5310
  });
5015
5311
  return "continue-templates";
5016
5312
  }
@@ -5023,35 +5319,36 @@ function pipelineStringToJsonSync(pipelineString) {
5023
5319
  return "continue-templates";
5024
5320
  }
5025
5321
  expectResultingParameterName();
5026
- blockType = command.blockType;
5027
- isBlockTypeSet = true;
5322
+ templateJson.blockType = command.blockType;
5323
+ isBlockTypeSet = true; //<- Note: [2]
5028
5324
  break;
5029
5325
  case 'EXPECT_AMOUNT':
5030
5326
  // eslint-disable-next-line no-case-declarations
5031
5327
  var unit = command.unit.toLowerCase();
5032
- expectAmount[unit] = expectAmount[unit] || {};
5328
+ templateJson.expectations = templateJson.expectations || {};
5329
+ templateJson.expectations[unit] = templateJson.expectations[unit] || {};
5033
5330
  if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
5034
- if (expectAmount[unit].min !== undefined) {
5035
- throw new ParsingError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
5331
+ if (templateJson.expectations[unit].min !== undefined) {
5332
+ throw new ParsingError("Already defined minumum ".concat(templateJson.expectations[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
5036
5333
  }
5037
- expectAmount[unit].min = command.amount;
5334
+ templateJson.expectations[unit].min = command.amount;
5038
5335
  } /* not else */
5039
5336
  if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
5040
- if (expectAmount[unit].max !== undefined) {
5041
- throw new ParsingError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
5337
+ if (templateJson.expectations[unit].max !== undefined) {
5338
+ throw new ParsingError("Already defined maximum ".concat(templateJson.expectations[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
5042
5339
  }
5043
- expectAmount[unit].max = command.amount;
5340
+ templateJson.expectations[unit].max = command.amount;
5044
5341
  }
5045
5342
  break;
5046
5343
  case 'EXPECT_FORMAT':
5047
- if (expectFormat !== undefined && command.format !== expectFormat) {
5048
- throw new ParsingError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
5344
+ if (templateJson.expectFormat !== undefined && command.format !== templateJson.expectFormat) {
5345
+ throw new ParsingError(spaceTrim$1("\n Expect format is already defined to \"".concat(templateJson.expectFormat, "\".\n Now you try to redefine it by \"").concat(command.format, "\".\n ")));
5049
5346
  }
5050
- expectFormat = command.format;
5347
+ templateJson.expectFormat = command.format;
5051
5348
  break;
5052
5349
  case 'JOKER':
5053
- jokers.push(command.parameterName);
5054
- dependentParameterNames.add(command.parameterName);
5350
+ templateJson.jokerParameterNames = templateJson.jokerParameterNames || [];
5351
+ templateJson.jokerParameterNames.push(command.parameterName);
5055
5352
  break;
5056
5353
  case 'MODEL':
5057
5354
  templateModelRequirements[command.key] = command.value;
@@ -5061,11 +5358,12 @@ function pipelineStringToJsonSync(pipelineString) {
5061
5358
  defineParam(command);
5062
5359
  break;
5063
5360
  case 'POSTPROCESS':
5064
- postprocessing.push(command.functionName);
5361
+ templateJson.postprocessingFunctionNames = templateJson.postprocessingFunctionNames || [];
5362
+ templateJson.postprocessingFunctionNames.push(command.functionName);
5065
5363
  break;
5066
5364
  case 'KNOWLEDGE':
5067
5365
  // TODO: [👙] The knowledge is maybe relevant for just this template
5068
- knowledgeCommandParser.applyToPipelineJson(pipelineJson, command);
5366
+ knowledgeCommandParser.applyToPipelineJson(command, { pipelineJson: pipelineJson, templateJson: templateJson });
5069
5367
  break;
5070
5368
  case 'ACTION':
5071
5369
  // TODO: [👙] The action is maybe relevant for just this template
@@ -5076,7 +5374,7 @@ function pipelineStringToJsonSync(pipelineString) {
5076
5374
  console.error(new NotYetImplementedError('Instruments are not implemented yet'));
5077
5375
  break;
5078
5376
  case 'PERSONA':
5079
- personaCommandParser.applyToPipelineJson(pipelineJson, command);
5377
+ personaCommandParser.applyToPipelineJson(command, { pipelineJson: pipelineJson, templateJson: templateJson });
5080
5378
  // <- Note: Prototype of [🍧] (remove this comment after full implementation)
5081
5379
  break;
5082
5380
  case 'BOILERPLATE':
@@ -5095,60 +5393,29 @@ function pipelineStringToJsonSync(pipelineString) {
5095
5393
  }
5096
5394
  finally { if (e_3) throw e_3.error; }
5097
5395
  }
5098
- if (blockType === 'SCRIPT') {
5396
+ // TODO: [🍧] Should be done in BLOCK command
5397
+ if (templateJson.blockType === 'SCRIPT') {
5099
5398
  if (!language) {
5100
5399
  throw new ParsingError('You must specify the language of the script in the prompt template');
5101
5400
  }
5102
- else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
5401
+ if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
5103
5402
  throw new ParsingError(spaceTrim$1(function (block) { return "\n Script language ".concat(language, " is not supported.\n\n Supported languages are:\n ").concat(block(SUPPORTED_SCRIPT_LANGUAGES.join(', ')), "\n\n "); }));
5104
5403
  }
5404
+ templateJson.contentLanguage = language;
5105
5405
  }
5106
- // TODO: [🎾][1] DRY description
5107
- var description_1 = section.content;
5108
- // Note: Remove codeblocks - TODO: [🎾]
5109
- description_1 = description_1.split(/^```.*^```/gms).join('');
5110
- description_1 = description_1.split(/^>.*$/gm).join('');
5111
- //Note: Remove lists and return statement - TODO: [🎾]
5112
- description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
5113
- description_1 = spaceTrim$1(description_1);
5114
- if (description_1 === '') {
5115
- description_1 = undefined;
5116
- }
5117
- if (Object.keys(jokers).length === 0) {
5118
- jokers = undefined;
5119
- }
5120
- if (Object.keys(expectAmount).length === 0) {
5121
- expectAmount = undefined;
5122
- }
5123
- if (Object.keys(postprocessing).length === 0) {
5124
- postprocessing = undefined;
5125
- }
5126
- dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, blockType: blockType, content: content })));
5406
+ // TODO: [🍧][] Should be done in BLOCK command
5127
5407
  if (templateModelRequirements.modelVariant === undefined) {
5128
5408
  templateModelRequirements.modelVariant = 'CHAT';
5129
5409
  }
5130
- dependentParameterNames = difference(dependentParameterNames, new Set(RESERVED_PARAMETER_NAMES));
5131
- var template = {
5132
- name: titleToName(section.title),
5133
- title: section.title,
5134
- description: description_1,
5135
- dependentParameterNames: Array.from(dependentParameterNames),
5136
- blockType: blockType,
5137
- jokers: jokers,
5138
- postprocessing: postprocessing,
5139
- expectations: expectAmount,
5140
- expectFormat: expectFormat,
5141
- personaName: null,
5142
- modelRequirements: templateModelRequirements,
5143
- contentLanguage: blockType === 'SCRIPT' ? language : undefined,
5144
- content: content,
5145
- resultingParameterName: expectResultingParameterName( /* <- Note: This is once more redundant */),
5146
- };
5147
- if (blockType !== 'PROMPT_TEMPLATE') {
5148
- delete template.modelRequirements;
5410
+ templateJson.dependentParameterNames = Array.from(extractParametersFromPromptTemplate(templateJson));
5411
+ // TODO: [🍧][❔] Remove this condition - modelRequirements should be put here via BLOCK command not removed when PROMPT_TEMPLATE
5412
+ if (templateJson.blockType !== 'PROMPT_TEMPLATE') {
5413
+ delete templateJson.modelRequirements;
5149
5414
  }
5415
+ // TODO: [🍧] Make this better - for example each command parser can call and apply this
5416
+ templateJson.resultingParameterName = expectResultingParameterName( /* <- Note: This is once more redundant */);
5150
5417
  // TODO: [🍧] What actually about preparation and pushing the block into `promptTemplates`
5151
- pipelineJson.promptTemplates.push(template);
5418
+ pipelineJson.promptTemplates.push(templateJson);
5152
5419
  };
5153
5420
  try {
5154
5421
  // =============================================================
@@ -5434,6 +5701,27 @@ function prettifyPipelineString(pipelineString, options) {
5434
5701
  * TODO: [🕌] When more than 2 functionalities, split into separate functions
5435
5702
  */
5436
5703
 
5704
+ /**
5705
+ * Stringify the PipelineJson with proper formatting
5706
+ *
5707
+ * Note: [0] It can be used for more JSON types like whole collection of pipelines, single knowledge piece, etc.
5708
+ * Note: In contrast to JSON.stringify, this function ensures that **embedding index** is on single line
5709
+ */
5710
+ function stringifyPipelineJson(pipeline) {
5711
+ var pipelineJsonStringified = JSON.stringify(pipeline, null, 4);
5712
+ for (var i = 0; i < LOOP_LIMIT; i++) {
5713
+ pipelineJsonStringified = pipelineJsonStringified.replace(/(-?0\.\d+),[\n\s]+(-?0\.\d+)/gms, "$1".concat(REPLACING_NONCE, "$2"));
5714
+ }
5715
+ pipelineJsonStringified = pipelineJsonStringified.split(REPLACING_NONCE).join(', ');
5716
+ pipelineJsonStringified += '\n';
5717
+ return pipelineJsonStringified;
5718
+ }
5719
+ /**
5720
+ * TODO: !!!! Not Working propperly @see https://promptbook.studio/samples/mixed-knowledge.ptbk.md
5721
+ * TODO: [🧠][0] Maybe rename to `stringifyPipelineJson`, `stringifyIndexedJson`,...
5722
+ * TODO: [🧠] Maybe more elegant solution than replacing via regex
5723
+ */
5724
+
5437
5725
  /**
5438
5726
  * This error indicates that the pipeline collection cannot be propperly loaded
5439
5727
  */
@@ -5667,26 +5955,6 @@ function escapeMarkdownBlock(value) {
5667
5955
  return value.replace(/```/g, '\\`\\`\\`');
5668
5956
  }
5669
5957
 
5670
- /**
5671
- * Returns the same value that is passed as argument.
5672
- * No side effects.
5673
- *
5674
- * Note: It can be usefull for:
5675
- *
5676
- * 1) Leveling indentation
5677
- * 2) Putting always-true or always-false conditions without getting eslint errors
5678
- *
5679
- * @param value any values
5680
- * @returns the same values
5681
- * @private within the repository
5682
- */
5683
- function just(value) {
5684
- if (value === undefined) {
5685
- return undefined;
5686
- }
5687
- return value;
5688
- }
5689
-
5690
5958
  /**
5691
5959
  * The thresholds for the relative time in the `moment` NPM package.
5692
5960
  *
@@ -5897,5 +6165,5 @@ function executionReportJsonToString(executionReportJson, options) {
5897
6165
  * TODO: [🧠] Allow to filter out some parts of the report by options
5898
6166
  */
5899
6167
 
5900
- export { BlockTypes, CallbackInterfaceTools, CollectionError, ExecutionReportStringOptionsDefaults, ExpectError, NotFoundError, PROMPTBOOK_VERSION, ParsingError, PipelineExecutionError, PipelineLogicError, RESERVED_PARAMETER_NAMES, ReferenceError$1 as ReferenceError, SimplePromptInterfaceTools, UnexpectedError, VersionMismatchError, addUsage, assertsExecutionSuccessful, checkExpectations, collectionToJson, createCollectionFromJson, createCollectionFromPromise, createCollectionFromUrl, createPipelineExecutor, createSubcollection, embeddingVectorToString, executionReportJsonToString, isPassingExpectations, joinLlmExecutionTools, pipelineJsonToString, pipelineStringToJson, pipelineStringToJsonSync, prepareKnowledgeFromMarkdown, preparePipeline, prettifyPipelineString, unpreparePipeline, usageToWorktime, validatePipeline };
6168
+ export { BlockTypes, CallbackInterfaceTools, CollectionError, ExecutionReportStringOptionsDefaults, ExpectError, NotFoundError, PROMPTBOOK_VERSION, ParsingError, PipelineExecutionError, PipelineLogicError, RESERVED_PARAMETER_NAMES, ReferenceError$1 as ReferenceError, SimplePromptInterfaceTools, UnexpectedError, VersionMismatchError, addUsage, assertsExecutionSuccessful, checkExpectations, collectionToJson, createCollectionFromJson, createCollectionFromPromise, createCollectionFromUrl, createPipelineExecutor, createSubcollection, embeddingVectorToString, executionReportJsonToString, isPassingExpectations, joinLlmExecutionTools, pipelineJsonToString, pipelineStringToJson, pipelineStringToJsonSync, prepareKnowledgeFromMarkdown, preparePipeline, prettifyPipelineString, stringifyPipelineJson, unpreparePipeline, usageToWorktime, validatePipeline };
5901
6169
  //# sourceMappingURL=index.es.js.map