@promptbook/utils 0.52.0-3 → 0.52.0-5

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