@promptbook/core 0.52.0-18 → 0.52.0-19

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/umd/index.umd.js CHANGED
@@ -454,7 +454,7 @@
454
454
  /**
455
455
  * The version of the Promptbook library
456
456
  */
457
- var PROMPTBOOK_VERSION = '0.52.0-17';
457
+ var PROMPTBOOK_VERSION = '0.52.0-18';
458
458
 
459
459
  /**
460
460
  * Parses the template and returns the list of all parameter names
@@ -2532,7 +2532,7 @@
2532
2532
  title: currentTemplate.title,
2533
2533
  promptbookUrl: "".concat(promptbook.promptbookUrl
2534
2534
  ? promptbook.promptbookUrl
2535
- : 'anonymous' /* <- [🧠][🈴] How to deal with anonymous PROMPTBOOKs, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
2535
+ : 'anonymous' /* <- TODO: [🧠] How to deal with anonymous PROMPTBOOKs, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
2536
2536
  parameters: parametersToPass,
2537
2537
  content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
2538
2538
  modelRequirements: currentTemplate.modelRequirements,
@@ -3249,6 +3249,12 @@
3249
3249
  throw new PromptbookReferenceError(spaceTrim.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 ")));
3250
3250
  }
3251
3251
  validatePromptbookJson(promptbook);
3252
+ // Note: [🦄]
3253
+ if (this.library.has(promptbook.promptbookUrl) &&
3254
+ promptbookJsonToString(promptbook) !==
3255
+ promptbookJsonToString(this.library.get(promptbook.promptbookUrl))) {
3256
+ throw new PromptbookReferenceError(spaceTrim.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 ")));
3257
+ }
3252
3258
  this.library.set(promptbook.promptbookUrl, promptbook);
3253
3259
  }
3254
3260
  }
@@ -3433,102 +3439,165 @@
3433
3439
  * @param options - Misc options for the library
3434
3440
  * @returns PromptbookLibrary
3435
3441
  */
3436
- function createPromptbookLibraryFromDirectory(path$1, options) {
3442
+ function createPromptbookLibraryFromDirectory(path, options) {
3437
3443
  var _this = this;
3438
3444
  if (!isRunningInNode()) {
3439
3445
  throw new Error('Function `createPromptbookLibraryFromDirectory` can only be run in Node.js environment because it reads the file system.');
3440
3446
  }
3441
3447
  var _a = options || {}, _b = _a.isRecursive, isRecursive = _b === void 0 ? true : _b, _c = _a.isVerbose, isVerbose = _c === void 0 ? false : _c;
3442
3448
  return createPromptbookLibraryFromPromise(function () { return __awaiter(_this, void 0, void 0, function () {
3443
- var justR_equire, fsPromises, readdir, readFile, dirents, fileNames, promptbooks, fileNames_1, fileNames_1_1, fileName, promptbookString, promptbook, e_1_1;
3444
- var e_1, _a;
3445
- return __generator(this, function (_b) {
3446
- switch (_b.label) {
3449
+ var fsPromises, readFile, fileNames, promptbooks, fileNames_1, fileNames_1_1, fileName, promptbook, promptbookString, _a, _b, e_1_1;
3450
+ var e_1, _c;
3451
+ return __generator(this, function (_d) {
3452
+ switch (_d.label) {
3447
3453
  case 0:
3448
- justR_equire = require;
3449
- fsPromises = justR_equire(just('fs/promises') /* <- Note: [2] */);
3450
- readdir = fsPromises.readdir;
3454
+ fsPromises = require(just('fs/promises') /* <- Note: [1] */);
3451
3455
  readFile = fsPromises.readFile;
3452
- return [4 /*yield*/, readdir(path$1, {
3453
- withFileTypes: true /* Note: This is not working: recursive: isRecursive */,
3454
- })];
3456
+ return [4 /*yield*/, listAllFiles(path, isRecursive)];
3455
3457
  case 1:
3456
- dirents = _b.sent();
3457
- fileNames = dirents.filter(function (dirent) { return dirent.isFile(); }).map(function (_a) {
3458
- var name = _a.name;
3459
- return path.join(path$1, name);
3460
- });
3458
+ fileNames = _d.sent();
3461
3459
  if (isVerbose) {
3462
- console.info('createPromptbookLibraryFromDirectory', { path: path$1, isRecursive: isRecursive, dirents: dirents, fileNames: fileNames });
3460
+ console.info('createPromptbookLibraryFromDirectory', { path: path, isRecursive: isRecursive, fileNames: fileNames });
3463
3461
  }
3464
3462
  promptbooks = [];
3465
- _b.label = 2;
3463
+ _d.label = 2;
3466
3464
  case 2:
3467
- _b.trys.push([2, 8, 9, 10]);
3465
+ _d.trys.push([2, 11, 12, 13]);
3468
3466
  fileNames_1 = __values(fileNames), fileNames_1_1 = fileNames_1.next();
3469
- _b.label = 3;
3467
+ _d.label = 3;
3470
3468
  case 3:
3471
- if (!!fileNames_1_1.done) return [3 /*break*/, 7];
3469
+ if (!!fileNames_1_1.done) return [3 /*break*/, 10];
3472
3470
  fileName = fileNames_1_1.value;
3471
+ promptbook = null;
3473
3472
  if (!fileName.endsWith('.ptbk.md')) return [3 /*break*/, 5];
3474
3473
  return [4 /*yield*/, readFile(fileName, 'utf8')];
3475
3474
  case 4:
3476
- promptbookString = (_b.sent());
3475
+ promptbookString = (_d.sent());
3477
3476
  promptbook = promptbookStringToJson(promptbookString);
3478
- if (!promptbook.promptbookUrl) {
3479
- if (isVerbose) {
3480
- console.info("Not loading ".concat(fileName, " - missing URL"));
3481
- }
3477
+ return [3 /*break*/, 8];
3478
+ case 5:
3479
+ if (!fileName.endsWith('.ptbk.json')) return [3 /*break*/, 7];
3480
+ if (isVerbose) {
3481
+ console.info("Loading ".concat(fileName));
3482
3482
  }
3483
- else {
3484
- if (isVerbose) {
3485
- console.info("Loading ".concat(fileName));
3486
- }
3487
- // TODO: !!!! Chack url uniqueness and collisions
3488
- promptbooks.push(promptbook);
3483
+ _b = (_a = JSON).parse;
3484
+ return [4 /*yield*/, readFile(fileName, 'utf8')];
3485
+ case 6:
3486
+ // TODO: Handle non-valid JSON files
3487
+ promptbook = _b.apply(_a, [_d.sent()]);
3488
+ return [3 /*break*/, 8];
3489
+ case 7:
3490
+ if (isVerbose) {
3491
+ console.info("Skipping file ".concat(fileName));
3489
3492
  }
3490
- return [3 /*break*/, 6];
3491
- case 5:
3492
- if (fileName.endsWith('.ptbk.json')) {
3493
- if (isVerbose) {
3494
- console.info("Loading ".concat(fileName));
3493
+ _d.label = 8;
3494
+ case 8:
3495
+ // ---
3496
+ if (promptbook !== null) {
3497
+ if (!promptbook.promptbookUrl) {
3498
+ if (isVerbose) {
3499
+ console.info("Not loading ".concat(fileName, " - missing URL"));
3500
+ }
3495
3501
  }
3496
- // TODO: !!!! Implement JSON loading
3497
- // TODO: !!!! Chack url uniqueness and collisions
3498
- }
3499
- else {
3500
- if (isVerbose) {
3501
- console.info("Skipping file ".concat(fileName));
3502
+ else {
3503
+ if (isVerbose) {
3504
+ console.info("Loading ".concat(fileName));
3505
+ }
3506
+ // Note: [🦄] Promptbook with same url uniqueness will be checked automatically in SimplePromptbookLibrary
3507
+ promptbooks.push(promptbook);
3502
3508
  }
3503
3509
  }
3504
- _b.label = 6;
3505
- case 6:
3510
+ _d.label = 9;
3511
+ case 9:
3506
3512
  fileNames_1_1 = fileNames_1.next();
3507
3513
  return [3 /*break*/, 3];
3508
- case 7: return [3 /*break*/, 10];
3509
- case 8:
3510
- e_1_1 = _b.sent();
3514
+ case 10: return [3 /*break*/, 13];
3515
+ case 11:
3516
+ e_1_1 = _d.sent();
3511
3517
  e_1 = { error: e_1_1 };
3512
- return [3 /*break*/, 10];
3513
- case 9:
3518
+ return [3 /*break*/, 13];
3519
+ case 12:
3514
3520
  try {
3515
- if (fileNames_1_1 && !fileNames_1_1.done && (_a = fileNames_1.return)) _a.call(fileNames_1);
3521
+ if (fileNames_1_1 && !fileNames_1_1.done && (_c = fileNames_1.return)) _c.call(fileNames_1);
3516
3522
  }
3517
3523
  finally { if (e_1) throw e_1.error; }
3518
3524
  return [7 /*endfinally*/];
3519
- case 10: return [2 /*return*/, promptbooks];
3525
+ case 13: return [2 /*return*/, promptbooks];
3520
3526
  }
3521
3527
  });
3522
3528
  }); });
3523
3529
  }
3530
+ /**
3531
+ * Reads all files in the directory
3532
+ *
3533
+ * @param path
3534
+ * @param isRecursive
3535
+ * @returns List of all files in the directory
3536
+ * @private internal function for `createPromptbookLibraryFromDirectory`
3537
+ */
3538
+ function listAllFiles(path$1, isRecursive) {
3539
+ return __awaiter(this, void 0, void 0, function () {
3540
+ var fsPromises, readdir, dirents, fileNames, _a, _b, dirent, subPath, _c, _d, _e, _f, e_2_1;
3541
+ var e_2, _g;
3542
+ return __generator(this, function (_h) {
3543
+ switch (_h.label) {
3544
+ case 0:
3545
+ fsPromises = require(just('fs/promises') /* <- Note: [1] */);
3546
+ readdir = fsPromises.readdir;
3547
+ return [4 /*yield*/, readdir(path$1, {
3548
+ withFileTypes: true /* Note: This is not working: recursive: isRecursive */,
3549
+ })];
3550
+ case 1:
3551
+ dirents = _h.sent();
3552
+ fileNames = dirents.filter(function (dirent) { return dirent.isFile(); }).map(function (_a) {
3553
+ var name = _a.name;
3554
+ return path.join(path$1, name);
3555
+ });
3556
+ if (!isRecursive) return [3 /*break*/, 9];
3557
+ _h.label = 2;
3558
+ case 2:
3559
+ _h.trys.push([2, 7, 8, 9]);
3560
+ _a = __values(dirents.filter(function (dirent) { return dirent.isDirectory(); })), _b = _a.next();
3561
+ _h.label = 3;
3562
+ case 3:
3563
+ if (!!_b.done) return [3 /*break*/, 6];
3564
+ dirent = _b.value;
3565
+ subPath = path.join(path$1, dirent.name);
3566
+ _d = (_c = fileNames.push).apply;
3567
+ _e = [fileNames];
3568
+ _f = [[]];
3569
+ return [4 /*yield*/, listAllFiles(subPath, isRecursive)];
3570
+ case 4:
3571
+ _d.apply(_c, _e.concat([__spreadArray.apply(void 0, _f.concat([__read.apply(void 0, [(_h.sent())]), false]))]));
3572
+ _h.label = 5;
3573
+ case 5:
3574
+ _b = _a.next();
3575
+ return [3 /*break*/, 3];
3576
+ case 6: return [3 /*break*/, 9];
3577
+ case 7:
3578
+ e_2_1 = _h.sent();
3579
+ e_2 = { error: e_2_1 };
3580
+ return [3 /*break*/, 9];
3581
+ case 8:
3582
+ try {
3583
+ if (_b && !_b.done && (_g = _a.return)) _g.call(_a);
3584
+ }
3585
+ finally { if (e_2) throw e_2.error; }
3586
+ return [7 /*endfinally*/];
3587
+ case 9: return [2 /*return*/, fileNames];
3588
+ }
3589
+ });
3590
+ });
3591
+ }
3524
3592
  /***
3525
- * Note: [1] Fixing the dynamic import issue in Webpack (! Not working !)
3593
+ * TODO: [🧠] Maybe do not do hacks like [1] and just create package @promptbook/node
3594
+ * [🍓][🚯] maybe make `@promptbook/library` package
3595
+ * TODO: Fix the dynamic import issue in Webpack (! Not working !)
3526
3596
  * > ./node_modules/@promptbook/core/esm/index.es.js
3527
3597
  * > Critical dependency: the request of a dependency is an expression
3528
3598
  *
3529
- * Note: [2] Using require(just('fs/promises')) to allow
3599
+ * Note: [1] Using require(just('fs/promises')) to allow
3530
3600
  * the `@promptbook/core` work for both Node.js and browser environments
3531
- * TODO: [🍓][🚯] !!! Add to README and samples + maybe make `@promptbook/library` package
3532
3601
  */
3533
3602
 
3534
3603
  /**