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