@promptbook/utils 0.52.0-0 → 0.52.0-10

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 (62) hide show
  1. package/README.md +10 -6
  2. package/esm/index.es.js +464 -1614
  3. package/esm/index.es.js.map +1 -1
  4. package/esm/typings/_packages/anthropic-claude.index.d.ts +3 -0
  5. package/esm/typings/_packages/azure-openai.index.d.ts +3 -0
  6. package/esm/typings/_packages/core.index.d.ts +13 -5
  7. package/esm/typings/_packages/openai.index.d.ts +1 -1
  8. package/esm/typings/_packages/utils.index.d.ts +3 -8
  9. package/esm/typings/execution/PromptResult.d.ts +3 -3
  10. package/esm/typings/execution/plugins/llm-execution-tools/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +42 -0
  11. package/esm/typings/execution/plugins/llm-execution-tools/anthropic-claude/AnthropicClaudeExecutionToolsOptions.d.ts +8 -0
  12. package/esm/typings/execution/plugins/llm-execution-tools/anthropic-claude/anthropic-claude-models.d.ts +20 -0
  13. package/esm/typings/execution/plugins/llm-execution-tools/anthropic-claude/playground/playground.d.ts +2 -0
  14. package/esm/typings/execution/plugins/llm-execution-tools/azure-openai/AzureOpenAiExecutionTools.d.ts +41 -0
  15. package/esm/typings/execution/plugins/llm-execution-tools/azure-openai/AzureOpenAiExecutionToolsOptions.d.ts +34 -0
  16. package/esm/typings/execution/plugins/llm-execution-tools/azure-openai/playground/playground.d.ts +2 -0
  17. package/esm/typings/execution/plugins/llm-execution-tools/langtail/playground/playground.d.ts +2 -0
  18. package/esm/typings/execution/plugins/llm-execution-tools/mocked/MockedEchoLlmExecutionTools.d.ts +2 -2
  19. package/esm/typings/execution/plugins/llm-execution-tools/mocked/MockedFackedLlmExecutionTools.d.ts +2 -2
  20. package/esm/typings/execution/plugins/llm-execution-tools/multiple/MultipleLlmExecutionTools.d.ts +35 -0
  21. package/esm/typings/execution/plugins/llm-execution-tools/multiple/MultipleLlmExecutionToolsOptions.d.ts +23 -0
  22. package/esm/typings/execution/plugins/llm-execution-tools/multiple/playground/playground.d.ts +2 -0
  23. package/esm/typings/execution/plugins/llm-execution-tools/openai/OpenAiExecutionTools.d.ts +3 -3
  24. package/esm/typings/execution/plugins/llm-execution-tools/openai/OpenAiExecutionToolsOptions.d.ts +3 -1
  25. package/esm/typings/execution/plugins/llm-execution-tools/openai/computeUsage.d.ts +6 -3
  26. package/esm/typings/execution/plugins/llm-execution-tools/openai/{models.d.ts → openai-models.d.ts} +2 -1
  27. package/esm/typings/execution/plugins/llm-execution-tools/openai/playground/playground.d.ts +2 -0
  28. package/esm/typings/execution/plugins/script-execution-tools/javascript/JavascriptEvalExecutionTools.d.ts +1 -1
  29. package/esm/typings/execution/plugins/script-execution-tools/javascript/JavascriptExecutionTools.d.ts +4 -17
  30. package/esm/typings/library/constructors/createPromptbookLibraryFromDirectory.d.ts +16 -1
  31. package/package.json +1 -3
  32. package/umd/index.umd.js +462 -1620
  33. package/umd/index.umd.js.map +1 -1
  34. package/umd/typings/_packages/anthropic-claude.index.d.ts +3 -0
  35. package/umd/typings/_packages/azure-openai.index.d.ts +3 -0
  36. package/umd/typings/_packages/core.index.d.ts +13 -5
  37. package/umd/typings/_packages/openai.index.d.ts +1 -1
  38. package/umd/typings/_packages/utils.index.d.ts +3 -8
  39. package/umd/typings/execution/PromptResult.d.ts +3 -3
  40. package/umd/typings/execution/plugins/llm-execution-tools/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +42 -0
  41. package/umd/typings/execution/plugins/llm-execution-tools/anthropic-claude/AnthropicClaudeExecutionToolsOptions.d.ts +8 -0
  42. package/umd/typings/execution/plugins/llm-execution-tools/anthropic-claude/anthropic-claude-models.d.ts +20 -0
  43. package/umd/typings/execution/plugins/llm-execution-tools/anthropic-claude/playground/playground.d.ts +2 -0
  44. package/umd/typings/execution/plugins/llm-execution-tools/azure-openai/AzureOpenAiExecutionTools.d.ts +41 -0
  45. package/umd/typings/execution/plugins/llm-execution-tools/azure-openai/AzureOpenAiExecutionToolsOptions.d.ts +34 -0
  46. package/umd/typings/execution/plugins/llm-execution-tools/azure-openai/playground/playground.d.ts +2 -0
  47. package/umd/typings/execution/plugins/llm-execution-tools/langtail/playground/playground.d.ts +2 -0
  48. package/umd/typings/execution/plugins/llm-execution-tools/mocked/MockedEchoLlmExecutionTools.d.ts +2 -2
  49. package/umd/typings/execution/plugins/llm-execution-tools/mocked/MockedFackedLlmExecutionTools.d.ts +2 -2
  50. package/umd/typings/execution/plugins/llm-execution-tools/multiple/MultipleLlmExecutionTools.d.ts +35 -0
  51. package/umd/typings/execution/plugins/llm-execution-tools/multiple/MultipleLlmExecutionToolsOptions.d.ts +23 -0
  52. package/umd/typings/execution/plugins/llm-execution-tools/multiple/playground/playground.d.ts +2 -0
  53. package/umd/typings/execution/plugins/llm-execution-tools/openai/OpenAiExecutionTools.d.ts +3 -3
  54. package/umd/typings/execution/plugins/llm-execution-tools/openai/OpenAiExecutionToolsOptions.d.ts +3 -1
  55. package/umd/typings/execution/plugins/llm-execution-tools/openai/computeUsage.d.ts +6 -3
  56. package/umd/typings/execution/plugins/llm-execution-tools/openai/{models.d.ts → openai-models.d.ts} +2 -1
  57. package/umd/typings/execution/plugins/llm-execution-tools/openai/playground/playground.d.ts +2 -0
  58. package/umd/typings/execution/plugins/script-execution-tools/javascript/JavascriptEvalExecutionTools.d.ts +1 -1
  59. package/umd/typings/execution/plugins/script-execution-tools/javascript/JavascriptExecutionTools.d.ts +4 -17
  60. package/umd/typings/library/constructors/createPromptbookLibraryFromDirectory.d.ts +16 -1
  61. package/esm/typings/_packages/wizzard.index.d.ts +0 -5
  62. package/umd/typings/_packages/wizzard.index.d.ts +0 -5
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.
@@ -91,501 +86,246 @@
91
86
  }
92
87
 
93
88
  /**
94
- * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
89
+ * This error type indicates that the error should not happen and its last check before crashing with some other error
95
90
  */
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);
91
+ var UnexpectedError = /** @class */ (function (_super) {
92
+ __extends(UnexpectedError, _super);
93
+ function UnexpectedError(message) {
94
+ 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;
95
+ _this.name = 'UnexpectedError';
96
+ Object.setPrototypeOf(_this, UnexpectedError.prototype);
102
97
  return _this;
103
98
  }
104
- return PromptbookSyntaxError;
99
+ return UnexpectedError;
105
100
  }(Error));
106
101
 
107
102
  /**
108
- * Supported script languages
103
+ * Removes emojis from a string and fix whitespaces
104
+ *
105
+ * @param text with emojis
106
+ * @returns text without emojis
107
+ */
108
+ function removeEmojis(text) {
109
+ // Replace emojis (and also ZWJ sequence) with hyphens
110
+ text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
111
+ text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
112
+ text = text.replace(/(\p{Extended_Pictographic})(\u{200D}\p{Extended_Pictographic})*/gu, '$1');
113
+ text = text.replace(/\p{Extended_Pictographic}/gu, '');
114
+ return text;
115
+ }
116
+
117
+ /**
118
+ * Function normalizes title to name which can be used as identifier
119
+ */
120
+ function titleToName(value) {
121
+ value = removeEmojis(value);
122
+ value = normalizeToKebabCase(value);
123
+ // TODO: [🧠] Maybe warn or add some padding to short name which are not good identifiers
124
+ return value;
125
+ }
126
+
127
+ /**
128
+ * Creates a Mermaid graph based on the promptbook
129
+ *
130
+ * Note: The result is not wrapped in a Markdown code block
131
+ */
132
+ function renderPromptbookMermaid(promptbookJson, options) {
133
+ var _a = (options || {}).linkPromptTemplate, linkPromptTemplate = _a === void 0 ? function () { return null; } : _a;
134
+ var parameterNameToTemplateName = function (parameterName) {
135
+ var parameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
136
+ if (!parameter) {
137
+ throw new UnexpectedError("Could not find {".concat(parameterName, "}"));
138
+ }
139
+ if (parameter.isInput) {
140
+ return 'input';
141
+ }
142
+ var template = promptbookJson.promptTemplates.find(function (template) { return template.resultingParameterName === parameterName; });
143
+ if (!template) {
144
+ throw new Error("Could not find template for {".concat(parameterName, "}"));
145
+ }
146
+ return normalizeTo_camelCase('template-' + titleToName(template.title));
147
+ };
148
+ 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
149
+ .flatMap(function (_a) {
150
+ var title = _a.title, dependentParameterNames = _a.dependentParameterNames, resultingParameterName = _a.resultingParameterName;
151
+ return __spreadArray([
152
+ "".concat(parameterNameToTemplateName(resultingParameterName), "(\"").concat(title, "\")")
153
+ ], __read(dependentParameterNames.map(function (dependentParameterName) {
154
+ return "".concat(parameterNameToTemplateName(dependentParameterName), "--\"{").concat(dependentParameterName, "}\"-->").concat(parameterNameToTemplateName(resultingParameterName));
155
+ })), false);
156
+ })
157
+ .join('\n')), "\n\n ").concat(block(promptbookJson.parameters
158
+ .filter(function (_a) {
159
+ var isOutput = _a.isOutput;
160
+ return isOutput;
161
+ })
162
+ .map(function (_a) {
163
+ var name = _a.name;
164
+ return "".concat(parameterNameToTemplateName(name), "--\"{").concat(name, "}\"-->output");
165
+ })
166
+ .join('\n')), "\n output((Output)):::output\n\n ").concat(block(promptbookJson.promptTemplates
167
+ .map(function (promptTemplate) {
168
+ var link = linkPromptTemplate(promptTemplate);
169
+ if (link === null) {
170
+ return '';
171
+ }
172
+ var href = link.href, title = link.title;
173
+ var templateName = parameterNameToTemplateName(promptTemplate.resultingParameterName);
174
+ return "click ".concat(templateName, " href \"").concat(href, "\" \"").concat(title, "\";");
175
+ })
176
+ .filter(function (line) { return line !== ''; })
177
+ .join('\n')), "\n\n classDef input color: grey;\n classDef output color: grey;\n\n end;\n\n "); });
178
+ return promptbookMermaid;
179
+ }
180
+ /**
181
+ * TODO: Maybe use some Mermaid library instead of string templating
182
+ * TODO: [🕌] When more than 2 functionalities, split into separate functions
109
183
  */
110
- var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
111
184
 
112
185
  /**
113
- * Computes the deepness of the markdown structure.
186
+ * Parses the template and returns the list of all parameter names
114
187
  *
115
- * @private within the library
188
+ * @param template the template with parameters in {curly} braces
189
+ * @returns the list of parameter names
116
190
  */
117
- function countMarkdownStructureDeepness(markdownStructure) {
191
+ function extractParameters(template) {
118
192
  var e_1, _a;
119
- var maxDeepness = 0;
193
+ var matches = template.matchAll(/{\w+}/g);
194
+ var parameterNames = new Set();
120
195
  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));
196
+ for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
197
+ var match = matches_1_1.value;
198
+ var parameterName = match[0].slice(1, -1);
199
+ parameterNames.add(parameterName);
124
200
  }
125
201
  }
126
202
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
127
203
  finally {
128
204
  try {
129
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
205
+ if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
130
206
  }
131
207
  finally { if (e_1) throw e_1.error; }
132
208
  }
133
- return maxDeepness + 1;
209
+ return parameterNames;
134
210
  }
135
211
 
136
212
  /**
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
213
+ * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
143
214
  */
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);
215
+ var PromptbookSyntaxError = /** @class */ (function (_super) {
216
+ __extends(PromptbookSyntaxError, _super);
217
+ function PromptbookSyntaxError(message) {
218
+ var _this = _super.call(this, message) || this;
219
+ _this.name = 'PromptbookSyntaxError';
220
+ Object.setPrototypeOf(_this, PromptbookSyntaxError.prototype);
150
221
  return _this;
151
222
  }
152
- return UnexpectedError;
223
+ return PromptbookSyntaxError;
153
224
  }(Error));
154
225
 
155
226
  /**
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.
227
+ * Parses the given script and returns the list of all used variables that are not defined in the script
163
228
  *
164
- * @private within the library
229
+ * @param script from which to extract the variables
230
+ * @returns the list of variable names
231
+ * @throws {PromptbookSyntaxError} if the script is invalid
165
232
  */
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;
233
+ function extractVariables(script) {
234
+ var variables = new Set();
235
+ script = "(()=>{".concat(script, "})()");
172
236
  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);
237
+ for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
238
+ try {
239
+ eval(script);
181
240
  }
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;
241
+ catch (error) {
242
+ if (!(error instanceof ReferenceError)) {
243
+ throw error;
244
+ }
245
+ var undefinedName = error.message.split(' ')[0];
246
+ /*
247
+ Note: Remapping error
248
+ From: [ReferenceError: thing is not defined],
249
+ To: [Error: Parameter {thing} is not defined],
250
+ */
251
+ if (!undefinedName) {
252
+ throw error;
253
+ }
254
+ if (script.includes(undefinedName + '(')) {
255
+ script = "const ".concat(undefinedName, " = ()=>'';") + script;
189
256
  }
190
257
  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
- }
258
+ variables.add(undefinedName);
259
+ script = "const ".concat(undefinedName, " = '';") + script;
204
260
  }
205
- var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
206
- parent_1.sections.push(section);
207
- current = section;
208
261
  }
209
- }
210
262
  }
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);
263
+ catch (error) {
264
+ if (!(error instanceof Error)) {
265
+ throw error;
215
266
  }
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;
267
+ 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
268
  }
222
- // [🌻]
223
- throw new Error('The markdown file must have exactly one top-level section.');
224
- // return root;
269
+ return variables;
225
270
  }
226
271
  /**
227
- * @private
272
+ * TODO: [🔣] Support for multiple languages - python, java,...
228
273
  */
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
274
 
239
275
  /**
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
276
+ * Parses the prompt template and returns the set of all used parameters
246
277
  *
247
- * @param markdown any valid markdown
248
- * @returns
278
+ * @param promptTemplate the template with used parameters
279
+ * @returns the set of parameter names
280
+ * @throws {PromptbookSyntaxError} if the script is invalid
249
281
  */
250
- function extractAllListItemsFromMarkdown(markdown) {
251
- var e_1, _a;
252
- var lines = markdown.split('\n');
253
- var listItems = [];
254
- var isInCodeBlock = false;
282
+ function extractParametersFromPromptTemplate(promptTemplate) {
283
+ var e_1, _a, e_2, _b;
284
+ var parameterNames = new Set();
255
285
  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
- }
286
+ 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()) {
287
+ var parameterName = _d.value;
288
+ parameterNames.add(parameterName);
266
289
  }
267
290
  }
268
291
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
269
292
  finally {
270
293
  try {
271
- if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
294
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
272
295
  }
273
296
  finally { if (e_1) throw e_1.error; }
274
297
  }
275
- return listItems;
298
+ if (promptTemplate.executionType === 'SCRIPT') {
299
+ try {
300
+ for (var _e = __values(extractVariables(promptTemplate.content)), _f = _e.next(); !_f.done; _f = _e.next()) {
301
+ var parameterName = _f.value;
302
+ parameterNames.add(parameterName);
303
+ }
304
+ }
305
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
306
+ finally {
307
+ try {
308
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
309
+ }
310
+ finally { if (e_2) throw e_2.error; }
311
+ }
312
+ }
313
+ return parameterNames;
276
314
  }
277
-
278
315
  /**
279
- * Makes first letter of a string uppercase
280
- *
316
+ * TODO: [🔣] If script require contentLanguage
281
317
  */
282
- function capitalize(word) {
283
- return word.substring(0, 1).toUpperCase() + word.substring(1);
284
- }
285
318
 
286
319
  /**
287
- * Extracts all code blocks from markdown.
320
+ * Function parseNumber will parse number from string
288
321
  *
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
322
+ * Unlike Number.parseInt, Number.parseFloat it will never ever result in NaN
323
+ * Note: it also works only with decimal numbers
293
324
  *
294
- * @param markdown any valid markdown
295
- * @returns code blocks with language and content
325
+ * @returns parsed number
326
+ * @throws {PromptbookSyntaxError} if the value is not a number
296
327
  *
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.51.0';
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
584
- *
585
- * @returns parsed number
586
- * @throws {PromptbookSyntaxError} if the value is not a number
587
- *
588
- * @private within the parseCommand
328
+ * @private within the parseCommand
589
329
  */
590
330
  function parseNumber(value) {
591
331
  var originalValue = value;
@@ -620,686 +360,27 @@
620
360
  var denominator = parseNumber(denominator_);
621
361
  if (denominator === 0) {
622
362
  throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\" because denominator is zero"));
623
- }
624
- return numerator / denominator;
625
- }
626
- 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
- });
1294
- }
1295
- if (isPrettifyed) {
1296
- promptbookString = prettifyMarkdown(promptbookString);
1297
- }
1298
- return promptbookString;
363
+ }
364
+ return numerator / denominator;
365
+ }
366
+ if (/^(NAN|NULL|NONE|UNDEFINED|ZERO|NO.*)$/.test(value)) {
367
+ return 0;
368
+ }
369
+ if (value.includes('E')) {
370
+ var _b = __read(value.split('E'), 2), significand = _b[0], exponent = _b[1];
371
+ return parseNumber(significand) * Math.pow(10, parseNumber(exponent));
372
+ }
373
+ if (!/^[0-9.]+$/.test(value) || value.split('.').length > 2) {
374
+ throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\""));
375
+ }
376
+ var num = parseFloat(value);
377
+ if (isNaN(num)) {
378
+ throw new PromptbookSyntaxError("Unexpected NaN when parsing number from \"".concat(originalValue, "\""));
379
+ }
380
+ return num;
1299
381
  }
1300
382
  /**
1301
- * TODO: Maybe use some Mermaid library instead of string templating
1302
- * TODO: [🕌] When more than 2 functionalities, split into separate functions
383
+ * TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
1303
384
  */
1304
385
 
1305
386
  /**
@@ -1373,60 +454,85 @@
1373
454
  }
1374
455
 
1375
456
  /**
1376
- * This error indicates errors during the execution of the promptbook
457
+ * The maximum number of iterations for a loops
458
+ */
459
+ var LOOP_LIMIT = 1000;
460
+
461
+ /**
462
+ * This error occurs during the parameter replacement in the template
463
+ *
464
+ * Note: This is a kindof subtype of PromptbookExecutionError because it occurs during the execution of the pipeline
1377
465
  */
1378
- var PromptbookExecutionError = /** @class */ (function (_super) {
1379
- __extends(PromptbookExecutionError, _super);
1380
- function PromptbookExecutionError(message) {
466
+ var TemplateError = /** @class */ (function (_super) {
467
+ __extends(TemplateError, _super);
468
+ function TemplateError(message) {
1381
469
  var _this = _super.call(this, message) || this;
1382
- _this.name = 'PromptbookExecutionError';
1383
- Object.setPrototypeOf(_this, PromptbookExecutionError.prototype);
470
+ _this.name = 'TemplateError';
471
+ Object.setPrototypeOf(_this, TemplateError.prototype);
1384
472
  return _this;
1385
473
  }
1386
- return PromptbookExecutionError;
474
+ return TemplateError;
1387
475
  }(Error));
1388
476
 
1389
477
  /**
1390
- * Asserts that the execution of a promptnook is successful
478
+ * Replaces parameters in template with values from parameters object
479
+ *
480
+ * @param template the template with parameters in {curly} braces
481
+ * @param parameters the object with parameters
482
+ * @returns the template with replaced parameters
483
+ * @throws {TemplateError} if parameter is not defined, not closed, or not opened
1391
484
  *
1392
- * @param executionResult - The partial result of the promptnook execution
1393
- * @throws {PromptbookExecutionError} If the execution is not successful or if multiple errors occurred
485
+ * @private within the createPromptbookExecutor
1394
486
  */
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");
487
+ function replaceParameters(template, parameters) {
488
+ var replacedTemplate = template;
489
+ var match;
490
+ var loopLimit = LOOP_LIMIT;
491
+ var _loop_1 = function () {
492
+ if (loopLimit-- < 0) {
493
+ throw new UnexpectedError('Loop limit reached during parameters replacement in `replaceParameters`');
494
+ }
495
+ var precol = match.groups.precol;
496
+ var parameterName = match.groups.parameterName;
497
+ if (parameterName === '') {
498
+ return "continue";
499
+ }
500
+ if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
501
+ throw new TemplateError('Parameter is already opened or not closed');
502
+ }
503
+ if (parameters[parameterName] === undefined) {
504
+ throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
505
+ }
506
+ var parameterValue = parameters[parameterName];
507
+ if (parameterValue === undefined) {
508
+ throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
509
+ }
510
+ parameterValue = parameterValue.toString();
511
+ if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
512
+ parameterValue = parameterValue
513
+ .split('\n')
514
+ .map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
515
+ .join('\n');
516
+ }
517
+ replacedTemplate =
518
+ replacedTemplate.substring(0, match.index + precol.length) +
519
+ parameterValue +
520
+ replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
521
+ };
522
+ while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
523
+ .exec(replacedTemplate))) {
524
+ _loop_1();
1402
525
  }
1403
- else if (errors.length === 1) {
1404
- throw errors[0];
526
+ // [💫] Check if there are parameters that are not closed properly
527
+ if (/{\w+$/.test(replacedTemplate)) {
528
+ throw new TemplateError('Parameter is not closed');
1405
529
  }
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 "); }));
530
+ // [💫] Check if there are parameters that are not opened properly
531
+ if (/^\w+}/.test(replacedTemplate)) {
532
+ throw new TemplateError('Parameter is not opened');
1408
533
  }
534
+ return replacedTemplate;
1409
535
  }
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
536
 
1431
537
  /**
1432
538
  * Counts number of characters in the text
@@ -1704,523 +810,227 @@
1704
810
  var letters = defaultDiacriticsRemovalMap[i].letters;
1705
811
  // tslint:disable-next-line: prefer-for-of
1706
812
  for (var j = 0; j < letters.length; j++) {
1707
- DIACRITIC_VARIANTS_LETTERS[letters[j]] = defaultDiacriticsRemovalMap[i].base;
1708
- }
1709
- }
1710
- // <- TODO: [🍓] Put to maker function to save execution time if not needed
1711
- /*
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');
813
+ DIACRITIC_VARIANTS_LETTERS[letters[j]] = defaultDiacriticsRemovalMap[i].base;
1886
814
  }
1887
- return replacedTemplate;
1888
815
  }
816
+ // <- TODO: [🍓] Put to maker function to save execution time if not needed
817
+ /*
818
+ @see https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
819
+ Licensed under the Apache License, Version 2.0 (the "License");
820
+ you may not use this file except in compliance with the License.
821
+ You may obtain a copy of the License at
822
+
823
+ http://www.apache.org/licenses/LICENSE-2.0
824
+
825
+ Unless required by applicable law or agreed to in writing, software
826
+ distributed under the License is distributed on an "AS IS" BASIS,
827
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
828
+ See the License for the specific language governing permissions and
829
+ limitations under the License.
830
+ */
1889
831
 
1890
832
  /**
1891
- * Format either small or big number
1892
833
  *
1893
- * @private within the library
1894
834
  */
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();
835
+ function removeDiacritics(input) {
836
+ /*eslint no-control-regex: "off"*/
837
+ return input.replace(/[^\u0000-\u007E]/g, function (a) {
838
+ return DIACRITIC_VARIANTS_LETTERS[a] || a;
839
+ });
1908
840
  }
1909
841
 
1910
842
  /**
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
843
+ * Counts number of words in the text
1918
844
  */
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;
845
+ function countWords(text) {
846
+ text = text.replace(/[\p{Extended_Pictographic}]/gu, 'a');
847
+ text = removeDiacritics(text);
848
+ return text.split(/[^a-zа-я0-9]+/i).filter(function (word) { return word.length > 0; }).length;
1925
849
  }
1926
850
 
1927
851
  /**
1928
- * Create a markdown table from a 2D array of strings
1929
- *
1930
- * @private within the library
852
+ * Index of all counter functions
1931
853
  */
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
- }
854
+ var CountUtils = {
855
+ CHARACTERS: countCharacters,
856
+ WORDS: countWords,
857
+ SENTENCES: countSentences,
858
+ PARAGRAPHS: countParagraphs,
859
+ LINES: countLines,
860
+ PAGES: countPages,
861
+ };
1954
862
 
1955
863
  /**
1956
- * Function createMarkdownChart will draw a chart in markdown from ⬛+🟦 tiles
1957
- *
1958
- * @private within the library
864
+ * Function isValidJsonString will tell you if the string is valid JSON or not
1959
865
  */
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]];
866
+ function isValidJsonString(value) {
1967
867
  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
- }
868
+ JSON.parse(value);
869
+ return true;
1975
870
  }
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);
871
+ catch (error) {
872
+ if (!(error instanceof Error)) {
873
+ throw error;
1980
874
  }
1981
- finally { if (e_1) throw e_1.error; }
875
+ if (error.message.includes('Unexpected token')) {
876
+ return false;
877
+ }
878
+ return false;
1982
879
  }
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
880
  }
1986
- /**
1987
- * TODO: Maybe use Mermain Gant Diagrams
1988
- * @see https://jojozhuang.github.io/tutorial/mermaid-cheat-sheet/
1989
- */
1990
881
 
1991
882
  /**
1992
- * Function escapeMarkdownBlock will escape markdown block if needed
1993
- * It is useful when you want have block in block
883
+ * Makes first letter of a string uppercase
884
+ *
1994
885
  */
1995
- function escapeMarkdownBlock(value) {
1996
- return value.replace(/```/g, '\\`\\`\\`');
886
+ function capitalize(word) {
887
+ return word.substring(0, 1).toUpperCase() + word.substring(1);
1997
888
  }
1998
889
 
1999
890
  /**
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.
891
+ * Extracts all code blocks from markdown.
2009
892
  *
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
893
+ * Note: There are 3 simmilar function:
894
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
895
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
896
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
897
+ *
898
+ * @param markdown any valid markdown
899
+ * @returns code blocks with language and content
2018
900
  *
2019
- * @private within the library
2020
901
  */
2021
- function countWorkingDuration(items) {
902
+ function extractAllBlocksFromMarkdown(markdown) {
2022
903
  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
- };
904
+ var codeBlocks = [];
905
+ var lines = markdown.split('\n');
906
+ var currentCodeBlock = null;
2033
907
  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);
908
+ for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
909
+ var line = lines_1_1.value;
910
+ if (line.startsWith('```')) {
911
+ var language = line.slice(3).trim() || null;
912
+ if (currentCodeBlock === null) {
913
+ currentCodeBlock = { language: language, content: '' };
914
+ }
915
+ else {
916
+ if (language !== null) {
917
+ // [🌻]
918
+ throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
919
+ }
920
+ codeBlocks.push(currentCodeBlock);
921
+ currentCodeBlock = null;
922
+ }
923
+ }
924
+ else if (currentCodeBlock !== null) {
925
+ if (currentCodeBlock.content !== '') {
926
+ currentCodeBlock.content += '\n';
927
+ }
928
+ currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
929
+ }
2037
930
  }
2038
931
  }
2039
932
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
2040
933
  finally {
2041
934
  try {
2042
- if (intervals_1_1 && !intervals_1_1.done && (_a = intervals_1.return)) _a.call(intervals_1);
935
+ if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
2043
936
  }
2044
937
  finally { if (e_1) throw e_1.error; }
2045
938
  }
2046
- return duration;
939
+ if (currentCodeBlock !== null) {
940
+ // [🌻]
941
+ throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
942
+ }
943
+ return codeBlocks;
2047
944
  }
2048
945
 
2049
946
  /**
2050
- * Converts execution report from JSON to string format
947
+ * Utility function to extract all list items from markdown
948
+ *
949
+ * Note: It works with both ul and ol
950
+ * Note: It omits list items in code blocks
951
+ * Note: It flattens nested lists
952
+ * Note: It can not work with html syntax and comments
953
+ *
954
+ * @param markdown any valid markdown
955
+ * @returns
2051
956
  */
2052
- function executionReportJsonToString(executionReportJson, options) {
957
+ function extractAllListItemsFromMarkdown(markdown) {
2053
958
  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
- };
959
+ var lines = markdown.split('\n');
960
+ var listItems = [];
961
+ var isInCodeBlock = false;
2186
962
  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);
963
+ for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
964
+ var line = lines_1_1.value;
965
+ var trimmedLine = line.trim();
966
+ if (trimmedLine.startsWith('```')) {
967
+ isInCodeBlock = !isInCodeBlock;
968
+ }
969
+ if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
970
+ var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
971
+ listItems.push(listItem);
972
+ }
2190
973
  }
2191
974
  }
2192
975
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
2193
976
  finally {
2194
977
  try {
2195
- if (_k && !_k.done && (_a = _j.return)) _a.call(_j);
978
+ if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
2196
979
  }
2197
980
  finally { if (e_1) throw e_1.error; }
2198
981
  }
2199
- executionReportString = prettifyMarkdown(executionReportString);
2200
- return executionReportString;
982
+ return listItems;
2201
983
  }
984
+
2202
985
  /**
2203
- * TODO: Add mermaid chart for every report
2204
- * TODO: [🧠] Allow to filter out some parts of the report by options
986
+ * Extracts exactly ONE code block from markdown.
987
+ *
988
+ * Note: If there are multiple or no code blocks the function throws an error
989
+ *
990
+ * Note: There are 3 simmilar function:
991
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
992
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
993
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
994
+ *
995
+ * @param markdown any valid markdown
996
+ * @returns code block with language and content
997
+ */
998
+ function extractOneBlockFromMarkdown(markdown) {
999
+ var codeBlocks = extractAllBlocksFromMarkdown(markdown);
1000
+ if (codeBlocks.length !== 1) {
1001
+ // TODO: Report more specific place where the error happened
1002
+ throw new Error(/* <- [🌻] */ 'There should be exactly one code block in the markdown');
1003
+ }
1004
+ return codeBlocks[0];
1005
+ }
1006
+ /***
1007
+ * TODO: [🍓][🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
2205
1008
  */
2206
1009
 
2207
1010
  /**
2208
- * Function isValidJsonString will tell you if the string is valid JSON or not
1011
+ * Removes HTML or Markdown comments from a string.
1012
+ *
1013
+ * @param {string} content - The string to remove comments from.
1014
+ * @returns {string} The input string with all comments removed.
2209
1015
  */
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
- }
1016
+ function removeContentComments(content) {
1017
+ return spacetrim.spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
1018
+ }
1019
+
1020
+ /**
1021
+ * Removes Markdown formatting tags from a string.
1022
+ *
1023
+ * @param {string} str - The string to remove Markdown tags from.
1024
+ * @returns {string} The input string with all Markdown tags removed.
1025
+ */
1026
+ function removeMarkdownFormatting(str) {
1027
+ // Remove bold formatting
1028
+ str = str.replace(/\*\*(.*?)\*\*/g, '$1');
1029
+ // Remove italic formatting
1030
+ str = str.replace(/\*(.*?)\*/g, '$1');
1031
+ // Remove code formatting
1032
+ str = str.replace(/`(.*?)`/g, '$1');
1033
+ return str;
2224
1034
  }
2225
1035
 
2226
1036
  /**
@@ -2632,6 +1442,44 @@
2632
1442
  return intersection;
2633
1443
  }
2634
1444
 
1445
+ /**
1446
+ * Creates a new set with all elements that are present in either set
1447
+ */
1448
+ function union() {
1449
+ var e_1, _a, e_2, _b;
1450
+ var sets = [];
1451
+ for (var _i = 0; _i < arguments.length; _i++) {
1452
+ sets[_i] = arguments[_i];
1453
+ }
1454
+ var union = new Set();
1455
+ try {
1456
+ for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
1457
+ var set = sets_1_1.value;
1458
+ try {
1459
+ for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
1460
+ var item = _d.value;
1461
+ union.add(item);
1462
+ }
1463
+ }
1464
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1465
+ finally {
1466
+ try {
1467
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
1468
+ }
1469
+ finally { if (e_2) throw e_2.error; }
1470
+ }
1471
+ }
1472
+ }
1473
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1474
+ finally {
1475
+ try {
1476
+ if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
1477
+ }
1478
+ finally { if (e_1) throw e_1.error; }
1479
+ }
1480
+ return union;
1481
+ }
1482
+
2635
1483
  /**
2636
1484
  * Function trimCodeBlock will trim starting and ending code block from the string if it is present.
2637
1485
  *
@@ -2755,10 +1603,7 @@
2755
1603
  });
2756
1604
  exports.CountUtils = CountUtils;
2757
1605
  exports.DIACRITIC_VARIANTS_LETTERS = DIACRITIC_VARIANTS_LETTERS;
2758
- exports.ExecutionReportStringOptionsDefaults = ExecutionReportStringOptionsDefaults;
2759
- exports.assertsExecutionSuccessful = assertsExecutionSuccessful;
2760
1606
  exports.capitalize = capitalize;
2761
- exports.checkExpectations = checkExpectations;
2762
1607
  exports.countCharacters = countCharacters;
2763
1608
  exports.countLines = countLines;
2764
1609
  exports.countPages = countPages;
@@ -2767,7 +1612,6 @@
2767
1612
  exports.countWords = countWords;
2768
1613
  exports.decapitalize = decapitalize;
2769
1614
  exports.difference = difference;
2770
- exports.executionReportJsonToString = executionReportJsonToString;
2771
1615
  exports.extractAllBlocksFromMarkdown = extractAllBlocksFromMarkdown;
2772
1616
  exports.extractAllListItemsFromMarkdown = extractAllListItemsFromMarkdown;
2773
1617
  exports.extractBlock = extractBlock;
@@ -2776,7 +1620,6 @@
2776
1620
  exports.extractParametersFromPromptTemplate = extractParametersFromPromptTemplate;
2777
1621
  exports.extractVariables = extractVariables;
2778
1622
  exports.intersection = intersection;
2779
- exports.isPassingExpectations = isPassingExpectations;
2780
1623
  exports.isValidJsonString = isValidJsonString;
2781
1624
  exports.isValidKeyword = isValidKeyword;
2782
1625
  exports.nameToUriPart = nameToUriPart;
@@ -2791,7 +1634,6 @@
2791
1634
  exports.parseKeywords = parseKeywords;
2792
1635
  exports.parseKeywordsFromString = parseKeywordsFromString;
2793
1636
  exports.parseNumber = parseNumber;
2794
- exports.prettifyPromptbookString = prettifyPromptbookString;
2795
1637
  exports.removeContentComments = removeContentComments;
2796
1638
  exports.removeDiacritics = removeDiacritics;
2797
1639
  exports.removeEmojis = removeEmojis;