@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/esm/index.es.js CHANGED
@@ -1,4 +1,4 @@
1
- import { capitalize, normalizeTo_SCREAMING_CASE, normalizeTo_PascalCase, normalizeTo_camelCase, removeDiacritics } from 'n12';
1
+ import { capitalize, normalizeTo_SCREAMING_CASE, normalizeTo_PascalCase, removeDiacritics } from 'n12';
2
2
  import spaceTrim from 'spacetrim';
3
3
 
4
4
  /*! *****************************************************************************
@@ -15,6 +15,22 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
15
15
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16
16
  PERFORMANCE OF THIS SOFTWARE.
17
17
  ***************************************************************************** */
18
+ /* global Reflect, Promise */
19
+
20
+ var extendStatics = function(d, b) {
21
+ extendStatics = Object.setPrototypeOf ||
22
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
23
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
24
+ return extendStatics(d, b);
25
+ };
26
+
27
+ function __extends(d, b) {
28
+ if (typeof b !== "function" && b !== null)
29
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
30
+ extendStatics(d, b);
31
+ function __() { this.constructor = d; }
32
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
33
+ }
18
34
 
19
35
  var __assign = function() {
20
36
  __assign = Object.assign || function __assign(t) {
@@ -526,7 +542,7 @@ function parseCommand(listItem) {
526
542
  };
527
543
  }
528
544
  else {
529
- throw new Error(spaceTrim(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 "); }));
545
+ throw new Error(spaceTrim(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 "); }));
530
546
  }
531
547
  }
532
548
  else if (type.startsWith('PARAM') ||
@@ -636,7 +652,7 @@ function parseCommand(listItem) {
636
652
  }
637
653
  }
638
654
  else {
639
- throw new Error(spaceTrim("\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 ")));
655
+ throw new Error(spaceTrim("\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 ")));
640
656
  }
641
657
  }
642
658
 
@@ -737,12 +753,12 @@ function promptTemplatePipelineStringToJson(promptTemplatePipelineString) {
737
753
  finally { if (e_1) throw e_1.error; }
738
754
  }
739
755
  var _loop_1 = function (section) {
740
- var e_3, _e, e_4, _f;
756
+ var e_3, _e;
741
757
  // TODO: Parse prompt template description (the content out of the codeblock and lists)
742
758
  var templateModelRequirements = __assign({}, defaultModelRequirements);
743
- var postprocessingCommands = [];
744
759
  var listItems_3 = extractAllListItemsFromMarkdown(section.content);
745
760
  var executionType = 'PROMPT_TEMPLATE';
761
+ var postprocessing = [];
746
762
  var expectAmount = {};
747
763
  var expectFormat = undefined;
748
764
  var isExecutionTypeChanged = false;
@@ -766,7 +782,7 @@ function promptTemplatePipelineStringToJson(promptTemplatePipelineString) {
766
782
  addParam(command);
767
783
  break;
768
784
  case 'POSTPROCESS':
769
- postprocessingCommands.push(command);
785
+ postprocessing.push(command.functionName);
770
786
  break;
771
787
  case 'EXPECT_AMOUNT':
772
788
  // eslint-disable-next-line no-case-declarations
@@ -803,7 +819,7 @@ function promptTemplatePipelineStringToJson(promptTemplatePipelineString) {
803
819
  }
804
820
  finally { if (e_3) throw e_3.error; }
805
821
  }
806
- var _g = extractOneBlockFromMarkdown(section.content), language = _g.language, content = _g.content;
822
+ var _f = extractOneBlockFromMarkdown(section.content), language = _f.language, content = _f.content;
807
823
  if (executionType === 'SCRIPT') {
808
824
  if (!language) {
809
825
  throw new Error('You must specify the language of the script in the prompt template');
@@ -833,61 +849,25 @@ function promptTemplatePipelineStringToJson(promptTemplatePipelineString) {
833
849
  if (description_1 === '') {
834
850
  description_1 = undefined;
835
851
  }
836
- var getParameterName = function (i) {
837
- var parameterName = postprocessingCommands.length <= i
838
- ? resultingParameterName
839
- : normalizeTo_camelCase("".concat(resultingParameterName, " before ").concat(postprocessingCommands[i].functionName));
840
- var isParameterDefined = ptbJson.parameters.some(function (parameter) { return parameter.name === parameterName; });
841
- if (!isParameterDefined) {
842
- var parameterDescription = "*(".concat(capitalize(section.title), " postprocessing ").concat(i + 1, "/").concat(postprocessingCommands.length, ")* {").concat(resultingParameterName, "} before `").concat(postprocessingCommands[i].functionName, "`");
843
- addParam({
844
- parameterName: parameterName,
845
- parameterDescription: parameterDescription,
846
- isInputParameter: false,
847
- // TODO:> isPrivate: true,
848
- });
849
- }
850
- return parameterName;
851
- };
852
852
  if (Object.keys(expectAmount).length === 0) {
853
853
  expectAmount = undefined;
854
854
  }
855
+ if (Object.keys(postprocessing).length === 0) {
856
+ postprocessing = undefined;
857
+ }
855
858
  ptbJson.promptTemplates.push({
856
859
  name: normalizeTo_PascalCase(section.title),
857
860
  title: section.title,
858
861
  description: description_1,
859
862
  executionType: executionType,
863
+ postprocessing: postprocessing,
860
864
  expectAmount: expectAmount,
861
865
  expectFormat: expectFormat,
862
866
  modelRequirements: templateModelRequirements,
863
867
  contentLanguage: executionType === 'SCRIPT' ? language : undefined,
864
868
  content: content,
865
- resultingParameterName: getParameterName(0),
869
+ resultingParameterName: resultingParameterName,
866
870
  });
867
- try {
868
- for (var _h = (e_4 = void 0, __values(postprocessingCommands.map(function (_a, i) {
869
- var functionName = _a.functionName;
870
- return [functionName, i];
871
- }))), _j = _h.next(); !_j.done; _j = _h.next()) {
872
- var _k = __read(_j.value, 2), functionName = _k[0], i = _k[1];
873
- ptbJson.promptTemplates.push({
874
- name: normalizeTo_PascalCase(section.title + ' Postprocessing ' + i),
875
- title: "(".concat(i + 1, "/").concat(postprocessingCommands.length, ") ").concat(section.title, " postprocessing"),
876
- description: "Postprocessing of section ".concat(section.title, " finally with resulting parameter {").concat(resultingParameterName, "}"),
877
- executionType: 'SCRIPT',
878
- contentLanguage: 'javascript',
879
- content: "".concat(functionName, "(").concat(getParameterName(i), ")"),
880
- resultingParameterName: getParameterName(i + 1),
881
- });
882
- }
883
- }
884
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
885
- finally {
886
- try {
887
- if (_j && !_j.done && (_f = _h.return)) _f.call(_h);
888
- }
889
- finally { if (e_4) throw e_4.error; }
890
- }
891
871
  };
892
872
  try {
893
873
  for (var _c = __values(markdownStructure.sections), _d = _c.next(); !_d.done; _d = _c.next()) {
@@ -908,9 +888,7 @@ function promptTemplatePipelineStringToJson(promptTemplatePipelineString) {
908
888
  /**
909
889
  * TODO: Report here line/column of error
910
890
  * TODO: Use spaceTrim more effectively
911
- * TODO: !!!! Parameter flags - isInput, isOutput, isInternal, isBeforePostprocessing, isBeforeFinal, canonicalName
912
- * TODO: !!!! Allow to have non-immutable parameters - suffix them with fooPrevious3 -> fooPrevious2 -> fooPrevious1 -> foo
913
- * This must work with other technial parameters
891
+ * TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
914
892
  */
915
893
 
916
894
  /**
@@ -1041,7 +1019,7 @@ var PromptTemplatePipeline = /** @class */ (function () {
1041
1019
  */
1042
1020
  PromptTemplatePipeline.fromJson = function (ptbkjson) {
1043
1021
  validatePromptTemplatePipelineJson(ptbkjson);
1044
- 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);
1022
+ 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);
1045
1023
  };
1046
1024
  Object.defineProperty(PromptTemplatePipeline.prototype, "entryPromptTemplate", {
1047
1025
  /**
@@ -1301,6 +1279,23 @@ function replaceParameters(template, parameters) {
1301
1279
  return replacedTemplate;
1302
1280
  }
1303
1281
 
1282
+ /**
1283
+ * This error occurs when some expectation is not met in the execution of the pipeline
1284
+ */
1285
+ var ExpectError = /** @class */ (function (_super) {
1286
+ __extends(ExpectError, _super);
1287
+ function ExpectError(message) {
1288
+ var _this = _super.call(this, message) || this;
1289
+ _this.name = 'ExpectError';
1290
+ Object.setPrototypeOf(_this, ExpectError.prototype);
1291
+ return _this;
1292
+ }
1293
+ return ExpectError;
1294
+ }(Error));
1295
+ /**
1296
+ * TODO: [🧠] Should be this exported from the library
1297
+ */
1298
+
1304
1299
  /**
1305
1300
  * Creates executor function from prompt template pipeline and execution tools.
1306
1301
  *
@@ -1309,7 +1304,7 @@ function replaceParameters(template, parameters) {
1309
1304
  function createPtpExecutor(options) {
1310
1305
  var _this = this;
1311
1306
  var ptp = options.ptp, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
1312
- var _b = settings.maxNaturalExecutionAttempts, maxNaturalExecutionAttempts = _b === void 0 ? 3 : _b;
1307
+ var _b = settings.maxExecutionAttempts, maxExecutionAttempts = _b === void 0 ? 3 : _b;
1313
1308
  var ptpExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
1314
1309
  var parametersToPass, currentTemplate, executionReport, _loop_1, error_1;
1315
1310
  var _a;
@@ -1330,14 +1325,21 @@ function createPtpExecutor(options) {
1330
1325
  case 1:
1331
1326
  _b.trys.push([1, 5, , 6]);
1332
1327
  _loop_1 = function () {
1333
- 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;
1334
- var e_2, _l, e_1, _m, _o;
1335
- return __generator(this, function (_p) {
1336
- switch (_p.label) {
1328
+ 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;
1329
+ var e_1, _q, e_3, _r, e_2, _s, e_4, _t, _u;
1330
+ return __generator(this, function (_v) {
1331
+ switch (_v.label) {
1337
1332
  case 0:
1338
1333
  resultingParameter = ptp.getResultingParameter(currentTemplate.name);
1339
1334
  name_1 = "ptp-executor-frame-".concat(currentTemplate.name);
1340
1335
  title = removeEmojis(removeMarkdownFormatting(currentTemplate.title));
1336
+ console.info('πŸ›¬ currentTemplate', {
1337
+ currentTemplate: currentTemplate,
1338
+ resultingParameter: resultingParameter,
1339
+ name: name_1,
1340
+ title: title,
1341
+ parametersToPass: __assign({}, parametersToPass),
1342
+ });
1341
1343
  if (!onProgress) return [3 /*break*/, 2];
1342
1344
  return [4 /*yield*/, onProgress({
1343
1345
  name: name_1,
@@ -1349,180 +1351,129 @@ function createPtpExecutor(options) {
1349
1351
  parameterValue: null,
1350
1352
  })];
1351
1353
  case 1:
1352
- _p.sent();
1353
- _p.label = 2;
1354
+ _v.sent();
1355
+ _v.label = 2;
1354
1356
  case 2:
1355
1357
  prompt_1 = void 0;
1356
1358
  chatThread = void 0;
1357
1359
  completionResult = void 0;
1358
1360
  result = null;
1359
1361
  resultString = null;
1360
- naturalExecutionError = null;
1361
- 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
+ console.info('πŸ›¬ currentTemplate attempt', attempt);
1368
+ result = null;
1369
+ resultString = null;
1370
+ expectError = null;
1371
+ _v.label = 4;
1372
+ case 4:
1373
+ _v.trys.push([4, 44, 45, 46]);
1362
1374
  _c = currentTemplate.executionType;
1363
1375
  switch (_c) {
1364
- case 'SIMPLE_TEMPLATE': return [3 /*break*/, 3];
1365
- case 'PROMPT_TEMPLATE': return [3 /*break*/, 4];
1366
- case 'SCRIPT': return [3 /*break*/, 18];
1367
- case 'PROMPT_DIALOG': return [3 /*break*/, 29];
1376
+ case 'SIMPLE_TEMPLATE': return [3 /*break*/, 5];
1377
+ case 'PROMPT_TEMPLATE': return [3 /*break*/, 6];
1378
+ case 'SCRIPT': return [3 /*break*/, 13];
1379
+ case 'PROMPT_DIALOG': return [3 /*break*/, 24];
1368
1380
  }
1369
- return [3 /*break*/, 31];
1370
- case 3:
1381
+ return [3 /*break*/, 26];
1382
+ case 5:
1383
+ console.info('πŸ›¬ currentTemplate attempt SIMPLE_TEMPLATE');
1371
1384
  resultString = replaceParameters(currentTemplate.content, parametersToPass);
1372
- return [3 /*break*/, 32];
1373
- case 4:
1385
+ return [3 /*break*/, 27];
1386
+ case 6:
1387
+ console.info('πŸ›¬ currentTemplate attempt PROMPT_TEMPLATE');
1374
1388
  prompt_1 = {
1375
1389
  title: currentTemplate.title,
1376
1390
  ptbkUrl: "".concat(ptp.ptbkUrl
1377
1391
  ? ptp.ptbkUrl.href
1378
- : 'anonymous' /* <- [🧠] !!!! How to deal with anonymous PTPs, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
1392
+ : 'anonymous' /* <- [🧠] !!! How to deal with anonymous PTPs, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
1379
1393
  parameters: parametersToPass,
1380
- content: replaceParameters(currentTemplate.content, parametersToPass),
1394
+ content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
1381
1395
  modelRequirements: currentTemplate.modelRequirements,
1382
1396
  };
1383
- attempt = 0;
1384
- _p.label = 5;
1385
- case 5:
1386
- if (!(attempt < maxNaturalExecutionAttempts)) return [3 /*break*/, 17];
1387
- result = null;
1388
- resultString = null;
1389
- naturalExecutionError = null;
1390
- _p.label = 6;
1391
- case 6:
1392
- _p.trys.push([6, 13, 14, 15]);
1393
1397
  _d = currentTemplate.modelRequirements.modelVariant;
1394
1398
  switch (_d) {
1395
1399
  case 'CHAT': return [3 /*break*/, 7];
1396
1400
  case 'COMPLETION': return [3 /*break*/, 9];
1397
1401
  }
1398
1402
  return [3 /*break*/, 11];
1399
- case 7: return [4 /*yield*/, tools.natural.gptChat(prompt_1)];
1403
+ case 7:
1404
+ console.info('πŸ›¬ currentTemplate attempt PROMPT_TEMPLATE CHAT');
1405
+ return [4 /*yield*/, tools.natural.gptChat(prompt_1)];
1400
1406
  case 8:
1401
- chatThread = _p.sent();
1407
+ chatThread = _v.sent();
1402
1408
  // TODO: [🍬] Destroy chatThread
1403
1409
  result = chatThread;
1404
1410
  resultString = chatThread.content;
1405
1411
  return [3 /*break*/, 12];
1406
- case 9: return [4 /*yield*/, tools.natural.gptComplete(prompt_1)];
1412
+ case 9:
1413
+ console.info('πŸ›¬ currentTemplate attempt PROMPT_TEMPLATE COMPLETION');
1414
+ return [4 /*yield*/, tools.natural.gptComplete(prompt_1)];
1407
1415
  case 10:
1408
- completionResult = _p.sent();
1416
+ completionResult = _v.sent();
1409
1417
  result = completionResult;
1410
1418
  resultString = completionResult.content;
1411
1419
  return [3 /*break*/, 12];
1412
- case 11: throw new Error("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
1413
- case 12:
1414
- // TODO: !!!!!! Here should postprocessing happen
1415
- if (currentTemplate.expectFormat) {
1416
- if (currentTemplate.expectFormat === 'JSON') {
1417
- if (!isValidJsonString(resultString)) {
1418
- throw new Error('Expected valid JSON string');
1419
- }
1420
- }
1421
- }
1422
- if (currentTemplate.expectAmount) {
1423
- try {
1424
- for (_e = (e_2 = void 0, __values(Object.entries(currentTemplate.expectAmount))), _f = _e.next(); !_f.done; _f = _e.next()) {
1425
- _g = __read(_f.value, 2), unit = _g[0], _h = _g[1], max = _h.max, min = _h.min;
1426
- amount = CountUtils[unit.toUpperCase()](resultString);
1427
- if (min && amount < min) {
1428
- throw new Error("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
1429
- } /* not else */
1430
- if (max && amount > max) {
1431
- throw new Error("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
1432
- }
1433
- }
1434
- }
1435
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
1436
- finally {
1437
- try {
1438
- if (_f && !_f.done && (_l = _e.return)) _l.call(_e);
1439
- }
1440
- finally { if (e_2) throw e_2.error; }
1441
- }
1442
- }
1443
- return [3 /*break*/, 15];
1420
+ case 11:
1421
+ console.info('πŸ›¬ currentTemplate attempt PROMPT_TEMPLATE UNKNOWN');
1422
+ throw new Error("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
1423
+ case 12: return [3 /*break*/, 27];
1444
1424
  case 13:
1445
- error_2 = _p.sent();
1446
- if (!(error_2 instanceof Error)) {
1447
- throw error_2;
1448
- }
1449
- naturalExecutionError = error_2;
1450
- return [3 /*break*/, 15];
1451
- case 14:
1452
- executionReport.promptExecutions.push({
1453
- prompt: {
1454
- title: prompt_1.title,
1455
- content: prompt_1.content,
1456
- modelRequirements: prompt_1.modelRequirements,
1457
- // <- Note: Do want to pass ONLY wanted information to the report
1458
- },
1459
- result: result /* <- !!!!! Look what is exposed here and probbably also filter out */ ||
1460
- undefined,
1461
- error: naturalExecutionError || undefined,
1462
- });
1463
- return [7 /*endfinally*/];
1464
- case 15:
1465
- if (result !== null && naturalExecutionError === null) {
1466
- return [3 /*break*/, 32];
1467
- }
1468
- _p.label = 16;
1469
- case 16:
1470
- attempt++;
1471
- return [3 /*break*/, 5];
1472
- case 17: throw new Error(spaceTrim(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 "); }));
1473
- case 18:
1425
+ console.info('πŸ›¬ currentTemplate attempt SCRIPT');
1474
1426
  if (tools.script.length === 0) {
1475
1427
  throw new Error('No script execution tools are available');
1476
1428
  }
1477
1429
  if (!currentTemplate.contentLanguage) {
1478
1430
  throw new Error("Script language is not defined for prompt template \"".concat(currentTemplate.name, "\""));
1479
1431
  }
1432
+ // TODO: DRY [1]
1480
1433
  scriptExecutionErrors = [];
1481
- isScriptExecutionSuccessful = false;
1482
- _p.label = 19;
1483
- case 19:
1484
- _p.trys.push([19, 26, 27, 28]);
1485
- _j = (e_1 = void 0, __values(tools.script)), _k = _j.next();
1486
- _p.label = 20;
1487
- case 20:
1488
- if (!!_k.done) return [3 /*break*/, 25];
1489
- scriptTools = _k.value;
1490
- _p.label = 21;
1491
- case 21:
1492
- _p.trys.push([21, 23, , 24]);
1434
+ _v.label = 14;
1435
+ case 14:
1436
+ _v.trys.push([14, 21, 22, 23]);
1437
+ _e = (e_1 = void 0, __values(tools.script)), _f = _e.next();
1438
+ _v.label = 15;
1439
+ case 15:
1440
+ if (!!_f.done) return [3 /*break*/, 20];
1441
+ scriptTools = _f.value;
1442
+ _v.label = 16;
1443
+ case 16:
1444
+ _v.trys.push([16, 18, , 19]);
1493
1445
  return [4 /*yield*/, scriptTools.execute({
1494
1446
  scriptLanguage: currentTemplate.contentLanguage,
1495
1447
  script: currentTemplate.content,
1496
1448
  parameters: parametersToPass,
1497
1449
  })];
1498
- case 22:
1499
- resultString = _p.sent();
1500
- isScriptExecutionSuccessful = true;
1501
- return [3 /*break*/, 25];
1502
- case 23:
1503
- error_3 = _p.sent();
1504
- if (!(error_3 instanceof Error)) {
1505
- throw error_3;
1506
- }
1507
- scriptExecutionErrors.push(error_3);
1508
- return [3 /*break*/, 24];
1509
- case 24:
1510
- _k = _j.next();
1450
+ case 17:
1451
+ resultString = _v.sent();
1511
1452
  return [3 /*break*/, 20];
1512
- case 25: return [3 /*break*/, 28];
1513
- case 26:
1514
- e_1_1 = _p.sent();
1453
+ case 18:
1454
+ error_2 = _v.sent();
1455
+ if (!(error_2 instanceof Error)) {
1456
+ throw error_2;
1457
+ }
1458
+ scriptExecutionErrors.push(error_2);
1459
+ return [3 /*break*/, 19];
1460
+ case 19:
1461
+ _f = _e.next();
1462
+ return [3 /*break*/, 15];
1463
+ case 20: return [3 /*break*/, 23];
1464
+ case 21:
1465
+ e_1_1 = _v.sent();
1515
1466
  e_1 = { error: e_1_1 };
1516
- return [3 /*break*/, 28];
1517
- case 27:
1467
+ return [3 /*break*/, 23];
1468
+ case 22:
1518
1469
  try {
1519
- if (_k && !_k.done && (_m = _j.return)) _m.call(_j);
1470
+ if (_f && !_f.done && (_q = _e.return)) _q.call(_e);
1520
1471
  }
1521
1472
  finally { if (e_1) throw e_1.error; }
1522
1473
  return [7 /*endfinally*/];
1523
- case 28:
1524
- if (isScriptExecutionSuccessful) {
1525
- return [3 /*break*/, 32];
1474
+ case 23:
1475
+ if (resultString) {
1476
+ return [3 /*break*/, 27];
1526
1477
  }
1527
1478
  if (scriptExecutionErrors.length === 1) {
1528
1479
  throw scriptExecutionErrors[0];
@@ -1530,19 +1481,164 @@ function createPtpExecutor(options) {
1530
1481
  else {
1531
1482
  throw new Error(spaceTrim(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 "); }));
1532
1483
  }
1533
- case 29: return [4 /*yield*/, tools.userInterface.promptDialog({
1534
- prompt: replaceParameters(currentTemplate.description || '', parametersToPass),
1535
- defaultValue: replaceParameters(currentTemplate.content, parametersToPass),
1536
- // TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
1537
- placeholder: undefined,
1538
- })];
1484
+ case 24:
1485
+ console.info('πŸ›¬ currentTemplate attempt PROMPT_DIALOG');
1486
+ return [4 /*yield*/, tools.userInterface.promptDialog({
1487
+ prompt: replaceParameters(currentTemplate.description || '', parametersToPass),
1488
+ defaultValue: replaceParameters(currentTemplate.content, parametersToPass),
1489
+ // TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
1490
+ placeholder: undefined,
1491
+ })];
1492
+ case 25:
1493
+ resultString = _v.sent();
1494
+ return [3 /*break*/, 27];
1495
+ case 26:
1496
+ console.info('πŸ›¬ currentTemplate attempt UNKNOWN');
1497
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1498
+ throw new Error("Unknown execution type \"".concat(currentTemplate.executionType, "\""));
1499
+ case 27:
1500
+ if (!currentTemplate.postprocessing) return [3 /*break*/, 43];
1501
+ console.info('πŸ›¬ currentTemplate attempt postprocessing');
1502
+ _v.label = 28;
1503
+ case 28:
1504
+ _v.trys.push([28, 41, 42, 43]);
1505
+ _g = (e_3 = void 0, __values(currentTemplate.postprocessing)), _h = _g.next();
1506
+ _v.label = 29;
1507
+ case 29:
1508
+ if (!!_h.done) return [3 /*break*/, 40];
1509
+ functionName = _h.value;
1510
+ console.info('πŸ›¬ currentTemplate attempt postprocessing', functionName);
1511
+ // TODO: DRY [1]
1512
+ scriptExecutionErrors = [];
1513
+ _v.label = 30;
1539
1514
  case 30:
1540
- resultString = _p.sent();
1541
- return [3 /*break*/, 32];
1542
- case 31:
1543
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1544
- throw new Error("Unknown execution type \"".concat(currentTemplate.executionType, "\""));
1515
+ _v.trys.push([30, 37, 38, 39]);
1516
+ _j = (e_2 = void 0, __values(tools.script)), _k = _j.next();
1517
+ _v.label = 31;
1518
+ case 31:
1519
+ if (!!_k.done) return [3 /*break*/, 36];
1520
+ scriptTools = _k.value;
1521
+ _v.label = 32;
1545
1522
  case 32:
1523
+ _v.trys.push([32, 34, , 35]);
1524
+ console.info('πŸ›¬ currentTemplate attempt postprocessing', functionName, {
1525
+ scriptTools: scriptTools,
1526
+ });
1527
+ return [4 /*yield*/, scriptTools.execute({
1528
+ scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
1529
+ script: "".concat(functionName, "(resultString)"),
1530
+ parameters: __assign(__assign({}, parametersToPass), { resultString: resultString }),
1531
+ })];
1532
+ case 33:
1533
+ resultString = _v.sent();
1534
+ return [3 /*break*/, 36];
1535
+ case 34:
1536
+ error_3 = _v.sent();
1537
+ if (!(error_3 instanceof Error)) {
1538
+ throw error_3;
1539
+ }
1540
+ scriptExecutionErrors.push(error_3);
1541
+ return [3 /*break*/, 35];
1542
+ case 35:
1543
+ _k = _j.next();
1544
+ return [3 /*break*/, 31];
1545
+ case 36: return [3 /*break*/, 39];
1546
+ case 37:
1547
+ e_2_1 = _v.sent();
1548
+ e_2 = { error: e_2_1 };
1549
+ return [3 /*break*/, 39];
1550
+ case 38:
1551
+ try {
1552
+ if (_k && !_k.done && (_s = _j.return)) _s.call(_j);
1553
+ }
1554
+ finally { if (e_2) throw e_2.error; }
1555
+ return [7 /*endfinally*/];
1556
+ case 39:
1557
+ _h = _g.next();
1558
+ return [3 /*break*/, 29];
1559
+ case 40: return [3 /*break*/, 43];
1560
+ case 41:
1561
+ e_3_1 = _v.sent();
1562
+ e_3 = { error: e_3_1 };
1563
+ return [3 /*break*/, 43];
1564
+ case 42:
1565
+ try {
1566
+ if (_h && !_h.done && (_r = _g.return)) _r.call(_g);
1567
+ }
1568
+ finally { if (e_3) throw e_3.error; }
1569
+ return [7 /*endfinally*/];
1570
+ case 43:
1571
+ if (currentTemplate.expectFormat) {
1572
+ console.info('πŸ›¬ currentTemplate attempt expectFormat');
1573
+ if (currentTemplate.expectFormat === 'JSON') {
1574
+ if (!isValidJsonString(resultString)) {
1575
+ throw new ExpectError('Expected valid JSON string');
1576
+ }
1577
+ }
1578
+ }
1579
+ if (currentTemplate.expectAmount) {
1580
+ console.info('πŸ›¬ currentTemplate attempt expectAmount');
1581
+ try {
1582
+ for (_l = (e_4 = void 0, __values(Object.entries(currentTemplate.expectAmount))), _m = _l.next(); !_m.done; _m = _l.next()) {
1583
+ _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], max = _p.max, min = _p.min;
1584
+ amount = CountUtils[unit.toUpperCase()](resultString);
1585
+ console.info('πŸ›¬ currentTemplate attempt expectAmount', { unit: unit, max: max, min: min, amount: amount });
1586
+ if (min && amount < min) {
1587
+ throw new ExpectError("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
1588
+ } /* not else */
1589
+ if (max && amount > max) {
1590
+ throw new ExpectError("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
1591
+ }
1592
+ }
1593
+ }
1594
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
1595
+ finally {
1596
+ try {
1597
+ if (_m && !_m.done && (_t = _l.return)) _t.call(_l);
1598
+ }
1599
+ finally { if (e_4) throw e_4.error; }
1600
+ }
1601
+ }
1602
+ return [3 /*break*/, 46];
1603
+ case 44:
1604
+ error_4 = _v.sent();
1605
+ console.info('πŸ›¬ currentTemplate attempt catch', { error: error_4 });
1606
+ if (!(error_4 instanceof ExpectError)) {
1607
+ throw error_4;
1608
+ }
1609
+ expectError = error_4;
1610
+ return [3 /*break*/, 46];
1611
+ case 45:
1612
+ console.info('πŸ›¬ currentTemplate attempt finally', { currentTemplate: currentTemplate, result: result, expectError: expectError });
1613
+ if (currentTemplate.executionType === 'PROMPT_TEMPLATE' &&
1614
+ prompt_1
1615
+ // <- Note: [2] When some expected parameter is not defined, error will occur in replaceParameters
1616
+ // In that case we don’t want to make a report about it because it’s not a natural execution error
1617
+ ) {
1618
+ console.info('πŸ›¬ currentTemplate attempt finally PROMPT_TEMPLATE', { prompt: prompt_1 });
1619
+ // TODO: [🧠] Maybe put other executionTypes into report
1620
+ executionReport.promptExecutions.push({
1621
+ prompt: {
1622
+ title: currentTemplate.title /* <- Note: If title in promptbook contains emojis, pass it innto report */,
1623
+ content: prompt_1.content,
1624
+ modelRequirements: prompt_1.modelRequirements,
1625
+ // <- Note: Do want to pass ONLY wanted information to the report
1626
+ },
1627
+ result: result || undefined,
1628
+ error: expectError || undefined,
1629
+ });
1630
+ }
1631
+ return [7 /*endfinally*/];
1632
+ case 46:
1633
+ if (result === null && attempt === maxExecutionAttempts) {
1634
+ console.info('πŸ›¬ currentTemplate attempt fail', { attempt: attempt, maxExecutionAttempts: maxExecutionAttempts });
1635
+ throw new Error(spaceTrim(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 "); }));
1636
+ }
1637
+ _v.label = 47;
1638
+ case 47:
1639
+ attempt++;
1640
+ return [3 /*break*/, 3];
1641
+ case 48:
1546
1642
  if (resultString === null) {
1547
1643
  // <- TODO: [πŸ₯¨] Make some NeverShouldHappenError
1548
1644
  throw new Error('Something went wrong and prompt result is null');
@@ -1558,7 +1654,7 @@ function createPtpExecutor(options) {
1558
1654
  parameterValue: resultString,
1559
1655
  });
1560
1656
  }
1561
- 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));
1657
+ 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));
1562
1658
  currentTemplate = ptp.getFollowingPromptTemplate(currentTemplate.name);
1563
1659
  return [2 /*return*/];
1564
1660
  }