@promptbook/core 0.47.0 → 0.48.0-1

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 (46) hide show
  1. package/README.md +12 -1
  2. package/esm/index.es.js +464 -407
  3. package/esm/index.es.js.map +1 -1
  4. package/esm/typings/_packages/core.index.d.ts +2 -1
  5. package/esm/typings/_packages/utils.index.d.ts +2 -1
  6. package/esm/typings/conversion/prettify/prettifyPromptbookStringCli.d.ts +1 -1
  7. package/esm/typings/conversion/promptbookJsonToString.d.ts +8 -0
  8. package/esm/typings/conversion/utils/titleToName.d.ts +4 -0
  9. package/esm/typings/conversion/utils/titleToName.test.d.ts +1 -0
  10. package/esm/typings/execution/ExecutionTools.d.ts +1 -1
  11. package/esm/typings/execution/LlmExecutionTools.d.ts +1 -0
  12. package/esm/typings/execution/plugins/llm-execution-tools/langtail/LangtailExecutionTools.d.ts +1 -1
  13. package/esm/typings/execution/plugins/llm-execution-tools/openai/OpenAiExecutionTools.d.ts +1 -1
  14. package/esm/typings/execution/plugins/llm-execution-tools/openai/computeOpenaiUsage.d.ts +4 -1
  15. package/esm/typings/execution/plugins/llm-execution-tools/remote/RemoteLlmExecutionTools.d.ts +1 -0
  16. package/esm/typings/library/SimplePromptbookLibrary.d.ts +2 -1
  17. package/esm/typings/library/constructors/createPromptbookLibraryFromDirectory.d.ts +10 -1
  18. package/esm/typings/library/constructors/createPromptbookLibraryFromPromise.d.ts +20 -1
  19. package/esm/typings/library/constructors/createPromptbookLibraryFromSources.d.ts +5 -6
  20. package/esm/typings/library/constructors/createPromptbookLibraryFromUrl.d.ts +13 -0
  21. package/esm/typings/library/constructors/createPromptbookSublibrary.d.ts +10 -1
  22. package/esm/typings/utils/markdown/extractOneBlockFromMarkdown.d.ts +1 -1
  23. package/package.json +1 -1
  24. package/umd/index.umd.js +506 -444
  25. package/umd/index.umd.js.map +1 -1
  26. package/umd/typings/_packages/core.index.d.ts +2 -1
  27. package/umd/typings/_packages/utils.index.d.ts +2 -1
  28. package/umd/typings/conversion/prettify/prettifyPromptbookStringCli.d.ts +1 -1
  29. package/umd/typings/conversion/promptbookJsonToString.d.ts +8 -0
  30. package/umd/typings/conversion/utils/titleToName.d.ts +4 -0
  31. package/umd/typings/conversion/utils/titleToName.test.d.ts +1 -0
  32. package/umd/typings/execution/ExecutionTools.d.ts +1 -1
  33. package/umd/typings/execution/LlmExecutionTools.d.ts +1 -0
  34. package/umd/typings/execution/plugins/llm-execution-tools/langtail/LangtailExecutionTools.d.ts +1 -1
  35. package/umd/typings/execution/plugins/llm-execution-tools/openai/OpenAiExecutionTools.d.ts +1 -1
  36. package/umd/typings/execution/plugins/llm-execution-tools/openai/computeOpenaiUsage.d.ts +4 -1
  37. package/umd/typings/execution/plugins/llm-execution-tools/remote/RemoteLlmExecutionTools.d.ts +1 -0
  38. package/umd/typings/library/SimplePromptbookLibrary.d.ts +2 -1
  39. package/umd/typings/library/constructors/createPromptbookLibraryFromDirectory.d.ts +10 -1
  40. package/umd/typings/library/constructors/createPromptbookLibraryFromPromise.d.ts +20 -1
  41. package/umd/typings/library/constructors/createPromptbookLibraryFromSources.d.ts +5 -6
  42. package/umd/typings/library/constructors/createPromptbookLibraryFromUrl.d.ts +13 -0
  43. package/umd/typings/library/constructors/createPromptbookSublibrary.d.ts +10 -1
  44. package/umd/typings/utils/markdown/extractOneBlockFromMarkdown.d.ts +1 -1
  45. package/esm/typings/library/constructors/createPromptbookLibraryFromList.d.ts +0 -5
  46. package/umd/typings/library/constructors/createPromptbookLibraryFromList.d.ts +0 -5
package/esm/index.es.js CHANGED
@@ -1,4 +1,4 @@
1
- import { spaceTrim } from 'spacetrim';
1
+ import spaceTrim$1, { spaceTrim } from 'spacetrim';
2
2
  import 'prettier';
3
3
  import 'prettier/parser-html';
4
4
  import 'moment';
@@ -122,6 +122,311 @@ function __spreadArray(to, from, pack) {
122
122
  return to.concat(ar || Array.prototype.slice.call(from));
123
123
  }
124
124
 
125
+ /**
126
+ * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
127
+ */
128
+ var PromptbookSyntaxError = /** @class */ (function (_super) {
129
+ __extends(PromptbookSyntaxError, _super);
130
+ function PromptbookSyntaxError(message) {
131
+ var _this = _super.call(this, message) || this;
132
+ _this.name = 'PromptbookSyntaxError';
133
+ Object.setPrototypeOf(_this, PromptbookSyntaxError.prototype);
134
+ return _this;
135
+ }
136
+ return PromptbookSyntaxError;
137
+ }(Error));
138
+
139
+ /**
140
+ * Supported script languages
141
+ */
142
+ var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
143
+
144
+ /**
145
+ * Parses the template and returns the list of all parameter names
146
+ *
147
+ * @param template the template with parameters in {curly} braces
148
+ * @returns the list of parameter names
149
+ *
150
+ * @private within the library
151
+ */
152
+ function extractParameters(template) {
153
+ var e_1, _a;
154
+ var matches = template.matchAll(/{\w+}/g);
155
+ var parameterNames = [];
156
+ try {
157
+ for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
158
+ var match = matches_1_1.value;
159
+ var parameterName = match[0].slice(1, -1);
160
+ if (!parameterNames.includes(parameterName)) {
161
+ parameterNames.push(parameterName);
162
+ }
163
+ }
164
+ }
165
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
166
+ finally {
167
+ try {
168
+ if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
169
+ }
170
+ finally { if (e_1) throw e_1.error; }
171
+ }
172
+ return parameterNames;
173
+ }
174
+
175
+ /**
176
+ * Computes the deepness of the markdown structure.
177
+ *
178
+ * @private within the library
179
+ */
180
+ function countMarkdownStructureDeepness(markdownStructure) {
181
+ var e_1, _a;
182
+ var maxDeepness = 0;
183
+ try {
184
+ for (var _b = __values(markdownStructure.sections), _c = _b.next(); !_c.done; _c = _b.next()) {
185
+ var section = _c.value;
186
+ maxDeepness = Math.max(maxDeepness, countMarkdownStructureDeepness(section));
187
+ }
188
+ }
189
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
190
+ finally {
191
+ try {
192
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
193
+ }
194
+ finally { if (e_1) throw e_1.error; }
195
+ }
196
+ return maxDeepness + 1;
197
+ }
198
+
199
+ /**
200
+ * The maximum number of iterations for a loops
201
+ */
202
+ var LOOP_LIMIT = 1000;
203
+
204
+ /**
205
+ * This error type indicates that the error should not happen and its last check before crashing with some other error
206
+ */
207
+ var UnexpectedError = /** @class */ (function (_super) {
208
+ __extends(UnexpectedError, _super);
209
+ function UnexpectedError(message) {
210
+ 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;
211
+ _this.name = 'UnexpectedError';
212
+ Object.setPrototypeOf(_this, UnexpectedError.prototype);
213
+ return _this;
214
+ }
215
+ return UnexpectedError;
216
+ }(Error));
217
+
218
+ /**
219
+ * Parse a markdown string into a MarkdownStructure object.
220
+ *
221
+ * Note: This function does work with code blocks
222
+ * Note: This function does not work with markdown comments
223
+ *
224
+ * @param markdown The markdown string to parse.
225
+ * @returns The MarkdownStructure object.
226
+ *
227
+ * @private within the library
228
+ */
229
+ function markdownToMarkdownStructure(markdown) {
230
+ var e_1, _a;
231
+ var lines = markdown.split('\n');
232
+ var root = { level: 0, title: '', contentLines: [], sections: [], parent: null };
233
+ var current = root;
234
+ var isInsideCodeBlock = false;
235
+ try {
236
+ for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
237
+ var line = lines_1_1.value;
238
+ var headingMatch = line.match(/^(?<mark>#{1,6})\s(?<title>.*)/);
239
+ if (isInsideCodeBlock || !headingMatch) {
240
+ if (line.startsWith('```')) {
241
+ isInsideCodeBlock = !isInsideCodeBlock;
242
+ }
243
+ current.contentLines.push(line);
244
+ }
245
+ else {
246
+ var level = headingMatch.groups.mark.length;
247
+ var title = headingMatch.groups.title.trim();
248
+ var parent_1 = void 0;
249
+ if (level > current.level) {
250
+ // Note: Going deeper (next section is child of current)
251
+ parent_1 = current;
252
+ }
253
+ else {
254
+ // Note: Going up or staying at the same level (next section is sibling or parent or grandparent,... of current)
255
+ parent_1 = current;
256
+ var loopLimit = LOOP_LIMIT;
257
+ while (parent_1.level !== level - 1) {
258
+ if (loopLimit-- < 0) {
259
+ throw new UnexpectedError('Loop limit reached during parsing of markdown structure in `markdownToMarkdownStructure`');
260
+ }
261
+ if (parent_1.parent === null /* <- Note: We are in root */) {
262
+ // [🌻]
263
+ throw new Error(spaceTrim("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
264
+ }
265
+ parent_1 = parent_1.parent;
266
+ }
267
+ }
268
+ var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
269
+ parent_1.sections.push(section);
270
+ current = section;
271
+ }
272
+ }
273
+ }
274
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
275
+ finally {
276
+ try {
277
+ if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
278
+ }
279
+ finally { if (e_1) throw e_1.error; }
280
+ }
281
+ if (root.sections.length === 1) {
282
+ var markdownStructure = parsingMarkdownStructureToMarkdownStructure(root.sections[0]);
283
+ return markdownStructure;
284
+ }
285
+ // [🌻]
286
+ throw new Error('The markdown file must have exactly one top-level section.');
287
+ // return root;
288
+ }
289
+ /**
290
+ * @private
291
+ */
292
+ function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
293
+ var level = parsingMarkdownStructure.level, title = parsingMarkdownStructure.title, contentLines = parsingMarkdownStructure.contentLines, sections = parsingMarkdownStructure.sections;
294
+ return {
295
+ level: level,
296
+ title: title,
297
+ content: spaceTrim(contentLines.join('\n')),
298
+ sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
299
+ };
300
+ }
301
+
302
+ /**
303
+ * Utility function to extract all list items from markdown
304
+ *
305
+ * Note: It works with both ul and ol
306
+ * Note: It omits list items in code blocks
307
+ * Note: It flattens nested lists
308
+ * Note: It can not work with html syntax and comments
309
+ *
310
+ * @param markdown any valid markdown
311
+ * @returns
312
+ */
313
+ function extractAllListItemsFromMarkdown(markdown) {
314
+ var e_1, _a;
315
+ var lines = markdown.split('\n');
316
+ var listItems = [];
317
+ var isInCodeBlock = false;
318
+ try {
319
+ for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
320
+ var line = lines_1_1.value;
321
+ var trimmedLine = line.trim();
322
+ if (trimmedLine.startsWith('```')) {
323
+ isInCodeBlock = !isInCodeBlock;
324
+ }
325
+ if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
326
+ var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
327
+ listItems.push(listItem);
328
+ }
329
+ }
330
+ }
331
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
332
+ finally {
333
+ try {
334
+ if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
335
+ }
336
+ finally { if (e_1) throw e_1.error; }
337
+ }
338
+ return listItems;
339
+ }
340
+
341
+ /**
342
+ * Makes first letter of a string uppercase
343
+ *
344
+ */
345
+ function capitalize(word) {
346
+ return word.substring(0, 1).toUpperCase() + word.substring(1);
347
+ }
348
+
349
+ /**
350
+ * Extracts all code blocks from markdown.
351
+ *
352
+ * Note: There are 3 simmilar function:
353
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
354
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
355
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
356
+ *
357
+ * @param markdown any valid markdown
358
+ * @returns code blocks with language and content
359
+ *
360
+ */
361
+ function extractAllBlocksFromMarkdown(markdown) {
362
+ var e_1, _a;
363
+ var codeBlocks = [];
364
+ var lines = markdown.split('\n');
365
+ var currentCodeBlock = null;
366
+ try {
367
+ for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
368
+ var line = lines_1_1.value;
369
+ if (line.startsWith('```')) {
370
+ var language = line.slice(3).trim() || null;
371
+ if (currentCodeBlock === null) {
372
+ currentCodeBlock = { language: language, content: '' };
373
+ }
374
+ else {
375
+ if (language !== null) {
376
+ // [🌻]
377
+ throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
378
+ }
379
+ codeBlocks.push(currentCodeBlock);
380
+ currentCodeBlock = null;
381
+ }
382
+ }
383
+ else if (currentCodeBlock !== null) {
384
+ if (currentCodeBlock.content !== '') {
385
+ currentCodeBlock.content += '\n';
386
+ }
387
+ currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
388
+ }
389
+ }
390
+ }
391
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
392
+ finally {
393
+ try {
394
+ if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
395
+ }
396
+ finally { if (e_1) throw e_1.error; }
397
+ }
398
+ if (currentCodeBlock !== null) {
399
+ // [🌻]
400
+ throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
401
+ }
402
+ return codeBlocks;
403
+ }
404
+
405
+ /**
406
+ * Extracts exactly ONE code block from markdown.
407
+ *
408
+ * Note: If there are multiple or no code blocks the function throws an error
409
+ *
410
+ * Note: There are 3 simmilar function:
411
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
412
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
413
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
414
+ *
415
+ * @param markdown any valid markdown
416
+ * @returns code block with language and content
417
+ */
418
+ function extractOneBlockFromMarkdown(markdown) {
419
+ var codeBlocks = extractAllBlocksFromMarkdown(markdown);
420
+ if (codeBlocks.length !== 1) {
421
+ // TODO: Report more specific place where the error happened
422
+ throw new Error(/* <- [🌻] */ 'There should be exactly one code block in the markdown');
423
+ }
424
+ return codeBlocks[0];
425
+ }
426
+ /***
427
+ * TODO: [🍓][🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
428
+ */
429
+
125
430
  /**
126
431
  * Removes HTML or Markdown comments from a string.
127
432
  *
@@ -133,32 +438,82 @@ function removeContentComments(content) {
133
438
  }
134
439
 
135
440
  /**
136
- * This error type indicates that the error should not happen and its last check before crashing with some other error
441
+ * The version of the Promptbook library
137
442
  */
138
- var UnexpectedError = /** @class */ (function (_super) {
139
- __extends(UnexpectedError, _super);
140
- function UnexpectedError(message) {
141
- 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;
142
- _this.name = 'UnexpectedError';
143
- Object.setPrototypeOf(_this, UnexpectedError.prototype);
144
- return _this;
145
- }
146
- return UnexpectedError;
147
- }(Error));
443
+ var PROMPTBOOK_VERSION = '0.48.0-0';
148
444
 
149
445
  /**
150
- * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
446
+ * Parses the given script and returns the list of all used variables that are not defined in the script
447
+ *
448
+ * @param script from which to extract the variables
449
+ * @returns the list of variable names
450
+ * @throws {PromptbookSyntaxError} if the script is invalid
451
+ *
452
+ * @private within the promptbookStringToJson
151
453
  */
152
- var PromptbookSyntaxError = /** @class */ (function (_super) {
153
- __extends(PromptbookSyntaxError, _super);
154
- function PromptbookSyntaxError(message) {
155
- var _this = _super.call(this, message) || this;
156
- _this.name = 'PromptbookSyntaxError';
157
- Object.setPrototypeOf(_this, PromptbookSyntaxError.prototype);
158
- return _this;
454
+ function extractVariables(script) {
455
+ var variables = [];
456
+ script = "(()=>{".concat(script, "})()");
457
+ try {
458
+ for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
459
+ try {
460
+ eval(script);
461
+ }
462
+ catch (error) {
463
+ if (!(error instanceof ReferenceError)) {
464
+ throw error;
465
+ }
466
+ var undefinedName = error.message.split(' ')[0];
467
+ /*
468
+ Note: Remapping error
469
+ From: [ReferenceError: thing is not defined],
470
+ To: [Error: Parameter {thing} is not defined],
471
+ */
472
+ if (!undefinedName) {
473
+ throw error;
474
+ }
475
+ if (script.includes(undefinedName + '(')) {
476
+ script = "const ".concat(undefinedName, " = ()=>'';") + script;
477
+ }
478
+ else {
479
+ variables.push(undefinedName);
480
+ script = "const ".concat(undefinedName, " = '';") + script;
481
+ }
482
+ }
159
483
  }
160
- return PromptbookSyntaxError;
161
- }(Error));
484
+ catch (error) {
485
+ if (!(error instanceof Error)) {
486
+ throw error;
487
+ }
488
+ 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 "); }));
489
+ }
490
+ return variables;
491
+ }
492
+
493
+ /**
494
+ * Removes emojis from a string and fix whitespaces
495
+ *
496
+ * @param text with emojis
497
+ * @returns text without emojis
498
+ */
499
+ function removeEmojis(text) {
500
+ // Replace emojis (and also ZWJ sequence) with hyphens
501
+ text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
502
+ text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
503
+ text = text.replace(/(\p{Extended_Pictographic})(\u{200D}\p{Extended_Pictographic})*/gu, '$1');
504
+ text = text.replace(/\p{Extended_Pictographic}/gu, '');
505
+ return text;
506
+ }
507
+
508
+ /**
509
+ * Function normalizes title to name which can be used as identifier
510
+ */
511
+ function titleToName(value) {
512
+ value = removeEmojis(value);
513
+ value = normalizeToKebabCase(value);
514
+ // TODO: [🧠] Maybe warn or add some padding to short name which are not good identifiers
515
+ return value;
516
+ }
162
517
 
163
518
  /**
164
519
  * Function parseNumber will parse number from string
@@ -534,11 +889,10 @@ for (var i = 0; i < defaultDiacriticsRemovalMap.length; i++) {
534
889
  var letters = defaultDiacriticsRemovalMap[i].letters;
535
890
  // tslint:disable-next-line: prefer-for-of
536
891
  for (var j = 0; j < letters.length; j++) {
537
- DIACRITIC_VARIANTS_LETTERS[letters[j]] =
538
- defaultDiacriticsRemovalMap[i].base;
892
+ DIACRITIC_VARIANTS_LETTERS[letters[j]] = defaultDiacriticsRemovalMap[i].base;
539
893
  }
540
894
  }
541
- // <- TODO: !!!! Put to maker function
895
+ // <- TODO: [🍓] Put to maker function to save execution time if not needed
542
896
  /*
543
897
  @see https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
544
898
  Licensed under the Apache License, Version 2.0 (the "License");
@@ -621,11 +975,6 @@ function checkExpectations(expectations, value) {
621
975
  * TODO: [💝] Unite object for expecting amount and format
622
976
  */
623
977
 
624
- /**
625
- * The maximum number of iterations for a loops
626
- */
627
- var LOOP_LIMIT = 1000;
628
-
629
978
  /**
630
979
  * This error occurs during the parameter replacement in the template
631
980
  *
@@ -699,155 +1048,27 @@ function replaceParameters(template, parameters) {
699
1048
  if (/^\w+}/.test(replacedTemplate)) {
700
1049
  throw new TemplateError('Parameter is not opened');
701
1050
  }
702
- return replacedTemplate;
703
- }
704
-
705
- /**
706
- * Function isValidJsonString will tell you if the string is valid JSON or not
707
- */
708
- function isValidJsonString(value) {
709
- try {
710
- JSON.parse(value);
711
- return true;
712
- }
713
- catch (error) {
714
- if (!(error instanceof Error)) {
715
- throw error;
716
- }
717
- if (error.message.includes('Unexpected token')) {
718
- return false;
719
- }
720
- return false;
721
- }
722
- }
723
-
724
- /**
725
- * Makes first letter of a string uppercase
726
- *
727
- */
728
- function capitalize(word) {
729
- return word.substring(0, 1).toUpperCase() + word.substring(1);
730
- }
731
-
732
- /**
733
- * Extracts all code blocks from markdown.
734
- *
735
- * Note: There are 3 simmilar function:
736
- * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
737
- * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
738
- * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
739
- *
740
- * @param markdown any valid markdown
741
- * @returns code blocks with language and content
742
- *
743
- */
744
- function extractAllBlocksFromMarkdown(markdown) {
745
- var e_1, _a;
746
- var codeBlocks = [];
747
- var lines = markdown.split('\n');
748
- var currentCodeBlock = null;
749
- try {
750
- for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
751
- var line = lines_1_1.value;
752
- if (line.startsWith('```')) {
753
- var language = line.slice(3).trim() || null;
754
- if (currentCodeBlock === null) {
755
- currentCodeBlock = { language: language, content: '' };
756
- }
757
- else {
758
- if (language !== null) {
759
- // [🌻]
760
- throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
761
- }
762
- codeBlocks.push(currentCodeBlock);
763
- currentCodeBlock = null;
764
- }
765
- }
766
- else if (currentCodeBlock !== null) {
767
- if (currentCodeBlock.content !== '') {
768
- currentCodeBlock.content += '\n';
769
- }
770
- currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
771
- }
772
- }
773
- }
774
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
775
- finally {
776
- try {
777
- if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
778
- }
779
- finally { if (e_1) throw e_1.error; }
780
- }
781
- if (currentCodeBlock !== null) {
782
- // [🌻]
783
- throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
784
- }
785
- return codeBlocks;
786
- }
787
-
788
- /**
789
- * Utility function to extract all list items from markdown
790
- *
791
- * Note: It works with both ul and ol
792
- * Note: It omits list items in code blocks
793
- * Note: It flattens nested lists
794
- * Note: It can not work with html syntax and comments
795
- *
796
- * @param markdown any valid markdown
797
- * @returns
798
- */
799
- function extractAllListItemsFromMarkdown(markdown) {
800
- var e_1, _a;
801
- var lines = markdown.split('\n');
802
- var listItems = [];
803
- var isInCodeBlock = false;
804
- try {
805
- for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
806
- var line = lines_1_1.value;
807
- var trimmedLine = line.trim();
808
- if (trimmedLine.startsWith('```')) {
809
- isInCodeBlock = !isInCodeBlock;
810
- }
811
- if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
812
- var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
813
- listItems.push(listItem);
814
- }
815
- }
816
- }
817
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
818
- finally {
819
- try {
820
- if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
821
- }
822
- finally { if (e_1) throw e_1.error; }
823
- }
824
- return listItems;
825
- }
826
-
827
- /**
828
- * Extracts exactly ONE code block from markdown.
829
- *
830
- * Note: If there are multiple or no code blocks the function throws an error
831
- *
832
- * Note: There are 3 simmilar function:
833
- * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
834
- * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
835
- * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
836
- *
837
- * @param markdown any valid markdown
838
- * @returns code block with language and content
839
- */
840
- function extractOneBlockFromMarkdown(markdown) {
841
- var codeBlocks = extractAllBlocksFromMarkdown(markdown);
842
- if (codeBlocks.length !== 1) {
843
- // TODO: Report more specific place where the error happened
844
- throw new Error(/* <- [🌻] */ 'There should be exactly one code block in the markdown');
845
- }
846
- return codeBlocks[0];
1051
+ return replacedTemplate;
847
1052
  }
848
- /***
849
- * TODO: [🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
1053
+
1054
+ /**
1055
+ * Function isValidJsonString will tell you if the string is valid JSON or not
850
1056
  */
1057
+ function isValidJsonString(value) {
1058
+ try {
1059
+ JSON.parse(value);
1060
+ return true;
1061
+ }
1062
+ catch (error) {
1063
+ if (!(error instanceof Error)) {
1064
+ throw error;
1065
+ }
1066
+ if (error.message.includes('Unexpected token')) {
1067
+ return false;
1068
+ }
1069
+ return false;
1070
+ }
1071
+ }
851
1072
 
852
1073
  /**
853
1074
  * Removes Markdown formatting tags from a string.
@@ -930,11 +1151,11 @@ function normalizeTo_SCREAMING_CASE(sentence) {
930
1151
  */
931
1152
 
932
1153
  /* tslint:disable */
933
- function normalizeTo_camelCase(sentence, __firstLetterCapital) {
1154
+ function normalizeToKebabCase(sentence) {
934
1155
  var e_1, _a;
935
- if (__firstLetterCapital === void 0) { __firstLetterCapital = false; }
1156
+ sentence = removeDiacritics(sentence);
936
1157
  var charType;
937
- var lastCharType = null;
1158
+ var lastCharType = 'OTHER';
938
1159
  var normalizedName = '';
939
1160
  try {
940
1161
  for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
@@ -952,20 +1173,19 @@ function normalizeTo_camelCase(sentence, __firstLetterCapital) {
952
1173
  charType = 'NUMBER';
953
1174
  normalizedChar = char;
954
1175
  }
1176
+ else if (/^\/$/.test(char)) {
1177
+ charType = 'SLASH';
1178
+ normalizedChar = char;
1179
+ }
955
1180
  else {
956
1181
  charType = 'OTHER';
957
- normalizedChar = '';
958
- }
959
- if (!lastCharType) {
960
- if (__firstLetterCapital) {
961
- normalizedChar = normalizedChar.toUpperCase(); //TODO: DRY
962
- }
1182
+ normalizedChar = '-';
963
1183
  }
964
- else if (charType !== lastCharType &&
965
- !(charType === 'LOWERCASE' && lastCharType === 'UPPERCASE') &&
1184
+ if (charType !== lastCharType &&
1185
+ !(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
966
1186
  !(lastCharType === 'NUMBER') &&
967
1187
  !(charType === 'NUMBER')) {
968
- normalizedChar = normalizedChar.toUpperCase(); //TODO: [🌺] DRY
1188
+ normalizedName += '-';
969
1189
  }
970
1190
  normalizedName += normalizedChar;
971
1191
  lastCharType = charType;
@@ -978,212 +1198,12 @@ function normalizeTo_camelCase(sentence, __firstLetterCapital) {
978
1198
  }
979
1199
  finally { if (e_1) throw e_1.error; }
980
1200
  }
1201
+ normalizedName = normalizedName.split(/-+/g).join('-');
1202
+ normalizedName = normalizedName.split(/-?\/-?/g).join('/');
1203
+ normalizedName = normalizedName.replace(/^-/, '');
1204
+ normalizedName = normalizedName.replace(/-$/, '');
981
1205
  return normalizedName;
982
1206
  }
983
- /**
984
- * TODO: [🌺] Use some intermediate util splitWords
985
- */
986
-
987
- function normalizeTo_PascalCase(sentence) {
988
- return normalizeTo_camelCase(sentence, true);
989
- }
990
-
991
- /**
992
- * Supported script languages
993
- */
994
- var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
995
-
996
- /**
997
- * Parses the template and returns the list of all parameter names
998
- *
999
- * @param template the template with parameters in {curly} braces
1000
- * @returns the list of parameter names
1001
- *
1002
- * @private within the library
1003
- */
1004
- function extractParameters(template) {
1005
- var e_1, _a;
1006
- var matches = template.matchAll(/{\w+}/g);
1007
- var parameterNames = [];
1008
- try {
1009
- for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
1010
- var match = matches_1_1.value;
1011
- var parameterName = match[0].slice(1, -1);
1012
- if (!parameterNames.includes(parameterName)) {
1013
- parameterNames.push(parameterName);
1014
- }
1015
- }
1016
- }
1017
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
1018
- finally {
1019
- try {
1020
- if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
1021
- }
1022
- finally { if (e_1) throw e_1.error; }
1023
- }
1024
- return parameterNames;
1025
- }
1026
-
1027
- /**
1028
- * Computes the deepness of the markdown structure.
1029
- *
1030
- * @private within the library
1031
- */
1032
- function countMarkdownStructureDeepness(markdownStructure) {
1033
- var e_1, _a;
1034
- var maxDeepness = 0;
1035
- try {
1036
- for (var _b = __values(markdownStructure.sections), _c = _b.next(); !_c.done; _c = _b.next()) {
1037
- var section = _c.value;
1038
- maxDeepness = Math.max(maxDeepness, countMarkdownStructureDeepness(section));
1039
- }
1040
- }
1041
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
1042
- finally {
1043
- try {
1044
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1045
- }
1046
- finally { if (e_1) throw e_1.error; }
1047
- }
1048
- return maxDeepness + 1;
1049
- }
1050
-
1051
- /**
1052
- * Parse a markdown string into a MarkdownStructure object.
1053
- *
1054
- * Note: This function does work with code blocks
1055
- * Note: This function does not work with markdown comments
1056
- *
1057
- * @param markdown The markdown string to parse.
1058
- * @returns The MarkdownStructure object.
1059
- *
1060
- * @private within the library
1061
- */
1062
- function markdownToMarkdownStructure(markdown) {
1063
- var e_1, _a;
1064
- var lines = markdown.split('\n');
1065
- var root = { level: 0, title: '', contentLines: [], sections: [], parent: null };
1066
- var current = root;
1067
- var isInsideCodeBlock = false;
1068
- try {
1069
- for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
1070
- var line = lines_1_1.value;
1071
- var headingMatch = line.match(/^(?<mark>#{1,6})\s(?<title>.*)/);
1072
- if (isInsideCodeBlock || !headingMatch) {
1073
- if (line.startsWith('```')) {
1074
- isInsideCodeBlock = !isInsideCodeBlock;
1075
- }
1076
- current.contentLines.push(line);
1077
- }
1078
- else {
1079
- var level = headingMatch.groups.mark.length;
1080
- var title = headingMatch.groups.title.trim();
1081
- var parent_1 = void 0;
1082
- if (level > current.level) {
1083
- // Note: Going deeper (next section is child of current)
1084
- parent_1 = current;
1085
- }
1086
- else {
1087
- // Note: Going up or staying at the same level (next section is sibling or parent or grandparent,... of current)
1088
- parent_1 = current;
1089
- var loopLimit = LOOP_LIMIT;
1090
- while (parent_1.level !== level - 1) {
1091
- if (loopLimit-- < 0) {
1092
- throw new UnexpectedError('Loop limit reached during parsing of markdown structure in `markdownToMarkdownStructure`');
1093
- }
1094
- if (parent_1.parent === null /* <- Note: We are in root */) {
1095
- // [🌻]
1096
- throw new Error(spaceTrim("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
1097
- }
1098
- parent_1 = parent_1.parent;
1099
- }
1100
- }
1101
- var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
1102
- parent_1.sections.push(section);
1103
- current = section;
1104
- }
1105
- }
1106
- }
1107
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
1108
- finally {
1109
- try {
1110
- if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
1111
- }
1112
- finally { if (e_1) throw e_1.error; }
1113
- }
1114
- if (root.sections.length === 1) {
1115
- var markdownStructure = parsingMarkdownStructureToMarkdownStructure(root.sections[0]);
1116
- return markdownStructure;
1117
- }
1118
- // [🌻]
1119
- throw new Error('The markdown file must have exactly one top-level section.');
1120
- // return root;
1121
- }
1122
- /**
1123
- * @private
1124
- */
1125
- function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
1126
- var level = parsingMarkdownStructure.level, title = parsingMarkdownStructure.title, contentLines = parsingMarkdownStructure.contentLines, sections = parsingMarkdownStructure.sections;
1127
- return {
1128
- level: level,
1129
- title: title,
1130
- content: spaceTrim(contentLines.join('\n')),
1131
- sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
1132
- };
1133
- }
1134
-
1135
- /**
1136
- * The version of the Promptbook library
1137
- */
1138
- var PROMPTBOOK_VERSION = '0.47.0-0';
1139
-
1140
- /**
1141
- * Parses the given script and returns the list of all used variables that are not defined in the script
1142
- *
1143
- * @param script from which to extract the variables
1144
- * @returns the list of variable names
1145
- * @throws {PromptbookSyntaxError} if the script is invalid
1146
- *
1147
- * @private within the promptbookStringToJson
1148
- */
1149
- function extractVariables(script) {
1150
- var variables = [];
1151
- script = "(()=>{".concat(script, "})()");
1152
- try {
1153
- for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
1154
- try {
1155
- eval(script);
1156
- }
1157
- catch (error) {
1158
- if (!(error instanceof ReferenceError)) {
1159
- throw error;
1160
- }
1161
- var undefinedName = error.message.split(' ')[0];
1162
- /*
1163
- Note: Remapping error
1164
- From: [ReferenceError: thing is not defined],
1165
- To: [Error: Parameter {thing} is not defined],
1166
- */
1167
- if (!undefinedName) {
1168
- throw error;
1169
- }
1170
- if (script.includes(undefinedName + '(')) {
1171
- script = "const ".concat(undefinedName, " = ()=>'';") + script;
1172
- }
1173
- else {
1174
- variables.push(undefinedName);
1175
- script = "const ".concat(undefinedName, " = '';") + script;
1176
- }
1177
- }
1178
- }
1179
- catch (error) {
1180
- if (!(error instanceof Error)) {
1181
- throw error;
1182
- }
1183
- 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 "); }));
1184
- }
1185
- return variables;
1186
- }
1187
1207
 
1188
1208
  /**
1189
1209
  * Execution type describes the way how the block is executed
@@ -1693,7 +1713,7 @@ function promptbookStringToJson(promptbookString) {
1693
1713
  }
1694
1714
  dependentParameterNames = __spreadArray([], __read(new Set(dependentParameterNames)), false);
1695
1715
  promptbookJson.promptTemplates.push({
1696
- name: normalizeTo_PascalCase(section.title),
1716
+ name: titleToName(section.title),
1697
1717
  title: section.title,
1698
1718
  description: description_1,
1699
1719
  dependentParameterNames: dependentParameterNames,
@@ -1730,6 +1750,15 @@ function promptbookStringToJson(promptbookString) {
1730
1750
  * TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
1731
1751
  */
1732
1752
 
1753
+ function promptbookJsonToString(promptbookJson) {
1754
+ return spaceTrim$1(function (block) { return "\n\n # ".concat(block(promptbookJson.title), "\n\n "); });
1755
+ }
1756
+ /**
1757
+ * TODO: !!!!! Implement
1758
+ * TODO: !!!!! Annotate and warn
1759
+ * TODO: !!!!! Test + test together with promptbookStringToJson
1760
+ */
1761
+
1733
1762
  /**
1734
1763
  * This error indicates that the promptbook object has valid syntax but contains logical errors (like circular dependencies)
1735
1764
  */
@@ -2555,7 +2584,8 @@ var PromptbookReferenceError = /** @class */ (function (_super) {
2555
2584
  * @see https://github.com/webgptorg/promptbook#promptbook-library
2556
2585
  */
2557
2586
  var SimplePromptbookLibrary = /** @class */ (function () {
2558
- /**!!!
2587
+ /**
2588
+ * Constructs a promptbook library from promptbooks
2559
2589
  *
2560
2590
  * @param promptbooks !!!
2561
2591
  *
@@ -2617,6 +2647,15 @@ var SimplePromptbookLibrary = /** @class */ (function () {
2617
2647
  return SimplePromptbookLibrary;
2618
2648
  }());
2619
2649
 
2650
+ /**
2651
+ * Creates PromptbookLibrary from array of PromptbookJson or PromptbookString
2652
+ *
2653
+ * Note: You can combine `PromptbookString` (`.ptbk.md`) with `PromptbookJson` BUT it is not recommended
2654
+ * Note: During the construction syntax and logic of all sources are validated
2655
+ *
2656
+ * @param promptbookSources
2657
+ * @returns PromptbookLibrary
2658
+ */
2620
2659
  function createPromptbookLibraryFromSources() {
2621
2660
  var e_1, _a;
2622
2661
  var promptbookSources = [];
@@ -2648,20 +2687,29 @@ function createPromptbookLibraryFromSources() {
2648
2687
  }
2649
2688
  return new (SimplePromptbookLibrary.bind.apply(SimplePromptbookLibrary, __spreadArray([void 0], __read(promptbooks), false)))();
2650
2689
  }
2690
+ /***
2691
+ * TODO: [🍓][🚯] !!! Add to README and samples + maybe make `@promptbook/library` package
2692
+ */
2693
+
2651
2694
  /**
2652
- * Constructs Promptbook from any sources
2695
+ * Constructs Promptbook from async sources
2696
+ * It can be one of the following:
2697
+ * - Promise of array of PromptbookJson or PromptbookString
2698
+ * - Factory function that returns Promise of array of PromptbookJson or PromptbookString
2653
2699
  *
2654
- * Note: During the construction syntax and logic of all sources are validated
2655
- * Note: You can combine .ptbk.md and .ptbk.json files BUT it is not recommended
2700
+ * Note: This is useful as internal tool for other constructor functions like
2701
+ * `createPromptbookLibraryFromUrl` or `createPromptbookLibraryFromDirectory`
2702
+ * Consider using those functions instead of this one
2703
+ *
2704
+ * Note: The function does NOT return promise it returns the library directly which waits for the sources to be resolved
2705
+ * when error occurs in given promise or factory function, it is thrown during `listPromptbooks` or `getPromptbookByUrl` call
2656
2706
  *
2657
- * @param promptbookSources contents of .ptbk.md or .ptbk.json files
2658
- * @param settings settings for creating executor functions
2707
+ *
2708
+ * @param promptbookSourcesPromiseOrFactory
2659
2709
  * @returns PromptbookLibrary
2710
+ *
2711
+ * @deprecated Consider using `createPromptbookLibraryFromUrl` or `createPromptbookLibraryFromDirectory`
2660
2712
  */
2661
- /***
2662
- * TODO: !!! Annotate all + all to README and samples
2663
- */
2664
-
2665
2713
  function createPromptbookLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
2666
2714
  var library;
2667
2715
  function forSources() {
@@ -2726,9 +2774,18 @@ function createPromptbookLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
2726
2774
  };
2727
2775
  }
2728
2776
  /***
2729
- * TODO: !!! Annotate all + all to README and samples
2777
+ * TODO: [🍓][🚯] !!! Add to README and samples + maybe make `@promptbook/library` package
2730
2778
  */
2731
2779
 
2780
+ /**
2781
+ * Creates PromptbookLibrary as a subset of another PromptbookLibrary
2782
+ *
2783
+ * Note: You can use any type of library as a parent library - local, remote, etc.
2784
+ * Note: This is just a thin wrapper / proxy around the parent library
2785
+ *
2786
+ * @param promptbookSources
2787
+ * @returns PromptbookLibrary
2788
+ */
2732
2789
  function createPromptbookSublibrary(library, predicate) {
2733
2790
  function listPromptbooks() {
2734
2791
  return __awaiter(this, void 0, void 0, function () {
@@ -2799,8 +2856,8 @@ function createPromptbookSublibrary(library, predicate) {
2799
2856
  };
2800
2857
  }
2801
2858
  /***
2802
- * TODO: !!! Annotate all + all to README and samples
2859
+ * TODO: [🍓][🚯] !!! Add to README and samples + maybe make `@promptbook/library` package
2803
2860
  */
2804
2861
 
2805
- export { CallbackInterfaceTools, ExecutionTypes, PROMPTBOOK_VERSION, SimplePromptInterfaceTools, SimplePromptbookLibrary, createPromptbookExecutor, createPromptbookLibraryFromPromise, createPromptbookLibraryFromSources, createPromptbookSublibrary, promptbookStringToJson, validatePromptbookJson };
2862
+ export { CallbackInterfaceTools, ExecutionTypes, PROMPTBOOK_VERSION, SimplePromptInterfaceTools, SimplePromptbookLibrary, createPromptbookExecutor, createPromptbookLibraryFromPromise, createPromptbookLibraryFromSources, createPromptbookSublibrary, promptbookJsonToString, promptbookStringToJson, validatePromptbookJson };
2806
2863
  //# sourceMappingURL=index.es.js.map