@promptbook/core 0.52.0-9 → 0.52.0
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 +1 -13
- package/esm/index.es.js +321 -44
- package/esm/index.es.js.map +1 -1
- package/esm/typings/_packages/core.index.d.ts +4 -2
- package/esm/typings/_packages/utils.index.d.ts +2 -0
- package/esm/typings/errors/PromptbookLibraryError.d.ts +7 -0
- package/esm/typings/execution/ExecutionTools.d.ts +5 -3
- package/esm/typings/execution/plugins/llm-execution-tools/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +0 -1
- package/esm/typings/execution/plugins/llm-execution-tools/azure-openai/AzureOpenAiExecutionTools.d.ts +0 -1
- package/esm/typings/execution/plugins/llm-execution-tools/langtail/LangtailExecutionTools.d.ts +0 -3
- package/esm/typings/execution/plugins/llm-execution-tools/openai/OpenAiExecutionTools.d.ts +1 -1
- package/esm/typings/execution/plugins/llm-execution-tools/remote/RemoteLlmExecutionTools.d.ts +1 -1
- package/esm/typings/execution/plugins/llm-execution-tools/remote/startRemoteServer.d.ts +1 -0
- package/esm/typings/execution/utils/forEachAsync.d.ts +18 -0
- package/esm/typings/library/constructors/createPromptbookLibraryFromDirectory.d.ts +28 -4
- package/esm/typings/library/constructors/createPromptbookLibraryFromDirectory.test.d.ts +1 -0
- package/esm/typings/library/constructors/createPromptbookLibraryFromPromise.d.ts +2 -2
- package/esm/typings/library/constructors/createPromptbookLibraryFromPromise.test.d.ts +1 -0
- package/esm/typings/library/constructors/createPromptbookLibraryFromSources.test.d.ts +1 -0
- package/esm/typings/library/constructors/createPromptbookLibraryFromUrl.d.ts +22 -5
- package/esm/typings/library/constructors/justTestFsImport.d.ts +7 -0
- package/esm/typings/types/Prompt.d.ts +1 -1
- package/esm/typings/types/typeAliases.d.ts +2 -2
- package/package.json +2 -2
- package/umd/index.umd.js +325 -48
- package/umd/index.umd.js.map +1 -1
- package/umd/typings/_packages/core.index.d.ts +4 -2
- package/umd/typings/_packages/utils.index.d.ts +2 -0
- package/umd/typings/errors/PromptbookLibraryError.d.ts +7 -0
- package/umd/typings/execution/ExecutionTools.d.ts +5 -3
- package/umd/typings/execution/plugins/llm-execution-tools/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +0 -1
- package/umd/typings/execution/plugins/llm-execution-tools/azure-openai/AzureOpenAiExecutionTools.d.ts +0 -1
- package/umd/typings/execution/plugins/llm-execution-tools/langtail/LangtailExecutionTools.d.ts +0 -3
- package/umd/typings/execution/plugins/llm-execution-tools/openai/OpenAiExecutionTools.d.ts +1 -1
- package/umd/typings/execution/plugins/llm-execution-tools/remote/RemoteLlmExecutionTools.d.ts +1 -1
- package/umd/typings/execution/plugins/llm-execution-tools/remote/startRemoteServer.d.ts +1 -0
- package/umd/typings/execution/utils/forEachAsync.d.ts +18 -0
- package/umd/typings/library/constructors/createPromptbookLibraryFromDirectory.d.ts +28 -4
- package/umd/typings/library/constructors/createPromptbookLibraryFromDirectory.test.d.ts +1 -0
- package/umd/typings/library/constructors/createPromptbookLibraryFromPromise.d.ts +2 -2
- package/umd/typings/library/constructors/createPromptbookLibraryFromPromise.test.d.ts +1 -0
- package/umd/typings/library/constructors/createPromptbookLibraryFromSources.test.d.ts +1 -0
- package/umd/typings/library/constructors/createPromptbookLibraryFromUrl.d.ts +22 -5
- package/umd/typings/library/constructors/justTestFsImport.d.ts +7 -0
- package/umd/typings/types/Prompt.d.ts +1 -1
- package/umd/typings/types/typeAliases.d.ts +2 -2
- package/esm/typings/wizzard/Wizzard.d.ts +0 -4
- package/esm/typings/wizzard/sample.d.ts +0 -6
- package/umd/typings/wizzard/Wizzard.d.ts +0 -4
- package/umd/typings/wizzard/sample.d.ts +0 -6
package/README.md
CHANGED
|
@@ -11,7 +11,6 @@ Library to supercharge your use of large language models
|
|
|
11
11
|
[](https://github.com/webgptorg/promptbook/issues)
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
[](https://socket.dev/npm/package/@promptbook/core)
|
|
15
14
|
|
|
16
15
|
|
|
17
16
|
|
|
@@ -75,15 +74,6 @@ In any of these situations, but especially in (3), the Promptbook library can ma
|
|
|
75
74
|
- _(Not ready yet)_ Leverage the **streaming** to make super cool UI/UX.
|
|
76
75
|
- _(Not ready yet)_ **A/B testing** to determine which prompt works best for the job.
|
|
77
76
|
|
|
78
|
-

|
|
79
|
-
|
|
80
|
-
## 🧔 Promptbook _(for prompt-engeneers)_
|
|
81
|
-
|
|
82
|
-
**P**romp**t** **b**oo**k** markdown file (**PTBK** for short, or `.ptbk.md`) is document that describes a series of prompts that are chained together to form somewhat reciepe for transforming natural language input. Inside a PTBK you can use chat prompts, completion prompts, scripting or trigger interaction with user to ask for additional information.
|
|
83
|
-
|
|
84
|
-
- Multiple promptbooks forms a library which will become a **part of your application codebase**.
|
|
85
|
-
- Theese promptbooks are designed such as they **can be written by non-programmers**.
|
|
86
|
-
|
|
87
77
|
|
|
88
78
|
|
|
89
79
|
### Sample:
|
|
@@ -96,7 +86,7 @@ File `write-website-content.ptbk.md`:
|
|
|
96
86
|
>
|
|
97
87
|
> Instructions for creating web page content.
|
|
98
88
|
>
|
|
99
|
-
> - PROMPTBOOK URL https://promptbook.webgpt.com/en/write-website-content.ptbk.md
|
|
89
|
+
> - PROMPTBOOK URL https://promptbook.webgpt.com/en/write-website-content.ptbk.md
|
|
100
90
|
> - PROMPTBOOK VERSION 0.0.1
|
|
101
91
|
> - INPUT PARAM `{rawTitle}` Automatically suggested a site name or empty text
|
|
102
92
|
> - INPUT PARAM `{rawAssigment}` Automatically generated site entry from image recognition
|
|
@@ -555,8 +545,6 @@ _Note: LLMs work with tokens, not characters, but in Promptbooks we want to use
|
|
|
555
545
|
```markdown
|
|
556
546
|
# ✨ Sample: Expectations
|
|
557
547
|
|
|
558
|
-
- PROMPTBOOK URL https://promptbook.example.com/samples/postprocessing-2.ptbk.md@v1
|
|
559
|
-
- PROMPTBOOK VERSION 1.0.0
|
|
560
548
|
- INPUT PARAMETER {yourName} Name of the hero
|
|
561
549
|
|
|
562
550
|
## 💬 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-
|
|
451
|
+
var PROMPTBOOK_VERSION = '0.52.0-33';
|
|
451
452
|
|
|
452
453
|
/**
|
|
453
454
|
* Parses the template and returns the list of all parameter names
|
|
@@ -1824,6 +1825,9 @@ function promptbookStringToJson(promptbookString) {
|
|
|
1824
1825
|
postprocessing = undefined;
|
|
1825
1826
|
}
|
|
1826
1827
|
dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, executionType: executionType, content: content })));
|
|
1828
|
+
if (templateModelRequirements.modelVariant === undefined) {
|
|
1829
|
+
templateModelRequirements.modelVariant = 'CHAT';
|
|
1830
|
+
}
|
|
1827
1831
|
promptbookJson.promptTemplates.push({
|
|
1828
1832
|
name: titleToName(section.title),
|
|
1829
1833
|
title: section.title,
|
|
@@ -2169,6 +2173,7 @@ function isValidUrl(url) {
|
|
|
2169
2173
|
* @throws {PromptbookLogicError} on logical error in the promptbook
|
|
2170
2174
|
*/
|
|
2171
2175
|
function validatePromptbookJson(promptbook) {
|
|
2176
|
+
// TODO: [🧠] Maybe test if promptbook is a promise and make specific error case for that
|
|
2172
2177
|
var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
|
|
2173
2178
|
if (promptbook.promptbookUrl !== undefined) {
|
|
2174
2179
|
if (!isValidUrl(promptbook.promptbookUrl)) {
|
|
@@ -2176,6 +2181,16 @@ function validatePromptbookJson(promptbook) {
|
|
|
2176
2181
|
throw new PromptbookLogicError("Invalid promptbook URL \"".concat(promptbook.promptbookUrl, "\""));
|
|
2177
2182
|
}
|
|
2178
2183
|
}
|
|
2184
|
+
// TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
|
|
2185
|
+
if (!Array.isArray(promptbook.parameters)) {
|
|
2186
|
+
// TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
|
|
2187
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Promptbook is valid JSON but with wrong structure\n\n promptbook.parameters expected to be an array, but got ".concat(typeof promptbook.parameters, "\n ")));
|
|
2188
|
+
}
|
|
2189
|
+
// TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
|
|
2190
|
+
if (!Array.isArray(promptbook.promptTemplates)) {
|
|
2191
|
+
// TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
|
|
2192
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Promptbook is valid JSON but with wrong structure\n\n promptbook.promptTemplates expected to be an array, but got ".concat(typeof promptbook.promptTemplates, "\n ")));
|
|
2193
|
+
}
|
|
2179
2194
|
var _loop_1 = function (parameter) {
|
|
2180
2195
|
if (parameter.isInput && parameter.isOutput) {
|
|
2181
2196
|
throw new PromptbookLogicError("Parameter {".concat(parameter.name, "} can not be both input and output"));
|
|
@@ -2222,9 +2237,8 @@ function validatePromptbookJson(promptbook) {
|
|
|
2222
2237
|
}
|
|
2223
2238
|
definedParameters.add(template.resultingParameterName);
|
|
2224
2239
|
if (template.executionType === 'PROMPT_TEMPLATE' &&
|
|
2225
|
-
(template.modelRequirements.modelVariant === undefined
|
|
2226
|
-
|
|
2227
|
-
throw new PromptbookLogicError(spaceTrim("\n\n You must specify MODEL VARIANT and MODEL NAME in the prompt template \"".concat(template.title, "\"\n\n For example:\n - MODEL VARIANT Chat\n - MODEL NAME `gpt-4-1106-preview`\n\n ")));
|
|
2240
|
+
(template.modelRequirements.modelVariant === undefined)) {
|
|
2241
|
+
throw new PromptbookLogicError(spaceTrim("\n\n You must specify MODEL VARIANT in the prompt template \"".concat(template.title, "\"\n\n For example:\n - MODEL VARIANT Chat\n - MODEL NAME `gpt-4-1106-preview`\n\n ")));
|
|
2228
2242
|
}
|
|
2229
2243
|
if (template.jokers && template.jokers.length > 0) {
|
|
2230
2244
|
if (!template.expectFormat &&
|
|
@@ -2525,7 +2539,7 @@ function createPromptbookExecutor(options) {
|
|
|
2525
2539
|
title: currentTemplate.title,
|
|
2526
2540
|
promptbookUrl: "".concat(promptbook.promptbookUrl
|
|
2527
2541
|
? promptbook.promptbookUrl
|
|
2528
|
-
: 'anonymous' /* <- [🧠]
|
|
2542
|
+
: 'anonymous' /* <- TODO: [🧠] How to deal with anonymous PROMPTBOOKs, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
|
|
2529
2543
|
parameters: parametersToPass,
|
|
2530
2544
|
content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
|
|
2531
2545
|
modelRequirements: currentTemplate.modelRequirements,
|
|
@@ -3155,6 +3169,20 @@ var SimplePromptInterfaceTools = /** @class */ (function () {
|
|
|
3155
3169
|
return SimplePromptInterfaceTools;
|
|
3156
3170
|
}());
|
|
3157
3171
|
|
|
3172
|
+
/**
|
|
3173
|
+
* This error indicates that the promptbook library cannot be propperly loaded
|
|
3174
|
+
*/
|
|
3175
|
+
var PromptbookLibraryError = /** @class */ (function (_super) {
|
|
3176
|
+
__extends(PromptbookLibraryError, _super);
|
|
3177
|
+
function PromptbookLibraryError(message) {
|
|
3178
|
+
var _this = _super.call(this, message) || this;
|
|
3179
|
+
_this.name = 'PromptbookLibraryError';
|
|
3180
|
+
Object.setPrototypeOf(_this, PromptbookLibraryError.prototype);
|
|
3181
|
+
return _this;
|
|
3182
|
+
}
|
|
3183
|
+
return PromptbookLibraryError;
|
|
3184
|
+
}(Error));
|
|
3185
|
+
|
|
3158
3186
|
/**
|
|
3159
3187
|
* Detects if the code is running in a browser environment in main thread (Not in a web worker)
|
|
3160
3188
|
*/
|
|
@@ -3168,6 +3196,23 @@ var isRunningInNode = new Function("\n try {\n return this === global;
|
|
|
3168
3196
|
*/
|
|
3169
3197
|
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
3198
|
|
|
3199
|
+
/**
|
|
3200
|
+
* Returns the same value that is passed as argument.
|
|
3201
|
+
* No side effects.
|
|
3202
|
+
*
|
|
3203
|
+
* Note: It can be usefull for leveling indentation
|
|
3204
|
+
*
|
|
3205
|
+
* @param value any values
|
|
3206
|
+
* @returns the same values
|
|
3207
|
+
*/
|
|
3208
|
+
function just(value) {
|
|
3209
|
+
if (value === undefined) {
|
|
3210
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3211
|
+
return undefined;
|
|
3212
|
+
}
|
|
3213
|
+
return value;
|
|
3214
|
+
}
|
|
3215
|
+
|
|
3171
3216
|
/**
|
|
3172
3217
|
* This error indicates that promptbook not found in the library
|
|
3173
3218
|
*/
|
|
@@ -3225,6 +3270,12 @@ var SimplePromptbookLibrary = /** @class */ (function () {
|
|
|
3225
3270
|
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
3271
|
}
|
|
3227
3272
|
validatePromptbookJson(promptbook);
|
|
3273
|
+
// Note: [🦄]
|
|
3274
|
+
if (this.library.has(promptbook.promptbookUrl) &&
|
|
3275
|
+
promptbookJsonToString(promptbook) !==
|
|
3276
|
+
promptbookJsonToString(this.library.get(promptbook.promptbookUrl))) {
|
|
3277
|
+
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 ")));
|
|
3278
|
+
}
|
|
3228
3279
|
this.library.set(promptbook.promptbookUrl, promptbook);
|
|
3229
3280
|
}
|
|
3230
3281
|
}
|
|
@@ -3251,6 +3302,9 @@ var SimplePromptbookLibrary = /** @class */ (function () {
|
|
|
3251
3302
|
var _this = this;
|
|
3252
3303
|
var promptbook = this.library.get(url);
|
|
3253
3304
|
if (!promptbook) {
|
|
3305
|
+
if (this.listPromptbooks().length === 0) {
|
|
3306
|
+
throw new PromptbookNotFoundError(spaceTrim("\n Promptbook with url \"".concat(url, "\" not found\n\n No promptbooks available\n ")));
|
|
3307
|
+
}
|
|
3254
3308
|
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
3309
|
.map(function (promptbookUrl) { return "- ".concat(promptbookUrl); })
|
|
3256
3310
|
.join('\n')), "\n\n "); }));
|
|
@@ -3323,11 +3377,11 @@ function createPromptbookLibraryFromSources() {
|
|
|
3323
3377
|
* Note: The function does NOT return promise it returns the library directly which waits for the sources to be resolved
|
|
3324
3378
|
* when error occurs in given promise or factory function, it is thrown during `listPromptbooks` or `getPromptbookByUrl` call
|
|
3325
3379
|
*
|
|
3380
|
+
* Note: Consider using `createPromptbookLibraryFromDirectory` or `createPromptbookLibraryFromUrl`
|
|
3326
3381
|
*
|
|
3327
3382
|
* @param promptbookSourcesPromiseOrFactory
|
|
3328
3383
|
* @returns PromptbookLibrary
|
|
3329
|
-
*
|
|
3330
|
-
* @deprecated Consider using `createPromptbookLibraryFromUrl` or `createPromptbookLibraryFromDirectory`
|
|
3384
|
+
* @private Just internal tool for other constructor functions
|
|
3331
3385
|
*/
|
|
3332
3386
|
function createPromptbookLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
|
|
3333
3387
|
var library;
|
|
@@ -3400,37 +3454,252 @@ function createPromptbookLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
|
|
|
3400
3454
|
* Constructs Promptbook from given directory
|
|
3401
3455
|
*
|
|
3402
3456
|
* Note: Works only in Node.js environment because it reads the file system
|
|
3403
|
-
* Note: The function does NOT return promise it returns the library directly which dynamically loads promptbooks when needed
|
|
3404
|
-
* SO during the construction syntax and logic sources IS NOT validated
|
|
3405
3457
|
*
|
|
3406
3458
|
* @param path - path to the directory with promptbooks
|
|
3407
3459
|
* @param options - Misc options for the library
|
|
3408
3460
|
* @returns PromptbookLibrary
|
|
3409
3461
|
*/
|
|
3410
3462
|
function createPromptbookLibraryFromDirectory(path, options) {
|
|
3411
|
-
|
|
3412
|
-
|
|
3413
|
-
|
|
3414
|
-
|
|
3415
|
-
|
|
3416
|
-
|
|
3417
|
-
|
|
3418
|
-
|
|
3419
|
-
|
|
3420
|
-
|
|
3463
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
3464
|
+
var _a, _b, isRecursive, _c, isVerbose, _d, isLazyLoaded, _e, isCrashOnError, library;
|
|
3465
|
+
var _this = this;
|
|
3466
|
+
return __generator(this, function (_f) {
|
|
3467
|
+
switch (_f.label) {
|
|
3468
|
+
case 0:
|
|
3469
|
+
if (!isRunningInNode()) {
|
|
3470
|
+
throw new Error('Function `createPromptbookLibraryFromDirectory` can only be run in Node.js environment because it reads the file system.');
|
|
3471
|
+
}
|
|
3472
|
+
_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;
|
|
3473
|
+
library = createPromptbookLibraryFromPromise(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
3474
|
+
var fsPromises, readFile, fileNames, promptbooks, _loop_1, fileNames_1, fileNames_1_1, fileName, e_1_1;
|
|
3475
|
+
var e_1, _a;
|
|
3476
|
+
return __generator(this, function (_b) {
|
|
3477
|
+
switch (_b.label) {
|
|
3478
|
+
case 0:
|
|
3479
|
+
if (isVerbose) {
|
|
3480
|
+
console.info("Creating promptbook library from path ".concat(path));
|
|
3481
|
+
}
|
|
3482
|
+
fsPromises = require(just('fs/promises') /* <- Note: [1] */);
|
|
3483
|
+
readFile = fsPromises.readFile;
|
|
3484
|
+
return [4 /*yield*/, listAllFiles(path, isRecursive)];
|
|
3485
|
+
case 1:
|
|
3486
|
+
fileNames = _b.sent();
|
|
3487
|
+
if (isVerbose) {
|
|
3488
|
+
console.info('createPromptbookLibraryFromDirectory', { path: path, isRecursive: isRecursive, fileNames: fileNames });
|
|
3489
|
+
}
|
|
3490
|
+
promptbooks = [];
|
|
3491
|
+
_loop_1 = function (fileName) {
|
|
3492
|
+
var promptbook, promptbookString, _c, _d, error_1, wrappedErrorMessage;
|
|
3493
|
+
return __generator(this, function (_e) {
|
|
3494
|
+
switch (_e.label) {
|
|
3495
|
+
case 0:
|
|
3496
|
+
_e.trys.push([0, 6, , 7]);
|
|
3497
|
+
promptbook = null;
|
|
3498
|
+
if (!fileName.endsWith('.ptbk.md')) return [3 /*break*/, 2];
|
|
3499
|
+
return [4 /*yield*/, readFile(fileName, 'utf8')];
|
|
3500
|
+
case 1:
|
|
3501
|
+
promptbookString = (_e.sent());
|
|
3502
|
+
promptbook = promptbookStringToJson(promptbookString);
|
|
3503
|
+
return [3 /*break*/, 5];
|
|
3504
|
+
case 2:
|
|
3505
|
+
if (!fileName.endsWith('.ptbk.json')) return [3 /*break*/, 4];
|
|
3506
|
+
if (isVerbose) {
|
|
3507
|
+
console.info("Loading ".concat(fileName));
|
|
3508
|
+
}
|
|
3509
|
+
_d = (_c = JSON).parse;
|
|
3510
|
+
return [4 /*yield*/, readFile(fileName, 'utf8')];
|
|
3511
|
+
case 3:
|
|
3512
|
+
// TODO: Handle non-valid JSON files
|
|
3513
|
+
promptbook = _d.apply(_c, [_e.sent()]);
|
|
3514
|
+
return [3 /*break*/, 5];
|
|
3515
|
+
case 4:
|
|
3516
|
+
if (isVerbose) {
|
|
3517
|
+
console.info("Skipping file ".concat(fileName));
|
|
3518
|
+
}
|
|
3519
|
+
_e.label = 5;
|
|
3520
|
+
case 5:
|
|
3521
|
+
// ---
|
|
3522
|
+
if (promptbook !== null) {
|
|
3523
|
+
if (!promptbook.promptbookUrl) {
|
|
3524
|
+
if (isVerbose) {
|
|
3525
|
+
console.info("Not loading ".concat(fileName, " - missing URL"));
|
|
3526
|
+
}
|
|
3527
|
+
}
|
|
3528
|
+
else {
|
|
3529
|
+
if (isVerbose) {
|
|
3530
|
+
console.info("Loading ".concat(fileName));
|
|
3531
|
+
}
|
|
3532
|
+
if (!isCrashOnError) {
|
|
3533
|
+
// Note: Validate promptbook to check if it is logically correct to not crash on invalid promptbooks
|
|
3534
|
+
// But be handled in current try-catch block
|
|
3535
|
+
validatePromptbookJson(promptbook);
|
|
3536
|
+
}
|
|
3537
|
+
// Note: [🦄] Promptbook with same url uniqueness will be checked automatically in SimplePromptbookLibrary
|
|
3538
|
+
promptbooks.push(promptbook);
|
|
3539
|
+
}
|
|
3540
|
+
}
|
|
3541
|
+
return [3 /*break*/, 7];
|
|
3542
|
+
case 6:
|
|
3543
|
+
error_1 = _e.sent();
|
|
3544
|
+
if (!(error_1 instanceof Error)) {
|
|
3545
|
+
throw error_1;
|
|
3546
|
+
}
|
|
3547
|
+
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 "); });
|
|
3548
|
+
if (isCrashOnError) {
|
|
3549
|
+
throw new PromptbookLibraryError(wrappedErrorMessage);
|
|
3550
|
+
}
|
|
3551
|
+
console.error(wrappedErrorMessage);
|
|
3552
|
+
return [3 /*break*/, 7];
|
|
3553
|
+
case 7: return [2 /*return*/];
|
|
3554
|
+
}
|
|
3555
|
+
});
|
|
3556
|
+
};
|
|
3557
|
+
_b.label = 2;
|
|
3558
|
+
case 2:
|
|
3559
|
+
_b.trys.push([2, 7, 8, 9]);
|
|
3560
|
+
fileNames_1 = __values(fileNames), fileNames_1_1 = fileNames_1.next();
|
|
3561
|
+
_b.label = 3;
|
|
3562
|
+
case 3:
|
|
3563
|
+
if (!!fileNames_1_1.done) return [3 /*break*/, 6];
|
|
3564
|
+
fileName = fileNames_1_1.value;
|
|
3565
|
+
return [5 /*yield**/, _loop_1(fileName)];
|
|
3566
|
+
case 4:
|
|
3567
|
+
_b.sent();
|
|
3568
|
+
_b.label = 5;
|
|
3569
|
+
case 5:
|
|
3570
|
+
fileNames_1_1 = fileNames_1.next();
|
|
3571
|
+
return [3 /*break*/, 3];
|
|
3572
|
+
case 6: return [3 /*break*/, 9];
|
|
3573
|
+
case 7:
|
|
3574
|
+
e_1_1 = _b.sent();
|
|
3575
|
+
e_1 = { error: e_1_1 };
|
|
3576
|
+
return [3 /*break*/, 9];
|
|
3577
|
+
case 8:
|
|
3578
|
+
try {
|
|
3579
|
+
if (fileNames_1_1 && !fileNames_1_1.done && (_a = fileNames_1.return)) _a.call(fileNames_1);
|
|
3580
|
+
}
|
|
3581
|
+
finally { if (e_1) throw e_1.error; }
|
|
3582
|
+
return [7 /*endfinally*/];
|
|
3583
|
+
case 9: return [2 /*return*/, promptbooks];
|
|
3584
|
+
}
|
|
3585
|
+
});
|
|
3586
|
+
}); });
|
|
3587
|
+
if (!(isLazyLoaded === false)) return [3 /*break*/, 2];
|
|
3588
|
+
return [4 /*yield*/, library.listPromptbooks()];
|
|
3589
|
+
case 1:
|
|
3590
|
+
_f.sent();
|
|
3591
|
+
_f.label = 2;
|
|
3592
|
+
case 2: return [2 /*return*/, library];
|
|
3593
|
+
}
|
|
3594
|
+
});
|
|
3595
|
+
});
|
|
3596
|
+
}
|
|
3597
|
+
/**
|
|
3598
|
+
* Reads all files in the directory
|
|
3599
|
+
*
|
|
3600
|
+
* @param path
|
|
3601
|
+
* @param isRecursive
|
|
3602
|
+
* @returns List of all files in the directory
|
|
3603
|
+
* @private internal function for `createPromptbookLibraryFromDirectory`
|
|
3604
|
+
*/
|
|
3605
|
+
function listAllFiles(path, isRecursive) {
|
|
3606
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
3607
|
+
var fsPromises, readdir, dirents, fileNames, _a, _b, dirent, subPath, _c, _d, _e, _f, e_2_1;
|
|
3608
|
+
var e_2, _g;
|
|
3609
|
+
return __generator(this, function (_h) {
|
|
3610
|
+
switch (_h.label) {
|
|
3611
|
+
case 0:
|
|
3612
|
+
fsPromises = require(just('fs/promises') /* <- Note: [1] */);
|
|
3613
|
+
readdir = fsPromises.readdir;
|
|
3614
|
+
return [4 /*yield*/, readdir(path, {
|
|
3615
|
+
withFileTypes: true /* Note: This is not working: recursive: isRecursive */,
|
|
3616
|
+
})];
|
|
3421
3617
|
case 1:
|
|
3422
|
-
|
|
3423
|
-
return
|
|
3618
|
+
dirents = _h.sent();
|
|
3619
|
+
fileNames = dirents.filter(function (dirent) { return dirent.isFile(); }).map(function (_a) {
|
|
3620
|
+
var name = _a.name;
|
|
3621
|
+
return join(path, name);
|
|
3622
|
+
});
|
|
3623
|
+
if (!isRecursive) return [3 /*break*/, 9];
|
|
3624
|
+
_h.label = 2;
|
|
3424
3625
|
case 2:
|
|
3425
|
-
|
|
3426
|
-
|
|
3427
|
-
|
|
3428
|
-
|
|
3626
|
+
_h.trys.push([2, 7, 8, 9]);
|
|
3627
|
+
_a = __values(dirents.filter(function (dirent) { return dirent.isDirectory(); })), _b = _a.next();
|
|
3628
|
+
_h.label = 3;
|
|
3629
|
+
case 3:
|
|
3630
|
+
if (!!_b.done) return [3 /*break*/, 6];
|
|
3631
|
+
dirent = _b.value;
|
|
3632
|
+
subPath = join(path, dirent.name);
|
|
3633
|
+
_d = (_c = fileNames.push).apply;
|
|
3634
|
+
_e = [fileNames];
|
|
3635
|
+
_f = [[]];
|
|
3636
|
+
return [4 /*yield*/, listAllFiles(subPath, isRecursive)];
|
|
3637
|
+
case 4:
|
|
3638
|
+
_d.apply(_c, _e.concat([__spreadArray.apply(void 0, _f.concat([__read.apply(void 0, [(_h.sent())]), false]))]));
|
|
3639
|
+
_h.label = 5;
|
|
3640
|
+
case 5:
|
|
3641
|
+
_b = _a.next();
|
|
3642
|
+
return [3 /*break*/, 3];
|
|
3643
|
+
case 6: return [3 /*break*/, 9];
|
|
3644
|
+
case 7:
|
|
3645
|
+
e_2_1 = _h.sent();
|
|
3646
|
+
e_2 = { error: e_2_1 };
|
|
3647
|
+
return [3 /*break*/, 9];
|
|
3648
|
+
case 8:
|
|
3649
|
+
try {
|
|
3650
|
+
if (_b && !_b.done && (_g = _a.return)) _g.call(_a);
|
|
3651
|
+
}
|
|
3652
|
+
finally { if (e_2) throw e_2.error; }
|
|
3653
|
+
return [7 /*endfinally*/];
|
|
3654
|
+
case 9: return [2 /*return*/, fileNames];
|
|
3429
3655
|
}
|
|
3430
3656
|
});
|
|
3431
|
-
});
|
|
3657
|
+
});
|
|
3432
3658
|
}
|
|
3433
3659
|
/***
|
|
3660
|
+
* TODO: [🧠] Maybe do not do hacks like [1] and just create package @promptbook/node
|
|
3661
|
+
* [🍓][🚯] maybe make `@promptbook/library` package
|
|
3662
|
+
* TODO: Fix the dynamic import issue in Webpack (! Not working !)
|
|
3663
|
+
* > ./node_modules/@promptbook/core/esm/index.es.js
|
|
3664
|
+
* > Critical dependency: the request of a dependency is an expression
|
|
3665
|
+
*
|
|
3666
|
+
* Note: [1] Using require(just('fs/promises')) to allow
|
|
3667
|
+
* the `@promptbook/core` work for both Node.js and browser environments
|
|
3668
|
+
*/
|
|
3669
|
+
|
|
3670
|
+
/**
|
|
3671
|
+
* Constructs Promptbook from remote Promptbase URL
|
|
3672
|
+
|
|
3673
|
+
* @returns PromptbookLibrary
|
|
3674
|
+
*/
|
|
3675
|
+
function createPromptbookLibraryFromUrl(url, options) {
|
|
3676
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
3677
|
+
var _a, _b, isVerbose, _c, isLazyLoaded, library;
|
|
3678
|
+
var _this = this;
|
|
3679
|
+
return __generator(this, function (_d) {
|
|
3680
|
+
switch (_d.label) {
|
|
3681
|
+
case 0:
|
|
3682
|
+
_a = options || {}, _b = _a.isVerbose, isVerbose = _b === void 0 ? false : _b, _c = _a.isLazyLoaded, isLazyLoaded = _c === void 0 ? false : _c;
|
|
3683
|
+
library = createPromptbookLibraryFromPromise(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
3684
|
+
return __generator(this, function (_a) {
|
|
3685
|
+
if (isVerbose) {
|
|
3686
|
+
console.info("Creating promptbook library from url ".concat(url.toString()));
|
|
3687
|
+
}
|
|
3688
|
+
throw new Error('Not implemented yet');
|
|
3689
|
+
});
|
|
3690
|
+
}); });
|
|
3691
|
+
if (!(isLazyLoaded === false)) return [3 /*break*/, 2];
|
|
3692
|
+
return [4 /*yield*/, library.listPromptbooks()];
|
|
3693
|
+
case 1:
|
|
3694
|
+
_d.sent();
|
|
3695
|
+
_d.label = 2;
|
|
3696
|
+
case 2: return [2 /*return*/, library];
|
|
3697
|
+
}
|
|
3698
|
+
});
|
|
3699
|
+
});
|
|
3700
|
+
}
|
|
3701
|
+
/***
|
|
3702
|
+
* TODO: [⚖] Compatible with remote server
|
|
3434
3703
|
* TODO: [🍓][🚯] !!! Add to README and samples + maybe make `@promptbook/library` package
|
|
3435
3704
|
*/
|
|
3436
3705
|
|
|
@@ -3516,6 +3785,31 @@ function createPromptbookSublibrary(library, predicate) {
|
|
|
3516
3785
|
* TODO: [🍓][🚯] !!! Add to README and samples + maybe make `@promptbook/library` package
|
|
3517
3786
|
*/
|
|
3518
3787
|
|
|
3788
|
+
/**
|
|
3789
|
+
* Just testing imports and compatibility
|
|
3790
|
+
*/
|
|
3791
|
+
function justTestFsImport() {
|
|
3792
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
3793
|
+
var files;
|
|
3794
|
+
return __generator(this, function (_a) {
|
|
3795
|
+
switch (_a.label) {
|
|
3796
|
+
case 0:
|
|
3797
|
+
if (!isRunningInNode()) {
|
|
3798
|
+
throw new Error('Function `testFiles` can only be run in Node.js environment because it reads the file system.');
|
|
3799
|
+
}
|
|
3800
|
+
return [4 /*yield*/, require(just('glob-promise'))('/**/*')];
|
|
3801
|
+
case 1:
|
|
3802
|
+
files = _a.sent();
|
|
3803
|
+
console.info('testFiles', { files: files });
|
|
3804
|
+
return [2 /*return*/];
|
|
3805
|
+
}
|
|
3806
|
+
});
|
|
3807
|
+
});
|
|
3808
|
+
}
|
|
3809
|
+
/**
|
|
3810
|
+
* TODO: !!! Remove this file
|
|
3811
|
+
*/
|
|
3812
|
+
|
|
3519
3813
|
/**
|
|
3520
3814
|
* Format either small or big number
|
|
3521
3815
|
*
|
|
@@ -3536,23 +3830,6 @@ function formatNumber(value) {
|
|
|
3536
3830
|
return value.toString();
|
|
3537
3831
|
}
|
|
3538
3832
|
|
|
3539
|
-
/**
|
|
3540
|
-
* Returns the same value that is passed as argument.
|
|
3541
|
-
* No side effects.
|
|
3542
|
-
*
|
|
3543
|
-
* Note: It can be usefull for leveling indentation
|
|
3544
|
-
*
|
|
3545
|
-
* @param value any values
|
|
3546
|
-
* @returns the same values
|
|
3547
|
-
*/
|
|
3548
|
-
function just(value) {
|
|
3549
|
-
if (value === undefined) {
|
|
3550
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3551
|
-
return undefined;
|
|
3552
|
-
}
|
|
3553
|
-
return value;
|
|
3554
|
-
}
|
|
3555
|
-
|
|
3556
3833
|
/**
|
|
3557
3834
|
* Create a markdown table from a 2D array of strings
|
|
3558
3835
|
*
|
|
@@ -3833,5 +4110,5 @@ function executionReportJsonToString(executionReportJson, options) {
|
|
|
3833
4110
|
* TODO: [🧠] Allow to filter out some parts of the report by options
|
|
3834
4111
|
*/
|
|
3835
4112
|
|
|
3836
|
-
export { CallbackInterfaceTools, ExecutionReportStringOptionsDefaults, ExecutionTypes, MultipleLlmExecutionTools, PROMPTBOOK_VERSION, SimplePromptInterfaceTools, SimplePromptbookLibrary, assertsExecutionSuccessful, checkExpectations, createPromptbookExecutor, createPromptbookLibraryFromDirectory,
|
|
4113
|
+
export { CallbackInterfaceTools, ExecutionReportStringOptionsDefaults, ExecutionTypes, MultipleLlmExecutionTools, PROMPTBOOK_VERSION, SimplePromptInterfaceTools, SimplePromptbookLibrary, assertsExecutionSuccessful, checkExpectations, createPromptbookExecutor, createPromptbookLibraryFromDirectory, createPromptbookLibraryFromSources, createPromptbookLibraryFromUrl, createPromptbookSublibrary, executionReportJsonToString, isPassingExpectations, justTestFsImport, prettifyPromptbookString, promptbookJsonToString, promptbookStringToJson, validatePromptbookJson };
|
|
3837
4114
|
//# sourceMappingURL=index.es.js.map
|