@promptbook/cli 0.44.0-1 → 0.44.0-2

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
@@ -143,7 +143,7 @@ new Function("\n try {\n if (typeof WorkerGlobalScope !== 'undefined'
143
143
  /**
144
144
  * The version of the Promptbook library
145
145
  */
146
- var PROMPTBOOK_VERSION = '0.44.0-0';
146
+ var PROMPTBOOK_VERSION = '0.44.0-1';
147
147
 
148
148
  /**
149
149
  * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
@@ -159,392 +159,6 @@ var PromptbookSyntaxError = /** @class */ (function (_super) {
159
159
  return PromptbookSyntaxError;
160
160
  }(Error));
161
161
 
162
- /**
163
- * Supported script languages
164
- */
165
- var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
166
-
167
- /**
168
- * Parses the template and returns the list of all parameter names
169
- *
170
- * @param template the template with parameters in {curly} braces
171
- * @returns the list of parameter names
172
- *
173
- * @private within the library
174
- */
175
- function extractParameters(template) {
176
- var e_1, _a;
177
- var matches = template.matchAll(/{\w+}/g);
178
- var parameterNames = [];
179
- try {
180
- for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
181
- var match = matches_1_1.value;
182
- var parameterName = match[0].slice(1, -1);
183
- if (!parameterNames.includes(parameterName)) {
184
- parameterNames.push(parameterName);
185
- }
186
- }
187
- }
188
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
189
- finally {
190
- try {
191
- if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
192
- }
193
- finally { if (e_1) throw e_1.error; }
194
- }
195
- return parameterNames;
196
- }
197
-
198
- /**
199
- * Computes the deepness of the markdown structure.
200
- *
201
- * @private within the library
202
- */
203
- function countMarkdownStructureDeepness(markdownStructure) {
204
- var e_1, _a;
205
- var maxDeepness = 0;
206
- try {
207
- for (var _b = __values(markdownStructure.sections), _c = _b.next(); !_c.done; _c = _b.next()) {
208
- var section = _c.value;
209
- maxDeepness = Math.max(maxDeepness, countMarkdownStructureDeepness(section));
210
- }
211
- }
212
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
213
- finally {
214
- try {
215
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
216
- }
217
- finally { if (e_1) throw e_1.error; }
218
- }
219
- return maxDeepness + 1;
220
- }
221
-
222
- /**
223
- * The maximum number of iterations for a loops
224
- */
225
- var LOOP_LIMIT = 1000;
226
-
227
- /**
228
- * This error type indicates that the error should not happen and its last check before crashing with some other error
229
- */
230
- var UnexpectedError = /** @class */ (function (_super) {
231
- __extends(UnexpectedError, _super);
232
- function UnexpectedError(message) {
233
- 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;
234
- _this.name = 'UnexpectedError';
235
- Object.setPrototypeOf(_this, UnexpectedError.prototype);
236
- return _this;
237
- }
238
- return UnexpectedError;
239
- }(Error));
240
-
241
- /**
242
- * Parse a markdown string into a MarkdownStructure object.
243
- *
244
- * Note: This function does work with code blocks
245
- * Note: This function does not work with markdown comments
246
- *
247
- * @param markdown The markdown string to parse.
248
- * @returns The MarkdownStructure object.
249
- *
250
- * @private within the library
251
- */
252
- function markdownToMarkdownStructure(markdown) {
253
- var e_1, _a;
254
- var lines = markdown.split('\n');
255
- var root = { level: 0, title: '', contentLines: [], sections: [], parent: null };
256
- var current = root;
257
- var isInsideCodeBlock = false;
258
- try {
259
- for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
260
- var line = lines_1_1.value;
261
- var headingMatch = line.match(/^(?<mark>#{1,6})\s(?<title>.*)/);
262
- if (isInsideCodeBlock || !headingMatch) {
263
- if (line.startsWith('```')) {
264
- isInsideCodeBlock = !isInsideCodeBlock;
265
- }
266
- current.contentLines.push(line);
267
- }
268
- else {
269
- var level = headingMatch.groups.mark.length;
270
- var title = headingMatch.groups.title.trim();
271
- var parent_1 = void 0;
272
- if (level > current.level) {
273
- // Note: Going deeper (next section is child of current)
274
- parent_1 = current;
275
- }
276
- else {
277
- // Note: Going up or staying at the same level (next section is sibling or parent or grandparent,... of current)
278
- parent_1 = current;
279
- var loopLimit = LOOP_LIMIT;
280
- while (parent_1.level !== level - 1) {
281
- if (loopLimit-- < 0) {
282
- throw new UnexpectedError('Loop limit reached during parsing of markdown structure in `markdownToMarkdownStructure`');
283
- }
284
- if (parent_1.parent === null /* <- Note: We are in root */) {
285
- // [🌻]
286
- throw new Error(spaceTrim("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
287
- }
288
- parent_1 = parent_1.parent;
289
- }
290
- }
291
- var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
292
- parent_1.sections.push(section);
293
- current = section;
294
- }
295
- }
296
- }
297
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
298
- finally {
299
- try {
300
- if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
301
- }
302
- finally { if (e_1) throw e_1.error; }
303
- }
304
- if (root.sections.length === 1) {
305
- var markdownStructure = parsingMarkdownStructureToMarkdownStructure(root.sections[0]);
306
- return markdownStructure;
307
- }
308
- // [🌻]
309
- throw new Error('The markdown file must have exactly one top-level section.');
310
- // return root;
311
- }
312
- /**
313
- * @private
314
- */
315
- function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
316
- var level = parsingMarkdownStructure.level, title = parsingMarkdownStructure.title, contentLines = parsingMarkdownStructure.contentLines, sections = parsingMarkdownStructure.sections;
317
- return {
318
- level: level,
319
- title: title,
320
- content: spaceTrim(contentLines.join('\n')),
321
- sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
322
- };
323
- }
324
-
325
- /**
326
- * Utility function to extract all list items from markdown
327
- *
328
- * Note: It works with both ul and ol
329
- * Note: It omits list items in code blocks
330
- * Note: It flattens nested lists
331
- * Note: It can not work with html syntax and comments
332
- *
333
- * @param markdown any valid markdown
334
- * @returns
335
- */
336
- function extractAllListItemsFromMarkdown(markdown) {
337
- var e_1, _a;
338
- var lines = markdown.split('\n');
339
- var listItems = [];
340
- var isInCodeBlock = false;
341
- try {
342
- for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
343
- var line = lines_1_1.value;
344
- var trimmedLine = line.trim();
345
- if (trimmedLine.startsWith('```')) {
346
- isInCodeBlock = !isInCodeBlock;
347
- }
348
- if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
349
- var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
350
- listItems.push(listItem);
351
- }
352
- }
353
- }
354
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
355
- finally {
356
- try {
357
- if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
358
- }
359
- finally { if (e_1) throw e_1.error; }
360
- }
361
- return listItems;
362
- }
363
-
364
- /**
365
- * Makes first letter of a string uppercase
366
- *
367
- */
368
- function capitalize(word) {
369
- return word.substring(0, 1).toUpperCase() + word.substring(1);
370
- }
371
-
372
- /**
373
- * Extracts all code blocks from markdown.
374
- *
375
- * Note: There are 3 simmilar function:
376
- * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
377
- * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
378
- * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
379
- *
380
- * @param markdown any valid markdown
381
- * @returns code blocks with language and content
382
- *
383
- */
384
- function extractAllBlocksFromMarkdown(markdown) {
385
- var e_1, _a;
386
- var codeBlocks = [];
387
- var lines = markdown.split('\n');
388
- var currentCodeBlock = null;
389
- try {
390
- for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
391
- var line = lines_1_1.value;
392
- if (line.startsWith('```')) {
393
- var language = line.slice(3).trim() || null;
394
- if (currentCodeBlock === null) {
395
- currentCodeBlock = { language: language, content: '' };
396
- }
397
- else {
398
- if (language !== null) {
399
- // [🌻]
400
- throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
401
- }
402
- codeBlocks.push(currentCodeBlock);
403
- currentCodeBlock = null;
404
- }
405
- }
406
- else if (currentCodeBlock !== null) {
407
- if (currentCodeBlock.content !== '') {
408
- currentCodeBlock.content += '\n';
409
- }
410
- currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
411
- }
412
- }
413
- }
414
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
415
- finally {
416
- try {
417
- if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
418
- }
419
- finally { if (e_1) throw e_1.error; }
420
- }
421
- if (currentCodeBlock !== null) {
422
- // [🌻]
423
- throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
424
- }
425
- return codeBlocks;
426
- }
427
-
428
- /**
429
- * Extracts exactly ONE code block from markdown.
430
- *
431
- * Note: If there are multiple or no code blocks the function throws an error
432
- *
433
- * Note: There are 3 simmilar function:
434
- * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
435
- * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
436
- * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
437
- *
438
- * @param markdown any valid markdown
439
- * @returns code block with language and content
440
- */
441
- function extractOneBlockFromMarkdown(markdown) {
442
- var codeBlocks = extractAllBlocksFromMarkdown(markdown);
443
- if (codeBlocks.length !== 1) {
444
- // TODO: Report more specific place where the error happened
445
- throw new Error(/* <- [🌻] */ 'There should be exactly one code block in the markdown');
446
- }
447
- return codeBlocks[0];
448
- }
449
- /***
450
- * TODO: [🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
451
- */
452
-
453
- /**
454
- * Removes HTML or Markdown comments from a string.
455
- *
456
- * @param {string} content - The string to remove comments from.
457
- * @returns {string} The input string with all comments removed.
458
- */
459
- function removeContentComments(content) {
460
- return spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
461
- }
462
-
463
- /**
464
- * Parses the given script and returns the list of all used variables that are not defined in the script
465
- *
466
- * @param script from which to extract the variables
467
- * @returns the list of variable names
468
- * @throws {PromptbookSyntaxError} if the script is invalid
469
- *
470
- * @private within the promptbookStringToJson
471
- */
472
- function extractVariables(script) {
473
- var variables = [];
474
- script = "(()=>{".concat(script, "})()");
475
- try {
476
- for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
477
- try {
478
- eval(script);
479
- }
480
- catch (error) {
481
- if (!(error instanceof ReferenceError)) {
482
- throw error;
483
- }
484
- var undefinedName = error.message.split(' ')[0];
485
- /*
486
- Note: Remapping error
487
- From: [ReferenceError: thing is not defined],
488
- To: [Error: Parameter {thing} is not defined],
489
- */
490
- if (!undefinedName) {
491
- throw error;
492
- }
493
- if (script.includes(undefinedName + '(')) {
494
- script = "const ".concat(undefinedName, " = ()=>'';") + script;
495
- }
496
- else {
497
- variables.push(undefinedName);
498
- script = "const ".concat(undefinedName, " = '';") + script;
499
- }
500
- }
501
- }
502
- catch (error) {
503
- if (!(error instanceof Error)) {
504
- throw error;
505
- }
506
- 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 "); }));
507
- }
508
- return variables;
509
- }
510
-
511
- /**
512
- * Execution type describes the way how the block is executed
513
- *
514
- * @see https://github.com/webgptorg/promptbook#execution-type
515
- */
516
- var ExecutionTypes = [
517
- 'PROMPT_TEMPLATE',
518
- 'SIMPLE_TEMPLATE',
519
- 'SCRIPT',
520
- 'PROMPT_DIALOG',
521
- // <- [🥻] Insert here when making new command
522
- ];
523
-
524
- /**
525
- * Units of text measurement
526
- */
527
- var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'PARAGRAPHS', 'LINES', 'PAGES'];
528
- /**
529
- * TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
530
- */
531
-
532
- /**
533
- * Removes Markdown formatting tags from a string.
534
- *
535
- * @param {string} str - The string to remove Markdown tags from.
536
- * @returns {string} The input string with all Markdown tags removed.
537
- */
538
- function removeMarkdownFormatting(str) {
539
- // Remove bold formatting
540
- str = str.replace(/\*\*(.*?)\*\*/g, '$1');
541
- // Remove italic formatting
542
- str = str.replace(/\*(.*?)\*/g, '$1');
543
- // Remove code formatting
544
- str = str.replace(/`(.*?)`/g, '$1');
545
- return str;
546
- }
547
-
548
162
  /**
549
163
  * Function parseNumber will parse number from string
550
164
  *
@@ -626,6 +240,11 @@ function parseNumber(value) {
626
240
  return PromptbookExecutionError;
627
241
  })(Error));
628
242
 
243
+ /**
244
+ * The maximum number of iterations for a loops
245
+ */
246
+ var LOOP_LIMIT = 1000;
247
+
629
248
  /**
630
249
  * This error occurs during the parameter replacement in the template
631
250
  *
@@ -642,6 +261,20 @@ function parseNumber(value) {
642
261
  return TemplateError;
643
262
  })(Error));
644
263
 
264
+ /**
265
+ * This error type indicates that the error should not happen and its last check before crashing with some other error
266
+ */
267
+ var UnexpectedError = /** @class */ (function (_super) {
268
+ __extends(UnexpectedError, _super);
269
+ function UnexpectedError(message) {
270
+ 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;
271
+ _this.name = 'UnexpectedError';
272
+ Object.setPrototypeOf(_this, UnexpectedError.prototype);
273
+ return _this;
274
+ }
275
+ return UnexpectedError;
276
+ }(Error));
277
+
645
278
  /**
646
279
  * Prettify the html code
647
280
  *
@@ -901,21 +534,175 @@ for (var i = 0; i < defaultDiacriticsRemovalMap.length; i++) {
901
534
  defaultDiacriticsRemovalMap[i].base;
902
535
  }
903
536
  }
904
- // <- TODO: !!!! Put to maker function
905
- /*
906
- @see https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
907
- Licensed under the Apache License, Version 2.0 (the "License");
908
- you may not use this file except in compliance with the License.
909
- You may obtain a copy of the License at
537
+ // <- TODO: !!!! Put to maker function
538
+ /*
539
+ @see https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
540
+ Licensed under the Apache License, Version 2.0 (the "License");
541
+ you may not use this file except in compliance with the License.
542
+ You may obtain a copy of the License at
543
+
544
+ http://www.apache.org/licenses/LICENSE-2.0
545
+
546
+ Unless required by applicable law or agreed to in writing, software
547
+ distributed under the License is distributed on an "AS IS" BASIS,
548
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
549
+ See the License for the specific language governing permissions and
550
+ limitations under the License.
551
+ */
552
+
553
+ /**
554
+ * Makes first letter of a string uppercase
555
+ *
556
+ */
557
+ function capitalize(word) {
558
+ return word.substring(0, 1).toUpperCase() + word.substring(1);
559
+ }
560
+
561
+ /**
562
+ * Extracts all code blocks from markdown.
563
+ *
564
+ * Note: There are 3 simmilar function:
565
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
566
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
567
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
568
+ *
569
+ * @param markdown any valid markdown
570
+ * @returns code blocks with language and content
571
+ *
572
+ */
573
+ function extractAllBlocksFromMarkdown(markdown) {
574
+ var e_1, _a;
575
+ var codeBlocks = [];
576
+ var lines = markdown.split('\n');
577
+ var currentCodeBlock = null;
578
+ try {
579
+ for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
580
+ var line = lines_1_1.value;
581
+ if (line.startsWith('```')) {
582
+ var language = line.slice(3).trim() || null;
583
+ if (currentCodeBlock === null) {
584
+ currentCodeBlock = { language: language, content: '' };
585
+ }
586
+ else {
587
+ if (language !== null) {
588
+ // [🌻]
589
+ throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
590
+ }
591
+ codeBlocks.push(currentCodeBlock);
592
+ currentCodeBlock = null;
593
+ }
594
+ }
595
+ else if (currentCodeBlock !== null) {
596
+ if (currentCodeBlock.content !== '') {
597
+ currentCodeBlock.content += '\n';
598
+ }
599
+ currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
600
+ }
601
+ }
602
+ }
603
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
604
+ finally {
605
+ try {
606
+ if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
607
+ }
608
+ finally { if (e_1) throw e_1.error; }
609
+ }
610
+ if (currentCodeBlock !== null) {
611
+ // [🌻]
612
+ throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
613
+ }
614
+ return codeBlocks;
615
+ }
616
+
617
+ /**
618
+ * Utility function to extract all list items from markdown
619
+ *
620
+ * Note: It works with both ul and ol
621
+ * Note: It omits list items in code blocks
622
+ * Note: It flattens nested lists
623
+ * Note: It can not work with html syntax and comments
624
+ *
625
+ * @param markdown any valid markdown
626
+ * @returns
627
+ */
628
+ function extractAllListItemsFromMarkdown(markdown) {
629
+ var e_1, _a;
630
+ var lines = markdown.split('\n');
631
+ var listItems = [];
632
+ var isInCodeBlock = false;
633
+ try {
634
+ for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
635
+ var line = lines_1_1.value;
636
+ var trimmedLine = line.trim();
637
+ if (trimmedLine.startsWith('```')) {
638
+ isInCodeBlock = !isInCodeBlock;
639
+ }
640
+ if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
641
+ var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
642
+ listItems.push(listItem);
643
+ }
644
+ }
645
+ }
646
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
647
+ finally {
648
+ try {
649
+ if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
650
+ }
651
+ finally { if (e_1) throw e_1.error; }
652
+ }
653
+ return listItems;
654
+ }
655
+
656
+ /**
657
+ * Extracts exactly ONE code block from markdown.
658
+ *
659
+ * Note: If there are multiple or no code blocks the function throws an error
660
+ *
661
+ * Note: There are 3 simmilar function:
662
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
663
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
664
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
665
+ *
666
+ * @param markdown any valid markdown
667
+ * @returns code block with language and content
668
+ */
669
+ function extractOneBlockFromMarkdown(markdown) {
670
+ var codeBlocks = extractAllBlocksFromMarkdown(markdown);
671
+ if (codeBlocks.length !== 1) {
672
+ // TODO: Report more specific place where the error happened
673
+ throw new Error(/* <- [🌻] */ 'There should be exactly one code block in the markdown');
674
+ }
675
+ return codeBlocks[0];
676
+ }
677
+ /***
678
+ * TODO: [🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
679
+ */
910
680
 
911
- http://www.apache.org/licenses/LICENSE-2.0
681
+ /**
682
+ * Removes HTML or Markdown comments from a string.
683
+ *
684
+ * @param {string} content - The string to remove comments from.
685
+ * @returns {string} The input string with all comments removed.
686
+ */
687
+ function removeContentComments(content) {
688
+ return spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
689
+ }
912
690
 
913
- Unless required by applicable law or agreed to in writing, software
914
- distributed under the License is distributed on an "AS IS" BASIS,
915
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
916
- See the License for the specific language governing permissions and
917
- limitations under the License.
918
- */
691
+ /**
692
+ * Removes Markdown formatting tags from a string.
693
+ *
694
+ * @param {string} str - The string to remove Markdown tags from.
695
+ * @returns {string} The input string with all Markdown tags removed.
696
+ */
697
+ function removeMarkdownFormatting(str) {
698
+ // Remove bold formatting
699
+ str = str.replace(/\*\*(.*?)\*\*/g, '$1');
700
+ // Remove italic formatting
701
+ str = str.replace(/\*(.*?)\*/g, '$1');
702
+ // Remove code formatting
703
+ str = str.replace(/`(.*?)`/g, '$1');
704
+ return str;
705
+ }
919
706
 
920
707
  /* tslint:disable */
921
708
  /*
@@ -1040,6 +827,219 @@ function normalizeTo_PascalCase(sentence) {
1040
827
  return normalizeTo_camelCase(sentence, true);
1041
828
  }
1042
829
 
830
+ /**
831
+ * Supported script languages
832
+ */
833
+ var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
834
+
835
+ /**
836
+ * Parses the template and returns the list of all parameter names
837
+ *
838
+ * @param template the template with parameters in {curly} braces
839
+ * @returns the list of parameter names
840
+ *
841
+ * @private within the library
842
+ */
843
+ function extractParameters(template) {
844
+ var e_1, _a;
845
+ var matches = template.matchAll(/{\w+}/g);
846
+ var parameterNames = [];
847
+ try {
848
+ for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
849
+ var match = matches_1_1.value;
850
+ var parameterName = match[0].slice(1, -1);
851
+ if (!parameterNames.includes(parameterName)) {
852
+ parameterNames.push(parameterName);
853
+ }
854
+ }
855
+ }
856
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
857
+ finally {
858
+ try {
859
+ if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
860
+ }
861
+ finally { if (e_1) throw e_1.error; }
862
+ }
863
+ return parameterNames;
864
+ }
865
+
866
+ /**
867
+ * Computes the deepness of the markdown structure.
868
+ *
869
+ * @private within the library
870
+ */
871
+ function countMarkdownStructureDeepness(markdownStructure) {
872
+ var e_1, _a;
873
+ var maxDeepness = 0;
874
+ try {
875
+ for (var _b = __values(markdownStructure.sections), _c = _b.next(); !_c.done; _c = _b.next()) {
876
+ var section = _c.value;
877
+ maxDeepness = Math.max(maxDeepness, countMarkdownStructureDeepness(section));
878
+ }
879
+ }
880
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
881
+ finally {
882
+ try {
883
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
884
+ }
885
+ finally { if (e_1) throw e_1.error; }
886
+ }
887
+ return maxDeepness + 1;
888
+ }
889
+
890
+ /**
891
+ * Parse a markdown string into a MarkdownStructure object.
892
+ *
893
+ * Note: This function does work with code blocks
894
+ * Note: This function does not work with markdown comments
895
+ *
896
+ * @param markdown The markdown string to parse.
897
+ * @returns The MarkdownStructure object.
898
+ *
899
+ * @private within the library
900
+ */
901
+ function markdownToMarkdownStructure(markdown) {
902
+ var e_1, _a;
903
+ var lines = markdown.split('\n');
904
+ var root = { level: 0, title: '', contentLines: [], sections: [], parent: null };
905
+ var current = root;
906
+ var isInsideCodeBlock = false;
907
+ try {
908
+ for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
909
+ var line = lines_1_1.value;
910
+ var headingMatch = line.match(/^(?<mark>#{1,6})\s(?<title>.*)/);
911
+ if (isInsideCodeBlock || !headingMatch) {
912
+ if (line.startsWith('```')) {
913
+ isInsideCodeBlock = !isInsideCodeBlock;
914
+ }
915
+ current.contentLines.push(line);
916
+ }
917
+ else {
918
+ var level = headingMatch.groups.mark.length;
919
+ var title = headingMatch.groups.title.trim();
920
+ var parent_1 = void 0;
921
+ if (level > current.level) {
922
+ // Note: Going deeper (next section is child of current)
923
+ parent_1 = current;
924
+ }
925
+ else {
926
+ // Note: Going up or staying at the same level (next section is sibling or parent or grandparent,... of current)
927
+ parent_1 = current;
928
+ var loopLimit = LOOP_LIMIT;
929
+ while (parent_1.level !== level - 1) {
930
+ if (loopLimit-- < 0) {
931
+ throw new UnexpectedError('Loop limit reached during parsing of markdown structure in `markdownToMarkdownStructure`');
932
+ }
933
+ if (parent_1.parent === null /* <- Note: We are in root */) {
934
+ // [🌻]
935
+ throw new Error(spaceTrim("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
936
+ }
937
+ parent_1 = parent_1.parent;
938
+ }
939
+ }
940
+ var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
941
+ parent_1.sections.push(section);
942
+ current = section;
943
+ }
944
+ }
945
+ }
946
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
947
+ finally {
948
+ try {
949
+ if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
950
+ }
951
+ finally { if (e_1) throw e_1.error; }
952
+ }
953
+ if (root.sections.length === 1) {
954
+ var markdownStructure = parsingMarkdownStructureToMarkdownStructure(root.sections[0]);
955
+ return markdownStructure;
956
+ }
957
+ // [🌻]
958
+ throw new Error('The markdown file must have exactly one top-level section.');
959
+ // return root;
960
+ }
961
+ /**
962
+ * @private
963
+ */
964
+ function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
965
+ var level = parsingMarkdownStructure.level, title = parsingMarkdownStructure.title, contentLines = parsingMarkdownStructure.contentLines, sections = parsingMarkdownStructure.sections;
966
+ return {
967
+ level: level,
968
+ title: title,
969
+ content: spaceTrim(contentLines.join('\n')),
970
+ sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
971
+ };
972
+ }
973
+
974
+ /**
975
+ * Parses the given script and returns the list of all used variables that are not defined in the script
976
+ *
977
+ * @param script from which to extract the variables
978
+ * @returns the list of variable names
979
+ * @throws {PromptbookSyntaxError} if the script is invalid
980
+ *
981
+ * @private within the promptbookStringToJson
982
+ */
983
+ function extractVariables(script) {
984
+ var variables = [];
985
+ script = "(()=>{".concat(script, "})()");
986
+ try {
987
+ for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
988
+ try {
989
+ eval(script);
990
+ }
991
+ catch (error) {
992
+ if (!(error instanceof ReferenceError)) {
993
+ throw error;
994
+ }
995
+ var undefinedName = error.message.split(' ')[0];
996
+ /*
997
+ Note: Remapping error
998
+ From: [ReferenceError: thing is not defined],
999
+ To: [Error: Parameter {thing} is not defined],
1000
+ */
1001
+ if (!undefinedName) {
1002
+ throw error;
1003
+ }
1004
+ if (script.includes(undefinedName + '(')) {
1005
+ script = "const ".concat(undefinedName, " = ()=>'';") + script;
1006
+ }
1007
+ else {
1008
+ variables.push(undefinedName);
1009
+ script = "const ".concat(undefinedName, " = '';") + script;
1010
+ }
1011
+ }
1012
+ }
1013
+ catch (error) {
1014
+ if (!(error instanceof Error)) {
1015
+ throw error;
1016
+ }
1017
+ 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 "); }));
1018
+ }
1019
+ return variables;
1020
+ }
1021
+
1022
+ /**
1023
+ * Execution type describes the way how the block is executed
1024
+ *
1025
+ * @see https://github.com/webgptorg/promptbook#execution-type
1026
+ */
1027
+ var ExecutionTypes = [
1028
+ 'PROMPT_TEMPLATE',
1029
+ 'SIMPLE_TEMPLATE',
1030
+ 'SCRIPT',
1031
+ 'PROMPT_DIALOG',
1032
+ // <- [🥻] Insert here when making new command
1033
+ ];
1034
+
1035
+ /**
1036
+ * Units of text measurement
1037
+ */
1038
+ var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'PARAGRAPHS', 'LINES', 'PAGES'];
1039
+ /**
1040
+ * TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
1041
+ */
1042
+
1043
1043
  /**
1044
1044
  * Parses one line of ul/ol to command
1045
1045
  *
@@ -1533,7 +1533,7 @@ function promptbookStringToJson(promptbookString) {
1533
1533
  executionType: executionType,
1534
1534
  jokers: jokers,
1535
1535
  postprocessing: postprocessing,
1536
- expectAmount: expectAmount,
1536
+ expectations: expectAmount,
1537
1537
  expectFormat: expectFormat,
1538
1538
  modelRequirements: templateModelRequirements,
1539
1539
  contentLanguage: executionType === 'SCRIPT' ? language : undefined,