@promptbook/cli 0.59.0-24 → 0.59.0-25

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/esm/index.es.js CHANGED
@@ -2,10 +2,11 @@ import commander from 'commander';
2
2
  import spaceTrim, { spaceTrim as spaceTrim$1 } from 'spacetrim';
3
3
  import colors from 'colors';
4
4
  import { forTime } from 'waitasecond';
5
- import { writeFile, readFile } from 'fs/promises';
6
- import glob from 'glob-promise';
5
+ import { access, constants, readFile, readdir, writeFile } from 'fs/promises';
6
+ import { join } from 'path';
7
7
  import { format } from 'prettier';
8
8
  import parserHtml from 'prettier/parser-html';
9
+ import glob from 'glob-promise';
9
10
 
10
11
  /*! *****************************************************************************
11
12
  Copyright (c) Microsoft Corporation.
@@ -142,7 +143,7 @@ new Function("\n try {\n if (typeof WorkerGlobalScope !== 'undefined'
142
143
  /**
143
144
  * The version of the Promptbook library
144
145
  */
145
- var PROMPTBOOK_VERSION = '0.59.0-23';
146
+ var PROMPTBOOK_VERSION = '0.59.0-24';
146
147
 
147
148
  /**
148
149
  * Initializes testing `hello` command for Promptbook CLI utilities
@@ -175,94 +176,13 @@ function initializeHello(program) {
175
176
  }
176
177
 
177
178
  /**
178
- * Initializes `make` command for Promptbook CLI utilities
179
- *
180
- * @private part of `promptbookCli`
181
- */
182
- function initializeMake(program) {
183
- var _this = this;
184
- var helloCommand = program.command('make <path>');
185
- helloCommand.description(spaceTrim("\n Makes a new promptbook library in given folder\n "));
186
- helloCommand.argument('<path>', 'Path to promptbook folder');
187
- helloCommand.option('-f, --format <format>', "Output format of builded library \"js\", \"ts\" or \"json\"", 'js');
188
- helloCommand.option('-v, --validate', "Validate logic of promptbook", true);
189
- // TODO: !!! Auto-detect AI api keys + explicit api keys as argv
190
- helloCommand.action(function (path, _a) {
191
- var format = _a.format, validate = _a.validate;
192
- return __awaiter(_this, void 0, void 0, function () {
193
- return __generator(this, function (_b) {
194
- console.info({ path: path, format: format, validate: validate });
195
- // TODO: !!! Implement
196
- // TODO: !!! Validate logic
197
- process.exit(0);
198
- return [2 /*return*/];
199
- });
200
- });
201
- });
202
- }
203
-
204
- /**
205
- * Removes HTML or Markdown comments from a string.
206
- *
207
- * @param {string} content - The string to remove comments from.
208
- * @returns {string} The input string with all comments removed.
209
- */
210
- function removeContentComments(content) {
211
- return spaceTrim$1(content.replace(/<!--(.*?)-->/gs, ''));
212
- }
213
-
214
- /**
215
- * Add or modify an auto-generated section in a markdown file
216
- *
217
- * @private within the library
179
+ * The maximum number of iterations for a loops
218
180
  */
219
- function addAutoGeneratedSection(content, options) {
220
- var sectionName = options.sectionName, sectionContent = options.sectionContent;
221
- var warningLine = "<!-- \u26A0\uFE0F WARNING: This section was auto-generated -->";
222
- var sectionRegex = new RegExp("<!--".concat(sectionName, "-->([\\s\\S]*?)<!--/").concat(sectionName, "-->"), 'g');
223
- var sectionMatch = content.match(sectionRegex);
224
- if (sectionMatch) {
225
- return content.replace(sectionRegex, spaceTrim$1(function (block) { return "\n <!--".concat(sectionName, "-->\n ").concat(block(warningLine), "\n ").concat(block(sectionContent), "\n <!--/").concat(sectionName, "-->\n "); }));
226
- }
227
- var placeForSection = removeContentComments(content).match(/^##.*$/im);
228
- if (!placeForSection) {
229
- throw new Error("No place where to put the section <!--".concat(sectionName, "-->"));
230
- }
231
- var _a = __read(placeForSection, 1), heading = _a[0];
232
- return content.replace(heading, "<!--".concat(sectionName, "-->\n").concat(warningLine, "\n").concat(sectionContent, "\n<!--/").concat(sectionName, "-->\n\n").concat(heading));
233
- }
234
-
181
+ var LOOP_LIMIT = 1000;
235
182
  /**
236
- * Prettify the html code
237
- *
238
- * @param content raw html code
239
- * @returns formatted html code
183
+ * The name of the builded promptbook library made by CLI `promptbook make` and for lookup in `createLibraryFromDirectory`
240
184
  */
241
- function prettifyMarkdown(content) {
242
- try {
243
- return format(content, {
244
- parser: 'markdown',
245
- plugins: [parserHtml],
246
- // TODO: DRY - make some import or auto-copy of .prettierrc
247
- endOfLine: 'lf',
248
- tabWidth: 4,
249
- singleQuote: true,
250
- trailingComma: 'all',
251
- arrowParens: 'always',
252
- printWidth: 120,
253
- htmlWhitespaceSensitivity: 'ignore',
254
- jsxBracketSameLine: false,
255
- bracketSpacing: true,
256
- });
257
- }
258
- catch (error) {
259
- console.error('There was an error with prettifying the markdown, using the original as the fallback', {
260
- error: error,
261
- html: content,
262
- });
263
- return content;
264
- }
265
- }
185
+ var PROMPTBOOK_MAKED_BASE_FILENAME = "promptbook-library";
266
186
 
267
187
  /**
268
188
  * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
@@ -278,7 +198,7 @@ var PromptbookSyntaxError = /** @class */ (function (_super) {
278
198
  return PromptbookSyntaxError;
279
199
  }(Error));
280
200
 
281
- var promptbookLibrary = [{title:"Prepare Knowledge from Markdown",promptbookUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.59.0-23",parameters:[{name:"content",description:"Markdown document content",isInput:true,isOutput:false},{name:"knowledge",description:"The knowledge JSON object",isInput:false,isOutput:true}],promptTemplates:[{name:"knowledge",title:"Knowledge",dependentParameterNames:["content"],executionType:"PROMPT_TEMPLATE",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, extract the important knowledge from the document.\n\n# Rules\n\n- Make pieces of information concise, clear, and easy to understand\n- One piece of information should be approximately 1 paragraph\n- Divide the paragraphs by markdown horizontal lines ---\n- Omit irrelevant information\n- Group redundant information\n- Write just extracted information, nothing else\n\n# The document\n\nTake information from this document:\n\n> {content}",resultingParameterName:"knowledge"}],knowledge:[]}];
201
+ var promptbookLibrary = [{title:"Prepare Knowledge from Markdown",promptbookUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.59.0-24",parameters:[{name:"content",description:"Markdown document content",isInput:true,isOutput:false},{name:"knowledge",description:"The knowledge JSON object",isInput:false,isOutput:true}],promptTemplates:[{name:"knowledge",title:"Knowledge",dependentParameterNames:["content"],executionType:"PROMPT_TEMPLATE",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, extract the important knowledge from the document.\n\n# Rules\n\n- Make pieces of information concise, clear, and easy to understand\n- One piece of information should be approximately 1 paragraph\n- Divide the paragraphs by markdown horizontal lines ---\n- Omit irrelevant information\n- Group redundant information\n- Write just extracted information, nothing else\n\n# The document\n\nTake information from this document:\n\n> {content}",resultingParameterName:"knowledge"}],knowledge:[]}];
282
202
 
283
203
  /**
284
204
  * This error indicates errors during the execution of the promptbook
@@ -319,11 +239,6 @@ function assertsExecutionSuccessful(executionResult) {
319
239
  * TODO: [🧠] Can this return type be better typed than void
320
240
  */
321
241
 
322
- /**
323
- * The maximum number of iterations for a loops
324
- */
325
- var LOOP_LIMIT = 1000;
326
-
327
242
  /**
328
243
  * This error indicates that the promptbook object has valid syntax but contains logical errors (like circular dependencies)
329
244
  */
@@ -1660,6 +1575,38 @@ function createPromptbookExecutor(options) {
1660
1575
  * TODO: [🧠][3] transparent = (report intermediate parameters) / opaque execution = (report only output parameters) progress reporting mode
1661
1576
  */
1662
1577
 
1578
+ /**
1579
+ * Prettify the html code
1580
+ *
1581
+ * @param content raw html code
1582
+ * @returns formatted html code
1583
+ */
1584
+ function prettifyMarkdown(content) {
1585
+ try {
1586
+ return format(content, {
1587
+ parser: 'markdown',
1588
+ plugins: [parserHtml],
1589
+ // TODO: DRY - make some import or auto-copy of .prettierrc
1590
+ endOfLine: 'lf',
1591
+ tabWidth: 4,
1592
+ singleQuote: true,
1593
+ trailingComma: 'all',
1594
+ arrowParens: 'always',
1595
+ printWidth: 120,
1596
+ htmlWhitespaceSensitivity: 'ignore',
1597
+ jsxBracketSameLine: false,
1598
+ bracketSpacing: true,
1599
+ });
1600
+ }
1601
+ catch (error) {
1602
+ console.error('There was an error with prettifying the markdown, using the original as the fallback', {
1603
+ error: error,
1604
+ html: content,
1605
+ });
1606
+ return content;
1607
+ }
1608
+ }
1609
+
1663
1610
  /**
1664
1611
  * Makes first letter of a string uppercase
1665
1612
  *
@@ -2354,6 +2301,16 @@ function extractOneBlockFromMarkdown(markdown) {
2354
2301
  * TODO: [🍓][🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
2355
2302
  */
2356
2303
 
2304
+ /**
2305
+ * Removes HTML or Markdown comments from a string.
2306
+ *
2307
+ * @param {string} content - The string to remove comments from.
2308
+ * @returns {string} The input string with all comments removed.
2309
+ */
2310
+ function removeContentComments(content) {
2311
+ return spaceTrim$1(content.replace(/<!--(.*?)-->/gs, ''));
2312
+ }
2313
+
2357
2314
  /**
2358
2315
  * Creates a new set with all elements that are present in either set
2359
2316
  */
@@ -3225,6 +3182,443 @@ function promptbookStringToJson(promptbookString, options) {
3225
3182
  * TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
3226
3183
  */
3227
3184
 
3185
+ /**
3186
+ * This error indicates that the promptbook library cannot be propperly loaded
3187
+ */
3188
+ var PromptbookLibraryError = /** @class */ (function (_super) {
3189
+ __extends(PromptbookLibraryError, _super);
3190
+ function PromptbookLibraryError(message) {
3191
+ var _this = _super.call(this, message) || this;
3192
+ _this.name = 'PromptbookLibraryError';
3193
+ Object.setPrototypeOf(_this, PromptbookLibraryError.prototype);
3194
+ return _this;
3195
+ }
3196
+ return PromptbookLibraryError;
3197
+ }(Error));
3198
+
3199
+ /**
3200
+ * Constructs Promptbook from async sources
3201
+ * It can be one of the following:
3202
+ * - Promise of array of PromptbookJson or PromptbookString
3203
+ * - Factory function that returns Promise of array of PromptbookJson or PromptbookString
3204
+ *
3205
+ * Note: This is useful as internal tool for other constructor functions like
3206
+ * `createLibraryFromUrl` or `createLibraryFromDirectory`
3207
+ * Consider using those functions instead of this one
3208
+ *
3209
+ * Note: The function does NOT return promise it returns the library directly which waits for the sources to be resolved
3210
+ * when error occurs in given promise or factory function, it is thrown during `listPromptbooks` or `getPromptbookByUrl` call
3211
+ *
3212
+ * Note: Consider using `createLibraryFromDirectory` or `createLibraryFromUrl`
3213
+ *
3214
+ * @param promptbookSourcesPromiseOrFactory
3215
+ * @returns PromptbookLibrary
3216
+ * @deprecated Do not use, it will became internal tool for other constructor functions
3217
+ */
3218
+ function createLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
3219
+ var library;
3220
+ function forSources() {
3221
+ return __awaiter(this, void 0, void 0, function () {
3222
+ var promptbookSources;
3223
+ return __generator(this, function (_a) {
3224
+ switch (_a.label) {
3225
+ case 0:
3226
+ if (typeof promptbookSourcesPromiseOrFactory === 'function') {
3227
+ // Note: Calling factory function only once despite multiple calls to resolveSources
3228
+ promptbookSourcesPromiseOrFactory = promptbookSourcesPromiseOrFactory();
3229
+ }
3230
+ return [4 /*yield*/, promptbookSourcesPromiseOrFactory];
3231
+ case 1:
3232
+ promptbookSources = _a.sent();
3233
+ return [4 /*yield*/, createLibraryFromJson.apply(void 0, __spreadArray([], __read(promptbookSources), false))];
3234
+ case 2:
3235
+ library = _a.sent();
3236
+ return [2 /*return*/];
3237
+ }
3238
+ });
3239
+ });
3240
+ }
3241
+ function listPromptbooks() {
3242
+ return __awaiter(this, void 0, void 0, function () {
3243
+ return __generator(this, function (_a) {
3244
+ switch (_a.label) {
3245
+ case 0: return [4 /*yield*/, forSources()];
3246
+ case 1:
3247
+ _a.sent();
3248
+ return [2 /*return*/, /* not await */ library.listPromptbooks()];
3249
+ }
3250
+ });
3251
+ });
3252
+ }
3253
+ function getPromptbookByUrl(url) {
3254
+ return __awaiter(this, void 0, void 0, function () {
3255
+ return __generator(this, function (_a) {
3256
+ switch (_a.label) {
3257
+ case 0: return [4 /*yield*/, forSources()];
3258
+ case 1:
3259
+ _a.sent();
3260
+ return [2 /*return*/, /* not await */ library.getPromptbookByUrl(url)];
3261
+ }
3262
+ });
3263
+ });
3264
+ }
3265
+ function isResponsibleForPrompt(prompt) {
3266
+ return __awaiter(this, void 0, void 0, function () {
3267
+ return __generator(this, function (_a) {
3268
+ switch (_a.label) {
3269
+ case 0: return [4 /*yield*/, forSources()];
3270
+ case 1:
3271
+ _a.sent();
3272
+ return [2 /*return*/, /* not await */ library.isResponsibleForPrompt(prompt)];
3273
+ }
3274
+ });
3275
+ });
3276
+ }
3277
+ return {
3278
+ listPromptbooks: listPromptbooks,
3279
+ getPromptbookByUrl: getPromptbookByUrl,
3280
+ isResponsibleForPrompt: isResponsibleForPrompt,
3281
+ };
3282
+ }
3283
+
3284
+ /**
3285
+ * Constructs Promptbook from given directory
3286
+ *
3287
+ * Note: Works only in Node.js environment because it reads the file system
3288
+ *
3289
+ * @param path - path to the directory with promptbooks
3290
+ * @param options - Misc options for the library
3291
+ * @returns PromptbookLibrary
3292
+ */
3293
+ function createLibraryFromDirectory(path, options) {
3294
+ return __awaiter(this, void 0, void 0, function () {
3295
+ var makedLibraryFilePath, makedLibraryFileExists, _a, _b, isRecursive, _c, isVerbose, _d, isLazyLoaded, _e, isCrashOnError, library;
3296
+ var _this = this;
3297
+ return __generator(this, function (_f) {
3298
+ switch (_f.label) {
3299
+ case 0:
3300
+ if (!isRunningInNode()) {
3301
+ throw new Error('Function `createLibraryFromDirectory` can only be run in Node.js environment because it reads the file system.');
3302
+ }
3303
+ makedLibraryFilePath = join(path, "".concat(PROMPTBOOK_MAKED_BASE_FILENAME, ".json"));
3304
+ return [4 /*yield*/, access(makedLibraryFilePath, constants.R_OK)
3305
+ .then(function () { return true; })
3306
+ .catch(function () { return false; })];
3307
+ case 1:
3308
+ makedLibraryFileExists = _f.sent();
3309
+ if (!makedLibraryFileExists) {
3310
+ console.info(colors.yellow("Tip: Prebuild your promptbook library (file with supposed prebuild ".concat(makedLibraryFilePath, " not found) with CLI util \"promptbook make\" to speed up the library creation.")));
3311
+ }
3312
+ else {
3313
+ colors.green("Using your prebuild promptbook library ".concat(makedLibraryFilePath));
3314
+ // TODO: !!!!! Implement;
3315
+ }
3316
+ _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;
3317
+ library = createLibraryFromPromise(function () { return __awaiter(_this, void 0, void 0, function () {
3318
+ var fileNames, promptbooks, _loop_1, fileNames_1, fileNames_1_1, fileName, e_1_1;
3319
+ var e_1, _a;
3320
+ return __generator(this, function (_b) {
3321
+ switch (_b.label) {
3322
+ case 0:
3323
+ if (isVerbose) {
3324
+ console.info("Creating promptbook library from path ".concat(path));
3325
+ }
3326
+ return [4 /*yield*/, listAllFiles(path, isRecursive)];
3327
+ case 1:
3328
+ fileNames = _b.sent();
3329
+ if (isVerbose) {
3330
+ console.info('createLibraryFromDirectory', { path: path, isRecursive: isRecursive, fileNames: fileNames });
3331
+ }
3332
+ promptbooks = [];
3333
+ _loop_1 = function (fileName) {
3334
+ var promptbook, promptbookString, _c, _d, error_1, wrappedErrorMessage;
3335
+ return __generator(this, function (_e) {
3336
+ switch (_e.label) {
3337
+ case 0:
3338
+ _e.trys.push([0, 7, , 8]);
3339
+ promptbook = null;
3340
+ if (!fileName.endsWith('.ptbk.md')) return [3 /*break*/, 3];
3341
+ return [4 /*yield*/, readFile(fileName, 'utf8')];
3342
+ case 1:
3343
+ promptbookString = (_e.sent());
3344
+ return [4 /*yield*/, promptbookStringToJson(promptbookString)];
3345
+ case 2:
3346
+ promptbook = _e.sent();
3347
+ return [3 /*break*/, 6];
3348
+ case 3:
3349
+ if (!fileName.endsWith('.ptbk.json')) return [3 /*break*/, 5];
3350
+ if (isVerbose) {
3351
+ console.info("Loading ".concat(fileName));
3352
+ }
3353
+ _d = (_c = JSON).parse;
3354
+ return [4 /*yield*/, readFile(fileName, 'utf8')];
3355
+ case 4:
3356
+ // TODO: Handle non-valid JSON files
3357
+ promptbook = _d.apply(_c, [_e.sent()]);
3358
+ return [3 /*break*/, 6];
3359
+ case 5:
3360
+ if (isVerbose) {
3361
+ console.info("Skipping file ".concat(fileName));
3362
+ }
3363
+ _e.label = 6;
3364
+ case 6:
3365
+ // ---
3366
+ if (promptbook !== null) {
3367
+ if (!promptbook.promptbookUrl) {
3368
+ if (isVerbose) {
3369
+ console.info("Not loading ".concat(fileName, " - missing URL"));
3370
+ }
3371
+ }
3372
+ else {
3373
+ if (isVerbose) {
3374
+ console.info("Loading ".concat(fileName));
3375
+ }
3376
+ if (!isCrashOnError) {
3377
+ // Note: Validate promptbook to check if it is logically correct to not crash on invalid promptbooks
3378
+ // But be handled in current try-catch block
3379
+ validatePromptbookJson(promptbook);
3380
+ }
3381
+ // Note: [🦄] Promptbook with same url uniqueness will be checked automatically in SimplePromptbookLibrary
3382
+ promptbooks.push(promptbook);
3383
+ }
3384
+ }
3385
+ return [3 /*break*/, 8];
3386
+ case 7:
3387
+ error_1 = _e.sent();
3388
+ if (!(error_1 instanceof Error)) {
3389
+ throw error_1;
3390
+ }
3391
+ wrappedErrorMessage = spaceTrim(function (block) { return "\n Error during loading promptbook from file ".concat(fileName.split('\\').join('/'), ":\n\n ").concat(block(error_1.message), "\n\n "); });
3392
+ if (isCrashOnError) {
3393
+ throw new PromptbookLibraryError(wrappedErrorMessage);
3394
+ }
3395
+ console.error(wrappedErrorMessage);
3396
+ return [3 /*break*/, 8];
3397
+ case 8: return [2 /*return*/];
3398
+ }
3399
+ });
3400
+ };
3401
+ _b.label = 2;
3402
+ case 2:
3403
+ _b.trys.push([2, 7, 8, 9]);
3404
+ fileNames_1 = __values(fileNames), fileNames_1_1 = fileNames_1.next();
3405
+ _b.label = 3;
3406
+ case 3:
3407
+ if (!!fileNames_1_1.done) return [3 /*break*/, 6];
3408
+ fileName = fileNames_1_1.value;
3409
+ return [5 /*yield**/, _loop_1(fileName)];
3410
+ case 4:
3411
+ _b.sent();
3412
+ _b.label = 5;
3413
+ case 5:
3414
+ fileNames_1_1 = fileNames_1.next();
3415
+ return [3 /*break*/, 3];
3416
+ case 6: return [3 /*break*/, 9];
3417
+ case 7:
3418
+ e_1_1 = _b.sent();
3419
+ e_1 = { error: e_1_1 };
3420
+ return [3 /*break*/, 9];
3421
+ case 8:
3422
+ try {
3423
+ if (fileNames_1_1 && !fileNames_1_1.done && (_a = fileNames_1.return)) _a.call(fileNames_1);
3424
+ }
3425
+ finally { if (e_1) throw e_1.error; }
3426
+ return [7 /*endfinally*/];
3427
+ case 9: return [2 /*return*/, promptbooks];
3428
+ }
3429
+ });
3430
+ }); });
3431
+ if (!(isLazyLoaded === false)) return [3 /*break*/, 3];
3432
+ return [4 /*yield*/, library.listPromptbooks()];
3433
+ case 2:
3434
+ _f.sent();
3435
+ _f.label = 3;
3436
+ case 3: return [2 /*return*/, library];
3437
+ }
3438
+ });
3439
+ });
3440
+ }
3441
+ /**
3442
+ * Reads all files in the directory
3443
+ *
3444
+ * @param path
3445
+ * @param isRecursive
3446
+ * @returns List of all files in the directory
3447
+ * @private internal function for `createLibraryFromDirectory`
3448
+ */
3449
+ function listAllFiles(path, isRecursive) {
3450
+ return __awaiter(this, void 0, void 0, function () {
3451
+ var dirents, fileNames, _a, _b, dirent, subPath, _c, _d, _e, _f, e_2_1;
3452
+ var e_2, _g;
3453
+ return __generator(this, function (_h) {
3454
+ switch (_h.label) {
3455
+ case 0: return [4 /*yield*/, readdir(path, {
3456
+ withFileTypes: true /* Note: This is not working: recursive: isRecursive */,
3457
+ })];
3458
+ case 1:
3459
+ dirents = _h.sent();
3460
+ fileNames = dirents.filter(function (dirent) { return dirent.isFile(); }).map(function (_a) {
3461
+ var name = _a.name;
3462
+ return join(path, name);
3463
+ });
3464
+ if (!isRecursive) return [3 /*break*/, 9];
3465
+ _h.label = 2;
3466
+ case 2:
3467
+ _h.trys.push([2, 7, 8, 9]);
3468
+ _a = __values(dirents.filter(function (dirent) { return dirent.isDirectory(); })), _b = _a.next();
3469
+ _h.label = 3;
3470
+ case 3:
3471
+ if (!!_b.done) return [3 /*break*/, 6];
3472
+ dirent = _b.value;
3473
+ subPath = join(path, dirent.name);
3474
+ _d = (_c = fileNames.push).apply;
3475
+ _e = [fileNames];
3476
+ _f = [[]];
3477
+ return [4 /*yield*/, listAllFiles(subPath, isRecursive)];
3478
+ case 4:
3479
+ _d.apply(_c, _e.concat([__spreadArray.apply(void 0, _f.concat([__read.apply(void 0, [(_h.sent())]), false]))]));
3480
+ _h.label = 5;
3481
+ case 5:
3482
+ _b = _a.next();
3483
+ return [3 /*break*/, 3];
3484
+ case 6: return [3 /*break*/, 9];
3485
+ case 7:
3486
+ e_2_1 = _h.sent();
3487
+ e_2 = { error: e_2_1 };
3488
+ return [3 /*break*/, 9];
3489
+ case 8:
3490
+ try {
3491
+ if (_b && !_b.done && (_g = _a.return)) _g.call(_a);
3492
+ }
3493
+ finally { if (e_2) throw e_2.error; }
3494
+ return [7 /*endfinally*/];
3495
+ case 9: return [2 /*return*/, fileNames];
3496
+ }
3497
+ });
3498
+ });
3499
+ }
3500
+ /**
3501
+ * TODO: !!!! [🧠] Library precompilation and do not mix markdown and json promptbooks
3502
+ */
3503
+
3504
+ /**
3505
+ * Converts PromptbookLibrary to serialized JSON
3506
+ *
3507
+ * Note: Functions `libraryToJson` and `createLibraryFromJson` are complementary
3508
+ */
3509
+ function libraryToJson(library) {
3510
+ return __awaiter(this, void 0, void 0, function () {
3511
+ var promptbookUrls, promptbooks;
3512
+ return __generator(this, function (_a) {
3513
+ switch (_a.label) {
3514
+ case 0: return [4 /*yield*/, library.listPromptbooks()];
3515
+ case 1:
3516
+ promptbookUrls = _a.sent();
3517
+ return [4 /*yield*/, Promise.all(promptbookUrls.map(function (url) { return library.getPromptbookByUrl(url); }))];
3518
+ case 2:
3519
+ promptbooks = _a.sent();
3520
+ return [2 /*return*/, promptbooks];
3521
+ }
3522
+ });
3523
+ });
3524
+ }
3525
+
3526
+ /**
3527
+ * Initializes `make` command for Promptbook CLI utilities
3528
+ *
3529
+ * @private part of `promptbookCli`
3530
+ */
3531
+ function initializeMake(program) {
3532
+ var _this = this;
3533
+ var helloCommand = program.command('make');
3534
+ helloCommand.description(spaceTrim("\n Makes a new promptbook library in given folder\n "));
3535
+ helloCommand.argument('<path>', 'Path to promptbook directory');
3536
+ helloCommand.option('--project-name', "Name of the project for whom library is", 'Project');
3537
+ helloCommand.option('-f, --format <format>', spaceTrim("\n Output format of builded library \"javascript\", \"typescript\" or \"json\"\n\n Note: You can use multiple formats separated by comma\n "), 'javascript' /* <- Note: [🏳‍🌈] */);
3538
+ helloCommand.option('--no-validation', "Do not validate logic of promptbooks in library", true);
3539
+ helloCommand.option('--validation', "Types of validations separated by comma (options \"logic\",\"imports\")", 'logic,imports');
3540
+ // TODO: !!! Auto-detect AI api keys + explicit api keys as argv
3541
+ helloCommand.action(function (path, _a) {
3542
+ var projectName = _a.projectName, format = _a.format, validation = _a.validation;
3543
+ return __awaiter(_this, void 0, void 0, function () {
3544
+ var library, libraryJson, libraryJsonString, saveFile;
3545
+ var _this = this;
3546
+ return __generator(this, function (_b) {
3547
+ switch (_b.label) {
3548
+ case 0:
3549
+ console.info('!!!', { projectName: projectName, path: path, format: format, validation: validation });
3550
+ return [4 /*yield*/, createLibraryFromDirectory(path, {
3551
+ isVerbose: true,
3552
+ isRecursive: true,
3553
+ })];
3554
+ case 1:
3555
+ library = _b.sent();
3556
+ return [4 /*yield*/, libraryToJson(library)];
3557
+ case 2:
3558
+ libraryJson = _b.sent();
3559
+ libraryJsonString = JSON.stringify(libraryJson);
3560
+ saveFile = function (extension, content) { return __awaiter(_this, void 0, void 0, function () {
3561
+ var filePath;
3562
+ return __generator(this, function (_a) {
3563
+ switch (_a.label) {
3564
+ case 0:
3565
+ filePath = join(path, "promptbook-library.".concat(extension));
3566
+ return [4 /*yield*/, writeFile(filePath, content, 'utf-8')];
3567
+ case 1:
3568
+ _a.sent();
3569
+ console.info(colors.green("Maked ".concat(filePath)));
3570
+ return [2 /*return*/];
3571
+ }
3572
+ });
3573
+ }); };
3574
+ if (!format.includes('json')) return [3 /*break*/, 4];
3575
+ return [4 /*yield*/, saveFile('json', libraryJsonString + '\n')];
3576
+ case 3:
3577
+ _b.sent();
3578
+ _b.label = 4;
3579
+ case 4:
3580
+ if (format.includes('javascript')) {
3581
+ // TODO: !!!!;
3582
+ // TODO: !!! DRY javascript and typescript
3583
+ format.push('javascript');
3584
+ }
3585
+ if (!format.includes('typescript')) return [3 /*break*/, 6];
3586
+ // TODO: !!!!!!!! Javascript json
3587
+ return [4 /*yield*/, saveFile('ts', spaceTrim("\n import type { PromptbookLibrary, SimplePromptbookLibrary } from '@promptbook/types';\n import type { PromptbookLibrary } from '@promptbook/core';\n\n /**\n * Promptbook library for ".concat(projectName, "\n *\n * @private internal cache for `getPromptbookLibrary`\n */\n let promptbookLibrary: null | SimplePromptbookLibrary = null;\n\n\n /**\n * Get promptbook library for ").concat(projectName, "\n *\n * @returns {PromptbookLibrary} Library of promptbooks for ").concat(projectName, "\n * @generated by `@promptbook/cli`\n */\n export function getPromptbookLibrary(): PromptbookLibrary{\n if(promptbookLibrary===null){\n promptbookLibrary = createLibraryFromJson(...").concat(libraryJsonString, ");\n }\n\n return promptbookLibrary;\n }\n ") + '\n'))];
3588
+ case 5:
3589
+ // TODO: !!!!!!!! Javascript json
3590
+ _b.sent();
3591
+ _b.label = 6;
3592
+ case 6:
3593
+ process.exit(0);
3594
+ return [2 /*return*/];
3595
+ }
3596
+ });
3597
+ });
3598
+ });
3599
+ }
3600
+
3601
+ /**
3602
+ * Add or modify an auto-generated section in a markdown file
3603
+ *
3604
+ * @private within the library
3605
+ */
3606
+ function addAutoGeneratedSection(content, options) {
3607
+ var sectionName = options.sectionName, sectionContent = options.sectionContent;
3608
+ var warningLine = "<!-- \u26A0\uFE0F WARNING: This section was auto-generated -->";
3609
+ var sectionRegex = new RegExp("<!--".concat(sectionName, "-->([\\s\\S]*?)<!--/").concat(sectionName, "-->"), 'g');
3610
+ var sectionMatch = content.match(sectionRegex);
3611
+ if (sectionMatch) {
3612
+ return content.replace(sectionRegex, spaceTrim$1(function (block) { return "\n <!--".concat(sectionName, "-->\n ").concat(block(warningLine), "\n ").concat(block(sectionContent), "\n <!--/").concat(sectionName, "-->\n "); }));
3613
+ }
3614
+ var placeForSection = removeContentComments(content).match(/^##.*$/im);
3615
+ if (!placeForSection) {
3616
+ throw new Error("No place where to put the section <!--".concat(sectionName, "-->"));
3617
+ }
3618
+ var _a = __read(placeForSection, 1), heading = _a[0];
3619
+ return content.replace(heading, "<!--".concat(sectionName, "-->\n").concat(warningLine, "\n").concat(sectionContent, "\n<!--/").concat(sectionName, "-->\n\n").concat(heading));
3620
+ }
3621
+
3228
3622
  /* tslint:disable */
3229
3623
  function normalizeTo_camelCase(sentence, __firstLetterCapital) {
3230
3624
  var e_1, _a;