@promptbook/core 0.55.0 → 0.56.0-2

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.
Files changed (44) hide show
  1. package/README.md +3 -2
  2. package/esm/index.es.js +31 -262
  3. package/esm/index.es.js.map +1 -1
  4. package/esm/typings/_packages/anthropic-claude.index.d.ts +2 -0
  5. package/esm/typings/_packages/azure-openai.index.d.ts +2 -0
  6. package/esm/typings/_packages/cli.index.d.ts +2 -0
  7. package/esm/typings/_packages/core.index.d.ts +3 -3
  8. package/esm/typings/_packages/execute-javascript.index.d.ts +2 -0
  9. package/esm/typings/_packages/{mock.index.d.ts → fake-llm.index.d.ts} +2 -1
  10. package/esm/typings/_packages/langtail.index.d.ts +2 -0
  11. package/esm/typings/_packages/node.index.d.ts +4 -0
  12. package/esm/typings/_packages/openai.index.d.ts +2 -0
  13. package/esm/typings/_packages/remote-client.index.d.ts +2 -0
  14. package/esm/typings/_packages/remote-server.index.d.ts +2 -0
  15. package/esm/typings/_packages/types.index.d.ts +3 -2
  16. package/esm/typings/_packages/utils.index.d.ts +2 -1
  17. package/esm/typings/execution/plugins/llm-execution-tools/mocked/fakeTextToExpectations.d.ts +0 -1
  18. package/esm/typings/library/constructors/createPromptbookLibraryFromDirectory.d.ts +0 -10
  19. package/esm/typings/library/constructors/createPromptbookLibraryFromPromise.d.ts +0 -3
  20. package/esm/typings/library/constructors/createPromptbookLibraryFromSources.d.ts +0 -3
  21. package/esm/typings/library/constructors/createPromptbookLibraryFromUrl.d.ts +0 -4
  22. package/esm/typings/library/constructors/createPromptbookSublibrary.d.ts +0 -3
  23. package/package.json +1 -1
  24. package/umd/index.umd.js +34 -265
  25. package/umd/index.umd.js.map +1 -1
  26. package/umd/typings/_packages/anthropic-claude.index.d.ts +2 -0
  27. package/umd/typings/_packages/azure-openai.index.d.ts +2 -0
  28. package/umd/typings/_packages/cli.index.d.ts +2 -0
  29. package/umd/typings/_packages/core.index.d.ts +3 -3
  30. package/umd/typings/_packages/execute-javascript.index.d.ts +2 -0
  31. package/umd/typings/_packages/{mock.index.d.ts → fake-llm.index.d.ts} +2 -1
  32. package/umd/typings/_packages/langtail.index.d.ts +2 -0
  33. package/umd/typings/_packages/node.index.d.ts +4 -0
  34. package/umd/typings/_packages/openai.index.d.ts +2 -0
  35. package/umd/typings/_packages/remote-client.index.d.ts +2 -0
  36. package/umd/typings/_packages/remote-server.index.d.ts +2 -0
  37. package/umd/typings/_packages/types.index.d.ts +3 -2
  38. package/umd/typings/_packages/utils.index.d.ts +2 -1
  39. package/umd/typings/execution/plugins/llm-execution-tools/mocked/fakeTextToExpectations.d.ts +0 -1
  40. package/umd/typings/library/constructors/createPromptbookLibraryFromDirectory.d.ts +0 -10
  41. package/umd/typings/library/constructors/createPromptbookLibraryFromPromise.d.ts +0 -3
  42. package/umd/typings/library/constructors/createPromptbookLibraryFromSources.d.ts +0 -3
  43. package/umd/typings/library/constructors/createPromptbookLibraryFromUrl.d.ts +0 -4
  44. package/umd/typings/library/constructors/createPromptbookSublibrary.d.ts +0 -3
package/README.md CHANGED
@@ -320,6 +320,7 @@ Or you can install them separately:
320
320
  - ⭐ **[ptbk](https://www.npmjs.com/package/ptbk)** - Bundle of all packages, when you want to install everything and you don't care about the size
321
321
  - **[promptbook](https://www.npmjs.com/package/promptbook)** - Same as `ptbk`
322
322
  - **[@promptbook/core](https://www.npmjs.com/package/@promptbook/core)** - Core of the library, it contains the main logic for promptbooks
323
+ - **[@promptbook/node](https://www.npmjs.com/package/@promptbook/node)** - Core of the library for Node.js
323
324
  - ⭐ **[@promptbook/utils](https://www.npmjs.com/package/@promptbook/utils)** - Utility functions used in the library but also useful for individual use in preprocessing and postprocessing LLM inputs and outputs
324
325
  - _(Not finished)_ **[@promptbook/wizzard](https://www.npmjs.com/package/@promptbook/wizzard)** - Wizard for creating+running promptbooks in single line
325
326
  - **[@promptbook/execute-javascript](https://www.npmjs.com/package/@promptbook/execute-javascript)** - Execution tools for javascript inside promptbooks
@@ -327,7 +328,7 @@ Or you can install them separately:
327
328
  - **[@promptbook/anthropic-claude](https://www.npmjs.com/package/@promptbook/anthropic-claude)** - Execution tools for Anthropic Claude API, wrapper around Anthropic Claude SDK
328
329
  - **[@promptbook/azure-openai](https://www.npmjs.com/package/@promptbook/azure-openai)** - Execution tools for Azure OpenAI API
329
330
  - **[@promptbook/langtail](https://www.npmjs.com/package/@promptbook/langtail)** - Execution tools for Langtail API, wrapper around Langtail SDK
330
- - **[@promptbook/mock](https://www.npmjs.com/package/@promptbook/mock)** - Mocked execution tools for testing the library and saving the tokens
331
+ - **[@promptbook/fake-llm](https://www.npmjs.com/package/@promptbook/fake-llm)** - Mocked execution tools for testing the library and saving the tokens
331
332
  - **[@promptbook/remote-client](https://www.npmjs.com/package/@promptbook/remote-client)** - Remote client for remote execution of promptbooks
332
333
  - **[@promptbook/remote-server](https://www.npmjs.com/package/@promptbook/remote-server)** - Remote server for remote execution of promptbooks
333
334
  - **[@promptbook/types](https://www.npmjs.com/package/@promptbook/types)** - Just typescript types used in the library
@@ -512,7 +513,7 @@ There are [postprocessing functions](#postprocessing-functions) that can be used
512
513
  Executor is a simple async function that takes **input parameters** and returns **output parameters**.
513
514
  It is constructed by combining execution tools and promptbook to execute together.
514
515
 
515
- ### 🃏 Jokers (conditions)
516
+ ### 🃏 Jokers (conditions)
516
517
 
517
518
  Joker is a previously defined parameter that is used to bypass some parts of the pipeline.
518
519
  If the joker is present in the template, it is checked to see if it meets the requirements (without postprocessing), and if so, it is used instead of executing that prompt template. There can be multiple wildcards in a prompt template, if so they are checked in order and the first one that meets the requirements is used.
package/esm/index.es.js CHANGED
@@ -1,7 +1,6 @@
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';
5
4
  import moment from 'moment';
6
5
 
7
6
  /*! *****************************************************************************
@@ -448,7 +447,7 @@ function union() {
448
447
  /**
449
448
  * The version of the Promptbook library
450
449
  */
451
- var PROMPTBOOK_VERSION = '0.55.0-2';
450
+ var PROMPTBOOK_VERSION = '0.56.0-1';
452
451
 
453
452
  /**
454
453
  * Parses the template and returns the list of all parameter names
@@ -3343,36 +3342,6 @@ function usageToWorktime(usage) {
3343
3342
  return uncertainNumber;
3344
3343
  }
3345
3344
 
3346
- /**
3347
- * Detects if the code is running in a browser environment in main thread (Not in a web worker)
3348
- */
3349
- new Function("\n try {\n return this === window;\n } catch (e) {\n return false;\n }\n");
3350
- /**
3351
- * Detects if the code is running in a Node.js environment
3352
- */
3353
- var isRunningInNode = new Function("\n try {\n return this === global;\n } catch (e) {\n return false;\n }\n");
3354
- /**
3355
- * Detects if the code is running in a web worker
3356
- */
3357
- 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");
3358
-
3359
- /**
3360
- * Returns the same value that is passed as argument.
3361
- * No side effects.
3362
- *
3363
- * Note: It can be usefull for leveling indentation
3364
- *
3365
- * @param value any values
3366
- * @returns the same values
3367
- */
3368
- function just(value) {
3369
- if (value === undefined) {
3370
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3371
- return undefined;
3372
- }
3373
- return value;
3374
- }
3375
-
3376
3345
  /**
3377
3346
  * Library of promptbooks that groups together promptbooks for an application.
3378
3347
  * This implementation is a very thin wrapper around the Array / Map of promptbooks.
@@ -3492,9 +3461,6 @@ function createPromptbookLibraryFromSources() {
3492
3461
  }
3493
3462
  return new (SimplePromptbookLibrary.bind.apply(SimplePromptbookLibrary, __spreadArray([void 0], __read(promptbooks), false)))();
3494
3463
  }
3495
- /***
3496
- * TODO: [🍓][🚯] !!! Add to README and samples + maybe make `@promptbook/library` package
3497
- */
3498
3464
 
3499
3465
  /**
3500
3466
  * Constructs Promptbook from async sources
@@ -3578,226 +3544,6 @@ function createPromptbookLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
3578
3544
  isResponsibleForPrompt: isResponsibleForPrompt,
3579
3545
  };
3580
3546
  }
3581
- /***
3582
- * TODO: [🍓][🚯] !!! Add to README and samples + maybe make `@promptbook/library` package
3583
- */
3584
-
3585
- /**
3586
- * Constructs Promptbook from given directory
3587
- *
3588
- * Note: Works only in Node.js environment because it reads the file system
3589
- *
3590
- * @param path - path to the directory with promptbooks
3591
- * @param options - Misc options for the library
3592
- * @returns PromptbookLibrary
3593
- */
3594
- function createPromptbookLibraryFromDirectory(path, options) {
3595
- return __awaiter(this, void 0, void 0, function () {
3596
- var _a, _b, isRecursive, _c, isVerbose, _d, isLazyLoaded, _e, isCrashOnError, library;
3597
- var _this = this;
3598
- return __generator(this, function (_f) {
3599
- switch (_f.label) {
3600
- case 0:
3601
- if (!isRunningInNode()) {
3602
- throw new Error('Function `createPromptbookLibraryFromDirectory` can only be run in Node.js environment because it reads the file system.');
3603
- }
3604
- _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;
3605
- library = createPromptbookLibraryFromPromise(function () { return __awaiter(_this, void 0, void 0, function () {
3606
- var fsPromises, readFile, fileNames, promptbooks, _loop_1, fileNames_1, fileNames_1_1, fileName, e_1_1;
3607
- var e_1, _a;
3608
- return __generator(this, function (_b) {
3609
- switch (_b.label) {
3610
- case 0:
3611
- if (isVerbose) {
3612
- console.info("Creating promptbook library from path ".concat(path));
3613
- }
3614
- fsPromises = require(just('fs/promises') /* <- Note: [1] */);
3615
- readFile = fsPromises.readFile;
3616
- return [4 /*yield*/, listAllFiles(path, isRecursive)];
3617
- case 1:
3618
- fileNames = _b.sent();
3619
- if (isVerbose) {
3620
- console.info('createPromptbookLibraryFromDirectory', { path: path, isRecursive: isRecursive, fileNames: fileNames });
3621
- }
3622
- promptbooks = [];
3623
- _loop_1 = function (fileName) {
3624
- var promptbook, promptbookString, _c, _d, error_1, wrappedErrorMessage;
3625
- return __generator(this, function (_e) {
3626
- switch (_e.label) {
3627
- case 0:
3628
- _e.trys.push([0, 6, , 7]);
3629
- promptbook = null;
3630
- if (!fileName.endsWith('.ptbk.md')) return [3 /*break*/, 2];
3631
- return [4 /*yield*/, readFile(fileName, 'utf8')];
3632
- case 1:
3633
- promptbookString = (_e.sent());
3634
- promptbook = promptbookStringToJson(promptbookString);
3635
- return [3 /*break*/, 5];
3636
- case 2:
3637
- if (!fileName.endsWith('.ptbk.json')) return [3 /*break*/, 4];
3638
- if (isVerbose) {
3639
- console.info("Loading ".concat(fileName));
3640
- }
3641
- _d = (_c = JSON).parse;
3642
- return [4 /*yield*/, readFile(fileName, 'utf8')];
3643
- case 3:
3644
- // TODO: Handle non-valid JSON files
3645
- promptbook = _d.apply(_c, [_e.sent()]);
3646
- return [3 /*break*/, 5];
3647
- case 4:
3648
- if (isVerbose) {
3649
- console.info("Skipping file ".concat(fileName));
3650
- }
3651
- _e.label = 5;
3652
- case 5:
3653
- // ---
3654
- if (promptbook !== null) {
3655
- if (!promptbook.promptbookUrl) {
3656
- if (isVerbose) {
3657
- console.info("Not loading ".concat(fileName, " - missing URL"));
3658
- }
3659
- }
3660
- else {
3661
- if (isVerbose) {
3662
- console.info("Loading ".concat(fileName));
3663
- }
3664
- if (!isCrashOnError) {
3665
- // Note: Validate promptbook to check if it is logically correct to not crash on invalid promptbooks
3666
- // But be handled in current try-catch block
3667
- validatePromptbookJson(promptbook);
3668
- }
3669
- // Note: [🦄] Promptbook with same url uniqueness will be checked automatically in SimplePromptbookLibrary
3670
- promptbooks.push(promptbook);
3671
- }
3672
- }
3673
- return [3 /*break*/, 7];
3674
- case 6:
3675
- error_1 = _e.sent();
3676
- if (!(error_1 instanceof Error)) {
3677
- throw error_1;
3678
- }
3679
- 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 "); });
3680
- if (isCrashOnError) {
3681
- throw new PromptbookLibraryError(wrappedErrorMessage);
3682
- }
3683
- console.error(wrappedErrorMessage);
3684
- return [3 /*break*/, 7];
3685
- case 7: return [2 /*return*/];
3686
- }
3687
- });
3688
- };
3689
- _b.label = 2;
3690
- case 2:
3691
- _b.trys.push([2, 7, 8, 9]);
3692
- fileNames_1 = __values(fileNames), fileNames_1_1 = fileNames_1.next();
3693
- _b.label = 3;
3694
- case 3:
3695
- if (!!fileNames_1_1.done) return [3 /*break*/, 6];
3696
- fileName = fileNames_1_1.value;
3697
- return [5 /*yield**/, _loop_1(fileName)];
3698
- case 4:
3699
- _b.sent();
3700
- _b.label = 5;
3701
- case 5:
3702
- fileNames_1_1 = fileNames_1.next();
3703
- return [3 /*break*/, 3];
3704
- case 6: return [3 /*break*/, 9];
3705
- case 7:
3706
- e_1_1 = _b.sent();
3707
- e_1 = { error: e_1_1 };
3708
- return [3 /*break*/, 9];
3709
- case 8:
3710
- try {
3711
- if (fileNames_1_1 && !fileNames_1_1.done && (_a = fileNames_1.return)) _a.call(fileNames_1);
3712
- }
3713
- finally { if (e_1) throw e_1.error; }
3714
- return [7 /*endfinally*/];
3715
- case 9: return [2 /*return*/, promptbooks];
3716
- }
3717
- });
3718
- }); });
3719
- if (!(isLazyLoaded === false)) return [3 /*break*/, 2];
3720
- return [4 /*yield*/, library.listPromptbooks()];
3721
- case 1:
3722
- _f.sent();
3723
- _f.label = 2;
3724
- case 2: return [2 /*return*/, library];
3725
- }
3726
- });
3727
- });
3728
- }
3729
- /**
3730
- * Reads all files in the directory
3731
- *
3732
- * @param path
3733
- * @param isRecursive
3734
- * @returns List of all files in the directory
3735
- * @private internal function for `createPromptbookLibraryFromDirectory`
3736
- */
3737
- function listAllFiles(path, isRecursive) {
3738
- return __awaiter(this, void 0, void 0, function () {
3739
- var fsPromises, readdir, dirents, fileNames, _a, _b, dirent, subPath, _c, _d, _e, _f, e_2_1;
3740
- var e_2, _g;
3741
- return __generator(this, function (_h) {
3742
- switch (_h.label) {
3743
- case 0:
3744
- fsPromises = require(just('fs/promises') /* <- Note: [1] */);
3745
- readdir = fsPromises.readdir;
3746
- return [4 /*yield*/, readdir(path, {
3747
- withFileTypes: true /* Note: This is not working: recursive: isRecursive */,
3748
- })];
3749
- case 1:
3750
- dirents = _h.sent();
3751
- fileNames = dirents.filter(function (dirent) { return dirent.isFile(); }).map(function (_a) {
3752
- var name = _a.name;
3753
- return join(path, name);
3754
- });
3755
- if (!isRecursive) return [3 /*break*/, 9];
3756
- _h.label = 2;
3757
- case 2:
3758
- _h.trys.push([2, 7, 8, 9]);
3759
- _a = __values(dirents.filter(function (dirent) { return dirent.isDirectory(); })), _b = _a.next();
3760
- _h.label = 3;
3761
- case 3:
3762
- if (!!_b.done) return [3 /*break*/, 6];
3763
- dirent = _b.value;
3764
- subPath = join(path, dirent.name);
3765
- _d = (_c = fileNames.push).apply;
3766
- _e = [fileNames];
3767
- _f = [[]];
3768
- return [4 /*yield*/, listAllFiles(subPath, isRecursive)];
3769
- case 4:
3770
- _d.apply(_c, _e.concat([__spreadArray.apply(void 0, _f.concat([__read.apply(void 0, [(_h.sent())]), false]))]));
3771
- _h.label = 5;
3772
- case 5:
3773
- _b = _a.next();
3774
- return [3 /*break*/, 3];
3775
- case 6: return [3 /*break*/, 9];
3776
- case 7:
3777
- e_2_1 = _h.sent();
3778
- e_2 = { error: e_2_1 };
3779
- return [3 /*break*/, 9];
3780
- case 8:
3781
- try {
3782
- if (_b && !_b.done && (_g = _a.return)) _g.call(_a);
3783
- }
3784
- finally { if (e_2) throw e_2.error; }
3785
- return [7 /*endfinally*/];
3786
- case 9: return [2 /*return*/, fileNames];
3787
- }
3788
- });
3789
- });
3790
- }
3791
- /***
3792
- * TODO: [🧠] Maybe do not do hacks like [1] and just create package @promptbook/node
3793
- * [🍓][🚯] maybe make `@promptbook/library` package
3794
- * TODO: Fix the dynamic import issue in Webpack (! Not working !)
3795
- * > ./node_modules/@promptbook/core/esm/index.es.js
3796
- * > Critical dependency: the request of a dependency is an expression
3797
- *
3798
- * Note: [1] Using require(just('fs/promises')) to allow
3799
- * the `@promptbook/core` work for both Node.js and browser environments
3800
- */
3801
3547
 
3802
3548
  /**
3803
3549
  * Constructs Promptbook from remote Promptbase URL
@@ -3830,10 +3576,6 @@ function createPromptbookLibraryFromUrl(url, options) {
3830
3576
  });
3831
3577
  });
3832
3578
  }
3833
- /***
3834
- * TODO: [⚖] Compatible with remote server
3835
- * TODO: [🍓][🚯] !!! Add to README and samples + maybe make `@promptbook/library` package
3836
- */
3837
3579
 
3838
3580
  /**
3839
3581
  * Creates PromptbookLibrary as a subset of another PromptbookLibrary
@@ -3913,9 +3655,36 @@ function createPromptbookSublibrary(library, predicate) {
3913
3655
  isResponsibleForPrompt: isResponsibleForPrompt,
3914
3656
  };
3915
3657
  }
3916
- /***
3917
- * TODO: [🍓][🚯] !!! Add to README and samples + maybe make `@promptbook/library` package
3658
+
3659
+ /**
3660
+ * Detects if the code is running in a browser environment in main thread (Not in a web worker)
3661
+ */
3662
+ new Function("\n try {\n return this === window;\n } catch (e) {\n return false;\n }\n");
3663
+ /**
3664
+ * Detects if the code is running in a Node.js environment
3918
3665
  */
3666
+ var isRunningInNode = new Function("\n try {\n return this === global;\n } catch (e) {\n return false;\n }\n");
3667
+ /**
3668
+ * Detects if the code is running in a web worker
3669
+ */
3670
+ 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");
3671
+
3672
+ /**
3673
+ * Returns the same value that is passed as argument.
3674
+ * No side effects.
3675
+ *
3676
+ * Note: It can be usefull for leveling indentation
3677
+ *
3678
+ * @param value any values
3679
+ * @returns the same values
3680
+ */
3681
+ function just(value) {
3682
+ if (value === undefined) {
3683
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3684
+ return undefined;
3685
+ }
3686
+ return value;
3687
+ }
3919
3688
 
3920
3689
  /**
3921
3690
  * Just testing imports and compatibility
@@ -4245,5 +4014,5 @@ function executionReportJsonToString(executionReportJson, options) {
4245
4014
  * TODO: [🧠] Allow to filter out some parts of the report by options
4246
4015
  */
4247
4016
 
4248
- export { CallbackInterfaceTools, ExecutionReportStringOptionsDefaults, ExecutionTypes, ExpectError, MultipleLlmExecutionTools, PROMPTBOOK_VERSION, PromptbookExecutionError, PromptbookLibraryError, PromptbookLogicError, PromptbookNotFoundError, PromptbookReferenceError, PromptbookSyntaxError, SimplePromptInterfaceTools, SimplePromptbookLibrary, TemplateError, UnexpectedError, addUsage, assertsExecutionSuccessful, checkExpectations, createPromptbookExecutor, createPromptbookLibraryFromDirectory, createPromptbookLibraryFromSources, createPromptbookLibraryFromUrl, createPromptbookSublibrary, executionReportJsonToString, isPassingExpectations, justTestFsImport, prettifyPromptbookString, promptbookJsonToString, promptbookStringToJson, usageToWorktime, validatePromptbookJson };
4017
+ export { CallbackInterfaceTools, ExecutionReportStringOptionsDefaults, ExecutionTypes, ExpectError, MultipleLlmExecutionTools, PROMPTBOOK_VERSION, PromptbookExecutionError, PromptbookLibraryError, PromptbookLogicError, PromptbookNotFoundError, PromptbookReferenceError, PromptbookSyntaxError, SimplePromptInterfaceTools, SimplePromptbookLibrary, TemplateError, UnexpectedError, addUsage, assertsExecutionSuccessful, checkExpectations, createPromptbookExecutor, createPromptbookLibraryFromSources, createPromptbookLibraryFromUrl, createPromptbookSublibrary, executionReportJsonToString, isPassingExpectations, justTestFsImport, prettifyPromptbookString, promptbookJsonToString, promptbookStringToJson, usageToWorktime, validatePromptbookJson };
4249
4018
  //# sourceMappingURL=index.es.js.map