@promptbook/core 0.52.0-31 → 0.52.0-33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/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
 
package/esm/index.es.js CHANGED
@@ -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-30';
451
+ var PROMPTBOOK_VERSION = '0.52.0-32';
452
452
 
453
453
  /**
454
454
  * Parses the template and returns the list of all parameter names
@@ -3169,6 +3169,20 @@ var SimplePromptInterfaceTools = /** @class */ (function () {
3169
3169
  return SimplePromptInterfaceTools;
3170
3170
  }());
3171
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
+
3172
3186
  /**
3173
3187
  * Detects if the code is running in a browser environment in main thread (Not in a web worker)
3174
3188
  */
@@ -3447,20 +3461,20 @@ function createPromptbookLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
3447
3461
  */
3448
3462
  function createPromptbookLibraryFromDirectory(path, options) {
3449
3463
  return __awaiter(this, void 0, void 0, function () {
3450
- var _a, _b, isRecursive, _c, isVerbose, _d, isLazyLoaded, library;
3464
+ var _a, _b, isRecursive, _c, isVerbose, _d, isLazyLoaded, _e, isCrashOnError, library;
3451
3465
  var _this = this;
3452
- return __generator(this, function (_e) {
3453
- switch (_e.label) {
3466
+ return __generator(this, function (_f) {
3467
+ switch (_f.label) {
3454
3468
  case 0:
3455
3469
  if (!isRunningInNode()) {
3456
3470
  throw new Error('Function `createPromptbookLibraryFromDirectory` can only be run in Node.js environment because it reads the file system.');
3457
3471
  }
3458
- _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;
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;
3459
3473
  library = createPromptbookLibraryFromPromise(function () { return __awaiter(_this, void 0, void 0, function () {
3460
- var fsPromises, readFile, fileNames, promptbooks, fileNames_1, fileNames_1_1, fileName, promptbook, promptbookString, _a, _b, e_1_1;
3461
- var e_1, _c;
3462
- return __generator(this, function (_d) {
3463
- switch (_d.label) {
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) {
3464
3478
  case 0:
3465
3479
  if (isVerbose) {
3466
3480
  console.info("Creating promptbook library from path ".concat(path));
@@ -3469,82 +3483,113 @@ function createPromptbookLibraryFromDirectory(path, options) {
3469
3483
  readFile = fsPromises.readFile;
3470
3484
  return [4 /*yield*/, listAllFiles(path, isRecursive)];
3471
3485
  case 1:
3472
- fileNames = _d.sent();
3486
+ fileNames = _b.sent();
3473
3487
  if (isVerbose) {
3474
3488
  console.info('createPromptbookLibraryFromDirectory', { path: path, isRecursive: isRecursive, fileNames: fileNames });
3475
3489
  }
3476
3490
  promptbooks = [];
3477
- _d.label = 2;
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
+ // TODO: !!!! Is it working
3534
+ // Note: Validate promptbook to check if it is logically correct to not crash on invalid promptbooks
3535
+ // But be handled in current try-catch block
3536
+ validatePromptbookJson(promptbook);
3537
+ }
3538
+ // Note: [🦄] Promptbook with same url uniqueness will be checked automatically in SimplePromptbookLibrary
3539
+ promptbooks.push(promptbook);
3540
+ }
3541
+ }
3542
+ return [3 /*break*/, 7];
3543
+ case 6:
3544
+ error_1 = _e.sent();
3545
+ if (!(error_1 instanceof Error)) {
3546
+ throw error_1;
3547
+ }
3548
+ 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 "); });
3549
+ if (isCrashOnError) {
3550
+ throw new PromptbookLibraryError(wrappedErrorMessage);
3551
+ }
3552
+ console.error(wrappedErrorMessage);
3553
+ return [3 /*break*/, 7];
3554
+ case 7: return [2 /*return*/];
3555
+ }
3556
+ });
3557
+ };
3558
+ _b.label = 2;
3478
3559
  case 2:
3479
- _d.trys.push([2, 11, 12, 13]);
3560
+ _b.trys.push([2, 7, 8, 9]);
3480
3561
  fileNames_1 = __values(fileNames), fileNames_1_1 = fileNames_1.next();
3481
- _d.label = 3;
3562
+ _b.label = 3;
3482
3563
  case 3:
3483
- if (!!fileNames_1_1.done) return [3 /*break*/, 10];
3564
+ if (!!fileNames_1_1.done) return [3 /*break*/, 6];
3484
3565
  fileName = fileNames_1_1.value;
3485
- promptbook = null;
3486
- if (!fileName.endsWith('.ptbk.md')) return [3 /*break*/, 5];
3487
- return [4 /*yield*/, readFile(fileName, 'utf8')];
3566
+ return [5 /*yield**/, _loop_1(fileName)];
3488
3567
  case 4:
3489
- promptbookString = (_d.sent());
3490
- promptbook = promptbookStringToJson(promptbookString);
3491
- return [3 /*break*/, 8];
3568
+ _b.sent();
3569
+ _b.label = 5;
3492
3570
  case 5:
3493
- if (!fileName.endsWith('.ptbk.json')) return [3 /*break*/, 7];
3494
- if (isVerbose) {
3495
- console.info("Loading ".concat(fileName));
3496
- }
3497
- _b = (_a = JSON).parse;
3498
- return [4 /*yield*/, readFile(fileName, 'utf8')];
3499
- case 6:
3500
- // TODO: Handle non-valid JSON files
3501
- promptbook = _b.apply(_a, [_d.sent()]);
3502
- return [3 /*break*/, 8];
3503
- case 7:
3504
- if (isVerbose) {
3505
- console.info("Skipping file ".concat(fileName));
3506
- }
3507
- _d.label = 8;
3508
- case 8:
3509
- // ---
3510
- if (promptbook !== null) {
3511
- if (!promptbook.promptbookUrl) {
3512
- if (isVerbose) {
3513
- console.info("Not loading ".concat(fileName, " - missing URL"));
3514
- }
3515
- }
3516
- else {
3517
- if (isVerbose) {
3518
- console.info("Loading ".concat(fileName));
3519
- }
3520
- // Note: [🦄] Promptbook with same url uniqueness will be checked automatically in SimplePromptbookLibrary
3521
- promptbooks.push(promptbook);
3522
- }
3523
- }
3524
- _d.label = 9;
3525
- case 9:
3526
3571
  fileNames_1_1 = fileNames_1.next();
3527
3572
  return [3 /*break*/, 3];
3528
- case 10: return [3 /*break*/, 13];
3529
- case 11:
3530
- e_1_1 = _d.sent();
3573
+ case 6: return [3 /*break*/, 9];
3574
+ case 7:
3575
+ e_1_1 = _b.sent();
3531
3576
  e_1 = { error: e_1_1 };
3532
- return [3 /*break*/, 13];
3533
- case 12:
3577
+ return [3 /*break*/, 9];
3578
+ case 8:
3534
3579
  try {
3535
- if (fileNames_1_1 && !fileNames_1_1.done && (_c = fileNames_1.return)) _c.call(fileNames_1);
3580
+ if (fileNames_1_1 && !fileNames_1_1.done && (_a = fileNames_1.return)) _a.call(fileNames_1);
3536
3581
  }
3537
3582
  finally { if (e_1) throw e_1.error; }
3538
3583
  return [7 /*endfinally*/];
3539
- case 13: return [2 /*return*/, promptbooks];
3584
+ case 9: return [2 /*return*/, promptbooks];
3540
3585
  }
3541
3586
  });
3542
3587
  }); });
3543
3588
  if (!(isLazyLoaded === false)) return [3 /*break*/, 2];
3544
3589
  return [4 /*yield*/, library.listPromptbooks()];
3545
3590
  case 1:
3546
- _e.sent();
3547
- _e.label = 2;
3591
+ _f.sent();
3592
+ _f.label = 2;
3548
3593
  case 2: return [2 /*return*/, library];
3549
3594
  }
3550
3595
  });