@promptbook/node 0.59.0-2 → 0.59.0-8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/umd/index.umd.js CHANGED
@@ -142,49 +142,64 @@
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
-
154
145
  /**
155
- * Supported script languages
146
+ * This error indicates errors during the execution of the promptbook
156
147
  */
157
- var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
148
+ var PromptbookExecutionError = /** @class */ (function (_super) {
149
+ __extends(PromptbookExecutionError, _super);
150
+ function PromptbookExecutionError(message) {
151
+ var _this = _super.call(this, message) || this;
152
+ _this.name = 'PromptbookExecutionError';
153
+ Object.setPrototypeOf(_this, PromptbookExecutionError.prototype);
154
+ return _this;
155
+ }
156
+ return PromptbookExecutionError;
157
+ }(Error));
158
158
 
159
159
  /**
160
- * Computes the deepness of the markdown structure.
160
+ * Asserts that the execution of a promptnook is successful
161
161
  *
162
- * @private within the library
162
+ * @param executionResult - The partial result of the promptnook execution
163
+ * @throws {PromptbookExecutionError} If the execution is not successful or if multiple errors occurred
163
164
  */
164
- function countMarkdownStructureDeepness(markdownStructure) {
165
- var e_1, _a;
166
- var maxDeepness = 0;
167
- try {
168
- for (var _b = __values(markdownStructure.sections), _c = _b.next(); !_c.done; _c = _b.next()) {
169
- var section = _c.value;
170
- maxDeepness = Math.max(maxDeepness, countMarkdownStructureDeepness(section));
171
- }
165
+ function assertsExecutionSuccessful(executionResult) {
166
+ var isSuccessful = executionResult.isSuccessful, errors = executionResult.errors;
167
+ if (isSuccessful === true) {
168
+ return;
172
169
  }
173
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
174
- finally {
175
- try {
176
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
177
- }
178
- finally { if (e_1) throw e_1.error; }
170
+ if (errors.length === 0) {
171
+ throw new PromptbookExecutionError("Promptnook Execution failed because of unknown reason");
172
+ }
173
+ else if (errors.length === 1) {
174
+ throw errors[0];
175
+ }
176
+ else {
177
+ throw new PromptbookExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Multiple errors occurred during promptnook execution\n\n ".concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n')), "\n "); }));
179
178
  }
180
- return maxDeepness + 1;
181
179
  }
180
+ /**
181
+ * TODO: [🧠] Can this return type be better typed than void
182
+ */
182
183
 
183
184
  /**
184
185
  * The maximum number of iterations for a loops
185
186
  */
186
187
  var LOOP_LIMIT = 1000;
187
188
 
189
+ /**
190
+ * This error indicates that the promptbook object has valid syntax but contains logical errors (like circular dependencies)
191
+ */
192
+ var PromptbookLogicError = /** @class */ (function (_super) {
193
+ __extends(PromptbookLogicError, _super);
194
+ function PromptbookLogicError(message) {
195
+ var _this = _super.call(this, message) || this;
196
+ _this.name = 'PromptbookLogicError';
197
+ Object.setPrototypeOf(_this, PromptbookLogicError.prototype);
198
+ return _this;
199
+ }
200
+ return PromptbookLogicError;
201
+ }(Error));
202
+
188
203
  /**
189
204
  * This error type indicates that the error should not happen and its last check before crashing with some other error
190
205
  */
@@ -200,804 +215,400 @@
200
215
  }(Error));
201
216
 
202
217
  /**
203
- * Parse a markdown string into a MarkdownStructure object.
204
- *
205
- * Note: This function does work with code blocks
206
- * Note: This function does not work with markdown comments
207
- *
208
- * @param markdown The markdown string to parse.
209
- * @returns The MarkdownStructure object.
218
+ * Tests if given string is valid URL.
210
219
  *
211
- * @private within the library
220
+ * Note: Dataurl are considered perfectly valid.
212
221
  */
213
- function markdownToMarkdownStructure(markdown) {
214
- var e_1, _a;
215
- var lines = markdown.split('\n');
216
- var root = { level: 0, title: '', contentLines: [], sections: [], parent: null };
217
- var current = root;
218
- var isInsideCodeBlock = false;
222
+ function isValidUrl(url) {
223
+ if (typeof url !== 'string') {
224
+ return false;
225
+ }
219
226
  try {
220
- for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
221
- var line = lines_1_1.value;
222
- var headingMatch = line.match(/^(?<mark>#{1,6})\s(?<title>.*)/);
223
- if (isInsideCodeBlock || !headingMatch) {
224
- if (line.startsWith('```')) {
225
- isInsideCodeBlock = !isInsideCodeBlock;
226
- }
227
- current.contentLines.push(line);
228
- }
229
- else {
230
- var level = headingMatch.groups.mark.length;
231
- var title = headingMatch.groups.title.trim();
232
- var parent_1 = void 0;
233
- if (level > current.level) {
234
- // Note: Going deeper (next section is child of current)
235
- parent_1 = current;
236
- }
237
- else {
238
- // Note: Going up or staying at the same level (next section is sibling or parent or grandparent,... of current)
239
- parent_1 = current;
240
- var loopLimit = LOOP_LIMIT;
241
- while (parent_1.level !== level - 1) {
242
- if (loopLimit-- < 0) {
243
- throw new UnexpectedError('Loop limit reached during parsing of markdown structure in `markdownToMarkdownStructure`');
244
- }
245
- if (parent_1.parent === null /* <- Note: We are in root */) {
246
- // [🌻]
247
- throw new Error(spaceTrim.spaceTrim("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
248
- }
249
- parent_1 = parent_1.parent;
250
- }
251
- }
252
- var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
253
- parent_1.sections.push(section);
254
- current = section;
255
- }
227
+ if (url.startsWith('blob:')) {
228
+ url = url.replace(/^blob:/, '');
256
229
  }
257
- }
258
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
259
- finally {
260
- try {
261
- if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
230
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
231
+ var urlObject = new URL(url);
232
+ if (!['http:', 'https:', 'data:'].includes(urlObject.protocol)) {
233
+ return false;
262
234
  }
263
- finally { if (e_1) throw e_1.error; }
235
+ return true;
264
236
  }
265
- if (root.sections.length === 1) {
266
- var markdownStructure = parsingMarkdownStructureToMarkdownStructure(root.sections[0]);
267
- return markdownStructure;
237
+ catch (error) {
238
+ return false;
268
239
  }
269
- // [🌻]
270
- throw new Error('The markdown file must have exactly one top-level section.');
271
- // return root;
272
- }
273
- /**
274
- * @private
275
- */
276
- function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
277
- var level = parsingMarkdownStructure.level, title = parsingMarkdownStructure.title, contentLines = parsingMarkdownStructure.contentLines, sections = parsingMarkdownStructure.sections;
278
- return {
279
- level: level,
280
- title: title,
281
- content: spaceTrim.spaceTrim(contentLines.join('\n')),
282
- sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
283
- };
284
240
  }
285
241
 
286
242
  /**
287
- * Utility function to extract all list items from markdown
243
+ * Validates PromptbookJson if it is logically valid
288
244
  *
289
- * Note: It works with both ul and ol
290
- * Note: It omits list items in code blocks
291
- * Note: It flattens nested lists
292
- * Note: It can not work with html syntax and comments
245
+ * It checks:
246
+ * - if it has correct parameters dependency
293
247
  *
294
- * @param markdown any valid markdown
295
- * @returns
248
+ * It does NOT check:
249
+ * - if it is valid json
250
+ * - if it is meaningful
251
+ *
252
+ * @param promptbook valid or invalid PromptbookJson
253
+ * @returns the same promptbook if it is logically valid
254
+ * @throws {PromptbookLogicError} on logical error in the promptbook
296
255
  */
297
- function extractAllListItemsFromMarkdown(markdown) {
298
- var e_1, _a;
299
- var lines = markdown.split('\n');
300
- var listItems = [];
301
- var isInCodeBlock = false;
256
+ function validatePromptbookJson(promptbook) {
257
+ // TODO: [🧠] Maybe test if promptbook is a promise and make specific error case for that
258
+ var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
259
+ if (promptbook.promptbookUrl !== undefined) {
260
+ if (!isValidUrl(promptbook.promptbookUrl)) {
261
+ // TODO: This should be maybe the syntax error detected during parsing
262
+ throw new PromptbookLogicError("Invalid promptbook URL \"".concat(promptbook.promptbookUrl, "\""));
263
+ }
264
+ }
265
+ // TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
266
+ if (!Array.isArray(promptbook.parameters)) {
267
+ // TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
268
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Promptbook is valid JSON but with wrong structure\n\n promptbook.parameters expected to be an array, but got ".concat(typeof promptbook.parameters, "\n ")));
269
+ }
270
+ // TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
271
+ if (!Array.isArray(promptbook.promptTemplates)) {
272
+ // TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
273
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Promptbook is valid JSON but with wrong structure\n\n promptbook.promptTemplates expected to be an array, but got ".concat(typeof promptbook.promptTemplates, "\n ")));
274
+ }
275
+ var _loop_1 = function (parameter) {
276
+ if (parameter.isInput && parameter.isOutput) {
277
+ throw new PromptbookLogicError("Parameter {".concat(parameter.name, "} can not be both input and output"));
278
+ }
279
+ // Note: Testing that parameter is either intermediate or output BUT not created and unused
280
+ if (!parameter.isInput &&
281
+ !parameter.isOutput &&
282
+ !promptbook.promptTemplates.some(function (template) { return template.dependentParameterNames.includes(parameter.name); })) {
283
+ throw new PromptbookLogicError(spaceTrim.spaceTrim("\n Parameter {".concat(parameter.name, "} is created but not used\n\n You can declare {").concat(parameter.name, "} as output parameter by adding in the header:\n - OUTPUT PARAMETER `{").concat(parameter.name, "}` ").concat(parameter.description || '', "\n\n ")));
284
+ }
285
+ // Note: Testing that parameter is either input or result of some template
286
+ if (!parameter.isInput &&
287
+ !promptbook.promptTemplates.some(function (template) { return template.resultingParameterName === parameter.name; })) {
288
+ throw new PromptbookLogicError(spaceTrim.spaceTrim("\n Parameter {".concat(parameter.name, "} is declared but not defined\n\n You can do one of these:\n - Remove declaration of {").concat(parameter.name, "}\n - Add prompt template that results in -> {").concat(parameter.name, "}\n\n ")));
289
+ }
290
+ };
302
291
  try {
303
- for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
304
- var line = lines_1_1.value;
305
- var trimmedLine = line.trim();
306
- if (trimmedLine.startsWith('```')) {
307
- isInCodeBlock = !isInCodeBlock;
308
- }
309
- if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
310
- var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
311
- listItems.push(listItem);
312
- }
292
+ // Note: Check each parameter individually
293
+ for (var _e = __values(promptbook.parameters), _f = _e.next(); !_f.done; _f = _e.next()) {
294
+ var parameter = _f.value;
295
+ _loop_1(parameter);
313
296
  }
314
297
  }
315
298
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
316
299
  finally {
317
300
  try {
318
- if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
301
+ if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
319
302
  }
320
303
  finally { if (e_1) throw e_1.error; }
321
304
  }
322
- return listItems;
323
- }
324
-
325
- /**
326
- * Makes first letter of a string uppercase
327
- *
328
- */
329
- function capitalize(word) {
330
- return word.substring(0, 1).toUpperCase() + word.substring(1);
331
- }
332
-
333
- /**
334
- * Extracts all code blocks from markdown.
335
- *
336
- * Note: There are 3 simmilar function:
337
- * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
338
- * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
339
- * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
340
- *
341
- * @param markdown any valid markdown
342
- * @returns code blocks with language and content
343
- *
344
- */
345
- function extractAllBlocksFromMarkdown(markdown) {
346
- var e_1, _a;
347
- var codeBlocks = [];
348
- var lines = markdown.split('\n');
349
- var currentCodeBlock = null;
305
+ // Note: Check each template individually
306
+ var definedParameters = new Set(promptbook.parameters.filter(function (_a) {
307
+ var isInput = _a.isInput;
308
+ return isInput;
309
+ }).map(function (_a) {
310
+ var name = _a.name;
311
+ return name;
312
+ }));
350
313
  try {
351
- for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
352
- var line = lines_1_1.value;
353
- if (line.startsWith('```')) {
354
- var language = line.slice(3).trim() || null;
355
- if (currentCodeBlock === null) {
356
- currentCodeBlock = { language: language, content: '' };
357
- }
358
- else {
359
- if (language !== null) {
360
- // [🌻]
361
- throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
314
+ for (var _g = __values(promptbook.promptTemplates), _h = _g.next(); !_h.done; _h = _g.next()) {
315
+ var template = _h.value;
316
+ if (definedParameters.has(template.resultingParameterName)) {
317
+ throw new PromptbookLogicError("Parameter {".concat(template.resultingParameterName, "} is defined multiple times"));
318
+ }
319
+ definedParameters.add(template.resultingParameterName);
320
+ if (template.executionType === 'PROMPT_TEMPLATE' &&
321
+ (template.modelRequirements.modelVariant === undefined)) {
322
+ throw new PromptbookLogicError(spaceTrim.spaceTrim("\n\n You must specify MODEL VARIANT in the prompt template \"".concat(template.title, "\"\n\n For example:\n - MODEL VARIANT Chat\n - MODEL NAME `gpt-4-1106-preview`\n\n ")));
323
+ }
324
+ if (template.jokers && template.jokers.length > 0) {
325
+ if (!template.expectFormat &&
326
+ !template.expectations /* <- TODO: Require at least 1 -> min <- expectation to use jokers */) {
327
+ throw new PromptbookLogicError("Joker parameters are used for {".concat(template.resultingParameterName, "} but no expectations are defined"));
328
+ }
329
+ try {
330
+ for (var _j = (e_3 = void 0, __values(template.jokers)), _k = _j.next(); !_k.done; _k = _j.next()) {
331
+ var joker = _k.value;
332
+ if (!template.dependentParameterNames.includes(joker)) {
333
+ throw new PromptbookLogicError("Parameter {".concat(joker, "} is used for {").concat(template.resultingParameterName, "} as joker but not in dependentParameterNames"));
334
+ }
362
335
  }
363
- codeBlocks.push(currentCodeBlock);
364
- currentCodeBlock = null;
336
+ }
337
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
338
+ finally {
339
+ try {
340
+ if (_k && !_k.done && (_c = _j.return)) _c.call(_j);
341
+ }
342
+ finally { if (e_3) throw e_3.error; }
365
343
  }
366
344
  }
367
- else if (currentCodeBlock !== null) {
368
- if (currentCodeBlock.content !== '') {
369
- currentCodeBlock.content += '\n';
345
+ if (template.expectations) {
346
+ try {
347
+ for (var _l = (e_4 = void 0, __values(Object.entries(template.expectations))), _m = _l.next(); !_m.done; _m = _l.next()) {
348
+ var _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], min = _p.min, max = _p.max;
349
+ if (min !== undefined && max !== undefined && min > max) {
350
+ throw new PromptbookLogicError("Min expectation (=".concat(min, ") of ").concat(unit, " is higher than max expectation (=").concat(max, ")"));
351
+ }
352
+ if (min !== undefined && min < 0) {
353
+ throw new PromptbookLogicError("Min expectation of ".concat(unit, " must be zero or positive"));
354
+ }
355
+ if (max !== undefined && max <= 0) {
356
+ throw new PromptbookLogicError("Max expectation of ".concat(unit, " must be positive"));
357
+ }
358
+ }
359
+ }
360
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
361
+ finally {
362
+ try {
363
+ if (_m && !_m.done && (_d = _l.return)) _d.call(_l);
364
+ }
365
+ finally { if (e_4) throw e_4.error; }
370
366
  }
371
- currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
372
367
  }
373
368
  }
374
369
  }
375
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
370
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
376
371
  finally {
377
372
  try {
378
- if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
373
+ if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
379
374
  }
380
- finally { if (e_1) throw e_1.error; }
375
+ finally { if (e_2) throw e_2.error; }
381
376
  }
382
- if (currentCodeBlock !== null) {
383
- // [🌻]
384
- throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
377
+ // Note: Detect circular dependencies
378
+ var resovedParameters = promptbook.parameters
379
+ .filter(function (_a) {
380
+ var isInput = _a.isInput;
381
+ return isInput;
382
+ })
383
+ .map(function (_a) {
384
+ var name = _a.name;
385
+ return name;
386
+ });
387
+ var unresovedTemplates = __spreadArray([], __read(promptbook.promptTemplates), false);
388
+ var loopLimit = LOOP_LIMIT;
389
+ var _loop_2 = function () {
390
+ if (loopLimit-- < 0) {
391
+ throw new UnexpectedError('Loop limit reached during detection of circular dependencies in `validatePromptbookJson`');
392
+ }
393
+ var currentlyResovedTemplates = unresovedTemplates.filter(function (template) {
394
+ return template.dependentParameterNames.every(function (name) { return resovedParameters.includes(name); });
395
+ });
396
+ if (currentlyResovedTemplates.length === 0) {
397
+ throw new PromptbookLogicError(spaceTrim.spaceTrim(function (block) { return "\n\n Can not resolve some parameters\n It may be circular dependencies\n\n Can not resolve:\n ".concat(block(unresovedTemplates
398
+ .map(function (_a) {
399
+ var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
400
+ return "- {".concat(resultingParameterName, "} depends on ").concat(dependentParameterNames
401
+ .map(function (dependentParameterName) { return "{".concat(dependentParameterName, "}"); })
402
+ .join(', '));
403
+ })
404
+ .join('\n')), "\n\n Resolved:\n ").concat(block(resovedParameters.map(function (name) { return "- {".concat(name, "}"); }).join('\n')), "\n "); }));
405
+ }
406
+ resovedParameters = __spreadArray(__spreadArray([], __read(resovedParameters), false), __read(currentlyResovedTemplates.map(function (_a) {
407
+ var resultingParameterName = _a.resultingParameterName;
408
+ return resultingParameterName;
409
+ })), false);
410
+ unresovedTemplates = unresovedTemplates.filter(function (template) { return !currentlyResovedTemplates.includes(template); });
411
+ };
412
+ while (unresovedTemplates.length > 0) {
413
+ _loop_2();
385
414
  }
386
- return codeBlocks;
415
+ return promptbook;
387
416
  }
417
+ /**
418
+ * TODO: [🧠] Work with promptbookVersion
419
+ * TODO: Use here some json-schema, Zod or something similar and change it to:
420
+ * > /**
421
+ * > * Validates PromptbookJson if it is logically valid.
422
+ * > *
423
+ * > * It checks:
424
+ * > * - it has a valid structure
425
+ * > * - ...
426
+ * > ex port function validatePromptbookJson(promptbook: unknown): asserts promptbook is PromptbookJson {
427
+ */
388
428
 
389
429
  /**
390
- * Extracts exactly ONE code block from markdown.
391
- *
392
- * Note: If there are multiple or no code blocks the function throws an error
393
- *
394
- * Note: There are 3 simmilar function:
395
- * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
396
- * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
397
- * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
430
+ * This error occurs when some expectation is not met in the execution of the pipeline
398
431
  *
399
- * @param markdown any valid markdown
400
- * @returns code block with language and content
432
+ * @private Always catched and rethrown as `PromptbookExecutionError`
433
+ * Note: This is a kindof subtype of PromptbookExecutionError
401
434
  */
402
- function extractOneBlockFromMarkdown(markdown) {
403
- var codeBlocks = extractAllBlocksFromMarkdown(markdown);
404
- if (codeBlocks.length !== 1) {
405
- // TODO: Report more specific place where the error happened
406
- throw new Error(/* <- [🌻] */ 'There should be exactly one code block in the markdown');
435
+ var ExpectError = /** @class */ (function (_super) {
436
+ __extends(ExpectError, _super);
437
+ function ExpectError(message) {
438
+ var _this = _super.call(this, message) || this;
439
+ _this.name = 'ExpectError';
440
+ Object.setPrototypeOf(_this, ExpectError.prototype);
441
+ return _this;
407
442
  }
408
- return codeBlocks[0];
409
- }
410
- /***
411
- * TODO: [🍓][🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
412
- */
443
+ return ExpectError;
444
+ }(Error));
413
445
 
414
446
  /**
415
- * Removes HTML or Markdown comments from a string.
416
- *
417
- * @param {string} content - The string to remove comments from.
418
- * @returns {string} The input string with all comments removed.
447
+ * Function isValidJsonString will tell you if the string is valid JSON or not
419
448
  */
420
- function removeContentComments(content) {
421
- return spaceTrim.spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
449
+ function isValidJsonString(value /* <-[👨‍⚖️] */) {
450
+ try {
451
+ JSON.parse(value);
452
+ return true;
453
+ }
454
+ catch (error) {
455
+ if (!(error instanceof Error)) {
456
+ throw error;
457
+ }
458
+ if (error.message.includes('Unexpected token')) {
459
+ return false;
460
+ }
461
+ return false;
462
+ }
422
463
  }
423
464
 
424
465
  /**
425
- * Creates a new set with all elements that are present in either set
466
+ * The version of the Promptbook library
426
467
  */
427
- function union() {
428
- var e_1, _a, e_2, _b;
429
- var sets = [];
468
+ var PROMPTBOOK_VERSION = '0.59.0-7';
469
+
470
+ /**
471
+ * Function `addUsage` will add multiple usages into one
472
+ *
473
+ * Note: If you provide 0 values, it returns void usage
474
+ */
475
+ function addUsage() {
476
+ var usageItems = [];
430
477
  for (var _i = 0; _i < arguments.length; _i++) {
431
- sets[_i] = arguments[_i];
432
- }
433
- var union = new Set();
434
- try {
435
- for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
436
- var set = sets_1_1.value;
437
- try {
438
- for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
439
- var item = _d.value;
440
- union.add(item);
478
+ usageItems[_i] = arguments[_i];
479
+ }
480
+ var initialStructure = {
481
+ price: { value: 0 },
482
+ input: {
483
+ tokensCount: { value: 0 },
484
+ charactersCount: { value: 0 },
485
+ wordsCount: { value: 0 },
486
+ sentencesCount: { value: 0 },
487
+ linesCount: { value: 0 },
488
+ paragraphsCount: { value: 0 },
489
+ pagesCount: { value: 0 },
490
+ },
491
+ output: {
492
+ tokensCount: { value: 0 },
493
+ charactersCount: { value: 0 },
494
+ wordsCount: { value: 0 },
495
+ sentencesCount: { value: 0 },
496
+ linesCount: { value: 0 },
497
+ paragraphsCount: { value: 0 },
498
+ pagesCount: { value: 0 },
499
+ },
500
+ };
501
+ return usageItems.reduce(function (acc, item) {
502
+ var e_1, _a, e_2, _b;
503
+ var _c;
504
+ acc.price.value += ((_c = item.price) === null || _c === void 0 ? void 0 : _c.value) || 0;
505
+ try {
506
+ for (var _d = __values(Object.keys(acc.input)), _e = _d.next(); !_e.done; _e = _d.next()) {
507
+ var key = _e.value;
508
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
509
+ //@ts-ignore
510
+ if (item.input[key]) {
511
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
512
+ //@ts-ignore
513
+ acc.input[key].value += item.input[key].value || 0;
514
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
515
+ //@ts-ignore
516
+ if (item.input[key].isUncertain) {
517
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
518
+ //@ts-ignore
519
+ acc.input[key].isUncertain = true;
520
+ }
441
521
  }
442
522
  }
443
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
444
- finally {
445
- try {
446
- if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
447
- }
448
- finally { if (e_2) throw e_2.error; }
523
+ }
524
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
525
+ finally {
526
+ try {
527
+ if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
449
528
  }
529
+ finally { if (e_1) throw e_1.error; }
450
530
  }
451
- }
452
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
453
- finally {
454
531
  try {
455
- if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
532
+ for (var _f = __values(Object.keys(acc.output)), _g = _f.next(); !_g.done; _g = _f.next()) {
533
+ var key = _g.value;
534
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
535
+ //@ts-ignore
536
+ if (item.output[key]) {
537
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
538
+ //@ts-ignore
539
+ acc.output[key].value += item.output[key].value || 0;
540
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
541
+ //@ts-ignore
542
+ if (item.output[key].isUncertain) {
543
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
544
+ //@ts-ignore
545
+ acc.output[key].isUncertain = true;
546
+ }
547
+ }
548
+ }
456
549
  }
457
- finally { if (e_1) throw e_1.error; }
458
- }
459
- return union;
550
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
551
+ finally {
552
+ try {
553
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
554
+ }
555
+ finally { if (e_2) throw e_2.error; }
556
+ }
557
+ return acc;
558
+ }, initialStructure);
460
559
  }
461
560
 
462
561
  /**
463
- * The version of the Promptbook library
562
+ * Counts number of characters in the text
464
563
  */
465
- var PROMPTBOOK_VERSION = '0.59.0-1';
564
+ function countCharacters(text) {
565
+ // Remove null characters
566
+ text = text.replace(/\0/g, '');
567
+ // Replace emojis (and also ZWJ sequence) with hyphens
568
+ text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
569
+ text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
570
+ text = text.replace(/\p{Extended_Pictographic}(\u{200D}\p{Extended_Pictographic})*/gu, '-');
571
+ return text.length;
572
+ }
466
573
 
467
574
  /**
468
- * Parses the template and returns the list of all parameter names
469
- *
470
- * @param template the template with parameters in {curly} braces
471
- * @returns the list of parameter names
575
+ * Counts number of lines in the text
472
576
  */
473
- function extractParameters(template) {
474
- var e_1, _a;
475
- var matches = template.matchAll(/{\w+}/g);
476
- var parameterNames = new Set();
477
- try {
478
- for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
479
- var match = matches_1_1.value;
480
- var parameterName = match[0].slice(1, -1);
481
- parameterNames.add(parameterName);
482
- }
483
- }
484
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
485
- finally {
486
- try {
487
- if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
488
- }
489
- finally { if (e_1) throw e_1.error; }
577
+ function countLines(text) {
578
+ if (text === '') {
579
+ return 0;
490
580
  }
491
- return parameterNames;
581
+ return text.split('\n').length;
492
582
  }
493
583
 
494
584
  /**
495
- * Parses the given script and returns the list of all used variables that are not defined in the script
496
- *
497
- * @param script from which to extract the variables
498
- * @returns the list of variable names
499
- * @throws {PromptbookSyntaxError} if the script is invalid
585
+ * Counts number of pages in the text
500
586
  */
501
- function extractVariables(script) {
502
- var variables = new Set();
503
- script = "(()=>{".concat(script, "})()");
504
- try {
505
- for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
506
- try {
507
- eval(script);
508
- }
509
- catch (error) {
510
- if (!(error instanceof ReferenceError)) {
511
- throw error;
512
- }
513
- var undefinedName = error.message.split(' ')[0];
514
- /*
515
- Note: Remapping error
516
- From: [ReferenceError: thing is not defined],
517
- To: [Error: Parameter {thing} is not defined],
518
- */
519
- if (!undefinedName) {
520
- throw error;
521
- }
522
- if (script.includes(undefinedName + '(')) {
523
- script = "const ".concat(undefinedName, " = ()=>'';") + script;
524
- }
525
- else {
526
- variables.add(undefinedName);
527
- script = "const ".concat(undefinedName, " = '';") + script;
528
- }
529
- }
530
- }
531
- catch (error) {
532
- if (!(error instanceof Error)) {
533
- throw error;
534
- }
535
- throw new PromptbookSyntaxError(spaceTrim.spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
536
- }
537
- return variables;
587
+ function countPages(text) {
588
+ var sentencesPerPage = 5; // Assuming each page has 5 sentences
589
+ var sentences = text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
590
+ var pageCount = Math.ceil(sentences.length / sentencesPerPage);
591
+ return pageCount;
538
592
  }
539
- /**
540
- * TODO: [🔣] Support for multiple languages - python, java,...
541
- */
542
593
 
543
594
  /**
544
- * Parses the prompt template and returns the set of all used parameters
545
- *
546
- * @param promptTemplate the template with used parameters
547
- * @returns the set of parameter names
548
- * @throws {PromptbookSyntaxError} if the script is invalid
549
- */
550
- function extractParametersFromPromptTemplate(promptTemplate) {
551
- var e_1, _a, e_2, _b;
552
- var parameterNames = new Set();
553
- try {
554
- 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()) {
555
- var parameterName = _d.value;
556
- parameterNames.add(parameterName);
557
- }
558
- }
559
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
560
- finally {
561
- try {
562
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
563
- }
564
- finally { if (e_1) throw e_1.error; }
565
- }
566
- if (promptTemplate.executionType === 'SCRIPT') {
567
- try {
568
- for (var _e = __values(extractVariables(promptTemplate.content)), _f = _e.next(); !_f.done; _f = _e.next()) {
569
- var parameterName = _f.value;
570
- parameterNames.add(parameterName);
571
- }
572
- }
573
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
574
- finally {
575
- try {
576
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
577
- }
578
- finally { if (e_2) throw e_2.error; }
579
- }
580
- }
581
- return parameterNames;
582
- }
583
- /**
584
- * TODO: [🔣] If script require contentLanguage
595
+ * Counts number of paragraphs in the text
585
596
  */
586
-
587
- /* tslint:disable */
588
- /*
589
- TODO: Tests
590
- expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: 'Moje tabule' })).toEqual('/VtG7sR9rRJqwNEdM2/Moje tabule');
591
- expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: 'ěščřžžýáíúů' })).toEqual('/VtG7sR9rRJqwNEdM2/escrzyaieuu');
592
- expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: ' ahoj ' })).toEqual('/VtG7sR9rRJqwNEdM2/ahoj');
593
- expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: ' ahoj_ahojAhoj ahoj ' })).toEqual('/VtG7sR9rRJqwNEdM2/ahoj-ahoj-ahoj-ahoj');
594
- */
595
- function normalizeTo_SCREAMING_CASE(sentence) {
596
- var e_1, _a;
597
- var charType;
598
- var lastCharType = 'OTHER';
599
- var normalizedName = '';
600
- try {
601
- for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
602
- var char = sentence_1_1.value;
603
- var normalizedChar = void 0;
604
- if (/^[a-z]$/.test(char)) {
605
- charType = 'LOWERCASE';
606
- normalizedChar = char.toUpperCase();
607
- }
608
- else if (/^[A-Z]$/.test(char)) {
609
- charType = 'UPPERCASE';
610
- normalizedChar = char;
611
- }
612
- else if (/^[0-9]$/.test(char)) {
613
- charType = 'NUMBER';
614
- normalizedChar = char;
615
- }
616
- else if (/^\/$/.test(char)) {
617
- charType = 'SLASH';
618
- normalizedChar = char;
619
- }
620
- else {
621
- charType = 'OTHER';
622
- normalizedChar = '_';
623
- }
624
- if (charType !== lastCharType &&
625
- !(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
626
- !(lastCharType === 'NUMBER') &&
627
- !(charType === 'NUMBER')) {
628
- normalizedName += '_';
629
- }
630
- normalizedName += normalizedChar;
631
- lastCharType = charType;
632
- }
633
- }
634
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
635
- finally {
636
- try {
637
- if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
638
- }
639
- finally { if (e_1) throw e_1.error; }
640
- }
641
- normalizedName = normalizedName.replace(/_+/g, '_');
642
- normalizedName = normalizedName.replace(/_?\/_?/g, '/');
643
- normalizedName = normalizedName.replace(/^_/, '');
644
- normalizedName = normalizedName.replace(/_$/, '');
645
- return normalizedName;
597
+ function countParagraphs(text) {
598
+ return text.split(/\n\s*\n/).filter(function (paragraph) { return paragraph.trim() !== ''; }).length;
646
599
  }
647
- /**
648
- * TODO: [🌺] Use some intermediate util splitWords
649
- */
650
-
651
- /**
652
- * Execution type describes the way how the block is executed
653
- *
654
- * @see https://github.com/webgptorg/promptbook#execution-type
655
- */
656
- var ExecutionTypes = [
657
- 'PROMPT_TEMPLATE',
658
- 'SIMPLE_TEMPLATE',
659
- 'SCRIPT',
660
- 'PROMPT_DIALOG',
661
- // <- [🥻] Insert here when making new command
662
- ];
663
-
664
- /**
665
- * Units of text measurement
666
- */
667
- var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'LINES', 'PARAGRAPHS', 'PAGES'];
668
- /**
669
- * TODO: [💝] Unite object for expecting amount and format - remove expectFormat
670
- * TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
671
- * TODO: [👙][🧠] Just selecting gpt3 or gpt4 level of model
672
- */
673
600
 
674
601
  /**
675
- * Removes Markdown formatting tags from a string.
676
- *
677
- * @param {string} str - The string to remove Markdown tags from.
678
- * @returns {string} The input string with all Markdown tags removed.
602
+ * Split text into sentences
679
603
  */
680
- function removeMarkdownFormatting(str) {
681
- // Remove bold formatting
682
- str = str.replace(/\*\*(.*?)\*\*/g, '$1');
683
- // Remove italic formatting
684
- str = str.replace(/\*(.*?)\*/g, '$1');
685
- // Remove code formatting
686
- str = str.replace(/`(.*?)`/g, '$1');
687
- return str;
604
+ function splitIntoSentences(text) {
605
+ return text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
688
606
  }
689
-
690
607
  /**
691
- * Function parseNumber will parse number from string
692
- *
693
- * Unlike Number.parseInt, Number.parseFloat it will never ever result in NaN
694
- * Note: it also works only with decimal numbers
695
- *
696
- * @returns parsed number
697
- * @throws {PromptbookSyntaxError} if the value is not a number
698
- *
699
- * @private within the parseCommand
608
+ * Counts number of sentences in the text
700
609
  */
701
- function parseNumber(value) {
702
- var originalValue = value;
703
- if (typeof value === 'number') {
704
- value = value.toString(); // <- TODO: Maybe more efficient way to do this
705
- }
706
- if (typeof value !== 'string') {
707
- return 0;
708
- }
709
- value = value.trim();
710
- if (value.startsWith('+')) {
711
- return parseNumber(value.substring(1));
712
- }
713
- if (value.startsWith('-')) {
714
- var number = parseNumber(value.substring(1));
715
- if (number === 0) {
716
- return 0; // <- Note: To prevent -0
717
- }
718
- return -number;
719
- }
720
- value = value.replace(/,/g, '.');
721
- value = value.toUpperCase();
722
- if (value === '') {
723
- return 0;
724
- }
725
- if (value === '♾' || value.startsWith('INF')) {
726
- return Infinity;
727
- }
728
- if (value.includes('/')) {
729
- var _a = __read(value.split('/'), 2), numerator_ = _a[0], denominator_ = _a[1];
730
- var numerator = parseNumber(numerator_);
731
- var denominator = parseNumber(denominator_);
732
- if (denominator === 0) {
733
- throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\" because denominator is zero"));
734
- }
735
- return numerator / denominator;
736
- }
737
- if (/^(NAN|NULL|NONE|UNDEFINED|ZERO|NO.*)$/.test(value)) {
738
- return 0;
739
- }
740
- if (value.includes('E')) {
741
- var _b = __read(value.split('E'), 2), significand = _b[0], exponent = _b[1];
742
- return parseNumber(significand) * Math.pow(10, parseNumber(exponent));
743
- }
744
- if (!/^[0-9.]+$/.test(value) || value.split('.').length > 2) {
745
- throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\""));
746
- }
747
- var num = parseFloat(value);
748
- if (isNaN(num)) {
749
- throw new PromptbookSyntaxError("Unexpected NaN when parsing number from \"".concat(originalValue, "\""));
750
- }
751
- return num;
752
- }
753
- /**
754
- * TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
755
- */
756
-
757
- /**
758
- * Parses one line of ul/ol to command
759
- *
760
- * @returns parsed command object
761
- * @throws {PromptbookSyntaxError} if the command is invalid
762
- *
763
- * @private within the promptbookStringToJson
764
- */
765
- function parseCommand(listItem) {
766
- var e_1, _a;
767
- if (listItem.includes('\n') || listItem.includes('\r')) {
768
- throw new PromptbookSyntaxError('Command can not contain new line characters:');
769
- }
770
- var type = listItem.trim();
771
- type = type.split('`').join('');
772
- type = type.split('"').join('');
773
- type = type.split("'").join('');
774
- type = type.split('~').join('');
775
- type = type.split('[').join('');
776
- type = type.split(']').join('');
777
- type = type.split('(').join('');
778
- type = type.split(')').join('');
779
- type = normalizeTo_SCREAMING_CASE(type);
780
- type = type.split('DIALOGUE').join('DIALOG');
781
- var listItemParts = listItem
782
- .split(' ')
783
- .map(function (part) { return part.trim(); })
784
- .filter(function (item) { return item !== ''; })
785
- .filter(function (item) { return !/^PTBK$/i.test(item); })
786
- .filter(function (item) { return !/^PROMPTBOOK$/i.test(item); })
787
- .map(removeMarkdownFormatting);
788
- if (type.startsWith('URL') ||
789
- type.startsWith('PTBK_URL') ||
790
- type.startsWith('PTBKURL') ||
791
- type.startsWith('PROMPTBOOK_URL') ||
792
- type.startsWith('PROMPTBOOKURL') ||
793
- type.startsWith('HTTPS')) {
794
- if (!(listItemParts.length === 2 || (listItemParts.length === 1 && type.startsWith('HTTPS')))) {
795
- throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n ")));
796
- }
797
- var promptbookUrlString = listItemParts.pop();
798
- var promptbookUrl = new URL(promptbookUrlString);
799
- if (promptbookUrl.protocol !== 'https:') {
800
- throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n Protocol must be HTTPS\n ")));
801
- }
802
- if (promptbookUrl.hash !== '') {
803
- throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n URL must not contain hash\n Hash is used for identification of the prompt template in the pipeline\n ")));
804
- }
805
- return {
806
- type: 'PROMPTBOOK_URL',
807
- promptbookUrl: promptbookUrl,
808
- };
809
- }
810
- else if (type.startsWith('PROMPTBOOK_VERSION') || type.startsWith('PTBK_VERSION')) {
811
- if (listItemParts.length !== 2) {
812
- throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Invalid PROMPTBOOK_VERSION command:\n\n - ".concat(listItem, "\n ")));
813
- }
814
- var promptbookVersion = listItemParts.pop();
815
- // TODO: Validate version
816
- return {
817
- type: 'PROMPTBOOK_VERSION',
818
- promptbookVersion: promptbookVersion,
819
- };
820
- }
821
- else if (type.startsWith('EXECUTE') ||
822
- type.startsWith('EXEC') ||
823
- type.startsWith('PROMPT_DIALOG') ||
824
- type.startsWith('SIMPLE_TEMPLATE')) {
825
- var executionTypes = ExecutionTypes.filter(function (executionType) { return type.includes(executionType); });
826
- if (executionTypes.length !== 1) {
827
- throw new PromptbookSyntaxError(spaceTrim.spaceTrim(function (block) { return "\n Unknown execution type in command:\n\n - ".concat(listItem, "\n\n Supported execution types are:\n ").concat(block(ExecutionTypes.join(', ')), "\n "); }));
828
- }
829
- return {
830
- type: 'EXECUTE',
831
- executionType: executionTypes[0],
832
- };
833
- }
834
- else if (type.startsWith('MODEL')) {
835
- // TODO: Make this more elegant and dynamically
836
- if (type.startsWith('MODEL_VARIANT')) {
837
- if (type === 'MODEL_VARIANT_CHAT') {
838
- return {
839
- type: 'MODEL',
840
- key: 'modelVariant',
841
- value: 'CHAT',
842
- };
843
- }
844
- else if (type === 'MODEL_VARIANT_COMPLETION') {
845
- return {
846
- type: 'MODEL',
847
- key: 'modelVariant',
848
- value: 'COMPLETION',
849
- };
850
- }
851
- else {
852
- throw new PromptbookSyntaxError(spaceTrim.spaceTrim(function (block) { return "\n Unknown model variant in command:\n\n - ".concat(listItem, "\n\n Supported variants are:\n ").concat(block(['CHAT', 'COMPLETION'].join(', ')), "\n "); }));
853
- }
854
- }
855
- if (type.startsWith('MODEL_NAME')) {
856
- return {
857
- type: 'MODEL',
858
- key: 'modelName',
859
- value: listItemParts.pop(),
860
- };
861
- }
862
- else {
863
- throw new PromptbookSyntaxError(spaceTrim.spaceTrim(function (block) { return "\n Unknown model key in command:\n\n - ".concat(listItem, "\n\n Supported model keys are:\n ").concat(block(['variant', 'name'].join(', ')), "\n\n Example:\n\n - MODEL VARIANT Chat\n - MODEL NAME gpt-4\n "); }));
864
- }
865
- }
866
- else if (type.startsWith('PARAM') ||
867
- type.startsWith('INPUT_PARAM') ||
868
- type.startsWith('OUTPUT_PARAM') ||
869
- listItem.startsWith('{') ||
870
- listItem.startsWith('> {') /* <- Note: This is a bit hack to parse return parameters defined at the end of each section */) {
871
- var parametersMatch = listItem.match(/\{(?<parameterName>[a-z0-9_]+)\}[^\S\r\n]*(?<parameterDescription>.*)$/im);
872
- if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
873
- throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
874
- }
875
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
876
- var _b = parametersMatch.groups, parameterName = _b.parameterName, parameterDescription = _b.parameterDescription;
877
- if (parameterDescription && parameterDescription.match(/\{(?<parameterName>[a-z0-9_]+)\}/im)) {
878
- throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Parameter {".concat(parameterName, "} can not contain another parameter in description:\n\n - ").concat(listItem, "\n ")));
879
- }
880
- var isInput = type.startsWith('INPUT');
881
- var isOutput = type.startsWith('OUTPUT');
882
- if (listItem.startsWith('> {')) {
883
- isInput = false;
884
- isOutput = false;
885
- }
886
- return {
887
- type: 'PARAMETER',
888
- parameterName: parameterName,
889
- parameterDescription: parameterDescription.trim() || null,
890
- isInput: isInput,
891
- isOutput: isOutput,
892
- };
893
- }
894
- else if (type.startsWith('JOKER')) {
895
- if (listItemParts.length !== 2) {
896
- throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Invalid JOKER command:\n\n - ".concat(listItem, "\n ")));
897
- }
898
- var parametersMatch = (listItemParts.pop() || '').match(/^\{(?<parameterName>[a-z0-9_]+)\}$/im);
899
- if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
900
- throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
901
- }
902
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
903
- var parameterName = parametersMatch.groups.parameterName;
904
- return {
905
- type: 'JOKER',
906
- parameterName: parameterName,
907
- };
908
- }
909
- else if (type.startsWith('POSTPROCESS') || type.startsWith('POST_PROCESS')) {
910
- if (listItemParts.length !== 2) {
911
- throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Invalid POSTPROCESSING command:\n\n - ".concat(listItem, "\n ")));
912
- }
913
- var functionName = listItemParts.pop();
914
- return {
915
- type: 'POSTPROCESS',
916
- functionName: functionName,
917
- };
918
- }
919
- else if (type.startsWith('EXPECT_JSON')) {
920
- return {
921
- type: 'EXPECT_FORMAT',
922
- format: 'JSON',
923
- };
924
- // [🥤]
925
- }
926
- else if (type.startsWith('EXPECT')) {
927
- try {
928
- listItemParts.shift();
929
- var sign = void 0;
930
- var signRaw = listItemParts.shift();
931
- if (/^exact/i.test(signRaw)) {
932
- sign = 'EXACTLY';
933
- }
934
- else if (/^min/i.test(signRaw)) {
935
- sign = 'MINIMUM';
936
- }
937
- else if (/^max/i.test(signRaw)) {
938
- sign = 'MAXIMUM';
939
- }
940
- else {
941
- throw new PromptbookSyntaxError("Invalid sign \"".concat(signRaw, "\", expected EXACTLY, MIN or MAX"));
942
- }
943
- var amountRaw = listItemParts.shift();
944
- var amount = parseNumber(amountRaw);
945
- if (amount < 0) {
946
- throw new PromptbookSyntaxError('Amount must be positive number or zero');
947
- }
948
- if (amount !== Math.floor(amount)) {
949
- throw new PromptbookSyntaxError('Amount must be whole number');
950
- }
951
- var unitRaw = listItemParts.shift();
952
- var unit = undefined;
953
- try {
954
- for (var EXPECTATION_UNITS_1 = __values(EXPECTATION_UNITS), EXPECTATION_UNITS_1_1 = EXPECTATION_UNITS_1.next(); !EXPECTATION_UNITS_1_1.done; EXPECTATION_UNITS_1_1 = EXPECTATION_UNITS_1.next()) {
955
- var existingUnit = EXPECTATION_UNITS_1_1.value;
956
- var existingUnitText = existingUnit;
957
- existingUnitText = existingUnitText.substring(0, existingUnitText.length - 1);
958
- if (existingUnitText === 'CHARACTER') {
959
- existingUnitText = 'CHAR';
960
- }
961
- if (new RegExp("^".concat(existingUnitText.toLowerCase())).test(unitRaw.toLowerCase()) ||
962
- new RegExp("^".concat(unitRaw.toLowerCase())).test(existingUnitText.toLowerCase())) {
963
- if (unit !== undefined) {
964
- throw new PromptbookSyntaxError("Ambiguous unit \"".concat(unitRaw, "\""));
965
- }
966
- unit = existingUnit;
967
- }
968
- }
969
- }
970
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
971
- finally {
972
- try {
973
- if (EXPECTATION_UNITS_1_1 && !EXPECTATION_UNITS_1_1.done && (_a = EXPECTATION_UNITS_1.return)) _a.call(EXPECTATION_UNITS_1);
974
- }
975
- finally { if (e_1) throw e_1.error; }
976
- }
977
- if (unit === undefined) {
978
- throw new PromptbookSyntaxError("Invalid unit \"".concat(unitRaw, "\""));
979
- }
980
- return {
981
- type: 'EXPECT_AMOUNT',
982
- sign: sign,
983
- unit: unit,
984
- amount: amount,
985
- };
986
- }
987
- catch (error) {
988
- if (!(error instanceof Error)) {
989
- throw error;
990
- }
991
- throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Invalid EXPECT command; ".concat(error.message, ":\n\n - ").concat(listItem, "\n ")));
992
- }
993
- /*
994
- } else if (type.startsWith('__________________')) {
995
- // <- [🥻] Insert here when making new command
996
- */
997
- }
998
- else {
999
- throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Unknown command:\n\n - ".concat(listItem, "\n\n Supported commands are:\n - PROMPTBOOK_URL <url>\n - PROMPTBOOK_VERSION <version>\n - EXECUTE PROMPT TEMPLATE\n - EXECUTE SIMPLE TEMPLATE\n - SIMPLE TEMPLATE\n - EXECUTE SCRIPT\n - EXECUTE PROMPT_DIALOG'\n - PROMPT_DIALOG'\n - MODEL NAME <name>\n - MODEL VARIANT <\"Chat\"|\"Completion\">\n - INPUT PARAM {<name>} <description>\n - OUTPUT PARAM {<name>} <description>\n - POSTPROCESS `{functionName}`\n - JOKER {<name>}\n - EXPECT JSON\n - EXPECT <\"Exactly\"|\"Min\"|\"Max\"> <number> <\"Chars\"|\"Words\"|\"Sentences\"|\"Paragraphs\"|\"Pages\">\n\n ")));
1000
- }
610
+ function countSentences(text) {
611
+ return splitIntoSentences(text).length;
1001
612
  }
1002
613
 
1003
614
  var defaultDiacriticsRemovalMap = [
@@ -1252,674 +863,744 @@
1252
863
  });
1253
864
  }
1254
865
 
1255
- /* tslint:disable */
1256
- function normalizeToKebabCase(sentence) {
866
+ /**
867
+ * Counts number of words in the text
868
+ */
869
+ function countWords(text) {
870
+ text = text.replace(/[\p{Extended_Pictographic}]/gu, 'a');
871
+ text = removeDiacritics(text);
872
+ return text.split(/[^a-zа-я0-9]+/i).filter(function (word) { return word.length > 0; }).length;
873
+ }
874
+
875
+ /**
876
+ * Index of all counter functions
877
+ */
878
+ var CountUtils = {
879
+ CHARACTERS: countCharacters,
880
+ WORDS: countWords,
881
+ SENTENCES: countSentences,
882
+ PARAGRAPHS: countParagraphs,
883
+ LINES: countLines,
884
+ PAGES: countPages,
885
+ };
886
+
887
+ /**
888
+ * Function checkExpectations will check if the expectations on given value are met
889
+ *
890
+ * Note: There are two simmilar functions:
891
+ * - `checkExpectations` which throws an error if the expectations are not met
892
+ * - `isPassingExpectations` which returns a boolean
893
+ *
894
+ * @throws {ExpectError} if the expectations are not met
895
+ * @returns {void} Nothing
896
+ */
897
+ function checkExpectations(expectations, value) {
1257
898
  var e_1, _a;
1258
- sentence = removeDiacritics(sentence);
1259
- var charType;
1260
- var lastCharType = 'OTHER';
1261
- var normalizedName = '';
1262
899
  try {
1263
- for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
1264
- var char = sentence_1_1.value;
1265
- var normalizedChar = void 0;
1266
- if (/^[a-z]$/.test(char)) {
1267
- charType = 'LOWERCASE';
1268
- normalizedChar = char;
1269
- }
1270
- else if (/^[A-Z]$/.test(char)) {
1271
- charType = 'UPPERCASE';
1272
- normalizedChar = char.toLowerCase();
1273
- }
1274
- else if (/^[0-9]$/.test(char)) {
1275
- charType = 'NUMBER';
1276
- normalizedChar = char;
1277
- }
1278
- else if (/^\/$/.test(char)) {
1279
- charType = 'SLASH';
1280
- normalizedChar = char;
1281
- }
1282
- else {
1283
- charType = 'OTHER';
1284
- normalizedChar = '-';
900
+ for (var _b = __values(Object.entries(expectations)), _c = _b.next(); !_c.done; _c = _b.next()) {
901
+ var _d = __read(_c.value, 2), unit = _d[0], _e = _d[1], max = _e.max, min = _e.min;
902
+ var amount = CountUtils[unit.toUpperCase()](value);
903
+ if (min && amount < min) {
904
+ throw new ExpectError("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
905
+ } /* not else */
906
+ if (max && amount > max) {
907
+ throw new ExpectError("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
1285
908
  }
1286
- if (charType !== lastCharType &&
1287
- !(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
1288
- !(lastCharType === 'NUMBER') &&
1289
- !(charType === 'NUMBER')) {
1290
- normalizedName += '-';
1291
- }
1292
- normalizedName += normalizedChar;
1293
- lastCharType = charType;
1294
909
  }
1295
910
  }
1296
911
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
1297
912
  finally {
1298
913
  try {
1299
- if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
914
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1300
915
  }
1301
916
  finally { if (e_1) throw e_1.error; }
1302
917
  }
1303
- normalizedName = normalizedName.split(/-+/g).join('-');
1304
- normalizedName = normalizedName.split(/-?\/-?/g).join('/');
1305
- normalizedName = normalizedName.replace(/^-/, '');
1306
- normalizedName = normalizedName.replace(/-$/, '');
1307
- return normalizedName;
1308
918
  }
919
+ /**
920
+ * TODO: [💝] Unite object for expecting amount and format
921
+ */
1309
922
 
1310
923
  /**
1311
- * Removes emojis from a string and fix whitespaces
924
+ * This error occurs during the parameter replacement in the template
1312
925
  *
1313
- * @param text with emojis
1314
- * @returns text without emojis
926
+ * Note: This is a kindof subtype of PromptbookExecutionError because it occurs during the execution of the pipeline
1315
927
  */
1316
- function removeEmojis(text) {
1317
- // Replace emojis (and also ZWJ sequence) with hyphens
1318
- text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
1319
- text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
1320
- text = text.replace(/(\p{Extended_Pictographic})(\u{200D}\p{Extended_Pictographic})*/gu, '$1');
1321
- text = text.replace(/\p{Extended_Pictographic}/gu, '');
1322
- return text;
1323
- }
928
+ var TemplateError = /** @class */ (function (_super) {
929
+ __extends(TemplateError, _super);
930
+ function TemplateError(message) {
931
+ var _this = _super.call(this, message) || this;
932
+ _this.name = 'TemplateError';
933
+ Object.setPrototypeOf(_this, TemplateError.prototype);
934
+ return _this;
935
+ }
936
+ return TemplateError;
937
+ }(Error));
1324
938
 
1325
939
  /**
1326
- * Function normalizes title to name which can be used as identifier
940
+ * Replaces parameters in template with values from parameters object
941
+ *
942
+ * @param template the template with parameters in {curly} braces
943
+ * @param parameters the object with parameters
944
+ * @returns the template with replaced parameters
945
+ * @throws {TemplateError} if parameter is not defined, not closed, or not opened
946
+ *
947
+ * @private within the createPromptbookExecutor
1327
948
  */
1328
- function titleToName(value) {
1329
- value = removeEmojis(value);
1330
- value = normalizeToKebabCase(value);
1331
- // TODO: [🧠] Maybe warn or add some padding to short name which are not good identifiers
1332
- return value;
949
+ function replaceParameters(template, parameters) {
950
+ var replacedTemplate = template;
951
+ var match;
952
+ var loopLimit = LOOP_LIMIT;
953
+ var _loop_1 = function () {
954
+ if (loopLimit-- < 0) {
955
+ throw new UnexpectedError('Loop limit reached during parameters replacement in `replaceParameters`');
956
+ }
957
+ var precol = match.groups.precol;
958
+ var parameterName = match.groups.parameterName;
959
+ if (parameterName === '') {
960
+ return "continue";
961
+ }
962
+ if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
963
+ throw new TemplateError('Parameter is already opened or not closed');
964
+ }
965
+ if (parameters[parameterName] === undefined) {
966
+ throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
967
+ }
968
+ var parameterValue = parameters[parameterName];
969
+ if (parameterValue === undefined) {
970
+ throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
971
+ }
972
+ parameterValue = parameterValue.toString();
973
+ if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
974
+ parameterValue = parameterValue
975
+ .split('\n')
976
+ .map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
977
+ .join('\n');
978
+ }
979
+ replacedTemplate =
980
+ replacedTemplate.substring(0, match.index + precol.length) +
981
+ parameterValue +
982
+ replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
983
+ };
984
+ while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
985
+ .exec(replacedTemplate))) {
986
+ _loop_1();
987
+ }
988
+ // [💫] Check if there are parameters that are not closed properly
989
+ if (/{\w+$/.test(replacedTemplate)) {
990
+ throw new TemplateError('Parameter is not closed');
991
+ }
992
+ // [💫] Check if there are parameters that are not opened properly
993
+ if (/^\w+}/.test(replacedTemplate)) {
994
+ throw new TemplateError('Parameter is not opened');
995
+ }
996
+ return replacedTemplate;
1333
997
  }
1334
998
 
1335
999
  /**
1336
- * Compile promptbook from string (markdown) format to JSON format
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)
1341
- * @throws {PromptbookSyntaxError} if the promptbook string is not valid
1000
+ * Creates executor function from promptbook and execution tools.
1342
1001
  *
1343
- * Note: This function does not validate logic of the pipeline only the syntax
1344
- * Note: This function acts as compilation process
1002
+ * @returns The executor function
1003
+ * @throws {PromptbookLogicError} on logical error in the promptbook
1345
1004
  */
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 = {
1005
+ function createPromptbookExecutor(options) {
1006
+ var _this = this;
1007
+ var promptbook = options.promptbook, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
1008
+ var _b = settings.maxExecutionAttempts, maxExecutionAttempts = _b === void 0 ? 3 : _b;
1009
+ validatePromptbookJson(promptbook);
1010
+ var promptbookExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
1011
+ function executeSingleTemplate(currentTemplate) {
1012
+ return __awaiter(this, void 0, void 0, function () {
1013
+ var name, title, priority, prompt, chatThread, completionResult, result, resultString, expectError, scriptExecutionErrors, maxAttempts, jokers, attempt, isJokerAttempt, joker, _a, _b, _c, _d, scriptTools, error_2, e_2_1, _e, _f, functionName, postprocessingError, _g, _h, scriptTools, error_3, e_3_1, e_4_1, error_4;
1014
+ var e_2, _j, e_4, _k, e_3, _l, _m;
1015
+ var _this = this;
1016
+ return __generator(this, function (_o) {
1017
+ switch (_o.label) {
1018
+ case 0:
1019
+ name = "promptbook-executor-frame-".concat(currentTemplate.name);
1020
+ title = currentTemplate.title;
1021
+ priority = promptbook.promptTemplates.length - promptbook.promptTemplates.indexOf(currentTemplate);
1022
+ if (!onProgress /* <- [3] */) return [3 /*break*/, 2]; /* <- [3] */
1023
+ return [4 /*yield*/, onProgress({
1024
+ name: name,
1025
+ title: title,
1026
+ isStarted: false,
1027
+ isDone: false,
1028
+ executionType: currentTemplate.executionType,
1029
+ parameterName: currentTemplate.resultingParameterName,
1030
+ parameterValue: null,
1031
+ // <- [3]
1032
+ })];
1033
+ case 1:
1034
+ _o.sent();
1035
+ _o.label = 2;
1036
+ case 2:
1037
+ result = null;
1038
+ resultString = null;
1039
+ expectError = null;
1040
+ maxAttempts = currentTemplate.executionType === 'PROMPT_DIALOG' ? Infinity : maxExecutionAttempts;
1041
+ jokers = currentTemplate.jokers || [];
1042
+ attempt = -jokers.length;
1043
+ _o.label = 3;
1044
+ case 3:
1045
+ if (!(attempt < maxAttempts)) return [3 /*break*/, 49];
1046
+ isJokerAttempt = attempt < 0;
1047
+ joker = jokers[jokers.length + attempt];
1048
+ if (isJokerAttempt && !joker) {
1049
+ throw new UnexpectedError("Joker not found in attempt ".concat(attempt));
1050
+ }
1051
+ result = null;
1052
+ resultString = null;
1053
+ expectError = null;
1054
+ if (isJokerAttempt) {
1055
+ if (typeof parametersToPass[joker] === 'undefined') {
1056
+ throw new PromptbookExecutionError("Joker parameter {".concat(joker, "} not defined"));
1057
+ }
1058
+ resultString = parametersToPass[joker];
1059
+ }
1060
+ _o.label = 4;
1061
+ case 4:
1062
+ _o.trys.push([4, 45, 46, 47]);
1063
+ if (!!isJokerAttempt) return [3 /*break*/, 27];
1064
+ _a = currentTemplate.executionType;
1065
+ switch (_a) {
1066
+ case 'SIMPLE_TEMPLATE': return [3 /*break*/, 5];
1067
+ case 'PROMPT_TEMPLATE': return [3 /*break*/, 6];
1068
+ case 'SCRIPT': return [3 /*break*/, 13];
1069
+ case 'PROMPT_DIALOG': return [3 /*break*/, 24];
1070
+ }
1071
+ return [3 /*break*/, 26];
1072
+ case 5:
1073
+ resultString = replaceParameters(currentTemplate.content, parametersToPass);
1074
+ return [3 /*break*/, 27];
1075
+ case 6:
1076
+ prompt = {
1077
+ title: currentTemplate.title,
1078
+ promptbookUrl: "".concat(promptbook.promptbookUrl
1079
+ ? promptbook.promptbookUrl
1080
+ : 'anonymous' /* <- TODO: [🧠] How to deal with anonymous PROMPTBOOKs, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
1081
+ parameters: parametersToPass,
1082
+ content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
1083
+ modelRequirements: currentTemplate.modelRequirements,
1084
+ expectations: currentTemplate.expectations,
1085
+ expectFormat: currentTemplate.expectFormat,
1086
+ postprocessing: (currentTemplate.postprocessing || []).map(function (functionName) { return function (result) { return __awaiter(_this, void 0, void 0, function () {
1087
+ var errors, _a, _b, scriptTools, error_5, e_5_1;
1088
+ var e_5, _c;
1089
+ return __generator(this, function (_d) {
1090
+ switch (_d.label) {
1091
+ case 0:
1092
+ errors = [];
1093
+ _d.label = 1;
1094
+ case 1:
1095
+ _d.trys.push([1, 8, 9, 10]);
1096
+ _a = __values(tools.script), _b = _a.next();
1097
+ _d.label = 2;
1098
+ case 2:
1099
+ if (!!_b.done) return [3 /*break*/, 7];
1100
+ scriptTools = _b.value;
1101
+ _d.label = 3;
1102
+ case 3:
1103
+ _d.trys.push([3, 5, , 6]);
1104
+ return [4 /*yield*/, scriptTools.execute({
1105
+ scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
1106
+ script: "".concat(functionName, "(result)"),
1107
+ parameters: {
1108
+ result: result || '',
1109
+ // Note: No ...parametersToPass, because working with result only
1110
+ },
1111
+ })];
1112
+ case 4: return [2 /*return*/, _d.sent()];
1113
+ case 5:
1114
+ error_5 = _d.sent();
1115
+ if (!(error_5 instanceof Error)) {
1116
+ throw error_5;
1117
+ }
1118
+ errors.push(error_5);
1119
+ return [3 /*break*/, 6];
1120
+ case 6:
1121
+ _b = _a.next();
1122
+ return [3 /*break*/, 2];
1123
+ case 7: return [3 /*break*/, 10];
1124
+ case 8:
1125
+ e_5_1 = _d.sent();
1126
+ e_5 = { error: e_5_1 };
1127
+ return [3 /*break*/, 10];
1128
+ case 9:
1129
+ try {
1130
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
1131
+ }
1132
+ finally { if (e_5) throw e_5.error; }
1133
+ return [7 /*endfinally*/];
1134
+ case 10:
1135
+ if (errors.length === 0) {
1136
+ throw new PromptbookExecutionError('Postprocessing in LlmExecutionTools failed because no ScriptExecutionTools were provided');
1137
+ }
1138
+ else if (errors.length === 1) {
1139
+ throw errors[0];
1140
+ }
1141
+ else {
1142
+ throw new PromptbookExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Postprocessing in LlmExecutionTools failed ".concat(errors.length, "x\n\n ").concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n\n')), "\n "); }));
1143
+ }
1144
+ }
1145
+ });
1146
+ }); }; }),
1147
+ };
1148
+ _b = currentTemplate.modelRequirements.modelVariant;
1149
+ switch (_b) {
1150
+ case 'CHAT': return [3 /*break*/, 7];
1151
+ case 'COMPLETION': return [3 /*break*/, 9];
1152
+ }
1153
+ return [3 /*break*/, 11];
1154
+ case 7: return [4 /*yield*/, tools.llm.gptChat(prompt)];
1155
+ case 8:
1156
+ chatThread = _o.sent();
1157
+ // TODO: [🍬] Destroy chatThread
1158
+ result = chatThread;
1159
+ resultString = chatThread.content;
1160
+ return [3 /*break*/, 12];
1161
+ case 9: return [4 /*yield*/, tools.llm.gptComplete(prompt)];
1162
+ case 10:
1163
+ completionResult = _o.sent();
1164
+ result = completionResult;
1165
+ resultString = completionResult.content;
1166
+ return [3 /*break*/, 12];
1167
+ case 11: throw new PromptbookExecutionError("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
1168
+ case 12: return [3 /*break*/, 27];
1169
+ case 13:
1170
+ if (tools.script.length === 0) {
1171
+ throw new PromptbookExecutionError('No script execution tools are available');
1172
+ }
1173
+ if (!currentTemplate.contentLanguage) {
1174
+ throw new PromptbookExecutionError("Script language is not defined for prompt template \"".concat(currentTemplate.name, "\""));
1175
+ }
1176
+ // TODO: DRY [1]
1177
+ scriptExecutionErrors = [];
1178
+ _o.label = 14;
1179
+ case 14:
1180
+ _o.trys.push([14, 21, 22, 23]);
1181
+ _c = (e_2 = void 0, __values(tools.script)), _d = _c.next();
1182
+ _o.label = 15;
1183
+ case 15:
1184
+ if (!!_d.done) return [3 /*break*/, 20];
1185
+ scriptTools = _d.value;
1186
+ _o.label = 16;
1187
+ case 16:
1188
+ _o.trys.push([16, 18, , 19]);
1189
+ return [4 /*yield*/, scriptTools.execute({
1190
+ scriptLanguage: currentTemplate.contentLanguage,
1191
+ script: currentTemplate.content,
1192
+ parameters: parametersToPass,
1193
+ })];
1194
+ case 17:
1195
+ resultString = _o.sent();
1196
+ return [3 /*break*/, 20];
1197
+ case 18:
1198
+ error_2 = _o.sent();
1199
+ if (!(error_2 instanceof Error)) {
1200
+ throw error_2;
1201
+ }
1202
+ scriptExecutionErrors.push(error_2);
1203
+ return [3 /*break*/, 19];
1204
+ case 19:
1205
+ _d = _c.next();
1206
+ return [3 /*break*/, 15];
1207
+ case 20: return [3 /*break*/, 23];
1208
+ case 21:
1209
+ e_2_1 = _o.sent();
1210
+ e_2 = { error: e_2_1 };
1211
+ return [3 /*break*/, 23];
1212
+ case 22:
1213
+ try {
1214
+ if (_d && !_d.done && (_j = _c.return)) _j.call(_c);
1215
+ }
1216
+ finally { if (e_2) throw e_2.error; }
1217
+ return [7 /*endfinally*/];
1218
+ case 23:
1219
+ if (resultString !== null) {
1220
+ return [3 /*break*/, 27];
1221
+ }
1222
+ if (scriptExecutionErrors.length === 1) {
1223
+ throw scriptExecutionErrors[0];
1224
+ }
1225
+ else {
1226
+ throw new PromptbookExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Script execution failed ".concat(scriptExecutionErrors.length, " times\n\n ").concat(block(scriptExecutionErrors
1227
+ .map(function (error) { return '- ' + error.message; })
1228
+ .join('\n\n')), "\n "); }));
1229
+ }
1230
+ case 24:
1231
+ if (tools.userInterface === undefined) {
1232
+ throw new PromptbookExecutionError('User interface tools are not available');
1233
+ }
1234
+ return [4 /*yield*/, tools.userInterface.promptDialog({
1235
+ promptTitle: currentTemplate.title,
1236
+ promptMessage: replaceParameters(currentTemplate.description || '', parametersToPass),
1237
+ defaultValue: replaceParameters(currentTemplate.content, parametersToPass),
1238
+ // TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
1239
+ placeholder: undefined,
1240
+ priority: priority,
1241
+ })];
1242
+ case 25:
1243
+ // TODO: [🌹] When making next attempt for `PROMPT DIALOG`, preserve the previous user input
1244
+ resultString = _o.sent();
1245
+ return [3 /*break*/, 27];
1246
+ case 26: throw new PromptbookExecutionError(
1356
1247
  // 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: [],
1248
+ "Unknown execution type \"".concat(currentTemplate.executionType, "\""));
1249
+ case 27:
1250
+ if (!(!isJokerAttempt && currentTemplate.postprocessing)) return [3 /*break*/, 44];
1251
+ _o.label = 28;
1252
+ case 28:
1253
+ _o.trys.push([28, 42, 43, 44]);
1254
+ _e = (e_4 = void 0, __values(currentTemplate.postprocessing)), _f = _e.next();
1255
+ _o.label = 29;
1256
+ case 29:
1257
+ if (!!_f.done) return [3 /*break*/, 41];
1258
+ functionName = _f.value;
1259
+ // TODO: DRY [1]
1260
+ scriptExecutionErrors = [];
1261
+ postprocessingError = null;
1262
+ _o.label = 30;
1263
+ case 30:
1264
+ _o.trys.push([30, 37, 38, 39]);
1265
+ _g = (e_3 = void 0, __values(tools.script)), _h = _g.next();
1266
+ _o.label = 31;
1267
+ case 31:
1268
+ if (!!_h.done) return [3 /*break*/, 36];
1269
+ scriptTools = _h.value;
1270
+ _o.label = 32;
1271
+ case 32:
1272
+ _o.trys.push([32, 34, , 35]);
1273
+ return [4 /*yield*/, scriptTools.execute({
1274
+ scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
1275
+ script: "".concat(functionName, "(resultString)"),
1276
+ parameters: {
1277
+ resultString: resultString || '',
1278
+ // Note: No ...parametersToPass, because working with result only
1279
+ },
1280
+ })];
1281
+ case 33:
1282
+ resultString = _o.sent();
1283
+ postprocessingError = null;
1284
+ return [3 /*break*/, 36];
1285
+ case 34:
1286
+ error_3 = _o.sent();
1287
+ if (!(error_3 instanceof Error)) {
1288
+ throw error_3;
1289
+ }
1290
+ postprocessingError = error_3;
1291
+ scriptExecutionErrors.push(error_3);
1292
+ return [3 /*break*/, 35];
1293
+ case 35:
1294
+ _h = _g.next();
1295
+ return [3 /*break*/, 31];
1296
+ case 36: return [3 /*break*/, 39];
1297
+ case 37:
1298
+ e_3_1 = _o.sent();
1299
+ e_3 = { error: e_3_1 };
1300
+ return [3 /*break*/, 39];
1301
+ case 38:
1302
+ try {
1303
+ if (_h && !_h.done && (_l = _g.return)) _l.call(_g);
1304
+ }
1305
+ finally { if (e_3) throw e_3.error; }
1306
+ return [7 /*endfinally*/];
1307
+ case 39:
1308
+ if (postprocessingError) {
1309
+ throw postprocessingError;
1310
+ }
1311
+ _o.label = 40;
1312
+ case 40:
1313
+ _f = _e.next();
1314
+ return [3 /*break*/, 29];
1315
+ case 41: return [3 /*break*/, 44];
1316
+ case 42:
1317
+ e_4_1 = _o.sent();
1318
+ e_4 = { error: e_4_1 };
1319
+ return [3 /*break*/, 44];
1320
+ case 43:
1321
+ try {
1322
+ if (_f && !_f.done && (_k = _e.return)) _k.call(_e);
1323
+ }
1324
+ finally { if (e_4) throw e_4.error; }
1325
+ return [7 /*endfinally*/];
1326
+ case 44:
1327
+ // TODO: [💝] Unite object for expecting amount and format
1328
+ if (currentTemplate.expectFormat) {
1329
+ if (currentTemplate.expectFormat === 'JSON') {
1330
+ if (!isValidJsonString(resultString || '')) {
1331
+ throw new ExpectError('Expected valid JSON string');
1332
+ }
1333
+ }
1334
+ }
1335
+ // TODO: [💝] Unite object for expecting amount and format
1336
+ if (currentTemplate.expectations) {
1337
+ checkExpectations(currentTemplate.expectations, resultString || '');
1338
+ }
1339
+ return [3 /*break*/, 49];
1340
+ case 45:
1341
+ error_4 = _o.sent();
1342
+ if (!(error_4 instanceof ExpectError)) {
1343
+ throw error_4;
1344
+ }
1345
+ expectError = error_4;
1346
+ return [3 /*break*/, 47];
1347
+ case 46:
1348
+ if (!isJokerAttempt &&
1349
+ currentTemplate.executionType === 'PROMPT_TEMPLATE' &&
1350
+ prompt
1351
+ // <- Note: [2] When some expected parameter is not defined, error will occur in replaceParameters
1352
+ // In that case we don’t want to make a report about it because it’s not a llm execution error
1353
+ ) {
1354
+ // TODO: [🧠] Maybe put other executionTypes into report
1355
+ executionReport.promptExecutions.push({
1356
+ prompt: {
1357
+ title: currentTemplate.title /* <- Note: If title in promptbook contains emojis, pass it innto report */,
1358
+ content: prompt.content,
1359
+ modelRequirements: prompt.modelRequirements,
1360
+ expectations: prompt.expectations,
1361
+ expectFormat: prompt.expectFormat,
1362
+ // <- Note: Do want to pass ONLY wanted information to the report
1363
+ },
1364
+ result: result || undefined,
1365
+ error: expectError || undefined,
1366
+ });
1367
+ }
1368
+ return [7 /*endfinally*/];
1369
+ case 47:
1370
+ if (expectError !== null && attempt === maxAttempts - 1) {
1371
+ throw new PromptbookExecutionError(spaceTrim.spaceTrim(function (block) { return "\n LLM execution failed ".concat(maxExecutionAttempts, "x\n\n ---\n Last error ").concat((expectError === null || expectError === void 0 ? void 0 : expectError.name) || '', ":\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n\n Last result:\n ").concat(resultString, "\n ---\n "); }));
1372
+ }
1373
+ _o.label = 48;
1374
+ case 48:
1375
+ attempt++;
1376
+ return [3 /*break*/, 3];
1377
+ case 49:
1378
+ if (resultString === null) {
1379
+ throw new UnexpectedError('Something went wrong and prompt result is null');
1380
+ }
1381
+ if (onProgress /* <- [3] */) {
1382
+ onProgress({
1383
+ name: name,
1384
+ title: title,
1385
+ isStarted: true,
1386
+ isDone: true,
1387
+ executionType: currentTemplate.executionType,
1388
+ parameterName: currentTemplate.resultingParameterName,
1389
+ parameterValue: resultString,
1390
+ // <- [3]
1391
+ });
1392
+ }
1393
+ parametersToPass = __assign(__assign({}, parametersToPass), (_m = {}, _m[currentTemplate.resultingParameterName] = resultString /* <- Note: Not need to detect parameter collision here because Promptbook checks logic consistency during construction */, _m));
1394
+ return [2 /*return*/];
1395
+ }
1396
+ });
1397
+ });
1398
+ }
1399
+ var parametersToPass, executionReport, resovedParameters_1, unresovedTemplates, resolving_1, loopLimit, _loop_1, error_1, usage_1, _a, _b, parameter, usage;
1400
+ var e_1, _c;
1401
+ return __generator(this, function (_d) {
1402
+ switch (_d.label) {
1403
+ case 0:
1404
+ parametersToPass = inputParameters;
1405
+ executionReport = {
1406
+ promptbookUrl: promptbook.promptbookUrl,
1407
+ title: promptbook.title,
1408
+ promptbookUsedVersion: PROMPTBOOK_VERSION,
1409
+ promptbookRequestedVersion: promptbook.promptbookVersion,
1410
+ description: promptbook.description,
1411
+ promptExecutions: [],
1364
1412
  };
1365
- if (!llmTools) return [3 /*break*/, 2];
1366
- return [4 /*yield*/, prepareKnowledgeFromMarkdown()];
1413
+ _d.label = 1;
1367
1414
  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 "); }));
1385
- }
1386
- if (existingParameter) {
1387
- if (parameterDescription) {
1388
- existingParameter.description = parameterDescription;
1415
+ _d.trys.push([1, 6, , 7]);
1416
+ resovedParameters_1 = promptbook.parameters
1417
+ .filter(function (_a) {
1418
+ var isInput = _a.isInput;
1419
+ return isInput;
1420
+ })
1421
+ .map(function (_a) {
1422
+ var name = _a.name;
1423
+ return name;
1424
+ });
1425
+ unresovedTemplates = __spreadArray([], __read(promptbook.promptTemplates), false);
1426
+ resolving_1 = [];
1427
+ loopLimit = LOOP_LIMIT;
1428
+ _loop_1 = function () {
1429
+ var currentTemplate, work_1;
1430
+ return __generator(this, function (_e) {
1431
+ switch (_e.label) {
1432
+ case 0:
1433
+ if (loopLimit-- < 0) {
1434
+ throw new UnexpectedError('Loop limit reached during resolving parameters promptbook execution');
1435
+ }
1436
+ currentTemplate = unresovedTemplates.find(function (template) {
1437
+ return template.dependentParameterNames.every(function (name) { return resovedParameters_1.includes(name); });
1438
+ });
1439
+ if (!(!currentTemplate && resolving_1.length === 0)) return [3 /*break*/, 1];
1440
+ throw new UnexpectedError(spaceTrim.spaceTrim("\n Can not resolve some parameters\n\n Note: This should be catched during validatePromptbookJson\n "));
1441
+ case 1:
1442
+ if (!!currentTemplate) return [3 /*break*/, 3];
1443
+ /* [5] */ return [4 /*yield*/, Promise.race(resolving_1)];
1444
+ case 2:
1445
+ /* [5] */ _e.sent();
1446
+ return [3 /*break*/, 4];
1447
+ case 3:
1448
+ unresovedTemplates = unresovedTemplates.filter(function (template) { return template !== currentTemplate; });
1449
+ work_1 = executeSingleTemplate(currentTemplate)
1450
+ .then(function () {
1451
+ resovedParameters_1 = __spreadArray(__spreadArray([], __read(resovedParameters_1), false), [currentTemplate.resultingParameterName], false);
1452
+ })
1453
+ .then(function () {
1454
+ resolving_1 = resolving_1.filter(function (w) { return w !== work_1; });
1455
+ });
1456
+ resolving_1.push(work_1);
1457
+ _e.label = 4;
1458
+ case 4: return [2 /*return*/];
1389
1459
  }
1390
- }
1391
- else {
1392
- promptbookJson.parameters.push({
1393
- name: parameterName,
1394
- description: parameterDescription || undefined,
1395
- isInput: isInput,
1396
- isOutput: isOutput,
1397
- });
1398
- }
1460
+ });
1399
1461
  };
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;
1462
+ _d.label = 2;
1463
+ case 2:
1464
+ if (!(unresovedTemplates.length > 0)) return [3 /*break*/, 4];
1465
+ return [5 /*yield**/, _loop_1()];
1466
+ case 3:
1467
+ _d.sent();
1468
+ return [3 /*break*/, 2];
1469
+ case 4: return [4 /*yield*/, Promise.all(resolving_1)];
1470
+ case 5:
1471
+ _d.sent();
1472
+ return [3 /*break*/, 7];
1473
+ case 6:
1474
+ error_1 = _d.sent();
1475
+ if (!(error_1 instanceof Error)) {
1476
+ throw error_1;
1414
1477
  }
1415
- promptbookJson.description = description;
1416
- defaultModelRequirements = {};
1417
- listItems = extractAllListItemsFromMarkdown(markdownStructure.content);
1478
+ usage_1 = addUsage.apply(void 0, __spreadArray([], __read(executionReport.promptExecutions.map(function (_a) {
1479
+ var result = _a.result;
1480
+ return (result === null || result === void 0 ? void 0 : result.usage) || addUsage();
1481
+ })), false));
1482
+ return [2 /*return*/, {
1483
+ isSuccessful: false,
1484
+ errors: [error_1],
1485
+ usage: usage_1,
1486
+ executionReport: executionReport,
1487
+ outputParameters: parametersToPass,
1488
+ }];
1489
+ case 7:
1418
1490
  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"));
1491
+ // Note: Filter ONLY output parameters
1492
+ for (_a = __values(promptbook.parameters), _b = _a.next(); !_b.done; _b = _a.next()) {
1493
+ parameter = _b.value;
1494
+ if (parameter.isOutput) {
1495
+ continue;
1437
1496
  }
1497
+ delete parametersToPass[parameter.name];
1438
1498
  }
1439
1499
  }
1440
1500
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
1441
1501
  finally {
1442
1502
  try {
1443
- if (listItems_1_1 && !listItems_1_1.done && (_c = listItems_1.return)) _c.call(listItems_1);
1503
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
1444
1504
  }
1445
1505
  finally { if (e_1) throw e_1.error; }
1446
1506
  }
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];
1507
+ usage = addUsage.apply(void 0, __spreadArray([], __read(executionReport.promptExecutions.map(function (_a) {
1508
+ var result = _a.result;
1509
+ return (result === null || result === void 0 ? void 0 : result.usage) || addUsage();
1510
+ })), false));
1511
+ return [2 /*return*/, {
1512
+ isSuccessful: true,
1513
+ errors: [],
1514
+ usage: usage,
1515
+ executionReport: executionReport,
1516
+ outputParameters: parametersToPass,
1517
+ }];
1594
1518
  }
1595
1519
  });
1596
- });
1520
+ }); };
1521
+ return promptbookExecutor;
1597
1522
  }
1598
1523
  /**
1599
- * TODO: Report here line/column of error
1600
- * TODO: Use spaceTrim more effectively
1601
- * TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
1602
- */
1603
-
1604
- /**
1605
- * This error indicates that the promptbook object has valid syntax but contains logical errors (like circular dependencies)
1524
+ * TODO: [🧠] When not meet expectations in PROMPT_DIALOG, make some way to tell the user
1525
+ * TODO: [👧] Strongly type the executors to avoid need of remove nullables whtn noUncheckedIndexedAccess in tsconfig.json
1526
+ * Note: CreatePromptbookExecutorOptions are just connected to PromptbookExecutor so do not extract to types folder
1527
+ * TODO: [🧠][3] transparent = (report intermediate parameters) / opaque execution = (report only output parameters) progress reporting mode
1606
1528
  */
1607
- var PromptbookLogicError = /** @class */ (function (_super) {
1608
- __extends(PromptbookLogicError, _super);
1609
- function PromptbookLogicError(message) {
1610
- var _this = _super.call(this, message) || this;
1611
- _this.name = 'PromptbookLogicError';
1612
- Object.setPrototypeOf(_this, PromptbookLogicError.prototype);
1613
- return _this;
1614
- }
1615
- return PromptbookLogicError;
1616
- }(Error));
1617
1529
 
1618
1530
  /**
1619
- * Tests if given string is valid URL.
1531
+ * Prettify the html code
1620
1532
  *
1621
- * Note: Dataurl are considered perfectly valid.
1533
+ * @param content raw html code
1534
+ * @returns formatted html code
1622
1535
  */
1623
- function isValidUrl(url) {
1624
- if (typeof url !== 'string') {
1625
- return false;
1626
- }
1536
+ function prettifyMarkdown(content) {
1627
1537
  try {
1628
- if (url.startsWith('blob:')) {
1629
- url = url.replace(/^blob:/, '');
1630
- }
1631
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1632
- var urlObject = new URL(url);
1633
- if (!['http:', 'https:', 'data:'].includes(urlObject.protocol)) {
1634
- return false;
1635
- }
1636
- return true;
1538
+ return prettier.format(content, {
1539
+ parser: 'markdown',
1540
+ plugins: [parserHtml__default["default"]],
1541
+ // TODO: DRY - make some import or auto-copy of .prettierrc
1542
+ endOfLine: 'lf',
1543
+ tabWidth: 4,
1544
+ singleQuote: true,
1545
+ trailingComma: 'all',
1546
+ arrowParens: 'always',
1547
+ printWidth: 120,
1548
+ htmlWhitespaceSensitivity: 'ignore',
1549
+ jsxBracketSameLine: false,
1550
+ bracketSpacing: true,
1551
+ });
1637
1552
  }
1638
1553
  catch (error) {
1639
- return false;
1554
+ console.error('There was an error with prettifying the markdown, using the original as the fallback', {
1555
+ error: error,
1556
+ html: content,
1557
+ });
1558
+ return content;
1640
1559
  }
1641
1560
  }
1642
1561
 
1643
1562
  /**
1644
- * Validates PromptbookJson if it is logically valid
1645
- *
1646
- * It checks:
1647
- * - if it has correct parameters dependency
1563
+ * Makes first letter of a string uppercase
1648
1564
  *
1649
- * It does NOT check:
1650
- * - if it is valid json
1651
- * - if it is meaningful
1565
+ */
1566
+ function capitalize(word) {
1567
+ return word.substring(0, 1).toUpperCase() + word.substring(1);
1568
+ }
1569
+
1570
+ /**
1571
+ * Converts promptbook in JSON format to string format
1652
1572
  *
1653
- * @param promptbook valid or invalid PromptbookJson
1654
- * @returns the same promptbook if it is logically valid
1655
- * @throws {PromptbookLogicError} on logical error in the promptbook
1573
+ * @param promptbookJson Promptbook in JSON format (.ptbk.json)
1574
+ * @returns Promptbook in string format (.ptbk.md)
1656
1575
  */
1657
- function validatePromptbookJson(promptbook) {
1658
- // TODO: [🧠] Maybe test if promptbook is a promise and make specific error case for that
1659
- var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
1660
- if (promptbook.promptbookUrl !== undefined) {
1661
- if (!isValidUrl(promptbook.promptbookUrl)) {
1662
- // TODO: This should be maybe the syntax error detected during parsing
1663
- throw new PromptbookLogicError("Invalid promptbook URL \"".concat(promptbook.promptbookUrl, "\""));
1664
- }
1665
- }
1666
- // TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
1667
- if (!Array.isArray(promptbook.parameters)) {
1668
- // TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
1669
- throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Promptbook is valid JSON but with wrong structure\n\n promptbook.parameters expected to be an array, but got ".concat(typeof promptbook.parameters, "\n ")));
1576
+ function promptbookJsonToString(promptbookJson) {
1577
+ var e_1, _a, e_2, _b, e_3, _c, e_4, _d, e_5, _e, e_6, _f;
1578
+ var title = promptbookJson.title, promptbookUrl = promptbookJson.promptbookUrl, promptbookVersion = promptbookJson.promptbookVersion, description = promptbookJson.description, parameters = promptbookJson.parameters, promptTemplates = promptbookJson.promptTemplates;
1579
+ var promptbookString = "# ".concat(title);
1580
+ if (description) {
1581
+ promptbookString += '\n\n';
1582
+ promptbookString += description;
1670
1583
  }
1671
- // TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
1672
- if (!Array.isArray(promptbook.promptTemplates)) {
1673
- // TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
1674
- throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Promptbook is valid JSON but with wrong structure\n\n promptbook.promptTemplates expected to be an array, but got ".concat(typeof promptbook.promptTemplates, "\n ")));
1584
+ // TODO:> const commands: Array<Command>
1585
+ var commands = [];
1586
+ if (promptbookUrl) {
1587
+ commands.push("PROMPTBOOK URL ".concat(promptbookUrl));
1675
1588
  }
1676
- var _loop_1 = function (parameter) {
1677
- if (parameter.isInput && parameter.isOutput) {
1678
- throw new PromptbookLogicError("Parameter {".concat(parameter.name, "} can not be both input and output"));
1679
- }
1680
- // Note: Testing that parameter is either intermediate or output BUT not created and unused
1681
- if (!parameter.isInput &&
1682
- !parameter.isOutput &&
1683
- !promptbook.promptTemplates.some(function (template) { return template.dependentParameterNames.includes(parameter.name); })) {
1684
- throw new PromptbookLogicError(spaceTrim.spaceTrim("\n Parameter {".concat(parameter.name, "} is created but not used\n\n You can declare {").concat(parameter.name, "} as output parameter by adding in the header:\n - OUTPUT PARAMETER `{").concat(parameter.name, "}` ").concat(parameter.description || '', "\n\n ")));
1685
- }
1686
- // Note: Testing that parameter is either input or result of some template
1687
- if (!parameter.isInput &&
1688
- !promptbook.promptTemplates.some(function (template) { return template.resultingParameterName === parameter.name; })) {
1689
- throw new PromptbookLogicError(spaceTrim.spaceTrim("\n Parameter {".concat(parameter.name, "} is declared but not defined\n\n You can do one of these:\n - Remove declaration of {").concat(parameter.name, "}\n - Add prompt template that results in -> {").concat(parameter.name, "}\n\n ")));
1690
- }
1691
- };
1589
+ commands.push("PROMPTBOOK VERSION ".concat(promptbookVersion));
1590
+ promptbookString = prettifyMarkdown(promptbookString);
1692
1591
  try {
1693
- // Note: Check each parameter individually
1694
- for (var _e = __values(promptbook.parameters), _f = _e.next(); !_f.done; _f = _e.next()) {
1695
- var parameter = _f.value;
1696
- _loop_1(parameter);
1592
+ for (var _g = __values(parameters.filter(function (_a) {
1593
+ var isInput = _a.isInput;
1594
+ return isInput;
1595
+ })), _h = _g.next(); !_h.done; _h = _g.next()) {
1596
+ var parameter = _h.value;
1597
+ commands.push("INPUT PARAMETER ".concat(promptTemplateParameterJsonToString(parameter)));
1697
1598
  }
1698
1599
  }
1699
1600
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
1700
1601
  finally {
1701
1602
  try {
1702
- if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
1703
- }
1704
- finally { if (e_1) throw e_1.error; }
1705
- }
1706
- // Note: Check each template individually
1707
- var definedParameters = new Set(promptbook.parameters.filter(function (_a) {
1708
- var isInput = _a.isInput;
1709
- return isInput;
1710
- }).map(function (_a) {
1711
- var name = _a.name;
1712
- return name;
1713
- }));
1714
- try {
1715
- for (var _g = __values(promptbook.promptTemplates), _h = _g.next(); !_h.done; _h = _g.next()) {
1716
- var template = _h.value;
1717
- if (definedParameters.has(template.resultingParameterName)) {
1718
- throw new PromptbookLogicError("Parameter {".concat(template.resultingParameterName, "} is defined multiple times"));
1719
- }
1720
- definedParameters.add(template.resultingParameterName);
1721
- if (template.executionType === 'PROMPT_TEMPLATE' &&
1722
- (template.modelRequirements.modelVariant === undefined)) {
1723
- throw new PromptbookLogicError(spaceTrim.spaceTrim("\n\n You must specify MODEL VARIANT in the prompt template \"".concat(template.title, "\"\n\n For example:\n - MODEL VARIANT Chat\n - MODEL NAME `gpt-4-1106-preview`\n\n ")));
1724
- }
1725
- if (template.jokers && template.jokers.length > 0) {
1726
- if (!template.expectFormat &&
1727
- !template.expectations /* <- TODO: Require at least 1 -> min <- expectation to use jokers */) {
1728
- throw new PromptbookLogicError("Joker parameters are used for {".concat(template.resultingParameterName, "} but no expectations are defined"));
1729
- }
1730
- try {
1731
- for (var _j = (e_3 = void 0, __values(template.jokers)), _k = _j.next(); !_k.done; _k = _j.next()) {
1732
- var joker = _k.value;
1733
- if (!template.dependentParameterNames.includes(joker)) {
1734
- throw new PromptbookLogicError("Parameter {".concat(joker, "} is used for {").concat(template.resultingParameterName, "} as joker but not in dependentParameterNames"));
1735
- }
1736
- }
1737
- }
1738
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
1739
- finally {
1740
- try {
1741
- if (_k && !_k.done && (_c = _j.return)) _c.call(_j);
1742
- }
1743
- finally { if (e_3) throw e_3.error; }
1744
- }
1745
- }
1746
- if (template.expectations) {
1747
- try {
1748
- for (var _l = (e_4 = void 0, __values(Object.entries(template.expectations))), _m = _l.next(); !_m.done; _m = _l.next()) {
1749
- var _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], min = _p.min, max = _p.max;
1750
- if (min !== undefined && max !== undefined && min > max) {
1751
- throw new PromptbookLogicError("Min expectation (=".concat(min, ") of ").concat(unit, " is higher than max expectation (=").concat(max, ")"));
1752
- }
1753
- if (min !== undefined && min < 0) {
1754
- throw new PromptbookLogicError("Min expectation of ".concat(unit, " must be zero or positive"));
1755
- }
1756
- if (max !== undefined && max <= 0) {
1757
- throw new PromptbookLogicError("Max expectation of ".concat(unit, " must be positive"));
1758
- }
1759
- }
1760
- }
1761
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
1762
- finally {
1763
- try {
1764
- if (_m && !_m.done && (_d = _l.return)) _d.call(_l);
1765
- }
1766
- finally { if (e_4) throw e_4.error; }
1767
- }
1768
- }
1769
- }
1770
- }
1771
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
1772
- finally {
1773
- try {
1774
- if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
1775
- }
1776
- finally { if (e_2) throw e_2.error; }
1777
- }
1778
- // Note: Detect circular dependencies
1779
- var resovedParameters = promptbook.parameters
1780
- .filter(function (_a) {
1781
- var isInput = _a.isInput;
1782
- return isInput;
1783
- })
1784
- .map(function (_a) {
1785
- var name = _a.name;
1786
- return name;
1787
- });
1788
- var unresovedTemplates = __spreadArray([], __read(promptbook.promptTemplates), false);
1789
- var loopLimit = LOOP_LIMIT;
1790
- var _loop_2 = function () {
1791
- if (loopLimit-- < 0) {
1792
- throw new UnexpectedError('Loop limit reached during detection of circular dependencies in `validatePromptbookJson`');
1793
- }
1794
- var currentlyResovedTemplates = unresovedTemplates.filter(function (template) {
1795
- return template.dependentParameterNames.every(function (name) { return resovedParameters.includes(name); });
1796
- });
1797
- if (currentlyResovedTemplates.length === 0) {
1798
- throw new PromptbookLogicError(spaceTrim.spaceTrim(function (block) { return "\n\n Can not resolve some parameters\n It may be circular dependencies\n\n Can not resolve:\n ".concat(block(unresovedTemplates
1799
- .map(function (_a) {
1800
- var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
1801
- return "- {".concat(resultingParameterName, "} depends on ").concat(dependentParameterNames
1802
- .map(function (dependentParameterName) { return "{".concat(dependentParameterName, "}"); })
1803
- .join(', '));
1804
- })
1805
- .join('\n')), "\n\n Resolved:\n ").concat(block(resovedParameters.map(function (name) { return "- {".concat(name, "}"); }).join('\n')), "\n "); }));
1806
- }
1807
- resovedParameters = __spreadArray(__spreadArray([], __read(resovedParameters), false), __read(currentlyResovedTemplates.map(function (_a) {
1808
- var resultingParameterName = _a.resultingParameterName;
1809
- return resultingParameterName;
1810
- })), false);
1811
- unresovedTemplates = unresovedTemplates.filter(function (template) { return !currentlyResovedTemplates.includes(template); });
1812
- };
1813
- while (unresovedTemplates.length > 0) {
1814
- _loop_2();
1815
- }
1816
- return promptbook;
1817
- }
1818
- /**
1819
- * TODO: [🧠] Work with promptbookVersion
1820
- * TODO: Use here some json-schema, Zod or something similar and change it to:
1821
- * > /**
1822
- * > * Validates PromptbookJson if it is logically valid.
1823
- * > *
1824
- * > * It checks:
1825
- * > * - it has a valid structure
1826
- * > * - ...
1827
- * > ex port function validatePromptbookJson(promptbook: unknown): asserts promptbook is PromptbookJson {
1828
- */
1829
-
1830
- /**
1831
- * This error indicates that the promptbook library cannot be propperly loaded
1832
- */
1833
- var PromptbookLibraryError = /** @class */ (function (_super) {
1834
- __extends(PromptbookLibraryError, _super);
1835
- function PromptbookLibraryError(message) {
1836
- var _this = _super.call(this, message) || this;
1837
- _this.name = 'PromptbookLibraryError';
1838
- Object.setPrototypeOf(_this, PromptbookLibraryError.prototype);
1839
- return _this;
1840
- }
1841
- return PromptbookLibraryError;
1842
- }(Error));
1843
-
1844
- /**
1845
- * Detects if the code is running in a browser environment in main thread (Not in a web worker)
1846
- */
1847
- new Function("\n try {\n return this === window;\n } catch (e) {\n return false;\n }\n");
1848
- /**
1849
- * Detects if the code is running in a Node.js environment
1850
- */
1851
- var isRunningInNode = new Function("\n try {\n return this === global;\n } catch (e) {\n return false;\n }\n");
1852
- /**
1853
- * Detects if the code is running in a web worker
1854
- */
1855
- new Function("\n try {\n if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {\n return true;\n } else {\n return false;\n }\n } catch (e) {\n return false;\n }\n");
1856
-
1857
- /**
1858
- * Prettify the html code
1859
- *
1860
- * @param content raw html code
1861
- * @returns formatted html code
1862
- */
1863
- function prettifyMarkdown(content) {
1864
- try {
1865
- return prettier.format(content, {
1866
- parser: 'markdown',
1867
- plugins: [parserHtml__default["default"]],
1868
- // TODO: DRY - make some import or auto-copy of .prettierrc
1869
- endOfLine: 'lf',
1870
- tabWidth: 4,
1871
- singleQuote: true,
1872
- trailingComma: 'all',
1873
- arrowParens: 'always',
1874
- printWidth: 120,
1875
- htmlWhitespaceSensitivity: 'ignore',
1876
- jsxBracketSameLine: false,
1877
- bracketSpacing: true,
1878
- });
1879
- }
1880
- catch (error) {
1881
- console.error('There was an error with prettifying the markdown, using the original as the fallback', {
1882
- error: error,
1883
- html: content,
1884
- });
1885
- return content;
1886
- }
1887
- }
1888
-
1889
- /**
1890
- * Converts promptbook in JSON format to string format
1891
- *
1892
- * @param promptbookJson Promptbook in JSON format (.ptbk.json)
1893
- * @returns Promptbook in string format (.ptbk.md)
1894
- */
1895
- function promptbookJsonToString(promptbookJson) {
1896
- var e_1, _a, e_2, _b, e_3, _c, e_4, _d, e_5, _e, e_6, _f;
1897
- var title = promptbookJson.title, promptbookUrl = promptbookJson.promptbookUrl, promptbookVersion = promptbookJson.promptbookVersion, description = promptbookJson.description, parameters = promptbookJson.parameters, promptTemplates = promptbookJson.promptTemplates;
1898
- var promptbookString = "# ".concat(title);
1899
- if (description) {
1900
- promptbookString += '\n\n';
1901
- promptbookString += description;
1902
- }
1903
- // TODO:> const commands: Array<Command>
1904
- var commands = [];
1905
- if (promptbookUrl) {
1906
- commands.push("PROMPTBOOK URL ".concat(promptbookUrl));
1907
- }
1908
- commands.push("PROMPTBOOK VERSION ".concat(promptbookVersion));
1909
- promptbookString = prettifyMarkdown(promptbookString);
1910
- try {
1911
- for (var _g = __values(parameters.filter(function (_a) {
1912
- var isInput = _a.isInput;
1913
- return isInput;
1914
- })), _h = _g.next(); !_h.done; _h = _g.next()) {
1915
- var parameter = _h.value;
1916
- commands.push("INPUT PARAMETER ".concat(promptTemplateParameterJsonToString(parameter)));
1917
- }
1918
- }
1919
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
1920
- finally {
1921
- try {
1922
- if (_h && !_h.done && (_a = _g.return)) _a.call(_g);
1603
+ if (_h && !_h.done && (_a = _g.return)) _a.call(_g);
1923
1604
  }
1924
1605
  finally { if (e_1) throw e_1.error; }
1925
1606
  }
@@ -2011,252 +1692,1535 @@
2011
1692
  catch (e_5_1) { e_5 = { error: e_5_1 }; }
2012
1693
  finally {
2013
1694
  try {
2014
- if (postprocessing_1_1 && !postprocessing_1_1.done && (_e = postprocessing_1.return)) _e.call(postprocessing_1);
2015
- }
2016
- finally { if (e_5) throw e_5.error; }
2017
- }
2018
- } /* not else */
2019
- if (expectations) {
2020
- try {
2021
- for (var _l = (e_6 = void 0, __values(Object.entries(expectations))), _m = _l.next(); !_m.done; _m = _l.next()) {
2022
- var _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], min = _p.min, max = _p.max;
2023
- if (min === max) {
2024
- commands_1.push("EXPECT EXACTLY ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
2025
- }
2026
- else {
2027
- if (min !== undefined) {
2028
- commands_1.push("EXPECT MIN ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
2029
- } /* not else */
2030
- if (max !== undefined) {
2031
- commands_1.push("EXPECT MAX ".concat(max, " ").concat(capitalize(unit + (max > 1 ? 's' : ''))));
2032
- }
1695
+ if (postprocessing_1_1 && !postprocessing_1_1.done && (_e = postprocessing_1.return)) _e.call(postprocessing_1);
1696
+ }
1697
+ finally { if (e_5) throw e_5.error; }
1698
+ }
1699
+ } /* not else */
1700
+ if (expectations) {
1701
+ try {
1702
+ for (var _l = (e_6 = void 0, __values(Object.entries(expectations))), _m = _l.next(); !_m.done; _m = _l.next()) {
1703
+ var _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], min = _p.min, max = _p.max;
1704
+ if (min === max) {
1705
+ commands_1.push("EXPECT EXACTLY ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
1706
+ }
1707
+ else {
1708
+ if (min !== undefined) {
1709
+ commands_1.push("EXPECT MIN ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
1710
+ } /* not else */
1711
+ if (max !== undefined) {
1712
+ commands_1.push("EXPECT MAX ".concat(max, " ").concat(capitalize(unit + (max > 1 ? 's' : ''))));
1713
+ }
1714
+ }
1715
+ }
1716
+ }
1717
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
1718
+ finally {
1719
+ try {
1720
+ if (_m && !_m.done && (_f = _l.return)) _f.call(_l);
1721
+ }
1722
+ finally { if (e_6) throw e_6.error; }
1723
+ }
1724
+ } /* not else */
1725
+ if (expectFormat) {
1726
+ if (expectFormat === 'JSON') {
1727
+ // TODO: @deprecated remove
1728
+ commands_1.push("EXPECT JSON");
1729
+ }
1730
+ } /* not else */
1731
+ promptbookString += '\n\n';
1732
+ promptbookString += commands_1.map(function (command) { return "- ".concat(command); }).join('\n');
1733
+ promptbookString += '\n\n';
1734
+ promptbookString += '```' + contentLanguage;
1735
+ promptbookString += '\n';
1736
+ promptbookString += spaceTrim__default["default"](content);
1737
+ // <- TODO: !!! Escape
1738
+ // <- TODO: [🧠] Some clear strategy how to spaceTrim the blocks
1739
+ promptbookString += '\n';
1740
+ promptbookString += '```';
1741
+ promptbookString += '\n\n';
1742
+ promptbookString += "`-> {".concat(resultingParameterName, "}`"); // <- TODO: !!! If the parameter here has description, add it and use promptTemplateParameterJsonToString
1743
+ }
1744
+ }
1745
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
1746
+ finally {
1747
+ try {
1748
+ if (promptTemplates_1_1 && !promptTemplates_1_1.done && (_c = promptTemplates_1.return)) _c.call(promptTemplates_1);
1749
+ }
1750
+ finally { if (e_3) throw e_3.error; }
1751
+ }
1752
+ return promptbookString;
1753
+ }
1754
+ /**
1755
+ * @private internal util of promptbookJsonToString
1756
+ */
1757
+ function promptTemplateParameterJsonToString(promptTemplateParameterJson) {
1758
+ var name = promptTemplateParameterJson.name, description = promptTemplateParameterJson.description;
1759
+ var parameterString = "{".concat(name, "}");
1760
+ if (description) {
1761
+ parameterString = "".concat(parameterString, " ").concat(description);
1762
+ }
1763
+ return parameterString;
1764
+ }
1765
+ /**
1766
+ * TODO: Escape all
1767
+ */
1768
+
1769
+ /**
1770
+ * This error indicates that promptbook not found in the library
1771
+ */
1772
+ var PromptbookNotFoundError = /** @class */ (function (_super) {
1773
+ __extends(PromptbookNotFoundError, _super);
1774
+ function PromptbookNotFoundError(message) {
1775
+ var _this = _super.call(this, message) || this;
1776
+ _this.name = 'PromptbookNotFoundError';
1777
+ Object.setPrototypeOf(_this, PromptbookNotFoundError.prototype);
1778
+ return _this;
1779
+ }
1780
+ return PromptbookNotFoundError;
1781
+ }(Error));
1782
+
1783
+ /**
1784
+ * This error indicates errors in referencing promptbooks between each other
1785
+ */
1786
+ var PromptbookReferenceError = /** @class */ (function (_super) {
1787
+ __extends(PromptbookReferenceError, _super);
1788
+ function PromptbookReferenceError(message) {
1789
+ var _this = _super.call(this, message) || this;
1790
+ _this.name = 'PromptbookReferenceError';
1791
+ Object.setPrototypeOf(_this, PromptbookReferenceError.prototype);
1792
+ return _this;
1793
+ }
1794
+ return PromptbookReferenceError;
1795
+ }(Error));
1796
+
1797
+ /**
1798
+ * Library of promptbooks that groups together promptbooks for an application.
1799
+ * This implementation is a very thin wrapper around the Array / Map of promptbooks.
1800
+ *
1801
+ * @see https://github.com/webgptorg/promptbook#promptbook-library
1802
+ */
1803
+ var SimplePromptbookLibrary = /** @class */ (function () {
1804
+ /**
1805
+ * Constructs a promptbook library from promptbooks
1806
+ *
1807
+ * @param promptbooks !!!
1808
+ *
1809
+ * Note: During the construction logic of all promptbooks are validated
1810
+ * Note: It is not recommended to use this constructor directly, use `createPromptbookLibraryFromSources` *(or other variant)* instead
1811
+ */
1812
+ function SimplePromptbookLibrary() {
1813
+ var e_1, _a;
1814
+ var promptbooks = [];
1815
+ for (var _i = 0; _i < arguments.length; _i++) {
1816
+ promptbooks[_i] = arguments[_i];
1817
+ }
1818
+ this.library = new Map();
1819
+ try {
1820
+ for (var promptbooks_1 = __values(promptbooks), promptbooks_1_1 = promptbooks_1.next(); !promptbooks_1_1.done; promptbooks_1_1 = promptbooks_1.next()) {
1821
+ var promptbook = promptbooks_1_1.value;
1822
+ if (promptbook.promptbookUrl === undefined) {
1823
+ throw new PromptbookReferenceError(spaceTrim.spaceTrim("\n Promptbook with name \"".concat(promptbook.title, "\" does not have defined URL\n\n Note: Promptbooks without URLs are called anonymous promptbooks\n They can be used as standalone promptbooks, but they cannot be referenced by other promptbooks\n And also they cannot be used in the promptbook library\n\n ")));
1824
+ }
1825
+ validatePromptbookJson(promptbook);
1826
+ // Note: [🦄]
1827
+ if (this.library.has(promptbook.promptbookUrl) &&
1828
+ promptbookJsonToString(promptbook) !==
1829
+ promptbookJsonToString(this.library.get(promptbook.promptbookUrl))) {
1830
+ throw new PromptbookReferenceError(spaceTrim.spaceTrim("\n Promptbook with URL \"".concat(promptbook.promptbookUrl, "\" is already in the library\n\n Note: Promptbooks with the same URL are not allowed\n Note: Automatically check whether the promptbooks are the same BUT they are DIFFERENT\n\n ")));
1831
+ }
1832
+ this.library.set(promptbook.promptbookUrl, promptbook);
1833
+ }
1834
+ }
1835
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1836
+ finally {
1837
+ try {
1838
+ if (promptbooks_1_1 && !promptbooks_1_1.done && (_a = promptbooks_1.return)) _a.call(promptbooks_1);
1839
+ }
1840
+ finally { if (e_1) throw e_1.error; }
1841
+ }
1842
+ }
1843
+ /**
1844
+ * Gets all promptbooks in the library
1845
+ */
1846
+ SimplePromptbookLibrary.prototype.listPromptbooks = function () {
1847
+ return Array.from(this.library.keys());
1848
+ };
1849
+ /**
1850
+ * Gets promptbook by its URL
1851
+ *
1852
+ * Note: This is not a direct fetching from the URL, but a lookup in the library
1853
+ */
1854
+ SimplePromptbookLibrary.prototype.getPromptbookByUrl = function (url) {
1855
+ var _this = this;
1856
+ var promptbook = this.library.get(url);
1857
+ if (!promptbook) {
1858
+ if (this.listPromptbooks().length === 0) {
1859
+ throw new PromptbookNotFoundError(spaceTrim.spaceTrim("\n Promptbook with url \"".concat(url, "\" not found\n\n No promptbooks available\n ")));
1860
+ }
1861
+ throw new PromptbookNotFoundError(spaceTrim.spaceTrim(function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPromptbooks()
1862
+ .map(function (promptbookUrl) { return "- ".concat(promptbookUrl); })
1863
+ .join('\n')), "\n\n "); }));
1864
+ }
1865
+ return promptbook;
1866
+ };
1867
+ /**
1868
+ * Checks whether given prompt was defined in any promptbook in the library
1869
+ */
1870
+ SimplePromptbookLibrary.prototype.isResponsibleForPrompt = function (prompt) {
1871
+ return true;
1872
+ };
1873
+ return SimplePromptbookLibrary;
1874
+ }());
1875
+
1876
+ /**
1877
+ * Creates PromptbookLibrary from array of PromptbookJson or PromptbookString
1878
+ *
1879
+ * Note: You can combine `PromptbookString` (`.ptbk.md`) with `PromptbookJson` BUT it is not recommended
1880
+ * Note: During the construction syntax and logic of all sources are validated
1881
+ *
1882
+ * @param promptbookSources
1883
+ * @returns PromptbookLibrary
1884
+ */
1885
+ function createPromptbookLibraryFromSources() {
1886
+ var promptbookSources = [];
1887
+ for (var _i = 0; _i < arguments.length; _i++) {
1888
+ promptbookSources[_i] = arguments[_i];
1889
+ }
1890
+ return __awaiter(this, void 0, void 0, function () {
1891
+ var promptbooks, promptbookSources_1, promptbookSources_1_1, source, promptbook, e_1_1;
1892
+ var e_1, _a;
1893
+ return __generator(this, function (_b) {
1894
+ switch (_b.label) {
1895
+ case 0:
1896
+ promptbooks = new Array();
1897
+ _b.label = 1;
1898
+ case 1:
1899
+ _b.trys.push([1, 8, 9, 10]);
1900
+ promptbookSources_1 = __values(promptbookSources), promptbookSources_1_1 = promptbookSources_1.next();
1901
+ _b.label = 2;
1902
+ case 2:
1903
+ if (!!promptbookSources_1_1.done) return [3 /*break*/, 7];
1904
+ source = promptbookSources_1_1.value;
1905
+ promptbook = void 0;
1906
+ if (!(typeof source === 'string')) return [3 /*break*/, 4];
1907
+ return [4 /*yield*/, promptbookStringToJson(source)];
1908
+ case 3:
1909
+ // Note: When directly creating from string, no need to validate the source
1910
+ // The validation is performed always before execution
1911
+ promptbook = _b.sent();
1912
+ return [3 /*break*/, 5];
1913
+ case 4:
1914
+ promptbook = source;
1915
+ _b.label = 5;
1916
+ case 5:
1917
+ promptbooks.push(promptbook);
1918
+ _b.label = 6;
1919
+ case 6:
1920
+ promptbookSources_1_1 = promptbookSources_1.next();
1921
+ return [3 /*break*/, 2];
1922
+ case 7: return [3 /*break*/, 10];
1923
+ case 8:
1924
+ e_1_1 = _b.sent();
1925
+ e_1 = { error: e_1_1 };
1926
+ return [3 /*break*/, 10];
1927
+ case 9:
1928
+ try {
1929
+ if (promptbookSources_1_1 && !promptbookSources_1_1.done && (_a = promptbookSources_1.return)) _a.call(promptbookSources_1);
1930
+ }
1931
+ finally { if (e_1) throw e_1.error; }
1932
+ return [7 /*endfinally*/];
1933
+ case 10: return [2 /*return*/, new (SimplePromptbookLibrary.bind.apply(SimplePromptbookLibrary, __spreadArray([void 0], __read(promptbooks), false)))()];
1934
+ }
1935
+ });
1936
+ });
1937
+ }
1938
+ /**
1939
+ * TODO: !!!! [🧠] Library precompilation and do not mix markdown and json promptbooks
1940
+ */
1941
+
1942
+ /* tslint:disable */
1943
+ function normalizeToKebabCase(sentence) {
1944
+ var e_1, _a;
1945
+ sentence = removeDiacritics(sentence);
1946
+ var charType;
1947
+ var lastCharType = 'OTHER';
1948
+ var normalizedName = '';
1949
+ try {
1950
+ for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
1951
+ var char = sentence_1_1.value;
1952
+ var normalizedChar = void 0;
1953
+ if (/^[a-z]$/.test(char)) {
1954
+ charType = 'LOWERCASE';
1955
+ normalizedChar = char;
1956
+ }
1957
+ else if (/^[A-Z]$/.test(char)) {
1958
+ charType = 'UPPERCASE';
1959
+ normalizedChar = char.toLowerCase();
1960
+ }
1961
+ else if (/^[0-9]$/.test(char)) {
1962
+ charType = 'NUMBER';
1963
+ normalizedChar = char;
1964
+ }
1965
+ else if (/^\/$/.test(char)) {
1966
+ charType = 'SLASH';
1967
+ normalizedChar = char;
1968
+ }
1969
+ else {
1970
+ charType = 'OTHER';
1971
+ normalizedChar = '-';
1972
+ }
1973
+ if (charType !== lastCharType &&
1974
+ !(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
1975
+ !(lastCharType === 'NUMBER') &&
1976
+ !(charType === 'NUMBER')) {
1977
+ normalizedName += '-';
1978
+ }
1979
+ normalizedName += normalizedChar;
1980
+ lastCharType = charType;
1981
+ }
1982
+ }
1983
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1984
+ finally {
1985
+ try {
1986
+ if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
1987
+ }
1988
+ finally { if (e_1) throw e_1.error; }
1989
+ }
1990
+ normalizedName = normalizedName.split(/-+/g).join('-');
1991
+ normalizedName = normalizedName.split(/-?\/-?/g).join('/');
1992
+ normalizedName = normalizedName.replace(/^-/, '');
1993
+ normalizedName = normalizedName.replace(/-$/, '');
1994
+ return normalizedName;
1995
+ }
1996
+
1997
+ function prepareKnowledgeFromMarkdown(options) {
1998
+ return __awaiter(this, void 0, void 0, function () {
1999
+ var content, llmTools, library, promptbook, executor, result, outputParameters, knowledgeRaw, knowledgeTextPieces, knowledge;
2000
+ var _this = this;
2001
+ return __generator(this, function (_a) {
2002
+ switch (_a.label) {
2003
+ case 0:
2004
+ content = options.content, llmTools = options.llmTools;
2005
+ return [4 /*yield*/, createPromptbookLibraryFromSources(
2006
+ /* !!!! ...(promptbookLibrary as Array<PromptbookJson>)*/ {
2007
+ title: 'Prepare Knowledge from Markdown',
2008
+ promptbookUrl: 'https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md',
2009
+ promptbookVersion: '0.59.0-5',
2010
+ parameters: [
2011
+ { name: 'content', description: 'Markdown document content', isInput: true, isOutput: false },
2012
+ { name: 'knowledge', description: 'The knowledge JSON object', isInput: false, isOutput: true },
2013
+ ],
2014
+ promptTemplates: [
2015
+ {
2016
+ name: 'knowledge',
2017
+ title: 'Knowledge',
2018
+ dependentParameterNames: ['content'],
2019
+ executionType: 'PROMPT_TEMPLATE',
2020
+ modelRequirements: { modelVariant: 'CHAT', modelName: 'claude-3-opus-20240229' },
2021
+ content: 'You are experienced data researcher, extract the important knowledge from the document.\n\n# Rules\n\n- Make pieces of information concise, clear, and easy to understand\n- One piece of information should be approximately 1 paragraph\n- Divide the paragraphs by markdown horizontal lines ---\n- Omit irrelevant information\n- Group redundant information\n- Write just extracted information, nothing else\n\n# The document\n\nTake information from this document:\n\n> {content}',
2022
+ resultingParameterName: 'knowledge',
2023
+ },
2024
+ ],
2025
+ knowledge: [],
2026
+ })];
2027
+ case 1:
2028
+ library = _a.sent();
2029
+ promptbook = library.getPromptbookByUrl('https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md');
2030
+ executor = createPromptbookExecutor({
2031
+ promptbook: promptbook,
2032
+ tools: {
2033
+ llm: llmTools,
2034
+ script: [
2035
+ /* <- TODO: Allow to just not define script tools */
2036
+ ],
2037
+ },
2038
+ });
2039
+ return [4 /*yield*/, executor({ content: content })];
2040
+ case 2:
2041
+ result = _a.sent();
2042
+ assertsExecutionSuccessful(result);
2043
+ outputParameters = result.outputParameters;
2044
+ knowledgeRaw = outputParameters.knowledge;
2045
+ knowledgeTextPieces = (knowledgeRaw || '').split('\n---\n');
2046
+ return [4 /*yield*/, Promise.all(knowledgeTextPieces.map(function (knowledgeTextPiece, i) { return __awaiter(_this, void 0, void 0, function () {
2047
+ var name, title, content, keywords, index, sources;
2048
+ return __generator(this, function (_a) {
2049
+ name = "piece-".concat(i);
2050
+ title = spaceTrim__default["default"](knowledgeTextPiece.substring(0, 100));
2051
+ content = spaceTrim__default["default"](knowledgeTextPiece);
2052
+ keywords = [];
2053
+ index = [];
2054
+ sources = [];
2055
+ try {
2056
+ // TODO: !!!! Summarize name and title from the content
2057
+ title = spaceTrim__default["default"](knowledgeTextPiece.substring(0, 30));
2058
+ name = normalizeToKebabCase(title);
2059
+ // TODO: !!!! Extract keywords via prompt
2060
+ // TODO: !!!! Index through LLM model
2061
+ // TODO: [🖖] !!!! Make system for sources and identification of sources
2062
+ }
2063
+ catch (error) {
2064
+ console.error(error);
2065
+ }
2066
+ return [2 /*return*/, {
2067
+ name: name,
2068
+ title: title,
2069
+ content: content,
2070
+ keywords: keywords,
2071
+ index: index,
2072
+ sources: sources,
2073
+ }];
2074
+ });
2075
+ }); }))];
2076
+ case 3:
2077
+ knowledge = _a.sent();
2078
+ return [2 /*return*/, knowledge];
2079
+ }
2080
+ });
2081
+ });
2082
+ }
2083
+
2084
+ /**
2085
+ * Supported script languages
2086
+ */
2087
+ var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
2088
+
2089
+ /**
2090
+ * Computes the deepness of the markdown structure.
2091
+ *
2092
+ * @private within the library
2093
+ */
2094
+ function countMarkdownStructureDeepness(markdownStructure) {
2095
+ var e_1, _a;
2096
+ var maxDeepness = 0;
2097
+ try {
2098
+ for (var _b = __values(markdownStructure.sections), _c = _b.next(); !_c.done; _c = _b.next()) {
2099
+ var section = _c.value;
2100
+ maxDeepness = Math.max(maxDeepness, countMarkdownStructureDeepness(section));
2101
+ }
2102
+ }
2103
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2104
+ finally {
2105
+ try {
2106
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
2107
+ }
2108
+ finally { if (e_1) throw e_1.error; }
2109
+ }
2110
+ return maxDeepness + 1;
2111
+ }
2112
+
2113
+ /**
2114
+ * Parse a markdown string into a MarkdownStructure object.
2115
+ *
2116
+ * Note: This function does work with code blocks
2117
+ * Note: This function does not work with markdown comments
2118
+ *
2119
+ * @param markdown The markdown string to parse.
2120
+ * @returns The MarkdownStructure object.
2121
+ *
2122
+ * @private within the library
2123
+ */
2124
+ function markdownToMarkdownStructure(markdown) {
2125
+ var e_1, _a;
2126
+ var lines = markdown.split('\n');
2127
+ var root = { level: 0, title: '', contentLines: [], sections: [], parent: null };
2128
+ var current = root;
2129
+ var isInsideCodeBlock = false;
2130
+ try {
2131
+ for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
2132
+ var line = lines_1_1.value;
2133
+ var headingMatch = line.match(/^(?<mark>#{1,6})\s(?<title>.*)/);
2134
+ if (isInsideCodeBlock || !headingMatch) {
2135
+ if (line.startsWith('```')) {
2136
+ isInsideCodeBlock = !isInsideCodeBlock;
2137
+ }
2138
+ current.contentLines.push(line);
2139
+ }
2140
+ else {
2141
+ var level = headingMatch.groups.mark.length;
2142
+ var title = headingMatch.groups.title.trim();
2143
+ var parent_1 = void 0;
2144
+ if (level > current.level) {
2145
+ // Note: Going deeper (next section is child of current)
2146
+ parent_1 = current;
2147
+ }
2148
+ else {
2149
+ // Note: Going up or staying at the same level (next section is sibling or parent or grandparent,... of current)
2150
+ parent_1 = current;
2151
+ var loopLimit = LOOP_LIMIT;
2152
+ while (parent_1.level !== level - 1) {
2153
+ if (loopLimit-- < 0) {
2154
+ throw new UnexpectedError('Loop limit reached during parsing of markdown structure in `markdownToMarkdownStructure`');
2155
+ }
2156
+ if (parent_1.parent === null /* <- Note: We are in root */) {
2157
+ // [🌻]
2158
+ throw new Error(spaceTrim.spaceTrim("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
2159
+ }
2160
+ parent_1 = parent_1.parent;
2161
+ }
2162
+ }
2163
+ var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
2164
+ parent_1.sections.push(section);
2165
+ current = section;
2166
+ }
2167
+ }
2168
+ }
2169
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2170
+ finally {
2171
+ try {
2172
+ if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
2173
+ }
2174
+ finally { if (e_1) throw e_1.error; }
2175
+ }
2176
+ if (root.sections.length === 1) {
2177
+ var markdownStructure = parsingMarkdownStructureToMarkdownStructure(root.sections[0]);
2178
+ return markdownStructure;
2179
+ }
2180
+ // [🌻]
2181
+ throw new Error('The markdown file must have exactly one top-level section.');
2182
+ // return root;
2183
+ }
2184
+ /**
2185
+ * @private
2186
+ */
2187
+ function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
2188
+ var level = parsingMarkdownStructure.level, title = parsingMarkdownStructure.title, contentLines = parsingMarkdownStructure.contentLines, sections = parsingMarkdownStructure.sections;
2189
+ return {
2190
+ level: level,
2191
+ title: title,
2192
+ content: spaceTrim.spaceTrim(contentLines.join('\n')),
2193
+ sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
2194
+ };
2195
+ }
2196
+
2197
+ /**
2198
+ * Utility function to extract all list items from markdown
2199
+ *
2200
+ * Note: It works with both ul and ol
2201
+ * Note: It omits list items in code blocks
2202
+ * Note: It flattens nested lists
2203
+ * Note: It can not work with html syntax and comments
2204
+ *
2205
+ * @param markdown any valid markdown
2206
+ * @returns
2207
+ */
2208
+ function extractAllListItemsFromMarkdown(markdown) {
2209
+ var e_1, _a;
2210
+ var lines = markdown.split('\n');
2211
+ var listItems = [];
2212
+ var isInCodeBlock = false;
2213
+ try {
2214
+ for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
2215
+ var line = lines_1_1.value;
2216
+ var trimmedLine = line.trim();
2217
+ if (trimmedLine.startsWith('```')) {
2218
+ isInCodeBlock = !isInCodeBlock;
2219
+ }
2220
+ if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
2221
+ var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
2222
+ listItems.push(listItem);
2223
+ }
2224
+ }
2225
+ }
2226
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2227
+ finally {
2228
+ try {
2229
+ if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
2230
+ }
2231
+ finally { if (e_1) throw e_1.error; }
2232
+ }
2233
+ return listItems;
2234
+ }
2235
+
2236
+ /**
2237
+ * Extracts all code blocks from markdown.
2238
+ *
2239
+ * Note: There are 3 simmilar function:
2240
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
2241
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
2242
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
2243
+ *
2244
+ * @param markdown any valid markdown
2245
+ * @returns code blocks with language and content
2246
+ *
2247
+ */
2248
+ function extractAllBlocksFromMarkdown(markdown) {
2249
+ var e_1, _a;
2250
+ var codeBlocks = [];
2251
+ var lines = markdown.split('\n');
2252
+ var currentCodeBlock = null;
2253
+ try {
2254
+ for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
2255
+ var line = lines_1_1.value;
2256
+ if (line.startsWith('```')) {
2257
+ var language = line.slice(3).trim() || null;
2258
+ if (currentCodeBlock === null) {
2259
+ currentCodeBlock = { language: language, content: '' };
2260
+ }
2261
+ else {
2262
+ if (language !== null) {
2263
+ // [🌻]
2264
+ throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
2265
+ }
2266
+ codeBlocks.push(currentCodeBlock);
2267
+ currentCodeBlock = null;
2268
+ }
2269
+ }
2270
+ else if (currentCodeBlock !== null) {
2271
+ if (currentCodeBlock.content !== '') {
2272
+ currentCodeBlock.content += '\n';
2273
+ }
2274
+ currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
2275
+ }
2276
+ }
2277
+ }
2278
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2279
+ finally {
2280
+ try {
2281
+ if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
2282
+ }
2283
+ finally { if (e_1) throw e_1.error; }
2284
+ }
2285
+ if (currentCodeBlock !== null) {
2286
+ // [🌻]
2287
+ throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
2288
+ }
2289
+ return codeBlocks;
2290
+ }
2291
+
2292
+ /**
2293
+ * Extracts exactly ONE code block from markdown.
2294
+ *
2295
+ * Note: If there are multiple or no code blocks the function throws an error
2296
+ *
2297
+ * Note: There are 3 simmilar function:
2298
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
2299
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
2300
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
2301
+ *
2302
+ * @param markdown any valid markdown
2303
+ * @returns code block with language and content
2304
+ */
2305
+ function extractOneBlockFromMarkdown(markdown) {
2306
+ var codeBlocks = extractAllBlocksFromMarkdown(markdown);
2307
+ if (codeBlocks.length !== 1) {
2308
+ // TODO: Report more specific place where the error happened
2309
+ throw new Error(/* <- [🌻] */ 'There should be exactly one code block in the markdown');
2310
+ }
2311
+ return codeBlocks[0];
2312
+ }
2313
+ /***
2314
+ * TODO: [🍓][🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
2315
+ */
2316
+
2317
+ /**
2318
+ * Removes HTML or Markdown comments from a string.
2319
+ *
2320
+ * @param {string} content - The string to remove comments from.
2321
+ * @returns {string} The input string with all comments removed.
2322
+ */
2323
+ function removeContentComments(content) {
2324
+ return spaceTrim.spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
2325
+ }
2326
+
2327
+ /**
2328
+ * Creates a new set with all elements that are present in either set
2329
+ */
2330
+ function union() {
2331
+ var e_1, _a, e_2, _b;
2332
+ var sets = [];
2333
+ for (var _i = 0; _i < arguments.length; _i++) {
2334
+ sets[_i] = arguments[_i];
2335
+ }
2336
+ var union = new Set();
2337
+ try {
2338
+ for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
2339
+ var set = sets_1_1.value;
2340
+ try {
2341
+ for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
2342
+ var item = _d.value;
2343
+ union.add(item);
2344
+ }
2345
+ }
2346
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
2347
+ finally {
2348
+ try {
2349
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
2350
+ }
2351
+ finally { if (e_2) throw e_2.error; }
2352
+ }
2353
+ }
2354
+ }
2355
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2356
+ finally {
2357
+ try {
2358
+ if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
2359
+ }
2360
+ finally { if (e_1) throw e_1.error; }
2361
+ }
2362
+ return union;
2363
+ }
2364
+
2365
+ /**
2366
+ * Parses the template and returns the list of all parameter names
2367
+ *
2368
+ * @param template the template with parameters in {curly} braces
2369
+ * @returns the list of parameter names
2370
+ */
2371
+ function extractParameters(template) {
2372
+ var e_1, _a;
2373
+ var matches = template.matchAll(/{\w+}/g);
2374
+ var parameterNames = new Set();
2375
+ try {
2376
+ for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
2377
+ var match = matches_1_1.value;
2378
+ var parameterName = match[0].slice(1, -1);
2379
+ parameterNames.add(parameterName);
2380
+ }
2381
+ }
2382
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2383
+ finally {
2384
+ try {
2385
+ if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
2386
+ }
2387
+ finally { if (e_1) throw e_1.error; }
2388
+ }
2389
+ return parameterNames;
2390
+ }
2391
+
2392
+ /**
2393
+ * Parses the given script and returns the list of all used variables that are not defined in the script
2394
+ *
2395
+ * @param script from which to extract the variables
2396
+ * @returns the list of variable names
2397
+ * @throws {PromptbookSyntaxError} if the script is invalid
2398
+ */
2399
+ function extractVariables(script) {
2400
+ var variables = new Set();
2401
+ script = "(()=>{".concat(script, "})()");
2402
+ try {
2403
+ for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
2404
+ try {
2405
+ eval(script);
2406
+ }
2407
+ catch (error) {
2408
+ if (!(error instanceof ReferenceError)) {
2409
+ throw error;
2410
+ }
2411
+ var undefinedName = error.message.split(' ')[0];
2412
+ /*
2413
+ Note: Remapping error
2414
+ From: [ReferenceError: thing is not defined],
2415
+ To: [Error: Parameter {thing} is not defined],
2416
+ */
2417
+ if (!undefinedName) {
2418
+ throw error;
2419
+ }
2420
+ if (script.includes(undefinedName + '(')) {
2421
+ script = "const ".concat(undefinedName, " = ()=>'';") + script;
2422
+ }
2423
+ else {
2424
+ variables.add(undefinedName);
2425
+ script = "const ".concat(undefinedName, " = '';") + script;
2426
+ }
2427
+ }
2428
+ }
2429
+ catch (error) {
2430
+ if (!(error instanceof Error)) {
2431
+ throw error;
2432
+ }
2433
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
2434
+ }
2435
+ return variables;
2436
+ }
2437
+ /**
2438
+ * TODO: [🔣] Support for multiple languages - python, java,...
2439
+ */
2440
+
2441
+ /**
2442
+ * Parses the prompt template and returns the set of all used parameters
2443
+ *
2444
+ * @param promptTemplate the template with used parameters
2445
+ * @returns the set of parameter names
2446
+ * @throws {PromptbookSyntaxError} if the script is invalid
2447
+ */
2448
+ function extractParametersFromPromptTemplate(promptTemplate) {
2449
+ var e_1, _a, e_2, _b;
2450
+ var parameterNames = new Set();
2451
+ try {
2452
+ 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()) {
2453
+ var parameterName = _d.value;
2454
+ parameterNames.add(parameterName);
2455
+ }
2456
+ }
2457
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2458
+ finally {
2459
+ try {
2460
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
2461
+ }
2462
+ finally { if (e_1) throw e_1.error; }
2463
+ }
2464
+ if (promptTemplate.executionType === 'SCRIPT') {
2465
+ try {
2466
+ for (var _e = __values(extractVariables(promptTemplate.content)), _f = _e.next(); !_f.done; _f = _e.next()) {
2467
+ var parameterName = _f.value;
2468
+ parameterNames.add(parameterName);
2469
+ }
2470
+ }
2471
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
2472
+ finally {
2473
+ try {
2474
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
2475
+ }
2476
+ finally { if (e_2) throw e_2.error; }
2477
+ }
2478
+ }
2479
+ return parameterNames;
2480
+ }
2481
+ /**
2482
+ * TODO: [🔣] If script require contentLanguage
2483
+ */
2484
+
2485
+ /* tslint:disable */
2486
+ /*
2487
+ TODO: Tests
2488
+ expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: 'Moje tabule' })).toEqual('/VtG7sR9rRJqwNEdM2/Moje tabule');
2489
+ expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: 'ěščřžžýáíúů' })).toEqual('/VtG7sR9rRJqwNEdM2/escrzyaieuu');
2490
+ expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: ' ahoj ' })).toEqual('/VtG7sR9rRJqwNEdM2/ahoj');
2491
+ expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: ' ahoj_ahojAhoj ahoj ' })).toEqual('/VtG7sR9rRJqwNEdM2/ahoj-ahoj-ahoj-ahoj');
2492
+ */
2493
+ function normalizeTo_SCREAMING_CASE(sentence) {
2494
+ var e_1, _a;
2495
+ var charType;
2496
+ var lastCharType = 'OTHER';
2497
+ var normalizedName = '';
2498
+ try {
2499
+ for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
2500
+ var char = sentence_1_1.value;
2501
+ var normalizedChar = void 0;
2502
+ if (/^[a-z]$/.test(char)) {
2503
+ charType = 'LOWERCASE';
2504
+ normalizedChar = char.toUpperCase();
2505
+ }
2506
+ else if (/^[A-Z]$/.test(char)) {
2507
+ charType = 'UPPERCASE';
2508
+ normalizedChar = char;
2509
+ }
2510
+ else if (/^[0-9]$/.test(char)) {
2511
+ charType = 'NUMBER';
2512
+ normalizedChar = char;
2513
+ }
2514
+ else if (/^\/$/.test(char)) {
2515
+ charType = 'SLASH';
2516
+ normalizedChar = char;
2517
+ }
2518
+ else {
2519
+ charType = 'OTHER';
2520
+ normalizedChar = '_';
2521
+ }
2522
+ if (charType !== lastCharType &&
2523
+ !(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
2524
+ !(lastCharType === 'NUMBER') &&
2525
+ !(charType === 'NUMBER')) {
2526
+ normalizedName += '_';
2527
+ }
2528
+ normalizedName += normalizedChar;
2529
+ lastCharType = charType;
2530
+ }
2531
+ }
2532
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2533
+ finally {
2534
+ try {
2535
+ if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
2536
+ }
2537
+ finally { if (e_1) throw e_1.error; }
2538
+ }
2539
+ normalizedName = normalizedName.replace(/_+/g, '_');
2540
+ normalizedName = normalizedName.replace(/_?\/_?/g, '/');
2541
+ normalizedName = normalizedName.replace(/^_/, '');
2542
+ normalizedName = normalizedName.replace(/_$/, '');
2543
+ return normalizedName;
2544
+ }
2545
+ /**
2546
+ * TODO: [🌺] Use some intermediate util splitWords
2547
+ */
2548
+
2549
+ /**
2550
+ * Execution type describes the way how the block is executed
2551
+ *
2552
+ * @see https://github.com/webgptorg/promptbook#execution-type
2553
+ */
2554
+ var ExecutionTypes = [
2555
+ 'PROMPT_TEMPLATE',
2556
+ 'SIMPLE_TEMPLATE',
2557
+ 'SCRIPT',
2558
+ 'PROMPT_DIALOG',
2559
+ // <- [🥻] Insert here when making new command
2560
+ ];
2561
+
2562
+ /**
2563
+ * Units of text measurement
2564
+ */
2565
+ var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'LINES', 'PARAGRAPHS', 'PAGES'];
2566
+ /**
2567
+ * TODO: [💝] Unite object for expecting amount and format - remove expectFormat
2568
+ * TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
2569
+ * TODO: [👙][🧠] Just selecting gpt3 or gpt4 level of model
2570
+ */
2571
+
2572
+ /**
2573
+ * Removes Markdown formatting tags from a string.
2574
+ *
2575
+ * @param {string} str - The string to remove Markdown tags from.
2576
+ * @returns {string} The input string with all Markdown tags removed.
2577
+ */
2578
+ function removeMarkdownFormatting(str) {
2579
+ // Remove bold formatting
2580
+ str = str.replace(/\*\*(.*?)\*\*/g, '$1');
2581
+ // Remove italic formatting
2582
+ str = str.replace(/\*(.*?)\*/g, '$1');
2583
+ // Remove code formatting
2584
+ str = str.replace(/`(.*?)`/g, '$1');
2585
+ return str;
2586
+ }
2587
+
2588
+ /**
2589
+ * Function parseNumber will parse number from string
2590
+ *
2591
+ * Unlike Number.parseInt, Number.parseFloat it will never ever result in NaN
2592
+ * Note: it also works only with decimal numbers
2593
+ *
2594
+ * @returns parsed number
2595
+ * @throws {PromptbookSyntaxError} if the value is not a number
2596
+ *
2597
+ * @private within the parseCommand
2598
+ */
2599
+ function parseNumber(value) {
2600
+ var originalValue = value;
2601
+ if (typeof value === 'number') {
2602
+ value = value.toString(); // <- TODO: Maybe more efficient way to do this
2603
+ }
2604
+ if (typeof value !== 'string') {
2605
+ return 0;
2606
+ }
2607
+ value = value.trim();
2608
+ if (value.startsWith('+')) {
2609
+ return parseNumber(value.substring(1));
2610
+ }
2611
+ if (value.startsWith('-')) {
2612
+ var number = parseNumber(value.substring(1));
2613
+ if (number === 0) {
2614
+ return 0; // <- Note: To prevent -0
2615
+ }
2616
+ return -number;
2617
+ }
2618
+ value = value.replace(/,/g, '.');
2619
+ value = value.toUpperCase();
2620
+ if (value === '') {
2621
+ return 0;
2622
+ }
2623
+ if (value === '♾' || value.startsWith('INF')) {
2624
+ return Infinity;
2625
+ }
2626
+ if (value.includes('/')) {
2627
+ var _a = __read(value.split('/'), 2), numerator_ = _a[0], denominator_ = _a[1];
2628
+ var numerator = parseNumber(numerator_);
2629
+ var denominator = parseNumber(denominator_);
2630
+ if (denominator === 0) {
2631
+ throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\" because denominator is zero"));
2632
+ }
2633
+ return numerator / denominator;
2634
+ }
2635
+ if (/^(NAN|NULL|NONE|UNDEFINED|ZERO|NO.*)$/.test(value)) {
2636
+ return 0;
2637
+ }
2638
+ if (value.includes('E')) {
2639
+ var _b = __read(value.split('E'), 2), significand = _b[0], exponent = _b[1];
2640
+ return parseNumber(significand) * Math.pow(10, parseNumber(exponent));
2641
+ }
2642
+ if (!/^[0-9.]+$/.test(value) || value.split('.').length > 2) {
2643
+ throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\""));
2644
+ }
2645
+ var num = parseFloat(value);
2646
+ if (isNaN(num)) {
2647
+ throw new PromptbookSyntaxError("Unexpected NaN when parsing number from \"".concat(originalValue, "\""));
2648
+ }
2649
+ return num;
2650
+ }
2651
+ /**
2652
+ * TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
2653
+ */
2654
+
2655
+ /**
2656
+ * Parses one line of ul/ol to command
2657
+ *
2658
+ * @returns parsed command object
2659
+ * @throws {PromptbookSyntaxError} if the command is invalid
2660
+ *
2661
+ * @private within the promptbookStringToJson
2662
+ */
2663
+ function parseCommand(listItem) {
2664
+ var e_1, _a;
2665
+ if (listItem.includes('\n') || listItem.includes('\r')) {
2666
+ throw new PromptbookSyntaxError('Command can not contain new line characters:');
2667
+ }
2668
+ var type = listItem.trim();
2669
+ type = type.split('`').join('');
2670
+ type = type.split('"').join('');
2671
+ type = type.split("'").join('');
2672
+ type = type.split('~').join('');
2673
+ type = type.split('[').join('');
2674
+ type = type.split(']').join('');
2675
+ type = type.split('(').join('');
2676
+ type = type.split(')').join('');
2677
+ type = normalizeTo_SCREAMING_CASE(type);
2678
+ type = type.split('DIALOGUE').join('DIALOG');
2679
+ var listItemParts = listItem
2680
+ .split(' ')
2681
+ .map(function (part) { return part.trim(); })
2682
+ .filter(function (item) { return item !== ''; })
2683
+ .filter(function (item) { return !/^PTBK$/i.test(item); })
2684
+ .filter(function (item) { return !/^PROMPTBOOK$/i.test(item); })
2685
+ .map(removeMarkdownFormatting);
2686
+ if (type.startsWith('URL') ||
2687
+ type.startsWith('PTBK_URL') ||
2688
+ type.startsWith('PTBKURL') ||
2689
+ type.startsWith('PROMPTBOOK_URL') ||
2690
+ type.startsWith('PROMPTBOOKURL') ||
2691
+ type.startsWith('HTTPS')) {
2692
+ if (!(listItemParts.length === 2 || (listItemParts.length === 1 && type.startsWith('HTTPS')))) {
2693
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n ")));
2694
+ }
2695
+ var promptbookUrlString = listItemParts.pop();
2696
+ var promptbookUrl = new URL(promptbookUrlString);
2697
+ if (promptbookUrl.protocol !== 'https:') {
2698
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n Protocol must be HTTPS\n ")));
2699
+ }
2700
+ if (promptbookUrl.hash !== '') {
2701
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n URL must not contain hash\n Hash is used for identification of the prompt template in the pipeline\n ")));
2702
+ }
2703
+ return {
2704
+ type: 'PROMPTBOOK_URL',
2705
+ promptbookUrl: promptbookUrl,
2706
+ };
2707
+ }
2708
+ else if (type.startsWith('PROMPTBOOK_VERSION') || type.startsWith('PTBK_VERSION')) {
2709
+ if (listItemParts.length !== 2) {
2710
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Invalid PROMPTBOOK_VERSION command:\n\n - ".concat(listItem, "\n ")));
2711
+ }
2712
+ var promptbookVersion = listItemParts.pop();
2713
+ // TODO: Validate version
2714
+ return {
2715
+ type: 'PROMPTBOOK_VERSION',
2716
+ promptbookVersion: promptbookVersion,
2717
+ };
2718
+ }
2719
+ else if (type.startsWith('EXECUTE') ||
2720
+ type.startsWith('EXEC') ||
2721
+ type.startsWith('PROMPT_DIALOG') ||
2722
+ type.startsWith('SIMPLE_TEMPLATE')) {
2723
+ var executionTypes = ExecutionTypes.filter(function (executionType) { return type.includes(executionType); });
2724
+ if (executionTypes.length !== 1) {
2725
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim(function (block) { return "\n Unknown execution type in command:\n\n - ".concat(listItem, "\n\n Supported execution types are:\n ").concat(block(ExecutionTypes.join(', ')), "\n "); }));
2726
+ }
2727
+ return {
2728
+ type: 'EXECUTE',
2729
+ executionType: executionTypes[0],
2730
+ };
2731
+ }
2732
+ else if (type.startsWith('MODEL')) {
2733
+ // TODO: Make this more elegant and dynamically
2734
+ if (type.startsWith('MODEL_VARIANT')) {
2735
+ if (type === 'MODEL_VARIANT_CHAT') {
2736
+ return {
2737
+ type: 'MODEL',
2738
+ key: 'modelVariant',
2739
+ value: 'CHAT',
2740
+ };
2741
+ }
2742
+ else if (type === 'MODEL_VARIANT_COMPLETION') {
2743
+ return {
2744
+ type: 'MODEL',
2745
+ key: 'modelVariant',
2746
+ value: 'COMPLETION',
2747
+ };
2748
+ }
2749
+ else {
2750
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim(function (block) { return "\n Unknown model variant in command:\n\n - ".concat(listItem, "\n\n Supported variants are:\n ").concat(block(['CHAT', 'COMPLETION'].join(', ')), "\n "); }));
2751
+ }
2752
+ }
2753
+ if (type.startsWith('MODEL_NAME')) {
2754
+ return {
2755
+ type: 'MODEL',
2756
+ key: 'modelName',
2757
+ value: listItemParts.pop(),
2758
+ };
2759
+ }
2760
+ else {
2761
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim(function (block) { return "\n Unknown model key in command:\n\n - ".concat(listItem, "\n\n Supported model keys are:\n ").concat(block(['variant', 'name'].join(', ')), "\n\n Example:\n\n - MODEL VARIANT Chat\n - MODEL NAME gpt-4\n "); }));
2762
+ }
2763
+ }
2764
+ else if (type.startsWith('PARAM') ||
2765
+ type.startsWith('INPUT_PARAM') ||
2766
+ type.startsWith('OUTPUT_PARAM') ||
2767
+ listItem.startsWith('{') ||
2768
+ listItem.startsWith('> {') /* <- Note: This is a bit hack to parse return parameters defined at the end of each section */) {
2769
+ var parametersMatch = listItem.match(/\{(?<parameterName>[a-z0-9_]+)\}[^\S\r\n]*(?<parameterDescription>.*)$/im);
2770
+ if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
2771
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
2772
+ }
2773
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2774
+ var _b = parametersMatch.groups, parameterName = _b.parameterName, parameterDescription = _b.parameterDescription;
2775
+ if (parameterDescription && parameterDescription.match(/\{(?<parameterName>[a-z0-9_]+)\}/im)) {
2776
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Parameter {".concat(parameterName, "} can not contain another parameter in description:\n\n - ").concat(listItem, "\n ")));
2777
+ }
2778
+ var isInput = type.startsWith('INPUT');
2779
+ var isOutput = type.startsWith('OUTPUT');
2780
+ if (listItem.startsWith('> {')) {
2781
+ isInput = false;
2782
+ isOutput = false;
2783
+ }
2784
+ return {
2785
+ type: 'PARAMETER',
2786
+ parameterName: parameterName,
2787
+ parameterDescription: parameterDescription.trim() || null,
2788
+ isInput: isInput,
2789
+ isOutput: isOutput,
2790
+ };
2791
+ }
2792
+ else if (type.startsWith('JOKER')) {
2793
+ if (listItemParts.length !== 2) {
2794
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Invalid JOKER command:\n\n - ".concat(listItem, "\n ")));
2795
+ }
2796
+ var parametersMatch = (listItemParts.pop() || '').match(/^\{(?<parameterName>[a-z0-9_]+)\}$/im);
2797
+ if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
2798
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
2799
+ }
2800
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2801
+ var parameterName = parametersMatch.groups.parameterName;
2802
+ return {
2803
+ type: 'JOKER',
2804
+ parameterName: parameterName,
2805
+ };
2806
+ }
2807
+ else if (type.startsWith('POSTPROCESS') || type.startsWith('POST_PROCESS')) {
2808
+ if (listItemParts.length !== 2) {
2809
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Invalid POSTPROCESSING command:\n\n - ".concat(listItem, "\n ")));
2810
+ }
2811
+ var functionName = listItemParts.pop();
2812
+ return {
2813
+ type: 'POSTPROCESS',
2814
+ functionName: functionName,
2815
+ };
2816
+ }
2817
+ else if (type.startsWith('EXPECT_JSON')) {
2818
+ return {
2819
+ type: 'EXPECT_FORMAT',
2820
+ format: 'JSON',
2821
+ };
2822
+ // [🥤]
2823
+ }
2824
+ else if (type.startsWith('EXPECT')) {
2825
+ try {
2826
+ listItemParts.shift();
2827
+ var sign = void 0;
2828
+ var signRaw = listItemParts.shift();
2829
+ if (/^exact/i.test(signRaw)) {
2830
+ sign = 'EXACTLY';
2831
+ }
2832
+ else if (/^min/i.test(signRaw)) {
2833
+ sign = 'MINIMUM';
2834
+ }
2835
+ else if (/^max/i.test(signRaw)) {
2836
+ sign = 'MAXIMUM';
2837
+ }
2838
+ else {
2839
+ throw new PromptbookSyntaxError("Invalid sign \"".concat(signRaw, "\", expected EXACTLY, MIN or MAX"));
2840
+ }
2841
+ var amountRaw = listItemParts.shift();
2842
+ var amount = parseNumber(amountRaw);
2843
+ if (amount < 0) {
2844
+ throw new PromptbookSyntaxError('Amount must be positive number or zero');
2845
+ }
2846
+ if (amount !== Math.floor(amount)) {
2847
+ throw new PromptbookSyntaxError('Amount must be whole number');
2848
+ }
2849
+ var unitRaw = listItemParts.shift();
2850
+ var unit = undefined;
2851
+ try {
2852
+ for (var EXPECTATION_UNITS_1 = __values(EXPECTATION_UNITS), EXPECTATION_UNITS_1_1 = EXPECTATION_UNITS_1.next(); !EXPECTATION_UNITS_1_1.done; EXPECTATION_UNITS_1_1 = EXPECTATION_UNITS_1.next()) {
2853
+ var existingUnit = EXPECTATION_UNITS_1_1.value;
2854
+ var existingUnitText = existingUnit;
2855
+ existingUnitText = existingUnitText.substring(0, existingUnitText.length - 1);
2856
+ if (existingUnitText === 'CHARACTER') {
2857
+ existingUnitText = 'CHAR';
2858
+ }
2859
+ if (new RegExp("^".concat(existingUnitText.toLowerCase())).test(unitRaw.toLowerCase()) ||
2860
+ new RegExp("^".concat(unitRaw.toLowerCase())).test(existingUnitText.toLowerCase())) {
2861
+ if (unit !== undefined) {
2862
+ throw new PromptbookSyntaxError("Ambiguous unit \"".concat(unitRaw, "\""));
2863
+ }
2864
+ unit = existingUnit;
2865
+ }
2866
+ }
2867
+ }
2868
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2869
+ finally {
2870
+ try {
2871
+ if (EXPECTATION_UNITS_1_1 && !EXPECTATION_UNITS_1_1.done && (_a = EXPECTATION_UNITS_1.return)) _a.call(EXPECTATION_UNITS_1);
2872
+ }
2873
+ finally { if (e_1) throw e_1.error; }
2874
+ }
2875
+ if (unit === undefined) {
2876
+ throw new PromptbookSyntaxError("Invalid unit \"".concat(unitRaw, "\""));
2877
+ }
2878
+ return {
2879
+ type: 'EXPECT_AMOUNT',
2880
+ sign: sign,
2881
+ unit: unit,
2882
+ amount: amount,
2883
+ };
2884
+ }
2885
+ catch (error) {
2886
+ if (!(error instanceof Error)) {
2887
+ throw error;
2888
+ }
2889
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Invalid EXPECT command; ".concat(error.message, ":\n\n - ").concat(listItem, "\n ")));
2890
+ }
2891
+ /*
2892
+ } else if (type.startsWith('__________________')) {
2893
+ // <- [🥻] Insert here when making new command
2894
+ */
2895
+ }
2896
+ else {
2897
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim("\n Unknown command:\n\n - ".concat(listItem, "\n\n Supported commands are:\n - PROMPTBOOK_URL <url>\n - PROMPTBOOK_VERSION <version>\n - EXECUTE PROMPT TEMPLATE\n - EXECUTE SIMPLE TEMPLATE\n - SIMPLE TEMPLATE\n - EXECUTE SCRIPT\n - EXECUTE PROMPT_DIALOG'\n - PROMPT_DIALOG'\n - MODEL NAME <name>\n - MODEL VARIANT <\"Chat\"|\"Completion\">\n - INPUT PARAM {<name>} <description>\n - OUTPUT PARAM {<name>} <description>\n - POSTPROCESS `{functionName}`\n - JOKER {<name>}\n - EXPECT JSON\n - EXPECT <\"Exactly\"|\"Min\"|\"Max\"> <number> <\"Chars\"|\"Words\"|\"Sentences\"|\"Paragraphs\"|\"Pages\">\n\n ")));
2898
+ }
2899
+ }
2900
+
2901
+ /**
2902
+ * Removes emojis from a string and fix whitespaces
2903
+ *
2904
+ * @param text with emojis
2905
+ * @returns text without emojis
2906
+ */
2907
+ function removeEmojis(text) {
2908
+ // Replace emojis (and also ZWJ sequence) with hyphens
2909
+ text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
2910
+ text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
2911
+ text = text.replace(/(\p{Extended_Pictographic})(\u{200D}\p{Extended_Pictographic})*/gu, '$1');
2912
+ text = text.replace(/\p{Extended_Pictographic}/gu, '');
2913
+ return text;
2914
+ }
2915
+
2916
+ /**
2917
+ * Function normalizes title to name which can be used as identifier
2918
+ */
2919
+ function titleToName(value) {
2920
+ value = removeEmojis(value);
2921
+ value = normalizeToKebabCase(value);
2922
+ // TODO: [🧠] Maybe warn or add some padding to short name which are not good identifiers
2923
+ return value;
2924
+ }
2925
+
2926
+ /**
2927
+ * Compile promptbook from string (markdown) format to JSON format
2928
+ *
2929
+ * @param promptbookString {Promptbook} in string markdown format (.ptbk.md)
2930
+ * @param options - Options and tools for the compilation
2931
+ * @returns {Promptbook} compiled in JSON format (.ptbk.json)
2932
+ * @throws {PromptbookSyntaxError} if the promptbook string is not valid
2933
+ *
2934
+ * Note: This function does not validate logic of the pipeline only the syntax
2935
+ * Note: This function acts as compilation process
2936
+ */
2937
+ function promptbookStringToJson(promptbookString, options) {
2938
+ if (options === void 0) { options = {}; }
2939
+ return __awaiter(this, void 0, void 0, function () {
2940
+ var llmTools, promptbookJson, knowledge, addParam, markdownStructure, markdownStructureDeepness, description, defaultModelRequirements, listItems, listItems_1, listItems_1_1, listItem, command, _loop_1, _a, _b, section;
2941
+ var e_1, _c, e_2, _d;
2942
+ return __generator(this, function (_e) {
2943
+ switch (_e.label) {
2944
+ case 0:
2945
+ llmTools = options.llmTools;
2946
+ promptbookJson = {
2947
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2948
+ title: undefined /* <- Note: Putting here placeholder to keep `title` on top at final JSON */,
2949
+ promptbookUrl: undefined /* <- Note: Putting here placeholder to keep `promptbookUrl` on top at final JSON */,
2950
+ promptbookVersion: PROMPTBOOK_VERSION,
2951
+ description: undefined /* <- Note: Putting here placeholder to keep `description` on top at final JSON */,
2952
+ parameters: [],
2953
+ promptTemplates: [],
2954
+ knowledge: [],
2955
+ };
2956
+ if (!llmTools) return [3 /*break*/, 2];
2957
+ return [4 /*yield*/, prepareKnowledgeFromMarkdown({
2958
+ content: 'Roses are red, violets are blue, programmers use Promptbook, users too',
2959
+ llmTools: llmTools,
2960
+ })];
2961
+ case 1:
2962
+ knowledge = _e.sent();
2963
+ console.info('!!!! knowledge', knowledge);
2964
+ _e.label = 2;
2965
+ case 2:
2966
+ // =============================================================
2967
+ // Note: 1️⃣ Normalization of the PROMPTBOOK string
2968
+ promptbookString = removeContentComments(promptbookString);
2969
+ promptbookString = promptbookString.replaceAll(/`\{(?<parameterName>[a-z0-9_]+)\}`/gi, '{$<parameterName>}');
2970
+ promptbookString = promptbookString.replaceAll(/`->\s+\{(?<parameterName>[a-z0-9_]+)\}`/gi, '-> {$<parameterName>}');
2971
+ addParam = function (parameterCommand) {
2972
+ var parameterName = parameterCommand.parameterName, parameterDescription = parameterCommand.parameterDescription, isInput = parameterCommand.isInput, isOutput = parameterCommand.isOutput;
2973
+ var existingParameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
2974
+ if (existingParameter &&
2975
+ existingParameter.description &&
2976
+ existingParameter.description !== parameterDescription &&
2977
+ parameterDescription) {
2978
+ 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 "); }));
2979
+ }
2980
+ if (existingParameter) {
2981
+ if (parameterDescription) {
2982
+ existingParameter.description = parameterDescription;
2983
+ }
2984
+ }
2985
+ else {
2986
+ promptbookJson.parameters.push({
2987
+ name: parameterName,
2988
+ description: parameterDescription || undefined,
2989
+ isInput: isInput,
2990
+ isOutput: isOutput,
2991
+ });
2992
+ }
2993
+ };
2994
+ markdownStructure = markdownToMarkdownStructure(promptbookString);
2995
+ markdownStructureDeepness = countMarkdownStructureDeepness(markdownStructure);
2996
+ if (markdownStructureDeepness !== 2) {
2997
+ 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 ")));
2998
+ }
2999
+ promptbookJson.title = markdownStructure.title;
3000
+ description = markdownStructure.content;
3001
+ // Note: Remove codeblocks
3002
+ description = description.split(/^```.*^```/gms).join('');
3003
+ //Note: Remove lists and return statement
3004
+ description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
3005
+ description = spaceTrim.spaceTrim(description);
3006
+ if (description === '') {
3007
+ description = undefined;
3008
+ }
3009
+ promptbookJson.description = description;
3010
+ defaultModelRequirements = {};
3011
+ listItems = extractAllListItemsFromMarkdown(markdownStructure.content);
3012
+ try {
3013
+ for (listItems_1 = __values(listItems), listItems_1_1 = listItems_1.next(); !listItems_1_1.done; listItems_1_1 = listItems_1.next()) {
3014
+ listItem = listItems_1_1.value;
3015
+ command = parseCommand(listItem);
3016
+ switch (command.type) {
3017
+ case 'PROMPTBOOK_URL':
3018
+ promptbookJson.promptbookUrl = command.promptbookUrl.href;
3019
+ break;
3020
+ case 'PROMPTBOOK_VERSION':
3021
+ promptbookJson.promptbookVersion = command.promptbookVersion;
3022
+ break;
3023
+ case 'MODEL':
3024
+ defaultModelRequirements[command.key] = command.value;
3025
+ break;
3026
+ case 'PARAMETER':
3027
+ addParam(command);
3028
+ break;
3029
+ default:
3030
+ throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the head of the promptbook ONLY at the prompt template block"));
3031
+ }
3032
+ }
3033
+ }
3034
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
3035
+ finally {
3036
+ try {
3037
+ if (listItems_1_1 && !listItems_1_1.done && (_c = listItems_1.return)) _c.call(listItems_1);
3038
+ }
3039
+ finally { if (e_1) throw e_1.error; }
3040
+ }
3041
+ _loop_1 = function (section) {
3042
+ var e_3, _f;
3043
+ // TODO: Parse prompt template description (the content out of the codeblock and lists)
3044
+ var templateModelRequirements = __assign({}, defaultModelRequirements);
3045
+ var listItems_3 = extractAllListItemsFromMarkdown(section.content);
3046
+ var dependentParameterNames = new Set();
3047
+ var executionType = 'PROMPT_TEMPLATE';
3048
+ var jokers = [];
3049
+ var postprocessing = [];
3050
+ var expectAmount = {};
3051
+ var expectFormat = undefined;
3052
+ var isExecutionTypeChanged = false;
3053
+ try {
3054
+ 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()) {
3055
+ var listItem = listItems_2_1.value;
3056
+ var command = parseCommand(listItem);
3057
+ switch (command.type) {
3058
+ case 'JOKER':
3059
+ jokers.push(command.parameterName);
3060
+ dependentParameterNames.add(command.parameterName);
3061
+ break;
3062
+ case 'EXECUTE':
3063
+ if (isExecutionTypeChanged) {
3064
+ throw new PromptbookSyntaxError('Execution type is already defined in the prompt template. It can be defined only once.');
3065
+ }
3066
+ executionType = command.executionType;
3067
+ isExecutionTypeChanged = true;
3068
+ break;
3069
+ case 'MODEL':
3070
+ templateModelRequirements[command.key] = command.value;
3071
+ break;
3072
+ case 'PARAMETER':
3073
+ // Note: This is just for detecting resulitng parameter name
3074
+ addParam(command);
3075
+ break;
3076
+ case 'POSTPROCESS':
3077
+ postprocessing.push(command.functionName);
3078
+ break;
3079
+ case 'EXPECT_AMOUNT':
3080
+ // eslint-disable-next-line no-case-declarations
3081
+ var unit = command.unit.toLowerCase();
3082
+ expectAmount[unit] = expectAmount[unit] || {};
3083
+ if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
3084
+ if (expectAmount[unit].min !== undefined) {
3085
+ throw new PromptbookSyntaxError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
3086
+ }
3087
+ expectAmount[unit].min = command.amount;
3088
+ } /* not else */
3089
+ if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
3090
+ if (expectAmount[unit].max !== undefined) {
3091
+ throw new PromptbookSyntaxError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
3092
+ }
3093
+ expectAmount[unit].max = command.amount;
3094
+ }
3095
+ break;
3096
+ case 'EXPECT_FORMAT':
3097
+ if (expectFormat !== undefined && command.format !== expectFormat) {
3098
+ throw new PromptbookSyntaxError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
3099
+ }
3100
+ expectFormat = command.format;
3101
+ break;
3102
+ default:
3103
+ throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the block of the prompt template ONLY at the head of the promptbook"));
3104
+ }
3105
+ }
3106
+ }
3107
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
3108
+ finally {
3109
+ try {
3110
+ if (listItems_2_1 && !listItems_2_1.done && (_f = listItems_2.return)) _f.call(listItems_2);
3111
+ }
3112
+ finally { if (e_3) throw e_3.error; }
3113
+ }
3114
+ var _g = extractOneBlockFromMarkdown(section.content), language = _g.language, content = _g.content;
3115
+ if (executionType === 'SCRIPT') {
3116
+ if (!language) {
3117
+ throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
3118
+ }
3119
+ else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
3120
+ 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 "); }));
3121
+ }
3122
+ }
3123
+ var lastLine = section.content.split('\n').pop();
3124
+ var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
3125
+ if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
3126
+ throw new PromptbookSyntaxError(spaceTrim.spaceTrim(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
3127
+ // TODO: Show code of invalid sections each time + DRY
3128
+ section.content
3129
+ .split('\n')
3130
+ .map(function (line) { return "> ".concat(line); })
3131
+ .join('\n')), "\n "); }));
3132
+ }
3133
+ var resultingParameterName = match.groups.resultingParamName;
3134
+ // TODO: [1] DRY description
3135
+ var description_1 = section.content;
3136
+ // Note: Remove codeblocks
3137
+ description_1 = description_1.split(/^```.*^```/gms).join('');
3138
+ //Note: Remove lists and return statement
3139
+ description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
3140
+ description_1 = spaceTrim.spaceTrim(description_1);
3141
+ if (description_1 === '') {
3142
+ description_1 = undefined;
3143
+ }
3144
+ if (Object.keys(jokers).length === 0) {
3145
+ jokers = undefined;
3146
+ }
3147
+ if (Object.keys(expectAmount).length === 0) {
3148
+ expectAmount = undefined;
3149
+ }
3150
+ if (Object.keys(postprocessing).length === 0) {
3151
+ postprocessing = undefined;
3152
+ }
3153
+ dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, executionType: executionType, content: content })));
3154
+ if (templateModelRequirements.modelVariant === undefined) {
3155
+ templateModelRequirements.modelVariant = 'CHAT';
3156
+ }
3157
+ promptbookJson.promptTemplates.push({
3158
+ name: titleToName(section.title),
3159
+ title: section.title,
3160
+ description: description_1,
3161
+ dependentParameterNames: Array.from(dependentParameterNames),
3162
+ executionType: executionType,
3163
+ jokers: jokers,
3164
+ postprocessing: postprocessing,
3165
+ expectations: expectAmount,
3166
+ expectFormat: expectFormat,
3167
+ modelRequirements: templateModelRequirements,
3168
+ contentLanguage: executionType === 'SCRIPT' ? language : undefined,
3169
+ content: content,
3170
+ resultingParameterName: resultingParameterName,
3171
+ });
3172
+ };
3173
+ try {
3174
+ for (_a = __values(markdownStructure.sections), _b = _a.next(); !_b.done; _b = _a.next()) {
3175
+ section = _b.value;
3176
+ _loop_1(section);
2033
3177
  }
2034
3178
  }
2035
- }
2036
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
2037
- finally {
2038
- try {
2039
- if (_m && !_m.done && (_f = _l.return)) _f.call(_l);
3179
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
3180
+ finally {
3181
+ try {
3182
+ if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
3183
+ }
3184
+ finally { if (e_2) throw e_2.error; }
2040
3185
  }
2041
- finally { if (e_6) throw e_6.error; }
2042
- }
2043
- } /* not else */
2044
- if (expectFormat) {
2045
- if (expectFormat === 'JSON') {
2046
- // TODO: @deprecated remove
2047
- commands_1.push("EXPECT JSON");
2048
- }
2049
- } /* not else */
2050
- promptbookString += '\n\n';
2051
- promptbookString += commands_1.map(function (command) { return "- ".concat(command); }).join('\n');
2052
- promptbookString += '\n\n';
2053
- promptbookString += '```' + contentLanguage;
2054
- promptbookString += '\n';
2055
- promptbookString += spaceTrim__default["default"](content);
2056
- // <- TODO: !!! Escape
2057
- // <- TODO: [🧠] Some clear strategy how to spaceTrim the blocks
2058
- promptbookString += '\n';
2059
- promptbookString += '```';
2060
- promptbookString += '\n\n';
2061
- promptbookString += "`-> {".concat(resultingParameterName, "}`"); // <- TODO: !!! If the parameter here has description, add it and use promptTemplateParameterJsonToString
2062
- }
2063
- }
2064
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
2065
- finally {
2066
- try {
2067
- if (promptTemplates_1_1 && !promptTemplates_1_1.done && (_c = promptTemplates_1.return)) _c.call(promptTemplates_1);
2068
- }
2069
- finally { if (e_3) throw e_3.error; }
2070
- }
2071
- return promptbookString;
2072
- }
2073
- /**
2074
- * @private internal util of promptbookJsonToString
2075
- */
2076
- function promptTemplateParameterJsonToString(promptTemplateParameterJson) {
2077
- var name = promptTemplateParameterJson.name, description = promptTemplateParameterJson.description;
2078
- var parameterString = "{".concat(name, "}");
2079
- if (description) {
2080
- parameterString = "".concat(parameterString, " ").concat(description);
2081
- }
2082
- return parameterString;
3186
+ // =============================================================
3187
+ return [2 /*return*/, promptbookJson];
3188
+ }
3189
+ });
3190
+ });
2083
3191
  }
2084
3192
  /**
2085
- * TODO: Escape all
2086
- */
2087
-
2088
- /**
2089
- * This error indicates that promptbook not found in the library
3193
+ * TODO: Report here line/column of error
3194
+ * TODO: Use spaceTrim more effectively
3195
+ * TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
2090
3196
  */
2091
- var PromptbookNotFoundError = /** @class */ (function (_super) {
2092
- __extends(PromptbookNotFoundError, _super);
2093
- function PromptbookNotFoundError(message) {
2094
- var _this = _super.call(this, message) || this;
2095
- _this.name = 'PromptbookNotFoundError';
2096
- Object.setPrototypeOf(_this, PromptbookNotFoundError.prototype);
2097
- return _this;
2098
- }
2099
- return PromptbookNotFoundError;
2100
- }(Error));
2101
3197
 
2102
3198
  /**
2103
- * This error indicates errors in referencing promptbooks between each other
3199
+ * This error indicates that the promptbook library cannot be propperly loaded
2104
3200
  */
2105
- var PromptbookReferenceError = /** @class */ (function (_super) {
2106
- __extends(PromptbookReferenceError, _super);
2107
- function PromptbookReferenceError(message) {
3201
+ var PromptbookLibraryError = /** @class */ (function (_super) {
3202
+ __extends(PromptbookLibraryError, _super);
3203
+ function PromptbookLibraryError(message) {
2108
3204
  var _this = _super.call(this, message) || this;
2109
- _this.name = 'PromptbookReferenceError';
2110
- Object.setPrototypeOf(_this, PromptbookReferenceError.prototype);
3205
+ _this.name = 'PromptbookLibraryError';
3206
+ Object.setPrototypeOf(_this, PromptbookLibraryError.prototype);
2111
3207
  return _this;
2112
3208
  }
2113
- return PromptbookReferenceError;
3209
+ return PromptbookLibraryError;
2114
3210
  }(Error));
2115
3211
 
2116
3212
  /**
2117
- * Library of promptbooks that groups together promptbooks for an application.
2118
- * This implementation is a very thin wrapper around the Array / Map of promptbooks.
2119
- *
2120
- * @see https://github.com/webgptorg/promptbook#promptbook-library
3213
+ * Detects if the code is running in a browser environment in main thread (Not in a web worker)
2121
3214
  */
2122
- var SimplePromptbookLibrary = /** @class */ (function () {
2123
- /**
2124
- * Constructs a promptbook library from promptbooks
2125
- *
2126
- * @param promptbooks !!!
2127
- *
2128
- * Note: During the construction logic of all promptbooks are validated
2129
- * Note: It is not recommended to use this constructor directly, use `createPromptbookLibraryFromSources` *(or other variant)* instead
2130
- */
2131
- function SimplePromptbookLibrary() {
2132
- var e_1, _a;
2133
- var promptbooks = [];
2134
- for (var _i = 0; _i < arguments.length; _i++) {
2135
- promptbooks[_i] = arguments[_i];
2136
- }
2137
- this.library = new Map();
2138
- try {
2139
- for (var promptbooks_1 = __values(promptbooks), promptbooks_1_1 = promptbooks_1.next(); !promptbooks_1_1.done; promptbooks_1_1 = promptbooks_1.next()) {
2140
- var promptbook = promptbooks_1_1.value;
2141
- if (promptbook.promptbookUrl === undefined) {
2142
- throw new PromptbookReferenceError(spaceTrim.spaceTrim("\n Promptbook with name \"".concat(promptbook.title, "\" does not have defined URL\n\n Note: Promptbooks without URLs are called anonymous promptbooks\n They can be used as standalone promptbooks, but they cannot be referenced by other promptbooks\n And also they cannot be used in the promptbook library\n\n ")));
2143
- }
2144
- validatePromptbookJson(promptbook);
2145
- // Note: [🦄]
2146
- if (this.library.has(promptbook.promptbookUrl) &&
2147
- promptbookJsonToString(promptbook) !==
2148
- promptbookJsonToString(this.library.get(promptbook.promptbookUrl))) {
2149
- throw new PromptbookReferenceError(spaceTrim.spaceTrim("\n Promptbook with URL \"".concat(promptbook.promptbookUrl, "\" is already in the library\n\n Note: Promptbooks with the same URL are not allowed\n Note: Automatically check whether the promptbooks are the same BUT they are DIFFERENT\n\n ")));
2150
- }
2151
- this.library.set(promptbook.promptbookUrl, promptbook);
2152
- }
2153
- }
2154
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
2155
- finally {
2156
- try {
2157
- if (promptbooks_1_1 && !promptbooks_1_1.done && (_a = promptbooks_1.return)) _a.call(promptbooks_1);
2158
- }
2159
- finally { if (e_1) throw e_1.error; }
2160
- }
2161
- }
2162
- /**
2163
- * Gets all promptbooks in the library
2164
- */
2165
- SimplePromptbookLibrary.prototype.listPromptbooks = function () {
2166
- return Array.from(this.library.keys());
2167
- };
2168
- /**
2169
- * Gets promptbook by its URL
2170
- *
2171
- * Note: This is not a direct fetching from the URL, but a lookup in the library
2172
- */
2173
- SimplePromptbookLibrary.prototype.getPromptbookByUrl = function (url) {
2174
- var _this = this;
2175
- var promptbook = this.library.get(url);
2176
- if (!promptbook) {
2177
- if (this.listPromptbooks().length === 0) {
2178
- throw new PromptbookNotFoundError(spaceTrim.spaceTrim("\n Promptbook with url \"".concat(url, "\" not found\n\n No promptbooks available\n ")));
2179
- }
2180
- throw new PromptbookNotFoundError(spaceTrim.spaceTrim(function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPromptbooks()
2181
- .map(function (promptbookUrl) { return "- ".concat(promptbookUrl); })
2182
- .join('\n')), "\n\n "); }));
2183
- }
2184
- return promptbook;
2185
- };
2186
- /**
2187
- * Checks whether given prompt was defined in any promptbook in the library
2188
- */
2189
- SimplePromptbookLibrary.prototype.isResponsibleForPrompt = function (prompt) {
2190
- return true;
2191
- };
2192
- return SimplePromptbookLibrary;
2193
- }());
2194
-
3215
+ new Function("\n try {\n return this === window;\n } catch (e) {\n return false;\n }\n");
2195
3216
  /**
2196
- * Creates PromptbookLibrary from array of PromptbookJson or PromptbookString
2197
- *
2198
- * Note: You can combine `PromptbookString` (`.ptbk.md`) with `PromptbookJson` BUT it is not recommended
2199
- * Note: During the construction syntax and logic of all sources are validated
2200
- *
2201
- * @param promptbookSources
2202
- * @returns PromptbookLibrary
3217
+ * Detects if the code is running in a Node.js environment
2203
3218
  */
2204
- function createPromptbookLibraryFromSources() {
2205
- var promptbookSources = [];
2206
- for (var _i = 0; _i < arguments.length; _i++) {
2207
- promptbookSources[_i] = arguments[_i];
2208
- }
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)))()];
2253
- }
2254
- });
2255
- });
2256
- }
3219
+ var isRunningInNode = new Function("\n try {\n return this === global;\n } catch (e) {\n return false;\n }\n");
2257
3220
  /**
2258
- * TODO: !!!! [🧠] Library precompilation and do not mix markdown and json promptbooks
3221
+ * Detects if the code is running in a web worker
2259
3222
  */
3223
+ new Function("\n try {\n if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {\n return true;\n } else {\n return false;\n }\n } catch (e) {\n return false;\n }\n");
2260
3224
 
2261
3225
  /**
2262
3226
  * Constructs Promptbook from async sources