@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/umd/index.umd.js CHANGED
@@ -1,8 +1,8 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('spacetrim'), require('prettier'), require('prettier/parser-html'), require('moment')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'spacetrim', 'prettier', 'prettier/parser-html', 'moment'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-core"] = {}, global.spaceTrim, global.prettier, global.parserHtml, global.moment));
5
- })(this, (function (exports, spaceTrim, prettier, parserHtml, moment) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('spacetrim'), require('prettier'), require('prettier/parser-html'), require('path'), require('moment')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'spacetrim', 'prettier', 'prettier/parser-html', 'path', 'moment'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-core"] = {}, global.spaceTrim, global.prettier, global.parserHtml, global.path, global.moment));
5
+ })(this, (function (exports, spaceTrim, prettier, parserHtml, path, moment) { 'use strict';
6
6
 
7
7
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
8
 
@@ -454,7 +454,7 @@
454
454
  /**
455
455
  * The version of the Promptbook library
456
456
  */
457
- var PROMPTBOOK_VERSION = '0.52.0-16';
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,
@@ -3175,6 +3175,23 @@
3175
3175
  */
3176
3176
  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");
3177
3177
 
3178
+ /**
3179
+ * Returns the same value that is passed as argument.
3180
+ * No side effects.
3181
+ *
3182
+ * Note: It can be usefull for leveling indentation
3183
+ *
3184
+ * @param value any values
3185
+ * @returns the same values
3186
+ */
3187
+ function just(value) {
3188
+ if (value === undefined) {
3189
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3190
+ return undefined;
3191
+ }
3192
+ return value;
3193
+ }
3194
+
3178
3195
  /**
3179
3196
  * This error indicates that promptbook not found in the library
3180
3197
  */
@@ -3232,6 +3249,12 @@
3232
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 ")));
3233
3250
  }
3234
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
+ }
3235
3258
  this.library.set(promptbook.promptbookUrl, promptbook);
3236
3259
  }
3237
3260
  }
@@ -3258,6 +3281,9 @@
3258
3281
  var _this = this;
3259
3282
  var promptbook = this.library.get(url);
3260
3283
  if (!promptbook) {
3284
+ if (this.listPromptbooks().length === 0) {
3285
+ throw new PromptbookNotFoundError(spaceTrim.spaceTrim("\n Promptbook with url \"".concat(url, "\" not found\n\n No promptbooks available\n ")));
3286
+ }
3261
3287
  throw new PromptbookNotFoundError(spaceTrim.spaceTrim(function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPromptbooks()
3262
3288
  .map(function (promptbookUrl) { return "- ".concat(promptbookUrl); })
3263
3289
  .join('\n')), "\n\n "); }));
@@ -3418,40 +3444,160 @@
3418
3444
  if (!isRunningInNode()) {
3419
3445
  throw new Error('Function `createPromptbookLibraryFromDirectory` can only be run in Node.js environment because it reads the file system.');
3420
3446
  }
3421
- var _a = (options || {}).isRecursive, isRecursive = _a === void 0 ? true : _a;
3447
+ var _a = options || {}, _b = _a.isRecursive, isRecursive = _b === void 0 ? true : _b, _c = _a.isVerbose, isVerbose = _c === void 0 ? false : _c;
3422
3448
  return createPromptbookLibraryFromPromise(function () { return __awaiter(_this, void 0, void 0, function () {
3423
- return __generator(this, function (_a) {
3424
- /*
3425
- const justR_equire = require; /* <- Note: [1] * /
3426
- const readdir = justR_equire(just('fs/promises') /* <- Note: [2] * /).readdir as typeof readdirType;
3427
-
3428
- // TODO: !!!! Implement recursive reading
3429
- // TODO: !!!!! readAllFiles util
3430
-
3431
- const dirents = await readdir(path, {
3432
- withFileTypes: true /* Note: This is not working: recursive: isRecursive * /,
3433
- });
3434
- const fileNames = dirents.filter((dirent) => dirent.isFile()).map(({ name }) => join(name, path));
3435
-
3436
- // TODO: !!! Implement
3437
-
3438
- console.info('createPromptbookLibraryFromDirectory', { path, isRecursive, fileNames });
3439
- throw new Error('Not implemented yet');
3440
-
3441
- */
3442
- console.info('createPromptbookLibraryFromDirectory', { path: path, isRecursive: isRecursive });
3443
- return [2 /*return*/, []];
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) {
3453
+ case 0:
3454
+ fsPromises = require(just('fs/promises') /* <- Note: [1] */);
3455
+ readFile = fsPromises.readFile;
3456
+ return [4 /*yield*/, listAllFiles(path, isRecursive)];
3457
+ case 1:
3458
+ fileNames = _d.sent();
3459
+ if (isVerbose) {
3460
+ console.info('createPromptbookLibraryFromDirectory', { path: path, isRecursive: isRecursive, fileNames: fileNames });
3461
+ }
3462
+ promptbooks = [];
3463
+ _d.label = 2;
3464
+ case 2:
3465
+ _d.trys.push([2, 11, 12, 13]);
3466
+ fileNames_1 = __values(fileNames), fileNames_1_1 = fileNames_1.next();
3467
+ _d.label = 3;
3468
+ case 3:
3469
+ if (!!fileNames_1_1.done) return [3 /*break*/, 10];
3470
+ fileName = fileNames_1_1.value;
3471
+ promptbook = null;
3472
+ if (!fileName.endsWith('.ptbk.md')) return [3 /*break*/, 5];
3473
+ return [4 /*yield*/, readFile(fileName, 'utf8')];
3474
+ case 4:
3475
+ promptbookString = (_d.sent());
3476
+ promptbook = promptbookStringToJson(promptbookString);
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
+ }
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));
3492
+ }
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
+ }
3501
+ }
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);
3508
+ }
3509
+ }
3510
+ _d.label = 9;
3511
+ case 9:
3512
+ fileNames_1_1 = fileNames_1.next();
3513
+ return [3 /*break*/, 3];
3514
+ case 10: return [3 /*break*/, 13];
3515
+ case 11:
3516
+ e_1_1 = _d.sent();
3517
+ e_1 = { error: e_1_1 };
3518
+ return [3 /*break*/, 13];
3519
+ case 12:
3520
+ try {
3521
+ if (fileNames_1_1 && !fileNames_1_1.done && (_c = fileNames_1.return)) _c.call(fileNames_1);
3522
+ }
3523
+ finally { if (e_1) throw e_1.error; }
3524
+ return [7 /*endfinally*/];
3525
+ case 13: return [2 /*return*/, promptbooks];
3526
+ }
3444
3527
  });
3445
3528
  }); });
3446
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
+ }
3447
3592
  /***
3448
- * Note: [1] Fixing the dynamic import issue in Webpack
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 !)
3449
3596
  * > ./node_modules/@promptbook/core/esm/index.es.js
3450
3597
  * > Critical dependency: the request of a dependency is an expression
3451
3598
  *
3452
- * Note: [2] Using require(just('fs/promises')) to allow
3599
+ * Note: [1] Using require(just('fs/promises')) to allow
3453
3600
  * the `@promptbook/core` work for both Node.js and browser environments
3454
- * TODO: [🍓][🚯] !!! Add to README and samples + maybe make `@promptbook/library` package
3455
3601
  */
3456
3602
 
3457
3603
  /**
@@ -3552,23 +3698,6 @@
3552
3698
  * TODO: [🍓][🚯] !!! Add to README and samples + maybe make `@promptbook/library` package
3553
3699
  */
3554
3700
 
3555
- /**
3556
- * Returns the same value that is passed as argument.
3557
- * No side effects.
3558
- *
3559
- * Note: It can be usefull for leveling indentation
3560
- *
3561
- * @param value any values
3562
- * @returns the same values
3563
- */
3564
- function just(value) {
3565
- if (value === undefined) {
3566
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3567
- return undefined;
3568
- }
3569
- return value;
3570
- }
3571
-
3572
3701
  /**
3573
3702
  * Just testing imports and compatibility
3574
3703
  */