@promptbook/utils 0.52.0 → 0.53.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/esm/index.es.js +727 -729
  2. package/esm/index.es.js.map +1 -1
  3. package/esm/typings/conversion/prettify/prettifyPromptbookString.d.ts +1 -1
  4. package/esm/typings/conversion/prettify/renderPromptbookMermaid.d.ts +2 -1
  5. package/esm/typings/conversion/utils/extractParametersFromPromptTemplate.d.ts +3 -3
  6. package/esm/typings/conversion/utils/extractVariables.d.ts +2 -1
  7. package/esm/typings/conversion/validation/_importPromptbook.d.ts +2 -2
  8. package/esm/typings/execution/LlmExecutionTools.d.ts +4 -2
  9. package/esm/typings/execution/PromptResult.d.ts +5 -1
  10. package/esm/typings/execution/ScriptExecutionTools.d.ts +2 -1
  11. package/esm/typings/execution/UserInterfaceTools.d.ts +2 -1
  12. package/esm/typings/execution/createPromptbookExecutor.d.ts +3 -3
  13. package/esm/typings/execution/plugins/llm-execution-tools/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +4 -2
  14. package/esm/typings/execution/plugins/llm-execution-tools/anthropic-claude/anthropic-claude-models.d.ts +1 -1
  15. package/esm/typings/execution/plugins/llm-execution-tools/azure-openai/AzureOpenAiExecutionTools.d.ts +4 -2
  16. package/esm/typings/execution/plugins/llm-execution-tools/azure-openai/AzureOpenAiExecutionToolsOptions.d.ts +2 -1
  17. package/esm/typings/execution/plugins/llm-execution-tools/mocked/MockedEchoLlmExecutionTools.d.ts +4 -2
  18. package/esm/typings/execution/plugins/llm-execution-tools/mocked/MockedFackedLlmExecutionTools.d.ts +4 -2
  19. package/esm/typings/execution/plugins/llm-execution-tools/mocked/fakeTextToExpectations.d.ts +1 -1
  20. package/esm/typings/execution/plugins/llm-execution-tools/multiple/MultipleLlmExecutionTools.d.ts +4 -2
  21. package/esm/typings/execution/plugins/llm-execution-tools/multiple/MultipleLlmExecutionToolsOptions.d.ts +2 -1
  22. package/esm/typings/execution/plugins/llm-execution-tools/openai/OpenAiExecutionTools.d.ts +4 -2
  23. package/esm/typings/execution/plugins/llm-execution-tools/openai/OpenAiExecutionToolsOptions.d.ts +1 -1
  24. package/esm/typings/execution/plugins/llm-execution-tools/openai/openai-models.d.ts +1 -1
  25. package/esm/typings/execution/plugins/llm-execution-tools/remote/RemoteLlmExecutionTools.d.ts +4 -2
  26. package/esm/typings/execution/plugins/llm-execution-tools/remote/RemoteLlmExecutionToolsOptions.d.ts +2 -1
  27. package/esm/typings/execution/plugins/llm-execution-tools/remote/interfaces/Promptbook_Server_Progress.d.ts +1 -1
  28. package/esm/typings/execution/plugins/llm-execution-tools/remote/interfaces/Promptbook_Server_Response.d.ts +1 -1
  29. package/esm/typings/execution/plugins/llm-execution-tools/remote/interfaces/RemoteServerOptions.d.ts +2 -1
  30. package/esm/typings/execution/plugins/llm-execution-tools/remote/startRemoteServer.d.ts +1 -1
  31. package/esm/typings/execution/plugins/script-execution-tools/python/PythonExecutionTools.d.ts +3 -2
  32. package/esm/typings/execution/plugins/script-execution-tools/typescript/TypescriptExecutionTools.d.ts +3 -2
  33. package/esm/typings/execution/plugins/user-interface-execution-tools/callback/CallbackInterfaceTools.d.ts +3 -2
  34. package/esm/typings/execution/plugins/user-interface-execution-tools/simple-prompt/SimplePromptInterfaceTools.d.ts +3 -2
  35. package/esm/typings/execution/translation/automatic-translate/automatic-translators/DebugAutomaticTranslator.d.ts +1 -1
  36. package/esm/typings/execution/translation/automatic-translate/automatic-translators/FakeAutomaticTranslator.d.ts +1 -1
  37. package/esm/typings/execution/translation/automatic-translate/automatic-translators/LindatAutomaticTranslator.d.ts +2 -2
  38. package/esm/typings/execution/translation/automatic-translate/translateMessages.d.ts +2 -2
  39. package/esm/typings/execution/utils/replaceParameters.d.ts +2 -2
  40. package/esm/typings/library/SimplePromptbookLibrary.d.ts +1 -1
  41. package/esm/typings/library/constructors/createPromptbookLibraryFromDirectory.d.ts +2 -2
  42. package/esm/typings/library/constructors/createPromptbookLibraryFromPromise.d.ts +2 -2
  43. package/esm/typings/library/constructors/createPromptbookLibraryFromSources.d.ts +2 -1
  44. package/esm/typings/library/constructors/createPromptbookLibraryFromUrl.d.ts +2 -2
  45. package/esm/typings/library/constructors/createPromptbookSublibrary.d.ts +1 -1
  46. package/esm/typings/types/Command.d.ts +5 -2
  47. package/esm/typings/types/Prompt.d.ts +4 -1
  48. package/esm/typings/types/PromptbookJson/PromptTemplateJson.d.ts +8 -1
  49. package/esm/typings/types/PromptbookJson/PromptbookJson.d.ts +2 -1
  50. package/esm/typings/types/TaskProgress.d.ts +2 -1
  51. package/esm/typings/types/execution-report/ExecutionReportJson.d.ts +2 -1
  52. package/esm/typings/types/execution-report/countWorkingDuration.d.ts +1 -1
  53. package/esm/typings/utils/emojis.d.ts +1 -1
  54. package/esm/typings/utils/expectation-counters/index.d.ts +2 -1
  55. package/esm/typings/utils/extractParameters.d.ts +2 -1
  56. package/esm/typings/utils/markdown/addAutoGeneratedSection.d.ts +2 -1
  57. package/esm/typings/utils/markdown/createMarkdownChart.d.ts +2 -2
  58. package/esm/typings/utils/markdown/createMarkdownTable.d.ts +2 -1
  59. package/esm/typings/utils/markdown/escapeMarkdownBlock.d.ts +1 -1
  60. package/esm/typings/utils/markdown/extractAllListItemsFromMarkdown.d.ts +2 -1
  61. package/esm/typings/utils/markdown/extractOneBlockFromMarkdown.d.ts +2 -2
  62. package/esm/typings/utils/markdown/removeContentComments.d.ts +2 -1
  63. package/esm/typings/utils/markdown/removeMarkdownFormatting.d.ts +1 -1
  64. package/esm/typings/utils/markdown-json/countMarkdownStructureDeepness.d.ts +1 -1
  65. package/esm/typings/utils/markdown-json/markdownToMarkdownStructure.d.ts +1 -1
  66. package/esm/typings/utils/normalization/isValidKeyword.d.ts +1 -1
  67. package/esm/typings/utils/normalization/parseKeywords.d.ts +1 -1
  68. package/esm/typings/utils/normalization/parseKeywordsFromString.d.ts +1 -1
  69. package/esm/typings/utils/normalization/searchKeywords.d.ts +1 -1
  70. package/esm/typings/utils/postprocessing/extractBlock.d.ts +1 -1
  71. package/package.json +1 -1
  72. package/umd/index.umd.js +727 -729
  73. package/umd/index.umd.js.map +1 -1
  74. package/umd/typings/conversion/prettify/prettifyPromptbookString.d.ts +1 -1
  75. package/umd/typings/conversion/prettify/renderPromptbookMermaid.d.ts +2 -1
  76. package/umd/typings/conversion/utils/extractParametersFromPromptTemplate.d.ts +3 -3
  77. package/umd/typings/conversion/utils/extractVariables.d.ts +2 -1
  78. package/umd/typings/conversion/validation/_importPromptbook.d.ts +2 -2
  79. package/umd/typings/execution/LlmExecutionTools.d.ts +4 -2
  80. package/umd/typings/execution/PromptResult.d.ts +5 -1
  81. package/umd/typings/execution/ScriptExecutionTools.d.ts +2 -1
  82. package/umd/typings/execution/UserInterfaceTools.d.ts +2 -1
  83. package/umd/typings/execution/createPromptbookExecutor.d.ts +3 -3
  84. package/umd/typings/execution/plugins/llm-execution-tools/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +4 -2
  85. package/umd/typings/execution/plugins/llm-execution-tools/anthropic-claude/anthropic-claude-models.d.ts +1 -1
  86. package/umd/typings/execution/plugins/llm-execution-tools/azure-openai/AzureOpenAiExecutionTools.d.ts +4 -2
  87. package/umd/typings/execution/plugins/llm-execution-tools/azure-openai/AzureOpenAiExecutionToolsOptions.d.ts +2 -1
  88. package/umd/typings/execution/plugins/llm-execution-tools/mocked/MockedEchoLlmExecutionTools.d.ts +4 -2
  89. package/umd/typings/execution/plugins/llm-execution-tools/mocked/MockedFackedLlmExecutionTools.d.ts +4 -2
  90. package/umd/typings/execution/plugins/llm-execution-tools/mocked/fakeTextToExpectations.d.ts +1 -1
  91. package/umd/typings/execution/plugins/llm-execution-tools/multiple/MultipleLlmExecutionTools.d.ts +4 -2
  92. package/umd/typings/execution/plugins/llm-execution-tools/multiple/MultipleLlmExecutionToolsOptions.d.ts +2 -1
  93. package/umd/typings/execution/plugins/llm-execution-tools/openai/OpenAiExecutionTools.d.ts +4 -2
  94. package/umd/typings/execution/plugins/llm-execution-tools/openai/OpenAiExecutionToolsOptions.d.ts +1 -1
  95. package/umd/typings/execution/plugins/llm-execution-tools/openai/openai-models.d.ts +1 -1
  96. package/umd/typings/execution/plugins/llm-execution-tools/remote/RemoteLlmExecutionTools.d.ts +4 -2
  97. package/umd/typings/execution/plugins/llm-execution-tools/remote/RemoteLlmExecutionToolsOptions.d.ts +2 -1
  98. package/umd/typings/execution/plugins/llm-execution-tools/remote/interfaces/Promptbook_Server_Progress.d.ts +1 -1
  99. package/umd/typings/execution/plugins/llm-execution-tools/remote/interfaces/Promptbook_Server_Response.d.ts +1 -1
  100. package/umd/typings/execution/plugins/llm-execution-tools/remote/interfaces/RemoteServerOptions.d.ts +2 -1
  101. package/umd/typings/execution/plugins/llm-execution-tools/remote/startRemoteServer.d.ts +1 -1
  102. package/umd/typings/execution/plugins/script-execution-tools/python/PythonExecutionTools.d.ts +3 -2
  103. package/umd/typings/execution/plugins/script-execution-tools/typescript/TypescriptExecutionTools.d.ts +3 -2
  104. package/umd/typings/execution/plugins/user-interface-execution-tools/callback/CallbackInterfaceTools.d.ts +3 -2
  105. package/umd/typings/execution/plugins/user-interface-execution-tools/simple-prompt/SimplePromptInterfaceTools.d.ts +3 -2
  106. package/umd/typings/execution/translation/automatic-translate/automatic-translators/DebugAutomaticTranslator.d.ts +1 -1
  107. package/umd/typings/execution/translation/automatic-translate/automatic-translators/FakeAutomaticTranslator.d.ts +1 -1
  108. package/umd/typings/execution/translation/automatic-translate/automatic-translators/LindatAutomaticTranslator.d.ts +2 -2
  109. package/umd/typings/execution/translation/automatic-translate/translateMessages.d.ts +2 -2
  110. package/umd/typings/execution/utils/replaceParameters.d.ts +2 -2
  111. package/umd/typings/library/SimplePromptbookLibrary.d.ts +1 -1
  112. package/umd/typings/library/constructors/createPromptbookLibraryFromDirectory.d.ts +2 -2
  113. package/umd/typings/library/constructors/createPromptbookLibraryFromPromise.d.ts +2 -2
  114. package/umd/typings/library/constructors/createPromptbookLibraryFromSources.d.ts +2 -1
  115. package/umd/typings/library/constructors/createPromptbookLibraryFromUrl.d.ts +2 -2
  116. package/umd/typings/library/constructors/createPromptbookSublibrary.d.ts +1 -1
  117. package/umd/typings/types/Command.d.ts +5 -2
  118. package/umd/typings/types/Prompt.d.ts +4 -1
  119. package/umd/typings/types/PromptbookJson/PromptTemplateJson.d.ts +8 -1
  120. package/umd/typings/types/PromptbookJson/PromptbookJson.d.ts +2 -1
  121. package/umd/typings/types/TaskProgress.d.ts +2 -1
  122. package/umd/typings/types/execution-report/ExecutionReportJson.d.ts +2 -1
  123. package/umd/typings/types/execution-report/countWorkingDuration.d.ts +1 -1
  124. package/umd/typings/utils/emojis.d.ts +1 -1
  125. package/umd/typings/utils/expectation-counters/index.d.ts +2 -1
  126. package/umd/typings/utils/extractParameters.d.ts +2 -1
  127. package/umd/typings/utils/markdown/addAutoGeneratedSection.d.ts +2 -1
  128. package/umd/typings/utils/markdown/createMarkdownChart.d.ts +2 -2
  129. package/umd/typings/utils/markdown/createMarkdownTable.d.ts +2 -1
  130. package/umd/typings/utils/markdown/escapeMarkdownBlock.d.ts +1 -1
  131. package/umd/typings/utils/markdown/extractAllListItemsFromMarkdown.d.ts +2 -1
  132. package/umd/typings/utils/markdown/extractOneBlockFromMarkdown.d.ts +2 -2
  133. package/umd/typings/utils/markdown/removeContentComments.d.ts +2 -1
  134. package/umd/typings/utils/markdown/removeMarkdownFormatting.d.ts +1 -1
  135. package/umd/typings/utils/markdown-json/countMarkdownStructureDeepness.d.ts +1 -1
  136. package/umd/typings/utils/markdown-json/markdownToMarkdownStructure.d.ts +1 -1
  137. package/umd/typings/utils/normalization/isValidKeyword.d.ts +1 -1
  138. package/umd/typings/utils/normalization/parseKeywords.d.ts +1 -1
  139. package/umd/typings/utils/normalization/parseKeywordsFromString.d.ts +1 -1
  140. package/umd/typings/utils/normalization/searchKeywords.d.ts +1 -1
  141. package/umd/typings/utils/postprocessing/extractBlock.d.ts +1 -1
package/esm/index.es.js CHANGED
@@ -120,242 +120,604 @@ function __spreadArray(to, from, pack) {
120
120
  return to.concat(ar || Array.prototype.slice.call(from));
121
121
  }
122
122
 
123
- /**
124
- * This error type indicates that the error should not happen and its last check before crashing with some other error
125
- */
126
- var UnexpectedError = /** @class */ (function (_super) {
127
- __extends(UnexpectedError, _super);
128
- function UnexpectedError(message) {
129
- 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;
130
- _this.name = 'UnexpectedError';
131
- Object.setPrototypeOf(_this, UnexpectedError.prototype);
132
- return _this;
133
- }
134
- return UnexpectedError;
135
- }(Error));
136
-
137
- /**
138
- * Removes emojis from a string and fix whitespaces
139
- *
140
- * @param text with emojis
141
- * @returns text without emojis
142
- */
143
- function removeEmojis(text) {
144
- // Replace emojis (and also ZWJ sequence) with hyphens
145
- text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
146
- text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
147
- text = text.replace(/(\p{Extended_Pictographic})(\u{200D}\p{Extended_Pictographic})*/gu, '$1');
148
- text = text.replace(/\p{Extended_Pictographic}/gu, '');
149
- return text;
150
- }
151
-
152
- /**
153
- * Function normalizes title to name which can be used as identifier
154
- */
155
- function titleToName(value) {
156
- value = removeEmojis(value);
157
- value = normalizeToKebabCase(value);
158
- // TODO: [🧠] Maybe warn or add some padding to short name which are not good identifiers
159
- return value;
160
- }
161
-
162
- /**
163
- * Creates a Mermaid graph based on the promptbook
164
- *
165
- * Note: The result is not wrapped in a Markdown code block
166
- */
167
- function renderPromptbookMermaid(promptbookJson, options) {
168
- var _a = (options || {}).linkPromptTemplate, linkPromptTemplate = _a === void 0 ? function () { return null; } : _a;
169
- var parameterNameToTemplateName = function (parameterName) {
170
- var parameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
171
- if (!parameter) {
172
- throw new UnexpectedError("Could not find {".concat(parameterName, "}"));
173
- }
174
- if (parameter.isInput) {
175
- return 'input';
176
- }
177
- var template = promptbookJson.promptTemplates.find(function (template) { return template.resultingParameterName === parameterName; });
178
- if (!template) {
179
- throw new Error("Could not find template for {".concat(parameterName, "}"));
180
- }
181
- return normalizeTo_camelCase('template-' + titleToName(template.title));
182
- };
183
- 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
184
- .flatMap(function (_a) {
185
- var title = _a.title, dependentParameterNames = _a.dependentParameterNames, resultingParameterName = _a.resultingParameterName;
186
- return __spreadArray([
187
- "".concat(parameterNameToTemplateName(resultingParameterName), "(\"").concat(title, "\")")
188
- ], __read(dependentParameterNames.map(function (dependentParameterName) {
189
- return "".concat(parameterNameToTemplateName(dependentParameterName), "--\"{").concat(dependentParameterName, "}\"-->").concat(parameterNameToTemplateName(resultingParameterName));
190
- })), false);
191
- })
192
- .join('\n')), "\n\n ").concat(block(promptbookJson.parameters
193
- .filter(function (_a) {
194
- var isOutput = _a.isOutput;
195
- return isOutput;
196
- })
197
- .map(function (_a) {
198
- var name = _a.name;
199
- return "".concat(parameterNameToTemplateName(name), "--\"{").concat(name, "}\"-->output");
200
- })
201
- .join('\n')), "\n output((Output)):::output\n\n ").concat(block(promptbookJson.promptTemplates
202
- .map(function (promptTemplate) {
203
- var link = linkPromptTemplate(promptTemplate);
204
- if (link === null) {
205
- return '';
206
- }
207
- var href = link.href, title = link.title;
208
- var templateName = parameterNameToTemplateName(promptTemplate.resultingParameterName);
209
- return "click ".concat(templateName, " href \"").concat(href, "\" \"").concat(title, "\";");
210
- })
211
- .filter(function (line) { return line !== ''; })
212
- .join('\n')), "\n\n classDef input color: grey;\n classDef output color: grey;\n\n end;\n\n "); });
213
- return promptbookMermaid;
214
- }
215
- /**
216
- * TODO: Maybe use some Mermaid library instead of string templating
217
- * TODO: [🕌] When more than 2 functionalities, split into separate functions
218
- */
219
-
220
- /**
221
- * Parses the template and returns the list of all parameter names
222
- *
223
- * @param template the template with parameters in {curly} braces
224
- * @returns the list of parameter names
225
- */
226
- function extractParameters(template) {
123
+ /* tslint:disable */
124
+ function normalizeTo_camelCase(sentence, __firstLetterCapital) {
227
125
  var e_1, _a;
228
- var matches = template.matchAll(/{\w+}/g);
229
- var parameterNames = new Set();
230
- try {
231
- for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
232
- var match = matches_1_1.value;
233
- var parameterName = match[0].slice(1, -1);
234
- parameterNames.add(parameterName);
235
- }
236
- }
237
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
238
- finally {
239
- try {
240
- if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
241
- }
242
- finally { if (e_1) throw e_1.error; }
243
- }
244
- return parameterNames;
245
- }
246
-
247
- /**
248
- * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
249
- */
250
- var PromptbookSyntaxError = /** @class */ (function (_super) {
251
- __extends(PromptbookSyntaxError, _super);
252
- function PromptbookSyntaxError(message) {
253
- var _this = _super.call(this, message) || this;
254
- _this.name = 'PromptbookSyntaxError';
255
- Object.setPrototypeOf(_this, PromptbookSyntaxError.prototype);
256
- return _this;
257
- }
258
- return PromptbookSyntaxError;
259
- }(Error));
260
-
261
- /**
262
- * Parses the given script and returns the list of all used variables that are not defined in the script
263
- *
264
- * @param script from which to extract the variables
265
- * @returns the list of variable names
266
- * @throws {PromptbookSyntaxError} if the script is invalid
267
- */
268
- function extractVariables(script) {
269
- var variables = new Set();
270
- script = "(()=>{".concat(script, "})()");
126
+ if (__firstLetterCapital === void 0) { __firstLetterCapital = false; }
127
+ var charType;
128
+ var lastCharType = null;
129
+ var normalizedName = '';
271
130
  try {
272
- for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
273
- try {
274
- eval(script);
131
+ for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
132
+ var char = sentence_1_1.value;
133
+ var normalizedChar = void 0;
134
+ if (/^[a-z]$/.test(char)) {
135
+ charType = 'LOWERCASE';
136
+ normalizedChar = char;
275
137
  }
276
- catch (error) {
277
- if (!(error instanceof ReferenceError)) {
278
- throw error;
279
- }
280
- var undefinedName = error.message.split(' ')[0];
281
- /*
282
- Note: Remapping error
283
- From: [ReferenceError: thing is not defined],
284
- To: [Error: Parameter {thing} is not defined],
285
- */
286
- if (!undefinedName) {
287
- throw error;
288
- }
289
- if (script.includes(undefinedName + '(')) {
290
- script = "const ".concat(undefinedName, " = ()=>'';") + script;
291
- }
292
- else {
293
- variables.add(undefinedName);
294
- script = "const ".concat(undefinedName, " = '';") + script;
138
+ else if (/^[A-Z]$/.test(char)) {
139
+ charType = 'UPPERCASE';
140
+ normalizedChar = char.toLowerCase();
141
+ }
142
+ else if (/^[0-9]$/.test(char)) {
143
+ charType = 'NUMBER';
144
+ normalizedChar = char;
145
+ }
146
+ else {
147
+ charType = 'OTHER';
148
+ normalizedChar = '';
149
+ }
150
+ if (!lastCharType) {
151
+ if (__firstLetterCapital) {
152
+ normalizedChar = normalizedChar.toUpperCase(); //TODO: DRY
295
153
  }
296
154
  }
297
- }
298
- catch (error) {
299
- if (!(error instanceof Error)) {
300
- throw error;
301
- }
302
- 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 "); }));
303
- }
304
- return variables;
305
- }
306
- /**
307
- * TODO: [🔣] Support for multiple languages - python, java,...
308
- */
309
-
310
- /**
311
- * Parses the prompt template and returns the set of all used parameters
312
- *
313
- * @param promptTemplate the template with used parameters
314
- * @returns the set of parameter names
315
- * @throws {PromptbookSyntaxError} if the script is invalid
316
- */
317
- function extractParametersFromPromptTemplate(promptTemplate) {
318
- var e_1, _a, e_2, _b;
319
- var parameterNames = new Set();
320
- try {
321
- for (var _c = __values(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameters(promptTemplate.title)), false), __read(extractParameters(promptTemplate.description || '')), false), __read(extractParameters(promptTemplate.content)), false)), _d = _c.next(); !_d.done; _d = _c.next()) {
322
- var parameterName = _d.value;
323
- parameterNames.add(parameterName);
155
+ else if (charType !== lastCharType &&
156
+ !(charType === 'LOWERCASE' && lastCharType === 'UPPERCASE') &&
157
+ !(lastCharType === 'NUMBER') &&
158
+ !(charType === 'NUMBER')) {
159
+ normalizedChar = normalizedChar.toUpperCase(); //TODO: [🌺] DRY
160
+ }
161
+ normalizedName += normalizedChar;
162
+ lastCharType = charType;
324
163
  }
325
164
  }
326
165
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
327
166
  finally {
328
167
  try {
329
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
168
+ if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
330
169
  }
331
170
  finally { if (e_1) throw e_1.error; }
332
171
  }
333
- if (promptTemplate.executionType === 'SCRIPT') {
334
- try {
335
- for (var _e = __values(extractVariables(promptTemplate.content)), _f = _e.next(); !_f.done; _f = _e.next()) {
336
- var parameterName = _f.value;
337
- parameterNames.add(parameterName);
338
- }
339
- }
340
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
341
- finally {
342
- try {
343
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
344
- }
345
- finally { if (e_2) throw e_2.error; }
346
- }
347
- }
348
- return parameterNames;
172
+ return normalizedName;
349
173
  }
350
174
  /**
351
- * TODO: [🔣] If script require contentLanguage
175
+ * TODO: [🌺] Use some intermediate util splitWords
352
176
  */
353
177
 
354
178
  /**
355
- * Function parseNumber will parse number from string
356
- *
357
- * Unlike Number.parseInt, Number.parseFloat it will never ever result in NaN
358
- * Note: it also works only with decimal numbers
179
+ * This error type indicates that the error should not happen and its last check before crashing with some other error
180
+ */
181
+ var UnexpectedError = /** @class */ (function (_super) {
182
+ __extends(UnexpectedError, _super);
183
+ function UnexpectedError(message) {
184
+ 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;
185
+ _this.name = 'UnexpectedError';
186
+ Object.setPrototypeOf(_this, UnexpectedError.prototype);
187
+ return _this;
188
+ }
189
+ return UnexpectedError;
190
+ }(Error));
191
+
192
+ var defaultDiacriticsRemovalMap = [
193
+ {
194
+ base: 'A',
195
+ letters: '\u0041\u24B6\uFF21\u00C0\u00C1\u00C2\u1EA6\u1EA4\u1EAA\u1EA8\u00C3\u0100\u0102\u1EB0\u1EAE\u1EB4\u1EB2\u0226\u01E0\u00C4\u01DE\u1EA2\u00C5\u01FA\u01CD\u0200\u0202\u1EA0\u1EAC\u1EB6\u1E00\u0104\u023A\u2C6F',
196
+ },
197
+ { base: 'AA', letters: '\uA732' },
198
+ { base: 'AE', letters: '\u00C6\u01FC\u01E2' },
199
+ { base: 'AO', letters: '\uA734' },
200
+ { base: 'AU', letters: '\uA736' },
201
+ { base: 'AV', letters: '\uA738\uA73A' },
202
+ { base: 'AY', letters: '\uA73C' },
203
+ {
204
+ base: 'B',
205
+ letters: '\u0042\u24B7\uFF22\u1E02\u1E04\u1E06\u0243\u0182\u0181',
206
+ },
207
+ {
208
+ base: 'C',
209
+ letters: '\u0043\u24B8\uFF23\u0106\u0108\u010A\u010C\u00C7\u1E08\u0187\u023B\uA73E',
210
+ },
211
+ {
212
+ base: 'D',
213
+ letters: '\u0044\u24B9\uFF24\u1E0A\u010E\u1E0C\u1E10\u1E12\u1E0E\u0110\u018B\u018A\u0189\uA779\u00D0',
214
+ },
215
+ { base: 'DZ', letters: '\u01F1\u01C4' },
216
+ { base: 'Dz', letters: '\u01F2\u01C5' },
217
+ {
218
+ base: 'E',
219
+ letters: '\u0045\u24BA\uFF25\u00C8\u00C9\u00CA\u1EC0\u1EBE\u1EC4\u1EC2\u1EBC\u0112\u1E14\u1E16\u0114\u0116\u00CB\u1EBA\u011A\u0204\u0206\u1EB8\u1EC6\u0228\u1E1C\u0118\u1E18\u1E1A\u0190\u018E',
220
+ },
221
+ { base: 'F', letters: '\u0046\u24BB\uFF26\u1E1E\u0191\uA77B' },
222
+ {
223
+ base: 'G',
224
+ letters: '\u0047\u24BC\uFF27\u01F4\u011C\u1E20\u011E\u0120\u01E6\u0122\u01E4\u0193\uA7A0\uA77D\uA77E',
225
+ },
226
+ {
227
+ base: 'H',
228
+ letters: '\u0048\u24BD\uFF28\u0124\u1E22\u1E26\u021E\u1E24\u1E28\u1E2A\u0126\u2C67\u2C75\uA78D',
229
+ },
230
+ {
231
+ base: 'I',
232
+ letters: '\u0049\u24BE\uFF29\u00CC\u00CD\u00CE\u0128\u012A\u012C\u0130\u00CF\u1E2E\u1EC8\u01CF\u0208\u020A\u1ECA\u012E\u1E2C\u0197',
233
+ },
234
+ { base: 'J', letters: '\u004A\u24BF\uFF2A\u0134\u0248' },
235
+ {
236
+ base: 'K',
237
+ letters: '\u004B\u24C0\uFF2B\u1E30\u01E8\u1E32\u0136\u1E34\u0198\u2C69\uA740\uA742\uA744\uA7A2',
238
+ },
239
+ {
240
+ base: 'L',
241
+ letters: '\u004C\u24C1\uFF2C\u013F\u0139\u013D\u1E36\u1E38\u013B\u1E3C\u1E3A\u0141\u023D\u2C62\u2C60\uA748\uA746\uA780',
242
+ },
243
+ { base: 'LJ', letters: '\u01C7' },
244
+ { base: 'Lj', letters: '\u01C8' },
245
+ { base: 'M', letters: '\u004D\u24C2\uFF2D\u1E3E\u1E40\u1E42\u2C6E\u019C' },
246
+ {
247
+ base: 'N',
248
+ letters: '\u004E\u24C3\uFF2E\u01F8\u0143\u00D1\u1E44\u0147\u1E46\u0145\u1E4A\u1E48\u0220\u019D\uA790\uA7A4',
249
+ },
250
+ { base: 'NJ', letters: '\u01CA' },
251
+ { base: 'Nj', letters: '\u01CB' },
252
+ {
253
+ base: 'O',
254
+ letters: '\u004F\u24C4\uFF2F\u00D2\u00D3\u00D4\u1ED2\u1ED0\u1ED6\u1ED4\u00D5\u1E4C\u022C\u1E4E\u014C\u1E50\u1E52\u014E\u022E\u0230\u00D6\u022A\u1ECE\u0150\u01D1\u020C\u020E\u01A0\u1EDC\u1EDA\u1EE0\u1EDE\u1EE2\u1ECC\u1ED8\u01EA\u01EC\u00D8\u01FE\u0186\u019F\uA74A\uA74C',
255
+ },
256
+ { base: 'OI', letters: '\u01A2' },
257
+ { base: 'OO', letters: '\uA74E' },
258
+ { base: 'OU', letters: '\u0222' },
259
+ { base: 'OE', letters: '\u008C\u0152' },
260
+ { base: 'oe', letters: '\u009C\u0153' },
261
+ {
262
+ base: 'P',
263
+ letters: '\u0050\u24C5\uFF30\u1E54\u1E56\u01A4\u2C63\uA750\uA752\uA754',
264
+ },
265
+ { base: 'Q', letters: '\u0051\u24C6\uFF31\uA756\uA758\u024A' },
266
+ {
267
+ base: 'R',
268
+ letters: '\u0052\u24C7\uFF32\u0154\u1E58\u0158\u0210\u0212\u1E5A\u1E5C\u0156\u1E5E\u024C\u2C64\uA75A\uA7A6\uA782',
269
+ },
270
+ {
271
+ base: 'S',
272
+ letters: '\u0053\u24C8\uFF33\u1E9E\u015A\u1E64\u015C\u1E60\u0160\u1E66\u1E62\u1E68\u0218\u015E\u2C7E\uA7A8\uA784',
273
+ },
274
+ {
275
+ base: 'T',
276
+ letters: '\u0054\u24C9\uFF34\u1E6A\u0164\u1E6C\u021A\u0162\u1E70\u1E6E\u0166\u01AC\u01AE\u023E\uA786',
277
+ },
278
+ { base: 'TZ', letters: '\uA728' },
279
+ {
280
+ base: 'U',
281
+ letters: '\u0055\u24CA\uFF35\u00D9\u00DA\u00DB\u0168\u1E78\u016A\u1E7A\u016C\u00DC\u01DB\u01D7\u01D5\u01D9\u1EE6\u016E\u0170\u01D3\u0214\u0216\u01AF\u1EEA\u1EE8\u1EEE\u1EEC\u1EF0\u1EE4\u1E72\u0172\u1E76\u1E74\u0244',
282
+ },
283
+ { base: 'V', letters: '\u0056\u24CB\uFF36\u1E7C\u1E7E\u01B2\uA75E\u0245' },
284
+ { base: 'VY', letters: '\uA760' },
285
+ {
286
+ base: 'W',
287
+ letters: '\u0057\u24CC\uFF37\u1E80\u1E82\u0174\u1E86\u1E84\u1E88\u2C72',
288
+ },
289
+ { base: 'X', letters: '\u0058\u24CD\uFF38\u1E8A\u1E8C' },
290
+ {
291
+ base: 'Y',
292
+ letters: '\u0059\u24CE\uFF39\u1EF2\u00DD\u0176\u1EF8\u0232\u1E8E\u0178\u1EF6\u1EF4\u01B3\u024E\u1EFE',
293
+ },
294
+ {
295
+ base: 'Z',
296
+ letters: '\u005A\u24CF\uFF3A\u0179\u1E90\u017B\u017D\u1E92\u1E94\u01B5\u0224\u2C7F\u2C6B\uA762',
297
+ },
298
+ {
299
+ base: 'a',
300
+ letters: '\u0061\u24D0\uFF41\u1E9A\u00E0\u00E1\u00E2\u1EA7\u1EA5\u1EAB\u1EA9\u00E3\u0101\u0103\u1EB1\u1EAF\u1EB5\u1EB3\u0227\u01E1\u00E4\u01DF\u1EA3\u00E5\u01FB\u01CE\u0201\u0203\u1EA1\u1EAD\u1EB7\u1E01\u0105\u2C65\u0250',
301
+ },
302
+ { base: 'aa', letters: '\uA733' },
303
+ { base: 'ae', letters: '\u00E6\u01FD\u01E3' },
304
+ { base: 'ao', letters: '\uA735' },
305
+ { base: 'au', letters: '\uA737' },
306
+ { base: 'av', letters: '\uA739\uA73B' },
307
+ { base: 'ay', letters: '\uA73D' },
308
+ {
309
+ base: 'b',
310
+ letters: '\u0062\u24D1\uFF42\u1E03\u1E05\u1E07\u0180\u0183\u0253',
311
+ },
312
+ {
313
+ base: 'c',
314
+ letters: '\u0063\u24D2\uFF43\u0107\u0109\u010B\u010D\u00E7\u1E09\u0188\u023C\uA73F\u2184',
315
+ },
316
+ {
317
+ base: 'd',
318
+ letters: '\u0064\u24D3\uFF44\u1E0B\u010F\u1E0D\u1E11\u1E13\u1E0F\u0111\u018C\u0256\u0257\uA77A',
319
+ },
320
+ { base: 'dz', letters: '\u01F3\u01C6' },
321
+ {
322
+ base: 'e',
323
+ letters: '\u0065\u24D4\uFF45\u00E8\u00E9\u00EA\u1EC1\u1EBF\u1EC5\u1EC3\u1EBD\u0113\u1E15\u1E17\u0115\u0117\u00EB\u1EBB\u011B\u0205\u0207\u1EB9\u1EC7\u0229\u1E1D\u0119\u1E19\u1E1B\u0247\u025B\u01DD',
324
+ },
325
+ { base: 'f', letters: '\u0066\u24D5\uFF46\u1E1F\u0192\uA77C' },
326
+ {
327
+ base: 'g',
328
+ letters: '\u0067\u24D6\uFF47\u01F5\u011D\u1E21\u011F\u0121\u01E7\u0123\u01E5\u0260\uA7A1\u1D79\uA77F',
329
+ },
330
+ {
331
+ base: 'h',
332
+ letters: '\u0068\u24D7\uFF48\u0125\u1E23\u1E27\u021F\u1E25\u1E29\u1E2B\u1E96\u0127\u2C68\u2C76\u0265',
333
+ },
334
+ { base: 'hv', letters: '\u0195' },
335
+ {
336
+ base: 'i',
337
+ letters: '\u0069\u24D8\uFF49\u00EC\u00ED\u00EE\u0129\u012B\u012D\u00EF\u1E2F\u1EC9\u01D0\u0209\u020B\u1ECB\u012F\u1E2D\u0268\u0131',
338
+ },
339
+ { base: 'j', letters: '\u006A\u24D9\uFF4A\u0135\u01F0\u0249' },
340
+ {
341
+ base: 'k',
342
+ letters: '\u006B\u24DA\uFF4B\u1E31\u01E9\u1E33\u0137\u1E35\u0199\u2C6A\uA741\uA743\uA745\uA7A3',
343
+ },
344
+ {
345
+ base: 'l',
346
+ letters: '\u006C\u24DB\uFF4C\u0140\u013A\u013E\u1E37\u1E39\u013C\u1E3D\u1E3B\u017F\u0142\u019A\u026B\u2C61\uA749\uA781\uA747',
347
+ },
348
+ { base: 'lj', letters: '\u01C9' },
349
+ { base: 'm', letters: '\u006D\u24DC\uFF4D\u1E3F\u1E41\u1E43\u0271\u026F' },
350
+ {
351
+ base: 'n',
352
+ letters: '\u006E\u24DD\uFF4E\u01F9\u0144\u00F1\u1E45\u0148\u1E47\u0146\u1E4B\u1E49\u019E\u0272\u0149\uA791\uA7A5',
353
+ },
354
+ { base: 'nj', letters: '\u01CC' },
355
+ {
356
+ base: 'o',
357
+ letters: '\u006F\u24DE\uFF4F\u00F2\u00F3\u00F4\u1ED3\u1ED1\u1ED7\u1ED5\u00F5\u1E4D\u022D\u1E4F\u014D\u1E51\u1E53\u014F\u022F\u0231\u00F6\u022B\u1ECF\u0151\u01D2\u020D\u020F\u01A1\u1EDD\u1EDB\u1EE1\u1EDF\u1EE3\u1ECD\u1ED9\u01EB\u01ED\u00F8\u01FF\u0254\uA74B\uA74D\u0275',
358
+ },
359
+ { base: 'oi', letters: '\u01A3' },
360
+ { base: 'ou', letters: '\u0223' },
361
+ { base: 'oo', letters: '\uA74F' },
362
+ {
363
+ base: 'p',
364
+ letters: '\u0070\u24DF\uFF50\u1E55\u1E57\u01A5\u1D7D\uA751\uA753\uA755',
365
+ },
366
+ { base: 'q', letters: '\u0071\u24E0\uFF51\u024B\uA757\uA759' },
367
+ {
368
+ base: 'r',
369
+ letters: '\u0072\u24E1\uFF52\u0155\u1E59\u0159\u0211\u0213\u1E5B\u1E5D\u0157\u1E5F\u024D\u027D\uA75B\uA7A7\uA783',
370
+ },
371
+ {
372
+ base: 's',
373
+ letters: '\u0073\u24E2\uFF53\u00DF\u015B\u1E65\u015D\u1E61\u0161\u1E67\u1E63\u1E69\u0219\u015F\u023F\uA7A9\uA785\u1E9B',
374
+ },
375
+ {
376
+ base: 't',
377
+ letters: '\u0074\u24E3\uFF54\u1E6B\u1E97\u0165\u1E6D\u021B\u0163\u1E71\u1E6F\u0167\u01AD\u0288\u2C66\uA787',
378
+ },
379
+ { base: 'tz', letters: '\uA729' },
380
+ {
381
+ base: 'u',
382
+ letters: '\u0075\u24E4\uFF55\u00F9\u00FA\u00FB\u0169\u1E79\u016B\u1E7B\u016D\u00FC\u01DC\u01D8\u01D6\u01DA\u1EE7\u016F\u0171\u01D4\u0215\u0217\u01B0\u1EEB\u1EE9\u1EEF\u1EED\u1EF1\u1EE5\u1E73\u0173\u1E77\u1E75\u0289',
383
+ },
384
+ { base: 'v', letters: '\u0076\u24E5\uFF56\u1E7D\u1E7F\u028B\uA75F\u028C' },
385
+ { base: 'vy', letters: '\uA761' },
386
+ {
387
+ base: 'w',
388
+ letters: '\u0077\u24E6\uFF57\u1E81\u1E83\u0175\u1E87\u1E85\u1E98\u1E89\u2C73',
389
+ },
390
+ { base: 'x', letters: '\u0078\u24E7\uFF58\u1E8B\u1E8D' },
391
+ {
392
+ base: 'y',
393
+ letters: '\u0079\u24E8\uFF59\u1EF3\u00FD\u0177\u1EF9\u0233\u1E8F\u00FF\u1EF7\u1E99\u1EF5\u01B4\u024F\u1EFF',
394
+ },
395
+ {
396
+ base: 'z',
397
+ letters: '\u007A\u24E9\uFF5A\u017A\u1E91\u017C\u017E\u1E93\u1E95\u01B6\u0225\u0240\u2C6C\uA763',
398
+ },
399
+ ];
400
+ /**
401
+ * Map of letters from diacritic variant to diacritless variant
402
+ * Contains lowercase and uppercase separatelly
403
+ *
404
+ * > "á" => "a"
405
+ * > "ě" => "e"
406
+ * > "Ă" => "A"
407
+ * > ...
408
+ */
409
+ var DIACRITIC_VARIANTS_LETTERS = {};
410
+ // tslint:disable-next-line: prefer-for-of
411
+ for (var i = 0; i < defaultDiacriticsRemovalMap.length; i++) {
412
+ var letters = defaultDiacriticsRemovalMap[i].letters;
413
+ // tslint:disable-next-line: prefer-for-of
414
+ for (var j = 0; j < letters.length; j++) {
415
+ DIACRITIC_VARIANTS_LETTERS[letters[j]] = defaultDiacriticsRemovalMap[i].base;
416
+ }
417
+ }
418
+ // <- TODO: [🍓] Put to maker function to save execution time if not needed
419
+ /*
420
+ @see https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
421
+ Licensed under the Apache License, Version 2.0 (the "License");
422
+ you may not use this file except in compliance with the License.
423
+ You may obtain a copy of the License at
424
+
425
+ http://www.apache.org/licenses/LICENSE-2.0
426
+
427
+ Unless required by applicable law or agreed to in writing, software
428
+ distributed under the License is distributed on an "AS IS" BASIS,
429
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
430
+ See the License for the specific language governing permissions and
431
+ limitations under the License.
432
+ */
433
+
434
+ /**
435
+ *
436
+ */
437
+ function removeDiacritics(input) {
438
+ /*eslint no-control-regex: "off"*/
439
+ return input.replace(/[^\u0000-\u007E]/g, function (a) {
440
+ return DIACRITIC_VARIANTS_LETTERS[a] || a;
441
+ });
442
+ }
443
+
444
+ /* tslint:disable */
445
+ function normalizeToKebabCase(sentence) {
446
+ var e_1, _a;
447
+ sentence = removeDiacritics(sentence);
448
+ var charType;
449
+ var lastCharType = 'OTHER';
450
+ var normalizedName = '';
451
+ try {
452
+ for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
453
+ var char = sentence_1_1.value;
454
+ var normalizedChar = void 0;
455
+ if (/^[a-z]$/.test(char)) {
456
+ charType = 'LOWERCASE';
457
+ normalizedChar = char;
458
+ }
459
+ else if (/^[A-Z]$/.test(char)) {
460
+ charType = 'UPPERCASE';
461
+ normalizedChar = char.toLowerCase();
462
+ }
463
+ else if (/^[0-9]$/.test(char)) {
464
+ charType = 'NUMBER';
465
+ normalizedChar = char;
466
+ }
467
+ else if (/^\/$/.test(char)) {
468
+ charType = 'SLASH';
469
+ normalizedChar = char;
470
+ }
471
+ else {
472
+ charType = 'OTHER';
473
+ normalizedChar = '-';
474
+ }
475
+ if (charType !== lastCharType &&
476
+ !(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
477
+ !(lastCharType === 'NUMBER') &&
478
+ !(charType === 'NUMBER')) {
479
+ normalizedName += '-';
480
+ }
481
+ normalizedName += normalizedChar;
482
+ lastCharType = charType;
483
+ }
484
+ }
485
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
486
+ finally {
487
+ try {
488
+ if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
489
+ }
490
+ finally { if (e_1) throw e_1.error; }
491
+ }
492
+ normalizedName = normalizedName.split(/-+/g).join('-');
493
+ normalizedName = normalizedName.split(/-?\/-?/g).join('/');
494
+ normalizedName = normalizedName.replace(/^-/, '');
495
+ normalizedName = normalizedName.replace(/-$/, '');
496
+ return normalizedName;
497
+ }
498
+
499
+ /**
500
+ * Removes emojis from a string and fix whitespaces
501
+ *
502
+ * @param text with emojis
503
+ * @returns text without emojis
504
+ */
505
+ function removeEmojis(text) {
506
+ // Replace emojis (and also ZWJ sequence) with hyphens
507
+ text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
508
+ text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
509
+ text = text.replace(/(\p{Extended_Pictographic})(\u{200D}\p{Extended_Pictographic})*/gu, '$1');
510
+ text = text.replace(/\p{Extended_Pictographic}/gu, '');
511
+ return text;
512
+ }
513
+
514
+ /**
515
+ * Function normalizes title to name which can be used as identifier
516
+ */
517
+ function titleToName(value) {
518
+ value = removeEmojis(value);
519
+ value = normalizeToKebabCase(value);
520
+ // TODO: [🧠] Maybe warn or add some padding to short name which are not good identifiers
521
+ return value;
522
+ }
523
+
524
+ /**
525
+ * Creates a Mermaid graph based on the promptbook
526
+ *
527
+ * Note: The result is not wrapped in a Markdown code block
528
+ */
529
+ function renderPromptbookMermaid(promptbookJson, options) {
530
+ var _a = (options || {}).linkPromptTemplate, linkPromptTemplate = _a === void 0 ? function () { return null; } : _a;
531
+ var parameterNameToTemplateName = function (parameterName) {
532
+ var parameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
533
+ if (!parameter) {
534
+ throw new UnexpectedError("Could not find {".concat(parameterName, "}"));
535
+ }
536
+ if (parameter.isInput) {
537
+ return 'input';
538
+ }
539
+ var template = promptbookJson.promptTemplates.find(function (template) { return template.resultingParameterName === parameterName; });
540
+ if (!template) {
541
+ throw new Error("Could not find template for {".concat(parameterName, "}"));
542
+ }
543
+ return normalizeTo_camelCase('template-' + titleToName(template.title));
544
+ };
545
+ 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
546
+ .flatMap(function (_a) {
547
+ var title = _a.title, dependentParameterNames = _a.dependentParameterNames, resultingParameterName = _a.resultingParameterName;
548
+ return __spreadArray([
549
+ "".concat(parameterNameToTemplateName(resultingParameterName), "(\"").concat(title, "\")")
550
+ ], __read(dependentParameterNames.map(function (dependentParameterName) {
551
+ return "".concat(parameterNameToTemplateName(dependentParameterName), "--\"{").concat(dependentParameterName, "}\"-->").concat(parameterNameToTemplateName(resultingParameterName));
552
+ })), false);
553
+ })
554
+ .join('\n')), "\n\n ").concat(block(promptbookJson.parameters
555
+ .filter(function (_a) {
556
+ var isOutput = _a.isOutput;
557
+ return isOutput;
558
+ })
559
+ .map(function (_a) {
560
+ var name = _a.name;
561
+ return "".concat(parameterNameToTemplateName(name), "--\"{").concat(name, "}\"-->output");
562
+ })
563
+ .join('\n')), "\n output((Output)):::output\n\n ").concat(block(promptbookJson.promptTemplates
564
+ .map(function (promptTemplate) {
565
+ var link = linkPromptTemplate(promptTemplate);
566
+ if (link === null) {
567
+ return '';
568
+ }
569
+ var href = link.href, title = link.title;
570
+ var templateName = parameterNameToTemplateName(promptTemplate.resultingParameterName);
571
+ return "click ".concat(templateName, " href \"").concat(href, "\" \"").concat(title, "\";");
572
+ })
573
+ .filter(function (line) { return line !== ''; })
574
+ .join('\n')), "\n\n classDef input color: grey;\n classDef output color: grey;\n\n end;\n\n "); });
575
+ return promptbookMermaid;
576
+ }
577
+ /**
578
+ * TODO: Maybe use some Mermaid library instead of string templating
579
+ * TODO: [🕌] When more than 2 functionalities, split into separate functions
580
+ */
581
+
582
+ /**
583
+ * Parses the template and returns the list of all parameter names
584
+ *
585
+ * @param template the template with parameters in {curly} braces
586
+ * @returns the list of parameter names
587
+ */
588
+ function extractParameters(template) {
589
+ var e_1, _a;
590
+ var matches = template.matchAll(/{\w+}/g);
591
+ var parameterNames = new Set();
592
+ try {
593
+ for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
594
+ var match = matches_1_1.value;
595
+ var parameterName = match[0].slice(1, -1);
596
+ parameterNames.add(parameterName);
597
+ }
598
+ }
599
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
600
+ finally {
601
+ try {
602
+ if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
603
+ }
604
+ finally { if (e_1) throw e_1.error; }
605
+ }
606
+ return parameterNames;
607
+ }
608
+
609
+ /**
610
+ * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
611
+ */
612
+ var PromptbookSyntaxError = /** @class */ (function (_super) {
613
+ __extends(PromptbookSyntaxError, _super);
614
+ function PromptbookSyntaxError(message) {
615
+ var _this = _super.call(this, message) || this;
616
+ _this.name = 'PromptbookSyntaxError';
617
+ Object.setPrototypeOf(_this, PromptbookSyntaxError.prototype);
618
+ return _this;
619
+ }
620
+ return PromptbookSyntaxError;
621
+ }(Error));
622
+
623
+ /**
624
+ * Parses the given script and returns the list of all used variables that are not defined in the script
625
+ *
626
+ * @param script from which to extract the variables
627
+ * @returns the list of variable names
628
+ * @throws {PromptbookSyntaxError} if the script is invalid
629
+ */
630
+ function extractVariables(script) {
631
+ var variables = new Set();
632
+ script = "(()=>{".concat(script, "})()");
633
+ try {
634
+ for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
635
+ try {
636
+ eval(script);
637
+ }
638
+ catch (error) {
639
+ if (!(error instanceof ReferenceError)) {
640
+ throw error;
641
+ }
642
+ var undefinedName = error.message.split(' ')[0];
643
+ /*
644
+ Note: Remapping error
645
+ From: [ReferenceError: thing is not defined],
646
+ To: [Error: Parameter {thing} is not defined],
647
+ */
648
+ if (!undefinedName) {
649
+ throw error;
650
+ }
651
+ if (script.includes(undefinedName + '(')) {
652
+ script = "const ".concat(undefinedName, " = ()=>'';") + script;
653
+ }
654
+ else {
655
+ variables.add(undefinedName);
656
+ script = "const ".concat(undefinedName, " = '';") + script;
657
+ }
658
+ }
659
+ }
660
+ catch (error) {
661
+ if (!(error instanceof Error)) {
662
+ throw error;
663
+ }
664
+ 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 "); }));
665
+ }
666
+ return variables;
667
+ }
668
+ /**
669
+ * TODO: [🔣] Support for multiple languages - python, java,...
670
+ */
671
+
672
+ /**
673
+ * Parses the prompt template and returns the set of all used parameters
674
+ *
675
+ * @param promptTemplate the template with used parameters
676
+ * @returns the set of parameter names
677
+ * @throws {PromptbookSyntaxError} if the script is invalid
678
+ */
679
+ function extractParametersFromPromptTemplate(promptTemplate) {
680
+ var e_1, _a, e_2, _b;
681
+ var parameterNames = new Set();
682
+ try {
683
+ for (var _c = __values(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameters(promptTemplate.title)), false), __read(extractParameters(promptTemplate.description || '')), false), __read(extractParameters(promptTemplate.content)), false)), _d = _c.next(); !_d.done; _d = _c.next()) {
684
+ var parameterName = _d.value;
685
+ parameterNames.add(parameterName);
686
+ }
687
+ }
688
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
689
+ finally {
690
+ try {
691
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
692
+ }
693
+ finally { if (e_1) throw e_1.error; }
694
+ }
695
+ if (promptTemplate.executionType === 'SCRIPT') {
696
+ try {
697
+ for (var _e = __values(extractVariables(promptTemplate.content)), _f = _e.next(); !_f.done; _f = _e.next()) {
698
+ var parameterName = _f.value;
699
+ parameterNames.add(parameterName);
700
+ }
701
+ }
702
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
703
+ finally {
704
+ try {
705
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
706
+ }
707
+ finally { if (e_2) throw e_2.error; }
708
+ }
709
+ }
710
+ return parameterNames;
711
+ }
712
+ /**
713
+ * TODO: [🔣] If script require contentLanguage
714
+ */
715
+
716
+ /**
717
+ * Function parseNumber will parse number from string
718
+ *
719
+ * Unlike Number.parseInt, Number.parseFloat it will never ever result in NaN
720
+ * Note: it also works only with decimal numbers
359
721
  *
360
722
  * @returns parsed number
361
723
  * @throws {PromptbookSyntaxError} if the value is not a number
@@ -532,420 +894,168 @@ function forEachAsync(array, options, callbackfunction) {
532
894
  _e.trys.push([1, 6, 7, 8]);
533
895
  _b = __values(array), _c = _b.next();
534
896
  _e.label = 2;
535
- case 2:
536
- if (!!_c.done) return [3 /*break*/, 5];
537
- item = _c.value;
538
- return [5 /*yield**/, _loop_1(item)];
539
- case 3:
540
- _e.sent();
541
- _e.label = 4;
542
- case 4:
543
- _c = _b.next();
544
- return [3 /*break*/, 2];
545
- case 5: return [3 /*break*/, 8];
546
- case 6:
547
- e_1_1 = _e.sent();
548
- e_1 = { error: e_1_1 };
549
- return [3 /*break*/, 8];
550
- case 7:
551
- try {
552
- if (_c && !_c.done && (_d = _b.return)) _d.call(_b);
553
- }
554
- finally { if (e_1) throw e_1.error; }
555
- return [7 /*endfinally*/];
556
- case 8: return [4 /*yield*/, Promise.all(tasks)];
557
- case 9:
558
- _e.sent();
559
- return [2 /*return*/];
560
- }
561
- });
562
- });
563
- }
564
-
565
- /**
566
- * The maximum number of iterations for a loops
567
- */
568
- var LOOP_LIMIT = 1000;
569
-
570
- /**
571
- * This error occurs during the parameter replacement in the template
572
- *
573
- * Note: This is a kindof subtype of PromptbookExecutionError because it occurs during the execution of the pipeline
574
- */
575
- var TemplateError = /** @class */ (function (_super) {
576
- __extends(TemplateError, _super);
577
- function TemplateError(message) {
578
- var _this = _super.call(this, message) || this;
579
- _this.name = 'TemplateError';
580
- Object.setPrototypeOf(_this, TemplateError.prototype);
581
- return _this;
582
- }
583
- return TemplateError;
584
- }(Error));
585
-
586
- /**
587
- * Replaces parameters in template with values from parameters object
588
- *
589
- * @param template the template with parameters in {curly} braces
590
- * @param parameters the object with parameters
591
- * @returns the template with replaced parameters
592
- * @throws {TemplateError} if parameter is not defined, not closed, or not opened
593
- *
594
- * @private within the createPromptbookExecutor
595
- */
596
- function replaceParameters(template, parameters) {
597
- var replacedTemplate = template;
598
- var match;
599
- var loopLimit = LOOP_LIMIT;
600
- var _loop_1 = function () {
601
- if (loopLimit-- < 0) {
602
- throw new UnexpectedError('Loop limit reached during parameters replacement in `replaceParameters`');
603
- }
604
- var precol = match.groups.precol;
605
- var parameterName = match.groups.parameterName;
606
- if (parameterName === '') {
607
- return "continue";
608
- }
609
- if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
610
- throw new TemplateError('Parameter is already opened or not closed');
611
- }
612
- if (parameters[parameterName] === undefined) {
613
- throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
614
- }
615
- var parameterValue = parameters[parameterName];
616
- if (parameterValue === undefined) {
617
- throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
618
- }
619
- parameterValue = parameterValue.toString();
620
- if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
621
- parameterValue = parameterValue
622
- .split('\n')
623
- .map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
624
- .join('\n');
625
- }
626
- replacedTemplate =
627
- replacedTemplate.substring(0, match.index + precol.length) +
628
- parameterValue +
629
- replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
630
- };
631
- while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
632
- .exec(replacedTemplate))) {
633
- _loop_1();
634
- }
635
- // [💫] Check if there are parameters that are not closed properly
636
- if (/{\w+$/.test(replacedTemplate)) {
637
- throw new TemplateError('Parameter is not closed');
638
- }
639
- // [💫] Check if there are parameters that are not opened properly
640
- if (/^\w+}/.test(replacedTemplate)) {
641
- throw new TemplateError('Parameter is not opened');
642
- }
643
- return replacedTemplate;
644
- }
645
-
646
- /**
647
- * Counts number of characters in the text
648
- */
649
- function countCharacters(text) {
650
- // Remove null characters
651
- text = text.replace(/\0/g, '');
652
- // Replace emojis (and also ZWJ sequence) with hyphens
653
- text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
654
- text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
655
- text = text.replace(/\p{Extended_Pictographic}(\u{200D}\p{Extended_Pictographic})*/gu, '-');
656
- return text.length;
657
- }
658
-
659
- /**
660
- * Counts number of lines in the text
661
- */
662
- function countLines(text) {
663
- if (text === '') {
664
- return 0;
665
- }
666
- return text.split('\n').length;
667
- }
668
-
669
- /**
670
- * Counts number of pages in the text
671
- */
672
- function countPages(text) {
673
- var sentencesPerPage = 5; // Assuming each page has 5 sentences
674
- var sentences = text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
675
- var pageCount = Math.ceil(sentences.length / sentencesPerPage);
676
- return pageCount;
897
+ case 2:
898
+ if (!!_c.done) return [3 /*break*/, 5];
899
+ item = _c.value;
900
+ return [5 /*yield**/, _loop_1(item)];
901
+ case 3:
902
+ _e.sent();
903
+ _e.label = 4;
904
+ case 4:
905
+ _c = _b.next();
906
+ return [3 /*break*/, 2];
907
+ case 5: return [3 /*break*/, 8];
908
+ case 6:
909
+ e_1_1 = _e.sent();
910
+ e_1 = { error: e_1_1 };
911
+ return [3 /*break*/, 8];
912
+ case 7:
913
+ try {
914
+ if (_c && !_c.done && (_d = _b.return)) _d.call(_b);
915
+ }
916
+ finally { if (e_1) throw e_1.error; }
917
+ return [7 /*endfinally*/];
918
+ case 8: return [4 /*yield*/, Promise.all(tasks)];
919
+ case 9:
920
+ _e.sent();
921
+ return [2 /*return*/];
922
+ }
923
+ });
924
+ });
677
925
  }
678
926
 
679
927
  /**
680
- * Counts number of paragraphs in the text
928
+ * The maximum number of iterations for a loops
681
929
  */
682
- function countParagraphs(text) {
683
- return text.split(/\n\s*\n/).filter(function (paragraph) { return paragraph.trim() !== ''; }).length;
684
- }
930
+ var LOOP_LIMIT = 1000;
685
931
 
686
932
  /**
687
- * Split text into sentences
688
- */
689
- function splitIntoSentences(text) {
690
- return text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
691
- }
692
- /**
693
- * Counts number of sentences in the text
694
- */
695
- function countSentences(text) {
696
- return splitIntoSentences(text).length;
697
- }
698
-
699
- var defaultDiacriticsRemovalMap = [
700
- {
701
- base: 'A',
702
- letters: '\u0041\u24B6\uFF21\u00C0\u00C1\u00C2\u1EA6\u1EA4\u1EAA\u1EA8\u00C3\u0100\u0102\u1EB0\u1EAE\u1EB4\u1EB2\u0226\u01E0\u00C4\u01DE\u1EA2\u00C5\u01FA\u01CD\u0200\u0202\u1EA0\u1EAC\u1EB6\u1E00\u0104\u023A\u2C6F',
703
- },
704
- { base: 'AA', letters: '\uA732' },
705
- { base: 'AE', letters: '\u00C6\u01FC\u01E2' },
706
- { base: 'AO', letters: '\uA734' },
707
- { base: 'AU', letters: '\uA736' },
708
- { base: 'AV', letters: '\uA738\uA73A' },
709
- { base: 'AY', letters: '\uA73C' },
710
- {
711
- base: 'B',
712
- letters: '\u0042\u24B7\uFF22\u1E02\u1E04\u1E06\u0243\u0182\u0181',
713
- },
714
- {
715
- base: 'C',
716
- letters: '\u0043\u24B8\uFF23\u0106\u0108\u010A\u010C\u00C7\u1E08\u0187\u023B\uA73E',
717
- },
718
- {
719
- base: 'D',
720
- letters: '\u0044\u24B9\uFF24\u1E0A\u010E\u1E0C\u1E10\u1E12\u1E0E\u0110\u018B\u018A\u0189\uA779\u00D0',
721
- },
722
- { base: 'DZ', letters: '\u01F1\u01C4' },
723
- { base: 'Dz', letters: '\u01F2\u01C5' },
724
- {
725
- base: 'E',
726
- letters: '\u0045\u24BA\uFF25\u00C8\u00C9\u00CA\u1EC0\u1EBE\u1EC4\u1EC2\u1EBC\u0112\u1E14\u1E16\u0114\u0116\u00CB\u1EBA\u011A\u0204\u0206\u1EB8\u1EC6\u0228\u1E1C\u0118\u1E18\u1E1A\u0190\u018E',
727
- },
728
- { base: 'F', letters: '\u0046\u24BB\uFF26\u1E1E\u0191\uA77B' },
729
- {
730
- base: 'G',
731
- letters: '\u0047\u24BC\uFF27\u01F4\u011C\u1E20\u011E\u0120\u01E6\u0122\u01E4\u0193\uA7A0\uA77D\uA77E',
732
- },
733
- {
734
- base: 'H',
735
- letters: '\u0048\u24BD\uFF28\u0124\u1E22\u1E26\u021E\u1E24\u1E28\u1E2A\u0126\u2C67\u2C75\uA78D',
736
- },
737
- {
738
- base: 'I',
739
- letters: '\u0049\u24BE\uFF29\u00CC\u00CD\u00CE\u0128\u012A\u012C\u0130\u00CF\u1E2E\u1EC8\u01CF\u0208\u020A\u1ECA\u012E\u1E2C\u0197',
740
- },
741
- { base: 'J', letters: '\u004A\u24BF\uFF2A\u0134\u0248' },
742
- {
743
- base: 'K',
744
- letters: '\u004B\u24C0\uFF2B\u1E30\u01E8\u1E32\u0136\u1E34\u0198\u2C69\uA740\uA742\uA744\uA7A2',
745
- },
746
- {
747
- base: 'L',
748
- letters: '\u004C\u24C1\uFF2C\u013F\u0139\u013D\u1E36\u1E38\u013B\u1E3C\u1E3A\u0141\u023D\u2C62\u2C60\uA748\uA746\uA780',
749
- },
750
- { base: 'LJ', letters: '\u01C7' },
751
- { base: 'Lj', letters: '\u01C8' },
752
- { base: 'M', letters: '\u004D\u24C2\uFF2D\u1E3E\u1E40\u1E42\u2C6E\u019C' },
753
- {
754
- base: 'N',
755
- letters: '\u004E\u24C3\uFF2E\u01F8\u0143\u00D1\u1E44\u0147\u1E46\u0145\u1E4A\u1E48\u0220\u019D\uA790\uA7A4',
756
- },
757
- { base: 'NJ', letters: '\u01CA' },
758
- { base: 'Nj', letters: '\u01CB' },
759
- {
760
- base: 'O',
761
- letters: '\u004F\u24C4\uFF2F\u00D2\u00D3\u00D4\u1ED2\u1ED0\u1ED6\u1ED4\u00D5\u1E4C\u022C\u1E4E\u014C\u1E50\u1E52\u014E\u022E\u0230\u00D6\u022A\u1ECE\u0150\u01D1\u020C\u020E\u01A0\u1EDC\u1EDA\u1EE0\u1EDE\u1EE2\u1ECC\u1ED8\u01EA\u01EC\u00D8\u01FE\u0186\u019F\uA74A\uA74C',
762
- },
763
- { base: 'OI', letters: '\u01A2' },
764
- { base: 'OO', letters: '\uA74E' },
765
- { base: 'OU', letters: '\u0222' },
766
- { base: 'OE', letters: '\u008C\u0152' },
767
- { base: 'oe', letters: '\u009C\u0153' },
768
- {
769
- base: 'P',
770
- letters: '\u0050\u24C5\uFF30\u1E54\u1E56\u01A4\u2C63\uA750\uA752\uA754',
771
- },
772
- { base: 'Q', letters: '\u0051\u24C6\uFF31\uA756\uA758\u024A' },
773
- {
774
- base: 'R',
775
- letters: '\u0052\u24C7\uFF32\u0154\u1E58\u0158\u0210\u0212\u1E5A\u1E5C\u0156\u1E5E\u024C\u2C64\uA75A\uA7A6\uA782',
776
- },
777
- {
778
- base: 'S',
779
- letters: '\u0053\u24C8\uFF33\u1E9E\u015A\u1E64\u015C\u1E60\u0160\u1E66\u1E62\u1E68\u0218\u015E\u2C7E\uA7A8\uA784',
780
- },
781
- {
782
- base: 'T',
783
- letters: '\u0054\u24C9\uFF34\u1E6A\u0164\u1E6C\u021A\u0162\u1E70\u1E6E\u0166\u01AC\u01AE\u023E\uA786',
784
- },
785
- { base: 'TZ', letters: '\uA728' },
786
- {
787
- base: 'U',
788
- letters: '\u0055\u24CA\uFF35\u00D9\u00DA\u00DB\u0168\u1E78\u016A\u1E7A\u016C\u00DC\u01DB\u01D7\u01D5\u01D9\u1EE6\u016E\u0170\u01D3\u0214\u0216\u01AF\u1EEA\u1EE8\u1EEE\u1EEC\u1EF0\u1EE4\u1E72\u0172\u1E76\u1E74\u0244',
789
- },
790
- { base: 'V', letters: '\u0056\u24CB\uFF36\u1E7C\u1E7E\u01B2\uA75E\u0245' },
791
- { base: 'VY', letters: '\uA760' },
792
- {
793
- base: 'W',
794
- letters: '\u0057\u24CC\uFF37\u1E80\u1E82\u0174\u1E86\u1E84\u1E88\u2C72',
795
- },
796
- { base: 'X', letters: '\u0058\u24CD\uFF38\u1E8A\u1E8C' },
797
- {
798
- base: 'Y',
799
- letters: '\u0059\u24CE\uFF39\u1EF2\u00DD\u0176\u1EF8\u0232\u1E8E\u0178\u1EF6\u1EF4\u01B3\u024E\u1EFE',
800
- },
801
- {
802
- base: 'Z',
803
- letters: '\u005A\u24CF\uFF3A\u0179\u1E90\u017B\u017D\u1E92\u1E94\u01B5\u0224\u2C7F\u2C6B\uA762',
804
- },
805
- {
806
- base: 'a',
807
- letters: '\u0061\u24D0\uFF41\u1E9A\u00E0\u00E1\u00E2\u1EA7\u1EA5\u1EAB\u1EA9\u00E3\u0101\u0103\u1EB1\u1EAF\u1EB5\u1EB3\u0227\u01E1\u00E4\u01DF\u1EA3\u00E5\u01FB\u01CE\u0201\u0203\u1EA1\u1EAD\u1EB7\u1E01\u0105\u2C65\u0250',
808
- },
809
- { base: 'aa', letters: '\uA733' },
810
- { base: 'ae', letters: '\u00E6\u01FD\u01E3' },
811
- { base: 'ao', letters: '\uA735' },
812
- { base: 'au', letters: '\uA737' },
813
- { base: 'av', letters: '\uA739\uA73B' },
814
- { base: 'ay', letters: '\uA73D' },
815
- {
816
- base: 'b',
817
- letters: '\u0062\u24D1\uFF42\u1E03\u1E05\u1E07\u0180\u0183\u0253',
818
- },
819
- {
820
- base: 'c',
821
- letters: '\u0063\u24D2\uFF43\u0107\u0109\u010B\u010D\u00E7\u1E09\u0188\u023C\uA73F\u2184',
822
- },
823
- {
824
- base: 'd',
825
- letters: '\u0064\u24D3\uFF44\u1E0B\u010F\u1E0D\u1E11\u1E13\u1E0F\u0111\u018C\u0256\u0257\uA77A',
826
- },
827
- { base: 'dz', letters: '\u01F3\u01C6' },
828
- {
829
- base: 'e',
830
- letters: '\u0065\u24D4\uFF45\u00E8\u00E9\u00EA\u1EC1\u1EBF\u1EC5\u1EC3\u1EBD\u0113\u1E15\u1E17\u0115\u0117\u00EB\u1EBB\u011B\u0205\u0207\u1EB9\u1EC7\u0229\u1E1D\u0119\u1E19\u1E1B\u0247\u025B\u01DD',
831
- },
832
- { base: 'f', letters: '\u0066\u24D5\uFF46\u1E1F\u0192\uA77C' },
833
- {
834
- base: 'g',
835
- letters: '\u0067\u24D6\uFF47\u01F5\u011D\u1E21\u011F\u0121\u01E7\u0123\u01E5\u0260\uA7A1\u1D79\uA77F',
836
- },
837
- {
838
- base: 'h',
839
- letters: '\u0068\u24D7\uFF48\u0125\u1E23\u1E27\u021F\u1E25\u1E29\u1E2B\u1E96\u0127\u2C68\u2C76\u0265',
840
- },
841
- { base: 'hv', letters: '\u0195' },
842
- {
843
- base: 'i',
844
- letters: '\u0069\u24D8\uFF49\u00EC\u00ED\u00EE\u0129\u012B\u012D\u00EF\u1E2F\u1EC9\u01D0\u0209\u020B\u1ECB\u012F\u1E2D\u0268\u0131',
845
- },
846
- { base: 'j', letters: '\u006A\u24D9\uFF4A\u0135\u01F0\u0249' },
847
- {
848
- base: 'k',
849
- letters: '\u006B\u24DA\uFF4B\u1E31\u01E9\u1E33\u0137\u1E35\u0199\u2C6A\uA741\uA743\uA745\uA7A3',
850
- },
851
- {
852
- base: 'l',
853
- letters: '\u006C\u24DB\uFF4C\u0140\u013A\u013E\u1E37\u1E39\u013C\u1E3D\u1E3B\u017F\u0142\u019A\u026B\u2C61\uA749\uA781\uA747',
854
- },
855
- { base: 'lj', letters: '\u01C9' },
856
- { base: 'm', letters: '\u006D\u24DC\uFF4D\u1E3F\u1E41\u1E43\u0271\u026F' },
857
- {
858
- base: 'n',
859
- letters: '\u006E\u24DD\uFF4E\u01F9\u0144\u00F1\u1E45\u0148\u1E47\u0146\u1E4B\u1E49\u019E\u0272\u0149\uA791\uA7A5',
860
- },
861
- { base: 'nj', letters: '\u01CC' },
862
- {
863
- base: 'o',
864
- letters: '\u006F\u24DE\uFF4F\u00F2\u00F3\u00F4\u1ED3\u1ED1\u1ED7\u1ED5\u00F5\u1E4D\u022D\u1E4F\u014D\u1E51\u1E53\u014F\u022F\u0231\u00F6\u022B\u1ECF\u0151\u01D2\u020D\u020F\u01A1\u1EDD\u1EDB\u1EE1\u1EDF\u1EE3\u1ECD\u1ED9\u01EB\u01ED\u00F8\u01FF\u0254\uA74B\uA74D\u0275',
865
- },
866
- { base: 'oi', letters: '\u01A3' },
867
- { base: 'ou', letters: '\u0223' },
868
- { base: 'oo', letters: '\uA74F' },
869
- {
870
- base: 'p',
871
- letters: '\u0070\u24DF\uFF50\u1E55\u1E57\u01A5\u1D7D\uA751\uA753\uA755',
872
- },
873
- { base: 'q', letters: '\u0071\u24E0\uFF51\u024B\uA757\uA759' },
874
- {
875
- base: 'r',
876
- letters: '\u0072\u24E1\uFF52\u0155\u1E59\u0159\u0211\u0213\u1E5B\u1E5D\u0157\u1E5F\u024D\u027D\uA75B\uA7A7\uA783',
877
- },
878
- {
879
- base: 's',
880
- letters: '\u0073\u24E2\uFF53\u00DF\u015B\u1E65\u015D\u1E61\u0161\u1E67\u1E63\u1E69\u0219\u015F\u023F\uA7A9\uA785\u1E9B',
881
- },
882
- {
883
- base: 't',
884
- letters: '\u0074\u24E3\uFF54\u1E6B\u1E97\u0165\u1E6D\u021B\u0163\u1E71\u1E6F\u0167\u01AD\u0288\u2C66\uA787',
885
- },
886
- { base: 'tz', letters: '\uA729' },
887
- {
888
- base: 'u',
889
- letters: '\u0075\u24E4\uFF55\u00F9\u00FA\u00FB\u0169\u1E79\u016B\u1E7B\u016D\u00FC\u01DC\u01D8\u01D6\u01DA\u1EE7\u016F\u0171\u01D4\u0215\u0217\u01B0\u1EEB\u1EE9\u1EEF\u1EED\u1EF1\u1EE5\u1E73\u0173\u1E77\u1E75\u0289',
890
- },
891
- { base: 'v', letters: '\u0076\u24E5\uFF56\u1E7D\u1E7F\u028B\uA75F\u028C' },
892
- { base: 'vy', letters: '\uA761' },
893
- {
894
- base: 'w',
895
- letters: '\u0077\u24E6\uFF57\u1E81\u1E83\u0175\u1E87\u1E85\u1E98\u1E89\u2C73',
896
- },
897
- { base: 'x', letters: '\u0078\u24E7\uFF58\u1E8B\u1E8D' },
898
- {
899
- base: 'y',
900
- letters: '\u0079\u24E8\uFF59\u1EF3\u00FD\u0177\u1EF9\u0233\u1E8F\u00FF\u1EF7\u1E99\u1EF5\u01B4\u024F\u1EFF',
901
- },
902
- {
903
- base: 'z',
904
- letters: '\u007A\u24E9\uFF5A\u017A\u1E91\u017C\u017E\u1E93\u1E95\u01B6\u0225\u0240\u2C6C\uA763',
905
- },
906
- ];
933
+ * This error occurs during the parameter replacement in the template
934
+ *
935
+ * Note: This is a kindof subtype of PromptbookExecutionError because it occurs during the execution of the pipeline
936
+ */
937
+ var TemplateError = /** @class */ (function (_super) {
938
+ __extends(TemplateError, _super);
939
+ function TemplateError(message) {
940
+ var _this = _super.call(this, message) || this;
941
+ _this.name = 'TemplateError';
942
+ Object.setPrototypeOf(_this, TemplateError.prototype);
943
+ return _this;
944
+ }
945
+ return TemplateError;
946
+ }(Error));
947
+
907
948
  /**
908
- * Map of letters from diacritic variant to diacritless variant
909
- * Contains lowercase and uppercase separatelly
949
+ * Replaces parameters in template with values from parameters object
910
950
  *
911
- * > "á" => "a"
912
- * > "ě" => "e"
913
- * > "Ă" => "A"
914
- * > ...
951
+ * @param template the template with parameters in {curly} braces
952
+ * @param parameters the object with parameters
953
+ * @returns the template with replaced parameters
954
+ * @throws {TemplateError} if parameter is not defined, not closed, or not opened
955
+ *
956
+ * @private within the createPromptbookExecutor
915
957
  */
916
- var DIACRITIC_VARIANTS_LETTERS = {};
917
- // tslint:disable-next-line: prefer-for-of
918
- for (var i = 0; i < defaultDiacriticsRemovalMap.length; i++) {
919
- var letters = defaultDiacriticsRemovalMap[i].letters;
920
- // tslint:disable-next-line: prefer-for-of
921
- for (var j = 0; j < letters.length; j++) {
922
- DIACRITIC_VARIANTS_LETTERS[letters[j]] = defaultDiacriticsRemovalMap[i].base;
958
+ function replaceParameters(template, parameters) {
959
+ var replacedTemplate = template;
960
+ var match;
961
+ var loopLimit = LOOP_LIMIT;
962
+ var _loop_1 = function () {
963
+ if (loopLimit-- < 0) {
964
+ throw new UnexpectedError('Loop limit reached during parameters replacement in `replaceParameters`');
965
+ }
966
+ var precol = match.groups.precol;
967
+ var parameterName = match.groups.parameterName;
968
+ if (parameterName === '') {
969
+ return "continue";
970
+ }
971
+ if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
972
+ throw new TemplateError('Parameter is already opened or not closed');
973
+ }
974
+ if (parameters[parameterName] === undefined) {
975
+ throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
976
+ }
977
+ var parameterValue = parameters[parameterName];
978
+ if (parameterValue === undefined) {
979
+ throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
980
+ }
981
+ parameterValue = parameterValue.toString();
982
+ if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
983
+ parameterValue = parameterValue
984
+ .split('\n')
985
+ .map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
986
+ .join('\n');
987
+ }
988
+ replacedTemplate =
989
+ replacedTemplate.substring(0, match.index + precol.length) +
990
+ parameterValue +
991
+ replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
992
+ };
993
+ while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
994
+ .exec(replacedTemplate))) {
995
+ _loop_1();
996
+ }
997
+ // [💫] Check if there are parameters that are not closed properly
998
+ if (/{\w+$/.test(replacedTemplate)) {
999
+ throw new TemplateError('Parameter is not closed');
1000
+ }
1001
+ // [💫] Check if there are parameters that are not opened properly
1002
+ if (/^\w+}/.test(replacedTemplate)) {
1003
+ throw new TemplateError('Parameter is not opened');
923
1004
  }
1005
+ return replacedTemplate;
924
1006
  }
925
- // <- TODO: [🍓] Put to maker function to save execution time if not needed
926
- /*
927
- @see https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
928
- Licensed under the Apache License, Version 2.0 (the "License");
929
- you may not use this file except in compliance with the License.
930
- You may obtain a copy of the License at
931
1007
 
932
- http://www.apache.org/licenses/LICENSE-2.0
1008
+ /**
1009
+ * Counts number of characters in the text
1010
+ */
1011
+ function countCharacters(text) {
1012
+ // Remove null characters
1013
+ text = text.replace(/\0/g, '');
1014
+ // Replace emojis (and also ZWJ sequence) with hyphens
1015
+ text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
1016
+ text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
1017
+ text = text.replace(/\p{Extended_Pictographic}(\u{200D}\p{Extended_Pictographic})*/gu, '-');
1018
+ return text.length;
1019
+ }
933
1020
 
934
- Unless required by applicable law or agreed to in writing, software
935
- distributed under the License is distributed on an "AS IS" BASIS,
936
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
937
- See the License for the specific language governing permissions and
938
- limitations under the License.
939
- */
1021
+ /**
1022
+ * Counts number of lines in the text
1023
+ */
1024
+ function countLines(text) {
1025
+ if (text === '') {
1026
+ return 0;
1027
+ }
1028
+ return text.split('\n').length;
1029
+ }
940
1030
 
941
1031
  /**
942
- *
1032
+ * Counts number of pages in the text
943
1033
  */
944
- function removeDiacritics(input) {
945
- /*eslint no-control-regex: "off"*/
946
- return input.replace(/[^\u0000-\u007E]/g, function (a) {
947
- return DIACRITIC_VARIANTS_LETTERS[a] || a;
948
- });
1034
+ function countPages(text) {
1035
+ var sentencesPerPage = 5; // Assuming each page has 5 sentences
1036
+ var sentences = text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
1037
+ var pageCount = Math.ceil(sentences.length / sentencesPerPage);
1038
+ return pageCount;
1039
+ }
1040
+
1041
+ /**
1042
+ * Counts number of paragraphs in the text
1043
+ */
1044
+ function countParagraphs(text) {
1045
+ return text.split(/\n\s*\n/).filter(function (paragraph) { return paragraph.trim() !== ''; }).length;
1046
+ }
1047
+
1048
+ /**
1049
+ * Split text into sentences
1050
+ */
1051
+ function splitIntoSentences(text) {
1052
+ return text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
1053
+ }
1054
+ /**
1055
+ * Counts number of sentences in the text
1056
+ */
1057
+ function countSentences(text) {
1058
+ return splitIntoSentences(text).length;
949
1059
  }
950
1060
 
951
1061
  /**
@@ -1260,116 +1370,6 @@ function nameToUriParts(name) {
1260
1370
  .filter(function (value) { return value !== ''; });
1261
1371
  }
1262
1372
 
1263
- /* tslint:disable */
1264
- function normalizeToKebabCase(sentence) {
1265
- var e_1, _a;
1266
- sentence = removeDiacritics(sentence);
1267
- var charType;
1268
- var lastCharType = 'OTHER';
1269
- var normalizedName = '';
1270
- try {
1271
- for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
1272
- var char = sentence_1_1.value;
1273
- var normalizedChar = void 0;
1274
- if (/^[a-z]$/.test(char)) {
1275
- charType = 'LOWERCASE';
1276
- normalizedChar = char;
1277
- }
1278
- else if (/^[A-Z]$/.test(char)) {
1279
- charType = 'UPPERCASE';
1280
- normalizedChar = char.toLowerCase();
1281
- }
1282
- else if (/^[0-9]$/.test(char)) {
1283
- charType = 'NUMBER';
1284
- normalizedChar = char;
1285
- }
1286
- else if (/^\/$/.test(char)) {
1287
- charType = 'SLASH';
1288
- normalizedChar = char;
1289
- }
1290
- else {
1291
- charType = 'OTHER';
1292
- normalizedChar = '-';
1293
- }
1294
- if (charType !== lastCharType &&
1295
- !(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
1296
- !(lastCharType === 'NUMBER') &&
1297
- !(charType === 'NUMBER')) {
1298
- normalizedName += '-';
1299
- }
1300
- normalizedName += normalizedChar;
1301
- lastCharType = charType;
1302
- }
1303
- }
1304
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
1305
- finally {
1306
- try {
1307
- if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
1308
- }
1309
- finally { if (e_1) throw e_1.error; }
1310
- }
1311
- normalizedName = normalizedName.split(/-+/g).join('-');
1312
- normalizedName = normalizedName.split(/-?\/-?/g).join('/');
1313
- normalizedName = normalizedName.replace(/^-/, '');
1314
- normalizedName = normalizedName.replace(/-$/, '');
1315
- return normalizedName;
1316
- }
1317
-
1318
- /* tslint:disable */
1319
- function normalizeTo_camelCase(sentence, __firstLetterCapital) {
1320
- var e_1, _a;
1321
- if (__firstLetterCapital === void 0) { __firstLetterCapital = false; }
1322
- var charType;
1323
- var lastCharType = null;
1324
- var normalizedName = '';
1325
- try {
1326
- for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
1327
- var char = sentence_1_1.value;
1328
- var normalizedChar = void 0;
1329
- if (/^[a-z]$/.test(char)) {
1330
- charType = 'LOWERCASE';
1331
- normalizedChar = char;
1332
- }
1333
- else if (/^[A-Z]$/.test(char)) {
1334
- charType = 'UPPERCASE';
1335
- normalizedChar = char.toLowerCase();
1336
- }
1337
- else if (/^[0-9]$/.test(char)) {
1338
- charType = 'NUMBER';
1339
- normalizedChar = char;
1340
- }
1341
- else {
1342
- charType = 'OTHER';
1343
- normalizedChar = '';
1344
- }
1345
- if (!lastCharType) {
1346
- if (__firstLetterCapital) {
1347
- normalizedChar = normalizedChar.toUpperCase(); //TODO: DRY
1348
- }
1349
- }
1350
- else if (charType !== lastCharType &&
1351
- !(charType === 'LOWERCASE' && lastCharType === 'UPPERCASE') &&
1352
- !(lastCharType === 'NUMBER') &&
1353
- !(charType === 'NUMBER')) {
1354
- normalizedChar = normalizedChar.toUpperCase(); //TODO: [🌺] DRY
1355
- }
1356
- normalizedName += normalizedChar;
1357
- lastCharType = charType;
1358
- }
1359
- }
1360
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
1361
- finally {
1362
- try {
1363
- if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
1364
- }
1365
- finally { if (e_1) throw e_1.error; }
1366
- }
1367
- return normalizedName;
1368
- }
1369
- /**
1370
- * TODO: [🌺] Use some intermediate util splitWords
1371
- */
1372
-
1373
1373
  function normalizeTo_PascalCase(sentence) {
1374
1374
  return normalizeTo_camelCase(sentence, true);
1375
1375
  }
@@ -1424,9 +1424,7 @@ function parseKeywords(input) {
1424
1424
  function searchKeywords(haystack, needle) {
1425
1425
  var e_1, _a;
1426
1426
  var _loop_1 = function (needleWord) {
1427
- if (!__spreadArray([], __read(haystack), false).some(function (haystackWord) {
1428
- return haystackWord.substring(0, needleWord.length) === needleWord;
1429
- })) {
1427
+ if (!__spreadArray([], __read(haystack), false).some(function (haystackWord) { return haystackWord.substring(0, needleWord.length) === needleWord; })) {
1430
1428
  return { value: false };
1431
1429
  }
1432
1430
  };