@promptbook/cli 0.59.0-21 → 0.59.0-24

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 (48) hide show
  1. package/bin/promptbook-cli.js +2 -4
  2. package/esm/index.es.js +214 -211
  3. package/esm/index.es.js.map +1 -1
  4. package/esm/typings/src/_packages/cli.index.d.ts +4 -4
  5. package/esm/typings/src/_packages/core.index.d.ts +6 -6
  6. package/esm/typings/src/_packages/node.index.d.ts +2 -2
  7. package/esm/typings/src/_packages/types.index.d.ts +2 -1
  8. package/esm/typings/src/cli/actions/hello.d.ts +7 -0
  9. package/esm/typings/src/cli/actions/make.d.ts +7 -0
  10. package/esm/typings/src/cli/actions/prettify.d.ts +7 -0
  11. package/esm/typings/src/{conversion/prettify/prettifyPromptbookStringCli.d.ts → cli/promptbookCli.d.ts} +2 -2
  12. package/esm/typings/src/library/SimplePromptbookLibrary.d.ts +3 -1
  13. package/{umd/typings/src/library/constructors/createPromptbookLibraryFromDirectory.d.ts → esm/typings/src/library/constructors/createLibraryFromDirectory.d.ts} +2 -2
  14. package/esm/typings/src/library/constructors/createLibraryFromJson.d.ts +12 -0
  15. package/{umd/typings/src/library/constructors/createPromptbookLibraryFromPromise.d.ts → esm/typings/src/library/constructors/createLibraryFromPromise.d.ts} +3 -7
  16. package/{umd/typings/src/library/constructors/createPromptbookLibraryFromUrl.d.ts → esm/typings/src/library/constructors/createLibraryFromUrl.d.ts} +2 -2
  17. package/esm/typings/src/library/constructors/{createPromptbookSublibrary.d.ts → createSublibrary.d.ts} +1 -1
  18. package/esm/typings/src/library/libraryToJson.d.ts +8 -0
  19. package/esm/typings/src/llm-providers/remote/startRemoteServer.d.ts +1 -1
  20. package/package.json +2 -2
  21. package/umd/index.umd.js +170 -167
  22. package/umd/index.umd.js.map +1 -1
  23. package/umd/typings/src/_packages/cli.index.d.ts +4 -4
  24. package/umd/typings/src/_packages/core.index.d.ts +6 -6
  25. package/umd/typings/src/_packages/node.index.d.ts +2 -2
  26. package/umd/typings/src/_packages/types.index.d.ts +2 -1
  27. package/umd/typings/src/cli/actions/hello.d.ts +7 -0
  28. package/umd/typings/src/cli/actions/make.d.ts +7 -0
  29. package/umd/typings/src/cli/actions/prettify.d.ts +7 -0
  30. package/umd/typings/src/{conversion/prettify/prettifyPromptbookStringCli.d.ts → cli/promptbookCli.d.ts} +2 -2
  31. package/umd/typings/src/library/SimplePromptbookLibrary.d.ts +3 -1
  32. package/{esm/typings/src/library/constructors/createPromptbookLibraryFromDirectory.d.ts → umd/typings/src/library/constructors/createLibraryFromDirectory.d.ts} +2 -2
  33. package/umd/typings/src/library/constructors/createLibraryFromJson.d.ts +12 -0
  34. package/{esm/typings/src/library/constructors/createPromptbookLibraryFromPromise.d.ts → umd/typings/src/library/constructors/createLibraryFromPromise.d.ts} +3 -7
  35. package/umd/typings/src/library/constructors/createLibraryFromPromise.test.d.ts +1 -0
  36. package/{esm/typings/src/library/constructors/createPromptbookLibraryFromUrl.d.ts → umd/typings/src/library/constructors/createLibraryFromUrl.d.ts} +2 -2
  37. package/umd/typings/src/library/constructors/{createPromptbookSublibrary.d.ts → createSublibrary.d.ts} +1 -1
  38. package/umd/typings/src/library/libraryToJson.d.ts +8 -0
  39. package/umd/typings/src/library/libraryToJson.test.d.ts +1 -0
  40. package/umd/typings/src/llm-providers/remote/startRemoteServer.d.ts +1 -1
  41. package/esm/typings/src/library/constructors/createPromptbookLibraryFromSources.d.ts +0 -16
  42. package/umd/typings/src/library/constructors/createPromptbookLibraryFromSources.d.ts +0 -16
  43. /package/esm/typings/src/library/constructors/{createPromptbookLibraryFromDirectory.test.d.ts → createLibraryFromDirectory.test.d.ts} +0 -0
  44. /package/esm/typings/src/library/constructors/{createPromptbookLibraryFromPromise.test.d.ts → createLibraryFromJson.test.d.ts} +0 -0
  45. /package/esm/typings/src/library/constructors/{createPromptbookLibraryFromSources.test.d.ts → createLibraryFromPromise.test.d.ts} +0 -0
  46. /package/{umd/typings/src/library/constructors/createPromptbookLibraryFromDirectory.test.d.ts → esm/typings/src/library/libraryToJson.test.d.ts} +0 -0
  47. /package/umd/typings/src/library/constructors/{createPromptbookLibraryFromPromise.test.d.ts → createLibraryFromDirectory.test.d.ts} +0 -0
  48. /package/umd/typings/src/library/constructors/{createPromptbookLibraryFromSources.test.d.ts → createLibraryFromJson.test.d.ts} +0 -0
package/esm/index.es.js CHANGED
@@ -1,9 +1,9 @@
1
- import colors from 'colors';
2
1
  import commander from 'commander';
2
+ import spaceTrim, { spaceTrim as spaceTrim$1 } from 'spacetrim';
3
+ import colors from 'colors';
4
+ import { forTime } from 'waitasecond';
3
5
  import { writeFile, readFile } from 'fs/promises';
4
6
  import glob from 'glob-promise';
5
- import spaceTrim$1, { spaceTrim } from 'spacetrim';
6
- import { forTime } from 'waitasecond';
7
7
  import { format } from 'prettier';
8
8
  import parserHtml from 'prettier/parser-html';
9
9
 
@@ -142,7 +142,64 @@ new Function("\n try {\n if (typeof WorkerGlobalScope !== 'undefined'
142
142
  /**
143
143
  * The version of the Promptbook library
144
144
  */
145
- var PROMPTBOOK_VERSION = '0.59.0-20';
145
+ var PROMPTBOOK_VERSION = '0.59.0-23';
146
+
147
+ /**
148
+ * Initializes testing `hello` command for Promptbook CLI utilities
149
+ *
150
+ * @private part of `promptbookCli`
151
+ */
152
+ function initializeHello(program) {
153
+ var _this = this;
154
+ var helloCommand = program.command('hello');
155
+ helloCommand.description(spaceTrim("\n Just command for testing\n "));
156
+ helloCommand.argument('<name>', 'Your name');
157
+ helloCommand.option('-g, --greeting <greeting>', "Greeting", 'Hello');
158
+ helloCommand.action(function (name, _a) {
159
+ var greeting = _a.greeting;
160
+ return __awaiter(_this, void 0, void 0, function () {
161
+ return __generator(this, function (_b) {
162
+ switch (_b.label) {
163
+ case 0:
164
+ console.info(colors.cyan("".concat(greeting, " ").concat(name)));
165
+ return [4 /*yield*/, forTime(1000)];
166
+ case 1:
167
+ _b.sent();
168
+ console.info(colors.rainbow("Nice to meet you!"));
169
+ process.exit(0);
170
+ return [2 /*return*/];
171
+ }
172
+ });
173
+ });
174
+ });
175
+ }
176
+
177
+ /**
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
+ }
146
203
 
147
204
  /**
148
205
  * Removes HTML or Markdown comments from a string.
@@ -151,7 +208,7 @@ var PROMPTBOOK_VERSION = '0.59.0-20';
151
208
  * @returns {string} The input string with all comments removed.
152
209
  */
153
210
  function removeContentComments(content) {
154
- return spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
211
+ return spaceTrim$1(content.replace(/<!--(.*?)-->/gs, ''));
155
212
  }
156
213
 
157
214
  /**
@@ -165,7 +222,7 @@ function addAutoGeneratedSection(content, options) {
165
222
  var sectionRegex = new RegExp("<!--".concat(sectionName, "-->([\\s\\S]*?)<!--/").concat(sectionName, "-->"), 'g');
166
223
  var sectionMatch = content.match(sectionRegex);
167
224
  if (sectionMatch) {
168
- return content.replace(sectionRegex, spaceTrim(function (block) { return "\n <!--".concat(sectionName, "-->\n ").concat(block(warningLine), "\n ").concat(block(sectionContent), "\n <!--/").concat(sectionName, "-->\n "); }));
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 "); }));
169
226
  }
170
227
  var placeForSection = removeContentComments(content).match(/^##.*$/im);
171
228
  if (!placeForSection) {
@@ -221,7 +278,7 @@ var PromptbookSyntaxError = /** @class */ (function (_super) {
221
278
  return PromptbookSyntaxError;
222
279
  }(Error));
223
280
 
224
- var promptbookLibrary = [{title:"Prepare Knowledge from Markdown",promptbookUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.59.0-20",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:[]}];
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:[]}];
225
282
 
226
283
  /**
227
284
  * This error indicates errors during the execution of the promptbook
@@ -255,7 +312,7 @@ function assertsExecutionSuccessful(executionResult) {
255
312
  throw errors[0];
256
313
  }
257
314
  else {
258
- throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n Multiple errors occurred during promptnook execution\n\n ".concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n')), "\n "); }));
315
+ throw new PromptbookExecutionError(spaceTrim$1(function (block) { return "\n Multiple errors occurred during promptnook execution\n\n ".concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n')), "\n "); }));
259
316
  }
260
317
  }
261
318
  /**
@@ -287,7 +344,7 @@ var PromptbookLogicError = /** @class */ (function (_super) {
287
344
  var UnexpectedError = /** @class */ (function (_super) {
288
345
  __extends(UnexpectedError, _super);
289
346
  function UnexpectedError(message) {
290
- var _this = _super.call(this, spaceTrim(function (block) { return "\n ".concat(block(message), "\n\n Note: This error should not happen.\n It's probbably a bug in the promptbook library\n\n Please report issue:\n https://github.com/webgptorg/promptbook/issues\n\n Or contact us on me@pavolhejny.com\n\n "); })) || this;
347
+ var _this = _super.call(this, spaceTrim$1(function (block) { return "\n ".concat(block(message), "\n\n Note: This error should not happen.\n It's probbably a bug in the promptbook library\n\n Please report issue:\n https://github.com/webgptorg/promptbook/issues\n\n Or contact us on me@pavolhejny.com\n\n "); })) || this;
291
348
  _this.name = 'UnexpectedError';
292
349
  Object.setPrototypeOf(_this, UnexpectedError.prototype);
293
350
  return _this;
@@ -346,12 +403,12 @@ function validatePromptbookJson(promptbook) {
346
403
  // TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
347
404
  if (!Array.isArray(promptbook.parameters)) {
348
405
  // TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
349
- 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 ")));
406
+ throw new PromptbookSyntaxError(spaceTrim$1("\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 ")));
350
407
  }
351
408
  // TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
352
409
  if (!Array.isArray(promptbook.promptTemplates)) {
353
410
  // TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
354
- 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 ")));
411
+ throw new PromptbookSyntaxError(spaceTrim$1("\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 ")));
355
412
  }
356
413
  var _loop_1 = function (parameter) {
357
414
  if (parameter.isInput && parameter.isOutput) {
@@ -361,12 +418,12 @@ function validatePromptbookJson(promptbook) {
361
418
  if (!parameter.isInput &&
362
419
  !parameter.isOutput &&
363
420
  !promptbook.promptTemplates.some(function (template) { return template.dependentParameterNames.includes(parameter.name); })) {
364
- throw new PromptbookLogicError(spaceTrim("\n Parameter {".concat(parameter.name, "} is created but not used\n\n You can declare {").concat(parameter.name, "} as output parameter by adding in the header:\n - OUTPUT PARAMETER `{").concat(parameter.name, "}` ").concat(parameter.description || '', "\n\n ")));
421
+ throw new PromptbookLogicError(spaceTrim$1("\n Parameter {".concat(parameter.name, "} is created but not used\n\n You can declare {").concat(parameter.name, "} as output parameter by adding in the header:\n - OUTPUT PARAMETER `{").concat(parameter.name, "}` ").concat(parameter.description || '', "\n\n ")));
365
422
  }
366
423
  // Note: Testing that parameter is either input or result of some template
367
424
  if (!parameter.isInput &&
368
425
  !promptbook.promptTemplates.some(function (template) { return template.resultingParameterName === parameter.name; })) {
369
- throw new PromptbookLogicError(spaceTrim("\n Parameter {".concat(parameter.name, "} is declared but not defined\n\n You can do one of these:\n - Remove declaration of {").concat(parameter.name, "}\n - Add prompt template that results in -> {").concat(parameter.name, "}\n\n ")));
426
+ throw new PromptbookLogicError(spaceTrim$1("\n Parameter {".concat(parameter.name, "} is declared but not defined\n\n You can do one of these:\n - Remove declaration of {").concat(parameter.name, "}\n - Add prompt template that results in -> {").concat(parameter.name, "}\n\n ")));
370
427
  }
371
428
  };
372
429
  try {
@@ -400,7 +457,7 @@ function validatePromptbookJson(promptbook) {
400
457
  definedParameters.add(template.resultingParameterName);
401
458
  if (template.executionType === 'PROMPT_TEMPLATE' &&
402
459
  (template.modelRequirements.modelVariant === undefined)) {
403
- 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 ")));
460
+ throw new PromptbookLogicError(spaceTrim$1("\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 ")));
404
461
  }
405
462
  if (template.jokers && template.jokers.length > 0) {
406
463
  if (!template.expectFormat &&
@@ -475,7 +532,7 @@ function validatePromptbookJson(promptbook) {
475
532
  return template.dependentParameterNames.every(function (name) { return resovedParameters.includes(name); });
476
533
  });
477
534
  if (currentlyResovedTemplates.length === 0) {
478
- throw new PromptbookLogicError(spaceTrim(function (block) { return "\n\n Can not resolve some parameters\n It may be circular dependencies\n\n Can not resolve:\n ".concat(block(unresovedTemplates
535
+ throw new PromptbookLogicError(spaceTrim$1(function (block) { return "\n\n Can not resolve some parameters\n It may be circular dependencies\n\n Can not resolve:\n ".concat(block(unresovedTemplates
479
536
  .map(function (_a) {
480
537
  var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
481
538
  return "- {".concat(resultingParameterName, "} depends on ").concat(dependentParameterNames
@@ -1215,7 +1272,7 @@ function createPromptbookExecutor(options) {
1215
1272
  throw errors[0];
1216
1273
  }
1217
1274
  else {
1218
- throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n Postprocessing in LlmExecutionTools failed ".concat(errors.length, "x\n\n ").concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n\n')), "\n "); }));
1275
+ throw new PromptbookExecutionError(spaceTrim$1(function (block) { return "\n Postprocessing in LlmExecutionTools failed ".concat(errors.length, "x\n\n ").concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n\n')), "\n "); }));
1219
1276
  }
1220
1277
  }
1221
1278
  });
@@ -1299,7 +1356,7 @@ function createPromptbookExecutor(options) {
1299
1356
  throw scriptExecutionErrors[0];
1300
1357
  }
1301
1358
  else {
1302
- throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n Script execution failed ".concat(scriptExecutionErrors.length, " times\n\n ").concat(block(scriptExecutionErrors
1359
+ throw new PromptbookExecutionError(spaceTrim$1(function (block) { return "\n Script execution failed ".concat(scriptExecutionErrors.length, " times\n\n ").concat(block(scriptExecutionErrors
1303
1360
  .map(function (error) { return '- ' + error.message; })
1304
1361
  .join('\n\n')), "\n "); }));
1305
1362
  }
@@ -1444,7 +1501,7 @@ function createPromptbookExecutor(options) {
1444
1501
  return [7 /*endfinally*/];
1445
1502
  case 47:
1446
1503
  if (expectError !== null && attempt === maxAttempts - 1) {
1447
- throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n LLM execution failed ".concat(maxExecutionAttempts, "x\n\n ---\n Last error ").concat((expectError === null || expectError === void 0 ? void 0 : expectError.name) || '', ":\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n\n Last result:\n ").concat(resultString, "\n ---\n "); }));
1504
+ throw new PromptbookExecutionError(spaceTrim$1(function (block) { return "\n LLM execution failed ".concat(maxExecutionAttempts, "x\n\n ---\n Last error ").concat((expectError === null || expectError === void 0 ? void 0 : expectError.name) || '', ":\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n\n Last result:\n ").concat(resultString, "\n ---\n "); }));
1448
1505
  }
1449
1506
  _o.label = 48;
1450
1507
  case 48:
@@ -1513,7 +1570,7 @@ function createPromptbookExecutor(options) {
1513
1570
  return template.dependentParameterNames.every(function (name) { return resovedParameters_1.includes(name); });
1514
1571
  });
1515
1572
  if (!(!currentTemplate && resolving_1.length === 0)) return [3 /*break*/, 1];
1516
- throw new UnexpectedError(spaceTrim("\n Can not resolve some parameters\n\n Note: This should be catched during validatePromptbookJson\n "));
1573
+ throw new UnexpectedError(spaceTrim$1("\n Can not resolve some parameters\n\n Note: This should be catched during validatePromptbookJson\n "));
1517
1574
  case 1:
1518
1575
  if (!!currentTemplate) return [3 /*break*/, 3];
1519
1576
  /* [5] */ return [4 /*yield*/, Promise.race(resolving_1)];
@@ -1777,7 +1834,7 @@ function promptbookJsonToString(promptbookJson) {
1777
1834
  promptbookString += '\n\n';
1778
1835
  promptbookString += '```' + contentLanguage;
1779
1836
  promptbookString += '\n';
1780
- promptbookString += spaceTrim$1(content);
1837
+ promptbookString += spaceTrim(content);
1781
1838
  // <- TODO: !!! Escape
1782
1839
  // <- TODO: [🧠] Some clear strategy how to spaceTrim the blocks
1783
1840
  promptbookString += '\n';
@@ -1842,6 +1899,7 @@ var PromptbookReferenceError = /** @class */ (function (_super) {
1842
1899
  * Library of promptbooks that groups together promptbooks for an application.
1843
1900
  * This implementation is a very thin wrapper around the Array / Map of promptbooks.
1844
1901
  *
1902
+ * @private use `createLibraryFromJson` instead
1845
1903
  * @see https://github.com/webgptorg/promptbook#promptbook-library
1846
1904
  */
1847
1905
  var SimplePromptbookLibrary = /** @class */ (function () {
@@ -1850,8 +1908,9 @@ var SimplePromptbookLibrary = /** @class */ (function () {
1850
1908
  *
1851
1909
  * @param promptbooks !!!
1852
1910
  *
1911
+ * @private Use instead `createLibraryFromJson`
1853
1912
  * Note: During the construction logic of all promptbooks are validated
1854
- * Note: It is not recommended to use this constructor directly, use `createPromptbookLibraryFromSources` *(or other variant)* instead
1913
+ * Note: It is not recommended to use this constructor directly, use `createLibraryFromJson` *(or other variant)* instead
1855
1914
  */
1856
1915
  function SimplePromptbookLibrary() {
1857
1916
  var e_1, _a;
@@ -1864,14 +1923,14 @@ var SimplePromptbookLibrary = /** @class */ (function () {
1864
1923
  for (var promptbooks_1 = __values(promptbooks), promptbooks_1_1 = promptbooks_1.next(); !promptbooks_1_1.done; promptbooks_1_1 = promptbooks_1.next()) {
1865
1924
  var promptbook = promptbooks_1_1.value;
1866
1925
  if (promptbook.promptbookUrl === undefined) {
1867
- 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 ")));
1926
+ throw new PromptbookReferenceError(spaceTrim$1("\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 ")));
1868
1927
  }
1869
1928
  validatePromptbookJson(promptbook);
1870
1929
  // Note: [🦄]
1871
1930
  if (this.library.has(promptbook.promptbookUrl) &&
1872
1931
  promptbookJsonToString(promptbook) !==
1873
1932
  promptbookJsonToString(this.library.get(promptbook.promptbookUrl))) {
1874
- 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 ")));
1933
+ throw new PromptbookReferenceError(spaceTrim$1("\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 ")));
1875
1934
  }
1876
1935
  this.library.set(promptbook.promptbookUrl, promptbook);
1877
1936
  }
@@ -1900,9 +1959,9 @@ var SimplePromptbookLibrary = /** @class */ (function () {
1900
1959
  var promptbook = this.library.get(url);
1901
1960
  if (!promptbook) {
1902
1961
  if (this.listPromptbooks().length === 0) {
1903
- throw new PromptbookNotFoundError(spaceTrim("\n Promptbook with url \"".concat(url, "\" not found\n\n No promptbooks available\n ")));
1962
+ throw new PromptbookNotFoundError(spaceTrim$1("\n Promptbook with url \"".concat(url, "\" not found\n\n No promptbooks available\n ")));
1904
1963
  }
1905
- throw new PromptbookNotFoundError(spaceTrim(function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPromptbooks()
1964
+ throw new PromptbookNotFoundError(spaceTrim$1(function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPromptbooks()
1906
1965
  .map(function (promptbookUrl) { return "- ".concat(promptbookUrl); })
1907
1966
  .join('\n')), "\n\n "); }));
1908
1967
  }
@@ -1920,68 +1979,23 @@ var SimplePromptbookLibrary = /** @class */ (function () {
1920
1979
  /**
1921
1980
  * Creates PromptbookLibrary from array of PromptbookJson or PromptbookString
1922
1981
  *
1923
- * Note: You can combine `PromptbookString` (`.ptbk.md`) with `PromptbookJson` BUT it is not recommended
1982
+ * Note: Functions `libraryToJson` and `createLibraryFromJson` are complementary
1924
1983
  * Note: During the construction syntax and logic of all sources are validated
1925
1984
  *
1926
1985
  * @param promptbookSources
1927
1986
  * @returns PromptbookLibrary
1928
1987
  */
1929
- function createPromptbookLibraryFromSources() {
1930
- var promptbookSources = [];
1988
+ function createLibraryFromJson() {
1989
+ var promptbooks = [];
1931
1990
  for (var _i = 0; _i < arguments.length; _i++) {
1932
- promptbookSources[_i] = arguments[_i];
1991
+ promptbooks[_i] = arguments[_i];
1933
1992
  }
1934
1993
  return __awaiter(this, void 0, void 0, function () {
1935
- var promptbooks, promptbookSources_1, promptbookSources_1_1, source, promptbook, e_1_1;
1936
- var e_1, _a;
1937
- return __generator(this, function (_b) {
1938
- switch (_b.label) {
1939
- case 0:
1940
- promptbooks = new Array();
1941
- _b.label = 1;
1942
- case 1:
1943
- _b.trys.push([1, 8, 9, 10]);
1944
- promptbookSources_1 = __values(promptbookSources), promptbookSources_1_1 = promptbookSources_1.next();
1945
- _b.label = 2;
1946
- case 2:
1947
- if (!!promptbookSources_1_1.done) return [3 /*break*/, 7];
1948
- source = promptbookSources_1_1.value;
1949
- promptbook = void 0;
1950
- if (!(typeof source === 'string')) return [3 /*break*/, 4];
1951
- return [4 /*yield*/, promptbookStringToJson(source)];
1952
- case 3:
1953
- // Note: When directly creating from string, no need to validate the source
1954
- // The validation is performed always before execution
1955
- promptbook = _b.sent();
1956
- return [3 /*break*/, 5];
1957
- case 4:
1958
- promptbook = source;
1959
- _b.label = 5;
1960
- case 5:
1961
- promptbooks.push(promptbook);
1962
- _b.label = 6;
1963
- case 6:
1964
- promptbookSources_1_1 = promptbookSources_1.next();
1965
- return [3 /*break*/, 2];
1966
- case 7: return [3 /*break*/, 10];
1967
- case 8:
1968
- e_1_1 = _b.sent();
1969
- e_1 = { error: e_1_1 };
1970
- return [3 /*break*/, 10];
1971
- case 9:
1972
- try {
1973
- if (promptbookSources_1_1 && !promptbookSources_1_1.done && (_a = promptbookSources_1.return)) _a.call(promptbookSources_1);
1974
- }
1975
- finally { if (e_1) throw e_1.error; }
1976
- return [7 /*endfinally*/];
1977
- case 10: return [2 /*return*/, new (SimplePromptbookLibrary.bind.apply(SimplePromptbookLibrary, __spreadArray([void 0], __read(promptbooks), false)))()];
1978
- }
1994
+ return __generator(this, function (_a) {
1995
+ return [2 /*return*/, new (SimplePromptbookLibrary.bind.apply(SimplePromptbookLibrary, __spreadArray([void 0], __read(promptbooks), false)))()];
1979
1996
  });
1980
1997
  });
1981
1998
  }
1982
- /**
1983
- * TODO: !!!! [🧠] Library precompilation and do not mix markdown and json promptbooks
1984
- */
1985
1999
 
1986
2000
  /* tslint:disable */
1987
2001
  function normalizeToKebabCase(sentence) {
@@ -2046,7 +2060,7 @@ function prepareKnowledgeFromMarkdown(options) {
2046
2060
  switch (_b.label) {
2047
2061
  case 0:
2048
2062
  content = options.content, llmTools = options.llmTools, _a = options.isVerbose, isVerbose = _a === void 0 ? false : _a;
2049
- return [4 /*yield*/, createPromptbookLibraryFromSources.apply(void 0, __spreadArray([], __read(promptbookLibrary), false))];
2063
+ return [4 /*yield*/, createLibraryFromJson.apply(void 0, __spreadArray([], __read(promptbookLibrary), false))];
2050
2064
  case 1:
2051
2065
  library = _b.sent();
2052
2066
  promptbook = library.getPromptbookByUrl('https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md');
@@ -2073,14 +2087,14 @@ function prepareKnowledgeFromMarkdown(options) {
2073
2087
  var name, title, content, keywords, index, sources;
2074
2088
  return __generator(this, function (_a) {
2075
2089
  name = "piece-".concat(i);
2076
- title = spaceTrim$1(knowledgeTextPiece.substring(0, 100));
2077
- content = spaceTrim$1(knowledgeTextPiece);
2090
+ title = spaceTrim(knowledgeTextPiece.substring(0, 100));
2091
+ content = spaceTrim(knowledgeTextPiece);
2078
2092
  keywords = [];
2079
2093
  index = [];
2080
2094
  sources = [];
2081
2095
  try {
2082
2096
  // TODO: !!!! Summarize name and title from the content
2083
- title = spaceTrim$1(knowledgeTextPiece.substring(0, 30));
2097
+ title = spaceTrim(knowledgeTextPiece.substring(0, 30));
2084
2098
  name = normalizeToKebabCase(title);
2085
2099
  // TODO: !!!! Extract keywords via prompt
2086
2100
  // TODO: !!!! Index through LLM model
@@ -2181,7 +2195,7 @@ function markdownToMarkdownStructure(markdown) {
2181
2195
  }
2182
2196
  if (parent_1.parent === null /* <- Note: We are in root */) {
2183
2197
  // [🌻]
2184
- throw new Error(spaceTrim("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
2198
+ throw new Error(spaceTrim$1("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
2185
2199
  }
2186
2200
  parent_1 = parent_1.parent;
2187
2201
  }
@@ -2215,7 +2229,7 @@ function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
2215
2229
  return {
2216
2230
  level: level,
2217
2231
  title: title,
2218
- content: spaceTrim(contentLines.join('\n')),
2232
+ content: spaceTrim$1(contentLines.join('\n')),
2219
2233
  sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
2220
2234
  };
2221
2235
  }
@@ -2446,7 +2460,7 @@ function extractVariables(script) {
2446
2460
  if (!(error instanceof Error)) {
2447
2461
  throw error;
2448
2462
  }
2449
- throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
2463
+ throw new PromptbookSyntaxError(spaceTrim$1(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
2450
2464
  }
2451
2465
  return variables;
2452
2466
  }
@@ -2706,15 +2720,15 @@ function parseCommand(listItem) {
2706
2720
  type.startsWith('PROMPTBOOKURL') ||
2707
2721
  type.startsWith('HTTPS')) {
2708
2722
  if (!(listItemParts.length === 2 || (listItemParts.length === 1 && type.startsWith('HTTPS')))) {
2709
- throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n ")));
2723
+ throw new PromptbookSyntaxError(spaceTrim$1("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n ")));
2710
2724
  }
2711
2725
  var promptbookUrlString = listItemParts.pop();
2712
2726
  var promptbookUrl = new URL(promptbookUrlString);
2713
2727
  if (promptbookUrl.protocol !== 'https:') {
2714
- throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n Protocol must be HTTPS\n ")));
2728
+ throw new PromptbookSyntaxError(spaceTrim$1("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n Protocol must be HTTPS\n ")));
2715
2729
  }
2716
2730
  if (promptbookUrl.hash !== '') {
2717
- throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n URL must not contain hash\n Hash is used for identification of the prompt template in the pipeline\n ")));
2731
+ throw new PromptbookSyntaxError(spaceTrim$1("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n URL must not contain hash\n Hash is used for identification of the prompt template in the pipeline\n ")));
2718
2732
  }
2719
2733
  return {
2720
2734
  type: 'PROMPTBOOK_URL',
@@ -2723,7 +2737,7 @@ function parseCommand(listItem) {
2723
2737
  }
2724
2738
  else if (type.startsWith('PROMPTBOOK_VERSION') || type.startsWith('PTBK_VERSION')) {
2725
2739
  if (listItemParts.length !== 2) {
2726
- throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_VERSION command:\n\n - ".concat(listItem, "\n ")));
2740
+ throw new PromptbookSyntaxError(spaceTrim$1("\n Invalid PROMPTBOOK_VERSION command:\n\n - ".concat(listItem, "\n ")));
2727
2741
  }
2728
2742
  var promptbookVersion = listItemParts.pop();
2729
2743
  // TODO: Validate version
@@ -2738,7 +2752,7 @@ function parseCommand(listItem) {
2738
2752
  type.startsWith('SIMPLE_TEMPLATE')) {
2739
2753
  var executionTypes = ExecutionTypes.filter(function (executionType) { return type.includes(executionType); });
2740
2754
  if (executionTypes.length !== 1) {
2741
- throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Unknown execution type in command:\n\n - ".concat(listItem, "\n\n Supported execution types are:\n ").concat(block(ExecutionTypes.join(', ')), "\n "); }));
2755
+ throw new PromptbookSyntaxError(spaceTrim$1(function (block) { return "\n Unknown execution type in command:\n\n - ".concat(listItem, "\n\n Supported execution types are:\n ").concat(block(ExecutionTypes.join(', ')), "\n "); }));
2742
2756
  }
2743
2757
  return {
2744
2758
  type: 'EXECUTE',
@@ -2763,7 +2777,7 @@ function parseCommand(listItem) {
2763
2777
  };
2764
2778
  }
2765
2779
  else {
2766
- throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Unknown model variant in command:\n\n - ".concat(listItem, "\n\n Supported variants are:\n ").concat(block(['CHAT', 'COMPLETION'].join(', ')), "\n "); }));
2780
+ throw new PromptbookSyntaxError(spaceTrim$1(function (block) { return "\n Unknown model variant in command:\n\n - ".concat(listItem, "\n\n Supported variants are:\n ").concat(block(['CHAT', 'COMPLETION'].join(', ')), "\n "); }));
2767
2781
  }
2768
2782
  }
2769
2783
  if (type.startsWith('MODEL_NAME')) {
@@ -2774,7 +2788,7 @@ function parseCommand(listItem) {
2774
2788
  };
2775
2789
  }
2776
2790
  else {
2777
- throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Unknown model key in command:\n\n - ".concat(listItem, "\n\n Supported model keys are:\n ").concat(block(['variant', 'name'].join(', ')), "\n\n Example:\n\n - MODEL VARIANT Chat\n - MODEL NAME gpt-4\n "); }));
2791
+ throw new PromptbookSyntaxError(spaceTrim$1(function (block) { return "\n Unknown model key in command:\n\n - ".concat(listItem, "\n\n Supported model keys are:\n ").concat(block(['variant', 'name'].join(', ')), "\n\n Example:\n\n - MODEL VARIANT Chat\n - MODEL NAME gpt-4\n "); }));
2778
2792
  }
2779
2793
  }
2780
2794
  else if (type.startsWith('PARAM') ||
@@ -2784,12 +2798,12 @@ function parseCommand(listItem) {
2784
2798
  listItem.startsWith('> {') /* <- Note: This is a bit hack to parse return parameters defined at the end of each section */) {
2785
2799
  var parametersMatch = listItem.match(/\{(?<parameterName>[a-z0-9_]+)\}[^\S\r\n]*(?<parameterDescription>.*)$/im);
2786
2800
  if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
2787
- throw new PromptbookSyntaxError(spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
2801
+ throw new PromptbookSyntaxError(spaceTrim$1("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
2788
2802
  }
2789
2803
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
2790
2804
  var _b = parametersMatch.groups, parameterName = _b.parameterName, parameterDescription = _b.parameterDescription;
2791
2805
  if (parameterDescription && parameterDescription.match(/\{(?<parameterName>[a-z0-9_]+)\}/im)) {
2792
- throw new PromptbookSyntaxError(spaceTrim("\n Parameter {".concat(parameterName, "} can not contain another parameter in description:\n\n - ").concat(listItem, "\n ")));
2806
+ throw new PromptbookSyntaxError(spaceTrim$1("\n Parameter {".concat(parameterName, "} can not contain another parameter in description:\n\n - ").concat(listItem, "\n ")));
2793
2807
  }
2794
2808
  var isInput = type.startsWith('INPUT');
2795
2809
  var isOutput = type.startsWith('OUTPUT');
@@ -2807,11 +2821,11 @@ function parseCommand(listItem) {
2807
2821
  }
2808
2822
  else if (type.startsWith('JOKER')) {
2809
2823
  if (listItemParts.length !== 2) {
2810
- throw new PromptbookSyntaxError(spaceTrim("\n Invalid JOKER command:\n\n - ".concat(listItem, "\n ")));
2824
+ throw new PromptbookSyntaxError(spaceTrim$1("\n Invalid JOKER command:\n\n - ".concat(listItem, "\n ")));
2811
2825
  }
2812
2826
  var parametersMatch = (listItemParts.pop() || '').match(/^\{(?<parameterName>[a-z0-9_]+)\}$/im);
2813
2827
  if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
2814
- throw new PromptbookSyntaxError(spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
2828
+ throw new PromptbookSyntaxError(spaceTrim$1("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
2815
2829
  }
2816
2830
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
2817
2831
  var parameterName = parametersMatch.groups.parameterName;
@@ -2822,7 +2836,7 @@ function parseCommand(listItem) {
2822
2836
  }
2823
2837
  else if (type.startsWith('POSTPROCESS') || type.startsWith('POST_PROCESS')) {
2824
2838
  if (listItemParts.length !== 2) {
2825
- throw new PromptbookSyntaxError(spaceTrim("\n Invalid POSTPROCESSING command:\n\n - ".concat(listItem, "\n ")));
2839
+ throw new PromptbookSyntaxError(spaceTrim$1("\n Invalid POSTPROCESSING command:\n\n - ".concat(listItem, "\n ")));
2826
2840
  }
2827
2841
  var functionName = listItemParts.pop();
2828
2842
  return {
@@ -2902,7 +2916,7 @@ function parseCommand(listItem) {
2902
2916
  if (!(error instanceof Error)) {
2903
2917
  throw error;
2904
2918
  }
2905
- throw new PromptbookSyntaxError(spaceTrim("\n Invalid EXPECT command; ".concat(error.message, ":\n\n - ").concat(listItem, "\n ")));
2919
+ throw new PromptbookSyntaxError(spaceTrim$1("\n Invalid EXPECT command; ".concat(error.message, ":\n\n - ").concat(listItem, "\n ")));
2906
2920
  }
2907
2921
  /*
2908
2922
  } else if (type.startsWith('__________________')) {
@@ -2910,7 +2924,7 @@ function parseCommand(listItem) {
2910
2924
  */
2911
2925
  }
2912
2926
  else {
2913
- throw new PromptbookSyntaxError(spaceTrim("\n Unknown command:\n\n - ".concat(listItem, "\n\n Supported commands are:\n - PROMPTBOOK_URL <url>\n - PROMPTBOOK_VERSION <version>\n - EXECUTE PROMPT TEMPLATE\n - EXECUTE SIMPLE TEMPLATE\n - SIMPLE TEMPLATE\n - EXECUTE SCRIPT\n - EXECUTE PROMPT_DIALOG'\n - PROMPT_DIALOG'\n - MODEL NAME <name>\n - MODEL VARIANT <\"Chat\"|\"Completion\">\n - INPUT PARAM {<name>} <description>\n - OUTPUT PARAM {<name>} <description>\n - POSTPROCESS `{functionName}`\n - JOKER {<name>}\n - EXPECT JSON\n - EXPECT <\"Exactly\"|\"Min\"|\"Max\"> <number> <\"Chars\"|\"Words\"|\"Sentences\"|\"Paragraphs\"|\"Pages\">\n\n ")));
2927
+ throw new PromptbookSyntaxError(spaceTrim$1("\n Unknown command:\n\n - ".concat(listItem, "\n\n Supported commands are:\n - PROMPTBOOK_URL <url>\n - PROMPTBOOK_VERSION <version>\n - EXECUTE PROMPT TEMPLATE\n - EXECUTE SIMPLE TEMPLATE\n - SIMPLE TEMPLATE\n - EXECUTE SCRIPT\n - EXECUTE PROMPT_DIALOG'\n - PROMPT_DIALOG'\n - MODEL NAME <name>\n - MODEL VARIANT <\"Chat\"|\"Completion\">\n - INPUT PARAM {<name>} <description>\n - OUTPUT PARAM {<name>} <description>\n - POSTPROCESS `{functionName}`\n - JOKER {<name>}\n - EXPECT JSON\n - EXPECT <\"Exactly\"|\"Min\"|\"Max\"> <number> <\"Chars\"|\"Words\"|\"Sentences\"|\"Paragraphs\"|\"Pages\">\n\n ")));
2914
2928
  }
2915
2929
  }
2916
2930
 
@@ -2991,7 +3005,7 @@ function promptbookStringToJson(promptbookString, options) {
2991
3005
  existingParameter.description &&
2992
3006
  existingParameter.description !== parameterDescription &&
2993
3007
  parameterDescription) {
2994
- throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Parameter {".concat(parameterName, "} is defined multiple times with different description.\n\n First definition:\n ").concat(block(existingParameter.description || '[undefined]'), "\n\n Second definition:\n ").concat(block(parameterDescription || '[undefined]'), "\n "); }));
3008
+ throw new PromptbookSyntaxError(spaceTrim$1(function (block) { return "\n Parameter {".concat(parameterName, "} is defined multiple times with different description.\n\n First definition:\n ").concat(block(existingParameter.description || '[undefined]'), "\n\n Second definition:\n ").concat(block(parameterDescription || '[undefined]'), "\n "); }));
2995
3009
  }
2996
3010
  if (existingParameter) {
2997
3011
  if (parameterDescription) {
@@ -3010,7 +3024,7 @@ function promptbookStringToJson(promptbookString, options) {
3010
3024
  markdownStructure = markdownToMarkdownStructure(promptbookString);
3011
3025
  markdownStructureDeepness = countMarkdownStructureDeepness(markdownStructure);
3012
3026
  if (markdownStructureDeepness !== 2) {
3013
- throw new PromptbookSyntaxError(spaceTrim("\n Invalid markdown structure.\n The markdown must have exactly 2 levels of headings (one top-level section and one section for each template).\n Now it has ".concat(markdownStructureDeepness, " levels of headings.\n ")));
3027
+ throw new PromptbookSyntaxError(spaceTrim$1("\n Invalid markdown structure.\n The markdown must have exactly 2 levels of headings (one top-level section and one section for each template).\n Now it has ".concat(markdownStructureDeepness, " levels of headings.\n ")));
3014
3028
  }
3015
3029
  promptbookJson.title = markdownStructure.title;
3016
3030
  description = markdownStructure.content;
@@ -3018,7 +3032,7 @@ function promptbookStringToJson(promptbookString, options) {
3018
3032
  description = description.split(/^```.*^```/gms).join('');
3019
3033
  //Note: Remove lists and return statement
3020
3034
  description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
3021
- description = spaceTrim(description);
3035
+ description = spaceTrim$1(description);
3022
3036
  if (description === '') {
3023
3037
  description = undefined;
3024
3038
  }
@@ -3133,13 +3147,13 @@ function promptbookStringToJson(promptbookString, options) {
3133
3147
  throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
3134
3148
  }
3135
3149
  else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
3136
- throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Script language ".concat(language, " is not supported.\n\n Supported languages are:\n ").concat(block(SUPPORTED_SCRIPT_LANGUAGES.join(', ')), "\n\n "); }));
3150
+ throw new PromptbookSyntaxError(spaceTrim$1(function (block) { return "\n Script language ".concat(language, " is not supported.\n\n Supported languages are:\n ").concat(block(SUPPORTED_SCRIPT_LANGUAGES.join(', ')), "\n\n "); }));
3137
3151
  }
3138
3152
  }
3139
3153
  var lastLine = section.content.split('\n').pop();
3140
3154
  var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
3141
3155
  if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
3142
- throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
3156
+ throw new PromptbookSyntaxError(spaceTrim$1(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
3143
3157
  // TODO: Show code of invalid sections each time + DRY
3144
3158
  section.content
3145
3159
  .split('\n')
@@ -3153,7 +3167,7 @@ function promptbookStringToJson(promptbookString, options) {
3153
3167
  description_1 = description_1.split(/^```.*^```/gms).join('');
3154
3168
  //Note: Remove lists and return statement
3155
3169
  description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
3156
- description_1 = spaceTrim(description_1);
3170
+ description_1 = spaceTrim$1(description_1);
3157
3171
  if (description_1 === '') {
3158
3172
  description_1 = undefined;
3159
3173
  }
@@ -3287,7 +3301,7 @@ function renderPromptbookMermaid(promptbookJson, options) {
3287
3301
  }
3288
3302
  return normalizeTo_camelCase('template-' + titleToName(template.title));
3289
3303
  };
3290
- var promptbookMermaid = spaceTrim(function (block) { return "\n\n %% \uD83D\uDD2E Tip: Open this on GitHub or in the VSCode website to see the Mermaid graph visually\n\n flowchart LR\n subgraph \"".concat(promptbookJson.title, "\"\n\n direction TB\n\n input((Input)):::input\n ").concat(block(promptbookJson.promptTemplates
3304
+ var promptbookMermaid = spaceTrim$1(function (block) { return "\n\n %% \uD83D\uDD2E Tip: Open this on GitHub or in the VSCode website to see the Mermaid graph visually\n\n flowchart LR\n subgraph \"".concat(promptbookJson.title, "\"\n\n direction TB\n\n input((Input)):::input\n ").concat(block(promptbookJson.promptTemplates
3291
3305
  .flatMap(function (_a) {
3292
3306
  var title = _a.title, dependentParameterNames = _a.dependentParameterNames, resultingParameterName = _a.resultingParameterName;
3293
3307
  return __spreadArray([
@@ -3343,7 +3357,7 @@ function prettifyPromptbookString(promptbookString, options) {
3343
3357
  return { href: "#".concat(promptTemplate.name), title: promptTemplate.title };
3344
3358
  },
3345
3359
  });
3346
- promptbookMermaidBlock = spaceTrim(function (block) { return "\n ```mermaid\n ".concat(block(promptbookMermaid_1), "\n ```\n "); });
3360
+ promptbookMermaidBlock = spaceTrim$1(function (block) { return "\n ```mermaid\n ".concat(block(promptbookMermaid_1), "\n ```\n "); });
3347
3361
  promptbookString = addAutoGeneratedSection(promptbookString, {
3348
3362
  sectionName: 'Graph',
3349
3363
  sectionContent: promptbookMermaidBlock,
@@ -3364,117 +3378,106 @@ function prettifyPromptbookString(promptbookString, options) {
3364
3378
  */
3365
3379
 
3366
3380
  /**
3367
- * Runs CLI script for prettifying promptbooks
3381
+ * Initializes `prettify` command for Promptbook CLI utilities
3382
+ *
3383
+ * @private part of `promptbookCli`
3368
3384
  */
3369
- function prettifyPromptbookStringCli() {
3385
+ function initializePrettify(program) {
3386
+ var _this = this;
3387
+ var prettifyCommand = program.command('prettify');
3388
+ prettifyCommand.description(spaceTrim("\n Iterates over promptbooks and does multiple enhancing operations on them:\n\n 1) Adds Mermaid graph\n 2) Prettifies the markdown\n "));
3389
+ prettifyCommand.argument('<filesGlob>', 'Promptbooks to prettify as glob pattern');
3390
+ prettifyCommand.option('-i, --ignore <glob>', "Ignore as glob pattern");
3391
+ prettifyCommand.action(function (filesGlob, _a) {
3392
+ var ignore = _a.ignore;
3393
+ return __awaiter(_this, void 0, void 0, function () {
3394
+ var filePaths, filePaths_1, filePaths_1_1, filePath, promptbookMarkdown, error_1, e_1_1;
3395
+ var e_1, _b;
3396
+ return __generator(this, function (_c) {
3397
+ switch (_c.label) {
3398
+ case 0: return [4 /*yield*/, glob(filesGlob, { ignore: ignore })];
3399
+ case 1:
3400
+ filePaths = _c.sent();
3401
+ _c.label = 2;
3402
+ case 2:
3403
+ _c.trys.push([2, 11, 12, 13]);
3404
+ filePaths_1 = __values(filePaths), filePaths_1_1 = filePaths_1.next();
3405
+ _c.label = 3;
3406
+ case 3:
3407
+ if (!!filePaths_1_1.done) return [3 /*break*/, 10];
3408
+ filePath = filePaths_1_1.value;
3409
+ if (!filePath.endsWith('.ptbk.md')) {
3410
+ console.warn(colors.yellow("Skipping prettify of non-promptbook ".concat(filePath)));
3411
+ return [3 /*break*/, 9];
3412
+ }
3413
+ return [4 /*yield*/, readFile(filePath, 'utf-8')];
3414
+ case 4:
3415
+ promptbookMarkdown = (_c.sent());
3416
+ _c.label = 5;
3417
+ case 5:
3418
+ _c.trys.push([5, 8, , 9]);
3419
+ return [4 /*yield*/, prettifyPromptbookString(promptbookMarkdown, {
3420
+ isGraphAdded: true,
3421
+ isPrettifyed: true,
3422
+ // <- [🕌]
3423
+ })];
3424
+ case 6:
3425
+ promptbookMarkdown = _c.sent();
3426
+ return [4 /*yield*/, writeFile(filePath, promptbookMarkdown)];
3427
+ case 7:
3428
+ _c.sent();
3429
+ console.info(colors.green("Prettify ".concat(filePath)));
3430
+ return [3 /*break*/, 9];
3431
+ case 8:
3432
+ error_1 = _c.sent();
3433
+ if (!(error_1 instanceof Error)) {
3434
+ throw error_1;
3435
+ }
3436
+ console.info(colors.red("Prettify ".concat(error_1.name, " ").concat(filePath)));
3437
+ console.error(colors.bgRed(error_1.name));
3438
+ console.error(error_1);
3439
+ process.exit(1);
3440
+ return [3 /*break*/, 9];
3441
+ case 9:
3442
+ filePaths_1_1 = filePaths_1.next();
3443
+ return [3 /*break*/, 3];
3444
+ case 10: return [3 /*break*/, 13];
3445
+ case 11:
3446
+ e_1_1 = _c.sent();
3447
+ e_1 = { error: e_1_1 };
3448
+ return [3 /*break*/, 13];
3449
+ case 12:
3450
+ try {
3451
+ if (filePaths_1_1 && !filePaths_1_1.done && (_b = filePaths_1.return)) _b.call(filePaths_1);
3452
+ }
3453
+ finally { if (e_1) throw e_1.error; }
3454
+ return [7 /*endfinally*/];
3455
+ case 13:
3456
+ process.exit(0);
3457
+ return [2 /*return*/];
3458
+ }
3459
+ });
3460
+ });
3461
+ });
3462
+ }
3463
+
3464
+ /**
3465
+ * Runs CLI utilities of Promptbook package
3466
+ */
3467
+ function promptbookCli() {
3370
3468
  return __awaiter(this, void 0, void 0, function () {
3371
- var program, prettifyCommand, helloCommand;
3372
- var _this = this;
3469
+ var program;
3373
3470
  return __generator(this, function (_a) {
3374
3471
  if (!isRunningInNode()) {
3375
- throw new Error(spaceTrim("\n Function prettifyPromptbookStringCli is initiator of CLI script and should be run in Node.js environment.\n\n - In browser use prettifyPromptbookString.\n\n "));
3472
+ throw new Error(spaceTrim$1("\n Function promptbookCli is initiator of CLI script and should be run in Node.js environment.\n\n - In browser use function exported from `@promptbook/utils` or `@promptbook/core` directly, for example `prettifyPromptbookString`.\n\n "));
3376
3473
  }
3377
3474
  program = new commander.Command();
3378
3475
  program.name('promptbook');
3379
3476
  program.version(PROMPTBOOK_VERSION);
3380
- program.description(spaceTrim("\n Promptbook utilities\n "));
3381
- prettifyCommand = program.command('prettify');
3382
- prettifyCommand.description(spaceTrim("\n Iterates over promptbooks and does multiple enhancing operations on them:\n 1) Adds Mermaid graph\n 2) Prettifies the markdown\n "));
3383
- prettifyCommand.argument('<filesGlob>', 'Promptbooks to prettify as glob pattern');
3384
- prettifyCommand.option('-i, --ignore <glob>', "Ignore as glob pattern");
3385
- prettifyCommand.action(function (filesGlob, _a) {
3386
- var ignore = _a.ignore;
3387
- return __awaiter(_this, void 0, void 0, function () {
3388
- var filePaths, filePaths_1, filePaths_1_1, filePath, promptbookMarkdown, error_1, e_1_1;
3389
- var e_1, _b;
3390
- return __generator(this, function (_c) {
3391
- switch (_c.label) {
3392
- case 0: return [4 /*yield*/, glob(filesGlob, { ignore: ignore })];
3393
- case 1:
3394
- filePaths = _c.sent();
3395
- _c.label = 2;
3396
- case 2:
3397
- _c.trys.push([2, 11, 12, 13]);
3398
- filePaths_1 = __values(filePaths), filePaths_1_1 = filePaths_1.next();
3399
- _c.label = 3;
3400
- case 3:
3401
- if (!!filePaths_1_1.done) return [3 /*break*/, 10];
3402
- filePath = filePaths_1_1.value;
3403
- if (!filePath.endsWith('.ptbk.md')) {
3404
- console.warn(colors.yellow("Skipping prettify of non-promptbook ".concat(filePath)));
3405
- return [3 /*break*/, 9];
3406
- }
3407
- return [4 /*yield*/, readFile(filePath, 'utf-8')];
3408
- case 4:
3409
- promptbookMarkdown = (_c.sent());
3410
- _c.label = 5;
3411
- case 5:
3412
- _c.trys.push([5, 8, , 9]);
3413
- return [4 /*yield*/, prettifyPromptbookString(promptbookMarkdown, {
3414
- isGraphAdded: true,
3415
- isPrettifyed: true,
3416
- // <- [🕌]
3417
- })];
3418
- case 6:
3419
- promptbookMarkdown = _c.sent();
3420
- return [4 /*yield*/, writeFile(filePath, promptbookMarkdown)];
3421
- case 7:
3422
- _c.sent();
3423
- console.info(colors.green("Prettify ".concat(filePath)));
3424
- return [3 /*break*/, 9];
3425
- case 8:
3426
- error_1 = _c.sent();
3427
- if (!(error_1 instanceof Error)) {
3428
- throw error_1;
3429
- }
3430
- console.info(colors.red("Prettify ".concat(error_1.name, " ").concat(filePath)));
3431
- console.error(colors.bgRed(error_1.name));
3432
- console.error(error_1);
3433
- process.exit(1);
3434
- return [3 /*break*/, 9];
3435
- case 9:
3436
- filePaths_1_1 = filePaths_1.next();
3437
- return [3 /*break*/, 3];
3438
- case 10: return [3 /*break*/, 13];
3439
- case 11:
3440
- e_1_1 = _c.sent();
3441
- e_1 = { error: e_1_1 };
3442
- return [3 /*break*/, 13];
3443
- case 12:
3444
- try {
3445
- if (filePaths_1_1 && !filePaths_1_1.done && (_b = filePaths_1.return)) _b.call(filePaths_1);
3446
- }
3447
- finally { if (e_1) throw e_1.error; }
3448
- return [7 /*endfinally*/];
3449
- case 13:
3450
- process.exit(0);
3451
- return [2 /*return*/];
3452
- }
3453
- });
3454
- });
3455
- });
3456
- helloCommand = program.command('hello');
3457
- helloCommand.description(spaceTrim("\n Just command for testing\n "));
3458
- helloCommand.argument('<name>', 'Your name');
3459
- helloCommand.option('-g, --greeting <greeting>', "Greeting", 'Hello');
3460
- helloCommand.action(function (name, _a) {
3461
- var greeting = _a.greeting;
3462
- return __awaiter(_this, void 0, void 0, function () {
3463
- return __generator(this, function (_b) {
3464
- switch (_b.label) {
3465
- case 0:
3466
- console.info(colors.cyan("".concat(greeting, " ").concat(name)));
3467
- return [4 /*yield*/, forTime(1000)];
3468
- case 1:
3469
- _b.sent();
3470
- console.info(colors.rainbow("Nice to meet you!"));
3471
- process.exit(0);
3472
- return [2 /*return*/];
3473
- }
3474
- });
3475
- });
3476
- });
3477
- //------
3477
+ program.description(spaceTrim$1("\n Promptbook utilities for enhancing workflow with promptbooks\n "));
3478
+ initializeHello(program);
3479
+ initializeMake(program);
3480
+ initializePrettify(program);
3478
3481
  program.parse(process.argv);
3479
3482
  return [2 /*return*/];
3480
3483
  });
@@ -3490,10 +3493,10 @@ function prettifyPromptbookStringCli() {
3490
3493
  /**
3491
3494
  * Hidden utilities which should not be used by external consumers.
3492
3495
  */
3493
- var __ = {
3496
+ var __CLI = {
3494
3497
  // Note: [🥠]
3495
- prettifyPromptbookStringCli: prettifyPromptbookStringCli,
3498
+ __initialize: promptbookCli,
3496
3499
  };
3497
3500
 
3498
- export { PROMPTBOOK_VERSION, __ };
3501
+ export { PROMPTBOOK_VERSION, __CLI };
3499
3502
  //# sourceMappingURL=index.es.js.map