@promptbook/core 0.52.0-17 → 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
@@ -1,6 +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 { join } from 'path';
4
5
  import moment from 'moment';
5
6
 
6
7
  /*! *****************************************************************************
@@ -447,7 +448,7 @@ function union() {
447
448
  /**
448
449
  * The version of the Promptbook library
449
450
  */
450
- var PROMPTBOOK_VERSION = '0.52.0-16';
451
+ var PROMPTBOOK_VERSION = '0.52.0-18';
451
452
 
452
453
  /**
453
454
  * Parses the template and returns the list of all parameter names
@@ -2525,7 +2526,7 @@ function createPromptbookExecutor(options) {
2525
2526
  title: currentTemplate.title,
2526
2527
  promptbookUrl: "".concat(promptbook.promptbookUrl
2527
2528
  ? promptbook.promptbookUrl
2528
- : '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),
2529
2530
  parameters: parametersToPass,
2530
2531
  content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
2531
2532
  modelRequirements: currentTemplate.modelRequirements,
@@ -3168,6 +3169,23 @@ var isRunningInNode = new Function("\n try {\n return this === global;
3168
3169
  */
3169
3170
  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");
3170
3171
 
3172
+ /**
3173
+ * Returns the same value that is passed as argument.
3174
+ * No side effects.
3175
+ *
3176
+ * Note: It can be usefull for leveling indentation
3177
+ *
3178
+ * @param value any values
3179
+ * @returns the same values
3180
+ */
3181
+ function just(value) {
3182
+ if (value === undefined) {
3183
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3184
+ return undefined;
3185
+ }
3186
+ return value;
3187
+ }
3188
+
3171
3189
  /**
3172
3190
  * This error indicates that promptbook not found in the library
3173
3191
  */
@@ -3225,6 +3243,12 @@ var SimplePromptbookLibrary = /** @class */ (function () {
3225
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 ")));
3226
3244
  }
3227
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
+ }
3228
3252
  this.library.set(promptbook.promptbookUrl, promptbook);
3229
3253
  }
3230
3254
  }
@@ -3251,6 +3275,9 @@ var SimplePromptbookLibrary = /** @class */ (function () {
3251
3275
  var _this = this;
3252
3276
  var promptbook = this.library.get(url);
3253
3277
  if (!promptbook) {
3278
+ if (this.listPromptbooks().length === 0) {
3279
+ throw new PromptbookNotFoundError(spaceTrim("\n Promptbook with url \"".concat(url, "\" not found\n\n No promptbooks available\n ")));
3280
+ }
3254
3281
  throw new PromptbookNotFoundError(spaceTrim(function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPromptbooks()
3255
3282
  .map(function (promptbookUrl) { return "- ".concat(promptbookUrl); })
3256
3283
  .join('\n')), "\n\n "); }));
@@ -3411,40 +3438,160 @@ function createPromptbookLibraryFromDirectory(path, options) {
3411
3438
  if (!isRunningInNode()) {
3412
3439
  throw new Error('Function `createPromptbookLibraryFromDirectory` can only be run in Node.js environment because it reads the file system.');
3413
3440
  }
3414
- var _a = (options || {}).isRecursive, isRecursive = _a === void 0 ? true : _a;
3441
+ var _a = options || {}, _b = _a.isRecursive, isRecursive = _b === void 0 ? true : _b, _c = _a.isVerbose, isVerbose = _c === void 0 ? false : _c;
3415
3442
  return createPromptbookLibraryFromPromise(function () { return __awaiter(_this, void 0, void 0, function () {
3416
- return __generator(this, function (_a) {
3417
- /*
3418
- const justR_equire = require; /* <- Note: [1] * /
3419
- const readdir = justR_equire(just('fs/promises') /* <- Note: [2] * /).readdir as typeof readdirType;
3420
-
3421
- // TODO: !!!! Implement recursive reading
3422
- // TODO: !!!!! readAllFiles util
3423
-
3424
- const dirents = await readdir(path, {
3425
- withFileTypes: true /* Note: This is not working: recursive: isRecursive * /,
3426
- });
3427
- const fileNames = dirents.filter((dirent) => dirent.isFile()).map(({ name }) => join(name, path));
3428
-
3429
- // TODO: !!! Implement
3430
-
3431
- console.info('createPromptbookLibraryFromDirectory', { path, isRecursive, fileNames });
3432
- throw new Error('Not implemented yet');
3433
-
3434
- */
3435
- console.info('createPromptbookLibraryFromDirectory', { path: path, isRecursive: isRecursive });
3436
- return [2 /*return*/, []];
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) {
3447
+ case 0:
3448
+ fsPromises = require(just('fs/promises') /* <- Note: [1] */);
3449
+ readFile = fsPromises.readFile;
3450
+ return [4 /*yield*/, listAllFiles(path, isRecursive)];
3451
+ case 1:
3452
+ fileNames = _d.sent();
3453
+ if (isVerbose) {
3454
+ console.info('createPromptbookLibraryFromDirectory', { path: path, isRecursive: isRecursive, fileNames: fileNames });
3455
+ }
3456
+ promptbooks = [];
3457
+ _d.label = 2;
3458
+ case 2:
3459
+ _d.trys.push([2, 11, 12, 13]);
3460
+ fileNames_1 = __values(fileNames), fileNames_1_1 = fileNames_1.next();
3461
+ _d.label = 3;
3462
+ case 3:
3463
+ if (!!fileNames_1_1.done) return [3 /*break*/, 10];
3464
+ fileName = fileNames_1_1.value;
3465
+ promptbook = null;
3466
+ if (!fileName.endsWith('.ptbk.md')) return [3 /*break*/, 5];
3467
+ return [4 /*yield*/, readFile(fileName, 'utf8')];
3468
+ case 4:
3469
+ promptbookString = (_d.sent());
3470
+ promptbook = promptbookStringToJson(promptbookString);
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
+ }
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));
3486
+ }
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
+ }
3495
+ }
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);
3502
+ }
3503
+ }
3504
+ _d.label = 9;
3505
+ case 9:
3506
+ fileNames_1_1 = fileNames_1.next();
3507
+ return [3 /*break*/, 3];
3508
+ case 10: return [3 /*break*/, 13];
3509
+ case 11:
3510
+ e_1_1 = _d.sent();
3511
+ e_1 = { error: e_1_1 };
3512
+ return [3 /*break*/, 13];
3513
+ case 12:
3514
+ try {
3515
+ if (fileNames_1_1 && !fileNames_1_1.done && (_c = fileNames_1.return)) _c.call(fileNames_1);
3516
+ }
3517
+ finally { if (e_1) throw e_1.error; }
3518
+ return [7 /*endfinally*/];
3519
+ case 13: return [2 /*return*/, promptbooks];
3520
+ }
3437
3521
  });
3438
3522
  }); });
3439
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
+ }
3440
3586
  /***
3441
- * Note: [1] Fixing the dynamic import issue in Webpack
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 !)
3442
3590
  * > ./node_modules/@promptbook/core/esm/index.es.js
3443
3591
  * > Critical dependency: the request of a dependency is an expression
3444
3592
  *
3445
- * Note: [2] Using require(just('fs/promises')) to allow
3593
+ * Note: [1] Using require(just('fs/promises')) to allow
3446
3594
  * the `@promptbook/core` work for both Node.js and browser environments
3447
- * TODO: [🍓][🚯] !!! Add to README and samples + maybe make `@promptbook/library` package
3448
3595
  */
3449
3596
 
3450
3597
  /**
@@ -3545,23 +3692,6 @@ function createPromptbookSublibrary(library, predicate) {
3545
3692
  * TODO: [🍓][🚯] !!! Add to README and samples + maybe make `@promptbook/library` package
3546
3693
  */
3547
3694
 
3548
- /**
3549
- * Returns the same value that is passed as argument.
3550
- * No side effects.
3551
- *
3552
- * Note: It can be usefull for leveling indentation
3553
- *
3554
- * @param value any values
3555
- * @returns the same values
3556
- */
3557
- function just(value) {
3558
- if (value === undefined) {
3559
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3560
- return undefined;
3561
- }
3562
- return value;
3563
- }
3564
-
3565
3695
  /**
3566
3696
  * Just testing imports and compatibility
3567
3697
  */