@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/umd/index.umd.js CHANGED
@@ -142,6 +142,15 @@
142
142
  return PromptbookSyntaxError;
143
143
  }(Error));
144
144
 
145
+ // import prepareKnowledgeFromMarkdownStringPromptbook from './prepare-knowledge-from-markdown.ptbk.md';
146
+ function prepareKnowledgeFromMarkdown(options) {
147
+ return __awaiter(this, void 0, void 0, function () {
148
+ return __generator(this, function (_a) {
149
+ return [2 /*return*/, []];
150
+ });
151
+ });
152
+ }
153
+
145
154
  /**
146
155
  * Supported script languages
147
156
  */
@@ -453,7 +462,7 @@
453
462
  /**
454
463
  * The version of the Promptbook library
455
464
  */
456
- var PROMPTBOOK_VERSION = '0.57.2';
465
+ var PROMPTBOOK_VERSION = '0.59.0-0';
457
466
 
458
467
  /**
459
468
  * Parses the template and returns the list of all parameter names
@@ -1324,250 +1333,267 @@
1324
1333
  }
1325
1334
 
1326
1335
  /**
1327
- * Parse promptbook from string format to JSON format
1336
+ * Compile promptbook from string (markdown) format to JSON format
1328
1337
  *
1338
+ * @param promptbookString {Promptbook} in string markdown format (.ptbk.md)
1339
+ * @param options - Options and tools for the compilation
1340
+ * @returns {Promptbook} compiled in JSON format (.ptbk.json)
1329
1341
  * @throws {PromptbookSyntaxError} if the promptbook string is not valid
1330
1342
  *
1331
1343
  * Note: This function does not validate logic of the pipeline only the syntax
1344
+ * Note: This function acts as compilation process
1332
1345
  */
1333
- function promptbookStringToJson(promptbookString) {
1334
- var e_1, _a, e_2, _b;
1335
- var promptbookJson = {
1336
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1337
- title: undefined /* <- Note: Putting here placeholder to keep `title` on top at final JSON */,
1338
- promptbookUrl: undefined /* <- Note: Putting here placeholder to keep `promptbookUrl` on top at final JSON */,
1339
- promptbookVersion: PROMPTBOOK_VERSION,
1340
- description: undefined /* <- Note: Putting here placeholder to keep `description` on top at final JSON */,
1341
- parameters: [],
1342
- promptTemplates: [],
1343
- };
1344
- // =============================================================
1345
- // Note: 1️⃣ Normalization of the PROMPTBOOK string
1346
- promptbookString = removeContentComments(promptbookString);
1347
- promptbookString = promptbookString.replaceAll(/`\{(?<parameterName>[a-z0-9_]+)\}`/gi, '{$<parameterName>}');
1348
- promptbookString = promptbookString.replaceAll(/`->\s+\{(?<parameterName>[a-z0-9_]+)\}`/gi, '-> {$<parameterName>}');
1349
- // =============================================================
1350
- ///Note: 2️⃣ Function for adding parameters
1351
- var addParam = function (parameterCommand) {
1352
- var parameterName = parameterCommand.parameterName, parameterDescription = parameterCommand.parameterDescription, isInput = parameterCommand.isInput, isOutput = parameterCommand.isOutput;
1353
- var existingParameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
1354
- if (existingParameter &&
1355
- existingParameter.description &&
1356
- existingParameter.description !== parameterDescription &&
1357
- parameterDescription) {
1358
- throw new PromptbookSyntaxError(spaceTrim.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 "); }));
1359
- }
1360
- if (existingParameter) {
1361
- if (parameterDescription) {
1362
- existingParameter.description = parameterDescription;
1363
- }
1364
- }
1365
- else {
1366
- promptbookJson.parameters.push({
1367
- name: parameterName,
1368
- description: parameterDescription || undefined,
1369
- isInput: isInput,
1370
- isOutput: isOutput,
1371
- });
1372
- }
1373
- };
1374
- // =============================================================
1375
- // Note: 3️⃣ Parse the dynamic part - the template pipeline
1376
- var markdownStructure = markdownToMarkdownStructure(promptbookString);
1377
- var markdownStructureDeepness = countMarkdownStructureDeepness(markdownStructure);
1378
- if (markdownStructureDeepness !== 2) {
1379
- throw new PromptbookSyntaxError(spaceTrim.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 ")));
1380
- }
1381
- promptbookJson.title = markdownStructure.title;
1382
- // TODO: [1] DRY description
1383
- var description = markdownStructure.content;
1384
- // Note: Remove codeblocks
1385
- description = description.split(/^```.*^```/gms).join('');
1386
- //Note: Remove lists and return statement
1387
- description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
1388
- description = spaceTrim.spaceTrim(description);
1389
- if (description === '') {
1390
- description = undefined;
1391
- }
1392
- promptbookJson.description = description;
1393
- var defaultModelRequirements = {};
1394
- var listItems = extractAllListItemsFromMarkdown(markdownStructure.content);
1395
- try {
1396
- for (var listItems_1 = __values(listItems), listItems_1_1 = listItems_1.next(); !listItems_1_1.done; listItems_1_1 = listItems_1.next()) {
1397
- var listItem = listItems_1_1.value;
1398
- var command = parseCommand(listItem);
1399
- switch (command.type) {
1400
- case 'PROMPTBOOK_URL':
1401
- promptbookJson.promptbookUrl = command.promptbookUrl.href;
1402
- break;
1403
- case 'PROMPTBOOK_VERSION':
1404
- promptbookJson.promptbookVersion = command.promptbookVersion;
1405
- break;
1406
- case 'MODEL':
1407
- defaultModelRequirements[command.key] = command.value;
1408
- break;
1409
- case 'PARAMETER':
1410
- addParam(command);
1411
- break;
1412
- default:
1413
- throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the head of the promptbook ONLY at the prompt template block"));
1414
- }
1415
- }
1416
- }
1417
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
1418
- finally {
1419
- try {
1420
- if (listItems_1_1 && !listItems_1_1.done && (_a = listItems_1.return)) _a.call(listItems_1);
1421
- }
1422
- finally { if (e_1) throw e_1.error; }
1423
- }
1424
- var _loop_1 = function (section) {
1425
- var e_3, _e;
1426
- // TODO: Parse prompt template description (the content out of the codeblock and lists)
1427
- var templateModelRequirements = __assign({}, defaultModelRequirements);
1428
- var listItems_3 = extractAllListItemsFromMarkdown(section.content);
1429
- var dependentParameterNames = new Set();
1430
- var executionType = 'PROMPT_TEMPLATE';
1431
- var jokers = [];
1432
- var postprocessing = [];
1433
- var expectAmount = {};
1434
- var expectFormat = undefined;
1435
- var isExecutionTypeChanged = false;
1436
- try {
1437
- 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()) {
1438
- var listItem = listItems_2_1.value;
1439
- var command = parseCommand(listItem);
1440
- switch (command.type) {
1441
- case 'JOKER':
1442
- jokers.push(command.parameterName);
1443
- dependentParameterNames.add(command.parameterName);
1444
- break;
1445
- case 'EXECUTE':
1446
- if (isExecutionTypeChanged) {
1447
- throw new PromptbookSyntaxError('Execution type is already defined in the prompt template. It can be defined only once.');
1346
+ function promptbookStringToJson(promptbookString, options) {
1347
+ if (options === void 0) { options = {}; }
1348
+ return __awaiter(this, void 0, void 0, function () {
1349
+ var llmTools, promptbookJson, knowledge, addParam, markdownStructure, markdownStructureDeepness, description, defaultModelRequirements, listItems, listItems_1, listItems_1_1, listItem, command, _loop_1, _a, _b, section;
1350
+ var e_1, _c, e_2, _d;
1351
+ return __generator(this, function (_e) {
1352
+ switch (_e.label) {
1353
+ case 0:
1354
+ llmTools = options.llmTools;
1355
+ promptbookJson = {
1356
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1357
+ title: undefined /* <- Note: Putting here placeholder to keep `title` on top at final JSON */,
1358
+ promptbookUrl: undefined /* <- Note: Putting here placeholder to keep `promptbookUrl` on top at final JSON */,
1359
+ promptbookVersion: PROMPTBOOK_VERSION,
1360
+ description: undefined /* <- Note: Putting here placeholder to keep `description` on top at final JSON */,
1361
+ parameters: [],
1362
+ promptTemplates: [],
1363
+ knowledge: [],
1364
+ };
1365
+ if (!llmTools) return [3 /*break*/, 2];
1366
+ return [4 /*yield*/, prepareKnowledgeFromMarkdown()];
1367
+ case 1:
1368
+ knowledge = _e.sent();
1369
+ console.info('!!!! knowledge', knowledge);
1370
+ _e.label = 2;
1371
+ case 2:
1372
+ // =============================================================
1373
+ // Note: 1️⃣ Normalization of the PROMPTBOOK string
1374
+ promptbookString = removeContentComments(promptbookString);
1375
+ promptbookString = promptbookString.replaceAll(/`\{(?<parameterName>[a-z0-9_]+)\}`/gi, '{$<parameterName>}');
1376
+ promptbookString = promptbookString.replaceAll(/`->\s+\{(?<parameterName>[a-z0-9_]+)\}`/gi, '-> {$<parameterName>}');
1377
+ addParam = function (parameterCommand) {
1378
+ var parameterName = parameterCommand.parameterName, parameterDescription = parameterCommand.parameterDescription, isInput = parameterCommand.isInput, isOutput = parameterCommand.isOutput;
1379
+ var existingParameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
1380
+ if (existingParameter &&
1381
+ existingParameter.description &&
1382
+ existingParameter.description !== parameterDescription &&
1383
+ parameterDescription) {
1384
+ throw new PromptbookSyntaxError(spaceTrim.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 "); }));
1448
1385
  }
1449
- executionType = command.executionType;
1450
- isExecutionTypeChanged = true;
1451
- break;
1452
- case 'MODEL':
1453
- templateModelRequirements[command.key] = command.value;
1454
- break;
1455
- case 'PARAMETER':
1456
- // Note: This is just for detecting resulitng parameter name
1457
- addParam(command);
1458
- break;
1459
- case 'POSTPROCESS':
1460
- postprocessing.push(command.functionName);
1461
- break;
1462
- case 'EXPECT_AMOUNT':
1463
- // eslint-disable-next-line no-case-declarations
1464
- var unit = command.unit.toLowerCase();
1465
- expectAmount[unit] = expectAmount[unit] || {};
1466
- if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
1467
- if (expectAmount[unit].min !== undefined) {
1468
- throw new PromptbookSyntaxError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
1386
+ if (existingParameter) {
1387
+ if (parameterDescription) {
1388
+ existingParameter.description = parameterDescription;
1469
1389
  }
1470
- expectAmount[unit].min = command.amount;
1471
- } /* not else */
1472
- if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
1473
- if (expectAmount[unit].max !== undefined) {
1474
- throw new PromptbookSyntaxError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
1390
+ }
1391
+ else {
1392
+ promptbookJson.parameters.push({
1393
+ name: parameterName,
1394
+ description: parameterDescription || undefined,
1395
+ isInput: isInput,
1396
+ isOutput: isOutput,
1397
+ });
1398
+ }
1399
+ };
1400
+ markdownStructure = markdownToMarkdownStructure(promptbookString);
1401
+ markdownStructureDeepness = countMarkdownStructureDeepness(markdownStructure);
1402
+ if (markdownStructureDeepness !== 2) {
1403
+ throw new PromptbookSyntaxError(spaceTrim.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 ")));
1404
+ }
1405
+ promptbookJson.title = markdownStructure.title;
1406
+ description = markdownStructure.content;
1407
+ // Note: Remove codeblocks
1408
+ description = description.split(/^```.*^```/gms).join('');
1409
+ //Note: Remove lists and return statement
1410
+ description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
1411
+ description = spaceTrim.spaceTrim(description);
1412
+ if (description === '') {
1413
+ description = undefined;
1414
+ }
1415
+ promptbookJson.description = description;
1416
+ defaultModelRequirements = {};
1417
+ listItems = extractAllListItemsFromMarkdown(markdownStructure.content);
1418
+ try {
1419
+ for (listItems_1 = __values(listItems), listItems_1_1 = listItems_1.next(); !listItems_1_1.done; listItems_1_1 = listItems_1.next()) {
1420
+ listItem = listItems_1_1.value;
1421
+ command = parseCommand(listItem);
1422
+ switch (command.type) {
1423
+ case 'PROMPTBOOK_URL':
1424
+ promptbookJson.promptbookUrl = command.promptbookUrl.href;
1425
+ break;
1426
+ case 'PROMPTBOOK_VERSION':
1427
+ promptbookJson.promptbookVersion = command.promptbookVersion;
1428
+ break;
1429
+ case 'MODEL':
1430
+ defaultModelRequirements[command.key] = command.value;
1431
+ break;
1432
+ case 'PARAMETER':
1433
+ addParam(command);
1434
+ break;
1435
+ default:
1436
+ throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the head of the promptbook ONLY at the prompt template block"));
1475
1437
  }
1476
- expectAmount[unit].max = command.amount;
1477
1438
  }
1478
- break;
1479
- case 'EXPECT_FORMAT':
1480
- if (expectFormat !== undefined && command.format !== expectFormat) {
1481
- throw new PromptbookSyntaxError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
1439
+ }
1440
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1441
+ finally {
1442
+ try {
1443
+ if (listItems_1_1 && !listItems_1_1.done && (_c = listItems_1.return)) _c.call(listItems_1);
1482
1444
  }
1483
- expectFormat = command.format;
1484
- break;
1485
- default:
1486
- throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the block of the prompt template ONLY at the head of the promptbook"));
1487
- }
1488
- }
1489
- }
1490
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
1491
- finally {
1492
- try {
1493
- if (listItems_2_1 && !listItems_2_1.done && (_e = listItems_2.return)) _e.call(listItems_2);
1494
- }
1495
- finally { if (e_3) throw e_3.error; }
1496
- }
1497
- var _f = extractOneBlockFromMarkdown(section.content), language = _f.language, content = _f.content;
1498
- if (executionType === 'SCRIPT') {
1499
- if (!language) {
1500
- throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
1501
- }
1502
- else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
1503
- throw new PromptbookSyntaxError(spaceTrim.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 "); }));
1445
+ finally { if (e_1) throw e_1.error; }
1446
+ }
1447
+ _loop_1 = function (section) {
1448
+ var e_3, _f;
1449
+ // TODO: Parse prompt template description (the content out of the codeblock and lists)
1450
+ var templateModelRequirements = __assign({}, defaultModelRequirements);
1451
+ var listItems_3 = extractAllListItemsFromMarkdown(section.content);
1452
+ var dependentParameterNames = new Set();
1453
+ var executionType = 'PROMPT_TEMPLATE';
1454
+ var jokers = [];
1455
+ var postprocessing = [];
1456
+ var expectAmount = {};
1457
+ var expectFormat = undefined;
1458
+ var isExecutionTypeChanged = false;
1459
+ try {
1460
+ 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()) {
1461
+ var listItem = listItems_2_1.value;
1462
+ var command = parseCommand(listItem);
1463
+ switch (command.type) {
1464
+ case 'JOKER':
1465
+ jokers.push(command.parameterName);
1466
+ dependentParameterNames.add(command.parameterName);
1467
+ break;
1468
+ case 'EXECUTE':
1469
+ if (isExecutionTypeChanged) {
1470
+ throw new PromptbookSyntaxError('Execution type is already defined in the prompt template. It can be defined only once.');
1471
+ }
1472
+ executionType = command.executionType;
1473
+ isExecutionTypeChanged = true;
1474
+ break;
1475
+ case 'MODEL':
1476
+ templateModelRequirements[command.key] = command.value;
1477
+ break;
1478
+ case 'PARAMETER':
1479
+ // Note: This is just for detecting resulitng parameter name
1480
+ addParam(command);
1481
+ break;
1482
+ case 'POSTPROCESS':
1483
+ postprocessing.push(command.functionName);
1484
+ break;
1485
+ case 'EXPECT_AMOUNT':
1486
+ // eslint-disable-next-line no-case-declarations
1487
+ var unit = command.unit.toLowerCase();
1488
+ expectAmount[unit] = expectAmount[unit] || {};
1489
+ if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
1490
+ if (expectAmount[unit].min !== undefined) {
1491
+ throw new PromptbookSyntaxError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
1492
+ }
1493
+ expectAmount[unit].min = command.amount;
1494
+ } /* not else */
1495
+ if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
1496
+ if (expectAmount[unit].max !== undefined) {
1497
+ throw new PromptbookSyntaxError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
1498
+ }
1499
+ expectAmount[unit].max = command.amount;
1500
+ }
1501
+ break;
1502
+ case 'EXPECT_FORMAT':
1503
+ if (expectFormat !== undefined && command.format !== expectFormat) {
1504
+ throw new PromptbookSyntaxError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
1505
+ }
1506
+ expectFormat = command.format;
1507
+ break;
1508
+ default:
1509
+ throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the block of the prompt template ONLY at the head of the promptbook"));
1510
+ }
1511
+ }
1512
+ }
1513
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
1514
+ finally {
1515
+ try {
1516
+ if (listItems_2_1 && !listItems_2_1.done && (_f = listItems_2.return)) _f.call(listItems_2);
1517
+ }
1518
+ finally { if (e_3) throw e_3.error; }
1519
+ }
1520
+ var _g = extractOneBlockFromMarkdown(section.content), language = _g.language, content = _g.content;
1521
+ if (executionType === 'SCRIPT') {
1522
+ if (!language) {
1523
+ throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
1524
+ }
1525
+ else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
1526
+ throw new PromptbookSyntaxError(spaceTrim.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 "); }));
1527
+ }
1528
+ }
1529
+ var lastLine = section.content.split('\n').pop();
1530
+ var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
1531
+ if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
1532
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
1533
+ // TODO: Show code of invalid sections each time + DRY
1534
+ section.content
1535
+ .split('\n')
1536
+ .map(function (line) { return "> ".concat(line); })
1537
+ .join('\n')), "\n "); }));
1538
+ }
1539
+ var resultingParameterName = match.groups.resultingParamName;
1540
+ // TODO: [1] DRY description
1541
+ var description_1 = section.content;
1542
+ // Note: Remove codeblocks
1543
+ description_1 = description_1.split(/^```.*^```/gms).join('');
1544
+ //Note: Remove lists and return statement
1545
+ description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
1546
+ description_1 = spaceTrim.spaceTrim(description_1);
1547
+ if (description_1 === '') {
1548
+ description_1 = undefined;
1549
+ }
1550
+ if (Object.keys(jokers).length === 0) {
1551
+ jokers = undefined;
1552
+ }
1553
+ if (Object.keys(expectAmount).length === 0) {
1554
+ expectAmount = undefined;
1555
+ }
1556
+ if (Object.keys(postprocessing).length === 0) {
1557
+ postprocessing = undefined;
1558
+ }
1559
+ dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, executionType: executionType, content: content })));
1560
+ if (templateModelRequirements.modelVariant === undefined) {
1561
+ templateModelRequirements.modelVariant = 'CHAT';
1562
+ }
1563
+ promptbookJson.promptTemplates.push({
1564
+ name: titleToName(section.title),
1565
+ title: section.title,
1566
+ description: description_1,
1567
+ dependentParameterNames: Array.from(dependentParameterNames),
1568
+ executionType: executionType,
1569
+ jokers: jokers,
1570
+ postprocessing: postprocessing,
1571
+ expectations: expectAmount,
1572
+ expectFormat: expectFormat,
1573
+ modelRequirements: templateModelRequirements,
1574
+ contentLanguage: executionType === 'SCRIPT' ? language : undefined,
1575
+ content: content,
1576
+ resultingParameterName: resultingParameterName,
1577
+ });
1578
+ };
1579
+ try {
1580
+ for (_a = __values(markdownStructure.sections), _b = _a.next(); !_b.done; _b = _a.next()) {
1581
+ section = _b.value;
1582
+ _loop_1(section);
1583
+ }
1584
+ }
1585
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1586
+ finally {
1587
+ try {
1588
+ if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
1589
+ }
1590
+ finally { if (e_2) throw e_2.error; }
1591
+ }
1592
+ // =============================================================
1593
+ return [2 /*return*/, promptbookJson];
1504
1594
  }
1505
- }
1506
- var lastLine = section.content.split('\n').pop();
1507
- var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
1508
- if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
1509
- throw new PromptbookSyntaxError(spaceTrim.spaceTrim(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
1510
- // TODO: Show code of invalid sections each time + DRY
1511
- section.content
1512
- .split('\n')
1513
- .map(function (line) { return "> ".concat(line); })
1514
- .join('\n')), "\n "); }));
1515
- }
1516
- var resultingParameterName = match.groups.resultingParamName;
1517
- // TODO: [1] DRY description
1518
- var description_1 = section.content;
1519
- // Note: Remove codeblocks
1520
- description_1 = description_1.split(/^```.*^```/gms).join('');
1521
- //Note: Remove lists and return statement
1522
- description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
1523
- description_1 = spaceTrim.spaceTrim(description_1);
1524
- if (description_1 === '') {
1525
- description_1 = undefined;
1526
- }
1527
- if (Object.keys(jokers).length === 0) {
1528
- jokers = undefined;
1529
- }
1530
- if (Object.keys(expectAmount).length === 0) {
1531
- expectAmount = undefined;
1532
- }
1533
- if (Object.keys(postprocessing).length === 0) {
1534
- postprocessing = undefined;
1535
- }
1536
- dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, executionType: executionType, content: content })));
1537
- if (templateModelRequirements.modelVariant === undefined) {
1538
- templateModelRequirements.modelVariant = 'CHAT';
1539
- }
1540
- promptbookJson.promptTemplates.push({
1541
- name: titleToName(section.title),
1542
- title: section.title,
1543
- description: description_1,
1544
- dependentParameterNames: Array.from(dependentParameterNames),
1545
- executionType: executionType,
1546
- jokers: jokers,
1547
- postprocessing: postprocessing,
1548
- expectations: expectAmount,
1549
- expectFormat: expectFormat,
1550
- modelRequirements: templateModelRequirements,
1551
- contentLanguage: executionType === 'SCRIPT' ? language : undefined,
1552
- content: content,
1553
- resultingParameterName: resultingParameterName,
1554
1595
  });
1555
- };
1556
- try {
1557
- for (var _c = __values(markdownStructure.sections), _d = _c.next(); !_d.done; _d = _c.next()) {
1558
- var section = _d.value;
1559
- _loop_1(section);
1560
- }
1561
- }
1562
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
1563
- finally {
1564
- try {
1565
- if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
1566
- }
1567
- finally { if (e_2) throw e_2.error; }
1568
- }
1569
- // =============================================================
1570
- return promptbookJson;
1596
+ });
1571
1597
  }
1572
1598
  /**
1573
1599
  * TODO: Report here line/column of error
@@ -2176,36 +2202,61 @@
2176
2202
  * @returns PromptbookLibrary
2177
2203
  */
2178
2204
  function createPromptbookLibraryFromSources() {
2179
- var e_1, _a;
2180
2205
  var promptbookSources = [];
2181
2206
  for (var _i = 0; _i < arguments.length; _i++) {
2182
2207
  promptbookSources[_i] = arguments[_i];
2183
2208
  }
2184
- var promptbooks = new Array();
2185
- try {
2186
- for (var promptbookSources_1 = __values(promptbookSources), promptbookSources_1_1 = promptbookSources_1.next(); !promptbookSources_1_1.done; promptbookSources_1_1 = promptbookSources_1.next()) {
2187
- var source = promptbookSources_1_1.value;
2188
- var promptbook = void 0;
2189
- if (typeof source === 'string') {
2190
- // Note: When directly creating from string, no need to validate the source
2191
- // The validation is performed always before execution
2192
- promptbook = promptbookStringToJson(source);
2193
- }
2194
- else {
2195
- promptbook = source;
2209
+ return __awaiter(this, void 0, void 0, function () {
2210
+ var promptbooks, promptbookSources_1, promptbookSources_1_1, source, promptbook, e_1_1;
2211
+ var e_1, _a;
2212
+ return __generator(this, function (_b) {
2213
+ switch (_b.label) {
2214
+ case 0:
2215
+ promptbooks = new Array();
2216
+ _b.label = 1;
2217
+ case 1:
2218
+ _b.trys.push([1, 8, 9, 10]);
2219
+ promptbookSources_1 = __values(promptbookSources), promptbookSources_1_1 = promptbookSources_1.next();
2220
+ _b.label = 2;
2221
+ case 2:
2222
+ if (!!promptbookSources_1_1.done) return [3 /*break*/, 7];
2223
+ source = promptbookSources_1_1.value;
2224
+ promptbook = void 0;
2225
+ if (!(typeof source === 'string')) return [3 /*break*/, 4];
2226
+ return [4 /*yield*/, promptbookStringToJson(source)];
2227
+ case 3:
2228
+ // Note: When directly creating from string, no need to validate the source
2229
+ // The validation is performed always before execution
2230
+ promptbook = _b.sent();
2231
+ return [3 /*break*/, 5];
2232
+ case 4:
2233
+ promptbook = source;
2234
+ _b.label = 5;
2235
+ case 5:
2236
+ promptbooks.push(promptbook);
2237
+ _b.label = 6;
2238
+ case 6:
2239
+ promptbookSources_1_1 = promptbookSources_1.next();
2240
+ return [3 /*break*/, 2];
2241
+ case 7: return [3 /*break*/, 10];
2242
+ case 8:
2243
+ e_1_1 = _b.sent();
2244
+ e_1 = { error: e_1_1 };
2245
+ return [3 /*break*/, 10];
2246
+ case 9:
2247
+ try {
2248
+ if (promptbookSources_1_1 && !promptbookSources_1_1.done && (_a = promptbookSources_1.return)) _a.call(promptbookSources_1);
2249
+ }
2250
+ finally { if (e_1) throw e_1.error; }
2251
+ return [7 /*endfinally*/];
2252
+ case 10: return [2 /*return*/, new (SimplePromptbookLibrary.bind.apply(SimplePromptbookLibrary, __spreadArray([void 0], __read(promptbooks), false)))()];
2196
2253
  }
2197
- promptbooks.push(promptbook);
2198
- }
2199
- }
2200
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
2201
- finally {
2202
- try {
2203
- if (promptbookSources_1_1 && !promptbookSources_1_1.done && (_a = promptbookSources_1.return)) _a.call(promptbookSources_1);
2204
- }
2205
- finally { if (e_1) throw e_1.error; }
2206
- }
2207
- return new (SimplePromptbookLibrary.bind.apply(SimplePromptbookLibrary, __spreadArray([void 0], __read(promptbooks), false)))();
2254
+ });
2255
+ });
2208
2256
  }
2257
+ /**
2258
+ * TODO: !!!! [🧠] Library precompilation and do not mix markdown and json promptbooks
2259
+ */
2209
2260
 
2210
2261
  /**
2211
2262
  * Constructs Promptbook from async sources
@@ -2241,7 +2292,9 @@
2241
2292
  return [4 /*yield*/, promptbookSourcesPromiseOrFactory];
2242
2293
  case 1:
2243
2294
  promptbookSources = _a.sent();
2244
- library = createPromptbookLibraryFromSources.apply(void 0, __spreadArray([], __read(promptbookSources), false));
2295
+ return [4 /*yield*/, createPromptbookLibraryFromSources.apply(void 0, __spreadArray([], __read(promptbookSources), false))];
2296
+ case 2:
2297
+ library = _a.sent();
2245
2298
  return [2 /*return*/];
2246
2299
  }
2247
2300
  });
@@ -2289,6 +2342,9 @@
2289
2342
  isResponsibleForPrompt: isResponsibleForPrompt,
2290
2343
  };
2291
2344
  }
2345
+ /**
2346
+ * TODO: !!!! [🧠] Library precompilation and do not mix markdown and json promptbooks
2347
+ */
2292
2348
 
2293
2349
  /**
2294
2350
  * Constructs Promptbook from given directory
@@ -2331,31 +2387,33 @@
2331
2387
  return __generator(this, function (_e) {
2332
2388
  switch (_e.label) {
2333
2389
  case 0:
2334
- _e.trys.push([0, 6, , 7]);
2390
+ _e.trys.push([0, 7, , 8]);
2335
2391
  promptbook = null;
2336
- if (!fileName.endsWith('.ptbk.md')) return [3 /*break*/, 2];
2392
+ if (!fileName.endsWith('.ptbk.md')) return [3 /*break*/, 3];
2337
2393
  return [4 /*yield*/, promises.readFile(fileName, 'utf8')];
2338
2394
  case 1:
2339
2395
  promptbookString = (_e.sent());
2340
- promptbook = promptbookStringToJson(promptbookString);
2341
- return [3 /*break*/, 5];
2396
+ return [4 /*yield*/, promptbookStringToJson(promptbookString)];
2342
2397
  case 2:
2343
- if (!fileName.endsWith('.ptbk.json')) return [3 /*break*/, 4];
2398
+ promptbook = _e.sent();
2399
+ return [3 /*break*/, 6];
2400
+ case 3:
2401
+ if (!fileName.endsWith('.ptbk.json')) return [3 /*break*/, 5];
2344
2402
  if (isVerbose) {
2345
2403
  console.info("Loading ".concat(fileName));
2346
2404
  }
2347
2405
  _d = (_c = JSON).parse;
2348
2406
  return [4 /*yield*/, promises.readFile(fileName, 'utf8')];
2349
- case 3:
2407
+ case 4:
2350
2408
  // TODO: Handle non-valid JSON files
2351
2409
  promptbook = _d.apply(_c, [_e.sent()]);
2352
- return [3 /*break*/, 5];
2353
- case 4:
2410
+ return [3 /*break*/, 6];
2411
+ case 5:
2354
2412
  if (isVerbose) {
2355
2413
  console.info("Skipping file ".concat(fileName));
2356
2414
  }
2357
- _e.label = 5;
2358
- case 5:
2415
+ _e.label = 6;
2416
+ case 6:
2359
2417
  // ---
2360
2418
  if (promptbook !== null) {
2361
2419
  if (!promptbook.promptbookUrl) {
@@ -2376,8 +2434,8 @@
2376
2434
  promptbooks.push(promptbook);
2377
2435
  }
2378
2436
  }
2379
- return [3 /*break*/, 7];
2380
- case 6:
2437
+ return [3 /*break*/, 8];
2438
+ case 7:
2381
2439
  error_1 = _e.sent();
2382
2440
  if (!(error_1 instanceof Error)) {
2383
2441
  throw error_1;
@@ -2387,8 +2445,8 @@
2387
2445
  throw new PromptbookLibraryError(wrappedErrorMessage);
2388
2446
  }
2389
2447
  console.error(wrappedErrorMessage);
2390
- return [3 /*break*/, 7];
2391
- case 7: return [2 /*return*/];
2448
+ return [3 /*break*/, 8];
2449
+ case 8: return [2 /*return*/];
2392
2450
  }
2393
2451
  });
2394
2452
  };
@@ -2491,6 +2549,9 @@
2491
2549
  });
2492
2550
  });
2493
2551
  }
2552
+ /**
2553
+ * TODO: !!!! [🧠] Library precompilation and do not mix markdown and json promptbooks
2554
+ */
2494
2555
 
2495
2556
  exports.PROMPTBOOK_VERSION = PROMPTBOOK_VERSION;
2496
2557
  exports.createPromptbookLibraryFromDirectory = createPromptbookLibraryFromDirectory;