@promptbook/node 0.61.0-15 → 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 +851 -608
  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 +2 -2
  42. package/umd/index.umd.js +851 -608
  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
@@ -129,7 +129,7 @@ function __spreadArray(to, from, pack) {
129
129
  *
130
130
  * @returns The same object as the input, but deeply frozen
131
131
  *
132
- * Note: This function mutates the object
132
+ * Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
133
133
  */
134
134
  function deepFreeze(objectValue) {
135
135
  var e_1, _a;
@@ -152,6 +152,17 @@ function deepFreeze(objectValue) {
152
152
  }
153
153
  return Object.freeze(objectValue);
154
154
  }
155
+ /**
156
+ * @@@
157
+ * @@@
158
+ *
159
+ * @returns The same object as the input, but deeply frozen
160
+ *
161
+ * Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
162
+ */
163
+ function deepFreezeWithSameType(objectValue) {
164
+ return deepFreeze(objectValue);
165
+ }
155
166
  /**
156
167
  * TODO: [🔼] Export from `@promptbook/utils`
157
168
  * TODO: [🧠] Is there a way how to meaningfully test this utility
@@ -165,6 +176,10 @@ var LOOP_LIMIT = 1000;
165
176
  * The maximum number of (LLM) tasks running in parallel
166
177
  */
167
178
  var MAX_PARALLEL_COUNT = 5;
179
+ /**
180
+ * The maximum number of attempts to execute LLM task before giving up
181
+ */
182
+ var MAX_EXECUTION_ATTEMPTS = 3;
168
183
  /**
169
184
  * The name of the builded pipeline collection made by CLI `ptbk make` and for lookup in `createCollectionFromDirectory`
170
185
  */
@@ -174,6 +189,7 @@ var PIPELINE_COLLECTION_BASE_FILENAME = "index";
174
189
  */
175
190
  var RESERVED_PARAMETER_NAMES = deepFreeze([
176
191
  'context',
192
+ 'currentDate',
177
193
  // <- TODO: Add more like 'date', 'modelName',...
178
194
  // <- TODO: Add [emoji] + instructions ACRY when adding new reserved parameter
179
195
  ]);
@@ -181,6 +197,9 @@ var RESERVED_PARAMETER_NAMES = deepFreeze([
181
197
  TODO: !!! Just testing false-negative detection of [🟡][🟢][🔵][⚪] leak
182
198
  */
183
199
  // [🟡][🟢][🔵][⚪]
200
+ /**
201
+ * TODO: [🔼] Export all to core
202
+ */
184
203
 
185
204
  /**
186
205
  * @@@
@@ -374,7 +393,7 @@ function forEachAsync(array, options, callbackfunction) {
374
393
  });
375
394
  }
376
395
 
377
- var PipelineCollection = [{title:"Prepare Knowledge from Markdown",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.61.0-14",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-14",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-14",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-14",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-14",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-14",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-14",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-14",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"}];
396
+ 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"}];
378
397
 
379
398
  /**
380
399
  * Prettify the html code
@@ -480,7 +499,7 @@ function pipelineJsonToString(pipelineJson) {
480
499
  /* Note: Not using:> name, */
481
500
  title_1 = promptTemplate.title, description_1 = promptTemplate.description,
482
501
  /* Note: dependentParameterNames, */
483
- jokers = promptTemplate.jokers, blockType = promptTemplate.blockType, content = promptTemplate.content, postprocessing = promptTemplate.postprocessing, expectations = promptTemplate.expectations, expectFormat = promptTemplate.expectFormat, resultingParameterName = promptTemplate.resultingParameterName;
502
+ jokers = promptTemplate.jokerParameterNames, blockType = promptTemplate.blockType, content = promptTemplate.content, postprocessing = promptTemplate.postprocessingFunctionNames, expectations = promptTemplate.expectations, expectFormat = promptTemplate.expectFormat, resultingParameterName = promptTemplate.resultingParameterName;
484
503
  pipelineString += '\n\n';
485
504
  pipelineString += "## ".concat(title_1);
486
505
  if (description_1) {
@@ -820,7 +839,7 @@ function isValidPipelineUrl(url) {
820
839
  */
821
840
  function validatePipeline(pipeline) {
822
841
  // TODO: [🧠] Maybe test if promptbook is a promise and make specific error case for that
823
- var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
842
+ var e_1, _a, e_2, _b, e_3, _c, e_4, _d, e_5, _e;
824
843
  if (pipeline.pipelineUrl !== undefined && !isValidPipelineUrl(pipeline.pipelineUrl)) {
825
844
  // <- Note: [🚲]
826
845
  throw new PipelineLogicError("Invalid promptbook URL \"".concat(pipeline.pipelineUrl, "\""));
@@ -857,19 +876,19 @@ function validatePipeline(pipeline) {
857
876
  };
858
877
  try {
859
878
  // Note: Check each parameter individually
860
- for (var _e = __values(pipeline.parameters), _f = _e.next(); !_f.done; _f = _e.next()) {
861
- var parameter = _f.value;
879
+ for (var _f = __values(pipeline.parameters), _g = _f.next(); !_g.done; _g = _f.next()) {
880
+ var parameter = _g.value;
862
881
  _loop_1(parameter);
863
882
  }
864
883
  }
865
884
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
866
885
  finally {
867
886
  try {
868
- if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
887
+ if (_g && !_g.done && (_a = _f.return)) _a.call(_f);
869
888
  }
870
889
  finally { if (e_1) throw e_1.error; }
871
890
  }
872
- // Note: Check each template individually
891
+ // Note: All input parameters are defined - so that they can be used as result of some template
873
892
  var definedParameters = new Set(pipeline.parameters.filter(function (_a) {
874
893
  var isInput = _a.isInput;
875
894
  return isInput;
@@ -878,23 +897,27 @@ function validatePipeline(pipeline) {
878
897
  return name;
879
898
  }));
880
899
  try {
881
- for (var _g = __values(pipeline.promptTemplates), _h = _g.next(); !_h.done; _h = _g.next()) {
882
- var template = _h.value;
900
+ // Note: Checking each template individually
901
+ for (var _h = __values(pipeline.promptTemplates), _j = _h.next(); !_j.done; _j = _h.next()) {
902
+ var template = _j.value;
883
903
  if (definedParameters.has(template.resultingParameterName)) {
884
904
  throw new PipelineLogicError("Parameter {".concat(template.resultingParameterName, "} is defined multiple times"));
885
905
  }
906
+ if (RESERVED_PARAMETER_NAMES.includes(template.resultingParameterName)) {
907
+ throw new PipelineLogicError("Parameter name {".concat(template.resultingParameterName, "} is reserved, please use fifferent name"));
908
+ }
886
909
  definedParameters.add(template.resultingParameterName);
887
910
  if (template.blockType === 'PROMPT_TEMPLATE' && template.modelRequirements.modelVariant === undefined) {
888
911
  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 ")));
889
912
  }
890
- if (template.jokers && template.jokers.length > 0) {
913
+ if (template.jokerParameterNames && template.jokerParameterNames.length > 0) {
891
914
  if (!template.expectFormat &&
892
915
  !template.expectations /* <- TODO: Require at least 1 -> min <- expectation to use jokers */) {
893
916
  throw new PipelineLogicError("Joker parameters are used for {".concat(template.resultingParameterName, "} but no expectations are defined"));
894
917
  }
895
918
  try {
896
- for (var _j = (e_3 = void 0, __values(template.jokers)), _k = _j.next(); !_k.done; _k = _j.next()) {
897
- var joker = _k.value;
919
+ for (var _k = (e_3 = void 0, __values(template.jokerParameterNames)), _l = _k.next(); !_l.done; _l = _k.next()) {
920
+ var joker = _l.value;
898
921
  if (!template.dependentParameterNames.includes(joker)) {
899
922
  throw new PipelineLogicError("Parameter {".concat(joker, "} is used for {").concat(template.resultingParameterName, "} as joker but not in dependentParameterNames"));
900
923
  }
@@ -903,15 +926,15 @@ function validatePipeline(pipeline) {
903
926
  catch (e_3_1) { e_3 = { error: e_3_1 }; }
904
927
  finally {
905
928
  try {
906
- if (_k && !_k.done && (_c = _j.return)) _c.call(_j);
929
+ if (_l && !_l.done && (_c = _k.return)) _c.call(_k);
907
930
  }
908
931
  finally { if (e_3) throw e_3.error; }
909
932
  }
910
933
  }
911
934
  if (template.expectations) {
912
935
  try {
913
- for (var _l = (e_4 = void 0, __values(Object.entries(template.expectations))), _m = _l.next(); !_m.done; _m = _l.next()) {
914
- var _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], min = _p.min, max = _p.max;
936
+ for (var _m = (e_4 = void 0, __values(Object.entries(template.expectations))), _o = _m.next(); !_o.done; _o = _m.next()) {
937
+ var _p = __read(_o.value, 2), unit = _p[0], _q = _p[1], min = _q.min, max = _q.max;
915
938
  if (min !== undefined && max !== undefined && min > max) {
916
939
  throw new PipelineLogicError("Min expectation (=".concat(min, ") of ").concat(unit, " is higher than max expectation (=").concat(max, ")"));
917
940
  }
@@ -926,7 +949,7 @@ function validatePipeline(pipeline) {
926
949
  catch (e_4_1) { e_4 = { error: e_4_1 }; }
927
950
  finally {
928
951
  try {
929
- if (_m && !_m.done && (_d = _l.return)) _d.call(_l);
952
+ if (_o && !_o.done && (_d = _m.return)) _d.call(_m);
930
953
  }
931
954
  finally { if (e_4) throw e_4.error; }
932
955
  }
@@ -936,7 +959,7 @@ function validatePipeline(pipeline) {
936
959
  catch (e_2_1) { e_2 = { error: e_2_1 }; }
937
960
  finally {
938
961
  try {
939
- if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
962
+ if (_j && !_j.done && (_b = _h.return)) _b.call(_h);
940
963
  }
941
964
  finally { if (e_2) throw e_2.error; }
942
965
  }
@@ -950,7 +973,22 @@ function validatePipeline(pipeline) {
950
973
  var name = _a.name;
951
974
  return name;
952
975
  });
976
+ try {
977
+ // Note: All reserved parameters are resolved
978
+ 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()) {
979
+ var reservedParameterName = RESERVED_PARAMETER_NAMES_1_1.value;
980
+ resovedParameters = __spreadArray(__spreadArray([], __read(resovedParameters), false), [reservedParameterName], false);
981
+ }
982
+ }
983
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
984
+ finally {
985
+ try {
986
+ if (RESERVED_PARAMETER_NAMES_1_1 && !RESERVED_PARAMETER_NAMES_1_1.done && (_e = RESERVED_PARAMETER_NAMES_1.return)) _e.call(RESERVED_PARAMETER_NAMES_1);
987
+ }
988
+ finally { if (e_5) throw e_5.error; }
989
+ }
953
990
  var unresovedTemplates = __spreadArray([], __read(pipeline.promptTemplates), false);
991
+ // <- TODO: [🧠][🥜]
954
992
  var loopLimit = LOOP_LIMIT;
955
993
  var _loop_2 = function () {
956
994
  if (loopLimit-- < 0) {
@@ -995,6 +1033,9 @@ function validatePipeline(pipeline) {
995
1033
  /**
996
1034
  * TODO: [🧠][🐣] !!!! Validate that all samples match expectations
997
1035
  * TODO: [🧠][🐣] !!!! Validate that knowledge is valid (non-void)
1036
+ * TODO: [🧠][🐣] !!!! Validate that persona can be used only with CHAT variant
1037
+ * TODO: !!!! Validate that parameter with reserved name not used RESERVED_PARAMETER_NAMES
1038
+ * TODO: !!!! Validate that reserved parameter is not used as joker
998
1039
  * TODO: [🧠] !!! Validationg not only logic itself but imports around - files and websites and rerefenced pipelines exists
999
1040
  * TODO: [🛠] Actions, instruments (and maybe knowledge) => Functions and tools
1000
1041
  */
@@ -1503,6 +1544,212 @@ function assertsExecutionSuccessful(executionResult) {
1503
1544
  * TODO: [🧠] Can this return type be better typed than void
1504
1545
  */
1505
1546
 
1547
+ /**
1548
+ * Create difference set of two sets.
1549
+ *
1550
+ * @deprecated use new javascript set methods instead @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
1551
+ */
1552
+ function difference(a, b, isEqual) {
1553
+ var e_1, _a;
1554
+ if (isEqual === void 0) { isEqual = function (a, b) { return a === b; }; }
1555
+ var diff = new Set();
1556
+ var _loop_1 = function (itemA) {
1557
+ if (!Array.from(b).some(function (itemB) { return isEqual(itemA, itemB); })) {
1558
+ diff.add(itemA);
1559
+ }
1560
+ };
1561
+ try {
1562
+ for (var _b = __values(Array.from(a)), _c = _b.next(); !_c.done; _c = _b.next()) {
1563
+ var itemA = _c.value;
1564
+ _loop_1(itemA);
1565
+ }
1566
+ }
1567
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1568
+ finally {
1569
+ try {
1570
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1571
+ }
1572
+ finally { if (e_1) throw e_1.error; }
1573
+ }
1574
+ return diff;
1575
+ }
1576
+ /**
1577
+ * TODO: [🧠][💯] Maybe also implement symmetricDifference
1578
+ */
1579
+
1580
+ /**
1581
+ * Parses the template and returns the list of all parameter names
1582
+ *
1583
+ * @param template the template with parameters in {curly} braces
1584
+ * @returns the list of parameter names
1585
+ */
1586
+ function extractParameters(template) {
1587
+ var e_1, _a;
1588
+ var matches = template.matchAll(/{\w+}/g);
1589
+ var parameterNames = new Set();
1590
+ try {
1591
+ for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
1592
+ var match = matches_1_1.value;
1593
+ var parameterName = match[0].slice(1, -1);
1594
+ parameterNames.add(parameterName);
1595
+ }
1596
+ }
1597
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1598
+ finally {
1599
+ try {
1600
+ if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
1601
+ }
1602
+ finally { if (e_1) throw e_1.error; }
1603
+ }
1604
+ return parameterNames;
1605
+ }
1606
+
1607
+ /**
1608
+ * Parses the given script and returns the list of all used variables that are not defined in the script
1609
+ *
1610
+ * @param script from which to extract the variables
1611
+ * @returns the list of variable names
1612
+ * @throws {ParsingError} if the script is invalid
1613
+ */
1614
+ function extractVariables(script) {
1615
+ var variables = new Set();
1616
+ script = "(()=>{".concat(script, "})()");
1617
+ try {
1618
+ for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
1619
+ try {
1620
+ eval(script);
1621
+ }
1622
+ catch (error) {
1623
+ if (!(error instanceof ReferenceError)) {
1624
+ throw error;
1625
+ }
1626
+ var undefinedName = error.message.split(' ')[0];
1627
+ /*
1628
+ Note: Parsing the error
1629
+ [ReferenceError: thing is not defined]
1630
+ */
1631
+ if (!undefinedName) {
1632
+ throw error;
1633
+ }
1634
+ if (script.includes(undefinedName + '(')) {
1635
+ script = "const ".concat(undefinedName, " = ()=>'';") + script;
1636
+ }
1637
+ else {
1638
+ variables.add(undefinedName);
1639
+ script = "const ".concat(undefinedName, " = '';") + script;
1640
+ }
1641
+ }
1642
+ }
1643
+ catch (error) {
1644
+ if (!(error instanceof Error)) {
1645
+ throw error;
1646
+ }
1647
+ 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 "); }));
1648
+ }
1649
+ return variables;
1650
+ }
1651
+ /**
1652
+ * TODO: [🔣] Support for multiple languages - python, java,...
1653
+ */
1654
+
1655
+ /**
1656
+ * Parses the prompt template and returns the set of all used parameters
1657
+ *
1658
+ * @param promptTemplate the template with used parameters
1659
+ * @returns the set of parameter names
1660
+ * @throws {ParsingError} if the script is invalid
1661
+ */
1662
+ function extractParametersFromPromptTemplate(promptTemplate) {
1663
+ var e_1, _a, e_2, _b, e_3, _c;
1664
+ var title = promptTemplate.title, description = promptTemplate.description, blockType = promptTemplate.blockType, content = promptTemplate.content, jokerParameterNames = promptTemplate.jokerParameterNames;
1665
+ var parameterNames = new Set();
1666
+ try {
1667
+ 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()) {
1668
+ var parameterName = _e.value;
1669
+ parameterNames.add(parameterName);
1670
+ }
1671
+ }
1672
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1673
+ finally {
1674
+ try {
1675
+ if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
1676
+ }
1677
+ finally { if (e_1) throw e_1.error; }
1678
+ }
1679
+ if (blockType === 'SCRIPT') {
1680
+ try {
1681
+ for (var _f = __values(extractVariables(content)), _g = _f.next(); !_g.done; _g = _f.next()) {
1682
+ var parameterName = _g.value;
1683
+ parameterNames.add(parameterName);
1684
+ }
1685
+ }
1686
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1687
+ finally {
1688
+ try {
1689
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
1690
+ }
1691
+ finally { if (e_2) throw e_2.error; }
1692
+ }
1693
+ }
1694
+ try {
1695
+ for (var _h = __values(jokerParameterNames || []), _j = _h.next(); !_j.done; _j = _h.next()) {
1696
+ var jokerName = _j.value;
1697
+ parameterNames.add(jokerName);
1698
+ }
1699
+ }
1700
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
1701
+ finally {
1702
+ try {
1703
+ if (_j && !_j.done && (_c = _h.return)) _c.call(_h);
1704
+ }
1705
+ finally { if (e_3) throw e_3.error; }
1706
+ }
1707
+ return parameterNames;
1708
+ }
1709
+ /**
1710
+ * TODO: [🔣] If script require contentLanguage
1711
+ */
1712
+
1713
+ /**
1714
+ * Creates a new set with all elements that are present in either set
1715
+ *
1716
+ * @deprecated use new javascript set methods instead @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
1717
+ */
1718
+ function union() {
1719
+ var e_1, _a, e_2, _b;
1720
+ var sets = [];
1721
+ for (var _i = 0; _i < arguments.length; _i++) {
1722
+ sets[_i] = arguments[_i];
1723
+ }
1724
+ var union = new Set();
1725
+ try {
1726
+ for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
1727
+ var set = sets_1_1.value;
1728
+ try {
1729
+ for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
1730
+ var item = _d.value;
1731
+ union.add(item);
1732
+ }
1733
+ }
1734
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1735
+ finally {
1736
+ try {
1737
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
1738
+ }
1739
+ finally { if (e_2) throw e_2.error; }
1740
+ }
1741
+ }
1742
+ }
1743
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1744
+ finally {
1745
+ try {
1746
+ if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
1747
+ }
1748
+ finally { if (e_1) throw e_1.error; }
1749
+ }
1750
+ return union;
1751
+ }
1752
+
1506
1753
  /**
1507
1754
  * This error occurs when some expectation is not met in the execution of the pipeline
1508
1755
  *
@@ -1775,6 +2022,28 @@ function joinLlmExecutionTools() {
1775
2022
  return new (MultipleLlmExecutionTools.bind.apply(MultipleLlmExecutionTools, __spreadArray([void 0], __read(llmExecutionTools), false)))();
1776
2023
  }
1777
2024
 
2025
+ /**
2026
+ * Determine if the pipeline is fully prepared
2027
+ */
2028
+ function isPipelinePrepared(pipeline) {
2029
+ // Note: Ignoring `pipeline.preparations` @@@
2030
+ // Note: Ignoring `pipeline.knowledgePieces` @@@
2031
+ if (!pipeline.personas.every(function (persona) { return persona.modelRequirements !== undefined; })) {
2032
+ return false;
2033
+ }
2034
+ if (!pipeline.knowledgeSources.every(function (knowledgeSource) { return knowledgeSource.preparationIds !== undefined; })) {
2035
+ return false;
2036
+ }
2037
+ // TODO: !!!!! Is context in each template
2038
+ // TODO: !!!!! Are samples prepared
2039
+ // TODO: !!!!! Are templates prepared
2040
+ return true;
2041
+ }
2042
+ /**
2043
+ * TODO: [🔼] Export via core or utils
2044
+ * TODO: [🧊] Pipeline can be partially prepared, this should return true ONLY if fully prepared
2045
+ */
2046
+
1778
2047
  /**
1779
2048
  * Takes an item or an array of items and returns an array of items
1780
2049
  *
@@ -1795,29 +2064,118 @@ function arrayableToArray(input) {
1795
2064
  }
1796
2065
 
1797
2066
  /**
1798
- * The version of the Promptbook library
1799
- */
1800
- var PROMPTBOOK_VERSION = '0.61.0-14';
1801
- // TODO: !!!! List here all the versions and annotate + put into script
1802
-
1803
- /**
1804
- * Counts number of characters in the text
2067
+ * Just marks a place of place where should be something implemented
2068
+ * No side effects.
2069
+ *
2070
+ * Note: It can be usefull suppressing eslint errors of unused variables
2071
+ *
2072
+ * @param value any values
2073
+ * @returns void
2074
+ * @private within the repository
1805
2075
  */
1806
- function countCharacters(text) {
1807
- // Remove null characters
1808
- text = text.replace(/\0/g, '');
1809
- // Replace emojis (and also ZWJ sequence) with hyphens
1810
- text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
1811
- text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
1812
- text = text.replace(/\p{Extended_Pictographic}(\u{200D}\p{Extended_Pictographic})*/gu, '-');
1813
- return text.length;
2076
+ function TODO_USE() {
2077
+ var value = [];
2078
+ for (var _i = 0; _i < arguments.length; _i++) {
2079
+ value[_i] = arguments[_i];
2080
+ }
1814
2081
  }
1815
2082
 
1816
2083
  /**
1817
- * Counts number of lines in the text
2084
+ * This error type indicates that some limit was reached
1818
2085
  */
1819
- function countLines(text) {
1820
- if (text === '') {
2086
+ var LimitReachedError = /** @class */ (function (_super) {
2087
+ __extends(LimitReachedError, _super);
2088
+ function LimitReachedError(message) {
2089
+ var _this = _super.call(this, message) || this;
2090
+ _this.name = 'LimitReachedError';
2091
+ Object.setPrototypeOf(_this, LimitReachedError.prototype);
2092
+ return _this;
2093
+ }
2094
+ return LimitReachedError;
2095
+ }(Error));
2096
+
2097
+ /**
2098
+ * Replaces parameters in template with values from parameters object
2099
+ *
2100
+ * @param template the template with parameters in {curly} braces
2101
+ * @param parameters the object with parameters
2102
+ * @returns the template with replaced parameters
2103
+ * @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
2104
+ */
2105
+ function replaceParameters(template, parameters) {
2106
+ var replacedTemplate = template;
2107
+ var match;
2108
+ var loopLimit = LOOP_LIMIT;
2109
+ var _loop_1 = function () {
2110
+ if (loopLimit-- < 0) {
2111
+ throw new LimitReachedError('Loop limit reached during parameters replacement in `replaceParameters`');
2112
+ }
2113
+ var precol = match.groups.precol;
2114
+ var parameterName = match.groups.parameterName;
2115
+ if (parameterName === '') {
2116
+ return "continue";
2117
+ }
2118
+ if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
2119
+ throw new PipelineExecutionError('Parameter is already opened or not closed');
2120
+ }
2121
+ if (parameters[parameterName] === undefined) {
2122
+ throw new PipelineExecutionError("Parameter {".concat(parameterName, "} is not defined"));
2123
+ }
2124
+ var parameterValue = parameters[parameterName];
2125
+ if (parameterValue === undefined) {
2126
+ throw new PipelineExecutionError("Parameter {".concat(parameterName, "} is not defined"));
2127
+ }
2128
+ parameterValue = parameterValue.toString();
2129
+ if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
2130
+ parameterValue = parameterValue
2131
+ .split('\n')
2132
+ .map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
2133
+ .join('\n');
2134
+ }
2135
+ replacedTemplate =
2136
+ replacedTemplate.substring(0, match.index + precol.length) +
2137
+ parameterValue +
2138
+ replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
2139
+ };
2140
+ while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
2141
+ .exec(replacedTemplate))) {
2142
+ _loop_1();
2143
+ }
2144
+ // [💫] Check if there are parameters that are not closed properly
2145
+ if (/{\w+$/.test(replacedTemplate)) {
2146
+ throw new PipelineExecutionError('Parameter is not closed');
2147
+ }
2148
+ // [💫] Check if there are parameters that are not opened properly
2149
+ if (/^\w+}/.test(replacedTemplate)) {
2150
+ throw new PipelineExecutionError('Parameter is not opened');
2151
+ }
2152
+ return replacedTemplate;
2153
+ }
2154
+
2155
+ /**
2156
+ * The version of the Promptbook library
2157
+ */
2158
+ var PROMPTBOOK_VERSION = '0.61.0-15';
2159
+ // TODO: !!!! List here all the versions and annotate + put into script
2160
+
2161
+ /**
2162
+ * Counts number of characters in the text
2163
+ */
2164
+ function countCharacters(text) {
2165
+ // Remove null characters
2166
+ text = text.replace(/\0/g, '');
2167
+ // Replace emojis (and also ZWJ sequence) with hyphens
2168
+ text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
2169
+ text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
2170
+ text = text.replace(/\p{Extended_Pictographic}(\u{200D}\p{Extended_Pictographic})*/gu, '-');
2171
+ return text.length;
2172
+ }
2173
+
2174
+ /**
2175
+ * Counts number of lines in the text
2176
+ */
2177
+ function countLines(text) {
2178
+ if (text === '') {
1821
2179
  return 0;
1822
2180
  }
1823
2181
  return text.split('\n').length;
@@ -1910,80 +2268,6 @@ function checkExpectations(expectations, value) {
1910
2268
  * TODO: [💝] Unite object for expecting amount and format
1911
2269
  */
1912
2270
 
1913
- /**
1914
- * This error type indicates that some limit was reached
1915
- */
1916
- var LimitReachedError = /** @class */ (function (_super) {
1917
- __extends(LimitReachedError, _super);
1918
- function LimitReachedError(message) {
1919
- var _this = _super.call(this, message) || this;
1920
- _this.name = 'LimitReachedError';
1921
- Object.setPrototypeOf(_this, LimitReachedError.prototype);
1922
- return _this;
1923
- }
1924
- return LimitReachedError;
1925
- }(Error));
1926
-
1927
- /**
1928
- * Replaces parameters in template with values from parameters object
1929
- *
1930
- * @param template the template with parameters in {curly} braces
1931
- * @param parameters the object with parameters
1932
- * @returns the template with replaced parameters
1933
- * @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
1934
- *
1935
- * @private within the createPipelineExecutor
1936
- */
1937
- function replaceParameters(template, parameters) {
1938
- var replacedTemplate = template;
1939
- var match;
1940
- var loopLimit = LOOP_LIMIT;
1941
- var _loop_1 = function () {
1942
- if (loopLimit-- < 0) {
1943
- throw new LimitReachedError('Loop limit reached during parameters replacement in `replaceParameters`');
1944
- }
1945
- var precol = match.groups.precol;
1946
- var parameterName = match.groups.parameterName;
1947
- if (parameterName === '') {
1948
- return "continue";
1949
- }
1950
- if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
1951
- throw new PipelineExecutionError('Parameter is already opened or not closed');
1952
- }
1953
- if (parameters[parameterName] === undefined) {
1954
- throw new PipelineExecutionError("Parameter {".concat(parameterName, "} is not defined"));
1955
- }
1956
- var parameterValue = parameters[parameterName];
1957
- if (parameterValue === undefined) {
1958
- throw new PipelineExecutionError("Parameter {".concat(parameterName, "} is not defined"));
1959
- }
1960
- parameterValue = parameterValue.toString();
1961
- if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
1962
- parameterValue = parameterValue
1963
- .split('\n')
1964
- .map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
1965
- .join('\n');
1966
- }
1967
- replacedTemplate =
1968
- replacedTemplate.substring(0, match.index + precol.length) +
1969
- parameterValue +
1970
- replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
1971
- };
1972
- while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
1973
- .exec(replacedTemplate))) {
1974
- _loop_1();
1975
- }
1976
- // [💫] Check if there are parameters that are not closed properly
1977
- if (/{\w+$/.test(replacedTemplate)) {
1978
- throw new PipelineExecutionError('Parameter is not closed');
1979
- }
1980
- // [💫] Check if there are parameters that are not opened properly
1981
- if (/^\w+}/.test(replacedTemplate)) {
1982
- throw new PipelineExecutionError('Parameter is not opened');
1983
- }
1984
- return replacedTemplate;
1985
- }
1986
-
1987
2271
  /**
1988
2272
  * Creates executor function from pipeline and execution tools.
1989
2273
  *
@@ -1992,18 +2276,73 @@ function replaceParameters(template, parameters) {
1992
2276
  */
1993
2277
  function createPipelineExecutor(options) {
1994
2278
  var _this = this;
1995
- var pipeline = options.pipeline, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
1996
- var _b = settings.maxExecutionAttempts, maxExecutionAttempts = _b === void 0 ? 3 : _b;
1997
- validatePipeline(pipeline);
2279
+ var rawPipeline = options.pipeline, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
2280
+ 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;
2281
+ validatePipeline(rawPipeline);
1998
2282
  var llmTools = joinLlmExecutionTools.apply(void 0, __spreadArray([], __read(arrayableToArray(tools.llm)), false));
2283
+ var pipeline;
2284
+ if (isPipelinePrepared(rawPipeline)) {
2285
+ pipeline = rawPipeline;
2286
+ }
2287
+ else {
2288
+ 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 "));
2289
+ }
1999
2290
  var pipelineExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
2291
+ function getContextForTemplate(// <- TODO: [🧠][🥜]
2292
+ template) {
2293
+ return __awaiter(this, void 0, void 0, function () {
2294
+ return __generator(this, function (_a) {
2295
+ // TODO: !!!!!! Implement Better - use real index and keyword search
2296
+ TODO_USE(template);
2297
+ return [2 /*return*/, pipeline.knowledgePieces.map(function (_a) {
2298
+ var content = _a.content;
2299
+ return "- ".concat(content);
2300
+ }).join('\n')];
2301
+ });
2302
+ });
2303
+ }
2304
+ function getReservedParametersForTemplate(template) {
2305
+ return __awaiter(this, void 0, void 0, function () {
2306
+ var context, currentDate, reservedParameters, RESERVED_PARAMETER_NAMES_1, RESERVED_PARAMETER_NAMES_1_1, parameterName;
2307
+ var e_3, _a;
2308
+ return __generator(this, function (_b) {
2309
+ switch (_b.label) {
2310
+ case 0: return [4 /*yield*/, getContextForTemplate(template)];
2311
+ case 1:
2312
+ context = _b.sent();
2313
+ currentDate = new Date().toISOString();
2314
+ reservedParameters = {
2315
+ context: context,
2316
+ currentDate: currentDate,
2317
+ };
2318
+ try {
2319
+ // Note: Doublecheck that ALL reserved parameters are defined:
2320
+ 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()) {
2321
+ parameterName = RESERVED_PARAMETER_NAMES_1_1.value;
2322
+ if (reservedParameters[parameterName] === undefined) {
2323
+ throw new UnexpectedError("Reserved parameter {".concat(parameterName, "} is not defined"));
2324
+ }
2325
+ }
2326
+ }
2327
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
2328
+ finally {
2329
+ try {
2330
+ if (RESERVED_PARAMETER_NAMES_1_1 && !RESERVED_PARAMETER_NAMES_1_1.done && (_a = RESERVED_PARAMETER_NAMES_1.return)) _a.call(RESERVED_PARAMETER_NAMES_1);
2331
+ }
2332
+ finally { if (e_3) throw e_3.error; }
2333
+ }
2334
+ return [2 /*return*/, reservedParameters];
2335
+ }
2336
+ });
2337
+ });
2338
+ }
2000
2339
  function executeSingleTemplate(currentTemplate) {
2001
2340
  return __awaiter(this, void 0, void 0, function () {
2002
- 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;
2003
- var e_2, _j, e_4, _k, e_3, _l, _m;
2341
+ 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;
2342
+ var e_7, _p, e_4, _q, e_6, _r, e_5, _s, _t;
2004
2343
  var _this = this;
2005
- return __generator(this, function (_o) {
2006
- switch (_o.label) {
2344
+ return __generator(this, function (_u) {
2345
+ switch (_u.label) {
2007
2346
  case 0:
2008
2347
  name = "pipeline-executor-frame-".concat(currentTemplate.name);
2009
2348
  title = currentTemplate.title;
@@ -2020,64 +2359,108 @@ function createPipelineExecutor(options) {
2020
2359
  // <- [3]
2021
2360
  })];
2022
2361
  case 1:
2023
- _o.sent();
2024
- _o.label = 2;
2362
+ _u.sent();
2363
+ _u.label = 2;
2025
2364
  case 2:
2365
+ usedParameterNames = extractParametersFromPromptTemplate(currentTemplate);
2366
+ dependentParameterNames = new Set(currentTemplate.dependentParameterNames);
2367
+ if (union(difference(usedParameterNames, dependentParameterNames), difference(dependentParameterNames, usedParameterNames)).size !== 0) {
2368
+ 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 ")));
2369
+ }
2370
+ _b = (_a = Object).freeze;
2371
+ _c = [{}];
2372
+ return [4 /*yield*/, getReservedParametersForTemplate(currentTemplate)];
2373
+ case 3:
2374
+ definedParameters = _b.apply(_a, [__assign.apply(void 0, [__assign.apply(void 0, _c.concat([(_u.sent())])), parametersToPass])]);
2375
+ definedParameterNames = new Set(Object.keys(definedParameters));
2376
+ parameters = {};
2377
+ try {
2378
+ // Note: [2] Check that all used parameters are defined and removing unused parameters for this template
2379
+ for (_d = __values(Array.from(union(definedParameterNames, usedParameterNames, dependentParameterNames))), _e = _d.next(); !_e.done; _e = _d.next()) {
2380
+ parameterName = _e.value;
2381
+ // Situation: Parameter is defined and used
2382
+ if (definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
2383
+ parameters[parameterName] = definedParameters[parameterName];
2384
+ }
2385
+ // Situation: Parameter is defined but NOT used
2386
+ else if (definedParameterNames.has(parameterName) && !usedParameterNames.has(parameterName)) {
2387
+ // Do not pass this parameter to prompt
2388
+ }
2389
+ // Situation: Parameter is NOT defined BUT used
2390
+ else if (!definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
2391
+ // Houston, we have a problem
2392
+ // Note: Checking part is also done in `validatePipeline`, but it’s good to doublecheck
2393
+ 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 ")));
2394
+ }
2395
+ }
2396
+ }
2397
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
2398
+ finally {
2399
+ try {
2400
+ if (_e && !_e.done && (_p = _d.return)) _p.call(_d);
2401
+ }
2402
+ finally { if (e_7) throw e_7.error; }
2403
+ }
2404
+ // Note: Now we can freeze `parameters` because we are sure that all and only used parameters are defined
2405
+ Object.freeze(parameters);
2026
2406
  result = null;
2027
2407
  resultString = null;
2028
2408
  expectError = null;
2029
2409
  maxAttempts = currentTemplate.blockType === 'PROMPT_DIALOG' ? Infinity : maxExecutionAttempts;
2030
- jokers = currentTemplate.jokers || [];
2031
- attempt = -jokers.length;
2032
- _o.label = 3;
2033
- case 3:
2034
- if (!(attempt < maxAttempts)) return [3 /*break*/, 51];
2410
+ jokerParameterNames = currentTemplate.jokerParameterNames || [];
2411
+ attempt = -jokerParameterNames.length;
2412
+ _u.label = 4;
2413
+ case 4:
2414
+ if (!(attempt < maxAttempts)) return [3 /*break*/, 52];
2035
2415
  isJokerAttempt = attempt < 0;
2036
- joker = jokers[jokers.length + attempt];
2037
- if (isJokerAttempt && !joker) {
2416
+ jokerParameterName = jokerParameterNames[jokerParameterNames.length + attempt];
2417
+ if (isJokerAttempt && !jokerParameterName) {
2038
2418
  throw new UnexpectedError("Joker not found in attempt ".concat(attempt));
2039
2419
  }
2040
2420
  result = null;
2041
2421
  resultString = null;
2042
2422
  expectError = null;
2043
2423
  if (isJokerAttempt) {
2044
- if (typeof parametersToPass[joker] === 'undefined') {
2045
- throw new PipelineExecutionError("Joker parameter {".concat(joker, "} not defined"));
2424
+ if (parameters[jokerParameterName] === undefined) {
2425
+ throw new PipelineExecutionError("Joker parameter {".concat(jokerParameterName, "} not defined"));
2426
+ // <- TODO: This is maybe `PipelineLogicError` which should be detected in `validatePipeline` and here just thrown as `UnexpectedError`
2427
+ }
2428
+ else {
2429
+ resultString = parameters[jokerParameterName];
2046
2430
  }
2047
- resultString = parametersToPass[joker];
2048
- }
2049
- _o.label = 4;
2050
- case 4:
2051
- _o.trys.push([4, 47, 48, 49]);
2052
- if (!!isJokerAttempt) return [3 /*break*/, 29];
2053
- _a = currentTemplate.blockType;
2054
- switch (_a) {
2055
- case 'SIMPLE_TEMPLATE': return [3 /*break*/, 5];
2056
- case 'PROMPT_TEMPLATE': return [3 /*break*/, 6];
2057
- case 'SCRIPT': return [3 /*break*/, 15];
2058
- case 'PROMPT_DIALOG': return [3 /*break*/, 26];
2059
2431
  }
2060
- return [3 /*break*/, 28];
2432
+ _u.label = 5;
2061
2433
  case 5:
2062
- resultString = replaceParameters(currentTemplate.content, parametersToPass);
2434
+ _u.trys.push([5, 48, 49, 50]);
2435
+ if (!!isJokerAttempt) return [3 /*break*/, 30];
2436
+ _f = currentTemplate.blockType;
2437
+ switch (_f) {
2438
+ case 'SIMPLE_TEMPLATE': return [3 /*break*/, 6];
2439
+ case 'PROMPT_TEMPLATE': return [3 /*break*/, 7];
2440
+ case 'SCRIPT': return [3 /*break*/, 16];
2441
+ case 'PROMPT_DIALOG': return [3 /*break*/, 27];
2442
+ }
2063
2443
  return [3 /*break*/, 29];
2064
2444
  case 6:
2445
+ resultString = replaceParameters(currentTemplate.content, parameters);
2446
+ return [3 /*break*/, 30];
2447
+ case 7:
2065
2448
  prompt = {
2066
2449
  title: currentTemplate.title,
2067
2450
  pipelineUrl: "".concat(pipeline.pipelineUrl
2068
2451
  ? pipeline.pipelineUrl
2069
2452
  : 'anonymous' /* <- TODO: [🧠] How to deal with anonymous pipelines, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
2070
- parameters: parametersToPass,
2071
- content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
2072
- // <- TODO: !!!!! Apply {context} and knowledges
2073
- // <- TODO: !!!!! Apply samples
2453
+ parameters: parameters,
2454
+ content: currentTemplate.content,
2074
2455
  modelRequirements: currentTemplate.modelRequirements,
2075
- // <- TODO: !!!!! Apply persona
2076
- expectations: currentTemplate.expectations,
2456
+ expectations: __assign(__assign({}, (pipeline.personas.find(function (_a) {
2457
+ var name = _a.name;
2458
+ return name === currentTemplate.personaName;
2459
+ }) || {})), currentTemplate.expectations),
2077
2460
  expectFormat: currentTemplate.expectFormat,
2078
- postprocessing: (currentTemplate.postprocessing || []).map(function (functionName) { return function (result) { return __awaiter(_this, void 0, void 0, function () {
2079
- var errors, _a, _b, scriptTools, error_5, e_5_1;
2080
- var e_5, _c;
2461
+ postprocessing: (currentTemplate.postprocessingFunctionNames || []).map(function (functionName) { return function (result) { return __awaiter(_this, void 0, void 0, function () {
2462
+ var errors, _a, _b, scriptTools, error_5, e_8_1;
2463
+ var e_8, _c;
2081
2464
  return __generator(this, function (_d) {
2082
2465
  switch (_d.label) {
2083
2466
  case 0:
@@ -2098,7 +2481,7 @@ function createPipelineExecutor(options) {
2098
2481
  script: "".concat(functionName, "(result)"),
2099
2482
  parameters: {
2100
2483
  result: result || '',
2101
- // Note: No ...parametersToPass, because working with result only
2484
+ // Note: No ...parametersForTemplate, because working with result only
2102
2485
  },
2103
2486
  })];
2104
2487
  case 4: return [2 /*return*/, _d.sent()];
@@ -2107,6 +2490,9 @@ function createPipelineExecutor(options) {
2107
2490
  if (!(error_5 instanceof Error)) {
2108
2491
  throw error_5;
2109
2492
  }
2493
+ if (error_5 instanceof UnexpectedError) {
2494
+ throw error_5;
2495
+ }
2110
2496
  errors.push(error_5);
2111
2497
  return [3 /*break*/, 6];
2112
2498
  case 6:
@@ -2114,14 +2500,14 @@ function createPipelineExecutor(options) {
2114
2500
  return [3 /*break*/, 2];
2115
2501
  case 7: return [3 /*break*/, 10];
2116
2502
  case 8:
2117
- e_5_1 = _d.sent();
2118
- e_5 = { error: e_5_1 };
2503
+ e_8_1 = _d.sent();
2504
+ e_8 = { error: e_8_1 };
2119
2505
  return [3 /*break*/, 10];
2120
2506
  case 9:
2121
2507
  try {
2122
2508
  if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
2123
2509
  }
2124
- finally { if (e_5) throw e_5.error; }
2510
+ finally { if (e_8) throw e_8.error; }
2125
2511
  return [7 /*endfinally*/];
2126
2512
  case 10:
2127
2513
  if (errors.length === 0) {
@@ -2137,35 +2523,35 @@ function createPipelineExecutor(options) {
2137
2523
  });
2138
2524
  }); }; }),
2139
2525
  };
2140
- _b = currentTemplate.modelRequirements.modelVariant;
2141
- switch (_b) {
2142
- case 'CHAT': return [3 /*break*/, 7];
2143
- case 'COMPLETION': return [3 /*break*/, 9];
2144
- case 'EMBEDDING': return [3 /*break*/, 11];
2526
+ _g = currentTemplate.modelRequirements.modelVariant;
2527
+ switch (_g) {
2528
+ case 'CHAT': return [3 /*break*/, 8];
2529
+ case 'COMPLETION': return [3 /*break*/, 10];
2530
+ case 'EMBEDDING': return [3 /*break*/, 12];
2145
2531
  }
2146
- return [3 /*break*/, 13];
2147
- case 7: return [4 /*yield*/, llmTools.callChatModel(prompt)];
2148
- case 8:
2149
- chatResult = _o.sent();
2532
+ return [3 /*break*/, 14];
2533
+ case 8: return [4 /*yield*/, llmTools.callChatModel(deepFreeze(prompt))];
2534
+ case 9:
2535
+ chatResult = _u.sent();
2150
2536
  // TODO: [🍬] Destroy chatThread
2151
2537
  result = chatResult;
2152
2538
  resultString = chatResult.content;
2153
- return [3 /*break*/, 14];
2154
- case 9: return [4 /*yield*/, llmTools.callCompletionModel(prompt)];
2155
- case 10:
2156
- completionResult = _o.sent();
2539
+ return [3 /*break*/, 15];
2540
+ case 10: return [4 /*yield*/, llmTools.callCompletionModel(deepFreeze(prompt))];
2541
+ case 11:
2542
+ completionResult = _u.sent();
2157
2543
  result = completionResult;
2158
2544
  resultString = completionResult.content;
2159
- return [3 /*break*/, 14];
2160
- case 11: return [4 /*yield*/, llmTools.callEmbeddingModel(prompt)];
2161
- case 12:
2162
- embeddingResult = _o.sent();
2545
+ return [3 /*break*/, 15];
2546
+ case 12: return [4 /*yield*/, llmTools.callEmbeddingModel(deepFreeze(prompt))];
2547
+ case 13:
2548
+ embeddingResult = _u.sent();
2163
2549
  result = embeddingResult;
2164
2550
  resultString = embeddingResult.content.join(',');
2165
- return [3 /*break*/, 14];
2166
- case 13: throw new PipelineExecutionError("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
2167
- case 14: return [3 /*break*/, 29];
2168
- case 15:
2551
+ return [3 /*break*/, 15];
2552
+ case 14: throw new PipelineExecutionError("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
2553
+ case 15: return [3 /*break*/, 30];
2554
+ case 16:
2169
2555
  if (arrayableToArray(tools.script).length === 0) {
2170
2556
  throw new PipelineExecutionError('No script execution tools are available');
2171
2557
  }
@@ -2174,49 +2560,52 @@ function createPipelineExecutor(options) {
2174
2560
  }
2175
2561
  // TODO: DRY [1]
2176
2562
  scriptPipelineExecutionErrors = [];
2177
- _o.label = 16;
2178
- case 16:
2179
- _o.trys.push([16, 23, 24, 25]);
2180
- _c = (e_2 = void 0, __values(arrayableToArray(tools.script))), _d = _c.next();
2181
- _o.label = 17;
2563
+ _u.label = 17;
2182
2564
  case 17:
2183
- if (!!_d.done) return [3 /*break*/, 22];
2184
- scriptTools = _d.value;
2185
- _o.label = 18;
2565
+ _u.trys.push([17, 24, 25, 26]);
2566
+ _h = (e_4 = void 0, __values(arrayableToArray(tools.script))), _j = _h.next();
2567
+ _u.label = 18;
2186
2568
  case 18:
2187
- _o.trys.push([18, 20, , 21]);
2188
- return [4 /*yield*/, scriptTools.execute({
2569
+ if (!!_j.done) return [3 /*break*/, 23];
2570
+ scriptTools = _j.value;
2571
+ _u.label = 19;
2572
+ case 19:
2573
+ _u.trys.push([19, 21, , 22]);
2574
+ return [4 /*yield*/, scriptTools.execute(deepFreeze({
2189
2575
  scriptLanguage: currentTemplate.contentLanguage,
2190
2576
  script: currentTemplate.content,
2191
- parameters: parametersToPass,
2192
- })];
2193
- case 19:
2194
- resultString = _o.sent();
2195
- return [3 /*break*/, 22];
2577
+ parameters: parameters,
2578
+ }))];
2196
2579
  case 20:
2197
- error_2 = _o.sent();
2580
+ resultString = _u.sent();
2581
+ return [3 /*break*/, 23];
2582
+ case 21:
2583
+ error_2 = _u.sent();
2198
2584
  if (!(error_2 instanceof Error)) {
2199
2585
  throw error_2;
2200
2586
  }
2587
+ if (error_2 instanceof UnexpectedError) {
2588
+ throw error_2;
2589
+ }
2201
2590
  scriptPipelineExecutionErrors.push(error_2);
2202
- return [3 /*break*/, 21];
2203
- case 21:
2204
- _d = _c.next();
2205
- return [3 /*break*/, 17];
2206
- case 22: return [3 /*break*/, 25];
2207
- case 23:
2208
- e_2_1 = _o.sent();
2209
- e_2 = { error: e_2_1 };
2210
- return [3 /*break*/, 25];
2591
+ return [3 /*break*/, 22];
2592
+ case 22:
2593
+ _j = _h.next();
2594
+ return [3 /*break*/, 18];
2595
+ case 23: return [3 /*break*/, 26];
2211
2596
  case 24:
2597
+ e_4_1 = _u.sent();
2598
+ e_4 = { error: e_4_1 };
2599
+ return [3 /*break*/, 26];
2600
+ case 25:
2212
2601
  try {
2213
- if (_d && !_d.done && (_j = _c.return)) _j.call(_c);
2602
+ if (_j && !_j.done && (_q = _h.return)) _q.call(_h);
2214
2603
  }
2215
- finally { if (e_2) throw e_2.error; }
2604
+ finally { if (e_4) throw e_4.error; }
2216
2605
  return [7 /*endfinally*/];
2217
- case 25:
2606
+ case 26:
2218
2607
  if (resultString !== null) {
2219
- return [3 /*break*/, 29];
2608
+ return [3 /*break*/, 30];
2220
2609
  }
2221
2610
  if (scriptPipelineExecutionErrors.length === 1) {
2222
2611
  throw scriptPipelineExecutionErrors[0];
@@ -2226,101 +2615,104 @@ function createPipelineExecutor(options) {
2226
2615
  .map(function (error) { return '- ' + error.message; })
2227
2616
  .join('\n\n')), "\n "); }));
2228
2617
  }
2229
- case 26:
2618
+ case 27:
2230
2619
  if (tools.userInterface === undefined) {
2231
2620
  throw new PipelineExecutionError('User interface tools are not available');
2232
2621
  }
2233
- return [4 /*yield*/, tools.userInterface.promptDialog({
2622
+ return [4 /*yield*/, tools.userInterface.promptDialog(deepFreeze({
2234
2623
  promptTitle: currentTemplate.title,
2235
- promptMessage: replaceParameters(currentTemplate.description || '', parametersToPass),
2236
- defaultValue: replaceParameters(currentTemplate.content, parametersToPass),
2624
+ promptMessage: replaceParameters(currentTemplate.description || '', parameters),
2625
+ defaultValue: replaceParameters(currentTemplate.content, parameters),
2237
2626
  // TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
2238
2627
  placeholder: undefined,
2239
2628
  priority: priority,
2240
- })];
2241
- case 27:
2629
+ }))];
2630
+ case 28:
2242
2631
  // TODO: [🌹] When making next attempt for `PROMPT DIALOG`, preserve the previous user input
2243
- resultString = _o.sent();
2244
- return [3 /*break*/, 29];
2245
- case 28: throw new PipelineExecutionError("Unknown execution type \"".concat(currentTemplate.blockType, "\""));
2246
- case 29:
2247
- if (!(!isJokerAttempt && currentTemplate.postprocessing)) return [3 /*break*/, 46];
2248
- _o.label = 30;
2632
+ resultString = _u.sent();
2633
+ return [3 /*break*/, 30];
2634
+ case 29: throw new PipelineExecutionError("Unknown execution type \"".concat(currentTemplate.blockType, "\""));
2249
2635
  case 30:
2250
- _o.trys.push([30, 44, 45, 46]);
2251
- _e = (e_4 = void 0, __values(currentTemplate.postprocessing)), _f = _e.next();
2252
- _o.label = 31;
2636
+ if (!(!isJokerAttempt && currentTemplate.postprocessingFunctionNames)) return [3 /*break*/, 47];
2637
+ _u.label = 31;
2253
2638
  case 31:
2254
- if (!!_f.done) return [3 /*break*/, 43];
2255
- functionName = _f.value;
2639
+ _u.trys.push([31, 45, 46, 47]);
2640
+ _k = (e_6 = void 0, __values(currentTemplate.postprocessingFunctionNames)), _l = _k.next();
2641
+ _u.label = 32;
2642
+ case 32:
2643
+ if (!!_l.done) return [3 /*break*/, 44];
2644
+ functionName = _l.value;
2256
2645
  // TODO: DRY [1]
2257
2646
  scriptPipelineExecutionErrors = [];
2258
2647
  postprocessingError = null;
2259
- _o.label = 32;
2260
- case 32:
2261
- _o.trys.push([32, 39, 40, 41]);
2262
- _g = (e_3 = void 0, __values(arrayableToArray(tools.script))), _h = _g.next();
2263
- _o.label = 33;
2648
+ _u.label = 33;
2264
2649
  case 33:
2265
- if (!!_h.done) return [3 /*break*/, 38];
2266
- scriptTools = _h.value;
2267
- _o.label = 34;
2650
+ _u.trys.push([33, 40, 41, 42]);
2651
+ _m = (e_5 = void 0, __values(arrayableToArray(tools.script))), _o = _m.next();
2652
+ _u.label = 34;
2268
2653
  case 34:
2269
- _o.trys.push([34, 36, , 37]);
2654
+ if (!!_o.done) return [3 /*break*/, 39];
2655
+ scriptTools = _o.value;
2656
+ _u.label = 35;
2657
+ case 35:
2658
+ _u.trys.push([35, 37, , 38]);
2270
2659
  return [4 /*yield*/, scriptTools.execute({
2271
2660
  scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
2272
2661
  script: "".concat(functionName, "(resultString)"),
2273
2662
  parameters: {
2274
2663
  resultString: resultString || '',
2275
- // Note: No ...parametersToPass, because working with result only
2664
+ // Note: No ...parametersForTemplate, because working with result only
2276
2665
  },
2277
2666
  })];
2278
- case 35:
2279
- resultString = _o.sent();
2280
- postprocessingError = null;
2281
- return [3 /*break*/, 38];
2282
2667
  case 36:
2283
- error_3 = _o.sent();
2668
+ resultString = _u.sent();
2669
+ postprocessingError = null;
2670
+ return [3 /*break*/, 39];
2671
+ case 37:
2672
+ error_3 = _u.sent();
2284
2673
  if (!(error_3 instanceof Error)) {
2285
2674
  throw error_3;
2286
2675
  }
2676
+ if (error_3 instanceof UnexpectedError) {
2677
+ throw error_3;
2678
+ }
2287
2679
  postprocessingError = error_3;
2288
2680
  scriptPipelineExecutionErrors.push(error_3);
2289
- return [3 /*break*/, 37];
2290
- case 37:
2291
- _h = _g.next();
2292
- return [3 /*break*/, 33];
2293
- case 38: return [3 /*break*/, 41];
2294
- case 39:
2295
- e_3_1 = _o.sent();
2296
- e_3 = { error: e_3_1 };
2297
- return [3 /*break*/, 41];
2681
+ return [3 /*break*/, 38];
2682
+ case 38:
2683
+ _o = _m.next();
2684
+ return [3 /*break*/, 34];
2685
+ case 39: return [3 /*break*/, 42];
2298
2686
  case 40:
2687
+ e_5_1 = _u.sent();
2688
+ e_5 = { error: e_5_1 };
2689
+ return [3 /*break*/, 42];
2690
+ case 41:
2299
2691
  try {
2300
- if (_h && !_h.done && (_l = _g.return)) _l.call(_g);
2692
+ if (_o && !_o.done && (_s = _m.return)) _s.call(_m);
2301
2693
  }
2302
- finally { if (e_3) throw e_3.error; }
2694
+ finally { if (e_5) throw e_5.error; }
2303
2695
  return [7 /*endfinally*/];
2304
- case 41:
2696
+ case 42:
2305
2697
  if (postprocessingError) {
2306
2698
  throw postprocessingError;
2307
2699
  }
2308
- _o.label = 42;
2309
- case 42:
2310
- _f = _e.next();
2311
- return [3 /*break*/, 31];
2312
- case 43: return [3 /*break*/, 46];
2313
- case 44:
2314
- e_4_1 = _o.sent();
2315
- e_4 = { error: e_4_1 };
2316
- return [3 /*break*/, 46];
2700
+ _u.label = 43;
2701
+ case 43:
2702
+ _l = _k.next();
2703
+ return [3 /*break*/, 32];
2704
+ case 44: return [3 /*break*/, 47];
2317
2705
  case 45:
2706
+ e_6_1 = _u.sent();
2707
+ e_6 = { error: e_6_1 };
2708
+ return [3 /*break*/, 47];
2709
+ case 46:
2318
2710
  try {
2319
- if (_f && !_f.done && (_k = _e.return)) _k.call(_e);
2711
+ if (_l && !_l.done && (_r = _k.return)) _r.call(_k);
2320
2712
  }
2321
- finally { if (e_4) throw e_4.error; }
2713
+ finally { if (e_6) throw e_6.error; }
2322
2714
  return [7 /*endfinally*/];
2323
- case 46:
2715
+ case 47:
2324
2716
  // TODO: [💝] Unite object for expecting amount and format
2325
2717
  if (currentTemplate.expectFormat) {
2326
2718
  if (currentTemplate.expectFormat === 'JSON') {
@@ -2333,15 +2725,18 @@ function createPipelineExecutor(options) {
2333
2725
  if (currentTemplate.expectations) {
2334
2726
  checkExpectations(currentTemplate.expectations, resultString || '');
2335
2727
  }
2336
- return [3 /*break*/, 51];
2337
- case 47:
2338
- error_4 = _o.sent();
2728
+ return [3 /*break*/, 52];
2729
+ case 48:
2730
+ error_4 = _u.sent();
2339
2731
  if (!(error_4 instanceof ExpectError)) {
2340
2732
  throw error_4;
2341
2733
  }
2734
+ if (error_4 instanceof UnexpectedError) {
2735
+ throw error_4;
2736
+ }
2342
2737
  expectError = error_4;
2343
- return [3 /*break*/, 49];
2344
- case 48:
2738
+ return [3 /*break*/, 50];
2739
+ case 49:
2345
2740
  if (!isJokerAttempt &&
2346
2741
  currentTemplate.blockType === 'PROMPT_TEMPLATE' &&
2347
2742
  prompt
@@ -2363,15 +2758,15 @@ function createPipelineExecutor(options) {
2363
2758
  });
2364
2759
  }
2365
2760
  return [7 /*endfinally*/];
2366
- case 49:
2761
+ case 50:
2367
2762
  if (expectError !== null && attempt === maxAttempts - 1) {
2368
2763
  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 "); }));
2369
2764
  }
2370
- _o.label = 50;
2371
- case 50:
2372
- attempt++;
2373
- return [3 /*break*/, 3];
2765
+ _u.label = 51;
2374
2766
  case 51:
2767
+ attempt++;
2768
+ return [3 /*break*/, 4];
2769
+ case 52:
2375
2770
  if (resultString === null) {
2376
2771
  throw new UnexpectedError('Something went wrong and prompt result is null');
2377
2772
  }
@@ -2387,18 +2782,27 @@ function createPipelineExecutor(options) {
2387
2782
  // <- [3]
2388
2783
  });
2389
2784
  }
2390
- 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));
2785
+ 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)));
2391
2786
  return [2 /*return*/];
2392
2787
  }
2393
2788
  });
2394
2789
  });
2395
2790
  }
2396
- var parametersToPass, executionReport, resovedParameters_1, unresovedTemplates, resolving_1, loopLimit, _loop_1, error_1, usage_1, _a, _b, parameter, usage;
2397
- var e_1, _c;
2398
- return __generator(this, function (_d) {
2399
- switch (_d.label) {
2791
+ var executionReport, _a, _b, parameter, parametersToPass, resovedParameters_1, unresovedTemplates, resolving_1, loopLimit, _loop_1, error_1, usage_1, outputParameters, errors, _c, _d, parameter, usage;
2792
+ var e_1, _e, e_2, _f;
2793
+ return __generator(this, function (_g) {
2794
+ switch (_g.label) {
2400
2795
  case 0:
2401
- parametersToPass = inputParameters;
2796
+ if (!(pipeline === undefined)) return [3 /*break*/, 2];
2797
+ return [4 /*yield*/, preparePipeline(rawPipeline, {
2798
+ llmTools: llmTools,
2799
+ isVerbose: isVerbose,
2800
+ maxParallelCount: maxParallelCount,
2801
+ })];
2802
+ case 1:
2803
+ pipeline = _g.sent();
2804
+ _g.label = 2;
2805
+ case 2:
2402
2806
  executionReport = {
2403
2807
  pipelineUrl: pipeline.pipelineUrl,
2404
2808
  title: pipeline.title,
@@ -2407,9 +2811,35 @@ function createPipelineExecutor(options) {
2407
2811
  description: pipeline.description,
2408
2812
  promptExecutions: [],
2409
2813
  };
2410
- _d.label = 1;
2411
- case 1:
2412
- _d.trys.push([1, 6, , 7]);
2814
+ try {
2815
+ // Note: Check that all input input parameters are defined
2816
+ for (_a = __values(pipeline.parameters), _b = _a.next(); !_b.done; _b = _a.next()) {
2817
+ parameter = _b.value;
2818
+ if (parameter.isInput && inputParameters[parameter.name] === undefined) {
2819
+ return [2 /*return*/, deepFreezeWithSameType({
2820
+ isSuccessful: false,
2821
+ errors: [
2822
+ new PipelineExecutionError("Parameter {".concat(parameter.name, "} is required as an input parameter")),
2823
+ // <- TODO: !!!!! Test this error
2824
+ ],
2825
+ executionReport: executionReport,
2826
+ outputParameters: {},
2827
+ usage: ZERO_USAGE,
2828
+ })];
2829
+ }
2830
+ }
2831
+ }
2832
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2833
+ finally {
2834
+ try {
2835
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
2836
+ }
2837
+ finally { if (e_1) throw e_1.error; }
2838
+ }
2839
+ parametersToPass = inputParameters;
2840
+ _g.label = 3;
2841
+ case 3:
2842
+ _g.trys.push([3, 8, , 9]);
2413
2843
  resovedParameters_1 = pipeline.parameters
2414
2844
  .filter(function (_a) {
2415
2845
  var isInput = _a.isInput;
@@ -2424,8 +2854,8 @@ function createPipelineExecutor(options) {
2424
2854
  loopLimit = LOOP_LIMIT;
2425
2855
  _loop_1 = function () {
2426
2856
  var currentTemplate, work_1;
2427
- return __generator(this, function (_e) {
2428
- switch (_e.label) {
2857
+ return __generator(this, function (_h) {
2858
+ switch (_h.label) {
2429
2859
  case 0:
2430
2860
  if (loopLimit-- < 0) {
2431
2861
  // Note: Really UnexpectedError not LimitReachedError - this should be catched during validatePipeline
@@ -2440,7 +2870,7 @@ function createPipelineExecutor(options) {
2440
2870
  if (!!currentTemplate) return [3 /*break*/, 3];
2441
2871
  /* [5] */ return [4 /*yield*/, Promise.race(resolving_1)];
2442
2872
  case 2:
2443
- /* [5] */ _e.sent();
2873
+ /* [5] */ _h.sent();
2444
2874
  return [3 /*break*/, 4];
2445
2875
  case 3:
2446
2876
  unresovedTemplates = unresovedTemplates.filter(function (template) { return template !== currentTemplate; });
@@ -2452,24 +2882,24 @@ function createPipelineExecutor(options) {
2452
2882
  resolving_1 = resolving_1.filter(function (w) { return w !== work_1; });
2453
2883
  });
2454
2884
  resolving_1.push(work_1);
2455
- _e.label = 4;
2885
+ _h.label = 4;
2456
2886
  case 4: return [2 /*return*/];
2457
2887
  }
2458
2888
  });
2459
2889
  };
2460
- _d.label = 2;
2461
- case 2:
2462
- if (!(unresovedTemplates.length > 0)) return [3 /*break*/, 4];
2890
+ _g.label = 4;
2891
+ case 4:
2892
+ if (!(unresovedTemplates.length > 0)) return [3 /*break*/, 6];
2463
2893
  return [5 /*yield**/, _loop_1()];
2464
- case 3:
2465
- _d.sent();
2466
- return [3 /*break*/, 2];
2467
- case 4: return [4 /*yield*/, Promise.all(resolving_1)];
2468
2894
  case 5:
2469
- _d.sent();
2470
- return [3 /*break*/, 7];
2471
- case 6:
2472
- error_1 = _d.sent();
2895
+ _g.sent();
2896
+ return [3 /*break*/, 4];
2897
+ case 6: return [4 /*yield*/, Promise.all(resolving_1)];
2898
+ case 7:
2899
+ _g.sent();
2900
+ return [3 /*break*/, 9];
2901
+ case 8:
2902
+ error_1 = _g.sent();
2473
2903
  if (!(error_1 instanceof Error)) {
2474
2904
  throw error_1;
2475
2905
  }
@@ -2477,49 +2907,56 @@ function createPipelineExecutor(options) {
2477
2907
  var result = _a.result;
2478
2908
  return (result === null || result === void 0 ? void 0 : result.usage) || ZERO_USAGE;
2479
2909
  })), false));
2480
- return [2 /*return*/, {
2910
+ return [2 /*return*/, deepFreezeWithSameType({
2481
2911
  isSuccessful: false,
2482
2912
  errors: [error_1],
2483
2913
  usage: usage_1,
2484
2914
  executionReport: executionReport,
2485
2915
  outputParameters: parametersToPass,
2486
- }];
2487
- case 7:
2916
+ })];
2917
+ case 9:
2918
+ outputParameters = {};
2919
+ errors = [];
2488
2920
  try {
2489
2921
  // Note: Filter ONLY output parameters
2490
- for (_a = __values(pipeline.parameters), _b = _a.next(); !_b.done; _b = _a.next()) {
2491
- parameter = _b.value;
2492
- if (parameter.isOutput) {
2922
+ for (_c = __values(pipeline.parameters.filter(function (_a) {
2923
+ var isOutput = _a.isOutput;
2924
+ return isOutput;
2925
+ })), _d = _c.next(); !_d.done; _d = _c.next()) {
2926
+ parameter = _d.value;
2927
+ if (parametersToPass[parameter.name] === undefined) {
2928
+ errors.push(new PipelineExecutionError("Parameter {".concat(parameter.name, "} is required as an output parameter but not set in the pipeline")));
2493
2929
  continue;
2494
2930
  }
2495
- delete parametersToPass[parameter.name];
2931
+ outputParameters[parameter.name] = parametersToPass[parameter.name] || '';
2496
2932
  }
2497
2933
  }
2498
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
2934
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
2499
2935
  finally {
2500
2936
  try {
2501
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
2937
+ if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
2502
2938
  }
2503
- finally { if (e_1) throw e_1.error; }
2939
+ finally { if (e_2) throw e_2.error; }
2504
2940
  }
2505
2941
  usage = addUsage.apply(void 0, __spreadArray([], __read(executionReport.promptExecutions.map(function (_a) {
2506
2942
  var result = _a.result;
2507
2943
  return (result === null || result === void 0 ? void 0 : result.usage) || ZERO_USAGE;
2508
2944
  })), false));
2509
- return [2 /*return*/, {
2945
+ return [2 /*return*/, deepFreezeWithSameType({
2510
2946
  isSuccessful: true,
2511
- errors: [],
2947
+ errors: errors,
2512
2948
  usage: usage,
2513
2949
  executionReport: executionReport,
2514
- outputParameters: parametersToPass,
2515
- }];
2950
+ outputParameters: outputParameters,
2951
+ })];
2516
2952
  }
2517
2953
  });
2518
2954
  }); };
2519
2955
  return pipelineExecutor;
2520
2956
  }
2521
2957
  /**
2522
- * TODO: [🪂] Pass maxParallelCount here
2958
+ * TODO: Use isVerbose here (not only pass to `preparePipeline`)
2959
+ * TODO: [🪂] Use maxParallelCount here (not only pass to `preparePipeline`)
2523
2960
  * TODO: [♈] Probbably move expectations from templates to parameters
2524
2961
  * TODO: [🧠] When not meet expectations in PROMPT_DIALOG, make some way to tell the user
2525
2962
  * TODO: [👧] Strongly type the executors to avoid need of remove nullables whtn noUncheckedIndexedAccess in tsconfig.json
@@ -2528,23 +2965,6 @@ function createPipelineExecutor(options) {
2528
2965
  * TODO: [🛠] Actions, instruments (and maybe knowledge) => Functions and tools
2529
2966
  */
2530
2967
 
2531
- /**
2532
- * Just marks a place of place where should be something implemented
2533
- * No side effects.
2534
- *
2535
- * Note: It can be usefull suppressing eslint errors of unused variables
2536
- *
2537
- * @param value any values
2538
- * @returns void
2539
- * @private within the repository
2540
- */
2541
- function TODO_USE() {
2542
- var value = [];
2543
- for (var _i = 0; _i < arguments.length; _i++) {
2544
- value[_i] = arguments[_i];
2545
- }
2546
- }
2547
-
2548
2968
  /**
2549
2969
  * @@@
2550
2970
  */
@@ -2672,7 +3092,7 @@ function prepareKnowledgeFromMarkdown(content /* <- TODO: [🖖] (?maybe not) Al
2672
3092
  });
2673
3093
  }
2674
3094
  /**
2675
- * TODO: [🔼] !!! Export via `@promptbook/markdown`
3095
+ * TODO: [🐝][🔼] !!! Export via `@promptbook/markdown`
2676
3096
  * TODO: [🪂] Do it in parallel 11:11
2677
3097
  * Note: No need to aggregate usage here, it is done by intercepting the llmTools
2678
3098
  */
@@ -2695,7 +3115,7 @@ function prepareKnowledgePieces(knowledgeSources, options) {
2695
3115
  var partialPieces, pieces;
2696
3116
  return __generator(this, function (_a) {
2697
3117
  switch (_a.label) {
2698
- case 0: return [4 /*yield*/, prepareKnowledgeFromMarkdown(knowledgeSource.source, // <- TODO: [🐝] !!!!! Unhardcode markdown, detect which type it is
3118
+ case 0: return [4 /*yield*/, prepareKnowledgeFromMarkdown(knowledgeSource.source, // <- TODO: [🐝] !!! Unhardcode markdown, detect which type it is
2699
3119
  options)];
2700
3120
  case 1:
2701
3121
  partialPieces = _a.sent();
@@ -2733,7 +3153,7 @@ TODO: [🧊] This is how it can look in future
2733
3153
  > ):
2734
3154
  */
2735
3155
  /**
2736
- * TODO: [🔼] !!! Export via `@promptbook/core`
3156
+ * TODO: [🐝][🔼] !!! Export via `@promptbook/core`
2737
3157
  * TODO: [🧊] In future one preparation can take data from previous preparation and save tokens and time
2738
3158
  * Put `knowledgePieces` into `PrepareKnowledgeOptions`
2739
3159
  * TODO: [🪂] More than max things can run in parallel by acident [1,[2a,2b,_],[3a,3b,_]]
@@ -2859,12 +3279,15 @@ function preparePipeline(pipeline, options) {
2859
3279
  partialknowledgePiecesPrepared = _b.sent();
2860
3280
  knowledgePiecesPrepared = partialknowledgePiecesPrepared.map(function (piece) { return (__assign(__assign({}, piece), { preparationIds: [/* TODO: [🧊] -> */ currentPreparation.id] })); });
2861
3281
  // ----- /Knowledge preparation -----
3282
+ // TODO: !!!!! Add context to each template (if missing)
3283
+ // TODO: !!!!! Apply samples to each template (if missing)
2862
3284
  return [2 /*return*/, __assign(__assign({}, pipeline), { knowledgeSources: knowledgeSourcesPrepared, knowledgePieces: knowledgePiecesPrepared, personas: preparedPersonas, preparations: preparations })];
2863
3285
  }
2864
3286
  });
2865
3287
  });
2866
3288
  }
2867
3289
  /**
3290
+ * TODO: !!!!! Index the samples and maybe templates
2868
3291
  * TODO: [🔼] !!! Export via `@promptbook/core`
2869
3292
  * TODO: Write tests for `preparePipeline`
2870
3293
  * TODO: [🏏] Leverage the batch API and build queues @see https://platform.openai.com/docs/guides/batch
@@ -2920,7 +3343,7 @@ var knowledgeCommandParser = {
2920
3343
  /**
2921
3344
  * Link to discussion
2922
3345
  */
2923
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/41',
3346
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/41',
2924
3347
  /**
2925
3348
  * Example usages of the KNOWLEDGE command
2926
3349
  */
@@ -2957,8 +3380,9 @@ var knowledgeCommandParser = {
2957
3380
  /**
2958
3381
  * Note: Prototype of [🍧] (remove this comment after full implementation)
2959
3382
  */
2960
- applyToPipelineJson: function (pipelineJson, personaCommand) {
3383
+ applyToPipelineJson: function (personaCommand, subjects) {
2961
3384
  var source = personaCommand.source;
3385
+ var pipelineJson = subjects.pipelineJson;
2962
3386
  var name = titleToName(source);
2963
3387
  pipelineJson.knowledgeSources.push({
2964
3388
  name: name,
@@ -2993,7 +3417,7 @@ var personaCommandParser = {
2993
3417
  /**
2994
3418
  * Link to discussion
2995
3419
  */
2996
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/22',
3420
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/22',
2997
3421
  /**
2998
3422
  * Example usages of the PERSONA command
2999
3423
  */
@@ -3021,8 +3445,15 @@ var personaCommandParser = {
3021
3445
  /**
3022
3446
  * Note: Prototype of [🍧] (remove this comment after full implementation)
3023
3447
  */
3024
- applyToPipelineJson: function (pipelineJson, personaCommand) {
3448
+ applyToPipelineJson: function (personaCommand, subjects) {
3025
3449
  var personaName = personaCommand.personaName, personaDescription = personaCommand.personaDescription;
3450
+ var pipelineJson = subjects.pipelineJson, templateJson = subjects.templateJson;
3451
+ if (templateJson !== null) {
3452
+ if (templateJson.blockType !== 'PROMPT_TEMPLATE') {
3453
+ throw new ParsingError("PERSONA command can be used only in PROMPT_TEMPLATE block");
3454
+ }
3455
+ templateJson.personaName = personaName;
3456
+ }
3026
3457
  var persona = pipelineJson.personas.find(function (persona) { return persona.name === personaName; });
3027
3458
  if (persona === undefined) {
3028
3459
  pipelineJson.personas.push({
@@ -3181,7 +3612,7 @@ var blockCommandParser = {
3181
3612
  /**
3182
3613
  * Link to discussion
3183
3614
  */
3184
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/64',
3615
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/64',
3185
3616
  /**
3186
3617
  * Example usages of the BLOCK command
3187
3618
  */
@@ -3209,6 +3640,7 @@ var blockCommandParser = {
3209
3640
  //---
3210
3641
  /* <- TODO: [🧠] Maybe dynamic */
3211
3642
  ],
3643
+ // TODO: [♓️] order: -10 /* <- Note: Putting before other commands */
3212
3644
  /**
3213
3645
  * Parses the BLOCK command
3214
3646
  */
@@ -3325,7 +3757,7 @@ var expectCommandParser = {
3325
3757
  /**
3326
3758
  * Link to discussion
3327
3759
  */
3328
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/30',
3760
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/30',
3329
3761
  /**
3330
3762
  * Example usages of the EXPECT command
3331
3763
  */
@@ -3443,7 +3875,7 @@ var jokerCommandParser = {
3443
3875
  /**
3444
3876
  * Link to discussion
3445
3877
  */
3446
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/66',
3878
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/66',
3447
3879
  /**
3448
3880
  * Example usages of the JOKER command
3449
3881
  */
@@ -3486,7 +3918,11 @@ var modelCommandParser = {
3486
3918
  /**
3487
3919
  * BOILERPLATE command can be used in:
3488
3920
  */
3489
- usagePlaces: ['PIPELINE_HEAD', 'PIPELINE_TEMPLATE'],
3921
+ usagePlaces: [
3922
+ 'PIPELINE_HEAD',
3923
+ // <- TODO: [🧠][❔] Should there be possibility to set MODEL for entire pipeline?
3924
+ 'PIPELINE_TEMPLATE',
3925
+ ],
3490
3926
  /**
3491
3927
  * Description of the MODEL command
3492
3928
  */
@@ -3494,7 +3930,7 @@ var modelCommandParser = {
3494
3930
  /**
3495
3931
  * Link to discussion
3496
3932
  */
3497
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/67',
3933
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/67',
3498
3934
  /**
3499
3935
  * Example usages of the MODEL command
3500
3936
  */
@@ -3574,7 +4010,7 @@ var parameterCommandParser = {
3574
4010
  /**
3575
4011
  * Link to discussion
3576
4012
  */
3577
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/68',
4013
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/68',
3578
4014
  /**
3579
4015
  * Example usages of the PARAMETER command
3580
4016
  */
@@ -3638,7 +4074,7 @@ var postprocessCommandParser = {
3638
4074
  /**
3639
4075
  * Link to discussion
3640
4076
  */
3641
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/31',
4077
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/31',
3642
4078
  /**
3643
4079
  * Example usages of the POSTPROCESS command
3644
4080
  */
@@ -3693,7 +4129,7 @@ var promptbookVersionCommandParser = {
3693
4129
  /**
3694
4130
  * Link to discussion
3695
4131
  */
3696
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/69',
4132
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/69',
3697
4133
  /**
3698
4134
  * Example usages of the PROMPTBOOK_VERSION command
3699
4135
  */
@@ -3746,7 +4182,7 @@ var urlCommandParser = {
3746
4182
  /**
3747
4183
  * Link to discussion
3748
4184
  */
3749
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/70',
4185
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/70',
3750
4186
  /**
3751
4187
  * Example usages of the URL command
3752
4188
  */
@@ -3816,7 +4252,7 @@ var actionCommandParser = {
3816
4252
  /**
3817
4253
  * Link to discussion
3818
4254
  */
3819
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/72',
4255
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/72',
3820
4256
  /**
3821
4257
  * Example usages of the ACTION command
3822
4258
  */
@@ -3855,7 +4291,7 @@ var instrumentCommandParser = {
3855
4291
  /**
3856
4292
  * Link to discussion
3857
4293
  */
3858
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/71',
4294
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/71',
3859
4295
  /**
3860
4296
  * Example usages of the INSTRUMENT command
3861
4297
  */
@@ -3898,7 +4334,7 @@ var boilerplateCommandParser = {
3898
4334
  /**
3899
4335
  * Link to discussion
3900
4336
  */
3901
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/@@',
4337
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/@@',
3902
4338
  /**
3903
4339
  * Example usages of the BOILERPLATE command
3904
4340
  */
@@ -4032,9 +4468,9 @@ function parseCommand(raw, usagePlace) {
4032
4468
  */
4033
4469
  function getSupportedCommandsMessage() {
4034
4470
  return COMMANDS.flatMap(function (_a) {
4035
- var name = _a.name, aliasNames = _a.aliasNames, description = _a.description, discussionUrl = _a.discussionUrl;
4471
+ var name = _a.name, aliasNames = _a.aliasNames, description = _a.description, documentationUrl = _a.documentationUrl;
4036
4472
  return __spreadArray([
4037
- "- **".concat(name, "** ").concat(description, ", see [discussion](").concat(discussionUrl, ")")
4473
+ "- **".concat(name, "** ").concat(description, ", see [discussion](").concat(documentationUrl, ")")
4038
4474
  ], __read((aliasNames || []).map(function (aliasName) { return " - **".concat(aliasName, "** Alias for **").concat(name, "**"); })), false);
4039
4475
  }).join('\n');
4040
4476
  }
@@ -4389,195 +4825,6 @@ function removeContentComments(content) {
4389
4825
  return spaceTrim$1(content.replace(/<!--(.*?)-->/gs, ''));
4390
4826
  }
4391
4827
 
4392
- /**
4393
- * Create difference set of two sets.
4394
- *
4395
- * @deprecated use new javascript set methods instead @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
4396
- */
4397
- function difference(a, b, isEqual) {
4398
- var e_1, _a;
4399
- if (isEqual === void 0) { isEqual = function (a, b) { return a === b; }; }
4400
- var diff = new Set();
4401
- var _loop_1 = function (itemA) {
4402
- if (!Array.from(b).some(function (itemB) { return isEqual(itemA, itemB); })) {
4403
- diff.add(itemA);
4404
- }
4405
- };
4406
- try {
4407
- for (var _b = __values(Array.from(a)), _c = _b.next(); !_c.done; _c = _b.next()) {
4408
- var itemA = _c.value;
4409
- _loop_1(itemA);
4410
- }
4411
- }
4412
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
4413
- finally {
4414
- try {
4415
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
4416
- }
4417
- finally { if (e_1) throw e_1.error; }
4418
- }
4419
- return diff;
4420
- }
4421
-
4422
- /**
4423
- * Creates a new set with all elements that are present in either set
4424
- *
4425
- * @deprecated use new javascript set methods instead @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
4426
- */
4427
- function union() {
4428
- var e_1, _a, e_2, _b;
4429
- var sets = [];
4430
- for (var _i = 0; _i < arguments.length; _i++) {
4431
- sets[_i] = arguments[_i];
4432
- }
4433
- var union = new Set();
4434
- try {
4435
- for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
4436
- var set = sets_1_1.value;
4437
- try {
4438
- for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
4439
- var item = _d.value;
4440
- union.add(item);
4441
- }
4442
- }
4443
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
4444
- finally {
4445
- try {
4446
- if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
4447
- }
4448
- finally { if (e_2) throw e_2.error; }
4449
- }
4450
- }
4451
- }
4452
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
4453
- finally {
4454
- try {
4455
- if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
4456
- }
4457
- finally { if (e_1) throw e_1.error; }
4458
- }
4459
- return union;
4460
- }
4461
-
4462
- /**
4463
- * Parses the template and returns the list of all parameter names
4464
- *
4465
- * @param template the template with parameters in {curly} braces
4466
- * @returns the list of parameter names
4467
- */
4468
- function extractParameters(template) {
4469
- var e_1, _a;
4470
- var matches = template.matchAll(/{\w+}/g);
4471
- var parameterNames = new Set();
4472
- try {
4473
- for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
4474
- var match = matches_1_1.value;
4475
- var parameterName = match[0].slice(1, -1);
4476
- parameterNames.add(parameterName);
4477
- }
4478
- }
4479
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
4480
- finally {
4481
- try {
4482
- if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
4483
- }
4484
- finally { if (e_1) throw e_1.error; }
4485
- }
4486
- return parameterNames;
4487
- }
4488
-
4489
- /**
4490
- * Parses the given script and returns the list of all used variables that are not defined in the script
4491
- *
4492
- * @param script from which to extract the variables
4493
- * @returns the list of variable names
4494
- * @throws {ParsingError} if the script is invalid
4495
- */
4496
- function extractVariables(script) {
4497
- var variables = new Set();
4498
- script = "(()=>{".concat(script, "})()");
4499
- try {
4500
- for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
4501
- try {
4502
- eval(script);
4503
- }
4504
- catch (error) {
4505
- if (!(error instanceof ReferenceError)) {
4506
- throw error;
4507
- }
4508
- var undefinedName = error.message.split(' ')[0];
4509
- /*
4510
- Note: Parsing the error
4511
- [ReferenceError: thing is not defined]
4512
- */
4513
- if (!undefinedName) {
4514
- throw error;
4515
- }
4516
- if (script.includes(undefinedName + '(')) {
4517
- script = "const ".concat(undefinedName, " = ()=>'';") + script;
4518
- }
4519
- else {
4520
- variables.add(undefinedName);
4521
- script = "const ".concat(undefinedName, " = '';") + script;
4522
- }
4523
- }
4524
- }
4525
- catch (error) {
4526
- if (!(error instanceof Error)) {
4527
- throw error;
4528
- }
4529
- 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 "); }));
4530
- }
4531
- return variables;
4532
- }
4533
- /**
4534
- * TODO: [🔣] Support for multiple languages - python, java,...
4535
- */
4536
-
4537
- /**
4538
- * Parses the prompt template and returns the set of all used parameters
4539
- *
4540
- * @param promptTemplate the template with used parameters
4541
- * @returns the set of parameter names
4542
- * @throws {ParsingError} if the script is invalid
4543
- */
4544
- function extractParametersFromPromptTemplate(promptTemplate) {
4545
- var e_1, _a, e_2, _b;
4546
- var parameterNames = new Set();
4547
- try {
4548
- 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()) {
4549
- var parameterName = _d.value;
4550
- parameterNames.add(parameterName);
4551
- }
4552
- }
4553
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
4554
- finally {
4555
- try {
4556
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
4557
- }
4558
- finally { if (e_1) throw e_1.error; }
4559
- }
4560
- if (promptTemplate.blockType === 'SCRIPT') {
4561
- try {
4562
- for (var _e = __values(extractVariables(promptTemplate.content)), _f = _e.next(); !_f.done; _f = _e.next()) {
4563
- var parameterName = _f.value;
4564
- parameterNames.add(parameterName);
4565
- }
4566
- }
4567
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
4568
- finally {
4569
- try {
4570
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
4571
- }
4572
- finally { if (e_2) throw e_2.error; }
4573
- }
4574
- }
4575
- return parameterNames;
4576
- }
4577
- /**
4578
- * TODO: [🔣] If script require contentLanguage
4579
- */
4580
-
4581
4828
  /**
4582
4829
  * Compile pipeline from string (markdown) format to JSON format synchronously
4583
4830
  *
@@ -4687,7 +4934,7 @@ function pipelineStringToJsonSync(pipelineString) {
4687
4934
  pipelineJson.pipelineUrl = command.pipelineUrl.href;
4688
4935
  break;
4689
4936
  case 'KNOWLEDGE':
4690
- knowledgeCommandParser.applyToPipelineJson(pipelineJson, command);
4937
+ knowledgeCommandParser.applyToPipelineJson(command, { pipelineJson: pipelineJson, templateJson: null });
4691
4938
  break;
4692
4939
  case 'ACTION':
4693
4940
  console.error(new NotYetImplementedError('Actions are not implemented yet'));
@@ -4696,7 +4943,7 @@ function pipelineStringToJsonSync(pipelineString) {
4696
4943
  console.error(new NotYetImplementedError('Instruments are not implemented yet'));
4697
4944
  break;
4698
4945
  case 'PERSONA':
4699
- personaCommandParser.applyToPipelineJson(pipelineJson, command);
4946
+ personaCommandParser.applyToPipelineJson(command, { pipelineJson: pipelineJson, templateJson: null });
4700
4947
  // <- Note: Prototype of [🍧] (remove this comment after full implementation)
4701
4948
  break;
4702
4949
  case 'BOILERPLATE':
@@ -4720,13 +4967,6 @@ function pipelineStringToJsonSync(pipelineString) {
4720
4967
  // TODO: Parse prompt template description (the content out of the codeblock and lists)
4721
4968
  var templateModelRequirements = __assign({}, defaultModelRequirements);
4722
4969
  var listItems_3 = extractAllListItemsFromMarkdown(section.content);
4723
- var dependentParameterNames = new Set();
4724
- var blockType = 'PROMPT_TEMPLATE';
4725
- var jokers = [];
4726
- var postprocessing = [];
4727
- var expectAmount = {};
4728
- var expectFormat = undefined;
4729
- var isBlockTypeSet = false;
4730
4970
  var lastLine = section.content.split('\n').pop();
4731
4971
  var resultingParameterNameMatch = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
4732
4972
  var resultingParameterName = null;
@@ -4747,10 +4987,39 @@ function pipelineStringToJsonSync(pipelineString) {
4747
4987
  .join('\n')), "\n "); }));
4748
4988
  };
4749
4989
  var _e = extractOneBlockFromMarkdown(section.content), language = _e.language, content = _e.content;
4990
+ // TODO: [🎾][1] DRY description
4991
+ var description_1 = section.content;
4992
+ // Note: Remove codeblocks - TODO: [🎾]
4993
+ description_1 = description_1.split(/^```.*^```/gms).join('');
4994
+ description_1 = description_1.split(/^>.*$/gm).join('');
4995
+ //Note: Remove lists and return statement - TODO: [🎾]
4996
+ description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
4997
+ description_1 = spaceTrim$1(description_1);
4998
+ if (description_1 === '') {
4999
+ description_1 = undefined;
5000
+ }
5001
+ var templateJson = {
5002
+ blockType: 'PROMPT_TEMPLATE',
5003
+ name: titleToName(section.title),
5004
+ title: section.title,
5005
+ description: description_1,
5006
+ modelRequirements: templateModelRequirements,
5007
+ content: content,
5008
+ };
5009
+ /**
5010
+ * This is nessesary because block type can be
5011
+ * - Set zero times, so anticipate 'PROMPT_TEMPLATE'
5012
+ * - Set one time
5013
+ * - Set more times - throw error
5014
+ *
5015
+ * Note: [2]
5016
+ */
5017
+ var isBlockTypeSet = false;
4750
5018
  try {
4751
5019
  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()) {
4752
5020
  var listItem = listItems_2_1.value;
4753
5021
  var command = parseCommand(listItem, 'PIPELINE_TEMPLATE');
5022
+ // TODO [🍧][♓️] List commands and before apply order them
4754
5023
  switch (command.type) {
4755
5024
  // TODO: [🍧] Use here applyToPipelineJson and remove switch statement
4756
5025
  case 'BLOCK':
@@ -4768,9 +5037,12 @@ function pipelineStringToJsonSync(pipelineString) {
4768
5037
  return "continue-templates";
4769
5038
  }
4770
5039
  if (command.blockType === 'KNOWLEDGE') {
4771
- knowledgeCommandParser.applyToPipelineJson(pipelineJson, {
5040
+ knowledgeCommandParser.applyToPipelineJson({
4772
5041
  type: 'KNOWLEDGE',
4773
- source: content, // <- TODO: [🐝] !!!! Work with KNOWLEDGE which not referring to the source file/wweb, but its content itself
5042
+ source: content, // <- TODO: [🐝] !!! Work with KNOWLEDGE which not referring to the source file or website, but its content itself
5043
+ }, {
5044
+ pipelineJson: pipelineJson,
5045
+ templateJson: templateJson,
4774
5046
  });
4775
5047
  return "continue-templates";
4776
5048
  }
@@ -4783,35 +5055,36 @@ function pipelineStringToJsonSync(pipelineString) {
4783
5055
  return "continue-templates";
4784
5056
  }
4785
5057
  expectResultingParameterName();
4786
- blockType = command.blockType;
4787
- isBlockTypeSet = true;
5058
+ templateJson.blockType = command.blockType;
5059
+ isBlockTypeSet = true; //<- Note: [2]
4788
5060
  break;
4789
5061
  case 'EXPECT_AMOUNT':
4790
5062
  // eslint-disable-next-line no-case-declarations
4791
5063
  var unit = command.unit.toLowerCase();
4792
- expectAmount[unit] = expectAmount[unit] || {};
5064
+ templateJson.expectations = templateJson.expectations || {};
5065
+ templateJson.expectations[unit] = templateJson.expectations[unit] || {};
4793
5066
  if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
4794
- if (expectAmount[unit].min !== undefined) {
4795
- throw new ParsingError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
5067
+ if (templateJson.expectations[unit].min !== undefined) {
5068
+ throw new ParsingError("Already defined minumum ".concat(templateJson.expectations[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
4796
5069
  }
4797
- expectAmount[unit].min = command.amount;
5070
+ templateJson.expectations[unit].min = command.amount;
4798
5071
  } /* not else */
4799
5072
  if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
4800
- if (expectAmount[unit].max !== undefined) {
4801
- throw new ParsingError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
5073
+ if (templateJson.expectations[unit].max !== undefined) {
5074
+ throw new ParsingError("Already defined maximum ".concat(templateJson.expectations[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
4802
5075
  }
4803
- expectAmount[unit].max = command.amount;
5076
+ templateJson.expectations[unit].max = command.amount;
4804
5077
  }
4805
5078
  break;
4806
5079
  case 'EXPECT_FORMAT':
4807
- if (expectFormat !== undefined && command.format !== expectFormat) {
4808
- throw new ParsingError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
5080
+ if (templateJson.expectFormat !== undefined && command.format !== templateJson.expectFormat) {
5081
+ 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 ")));
4809
5082
  }
4810
- expectFormat = command.format;
5083
+ templateJson.expectFormat = command.format;
4811
5084
  break;
4812
5085
  case 'JOKER':
4813
- jokers.push(command.parameterName);
4814
- dependentParameterNames.add(command.parameterName);
5086
+ templateJson.jokerParameterNames = templateJson.jokerParameterNames || [];
5087
+ templateJson.jokerParameterNames.push(command.parameterName);
4815
5088
  break;
4816
5089
  case 'MODEL':
4817
5090
  templateModelRequirements[command.key] = command.value;
@@ -4821,11 +5094,12 @@ function pipelineStringToJsonSync(pipelineString) {
4821
5094
  defineParam(command);
4822
5095
  break;
4823
5096
  case 'POSTPROCESS':
4824
- postprocessing.push(command.functionName);
5097
+ templateJson.postprocessingFunctionNames = templateJson.postprocessingFunctionNames || [];
5098
+ templateJson.postprocessingFunctionNames.push(command.functionName);
4825
5099
  break;
4826
5100
  case 'KNOWLEDGE':
4827
5101
  // TODO: [👙] The knowledge is maybe relevant for just this template
4828
- knowledgeCommandParser.applyToPipelineJson(pipelineJson, command);
5102
+ knowledgeCommandParser.applyToPipelineJson(command, { pipelineJson: pipelineJson, templateJson: templateJson });
4829
5103
  break;
4830
5104
  case 'ACTION':
4831
5105
  // TODO: [👙] The action is maybe relevant for just this template
@@ -4836,7 +5110,7 @@ function pipelineStringToJsonSync(pipelineString) {
4836
5110
  console.error(new NotYetImplementedError('Instruments are not implemented yet'));
4837
5111
  break;
4838
5112
  case 'PERSONA':
4839
- personaCommandParser.applyToPipelineJson(pipelineJson, command);
5113
+ personaCommandParser.applyToPipelineJson(command, { pipelineJson: pipelineJson, templateJson: templateJson });
4840
5114
  // <- Note: Prototype of [🍧] (remove this comment after full implementation)
4841
5115
  break;
4842
5116
  case 'BOILERPLATE':
@@ -4855,60 +5129,29 @@ function pipelineStringToJsonSync(pipelineString) {
4855
5129
  }
4856
5130
  finally { if (e_3) throw e_3.error; }
4857
5131
  }
4858
- if (blockType === 'SCRIPT') {
5132
+ // TODO: [🍧] Should be done in BLOCK command
5133
+ if (templateJson.blockType === 'SCRIPT') {
4859
5134
  if (!language) {
4860
5135
  throw new ParsingError('You must specify the language of the script in the prompt template');
4861
5136
  }
4862
- else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
5137
+ if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
4863
5138
  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 "); }));
4864
5139
  }
5140
+ templateJson.contentLanguage = language;
4865
5141
  }
4866
- // TODO: [🎾][1] DRY description
4867
- var description_1 = section.content;
4868
- // Note: Remove codeblocks - TODO: [🎾]
4869
- description_1 = description_1.split(/^```.*^```/gms).join('');
4870
- description_1 = description_1.split(/^>.*$/gm).join('');
4871
- //Note: Remove lists and return statement - TODO: [🎾]
4872
- description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
4873
- description_1 = spaceTrim$1(description_1);
4874
- if (description_1 === '') {
4875
- description_1 = undefined;
4876
- }
4877
- if (Object.keys(jokers).length === 0) {
4878
- jokers = undefined;
4879
- }
4880
- if (Object.keys(expectAmount).length === 0) {
4881
- expectAmount = undefined;
4882
- }
4883
- if (Object.keys(postprocessing).length === 0) {
4884
- postprocessing = undefined;
4885
- }
4886
- dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, blockType: blockType, content: content })));
5142
+ // TODO: [🍧][] Should be done in BLOCK command
4887
5143
  if (templateModelRequirements.modelVariant === undefined) {
4888
5144
  templateModelRequirements.modelVariant = 'CHAT';
4889
5145
  }
4890
- dependentParameterNames = difference(dependentParameterNames, new Set(RESERVED_PARAMETER_NAMES));
4891
- var template = {
4892
- name: titleToName(section.title),
4893
- title: section.title,
4894
- description: description_1,
4895
- dependentParameterNames: Array.from(dependentParameterNames),
4896
- blockType: blockType,
4897
- jokers: jokers,
4898
- postprocessing: postprocessing,
4899
- expectations: expectAmount,
4900
- expectFormat: expectFormat,
4901
- personaName: null,
4902
- modelRequirements: templateModelRequirements,
4903
- contentLanguage: blockType === 'SCRIPT' ? language : undefined,
4904
- content: content,
4905
- resultingParameterName: expectResultingParameterName( /* <- Note: This is once more redundant */),
4906
- };
4907
- if (blockType !== 'PROMPT_TEMPLATE') {
4908
- delete template.modelRequirements;
5146
+ templateJson.dependentParameterNames = Array.from(extractParametersFromPromptTemplate(templateJson));
5147
+ // TODO: [🍧][❔] Remove this condition - modelRequirements should be put here via BLOCK command not removed when PROMPT_TEMPLATE
5148
+ if (templateJson.blockType !== 'PROMPT_TEMPLATE') {
5149
+ delete templateJson.modelRequirements;
4909
5150
  }
5151
+ // TODO: [🍧] Make this better - for example each command parser can call and apply this
5152
+ templateJson.resultingParameterName = expectResultingParameterName( /* <- Note: This is once more redundant */);
4910
5153
  // TODO: [🍧] What actually about preparation and pushing the block into `promptTemplates`
4911
- pipelineJson.promptTemplates.push(template);
5154
+ pipelineJson.promptTemplates.push(templateJson);
4912
5155
  };
4913
5156
  try {
4914
5157
  // =============================================================