@promptbook/cli 0.44.0-1 → 0.44.0-11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/esm/index.es.js +459 -441
  2. package/esm/index.es.js.map +1 -1
  3. package/esm/typings/_packages/core.index.d.ts +2 -1
  4. package/esm/typings/_packages/utils.index.d.ts +3 -2
  5. package/esm/typings/config.d.ts +4 -0
  6. package/esm/typings/execution/plugins/natural-execution-tools/mocked/MockedEchoNaturalExecutionTools.d.ts +1 -1
  7. package/esm/typings/execution/plugins/natural-execution-tools/mocked/MockedFackedNaturalExecutionTools.d.ts +19 -0
  8. package/esm/typings/execution/plugins/natural-execution-tools/mocked/fakeTextToExpectations.d.ts +15 -0
  9. package/esm/typings/execution/plugins/natural-execution-tools/mocked/fakeTextToExpectations.test.d.ts +1 -0
  10. package/esm/typings/execution/plugins/natural-execution-tools/mocked/faked-completion.test.d.ts +1 -0
  11. package/esm/typings/execution/plugins/script-execution-tools/javascript/JavascriptExecutionToolsOptions.d.ts +5 -1
  12. package/esm/typings/execution/utils/checkExpectations.d.ts +25 -0
  13. package/esm/typings/execution/utils/checkExpectations.test.d.ts +1 -0
  14. package/esm/typings/types/Prompt.d.ts +13 -0
  15. package/esm/typings/types/PromptbookJson/PromptTemplateJson.d.ts +14 -4
  16. package/package.json +3 -3
  17. package/umd/index.umd.js +487 -470
  18. package/umd/index.umd.js.map +1 -1
  19. package/umd/typings/_packages/core.index.d.ts +2 -1
  20. package/umd/typings/_packages/utils.index.d.ts +3 -2
  21. package/umd/typings/config.d.ts +4 -0
  22. package/umd/typings/execution/plugins/natural-execution-tools/mocked/MockedEchoNaturalExecutionTools.d.ts +1 -1
  23. package/umd/typings/execution/plugins/natural-execution-tools/mocked/MockedFackedNaturalExecutionTools.d.ts +19 -0
  24. package/umd/typings/execution/plugins/natural-execution-tools/mocked/fakeTextToExpectations.d.ts +15 -0
  25. package/umd/typings/execution/plugins/natural-execution-tools/mocked/fakeTextToExpectations.test.d.ts +1 -0
  26. package/umd/typings/execution/plugins/natural-execution-tools/mocked/faked-completion.test.d.ts +1 -0
  27. package/umd/typings/execution/plugins/script-execution-tools/javascript/JavascriptExecutionToolsOptions.d.ts +5 -1
  28. package/umd/typings/execution/utils/checkExpectations.d.ts +25 -0
  29. package/umd/typings/execution/utils/checkExpectations.test.d.ts +1 -0
  30. package/umd/typings/types/Prompt.d.ts +13 -0
  31. package/umd/typings/types/PromptbookJson/PromptTemplateJson.d.ts +14 -4
package/esm/index.es.js CHANGED
@@ -2,7 +2,7 @@ import colors from 'colors';
2
2
  import commander from 'commander';
3
3
  import { writeFile, readFile } from 'fs/promises';
4
4
  import glob from 'glob-promise';
5
- import spaceTrim from 'spacetrim';
5
+ import { spaceTrim } from 'spacetrim';
6
6
  import { forTime } from 'waitasecond';
7
7
  import { format } from 'prettier';
8
8
  import parserHtml from 'prettier/parser-html';
@@ -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-10';
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
  *
@@ -627,53 +241,22 @@ function parseNumber(value) {
627
241
  })(Error));
628
242
 
629
243
  /**
630
- * This error occurs during the parameter replacement in the template
244
+ * This error occurs when some expectation is not met in the execution of the pipeline
631
245
  *
632
- * Note: This is a kindof subtype of PromptbookExecutionError because it occurs during the execution of the pipeline
246
+ * @private Always catched and rethrown as `PromptbookExecutionError`
247
+ * Note: This is a kindof subtype of PromptbookExecutionError
633
248
  */
634
249
  /** @class */ ((function (_super) {
635
- __extends(TemplateError, _super);
636
- function TemplateError(message) {
250
+ __extends(ExpectError, _super);
251
+ function ExpectError(message) {
637
252
  var _this = _super.call(this, message) || this;
638
- _this.name = 'TemplateError';
639
- Object.setPrototypeOf(_this, TemplateError.prototype);
253
+ _this.name = 'ExpectError';
254
+ Object.setPrototypeOf(_this, ExpectError.prototype);
640
255
  return _this;
641
256
  }
642
- return TemplateError;
257
+ return ExpectError;
643
258
  })(Error));
644
259
 
645
- /**
646
- * Prettify the html code
647
- *
648
- * @param html raw html code
649
- * @returns formatted html code
650
- */
651
- function prettifyMarkdown(html) {
652
- try {
653
- return format(html, {
654
- parser: 'markdown',
655
- plugins: [parserHtml],
656
- // TODO: DRY - make some import or auto-copy of .prettierrc
657
- endOfLine: 'lf',
658
- tabWidth: 4,
659
- singleQuote: true,
660
- trailingComma: 'all',
661
- arrowParens: 'always',
662
- printWidth: 120,
663
- htmlWhitespaceSensitivity: 'ignore',
664
- jsxBracketSameLine: false,
665
- bracketSpacing: true,
666
- });
667
- }
668
- catch (error) {
669
- console.error('There was an error with prettifying the markdown, using the original as the fallback', {
670
- error: error,
671
- html: html,
672
- });
673
- return html;
674
- }
675
- }
676
-
677
260
  var defaultDiacriticsRemovalMap = [
678
261
  {
679
262
  base: 'A',
@@ -901,21 +484,242 @@ for (var i = 0; i < defaultDiacriticsRemovalMap.length; i++) {
901
484
  defaultDiacriticsRemovalMap[i].base;
902
485
  }
903
486
  }
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
487
+ // <- TODO: !!!! Put to maker function
488
+ /*
489
+ @see https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
490
+ Licensed under the Apache License, Version 2.0 (the "License");
491
+ you may not use this file except in compliance with the License.
492
+ You may obtain a copy of the License at
493
+
494
+ http://www.apache.org/licenses/LICENSE-2.0
495
+
496
+ Unless required by applicable law or agreed to in writing, software
497
+ distributed under the License is distributed on an "AS IS" BASIS,
498
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
499
+ See the License for the specific language governing permissions and
500
+ limitations under the License.
501
+ */
502
+
503
+ /**
504
+ * The maximum number of iterations for a loops
505
+ */
506
+ var LOOP_LIMIT = 1000;
507
+
508
+ /**
509
+ * This error occurs during the parameter replacement in the template
510
+ *
511
+ * Note: This is a kindof subtype of PromptbookExecutionError because it occurs during the execution of the pipeline
512
+ */
513
+ /** @class */ ((function (_super) {
514
+ __extends(TemplateError, _super);
515
+ function TemplateError(message) {
516
+ var _this = _super.call(this, message) || this;
517
+ _this.name = 'TemplateError';
518
+ Object.setPrototypeOf(_this, TemplateError.prototype);
519
+ return _this;
520
+ }
521
+ return TemplateError;
522
+ })(Error));
523
+
524
+ /**
525
+ * This error type indicates that the error should not happen and its last check before crashing with some other error
526
+ */
527
+ var UnexpectedError = /** @class */ (function (_super) {
528
+ __extends(UnexpectedError, _super);
529
+ function UnexpectedError(message) {
530
+ 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;
531
+ _this.name = 'UnexpectedError';
532
+ Object.setPrototypeOf(_this, UnexpectedError.prototype);
533
+ return _this;
534
+ }
535
+ return UnexpectedError;
536
+ }(Error));
537
+
538
+ /**
539
+ * Prettify the html code
540
+ *
541
+ * @param html raw html code
542
+ * @returns formatted html code
543
+ */
544
+ function prettifyMarkdown(html) {
545
+ try {
546
+ return format(html, {
547
+ parser: 'markdown',
548
+ plugins: [parserHtml],
549
+ // TODO: DRY - make some import or auto-copy of .prettierrc
550
+ endOfLine: 'lf',
551
+ tabWidth: 4,
552
+ singleQuote: true,
553
+ trailingComma: 'all',
554
+ arrowParens: 'always',
555
+ printWidth: 120,
556
+ htmlWhitespaceSensitivity: 'ignore',
557
+ jsxBracketSameLine: false,
558
+ bracketSpacing: true,
559
+ });
560
+ }
561
+ catch (error) {
562
+ console.error('There was an error with prettifying the markdown, using the original as the fallback', {
563
+ error: error,
564
+ html: html,
565
+ });
566
+ return html;
567
+ }
568
+ }
569
+
570
+ /**
571
+ * Makes first letter of a string uppercase
572
+ *
573
+ */
574
+ function capitalize(word) {
575
+ return word.substring(0, 1).toUpperCase() + word.substring(1);
576
+ }
577
+
578
+ /**
579
+ * Extracts all code blocks from markdown.
580
+ *
581
+ * Note: There are 3 simmilar function:
582
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
583
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
584
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
585
+ *
586
+ * @param markdown any valid markdown
587
+ * @returns code blocks with language and content
588
+ *
589
+ */
590
+ function extractAllBlocksFromMarkdown(markdown) {
591
+ var e_1, _a;
592
+ var codeBlocks = [];
593
+ var lines = markdown.split('\n');
594
+ var currentCodeBlock = null;
595
+ try {
596
+ for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
597
+ var line = lines_1_1.value;
598
+ if (line.startsWith('```')) {
599
+ var language = line.slice(3).trim() || null;
600
+ if (currentCodeBlock === null) {
601
+ currentCodeBlock = { language: language, content: '' };
602
+ }
603
+ else {
604
+ if (language !== null) {
605
+ // [🌻]
606
+ throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
607
+ }
608
+ codeBlocks.push(currentCodeBlock);
609
+ currentCodeBlock = null;
610
+ }
611
+ }
612
+ else if (currentCodeBlock !== null) {
613
+ if (currentCodeBlock.content !== '') {
614
+ currentCodeBlock.content += '\n';
615
+ }
616
+ currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
617
+ }
618
+ }
619
+ }
620
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
621
+ finally {
622
+ try {
623
+ if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
624
+ }
625
+ finally { if (e_1) throw e_1.error; }
626
+ }
627
+ if (currentCodeBlock !== null) {
628
+ // [🌻]
629
+ throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
630
+ }
631
+ return codeBlocks;
632
+ }
633
+
634
+ /**
635
+ * Utility function to extract all list items from markdown
636
+ *
637
+ * Note: It works with both ul and ol
638
+ * Note: It omits list items in code blocks
639
+ * Note: It flattens nested lists
640
+ * Note: It can not work with html syntax and comments
641
+ *
642
+ * @param markdown any valid markdown
643
+ * @returns
644
+ */
645
+ function extractAllListItemsFromMarkdown(markdown) {
646
+ var e_1, _a;
647
+ var lines = markdown.split('\n');
648
+ var listItems = [];
649
+ var isInCodeBlock = false;
650
+ try {
651
+ for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
652
+ var line = lines_1_1.value;
653
+ var trimmedLine = line.trim();
654
+ if (trimmedLine.startsWith('```')) {
655
+ isInCodeBlock = !isInCodeBlock;
656
+ }
657
+ if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
658
+ var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
659
+ listItems.push(listItem);
660
+ }
661
+ }
662
+ }
663
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
664
+ finally {
665
+ try {
666
+ if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
667
+ }
668
+ finally { if (e_1) throw e_1.error; }
669
+ }
670
+ return listItems;
671
+ }
672
+
673
+ /**
674
+ * Extracts exactly ONE code block from markdown.
675
+ *
676
+ * Note: If there are multiple or no code blocks the function throws an error
677
+ *
678
+ * Note: There are 3 simmilar function:
679
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
680
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
681
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
682
+ *
683
+ * @param markdown any valid markdown
684
+ * @returns code block with language and content
685
+ */
686
+ function extractOneBlockFromMarkdown(markdown) {
687
+ var codeBlocks = extractAllBlocksFromMarkdown(markdown);
688
+ if (codeBlocks.length !== 1) {
689
+ // TODO: Report more specific place where the error happened
690
+ throw new Error(/* <- [🌻] */ 'There should be exactly one code block in the markdown');
691
+ }
692
+ return codeBlocks[0];
693
+ }
694
+ /***
695
+ * TODO: [🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
696
+ */
910
697
 
911
- http://www.apache.org/licenses/LICENSE-2.0
698
+ /**
699
+ * Removes HTML or Markdown comments from a string.
700
+ *
701
+ * @param {string} content - The string to remove comments from.
702
+ * @returns {string} The input string with all comments removed.
703
+ */
704
+ function removeContentComments(content) {
705
+ return spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
706
+ }
912
707
 
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
- */
708
+ /**
709
+ * Removes Markdown formatting tags from a string.
710
+ *
711
+ * @param {string} str - The string to remove Markdown tags from.
712
+ * @returns {string} The input string with all Markdown tags removed.
713
+ */
714
+ function removeMarkdownFormatting(str) {
715
+ // Remove bold formatting
716
+ str = str.replace(/\*\*(.*?)\*\*/g, '$1');
717
+ // Remove italic formatting
718
+ str = str.replace(/\*(.*?)\*/g, '$1');
719
+ // Remove code formatting
720
+ str = str.replace(/`(.*?)`/g, '$1');
721
+ return str;
722
+ }
919
723
 
920
724
  /* tslint:disable */
921
725
  /*
@@ -1040,6 +844,220 @@ function normalizeTo_PascalCase(sentence) {
1040
844
  return normalizeTo_camelCase(sentence, true);
1041
845
  }
1042
846
 
847
+ /**
848
+ * Supported script languages
849
+ */
850
+ var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
851
+
852
+ /**
853
+ * Parses the template and returns the list of all parameter names
854
+ *
855
+ * @param template the template with parameters in {curly} braces
856
+ * @returns the list of parameter names
857
+ *
858
+ * @private within the library
859
+ */
860
+ function extractParameters(template) {
861
+ var e_1, _a;
862
+ var matches = template.matchAll(/{\w+}/g);
863
+ var parameterNames = [];
864
+ try {
865
+ for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
866
+ var match = matches_1_1.value;
867
+ var parameterName = match[0].slice(1, -1);
868
+ if (!parameterNames.includes(parameterName)) {
869
+ parameterNames.push(parameterName);
870
+ }
871
+ }
872
+ }
873
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
874
+ finally {
875
+ try {
876
+ if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
877
+ }
878
+ finally { if (e_1) throw e_1.error; }
879
+ }
880
+ return parameterNames;
881
+ }
882
+
883
+ /**
884
+ * Computes the deepness of the markdown structure.
885
+ *
886
+ * @private within the library
887
+ */
888
+ function countMarkdownStructureDeepness(markdownStructure) {
889
+ var e_1, _a;
890
+ var maxDeepness = 0;
891
+ try {
892
+ for (var _b = __values(markdownStructure.sections), _c = _b.next(); !_c.done; _c = _b.next()) {
893
+ var section = _c.value;
894
+ maxDeepness = Math.max(maxDeepness, countMarkdownStructureDeepness(section));
895
+ }
896
+ }
897
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
898
+ finally {
899
+ try {
900
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
901
+ }
902
+ finally { if (e_1) throw e_1.error; }
903
+ }
904
+ return maxDeepness + 1;
905
+ }
906
+
907
+ /**
908
+ * Parse a markdown string into a MarkdownStructure object.
909
+ *
910
+ * Note: This function does work with code blocks
911
+ * Note: This function does not work with markdown comments
912
+ *
913
+ * @param markdown The markdown string to parse.
914
+ * @returns The MarkdownStructure object.
915
+ *
916
+ * @private within the library
917
+ */
918
+ function markdownToMarkdownStructure(markdown) {
919
+ var e_1, _a;
920
+ var lines = markdown.split('\n');
921
+ var root = { level: 0, title: '', contentLines: [], sections: [], parent: null };
922
+ var current = root;
923
+ var isInsideCodeBlock = false;
924
+ try {
925
+ for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
926
+ var line = lines_1_1.value;
927
+ var headingMatch = line.match(/^(?<mark>#{1,6})\s(?<title>.*)/);
928
+ if (isInsideCodeBlock || !headingMatch) {
929
+ if (line.startsWith('```')) {
930
+ isInsideCodeBlock = !isInsideCodeBlock;
931
+ }
932
+ current.contentLines.push(line);
933
+ }
934
+ else {
935
+ var level = headingMatch.groups.mark.length;
936
+ var title = headingMatch.groups.title.trim();
937
+ var parent_1 = void 0;
938
+ if (level > current.level) {
939
+ // Note: Going deeper (next section is child of current)
940
+ parent_1 = current;
941
+ }
942
+ else {
943
+ // Note: Going up or staying at the same level (next section is sibling or parent or grandparent,... of current)
944
+ parent_1 = current;
945
+ var loopLimit = LOOP_LIMIT;
946
+ while (parent_1.level !== level - 1) {
947
+ if (loopLimit-- < 0) {
948
+ throw new UnexpectedError('Loop limit reached during parsing of markdown structure in `markdownToMarkdownStructure`');
949
+ }
950
+ if (parent_1.parent === null /* <- Note: We are in root */) {
951
+ // [🌻]
952
+ throw new Error(spaceTrim("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
953
+ }
954
+ parent_1 = parent_1.parent;
955
+ }
956
+ }
957
+ var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
958
+ parent_1.sections.push(section);
959
+ current = section;
960
+ }
961
+ }
962
+ }
963
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
964
+ finally {
965
+ try {
966
+ if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
967
+ }
968
+ finally { if (e_1) throw e_1.error; }
969
+ }
970
+ if (root.sections.length === 1) {
971
+ var markdownStructure = parsingMarkdownStructureToMarkdownStructure(root.sections[0]);
972
+ return markdownStructure;
973
+ }
974
+ // [🌻]
975
+ throw new Error('The markdown file must have exactly one top-level section.');
976
+ // return root;
977
+ }
978
+ /**
979
+ * @private
980
+ */
981
+ function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
982
+ var level = parsingMarkdownStructure.level, title = parsingMarkdownStructure.title, contentLines = parsingMarkdownStructure.contentLines, sections = parsingMarkdownStructure.sections;
983
+ return {
984
+ level: level,
985
+ title: title,
986
+ content: spaceTrim(contentLines.join('\n')),
987
+ sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
988
+ };
989
+ }
990
+
991
+ /**
992
+ * Parses the given script and returns the list of all used variables that are not defined in the script
993
+ *
994
+ * @param script from which to extract the variables
995
+ * @returns the list of variable names
996
+ * @throws {PromptbookSyntaxError} if the script is invalid
997
+ *
998
+ * @private within the promptbookStringToJson
999
+ */
1000
+ function extractVariables(script) {
1001
+ var variables = [];
1002
+ script = "(()=>{".concat(script, "})()");
1003
+ try {
1004
+ for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
1005
+ try {
1006
+ eval(script);
1007
+ }
1008
+ catch (error) {
1009
+ if (!(error instanceof ReferenceError)) {
1010
+ throw error;
1011
+ }
1012
+ var undefinedName = error.message.split(' ')[0];
1013
+ /*
1014
+ Note: Remapping error
1015
+ From: [ReferenceError: thing is not defined],
1016
+ To: [Error: Parameter {thing} is not defined],
1017
+ */
1018
+ if (!undefinedName) {
1019
+ throw error;
1020
+ }
1021
+ if (script.includes(undefinedName + '(')) {
1022
+ script = "const ".concat(undefinedName, " = ()=>'';") + script;
1023
+ }
1024
+ else {
1025
+ variables.push(undefinedName);
1026
+ script = "const ".concat(undefinedName, " = '';") + script;
1027
+ }
1028
+ }
1029
+ }
1030
+ catch (error) {
1031
+ if (!(error instanceof Error)) {
1032
+ throw error;
1033
+ }
1034
+ 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 "); }));
1035
+ }
1036
+ return variables;
1037
+ }
1038
+
1039
+ /**
1040
+ * Execution type describes the way how the block is executed
1041
+ *
1042
+ * @see https://github.com/webgptorg/promptbook#execution-type
1043
+ */
1044
+ var ExecutionTypes = [
1045
+ 'PROMPT_TEMPLATE',
1046
+ 'SIMPLE_TEMPLATE',
1047
+ 'SCRIPT',
1048
+ 'PROMPT_DIALOG',
1049
+ // <- [🥻] Insert here when making new command
1050
+ ];
1051
+
1052
+ /**
1053
+ * Units of text measurement
1054
+ */
1055
+ var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'PARAGRAPHS', 'LINES', 'PAGES'];
1056
+ /**
1057
+ * TODO: [💝] Unite object for expecting amount and format - remove expectFormat
1058
+ * TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
1059
+ */
1060
+
1043
1061
  /**
1044
1062
  * Parses one line of ul/ol to command
1045
1063
  *
@@ -1533,7 +1551,7 @@ function promptbookStringToJson(promptbookString) {
1533
1551
  executionType: executionType,
1534
1552
  jokers: jokers,
1535
1553
  postprocessing: postprocessing,
1536
- expectAmount: expectAmount,
1554
+ expectations: expectAmount,
1537
1555
  expectFormat: expectFormat,
1538
1556
  modelRequirements: templateModelRequirements,
1539
1557
  contentLanguage: executionType === 'SCRIPT' ? language : undefined,