@promptbook/node 0.58.0 → 0.59.0-1

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 (108) hide show
  1. package/README.md +1 -1
  2. package/esm/index.es.js +333 -272
  3. package/esm/index.es.js.map +1 -1
  4. package/esm/typings/_packages/core.index.d.ts +9 -10
  5. package/esm/typings/_packages/node.index.d.ts +1 -1
  6. package/esm/typings/_packages/types.index.d.ts +11 -70
  7. package/esm/typings/conversion/prettify/prettifyPromptbookString.d.ts +1 -1
  8. package/esm/typings/conversion/promptbookStringToJson.d.ts +17 -2
  9. package/esm/typings/execution/translation/automatic-translate/automatic-translators/LindatAutomaticTranslator.d.ts +1 -1
  10. package/esm/typings/execution/translation/automatic-translate/automatic-translators/TranslatorOptions.d.ts +1 -1
  11. package/esm/typings/expectations/drafts/isDomainNameFree.d.ts +6 -0
  12. package/esm/typings/expectations/drafts/isGithubNameFree.d.ts +6 -0
  13. package/esm/typings/formats/_common/FormatDefinition.d.ts +20 -4
  14. package/esm/typings/formats/csv/ListFormatDefinition.d.ts +11 -0
  15. package/esm/typings/formats/json/JsonFormatDefinition.d.ts +5 -1
  16. package/esm/typings/formats/list/ListFormatDefinition.d.ts +13 -0
  17. package/esm/typings/formats/xml/XmlFormatDefinition.d.ts +4 -0
  18. package/{umd/typings/knowledgebase → esm/typings/knowledge}/dialogs/simple-prompt/SimplePromptInterfaceTools.d.ts +1 -1
  19. package/esm/typings/knowledge/prepare-knowledge/_common/IndexPreparer.d.ts +4 -0
  20. package/esm/typings/knowledge/prepare-knowledge/markdown/playground/markdown-knowledge-playground.d.ts +2 -0
  21. package/esm/typings/knowledge/prepare-knowledge/markdown/prepareKnowledgeFromMarkdown.d.ts +7 -0
  22. package/esm/typings/knowledge/prepare-knowledge/pdf/prepareKnowledgeFromPdf.d.ts +16 -0
  23. package/esm/typings/llm-providers/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +2 -1
  24. package/esm/typings/llm-providers/mocked/MockedEchoLlmExecutionTools.d.ts +1 -1
  25. package/esm/typings/llm-providers/mocked/MockedFackedLlmExecutionTools.d.ts +1 -1
  26. package/esm/typings/llm-providers/openai/OpenAiExecutionTools.d.ts +1 -1
  27. package/esm/typings/llm-providers/remote/interfaces/RemoteServerOptions.d.ts +1 -1
  28. package/{umd/typings/library → esm/typings/promptbook-library}/constructors/createPromptbookLibraryFromDirectory.d.ts +3 -0
  29. package/esm/typings/{library → promptbook-library}/constructors/createPromptbookLibraryFromPromise.d.ts +3 -0
  30. package/{umd/typings/library → esm/typings/promptbook-library}/constructors/createPromptbookLibraryFromSources.d.ts +4 -1
  31. package/esm/typings/{library → promptbook-library}/constructors/createPromptbookLibraryFromUrl.d.ts +3 -0
  32. package/esm/typings/scripting/python/PythonExecutionTools.d.ts +1 -1
  33. package/esm/typings/scripting/typescript/TypescriptExecutionTools.d.ts +1 -1
  34. package/esm/typings/types/PromptbookJson/KnowledgeJson.d.ts +6 -0
  35. package/esm/typings/types/PromptbookJson/MaterialKnowledgePieceJson.d.ts +27 -0
  36. package/esm/typings/types/PromptbookJson/PromptbookJson.d.ts +8 -6
  37. package/esm/typings/types/typeAliases.d.ts +7 -1
  38. package/package.json +3 -3
  39. package/umd/index.umd.js +333 -272
  40. package/umd/index.umd.js.map +1 -1
  41. package/umd/typings/_packages/core.index.d.ts +9 -10
  42. package/umd/typings/_packages/node.index.d.ts +1 -1
  43. package/umd/typings/_packages/types.index.d.ts +11 -70
  44. package/umd/typings/conversion/prettify/prettifyPromptbookString.d.ts +1 -1
  45. package/umd/typings/conversion/promptbookStringToJson.d.ts +17 -2
  46. package/umd/typings/execution/translation/automatic-translate/automatic-translators/LindatAutomaticTranslator.d.ts +1 -1
  47. package/umd/typings/execution/translation/automatic-translate/automatic-translators/TranslatorOptions.d.ts +1 -1
  48. package/umd/typings/expectations/drafts/isDomainNameFree.d.ts +6 -0
  49. package/umd/typings/expectations/drafts/isGithubNameFree.d.ts +6 -0
  50. package/umd/typings/formats/_common/FormatDefinition.d.ts +20 -4
  51. package/umd/typings/formats/csv/ListFormatDefinition.d.ts +11 -0
  52. package/umd/typings/formats/json/JsonFormatDefinition.d.ts +5 -1
  53. package/umd/typings/formats/list/ListFormatDefinition.d.ts +13 -0
  54. package/umd/typings/formats/xml/XmlFormatDefinition.d.ts +4 -0
  55. package/{esm/typings/knowledgebase → umd/typings/knowledge}/dialogs/simple-prompt/SimplePromptInterfaceTools.d.ts +1 -1
  56. package/umd/typings/knowledge/prepare-knowledge/_common/IndexPreparer.d.ts +4 -0
  57. package/umd/typings/knowledge/prepare-knowledge/markdown/playground/markdown-knowledge-playground.d.ts +2 -0
  58. package/umd/typings/knowledge/prepare-knowledge/markdown/prepareKnowledgeFromMarkdown.d.ts +7 -0
  59. package/umd/typings/knowledge/prepare-knowledge/pdf/prepareKnowledgeFromPdf.d.ts +16 -0
  60. package/umd/typings/llm-providers/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +2 -1
  61. package/umd/typings/llm-providers/mocked/MockedEchoLlmExecutionTools.d.ts +1 -1
  62. package/umd/typings/llm-providers/mocked/MockedFackedLlmExecutionTools.d.ts +1 -1
  63. package/umd/typings/llm-providers/openai/OpenAiExecutionTools.d.ts +1 -1
  64. package/umd/typings/llm-providers/remote/interfaces/RemoteServerOptions.d.ts +1 -1
  65. package/{esm/typings/library → umd/typings/promptbook-library}/constructors/createPromptbookLibraryFromDirectory.d.ts +3 -0
  66. package/umd/typings/{library → promptbook-library}/constructors/createPromptbookLibraryFromPromise.d.ts +3 -0
  67. package/{esm/typings/library → umd/typings/promptbook-library}/constructors/createPromptbookLibraryFromSources.d.ts +4 -1
  68. package/umd/typings/{library → promptbook-library}/constructors/createPromptbookLibraryFromUrl.d.ts +3 -0
  69. package/umd/typings/scripting/python/PythonExecutionTools.d.ts +1 -1
  70. package/umd/typings/scripting/typescript/TypescriptExecutionTools.d.ts +1 -1
  71. package/umd/typings/types/PromptbookJson/KnowledgeJson.d.ts +6 -0
  72. package/umd/typings/types/PromptbookJson/MaterialKnowledgePieceJson.d.ts +27 -0
  73. package/umd/typings/types/PromptbookJson/PromptbookJson.d.ts +8 -6
  74. package/umd/typings/types/typeAliases.d.ts +7 -1
  75. package/esm/typings/execution/plugins/script-execution-tools/postprocessing.test.d.ts +0 -1
  76. package/esm/typings/execution/plugins/script-execution-tools/script-execution-errors.test.d.ts +0 -1
  77. package/esm/typings/execution/plugins/script-execution-tools/script-execution-tools.test.d.ts +0 -1
  78. package/umd/typings/execution/plugins/script-execution-tools/custom-function-async.test.ts.test.d.ts +0 -1
  79. package/umd/typings/execution/plugins/script-execution-tools/custom-function-missing.test.d.ts +0 -1
  80. package/umd/typings/execution/plugins/script-execution-tools/custom-function-with-dependencies.test.d.ts +0 -1
  81. package/umd/typings/execution/plugins/script-execution-tools/custom-function.test.d.ts +0 -1
  82. package/umd/typings/execution/plugins/script-execution-tools/postprocessing.test.d.ts +0 -1
  83. package/umd/typings/execution/plugins/script-execution-tools/script-execution-errors.test.d.ts +0 -1
  84. package/umd/typings/execution/plugins/script-execution-tools/script-execution-tools.test.d.ts +0 -1
  85. /package/esm/typings/{knowledgebase → knowledge}/dialogs/callback/CallbackInterfaceTools.d.ts +0 -0
  86. /package/esm/typings/{knowledgebase → knowledge}/dialogs/callback/CallbackInterfaceToolsOptions.d.ts +0 -0
  87. /package/esm/typings/{knowledgebase → knowledge}/dialogs/user-interface-execution-tools.test.d.ts +0 -0
  88. /package/esm/typings/{execution/plugins/script-execution-tools/custom-function-async.test.ts.test.d.ts → knowledge/prepare-knowledge/markdown/prepareKnowledgeFromMarkdown.test.d.ts} +0 -0
  89. /package/esm/typings/{execution/plugins/script-execution-tools/custom-function-missing.test.d.ts → knowledge/prepare-knowledge/pdf/prepareKnowledgeFromPdf.test.d.ts} +0 -0
  90. /package/esm/typings/{library → promptbook-library}/PromptbookLibrary.d.ts +0 -0
  91. /package/esm/typings/{library → promptbook-library}/SimplePromptbookLibrary.d.ts +0 -0
  92. /package/esm/typings/{library → promptbook-library}/constructors/createPromptbookLibraryFromDirectory.test.d.ts +0 -0
  93. /package/esm/typings/{library → promptbook-library}/constructors/createPromptbookLibraryFromPromise.test.d.ts +0 -0
  94. /package/esm/typings/{library → promptbook-library}/constructors/createPromptbookLibraryFromSources.test.d.ts +0 -0
  95. /package/esm/typings/{library → promptbook-library}/constructors/createPromptbookSublibrary.d.ts +0 -0
  96. /package/esm/typings/{library → promptbook-library}/constructors/justTestFsImport.d.ts +0 -0
  97. /package/umd/typings/{knowledgebase → knowledge}/dialogs/callback/CallbackInterfaceTools.d.ts +0 -0
  98. /package/umd/typings/{knowledgebase → knowledge}/dialogs/callback/CallbackInterfaceToolsOptions.d.ts +0 -0
  99. /package/umd/typings/{knowledgebase → knowledge}/dialogs/user-interface-execution-tools.test.d.ts +0 -0
  100. /package/{esm/typings/execution/plugins/script-execution-tools/custom-function-with-dependencies.test.d.ts → umd/typings/knowledge/prepare-knowledge/markdown/prepareKnowledgeFromMarkdown.test.d.ts} +0 -0
  101. /package/{esm/typings/execution/plugins/script-execution-tools/custom-function.test.d.ts → umd/typings/knowledge/prepare-knowledge/pdf/prepareKnowledgeFromPdf.test.d.ts} +0 -0
  102. /package/umd/typings/{library → promptbook-library}/PromptbookLibrary.d.ts +0 -0
  103. /package/umd/typings/{library → promptbook-library}/SimplePromptbookLibrary.d.ts +0 -0
  104. /package/umd/typings/{library → promptbook-library}/constructors/createPromptbookLibraryFromDirectory.test.d.ts +0 -0
  105. /package/umd/typings/{library → promptbook-library}/constructors/createPromptbookLibraryFromPromise.test.d.ts +0 -0
  106. /package/umd/typings/{library → promptbook-library}/constructors/createPromptbookLibraryFromSources.test.d.ts +0 -0
  107. /package/umd/typings/{library → promptbook-library}/constructors/createPromptbookSublibrary.d.ts +0 -0
  108. /package/umd/typings/{library → promptbook-library}/constructors/justTestFsImport.d.ts +0 -0
package/esm/index.es.js CHANGED
@@ -137,6 +137,15 @@ var PromptbookSyntaxError = /** @class */ (function (_super) {
137
137
  return PromptbookSyntaxError;
138
138
  }(Error));
139
139
 
140
+ // import prepareKnowledgeFromMarkdownStringPromptbook from './prepare-knowledge-from-markdown.ptbk.md';
141
+ function prepareKnowledgeFromMarkdown(options) {
142
+ return __awaiter(this, void 0, void 0, function () {
143
+ return __generator(this, function (_a) {
144
+ return [2 /*return*/, []];
145
+ });
146
+ });
147
+ }
148
+
140
149
  /**
141
150
  * Supported script languages
142
151
  */
@@ -448,7 +457,7 @@ function union() {
448
457
  /**
449
458
  * The version of the Promptbook library
450
459
  */
451
- var PROMPTBOOK_VERSION = '0.57.2';
460
+ var PROMPTBOOK_VERSION = '0.59.0-0';
452
461
 
453
462
  /**
454
463
  * Parses the template and returns the list of all parameter names
@@ -1319,250 +1328,267 @@ function titleToName(value) {
1319
1328
  }
1320
1329
 
1321
1330
  /**
1322
- * Parse promptbook from string format to JSON format
1331
+ * Compile promptbook from string (markdown) format to JSON format
1323
1332
  *
1333
+ * @param promptbookString {Promptbook} in string markdown format (.ptbk.md)
1334
+ * @param options - Options and tools for the compilation
1335
+ * @returns {Promptbook} compiled in JSON format (.ptbk.json)
1324
1336
  * @throws {PromptbookSyntaxError} if the promptbook string is not valid
1325
1337
  *
1326
1338
  * Note: This function does not validate logic of the pipeline only the syntax
1339
+ * Note: This function acts as compilation process
1327
1340
  */
1328
- function promptbookStringToJson(promptbookString) {
1329
- var e_1, _a, e_2, _b;
1330
- var promptbookJson = {
1331
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1332
- title: undefined /* <- Note: Putting here placeholder to keep `title` on top at final JSON */,
1333
- promptbookUrl: undefined /* <- Note: Putting here placeholder to keep `promptbookUrl` on top at final JSON */,
1334
- promptbookVersion: PROMPTBOOK_VERSION,
1335
- description: undefined /* <- Note: Putting here placeholder to keep `description` on top at final JSON */,
1336
- parameters: [],
1337
- promptTemplates: [],
1338
- };
1339
- // =============================================================
1340
- // Note: 1️⃣ Normalization of the PROMPTBOOK string
1341
- promptbookString = removeContentComments(promptbookString);
1342
- promptbookString = promptbookString.replaceAll(/`\{(?<parameterName>[a-z0-9_]+)\}`/gi, '{$<parameterName>}');
1343
- promptbookString = promptbookString.replaceAll(/`->\s+\{(?<parameterName>[a-z0-9_]+)\}`/gi, '-> {$<parameterName>}');
1344
- // =============================================================
1345
- ///Note: 2️⃣ Function for adding parameters
1346
- var addParam = function (parameterCommand) {
1347
- var parameterName = parameterCommand.parameterName, parameterDescription = parameterCommand.parameterDescription, isInput = parameterCommand.isInput, isOutput = parameterCommand.isOutput;
1348
- var existingParameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
1349
- if (existingParameter &&
1350
- existingParameter.description &&
1351
- existingParameter.description !== parameterDescription &&
1352
- parameterDescription) {
1353
- 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 "); }));
1354
- }
1355
- if (existingParameter) {
1356
- if (parameterDescription) {
1357
- existingParameter.description = parameterDescription;
1358
- }
1359
- }
1360
- else {
1361
- promptbookJson.parameters.push({
1362
- name: parameterName,
1363
- description: parameterDescription || undefined,
1364
- isInput: isInput,
1365
- isOutput: isOutput,
1366
- });
1367
- }
1368
- };
1369
- // =============================================================
1370
- // Note: 3️⃣ Parse the dynamic part - the template pipeline
1371
- var markdownStructure = markdownToMarkdownStructure(promptbookString);
1372
- var markdownStructureDeepness = countMarkdownStructureDeepness(markdownStructure);
1373
- if (markdownStructureDeepness !== 2) {
1374
- 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 ")));
1375
- }
1376
- promptbookJson.title = markdownStructure.title;
1377
- // TODO: [1] DRY description
1378
- var description = markdownStructure.content;
1379
- // Note: Remove codeblocks
1380
- description = description.split(/^```.*^```/gms).join('');
1381
- //Note: Remove lists and return statement
1382
- description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
1383
- description = spaceTrim(description);
1384
- if (description === '') {
1385
- description = undefined;
1386
- }
1387
- promptbookJson.description = description;
1388
- var defaultModelRequirements = {};
1389
- var listItems = extractAllListItemsFromMarkdown(markdownStructure.content);
1390
- try {
1391
- for (var listItems_1 = __values(listItems), listItems_1_1 = listItems_1.next(); !listItems_1_1.done; listItems_1_1 = listItems_1.next()) {
1392
- var listItem = listItems_1_1.value;
1393
- var command = parseCommand(listItem);
1394
- switch (command.type) {
1395
- case 'PROMPTBOOK_URL':
1396
- promptbookJson.promptbookUrl = command.promptbookUrl.href;
1397
- break;
1398
- case 'PROMPTBOOK_VERSION':
1399
- promptbookJson.promptbookVersion = command.promptbookVersion;
1400
- break;
1401
- case 'MODEL':
1402
- defaultModelRequirements[command.key] = command.value;
1403
- break;
1404
- case 'PARAMETER':
1405
- addParam(command);
1406
- break;
1407
- default:
1408
- throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the head of the promptbook ONLY at the prompt template block"));
1409
- }
1410
- }
1411
- }
1412
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
1413
- finally {
1414
- try {
1415
- if (listItems_1_1 && !listItems_1_1.done && (_a = listItems_1.return)) _a.call(listItems_1);
1416
- }
1417
- finally { if (e_1) throw e_1.error; }
1418
- }
1419
- var _loop_1 = function (section) {
1420
- var e_3, _e;
1421
- // TODO: Parse prompt template description (the content out of the codeblock and lists)
1422
- var templateModelRequirements = __assign({}, defaultModelRequirements);
1423
- var listItems_3 = extractAllListItemsFromMarkdown(section.content);
1424
- var dependentParameterNames = new Set();
1425
- var executionType = 'PROMPT_TEMPLATE';
1426
- var jokers = [];
1427
- var postprocessing = [];
1428
- var expectAmount = {};
1429
- var expectFormat = undefined;
1430
- var isExecutionTypeChanged = false;
1431
- try {
1432
- for (var listItems_2 = (e_3 = void 0, __values(listItems_3)), listItems_2_1 = listItems_2.next(); !listItems_2_1.done; listItems_2_1 = listItems_2.next()) {
1433
- var listItem = listItems_2_1.value;
1434
- var command = parseCommand(listItem);
1435
- switch (command.type) {
1436
- case 'JOKER':
1437
- jokers.push(command.parameterName);
1438
- dependentParameterNames.add(command.parameterName);
1439
- break;
1440
- case 'EXECUTE':
1441
- if (isExecutionTypeChanged) {
1442
- throw new PromptbookSyntaxError('Execution type is already defined in the prompt template. It can be defined only once.');
1341
+ function promptbookStringToJson(promptbookString, options) {
1342
+ if (options === void 0) { options = {}; }
1343
+ return __awaiter(this, void 0, void 0, function () {
1344
+ var llmTools, promptbookJson, knowledge, addParam, markdownStructure, markdownStructureDeepness, description, defaultModelRequirements, listItems, listItems_1, listItems_1_1, listItem, command, _loop_1, _a, _b, section;
1345
+ var e_1, _c, e_2, _d;
1346
+ return __generator(this, function (_e) {
1347
+ switch (_e.label) {
1348
+ case 0:
1349
+ llmTools = options.llmTools;
1350
+ promptbookJson = {
1351
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1352
+ title: undefined /* <- Note: Putting here placeholder to keep `title` on top at final JSON */,
1353
+ promptbookUrl: undefined /* <- Note: Putting here placeholder to keep `promptbookUrl` on top at final JSON */,
1354
+ promptbookVersion: PROMPTBOOK_VERSION,
1355
+ description: undefined /* <- Note: Putting here placeholder to keep `description` on top at final JSON */,
1356
+ parameters: [],
1357
+ promptTemplates: [],
1358
+ knowledge: [],
1359
+ };
1360
+ if (!llmTools) return [3 /*break*/, 2];
1361
+ return [4 /*yield*/, prepareKnowledgeFromMarkdown()];
1362
+ case 1:
1363
+ knowledge = _e.sent();
1364
+ console.info('!!!! knowledge', knowledge);
1365
+ _e.label = 2;
1366
+ case 2:
1367
+ // =============================================================
1368
+ // Note: 1️⃣ Normalization of the PROMPTBOOK string
1369
+ promptbookString = removeContentComments(promptbookString);
1370
+ promptbookString = promptbookString.replaceAll(/`\{(?<parameterName>[a-z0-9_]+)\}`/gi, '{$<parameterName>}');
1371
+ promptbookString = promptbookString.replaceAll(/`->\s+\{(?<parameterName>[a-z0-9_]+)\}`/gi, '-> {$<parameterName>}');
1372
+ addParam = function (parameterCommand) {
1373
+ var parameterName = parameterCommand.parameterName, parameterDescription = parameterCommand.parameterDescription, isInput = parameterCommand.isInput, isOutput = parameterCommand.isOutput;
1374
+ var existingParameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
1375
+ if (existingParameter &&
1376
+ existingParameter.description &&
1377
+ existingParameter.description !== parameterDescription &&
1378
+ parameterDescription) {
1379
+ 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 "); }));
1443
1380
  }
1444
- executionType = command.executionType;
1445
- isExecutionTypeChanged = true;
1446
- break;
1447
- case 'MODEL':
1448
- templateModelRequirements[command.key] = command.value;
1449
- break;
1450
- case 'PARAMETER':
1451
- // Note: This is just for detecting resulitng parameter name
1452
- addParam(command);
1453
- break;
1454
- case 'POSTPROCESS':
1455
- postprocessing.push(command.functionName);
1456
- break;
1457
- case 'EXPECT_AMOUNT':
1458
- // eslint-disable-next-line no-case-declarations
1459
- var unit = command.unit.toLowerCase();
1460
- expectAmount[unit] = expectAmount[unit] || {};
1461
- if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
1462
- if (expectAmount[unit].min !== undefined) {
1463
- throw new PromptbookSyntaxError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
1381
+ if (existingParameter) {
1382
+ if (parameterDescription) {
1383
+ existingParameter.description = parameterDescription;
1464
1384
  }
1465
- expectAmount[unit].min = command.amount;
1466
- } /* not else */
1467
- if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
1468
- if (expectAmount[unit].max !== undefined) {
1469
- throw new PromptbookSyntaxError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
1385
+ }
1386
+ else {
1387
+ promptbookJson.parameters.push({
1388
+ name: parameterName,
1389
+ description: parameterDescription || undefined,
1390
+ isInput: isInput,
1391
+ isOutput: isOutput,
1392
+ });
1393
+ }
1394
+ };
1395
+ markdownStructure = markdownToMarkdownStructure(promptbookString);
1396
+ markdownStructureDeepness = countMarkdownStructureDeepness(markdownStructure);
1397
+ if (markdownStructureDeepness !== 2) {
1398
+ 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 ")));
1399
+ }
1400
+ promptbookJson.title = markdownStructure.title;
1401
+ description = markdownStructure.content;
1402
+ // Note: Remove codeblocks
1403
+ description = description.split(/^```.*^```/gms).join('');
1404
+ //Note: Remove lists and return statement
1405
+ description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
1406
+ description = spaceTrim(description);
1407
+ if (description === '') {
1408
+ description = undefined;
1409
+ }
1410
+ promptbookJson.description = description;
1411
+ defaultModelRequirements = {};
1412
+ listItems = extractAllListItemsFromMarkdown(markdownStructure.content);
1413
+ try {
1414
+ for (listItems_1 = __values(listItems), listItems_1_1 = listItems_1.next(); !listItems_1_1.done; listItems_1_1 = listItems_1.next()) {
1415
+ listItem = listItems_1_1.value;
1416
+ command = parseCommand(listItem);
1417
+ switch (command.type) {
1418
+ case 'PROMPTBOOK_URL':
1419
+ promptbookJson.promptbookUrl = command.promptbookUrl.href;
1420
+ break;
1421
+ case 'PROMPTBOOK_VERSION':
1422
+ promptbookJson.promptbookVersion = command.promptbookVersion;
1423
+ break;
1424
+ case 'MODEL':
1425
+ defaultModelRequirements[command.key] = command.value;
1426
+ break;
1427
+ case 'PARAMETER':
1428
+ addParam(command);
1429
+ break;
1430
+ default:
1431
+ throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the head of the promptbook ONLY at the prompt template block"));
1470
1432
  }
1471
- expectAmount[unit].max = command.amount;
1472
1433
  }
1473
- break;
1474
- case 'EXPECT_FORMAT':
1475
- if (expectFormat !== undefined && command.format !== expectFormat) {
1476
- throw new PromptbookSyntaxError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
1434
+ }
1435
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1436
+ finally {
1437
+ try {
1438
+ if (listItems_1_1 && !listItems_1_1.done && (_c = listItems_1.return)) _c.call(listItems_1);
1477
1439
  }
1478
- expectFormat = command.format;
1479
- break;
1480
- default:
1481
- throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the block of the prompt template ONLY at the head of the promptbook"));
1482
- }
1483
- }
1484
- }
1485
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
1486
- finally {
1487
- try {
1488
- if (listItems_2_1 && !listItems_2_1.done && (_e = listItems_2.return)) _e.call(listItems_2);
1489
- }
1490
- finally { if (e_3) throw e_3.error; }
1491
- }
1492
- var _f = extractOneBlockFromMarkdown(section.content), language = _f.language, content = _f.content;
1493
- if (executionType === 'SCRIPT') {
1494
- if (!language) {
1495
- throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
1496
- }
1497
- else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
1498
- 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 "); }));
1440
+ finally { if (e_1) throw e_1.error; }
1441
+ }
1442
+ _loop_1 = function (section) {
1443
+ var e_3, _f;
1444
+ // TODO: Parse prompt template description (the content out of the codeblock and lists)
1445
+ var templateModelRequirements = __assign({}, defaultModelRequirements);
1446
+ var listItems_3 = extractAllListItemsFromMarkdown(section.content);
1447
+ var dependentParameterNames = new Set();
1448
+ var executionType = 'PROMPT_TEMPLATE';
1449
+ var jokers = [];
1450
+ var postprocessing = [];
1451
+ var expectAmount = {};
1452
+ var expectFormat = undefined;
1453
+ var isExecutionTypeChanged = false;
1454
+ try {
1455
+ for (var listItems_2 = (e_3 = void 0, __values(listItems_3)), listItems_2_1 = listItems_2.next(); !listItems_2_1.done; listItems_2_1 = listItems_2.next()) {
1456
+ var listItem = listItems_2_1.value;
1457
+ var command = parseCommand(listItem);
1458
+ switch (command.type) {
1459
+ case 'JOKER':
1460
+ jokers.push(command.parameterName);
1461
+ dependentParameterNames.add(command.parameterName);
1462
+ break;
1463
+ case 'EXECUTE':
1464
+ if (isExecutionTypeChanged) {
1465
+ throw new PromptbookSyntaxError('Execution type is already defined in the prompt template. It can be defined only once.');
1466
+ }
1467
+ executionType = command.executionType;
1468
+ isExecutionTypeChanged = true;
1469
+ break;
1470
+ case 'MODEL':
1471
+ templateModelRequirements[command.key] = command.value;
1472
+ break;
1473
+ case 'PARAMETER':
1474
+ // Note: This is just for detecting resulitng parameter name
1475
+ addParam(command);
1476
+ break;
1477
+ case 'POSTPROCESS':
1478
+ postprocessing.push(command.functionName);
1479
+ break;
1480
+ case 'EXPECT_AMOUNT':
1481
+ // eslint-disable-next-line no-case-declarations
1482
+ var unit = command.unit.toLowerCase();
1483
+ expectAmount[unit] = expectAmount[unit] || {};
1484
+ if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
1485
+ if (expectAmount[unit].min !== undefined) {
1486
+ throw new PromptbookSyntaxError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
1487
+ }
1488
+ expectAmount[unit].min = command.amount;
1489
+ } /* not else */
1490
+ if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
1491
+ if (expectAmount[unit].max !== undefined) {
1492
+ throw new PromptbookSyntaxError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
1493
+ }
1494
+ expectAmount[unit].max = command.amount;
1495
+ }
1496
+ break;
1497
+ case 'EXPECT_FORMAT':
1498
+ if (expectFormat !== undefined && command.format !== expectFormat) {
1499
+ throw new PromptbookSyntaxError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
1500
+ }
1501
+ expectFormat = command.format;
1502
+ break;
1503
+ default:
1504
+ throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the block of the prompt template ONLY at the head of the promptbook"));
1505
+ }
1506
+ }
1507
+ }
1508
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
1509
+ finally {
1510
+ try {
1511
+ if (listItems_2_1 && !listItems_2_1.done && (_f = listItems_2.return)) _f.call(listItems_2);
1512
+ }
1513
+ finally { if (e_3) throw e_3.error; }
1514
+ }
1515
+ var _g = extractOneBlockFromMarkdown(section.content), language = _g.language, content = _g.content;
1516
+ if (executionType === 'SCRIPT') {
1517
+ if (!language) {
1518
+ throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
1519
+ }
1520
+ else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
1521
+ 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 "); }));
1522
+ }
1523
+ }
1524
+ var lastLine = section.content.split('\n').pop();
1525
+ var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
1526
+ if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
1527
+ throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
1528
+ // TODO: Show code of invalid sections each time + DRY
1529
+ section.content
1530
+ .split('\n')
1531
+ .map(function (line) { return "> ".concat(line); })
1532
+ .join('\n')), "\n "); }));
1533
+ }
1534
+ var resultingParameterName = match.groups.resultingParamName;
1535
+ // TODO: [1] DRY description
1536
+ var description_1 = section.content;
1537
+ // Note: Remove codeblocks
1538
+ description_1 = description_1.split(/^```.*^```/gms).join('');
1539
+ //Note: Remove lists and return statement
1540
+ description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
1541
+ description_1 = spaceTrim(description_1);
1542
+ if (description_1 === '') {
1543
+ description_1 = undefined;
1544
+ }
1545
+ if (Object.keys(jokers).length === 0) {
1546
+ jokers = undefined;
1547
+ }
1548
+ if (Object.keys(expectAmount).length === 0) {
1549
+ expectAmount = undefined;
1550
+ }
1551
+ if (Object.keys(postprocessing).length === 0) {
1552
+ postprocessing = undefined;
1553
+ }
1554
+ dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, executionType: executionType, content: content })));
1555
+ if (templateModelRequirements.modelVariant === undefined) {
1556
+ templateModelRequirements.modelVariant = 'CHAT';
1557
+ }
1558
+ promptbookJson.promptTemplates.push({
1559
+ name: titleToName(section.title),
1560
+ title: section.title,
1561
+ description: description_1,
1562
+ dependentParameterNames: Array.from(dependentParameterNames),
1563
+ executionType: executionType,
1564
+ jokers: jokers,
1565
+ postprocessing: postprocessing,
1566
+ expectations: expectAmount,
1567
+ expectFormat: expectFormat,
1568
+ modelRequirements: templateModelRequirements,
1569
+ contentLanguage: executionType === 'SCRIPT' ? language : undefined,
1570
+ content: content,
1571
+ resultingParameterName: resultingParameterName,
1572
+ });
1573
+ };
1574
+ try {
1575
+ for (_a = __values(markdownStructure.sections), _b = _a.next(); !_b.done; _b = _a.next()) {
1576
+ section = _b.value;
1577
+ _loop_1(section);
1578
+ }
1579
+ }
1580
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1581
+ finally {
1582
+ try {
1583
+ if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
1584
+ }
1585
+ finally { if (e_2) throw e_2.error; }
1586
+ }
1587
+ // =============================================================
1588
+ return [2 /*return*/, promptbookJson];
1499
1589
  }
1500
- }
1501
- var lastLine = section.content.split('\n').pop();
1502
- var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
1503
- if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
1504
- throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
1505
- // TODO: Show code of invalid sections each time + DRY
1506
- section.content
1507
- .split('\n')
1508
- .map(function (line) { return "> ".concat(line); })
1509
- .join('\n')), "\n "); }));
1510
- }
1511
- var resultingParameterName = match.groups.resultingParamName;
1512
- // TODO: [1] DRY description
1513
- var description_1 = section.content;
1514
- // Note: Remove codeblocks
1515
- description_1 = description_1.split(/^```.*^```/gms).join('');
1516
- //Note: Remove lists and return statement
1517
- description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
1518
- description_1 = spaceTrim(description_1);
1519
- if (description_1 === '') {
1520
- description_1 = undefined;
1521
- }
1522
- if (Object.keys(jokers).length === 0) {
1523
- jokers = undefined;
1524
- }
1525
- if (Object.keys(expectAmount).length === 0) {
1526
- expectAmount = undefined;
1527
- }
1528
- if (Object.keys(postprocessing).length === 0) {
1529
- postprocessing = undefined;
1530
- }
1531
- dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, executionType: executionType, content: content })));
1532
- if (templateModelRequirements.modelVariant === undefined) {
1533
- templateModelRequirements.modelVariant = 'CHAT';
1534
- }
1535
- promptbookJson.promptTemplates.push({
1536
- name: titleToName(section.title),
1537
- title: section.title,
1538
- description: description_1,
1539
- dependentParameterNames: Array.from(dependentParameterNames),
1540
- executionType: executionType,
1541
- jokers: jokers,
1542
- postprocessing: postprocessing,
1543
- expectations: expectAmount,
1544
- expectFormat: expectFormat,
1545
- modelRequirements: templateModelRequirements,
1546
- contentLanguage: executionType === 'SCRIPT' ? language : undefined,
1547
- content: content,
1548
- resultingParameterName: resultingParameterName,
1549
1590
  });
1550
- };
1551
- try {
1552
- for (var _c = __values(markdownStructure.sections), _d = _c.next(); !_d.done; _d = _c.next()) {
1553
- var section = _d.value;
1554
- _loop_1(section);
1555
- }
1556
- }
1557
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
1558
- finally {
1559
- try {
1560
- if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
1561
- }
1562
- finally { if (e_2) throw e_2.error; }
1563
- }
1564
- // =============================================================
1565
- return promptbookJson;
1591
+ });
1566
1592
  }
1567
1593
  /**
1568
1594
  * TODO: Report here line/column of error
@@ -2171,36 +2197,61 @@ var SimplePromptbookLibrary = /** @class */ (function () {
2171
2197
  * @returns PromptbookLibrary
2172
2198
  */
2173
2199
  function createPromptbookLibraryFromSources() {
2174
- var e_1, _a;
2175
2200
  var promptbookSources = [];
2176
2201
  for (var _i = 0; _i < arguments.length; _i++) {
2177
2202
  promptbookSources[_i] = arguments[_i];
2178
2203
  }
2179
- var promptbooks = new Array();
2180
- try {
2181
- for (var promptbookSources_1 = __values(promptbookSources), promptbookSources_1_1 = promptbookSources_1.next(); !promptbookSources_1_1.done; promptbookSources_1_1 = promptbookSources_1.next()) {
2182
- var source = promptbookSources_1_1.value;
2183
- var promptbook = void 0;
2184
- if (typeof source === 'string') {
2185
- // Note: When directly creating from string, no need to validate the source
2186
- // The validation is performed always before execution
2187
- promptbook = promptbookStringToJson(source);
2188
- }
2189
- else {
2190
- promptbook = source;
2204
+ return __awaiter(this, void 0, void 0, function () {
2205
+ var promptbooks, promptbookSources_1, promptbookSources_1_1, source, promptbook, e_1_1;
2206
+ var e_1, _a;
2207
+ return __generator(this, function (_b) {
2208
+ switch (_b.label) {
2209
+ case 0:
2210
+ promptbooks = new Array();
2211
+ _b.label = 1;
2212
+ case 1:
2213
+ _b.trys.push([1, 8, 9, 10]);
2214
+ promptbookSources_1 = __values(promptbookSources), promptbookSources_1_1 = promptbookSources_1.next();
2215
+ _b.label = 2;
2216
+ case 2:
2217
+ if (!!promptbookSources_1_1.done) return [3 /*break*/, 7];
2218
+ source = promptbookSources_1_1.value;
2219
+ promptbook = void 0;
2220
+ if (!(typeof source === 'string')) return [3 /*break*/, 4];
2221
+ return [4 /*yield*/, promptbookStringToJson(source)];
2222
+ case 3:
2223
+ // Note: When directly creating from string, no need to validate the source
2224
+ // The validation is performed always before execution
2225
+ promptbook = _b.sent();
2226
+ return [3 /*break*/, 5];
2227
+ case 4:
2228
+ promptbook = source;
2229
+ _b.label = 5;
2230
+ case 5:
2231
+ promptbooks.push(promptbook);
2232
+ _b.label = 6;
2233
+ case 6:
2234
+ promptbookSources_1_1 = promptbookSources_1.next();
2235
+ return [3 /*break*/, 2];
2236
+ case 7: return [3 /*break*/, 10];
2237
+ case 8:
2238
+ e_1_1 = _b.sent();
2239
+ e_1 = { error: e_1_1 };
2240
+ return [3 /*break*/, 10];
2241
+ case 9:
2242
+ try {
2243
+ if (promptbookSources_1_1 && !promptbookSources_1_1.done && (_a = promptbookSources_1.return)) _a.call(promptbookSources_1);
2244
+ }
2245
+ finally { if (e_1) throw e_1.error; }
2246
+ return [7 /*endfinally*/];
2247
+ case 10: return [2 /*return*/, new (SimplePromptbookLibrary.bind.apply(SimplePromptbookLibrary, __spreadArray([void 0], __read(promptbooks), false)))()];
2191
2248
  }
2192
- promptbooks.push(promptbook);
2193
- }
2194
- }
2195
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
2196
- finally {
2197
- try {
2198
- if (promptbookSources_1_1 && !promptbookSources_1_1.done && (_a = promptbookSources_1.return)) _a.call(promptbookSources_1);
2199
- }
2200
- finally { if (e_1) throw e_1.error; }
2201
- }
2202
- return new (SimplePromptbookLibrary.bind.apply(SimplePromptbookLibrary, __spreadArray([void 0], __read(promptbooks), false)))();
2249
+ });
2250
+ });
2203
2251
  }
2252
+ /**
2253
+ * TODO: !!!! [🧠] Library precompilation and do not mix markdown and json promptbooks
2254
+ */
2204
2255
 
2205
2256
  /**
2206
2257
  * Constructs Promptbook from async sources
@@ -2236,7 +2287,9 @@ function createPromptbookLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
2236
2287
  return [4 /*yield*/, promptbookSourcesPromiseOrFactory];
2237
2288
  case 1:
2238
2289
  promptbookSources = _a.sent();
2239
- library = createPromptbookLibraryFromSources.apply(void 0, __spreadArray([], __read(promptbookSources), false));
2290
+ return [4 /*yield*/, createPromptbookLibraryFromSources.apply(void 0, __spreadArray([], __read(promptbookSources), false))];
2291
+ case 2:
2292
+ library = _a.sent();
2240
2293
  return [2 /*return*/];
2241
2294
  }
2242
2295
  });
@@ -2284,6 +2337,9 @@ function createPromptbookLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
2284
2337
  isResponsibleForPrompt: isResponsibleForPrompt,
2285
2338
  };
2286
2339
  }
2340
+ /**
2341
+ * TODO: !!!! [🧠] Library precompilation and do not mix markdown and json promptbooks
2342
+ */
2287
2343
 
2288
2344
  /**
2289
2345
  * Constructs Promptbook from given directory
@@ -2326,31 +2382,33 @@ function createPromptbookLibraryFromDirectory(path, options) {
2326
2382
  return __generator(this, function (_e) {
2327
2383
  switch (_e.label) {
2328
2384
  case 0:
2329
- _e.trys.push([0, 6, , 7]);
2385
+ _e.trys.push([0, 7, , 8]);
2330
2386
  promptbook = null;
2331
- if (!fileName.endsWith('.ptbk.md')) return [3 /*break*/, 2];
2387
+ if (!fileName.endsWith('.ptbk.md')) return [3 /*break*/, 3];
2332
2388
  return [4 /*yield*/, readFile(fileName, 'utf8')];
2333
2389
  case 1:
2334
2390
  promptbookString = (_e.sent());
2335
- promptbook = promptbookStringToJson(promptbookString);
2336
- return [3 /*break*/, 5];
2391
+ return [4 /*yield*/, promptbookStringToJson(promptbookString)];
2337
2392
  case 2:
2338
- if (!fileName.endsWith('.ptbk.json')) return [3 /*break*/, 4];
2393
+ promptbook = _e.sent();
2394
+ return [3 /*break*/, 6];
2395
+ case 3:
2396
+ if (!fileName.endsWith('.ptbk.json')) return [3 /*break*/, 5];
2339
2397
  if (isVerbose) {
2340
2398
  console.info("Loading ".concat(fileName));
2341
2399
  }
2342
2400
  _d = (_c = JSON).parse;
2343
2401
  return [4 /*yield*/, readFile(fileName, 'utf8')];
2344
- case 3:
2402
+ case 4:
2345
2403
  // TODO: Handle non-valid JSON files
2346
2404
  promptbook = _d.apply(_c, [_e.sent()]);
2347
- return [3 /*break*/, 5];
2348
- case 4:
2405
+ return [3 /*break*/, 6];
2406
+ case 5:
2349
2407
  if (isVerbose) {
2350
2408
  console.info("Skipping file ".concat(fileName));
2351
2409
  }
2352
- _e.label = 5;
2353
- case 5:
2410
+ _e.label = 6;
2411
+ case 6:
2354
2412
  // ---
2355
2413
  if (promptbook !== null) {
2356
2414
  if (!promptbook.promptbookUrl) {
@@ -2371,8 +2429,8 @@ function createPromptbookLibraryFromDirectory(path, options) {
2371
2429
  promptbooks.push(promptbook);
2372
2430
  }
2373
2431
  }
2374
- return [3 /*break*/, 7];
2375
- case 6:
2432
+ return [3 /*break*/, 8];
2433
+ case 7:
2376
2434
  error_1 = _e.sent();
2377
2435
  if (!(error_1 instanceof Error)) {
2378
2436
  throw error_1;
@@ -2382,8 +2440,8 @@ function createPromptbookLibraryFromDirectory(path, options) {
2382
2440
  throw new PromptbookLibraryError(wrappedErrorMessage);
2383
2441
  }
2384
2442
  console.error(wrappedErrorMessage);
2385
- return [3 /*break*/, 7];
2386
- case 7: return [2 /*return*/];
2443
+ return [3 /*break*/, 8];
2444
+ case 8: return [2 /*return*/];
2387
2445
  }
2388
2446
  });
2389
2447
  };
@@ -2486,6 +2544,9 @@ function listAllFiles(path, isRecursive) {
2486
2544
  });
2487
2545
  });
2488
2546
  }
2547
+ /**
2548
+ * TODO: !!!! [🧠] Library precompilation and do not mix markdown and json promptbooks
2549
+ */
2489
2550
 
2490
2551
  export { PROMPTBOOK_VERSION, createPromptbookLibraryFromDirectory };
2491
2552
  //# sourceMappingURL=index.es.js.map