@promptbook/core 0.25.0 → 0.27.0-0

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
@@ -22,6 +22,22 @@
22
22
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23
23
  PERFORMANCE OF THIS SOFTWARE.
24
24
  ***************************************************************************** */
25
+ /* global Reflect, Promise */
26
+
27
+ var extendStatics = function(d, b) {
28
+ extendStatics = Object.setPrototypeOf ||
29
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
30
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
31
+ return extendStatics(d, b);
32
+ };
33
+
34
+ function __extends(d, b) {
35
+ if (typeof b !== "function" && b !== null)
36
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
37
+ extendStatics(d, b);
38
+ function __() { this.constructor = d; }
39
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
40
+ }
25
41
 
26
42
  var __assign = function() {
27
43
  __assign = Object.assign || function __assign(t) {
@@ -568,6 +584,12 @@
568
584
  functionName: functionName,
569
585
  };
570
586
  }
587
+ else if (type.startsWith('EXPECT_JSON')) {
588
+ return {
589
+ type: 'EXPECT_FORMAT',
590
+ format: 'JSON',
591
+ };
592
+ }
571
593
  else if (type.startsWith('EXPECT')) {
572
594
  try {
573
595
  listItemParts.shift();
@@ -623,7 +645,7 @@
623
645
  throw new Error("Invalid unit \"".concat(unitRaw, "\""));
624
646
  }
625
647
  return {
626
- type: 'EXPECT',
648
+ type: 'EXPECT_AMOUNT',
627
649
  sign: sign,
628
650
  unit: unit,
629
651
  amount: amount,
@@ -738,13 +760,14 @@
738
760
  finally { if (e_1) throw e_1.error; }
739
761
  }
740
762
  var _loop_1 = function (section) {
741
- var e_3, _e, e_4, _f;
763
+ var e_3, _e;
742
764
  // TODO: Parse prompt template description (the content out of the codeblock and lists)
743
765
  var templateModelRequirements = __assign({}, defaultModelRequirements);
744
- var postprocessingCommands = [];
745
766
  var listItems_3 = extractAllListItemsFromMarkdown(section.content);
746
767
  var executionType = 'PROMPT_TEMPLATE';
747
- var expectations = {};
768
+ var postprocessing = [];
769
+ var expectAmount = {};
770
+ var expectFormat = undefined;
748
771
  var isExecutionTypeChanged = false;
749
772
  try {
750
773
  for (var listItems_2 = (e_3 = void 0, __values(listItems_3)), listItems_2_1 = listItems_2.next(); !listItems_2_1.done; listItems_2_1 = listItems_2.next()) {
@@ -766,24 +789,30 @@
766
789
  addParam(command);
767
790
  break;
768
791
  case 'POSTPROCESS':
769
- postprocessingCommands.push(command);
792
+ postprocessing.push(command.functionName);
770
793
  break;
771
- case 'EXPECT':
794
+ case 'EXPECT_AMOUNT':
772
795
  // eslint-disable-next-line no-case-declarations
773
796
  var unit = command.unit.toLowerCase();
774
- expectations[unit] = expectations[unit] || {};
797
+ expectAmount[unit] = expectAmount[unit] || {};
775
798
  if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
776
- if (expectations[unit].min !== undefined) {
777
- throw new Error("Already defined minumum ".concat(expectations[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
799
+ if (expectAmount[unit].min !== undefined) {
800
+ throw new Error("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
778
801
  }
779
- expectations[unit].min = command.amount;
802
+ expectAmount[unit].min = command.amount;
780
803
  } /* not else */
781
804
  if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
782
- if (expectations[unit].max !== undefined) {
783
- throw new Error("Already defined maximum ".concat(expectations[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
805
+ if (expectAmount[unit].max !== undefined) {
806
+ throw new Error("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
784
807
  }
785
- expectations[unit].max = command.amount;
808
+ expectAmount[unit].max = command.amount;
809
+ }
810
+ break;
811
+ case 'EXPECT_FORMAT':
812
+ if (expectFormat !== undefined && command.format !== expectFormat) {
813
+ throw new Error("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
786
814
  }
815
+ expectFormat = command.format;
787
816
  break;
788
817
  default:
789
818
  throw new Error("Command ".concat(command.type, " is not allowed in the block of the prompt template ONLY at the head of the prompt template pipeline"));
@@ -797,7 +826,7 @@
797
826
  }
798
827
  finally { if (e_3) throw e_3.error; }
799
828
  }
800
- var _g = extractOneBlockFromMarkdown(section.content), language = _g.language, content = _g.content;
829
+ var _f = extractOneBlockFromMarkdown(section.content), language = _f.language, content = _f.content;
801
830
  if (executionType === 'SCRIPT') {
802
831
  if (!language) {
803
832
  throw new Error('You must specify the language of the script in the prompt template');
@@ -827,57 +856,25 @@
827
856
  if (description_1 === '') {
828
857
  description_1 = undefined;
829
858
  }
830
- var getParameterName = function (i) {
831
- var parameterName = postprocessingCommands.length <= i
832
- ? resultingParameterName
833
- : n12.normalizeTo_camelCase("".concat(resultingParameterName, " before ").concat(postprocessingCommands[i].functionName));
834
- var isParameterDefined = ptbJson.parameters.some(function (parameter) { return parameter.name === parameterName; });
835
- if (!isParameterDefined) {
836
- var parameterDescription = "*(".concat(n12.capitalize(section.title), " postprocessing ").concat(i + 1, "/").concat(postprocessingCommands.length, ")* {").concat(resultingParameterName, "} before `").concat(postprocessingCommands[i].functionName, "`");
837
- addParam({
838
- parameterName: parameterName,
839
- parameterDescription: parameterDescription,
840
- isInputParameter: false,
841
- // TODO:> isPrivate: true,
842
- });
843
- }
844
- return parameterName;
845
- };
859
+ if (Object.keys(expectAmount).length === 0) {
860
+ expectAmount = undefined;
861
+ }
862
+ if (Object.keys(postprocessing).length === 0) {
863
+ postprocessing = undefined;
864
+ }
846
865
  ptbJson.promptTemplates.push({
847
866
  name: n12.normalizeTo_PascalCase(section.title),
848
867
  title: section.title,
849
868
  description: description_1,
850
869
  executionType: executionType,
851
- expectations: expectations,
870
+ postprocessing: postprocessing,
871
+ expectAmount: expectAmount,
872
+ expectFormat: expectFormat,
852
873
  modelRequirements: templateModelRequirements,
853
874
  contentLanguage: executionType === 'SCRIPT' ? language : undefined,
854
875
  content: content,
855
- resultingParameterName: getParameterName(0),
876
+ resultingParameterName: resultingParameterName,
856
877
  });
857
- try {
858
- for (var _h = (e_4 = void 0, __values(postprocessingCommands.map(function (_a, i) {
859
- var functionName = _a.functionName;
860
- return [functionName, i];
861
- }))), _j = _h.next(); !_j.done; _j = _h.next()) {
862
- var _k = __read(_j.value, 2), functionName = _k[0], i = _k[1];
863
- ptbJson.promptTemplates.push({
864
- name: n12.normalizeTo_PascalCase(section.title + ' Postprocessing ' + i),
865
- title: "(".concat(i + 1, "/").concat(postprocessingCommands.length, ") ").concat(section.title, " postprocessing"),
866
- description: "Postprocessing of section ".concat(section.title, " finally with resulting parameter {").concat(resultingParameterName, "}"),
867
- executionType: 'SCRIPT',
868
- contentLanguage: 'javascript',
869
- content: "".concat(functionName, "(").concat(getParameterName(i), ")"),
870
- resultingParameterName: getParameterName(i + 1),
871
- });
872
- }
873
- }
874
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
875
- finally {
876
- try {
877
- if (_j && !_j.done && (_f = _h.return)) _f.call(_h);
878
- }
879
- finally { if (e_4) throw e_4.error; }
880
- }
881
878
  };
882
879
  try {
883
880
  for (var _c = __values(markdownStructure.sections), _d = _c.next(); !_d.done; _d = _c.next()) {
@@ -898,9 +895,7 @@
898
895
  /**
899
896
  * TODO: Report here line/column of error
900
897
  * TODO: Use spaceTrim more effectively
901
- * TODO: !!!! Parameter flags - isInput, isOutput, isInternal, isBeforePostprocessing, isBeforeFinal, canonicalName
902
- * TODO: !!!! Allow to have non-immutable parameters - suffix them with fooPrevious3 -> fooPrevious2 -> fooPrevious1 -> foo
903
- * This must work with other technial parameters
898
+ * TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
904
899
  */
905
900
 
906
901
  /**
@@ -1031,7 +1026,7 @@
1031
1026
  */
1032
1027
  PromptTemplatePipeline.fromJson = function (ptbkjson) {
1033
1028
  validatePromptTemplatePipelineJson(ptbkjson);
1034
- return new PromptTemplatePipeline(ptbkjson.ptbkUrl ? new URL(ptbkjson.ptbkUrl) : null, ptbkjson.title, ptbkjson.ptbkVersion, ptbkjson.ptbkVersion, Object.fromEntries(ptbkjson.parameters.map(function (parameter) { return [parameter.name, parameter]; })), ptbkjson.promptTemplates);
1029
+ return new PromptTemplatePipeline(ptbkjson.ptbkUrl ? new URL(ptbkjson.ptbkUrl) : null, ptbkjson.title, ptbkjson.ptbkVersion, ptbkjson.description || null, Object.fromEntries(ptbkjson.parameters.map(function (parameter) { return [parameter.name, parameter]; })), ptbkjson.promptTemplates);
1035
1030
  };
1036
1031
  Object.defineProperty(PromptTemplatePipeline.prototype, "entryPromptTemplate", {
1037
1032
  /**
@@ -1156,6 +1151,25 @@
1156
1151
  PAGES: countPages,
1157
1152
  };
1158
1153
 
1154
+ /**
1155
+ * Function isValidJsonString will tell you if the string is valid JSON or not
1156
+ */
1157
+ function isValidJsonString(value) {
1158
+ try {
1159
+ JSON.parse(value);
1160
+ return true;
1161
+ }
1162
+ catch (error) {
1163
+ if (!(error instanceof Error)) {
1164
+ throw error;
1165
+ }
1166
+ if (error.message.includes('Unexpected token')) {
1167
+ return false;
1168
+ }
1169
+ return false;
1170
+ }
1171
+ }
1172
+
1159
1173
  /**
1160
1174
  * All possible emoji chars like "🍆", "🍡", "🍤"...
1161
1175
  * Note: this will be needed to update annually - now updated at 2022-01-19
@@ -1272,6 +1286,23 @@
1272
1286
  return replacedTemplate;
1273
1287
  }
1274
1288
 
1289
+ /**
1290
+ * This error occurs when some expectation is not met in the execution of the pipeline
1291
+ */
1292
+ var ExpectError = /** @class */ (function (_super) {
1293
+ __extends(ExpectError, _super);
1294
+ function ExpectError(message) {
1295
+ var _this = _super.call(this, message) || this;
1296
+ _this.name = 'ExpectError';
1297
+ Object.setPrototypeOf(_this, ExpectError.prototype);
1298
+ return _this;
1299
+ }
1300
+ return ExpectError;
1301
+ }(Error));
1302
+ /**
1303
+ * TODO: [🧠] Should be this exported from the library
1304
+ */
1305
+
1275
1306
  /**
1276
1307
  * Creates executor function from prompt template pipeline and execution tools.
1277
1308
  *
@@ -1280,7 +1311,7 @@
1280
1311
  function createPtpExecutor(options) {
1281
1312
  var _this = this;
1282
1313
  var ptp = options.ptp, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
1283
- var _b = settings.maxNaturalExecutionAttempts, maxNaturalExecutionAttempts = _b === void 0 ? 3 : _b;
1314
+ var _b = settings.maxExecutionAttempts, maxExecutionAttempts = _b === void 0 ? 3 : _b;
1284
1315
  var ptpExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
1285
1316
  var parametersToPass, currentTemplate, executionReport, _loop_1, error_1;
1286
1317
  var _a;
@@ -1301,10 +1332,10 @@
1301
1332
  case 1:
1302
1333
  _b.trys.push([1, 5, , 6]);
1303
1334
  _loop_1 = function () {
1304
- var resultingParameter, name_1, title, prompt_1, chatThread, completionResult, result, resultString, naturalExecutionError, scriptExecutionErrors, isScriptExecutionSuccessful, _c, attempt, _d, _e, _f, _g, unit, _h, max, min, amount, error_2, _j, _k, scriptTools, error_3, e_1_1;
1305
- var e_2, _l, e_1, _m, _o;
1306
- return __generator(this, function (_p) {
1307
- switch (_p.label) {
1335
+ var resultingParameter, name_1, title, prompt_1, chatThread, completionResult, result, resultString, expectError, scriptExecutionErrors, attempt, _c, _d, _e, _f, scriptTools, error_2, e_1_1, _g, _h, functionName, _j, _k, scriptTools, error_3, e_2_1, e_3_1, _l, _m, _o, unit, _p, max, min, amount, error_4;
1336
+ var e_1, _q, e_3, _r, e_2, _s, e_4, _t, _u;
1337
+ return __generator(this, function (_v) {
1338
+ switch (_v.label) {
1308
1339
  case 0:
1309
1340
  resultingParameter = ptp.getResultingParameter(currentTemplate.name);
1310
1341
  name_1 = "ptp-executor-frame-".concat(currentTemplate.name);
@@ -1320,47 +1351,46 @@
1320
1351
  parameterValue: null,
1321
1352
  })];
1322
1353
  case 1:
1323
- _p.sent();
1324
- _p.label = 2;
1354
+ _v.sent();
1355
+ _v.label = 2;
1325
1356
  case 2:
1326
1357
  prompt_1 = void 0;
1327
1358
  chatThread = void 0;
1328
1359
  completionResult = void 0;
1329
1360
  result = null;
1330
1361
  resultString = null;
1331
- naturalExecutionError = null;
1332
- isScriptExecutionSuccessful = void 0;
1362
+ expectError = null;
1363
+ attempt = 0;
1364
+ _v.label = 3;
1365
+ case 3:
1366
+ if (!(attempt < maxExecutionAttempts)) return [3 /*break*/, 48];
1367
+ result = null;
1368
+ resultString = null;
1369
+ expectError = null;
1370
+ _v.label = 4;
1371
+ case 4:
1372
+ _v.trys.push([4, 44, 45, 46]);
1333
1373
  _c = currentTemplate.executionType;
1334
1374
  switch (_c) {
1335
- case 'SIMPLE_TEMPLATE': return [3 /*break*/, 3];
1336
- case 'PROMPT_TEMPLATE': return [3 /*break*/, 4];
1337
- case 'SCRIPT': return [3 /*break*/, 18];
1338
- case 'PROMPT_DIALOG': return [3 /*break*/, 29];
1375
+ case 'SIMPLE_TEMPLATE': return [3 /*break*/, 5];
1376
+ case 'PROMPT_TEMPLATE': return [3 /*break*/, 6];
1377
+ case 'SCRIPT': return [3 /*break*/, 13];
1378
+ case 'PROMPT_DIALOG': return [3 /*break*/, 24];
1339
1379
  }
1340
- return [3 /*break*/, 31];
1341
- case 3:
1380
+ return [3 /*break*/, 26];
1381
+ case 5:
1342
1382
  resultString = replaceParameters(currentTemplate.content, parametersToPass);
1343
- return [3 /*break*/, 32];
1344
- case 4:
1383
+ return [3 /*break*/, 27];
1384
+ case 6:
1345
1385
  prompt_1 = {
1346
1386
  title: currentTemplate.title,
1347
1387
  ptbkUrl: "".concat(ptp.ptbkUrl
1348
1388
  ? ptp.ptbkUrl.href
1349
- : 'anonymous' /* <- [🧠] !!!! How to deal with anonymous PTPs, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
1389
+ : 'anonymous' /* <- [🧠] !!! How to deal with anonymous PTPs, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
1350
1390
  parameters: parametersToPass,
1351
- content: replaceParameters(currentTemplate.content, parametersToPass),
1391
+ content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
1352
1392
  modelRequirements: currentTemplate.modelRequirements,
1353
1393
  };
1354
- attempt = 0;
1355
- _p.label = 5;
1356
- case 5:
1357
- if (!(attempt < maxNaturalExecutionAttempts)) return [3 /*break*/, 17];
1358
- result = null;
1359
- resultString = null;
1360
- naturalExecutionError = null;
1361
- _p.label = 6;
1362
- case 6:
1363
- _p.trys.push([6, 13, 14, 15]);
1364
1394
  _d = currentTemplate.modelRequirements.modelVariant;
1365
1395
  switch (_d) {
1366
1396
  case 'CHAT': return [3 /*break*/, 7];
@@ -1369,122 +1399,71 @@
1369
1399
  return [3 /*break*/, 11];
1370
1400
  case 7: return [4 /*yield*/, tools.natural.gptChat(prompt_1)];
1371
1401
  case 8:
1372
- chatThread = _p.sent();
1402
+ chatThread = _v.sent();
1373
1403
  // TODO: [🍬] Destroy chatThread
1374
1404
  result = chatThread;
1375
1405
  resultString = chatThread.content;
1376
1406
  return [3 /*break*/, 12];
1377
1407
  case 9: return [4 /*yield*/, tools.natural.gptComplete(prompt_1)];
1378
1408
  case 10:
1379
- completionResult = _p.sent();
1409
+ completionResult = _v.sent();
1380
1410
  result = completionResult;
1381
1411
  resultString = completionResult.content;
1382
1412
  return [3 /*break*/, 12];
1383
1413
  case 11: throw new Error("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
1384
- case 12:
1385
- try {
1386
- // TODO: !!!!!! Here should postprocessing happen
1387
- for (_e = (e_2 = void 0, __values(Object.entries(currentTemplate.expectations))), _f = _e.next(); !_f.done; _f = _e.next()) {
1388
- _g = __read(_f.value, 2), unit = _g[0], _h = _g[1], max = _h.max, min = _h.min;
1389
- amount = CountUtils[unit.toUpperCase()](resultString);
1390
- if (min && amount < min) {
1391
- throw new Error("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
1392
- } /* not else */
1393
- if (max && amount > max) {
1394
- throw new Error("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
1395
- }
1396
- }
1397
- }
1398
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
1399
- finally {
1400
- try {
1401
- if (_f && !_f.done && (_l = _e.return)) _l.call(_e);
1402
- }
1403
- finally { if (e_2) throw e_2.error; }
1404
- }
1405
- return [3 /*break*/, 15];
1414
+ case 12: return [3 /*break*/, 27];
1406
1415
  case 13:
1407
- error_2 = _p.sent();
1408
- if (!(error_2 instanceof Error)) {
1409
- throw error_2;
1410
- }
1411
- naturalExecutionError = error_2;
1412
- return [3 /*break*/, 15];
1413
- case 14:
1414
- executionReport.promptExecutions.push({
1415
- prompt: {
1416
- title: prompt_1.title,
1417
- content: prompt_1.content,
1418
- modelRequirements: prompt_1.modelRequirements,
1419
- // <- Note: Do want to pass ONLY wanted information to the report
1420
- },
1421
- result: result /* <- !!!!! Look what is exposed here and probbably also filter out */ ||
1422
- undefined,
1423
- error: naturalExecutionError || undefined,
1424
- });
1425
- return [7 /*endfinally*/];
1426
- case 15:
1427
- if (result !== null && naturalExecutionError === null) {
1428
- return [3 /*break*/, 32];
1429
- }
1430
- _p.label = 16;
1431
- case 16:
1432
- attempt++;
1433
- return [3 /*break*/, 5];
1434
- case 17: throw new Error(spaceTrim__default["default"](function (block) { return "\n Natural execution failed ".concat(settings.maxNaturalExecutionAttempts, "x\n\n ").concat(block((naturalExecutionError === null || naturalExecutionError === void 0 ? void 0 : naturalExecutionError.message) || ''), "\n "); }));
1435
- case 18:
1436
1416
  if (tools.script.length === 0) {
1437
1417
  throw new Error('No script execution tools are available');
1438
1418
  }
1439
1419
  if (!currentTemplate.contentLanguage) {
1440
1420
  throw new Error("Script language is not defined for prompt template \"".concat(currentTemplate.name, "\""));
1441
1421
  }
1422
+ // TODO: DRY [1]
1442
1423
  scriptExecutionErrors = [];
1443
- isScriptExecutionSuccessful = false;
1444
- _p.label = 19;
1445
- case 19:
1446
- _p.trys.push([19, 26, 27, 28]);
1447
- _j = (e_1 = void 0, __values(tools.script)), _k = _j.next();
1448
- _p.label = 20;
1449
- case 20:
1450
- if (!!_k.done) return [3 /*break*/, 25];
1451
- scriptTools = _k.value;
1452
- _p.label = 21;
1453
- case 21:
1454
- _p.trys.push([21, 23, , 24]);
1424
+ _v.label = 14;
1425
+ case 14:
1426
+ _v.trys.push([14, 21, 22, 23]);
1427
+ _e = (e_1 = void 0, __values(tools.script)), _f = _e.next();
1428
+ _v.label = 15;
1429
+ case 15:
1430
+ if (!!_f.done) return [3 /*break*/, 20];
1431
+ scriptTools = _f.value;
1432
+ _v.label = 16;
1433
+ case 16:
1434
+ _v.trys.push([16, 18, , 19]);
1455
1435
  return [4 /*yield*/, scriptTools.execute({
1456
1436
  scriptLanguage: currentTemplate.contentLanguage,
1457
1437
  script: currentTemplate.content,
1458
1438
  parameters: parametersToPass,
1459
1439
  })];
1460
- case 22:
1461
- resultString = _p.sent();
1462
- isScriptExecutionSuccessful = true;
1463
- return [3 /*break*/, 25];
1464
- case 23:
1465
- error_3 = _p.sent();
1466
- if (!(error_3 instanceof Error)) {
1467
- throw error_3;
1468
- }
1469
- scriptExecutionErrors.push(error_3);
1470
- return [3 /*break*/, 24];
1471
- case 24:
1472
- _k = _j.next();
1440
+ case 17:
1441
+ resultString = _v.sent();
1473
1442
  return [3 /*break*/, 20];
1474
- case 25: return [3 /*break*/, 28];
1475
- case 26:
1476
- e_1_1 = _p.sent();
1443
+ case 18:
1444
+ error_2 = _v.sent();
1445
+ if (!(error_2 instanceof Error)) {
1446
+ throw error_2;
1447
+ }
1448
+ scriptExecutionErrors.push(error_2);
1449
+ return [3 /*break*/, 19];
1450
+ case 19:
1451
+ _f = _e.next();
1452
+ return [3 /*break*/, 15];
1453
+ case 20: return [3 /*break*/, 23];
1454
+ case 21:
1455
+ e_1_1 = _v.sent();
1477
1456
  e_1 = { error: e_1_1 };
1478
- return [3 /*break*/, 28];
1479
- case 27:
1457
+ return [3 /*break*/, 23];
1458
+ case 22:
1480
1459
  try {
1481
- if (_k && !_k.done && (_m = _j.return)) _m.call(_j);
1460
+ if (_f && !_f.done && (_q = _e.return)) _q.call(_e);
1482
1461
  }
1483
1462
  finally { if (e_1) throw e_1.error; }
1484
1463
  return [7 /*endfinally*/];
1485
- case 28:
1486
- if (isScriptExecutionSuccessful) {
1487
- return [3 /*break*/, 32];
1464
+ case 23:
1465
+ if (resultString) {
1466
+ return [3 /*break*/, 27];
1488
1467
  }
1489
1468
  if (scriptExecutionErrors.length === 1) {
1490
1469
  throw scriptExecutionErrors[0];
@@ -1492,19 +1471,149 @@
1492
1471
  else {
1493
1472
  throw new Error(spaceTrim__default["default"](function (block) { return "\n Script execution failed ".concat(scriptExecutionErrors.length, " times\n\n ").concat(block(scriptExecutionErrors.map(function (error) { return '- ' + error.message; }).join('\n\n')), "\n "); }));
1494
1473
  }
1495
- case 29: return [4 /*yield*/, tools.userInterface.promptDialog({
1474
+ case 24: return [4 /*yield*/, tools.userInterface.promptDialog({
1496
1475
  prompt: replaceParameters(currentTemplate.description || '', parametersToPass),
1497
1476
  defaultValue: replaceParameters(currentTemplate.content, parametersToPass),
1498
1477
  // TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
1499
1478
  placeholder: undefined,
1500
1479
  })];
1501
- case 30:
1502
- resultString = _p.sent();
1503
- return [3 /*break*/, 32];
1504
- case 31:
1480
+ case 25:
1481
+ resultString = _v.sent();
1482
+ return [3 /*break*/, 27];
1483
+ case 26:
1505
1484
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1506
1485
  throw new Error("Unknown execution type \"".concat(currentTemplate.executionType, "\""));
1486
+ case 27:
1487
+ if (!currentTemplate.postprocessing) return [3 /*break*/, 43];
1488
+ _v.label = 28;
1489
+ case 28:
1490
+ _v.trys.push([28, 41, 42, 43]);
1491
+ _g = (e_3 = void 0, __values(currentTemplate.postprocessing)), _h = _g.next();
1492
+ _v.label = 29;
1493
+ case 29:
1494
+ if (!!_h.done) return [3 /*break*/, 40];
1495
+ functionName = _h.value;
1496
+ // TODO: DRY [1]
1497
+ scriptExecutionErrors = [];
1498
+ _v.label = 30;
1499
+ case 30:
1500
+ _v.trys.push([30, 37, 38, 39]);
1501
+ _j = (e_2 = void 0, __values(tools.script)), _k = _j.next();
1502
+ _v.label = 31;
1503
+ case 31:
1504
+ if (!!_k.done) return [3 /*break*/, 36];
1505
+ scriptTools = _k.value;
1506
+ _v.label = 32;
1507
1507
  case 32:
1508
+ _v.trys.push([32, 34, , 35]);
1509
+ return [4 /*yield*/, scriptTools.execute({
1510
+ scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
1511
+ script: "".concat(functionName, "(resultString)"),
1512
+ parameters: __assign(__assign({}, parametersToPass), { resultString: resultString }),
1513
+ })];
1514
+ case 33:
1515
+ resultString = _v.sent();
1516
+ return [3 /*break*/, 36];
1517
+ case 34:
1518
+ error_3 = _v.sent();
1519
+ if (!(error_3 instanceof Error)) {
1520
+ throw error_3;
1521
+ }
1522
+ scriptExecutionErrors.push(error_3);
1523
+ return [3 /*break*/, 35];
1524
+ case 35:
1525
+ _k = _j.next();
1526
+ return [3 /*break*/, 31];
1527
+ case 36: return [3 /*break*/, 39];
1528
+ case 37:
1529
+ e_2_1 = _v.sent();
1530
+ e_2 = { error: e_2_1 };
1531
+ return [3 /*break*/, 39];
1532
+ case 38:
1533
+ try {
1534
+ if (_k && !_k.done && (_s = _j.return)) _s.call(_j);
1535
+ }
1536
+ finally { if (e_2) throw e_2.error; }
1537
+ return [7 /*endfinally*/];
1538
+ case 39:
1539
+ _h = _g.next();
1540
+ return [3 /*break*/, 29];
1541
+ case 40: return [3 /*break*/, 43];
1542
+ case 41:
1543
+ e_3_1 = _v.sent();
1544
+ e_3 = { error: e_3_1 };
1545
+ return [3 /*break*/, 43];
1546
+ case 42:
1547
+ try {
1548
+ if (_h && !_h.done && (_r = _g.return)) _r.call(_g);
1549
+ }
1550
+ finally { if (e_3) throw e_3.error; }
1551
+ return [7 /*endfinally*/];
1552
+ case 43:
1553
+ if (currentTemplate.expectFormat) {
1554
+ if (currentTemplate.expectFormat === 'JSON') {
1555
+ if (!isValidJsonString(resultString)) {
1556
+ throw new ExpectError('Expected valid JSON string');
1557
+ }
1558
+ }
1559
+ }
1560
+ if (currentTemplate.expectAmount) {
1561
+ try {
1562
+ for (_l = (e_4 = void 0, __values(Object.entries(currentTemplate.expectAmount))), _m = _l.next(); !_m.done; _m = _l.next()) {
1563
+ _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], max = _p.max, min = _p.min;
1564
+ amount = CountUtils[unit.toUpperCase()](resultString);
1565
+ if (min && amount < min) {
1566
+ throw new ExpectError("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
1567
+ } /* not else */
1568
+ if (max && amount > max) {
1569
+ throw new ExpectError("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
1570
+ }
1571
+ }
1572
+ }
1573
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
1574
+ finally {
1575
+ try {
1576
+ if (_m && !_m.done && (_t = _l.return)) _t.call(_l);
1577
+ }
1578
+ finally { if (e_4) throw e_4.error; }
1579
+ }
1580
+ }
1581
+ return [3 /*break*/, 46];
1582
+ case 44:
1583
+ error_4 = _v.sent();
1584
+ if (!(error_4 instanceof ExpectError)) {
1585
+ throw error_4;
1586
+ }
1587
+ expectError = error_4;
1588
+ return [3 /*break*/, 46];
1589
+ case 45:
1590
+ if (currentTemplate.executionType === 'PROMPT_TEMPLATE' &&
1591
+ prompt_1
1592
+ // <- Note: [2] When some expected parameter is not defined, error will occur in replaceParameters
1593
+ // In that case we don’t want to make a report about it because it’s not a natural execution error
1594
+ ) {
1595
+ // TODO: [🧠] Maybe put other executionTypes into report
1596
+ executionReport.promptExecutions.push({
1597
+ prompt: {
1598
+ title: currentTemplate.title /* <- Note: If title in promptbook contains emojis, pass it innto report */,
1599
+ content: prompt_1.content,
1600
+ modelRequirements: prompt_1.modelRequirements,
1601
+ // <- Note: Do want to pass ONLY wanted information to the report
1602
+ },
1603
+ result: result || undefined,
1604
+ error: expectError || undefined,
1605
+ });
1606
+ }
1607
+ return [7 /*endfinally*/];
1608
+ case 46:
1609
+ if (result === null && attempt === maxExecutionAttempts) {
1610
+ throw new Error(spaceTrim__default["default"](function (block) { return "\n Natural execution failed ".concat(settings.maxExecutionAttempts, "x\n\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n "); }));
1611
+ }
1612
+ _v.label = 47;
1613
+ case 47:
1614
+ attempt++;
1615
+ return [3 /*break*/, 3];
1616
+ case 48:
1508
1617
  if (resultString === null) {
1509
1618
  // <- TODO: [🥨] Make some NeverShouldHappenError
1510
1619
  throw new Error('Something went wrong and prompt result is null');
@@ -1520,7 +1629,7 @@
1520
1629
  parameterValue: resultString,
1521
1630
  });
1522
1631
  }
1523
- parametersToPass = __assign(__assign({}, parametersToPass), (_o = {}, _o[resultingParameter.name] = resultString /* <- Note: Not need to detect parameter collision here because PromptTemplatePipeline checks logic consistency during construction */, _o));
1632
+ parametersToPass = __assign(__assign({}, parametersToPass), (_u = {}, _u[resultingParameter.name] = resultString /* <- Note: Not need to detect parameter collision here because PromptTemplatePipeline checks logic consistency during construction */, _u));
1524
1633
  currentTemplate = ptp.getFollowingPromptTemplate(currentTemplate.name);
1525
1634
  return [2 /*return*/];
1526
1635
  }