@promptbook/core 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/umd/index.umd.js CHANGED
@@ -16,7 +16,7 @@
16
16
  /**
17
17
  * The version of the Promptbook library
18
18
  */
19
- var PROMPTBOOK_VERSION = '0.67.1';
19
+ var PROMPTBOOK_VERSION = '0.67.2';
20
20
  // TODO: !!!! List here all the versions and annotate + put into script
21
21
 
22
22
  /*! *****************************************************************************
@@ -1786,7 +1786,7 @@
1786
1786
  });
1787
1787
  }
1788
1788
 
1789
- 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"}];
1789
+ 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"}];
1790
1790
 
1791
1791
  var defaultDiacriticsRemovalMap = [
1792
1792
  {
@@ -3138,13 +3138,24 @@
3138
3138
  var pipeline = options.pipeline, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
3139
3139
  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;
3140
3140
  validatePipeline(pipeline);
3141
+ var pipelineIdentification = (function () {
3142
+ // Note: This is a 😐 implementation of [🚞]
3143
+ var _ = [];
3144
+ if (pipeline.sourceFile !== undefined) {
3145
+ _.push("File: ".concat(pipeline.sourceFile));
3146
+ }
3147
+ if (pipeline.pipelineUrl !== undefined) {
3148
+ _.push("Url: ".concat(pipeline.pipelineUrl));
3149
+ }
3150
+ return _.join('\n');
3151
+ })();
3141
3152
  var llmTools = joinLlmExecutionTools.apply(void 0, __spreadArray([], __read(arrayableToArray(tools.llm)), false));
3142
3153
  var preparedPipeline;
3143
3154
  if (isPipelinePrepared(pipeline)) {
3144
3155
  preparedPipeline = pipeline;
3145
3156
  }
3146
3157
  else if (isNotPreparedWarningSupressed !== true) {
3147
- console.warn(spaceTrim.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 ")));
3158
+ console.warn(spaceTrim.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 "); }));
3148
3159
  }
3149
3160
  var pipelineExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
3150
3161
  // TODO: !!! Extract to separate functions and files - ALL FUNCTIONS BELOW
@@ -3182,7 +3193,7 @@
3182
3193
  }
3183
3194
  function getReservedParametersForTemplate(template) {
3184
3195
  return __awaiter(this, void 0, void 0, function () {
3185
- var context, knowledge, samples, currentDate, modelName, reservedParameters, RESERVED_PARAMETER_NAMES_1, RESERVED_PARAMETER_NAMES_1_1, parameterName;
3196
+ var context, knowledge, samples, currentDate, modelName, reservedParameters, _loop_3, RESERVED_PARAMETER_NAMES_1, RESERVED_PARAMETER_NAMES_1_1, parameterName;
3186
3197
  var e_3, _a;
3187
3198
  return __generator(this, function (_b) {
3188
3199
  switch (_b.label) {
@@ -3205,13 +3216,16 @@
3205
3216
  currentDate: currentDate,
3206
3217
  modelName: modelName,
3207
3218
  };
3219
+ _loop_3 = function (parameterName) {
3220
+ if (reservedParameters[parameterName] === undefined) {
3221
+ throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Reserved parameter {".concat(parameterName, "} is not defined\n\n ").concat(block(pipelineIdentification), "\n "); }));
3222
+ }
3223
+ };
3208
3224
  try {
3209
3225
  // Note: Doublecheck that ALL reserved parameters are defined:
3210
3226
  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()) {
3211
3227
  parameterName = RESERVED_PARAMETER_NAMES_1_1.value;
3212
- if (reservedParameters[parameterName] === undefined) {
3213
- throw new UnexpectedError("Reserved parameter {".concat(parameterName, "} is not defined"));
3214
- }
3228
+ _loop_3(parameterName);
3215
3229
  }
3216
3230
  }
3217
3231
  catch (e_3_1) { e_3 = { error: e_3_1 }; }
@@ -3228,10 +3242,10 @@
3228
3242
  }
3229
3243
  function executeSingleTemplate(currentTemplate) {
3230
3244
  return __awaiter(this, void 0, void 0, function () {
3231
- 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;
3232
- var e_7, _p, e_4, _q, e_6, _r, e_5, _s, _t;
3233
- return __generator(this, function (_u) {
3234
- switch (_u.label) {
3245
+ 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;
3246
+ var e_4, _f, _g;
3247
+ return __generator(this, function (_h) {
3248
+ switch (_h.label) {
3235
3249
  case 0:
3236
3250
  name = "pipeline-executor-frame-".concat(currentTemplate.name);
3237
3251
  title = currentTemplate.title;
@@ -3248,47 +3262,48 @@
3248
3262
  // <- [3]
3249
3263
  })];
3250
3264
  case 1:
3251
- _u.sent();
3252
- _u.label = 2;
3265
+ _h.sent();
3266
+ _h.label = 2;
3253
3267
  case 2:
3254
3268
  usedParameterNames = extractParameterNamesFromPromptTemplate(currentTemplate);
3255
3269
  dependentParameterNames = new Set(currentTemplate.dependentParameterNames);
3256
3270
  if (union(difference(usedParameterNames, dependentParameterNames), difference(dependentParameterNames, usedParameterNames)).size !== 0) {
3257
- throw new UnexpectedError(spaceTrim.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 ")));
3271
+ throw new UnexpectedError(spaceTrim.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 "); }));
3258
3272
  }
3259
3273
  _b = (_a = Object).freeze;
3260
3274
  _c = [{}];
3261
3275
  return [4 /*yield*/, getReservedParametersForTemplate(currentTemplate)];
3262
3276
  case 3:
3263
- definedParameters = _b.apply(_a, [__assign.apply(void 0, [__assign.apply(void 0, _c.concat([(_u.sent())])), parametersToPass])]);
3277
+ definedParameters = _b.apply(_a, [__assign.apply(void 0, [__assign.apply(void 0, _c.concat([(_h.sent())])), parametersToPass])]);
3264
3278
  definedParameterNames = new Set(Object.keys(definedParameters));
3265
3279
  parameters = {};
3280
+ _loop_4 = function (parameterName) {
3281
+ // Situation: Parameter is defined and used
3282
+ if (definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
3283
+ parameters[parameterName] = definedParameters[parameterName];
3284
+ }
3285
+ // Situation: Parameter is defined but NOT used
3286
+ else if (definedParameterNames.has(parameterName) && !usedParameterNames.has(parameterName)) ;
3287
+ // Situation: Parameter is NOT defined BUT used
3288
+ else if (!definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
3289
+ // Houston, we have a problem
3290
+ // Note: Checking part is also done in `validatePipeline`, but it’s good to doublecheck
3291
+ throw new UnexpectedError(spaceTrim.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 "); }));
3292
+ }
3293
+ };
3266
3294
  try {
3267
3295
  // Note: [2] Check that all used parameters are defined and removing unused parameters for this template
3268
3296
  for (_d = __values(Array.from(union(definedParameterNames, usedParameterNames, dependentParameterNames))), _e = _d.next(); !_e.done; _e = _d.next()) {
3269
3297
  parameterName = _e.value;
3270
- // Situation: Parameter is defined and used
3271
- if (definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
3272
- parameters[parameterName] = definedParameters[parameterName];
3273
- }
3274
- // Situation: Parameter is defined but NOT used
3275
- else if (definedParameterNames.has(parameterName) && !usedParameterNames.has(parameterName)) {
3276
- // Do not pass this parameter to prompt
3277
- }
3278
- // Situation: Parameter is NOT defined BUT used
3279
- else if (!definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
3280
- // Houston, we have a problem
3281
- // Note: Checking part is also done in `validatePipeline`, but it’s good to doublecheck
3282
- throw new UnexpectedError(spaceTrim.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 ")));
3283
- }
3298
+ _loop_4(parameterName);
3284
3299
  }
3285
3300
  }
3286
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
3301
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
3287
3302
  finally {
3288
3303
  try {
3289
- if (_e && !_e.done && (_p = _d.return)) _p.call(_d);
3304
+ if (_e && !_e.done && (_f = _d.return)) _f.call(_d);
3290
3305
  }
3291
- finally { if (e_7) throw e_7.error; }
3306
+ finally { if (e_4) throw e_4.error; }
3292
3307
  }
3293
3308
  // Note: Now we can freeze `parameters` because we are sure that all and only used parameters are defined
3294
3309
  Object.freeze(parameters);
@@ -3300,294 +3315,310 @@
3300
3315
  preparedContent = (currentTemplate.preparedContent || '{content}')
3301
3316
  .split('{content}')
3302
3317
  .join(currentTemplate.content);
3318
+ _loop_5 = function (attempt) {
3319
+ 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;
3320
+ var e_5, _s, e_7, _t, e_6, _u;
3321
+ return __generator(this, function (_v) {
3322
+ switch (_v.label) {
3323
+ case 0:
3324
+ isJokerAttempt = attempt < 0;
3325
+ jokerParameterName = jokerParameterNames[jokerParameterNames.length + attempt];
3326
+ if (isJokerAttempt && !jokerParameterName) {
3327
+ throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Joker not found in attempt ".concat(attempt, "\n\n ").concat(block(pipelineIdentification), "\n "); }));
3328
+ }
3329
+ result = null;
3330
+ resultString = null;
3331
+ expectError = null;
3332
+ if (isJokerAttempt) {
3333
+ if (parameters[jokerParameterName] === undefined) {
3334
+ throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Joker parameter {".concat(jokerParameterName, "} not defined\n\n ").concat(block(pipelineIdentification), "\n "); }));
3335
+ // <- TODO: This is maybe `PipelineLogicError` which should be detected in `validatePipeline` and here just thrown as `UnexpectedError`
3336
+ }
3337
+ else {
3338
+ resultString = parameters[jokerParameterName];
3339
+ }
3340
+ }
3341
+ _v.label = 1;
3342
+ case 1:
3343
+ _v.trys.push([1, 44, 45, 46]);
3344
+ if (!!isJokerAttempt) return [3 /*break*/, 26];
3345
+ _j = currentTemplate.blockType;
3346
+ switch (_j) {
3347
+ case 'SIMPLE_TEMPLATE': return [3 /*break*/, 2];
3348
+ case 'PROMPT_TEMPLATE': return [3 /*break*/, 3];
3349
+ case 'SCRIPT': return [3 /*break*/, 12];
3350
+ case 'PROMPT_DIALOG': return [3 /*break*/, 23];
3351
+ }
3352
+ return [3 /*break*/, 25];
3353
+ case 2:
3354
+ resultString = replaceParameters(preparedContent, parameters);
3355
+ return [3 /*break*/, 26];
3356
+ case 3:
3357
+ prompt = {
3358
+ title: currentTemplate.title,
3359
+ pipelineUrl: "".concat(preparedPipeline.pipelineUrl
3360
+ ? preparedPipeline.pipelineUrl
3361
+ : 'anonymous' /* <- TODO: [🧠] How to deal with anonymous pipelines, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
3362
+ parameters: parameters,
3363
+ content: preparedContent,
3364
+ modelRequirements: currentTemplate.modelRequirements,
3365
+ expectations: __assign(__assign({}, (preparedPipeline.personas.find(function (_a) {
3366
+ var name = _a.name;
3367
+ return name === currentTemplate.personaName;
3368
+ }) || {})), currentTemplate.expectations),
3369
+ expectFormat: currentTemplate.expectFormat,
3370
+ postprocessingFunctionNames: currentTemplate.postprocessingFunctionNames,
3371
+ }; // <- TODO: Not very good type guard
3372
+ _k = currentTemplate.modelRequirements.modelVariant;
3373
+ switch (_k) {
3374
+ case 'CHAT': return [3 /*break*/, 4];
3375
+ case 'COMPLETION': return [3 /*break*/, 6];
3376
+ case 'EMBEDDING': return [3 /*break*/, 8];
3377
+ }
3378
+ return [3 /*break*/, 10];
3379
+ case 4: return [4 /*yield*/, llmTools.callChatModel($deepFreeze(prompt))];
3380
+ case 5:
3381
+ chatResult = _v.sent();
3382
+ // TODO: [🍬] Destroy chatThread
3383
+ result = chatResult;
3384
+ resultString = chatResult.content;
3385
+ return [3 /*break*/, 11];
3386
+ case 6: return [4 /*yield*/, llmTools.callCompletionModel($deepFreeze(prompt))];
3387
+ case 7:
3388
+ completionResult = _v.sent();
3389
+ result = completionResult;
3390
+ resultString = completionResult.content;
3391
+ return [3 /*break*/, 11];
3392
+ case 8: return [4 /*yield*/, llmTools.callEmbeddingModel($deepFreeze(prompt))];
3393
+ case 9:
3394
+ embeddingResult = _v.sent();
3395
+ result = embeddingResult;
3396
+ resultString = embeddingResult.content.join(',');
3397
+ return [3 /*break*/, 11];
3398
+ case 10: throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\"\n\n ").concat(block(pipelineIdentification), "\n\n\n "); }));
3399
+ case 11: return [3 /*break*/, 26];
3400
+ case 12:
3401
+ if (arrayableToArray(tools.script).length === 0) {
3402
+ throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n No script execution tools are available\n\n ".concat(block(pipelineIdentification), "\n "); }));
3403
+ }
3404
+ if (!currentTemplate.contentLanguage) {
3405
+ throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Script language is not defined for prompt template \"".concat(currentTemplate.name, "\"\n\n ").concat(block(pipelineIdentification), "\n "); }));
3406
+ }
3407
+ // TODO: DRY [1]
3408
+ scriptPipelineExecutionErrors = [];
3409
+ _v.label = 13;
3410
+ case 13:
3411
+ _v.trys.push([13, 20, 21, 22]);
3412
+ _l = (e_5 = void 0, __values(arrayableToArray(tools.script))), _m = _l.next();
3413
+ _v.label = 14;
3414
+ case 14:
3415
+ if (!!_m.done) return [3 /*break*/, 19];
3416
+ scriptTools = _m.value;
3417
+ _v.label = 15;
3418
+ case 15:
3419
+ _v.trys.push([15, 17, , 18]);
3420
+ return [4 /*yield*/, scriptTools.execute($deepFreeze({
3421
+ scriptLanguage: currentTemplate.contentLanguage,
3422
+ script: preparedContent,
3423
+ parameters: parameters,
3424
+ }))];
3425
+ case 16:
3426
+ resultString = _v.sent();
3427
+ return [3 /*break*/, 19];
3428
+ case 17:
3429
+ error_2 = _v.sent();
3430
+ if (!(error_2 instanceof Error)) {
3431
+ throw error_2;
3432
+ }
3433
+ if (error_2 instanceof UnexpectedError) {
3434
+ throw error_2;
3435
+ }
3436
+ scriptPipelineExecutionErrors.push(error_2);
3437
+ return [3 /*break*/, 18];
3438
+ case 18:
3439
+ _m = _l.next();
3440
+ return [3 /*break*/, 14];
3441
+ case 19: return [3 /*break*/, 22];
3442
+ case 20:
3443
+ e_5_1 = _v.sent();
3444
+ e_5 = { error: e_5_1 };
3445
+ return [3 /*break*/, 22];
3446
+ case 21:
3447
+ try {
3448
+ if (_m && !_m.done && (_s = _l.return)) _s.call(_l);
3449
+ }
3450
+ finally { if (e_5) throw e_5.error; }
3451
+ return [7 /*endfinally*/];
3452
+ case 22:
3453
+ if (resultString !== null) {
3454
+ return [3 /*break*/, 26];
3455
+ }
3456
+ if (scriptPipelineExecutionErrors.length === 1) {
3457
+ throw scriptPipelineExecutionErrors[0];
3458
+ }
3459
+ else {
3460
+ throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Script execution failed ".concat(scriptPipelineExecutionErrors.length, " times\n\n ").concat(block(pipelineIdentification), "\n\n ").concat(block(scriptPipelineExecutionErrors
3461
+ .map(function (error) { return '- ' + error.message; })
3462
+ .join('\n\n')), "\n "); }));
3463
+ }
3464
+ case 23:
3465
+ if (tools.userInterface === undefined) {
3466
+ throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n User interface tools are not available\n\n ".concat(block(pipelineIdentification), "\n "); }));
3467
+ }
3468
+ return [4 /*yield*/, tools.userInterface.promptDialog($deepFreeze({
3469
+ promptTitle: currentTemplate.title,
3470
+ promptMessage: replaceParameters(currentTemplate.description || '', parameters),
3471
+ defaultValue: replaceParameters(preparedContent, parameters),
3472
+ // TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
3473
+ placeholder: undefined,
3474
+ priority: priority,
3475
+ }))];
3476
+ case 24:
3477
+ // TODO: [🌹] When making next attempt for `PROMPT DIALOG`, preserve the previous user input
3478
+ resultString = _v.sent();
3479
+ return [3 /*break*/, 26];
3480
+ case 25: throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Unknown execution type \"".concat(currentTemplate.blockType, "\"\n\n ").concat(block(pipelineIdentification), "\n "); }));
3481
+ case 26:
3482
+ if (!(!isJokerAttempt && currentTemplate.postprocessingFunctionNames)) return [3 /*break*/, 43];
3483
+ _v.label = 27;
3484
+ case 27:
3485
+ _v.trys.push([27, 41, 42, 43]);
3486
+ _o = (e_7 = void 0, __values(currentTemplate.postprocessingFunctionNames)), _p = _o.next();
3487
+ _v.label = 28;
3488
+ case 28:
3489
+ if (!!_p.done) return [3 /*break*/, 40];
3490
+ functionName = _p.value;
3491
+ // TODO: DRY [1]
3492
+ scriptPipelineExecutionErrors = [];
3493
+ postprocessingError = null;
3494
+ _v.label = 29;
3495
+ case 29:
3496
+ _v.trys.push([29, 36, 37, 38]);
3497
+ _q = (e_6 = void 0, __values(arrayableToArray(tools.script))), _r = _q.next();
3498
+ _v.label = 30;
3499
+ case 30:
3500
+ if (!!_r.done) return [3 /*break*/, 35];
3501
+ scriptTools = _r.value;
3502
+ _v.label = 31;
3503
+ case 31:
3504
+ _v.trys.push([31, 33, , 34]);
3505
+ return [4 /*yield*/, scriptTools.execute({
3506
+ scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
3507
+ script: "".concat(functionName, "(resultString)"),
3508
+ parameters: {
3509
+ resultString: resultString || '',
3510
+ // Note: No ...parametersForTemplate, because working with result only
3511
+ },
3512
+ })];
3513
+ case 32:
3514
+ resultString = _v.sent();
3515
+ postprocessingError = null;
3516
+ return [3 /*break*/, 35];
3517
+ case 33:
3518
+ error_3 = _v.sent();
3519
+ if (!(error_3 instanceof Error)) {
3520
+ throw error_3;
3521
+ }
3522
+ if (error_3 instanceof UnexpectedError) {
3523
+ throw error_3;
3524
+ }
3525
+ postprocessingError = error_3;
3526
+ scriptPipelineExecutionErrors.push(error_3);
3527
+ return [3 /*break*/, 34];
3528
+ case 34:
3529
+ _r = _q.next();
3530
+ return [3 /*break*/, 30];
3531
+ case 35: return [3 /*break*/, 38];
3532
+ case 36:
3533
+ e_6_1 = _v.sent();
3534
+ e_6 = { error: e_6_1 };
3535
+ return [3 /*break*/, 38];
3536
+ case 37:
3537
+ try {
3538
+ if (_r && !_r.done && (_u = _q.return)) _u.call(_q);
3539
+ }
3540
+ finally { if (e_6) throw e_6.error; }
3541
+ return [7 /*endfinally*/];
3542
+ case 38:
3543
+ if (postprocessingError) {
3544
+ throw postprocessingError;
3545
+ }
3546
+ _v.label = 39;
3547
+ case 39:
3548
+ _p = _o.next();
3549
+ return [3 /*break*/, 28];
3550
+ case 40: return [3 /*break*/, 43];
3551
+ case 41:
3552
+ e_7_1 = _v.sent();
3553
+ e_7 = { error: e_7_1 };
3554
+ return [3 /*break*/, 43];
3555
+ case 42:
3556
+ try {
3557
+ if (_p && !_p.done && (_t = _o.return)) _t.call(_o);
3558
+ }
3559
+ finally { if (e_7) throw e_7.error; }
3560
+ return [7 /*endfinally*/];
3561
+ case 43:
3562
+ // TODO: [πŸ’] Unite object for expecting amount and format
3563
+ if (currentTemplate.expectFormat) {
3564
+ if (currentTemplate.expectFormat === 'JSON') {
3565
+ if (!isValidJsonString(resultString || '')) {
3566
+ throw new ExpectError(spaceTrim.spaceTrim(function (block) { return "\n Expected valid JSON string\n\n ".concat(block(pipelineIdentification), "\n "); }));
3567
+ }
3568
+ }
3569
+ }
3570
+ // TODO: [πŸ’] Unite object for expecting amount and format
3571
+ if (currentTemplate.expectations) {
3572
+ checkExpectations(currentTemplate.expectations, resultString || '');
3573
+ }
3574
+ return [2 /*return*/, "break-attempts"];
3575
+ case 44:
3576
+ error_4 = _v.sent();
3577
+ if (!(error_4 instanceof ExpectError)) {
3578
+ throw error_4;
3579
+ }
3580
+ expectError = error_4;
3581
+ return [3 /*break*/, 46];
3582
+ case 45:
3583
+ if (!isJokerAttempt &&
3584
+ currentTemplate.blockType === 'PROMPT_TEMPLATE' &&
3585
+ prompt
3586
+ // <- Note: [2] When some expected parameter is not defined, error will occur in replaceParameters
3587
+ // In that case we don’t want to make a report about it because it’s not a llm execution error
3588
+ ) {
3589
+ // TODO: [🧠] Maybe put other blockTypes into report
3590
+ executionReport.promptExecutions.push({
3591
+ prompt: __assign({}, prompt),
3592
+ result: result || undefined,
3593
+ error: expectError === null ? undefined : serializeError(expectError),
3594
+ });
3595
+ }
3596
+ return [7 /*endfinally*/];
3597
+ case 46:
3598
+ if (expectError !== null && attempt === maxAttempts - 1) {
3599
+ throw new PipelineExecutionError(spaceTrim.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 "); }));
3600
+ }
3601
+ return [2 /*return*/];
3602
+ }
3603
+ });
3604
+ };
3303
3605
  attempt = -jokerParameterNames.length;
3304
- _u.label = 4;
3606
+ _h.label = 4;
3305
3607
  case 4:
3306
- if (!(attempt < maxAttempts)) return [3 /*break*/, 52];
3307
- isJokerAttempt = attempt < 0;
3308
- jokerParameterName = jokerParameterNames[jokerParameterNames.length + attempt];
3309
- if (isJokerAttempt && !jokerParameterName) {
3310
- throw new UnexpectedError("Joker not found in attempt ".concat(attempt));
3311
- }
3312
- result = null;
3313
- resultString = null;
3314
- expectError = null;
3315
- if (isJokerAttempt) {
3316
- if (parameters[jokerParameterName] === undefined) {
3317
- throw new PipelineExecutionError("Joker parameter {".concat(jokerParameterName, "} not defined"));
3318
- // <- TODO: This is maybe `PipelineLogicError` which should be detected in `validatePipeline` and here just thrown as `UnexpectedError`
3319
- }
3320
- else {
3321
- resultString = parameters[jokerParameterName];
3322
- }
3323
- }
3324
- _u.label = 5;
3608
+ if (!(attempt < maxAttempts)) return [3 /*break*/, 7];
3609
+ return [5 /*yield**/, _loop_5(attempt)];
3325
3610
  case 5:
3326
- _u.trys.push([5, 48, 49, 50]);
3327
- if (!!isJokerAttempt) return [3 /*break*/, 30];
3328
- _f = currentTemplate.blockType;
3329
- switch (_f) {
3330
- case 'SIMPLE_TEMPLATE': return [3 /*break*/, 6];
3331
- case 'PROMPT_TEMPLATE': return [3 /*break*/, 7];
3332
- case 'SCRIPT': return [3 /*break*/, 16];
3333
- case 'PROMPT_DIALOG': return [3 /*break*/, 27];
3611
+ state_2 = _h.sent();
3612
+ switch (state_2) {
3613
+ case "break-attempts": return [3 /*break*/, 7];
3334
3614
  }
3335
- return [3 /*break*/, 29];
3615
+ _h.label = 6;
3336
3616
  case 6:
3337
- resultString = replaceParameters(preparedContent, parameters);
3338
- return [3 /*break*/, 30];
3339
- case 7:
3340
- prompt = {
3341
- title: currentTemplate.title,
3342
- pipelineUrl: "".concat(preparedPipeline.pipelineUrl
3343
- ? preparedPipeline.pipelineUrl
3344
- : 'anonymous' /* <- TODO: [🧠] How to deal with anonymous pipelines, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
3345
- parameters: parameters,
3346
- content: preparedContent,
3347
- modelRequirements: currentTemplate.modelRequirements,
3348
- expectations: __assign(__assign({}, (preparedPipeline.personas.find(function (_a) {
3349
- var name = _a.name;
3350
- return name === currentTemplate.personaName;
3351
- }) || {})), currentTemplate.expectations),
3352
- expectFormat: currentTemplate.expectFormat,
3353
- postprocessingFunctionNames: currentTemplate.postprocessingFunctionNames,
3354
- }; // <- TODO: Not very good type guard
3355
- _g = currentTemplate.modelRequirements.modelVariant;
3356
- switch (_g) {
3357
- case 'CHAT': return [3 /*break*/, 8];
3358
- case 'COMPLETION': return [3 /*break*/, 10];
3359
- case 'EMBEDDING': return [3 /*break*/, 12];
3360
- }
3361
- return [3 /*break*/, 14];
3362
- case 8: return [4 /*yield*/, llmTools.callChatModel($deepFreeze(prompt))];
3363
- case 9:
3364
- chatResult = _u.sent();
3365
- // TODO: [🍬] Destroy chatThread
3366
- result = chatResult;
3367
- resultString = chatResult.content;
3368
- return [3 /*break*/, 15];
3369
- case 10: return [4 /*yield*/, llmTools.callCompletionModel($deepFreeze(prompt))];
3370
- case 11:
3371
- completionResult = _u.sent();
3372
- result = completionResult;
3373
- resultString = completionResult.content;
3374
- return [3 /*break*/, 15];
3375
- case 12: return [4 /*yield*/, llmTools.callEmbeddingModel($deepFreeze(prompt))];
3376
- case 13:
3377
- embeddingResult = _u.sent();
3378
- result = embeddingResult;
3379
- resultString = embeddingResult.content.join(',');
3380
- return [3 /*break*/, 15];
3381
- case 14: throw new PipelineExecutionError("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
3382
- case 15: return [3 /*break*/, 30];
3383
- case 16:
3384
- if (arrayableToArray(tools.script).length === 0) {
3385
- throw new PipelineExecutionError('No script execution tools are available');
3386
- }
3387
- if (!currentTemplate.contentLanguage) {
3388
- throw new PipelineExecutionError("Script language is not defined for prompt template \"".concat(currentTemplate.name, "\""));
3389
- }
3390
- // TODO: DRY [1]
3391
- scriptPipelineExecutionErrors = [];
3392
- _u.label = 17;
3393
- case 17:
3394
- _u.trys.push([17, 24, 25, 26]);
3395
- _h = (e_4 = void 0, __values(arrayableToArray(tools.script))), _j = _h.next();
3396
- _u.label = 18;
3397
- case 18:
3398
- if (!!_j.done) return [3 /*break*/, 23];
3399
- scriptTools = _j.value;
3400
- _u.label = 19;
3401
- case 19:
3402
- _u.trys.push([19, 21, , 22]);
3403
- return [4 /*yield*/, scriptTools.execute($deepFreeze({
3404
- scriptLanguage: currentTemplate.contentLanguage,
3405
- script: preparedContent,
3406
- parameters: parameters,
3407
- }))];
3408
- case 20:
3409
- resultString = _u.sent();
3410
- return [3 /*break*/, 23];
3411
- case 21:
3412
- error_2 = _u.sent();
3413
- if (!(error_2 instanceof Error)) {
3414
- throw error_2;
3415
- }
3416
- if (error_2 instanceof UnexpectedError) {
3417
- throw error_2;
3418
- }
3419
- scriptPipelineExecutionErrors.push(error_2);
3420
- return [3 /*break*/, 22];
3421
- case 22:
3422
- _j = _h.next();
3423
- return [3 /*break*/, 18];
3424
- case 23: return [3 /*break*/, 26];
3425
- case 24:
3426
- e_4_1 = _u.sent();
3427
- e_4 = { error: e_4_1 };
3428
- return [3 /*break*/, 26];
3429
- case 25:
3430
- try {
3431
- if (_j && !_j.done && (_q = _h.return)) _q.call(_h);
3432
- }
3433
- finally { if (e_4) throw e_4.error; }
3434
- return [7 /*endfinally*/];
3435
- case 26:
3436
- if (resultString !== null) {
3437
- return [3 /*break*/, 30];
3438
- }
3439
- if (scriptPipelineExecutionErrors.length === 1) {
3440
- throw scriptPipelineExecutionErrors[0];
3441
- }
3442
- else {
3443
- throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Script execution failed ".concat(scriptPipelineExecutionErrors.length, " times\n\n ").concat(block(scriptPipelineExecutionErrors
3444
- .map(function (error) { return '- ' + error.message; })
3445
- .join('\n\n')), "\n "); }));
3446
- }
3447
- case 27:
3448
- if (tools.userInterface === undefined) {
3449
- throw new PipelineExecutionError('User interface tools are not available');
3450
- }
3451
- return [4 /*yield*/, tools.userInterface.promptDialog($deepFreeze({
3452
- promptTitle: currentTemplate.title,
3453
- promptMessage: replaceParameters(currentTemplate.description || '', parameters),
3454
- defaultValue: replaceParameters(preparedContent, parameters),
3455
- // TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
3456
- placeholder: undefined,
3457
- priority: priority,
3458
- }))];
3459
- case 28:
3460
- // TODO: [🌹] When making next attempt for `PROMPT DIALOG`, preserve the previous user input
3461
- resultString = _u.sent();
3462
- return [3 /*break*/, 30];
3463
- case 29: throw new PipelineExecutionError("Unknown execution type \"".concat(currentTemplate.blockType, "\""));
3464
- case 30:
3465
- if (!(!isJokerAttempt && currentTemplate.postprocessingFunctionNames)) return [3 /*break*/, 47];
3466
- _u.label = 31;
3467
- case 31:
3468
- _u.trys.push([31, 45, 46, 47]);
3469
- _k = (e_6 = void 0, __values(currentTemplate.postprocessingFunctionNames)), _l = _k.next();
3470
- _u.label = 32;
3471
- case 32:
3472
- if (!!_l.done) return [3 /*break*/, 44];
3473
- functionName = _l.value;
3474
- // TODO: DRY [1]
3475
- scriptPipelineExecutionErrors = [];
3476
- postprocessingError = null;
3477
- _u.label = 33;
3478
- case 33:
3479
- _u.trys.push([33, 40, 41, 42]);
3480
- _m = (e_5 = void 0, __values(arrayableToArray(tools.script))), _o = _m.next();
3481
- _u.label = 34;
3482
- case 34:
3483
- if (!!_o.done) return [3 /*break*/, 39];
3484
- scriptTools = _o.value;
3485
- _u.label = 35;
3486
- case 35:
3487
- _u.trys.push([35, 37, , 38]);
3488
- return [4 /*yield*/, scriptTools.execute({
3489
- scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
3490
- script: "".concat(functionName, "(resultString)"),
3491
- parameters: {
3492
- resultString: resultString || '',
3493
- // Note: No ...parametersForTemplate, because working with result only
3494
- },
3495
- })];
3496
- case 36:
3497
- resultString = _u.sent();
3498
- postprocessingError = null;
3499
- return [3 /*break*/, 39];
3500
- case 37:
3501
- error_3 = _u.sent();
3502
- if (!(error_3 instanceof Error)) {
3503
- throw error_3;
3504
- }
3505
- if (error_3 instanceof UnexpectedError) {
3506
- throw error_3;
3507
- }
3508
- postprocessingError = error_3;
3509
- scriptPipelineExecutionErrors.push(error_3);
3510
- return [3 /*break*/, 38];
3511
- case 38:
3512
- _o = _m.next();
3513
- return [3 /*break*/, 34];
3514
- case 39: return [3 /*break*/, 42];
3515
- case 40:
3516
- e_5_1 = _u.sent();
3517
- e_5 = { error: e_5_1 };
3518
- return [3 /*break*/, 42];
3519
- case 41:
3520
- try {
3521
- if (_o && !_o.done && (_s = _m.return)) _s.call(_m);
3522
- }
3523
- finally { if (e_5) throw e_5.error; }
3524
- return [7 /*endfinally*/];
3525
- case 42:
3526
- if (postprocessingError) {
3527
- throw postprocessingError;
3528
- }
3529
- _u.label = 43;
3530
- case 43:
3531
- _l = _k.next();
3532
- return [3 /*break*/, 32];
3533
- case 44: return [3 /*break*/, 47];
3534
- case 45:
3535
- e_6_1 = _u.sent();
3536
- e_6 = { error: e_6_1 };
3537
- return [3 /*break*/, 47];
3538
- case 46:
3539
- try {
3540
- if (_l && !_l.done && (_r = _k.return)) _r.call(_k);
3541
- }
3542
- finally { if (e_6) throw e_6.error; }
3543
- return [7 /*endfinally*/];
3544
- case 47:
3545
- // TODO: [πŸ’] Unite object for expecting amount and format
3546
- if (currentTemplate.expectFormat) {
3547
- if (currentTemplate.expectFormat === 'JSON') {
3548
- if (!isValidJsonString(resultString || '')) {
3549
- throw new ExpectError('Expected valid JSON string');
3550
- }
3551
- }
3552
- }
3553
- // TODO: [πŸ’] Unite object for expecting amount and format
3554
- if (currentTemplate.expectations) {
3555
- checkExpectations(currentTemplate.expectations, resultString || '');
3556
- }
3557
- return [3 /*break*/, 52];
3558
- case 48:
3559
- error_4 = _u.sent();
3560
- if (!(error_4 instanceof ExpectError)) {
3561
- throw error_4;
3562
- }
3563
- expectError = error_4;
3564
- return [3 /*break*/, 50];
3565
- case 49:
3566
- if (!isJokerAttempt &&
3567
- currentTemplate.blockType === 'PROMPT_TEMPLATE' &&
3568
- prompt
3569
- // <- Note: [2] When some expected parameter is not defined, error will occur in replaceParameters
3570
- // In that case we don’t want to make a report about it because it’s not a llm execution error
3571
- ) {
3572
- // TODO: [🧠] Maybe put other blockTypes into report
3573
- executionReport.promptExecutions.push({
3574
- prompt: __assign({}, prompt),
3575
- result: result || undefined,
3576
- error: expectError === null ? undefined : serializeError(expectError),
3577
- });
3578
- }
3579
- return [7 /*endfinally*/];
3580
- case 50:
3581
- if (expectError !== null && attempt === maxAttempts - 1) {
3582
- throw new PipelineExecutionError(spaceTrim.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 "); }));
3583
- }
3584
- _u.label = 51;
3585
- case 51:
3586
3617
  attempt++;
3587
3618
  return [3 /*break*/, 4];
3588
- case 52:
3619
+ case 7:
3589
3620
  if (resultString === null) {
3590
- throw new UnexpectedError('Something went wrong and prompt result is null');
3621
+ throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Something went wrong and prompt result is null\n\n ".concat(block(pipelineIdentification), "\n "); }));
3591
3622
  }
3592
3623
  if (onProgress /* <- [3] */) {
3593
3624
  onProgress({
@@ -3601,7 +3632,7 @@
3601
3632
  // <- [3]
3602
3633
  });
3603
3634
  }
3604
- 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)));
3635
+ 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)));
3605
3636
  return [2 /*return*/];
3606
3637
  }
3607
3638
  });
@@ -3610,6 +3641,14 @@
3610
3641
  function filterJustOutputParameters() {
3611
3642
  var e_8, _a;
3612
3643
  var outputParameters = {};
3644
+ var _loop_6 = function (parameter) {
3645
+ if (parametersToPass[parameter.name] === undefined) {
3646
+ // [4]
3647
+ warnings.push(new PipelineExecutionError(spaceTrim.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 "); })));
3648
+ return "continue";
3649
+ }
3650
+ outputParameters[parameter.name] = parametersToPass[parameter.name] || '';
3651
+ };
3613
3652
  try {
3614
3653
  // Note: Filter ONLY output parameters
3615
3654
  for (var _b = __values(preparedPipeline.parameters.filter(function (_a) {
@@ -3617,12 +3656,7 @@
3617
3656
  return isOutput;
3618
3657
  })), _c = _b.next(); !_c.done; _c = _b.next()) {
3619
3658
  var parameter = _c.value;
3620
- if (parametersToPass[parameter.name] === undefined) {
3621
- // [4]
3622
- warnings.push(new PipelineExecutionError("Parameter {".concat(parameter.name, "} should be an output parameter, but it was not generated during pipeline execution")));
3623
- continue;
3624
- }
3625
- outputParameters[parameter.name] = parametersToPass[parameter.name] || '';
3659
+ _loop_6(parameter);
3626
3660
  }
3627
3661
  }
3628
3662
  catch (e_8_1) { e_8 = { error: e_8_1 }; }
@@ -3694,13 +3728,13 @@
3694
3728
  return name === parameterName;
3695
3729
  });
3696
3730
  if (parameter === undefined) {
3697
- warnings.push(new PipelineExecutionError("Extra parameter {".concat(parameterName, "} is being passed which is not part of the pipeline.")));
3731
+ warnings.push(new PipelineExecutionError(spaceTrim.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 "); })));
3698
3732
  }
3699
3733
  else if (parameter.isInput === false) {
3700
- return { value: $asDeeplyFrozenSerializableJson("Unuccessful PipelineExecutorResult (with extra parameter {".concat(parameter.name, "}) PipelineExecutorResult"), {
3734
+ return { value: $asDeeplyFrozenSerializableJson(spaceTrim.spaceTrim(function (block) { return "\n Unuccessful PipelineExecutorResult (with extra parameter {".concat(parameter.name, "}) PipelineExecutorResult\n\n ").concat(block(pipelineIdentification), "\n "); }), {
3701
3735
  isSuccessful: false,
3702
3736
  errors: __spreadArray([
3703
- new PipelineExecutionError("Parameter {".concat(parameter.name, "} is passed as input parameter but it is not input"))
3737
+ new PipelineExecutionError(spaceTrim.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 "); }))
3704
3738
  ], __read(errors), false).map(serializeError),
3705
3739
  warnings: warnings.map(serializeError),
3706
3740
  executionReport: executionReport,
@@ -3749,7 +3783,7 @@
3749
3783
  case 0:
3750
3784
  if (loopLimit-- < 0) {
3751
3785
  // Note: Really UnexpectedError not LimitReachedError - this should be catched during validatePipeline
3752
- throw new UnexpectedError('Loop limit reached during resolving parameters pipeline execution');
3786
+ throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Loop limit reached during resolving parameters pipeline execution\n\n ".concat(block(pipelineIdentification), "\n "); }));
3753
3787
  }
3754
3788
  currentTemplate = unresovedTemplates_1.find(function (template) {
3755
3789
  return template.dependentParameterNames.every(function (name) {
@@ -3759,7 +3793,7 @@
3759
3793
  if (!(!currentTemplate && resolving_1.length === 0)) return [3 /*break*/, 1];
3760
3794
  throw new UnexpectedError(
3761
3795
  // TODO: [🐎] DRY
3762
- spaceTrim.spaceTrim(function (block) { return "\n Can not resolve some parameters:\n\n Can not resolve:\n ".concat(block(unresovedTemplates_1
3796
+ spaceTrim.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
3763
3797
  .map(function (_a) {
3764
3798
  var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
3765
3799
  return "- Parameter {".concat(resultingParameterName, "} which depends on ").concat(dependentParameterNames
@@ -3839,6 +3873,7 @@
3839
3873
  return pipelineExecutor;
3840
3874
  }
3841
3875
  /**
3876
+ * TODO: !!! Identify not only pipeline BUT exact template ${block(pipelineIdentification)}
3842
3877
  * TODO: Use isVerbose here (not only pass to `preparePipeline`)
3843
3878
  * TODO: [🧠][🌳] Use here `countTotalUsage` and put preparation and prepared pipiline to report
3844
3879
  * TODO: [πŸͺ‚] Use maxParallelCount here (not only pass to `preparePipeline`)