@promptbook/core 0.44.0-8 โ†’ 0.44.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
@@ -1,12 +1,8 @@
1
1
  (function (global, factory) {
2
2
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('spacetrim'), require('prettier'), require('prettier/parser-html'), require('moment'), require('lorem-ipsum')) :
3
3
  typeof define === 'function' && define.amd ? define(['exports', 'spacetrim', 'prettier', 'prettier/parser-html', 'moment', 'lorem-ipsum'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-core"] = {}, global.spaceTrim, null, null, null, global.loremIpsum));
5
- })(this, (function (exports, spaceTrim, prettier, parserHtml, moment, loremIpsum) { 'use strict';
6
-
7
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
-
9
- var spaceTrim__default = /*#__PURE__*/_interopDefaultLegacy(spaceTrim);
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-core"] = {}, global.spacetrim, null, null, null, global.loremIpsum));
5
+ })(this, (function (exports, spacetrim, prettier, parserHtml, moment, loremIpsum) { 'use strict';
10
6
 
11
7
  /*! *****************************************************************************
12
8
  Copyright (c) Microsoft Corporation.
@@ -133,7 +129,7 @@
133
129
  var UnexpectedError = /** @class */ (function (_super) {
134
130
  __extends(UnexpectedError, _super);
135
131
  function UnexpectedError(message) {
136
- var _this = _super.call(this, spaceTrim__default["default"](function (block) { return "\n ".concat(block(message), "\n\n Note: This error should not happen.\n It's probbably a bug in the promptbook library\n\n Please report issue:\n https://github.com/webgptorg/promptbook/issues\n\n Or contact us on me@pavolhejny.com\n\n "); })) || this;
132
+ var _this = _super.call(this, spacetrim.spaceTrim(function (block) { return "\n ".concat(block(message), "\n\n Note: This error should not happen.\n It's probbably a bug in the promptbook library\n\n Please report issue:\n https://github.com/webgptorg/promptbook/issues\n\n Or contact us on me@pavolhejny.com\n\n "); })) || this;
137
133
  _this.name = 'UnexpectedError';
138
134
  Object.setPrototypeOf(_this, UnexpectedError.prototype);
139
135
  return _this;
@@ -148,7 +144,7 @@
148
144
  * @returns {string} The input string with all comments removed.
149
145
  */
150
146
  function removeContentComments(content) {
151
- return spaceTrim__default["default"](content.replace(/<!--(.*?)-->/gs, ''));
147
+ return spacetrim.spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
152
148
  }
153
149
 
154
150
  /**
@@ -651,6 +647,10 @@
651
647
  * The maximum number of iterations for a loops
652
648
  */
653
649
  var LOOP_LIMIT = 1000;
650
+ /**
651
+ * The maximum number of iterations for a loops which adds characters one by one
652
+ */
653
+ var CHARACTER_LOOP_LIMIT = 100000;
654
654
 
655
655
  /**
656
656
  * This error occurs during the parameter replacement in the template
@@ -1119,7 +1119,7 @@
1119
1119
  }
1120
1120
  if (parent_1.parent === null /* <- Note: We are in root */) {
1121
1121
  // [๐ŸŒป]
1122
- throw new Error(spaceTrim__default["default"]("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
1122
+ throw new Error(spacetrim.spaceTrim("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
1123
1123
  }
1124
1124
  parent_1 = parent_1.parent;
1125
1125
  }
@@ -1153,7 +1153,7 @@
1153
1153
  return {
1154
1154
  level: level,
1155
1155
  title: title,
1156
- content: spaceTrim__default["default"](contentLines.join('\n')),
1156
+ content: spacetrim.spaceTrim(contentLines.join('\n')),
1157
1157
  sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
1158
1158
  };
1159
1159
  }
@@ -1161,7 +1161,7 @@
1161
1161
  /**
1162
1162
  * The version of the Promptbook library
1163
1163
  */
1164
- var PROMPTBOOK_VERSION = '0.44.0-7';
1164
+ var PROMPTBOOK_VERSION = '0.44.0-23';
1165
1165
 
1166
1166
  /**
1167
1167
  * Parses the given script and returns the list of all used variables that are not defined in the script
@@ -1206,7 +1206,7 @@
1206
1206
  if (!(error instanceof Error)) {
1207
1207
  throw error;
1208
1208
  }
1209
- throw new PromptbookSyntaxError(spaceTrim__default["default"](function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
1209
+ throw new PromptbookSyntaxError(spacetrim.spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
1210
1210
  }
1211
1211
  return variables;
1212
1212
  }
@@ -1271,15 +1271,15 @@
1271
1271
  type.startsWith('PROMPTBOOKURL') ||
1272
1272
  type.startsWith('HTTPS')) {
1273
1273
  if (!(listItemParts.length === 2 || (listItemParts.length === 1 && type.startsWith('HTTPS')))) {
1274
- throw new PromptbookSyntaxError(spaceTrim__default["default"]("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n ")));
1274
+ throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n ")));
1275
1275
  }
1276
1276
  var promptbookUrlString = listItemParts.pop();
1277
1277
  var promptbookUrl = new URL(promptbookUrlString);
1278
1278
  if (promptbookUrl.protocol !== 'https:') {
1279
- throw new PromptbookSyntaxError(spaceTrim__default["default"]("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n Protocol must be HTTPS\n ")));
1279
+ throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n Protocol must be HTTPS\n ")));
1280
1280
  }
1281
1281
  if (promptbookUrl.hash !== '') {
1282
- throw new PromptbookSyntaxError(spaceTrim__default["default"]("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n URL must not contain hash\n Hash is used for identification of the prompt template in the pipeline\n ")));
1282
+ throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n URL must not contain hash\n Hash is used for identification of the prompt template in the pipeline\n ")));
1283
1283
  }
1284
1284
  return {
1285
1285
  type: 'PROMPTBOOK_URL',
@@ -1288,7 +1288,7 @@
1288
1288
  }
1289
1289
  else if (type.startsWith('PROMPTBOOK_VERSION') || type.startsWith('PTBK_VERSION')) {
1290
1290
  if (listItemParts.length !== 2) {
1291
- throw new PromptbookSyntaxError(spaceTrim__default["default"]("\n Invalid PROMPTBOOK_VERSION command:\n\n - ".concat(listItem, "\n ")));
1291
+ throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid PROMPTBOOK_VERSION command:\n\n - ".concat(listItem, "\n ")));
1292
1292
  }
1293
1293
  var promptbookVersion = listItemParts.pop();
1294
1294
  // TODO: Validate version
@@ -1303,7 +1303,7 @@
1303
1303
  type.startsWith('SIMPLE_TEMPLATE')) {
1304
1304
  var executionTypes = ExecutionTypes.filter(function (executionType) { return type.includes(executionType); });
1305
1305
  if (executionTypes.length !== 1) {
1306
- throw new PromptbookSyntaxError(spaceTrim__default["default"](function (block) { return "\n Unknown execution type in command:\n\n - ".concat(listItem, "\n\n Supported execution types are:\n ").concat(block(ExecutionTypes.join(', ')), "\n "); }));
1306
+ throw new PromptbookSyntaxError(spacetrim.spaceTrim(function (block) { return "\n Unknown execution type in command:\n\n - ".concat(listItem, "\n\n Supported execution types are:\n ").concat(block(ExecutionTypes.join(', ')), "\n "); }));
1307
1307
  }
1308
1308
  return {
1309
1309
  type: 'EXECUTE',
@@ -1328,7 +1328,7 @@
1328
1328
  };
1329
1329
  }
1330
1330
  else {
1331
- throw new PromptbookSyntaxError(spaceTrim__default["default"](function (block) { return "\n Unknown model variant in command:\n\n - ".concat(listItem, "\n\n Supported variants are:\n ").concat(block(['CHAT', 'COMPLETION'].join(', ')), "\n "); }));
1331
+ throw new PromptbookSyntaxError(spacetrim.spaceTrim(function (block) { return "\n Unknown model variant in command:\n\n - ".concat(listItem, "\n\n Supported variants are:\n ").concat(block(['CHAT', 'COMPLETION'].join(', ')), "\n "); }));
1332
1332
  }
1333
1333
  }
1334
1334
  if (type.startsWith('MODEL_NAME')) {
@@ -1339,7 +1339,7 @@
1339
1339
  };
1340
1340
  }
1341
1341
  else {
1342
- throw new PromptbookSyntaxError(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 "); }));
1342
+ throw new PromptbookSyntaxError(spacetrim.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 "); }));
1343
1343
  }
1344
1344
  }
1345
1345
  else if (type.startsWith('PARAM') ||
@@ -1349,12 +1349,12 @@
1349
1349
  listItem.startsWith('> {') /* <- Note: This is a bit hack to parse return parameters defined at the end of each section */) {
1350
1350
  var parametersMatch = listItem.match(/\{(?<parameterName>[a-z0-9_]+)\}[^\S\r\n]*(?<parameterDescription>.*)$/im);
1351
1351
  if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
1352
- throw new PromptbookSyntaxError(spaceTrim__default["default"]("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
1352
+ throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
1353
1353
  }
1354
1354
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1355
1355
  var _b = parametersMatch.groups, parameterName = _b.parameterName, parameterDescription = _b.parameterDescription;
1356
1356
  if (parameterDescription && parameterDescription.match(/\{(?<parameterName>[a-z0-9_]+)\}/im)) {
1357
- throw new PromptbookSyntaxError(spaceTrim__default["default"]("\n Parameter {".concat(parameterName, "} can not contain another parameter in description:\n\n - ").concat(listItem, "\n ")));
1357
+ throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Parameter {".concat(parameterName, "} can not contain another parameter in description:\n\n - ").concat(listItem, "\n ")));
1358
1358
  }
1359
1359
  var isInput = type.startsWith('INPUT');
1360
1360
  var isOutput = type.startsWith('OUTPUT');
@@ -1372,11 +1372,11 @@
1372
1372
  }
1373
1373
  else if (type.startsWith('JOKER')) {
1374
1374
  if (listItemParts.length !== 2) {
1375
- throw new PromptbookSyntaxError(spaceTrim__default["default"]("\n Invalid JOKER command:\n\n - ".concat(listItem, "\n ")));
1375
+ throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid JOKER command:\n\n - ".concat(listItem, "\n ")));
1376
1376
  }
1377
1377
  var parametersMatch = (listItemParts.pop() || '').match(/^\{(?<parameterName>[a-z0-9_]+)\}$/im);
1378
1378
  if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
1379
- throw new PromptbookSyntaxError(spaceTrim__default["default"]("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
1379
+ throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
1380
1380
  }
1381
1381
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1382
1382
  var parameterName = parametersMatch.groups.parameterName;
@@ -1387,7 +1387,7 @@
1387
1387
  }
1388
1388
  else if (type.startsWith('POSTPROCESS') || type.startsWith('POST_PROCESS')) {
1389
1389
  if (listItemParts.length !== 2) {
1390
- throw new PromptbookSyntaxError(spaceTrim__default["default"]("\n Invalid POSTPROCESSING command:\n\n - ".concat(listItem, "\n ")));
1390
+ throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid POSTPROCESSING command:\n\n - ".concat(listItem, "\n ")));
1391
1391
  }
1392
1392
  var functionName = listItemParts.pop();
1393
1393
  return {
@@ -1467,7 +1467,7 @@
1467
1467
  if (!(error instanceof Error)) {
1468
1468
  throw error;
1469
1469
  }
1470
- throw new PromptbookSyntaxError(spaceTrim__default["default"]("\n Invalid EXPECT command; ".concat(error.message, ":\n\n - ").concat(listItem, "\n ")));
1470
+ throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid EXPECT command; ".concat(error.message, ":\n\n - ").concat(listItem, "\n ")));
1471
1471
  }
1472
1472
  /*
1473
1473
  } else if (type.startsWith('__________________')) {
@@ -1475,7 +1475,7 @@
1475
1475
  */
1476
1476
  }
1477
1477
  else {
1478
- throw new PromptbookSyntaxError(spaceTrim__default["default"]("\n Unknown command:\n\n - ".concat(listItem, "\n\n Supported commands are:\n - PROMPTBOOK_URL <url>\n - PROMPTBOOK_VERSION <version>\n - EXECUTE PROMPT TEMPLATE\n - EXECUTE SIMPLE TEMPLATE\n - SIMPLE TEMPLATE\n - EXECUTE SCRIPT\n - EXECUTE PROMPT_DIALOG'\n - PROMPT_DIALOG'\n - MODEL NAME <name>\n - MODEL VARIANT <\"Chat\"|\"Completion\">\n - INPUT PARAM {<name>} <description>\n - OUTPUT PARAM {<name>} <description>\n - POSTPROCESS `{functionName}`\n - JOKER {<name>}\n - EXPECT JSON\n - EXPECT <\"Exactly\"|\"Min\"|\"Max\"> <number> <\"Chars\"|\"Words\"|\"Sentences\"|\"Paragraphs\"|\"Pages\">\n\n ")));
1478
+ throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Unknown command:\n\n - ".concat(listItem, "\n\n Supported commands are:\n - PROMPTBOOK_URL <url>\n - PROMPTBOOK_VERSION <version>\n - EXECUTE PROMPT TEMPLATE\n - EXECUTE SIMPLE TEMPLATE\n - SIMPLE TEMPLATE\n - EXECUTE SCRIPT\n - EXECUTE PROMPT_DIALOG'\n - PROMPT_DIALOG'\n - MODEL NAME <name>\n - MODEL VARIANT <\"Chat\"|\"Completion\">\n - INPUT PARAM {<name>} <description>\n - OUTPUT PARAM {<name>} <description>\n - POSTPROCESS `{functionName}`\n - JOKER {<name>}\n - EXPECT JSON\n - EXPECT <\"Exactly\"|\"Min\"|\"Max\"> <number> <\"Chars\"|\"Words\"|\"Sentences\"|\"Paragraphs\"|\"Pages\">\n\n ")));
1479
1479
  }
1480
1480
  }
1481
1481
 
@@ -1511,7 +1511,7 @@
1511
1511
  existingParameter.description &&
1512
1512
  existingParameter.description !== parameterDescription &&
1513
1513
  parameterDescription) {
1514
- throw new PromptbookSyntaxError(spaceTrim__default["default"](function (block) { return "\n Parameter {".concat(parameterName, "} is defined multiple times with different description.\n\n First definition:\n ").concat(block(existingParameter.description || '[undefined]'), "\n\n Second definition:\n ").concat(block(parameterDescription || '[undefined]'), "\n "); }));
1514
+ throw new PromptbookSyntaxError(spacetrim.spaceTrim(function (block) { return "\n Parameter {".concat(parameterName, "} is defined multiple times with different description.\n\n First definition:\n ").concat(block(existingParameter.description || '[undefined]'), "\n\n Second definition:\n ").concat(block(parameterDescription || '[undefined]'), "\n "); }));
1515
1515
  }
1516
1516
  if (existingParameter) {
1517
1517
  if (parameterDescription) {
@@ -1532,7 +1532,7 @@
1532
1532
  var markdownStructure = markdownToMarkdownStructure(promptbookString);
1533
1533
  var markdownStructureDeepness = countMarkdownStructureDeepness(markdownStructure);
1534
1534
  if (markdownStructureDeepness !== 2) {
1535
- throw new PromptbookSyntaxError(spaceTrim__default["default"]("\n Invalid markdown structure.\n The markdown must have exactly 2 levels of headings (one top-level section and one section for each template).\n Now it has ".concat(markdownStructureDeepness, " levels of headings.\n ")));
1535
+ throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid markdown structure.\n The markdown must have exactly 2 levels of headings (one top-level section and one section for each template).\n Now it has ".concat(markdownStructureDeepness, " levels of headings.\n ")));
1536
1536
  }
1537
1537
  promptbookJson.title = markdownStructure.title;
1538
1538
  // TODO: [1] DRY description
@@ -1541,7 +1541,7 @@
1541
1541
  description = description.split(/^```.*^```/gms).join('');
1542
1542
  //Note: Remove lists and return statement
1543
1543
  description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
1544
- description = spaceTrim__default["default"](description);
1544
+ description = spacetrim.spaceTrim(description);
1545
1545
  if (description === '') {
1546
1546
  description = undefined;
1547
1547
  }
@@ -1656,13 +1656,13 @@
1656
1656
  throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
1657
1657
  }
1658
1658
  else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
1659
- throw new PromptbookSyntaxError(spaceTrim__default["default"](function (block) { return "\n Script language ".concat(language, " is not supported.\n\n Supported languages are:\n ").concat(block(SUPPORTED_SCRIPT_LANGUAGES.join(', ')), "\n\n "); }));
1659
+ throw new PromptbookSyntaxError(spacetrim.spaceTrim(function (block) { return "\n Script language ".concat(language, " is not supported.\n\n Supported languages are:\n ").concat(block(SUPPORTED_SCRIPT_LANGUAGES.join(', ')), "\n\n "); }));
1660
1660
  }
1661
1661
  }
1662
1662
  var lastLine = section.content.split('\n').pop();
1663
1663
  var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
1664
1664
  if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
1665
- throw new PromptbookSyntaxError(spaceTrim__default["default"](function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
1665
+ throw new PromptbookSyntaxError(spacetrim.spaceTrim(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
1666
1666
  // TODO: Show code of invalid sections each time + DRY
1667
1667
  section.content
1668
1668
  .split('\n')
@@ -1676,7 +1676,7 @@
1676
1676
  description_1 = description_1.split(/^```.*^```/gms).join('');
1677
1677
  //Note: Remove lists and return statement
1678
1678
  description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
1679
- description_1 = spaceTrim__default["default"](description_1);
1679
+ description_1 = spacetrim.spaceTrim(description_1);
1680
1680
  if (description_1 === '') {
1681
1681
  description_1 = undefined;
1682
1682
  }
@@ -1824,12 +1824,12 @@
1824
1824
  if (!parameter.isInput &&
1825
1825
  !parameter.isOutput &&
1826
1826
  !promptbook.promptTemplates.some(function (template) { return template.dependentParameterNames.includes(parameter.name); })) {
1827
- throw new PromptbookLogicError(spaceTrim__default["default"]("\n Parameter {".concat(parameter.name, "} is created but not used\n\n You can declare {").concat(parameter.name, "} as output parameter by adding in the header:\n - OUTPUT PARAMETER `{").concat(parameter.name, "}` ").concat(parameter.description || '', "\n\n ")));
1827
+ throw new PromptbookLogicError(spacetrim.spaceTrim("\n Parameter {".concat(parameter.name, "} is created but not used\n\n You can declare {").concat(parameter.name, "} as output parameter by adding in the header:\n - OUTPUT PARAMETER `{").concat(parameter.name, "}` ").concat(parameter.description || '', "\n\n ")));
1828
1828
  }
1829
1829
  // Note: Testing that parameter is either input or result of some template
1830
1830
  if (!parameter.isInput &&
1831
1831
  !promptbook.promptTemplates.some(function (template) { return template.resultingParameterName === parameter.name; })) {
1832
- throw new PromptbookLogicError(spaceTrim__default["default"]("\n Parameter {".concat(parameter.name, "} is declared but not defined\n\n You can do one of these:\n - Remove declaration of {").concat(parameter.name, "}\n - Add prompt template that results in -> {").concat(parameter.name, "}\n\n ")));
1832
+ throw new PromptbookLogicError(spacetrim.spaceTrim("\n Parameter {".concat(parameter.name, "} is declared but not defined\n\n You can do one of these:\n - Remove declaration of {").concat(parameter.name, "}\n - Add prompt template that results in -> {").concat(parameter.name, "}\n\n ")));
1833
1833
  }
1834
1834
  };
1835
1835
  try {
@@ -1864,7 +1864,7 @@
1864
1864
  if (template.executionType === 'PROMPT_TEMPLATE' &&
1865
1865
  (template.modelRequirements.modelVariant === undefined ||
1866
1866
  template.modelRequirements.modelName === undefined)) {
1867
- throw new PromptbookLogicError(spaceTrim__default["default"]("\n\n You must specify MODEL VARIANT and MODEL NAME in the prompt template \"".concat(template.title, "\"\n\n For example:\n - MODEL VARIANT Chat\n - MODEL NAME `gpt-4-1106-preview`\n\n ")));
1867
+ throw new PromptbookLogicError(spacetrim.spaceTrim("\n\n You must specify MODEL VARIANT and MODEL NAME in the prompt template \"".concat(template.title, "\"\n\n For example:\n - MODEL VARIANT Chat\n - MODEL NAME `gpt-4-1106-preview`\n\n ")));
1868
1868
  }
1869
1869
  if (template.jokers && template.jokers.length > 0) {
1870
1870
  if (!template.expectFormat &&
@@ -1939,7 +1939,7 @@
1939
1939
  return template.dependentParameterNames.every(function (name) { return resovedParameters.includes(name); });
1940
1940
  });
1941
1941
  if (currentlyResovedTemplates.length === 0) {
1942
- throw new PromptbookLogicError(spaceTrim__default["default"](function (block) { return "\n\n Can not resolve some parameters\n It may be circular dependencies\n\n Can not resolve:\n ".concat(block(unresovedTemplates
1942
+ throw new PromptbookLogicError(spacetrim.spaceTrim(function (block) { return "\n\n Can not resolve some parameters\n It may be circular dependencies\n\n Can not resolve:\n ".concat(block(unresovedTemplates
1943
1943
  .map(function (_a) {
1944
1944
  var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
1945
1945
  return "- {".concat(resultingParameterName, "} depends on ").concat(dependentParameterNames
@@ -1986,6 +1986,7 @@
1986
1986
  return __awaiter(this, void 0, void 0, function () {
1987
1987
  var name, title, priority, prompt, chatThread, completionResult, result, resultString, expectError, scriptExecutionErrors, maxAttempts, jokers, attempt, isJokerAttempt, joker, _a, _b, _c, _d, scriptTools, error_2, e_2_1, _e, _f, functionName, postprocessingError, _g, _h, scriptTools, error_3, e_3_1, e_4_1, error_4;
1988
1988
  var e_2, _j, e_4, _k, e_3, _l, _m;
1989
+ var _this = this;
1989
1990
  return __generator(this, function (_o) {
1990
1991
  switch (_o.label) {
1991
1992
  case 0:
@@ -2055,6 +2056,67 @@
2055
2056
  content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
2056
2057
  modelRequirements: currentTemplate.modelRequirements,
2057
2058
  expectations: currentTemplate.expectations,
2059
+ postprocessing: (currentTemplate.postprocessing || []).map(function (functionName) { return function (result) { return __awaiter(_this, void 0, void 0, function () {
2060
+ var errors, _a, _b, scriptTools, error_5, e_5_1;
2061
+ var e_5, _c;
2062
+ return __generator(this, function (_d) {
2063
+ switch (_d.label) {
2064
+ case 0:
2065
+ errors = [];
2066
+ _d.label = 1;
2067
+ case 1:
2068
+ _d.trys.push([1, 8, 9, 10]);
2069
+ _a = __values(tools.script), _b = _a.next();
2070
+ _d.label = 2;
2071
+ case 2:
2072
+ if (!!_b.done) return [3 /*break*/, 7];
2073
+ scriptTools = _b.value;
2074
+ _d.label = 3;
2075
+ case 3:
2076
+ _d.trys.push([3, 5, , 6]);
2077
+ return [4 /*yield*/, scriptTools.execute({
2078
+ scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
2079
+ script: "".concat(functionName, "(result)"),
2080
+ parameters: {
2081
+ result: result || '',
2082
+ // Note: No ...parametersToPass, because working with result only
2083
+ },
2084
+ })];
2085
+ case 4: return [2 /*return*/, _d.sent()];
2086
+ case 5:
2087
+ error_5 = _d.sent();
2088
+ if (!(error_5 instanceof Error)) {
2089
+ throw error_5;
2090
+ }
2091
+ errors.push(error_5);
2092
+ return [3 /*break*/, 6];
2093
+ case 6:
2094
+ _b = _a.next();
2095
+ return [3 /*break*/, 2];
2096
+ case 7: return [3 /*break*/, 10];
2097
+ case 8:
2098
+ e_5_1 = _d.sent();
2099
+ e_5 = { error: e_5_1 };
2100
+ return [3 /*break*/, 10];
2101
+ case 9:
2102
+ try {
2103
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
2104
+ }
2105
+ finally { if (e_5) throw e_5.error; }
2106
+ return [7 /*endfinally*/];
2107
+ case 10:
2108
+ if (errors.length === 0) {
2109
+ throw new PromptbookExecutionError('Postprocessing in NaturalExecutionTools failed because no ScriptExecutionTools were provided');
2110
+ }
2111
+ else if (errors.length === 1) {
2112
+ throw errors[0];
2113
+ }
2114
+ else {
2115
+ throw new PromptbookExecutionError(spacetrim.spaceTrim(function (block) { return "\n Postprocessing in NaturalExecutionTools failed ".concat(errors.length, "x\n\n ").concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n\n')), "\n "); }));
2116
+ }
2117
+ }
2118
+ });
2119
+ }); }; }),
2058
2120
  };
2059
2121
  _b = currentTemplate.modelRequirements.modelVariant;
2060
2122
  switch (_b) {
@@ -2127,14 +2189,14 @@
2127
2189
  finally { if (e_2) throw e_2.error; }
2128
2190
  return [7 /*endfinally*/];
2129
2191
  case 23:
2130
- if (resultString) {
2192
+ if (resultString !== null) {
2131
2193
  return [3 /*break*/, 27];
2132
2194
  }
2133
2195
  if (scriptExecutionErrors.length === 1) {
2134
2196
  throw scriptExecutionErrors[0];
2135
2197
  }
2136
2198
  else {
2137
- throw new PromptbookExecutionError(spaceTrim__default["default"](function (block) { return "\n Script execution failed ".concat(scriptExecutionErrors.length, " times\n\n ").concat(block(scriptExecutionErrors
2199
+ throw new PromptbookExecutionError(spacetrim.spaceTrim(function (block) { return "\n Script execution failed ".concat(scriptExecutionErrors.length, " times\n\n ").concat(block(scriptExecutionErrors
2138
2200
  .map(function (error) { return '- ' + error.message; })
2139
2201
  .join('\n\n')), "\n "); }));
2140
2202
  }
@@ -2180,7 +2242,10 @@
2180
2242
  return [4 /*yield*/, scriptTools.execute({
2181
2243
  scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
2182
2244
  script: "".concat(functionName, "(resultString)"),
2183
- parameters: __assign(__assign({}, parametersToPass), { resultString: resultString || '' }),
2245
+ parameters: {
2246
+ resultString: resultString || '',
2247
+ // Note: No ...parametersToPass, because working with result only
2248
+ },
2184
2249
  })];
2185
2250
  case 33:
2186
2251
  resultString = _o.sent();
@@ -2271,7 +2336,7 @@
2271
2336
  return [7 /*endfinally*/];
2272
2337
  case 47:
2273
2338
  if (expectError !== null && attempt === maxAttempts - 1) {
2274
- throw new PromptbookExecutionError(spaceTrim__default["default"](function (block) { return "\n Natural execution failed ".concat(maxExecutionAttempts, "x\n\n Last error ").concat((expectError === null || expectError === void 0 ? void 0 : expectError.name) || '', ":\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n\n Last result:\n ").concat(resultString, "\n "); }));
2339
+ throw new PromptbookExecutionError(spacetrim.spaceTrim(function (block) { return "\n Natural execution failed ".concat(maxExecutionAttempts, "x\n\n ---\n Last error ").concat((expectError === null || expectError === void 0 ? void 0 : expectError.name) || '', ":\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n\n Last result:\n ").concat(resultString, "\n ---\n "); }));
2275
2340
  }
2276
2341
  _o.label = 48;
2277
2342
  case 48:
@@ -2340,7 +2405,7 @@
2340
2405
  return template.dependentParameterNames.every(function (name) { return resovedParameters_1.includes(name); });
2341
2406
  });
2342
2407
  if (!(!currentTemplate && resolving_1.length === 0)) return [3 /*break*/, 1];
2343
- throw new UnexpectedError(spaceTrim__default["default"]("\n Can not resolve some parameters\n\n Note: This should be catched during validatePromptbookJson\n "));
2408
+ throw new UnexpectedError(spacetrim.spaceTrim("\n Can not resolve some parameters\n\n Note: This should be catched during validatePromptbookJson\n "));
2344
2409
  case 1:
2345
2410
  if (!!currentTemplate) return [3 /*break*/, 3];
2346
2411
  /* [5] */ return [4 /*yield*/, Promise.race(resolving_1)];
@@ -2446,7 +2511,7 @@
2446
2511
  console.info('๐Ÿ’ฌ Mocked gptChat call');
2447
2512
  }
2448
2513
  return [2 /*return*/, {
2449
- content: spaceTrim__default["default"](function (block) { return "\n You said:\n ".concat(block(prompt.content), "\n "); }),
2514
+ content: spacetrim.spaceTrim(function (block) { return "\n You said:\n ".concat(block(prompt.content), "\n "); }),
2450
2515
  model: 'mocked-echo',
2451
2516
  timing: {
2452
2517
  start: getCurrentIsoDate(),
@@ -2475,7 +2540,7 @@
2475
2540
  console.info('๐Ÿ–‹ Mocked gptComplete call');
2476
2541
  }
2477
2542
  return [2 /*return*/, {
2478
- content: spaceTrim__default["default"](function (block) { return "\n ".concat(block(prompt.content), "\n And so on...\n "); }),
2543
+ content: spacetrim.spaceTrim(function (block) { return "\n ".concat(block(prompt.content), "\n And so on...\n "); }),
2479
2544
  model: 'mocked-echo',
2480
2545
  timing: {
2481
2546
  start: getCurrentIsoDate(),
@@ -2503,29 +2568,73 @@
2503
2568
  /**
2504
2569
  * Gets the expectations and creates a fake text that meets the expectations
2505
2570
  *
2571
+ * Note: You can provide postprocessing functions to modify the text before checking the expectations
2572
+ * The result will be the text BEFORE the postprocessing
2573
+ *
2506
2574
  * @private internal util for MockedFackedNaturalExecutionTools
2507
2575
  */
2508
- function $fakeTextToExpectations(expectations) {
2509
- var lorem = new loremIpsum.LoremIpsum({
2510
- wordsPerSentence: { min: 5, max: 15 },
2511
- sentencesPerParagraph: { min: 5, max: 15 },
2576
+ function $fakeTextToExpectations(expectations, postprocessing) {
2577
+ return __awaiter(this, void 0, void 0, function () {
2578
+ var lorem, loremText, text, loopLimit, textToCheck, _a, _b, func, e_1_1;
2579
+ var e_1, _c;
2580
+ return __generator(this, function (_d) {
2581
+ switch (_d.label) {
2582
+ case 0:
2583
+ lorem = new loremIpsum.LoremIpsum({
2584
+ wordsPerSentence: { min: 5, max: 15 },
2585
+ sentencesPerParagraph: { min: 5, max: 15 },
2586
+ });
2587
+ loremText = '';
2588
+ text = '';
2589
+ loopLimit = CHARACTER_LOOP_LIMIT;
2590
+ _d.label = 1;
2591
+ case 1:
2592
+ if (!(loopLimit-- > 0)) return [3 /*break*/, 11];
2593
+ textToCheck = text;
2594
+ _d.label = 2;
2595
+ case 2:
2596
+ _d.trys.push([2, 7, 8, 9]);
2597
+ _a = (e_1 = void 0, __values(postprocessing || [])), _b = _a.next();
2598
+ _d.label = 3;
2599
+ case 3:
2600
+ if (!!_b.done) return [3 /*break*/, 6];
2601
+ func = _b.value;
2602
+ return [4 /*yield*/, func(textToCheck)];
2603
+ case 4:
2604
+ textToCheck = _d.sent();
2605
+ _d.label = 5;
2606
+ case 5:
2607
+ _b = _a.next();
2608
+ return [3 /*break*/, 3];
2609
+ case 6: return [3 /*break*/, 9];
2610
+ case 7:
2611
+ e_1_1 = _d.sent();
2612
+ e_1 = { error: e_1_1 };
2613
+ return [3 /*break*/, 9];
2614
+ case 8:
2615
+ try {
2616
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
2617
+ }
2618
+ finally { if (e_1) throw e_1.error; }
2619
+ return [7 /*endfinally*/];
2620
+ case 9:
2621
+ if (isPassingExpectations(expectations, textToCheck)) {
2622
+ return [2 /*return*/, text]; // <- Note: Returning the text because the postprocessing
2623
+ }
2624
+ if (loremText === '') {
2625
+ loremText = lorem.generateParagraphs(1) + '\n\n';
2626
+ }
2627
+ text += loremText.substring(0, 1);
2628
+ loremText = loremText.substring(1);
2629
+ _d.label = 10;
2630
+ case 10: return [3 /*break*/, 1];
2631
+ case 11: throw new Error(spacetrim.spaceTrim(function (block) { return "\n Can not generate fake text to met the expectations\n\n Loop limit reached\n The expectations:\n ".concat(block(JSON.stringify(expectations, null, 4)), "\n\n The draft text:\n ").concat(block(text), "\n\n "); }));
2632
+ }
2633
+ });
2512
2634
  });
2513
- var loremText = '';
2514
- var text = '';
2515
- for (var loopLimit = LOOP_LIMIT; loopLimit-- > 0;) {
2516
- if (isPassingExpectations(expectations, text)) {
2517
- return text;
2518
- }
2519
- if (loremText === '') {
2520
- loremText = lorem.generateParagraphs(1) + '\n\n';
2521
- }
2522
- text += loremText.substring(0, 1);
2523
- loremText = loremText.substring(1);
2524
- }
2525
- throw new Error(spaceTrim__default["default"](function (block) { return "\n Can not generate fake text to met the expectations\n\n Loop limit reached\n The expectations:\n ".concat(block(JSON.stringify(expectations, null, 4)), "\n\n The draft text:\n ").concat(block(text), "\n\n "); }));
2526
2635
  }
2527
2636
  /**
2528
- * TODO: Implement better
2637
+ * TODO: Implement better - create FakeLLM from this
2529
2638
  * TODO: [๐Ÿ’] Unite object for expecting amount and format - use here also a format
2530
2639
  */
2531
2640
 
@@ -2541,29 +2650,40 @@
2541
2650
  */
2542
2651
  MockedFackedNaturalExecutionTools.prototype.gptChat = function (prompt) {
2543
2652
  return __awaiter(this, void 0, void 0, function () {
2653
+ var content, result;
2544
2654
  return __generator(this, function (_a) {
2545
- if (this.options.isVerbose) {
2546
- console.info('๐Ÿ’ฌ Mocked faked call');
2655
+ switch (_a.label) {
2656
+ case 0:
2657
+ if (this.options.isVerbose) {
2658
+ console.info('๐Ÿ’ฌ Mocked faked prompt', prompt);
2659
+ }
2660
+ return [4 /*yield*/, $fakeTextToExpectations(prompt.expectations || {
2661
+ sentences: { min: 1, max: 1 },
2662
+ }, prompt.postprocessing)];
2663
+ case 1:
2664
+ content = _a.sent();
2665
+ result = {
2666
+ content: content,
2667
+ model: 'mocked-facked',
2668
+ timing: {
2669
+ start: getCurrentIsoDate(),
2670
+ complete: getCurrentIsoDate(),
2671
+ },
2672
+ usage: {
2673
+ price: 0,
2674
+ inputTokens: 0,
2675
+ outputTokens: 0,
2676
+ },
2677
+ rawResponse: {
2678
+ note: 'This is mocked echo',
2679
+ },
2680
+ // <- [๐Ÿคนโ€โ™‚๏ธ]
2681
+ };
2682
+ if (this.options.isVerbose) {
2683
+ console.info('๐Ÿ’ฌ Mocked faked result', result);
2684
+ }
2685
+ return [2 /*return*/, result];
2547
2686
  }
2548
- return [2 /*return*/, {
2549
- content: $fakeTextToExpectations(prompt.expectations || {
2550
- sentences: { min: 1, max: 1 },
2551
- }),
2552
- model: 'mocked-facked',
2553
- timing: {
2554
- start: getCurrentIsoDate(),
2555
- complete: getCurrentIsoDate(),
2556
- },
2557
- usage: {
2558
- price: 0,
2559
- inputTokens: 0,
2560
- outputTokens: 0,
2561
- },
2562
- rawResponse: {
2563
- note: 'This is mocked echo',
2564
- },
2565
- // <- [๐Ÿคนโ€โ™‚๏ธ]
2566
- }];
2567
2687
  });
2568
2688
  });
2569
2689
  };
@@ -2600,7 +2720,7 @@
2600
2720
  case 1:
2601
2721
  answer = _a.sent();
2602
2722
  if (this.options.isVerbose) {
2603
- console.info(spaceTrim__default["default"](function (block) { return "\n \uD83D\uDCD6 ".concat(block(options.promptTitle), "\n \uD83D\uDC64 ").concat(block(answer), "\n "); }));
2723
+ console.info(spacetrim.spaceTrim(function (block) { return "\n \uD83D\uDCD6 ".concat(block(options.promptTitle), "\n \uD83D\uDC64 ").concat(block(answer), "\n "); }));
2604
2724
  }
2605
2725
  return [2 /*return*/, answer];
2606
2726
  }
@@ -2627,9 +2747,9 @@
2627
2747
  return __awaiter(this, void 0, void 0, function () {
2628
2748
  var answer;
2629
2749
  return __generator(this, function (_a) {
2630
- answer = window.prompt(spaceTrim__default["default"](function (block) { return "\n ".concat(block(options.promptTitle), "\n\n ").concat(block(options.promptMessage), "\n "); }));
2750
+ answer = window.prompt(spacetrim.spaceTrim(function (block) { return "\n ".concat(block(options.promptTitle), "\n\n ").concat(block(options.promptMessage), "\n "); }));
2631
2751
  if (this.options.isVerbose) {
2632
- console.info(spaceTrim__default["default"](function (block) { return "\n \uD83D\uDCD6 ".concat(block(options.promptTitle), "\n \uD83D\uDC64 ").concat(block(answer || '๐Ÿšซ User cancelled prompt'), "\n "); }));
2752
+ console.info(spacetrim.spaceTrim(function (block) { return "\n \uD83D\uDCD6 ".concat(block(options.promptTitle), "\n \uD83D\uDC64 ").concat(block(answer || '๐Ÿšซ User cancelled prompt'), "\n "); }));
2633
2753
  }
2634
2754
  if (answer === null) {
2635
2755
  throw new PromptbookExecutionError('User cancelled prompt');
@@ -2694,7 +2814,7 @@
2694
2814
  for (var promptbooks_1 = __values(promptbooks), promptbooks_1_1 = promptbooks_1.next(); !promptbooks_1_1.done; promptbooks_1_1 = promptbooks_1.next()) {
2695
2815
  var promptbook = promptbooks_1_1.value;
2696
2816
  if (promptbook.promptbookUrl === undefined) {
2697
- throw new PromptbookReferenceError(spaceTrim__default["default"]("\n Promptbook with name \"".concat(promptbook.title, "\" does not have defined URL\n\n Note: Promptbooks without URLs are called anonymous promptbooks\n They can be used as standalone promptbooks, but they cannot be referenced by other promptbooks\n And also they cannot be used in the promptbook library\n\n ")));
2817
+ throw new PromptbookReferenceError(spacetrim.spaceTrim("\n Promptbook with name \"".concat(promptbook.title, "\" does not have defined URL\n\n Note: Promptbooks without URLs are called anonymous promptbooks\n They can be used as standalone promptbooks, but they cannot be referenced by other promptbooks\n And also they cannot be used in the promptbook library\n\n ")));
2698
2818
  }
2699
2819
  validatePromptbookJson(promptbook);
2700
2820
  this.library.set(promptbook.promptbookUrl, promptbook);
@@ -2723,7 +2843,7 @@
2723
2843
  var _this = this;
2724
2844
  var promptbook = this.library.get(url);
2725
2845
  if (!promptbook) {
2726
- throw new PromptbookNotFoundError(spaceTrim__default["default"](function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPromptbooks()
2846
+ throw new PromptbookNotFoundError(spacetrim.spaceTrim(function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPromptbooks()
2727
2847
  .map(function (promptbookUrl) { return "- ".concat(promptbookUrl); })
2728
2848
  .join('\n')), "\n\n "); }));
2729
2849
  }
@@ -2874,7 +2994,7 @@
2874
2994
  case 0:
2875
2995
  if (!!predicate(url)) return [3 /*break*/, 2];
2876
2996
  _a = PromptbookNotFoundError.bind;
2877
- return [4 /*yield*/, spaceTrim__default["default"](function (block) { return __awaiter(_this, void 0, void 0, function () {
2997
+ return [4 /*yield*/, spacetrim.spaceTrim(function (block) { return __awaiter(_this, void 0, void 0, function () {
2878
2998
  var _a, _b, _c, _d, _e, _f;
2879
2999
  return __generator(this, function (_g) {
2880
3000
  switch (_g.label) {