@promptbook/core 0.26.0 β†’ 0.27.0-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. package/esm/index.es.js +288 -192
  2. package/esm/index.es.js.map +1 -1
  3. package/esm/typings/conversion/parseCommand.test.d.ts +1 -2
  4. package/esm/typings/conversion/promptTemplatePipelineStringToJson.d.ts +1 -3
  5. package/esm/typings/execution/ExpectError.d.ts +10 -0
  6. package/esm/typings/execution/PtpExecutor.d.ts +1 -1
  7. package/esm/typings/execution/ScriptExecutionTools.d.ts +1 -1
  8. package/esm/typings/execution/createPtpExecutor.d.ts +2 -2
  9. package/esm/typings/execution/plugins/natural-execution-tools/openai/OpenAiExecutionTools.d.ts +1 -1
  10. package/esm/typings/types/Command.d.ts +2 -2
  11. package/esm/typings/types/Parameters.d.ts +2 -2
  12. package/esm/typings/types/PromptTemplatePipelineJson/PromptTemplateJson.d.ts +22 -14
  13. package/esm/typings/types/PromptTemplatePipelineJson/PromptTemplateParameterJson.d.ts +1 -1
  14. package/package.json +1 -1
  15. package/umd/index.umd.js +287 -191
  16. package/umd/index.umd.js.map +1 -1
  17. package/umd/typings/conversion/parseCommand.test.d.ts +1 -2
  18. package/umd/typings/conversion/promptTemplatePipelineStringToJson.d.ts +1 -3
  19. package/umd/typings/execution/ExpectError.d.ts +10 -0
  20. package/umd/typings/execution/PtpExecutor.d.ts +1 -1
  21. package/umd/typings/execution/ScriptExecutionTools.d.ts +1 -1
  22. package/umd/typings/execution/createPtpExecutor.d.ts +2 -2
  23. package/umd/typings/execution/plugins/natural-execution-tools/openai/OpenAiExecutionTools.d.ts +1 -1
  24. package/umd/typings/types/Command.d.ts +2 -2
  25. package/umd/typings/types/Parameters.d.ts +2 -2
  26. package/umd/typings/types/PromptTemplatePipelineJson/PromptTemplateJson.d.ts +22 -14
  27. package/umd/typings/types/PromptTemplatePipelineJson/PromptTemplateParameterJson.d.ts +1 -1
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) {
@@ -533,7 +549,7 @@
533
549
  };
534
550
  }
535
551
  else {
536
- throw new Error(spaceTrim__default["default"](function (block) { return "\n Unknown model key in command:\n\n - ".concat(listItem, "\n\n Supported model keys are:\n ").concat(block(['variant', 'name'].join(', ')), "\n\n Example:\n\n - MODEL VARIANT Chat\n - MODEL NAME gpt-4\n "); }));
552
+ throw new Error(spaceTrim__default["default"](function (block) { return "\n Unknown model key in command:\n\n - ".concat(listItem, "\n\n Supported model keys are:\n ").concat(block(['variant', 'name'].join(', ')), "\n\n Example:\n\n - MODEL VARIANT CHAT\n - MODEL NAME gpt-4\n "); }));
537
553
  }
538
554
  }
539
555
  else if (type.startsWith('PARAM') ||
@@ -643,7 +659,7 @@
643
659
  }
644
660
  }
645
661
  else {
646
- throw new Error(spaceTrim__default["default"]("\n Unknown command:\n\n - ".concat(listItem, "\n\n Supported commands are:\n - Execute\n - Model\n - Parameter\n - Input parameter\n - Output parameter\n - PTBK Version\n ")));
662
+ throw new Error(spaceTrim__default["default"]("\n Unknown command:\n\n - ".concat(listItem, "\n\n Supported commands are:\n - Execute\n - Model\n - Parameter\n - INPUT PARAMETER\n - OUTPUT PARAMETER\n - PTBK VERSION\n ")));
647
663
  }
648
664
  }
649
665
 
@@ -744,12 +760,12 @@
744
760
  finally { if (e_1) throw e_1.error; }
745
761
  }
746
762
  var _loop_1 = function (section) {
747
- var e_3, _e, e_4, _f;
763
+ var e_3, _e;
748
764
  // TODO: Parse prompt template description (the content out of the codeblock and lists)
749
765
  var templateModelRequirements = __assign({}, defaultModelRequirements);
750
- var postprocessingCommands = [];
751
766
  var listItems_3 = extractAllListItemsFromMarkdown(section.content);
752
767
  var executionType = 'PROMPT_TEMPLATE';
768
+ var postprocessing = [];
753
769
  var expectAmount = {};
754
770
  var expectFormat = undefined;
755
771
  var isExecutionTypeChanged = false;
@@ -773,7 +789,7 @@
773
789
  addParam(command);
774
790
  break;
775
791
  case 'POSTPROCESS':
776
- postprocessingCommands.push(command);
792
+ postprocessing.push(command.functionName);
777
793
  break;
778
794
  case 'EXPECT_AMOUNT':
779
795
  // eslint-disable-next-line no-case-declarations
@@ -810,7 +826,7 @@
810
826
  }
811
827
  finally { if (e_3) throw e_3.error; }
812
828
  }
813
- var _g = extractOneBlockFromMarkdown(section.content), language = _g.language, content = _g.content;
829
+ var _f = extractOneBlockFromMarkdown(section.content), language = _f.language, content = _f.content;
814
830
  if (executionType === 'SCRIPT') {
815
831
  if (!language) {
816
832
  throw new Error('You must specify the language of the script in the prompt template');
@@ -840,61 +856,25 @@
840
856
  if (description_1 === '') {
841
857
  description_1 = undefined;
842
858
  }
843
- var getParameterName = function (i) {
844
- var parameterName = postprocessingCommands.length <= i
845
- ? resultingParameterName
846
- : n12.normalizeTo_camelCase("".concat(resultingParameterName, " before ").concat(postprocessingCommands[i].functionName));
847
- var isParameterDefined = ptbJson.parameters.some(function (parameter) { return parameter.name === parameterName; });
848
- if (!isParameterDefined) {
849
- var parameterDescription = "*(".concat(n12.capitalize(section.title), " postprocessing ").concat(i + 1, "/").concat(postprocessingCommands.length, ")* {").concat(resultingParameterName, "} before `").concat(postprocessingCommands[i].functionName, "`");
850
- addParam({
851
- parameterName: parameterName,
852
- parameterDescription: parameterDescription,
853
- isInputParameter: false,
854
- // TODO:> isPrivate: true,
855
- });
856
- }
857
- return parameterName;
858
- };
859
859
  if (Object.keys(expectAmount).length === 0) {
860
860
  expectAmount = undefined;
861
861
  }
862
+ if (Object.keys(postprocessing).length === 0) {
863
+ postprocessing = undefined;
864
+ }
862
865
  ptbJson.promptTemplates.push({
863
866
  name: n12.normalizeTo_PascalCase(section.title),
864
867
  title: section.title,
865
868
  description: description_1,
866
869
  executionType: executionType,
870
+ postprocessing: postprocessing,
867
871
  expectAmount: expectAmount,
868
872
  expectFormat: expectFormat,
869
873
  modelRequirements: templateModelRequirements,
870
874
  contentLanguage: executionType === 'SCRIPT' ? language : undefined,
871
875
  content: content,
872
- resultingParameterName: getParameterName(0),
876
+ resultingParameterName: resultingParameterName,
873
877
  });
874
- try {
875
- for (var _h = (e_4 = void 0, __values(postprocessingCommands.map(function (_a, i) {
876
- var functionName = _a.functionName;
877
- return [functionName, i];
878
- }))), _j = _h.next(); !_j.done; _j = _h.next()) {
879
- var _k = __read(_j.value, 2), functionName = _k[0], i = _k[1];
880
- ptbJson.promptTemplates.push({
881
- name: n12.normalizeTo_PascalCase(section.title + ' Postprocessing ' + i),
882
- title: "(".concat(i + 1, "/").concat(postprocessingCommands.length, ") ").concat(section.title, " postprocessing"),
883
- description: "Postprocessing of section ".concat(section.title, " finally with resulting parameter {").concat(resultingParameterName, "}"),
884
- executionType: 'SCRIPT',
885
- contentLanguage: 'javascript',
886
- content: "".concat(functionName, "(").concat(getParameterName(i), ")"),
887
- resultingParameterName: getParameterName(i + 1),
888
- });
889
- }
890
- }
891
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
892
- finally {
893
- try {
894
- if (_j && !_j.done && (_f = _h.return)) _f.call(_h);
895
- }
896
- finally { if (e_4) throw e_4.error; }
897
- }
898
878
  };
899
879
  try {
900
880
  for (var _c = __values(markdownStructure.sections), _d = _c.next(); !_d.done; _d = _c.next()) {
@@ -915,9 +895,7 @@
915
895
  /**
916
896
  * TODO: Report here line/column of error
917
897
  * TODO: Use spaceTrim more effectively
918
- * TODO: !!!! Parameter flags - isInput, isOutput, isInternal, isBeforePostprocessing, isBeforeFinal, canonicalName
919
- * TODO: !!!! Allow to have non-immutable parameters - suffix them with fooPrevious3 -> fooPrevious2 -> fooPrevious1 -> foo
920
- * This must work with other technial parameters
898
+ * TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
921
899
  */
922
900
 
923
901
  /**
@@ -1048,7 +1026,7 @@
1048
1026
  */
1049
1027
  PromptTemplatePipeline.fromJson = function (ptbkjson) {
1050
1028
  validatePromptTemplatePipelineJson(ptbkjson);
1051
- 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);
1052
1030
  };
1053
1031
  Object.defineProperty(PromptTemplatePipeline.prototype, "entryPromptTemplate", {
1054
1032
  /**
@@ -1308,6 +1286,23 @@
1308
1286
  return replacedTemplate;
1309
1287
  }
1310
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
+
1311
1306
  /**
1312
1307
  * Creates executor function from prompt template pipeline and execution tools.
1313
1308
  *
@@ -1316,7 +1311,7 @@
1316
1311
  function createPtpExecutor(options) {
1317
1312
  var _this = this;
1318
1313
  var ptp = options.ptp, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
1319
- var _b = settings.maxNaturalExecutionAttempts, maxNaturalExecutionAttempts = _b === void 0 ? 3 : _b;
1314
+ var _b = settings.maxExecutionAttempts, maxExecutionAttempts = _b === void 0 ? 3 : _b;
1320
1315
  var ptpExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
1321
1316
  var parametersToPass, currentTemplate, executionReport, _loop_1, error_1;
1322
1317
  var _a;
@@ -1337,14 +1332,21 @@
1337
1332
  case 1:
1338
1333
  _b.trys.push([1, 5, , 6]);
1339
1334
  _loop_1 = function () {
1340
- 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;
1341
- var e_2, _l, e_1, _m, _o;
1342
- return __generator(this, function (_p) {
1343
- 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) {
1344
1339
  case 0:
1345
1340
  resultingParameter = ptp.getResultingParameter(currentTemplate.name);
1346
1341
  name_1 = "ptp-executor-frame-".concat(currentTemplate.name);
1347
1342
  title = removeEmojis(removeMarkdownFormatting(currentTemplate.title));
1343
+ console.info('πŸ›¬ currentTemplate', {
1344
+ currentTemplate: currentTemplate,
1345
+ resultingParameter: resultingParameter,
1346
+ name: name_1,
1347
+ title: title,
1348
+ parametersToPass: __assign({}, parametersToPass),
1349
+ });
1348
1350
  if (!onProgress) return [3 /*break*/, 2];
1349
1351
  return [4 /*yield*/, onProgress({
1350
1352
  name: name_1,
@@ -1356,180 +1358,129 @@
1356
1358
  parameterValue: null,
1357
1359
  })];
1358
1360
  case 1:
1359
- _p.sent();
1360
- _p.label = 2;
1361
+ _v.sent();
1362
+ _v.label = 2;
1361
1363
  case 2:
1362
1364
  prompt_1 = void 0;
1363
1365
  chatThread = void 0;
1364
1366
  completionResult = void 0;
1365
1367
  result = null;
1366
1368
  resultString = null;
1367
- naturalExecutionError = null;
1368
- isScriptExecutionSuccessful = void 0;
1369
+ expectError = null;
1370
+ attempt = 0;
1371
+ _v.label = 3;
1372
+ case 3:
1373
+ if (!(attempt < maxExecutionAttempts)) return [3 /*break*/, 48];
1374
+ console.info('πŸ›¬ currentTemplate attempt', attempt);
1375
+ result = null;
1376
+ resultString = null;
1377
+ expectError = null;
1378
+ _v.label = 4;
1379
+ case 4:
1380
+ _v.trys.push([4, 44, 45, 46]);
1369
1381
  _c = currentTemplate.executionType;
1370
1382
  switch (_c) {
1371
- case 'SIMPLE_TEMPLATE': return [3 /*break*/, 3];
1372
- case 'PROMPT_TEMPLATE': return [3 /*break*/, 4];
1373
- case 'SCRIPT': return [3 /*break*/, 18];
1374
- case 'PROMPT_DIALOG': return [3 /*break*/, 29];
1383
+ case 'SIMPLE_TEMPLATE': return [3 /*break*/, 5];
1384
+ case 'PROMPT_TEMPLATE': return [3 /*break*/, 6];
1385
+ case 'SCRIPT': return [3 /*break*/, 13];
1386
+ case 'PROMPT_DIALOG': return [3 /*break*/, 24];
1375
1387
  }
1376
- return [3 /*break*/, 31];
1377
- case 3:
1388
+ return [3 /*break*/, 26];
1389
+ case 5:
1390
+ console.info('πŸ›¬ currentTemplate attempt SIMPLE_TEMPLATE');
1378
1391
  resultString = replaceParameters(currentTemplate.content, parametersToPass);
1379
- return [3 /*break*/, 32];
1380
- case 4:
1392
+ return [3 /*break*/, 27];
1393
+ case 6:
1394
+ console.info('πŸ›¬ currentTemplate attempt PROMPT_TEMPLATE');
1381
1395
  prompt_1 = {
1382
1396
  title: currentTemplate.title,
1383
1397
  ptbkUrl: "".concat(ptp.ptbkUrl
1384
1398
  ? ptp.ptbkUrl.href
1385
- : 'anonymous' /* <- [🧠] !!!! How to deal with anonymous PTPs, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
1399
+ : 'anonymous' /* <- [🧠] !!! How to deal with anonymous PTPs, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
1386
1400
  parameters: parametersToPass,
1387
- content: replaceParameters(currentTemplate.content, parametersToPass),
1401
+ content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
1388
1402
  modelRequirements: currentTemplate.modelRequirements,
1389
1403
  };
1390
- attempt = 0;
1391
- _p.label = 5;
1392
- case 5:
1393
- if (!(attempt < maxNaturalExecutionAttempts)) return [3 /*break*/, 17];
1394
- result = null;
1395
- resultString = null;
1396
- naturalExecutionError = null;
1397
- _p.label = 6;
1398
- case 6:
1399
- _p.trys.push([6, 13, 14, 15]);
1400
1404
  _d = currentTemplate.modelRequirements.modelVariant;
1401
1405
  switch (_d) {
1402
1406
  case 'CHAT': return [3 /*break*/, 7];
1403
1407
  case 'COMPLETION': return [3 /*break*/, 9];
1404
1408
  }
1405
1409
  return [3 /*break*/, 11];
1406
- case 7: return [4 /*yield*/, tools.natural.gptChat(prompt_1)];
1410
+ case 7:
1411
+ console.info('πŸ›¬ currentTemplate attempt PROMPT_TEMPLATE CHAT');
1412
+ return [4 /*yield*/, tools.natural.gptChat(prompt_1)];
1407
1413
  case 8:
1408
- chatThread = _p.sent();
1414
+ chatThread = _v.sent();
1409
1415
  // TODO: [🍬] Destroy chatThread
1410
1416
  result = chatThread;
1411
1417
  resultString = chatThread.content;
1412
1418
  return [3 /*break*/, 12];
1413
- case 9: return [4 /*yield*/, tools.natural.gptComplete(prompt_1)];
1419
+ case 9:
1420
+ console.info('πŸ›¬ currentTemplate attempt PROMPT_TEMPLATE COMPLETION');
1421
+ return [4 /*yield*/, tools.natural.gptComplete(prompt_1)];
1414
1422
  case 10:
1415
- completionResult = _p.sent();
1423
+ completionResult = _v.sent();
1416
1424
  result = completionResult;
1417
1425
  resultString = completionResult.content;
1418
1426
  return [3 /*break*/, 12];
1419
- case 11: throw new Error("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
1420
- case 12:
1421
- // TODO: !!!!!! Here should postprocessing happen
1422
- if (currentTemplate.expectFormat) {
1423
- if (currentTemplate.expectFormat === 'JSON') {
1424
- if (!isValidJsonString(resultString)) {
1425
- throw new Error('Expected valid JSON string');
1426
- }
1427
- }
1428
- }
1429
- if (currentTemplate.expectAmount) {
1430
- try {
1431
- for (_e = (e_2 = void 0, __values(Object.entries(currentTemplate.expectAmount))), _f = _e.next(); !_f.done; _f = _e.next()) {
1432
- _g = __read(_f.value, 2), unit = _g[0], _h = _g[1], max = _h.max, min = _h.min;
1433
- amount = CountUtils[unit.toUpperCase()](resultString);
1434
- if (min && amount < min) {
1435
- throw new Error("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
1436
- } /* not else */
1437
- if (max && amount > max) {
1438
- throw new Error("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
1439
- }
1440
- }
1441
- }
1442
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
1443
- finally {
1444
- try {
1445
- if (_f && !_f.done && (_l = _e.return)) _l.call(_e);
1446
- }
1447
- finally { if (e_2) throw e_2.error; }
1448
- }
1449
- }
1450
- return [3 /*break*/, 15];
1427
+ case 11:
1428
+ console.info('πŸ›¬ currentTemplate attempt PROMPT_TEMPLATE UNKNOWN');
1429
+ throw new Error("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
1430
+ case 12: return [3 /*break*/, 27];
1451
1431
  case 13:
1452
- error_2 = _p.sent();
1453
- if (!(error_2 instanceof Error)) {
1454
- throw error_2;
1455
- }
1456
- naturalExecutionError = error_2;
1457
- return [3 /*break*/, 15];
1458
- case 14:
1459
- executionReport.promptExecutions.push({
1460
- prompt: {
1461
- title: prompt_1.title,
1462
- content: prompt_1.content,
1463
- modelRequirements: prompt_1.modelRequirements,
1464
- // <- Note: Do want to pass ONLY wanted information to the report
1465
- },
1466
- result: result /* <- !!!!! Look what is exposed here and probbably also filter out */ ||
1467
- undefined,
1468
- error: naturalExecutionError || undefined,
1469
- });
1470
- return [7 /*endfinally*/];
1471
- case 15:
1472
- if (result !== null && naturalExecutionError === null) {
1473
- return [3 /*break*/, 32];
1474
- }
1475
- _p.label = 16;
1476
- case 16:
1477
- attempt++;
1478
- return [3 /*break*/, 5];
1479
- 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 "); }));
1480
- case 18:
1432
+ console.info('πŸ›¬ currentTemplate attempt SCRIPT');
1481
1433
  if (tools.script.length === 0) {
1482
1434
  throw new Error('No script execution tools are available');
1483
1435
  }
1484
1436
  if (!currentTemplate.contentLanguage) {
1485
1437
  throw new Error("Script language is not defined for prompt template \"".concat(currentTemplate.name, "\""));
1486
1438
  }
1439
+ // TODO: DRY [1]
1487
1440
  scriptExecutionErrors = [];
1488
- isScriptExecutionSuccessful = false;
1489
- _p.label = 19;
1490
- case 19:
1491
- _p.trys.push([19, 26, 27, 28]);
1492
- _j = (e_1 = void 0, __values(tools.script)), _k = _j.next();
1493
- _p.label = 20;
1494
- case 20:
1495
- if (!!_k.done) return [3 /*break*/, 25];
1496
- scriptTools = _k.value;
1497
- _p.label = 21;
1498
- case 21:
1499
- _p.trys.push([21, 23, , 24]);
1441
+ _v.label = 14;
1442
+ case 14:
1443
+ _v.trys.push([14, 21, 22, 23]);
1444
+ _e = (e_1 = void 0, __values(tools.script)), _f = _e.next();
1445
+ _v.label = 15;
1446
+ case 15:
1447
+ if (!!_f.done) return [3 /*break*/, 20];
1448
+ scriptTools = _f.value;
1449
+ _v.label = 16;
1450
+ case 16:
1451
+ _v.trys.push([16, 18, , 19]);
1500
1452
  return [4 /*yield*/, scriptTools.execute({
1501
1453
  scriptLanguage: currentTemplate.contentLanguage,
1502
1454
  script: currentTemplate.content,
1503
1455
  parameters: parametersToPass,
1504
1456
  })];
1505
- case 22:
1506
- resultString = _p.sent();
1507
- isScriptExecutionSuccessful = true;
1508
- return [3 /*break*/, 25];
1509
- case 23:
1510
- error_3 = _p.sent();
1511
- if (!(error_3 instanceof Error)) {
1512
- throw error_3;
1513
- }
1514
- scriptExecutionErrors.push(error_3);
1515
- return [3 /*break*/, 24];
1516
- case 24:
1517
- _k = _j.next();
1457
+ case 17:
1458
+ resultString = _v.sent();
1518
1459
  return [3 /*break*/, 20];
1519
- case 25: return [3 /*break*/, 28];
1520
- case 26:
1521
- e_1_1 = _p.sent();
1460
+ case 18:
1461
+ error_2 = _v.sent();
1462
+ if (!(error_2 instanceof Error)) {
1463
+ throw error_2;
1464
+ }
1465
+ scriptExecutionErrors.push(error_2);
1466
+ return [3 /*break*/, 19];
1467
+ case 19:
1468
+ _f = _e.next();
1469
+ return [3 /*break*/, 15];
1470
+ case 20: return [3 /*break*/, 23];
1471
+ case 21:
1472
+ e_1_1 = _v.sent();
1522
1473
  e_1 = { error: e_1_1 };
1523
- return [3 /*break*/, 28];
1524
- case 27:
1474
+ return [3 /*break*/, 23];
1475
+ case 22:
1525
1476
  try {
1526
- if (_k && !_k.done && (_m = _j.return)) _m.call(_j);
1477
+ if (_f && !_f.done && (_q = _e.return)) _q.call(_e);
1527
1478
  }
1528
1479
  finally { if (e_1) throw e_1.error; }
1529
1480
  return [7 /*endfinally*/];
1530
- case 28:
1531
- if (isScriptExecutionSuccessful) {
1532
- return [3 /*break*/, 32];
1481
+ case 23:
1482
+ if (resultString) {
1483
+ return [3 /*break*/, 27];
1533
1484
  }
1534
1485
  if (scriptExecutionErrors.length === 1) {
1535
1486
  throw scriptExecutionErrors[0];
@@ -1537,19 +1488,164 @@
1537
1488
  else {
1538
1489
  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 "); }));
1539
1490
  }
1540
- case 29: return [4 /*yield*/, tools.userInterface.promptDialog({
1541
- prompt: replaceParameters(currentTemplate.description || '', parametersToPass),
1542
- defaultValue: replaceParameters(currentTemplate.content, parametersToPass),
1543
- // TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
1544
- placeholder: undefined,
1545
- })];
1491
+ case 24:
1492
+ console.info('πŸ›¬ currentTemplate attempt PROMPT_DIALOG');
1493
+ return [4 /*yield*/, tools.userInterface.promptDialog({
1494
+ prompt: replaceParameters(currentTemplate.description || '', parametersToPass),
1495
+ defaultValue: replaceParameters(currentTemplate.content, parametersToPass),
1496
+ // TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
1497
+ placeholder: undefined,
1498
+ })];
1499
+ case 25:
1500
+ resultString = _v.sent();
1501
+ return [3 /*break*/, 27];
1502
+ case 26:
1503
+ console.info('πŸ›¬ currentTemplate attempt UNKNOWN');
1504
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1505
+ throw new Error("Unknown execution type \"".concat(currentTemplate.executionType, "\""));
1506
+ case 27:
1507
+ if (!currentTemplate.postprocessing) return [3 /*break*/, 43];
1508
+ console.info('πŸ›¬ currentTemplate attempt postprocessing');
1509
+ _v.label = 28;
1510
+ case 28:
1511
+ _v.trys.push([28, 41, 42, 43]);
1512
+ _g = (e_3 = void 0, __values(currentTemplate.postprocessing)), _h = _g.next();
1513
+ _v.label = 29;
1514
+ case 29:
1515
+ if (!!_h.done) return [3 /*break*/, 40];
1516
+ functionName = _h.value;
1517
+ console.info('πŸ›¬ currentTemplate attempt postprocessing', functionName);
1518
+ // TODO: DRY [1]
1519
+ scriptExecutionErrors = [];
1520
+ _v.label = 30;
1546
1521
  case 30:
1547
- resultString = _p.sent();
1548
- return [3 /*break*/, 32];
1549
- case 31:
1550
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1551
- throw new Error("Unknown execution type \"".concat(currentTemplate.executionType, "\""));
1522
+ _v.trys.push([30, 37, 38, 39]);
1523
+ _j = (e_2 = void 0, __values(tools.script)), _k = _j.next();
1524
+ _v.label = 31;
1525
+ case 31:
1526
+ if (!!_k.done) return [3 /*break*/, 36];
1527
+ scriptTools = _k.value;
1528
+ _v.label = 32;
1552
1529
  case 32:
1530
+ _v.trys.push([32, 34, , 35]);
1531
+ console.info('πŸ›¬ currentTemplate attempt postprocessing', functionName, {
1532
+ scriptTools: scriptTools,
1533
+ });
1534
+ return [4 /*yield*/, scriptTools.execute({
1535
+ scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
1536
+ script: "".concat(functionName, "(resultString)"),
1537
+ parameters: __assign(__assign({}, parametersToPass), { resultString: resultString }),
1538
+ })];
1539
+ case 33:
1540
+ resultString = _v.sent();
1541
+ return [3 /*break*/, 36];
1542
+ case 34:
1543
+ error_3 = _v.sent();
1544
+ if (!(error_3 instanceof Error)) {
1545
+ throw error_3;
1546
+ }
1547
+ scriptExecutionErrors.push(error_3);
1548
+ return [3 /*break*/, 35];
1549
+ case 35:
1550
+ _k = _j.next();
1551
+ return [3 /*break*/, 31];
1552
+ case 36: return [3 /*break*/, 39];
1553
+ case 37:
1554
+ e_2_1 = _v.sent();
1555
+ e_2 = { error: e_2_1 };
1556
+ return [3 /*break*/, 39];
1557
+ case 38:
1558
+ try {
1559
+ if (_k && !_k.done && (_s = _j.return)) _s.call(_j);
1560
+ }
1561
+ finally { if (e_2) throw e_2.error; }
1562
+ return [7 /*endfinally*/];
1563
+ case 39:
1564
+ _h = _g.next();
1565
+ return [3 /*break*/, 29];
1566
+ case 40: return [3 /*break*/, 43];
1567
+ case 41:
1568
+ e_3_1 = _v.sent();
1569
+ e_3 = { error: e_3_1 };
1570
+ return [3 /*break*/, 43];
1571
+ case 42:
1572
+ try {
1573
+ if (_h && !_h.done && (_r = _g.return)) _r.call(_g);
1574
+ }
1575
+ finally { if (e_3) throw e_3.error; }
1576
+ return [7 /*endfinally*/];
1577
+ case 43:
1578
+ if (currentTemplate.expectFormat) {
1579
+ console.info('πŸ›¬ currentTemplate attempt expectFormat');
1580
+ if (currentTemplate.expectFormat === 'JSON') {
1581
+ if (!isValidJsonString(resultString)) {
1582
+ throw new ExpectError('Expected valid JSON string');
1583
+ }
1584
+ }
1585
+ }
1586
+ if (currentTemplate.expectAmount) {
1587
+ console.info('πŸ›¬ currentTemplate attempt expectAmount');
1588
+ try {
1589
+ for (_l = (e_4 = void 0, __values(Object.entries(currentTemplate.expectAmount))), _m = _l.next(); !_m.done; _m = _l.next()) {
1590
+ _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], max = _p.max, min = _p.min;
1591
+ amount = CountUtils[unit.toUpperCase()](resultString);
1592
+ console.info('πŸ›¬ currentTemplate attempt expectAmount', { unit: unit, max: max, min: min, amount: amount });
1593
+ if (min && amount < min) {
1594
+ throw new ExpectError("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
1595
+ } /* not else */
1596
+ if (max && amount > max) {
1597
+ throw new ExpectError("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
1598
+ }
1599
+ }
1600
+ }
1601
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
1602
+ finally {
1603
+ try {
1604
+ if (_m && !_m.done && (_t = _l.return)) _t.call(_l);
1605
+ }
1606
+ finally { if (e_4) throw e_4.error; }
1607
+ }
1608
+ }
1609
+ return [3 /*break*/, 46];
1610
+ case 44:
1611
+ error_4 = _v.sent();
1612
+ console.info('πŸ›¬ currentTemplate attempt catch', { error: error_4 });
1613
+ if (!(error_4 instanceof ExpectError)) {
1614
+ throw error_4;
1615
+ }
1616
+ expectError = error_4;
1617
+ return [3 /*break*/, 46];
1618
+ case 45:
1619
+ console.info('πŸ›¬ currentTemplate attempt finally', { currentTemplate: currentTemplate, result: result, expectError: expectError });
1620
+ if (currentTemplate.executionType === 'PROMPT_TEMPLATE' &&
1621
+ prompt_1
1622
+ // <- Note: [2] When some expected parameter is not defined, error will occur in replaceParameters
1623
+ // In that case we don’t want to make a report about it because it’s not a natural execution error
1624
+ ) {
1625
+ console.info('πŸ›¬ currentTemplate attempt finally PROMPT_TEMPLATE', { prompt: prompt_1 });
1626
+ // TODO: [🧠] Maybe put other executionTypes into report
1627
+ executionReport.promptExecutions.push({
1628
+ prompt: {
1629
+ title: currentTemplate.title /* <- Note: If title in promptbook contains emojis, pass it innto report */,
1630
+ content: prompt_1.content,
1631
+ modelRequirements: prompt_1.modelRequirements,
1632
+ // <- Note: Do want to pass ONLY wanted information to the report
1633
+ },
1634
+ result: result || undefined,
1635
+ error: expectError || undefined,
1636
+ });
1637
+ }
1638
+ return [7 /*endfinally*/];
1639
+ case 46:
1640
+ if (result === null && attempt === maxExecutionAttempts) {
1641
+ console.info('πŸ›¬ currentTemplate attempt fail', { attempt: attempt, maxExecutionAttempts: maxExecutionAttempts });
1642
+ 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 "); }));
1643
+ }
1644
+ _v.label = 47;
1645
+ case 47:
1646
+ attempt++;
1647
+ return [3 /*break*/, 3];
1648
+ case 48:
1553
1649
  if (resultString === null) {
1554
1650
  // <- TODO: [πŸ₯¨] Make some NeverShouldHappenError
1555
1651
  throw new Error('Something went wrong and prompt result is null');
@@ -1565,7 +1661,7 @@
1565
1661
  parameterValue: resultString,
1566
1662
  });
1567
1663
  }
1568
- 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));
1664
+ 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));
1569
1665
  currentTemplate = ptp.getFollowingPromptTemplate(currentTemplate.name);
1570
1666
  return [2 /*return*/];
1571
1667
  }