@promptbook/utils 0.52.0-3 → 0.52.0-30

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 (52) hide show
  1. package/README.md +1 -3
  2. package/esm/index.es.js +559 -1597
  3. package/esm/index.es.js.map +1 -1
  4. package/esm/typings/_packages/core.index.d.ts +15 -6
  5. package/esm/typings/_packages/utils.index.d.ts +5 -8
  6. package/esm/typings/execution/ExecutionTools.d.ts +5 -3
  7. package/esm/typings/execution/plugins/llm-execution-tools/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +0 -1
  8. package/esm/typings/execution/plugins/llm-execution-tools/azure-openai/AzureOpenAiExecutionTools.d.ts +0 -1
  9. package/esm/typings/execution/plugins/llm-execution-tools/langtail/LangtailExecutionTools.d.ts +0 -3
  10. package/esm/typings/execution/plugins/llm-execution-tools/openai/OpenAiExecutionTools.d.ts +1 -1
  11. package/esm/typings/execution/plugins/llm-execution-tools/remote/RemoteLlmExecutionTools.d.ts +1 -1
  12. package/esm/typings/execution/plugins/script-execution-tools/javascript/JavascriptEvalExecutionTools.d.ts +1 -1
  13. package/esm/typings/execution/plugins/script-execution-tools/javascript/JavascriptExecutionTools.d.ts +4 -17
  14. package/esm/typings/execution/utils/forEachAsync.d.ts +18 -0
  15. package/esm/typings/library/constructors/createPromptbookLibraryFromDirectory.d.ts +36 -4
  16. package/esm/typings/library/constructors/createPromptbookLibraryFromDirectory.test.d.ts +1 -0
  17. package/esm/typings/library/constructors/createPromptbookLibraryFromPromise.d.ts +2 -2
  18. package/esm/typings/library/constructors/createPromptbookLibraryFromPromise.test.d.ts +1 -0
  19. package/esm/typings/library/constructors/createPromptbookLibraryFromSources.test.d.ts +1 -0
  20. package/esm/typings/library/constructors/createPromptbookLibraryFromUrl.d.ts +21 -5
  21. package/esm/typings/library/constructors/justTestFsImport.d.ts +7 -0
  22. package/esm/typings/types/Prompt.d.ts +1 -1
  23. package/esm/typings/types/typeAliases.d.ts +2 -2
  24. package/package.json +2 -4
  25. package/umd/index.umd.js +563 -1608
  26. package/umd/index.umd.js.map +1 -1
  27. package/umd/typings/_packages/core.index.d.ts +15 -6
  28. package/umd/typings/_packages/utils.index.d.ts +5 -8
  29. package/umd/typings/execution/ExecutionTools.d.ts +5 -3
  30. package/umd/typings/execution/plugins/llm-execution-tools/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +0 -1
  31. package/umd/typings/execution/plugins/llm-execution-tools/azure-openai/AzureOpenAiExecutionTools.d.ts +0 -1
  32. package/umd/typings/execution/plugins/llm-execution-tools/langtail/LangtailExecutionTools.d.ts +0 -3
  33. package/umd/typings/execution/plugins/llm-execution-tools/openai/OpenAiExecutionTools.d.ts +1 -1
  34. package/umd/typings/execution/plugins/llm-execution-tools/remote/RemoteLlmExecutionTools.d.ts +1 -1
  35. package/umd/typings/execution/plugins/script-execution-tools/javascript/JavascriptEvalExecutionTools.d.ts +1 -1
  36. package/umd/typings/execution/plugins/script-execution-tools/javascript/JavascriptExecutionTools.d.ts +4 -17
  37. package/umd/typings/execution/utils/forEachAsync.d.ts +18 -0
  38. package/umd/typings/library/constructors/createPromptbookLibraryFromDirectory.d.ts +36 -4
  39. package/umd/typings/library/constructors/createPromptbookLibraryFromDirectory.test.d.ts +1 -0
  40. package/umd/typings/library/constructors/createPromptbookLibraryFromPromise.d.ts +2 -2
  41. package/umd/typings/library/constructors/createPromptbookLibraryFromPromise.test.d.ts +1 -0
  42. package/umd/typings/library/constructors/createPromptbookLibraryFromSources.test.d.ts +1 -0
  43. package/umd/typings/library/constructors/createPromptbookLibraryFromUrl.d.ts +21 -5
  44. package/umd/typings/library/constructors/justTestFsImport.d.ts +7 -0
  45. package/umd/typings/types/Prompt.d.ts +1 -1
  46. package/umd/typings/types/typeAliases.d.ts +2 -2
  47. package/esm/typings/_packages/wizzard.index.d.ts +0 -5
  48. package/esm/typings/wizzard/Wizzard.d.ts +0 -4
  49. package/esm/typings/wizzard/sample.d.ts +0 -6
  50. package/umd/typings/_packages/wizzard.index.d.ts +0 -5
  51. package/umd/typings/wizzard/Wizzard.d.ts +0 -4
  52. package/umd/typings/wizzard/sample.d.ts +0 -6
package/umd/index.umd.js CHANGED
@@ -1,13 +1,8 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('spacetrim'), require('prettier'), require('prettier/parser-html'), require('moment')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'spacetrim', 'prettier', 'prettier/parser-html', 'moment'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-utils"] = {}, global.spacetrim, global.prettier, global.parserHtml, global.moment));
5
- })(this, (function (exports, spacetrim, prettier, parserHtml, moment) { 'use strict';
6
-
7
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
-
9
- var parserHtml__default = /*#__PURE__*/_interopDefaultLegacy(parserHtml);
10
- var moment__default = /*#__PURE__*/_interopDefaultLegacy(moment);
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('spacetrim')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'spacetrim'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-utils"] = {}, global.spacetrim));
5
+ })(this, (function (exports, spacetrim) { 'use strict';
11
6
 
12
7
  /*! *****************************************************************************
13
8
  Copyright (c) Microsoft Corporation.
@@ -51,6 +46,44 @@
51
46
  return __assign.apply(this, arguments);
52
47
  };
53
48
 
49
+ function __awaiter(thisArg, _arguments, P, generator) {
50
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
51
+ return new (P || (P = Promise))(function (resolve, reject) {
52
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
53
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
54
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
55
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
56
+ });
57
+ }
58
+
59
+ function __generator(thisArg, body) {
60
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
61
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
62
+ function verb(n) { return function (v) { return step([n, v]); }; }
63
+ function step(op) {
64
+ if (f) throw new TypeError("Generator is already executing.");
65
+ while (_) try {
66
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
67
+ if (y = 0, t) op = [op[0] & 2, t.value];
68
+ switch (op[0]) {
69
+ case 0: case 1: t = op; break;
70
+ case 4: _.label++; return { value: op[1], done: false };
71
+ case 5: _.label++; y = op[1]; op = [0]; continue;
72
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
73
+ default:
74
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
75
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
76
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
77
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
78
+ if (t[2]) _.ops.pop();
79
+ _.trys.pop(); continue;
80
+ }
81
+ op = body.call(thisArg, _);
82
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
83
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
84
+ }
85
+ }
86
+
54
87
  function __values(o) {
55
88
  var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
56
89
  if (m) return m.call(o);
@@ -91,496 +124,241 @@
91
124
  }
92
125
 
93
126
  /**
94
- * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
127
+ * This error type indicates that the error should not happen and its last check before crashing with some other error
95
128
  */
96
- var PromptbookSyntaxError = /** @class */ (function (_super) {
97
- __extends(PromptbookSyntaxError, _super);
98
- function PromptbookSyntaxError(message) {
99
- var _this = _super.call(this, message) || this;
100
- _this.name = 'PromptbookSyntaxError';
101
- Object.setPrototypeOf(_this, PromptbookSyntaxError.prototype);
129
+ var UnexpectedError = /** @class */ (function (_super) {
130
+ __extends(UnexpectedError, _super);
131
+ function UnexpectedError(message) {
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;
133
+ _this.name = 'UnexpectedError';
134
+ Object.setPrototypeOf(_this, UnexpectedError.prototype);
102
135
  return _this;
103
136
  }
104
- return PromptbookSyntaxError;
137
+ return UnexpectedError;
105
138
  }(Error));
106
139
 
107
140
  /**
108
- * Supported script languages
141
+ * Removes emojis from a string and fix whitespaces
142
+ *
143
+ * @param text with emojis
144
+ * @returns text without emojis
145
+ */
146
+ function removeEmojis(text) {
147
+ // Replace emojis (and also ZWJ sequence) with hyphens
148
+ text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
149
+ text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
150
+ text = text.replace(/(\p{Extended_Pictographic})(\u{200D}\p{Extended_Pictographic})*/gu, '$1');
151
+ text = text.replace(/\p{Extended_Pictographic}/gu, '');
152
+ return text;
153
+ }
154
+
155
+ /**
156
+ * Function normalizes title to name which can be used as identifier
157
+ */
158
+ function titleToName(value) {
159
+ value = removeEmojis(value);
160
+ value = normalizeToKebabCase(value);
161
+ // TODO: [🧠] Maybe warn or add some padding to short name which are not good identifiers
162
+ return value;
163
+ }
164
+
165
+ /**
166
+ * Creates a Mermaid graph based on the promptbook
167
+ *
168
+ * Note: The result is not wrapped in a Markdown code block
169
+ */
170
+ function renderPromptbookMermaid(promptbookJson, options) {
171
+ var _a = (options || {}).linkPromptTemplate, linkPromptTemplate = _a === void 0 ? function () { return null; } : _a;
172
+ var parameterNameToTemplateName = function (parameterName) {
173
+ var parameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
174
+ if (!parameter) {
175
+ throw new UnexpectedError("Could not find {".concat(parameterName, "}"));
176
+ }
177
+ if (parameter.isInput) {
178
+ return 'input';
179
+ }
180
+ var template = promptbookJson.promptTemplates.find(function (template) { return template.resultingParameterName === parameterName; });
181
+ if (!template) {
182
+ throw new Error("Could not find template for {".concat(parameterName, "}"));
183
+ }
184
+ return normalizeTo_camelCase('template-' + titleToName(template.title));
185
+ };
186
+ var promptbookMermaid = spacetrim.spaceTrim(function (block) { return "\n\n %% \uD83D\uDD2E Tip: Open this on GitHub or in the VSCode website to see the Mermaid graph visually\n\n flowchart LR\n subgraph \"".concat(promptbookJson.title, "\"\n\n direction TB\n\n input((Input)):::input\n ").concat(block(promptbookJson.promptTemplates
187
+ .flatMap(function (_a) {
188
+ var title = _a.title, dependentParameterNames = _a.dependentParameterNames, resultingParameterName = _a.resultingParameterName;
189
+ return __spreadArray([
190
+ "".concat(parameterNameToTemplateName(resultingParameterName), "(\"").concat(title, "\")")
191
+ ], __read(dependentParameterNames.map(function (dependentParameterName) {
192
+ return "".concat(parameterNameToTemplateName(dependentParameterName), "--\"{").concat(dependentParameterName, "}\"-->").concat(parameterNameToTemplateName(resultingParameterName));
193
+ })), false);
194
+ })
195
+ .join('\n')), "\n\n ").concat(block(promptbookJson.parameters
196
+ .filter(function (_a) {
197
+ var isOutput = _a.isOutput;
198
+ return isOutput;
199
+ })
200
+ .map(function (_a) {
201
+ var name = _a.name;
202
+ return "".concat(parameterNameToTemplateName(name), "--\"{").concat(name, "}\"-->output");
203
+ })
204
+ .join('\n')), "\n output((Output)):::output\n\n ").concat(block(promptbookJson.promptTemplates
205
+ .map(function (promptTemplate) {
206
+ var link = linkPromptTemplate(promptTemplate);
207
+ if (link === null) {
208
+ return '';
209
+ }
210
+ var href = link.href, title = link.title;
211
+ var templateName = parameterNameToTemplateName(promptTemplate.resultingParameterName);
212
+ return "click ".concat(templateName, " href \"").concat(href, "\" \"").concat(title, "\";");
213
+ })
214
+ .filter(function (line) { return line !== ''; })
215
+ .join('\n')), "\n\n classDef input color: grey;\n classDef output color: grey;\n\n end;\n\n "); });
216
+ return promptbookMermaid;
217
+ }
218
+ /**
219
+ * TODO: Maybe use some Mermaid library instead of string templating
220
+ * TODO: [🕌] When more than 2 functionalities, split into separate functions
109
221
  */
110
- var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
111
222
 
112
223
  /**
113
- * Computes the deepness of the markdown structure.
224
+ * Parses the template and returns the list of all parameter names
114
225
  *
115
- * @private within the library
226
+ * @param template the template with parameters in {curly} braces
227
+ * @returns the list of parameter names
116
228
  */
117
- function countMarkdownStructureDeepness(markdownStructure) {
229
+ function extractParameters(template) {
118
230
  var e_1, _a;
119
- var maxDeepness = 0;
231
+ var matches = template.matchAll(/{\w+}/g);
232
+ var parameterNames = new Set();
120
233
  try {
121
- for (var _b = __values(markdownStructure.sections), _c = _b.next(); !_c.done; _c = _b.next()) {
122
- var section = _c.value;
123
- maxDeepness = Math.max(maxDeepness, countMarkdownStructureDeepness(section));
234
+ for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
235
+ var match = matches_1_1.value;
236
+ var parameterName = match[0].slice(1, -1);
237
+ parameterNames.add(parameterName);
124
238
  }
125
239
  }
126
240
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
127
241
  finally {
128
242
  try {
129
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
243
+ if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
130
244
  }
131
245
  finally { if (e_1) throw e_1.error; }
132
246
  }
133
- return maxDeepness + 1;
247
+ return parameterNames;
134
248
  }
135
249
 
136
250
  /**
137
- * The maximum number of iterations for a loops
138
- */
139
- var LOOP_LIMIT = 1000;
140
-
141
- /**
142
- * This error type indicates that the error should not happen and its last check before crashing with some other error
251
+ * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
143
252
  */
144
- var UnexpectedError = /** @class */ (function (_super) {
145
- __extends(UnexpectedError, _super);
146
- function UnexpectedError(message) {
147
- 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;
148
- _this.name = 'UnexpectedError';
149
- Object.setPrototypeOf(_this, UnexpectedError.prototype);
253
+ var PromptbookSyntaxError = /** @class */ (function (_super) {
254
+ __extends(PromptbookSyntaxError, _super);
255
+ function PromptbookSyntaxError(message) {
256
+ var _this = _super.call(this, message) || this;
257
+ _this.name = 'PromptbookSyntaxError';
258
+ Object.setPrototypeOf(_this, PromptbookSyntaxError.prototype);
150
259
  return _this;
151
260
  }
152
- return UnexpectedError;
261
+ return PromptbookSyntaxError;
153
262
  }(Error));
154
263
 
155
264
  /**
156
- * Parse a markdown string into a MarkdownStructure object.
157
- *
158
- * Note: This function does work with code blocks
159
- * Note: This function does not work with markdown comments
160
- *
161
- * @param markdown The markdown string to parse.
162
- * @returns The MarkdownStructure object.
265
+ * Parses the given script and returns the list of all used variables that are not defined in the script
163
266
  *
164
- * @private within the library
267
+ * @param script from which to extract the variables
268
+ * @returns the list of variable names
269
+ * @throws {PromptbookSyntaxError} if the script is invalid
165
270
  */
166
- function markdownToMarkdownStructure(markdown) {
167
- var e_1, _a;
168
- var lines = markdown.split('\n');
169
- var root = { level: 0, title: '', contentLines: [], sections: [], parent: null };
170
- var current = root;
171
- var isInsideCodeBlock = false;
271
+ function extractVariables(script) {
272
+ var variables = new Set();
273
+ script = "(()=>{".concat(script, "})()");
172
274
  try {
173
- for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
174
- var line = lines_1_1.value;
175
- var headingMatch = line.match(/^(?<mark>#{1,6})\s(?<title>.*)/);
176
- if (isInsideCodeBlock || !headingMatch) {
177
- if (line.startsWith('```')) {
178
- isInsideCodeBlock = !isInsideCodeBlock;
179
- }
180
- current.contentLines.push(line);
275
+ for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
276
+ try {
277
+ eval(script);
181
278
  }
182
- else {
183
- var level = headingMatch.groups.mark.length;
184
- var title = headingMatch.groups.title.trim();
185
- var parent_1 = void 0;
186
- if (level > current.level) {
187
- // Note: Going deeper (next section is child of current)
188
- parent_1 = current;
279
+ catch (error) {
280
+ if (!(error instanceof ReferenceError)) {
281
+ throw error;
282
+ }
283
+ var undefinedName = error.message.split(' ')[0];
284
+ /*
285
+ Note: Remapping error
286
+ From: [ReferenceError: thing is not defined],
287
+ To: [Error: Parameter {thing} is not defined],
288
+ */
289
+ if (!undefinedName) {
290
+ throw error;
291
+ }
292
+ if (script.includes(undefinedName + '(')) {
293
+ script = "const ".concat(undefinedName, " = ()=>'';") + script;
189
294
  }
190
295
  else {
191
- // Note: Going up or staying at the same level (next section is sibling or parent or grandparent,... of current)
192
- parent_1 = current;
193
- var loopLimit = LOOP_LIMIT;
194
- while (parent_1.level !== level - 1) {
195
- if (loopLimit-- < 0) {
196
- throw new UnexpectedError('Loop limit reached during parsing of markdown structure in `markdownToMarkdownStructure`');
197
- }
198
- if (parent_1.parent === null /* <- Note: We are in root */) {
199
- // [🌻]
200
- throw new Error(spacetrim.spaceTrim("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
201
- }
202
- parent_1 = parent_1.parent;
203
- }
296
+ variables.add(undefinedName);
297
+ script = "const ".concat(undefinedName, " = '';") + script;
204
298
  }
205
- var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
206
- parent_1.sections.push(section);
207
- current = section;
208
299
  }
209
- }
210
300
  }
211
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
212
- finally {
213
- try {
214
- if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
301
+ catch (error) {
302
+ if (!(error instanceof Error)) {
303
+ throw error;
215
304
  }
216
- finally { if (e_1) throw e_1.error; }
217
- }
218
- if (root.sections.length === 1) {
219
- var markdownStructure = parsingMarkdownStructureToMarkdownStructure(root.sections[0]);
220
- return markdownStructure;
305
+ 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 "); }));
221
306
  }
222
- // [🌻]
223
- throw new Error('The markdown file must have exactly one top-level section.');
224
- // return root;
307
+ return variables;
225
308
  }
226
309
  /**
227
- * @private
310
+ * TODO: [🔣] Support for multiple languages - python, java,...
228
311
  */
229
- function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
230
- var level = parsingMarkdownStructure.level, title = parsingMarkdownStructure.title, contentLines = parsingMarkdownStructure.contentLines, sections = parsingMarkdownStructure.sections;
231
- return {
232
- level: level,
233
- title: title,
234
- content: spacetrim.spaceTrim(contentLines.join('\n')),
235
- sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
236
- };
237
- }
238
312
 
239
313
  /**
240
- * Utility function to extract all list items from markdown
241
- *
242
- * Note: It works with both ul and ol
243
- * Note: It omits list items in code blocks
244
- * Note: It flattens nested lists
245
- * Note: It can not work with html syntax and comments
314
+ * Parses the prompt template and returns the set of all used parameters
246
315
  *
247
- * @param markdown any valid markdown
248
- * @returns
316
+ * @param promptTemplate the template with used parameters
317
+ * @returns the set of parameter names
318
+ * @throws {PromptbookSyntaxError} if the script is invalid
249
319
  */
250
- function extractAllListItemsFromMarkdown(markdown) {
251
- var e_1, _a;
252
- var lines = markdown.split('\n');
253
- var listItems = [];
254
- var isInCodeBlock = false;
320
+ function extractParametersFromPromptTemplate(promptTemplate) {
321
+ var e_1, _a, e_2, _b;
322
+ var parameterNames = new Set();
255
323
  try {
256
- for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
257
- var line = lines_1_1.value;
258
- var trimmedLine = line.trim();
259
- if (trimmedLine.startsWith('```')) {
260
- isInCodeBlock = !isInCodeBlock;
261
- }
262
- if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
263
- var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
264
- listItems.push(listItem);
265
- }
324
+ for (var _c = __values(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameters(promptTemplate.title)), false), __read(extractParameters(promptTemplate.description || '')), false), __read(extractParameters(promptTemplate.content)), false)), _d = _c.next(); !_d.done; _d = _c.next()) {
325
+ var parameterName = _d.value;
326
+ parameterNames.add(parameterName);
266
327
  }
267
328
  }
268
329
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
269
330
  finally {
270
331
  try {
271
- if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
332
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
272
333
  }
273
334
  finally { if (e_1) throw e_1.error; }
274
335
  }
275
- return listItems;
336
+ if (promptTemplate.executionType === 'SCRIPT') {
337
+ try {
338
+ for (var _e = __values(extractVariables(promptTemplate.content)), _f = _e.next(); !_f.done; _f = _e.next()) {
339
+ var parameterName = _f.value;
340
+ parameterNames.add(parameterName);
341
+ }
342
+ }
343
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
344
+ finally {
345
+ try {
346
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
347
+ }
348
+ finally { if (e_2) throw e_2.error; }
349
+ }
350
+ }
351
+ return parameterNames;
276
352
  }
277
-
278
353
  /**
279
- * Makes first letter of a string uppercase
280
- *
354
+ * TODO: [🔣] If script require contentLanguage
281
355
  */
282
- function capitalize(word) {
283
- return word.substring(0, 1).toUpperCase() + word.substring(1);
284
- }
285
356
 
286
357
  /**
287
- * Extracts all code blocks from markdown.
358
+ * Function parseNumber will parse number from string
288
359
  *
289
- * Note: There are 3 simmilar function:
290
- * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
291
- * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
292
- * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
293
- *
294
- * @param markdown any valid markdown
295
- * @returns code blocks with language and content
296
- *
297
- */
298
- function extractAllBlocksFromMarkdown(markdown) {
299
- var e_1, _a;
300
- var codeBlocks = [];
301
- var lines = markdown.split('\n');
302
- var currentCodeBlock = null;
303
- try {
304
- for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
305
- var line = lines_1_1.value;
306
- if (line.startsWith('```')) {
307
- var language = line.slice(3).trim() || null;
308
- if (currentCodeBlock === null) {
309
- currentCodeBlock = { language: language, content: '' };
310
- }
311
- else {
312
- if (language !== null) {
313
- // [🌻]
314
- throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
315
- }
316
- codeBlocks.push(currentCodeBlock);
317
- currentCodeBlock = null;
318
- }
319
- }
320
- else if (currentCodeBlock !== null) {
321
- if (currentCodeBlock.content !== '') {
322
- currentCodeBlock.content += '\n';
323
- }
324
- currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
325
- }
326
- }
327
- }
328
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
329
- finally {
330
- try {
331
- if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
332
- }
333
- finally { if (e_1) throw e_1.error; }
334
- }
335
- if (currentCodeBlock !== null) {
336
- // [🌻]
337
- throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
338
- }
339
- return codeBlocks;
340
- }
341
-
342
- /**
343
- * Extracts exactly ONE code block from markdown.
344
- *
345
- * Note: If there are multiple or no code blocks the function throws an error
346
- *
347
- * Note: There are 3 simmilar function:
348
- * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
349
- * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
350
- * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
351
- *
352
- * @param markdown any valid markdown
353
- * @returns code block with language and content
354
- */
355
- function extractOneBlockFromMarkdown(markdown) {
356
- var codeBlocks = extractAllBlocksFromMarkdown(markdown);
357
- if (codeBlocks.length !== 1) {
358
- // TODO: Report more specific place where the error happened
359
- throw new Error(/* <- [🌻] */ 'There should be exactly one code block in the markdown');
360
- }
361
- return codeBlocks[0];
362
- }
363
- /***
364
- * TODO: [🍓][🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
365
- */
366
-
367
- /**
368
- * Removes HTML or Markdown comments from a string.
369
- *
370
- * @param {string} content - The string to remove comments from.
371
- * @returns {string} The input string with all comments removed.
372
- */
373
- function removeContentComments(content) {
374
- return spacetrim.spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
375
- }
376
-
377
- /**
378
- * Creates a new set with all elements that are present in either set
379
- */
380
- function union() {
381
- var e_1, _a, e_2, _b;
382
- var sets = [];
383
- for (var _i = 0; _i < arguments.length; _i++) {
384
- sets[_i] = arguments[_i];
385
- }
386
- var union = new Set();
387
- try {
388
- for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
389
- var set = sets_1_1.value;
390
- try {
391
- for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
392
- var item = _d.value;
393
- union.add(item);
394
- }
395
- }
396
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
397
- finally {
398
- try {
399
- if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
400
- }
401
- finally { if (e_2) throw e_2.error; }
402
- }
403
- }
404
- }
405
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
406
- finally {
407
- try {
408
- if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
409
- }
410
- finally { if (e_1) throw e_1.error; }
411
- }
412
- return union;
413
- }
414
-
415
- /**
416
- * The version of the Promptbook library
417
- */
418
- var PROMPTBOOK_VERSION = '0.52.0-2';
419
-
420
- /**
421
- * Parses the template and returns the list of all parameter names
422
- *
423
- * @param template the template with parameters in {curly} braces
424
- * @returns the list of parameter names
425
- */
426
- function extractParameters(template) {
427
- var e_1, _a;
428
- var matches = template.matchAll(/{\w+}/g);
429
- var parameterNames = new Set();
430
- try {
431
- for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
432
- var match = matches_1_1.value;
433
- var parameterName = match[0].slice(1, -1);
434
- parameterNames.add(parameterName);
435
- }
436
- }
437
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
438
- finally {
439
- try {
440
- if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
441
- }
442
- finally { if (e_1) throw e_1.error; }
443
- }
444
- return parameterNames;
445
- }
446
-
447
- /**
448
- * Parses the given script and returns the list of all used variables that are not defined in the script
449
- *
450
- * @param script from which to extract the variables
451
- * @returns the list of variable names
452
- * @throws {PromptbookSyntaxError} if the script is invalid
453
- */
454
- function extractVariables(script) {
455
- var variables = new Set();
456
- script = "(()=>{".concat(script, "})()");
457
- try {
458
- for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
459
- try {
460
- eval(script);
461
- }
462
- catch (error) {
463
- if (!(error instanceof ReferenceError)) {
464
- throw error;
465
- }
466
- var undefinedName = error.message.split(' ')[0];
467
- /*
468
- Note: Remapping error
469
- From: [ReferenceError: thing is not defined],
470
- To: [Error: Parameter {thing} is not defined],
471
- */
472
- if (!undefinedName) {
473
- throw error;
474
- }
475
- if (script.includes(undefinedName + '(')) {
476
- script = "const ".concat(undefinedName, " = ()=>'';") + script;
477
- }
478
- else {
479
- variables.add(undefinedName);
480
- script = "const ".concat(undefinedName, " = '';") + script;
481
- }
482
- }
483
- }
484
- catch (error) {
485
- if (!(error instanceof Error)) {
486
- throw error;
487
- }
488
- 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 "); }));
489
- }
490
- return variables;
491
- }
492
- /**
493
- * TODO: [🔣] Support for multiple languages - python, java,...
494
- */
495
-
496
- /**
497
- * Parses the prompt template and returns the set of all used parameters
498
- *
499
- * @param promptTemplate the template with used parameters
500
- * @returns the set of parameter names
501
- * @throws {PromptbookSyntaxError} if the script is invalid
502
- */
503
- function extractParametersFromPromptTemplate(promptTemplate) {
504
- var e_1, _a, e_2, _b;
505
- var parameterNames = new Set();
506
- try {
507
- for (var _c = __values(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameters(promptTemplate.title)), false), __read(extractParameters(promptTemplate.description || '')), false), __read(extractParameters(promptTemplate.content)), false)), _d = _c.next(); !_d.done; _d = _c.next()) {
508
- var parameterName = _d.value;
509
- parameterNames.add(parameterName);
510
- }
511
- }
512
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
513
- finally {
514
- try {
515
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
516
- }
517
- finally { if (e_1) throw e_1.error; }
518
- }
519
- if (promptTemplate.executionType === 'SCRIPT') {
520
- try {
521
- for (var _e = __values(extractVariables(promptTemplate.content)), _f = _e.next(); !_f.done; _f = _e.next()) {
522
- var parameterName = _f.value;
523
- parameterNames.add(parameterName);
524
- }
525
- }
526
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
527
- finally {
528
- try {
529
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
530
- }
531
- finally { if (e_2) throw e_2.error; }
532
- }
533
- }
534
- return parameterNames;
535
- }
536
- /**
537
- * TODO: [🔣] If script require contentLanguage
538
- */
539
-
540
- /**
541
- * Execution type describes the way how the block is executed
542
- *
543
- * @see https://github.com/webgptorg/promptbook#execution-type
544
- */
545
- var ExecutionTypes = [
546
- 'PROMPT_TEMPLATE',
547
- 'SIMPLE_TEMPLATE',
548
- 'SCRIPT',
549
- 'PROMPT_DIALOG',
550
- // <- [🥻] Insert here when making new command
551
- ];
552
-
553
- /**
554
- * Units of text measurement
555
- */
556
- var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'LINES', 'PARAGRAPHS', 'PAGES'];
557
- /**
558
- * TODO: [💝] Unite object for expecting amount and format - remove expectFormat
559
- * TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
560
- * TODO: [👙][🧠] Just selecting gpt3 or gpt4 level of model
561
- */
562
-
563
- /**
564
- * Removes Markdown formatting tags from a string.
565
- *
566
- * @param {string} str - The string to remove Markdown tags from.
567
- * @returns {string} The input string with all Markdown tags removed.
568
- */
569
- function removeMarkdownFormatting(str) {
570
- // Remove bold formatting
571
- str = str.replace(/\*\*(.*?)\*\*/g, '$1');
572
- // Remove italic formatting
573
- str = str.replace(/\*(.*?)\*/g, '$1');
574
- // Remove code formatting
575
- str = str.replace(/`(.*?)`/g, '$1');
576
- return str;
577
- }
578
-
579
- /**
580
- * Function parseNumber will parse number from string
581
- *
582
- * Unlike Number.parseInt, Number.parseFloat it will never ever result in NaN
583
- * Note: it also works only with decimal numbers
360
+ * Unlike Number.parseInt, Number.parseFloat it will never ever result in NaN
361
+ * Note: it also works only with decimal numbers
584
362
  *
585
363
  * @returns parsed number
586
364
  * @throws {PromptbookSyntaxError} if the value is not a number
@@ -624,682 +402,23 @@
624
402
  return numerator / denominator;
625
403
  }
626
404
  if (/^(NAN|NULL|NONE|UNDEFINED|ZERO|NO.*)$/.test(value)) {
627
- return 0;
628
- }
629
- if (value.includes('E')) {
630
- var _b = __read(value.split('E'), 2), significand = _b[0], exponent = _b[1];
631
- return parseNumber(significand) * Math.pow(10, parseNumber(exponent));
632
- }
633
- if (!/^[0-9.]+$/.test(value) || value.split('.').length > 2) {
634
- throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\""));
635
- }
636
- var num = parseFloat(value);
637
- if (isNaN(num)) {
638
- throw new PromptbookSyntaxError("Unexpected NaN when parsing number from \"".concat(originalValue, "\""));
639
- }
640
- return num;
641
- }
642
- /**
643
- * TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
644
- */
645
-
646
- /**
647
- * Parses one line of ul/ol to command
648
- *
649
- * @returns parsed command object
650
- * @throws {PromptbookSyntaxError} if the command is invalid
651
- *
652
- * @private within the promptbookStringToJson
653
- */
654
- function parseCommand(listItem) {
655
- var e_1, _a;
656
- if (listItem.includes('\n') || listItem.includes('\r')) {
657
- throw new PromptbookSyntaxError('Command can not contain new line characters:');
658
- }
659
- var type = listItem.trim();
660
- type = type.split('`').join('');
661
- type = type.split('"').join('');
662
- type = type.split("'").join('');
663
- type = type.split('~').join('');
664
- type = type.split('[').join('');
665
- type = type.split(']').join('');
666
- type = type.split('(').join('');
667
- type = type.split(')').join('');
668
- type = normalizeTo_SCREAMING_CASE(type);
669
- type = type.split('DIALOGUE').join('DIALOG');
670
- var listItemParts = listItem
671
- .split(' ')
672
- .map(function (part) { return part.trim(); })
673
- .filter(function (item) { return item !== ''; })
674
- .filter(function (item) { return !/^PTBK$/i.test(item); })
675
- .filter(function (item) { return !/^PROMPTBOOK$/i.test(item); })
676
- .map(removeMarkdownFormatting);
677
- if (type.startsWith('URL') ||
678
- type.startsWith('PTBK_URL') ||
679
- type.startsWith('PTBKURL') ||
680
- type.startsWith('PROMPTBOOK_URL') ||
681
- type.startsWith('PROMPTBOOKURL') ||
682
- type.startsWith('HTTPS')) {
683
- if (!(listItemParts.length === 2 || (listItemParts.length === 1 && type.startsWith('HTTPS')))) {
684
- throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n ")));
685
- }
686
- var promptbookUrlString = listItemParts.pop();
687
- var promptbookUrl = new URL(promptbookUrlString);
688
- if (promptbookUrl.protocol !== 'https:') {
689
- throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n Protocol must be HTTPS\n ")));
690
- }
691
- if (promptbookUrl.hash !== '') {
692
- 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 ")));
693
- }
694
- return {
695
- type: 'PROMPTBOOK_URL',
696
- promptbookUrl: promptbookUrl,
697
- };
698
- }
699
- else if (type.startsWith('PROMPTBOOK_VERSION') || type.startsWith('PTBK_VERSION')) {
700
- if (listItemParts.length !== 2) {
701
- throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid PROMPTBOOK_VERSION command:\n\n - ".concat(listItem, "\n ")));
702
- }
703
- var promptbookVersion = listItemParts.pop();
704
- // TODO: Validate version
705
- return {
706
- type: 'PROMPTBOOK_VERSION',
707
- promptbookVersion: promptbookVersion,
708
- };
709
- }
710
- else if (type.startsWith('EXECUTE') ||
711
- type.startsWith('EXEC') ||
712
- type.startsWith('PROMPT_DIALOG') ||
713
- type.startsWith('SIMPLE_TEMPLATE')) {
714
- var executionTypes = ExecutionTypes.filter(function (executionType) { return type.includes(executionType); });
715
- if (executionTypes.length !== 1) {
716
- 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 "); }));
717
- }
718
- return {
719
- type: 'EXECUTE',
720
- executionType: executionTypes[0],
721
- };
722
- }
723
- else if (type.startsWith('MODEL')) {
724
- // TODO: Make this more elegant and dynamically
725
- if (type.startsWith('MODEL_VARIANT')) {
726
- if (type === 'MODEL_VARIANT_CHAT') {
727
- return {
728
- type: 'MODEL',
729
- key: 'modelVariant',
730
- value: 'CHAT',
731
- };
732
- }
733
- else if (type === 'MODEL_VARIANT_COMPLETION') {
734
- return {
735
- type: 'MODEL',
736
- key: 'modelVariant',
737
- value: 'COMPLETION',
738
- };
739
- }
740
- else {
741
- 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 "); }));
742
- }
743
- }
744
- if (type.startsWith('MODEL_NAME')) {
745
- return {
746
- type: 'MODEL',
747
- key: 'modelName',
748
- value: listItemParts.pop(),
749
- };
750
- }
751
- else {
752
- 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 "); }));
753
- }
754
- }
755
- else if (type.startsWith('PARAM') ||
756
- type.startsWith('INPUT_PARAM') ||
757
- type.startsWith('OUTPUT_PARAM') ||
758
- listItem.startsWith('{') ||
759
- listItem.startsWith('> {') /* <- Note: This is a bit hack to parse return parameters defined at the end of each section */) {
760
- var parametersMatch = listItem.match(/\{(?<parameterName>[a-z0-9_]+)\}[^\S\r\n]*(?<parameterDescription>.*)$/im);
761
- if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
762
- throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
763
- }
764
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
765
- var _b = parametersMatch.groups, parameterName = _b.parameterName, parameterDescription = _b.parameterDescription;
766
- if (parameterDescription && parameterDescription.match(/\{(?<parameterName>[a-z0-9_]+)\}/im)) {
767
- throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Parameter {".concat(parameterName, "} can not contain another parameter in description:\n\n - ").concat(listItem, "\n ")));
768
- }
769
- var isInput = type.startsWith('INPUT');
770
- var isOutput = type.startsWith('OUTPUT');
771
- if (listItem.startsWith('> {')) {
772
- isInput = false;
773
- isOutput = false;
774
- }
775
- return {
776
- type: 'PARAMETER',
777
- parameterName: parameterName,
778
- parameterDescription: parameterDescription.trim() || null,
779
- isInput: isInput,
780
- isOutput: isOutput,
781
- };
782
- }
783
- else if (type.startsWith('JOKER')) {
784
- if (listItemParts.length !== 2) {
785
- throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid JOKER command:\n\n - ".concat(listItem, "\n ")));
786
- }
787
- var parametersMatch = (listItemParts.pop() || '').match(/^\{(?<parameterName>[a-z0-9_]+)\}$/im);
788
- if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
789
- throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
790
- }
791
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
792
- var parameterName = parametersMatch.groups.parameterName;
793
- return {
794
- type: 'JOKER',
795
- parameterName: parameterName,
796
- };
797
- }
798
- else if (type.startsWith('POSTPROCESS') || type.startsWith('POST_PROCESS')) {
799
- if (listItemParts.length !== 2) {
800
- throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid POSTPROCESSING command:\n\n - ".concat(listItem, "\n ")));
801
- }
802
- var functionName = listItemParts.pop();
803
- return {
804
- type: 'POSTPROCESS',
805
- functionName: functionName,
806
- };
807
- }
808
- else if (type.startsWith('EXPECT_JSON')) {
809
- return {
810
- type: 'EXPECT_FORMAT',
811
- format: 'JSON',
812
- };
813
- // [🥤]
814
- }
815
- else if (type.startsWith('EXPECT')) {
816
- try {
817
- listItemParts.shift();
818
- var sign = void 0;
819
- var signRaw = listItemParts.shift();
820
- if (/^exact/i.test(signRaw)) {
821
- sign = 'EXACTLY';
822
- }
823
- else if (/^min/i.test(signRaw)) {
824
- sign = 'MINIMUM';
825
- }
826
- else if (/^max/i.test(signRaw)) {
827
- sign = 'MAXIMUM';
828
- }
829
- else {
830
- throw new PromptbookSyntaxError("Invalid sign \"".concat(signRaw, "\", expected EXACTLY, MIN or MAX"));
831
- }
832
- var amountRaw = listItemParts.shift();
833
- var amount = parseNumber(amountRaw);
834
- if (amount < 0) {
835
- throw new PromptbookSyntaxError('Amount must be positive number or zero');
836
- }
837
- if (amount !== Math.floor(amount)) {
838
- throw new PromptbookSyntaxError('Amount must be whole number');
839
- }
840
- var unitRaw = listItemParts.shift();
841
- var unit = undefined;
842
- try {
843
- for (var EXPECTATION_UNITS_1 = __values(EXPECTATION_UNITS), EXPECTATION_UNITS_1_1 = EXPECTATION_UNITS_1.next(); !EXPECTATION_UNITS_1_1.done; EXPECTATION_UNITS_1_1 = EXPECTATION_UNITS_1.next()) {
844
- var existingUnit = EXPECTATION_UNITS_1_1.value;
845
- var existingUnitText = existingUnit;
846
- existingUnitText = existingUnitText.substring(0, existingUnitText.length - 1);
847
- if (existingUnitText === 'CHARACTER') {
848
- existingUnitText = 'CHAR';
849
- }
850
- if (new RegExp("^".concat(existingUnitText.toLowerCase())).test(unitRaw.toLowerCase()) ||
851
- new RegExp("^".concat(unitRaw.toLowerCase())).test(existingUnitText.toLowerCase())) {
852
- if (unit !== undefined) {
853
- throw new PromptbookSyntaxError("Ambiguous unit \"".concat(unitRaw, "\""));
854
- }
855
- unit = existingUnit;
856
- }
857
- }
858
- }
859
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
860
- finally {
861
- try {
862
- if (EXPECTATION_UNITS_1_1 && !EXPECTATION_UNITS_1_1.done && (_a = EXPECTATION_UNITS_1.return)) _a.call(EXPECTATION_UNITS_1);
863
- }
864
- finally { if (e_1) throw e_1.error; }
865
- }
866
- if (unit === undefined) {
867
- throw new PromptbookSyntaxError("Invalid unit \"".concat(unitRaw, "\""));
868
- }
869
- return {
870
- type: 'EXPECT_AMOUNT',
871
- sign: sign,
872
- unit: unit,
873
- amount: amount,
874
- };
875
- }
876
- catch (error) {
877
- if (!(error instanceof Error)) {
878
- throw error;
879
- }
880
- throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid EXPECT command; ".concat(error.message, ":\n\n - ").concat(listItem, "\n ")));
881
- }
882
- /*
883
- } else if (type.startsWith('__________________')) {
884
- // <- [🥻] Insert here when making new command
885
- */
886
- }
887
- else {
888
- 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 ")));
889
- }
890
- }
891
-
892
- /**
893
- * Removes emojis from a string and fix whitespaces
894
- *
895
- * @param text with emojis
896
- * @returns text without emojis
897
- */
898
- function removeEmojis(text) {
899
- // Replace emojis (and also ZWJ sequence) with hyphens
900
- text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
901
- text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
902
- text = text.replace(/(\p{Extended_Pictographic})(\u{200D}\p{Extended_Pictographic})*/gu, '$1');
903
- text = text.replace(/\p{Extended_Pictographic}/gu, '');
904
- return text;
905
- }
906
-
907
- /**
908
- * Function normalizes title to name which can be used as identifier
909
- */
910
- function titleToName(value) {
911
- value = removeEmojis(value);
912
- value = normalizeToKebabCase(value);
913
- // TODO: [🧠] Maybe warn or add some padding to short name which are not good identifiers
914
- return value;
915
- }
916
-
917
- /**
918
- * Parse promptbook from string format to JSON format
919
- *
920
- * @throws {PromptbookSyntaxError} if the promptbook string is not valid
921
- *
922
- * Note: This function does not validate logic of the pipeline only the syntax
923
- */
924
- function promptbookStringToJson(promptbookString) {
925
- var e_1, _a, e_2, _b;
926
- var promptbookJson = {
927
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
928
- title: undefined /* <- Note: Putting here placeholder to keep `title` on top at final JSON */,
929
- promptbookUrl: undefined /* <- Note: Putting here placeholder to keep `promptbookUrl` on top at final JSON */,
930
- promptbookVersion: PROMPTBOOK_VERSION,
931
- description: undefined /* <- Note: Putting here placeholder to keep `description` on top at final JSON */,
932
- parameters: [],
933
- promptTemplates: [],
934
- };
935
- // =============================================================
936
- // Note: 1️⃣ Normalization of the PROMPTBOOK string
937
- promptbookString = removeContentComments(promptbookString);
938
- promptbookString = promptbookString.replaceAll(/`\{(?<parameterName>[a-z0-9_]+)\}`/gi, '{$<parameterName>}');
939
- promptbookString = promptbookString.replaceAll(/`->\s+\{(?<parameterName>[a-z0-9_]+)\}`/gi, '-> {$<parameterName>}');
940
- // =============================================================
941
- ///Note: 2️⃣ Function for adding parameters
942
- var addParam = function (parameterCommand) {
943
- var parameterName = parameterCommand.parameterName, parameterDescription = parameterCommand.parameterDescription, isInput = parameterCommand.isInput, isOutput = parameterCommand.isOutput;
944
- var existingParameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
945
- if (existingParameter &&
946
- existingParameter.description &&
947
- existingParameter.description !== parameterDescription &&
948
- parameterDescription) {
949
- 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 "); }));
950
- }
951
- if (existingParameter) {
952
- if (parameterDescription) {
953
- existingParameter.description = parameterDescription;
954
- }
955
- }
956
- else {
957
- promptbookJson.parameters.push({
958
- name: parameterName,
959
- description: parameterDescription || undefined,
960
- isInput: isInput,
961
- isOutput: isOutput,
962
- });
963
- }
964
- };
965
- // =============================================================
966
- // Note: 3️⃣ Parse the dynamic part - the template pipeline
967
- var markdownStructure = markdownToMarkdownStructure(promptbookString);
968
- var markdownStructureDeepness = countMarkdownStructureDeepness(markdownStructure);
969
- if (markdownStructureDeepness !== 2) {
970
- 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 ")));
971
- }
972
- promptbookJson.title = markdownStructure.title;
973
- // TODO: [1] DRY description
974
- var description = markdownStructure.content;
975
- // Note: Remove codeblocks
976
- description = description.split(/^```.*^```/gms).join('');
977
- //Note: Remove lists and return statement
978
- description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
979
- description = spacetrim.spaceTrim(description);
980
- if (description === '') {
981
- description = undefined;
982
- }
983
- promptbookJson.description = description;
984
- var defaultModelRequirements = {};
985
- var listItems = extractAllListItemsFromMarkdown(markdownStructure.content);
986
- try {
987
- for (var listItems_1 = __values(listItems), listItems_1_1 = listItems_1.next(); !listItems_1_1.done; listItems_1_1 = listItems_1.next()) {
988
- var listItem = listItems_1_1.value;
989
- var command = parseCommand(listItem);
990
- switch (command.type) {
991
- case 'PROMPTBOOK_URL':
992
- promptbookJson.promptbookUrl = command.promptbookUrl.href;
993
- break;
994
- case 'PROMPTBOOK_VERSION':
995
- promptbookJson.promptbookVersion = command.promptbookVersion;
996
- break;
997
- case 'MODEL':
998
- defaultModelRequirements[command.key] = command.value;
999
- break;
1000
- case 'PARAMETER':
1001
- addParam(command);
1002
- break;
1003
- default:
1004
- throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the head of the promptbook ONLY at the prompt template block"));
1005
- }
1006
- }
1007
- }
1008
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
1009
- finally {
1010
- try {
1011
- if (listItems_1_1 && !listItems_1_1.done && (_a = listItems_1.return)) _a.call(listItems_1);
1012
- }
1013
- finally { if (e_1) throw e_1.error; }
1014
- }
1015
- var _loop_1 = function (section) {
1016
- var e_3, _e;
1017
- // TODO: Parse prompt template description (the content out of the codeblock and lists)
1018
- var templateModelRequirements = __assign({}, defaultModelRequirements);
1019
- var listItems_3 = extractAllListItemsFromMarkdown(section.content);
1020
- var dependentParameterNames = new Set();
1021
- var executionType = 'PROMPT_TEMPLATE';
1022
- var jokers = [];
1023
- var postprocessing = [];
1024
- var expectAmount = {};
1025
- var expectFormat = undefined;
1026
- var isExecutionTypeChanged = false;
1027
- try {
1028
- for (var listItems_2 = (e_3 = void 0, __values(listItems_3)), listItems_2_1 = listItems_2.next(); !listItems_2_1.done; listItems_2_1 = listItems_2.next()) {
1029
- var listItem = listItems_2_1.value;
1030
- var command = parseCommand(listItem);
1031
- switch (command.type) {
1032
- case 'JOKER':
1033
- jokers.push(command.parameterName);
1034
- dependentParameterNames.add(command.parameterName);
1035
- break;
1036
- case 'EXECUTE':
1037
- if (isExecutionTypeChanged) {
1038
- throw new PromptbookSyntaxError('Execution type is already defined in the prompt template. It can be defined only once.');
1039
- }
1040
- executionType = command.executionType;
1041
- isExecutionTypeChanged = true;
1042
- break;
1043
- case 'MODEL':
1044
- templateModelRequirements[command.key] = command.value;
1045
- break;
1046
- case 'PARAMETER':
1047
- // Note: This is just for detecting resulitng parameter name
1048
- addParam(command);
1049
- break;
1050
- case 'POSTPROCESS':
1051
- postprocessing.push(command.functionName);
1052
- break;
1053
- case 'EXPECT_AMOUNT':
1054
- // eslint-disable-next-line no-case-declarations
1055
- var unit = command.unit.toLowerCase();
1056
- expectAmount[unit] = expectAmount[unit] || {};
1057
- if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
1058
- if (expectAmount[unit].min !== undefined) {
1059
- throw new PromptbookSyntaxError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
1060
- }
1061
- expectAmount[unit].min = command.amount;
1062
- } /* not else */
1063
- if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
1064
- if (expectAmount[unit].max !== undefined) {
1065
- throw new PromptbookSyntaxError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
1066
- }
1067
- expectAmount[unit].max = command.amount;
1068
- }
1069
- break;
1070
- case 'EXPECT_FORMAT':
1071
- if (expectFormat !== undefined && command.format !== expectFormat) {
1072
- throw new PromptbookSyntaxError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
1073
- }
1074
- expectFormat = command.format;
1075
- break;
1076
- default:
1077
- throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the block of the prompt template ONLY at the head of the promptbook"));
1078
- }
1079
- }
1080
- }
1081
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
1082
- finally {
1083
- try {
1084
- if (listItems_2_1 && !listItems_2_1.done && (_e = listItems_2.return)) _e.call(listItems_2);
1085
- }
1086
- finally { if (e_3) throw e_3.error; }
1087
- }
1088
- var _f = extractOneBlockFromMarkdown(section.content), language = _f.language, content = _f.content;
1089
- if (executionType === 'SCRIPT') {
1090
- if (!language) {
1091
- throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
1092
- }
1093
- else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
1094
- 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 "); }));
1095
- }
1096
- }
1097
- var lastLine = section.content.split('\n').pop();
1098
- var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
1099
- if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
1100
- throw new PromptbookSyntaxError(spacetrim.spaceTrim(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
1101
- // TODO: Show code of invalid sections each time + DRY
1102
- section.content
1103
- .split('\n')
1104
- .map(function (line) { return "> ".concat(line); })
1105
- .join('\n')), "\n "); }));
1106
- }
1107
- var resultingParameterName = match.groups.resultingParamName;
1108
- // TODO: [1] DRY description
1109
- var description_1 = section.content;
1110
- // Note: Remove codeblocks
1111
- description_1 = description_1.split(/^```.*^```/gms).join('');
1112
- //Note: Remove lists and return statement
1113
- description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
1114
- description_1 = spacetrim.spaceTrim(description_1);
1115
- if (description_1 === '') {
1116
- description_1 = undefined;
1117
- }
1118
- if (Object.keys(jokers).length === 0) {
1119
- jokers = undefined;
1120
- }
1121
- if (Object.keys(expectAmount).length === 0) {
1122
- expectAmount = undefined;
1123
- }
1124
- if (Object.keys(postprocessing).length === 0) {
1125
- postprocessing = undefined;
1126
- }
1127
- dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, executionType: executionType, content: content })));
1128
- promptbookJson.promptTemplates.push({
1129
- name: titleToName(section.title),
1130
- title: section.title,
1131
- description: description_1,
1132
- dependentParameterNames: Array.from(dependentParameterNames),
1133
- executionType: executionType,
1134
- jokers: jokers,
1135
- postprocessing: postprocessing,
1136
- expectations: expectAmount,
1137
- expectFormat: expectFormat,
1138
- modelRequirements: templateModelRequirements,
1139
- contentLanguage: executionType === 'SCRIPT' ? language : undefined,
1140
- content: content,
1141
- resultingParameterName: resultingParameterName,
1142
- });
1143
- };
1144
- try {
1145
- for (var _c = __values(markdownStructure.sections), _d = _c.next(); !_d.done; _d = _c.next()) {
1146
- var section = _d.value;
1147
- _loop_1(section);
1148
- }
1149
- }
1150
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
1151
- finally {
1152
- try {
1153
- if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
1154
- }
1155
- finally { if (e_2) throw e_2.error; }
1156
- }
1157
- // =============================================================
1158
- return promptbookJson;
1159
- }
1160
- /**
1161
- * TODO: Report here line/column of error
1162
- * TODO: Use spaceTrim more effectively
1163
- * TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
1164
- */
1165
-
1166
- /**
1167
- * Add or modify an auto-generated section in a markdown file
1168
- *
1169
- * @private within the library
1170
- */
1171
- function addAutoGeneratedSection(content, options) {
1172
- var sectionName = options.sectionName, sectionContent = options.sectionContent;
1173
- var warningLine = "<!-- \u26A0\uFE0F WARNING: This section was auto-generated -->";
1174
- var sectionRegex = new RegExp("<!--".concat(sectionName, "-->([\\s\\S]*?)<!--/").concat(sectionName, "-->"), 'g');
1175
- var sectionMatch = content.match(sectionRegex);
1176
- if (sectionMatch) {
1177
- return content.replace(sectionRegex, spacetrim.spaceTrim(function (block) { return "\n <!--".concat(sectionName, "-->\n ").concat(block(warningLine), "\n ").concat(block(sectionContent), "\n <!--/").concat(sectionName, "-->\n "); }));
1178
- }
1179
- var placeForSection = removeContentComments(content).match(/^##.*$/im);
1180
- if (!placeForSection) {
1181
- throw new Error("No place where to put the section <!--".concat(sectionName, "-->"));
1182
- }
1183
- var _a = __read(placeForSection, 1), heading = _a[0];
1184
- return content.replace(heading, "<!--".concat(sectionName, "-->\n").concat(warningLine, "\n").concat(sectionContent, "\n<!--/").concat(sectionName, "-->\n\n").concat(heading));
1185
- }
1186
-
1187
- /**
1188
- * Prettify the html code
1189
- *
1190
- * @param content raw html code
1191
- * @returns formatted html code
1192
- */
1193
- function prettifyMarkdown(content) {
1194
- try {
1195
- return prettier.format(content, {
1196
- parser: 'markdown',
1197
- plugins: [parserHtml__default["default"]],
1198
- // TODO: DRY - make some import or auto-copy of .prettierrc
1199
- endOfLine: 'lf',
1200
- tabWidth: 4,
1201
- singleQuote: true,
1202
- trailingComma: 'all',
1203
- arrowParens: 'always',
1204
- printWidth: 120,
1205
- htmlWhitespaceSensitivity: 'ignore',
1206
- jsxBracketSameLine: false,
1207
- bracketSpacing: true,
1208
- });
1209
- }
1210
- catch (error) {
1211
- console.error('There was an error with prettifying the markdown, using the original as the fallback', {
1212
- error: error,
1213
- html: content,
1214
- });
1215
- return content;
1216
- }
1217
- }
1218
-
1219
- /**
1220
- * Creates a Mermaid graph based on the promptbook
1221
- *
1222
- * Note: The result is not wrapped in a Markdown code block
1223
- */
1224
- function renderPromptbookMermaid(promptbookJson, options) {
1225
- var _a = (options || {}).linkPromptTemplate, linkPromptTemplate = _a === void 0 ? function () { return null; } : _a;
1226
- var parameterNameToTemplateName = function (parameterName) {
1227
- var parameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
1228
- if (!parameter) {
1229
- throw new UnexpectedError("Could not find {".concat(parameterName, "}"));
1230
- }
1231
- if (parameter.isInput) {
1232
- return 'input';
1233
- }
1234
- var template = promptbookJson.promptTemplates.find(function (template) { return template.resultingParameterName === parameterName; });
1235
- if (!template) {
1236
- throw new Error("Could not find template for {".concat(parameterName, "}"));
1237
- }
1238
- return normalizeTo_camelCase('template-' + titleToName(template.title));
1239
- };
1240
- var promptbookMermaid = spacetrim.spaceTrim(function (block) { return "\n\n %% \uD83D\uDD2E Tip: Open this on GitHub or in the VSCode website to see the Mermaid graph visually\n\n flowchart LR\n subgraph \"".concat(promptbookJson.title, "\"\n\n direction TB\n\n input((Input)):::input\n ").concat(block(promptbookJson.promptTemplates
1241
- .flatMap(function (_a) {
1242
- var title = _a.title, dependentParameterNames = _a.dependentParameterNames, resultingParameterName = _a.resultingParameterName;
1243
- return __spreadArray([
1244
- "".concat(parameterNameToTemplateName(resultingParameterName), "(\"").concat(title, "\")")
1245
- ], __read(dependentParameterNames.map(function (dependentParameterName) {
1246
- return "".concat(parameterNameToTemplateName(dependentParameterName), "--\"{").concat(dependentParameterName, "}\"-->").concat(parameterNameToTemplateName(resultingParameterName));
1247
- })), false);
1248
- })
1249
- .join('\n')), "\n\n ").concat(block(promptbookJson.parameters
1250
- .filter(function (_a) {
1251
- var isOutput = _a.isOutput;
1252
- return isOutput;
1253
- })
1254
- .map(function (_a) {
1255
- var name = _a.name;
1256
- return "".concat(parameterNameToTemplateName(name), "--\"{").concat(name, "}\"-->output");
1257
- })
1258
- .join('\n')), "\n output((Output)):::output\n\n ").concat(block(promptbookJson.promptTemplates
1259
- .map(function (promptTemplate) {
1260
- var link = linkPromptTemplate(promptTemplate);
1261
- if (link === null) {
1262
- return '';
1263
- }
1264
- var href = link.href, title = link.title;
1265
- var templateName = parameterNameToTemplateName(promptTemplate.resultingParameterName);
1266
- return "click ".concat(templateName, " href \"").concat(href, "\" \"").concat(title, "\";");
1267
- })
1268
- .filter(function (line) { return line !== ''; })
1269
- .join('\n')), "\n\n classDef input color: grey;\n classDef output color: grey;\n\n end;\n\n "); });
1270
- return promptbookMermaid;
1271
- }
1272
- /**
1273
- * TODO: Maybe use some Mermaid library instead of string templating
1274
- * TODO: [🕌] When more than 2 functionalities, split into separate functions
1275
- */
1276
-
1277
- /**
1278
- * Prettyfies Promptbook string and adds Mermaid graph
1279
- */
1280
- function prettifyPromptbookString(promptbookString, options) {
1281
- var isGraphAdded = options.isGraphAdded, isPrettifyed = options.isPrettifyed;
1282
- if (isGraphAdded) {
1283
- var promptbookJson = promptbookStringToJson(promptbookString);
1284
- var promptbookMermaid_1 = renderPromptbookMermaid(promptbookJson, {
1285
- linkPromptTemplate: function (promptTemplate) {
1286
- return { href: "#".concat(promptTemplate.name), title: promptTemplate.title };
1287
- },
1288
- });
1289
- var promptbookMermaidBlock = spacetrim.spaceTrim(function (block) { return "\n ```mermaid\n ".concat(block(promptbookMermaid_1), "\n ```\n "); });
1290
- promptbookString = addAutoGeneratedSection(promptbookString, {
1291
- sectionName: 'Graph',
1292
- sectionContent: promptbookMermaidBlock,
1293
- });
405
+ return 0;
406
+ }
407
+ if (value.includes('E')) {
408
+ var _b = __read(value.split('E'), 2), significand = _b[0], exponent = _b[1];
409
+ return parseNumber(significand) * Math.pow(10, parseNumber(exponent));
410
+ }
411
+ if (!/^[0-9.]+$/.test(value) || value.split('.').length > 2) {
412
+ throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\""));
1294
413
  }
1295
- if (isPrettifyed) {
1296
- promptbookString = prettifyMarkdown(promptbookString);
414
+ var num = parseFloat(value);
415
+ if (isNaN(num)) {
416
+ throw new PromptbookSyntaxError("Unexpected NaN when parsing number from \"".concat(originalValue, "\""));
1297
417
  }
1298
- return promptbookString;
418
+ return num;
1299
419
  }
1300
420
  /**
1301
- * TODO: Maybe use some Mermaid library instead of string templating
1302
- * TODO: [🕌] When more than 2 functionalities, split into separate functions
421
+ * TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
1303
422
  */
1304
423
 
1305
424
  /**
@@ -1373,60 +492,159 @@
1373
492
  }
1374
493
 
1375
494
  /**
1376
- * This error indicates errors during the execution of the promptbook
495
+ * Async version of Array.forEach
496
+ *
497
+ * @param array - Array to iterate over
498
+ * @param options - Options for the function
499
+ * @param callbackfunction - Function to call for each item
500
+ */
501
+ function forEachAsync(array, options, callbackfunction) {
502
+ return __awaiter(this, void 0, void 0, function () {
503
+ var _a, inParallelCount, index, runningTasks, tasks, _loop_1, _b, _c, item, e_1_1;
504
+ var e_1, _d;
505
+ return __generator(this, function (_e) {
506
+ switch (_e.label) {
507
+ case 0:
508
+ _a = options.inParallelCount, inParallelCount = _a === void 0 ? Infinity : _a;
509
+ index = 0;
510
+ runningTasks = [];
511
+ tasks = [];
512
+ _loop_1 = function (item) {
513
+ var currentIndex, task;
514
+ return __generator(this, function (_f) {
515
+ switch (_f.label) {
516
+ case 0:
517
+ currentIndex = index++;
518
+ task = callbackfunction(item, currentIndex, array);
519
+ tasks.push(task);
520
+ runningTasks.push(task);
521
+ /* not await */ Promise.resolve(task).then(function () {
522
+ runningTasks = runningTasks.filter(function (t) { return t !== task; });
523
+ });
524
+ if (!(inParallelCount < runningTasks.length)) return [3 /*break*/, 2];
525
+ return [4 /*yield*/, Promise.race(runningTasks)];
526
+ case 1:
527
+ _f.sent();
528
+ _f.label = 2;
529
+ case 2: return [2 /*return*/];
530
+ }
531
+ });
532
+ };
533
+ _e.label = 1;
534
+ case 1:
535
+ _e.trys.push([1, 6, 7, 8]);
536
+ _b = __values(array), _c = _b.next();
537
+ _e.label = 2;
538
+ case 2:
539
+ if (!!_c.done) return [3 /*break*/, 5];
540
+ item = _c.value;
541
+ return [5 /*yield**/, _loop_1(item)];
542
+ case 3:
543
+ _e.sent();
544
+ _e.label = 4;
545
+ case 4:
546
+ _c = _b.next();
547
+ return [3 /*break*/, 2];
548
+ case 5: return [3 /*break*/, 8];
549
+ case 6:
550
+ e_1_1 = _e.sent();
551
+ e_1 = { error: e_1_1 };
552
+ return [3 /*break*/, 8];
553
+ case 7:
554
+ try {
555
+ if (_c && !_c.done && (_d = _b.return)) _d.call(_b);
556
+ }
557
+ finally { if (e_1) throw e_1.error; }
558
+ return [7 /*endfinally*/];
559
+ case 8: return [4 /*yield*/, Promise.all(tasks)];
560
+ case 9:
561
+ _e.sent();
562
+ return [2 /*return*/];
563
+ }
564
+ });
565
+ });
566
+ }
567
+
568
+ /**
569
+ * The maximum number of iterations for a loops
570
+ */
571
+ var LOOP_LIMIT = 1000;
572
+
573
+ /**
574
+ * This error occurs during the parameter replacement in the template
575
+ *
576
+ * Note: This is a kindof subtype of PromptbookExecutionError because it occurs during the execution of the pipeline
1377
577
  */
1378
- var PromptbookExecutionError = /** @class */ (function (_super) {
1379
- __extends(PromptbookExecutionError, _super);
1380
- function PromptbookExecutionError(message) {
578
+ var TemplateError = /** @class */ (function (_super) {
579
+ __extends(TemplateError, _super);
580
+ function TemplateError(message) {
1381
581
  var _this = _super.call(this, message) || this;
1382
- _this.name = 'PromptbookExecutionError';
1383
- Object.setPrototypeOf(_this, PromptbookExecutionError.prototype);
582
+ _this.name = 'TemplateError';
583
+ Object.setPrototypeOf(_this, TemplateError.prototype);
1384
584
  return _this;
1385
585
  }
1386
- return PromptbookExecutionError;
586
+ return TemplateError;
1387
587
  }(Error));
1388
588
 
1389
589
  /**
1390
- * Asserts that the execution of a promptnook is successful
590
+ * Replaces parameters in template with values from parameters object
591
+ *
592
+ * @param template the template with parameters in {curly} braces
593
+ * @param parameters the object with parameters
594
+ * @returns the template with replaced parameters
595
+ * @throws {TemplateError} if parameter is not defined, not closed, or not opened
1391
596
  *
1392
- * @param executionResult - The partial result of the promptnook execution
1393
- * @throws {PromptbookExecutionError} If the execution is not successful or if multiple errors occurred
597
+ * @private within the createPromptbookExecutor
1394
598
  */
1395
- function assertsExecutionSuccessful(executionResult) {
1396
- var isSuccessful = executionResult.isSuccessful, errors = executionResult.errors;
1397
- if (isSuccessful === true) {
1398
- return;
1399
- }
1400
- if (errors.length === 0) {
1401
- throw new PromptbookExecutionError("Promptnook Execution failed because of unknown reason");
599
+ function replaceParameters(template, parameters) {
600
+ var replacedTemplate = template;
601
+ var match;
602
+ var loopLimit = LOOP_LIMIT;
603
+ var _loop_1 = function () {
604
+ if (loopLimit-- < 0) {
605
+ throw new UnexpectedError('Loop limit reached during parameters replacement in `replaceParameters`');
606
+ }
607
+ var precol = match.groups.precol;
608
+ var parameterName = match.groups.parameterName;
609
+ if (parameterName === '') {
610
+ return "continue";
611
+ }
612
+ if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
613
+ throw new TemplateError('Parameter is already opened or not closed');
614
+ }
615
+ if (parameters[parameterName] === undefined) {
616
+ throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
617
+ }
618
+ var parameterValue = parameters[parameterName];
619
+ if (parameterValue === undefined) {
620
+ throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
621
+ }
622
+ parameterValue = parameterValue.toString();
623
+ if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
624
+ parameterValue = parameterValue
625
+ .split('\n')
626
+ .map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
627
+ .join('\n');
628
+ }
629
+ replacedTemplate =
630
+ replacedTemplate.substring(0, match.index + precol.length) +
631
+ parameterValue +
632
+ replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
633
+ };
634
+ while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
635
+ .exec(replacedTemplate))) {
636
+ _loop_1();
1402
637
  }
1403
- else if (errors.length === 1) {
1404
- throw errors[0];
638
+ // [💫] Check if there are parameters that are not closed properly
639
+ if (/{\w+$/.test(replacedTemplate)) {
640
+ throw new TemplateError('Parameter is not closed');
1405
641
  }
1406
- else {
1407
- throw new PromptbookExecutionError(spacetrim.spaceTrim(function (block) { return "\n Multiple errors occurred during promptnook execution\n\n ".concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n')), "\n "); }));
642
+ // [💫] Check if there are parameters that are not opened properly
643
+ if (/^\w+}/.test(replacedTemplate)) {
644
+ throw new TemplateError('Parameter is not opened');
1408
645
  }
646
+ return replacedTemplate;
1409
647
  }
1410
- /**
1411
- * TODO: [🧠] Can this return type be better typed than void
1412
- */
1413
-
1414
- /**
1415
- * This error occurs when some expectation is not met in the execution of the pipeline
1416
- *
1417
- * @private Always catched and rethrown as `PromptbookExecutionError`
1418
- * Note: This is a kindof subtype of PromptbookExecutionError
1419
- */
1420
- var ExpectError = /** @class */ (function (_super) {
1421
- __extends(ExpectError, _super);
1422
- function ExpectError(message) {
1423
- var _this = _super.call(this, message) || this;
1424
- _this.name = 'ExpectError';
1425
- Object.setPrototypeOf(_this, ExpectError.prototype);
1426
- return _this;
1427
- }
1428
- return ExpectError;
1429
- }(Error));
1430
648
 
1431
649
  /**
1432
650
  * Counts number of characters in the text
@@ -1709,518 +927,222 @@
1709
927
  }
1710
928
  // <- TODO: [🍓] Put to maker function to save execution time if not needed
1711
929
  /*
1712
- @see https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
1713
- Licensed under the Apache License, Version 2.0 (the "License");
1714
- you may not use this file except in compliance with the License.
1715
- You may obtain a copy of the License at
1716
-
1717
- http://www.apache.org/licenses/LICENSE-2.0
1718
-
1719
- Unless required by applicable law or agreed to in writing, software
1720
- distributed under the License is distributed on an "AS IS" BASIS,
1721
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1722
- See the License for the specific language governing permissions and
1723
- limitations under the License.
1724
- */
1725
-
1726
- /**
1727
- *
1728
- */
1729
- function removeDiacritics(input) {
1730
- /*eslint no-control-regex: "off"*/
1731
- return input.replace(/[^\u0000-\u007E]/g, function (a) {
1732
- return DIACRITIC_VARIANTS_LETTERS[a] || a;
1733
- });
1734
- }
1735
-
1736
- /**
1737
- * Counts number of words in the text
1738
- */
1739
- function countWords(text) {
1740
- text = text.replace(/[\p{Extended_Pictographic}]/gu, 'a');
1741
- text = removeDiacritics(text);
1742
- return text.split(/[^a-zа-я0-9]+/i).filter(function (word) { return word.length > 0; }).length;
1743
- }
1744
-
1745
- /**
1746
- * Index of all counter functions
1747
- */
1748
- var CountUtils = {
1749
- CHARACTERS: countCharacters,
1750
- WORDS: countWords,
1751
- SENTENCES: countSentences,
1752
- PARAGRAPHS: countParagraphs,
1753
- LINES: countLines,
1754
- PAGES: countPages,
1755
- };
1756
-
1757
- /**
1758
- * Function checkExpectations will check if the expectations on given value are met
1759
- *
1760
- * Note: There are two simmilar functions:
1761
- * - `checkExpectations` which throws an error if the expectations are not met
1762
- * - `isPassingExpectations` which returns a boolean
1763
- *
1764
- * @throws {ExpectError} if the expectations are not met
1765
- * @returns {void} Nothing
1766
- */
1767
- function checkExpectations(expectations, value) {
1768
- var e_1, _a;
1769
- try {
1770
- for (var _b = __values(Object.entries(expectations)), _c = _b.next(); !_c.done; _c = _b.next()) {
1771
- var _d = __read(_c.value, 2), unit = _d[0], _e = _d[1], max = _e.max, min = _e.min;
1772
- var amount = CountUtils[unit.toUpperCase()](value);
1773
- if (min && amount < min) {
1774
- throw new ExpectError("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
1775
- } /* not else */
1776
- if (max && amount > max) {
1777
- throw new ExpectError("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
1778
- }
1779
- }
1780
- }
1781
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
1782
- finally {
1783
- try {
1784
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1785
- }
1786
- finally { if (e_1) throw e_1.error; }
1787
- }
1788
- }
1789
- /**
1790
- * Function checkExpectations will check if the expectations on given value are met
1791
- *
1792
- * Note: There are two simmilar functions:
1793
- * - `checkExpectations` which throws an error if the expectations are not met
1794
- * - `isPassingExpectations` which returns a boolean
1795
- *
1796
- * @returns {boolean} True if the expectations are met
1797
- */
1798
- function isPassingExpectations(expectations, value) {
1799
- try {
1800
- checkExpectations(expectations, value);
1801
- return true;
1802
- }
1803
- catch (error) {
1804
- if (!(error instanceof ExpectError)) {
1805
- throw error;
1806
- }
1807
- return false;
1808
- }
1809
- }
1810
- /**
1811
- * TODO: [💝] Unite object for expecting amount and format
1812
- */
1813
-
1814
- /**
1815
- * This error occurs during the parameter replacement in the template
1816
- *
1817
- * Note: This is a kindof subtype of PromptbookExecutionError because it occurs during the execution of the pipeline
1818
- */
1819
- var TemplateError = /** @class */ (function (_super) {
1820
- __extends(TemplateError, _super);
1821
- function TemplateError(message) {
1822
- var _this = _super.call(this, message) || this;
1823
- _this.name = 'TemplateError';
1824
- Object.setPrototypeOf(_this, TemplateError.prototype);
1825
- return _this;
1826
- }
1827
- return TemplateError;
1828
- }(Error));
1829
-
1830
- /**
1831
- * Replaces parameters in template with values from parameters object
1832
- *
1833
- * @param template the template with parameters in {curly} braces
1834
- * @param parameters the object with parameters
1835
- * @returns the template with replaced parameters
1836
- * @throws {TemplateError} if parameter is not defined, not closed, or not opened
1837
- *
1838
- * @private within the createPromptbookExecutor
1839
- */
1840
- function replaceParameters(template, parameters) {
1841
- var replacedTemplate = template;
1842
- var match;
1843
- var loopLimit = LOOP_LIMIT;
1844
- var _loop_1 = function () {
1845
- if (loopLimit-- < 0) {
1846
- throw new UnexpectedError('Loop limit reached during parameters replacement in `replaceParameters`');
1847
- }
1848
- var precol = match.groups.precol;
1849
- var parameterName = match.groups.parameterName;
1850
- if (parameterName === '') {
1851
- return "continue";
1852
- }
1853
- if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
1854
- throw new TemplateError('Parameter is already opened or not closed');
1855
- }
1856
- if (parameters[parameterName] === undefined) {
1857
- throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
1858
- }
1859
- var parameterValue = parameters[parameterName];
1860
- if (parameterValue === undefined) {
1861
- throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
1862
- }
1863
- parameterValue = parameterValue.toString();
1864
- if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
1865
- parameterValue = parameterValue
1866
- .split('\n')
1867
- .map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
1868
- .join('\n');
1869
- }
1870
- replacedTemplate =
1871
- replacedTemplate.substring(0, match.index + precol.length) +
1872
- parameterValue +
1873
- replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
1874
- };
1875
- while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
1876
- .exec(replacedTemplate))) {
1877
- _loop_1();
1878
- }
1879
- // [💫] Check if there are parameters that are not closed properly
1880
- if (/{\w+$/.test(replacedTemplate)) {
1881
- throw new TemplateError('Parameter is not closed');
1882
- }
1883
- // [💫] Check if there are parameters that are not opened properly
1884
- if (/^\w+}/.test(replacedTemplate)) {
1885
- throw new TemplateError('Parameter is not opened');
1886
- }
1887
- return replacedTemplate;
1888
- }
930
+ @see https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
931
+ Licensed under the Apache License, Version 2.0 (the "License");
932
+ you may not use this file except in compliance with the License.
933
+ You may obtain a copy of the License at
934
+
935
+ http://www.apache.org/licenses/LICENSE-2.0
936
+
937
+ Unless required by applicable law or agreed to in writing, software
938
+ distributed under the License is distributed on an "AS IS" BASIS,
939
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
940
+ See the License for the specific language governing permissions and
941
+ limitations under the License.
942
+ */
1889
943
 
1890
944
  /**
1891
- * Format either small or big number
1892
945
  *
1893
- * @private within the library
1894
946
  */
1895
- function formatNumber(value) {
1896
- if (value === 0) {
1897
- return '0';
1898
- }
1899
- for (var exponent = 0; exponent < 15; exponent++) {
1900
- var factor = Math.pow(10, exponent);
1901
- var valueRounded = Math.round(value * factor) / factor;
1902
- if (Math.abs(value - valueRounded) / value <
1903
- 0.001 /* <- TODO: Pass as option, pass to executionReportJsonToString as option */) {
1904
- return valueRounded.toFixed(exponent);
1905
- }
1906
- }
1907
- return value.toString();
947
+ function removeDiacritics(input) {
948
+ /*eslint no-control-regex: "off"*/
949
+ return input.replace(/[^\u0000-\u007E]/g, function (a) {
950
+ return DIACRITIC_VARIANTS_LETTERS[a] || a;
951
+ });
1908
952
  }
1909
953
 
1910
954
  /**
1911
- * Returns the same value that is passed as argument.
1912
- * No side effects.
1913
- *
1914
- * Note: It can be usefull for leveling indentation
1915
- *
1916
- * @param value any values
1917
- * @returns the same values
955
+ * Counts number of words in the text
1918
956
  */
1919
- function just(value) {
1920
- if (value === undefined) {
1921
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1922
- return undefined;
1923
- }
1924
- return value;
957
+ function countWords(text) {
958
+ text = text.replace(/[\p{Extended_Pictographic}]/gu, 'a');
959
+ text = removeDiacritics(text);
960
+ return text.split(/[^a-zа-я0-9]+/i).filter(function (word) { return word.length > 0; }).length;
1925
961
  }
1926
962
 
1927
963
  /**
1928
- * Create a markdown table from a 2D array of strings
1929
- *
1930
- * @private within the library
964
+ * Index of all counter functions
1931
965
  */
1932
- function createMarkdownTable(table) {
1933
- var columnWidths = table.reduce(function (widths, row) {
1934
- row.forEach(function (cell, columnIndex) {
1935
- var cellLength = cell.length;
1936
- if (!widths[columnIndex] || cellLength > widths[columnIndex]) {
1937
- widths[columnIndex] = cellLength;
1938
- }
1939
- });
1940
- return widths;
1941
- }, []);
1942
- var header = "| ".concat(table[0]
1943
- .map(function (cell, columnIndex) { return cell.padEnd(columnWidths[columnIndex]); })
1944
- .join(' | '), " |");
1945
- var separator = "|".concat(columnWidths.map(function (width) { return '-'.repeat(width + 2); }).join('|'), "|");
1946
- var rows = table.slice(1).map(function (row) {
1947
- var paddedRow = row.map(function (cell, columnIndex) {
1948
- return cell.padEnd(columnWidths[columnIndex]);
1949
- });
1950
- return "| ".concat(paddedRow.join(' | '), " |");
1951
- });
1952
- return __spreadArray([header, separator], __read(rows), false).join('\n');
1953
- }
966
+ var CountUtils = {
967
+ CHARACTERS: countCharacters,
968
+ WORDS: countWords,
969
+ SENTENCES: countSentences,
970
+ PARAGRAPHS: countParagraphs,
971
+ LINES: countLines,
972
+ PAGES: countPages,
973
+ };
1954
974
 
1955
975
  /**
1956
- * Function createMarkdownChart will draw a chart in markdown from ⬛+🟦 tiles
1957
- *
1958
- * @private within the library
976
+ * Function isValidJsonString will tell you if the string is valid JSON or not
1959
977
  */
1960
- function createMarkdownChart(options) {
1961
- var e_1, _a;
1962
- var nameHeader = options.nameHeader, valueHeader = options.valueHeader, items = options.items, width = options.width, unitName = options.unitName;
1963
- var from = Math.min.apply(Math, __spreadArray([], __read(items.map(function (item) { return item.from; })), false));
1964
- var to = Math.max.apply(Math, __spreadArray([], __read(items.map(function (item) { return item.to; })), false));
1965
- var scale = width / (to - from);
1966
- var table = [[nameHeader, valueHeader]];
978
+ function isValidJsonString(value) {
1967
979
  try {
1968
- for (var items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
1969
- var item = items_1_1.value;
1970
- var before = Math.round((item.from - from) * scale);
1971
- var during = Math.round((item.to - item.from) * scale);
1972
- var after = width - before - during;
1973
- table.push([removeEmojis(item.title).trim(), '░'.repeat(before) + '█'.repeat(during) + '░'.repeat(after)]);
1974
- }
980
+ JSON.parse(value);
981
+ return true;
1975
982
  }
1976
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
1977
- finally {
1978
- try {
1979
- if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
983
+ catch (error) {
984
+ if (!(error instanceof Error)) {
985
+ throw error;
1980
986
  }
1981
- finally { if (e_1) throw e_1.error; }
987
+ if (error.message.includes('Unexpected token')) {
988
+ return false;
989
+ }
990
+ return false;
1982
991
  }
1983
- var legend = "_Note: Each \u2588 represents ".concat(formatNumber(1 / scale), " ").concat(unitName, ", width of ").concat(valueHeader.toLowerCase(), " is ").concat(formatNumber(to - from), " ").concat(unitName, " = ").concat(width, " squares_");
1984
- return createMarkdownTable(table) + '\n\n' + legend;
1985
992
  }
1986
- /**
1987
- * TODO: Maybe use Mermain Gant Diagrams
1988
- * @see https://jojozhuang.github.io/tutorial/mermaid-cheat-sheet/
1989
- */
1990
993
 
1991
994
  /**
1992
- * Function escapeMarkdownBlock will escape markdown block if needed
1993
- * It is useful when you want have block in block
995
+ * Makes first letter of a string uppercase
996
+ *
1994
997
  */
1995
- function escapeMarkdownBlock(value) {
1996
- return value.replace(/```/g, '\\`\\`\\`');
998
+ function capitalize(word) {
999
+ return word.substring(0, 1).toUpperCase() + word.substring(1);
1997
1000
  }
1998
1001
 
1999
1002
  /**
2000
- * Default options for generating an execution report string
2001
- */
2002
- var ExecutionReportStringOptionsDefaults = {
2003
- taxRate: 0,
2004
- chartsWidth: 36,
2005
- };
2006
-
2007
- /**
2008
- * The thresholds for the relative time in the `moment` library.
1003
+ * Extracts all code blocks from markdown.
2009
1004
  *
2010
- * @see https://momentjscom.readthedocs.io/en/latest/moment/07-customization/13-relative-time-threshold/
2011
- */
2012
- var MOMENT_ARG_THRESHOLDS = {
2013
- ss: 3, // <- least number of seconds to be counted in seconds, minus 1. Must be set after setting the `s` unit or without setting the `s` unit.
2014
- };
2015
-
2016
- /**
2017
- * Count the duration of working time
1005
+ * Note: There are 3 simmilar function:
1006
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
1007
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
1008
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
1009
+ *
1010
+ * @param markdown any valid markdown
1011
+ * @returns code blocks with language and content
2018
1012
  *
2019
- * @private within the library
2020
1013
  */
2021
- function countWorkingDuration(items) {
1014
+ function extractAllBlocksFromMarkdown(markdown) {
2022
1015
  var e_1, _a;
2023
- var steps = Array.from(new Set(items.flatMap(function (item) { return [item.from, item.to]; })));
2024
- steps.sort(function (a, b) { return a - b; });
2025
- var intervals = steps.map(function (step, index) { return [step, steps[index + 1] || 0]; }).slice(0, -1);
2026
- var duration = 0;
2027
- var _loop_1 = function (interval) {
2028
- var _b = __read(interval, 2), from = _b[0], to = _b[1];
2029
- if (items.some(function (item) { return item.from < to && item.to > from; })) {
2030
- duration += to - from;
2031
- }
2032
- };
1016
+ var codeBlocks = [];
1017
+ var lines = markdown.split('\n');
1018
+ var currentCodeBlock = null;
2033
1019
  try {
2034
- for (var intervals_1 = __values(intervals), intervals_1_1 = intervals_1.next(); !intervals_1_1.done; intervals_1_1 = intervals_1.next()) {
2035
- var interval = intervals_1_1.value;
2036
- _loop_1(interval);
1020
+ for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
1021
+ var line = lines_1_1.value;
1022
+ if (line.startsWith('```')) {
1023
+ var language = line.slice(3).trim() || null;
1024
+ if (currentCodeBlock === null) {
1025
+ currentCodeBlock = { language: language, content: '' };
1026
+ }
1027
+ else {
1028
+ if (language !== null) {
1029
+ // [🌻]
1030
+ throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
1031
+ }
1032
+ codeBlocks.push(currentCodeBlock);
1033
+ currentCodeBlock = null;
1034
+ }
1035
+ }
1036
+ else if (currentCodeBlock !== null) {
1037
+ if (currentCodeBlock.content !== '') {
1038
+ currentCodeBlock.content += '\n';
1039
+ }
1040
+ currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
1041
+ }
2037
1042
  }
2038
1043
  }
2039
1044
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
2040
1045
  finally {
2041
1046
  try {
2042
- if (intervals_1_1 && !intervals_1_1.done && (_a = intervals_1.return)) _a.call(intervals_1);
1047
+ if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
2043
1048
  }
2044
1049
  finally { if (e_1) throw e_1.error; }
2045
1050
  }
2046
- return duration;
1051
+ if (currentCodeBlock !== null) {
1052
+ // [🌻]
1053
+ throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
1054
+ }
1055
+ return codeBlocks;
2047
1056
  }
2048
1057
 
2049
1058
  /**
2050
- * Converts execution report from JSON to string format
1059
+ * Utility function to extract all list items from markdown
1060
+ *
1061
+ * Note: It works with both ul and ol
1062
+ * Note: It omits list items in code blocks
1063
+ * Note: It flattens nested lists
1064
+ * Note: It can not work with html syntax and comments
1065
+ *
1066
+ * @param markdown any valid markdown
1067
+ * @returns
2051
1068
  */
2052
- function executionReportJsonToString(executionReportJson, options) {
1069
+ function extractAllListItemsFromMarkdown(markdown) {
2053
1070
  var e_1, _a;
2054
- var _b, _c, _d, _e, _f, _g;
2055
- var _h = __assign(__assign({}, ExecutionReportStringOptionsDefaults), (options || {})), taxRate = _h.taxRate, chartsWidth = _h.chartsWidth;
2056
- var executionReportString = spacetrim.spaceTrim(function (block) { return "\n # ".concat(executionReportJson.title || 'Execution report', "\n\n ").concat(block(executionReportJson.description || ''), "\n "); });
2057
- var headerList = [];
2058
- if (executionReportJson.promptbookUrl) {
2059
- headerList.push("PROMPTBOOK URL ".concat(executionReportJson.promptbookUrl));
2060
- }
2061
- headerList.push("PROMPTBOOK VERSION ".concat(executionReportJson.promptbookUsedVersion) +
2062
- (!executionReportJson.promptbookRequestedVersion
2063
- ? ''
2064
- : " *(requested ".concat(executionReportJson.promptbookRequestedVersion, ")*")));
2065
- if (executionReportJson.promptExecutions.length !== 0) {
2066
- // TODO: What if startedAt OR/AND completedAt is not defined?
2067
- var startedAt = moment__default["default"](Math.min.apply(Math, __spreadArray([], __read(executionReportJson.promptExecutions
2068
- .filter(function (promptExecution) { var _a, _b; return (_b = (_a = promptExecution.result) === null || _a === void 0 ? void 0 : _a.timing) === null || _b === void 0 ? void 0 : _b.start; })
2069
- .map(function (promptExecution) { return moment__default["default"](promptExecution.result.timing.start).valueOf(); })), false)));
2070
- var completedAt = moment__default["default"](Math.max.apply(Math, __spreadArray([], __read(executionReportJson.promptExecutions
2071
- .filter(function (promptExecution) { var _a, _b; return (_b = (_a = promptExecution.result) === null || _a === void 0 ? void 0 : _a.timing) === null || _b === void 0 ? void 0 : _b.complete; })
2072
- .map(function (promptExecution) { return moment__default["default"](promptExecution.result.timing.complete).valueOf(); })), false)));
2073
- var timingItems = executionReportJson.promptExecutions.map(function (promptExecution) {
2074
- var _a, _b, _c, _d;
2075
- return ({
2076
- title: promptExecution.prompt.title,
2077
- from: moment__default["default"]((_b = (_a = promptExecution.result) === null || _a === void 0 ? void 0 : _a.timing) === null || _b === void 0 ? void 0 : _b.start).valueOf() / 1000,
2078
- to: moment__default["default"]((_d = (_c = promptExecution.result) === null || _c === void 0 ? void 0 : _c.timing) === null || _d === void 0 ? void 0 : _d.complete).valueOf() / 1000,
2079
- });
2080
- });
2081
- var costItems = executionReportJson.promptExecutions
2082
- .filter(function (promptExecution) { var _a, _b; return typeof ((_b = (_a = promptExecution.result) === null || _a === void 0 ? void 0 : _a.usage) === null || _b === void 0 ? void 0 : _b.price) === 'number'; })
2083
- .map(function (promptExecution) {
2084
- var _a, _b;
2085
- return ({
2086
- title: promptExecution.prompt.title,
2087
- from: 0,
2088
- to: ((_b = (_a = promptExecution.result) === null || _a === void 0 ? void 0 : _a.usage) === null || _b === void 0 ? void 0 : _b.price) * (1 + taxRate),
2089
- });
2090
- });
2091
- var duration = moment__default["default"].duration(completedAt.diff(startedAt));
2092
- var llmDuration = moment__default["default"].duration(countWorkingDuration(timingItems) * 1000);
2093
- var executionsWithKnownCost = executionReportJson.promptExecutions.filter(function (promptExecution) { var _a, _b; return (((_b = (_a = promptExecution.result) === null || _a === void 0 ? void 0 : _a.usage) === null || _b === void 0 ? void 0 : _b.price) || 'UNKNOWN') !== 'UNKNOWN'; });
2094
- var cost = executionsWithKnownCost.reduce(function (cost, promptExecution) { return cost + (promptExecution.result.usage.price || 0); }, 0);
2095
- headerList.push("STARTED AT ".concat(moment__default["default"](startedAt).format("YYYY-MM-DD HH:mm:ss")));
2096
- headerList.push("COMPLETED AT ".concat(moment__default["default"](completedAt).format("YYYY-MM-DD HH:mm:ss")));
2097
- headerList.push("TOTAL DURATION ".concat(duration.humanize(MOMENT_ARG_THRESHOLDS)));
2098
- headerList.push("TOTAL LLM DURATION ".concat(llmDuration.humanize(MOMENT_ARG_THRESHOLDS)));
2099
- headerList.push("TOTAL COST $".concat(formatNumber(cost * (1 + taxRate))) +
2100
- (executionsWithKnownCost.length === executionReportJson.promptExecutions.length
2101
- ? ''
2102
- : " *(Some cost is unknown)*") +
2103
- (taxRate !== 0 ? " *(with tax ".concat(taxRate * 100, "%)*") : ''));
2104
- executionReportString += '\n\n' + headerList.map(function (header) { return "- ".concat(header); }).join('\n');
2105
- executionReportString +=
2106
- '\n\n' +
2107
- '## 🗃 Index' +
2108
- '\n\n' +
2109
- executionReportJson.promptExecutions
2110
- .map(function (promptExecution) {
2111
- // TODO: Make some better system to convert hedings to links
2112
- var hash = normalizeToKebabCase(promptExecution.prompt.title);
2113
- if (/^\s*\p{Extended_Pictographic}/u.test(promptExecution.prompt.title)) {
2114
- hash = '-' + hash;
2115
- }
2116
- // TODO: Make working hash link for the template in md + pdf
2117
- return "- [".concat(promptExecution.prompt.title, "](#").concat(hash, ")");
2118
- })
2119
- .join('\n');
2120
- executionReportString +=
2121
- '\n\n' +
2122
- '## ⌚ Time chart' +
2123
- '\n\n' +
2124
- createMarkdownChart({
2125
- nameHeader: 'Template',
2126
- valueHeader: 'Timeline',
2127
- items: timingItems,
2128
- width: chartsWidth,
2129
- unitName: 'seconds',
2130
- });
2131
- executionReportString +=
2132
- '\n\n' +
2133
- '## 💸 Cost chart' +
2134
- '\n\n' +
2135
- createMarkdownChart({
2136
- nameHeader: 'Template',
2137
- valueHeader: 'Cost',
2138
- items: costItems,
2139
- width: chartsWidth,
2140
- unitName: 'USD',
2141
- });
2142
- }
2143
- else {
2144
- headerList.push("TOTAL COST $0 *(Nothing executed)*");
2145
- }
2146
- var _loop_1 = function (promptExecution) {
2147
- executionReportString += '\n\n\n\n' + "## ".concat(promptExecution.prompt.title);
2148
- var templateList = [];
2149
- // TODO: What if startedAt OR/AND completedAt is not defined?
2150
- var startedAt = moment__default["default"]((_c = (_b = promptExecution.result) === null || _b === void 0 ? void 0 : _b.timing) === null || _c === void 0 ? void 0 : _c.start);
2151
- var completedAt = moment__default["default"]((_e = (_d = promptExecution.result) === null || _d === void 0 ? void 0 : _d.timing) === null || _e === void 0 ? void 0 : _e.complete);
2152
- var duration = moment__default["default"].duration(completedAt.diff(startedAt));
2153
- // Not need here:
2154
- // > templateList.push(`STARTED AT ${moment(startedAt).calendar()}`);
2155
- templateList.push("DURATION ".concat(duration.humanize(MOMENT_ARG_THRESHOLDS)));
2156
- if (typeof ((_g = (_f = promptExecution.result) === null || _f === void 0 ? void 0 : _f.usage) === null || _g === void 0 ? void 0 : _g.price) === 'number') {
2157
- templateList.push("COST $".concat(formatNumber(promptExecution.result.usage.price * (1 + taxRate))) +
2158
- (taxRate !== 0 ? " *(with tax ".concat(taxRate * 100, "%)*") : ''));
2159
- }
2160
- else {
2161
- templateList.push("COST UNKNOWN");
2162
- }
2163
- executionReportString += '\n\n' + templateList.map(function (header) { return "- ".concat(header); }).join('\n');
2164
- /*
2165
- - MODEL VARIANT ${promptExecution.prompt.modelRequirements.modelVariant}
2166
- - MODEL NAME \`${promptExecution.result?.model}\` (requested \`${
2167
- promptExecution.prompt.modelRequirements.modelName
2168
-
2169
- */
2170
- if (just(true)) {
2171
- executionReportString +=
2172
- '\n\n\n\n' +
2173
- spacetrim.spaceTrim(function (block) { return "\n\n ### Prompt\n\n ```\n ".concat(block(escapeMarkdownBlock(promptExecution.prompt.content)), "\n ```\n\n "); });
2174
- }
2175
- if (promptExecution.result && promptExecution.result.content) {
2176
- executionReportString +=
2177
- '\n\n\n\n' +
2178
- spacetrim.spaceTrim(function (block) { return "\n\n ### Result\n\n ```\n ".concat(block(escapeMarkdownBlock(promptExecution.result.content)), "\n ```\n "); });
2179
- }
2180
- if (promptExecution.error && promptExecution.error.message) {
2181
- executionReportString +=
2182
- '\n\n\n\n' +
2183
- spacetrim.spaceTrim(function (block) { return "\n\n ### Error\n\n ```\n ".concat(block(escapeMarkdownBlock(promptExecution.error.message)), "\n ```\n\n "); });
2184
- }
2185
- };
1071
+ var lines = markdown.split('\n');
1072
+ var listItems = [];
1073
+ var isInCodeBlock = false;
2186
1074
  try {
2187
- for (var _j = __values(executionReportJson.promptExecutions), _k = _j.next(); !_k.done; _k = _j.next()) {
2188
- var promptExecution = _k.value;
2189
- _loop_1(promptExecution);
1075
+ for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
1076
+ var line = lines_1_1.value;
1077
+ var trimmedLine = line.trim();
1078
+ if (trimmedLine.startsWith('```')) {
1079
+ isInCodeBlock = !isInCodeBlock;
1080
+ }
1081
+ if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
1082
+ var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
1083
+ listItems.push(listItem);
1084
+ }
2190
1085
  }
2191
1086
  }
2192
1087
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
2193
1088
  finally {
2194
1089
  try {
2195
- if (_k && !_k.done && (_a = _j.return)) _a.call(_j);
1090
+ if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
2196
1091
  }
2197
1092
  finally { if (e_1) throw e_1.error; }
2198
1093
  }
2199
- executionReportString = prettifyMarkdown(executionReportString);
2200
- return executionReportString;
1094
+ return listItems;
2201
1095
  }
1096
+
2202
1097
  /**
2203
- * TODO: Add mermaid chart for every report
2204
- * TODO: [🧠] Allow to filter out some parts of the report by options
1098
+ * Extracts exactly ONE code block from markdown.
1099
+ *
1100
+ * Note: If there are multiple or no code blocks the function throws an error
1101
+ *
1102
+ * Note: There are 3 simmilar function:
1103
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
1104
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
1105
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
1106
+ *
1107
+ * @param markdown any valid markdown
1108
+ * @returns code block with language and content
1109
+ */
1110
+ function extractOneBlockFromMarkdown(markdown) {
1111
+ var codeBlocks = extractAllBlocksFromMarkdown(markdown);
1112
+ if (codeBlocks.length !== 1) {
1113
+ // TODO: Report more specific place where the error happened
1114
+ throw new Error(/* <- [🌻] */ 'There should be exactly one code block in the markdown');
1115
+ }
1116
+ return codeBlocks[0];
1117
+ }
1118
+ /***
1119
+ * TODO: [🍓][🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
2205
1120
  */
2206
1121
 
2207
1122
  /**
2208
- * Function isValidJsonString will tell you if the string is valid JSON or not
1123
+ * Removes HTML or Markdown comments from a string.
1124
+ *
1125
+ * @param {string} content - The string to remove comments from.
1126
+ * @returns {string} The input string with all comments removed.
2209
1127
  */
2210
- function isValidJsonString(value) {
2211
- try {
2212
- JSON.parse(value);
2213
- return true;
2214
- }
2215
- catch (error) {
2216
- if (!(error instanceof Error)) {
2217
- throw error;
2218
- }
2219
- if (error.message.includes('Unexpected token')) {
2220
- return false;
2221
- }
2222
- return false;
2223
- }
1128
+ function removeContentComments(content) {
1129
+ return spacetrim.spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
1130
+ }
1131
+
1132
+ /**
1133
+ * Removes Markdown formatting tags from a string.
1134
+ *
1135
+ * @param {string} str - The string to remove Markdown tags from.
1136
+ * @returns {string} The input string with all Markdown tags removed.
1137
+ */
1138
+ function removeMarkdownFormatting(str) {
1139
+ // Remove bold formatting
1140
+ str = str.replace(/\*\*(.*?)\*\*/g, '$1');
1141
+ // Remove italic formatting
1142
+ str = str.replace(/\*(.*?)\*/g, '$1');
1143
+ // Remove code formatting
1144
+ str = str.replace(/`(.*?)`/g, '$1');
1145
+ return str;
2224
1146
  }
2225
1147
 
2226
1148
  /**
@@ -2632,6 +1554,44 @@
2632
1554
  return intersection;
2633
1555
  }
2634
1556
 
1557
+ /**
1558
+ * Creates a new set with all elements that are present in either set
1559
+ */
1560
+ function union() {
1561
+ var e_1, _a, e_2, _b;
1562
+ var sets = [];
1563
+ for (var _i = 0; _i < arguments.length; _i++) {
1564
+ sets[_i] = arguments[_i];
1565
+ }
1566
+ var union = new Set();
1567
+ try {
1568
+ for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
1569
+ var set = sets_1_1.value;
1570
+ try {
1571
+ for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
1572
+ var item = _d.value;
1573
+ union.add(item);
1574
+ }
1575
+ }
1576
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1577
+ finally {
1578
+ try {
1579
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
1580
+ }
1581
+ finally { if (e_2) throw e_2.error; }
1582
+ }
1583
+ }
1584
+ }
1585
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1586
+ finally {
1587
+ try {
1588
+ if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
1589
+ }
1590
+ finally { if (e_1) throw e_1.error; }
1591
+ }
1592
+ return union;
1593
+ }
1594
+
2635
1595
  /**
2636
1596
  * Function trimCodeBlock will trim starting and ending code block from the string if it is present.
2637
1597
  *
@@ -2755,10 +1715,7 @@
2755
1715
  });
2756
1716
  exports.CountUtils = CountUtils;
2757
1717
  exports.DIACRITIC_VARIANTS_LETTERS = DIACRITIC_VARIANTS_LETTERS;
2758
- exports.ExecutionReportStringOptionsDefaults = ExecutionReportStringOptionsDefaults;
2759
- exports.assertsExecutionSuccessful = assertsExecutionSuccessful;
2760
1718
  exports.capitalize = capitalize;
2761
- exports.checkExpectations = checkExpectations;
2762
1719
  exports.countCharacters = countCharacters;
2763
1720
  exports.countLines = countLines;
2764
1721
  exports.countPages = countPages;
@@ -2767,7 +1724,6 @@
2767
1724
  exports.countWords = countWords;
2768
1725
  exports.decapitalize = decapitalize;
2769
1726
  exports.difference = difference;
2770
- exports.executionReportJsonToString = executionReportJsonToString;
2771
1727
  exports.extractAllBlocksFromMarkdown = extractAllBlocksFromMarkdown;
2772
1728
  exports.extractAllListItemsFromMarkdown = extractAllListItemsFromMarkdown;
2773
1729
  exports.extractBlock = extractBlock;
@@ -2775,8 +1731,8 @@
2775
1731
  exports.extractParameters = extractParameters;
2776
1732
  exports.extractParametersFromPromptTemplate = extractParametersFromPromptTemplate;
2777
1733
  exports.extractVariables = extractVariables;
1734
+ exports.forEachAsync = forEachAsync;
2778
1735
  exports.intersection = intersection;
2779
- exports.isPassingExpectations = isPassingExpectations;
2780
1736
  exports.isValidJsonString = isValidJsonString;
2781
1737
  exports.isValidKeyword = isValidKeyword;
2782
1738
  exports.nameToUriPart = nameToUriPart;
@@ -2791,7 +1747,6 @@
2791
1747
  exports.parseKeywords = parseKeywords;
2792
1748
  exports.parseKeywordsFromString = parseKeywordsFromString;
2793
1749
  exports.parseNumber = parseNumber;
2794
- exports.prettifyPromptbookString = prettifyPromptbookString;
2795
1750
  exports.removeContentComments = removeContentComments;
2796
1751
  exports.removeDiacritics = removeDiacritics;
2797
1752
  exports.removeEmojis = removeEmojis;