@promptbook/node 0.67.2 β†’ 0.67.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/index.es.js CHANGED
@@ -13,7 +13,7 @@ import * as dotenv from 'dotenv';
13
13
  /**
14
14
  * The version of the Promptbook library
15
15
  */
16
- var PROMPTBOOK_VERSION = '0.67.1';
16
+ var PROMPTBOOK_VERSION = '0.67.2';
17
17
  // TODO: !!!! List here all the versions and annotate + put into script
18
18
 
19
19
  /*! *****************************************************************************
@@ -867,7 +867,7 @@ function forEachAsync(array, options, callbackfunction) {
867
867
  });
868
868
  }
869
869
 
870
- var PipelineCollection = [{title:"Prepare Knowledge from Markdown",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.67.1",parameters:[{name:"knowledgeContent",description:"Markdown document content",isInput:true,isOutput:false},{name:"knowledgePieces",description:"The knowledge JSON object",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT"},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> {knowledgeContent}",dependentParameterNames:["knowledgeContent"],resultingParameterName:"knowledgePieces"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-from-markdown.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-keywords.ptbk.md",promptbookVersion:"0.67.1",parameters:[{name:"knowledgePieceContent",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"},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> {knowledgePieceContent}",dependentParameterNames:["knowledgePieceContent"],resultingParameterName:"keywords"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-keywords.ptbk.md"},{title:"Prepare Title",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-title.ptbk.md",promptbookVersion:"0.67.1",parameters:[{name:"knowledgePieceContent",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"},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> {knowledgePieceContent}",expectations:{words:{min:1,max:8}},dependentParameterNames:["knowledgePieceContent"],resultingParameterName:"title"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-title.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-persona.ptbk.md",promptbookVersion:"0.67.1",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"},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- Your output format is JSON object\n- It contains the following keys:\n - `modelName`: The name of the model to use\n - `systemMessage`: The system message to provide context to the model\n - `temperature`: The sampling temperature to use\n\n### Key `modelName`\n\nPick from the following models:\n\n- {availableModelNames}\n\n### Key `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### Key `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:[],sourceFile:"./promptbook-collection/prepare-persona.ptbk.md"}];
870
+ var PipelineCollection = [{title:"Prepare Knowledge from Markdown",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.67.2",parameters:[{name:"knowledgeContent",description:"Markdown document content",isInput:true,isOutput:false},{name:"knowledgePieces",description:"The knowledge JSON object",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT"},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> {knowledgeContent}",dependentParameterNames:["knowledgeContent"],resultingParameterName:"knowledgePieces"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-from-markdown.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-keywords.ptbk.md",promptbookVersion:"0.67.2",parameters:[{name:"knowledgePieceContent",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"},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> {knowledgePieceContent}",dependentParameterNames:["knowledgePieceContent"],resultingParameterName:"keywords"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-keywords.ptbk.md"},{title:"Prepare Title",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-title.ptbk.md",promptbookVersion:"0.67.2",parameters:[{name:"knowledgePieceContent",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"},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> {knowledgePieceContent}",expectations:{words:{min:1,max:8}},dependentParameterNames:["knowledgePieceContent"],resultingParameterName:"title"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-title.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-persona.ptbk.md",promptbookVersion:"0.67.2",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"},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- Your output format is JSON object\n- Write just the JSON object, no other text should be present\n- It contains the following keys:\n - `modelName`: The name of the model to use\n - `systemMessage`: The system message to provide context to the model\n - `temperature`: The sampling temperature to use\n\n### Key `modelName`\n\nPick from the following models:\n\n- {availableModelNames}\n\n### Key `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### Key `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:[],sourceFile:"./promptbook-collection/prepare-persona.ptbk.md"}];
871
871
 
872
872
  /**
873
873
  * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
@@ -2811,13 +2811,24 @@ function createPipelineExecutor(options) {
2811
2811
  var pipeline = options.pipeline, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
2812
2812
  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 ? IS_VERBOSE : _d, _e = settings.isNotPreparedWarningSupressed, isNotPreparedWarningSupressed = _e === void 0 ? false : _e;
2813
2813
  validatePipeline(pipeline);
2814
+ var pipelineIdentification = (function () {
2815
+ // Note: This is a 😐 implementation of [🚞]
2816
+ var _ = [];
2817
+ if (pipeline.sourceFile !== undefined) {
2818
+ _.push("File: ".concat(pipeline.sourceFile));
2819
+ }
2820
+ if (pipeline.pipelineUrl !== undefined) {
2821
+ _.push("Url: ".concat(pipeline.pipelineUrl));
2822
+ }
2823
+ return _.join('\n');
2824
+ })();
2814
2825
  var llmTools = joinLlmExecutionTools.apply(void 0, __spreadArray([], __read(arrayableToArray(tools.llm)), false));
2815
2826
  var preparedPipeline;
2816
2827
  if (isPipelinePrepared(pipeline)) {
2817
2828
  preparedPipeline = pipeline;
2818
2829
  }
2819
2830
  else if (isNotPreparedWarningSupressed !== true) {
2820
- console.warn(spaceTrim("\n Pipeline ".concat(pipeline.pipelineUrl || pipeline.sourceFile || pipeline.title, " is not prepared\n\n ").concat(pipeline.sourceFile, "\n\n It will be prepared ad-hoc before the first execution and **returned as `preparedPipeline` in `PipelineExecutorResult`**\n But it is recommended to prepare the pipeline during collection preparation\n\n @see more at https://ptbk.io/prepare-pipeline\n ")));
2831
+ console.warn(spaceTrim(function (block) { return "\n Pipeline is not prepared\n\n ".concat(block(pipelineIdentification), "\n\n It will be prepared ad-hoc before the first execution and **returned as `preparedPipeline` in `PipelineExecutorResult`**\n But it is recommended to prepare the pipeline during collection preparation\n\n @see more at https://ptbk.io/prepare-pipeline\n "); }));
2821
2832
  }
2822
2833
  var pipelineExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
2823
2834
  // TODO: !!! Extract to separate functions and files - ALL FUNCTIONS BELOW
@@ -2855,7 +2866,7 @@ function createPipelineExecutor(options) {
2855
2866
  }
2856
2867
  function getReservedParametersForTemplate(template) {
2857
2868
  return __awaiter(this, void 0, void 0, function () {
2858
- var context, knowledge, samples, currentDate, modelName, reservedParameters, RESERVED_PARAMETER_NAMES_1, RESERVED_PARAMETER_NAMES_1_1, parameterName;
2869
+ var context, knowledge, samples, currentDate, modelName, reservedParameters, _loop_3, RESERVED_PARAMETER_NAMES_1, RESERVED_PARAMETER_NAMES_1_1, parameterName;
2859
2870
  var e_3, _a;
2860
2871
  return __generator(this, function (_b) {
2861
2872
  switch (_b.label) {
@@ -2878,13 +2889,16 @@ function createPipelineExecutor(options) {
2878
2889
  currentDate: currentDate,
2879
2890
  modelName: modelName,
2880
2891
  };
2892
+ _loop_3 = function (parameterName) {
2893
+ if (reservedParameters[parameterName] === undefined) {
2894
+ throw new UnexpectedError(spaceTrim(function (block) { return "\n Reserved parameter {".concat(parameterName, "} is not defined\n\n ").concat(block(pipelineIdentification), "\n "); }));
2895
+ }
2896
+ };
2881
2897
  try {
2882
2898
  // Note: Doublecheck that ALL reserved parameters are defined:
2883
2899
  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()) {
2884
2900
  parameterName = RESERVED_PARAMETER_NAMES_1_1.value;
2885
- if (reservedParameters[parameterName] === undefined) {
2886
- throw new UnexpectedError("Reserved parameter {".concat(parameterName, "} is not defined"));
2887
- }
2901
+ _loop_3(parameterName);
2888
2902
  }
2889
2903
  }
2890
2904
  catch (e_3_1) { e_3 = { error: e_3_1 }; }
@@ -2901,10 +2915,10 @@ function createPipelineExecutor(options) {
2901
2915
  }
2902
2916
  function executeSingleTemplate(currentTemplate) {
2903
2917
  return __awaiter(this, void 0, void 0, function () {
2904
- 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, preparedContent, 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;
2905
- var e_7, _p, e_4, _q, e_6, _r, e_5, _s, _t;
2906
- return __generator(this, function (_u) {
2907
- switch (_u.label) {
2918
+ var name, title, priority, usedParameterNames, dependentParameterNames, definedParameters, _a, _b, _c, definedParameterNames, parameters, _loop_4, _d, _e, parameterName, prompt, chatResult, completionResult, embeddingResult, result, resultString, expectError, scriptPipelineExecutionErrors, maxAttempts, jokerParameterNames, preparedContent, _loop_5, attempt, state_2;
2919
+ var e_4, _f, _g;
2920
+ return __generator(this, function (_h) {
2921
+ switch (_h.label) {
2908
2922
  case 0:
2909
2923
  name = "pipeline-executor-frame-".concat(currentTemplate.name);
2910
2924
  title = currentTemplate.title;
@@ -2921,47 +2935,48 @@ function createPipelineExecutor(options) {
2921
2935
  // <- [3]
2922
2936
  })];
2923
2937
  case 1:
2924
- _u.sent();
2925
- _u.label = 2;
2938
+ _h.sent();
2939
+ _h.label = 2;
2926
2940
  case 2:
2927
2941
  usedParameterNames = extractParameterNamesFromPromptTemplate(currentTemplate);
2928
2942
  dependentParameterNames = new Set(currentTemplate.dependentParameterNames);
2929
2943
  if (union(difference(usedParameterNames, dependentParameterNames), difference(dependentParameterNames, usedParameterNames)).size !== 0) {
2930
- throw new UnexpectedError(spaceTrim("\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 ")));
2944
+ throw new UnexpectedError(spaceTrim(function (block) { return "\n Dependent parameters are not consistent used parameters:\n\n ".concat(block(pipelineIdentification), "\n\n Dependent parameters:\n ").concat(Array.from(dependentParameterNames).join(', '), "\n\n Used parameters:\n ").concat(Array.from(usedParameterNames).join(', '), "\n\n "); }));
2931
2945
  }
2932
2946
  _b = (_a = Object).freeze;
2933
2947
  _c = [{}];
2934
2948
  return [4 /*yield*/, getReservedParametersForTemplate(currentTemplate)];
2935
2949
  case 3:
2936
- definedParameters = _b.apply(_a, [__assign.apply(void 0, [__assign.apply(void 0, _c.concat([(_u.sent())])), parametersToPass])]);
2950
+ definedParameters = _b.apply(_a, [__assign.apply(void 0, [__assign.apply(void 0, _c.concat([(_h.sent())])), parametersToPass])]);
2937
2951
  definedParameterNames = new Set(Object.keys(definedParameters));
2938
2952
  parameters = {};
2953
+ _loop_4 = function (parameterName) {
2954
+ // Situation: Parameter is defined and used
2955
+ if (definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
2956
+ parameters[parameterName] = definedParameters[parameterName];
2957
+ }
2958
+ // Situation: Parameter is defined but NOT used
2959
+ else if (definedParameterNames.has(parameterName) && !usedParameterNames.has(parameterName)) ;
2960
+ // Situation: Parameter is NOT defined BUT used
2961
+ else if (!definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
2962
+ // Houston, we have a problem
2963
+ // Note: Checking part is also done in `validatePipeline`, but it’s good to doublecheck
2964
+ throw new UnexpectedError(spaceTrim(function (block) { return "\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 ").concat(block(pipelineIdentification), "\n\n "); }));
2965
+ }
2966
+ };
2939
2967
  try {
2940
2968
  // Note: [2] Check that all used parameters are defined and removing unused parameters for this template
2941
2969
  for (_d = __values(Array.from(union(definedParameterNames, usedParameterNames, dependentParameterNames))), _e = _d.next(); !_e.done; _e = _d.next()) {
2942
2970
  parameterName = _e.value;
2943
- // Situation: Parameter is defined and used
2944
- if (definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
2945
- parameters[parameterName] = definedParameters[parameterName];
2946
- }
2947
- // Situation: Parameter is defined but NOT used
2948
- else if (definedParameterNames.has(parameterName) && !usedParameterNames.has(parameterName)) {
2949
- // Do not pass this parameter to prompt
2950
- }
2951
- // Situation: Parameter is NOT defined BUT used
2952
- else if (!definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
2953
- // Houston, we have a problem
2954
- // Note: Checking part is also done in `validatePipeline`, but it’s good to doublecheck
2955
- throw new UnexpectedError(spaceTrim("\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 ")));
2956
- }
2971
+ _loop_4(parameterName);
2957
2972
  }
2958
2973
  }
2959
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
2974
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
2960
2975
  finally {
2961
2976
  try {
2962
- if (_e && !_e.done && (_p = _d.return)) _p.call(_d);
2977
+ if (_e && !_e.done && (_f = _d.return)) _f.call(_d);
2963
2978
  }
2964
- finally { if (e_7) throw e_7.error; }
2979
+ finally { if (e_4) throw e_4.error; }
2965
2980
  }
2966
2981
  // Note: Now we can freeze `parameters` because we are sure that all and only used parameters are defined
2967
2982
  Object.freeze(parameters);
@@ -2973,294 +2988,310 @@ function createPipelineExecutor(options) {
2973
2988
  preparedContent = (currentTemplate.preparedContent || '{content}')
2974
2989
  .split('{content}')
2975
2990
  .join(currentTemplate.content);
2991
+ _loop_5 = function (attempt) {
2992
+ var isJokerAttempt, jokerParameterName, _j, _k, _l, _m, scriptTools, error_2, e_5_1, _o, _p, functionName, postprocessingError, _q, _r, scriptTools, error_3, e_6_1, e_7_1, error_4;
2993
+ var e_5, _s, e_7, _t, e_6, _u;
2994
+ return __generator(this, function (_v) {
2995
+ switch (_v.label) {
2996
+ case 0:
2997
+ isJokerAttempt = attempt < 0;
2998
+ jokerParameterName = jokerParameterNames[jokerParameterNames.length + attempt];
2999
+ if (isJokerAttempt && !jokerParameterName) {
3000
+ throw new UnexpectedError(spaceTrim(function (block) { return "\n Joker not found in attempt ".concat(attempt, "\n\n ").concat(block(pipelineIdentification), "\n "); }));
3001
+ }
3002
+ result = null;
3003
+ resultString = null;
3004
+ expectError = null;
3005
+ if (isJokerAttempt) {
3006
+ if (parameters[jokerParameterName] === undefined) {
3007
+ throw new PipelineExecutionError(spaceTrim(function (block) { return "\n Joker parameter {".concat(jokerParameterName, "} not defined\n\n ").concat(block(pipelineIdentification), "\n "); }));
3008
+ // <- TODO: This is maybe `PipelineLogicError` which should be detected in `validatePipeline` and here just thrown as `UnexpectedError`
3009
+ }
3010
+ else {
3011
+ resultString = parameters[jokerParameterName];
3012
+ }
3013
+ }
3014
+ _v.label = 1;
3015
+ case 1:
3016
+ _v.trys.push([1, 44, 45, 46]);
3017
+ if (!!isJokerAttempt) return [3 /*break*/, 26];
3018
+ _j = currentTemplate.blockType;
3019
+ switch (_j) {
3020
+ case 'SIMPLE_TEMPLATE': return [3 /*break*/, 2];
3021
+ case 'PROMPT_TEMPLATE': return [3 /*break*/, 3];
3022
+ case 'SCRIPT': return [3 /*break*/, 12];
3023
+ case 'PROMPT_DIALOG': return [3 /*break*/, 23];
3024
+ }
3025
+ return [3 /*break*/, 25];
3026
+ case 2:
3027
+ resultString = replaceParameters(preparedContent, parameters);
3028
+ return [3 /*break*/, 26];
3029
+ case 3:
3030
+ prompt = {
3031
+ title: currentTemplate.title,
3032
+ pipelineUrl: "".concat(preparedPipeline.pipelineUrl
3033
+ ? preparedPipeline.pipelineUrl
3034
+ : 'anonymous' /* <- TODO: [🧠] How to deal with anonymous pipelines, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
3035
+ parameters: parameters,
3036
+ content: preparedContent,
3037
+ modelRequirements: currentTemplate.modelRequirements,
3038
+ expectations: __assign(__assign({}, (preparedPipeline.personas.find(function (_a) {
3039
+ var name = _a.name;
3040
+ return name === currentTemplate.personaName;
3041
+ }) || {})), currentTemplate.expectations),
3042
+ expectFormat: currentTemplate.expectFormat,
3043
+ postprocessingFunctionNames: currentTemplate.postprocessingFunctionNames,
3044
+ }; // <- TODO: Not very good type guard
3045
+ _k = currentTemplate.modelRequirements.modelVariant;
3046
+ switch (_k) {
3047
+ case 'CHAT': return [3 /*break*/, 4];
3048
+ case 'COMPLETION': return [3 /*break*/, 6];
3049
+ case 'EMBEDDING': return [3 /*break*/, 8];
3050
+ }
3051
+ return [3 /*break*/, 10];
3052
+ case 4: return [4 /*yield*/, llmTools.callChatModel($deepFreeze(prompt))];
3053
+ case 5:
3054
+ chatResult = _v.sent();
3055
+ // TODO: [🍬] Destroy chatThread
3056
+ result = chatResult;
3057
+ resultString = chatResult.content;
3058
+ return [3 /*break*/, 11];
3059
+ case 6: return [4 /*yield*/, llmTools.callCompletionModel($deepFreeze(prompt))];
3060
+ case 7:
3061
+ completionResult = _v.sent();
3062
+ result = completionResult;
3063
+ resultString = completionResult.content;
3064
+ return [3 /*break*/, 11];
3065
+ case 8: return [4 /*yield*/, llmTools.callEmbeddingModel($deepFreeze(prompt))];
3066
+ case 9:
3067
+ embeddingResult = _v.sent();
3068
+ result = embeddingResult;
3069
+ resultString = embeddingResult.content.join(',');
3070
+ return [3 /*break*/, 11];
3071
+ case 10: throw new PipelineExecutionError(spaceTrim(function (block) { return "\n Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\"\n\n ").concat(block(pipelineIdentification), "\n\n\n "); }));
3072
+ case 11: return [3 /*break*/, 26];
3073
+ case 12:
3074
+ if (arrayableToArray(tools.script).length === 0) {
3075
+ throw new PipelineExecutionError(spaceTrim(function (block) { return "\n No script execution tools are available\n\n ".concat(block(pipelineIdentification), "\n "); }));
3076
+ }
3077
+ if (!currentTemplate.contentLanguage) {
3078
+ throw new PipelineExecutionError(spaceTrim(function (block) { return "\n Script language is not defined for prompt template \"".concat(currentTemplate.name, "\"\n\n ").concat(block(pipelineIdentification), "\n "); }));
3079
+ }
3080
+ // TODO: DRY [1]
3081
+ scriptPipelineExecutionErrors = [];
3082
+ _v.label = 13;
3083
+ case 13:
3084
+ _v.trys.push([13, 20, 21, 22]);
3085
+ _l = (e_5 = void 0, __values(arrayableToArray(tools.script))), _m = _l.next();
3086
+ _v.label = 14;
3087
+ case 14:
3088
+ if (!!_m.done) return [3 /*break*/, 19];
3089
+ scriptTools = _m.value;
3090
+ _v.label = 15;
3091
+ case 15:
3092
+ _v.trys.push([15, 17, , 18]);
3093
+ return [4 /*yield*/, scriptTools.execute($deepFreeze({
3094
+ scriptLanguage: currentTemplate.contentLanguage,
3095
+ script: preparedContent,
3096
+ parameters: parameters,
3097
+ }))];
3098
+ case 16:
3099
+ resultString = _v.sent();
3100
+ return [3 /*break*/, 19];
3101
+ case 17:
3102
+ error_2 = _v.sent();
3103
+ if (!(error_2 instanceof Error)) {
3104
+ throw error_2;
3105
+ }
3106
+ if (error_2 instanceof UnexpectedError) {
3107
+ throw error_2;
3108
+ }
3109
+ scriptPipelineExecutionErrors.push(error_2);
3110
+ return [3 /*break*/, 18];
3111
+ case 18:
3112
+ _m = _l.next();
3113
+ return [3 /*break*/, 14];
3114
+ case 19: return [3 /*break*/, 22];
3115
+ case 20:
3116
+ e_5_1 = _v.sent();
3117
+ e_5 = { error: e_5_1 };
3118
+ return [3 /*break*/, 22];
3119
+ case 21:
3120
+ try {
3121
+ if (_m && !_m.done && (_s = _l.return)) _s.call(_l);
3122
+ }
3123
+ finally { if (e_5) throw e_5.error; }
3124
+ return [7 /*endfinally*/];
3125
+ case 22:
3126
+ if (resultString !== null) {
3127
+ return [3 /*break*/, 26];
3128
+ }
3129
+ if (scriptPipelineExecutionErrors.length === 1) {
3130
+ throw scriptPipelineExecutionErrors[0];
3131
+ }
3132
+ else {
3133
+ throw new PipelineExecutionError(spaceTrim(function (block) { return "\n Script execution failed ".concat(scriptPipelineExecutionErrors.length, " times\n\n ").concat(block(pipelineIdentification), "\n\n ").concat(block(scriptPipelineExecutionErrors
3134
+ .map(function (error) { return '- ' + error.message; })
3135
+ .join('\n\n')), "\n "); }));
3136
+ }
3137
+ case 23:
3138
+ if (tools.userInterface === undefined) {
3139
+ throw new PipelineExecutionError(spaceTrim(function (block) { return "\n User interface tools are not available\n\n ".concat(block(pipelineIdentification), "\n "); }));
3140
+ }
3141
+ return [4 /*yield*/, tools.userInterface.promptDialog($deepFreeze({
3142
+ promptTitle: currentTemplate.title,
3143
+ promptMessage: replaceParameters(currentTemplate.description || '', parameters),
3144
+ defaultValue: replaceParameters(preparedContent, parameters),
3145
+ // TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
3146
+ placeholder: undefined,
3147
+ priority: priority,
3148
+ }))];
3149
+ case 24:
3150
+ // TODO: [🌹] When making next attempt for `PROMPT DIALOG`, preserve the previous user input
3151
+ resultString = _v.sent();
3152
+ return [3 /*break*/, 26];
3153
+ case 25: throw new PipelineExecutionError(spaceTrim(function (block) { return "\n Unknown execution type \"".concat(currentTemplate.blockType, "\"\n\n ").concat(block(pipelineIdentification), "\n "); }));
3154
+ case 26:
3155
+ if (!(!isJokerAttempt && currentTemplate.postprocessingFunctionNames)) return [3 /*break*/, 43];
3156
+ _v.label = 27;
3157
+ case 27:
3158
+ _v.trys.push([27, 41, 42, 43]);
3159
+ _o = (e_7 = void 0, __values(currentTemplate.postprocessingFunctionNames)), _p = _o.next();
3160
+ _v.label = 28;
3161
+ case 28:
3162
+ if (!!_p.done) return [3 /*break*/, 40];
3163
+ functionName = _p.value;
3164
+ // TODO: DRY [1]
3165
+ scriptPipelineExecutionErrors = [];
3166
+ postprocessingError = null;
3167
+ _v.label = 29;
3168
+ case 29:
3169
+ _v.trys.push([29, 36, 37, 38]);
3170
+ _q = (e_6 = void 0, __values(arrayableToArray(tools.script))), _r = _q.next();
3171
+ _v.label = 30;
3172
+ case 30:
3173
+ if (!!_r.done) return [3 /*break*/, 35];
3174
+ scriptTools = _r.value;
3175
+ _v.label = 31;
3176
+ case 31:
3177
+ _v.trys.push([31, 33, , 34]);
3178
+ return [4 /*yield*/, scriptTools.execute({
3179
+ scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
3180
+ script: "".concat(functionName, "(resultString)"),
3181
+ parameters: {
3182
+ resultString: resultString || '',
3183
+ // Note: No ...parametersForTemplate, because working with result only
3184
+ },
3185
+ })];
3186
+ case 32:
3187
+ resultString = _v.sent();
3188
+ postprocessingError = null;
3189
+ return [3 /*break*/, 35];
3190
+ case 33:
3191
+ error_3 = _v.sent();
3192
+ if (!(error_3 instanceof Error)) {
3193
+ throw error_3;
3194
+ }
3195
+ if (error_3 instanceof UnexpectedError) {
3196
+ throw error_3;
3197
+ }
3198
+ postprocessingError = error_3;
3199
+ scriptPipelineExecutionErrors.push(error_3);
3200
+ return [3 /*break*/, 34];
3201
+ case 34:
3202
+ _r = _q.next();
3203
+ return [3 /*break*/, 30];
3204
+ case 35: return [3 /*break*/, 38];
3205
+ case 36:
3206
+ e_6_1 = _v.sent();
3207
+ e_6 = { error: e_6_1 };
3208
+ return [3 /*break*/, 38];
3209
+ case 37:
3210
+ try {
3211
+ if (_r && !_r.done && (_u = _q.return)) _u.call(_q);
3212
+ }
3213
+ finally { if (e_6) throw e_6.error; }
3214
+ return [7 /*endfinally*/];
3215
+ case 38:
3216
+ if (postprocessingError) {
3217
+ throw postprocessingError;
3218
+ }
3219
+ _v.label = 39;
3220
+ case 39:
3221
+ _p = _o.next();
3222
+ return [3 /*break*/, 28];
3223
+ case 40: return [3 /*break*/, 43];
3224
+ case 41:
3225
+ e_7_1 = _v.sent();
3226
+ e_7 = { error: e_7_1 };
3227
+ return [3 /*break*/, 43];
3228
+ case 42:
3229
+ try {
3230
+ if (_p && !_p.done && (_t = _o.return)) _t.call(_o);
3231
+ }
3232
+ finally { if (e_7) throw e_7.error; }
3233
+ return [7 /*endfinally*/];
3234
+ case 43:
3235
+ // TODO: [πŸ’] Unite object for expecting amount and format
3236
+ if (currentTemplate.expectFormat) {
3237
+ if (currentTemplate.expectFormat === 'JSON') {
3238
+ if (!isValidJsonString(resultString || '')) {
3239
+ throw new ExpectError(spaceTrim(function (block) { return "\n Expected valid JSON string\n\n ".concat(block(pipelineIdentification), "\n "); }));
3240
+ }
3241
+ }
3242
+ }
3243
+ // TODO: [πŸ’] Unite object for expecting amount and format
3244
+ if (currentTemplate.expectations) {
3245
+ checkExpectations(currentTemplate.expectations, resultString || '');
3246
+ }
3247
+ return [2 /*return*/, "break-attempts"];
3248
+ case 44:
3249
+ error_4 = _v.sent();
3250
+ if (!(error_4 instanceof ExpectError)) {
3251
+ throw error_4;
3252
+ }
3253
+ expectError = error_4;
3254
+ return [3 /*break*/, 46];
3255
+ case 45:
3256
+ if (!isJokerAttempt &&
3257
+ currentTemplate.blockType === 'PROMPT_TEMPLATE' &&
3258
+ prompt
3259
+ // <- Note: [2] When some expected parameter is not defined, error will occur in replaceParameters
3260
+ // In that case we don’t want to make a report about it because it’s not a llm execution error
3261
+ ) {
3262
+ // TODO: [🧠] Maybe put other blockTypes into report
3263
+ executionReport.promptExecutions.push({
3264
+ prompt: __assign({}, prompt),
3265
+ result: result || undefined,
3266
+ error: expectError === null ? undefined : serializeError(expectError),
3267
+ });
3268
+ }
3269
+ return [7 /*endfinally*/];
3270
+ case 46:
3271
+ if (expectError !== null && attempt === maxAttempts - 1) {
3272
+ throw new PipelineExecutionError(spaceTrim(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 "); }));
3273
+ }
3274
+ return [2 /*return*/];
3275
+ }
3276
+ });
3277
+ };
2976
3278
  attempt = -jokerParameterNames.length;
2977
- _u.label = 4;
3279
+ _h.label = 4;
2978
3280
  case 4:
2979
- if (!(attempt < maxAttempts)) return [3 /*break*/, 52];
2980
- isJokerAttempt = attempt < 0;
2981
- jokerParameterName = jokerParameterNames[jokerParameterNames.length + attempt];
2982
- if (isJokerAttempt && !jokerParameterName) {
2983
- throw new UnexpectedError("Joker not found in attempt ".concat(attempt));
2984
- }
2985
- result = null;
2986
- resultString = null;
2987
- expectError = null;
2988
- if (isJokerAttempt) {
2989
- if (parameters[jokerParameterName] === undefined) {
2990
- throw new PipelineExecutionError("Joker parameter {".concat(jokerParameterName, "} not defined"));
2991
- // <- TODO: This is maybe `PipelineLogicError` which should be detected in `validatePipeline` and here just thrown as `UnexpectedError`
2992
- }
2993
- else {
2994
- resultString = parameters[jokerParameterName];
2995
- }
2996
- }
2997
- _u.label = 5;
3281
+ if (!(attempt < maxAttempts)) return [3 /*break*/, 7];
3282
+ return [5 /*yield**/, _loop_5(attempt)];
2998
3283
  case 5:
2999
- _u.trys.push([5, 48, 49, 50]);
3000
- if (!!isJokerAttempt) return [3 /*break*/, 30];
3001
- _f = currentTemplate.blockType;
3002
- switch (_f) {
3003
- case 'SIMPLE_TEMPLATE': return [3 /*break*/, 6];
3004
- case 'PROMPT_TEMPLATE': return [3 /*break*/, 7];
3005
- case 'SCRIPT': return [3 /*break*/, 16];
3006
- case 'PROMPT_DIALOG': return [3 /*break*/, 27];
3284
+ state_2 = _h.sent();
3285
+ switch (state_2) {
3286
+ case "break-attempts": return [3 /*break*/, 7];
3007
3287
  }
3008
- return [3 /*break*/, 29];
3288
+ _h.label = 6;
3009
3289
  case 6:
3010
- resultString = replaceParameters(preparedContent, parameters);
3011
- return [3 /*break*/, 30];
3012
- case 7:
3013
- prompt = {
3014
- title: currentTemplate.title,
3015
- pipelineUrl: "".concat(preparedPipeline.pipelineUrl
3016
- ? preparedPipeline.pipelineUrl
3017
- : 'anonymous' /* <- TODO: [🧠] How to deal with anonymous pipelines, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
3018
- parameters: parameters,
3019
- content: preparedContent,
3020
- modelRequirements: currentTemplate.modelRequirements,
3021
- expectations: __assign(__assign({}, (preparedPipeline.personas.find(function (_a) {
3022
- var name = _a.name;
3023
- return name === currentTemplate.personaName;
3024
- }) || {})), currentTemplate.expectations),
3025
- expectFormat: currentTemplate.expectFormat,
3026
- postprocessingFunctionNames: currentTemplate.postprocessingFunctionNames,
3027
- }; // <- TODO: Not very good type guard
3028
- _g = currentTemplate.modelRequirements.modelVariant;
3029
- switch (_g) {
3030
- case 'CHAT': return [3 /*break*/, 8];
3031
- case 'COMPLETION': return [3 /*break*/, 10];
3032
- case 'EMBEDDING': return [3 /*break*/, 12];
3033
- }
3034
- return [3 /*break*/, 14];
3035
- case 8: return [4 /*yield*/, llmTools.callChatModel($deepFreeze(prompt))];
3036
- case 9:
3037
- chatResult = _u.sent();
3038
- // TODO: [🍬] Destroy chatThread
3039
- result = chatResult;
3040
- resultString = chatResult.content;
3041
- return [3 /*break*/, 15];
3042
- case 10: return [4 /*yield*/, llmTools.callCompletionModel($deepFreeze(prompt))];
3043
- case 11:
3044
- completionResult = _u.sent();
3045
- result = completionResult;
3046
- resultString = completionResult.content;
3047
- return [3 /*break*/, 15];
3048
- case 12: return [4 /*yield*/, llmTools.callEmbeddingModel($deepFreeze(prompt))];
3049
- case 13:
3050
- embeddingResult = _u.sent();
3051
- result = embeddingResult;
3052
- resultString = embeddingResult.content.join(',');
3053
- return [3 /*break*/, 15];
3054
- case 14: throw new PipelineExecutionError("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
3055
- case 15: return [3 /*break*/, 30];
3056
- case 16:
3057
- if (arrayableToArray(tools.script).length === 0) {
3058
- throw new PipelineExecutionError('No script execution tools are available');
3059
- }
3060
- if (!currentTemplate.contentLanguage) {
3061
- throw new PipelineExecutionError("Script language is not defined for prompt template \"".concat(currentTemplate.name, "\""));
3062
- }
3063
- // TODO: DRY [1]
3064
- scriptPipelineExecutionErrors = [];
3065
- _u.label = 17;
3066
- case 17:
3067
- _u.trys.push([17, 24, 25, 26]);
3068
- _h = (e_4 = void 0, __values(arrayableToArray(tools.script))), _j = _h.next();
3069
- _u.label = 18;
3070
- case 18:
3071
- if (!!_j.done) return [3 /*break*/, 23];
3072
- scriptTools = _j.value;
3073
- _u.label = 19;
3074
- case 19:
3075
- _u.trys.push([19, 21, , 22]);
3076
- return [4 /*yield*/, scriptTools.execute($deepFreeze({
3077
- scriptLanguage: currentTemplate.contentLanguage,
3078
- script: preparedContent,
3079
- parameters: parameters,
3080
- }))];
3081
- case 20:
3082
- resultString = _u.sent();
3083
- return [3 /*break*/, 23];
3084
- case 21:
3085
- error_2 = _u.sent();
3086
- if (!(error_2 instanceof Error)) {
3087
- throw error_2;
3088
- }
3089
- if (error_2 instanceof UnexpectedError) {
3090
- throw error_2;
3091
- }
3092
- scriptPipelineExecutionErrors.push(error_2);
3093
- return [3 /*break*/, 22];
3094
- case 22:
3095
- _j = _h.next();
3096
- return [3 /*break*/, 18];
3097
- case 23: return [3 /*break*/, 26];
3098
- case 24:
3099
- e_4_1 = _u.sent();
3100
- e_4 = { error: e_4_1 };
3101
- return [3 /*break*/, 26];
3102
- case 25:
3103
- try {
3104
- if (_j && !_j.done && (_q = _h.return)) _q.call(_h);
3105
- }
3106
- finally { if (e_4) throw e_4.error; }
3107
- return [7 /*endfinally*/];
3108
- case 26:
3109
- if (resultString !== null) {
3110
- return [3 /*break*/, 30];
3111
- }
3112
- if (scriptPipelineExecutionErrors.length === 1) {
3113
- throw scriptPipelineExecutionErrors[0];
3114
- }
3115
- else {
3116
- throw new PipelineExecutionError(spaceTrim(function (block) { return "\n Script execution failed ".concat(scriptPipelineExecutionErrors.length, " times\n\n ").concat(block(scriptPipelineExecutionErrors
3117
- .map(function (error) { return '- ' + error.message; })
3118
- .join('\n\n')), "\n "); }));
3119
- }
3120
- case 27:
3121
- if (tools.userInterface === undefined) {
3122
- throw new PipelineExecutionError('User interface tools are not available');
3123
- }
3124
- return [4 /*yield*/, tools.userInterface.promptDialog($deepFreeze({
3125
- promptTitle: currentTemplate.title,
3126
- promptMessage: replaceParameters(currentTemplate.description || '', parameters),
3127
- defaultValue: replaceParameters(preparedContent, parameters),
3128
- // TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
3129
- placeholder: undefined,
3130
- priority: priority,
3131
- }))];
3132
- case 28:
3133
- // TODO: [🌹] When making next attempt for `PROMPT DIALOG`, preserve the previous user input
3134
- resultString = _u.sent();
3135
- return [3 /*break*/, 30];
3136
- case 29: throw new PipelineExecutionError("Unknown execution type \"".concat(currentTemplate.blockType, "\""));
3137
- case 30:
3138
- if (!(!isJokerAttempt && currentTemplate.postprocessingFunctionNames)) return [3 /*break*/, 47];
3139
- _u.label = 31;
3140
- case 31:
3141
- _u.trys.push([31, 45, 46, 47]);
3142
- _k = (e_6 = void 0, __values(currentTemplate.postprocessingFunctionNames)), _l = _k.next();
3143
- _u.label = 32;
3144
- case 32:
3145
- if (!!_l.done) return [3 /*break*/, 44];
3146
- functionName = _l.value;
3147
- // TODO: DRY [1]
3148
- scriptPipelineExecutionErrors = [];
3149
- postprocessingError = null;
3150
- _u.label = 33;
3151
- case 33:
3152
- _u.trys.push([33, 40, 41, 42]);
3153
- _m = (e_5 = void 0, __values(arrayableToArray(tools.script))), _o = _m.next();
3154
- _u.label = 34;
3155
- case 34:
3156
- if (!!_o.done) return [3 /*break*/, 39];
3157
- scriptTools = _o.value;
3158
- _u.label = 35;
3159
- case 35:
3160
- _u.trys.push([35, 37, , 38]);
3161
- return [4 /*yield*/, scriptTools.execute({
3162
- scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
3163
- script: "".concat(functionName, "(resultString)"),
3164
- parameters: {
3165
- resultString: resultString || '',
3166
- // Note: No ...parametersForTemplate, because working with result only
3167
- },
3168
- })];
3169
- case 36:
3170
- resultString = _u.sent();
3171
- postprocessingError = null;
3172
- return [3 /*break*/, 39];
3173
- case 37:
3174
- error_3 = _u.sent();
3175
- if (!(error_3 instanceof Error)) {
3176
- throw error_3;
3177
- }
3178
- if (error_3 instanceof UnexpectedError) {
3179
- throw error_3;
3180
- }
3181
- postprocessingError = error_3;
3182
- scriptPipelineExecutionErrors.push(error_3);
3183
- return [3 /*break*/, 38];
3184
- case 38:
3185
- _o = _m.next();
3186
- return [3 /*break*/, 34];
3187
- case 39: return [3 /*break*/, 42];
3188
- case 40:
3189
- e_5_1 = _u.sent();
3190
- e_5 = { error: e_5_1 };
3191
- return [3 /*break*/, 42];
3192
- case 41:
3193
- try {
3194
- if (_o && !_o.done && (_s = _m.return)) _s.call(_m);
3195
- }
3196
- finally { if (e_5) throw e_5.error; }
3197
- return [7 /*endfinally*/];
3198
- case 42:
3199
- if (postprocessingError) {
3200
- throw postprocessingError;
3201
- }
3202
- _u.label = 43;
3203
- case 43:
3204
- _l = _k.next();
3205
- return [3 /*break*/, 32];
3206
- case 44: return [3 /*break*/, 47];
3207
- case 45:
3208
- e_6_1 = _u.sent();
3209
- e_6 = { error: e_6_1 };
3210
- return [3 /*break*/, 47];
3211
- case 46:
3212
- try {
3213
- if (_l && !_l.done && (_r = _k.return)) _r.call(_k);
3214
- }
3215
- finally { if (e_6) throw e_6.error; }
3216
- return [7 /*endfinally*/];
3217
- case 47:
3218
- // TODO: [πŸ’] Unite object for expecting amount and format
3219
- if (currentTemplate.expectFormat) {
3220
- if (currentTemplate.expectFormat === 'JSON') {
3221
- if (!isValidJsonString(resultString || '')) {
3222
- throw new ExpectError('Expected valid JSON string');
3223
- }
3224
- }
3225
- }
3226
- // TODO: [πŸ’] Unite object for expecting amount and format
3227
- if (currentTemplate.expectations) {
3228
- checkExpectations(currentTemplate.expectations, resultString || '');
3229
- }
3230
- return [3 /*break*/, 52];
3231
- case 48:
3232
- error_4 = _u.sent();
3233
- if (!(error_4 instanceof ExpectError)) {
3234
- throw error_4;
3235
- }
3236
- expectError = error_4;
3237
- return [3 /*break*/, 50];
3238
- case 49:
3239
- if (!isJokerAttempt &&
3240
- currentTemplate.blockType === 'PROMPT_TEMPLATE' &&
3241
- prompt
3242
- // <- Note: [2] When some expected parameter is not defined, error will occur in replaceParameters
3243
- // In that case we don’t want to make a report about it because it’s not a llm execution error
3244
- ) {
3245
- // TODO: [🧠] Maybe put other blockTypes into report
3246
- executionReport.promptExecutions.push({
3247
- prompt: __assign({}, prompt),
3248
- result: result || undefined,
3249
- error: expectError === null ? undefined : serializeError(expectError),
3250
- });
3251
- }
3252
- return [7 /*endfinally*/];
3253
- case 50:
3254
- if (expectError !== null && attempt === maxAttempts - 1) {
3255
- throw new PipelineExecutionError(spaceTrim(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 "); }));
3256
- }
3257
- _u.label = 51;
3258
- case 51:
3259
3290
  attempt++;
3260
3291
  return [3 /*break*/, 4];
3261
- case 52:
3292
+ case 7:
3262
3293
  if (resultString === null) {
3263
- throw new UnexpectedError('Something went wrong and prompt result is null');
3294
+ throw new UnexpectedError(spaceTrim(function (block) { return "\n Something went wrong and prompt result is null\n\n ".concat(block(pipelineIdentification), "\n "); }));
3264
3295
  }
3265
3296
  if (onProgress /* <- [3] */) {
3266
3297
  onProgress({
@@ -3274,7 +3305,7 @@ function createPipelineExecutor(options) {
3274
3305
  // <- [3]
3275
3306
  });
3276
3307
  }
3277
- 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)));
3308
+ parametersToPass = Object.freeze(__assign(__assign({}, parametersToPass), (_g = {}, _g[currentTemplate.resultingParameterName] = resultString /* <- Note: Not need to detect parameter collision here because pipeline checks logic consistency during construction */, _g)));
3278
3309
  return [2 /*return*/];
3279
3310
  }
3280
3311
  });
@@ -3283,6 +3314,14 @@ function createPipelineExecutor(options) {
3283
3314
  function filterJustOutputParameters() {
3284
3315
  var e_8, _a;
3285
3316
  var outputParameters = {};
3317
+ var _loop_6 = function (parameter) {
3318
+ if (parametersToPass[parameter.name] === undefined) {
3319
+ // [4]
3320
+ warnings.push(new PipelineExecutionError(spaceTrim(function (block) { return "\n Parameter {".concat(parameter.name, "} should be an output parameter, but it was not generated during pipeline execution\n\n ").concat(block(pipelineIdentification), "\n "); })));
3321
+ return "continue";
3322
+ }
3323
+ outputParameters[parameter.name] = parametersToPass[parameter.name] || '';
3324
+ };
3286
3325
  try {
3287
3326
  // Note: Filter ONLY output parameters
3288
3327
  for (var _b = __values(preparedPipeline.parameters.filter(function (_a) {
@@ -3290,12 +3329,7 @@ function createPipelineExecutor(options) {
3290
3329
  return isOutput;
3291
3330
  })), _c = _b.next(); !_c.done; _c = _b.next()) {
3292
3331
  var parameter = _c.value;
3293
- if (parametersToPass[parameter.name] === undefined) {
3294
- // [4]
3295
- warnings.push(new PipelineExecutionError("Parameter {".concat(parameter.name, "} should be an output parameter, but it was not generated during pipeline execution")));
3296
- continue;
3297
- }
3298
- outputParameters[parameter.name] = parametersToPass[parameter.name] || '';
3332
+ _loop_6(parameter);
3299
3333
  }
3300
3334
  }
3301
3335
  catch (e_8_1) { e_8 = { error: e_8_1 }; }
@@ -3367,13 +3401,13 @@ function createPipelineExecutor(options) {
3367
3401
  return name === parameterName;
3368
3402
  });
3369
3403
  if (parameter === undefined) {
3370
- warnings.push(new PipelineExecutionError("Extra parameter {".concat(parameterName, "} is being passed which is not part of the pipeline.")));
3404
+ warnings.push(new PipelineExecutionError(spaceTrim(function (block) { return "\n Extra parameter {".concat(parameterName, "} is being passed which is not part of the pipeline.\n\n ").concat(block(pipelineIdentification), "\n "); })));
3371
3405
  }
3372
3406
  else if (parameter.isInput === false) {
3373
- return { value: $asDeeplyFrozenSerializableJson("Unuccessful PipelineExecutorResult (with extra parameter {".concat(parameter.name, "}) PipelineExecutorResult"), {
3407
+ return { value: $asDeeplyFrozenSerializableJson(spaceTrim(function (block) { return "\n Unuccessful PipelineExecutorResult (with extra parameter {".concat(parameter.name, "}) PipelineExecutorResult\n\n ").concat(block(pipelineIdentification), "\n "); }), {
3374
3408
  isSuccessful: false,
3375
3409
  errors: __spreadArray([
3376
- new PipelineExecutionError("Parameter {".concat(parameter.name, "} is passed as input parameter but it is not input"))
3410
+ new PipelineExecutionError(spaceTrim(function (block) { return "\n Parameter {".concat(parameter.name, "} is passed as input parameter but it is not input\n\n ").concat(block(pipelineIdentification), "\n "); }))
3377
3411
  ], __read(errors), false).map(serializeError),
3378
3412
  warnings: warnings.map(serializeError),
3379
3413
  executionReport: executionReport,
@@ -3422,7 +3456,7 @@ function createPipelineExecutor(options) {
3422
3456
  case 0:
3423
3457
  if (loopLimit-- < 0) {
3424
3458
  // Note: Really UnexpectedError not LimitReachedError - this should be catched during validatePipeline
3425
- throw new UnexpectedError('Loop limit reached during resolving parameters pipeline execution');
3459
+ throw new UnexpectedError(spaceTrim(function (block) { return "\n Loop limit reached during resolving parameters pipeline execution\n\n ".concat(block(pipelineIdentification), "\n "); }));
3426
3460
  }
3427
3461
  currentTemplate = unresovedTemplates_1.find(function (template) {
3428
3462
  return template.dependentParameterNames.every(function (name) {
@@ -3432,7 +3466,7 @@ function createPipelineExecutor(options) {
3432
3466
  if (!(!currentTemplate && resolving_1.length === 0)) return [3 /*break*/, 1];
3433
3467
  throw new UnexpectedError(
3434
3468
  // TODO: [🐎] DRY
3435
- spaceTrim(function (block) { return "\n Can not resolve some parameters:\n\n Can not resolve:\n ".concat(block(unresovedTemplates_1
3469
+ spaceTrim(function (block) { return "\n Can not resolve some parameters:\n\n ".concat(block(pipelineIdentification), "\n\n Can not resolve:\n ").concat(block(unresovedTemplates_1
3436
3470
  .map(function (_a) {
3437
3471
  var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
3438
3472
  return "- Parameter {".concat(resultingParameterName, "} which depends on ").concat(dependentParameterNames
@@ -3512,6 +3546,7 @@ function createPipelineExecutor(options) {
3512
3546
  return pipelineExecutor;
3513
3547
  }
3514
3548
  /**
3549
+ * TODO: !!! Identify not only pipeline BUT exact template ${block(pipelineIdentification)}
3515
3550
  * TODO: Use isVerbose here (not only pass to `preparePipeline`)
3516
3551
  * TODO: [🧠][🌳] Use here `countTotalUsage` and put preparation and prepared pipiline to report
3517
3552
  * TODO: [πŸͺ‚] Use maxParallelCount here (not only pass to `preparePipeline`)