@promptbook/core 0.52.0-8 → 0.52.0

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 (50) hide show
  1. package/README.md +1 -13
  2. package/esm/index.es.js +325 -32
  3. package/esm/index.es.js.map +1 -1
  4. package/esm/typings/_packages/core.index.d.ts +4 -2
  5. package/esm/typings/_packages/utils.index.d.ts +2 -0
  6. package/esm/typings/errors/PromptbookLibraryError.d.ts +7 -0
  7. package/esm/typings/execution/ExecutionTools.d.ts +5 -3
  8. package/esm/typings/execution/plugins/llm-execution-tools/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +0 -1
  9. package/esm/typings/execution/plugins/llm-execution-tools/azure-openai/AzureOpenAiExecutionTools.d.ts +0 -1
  10. package/esm/typings/execution/plugins/llm-execution-tools/langtail/LangtailExecutionTools.d.ts +0 -3
  11. package/esm/typings/execution/plugins/llm-execution-tools/openai/OpenAiExecutionTools.d.ts +1 -1
  12. package/esm/typings/execution/plugins/llm-execution-tools/remote/RemoteLlmExecutionTools.d.ts +1 -1
  13. package/esm/typings/execution/plugins/llm-execution-tools/remote/startRemoteServer.d.ts +1 -0
  14. package/esm/typings/execution/utils/forEachAsync.d.ts +18 -0
  15. package/esm/typings/library/constructors/createPromptbookLibraryFromDirectory.d.ts +28 -4
  16. package/esm/typings/library/constructors/createPromptbookLibraryFromDirectory.test.d.ts +1 -0
  17. package/esm/typings/library/constructors/createPromptbookLibraryFromPromise.d.ts +2 -2
  18. package/esm/typings/library/constructors/createPromptbookLibraryFromPromise.test.d.ts +1 -0
  19. package/esm/typings/library/constructors/createPromptbookLibraryFromSources.test.d.ts +1 -0
  20. package/esm/typings/library/constructors/createPromptbookLibraryFromUrl.d.ts +22 -5
  21. package/esm/typings/library/constructors/justTestFsImport.d.ts +7 -0
  22. package/esm/typings/types/Prompt.d.ts +1 -1
  23. package/esm/typings/types/typeAliases.d.ts +2 -2
  24. package/package.json +2 -2
  25. package/umd/index.umd.js +329 -35
  26. package/umd/index.umd.js.map +1 -1
  27. package/umd/typings/_packages/core.index.d.ts +4 -2
  28. package/umd/typings/_packages/utils.index.d.ts +2 -0
  29. package/umd/typings/errors/PromptbookLibraryError.d.ts +7 -0
  30. package/umd/typings/execution/ExecutionTools.d.ts +5 -3
  31. package/umd/typings/execution/plugins/llm-execution-tools/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +0 -1
  32. package/umd/typings/execution/plugins/llm-execution-tools/azure-openai/AzureOpenAiExecutionTools.d.ts +0 -1
  33. package/umd/typings/execution/plugins/llm-execution-tools/langtail/LangtailExecutionTools.d.ts +0 -3
  34. package/umd/typings/execution/plugins/llm-execution-tools/openai/OpenAiExecutionTools.d.ts +1 -1
  35. package/umd/typings/execution/plugins/llm-execution-tools/remote/RemoteLlmExecutionTools.d.ts +1 -1
  36. package/umd/typings/execution/plugins/llm-execution-tools/remote/startRemoteServer.d.ts +1 -0
  37. package/umd/typings/execution/utils/forEachAsync.d.ts +18 -0
  38. package/umd/typings/library/constructors/createPromptbookLibraryFromDirectory.d.ts +28 -4
  39. package/umd/typings/library/constructors/createPromptbookLibraryFromDirectory.test.d.ts +1 -0
  40. package/umd/typings/library/constructors/createPromptbookLibraryFromPromise.d.ts +2 -2
  41. package/umd/typings/library/constructors/createPromptbookLibraryFromPromise.test.d.ts +1 -0
  42. package/umd/typings/library/constructors/createPromptbookLibraryFromSources.test.d.ts +1 -0
  43. package/umd/typings/library/constructors/createPromptbookLibraryFromUrl.d.ts +22 -5
  44. package/umd/typings/library/constructors/justTestFsImport.d.ts +7 -0
  45. package/umd/typings/types/Prompt.d.ts +1 -1
  46. package/umd/typings/types/typeAliases.d.ts +2 -2
  47. package/esm/typings/wizzard/Wizzard.d.ts +0 -4
  48. package/esm/typings/wizzard/sample.d.ts +0 -6
  49. package/umd/typings/wizzard/Wizzard.d.ts +0 -4
  50. package/umd/typings/wizzard/sample.d.ts +0 -6
package/README.md CHANGED
@@ -11,7 +11,6 @@ Library to supercharge your use of large language models
11
11
  [![Issues](https://img.shields.io/github/issues/webgptorg/promptbook.svg?style=flat)](https://github.com/webgptorg/promptbook/issues)
12
12
 
13
13
 
14
- [![Socket Badge](https://socket.dev/api/badge/npm/package/@promptbook/core)](https://socket.dev/npm/package/@promptbook/core)
15
14
 
16
15
 
17
16
 
@@ -75,15 +74,6 @@ In any of these situations, but especially in (3), the Promptbook library can ma
75
74
  - _(Not ready yet)_ Leverage the **streaming** to make super cool UI/UX.
76
75
  - _(Not ready yet)_ **A/B testing** to determine which prompt works best for the job.
77
76
 
78
- ![WebGPT](./other/screencasts/screencast-fiabciakcmgepblmdkmemdbbkilneeeh-2023.10.26-21_46_17.gif)
79
-
80
- ## 🧔 Promptbook _(for prompt-engeneers)_
81
-
82
- **P**romp**t** **b**oo**k** markdown file (**PTBK** for short, or `.ptbk.md`) is document that describes a series of prompts that are chained together to form somewhat reciepe for transforming natural language input. Inside a PTBK you can use chat prompts, completion prompts, scripting or trigger interaction with user to ask for additional information.
83
-
84
- - Multiple promptbooks forms a library which will become a **part of your application codebase**.
85
- - Theese promptbooks are designed such as they **can be written by non-programmers**.
86
-
87
77
 
88
78
 
89
79
  ### Sample:
@@ -96,7 +86,7 @@ File `write-website-content.ptbk.md`:
96
86
  >
97
87
  > Instructions for creating web page content.
98
88
  >
99
- > - PROMPTBOOK URL https://promptbook.webgpt.com/en/write-website-content.ptbk.md@v0.1.0
89
+ > - PROMPTBOOK URL https://promptbook.webgpt.com/en/write-website-content.ptbk.md
100
90
  > - PROMPTBOOK VERSION 0.0.1
101
91
  > - INPUT  PARAM `{rawTitle}` Automatically suggested a site name or empty text
102
92
  > - INPUT  PARAM `{rawAssigment}` Automatically generated site entry from image recognition
@@ -555,8 +545,6 @@ _Note: LLMs work with tokens, not characters, but in Promptbooks we want to use
555
545
  ```markdown
556
546
  # ✨ Sample: Expectations
557
547
 
558
- - PROMPTBOOK URL https://promptbook.example.com/samples/postprocessing-2.ptbk.md@v1
559
- - PROMPTBOOK VERSION 1.0.0
560
548
  - INPUT  PARAMETER {yourName} Name of the hero
561
549
 
562
550
  ## 💬 Question
package/esm/index.es.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import spaceTrim$1, { spaceTrim } from 'spacetrim';
2
2
  import { format } from 'prettier';
3
3
  import parserHtml from 'prettier/parser-html';
4
- import 'fs/promises';
4
+ import { join } from 'path';
5
5
  import moment from 'moment';
6
6
 
7
7
  /*! *****************************************************************************
@@ -448,7 +448,7 @@ function union() {
448
448
  /**
449
449
  * The version of the Promptbook library
450
450
  */
451
- var PROMPTBOOK_VERSION = '0.52.0-7';
451
+ var PROMPTBOOK_VERSION = '0.52.0-33';
452
452
 
453
453
  /**
454
454
  * Parses the template and returns the list of all parameter names
@@ -1825,6 +1825,9 @@ function promptbookStringToJson(promptbookString) {
1825
1825
  postprocessing = undefined;
1826
1826
  }
1827
1827
  dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, executionType: executionType, content: content })));
1828
+ if (templateModelRequirements.modelVariant === undefined) {
1829
+ templateModelRequirements.modelVariant = 'CHAT';
1830
+ }
1828
1831
  promptbookJson.promptTemplates.push({
1829
1832
  name: titleToName(section.title),
1830
1833
  title: section.title,
@@ -2170,6 +2173,7 @@ function isValidUrl(url) {
2170
2173
  * @throws {PromptbookLogicError} on logical error in the promptbook
2171
2174
  */
2172
2175
  function validatePromptbookJson(promptbook) {
2176
+ // TODO: [🧠] Maybe test if promptbook is a promise and make specific error case for that
2173
2177
  var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
2174
2178
  if (promptbook.promptbookUrl !== undefined) {
2175
2179
  if (!isValidUrl(promptbook.promptbookUrl)) {
@@ -2177,6 +2181,16 @@ function validatePromptbookJson(promptbook) {
2177
2181
  throw new PromptbookLogicError("Invalid promptbook URL \"".concat(promptbook.promptbookUrl, "\""));
2178
2182
  }
2179
2183
  }
2184
+ // TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
2185
+ if (!Array.isArray(promptbook.parameters)) {
2186
+ // TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
2187
+ throw new PromptbookSyntaxError(spaceTrim("\n Promptbook is valid JSON but with wrong structure\n\n promptbook.parameters expected to be an array, but got ".concat(typeof promptbook.parameters, "\n ")));
2188
+ }
2189
+ // TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
2190
+ if (!Array.isArray(promptbook.promptTemplates)) {
2191
+ // TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
2192
+ throw new PromptbookSyntaxError(spaceTrim("\n Promptbook is valid JSON but with wrong structure\n\n promptbook.promptTemplates expected to be an array, but got ".concat(typeof promptbook.promptTemplates, "\n ")));
2193
+ }
2180
2194
  var _loop_1 = function (parameter) {
2181
2195
  if (parameter.isInput && parameter.isOutput) {
2182
2196
  throw new PromptbookLogicError("Parameter {".concat(parameter.name, "} can not be both input and output"));
@@ -2223,9 +2237,8 @@ function validatePromptbookJson(promptbook) {
2223
2237
  }
2224
2238
  definedParameters.add(template.resultingParameterName);
2225
2239
  if (template.executionType === 'PROMPT_TEMPLATE' &&
2226
- (template.modelRequirements.modelVariant === undefined ||
2227
- template.modelRequirements.modelName === undefined)) {
2228
- throw new PromptbookLogicError(spaceTrim("\n\n You must specify MODEL VARIANT and MODEL NAME in the prompt template \"".concat(template.title, "\"\n\n For example:\n - MODEL VARIANT Chat\n - MODEL NAME `gpt-4-1106-preview`\n\n ")));
2240
+ (template.modelRequirements.modelVariant === undefined)) {
2241
+ throw new PromptbookLogicError(spaceTrim("\n\n You must specify MODEL VARIANT in the prompt template \"".concat(template.title, "\"\n\n For example:\n - MODEL VARIANT Chat\n - MODEL NAME `gpt-4-1106-preview`\n\n ")));
2229
2242
  }
2230
2243
  if (template.jokers && template.jokers.length > 0) {
2231
2244
  if (!template.expectFormat &&
@@ -2526,7 +2539,7 @@ function createPromptbookExecutor(options) {
2526
2539
  title: currentTemplate.title,
2527
2540
  promptbookUrl: "".concat(promptbook.promptbookUrl
2528
2541
  ? promptbook.promptbookUrl
2529
- : 'anonymous' /* <- [🧠][🈴] How to deal with anonymous PROMPTBOOKs, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
2542
+ : 'anonymous' /* <- TODO: [🧠] How to deal with anonymous PROMPTBOOKs, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
2530
2543
  parameters: parametersToPass,
2531
2544
  content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
2532
2545
  modelRequirements: currentTemplate.modelRequirements,
@@ -3156,6 +3169,20 @@ var SimplePromptInterfaceTools = /** @class */ (function () {
3156
3169
  return SimplePromptInterfaceTools;
3157
3170
  }());
3158
3171
 
3172
+ /**
3173
+ * This error indicates that the promptbook library cannot be propperly loaded
3174
+ */
3175
+ var PromptbookLibraryError = /** @class */ (function (_super) {
3176
+ __extends(PromptbookLibraryError, _super);
3177
+ function PromptbookLibraryError(message) {
3178
+ var _this = _super.call(this, message) || this;
3179
+ _this.name = 'PromptbookLibraryError';
3180
+ Object.setPrototypeOf(_this, PromptbookLibraryError.prototype);
3181
+ return _this;
3182
+ }
3183
+ return PromptbookLibraryError;
3184
+ }(Error));
3185
+
3159
3186
  /**
3160
3187
  * Detects if the code is running in a browser environment in main thread (Not in a web worker)
3161
3188
  */
@@ -3169,6 +3196,23 @@ var isRunningInNode = new Function("\n try {\n return this === global;
3169
3196
  */
3170
3197
  new Function("\n try {\n if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {\n return true;\n } else {\n return false;\n }\n } catch (e) {\n return false;\n }\n");
3171
3198
 
3199
+ /**
3200
+ * Returns the same value that is passed as argument.
3201
+ * No side effects.
3202
+ *
3203
+ * Note: It can be usefull for leveling indentation
3204
+ *
3205
+ * @param value any values
3206
+ * @returns the same values
3207
+ */
3208
+ function just(value) {
3209
+ if (value === undefined) {
3210
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3211
+ return undefined;
3212
+ }
3213
+ return value;
3214
+ }
3215
+
3172
3216
  /**
3173
3217
  * This error indicates that promptbook not found in the library
3174
3218
  */
@@ -3226,6 +3270,12 @@ var SimplePromptbookLibrary = /** @class */ (function () {
3226
3270
  throw new PromptbookReferenceError(spaceTrim("\n Promptbook with name \"".concat(promptbook.title, "\" does not have defined URL\n\n Note: Promptbooks without URLs are called anonymous promptbooks\n They can be used as standalone promptbooks, but they cannot be referenced by other promptbooks\n And also they cannot be used in the promptbook library\n\n ")));
3227
3271
  }
3228
3272
  validatePromptbookJson(promptbook);
3273
+ // Note: [🦄]
3274
+ if (this.library.has(promptbook.promptbookUrl) &&
3275
+ promptbookJsonToString(promptbook) !==
3276
+ promptbookJsonToString(this.library.get(promptbook.promptbookUrl))) {
3277
+ throw new PromptbookReferenceError(spaceTrim("\n Promptbook with URL \"".concat(promptbook.promptbookUrl, "\" is already in the library\n\n Note: Promptbooks with the same URL are not allowed\n Note: Automatically check whether the promptbooks are the same BUT they are DIFFERENT\n\n ")));
3278
+ }
3229
3279
  this.library.set(promptbook.promptbookUrl, promptbook);
3230
3280
  }
3231
3281
  }
@@ -3252,6 +3302,9 @@ var SimplePromptbookLibrary = /** @class */ (function () {
3252
3302
  var _this = this;
3253
3303
  var promptbook = this.library.get(url);
3254
3304
  if (!promptbook) {
3305
+ if (this.listPromptbooks().length === 0) {
3306
+ throw new PromptbookNotFoundError(spaceTrim("\n Promptbook with url \"".concat(url, "\" not found\n\n No promptbooks available\n ")));
3307
+ }
3255
3308
  throw new PromptbookNotFoundError(spaceTrim(function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPromptbooks()
3256
3309
  .map(function (promptbookUrl) { return "- ".concat(promptbookUrl); })
3257
3310
  .join('\n')), "\n\n "); }));
@@ -3324,11 +3377,11 @@ function createPromptbookLibraryFromSources() {
3324
3377
  * Note: The function does NOT return promise it returns the library directly which waits for the sources to be resolved
3325
3378
  * when error occurs in given promise or factory function, it is thrown during `listPromptbooks` or `getPromptbookByUrl` call
3326
3379
  *
3380
+ * Note: Consider using `createPromptbookLibraryFromDirectory` or `createPromptbookLibraryFromUrl`
3327
3381
  *
3328
3382
  * @param promptbookSourcesPromiseOrFactory
3329
3383
  * @returns PromptbookLibrary
3330
- *
3331
- * @deprecated Consider using `createPromptbookLibraryFromUrl` or `createPromptbookLibraryFromDirectory`
3384
+ * @private Just internal tool for other constructor functions
3332
3385
  */
3333
3386
  function createPromptbookLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
3334
3387
  var library;
@@ -3401,20 +3454,252 @@ function createPromptbookLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
3401
3454
  * Constructs Promptbook from given directory
3402
3455
  *
3403
3456
  * Note: Works only in Node.js environment because it reads the file system
3404
- * Note: The function does NOT return promise it returns the library directly which dynamically loads promptbooks when needed
3405
- * SO during the construction syntax and logic sources IS NOT validated
3406
3457
  *
3407
3458
  * @param path - path to the directory with promptbooks
3408
3459
  * @param options - Misc options for the library
3409
3460
  * @returns PromptbookLibrary
3410
3461
  */
3411
3462
  function createPromptbookLibraryFromDirectory(path, options) {
3412
- if (!isRunningInNode()) {
3413
- throw new Error('Function `createPromptbookLibraryFromDirectory` can only be run in Node.js environment because it reads the file system.');
3414
- }
3415
- throw new Error('Not implemented yet');
3463
+ return __awaiter(this, void 0, void 0, function () {
3464
+ var _a, _b, isRecursive, _c, isVerbose, _d, isLazyLoaded, _e, isCrashOnError, library;
3465
+ var _this = this;
3466
+ return __generator(this, function (_f) {
3467
+ switch (_f.label) {
3468
+ case 0:
3469
+ if (!isRunningInNode()) {
3470
+ throw new Error('Function `createPromptbookLibraryFromDirectory` can only be run in Node.js environment because it reads the file system.');
3471
+ }
3472
+ _a = options || {}, _b = _a.isRecursive, isRecursive = _b === void 0 ? true : _b, _c = _a.isVerbose, isVerbose = _c === void 0 ? false : _c, _d = _a.isLazyLoaded, isLazyLoaded = _d === void 0 ? false : _d, _e = _a.isCrashOnError, isCrashOnError = _e === void 0 ? true : _e;
3473
+ library = createPromptbookLibraryFromPromise(function () { return __awaiter(_this, void 0, void 0, function () {
3474
+ var fsPromises, readFile, fileNames, promptbooks, _loop_1, fileNames_1, fileNames_1_1, fileName, e_1_1;
3475
+ var e_1, _a;
3476
+ return __generator(this, function (_b) {
3477
+ switch (_b.label) {
3478
+ case 0:
3479
+ if (isVerbose) {
3480
+ console.info("Creating promptbook library from path ".concat(path));
3481
+ }
3482
+ fsPromises = require(just('fs/promises') /* <- Note: [1] */);
3483
+ readFile = fsPromises.readFile;
3484
+ return [4 /*yield*/, listAllFiles(path, isRecursive)];
3485
+ case 1:
3486
+ fileNames = _b.sent();
3487
+ if (isVerbose) {
3488
+ console.info('createPromptbookLibraryFromDirectory', { path: path, isRecursive: isRecursive, fileNames: fileNames });
3489
+ }
3490
+ promptbooks = [];
3491
+ _loop_1 = function (fileName) {
3492
+ var promptbook, promptbookString, _c, _d, error_1, wrappedErrorMessage;
3493
+ return __generator(this, function (_e) {
3494
+ switch (_e.label) {
3495
+ case 0:
3496
+ _e.trys.push([0, 6, , 7]);
3497
+ promptbook = null;
3498
+ if (!fileName.endsWith('.ptbk.md')) return [3 /*break*/, 2];
3499
+ return [4 /*yield*/, readFile(fileName, 'utf8')];
3500
+ case 1:
3501
+ promptbookString = (_e.sent());
3502
+ promptbook = promptbookStringToJson(promptbookString);
3503
+ return [3 /*break*/, 5];
3504
+ case 2:
3505
+ if (!fileName.endsWith('.ptbk.json')) return [3 /*break*/, 4];
3506
+ if (isVerbose) {
3507
+ console.info("Loading ".concat(fileName));
3508
+ }
3509
+ _d = (_c = JSON).parse;
3510
+ return [4 /*yield*/, readFile(fileName, 'utf8')];
3511
+ case 3:
3512
+ // TODO: Handle non-valid JSON files
3513
+ promptbook = _d.apply(_c, [_e.sent()]);
3514
+ return [3 /*break*/, 5];
3515
+ case 4:
3516
+ if (isVerbose) {
3517
+ console.info("Skipping file ".concat(fileName));
3518
+ }
3519
+ _e.label = 5;
3520
+ case 5:
3521
+ // ---
3522
+ if (promptbook !== null) {
3523
+ if (!promptbook.promptbookUrl) {
3524
+ if (isVerbose) {
3525
+ console.info("Not loading ".concat(fileName, " - missing URL"));
3526
+ }
3527
+ }
3528
+ else {
3529
+ if (isVerbose) {
3530
+ console.info("Loading ".concat(fileName));
3531
+ }
3532
+ if (!isCrashOnError) {
3533
+ // Note: Validate promptbook to check if it is logically correct to not crash on invalid promptbooks
3534
+ // But be handled in current try-catch block
3535
+ validatePromptbookJson(promptbook);
3536
+ }
3537
+ // Note: [🦄] Promptbook with same url uniqueness will be checked automatically in SimplePromptbookLibrary
3538
+ promptbooks.push(promptbook);
3539
+ }
3540
+ }
3541
+ return [3 /*break*/, 7];
3542
+ case 6:
3543
+ error_1 = _e.sent();
3544
+ if (!(error_1 instanceof Error)) {
3545
+ throw error_1;
3546
+ }
3547
+ wrappedErrorMessage = spaceTrim$1(function (block) { return "\n Error during loading promptbook from file ".concat(fileName.split('\\').join('/'), ":\n\n ").concat(block(error_1.message), "\n\n "); });
3548
+ if (isCrashOnError) {
3549
+ throw new PromptbookLibraryError(wrappedErrorMessage);
3550
+ }
3551
+ console.error(wrappedErrorMessage);
3552
+ return [3 /*break*/, 7];
3553
+ case 7: return [2 /*return*/];
3554
+ }
3555
+ });
3556
+ };
3557
+ _b.label = 2;
3558
+ case 2:
3559
+ _b.trys.push([2, 7, 8, 9]);
3560
+ fileNames_1 = __values(fileNames), fileNames_1_1 = fileNames_1.next();
3561
+ _b.label = 3;
3562
+ case 3:
3563
+ if (!!fileNames_1_1.done) return [3 /*break*/, 6];
3564
+ fileName = fileNames_1_1.value;
3565
+ return [5 /*yield**/, _loop_1(fileName)];
3566
+ case 4:
3567
+ _b.sent();
3568
+ _b.label = 5;
3569
+ case 5:
3570
+ fileNames_1_1 = fileNames_1.next();
3571
+ return [3 /*break*/, 3];
3572
+ case 6: return [3 /*break*/, 9];
3573
+ case 7:
3574
+ e_1_1 = _b.sent();
3575
+ e_1 = { error: e_1_1 };
3576
+ return [3 /*break*/, 9];
3577
+ case 8:
3578
+ try {
3579
+ if (fileNames_1_1 && !fileNames_1_1.done && (_a = fileNames_1.return)) _a.call(fileNames_1);
3580
+ }
3581
+ finally { if (e_1) throw e_1.error; }
3582
+ return [7 /*endfinally*/];
3583
+ case 9: return [2 /*return*/, promptbooks];
3584
+ }
3585
+ });
3586
+ }); });
3587
+ if (!(isLazyLoaded === false)) return [3 /*break*/, 2];
3588
+ return [4 /*yield*/, library.listPromptbooks()];
3589
+ case 1:
3590
+ _f.sent();
3591
+ _f.label = 2;
3592
+ case 2: return [2 /*return*/, library];
3593
+ }
3594
+ });
3595
+ });
3596
+ }
3597
+ /**
3598
+ * Reads all files in the directory
3599
+ *
3600
+ * @param path
3601
+ * @param isRecursive
3602
+ * @returns List of all files in the directory
3603
+ * @private internal function for `createPromptbookLibraryFromDirectory`
3604
+ */
3605
+ function listAllFiles(path, isRecursive) {
3606
+ return __awaiter(this, void 0, void 0, function () {
3607
+ var fsPromises, readdir, dirents, fileNames, _a, _b, dirent, subPath, _c, _d, _e, _f, e_2_1;
3608
+ var e_2, _g;
3609
+ return __generator(this, function (_h) {
3610
+ switch (_h.label) {
3611
+ case 0:
3612
+ fsPromises = require(just('fs/promises') /* <- Note: [1] */);
3613
+ readdir = fsPromises.readdir;
3614
+ return [4 /*yield*/, readdir(path, {
3615
+ withFileTypes: true /* Note: This is not working: recursive: isRecursive */,
3616
+ })];
3617
+ case 1:
3618
+ dirents = _h.sent();
3619
+ fileNames = dirents.filter(function (dirent) { return dirent.isFile(); }).map(function (_a) {
3620
+ var name = _a.name;
3621
+ return join(path, name);
3622
+ });
3623
+ if (!isRecursive) return [3 /*break*/, 9];
3624
+ _h.label = 2;
3625
+ case 2:
3626
+ _h.trys.push([2, 7, 8, 9]);
3627
+ _a = __values(dirents.filter(function (dirent) { return dirent.isDirectory(); })), _b = _a.next();
3628
+ _h.label = 3;
3629
+ case 3:
3630
+ if (!!_b.done) return [3 /*break*/, 6];
3631
+ dirent = _b.value;
3632
+ subPath = join(path, dirent.name);
3633
+ _d = (_c = fileNames.push).apply;
3634
+ _e = [fileNames];
3635
+ _f = [[]];
3636
+ return [4 /*yield*/, listAllFiles(subPath, isRecursive)];
3637
+ case 4:
3638
+ _d.apply(_c, _e.concat([__spreadArray.apply(void 0, _f.concat([__read.apply(void 0, [(_h.sent())]), false]))]));
3639
+ _h.label = 5;
3640
+ case 5:
3641
+ _b = _a.next();
3642
+ return [3 /*break*/, 3];
3643
+ case 6: return [3 /*break*/, 9];
3644
+ case 7:
3645
+ e_2_1 = _h.sent();
3646
+ e_2 = { error: e_2_1 };
3647
+ return [3 /*break*/, 9];
3648
+ case 8:
3649
+ try {
3650
+ if (_b && !_b.done && (_g = _a.return)) _g.call(_a);
3651
+ }
3652
+ finally { if (e_2) throw e_2.error; }
3653
+ return [7 /*endfinally*/];
3654
+ case 9: return [2 /*return*/, fileNames];
3655
+ }
3656
+ });
3657
+ });
3416
3658
  }
3417
3659
  /***
3660
+ * TODO: [🧠] Maybe do not do hacks like [1] and just create package @promptbook/node
3661
+ * [🍓][🚯] maybe make `@promptbook/library` package
3662
+ * TODO: Fix the dynamic import issue in Webpack (! Not working !)
3663
+ * > ./node_modules/@promptbook/core/esm/index.es.js
3664
+ * > Critical dependency: the request of a dependency is an expression
3665
+ *
3666
+ * Note: [1] Using require(just('fs/promises')) to allow
3667
+ * the `@promptbook/core` work for both Node.js and browser environments
3668
+ */
3669
+
3670
+ /**
3671
+ * Constructs Promptbook from remote Promptbase URL
3672
+
3673
+ * @returns PromptbookLibrary
3674
+ */
3675
+ function createPromptbookLibraryFromUrl(url, options) {
3676
+ return __awaiter(this, void 0, void 0, function () {
3677
+ var _a, _b, isVerbose, _c, isLazyLoaded, library;
3678
+ var _this = this;
3679
+ return __generator(this, function (_d) {
3680
+ switch (_d.label) {
3681
+ case 0:
3682
+ _a = options || {}, _b = _a.isVerbose, isVerbose = _b === void 0 ? false : _b, _c = _a.isLazyLoaded, isLazyLoaded = _c === void 0 ? false : _c;
3683
+ library = createPromptbookLibraryFromPromise(function () { return __awaiter(_this, void 0, void 0, function () {
3684
+ return __generator(this, function (_a) {
3685
+ if (isVerbose) {
3686
+ console.info("Creating promptbook library from url ".concat(url.toString()));
3687
+ }
3688
+ throw new Error('Not implemented yet');
3689
+ });
3690
+ }); });
3691
+ if (!(isLazyLoaded === false)) return [3 /*break*/, 2];
3692
+ return [4 /*yield*/, library.listPromptbooks()];
3693
+ case 1:
3694
+ _d.sent();
3695
+ _d.label = 2;
3696
+ case 2: return [2 /*return*/, library];
3697
+ }
3698
+ });
3699
+ });
3700
+ }
3701
+ /***
3702
+ * TODO: [⚖] Compatible with remote server
3418
3703
  * TODO: [🍓][🚯] !!! Add to README and samples + maybe make `@promptbook/library` package
3419
3704
  */
3420
3705
 
@@ -3500,6 +3785,31 @@ function createPromptbookSublibrary(library, predicate) {
3500
3785
  * TODO: [🍓][🚯] !!! Add to README and samples + maybe make `@promptbook/library` package
3501
3786
  */
3502
3787
 
3788
+ /**
3789
+ * Just testing imports and compatibility
3790
+ */
3791
+ function justTestFsImport() {
3792
+ return __awaiter(this, void 0, void 0, function () {
3793
+ var files;
3794
+ return __generator(this, function (_a) {
3795
+ switch (_a.label) {
3796
+ case 0:
3797
+ if (!isRunningInNode()) {
3798
+ throw new Error('Function `testFiles` can only be run in Node.js environment because it reads the file system.');
3799
+ }
3800
+ return [4 /*yield*/, require(just('glob-promise'))('/**/*')];
3801
+ case 1:
3802
+ files = _a.sent();
3803
+ console.info('testFiles', { files: files });
3804
+ return [2 /*return*/];
3805
+ }
3806
+ });
3807
+ });
3808
+ }
3809
+ /**
3810
+ * TODO: !!! Remove this file
3811
+ */
3812
+
3503
3813
  /**
3504
3814
  * Format either small or big number
3505
3815
  *
@@ -3520,23 +3830,6 @@ function formatNumber(value) {
3520
3830
  return value.toString();
3521
3831
  }
3522
3832
 
3523
- /**
3524
- * Returns the same value that is passed as argument.
3525
- * No side effects.
3526
- *
3527
- * Note: It can be usefull for leveling indentation
3528
- *
3529
- * @param value any values
3530
- * @returns the same values
3531
- */
3532
- function just(value) {
3533
- if (value === undefined) {
3534
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3535
- return undefined;
3536
- }
3537
- return value;
3538
- }
3539
-
3540
3833
  /**
3541
3834
  * Create a markdown table from a 2D array of strings
3542
3835
  *
@@ -3817,5 +4110,5 @@ function executionReportJsonToString(executionReportJson, options) {
3817
4110
  * TODO: [🧠] Allow to filter out some parts of the report by options
3818
4111
  */
3819
4112
 
3820
- export { CallbackInterfaceTools, ExecutionReportStringOptionsDefaults, ExecutionTypes, MultipleLlmExecutionTools, PROMPTBOOK_VERSION, SimplePromptInterfaceTools, SimplePromptbookLibrary, assertsExecutionSuccessful, checkExpectations, createPromptbookExecutor, createPromptbookLibraryFromDirectory, createPromptbookLibraryFromPromise, createPromptbookLibraryFromSources, createPromptbookSublibrary, executionReportJsonToString, isPassingExpectations, prettifyPromptbookString, promptbookJsonToString, promptbookStringToJson, validatePromptbookJson };
4113
+ export { CallbackInterfaceTools, ExecutionReportStringOptionsDefaults, ExecutionTypes, MultipleLlmExecutionTools, PROMPTBOOK_VERSION, SimplePromptInterfaceTools, SimplePromptbookLibrary, assertsExecutionSuccessful, checkExpectations, createPromptbookExecutor, createPromptbookLibraryFromDirectory, createPromptbookLibraryFromSources, createPromptbookLibraryFromUrl, createPromptbookSublibrary, executionReportJsonToString, isPassingExpectations, justTestFsImport, prettifyPromptbookString, promptbookJsonToString, promptbookStringToJson, validatePromptbookJson };
3821
4114
  //# sourceMappingURL=index.es.js.map