@promptbook/cli 0.59.0-3 → 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/esm/index.es.js +2540 -978
- package/esm/index.es.js.map +1 -1
- package/esm/typings/_packages/core.index.d.ts +2 -1
- package/esm/typings/knowledge/prepare-knowledge/pdf/prepareKnowledgeFromPdf.d.ts +3 -0
- package/esm/typings/llm-providers/mocked/MockedEchoLlmExecutionTools.d.ts +1 -0
- package/esm/typings/llm-providers/mocked/MockedFackedLlmExecutionTools.d.ts +3 -0
- package/esm/typings/types/PromptbookJson/MaterialKnowledgePieceJson.d.ts +1 -1
- package/package.json +2 -2
- package/umd/index.umd.js +2545 -982
- package/umd/index.umd.js.map +1 -1
- package/umd/typings/_packages/core.index.d.ts +2 -1
- package/umd/typings/knowledge/prepare-knowledge/pdf/prepareKnowledgeFromPdf.d.ts +3 -0
- package/umd/typings/llm-providers/mocked/MockedEchoLlmExecutionTools.d.ts +1 -0
- package/umd/typings/llm-providers/mocked/MockedFackedLlmExecutionTools.d.ts +3 -0
- package/umd/typings/types/PromptbookJson/MaterialKnowledgePieceJson.d.ts +1 -1
package/esm/index.es.js
CHANGED
|
@@ -2,7 +2,7 @@ import colors from 'colors';
|
|
|
2
2
|
import commander from 'commander';
|
|
3
3
|
import { writeFile, readFile } from 'fs/promises';
|
|
4
4
|
import glob from 'glob-promise';
|
|
5
|
-
import { spaceTrim } from 'spacetrim';
|
|
5
|
+
import spaceTrim$1, { spaceTrim } from 'spacetrim';
|
|
6
6
|
import { forTime } from 'waitasecond';
|
|
7
7
|
import { format } from 'prettier';
|
|
8
8
|
import parserHtml from 'prettier/parser-html';
|
|
@@ -142,7 +142,7 @@ new Function("\n try {\n if (typeof WorkerGlobalScope !== 'undefined'
|
|
|
142
142
|
/**
|
|
143
143
|
* The version of the Promptbook library
|
|
144
144
|
*/
|
|
145
|
-
var PROMPTBOOK_VERSION = '0.59.0-
|
|
145
|
+
var PROMPTBOOK_VERSION = '0.59.0-7';
|
|
146
146
|
|
|
147
147
|
/**
|
|
148
148
|
* Removes HTML or Markdown comments from a string.
|
|
@@ -221,49 +221,64 @@ var PromptbookSyntaxError = /** @class */ (function (_super) {
|
|
|
221
221
|
return PromptbookSyntaxError;
|
|
222
222
|
}(Error));
|
|
223
223
|
|
|
224
|
-
// import prepareKnowledgeFromMarkdownStringPromptbook from './prepare-knowledge-from-markdown.ptbk.md';
|
|
225
|
-
function prepareKnowledgeFromMarkdown(options) {
|
|
226
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
227
|
-
return __generator(this, function (_a) {
|
|
228
|
-
return [2 /*return*/, []];
|
|
229
|
-
});
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
|
|
233
224
|
/**
|
|
234
|
-
*
|
|
225
|
+
* This error indicates errors during the execution of the promptbook
|
|
235
226
|
*/
|
|
236
|
-
var
|
|
227
|
+
var PromptbookExecutionError = /** @class */ (function (_super) {
|
|
228
|
+
__extends(PromptbookExecutionError, _super);
|
|
229
|
+
function PromptbookExecutionError(message) {
|
|
230
|
+
var _this = _super.call(this, message) || this;
|
|
231
|
+
_this.name = 'PromptbookExecutionError';
|
|
232
|
+
Object.setPrototypeOf(_this, PromptbookExecutionError.prototype);
|
|
233
|
+
return _this;
|
|
234
|
+
}
|
|
235
|
+
return PromptbookExecutionError;
|
|
236
|
+
}(Error));
|
|
237
237
|
|
|
238
238
|
/**
|
|
239
|
-
*
|
|
239
|
+
* Asserts that the execution of a promptnook is successful
|
|
240
240
|
*
|
|
241
|
-
* @
|
|
241
|
+
* @param executionResult - The partial result of the promptnook execution
|
|
242
|
+
* @throws {PromptbookExecutionError} If the execution is not successful or if multiple errors occurred
|
|
242
243
|
*/
|
|
243
|
-
function
|
|
244
|
-
var
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
for (var _b = __values(markdownStructure.sections), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
248
|
-
var section = _c.value;
|
|
249
|
-
maxDeepness = Math.max(maxDeepness, countMarkdownStructureDeepness(section));
|
|
250
|
-
}
|
|
244
|
+
function assertsExecutionSuccessful(executionResult) {
|
|
245
|
+
var isSuccessful = executionResult.isSuccessful, errors = executionResult.errors;
|
|
246
|
+
if (isSuccessful === true) {
|
|
247
|
+
return;
|
|
251
248
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
249
|
+
if (errors.length === 0) {
|
|
250
|
+
throw new PromptbookExecutionError("Promptnook Execution failed because of unknown reason");
|
|
251
|
+
}
|
|
252
|
+
else if (errors.length === 1) {
|
|
253
|
+
throw errors[0];
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n Multiple errors occurred during promptnook execution\n\n ".concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n')), "\n "); }));
|
|
258
257
|
}
|
|
259
|
-
return maxDeepness + 1;
|
|
260
258
|
}
|
|
259
|
+
/**
|
|
260
|
+
* TODO: [🧠] Can this return type be better typed than void
|
|
261
|
+
*/
|
|
261
262
|
|
|
262
263
|
/**
|
|
263
264
|
* The maximum number of iterations for a loops
|
|
264
265
|
*/
|
|
265
266
|
var LOOP_LIMIT = 1000;
|
|
266
267
|
|
|
268
|
+
/**
|
|
269
|
+
* This error indicates that the promptbook object has valid syntax but contains logical errors (like circular dependencies)
|
|
270
|
+
*/
|
|
271
|
+
var PromptbookLogicError = /** @class */ (function (_super) {
|
|
272
|
+
__extends(PromptbookLogicError, _super);
|
|
273
|
+
function PromptbookLogicError(message) {
|
|
274
|
+
var _this = _super.call(this, message) || this;
|
|
275
|
+
_this.name = 'PromptbookLogicError';
|
|
276
|
+
Object.setPrototypeOf(_this, PromptbookLogicError.prototype);
|
|
277
|
+
return _this;
|
|
278
|
+
}
|
|
279
|
+
return PromptbookLogicError;
|
|
280
|
+
}(Error));
|
|
281
|
+
|
|
267
282
|
/**
|
|
268
283
|
* This error type indicates that the error should not happen and its last check before crashing with some other error
|
|
269
284
|
*/
|
|
@@ -279,1096 +294,2640 @@ var UnexpectedError = /** @class */ (function (_super) {
|
|
|
279
294
|
}(Error));
|
|
280
295
|
|
|
281
296
|
/**
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
* Note: This function does work with code blocks
|
|
285
|
-
* Note: This function does not work with markdown comments
|
|
286
|
-
*
|
|
287
|
-
* @param markdown The markdown string to parse.
|
|
288
|
-
* @returns The MarkdownStructure object.
|
|
297
|
+
* Tests if given string is valid URL.
|
|
289
298
|
*
|
|
290
|
-
*
|
|
299
|
+
* Note: Dataurl are considered perfectly valid.
|
|
291
300
|
*/
|
|
292
|
-
function
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
var current = root;
|
|
297
|
-
var isInsideCodeBlock = false;
|
|
301
|
+
function isValidUrl(url) {
|
|
302
|
+
if (typeof url !== 'string') {
|
|
303
|
+
return false;
|
|
304
|
+
}
|
|
298
305
|
try {
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
var headingMatch = line.match(/^(?<mark>#{1,6})\s(?<title>.*)/);
|
|
302
|
-
if (isInsideCodeBlock || !headingMatch) {
|
|
303
|
-
if (line.startsWith('```')) {
|
|
304
|
-
isInsideCodeBlock = !isInsideCodeBlock;
|
|
305
|
-
}
|
|
306
|
-
current.contentLines.push(line);
|
|
307
|
-
}
|
|
308
|
-
else {
|
|
309
|
-
var level = headingMatch.groups.mark.length;
|
|
310
|
-
var title = headingMatch.groups.title.trim();
|
|
311
|
-
var parent_1 = void 0;
|
|
312
|
-
if (level > current.level) {
|
|
313
|
-
// Note: Going deeper (next section is child of current)
|
|
314
|
-
parent_1 = current;
|
|
315
|
-
}
|
|
316
|
-
else {
|
|
317
|
-
// Note: Going up or staying at the same level (next section is sibling or parent or grandparent,... of current)
|
|
318
|
-
parent_1 = current;
|
|
319
|
-
var loopLimit = LOOP_LIMIT;
|
|
320
|
-
while (parent_1.level !== level - 1) {
|
|
321
|
-
if (loopLimit-- < 0) {
|
|
322
|
-
throw new UnexpectedError('Loop limit reached during parsing of markdown structure in `markdownToMarkdownStructure`');
|
|
323
|
-
}
|
|
324
|
-
if (parent_1.parent === null /* <- Note: We are in root */) {
|
|
325
|
-
// [🌻]
|
|
326
|
-
throw new Error(spaceTrim("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
|
|
327
|
-
}
|
|
328
|
-
parent_1 = parent_1.parent;
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
|
|
332
|
-
parent_1.sections.push(section);
|
|
333
|
-
current = section;
|
|
334
|
-
}
|
|
306
|
+
if (url.startsWith('blob:')) {
|
|
307
|
+
url = url.replace(/^blob:/, '');
|
|
335
308
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
309
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
310
|
+
var urlObject = new URL(url);
|
|
311
|
+
if (!['http:', 'https:', 'data:'].includes(urlObject.protocol)) {
|
|
312
|
+
return false;
|
|
341
313
|
}
|
|
342
|
-
|
|
314
|
+
return true;
|
|
343
315
|
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
return markdownStructure;
|
|
316
|
+
catch (error) {
|
|
317
|
+
return false;
|
|
347
318
|
}
|
|
348
|
-
// [🌻]
|
|
349
|
-
throw new Error('The markdown file must have exactly one top-level section.');
|
|
350
|
-
// return root;
|
|
351
|
-
}
|
|
352
|
-
/**
|
|
353
|
-
* @private
|
|
354
|
-
*/
|
|
355
|
-
function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
|
|
356
|
-
var level = parsingMarkdownStructure.level, title = parsingMarkdownStructure.title, contentLines = parsingMarkdownStructure.contentLines, sections = parsingMarkdownStructure.sections;
|
|
357
|
-
return {
|
|
358
|
-
level: level,
|
|
359
|
-
title: title,
|
|
360
|
-
content: spaceTrim(contentLines.join('\n')),
|
|
361
|
-
sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
|
|
362
|
-
};
|
|
363
319
|
}
|
|
364
320
|
|
|
365
321
|
/**
|
|
366
|
-
*
|
|
322
|
+
* Validates PromptbookJson if it is logically valid
|
|
367
323
|
*
|
|
368
|
-
*
|
|
369
|
-
*
|
|
370
|
-
* Note: It flattens nested lists
|
|
371
|
-
* Note: It can not work with html syntax and comments
|
|
324
|
+
* It checks:
|
|
325
|
+
* - if it has correct parameters dependency
|
|
372
326
|
*
|
|
373
|
-
*
|
|
374
|
-
*
|
|
327
|
+
* It does NOT check:
|
|
328
|
+
* - if it is valid json
|
|
329
|
+
* - if it is meaningful
|
|
330
|
+
*
|
|
331
|
+
* @param promptbook valid or invalid PromptbookJson
|
|
332
|
+
* @returns the same promptbook if it is logically valid
|
|
333
|
+
* @throws {PromptbookLogicError} on logical error in the promptbook
|
|
375
334
|
*/
|
|
376
|
-
function
|
|
377
|
-
|
|
378
|
-
var
|
|
379
|
-
|
|
380
|
-
|
|
335
|
+
function validatePromptbookJson(promptbook) {
|
|
336
|
+
// TODO: [🧠] Maybe test if promptbook is a promise and make specific error case for that
|
|
337
|
+
var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
|
|
338
|
+
if (promptbook.promptbookUrl !== undefined) {
|
|
339
|
+
if (!isValidUrl(promptbook.promptbookUrl)) {
|
|
340
|
+
// TODO: This should be maybe the syntax error detected during parsing
|
|
341
|
+
throw new PromptbookLogicError("Invalid promptbook URL \"".concat(promptbook.promptbookUrl, "\""));
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
// TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
|
|
345
|
+
if (!Array.isArray(promptbook.parameters)) {
|
|
346
|
+
// TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
|
|
347
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Promptbook is valid JSON but with wrong structure\n\n promptbook.parameters expected to be an array, but got ".concat(typeof promptbook.parameters, "\n ")));
|
|
348
|
+
}
|
|
349
|
+
// TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
|
|
350
|
+
if (!Array.isArray(promptbook.promptTemplates)) {
|
|
351
|
+
// TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
|
|
352
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Promptbook is valid JSON but with wrong structure\n\n promptbook.promptTemplates expected to be an array, but got ".concat(typeof promptbook.promptTemplates, "\n ")));
|
|
353
|
+
}
|
|
354
|
+
var _loop_1 = function (parameter) {
|
|
355
|
+
if (parameter.isInput && parameter.isOutput) {
|
|
356
|
+
throw new PromptbookLogicError("Parameter {".concat(parameter.name, "} can not be both input and output"));
|
|
357
|
+
}
|
|
358
|
+
// Note: Testing that parameter is either intermediate or output BUT not created and unused
|
|
359
|
+
if (!parameter.isInput &&
|
|
360
|
+
!parameter.isOutput &&
|
|
361
|
+
!promptbook.promptTemplates.some(function (template) { return template.dependentParameterNames.includes(parameter.name); })) {
|
|
362
|
+
throw new PromptbookLogicError(spaceTrim("\n Parameter {".concat(parameter.name, "} is created but not used\n\n You can declare {").concat(parameter.name, "} as output parameter by adding in the header:\n - OUTPUT PARAMETER `{").concat(parameter.name, "}` ").concat(parameter.description || '', "\n\n ")));
|
|
363
|
+
}
|
|
364
|
+
// Note: Testing that parameter is either input or result of some template
|
|
365
|
+
if (!parameter.isInput &&
|
|
366
|
+
!promptbook.promptTemplates.some(function (template) { return template.resultingParameterName === parameter.name; })) {
|
|
367
|
+
throw new PromptbookLogicError(spaceTrim("\n Parameter {".concat(parameter.name, "} is declared but not defined\n\n You can do one of these:\n - Remove declaration of {").concat(parameter.name, "}\n - Add prompt template that results in -> {").concat(parameter.name, "}\n\n ")));
|
|
368
|
+
}
|
|
369
|
+
};
|
|
381
370
|
try {
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
var
|
|
385
|
-
|
|
386
|
-
isInCodeBlock = !isInCodeBlock;
|
|
387
|
-
}
|
|
388
|
-
if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
|
|
389
|
-
var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
|
|
390
|
-
listItems.push(listItem);
|
|
391
|
-
}
|
|
371
|
+
// Note: Check each parameter individually
|
|
372
|
+
for (var _e = __values(promptbook.parameters), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
373
|
+
var parameter = _f.value;
|
|
374
|
+
_loop_1(parameter);
|
|
392
375
|
}
|
|
393
376
|
}
|
|
394
377
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
395
378
|
finally {
|
|
396
379
|
try {
|
|
397
|
-
if (
|
|
380
|
+
if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
|
|
398
381
|
}
|
|
399
382
|
finally { if (e_1) throw e_1.error; }
|
|
400
383
|
}
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
return word.substring(0, 1).toUpperCase() + word.substring(1);
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
/**
|
|
413
|
-
* Extracts all code blocks from markdown.
|
|
414
|
-
*
|
|
415
|
-
* Note: There are 3 simmilar function:
|
|
416
|
-
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
417
|
-
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
418
|
-
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
419
|
-
*
|
|
420
|
-
* @param markdown any valid markdown
|
|
421
|
-
* @returns code blocks with language and content
|
|
422
|
-
*
|
|
423
|
-
*/
|
|
424
|
-
function extractAllBlocksFromMarkdown(markdown) {
|
|
425
|
-
var e_1, _a;
|
|
426
|
-
var codeBlocks = [];
|
|
427
|
-
var lines = markdown.split('\n');
|
|
428
|
-
var currentCodeBlock = null;
|
|
384
|
+
// Note: Check each template individually
|
|
385
|
+
var definedParameters = new Set(promptbook.parameters.filter(function (_a) {
|
|
386
|
+
var isInput = _a.isInput;
|
|
387
|
+
return isInput;
|
|
388
|
+
}).map(function (_a) {
|
|
389
|
+
var name = _a.name;
|
|
390
|
+
return name;
|
|
391
|
+
}));
|
|
429
392
|
try {
|
|
430
|
-
for (var
|
|
431
|
-
var
|
|
432
|
-
if (
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
393
|
+
for (var _g = __values(promptbook.promptTemplates), _h = _g.next(); !_h.done; _h = _g.next()) {
|
|
394
|
+
var template = _h.value;
|
|
395
|
+
if (definedParameters.has(template.resultingParameterName)) {
|
|
396
|
+
throw new PromptbookLogicError("Parameter {".concat(template.resultingParameterName, "} is defined multiple times"));
|
|
397
|
+
}
|
|
398
|
+
definedParameters.add(template.resultingParameterName);
|
|
399
|
+
if (template.executionType === 'PROMPT_TEMPLATE' &&
|
|
400
|
+
(template.modelRequirements.modelVariant === undefined)) {
|
|
401
|
+
throw new PromptbookLogicError(spaceTrim("\n\n You must specify MODEL VARIANT in the prompt template \"".concat(template.title, "\"\n\n For example:\n - MODEL VARIANT Chat\n - MODEL NAME `gpt-4-1106-preview`\n\n ")));
|
|
402
|
+
}
|
|
403
|
+
if (template.jokers && template.jokers.length > 0) {
|
|
404
|
+
if (!template.expectFormat &&
|
|
405
|
+
!template.expectations /* <- TODO: Require at least 1 -> min <- expectation to use jokers */) {
|
|
406
|
+
throw new PromptbookLogicError("Joker parameters are used for {".concat(template.resultingParameterName, "} but no expectations are defined"));
|
|
436
407
|
}
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
408
|
+
try {
|
|
409
|
+
for (var _j = (e_3 = void 0, __values(template.jokers)), _k = _j.next(); !_k.done; _k = _j.next()) {
|
|
410
|
+
var joker = _k.value;
|
|
411
|
+
if (!template.dependentParameterNames.includes(joker)) {
|
|
412
|
+
throw new PromptbookLogicError("Parameter {".concat(joker, "} is used for {").concat(template.resultingParameterName, "} as joker but not in dependentParameterNames"));
|
|
413
|
+
}
|
|
441
414
|
}
|
|
442
|
-
|
|
443
|
-
|
|
415
|
+
}
|
|
416
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
417
|
+
finally {
|
|
418
|
+
try {
|
|
419
|
+
if (_k && !_k.done && (_c = _j.return)) _c.call(_j);
|
|
420
|
+
}
|
|
421
|
+
finally { if (e_3) throw e_3.error; }
|
|
444
422
|
}
|
|
445
423
|
}
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
424
|
+
if (template.expectations) {
|
|
425
|
+
try {
|
|
426
|
+
for (var _l = (e_4 = void 0, __values(Object.entries(template.expectations))), _m = _l.next(); !_m.done; _m = _l.next()) {
|
|
427
|
+
var _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], min = _p.min, max = _p.max;
|
|
428
|
+
if (min !== undefined && max !== undefined && min > max) {
|
|
429
|
+
throw new PromptbookLogicError("Min expectation (=".concat(min, ") of ").concat(unit, " is higher than max expectation (=").concat(max, ")"));
|
|
430
|
+
}
|
|
431
|
+
if (min !== undefined && min < 0) {
|
|
432
|
+
throw new PromptbookLogicError("Min expectation of ".concat(unit, " must be zero or positive"));
|
|
433
|
+
}
|
|
434
|
+
if (max !== undefined && max <= 0) {
|
|
435
|
+
throw new PromptbookLogicError("Max expectation of ".concat(unit, " must be positive"));
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
440
|
+
finally {
|
|
441
|
+
try {
|
|
442
|
+
if (_m && !_m.done && (_d = _l.return)) _d.call(_l);
|
|
443
|
+
}
|
|
444
|
+
finally { if (e_4) throw e_4.error; }
|
|
449
445
|
}
|
|
450
|
-
currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
|
|
451
446
|
}
|
|
452
447
|
}
|
|
453
448
|
}
|
|
454
|
-
catch (
|
|
449
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
455
450
|
finally {
|
|
456
451
|
try {
|
|
457
|
-
if (
|
|
452
|
+
if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
|
|
458
453
|
}
|
|
459
|
-
finally { if (
|
|
454
|
+
finally { if (e_2) throw e_2.error; }
|
|
460
455
|
}
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
456
|
+
// Note: Detect circular dependencies
|
|
457
|
+
var resovedParameters = promptbook.parameters
|
|
458
|
+
.filter(function (_a) {
|
|
459
|
+
var isInput = _a.isInput;
|
|
460
|
+
return isInput;
|
|
461
|
+
})
|
|
462
|
+
.map(function (_a) {
|
|
463
|
+
var name = _a.name;
|
|
464
|
+
return name;
|
|
465
|
+
});
|
|
466
|
+
var unresovedTemplates = __spreadArray([], __read(promptbook.promptTemplates), false);
|
|
467
|
+
var loopLimit = LOOP_LIMIT;
|
|
468
|
+
var _loop_2 = function () {
|
|
469
|
+
if (loopLimit-- < 0) {
|
|
470
|
+
throw new UnexpectedError('Loop limit reached during detection of circular dependencies in `validatePromptbookJson`');
|
|
471
|
+
}
|
|
472
|
+
var currentlyResovedTemplates = unresovedTemplates.filter(function (template) {
|
|
473
|
+
return template.dependentParameterNames.every(function (name) { return resovedParameters.includes(name); });
|
|
474
|
+
});
|
|
475
|
+
if (currentlyResovedTemplates.length === 0) {
|
|
476
|
+
throw new PromptbookLogicError(spaceTrim(function (block) { return "\n\n Can not resolve some parameters\n It may be circular dependencies\n\n Can not resolve:\n ".concat(block(unresovedTemplates
|
|
477
|
+
.map(function (_a) {
|
|
478
|
+
var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
|
|
479
|
+
return "- {".concat(resultingParameterName, "} depends on ").concat(dependentParameterNames
|
|
480
|
+
.map(function (dependentParameterName) { return "{".concat(dependentParameterName, "}"); })
|
|
481
|
+
.join(', '));
|
|
482
|
+
})
|
|
483
|
+
.join('\n')), "\n\n Resolved:\n ").concat(block(resovedParameters.map(function (name) { return "- {".concat(name, "}"); }).join('\n')), "\n "); }));
|
|
484
|
+
}
|
|
485
|
+
resovedParameters = __spreadArray(__spreadArray([], __read(resovedParameters), false), __read(currentlyResovedTemplates.map(function (_a) {
|
|
486
|
+
var resultingParameterName = _a.resultingParameterName;
|
|
487
|
+
return resultingParameterName;
|
|
488
|
+
})), false);
|
|
489
|
+
unresovedTemplates = unresovedTemplates.filter(function (template) { return !currentlyResovedTemplates.includes(template); });
|
|
490
|
+
};
|
|
491
|
+
while (unresovedTemplates.length > 0) {
|
|
492
|
+
_loop_2();
|
|
464
493
|
}
|
|
465
|
-
return
|
|
494
|
+
return promptbook;
|
|
466
495
|
}
|
|
496
|
+
/**
|
|
497
|
+
* TODO: [🧠] Work with promptbookVersion
|
|
498
|
+
* TODO: Use here some json-schema, Zod or something similar and change it to:
|
|
499
|
+
* > /**
|
|
500
|
+
* > * Validates PromptbookJson if it is logically valid.
|
|
501
|
+
* > *
|
|
502
|
+
* > * It checks:
|
|
503
|
+
* > * - it has a valid structure
|
|
504
|
+
* > * - ...
|
|
505
|
+
* > ex port function validatePromptbookJson(promptbook: unknown): asserts promptbook is PromptbookJson {
|
|
506
|
+
*/
|
|
467
507
|
|
|
468
508
|
/**
|
|
469
|
-
*
|
|
470
|
-
*
|
|
471
|
-
* Note: If there are multiple or no code blocks the function throws an error
|
|
472
|
-
*
|
|
473
|
-
* Note: There are 3 simmilar function:
|
|
474
|
-
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
475
|
-
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
476
|
-
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
509
|
+
* This error occurs when some expectation is not met in the execution of the pipeline
|
|
477
510
|
*
|
|
478
|
-
* @
|
|
479
|
-
*
|
|
511
|
+
* @private Always catched and rethrown as `PromptbookExecutionError`
|
|
512
|
+
* Note: This is a kindof subtype of PromptbookExecutionError
|
|
480
513
|
*/
|
|
481
|
-
function
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
514
|
+
var ExpectError = /** @class */ (function (_super) {
|
|
515
|
+
__extends(ExpectError, _super);
|
|
516
|
+
function ExpectError(message) {
|
|
517
|
+
var _this = _super.call(this, message) || this;
|
|
518
|
+
_this.name = 'ExpectError';
|
|
519
|
+
Object.setPrototypeOf(_this, ExpectError.prototype);
|
|
520
|
+
return _this;
|
|
486
521
|
}
|
|
487
|
-
return
|
|
488
|
-
}
|
|
489
|
-
/***
|
|
490
|
-
* TODO: [🍓][🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
|
|
491
|
-
*/
|
|
522
|
+
return ExpectError;
|
|
523
|
+
}(Error));
|
|
492
524
|
|
|
493
525
|
/**
|
|
494
|
-
*
|
|
526
|
+
* Function isValidJsonString will tell you if the string is valid JSON or not
|
|
495
527
|
*/
|
|
496
|
-
function
|
|
497
|
-
var e_1, _a, e_2, _b;
|
|
498
|
-
var sets = [];
|
|
499
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
500
|
-
sets[_i] = arguments[_i];
|
|
501
|
-
}
|
|
502
|
-
var union = new Set();
|
|
503
|
-
try {
|
|
504
|
-
for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
|
|
505
|
-
var set = sets_1_1.value;
|
|
506
|
-
try {
|
|
507
|
-
for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
508
|
-
var item = _d.value;
|
|
509
|
-
union.add(item);
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
513
|
-
finally {
|
|
514
|
-
try {
|
|
515
|
-
if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
|
|
516
|
-
}
|
|
517
|
-
finally { if (e_2) throw e_2.error; }
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
522
|
-
finally {
|
|
523
|
-
try {
|
|
524
|
-
if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
|
|
525
|
-
}
|
|
526
|
-
finally { if (e_1) throw e_1.error; }
|
|
527
|
-
}
|
|
528
|
-
return union;
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
/**
|
|
532
|
-
* Parses the template and returns the list of all parameter names
|
|
533
|
-
*
|
|
534
|
-
* @param template the template with parameters in {curly} braces
|
|
535
|
-
* @returns the list of parameter names
|
|
536
|
-
*/
|
|
537
|
-
function extractParameters(template) {
|
|
538
|
-
var e_1, _a;
|
|
539
|
-
var matches = template.matchAll(/{\w+}/g);
|
|
540
|
-
var parameterNames = new Set();
|
|
541
|
-
try {
|
|
542
|
-
for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
|
|
543
|
-
var match = matches_1_1.value;
|
|
544
|
-
var parameterName = match[0].slice(1, -1);
|
|
545
|
-
parameterNames.add(parameterName);
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
549
|
-
finally {
|
|
550
|
-
try {
|
|
551
|
-
if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
|
|
552
|
-
}
|
|
553
|
-
finally { if (e_1) throw e_1.error; }
|
|
554
|
-
}
|
|
555
|
-
return parameterNames;
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
/**
|
|
559
|
-
* Parses the given script and returns the list of all used variables that are not defined in the script
|
|
560
|
-
*
|
|
561
|
-
* @param script from which to extract the variables
|
|
562
|
-
* @returns the list of variable names
|
|
563
|
-
* @throws {PromptbookSyntaxError} if the script is invalid
|
|
564
|
-
*/
|
|
565
|
-
function extractVariables(script) {
|
|
566
|
-
var variables = new Set();
|
|
567
|
-
script = "(()=>{".concat(script, "})()");
|
|
528
|
+
function isValidJsonString(value /* <-[👨⚖️] */) {
|
|
568
529
|
try {
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
eval(script);
|
|
572
|
-
}
|
|
573
|
-
catch (error) {
|
|
574
|
-
if (!(error instanceof ReferenceError)) {
|
|
575
|
-
throw error;
|
|
576
|
-
}
|
|
577
|
-
var undefinedName = error.message.split(' ')[0];
|
|
578
|
-
/*
|
|
579
|
-
Note: Remapping error
|
|
580
|
-
From: [ReferenceError: thing is not defined],
|
|
581
|
-
To: [Error: Parameter {thing} is not defined],
|
|
582
|
-
*/
|
|
583
|
-
if (!undefinedName) {
|
|
584
|
-
throw error;
|
|
585
|
-
}
|
|
586
|
-
if (script.includes(undefinedName + '(')) {
|
|
587
|
-
script = "const ".concat(undefinedName, " = ()=>'';") + script;
|
|
588
|
-
}
|
|
589
|
-
else {
|
|
590
|
-
variables.add(undefinedName);
|
|
591
|
-
script = "const ".concat(undefinedName, " = '';") + script;
|
|
592
|
-
}
|
|
593
|
-
}
|
|
530
|
+
JSON.parse(value);
|
|
531
|
+
return true;
|
|
594
532
|
}
|
|
595
533
|
catch (error) {
|
|
596
534
|
if (!(error instanceof Error)) {
|
|
597
535
|
throw error;
|
|
598
536
|
}
|
|
599
|
-
|
|
537
|
+
if (error.message.includes('Unexpected token')) {
|
|
538
|
+
return false;
|
|
539
|
+
}
|
|
540
|
+
return false;
|
|
600
541
|
}
|
|
601
|
-
return variables;
|
|
602
542
|
}
|
|
603
|
-
/**
|
|
604
|
-
* TODO: [🔣] Support for multiple languages - python, java,...
|
|
605
|
-
*/
|
|
606
543
|
|
|
607
544
|
/**
|
|
608
|
-
*
|
|
545
|
+
* Function `addUsage` will add multiple usages into one
|
|
609
546
|
*
|
|
610
|
-
*
|
|
611
|
-
* @returns the set of parameter names
|
|
612
|
-
* @throws {PromptbookSyntaxError} if the script is invalid
|
|
547
|
+
* Note: If you provide 0 values, it returns void usage
|
|
613
548
|
*/
|
|
614
|
-
function
|
|
615
|
-
var
|
|
616
|
-
var
|
|
617
|
-
|
|
618
|
-
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()) {
|
|
619
|
-
var parameterName = _d.value;
|
|
620
|
-
parameterNames.add(parameterName);
|
|
621
|
-
}
|
|
549
|
+
function addUsage() {
|
|
550
|
+
var usageItems = [];
|
|
551
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
552
|
+
usageItems[_i] = arguments[_i];
|
|
622
553
|
}
|
|
623
|
-
|
|
624
|
-
|
|
554
|
+
var initialStructure = {
|
|
555
|
+
price: { value: 0 },
|
|
556
|
+
input: {
|
|
557
|
+
tokensCount: { value: 0 },
|
|
558
|
+
charactersCount: { value: 0 },
|
|
559
|
+
wordsCount: { value: 0 },
|
|
560
|
+
sentencesCount: { value: 0 },
|
|
561
|
+
linesCount: { value: 0 },
|
|
562
|
+
paragraphsCount: { value: 0 },
|
|
563
|
+
pagesCount: { value: 0 },
|
|
564
|
+
},
|
|
565
|
+
output: {
|
|
566
|
+
tokensCount: { value: 0 },
|
|
567
|
+
charactersCount: { value: 0 },
|
|
568
|
+
wordsCount: { value: 0 },
|
|
569
|
+
sentencesCount: { value: 0 },
|
|
570
|
+
linesCount: { value: 0 },
|
|
571
|
+
paragraphsCount: { value: 0 },
|
|
572
|
+
pagesCount: { value: 0 },
|
|
573
|
+
},
|
|
574
|
+
};
|
|
575
|
+
return usageItems.reduce(function (acc, item) {
|
|
576
|
+
var e_1, _a, e_2, _b;
|
|
577
|
+
var _c;
|
|
578
|
+
acc.price.value += ((_c = item.price) === null || _c === void 0 ? void 0 : _c.value) || 0;
|
|
625
579
|
try {
|
|
626
|
-
|
|
580
|
+
for (var _d = __values(Object.keys(acc.input)), _e = _d.next(); !_e.done; _e = _d.next()) {
|
|
581
|
+
var key = _e.value;
|
|
582
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
583
|
+
//@ts-ignore
|
|
584
|
+
if (item.input[key]) {
|
|
585
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
586
|
+
//@ts-ignore
|
|
587
|
+
acc.input[key].value += item.input[key].value || 0;
|
|
588
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
589
|
+
//@ts-ignore
|
|
590
|
+
if (item.input[key].isUncertain) {
|
|
591
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
592
|
+
//@ts-ignore
|
|
593
|
+
acc.input[key].isUncertain = true;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
599
|
+
finally {
|
|
600
|
+
try {
|
|
601
|
+
if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
|
|
602
|
+
}
|
|
603
|
+
finally { if (e_1) throw e_1.error; }
|
|
627
604
|
}
|
|
628
|
-
finally { if (e_1) throw e_1.error; }
|
|
629
|
-
}
|
|
630
|
-
if (promptTemplate.executionType === 'SCRIPT') {
|
|
631
605
|
try {
|
|
632
|
-
for (var
|
|
633
|
-
var
|
|
634
|
-
|
|
606
|
+
for (var _f = __values(Object.keys(acc.output)), _g = _f.next(); !_g.done; _g = _f.next()) {
|
|
607
|
+
var key = _g.value;
|
|
608
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
609
|
+
//@ts-ignore
|
|
610
|
+
if (item.output[key]) {
|
|
611
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
612
|
+
//@ts-ignore
|
|
613
|
+
acc.output[key].value += item.output[key].value || 0;
|
|
614
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
615
|
+
//@ts-ignore
|
|
616
|
+
if (item.output[key].isUncertain) {
|
|
617
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
618
|
+
//@ts-ignore
|
|
619
|
+
acc.output[key].isUncertain = true;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
635
622
|
}
|
|
636
623
|
}
|
|
637
624
|
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
638
625
|
finally {
|
|
639
626
|
try {
|
|
640
|
-
if (
|
|
627
|
+
if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
|
|
641
628
|
}
|
|
642
629
|
finally { if (e_2) throw e_2.error; }
|
|
643
630
|
}
|
|
644
|
-
|
|
645
|
-
|
|
631
|
+
return acc;
|
|
632
|
+
}, initialStructure);
|
|
646
633
|
}
|
|
647
|
-
/**
|
|
648
|
-
* TODO: [🔣] If script require contentLanguage
|
|
649
|
-
*/
|
|
650
634
|
|
|
651
|
-
/* tslint:disable */
|
|
652
|
-
/*
|
|
653
|
-
TODO: Tests
|
|
654
|
-
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: 'Moje tabule' })).toEqual('/VtG7sR9rRJqwNEdM2/Moje tabule');
|
|
655
|
-
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: 'ěščřžžýáíúů' })).toEqual('/VtG7sR9rRJqwNEdM2/escrzyaieuu');
|
|
656
|
-
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: ' ahoj ' })).toEqual('/VtG7sR9rRJqwNEdM2/ahoj');
|
|
657
|
-
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: ' ahoj_ahojAhoj ahoj ' })).toEqual('/VtG7sR9rRJqwNEdM2/ahoj-ahoj-ahoj-ahoj');
|
|
658
|
-
*/
|
|
659
|
-
function normalizeTo_SCREAMING_CASE(sentence) {
|
|
660
|
-
var e_1, _a;
|
|
661
|
-
var charType;
|
|
662
|
-
var lastCharType = 'OTHER';
|
|
663
|
-
var normalizedName = '';
|
|
664
|
-
try {
|
|
665
|
-
for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
|
|
666
|
-
var char = sentence_1_1.value;
|
|
667
|
-
var normalizedChar = void 0;
|
|
668
|
-
if (/^[a-z]$/.test(char)) {
|
|
669
|
-
charType = 'LOWERCASE';
|
|
670
|
-
normalizedChar = char.toUpperCase();
|
|
671
|
-
}
|
|
672
|
-
else if (/^[A-Z]$/.test(char)) {
|
|
673
|
-
charType = 'UPPERCASE';
|
|
674
|
-
normalizedChar = char;
|
|
675
|
-
}
|
|
676
|
-
else if (/^[0-9]$/.test(char)) {
|
|
677
|
-
charType = 'NUMBER';
|
|
678
|
-
normalizedChar = char;
|
|
679
|
-
}
|
|
680
|
-
else if (/^\/$/.test(char)) {
|
|
681
|
-
charType = 'SLASH';
|
|
682
|
-
normalizedChar = char;
|
|
683
|
-
}
|
|
684
|
-
else {
|
|
685
|
-
charType = 'OTHER';
|
|
686
|
-
normalizedChar = '_';
|
|
687
|
-
}
|
|
688
|
-
if (charType !== lastCharType &&
|
|
689
|
-
!(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
|
|
690
|
-
!(lastCharType === 'NUMBER') &&
|
|
691
|
-
!(charType === 'NUMBER')) {
|
|
692
|
-
normalizedName += '_';
|
|
693
|
-
}
|
|
694
|
-
normalizedName += normalizedChar;
|
|
695
|
-
lastCharType = charType;
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
699
|
-
finally {
|
|
700
|
-
try {
|
|
701
|
-
if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
|
|
702
|
-
}
|
|
703
|
-
finally { if (e_1) throw e_1.error; }
|
|
704
|
-
}
|
|
705
|
-
normalizedName = normalizedName.replace(/_+/g, '_');
|
|
706
|
-
normalizedName = normalizedName.replace(/_?\/_?/g, '/');
|
|
707
|
-
normalizedName = normalizedName.replace(/^_/, '');
|
|
708
|
-
normalizedName = normalizedName.replace(/_$/, '');
|
|
709
|
-
return normalizedName;
|
|
710
|
-
}
|
|
711
635
|
/**
|
|
712
|
-
*
|
|
636
|
+
* Counts number of characters in the text
|
|
713
637
|
*/
|
|
638
|
+
function countCharacters(text) {
|
|
639
|
+
// Remove null characters
|
|
640
|
+
text = text.replace(/\0/g, '');
|
|
641
|
+
// Replace emojis (and also ZWJ sequence) with hyphens
|
|
642
|
+
text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
|
|
643
|
+
text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
|
|
644
|
+
text = text.replace(/\p{Extended_Pictographic}(\u{200D}\p{Extended_Pictographic})*/gu, '-');
|
|
645
|
+
return text.length;
|
|
646
|
+
}
|
|
714
647
|
|
|
715
648
|
/**
|
|
716
|
-
*
|
|
717
|
-
*
|
|
718
|
-
* @see https://github.com/webgptorg/promptbook#execution-type
|
|
649
|
+
* Counts number of lines in the text
|
|
719
650
|
*/
|
|
720
|
-
|
|
721
|
-
'
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
'
|
|
725
|
-
|
|
726
|
-
];
|
|
651
|
+
function countLines(text) {
|
|
652
|
+
if (text === '') {
|
|
653
|
+
return 0;
|
|
654
|
+
}
|
|
655
|
+
return text.split('\n').length;
|
|
656
|
+
}
|
|
727
657
|
|
|
728
658
|
/**
|
|
729
|
-
*
|
|
659
|
+
* Counts number of pages in the text
|
|
730
660
|
*/
|
|
731
|
-
|
|
661
|
+
function countPages(text) {
|
|
662
|
+
var sentencesPerPage = 5; // Assuming each page has 5 sentences
|
|
663
|
+
var sentences = text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
|
|
664
|
+
var pageCount = Math.ceil(sentences.length / sentencesPerPage);
|
|
665
|
+
return pageCount;
|
|
666
|
+
}
|
|
667
|
+
|
|
732
668
|
/**
|
|
733
|
-
*
|
|
734
|
-
* TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
|
|
735
|
-
* TODO: [👙][🧠] Just selecting gpt3 or gpt4 level of model
|
|
669
|
+
* Counts number of paragraphs in the text
|
|
736
670
|
*/
|
|
671
|
+
function countParagraphs(text) {
|
|
672
|
+
return text.split(/\n\s*\n/).filter(function (paragraph) { return paragraph.trim() !== ''; }).length;
|
|
673
|
+
}
|
|
737
674
|
|
|
738
675
|
/**
|
|
739
|
-
*
|
|
740
|
-
*
|
|
741
|
-
* @param {string} str - The string to remove Markdown tags from.
|
|
742
|
-
* @returns {string} The input string with all Markdown tags removed.
|
|
676
|
+
* Split text into sentences
|
|
743
677
|
*/
|
|
744
|
-
function
|
|
745
|
-
|
|
746
|
-
str = str.replace(/\*\*(.*?)\*\*/g, '$1');
|
|
747
|
-
// Remove italic formatting
|
|
748
|
-
str = str.replace(/\*(.*?)\*/g, '$1');
|
|
749
|
-
// Remove code formatting
|
|
750
|
-
str = str.replace(/`(.*?)`/g, '$1');
|
|
751
|
-
return str;
|
|
678
|
+
function splitIntoSentences(text) {
|
|
679
|
+
return text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
|
|
752
680
|
}
|
|
753
|
-
|
|
754
681
|
/**
|
|
755
|
-
*
|
|
756
|
-
*
|
|
757
|
-
* Unlike Number.parseInt, Number.parseFloat it will never ever result in NaN
|
|
758
|
-
* Note: it also works only with decimal numbers
|
|
759
|
-
*
|
|
760
|
-
* @returns parsed number
|
|
761
|
-
* @throws {PromptbookSyntaxError} if the value is not a number
|
|
762
|
-
*
|
|
763
|
-
* @private within the parseCommand
|
|
682
|
+
* Counts number of sentences in the text
|
|
764
683
|
*/
|
|
765
|
-
function
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
684
|
+
function countSentences(text) {
|
|
685
|
+
return splitIntoSentences(text).length;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
var defaultDiacriticsRemovalMap = [
|
|
689
|
+
{
|
|
690
|
+
base: 'A',
|
|
691
|
+
letters: '\u0041\u24B6\uFF21\u00C0\u00C1\u00C2\u1EA6\u1EA4\u1EAA\u1EA8\u00C3\u0100\u0102\u1EB0\u1EAE\u1EB4\u1EB2\u0226\u01E0\u00C4\u01DE\u1EA2\u00C5\u01FA\u01CD\u0200\u0202\u1EA0\u1EAC\u1EB6\u1E00\u0104\u023A\u2C6F',
|
|
692
|
+
},
|
|
693
|
+
{ base: 'AA', letters: '\uA732' },
|
|
694
|
+
{ base: 'AE', letters: '\u00C6\u01FC\u01E2' },
|
|
695
|
+
{ base: 'AO', letters: '\uA734' },
|
|
696
|
+
{ base: 'AU', letters: '\uA736' },
|
|
697
|
+
{ base: 'AV', letters: '\uA738\uA73A' },
|
|
698
|
+
{ base: 'AY', letters: '\uA73C' },
|
|
699
|
+
{
|
|
700
|
+
base: 'B',
|
|
701
|
+
letters: '\u0042\u24B7\uFF22\u1E02\u1E04\u1E06\u0243\u0182\u0181',
|
|
702
|
+
},
|
|
703
|
+
{
|
|
704
|
+
base: 'C',
|
|
705
|
+
letters: '\u0043\u24B8\uFF23\u0106\u0108\u010A\u010C\u00C7\u1E08\u0187\u023B\uA73E',
|
|
706
|
+
},
|
|
707
|
+
{
|
|
708
|
+
base: 'D',
|
|
709
|
+
letters: '\u0044\u24B9\uFF24\u1E0A\u010E\u1E0C\u1E10\u1E12\u1E0E\u0110\u018B\u018A\u0189\uA779\u00D0',
|
|
710
|
+
},
|
|
711
|
+
{ base: 'DZ', letters: '\u01F1\u01C4' },
|
|
712
|
+
{ base: 'Dz', letters: '\u01F2\u01C5' },
|
|
713
|
+
{
|
|
714
|
+
base: 'E',
|
|
715
|
+
letters: '\u0045\u24BA\uFF25\u00C8\u00C9\u00CA\u1EC0\u1EBE\u1EC4\u1EC2\u1EBC\u0112\u1E14\u1E16\u0114\u0116\u00CB\u1EBA\u011A\u0204\u0206\u1EB8\u1EC6\u0228\u1E1C\u0118\u1E18\u1E1A\u0190\u018E',
|
|
716
|
+
},
|
|
717
|
+
{ base: 'F', letters: '\u0046\u24BB\uFF26\u1E1E\u0191\uA77B' },
|
|
718
|
+
{
|
|
719
|
+
base: 'G',
|
|
720
|
+
letters: '\u0047\u24BC\uFF27\u01F4\u011C\u1E20\u011E\u0120\u01E6\u0122\u01E4\u0193\uA7A0\uA77D\uA77E',
|
|
721
|
+
},
|
|
722
|
+
{
|
|
723
|
+
base: 'H',
|
|
724
|
+
letters: '\u0048\u24BD\uFF28\u0124\u1E22\u1E26\u021E\u1E24\u1E28\u1E2A\u0126\u2C67\u2C75\uA78D',
|
|
725
|
+
},
|
|
726
|
+
{
|
|
727
|
+
base: 'I',
|
|
728
|
+
letters: '\u0049\u24BE\uFF29\u00CC\u00CD\u00CE\u0128\u012A\u012C\u0130\u00CF\u1E2E\u1EC8\u01CF\u0208\u020A\u1ECA\u012E\u1E2C\u0197',
|
|
729
|
+
},
|
|
730
|
+
{ base: 'J', letters: '\u004A\u24BF\uFF2A\u0134\u0248' },
|
|
731
|
+
{
|
|
732
|
+
base: 'K',
|
|
733
|
+
letters: '\u004B\u24C0\uFF2B\u1E30\u01E8\u1E32\u0136\u1E34\u0198\u2C69\uA740\uA742\uA744\uA7A2',
|
|
734
|
+
},
|
|
735
|
+
{
|
|
736
|
+
base: 'L',
|
|
737
|
+
letters: '\u004C\u24C1\uFF2C\u013F\u0139\u013D\u1E36\u1E38\u013B\u1E3C\u1E3A\u0141\u023D\u2C62\u2C60\uA748\uA746\uA780',
|
|
738
|
+
},
|
|
739
|
+
{ base: 'LJ', letters: '\u01C7' },
|
|
740
|
+
{ base: 'Lj', letters: '\u01C8' },
|
|
741
|
+
{ base: 'M', letters: '\u004D\u24C2\uFF2D\u1E3E\u1E40\u1E42\u2C6E\u019C' },
|
|
742
|
+
{
|
|
743
|
+
base: 'N',
|
|
744
|
+
letters: '\u004E\u24C3\uFF2E\u01F8\u0143\u00D1\u1E44\u0147\u1E46\u0145\u1E4A\u1E48\u0220\u019D\uA790\uA7A4',
|
|
745
|
+
},
|
|
746
|
+
{ base: 'NJ', letters: '\u01CA' },
|
|
747
|
+
{ base: 'Nj', letters: '\u01CB' },
|
|
748
|
+
{
|
|
749
|
+
base: 'O',
|
|
750
|
+
letters: '\u004F\u24C4\uFF2F\u00D2\u00D3\u00D4\u1ED2\u1ED0\u1ED6\u1ED4\u00D5\u1E4C\u022C\u1E4E\u014C\u1E50\u1E52\u014E\u022E\u0230\u00D6\u022A\u1ECE\u0150\u01D1\u020C\u020E\u01A0\u1EDC\u1EDA\u1EE0\u1EDE\u1EE2\u1ECC\u1ED8\u01EA\u01EC\u00D8\u01FE\u0186\u019F\uA74A\uA74C',
|
|
751
|
+
},
|
|
752
|
+
{ base: 'OI', letters: '\u01A2' },
|
|
753
|
+
{ base: 'OO', letters: '\uA74E' },
|
|
754
|
+
{ base: 'OU', letters: '\u0222' },
|
|
755
|
+
{ base: 'OE', letters: '\u008C\u0152' },
|
|
756
|
+
{ base: 'oe', letters: '\u009C\u0153' },
|
|
757
|
+
{
|
|
758
|
+
base: 'P',
|
|
759
|
+
letters: '\u0050\u24C5\uFF30\u1E54\u1E56\u01A4\u2C63\uA750\uA752\uA754',
|
|
760
|
+
},
|
|
761
|
+
{ base: 'Q', letters: '\u0051\u24C6\uFF31\uA756\uA758\u024A' },
|
|
762
|
+
{
|
|
763
|
+
base: 'R',
|
|
764
|
+
letters: '\u0052\u24C7\uFF32\u0154\u1E58\u0158\u0210\u0212\u1E5A\u1E5C\u0156\u1E5E\u024C\u2C64\uA75A\uA7A6\uA782',
|
|
765
|
+
},
|
|
766
|
+
{
|
|
767
|
+
base: 'S',
|
|
768
|
+
letters: '\u0053\u24C8\uFF33\u1E9E\u015A\u1E64\u015C\u1E60\u0160\u1E66\u1E62\u1E68\u0218\u015E\u2C7E\uA7A8\uA784',
|
|
769
|
+
},
|
|
770
|
+
{
|
|
771
|
+
base: 'T',
|
|
772
|
+
letters: '\u0054\u24C9\uFF34\u1E6A\u0164\u1E6C\u021A\u0162\u1E70\u1E6E\u0166\u01AC\u01AE\u023E\uA786',
|
|
773
|
+
},
|
|
774
|
+
{ base: 'TZ', letters: '\uA728' },
|
|
775
|
+
{
|
|
776
|
+
base: 'U',
|
|
777
|
+
letters: '\u0055\u24CA\uFF35\u00D9\u00DA\u00DB\u0168\u1E78\u016A\u1E7A\u016C\u00DC\u01DB\u01D7\u01D5\u01D9\u1EE6\u016E\u0170\u01D3\u0214\u0216\u01AF\u1EEA\u1EE8\u1EEE\u1EEC\u1EF0\u1EE4\u1E72\u0172\u1E76\u1E74\u0244',
|
|
778
|
+
},
|
|
779
|
+
{ base: 'V', letters: '\u0056\u24CB\uFF36\u1E7C\u1E7E\u01B2\uA75E\u0245' },
|
|
780
|
+
{ base: 'VY', letters: '\uA760' },
|
|
781
|
+
{
|
|
782
|
+
base: 'W',
|
|
783
|
+
letters: '\u0057\u24CC\uFF37\u1E80\u1E82\u0174\u1E86\u1E84\u1E88\u2C72',
|
|
784
|
+
},
|
|
785
|
+
{ base: 'X', letters: '\u0058\u24CD\uFF38\u1E8A\u1E8C' },
|
|
786
|
+
{
|
|
787
|
+
base: 'Y',
|
|
788
|
+
letters: '\u0059\u24CE\uFF39\u1EF2\u00DD\u0176\u1EF8\u0232\u1E8E\u0178\u1EF6\u1EF4\u01B3\u024E\u1EFE',
|
|
789
|
+
},
|
|
790
|
+
{
|
|
791
|
+
base: 'Z',
|
|
792
|
+
letters: '\u005A\u24CF\uFF3A\u0179\u1E90\u017B\u017D\u1E92\u1E94\u01B5\u0224\u2C7F\u2C6B\uA762',
|
|
793
|
+
},
|
|
794
|
+
{
|
|
795
|
+
base: 'a',
|
|
796
|
+
letters: '\u0061\u24D0\uFF41\u1E9A\u00E0\u00E1\u00E2\u1EA7\u1EA5\u1EAB\u1EA9\u00E3\u0101\u0103\u1EB1\u1EAF\u1EB5\u1EB3\u0227\u01E1\u00E4\u01DF\u1EA3\u00E5\u01FB\u01CE\u0201\u0203\u1EA1\u1EAD\u1EB7\u1E01\u0105\u2C65\u0250',
|
|
797
|
+
},
|
|
798
|
+
{ base: 'aa', letters: '\uA733' },
|
|
799
|
+
{ base: 'ae', letters: '\u00E6\u01FD\u01E3' },
|
|
800
|
+
{ base: 'ao', letters: '\uA735' },
|
|
801
|
+
{ base: 'au', letters: '\uA737' },
|
|
802
|
+
{ base: 'av', letters: '\uA739\uA73B' },
|
|
803
|
+
{ base: 'ay', letters: '\uA73D' },
|
|
804
|
+
{
|
|
805
|
+
base: 'b',
|
|
806
|
+
letters: '\u0062\u24D1\uFF42\u1E03\u1E05\u1E07\u0180\u0183\u0253',
|
|
807
|
+
},
|
|
808
|
+
{
|
|
809
|
+
base: 'c',
|
|
810
|
+
letters: '\u0063\u24D2\uFF43\u0107\u0109\u010B\u010D\u00E7\u1E09\u0188\u023C\uA73F\u2184',
|
|
811
|
+
},
|
|
812
|
+
{
|
|
813
|
+
base: 'd',
|
|
814
|
+
letters: '\u0064\u24D3\uFF44\u1E0B\u010F\u1E0D\u1E11\u1E13\u1E0F\u0111\u018C\u0256\u0257\uA77A',
|
|
815
|
+
},
|
|
816
|
+
{ base: 'dz', letters: '\u01F3\u01C6' },
|
|
817
|
+
{
|
|
818
|
+
base: 'e',
|
|
819
|
+
letters: '\u0065\u24D4\uFF45\u00E8\u00E9\u00EA\u1EC1\u1EBF\u1EC5\u1EC3\u1EBD\u0113\u1E15\u1E17\u0115\u0117\u00EB\u1EBB\u011B\u0205\u0207\u1EB9\u1EC7\u0229\u1E1D\u0119\u1E19\u1E1B\u0247\u025B\u01DD',
|
|
820
|
+
},
|
|
821
|
+
{ base: 'f', letters: '\u0066\u24D5\uFF46\u1E1F\u0192\uA77C' },
|
|
822
|
+
{
|
|
823
|
+
base: 'g',
|
|
824
|
+
letters: '\u0067\u24D6\uFF47\u01F5\u011D\u1E21\u011F\u0121\u01E7\u0123\u01E5\u0260\uA7A1\u1D79\uA77F',
|
|
825
|
+
},
|
|
826
|
+
{
|
|
827
|
+
base: 'h',
|
|
828
|
+
letters: '\u0068\u24D7\uFF48\u0125\u1E23\u1E27\u021F\u1E25\u1E29\u1E2B\u1E96\u0127\u2C68\u2C76\u0265',
|
|
829
|
+
},
|
|
830
|
+
{ base: 'hv', letters: '\u0195' },
|
|
831
|
+
{
|
|
832
|
+
base: 'i',
|
|
833
|
+
letters: '\u0069\u24D8\uFF49\u00EC\u00ED\u00EE\u0129\u012B\u012D\u00EF\u1E2F\u1EC9\u01D0\u0209\u020B\u1ECB\u012F\u1E2D\u0268\u0131',
|
|
834
|
+
},
|
|
835
|
+
{ base: 'j', letters: '\u006A\u24D9\uFF4A\u0135\u01F0\u0249' },
|
|
836
|
+
{
|
|
837
|
+
base: 'k',
|
|
838
|
+
letters: '\u006B\u24DA\uFF4B\u1E31\u01E9\u1E33\u0137\u1E35\u0199\u2C6A\uA741\uA743\uA745\uA7A3',
|
|
839
|
+
},
|
|
840
|
+
{
|
|
841
|
+
base: 'l',
|
|
842
|
+
letters: '\u006C\u24DB\uFF4C\u0140\u013A\u013E\u1E37\u1E39\u013C\u1E3D\u1E3B\u017F\u0142\u019A\u026B\u2C61\uA749\uA781\uA747',
|
|
843
|
+
},
|
|
844
|
+
{ base: 'lj', letters: '\u01C9' },
|
|
845
|
+
{ base: 'm', letters: '\u006D\u24DC\uFF4D\u1E3F\u1E41\u1E43\u0271\u026F' },
|
|
846
|
+
{
|
|
847
|
+
base: 'n',
|
|
848
|
+
letters: '\u006E\u24DD\uFF4E\u01F9\u0144\u00F1\u1E45\u0148\u1E47\u0146\u1E4B\u1E49\u019E\u0272\u0149\uA791\uA7A5',
|
|
849
|
+
},
|
|
850
|
+
{ base: 'nj', letters: '\u01CC' },
|
|
851
|
+
{
|
|
852
|
+
base: 'o',
|
|
853
|
+
letters: '\u006F\u24DE\uFF4F\u00F2\u00F3\u00F4\u1ED3\u1ED1\u1ED7\u1ED5\u00F5\u1E4D\u022D\u1E4F\u014D\u1E51\u1E53\u014F\u022F\u0231\u00F6\u022B\u1ECF\u0151\u01D2\u020D\u020F\u01A1\u1EDD\u1EDB\u1EE1\u1EDF\u1EE3\u1ECD\u1ED9\u01EB\u01ED\u00F8\u01FF\u0254\uA74B\uA74D\u0275',
|
|
854
|
+
},
|
|
855
|
+
{ base: 'oi', letters: '\u01A3' },
|
|
856
|
+
{ base: 'ou', letters: '\u0223' },
|
|
857
|
+
{ base: 'oo', letters: '\uA74F' },
|
|
858
|
+
{
|
|
859
|
+
base: 'p',
|
|
860
|
+
letters: '\u0070\u24DF\uFF50\u1E55\u1E57\u01A5\u1D7D\uA751\uA753\uA755',
|
|
861
|
+
},
|
|
862
|
+
{ base: 'q', letters: '\u0071\u24E0\uFF51\u024B\uA757\uA759' },
|
|
863
|
+
{
|
|
864
|
+
base: 'r',
|
|
865
|
+
letters: '\u0072\u24E1\uFF52\u0155\u1E59\u0159\u0211\u0213\u1E5B\u1E5D\u0157\u1E5F\u024D\u027D\uA75B\uA7A7\uA783',
|
|
866
|
+
},
|
|
867
|
+
{
|
|
868
|
+
base: 's',
|
|
869
|
+
letters: '\u0073\u24E2\uFF53\u00DF\u015B\u1E65\u015D\u1E61\u0161\u1E67\u1E63\u1E69\u0219\u015F\u023F\uA7A9\uA785\u1E9B',
|
|
870
|
+
},
|
|
871
|
+
{
|
|
872
|
+
base: 't',
|
|
873
|
+
letters: '\u0074\u24E3\uFF54\u1E6B\u1E97\u0165\u1E6D\u021B\u0163\u1E71\u1E6F\u0167\u01AD\u0288\u2C66\uA787',
|
|
874
|
+
},
|
|
875
|
+
{ base: 'tz', letters: '\uA729' },
|
|
876
|
+
{
|
|
877
|
+
base: 'u',
|
|
878
|
+
letters: '\u0075\u24E4\uFF55\u00F9\u00FA\u00FB\u0169\u1E79\u016B\u1E7B\u016D\u00FC\u01DC\u01D8\u01D6\u01DA\u1EE7\u016F\u0171\u01D4\u0215\u0217\u01B0\u1EEB\u1EE9\u1EEF\u1EED\u1EF1\u1EE5\u1E73\u0173\u1E77\u1E75\u0289',
|
|
879
|
+
},
|
|
880
|
+
{ base: 'v', letters: '\u0076\u24E5\uFF56\u1E7D\u1E7F\u028B\uA75F\u028C' },
|
|
881
|
+
{ base: 'vy', letters: '\uA761' },
|
|
882
|
+
{
|
|
883
|
+
base: 'w',
|
|
884
|
+
letters: '\u0077\u24E6\uFF57\u1E81\u1E83\u0175\u1E87\u1E85\u1E98\u1E89\u2C73',
|
|
885
|
+
},
|
|
886
|
+
{ base: 'x', letters: '\u0078\u24E7\uFF58\u1E8B\u1E8D' },
|
|
887
|
+
{
|
|
888
|
+
base: 'y',
|
|
889
|
+
letters: '\u0079\u24E8\uFF59\u1EF3\u00FD\u0177\u1EF9\u0233\u1E8F\u00FF\u1EF7\u1E99\u1EF5\u01B4\u024F\u1EFF',
|
|
890
|
+
},
|
|
891
|
+
{
|
|
892
|
+
base: 'z',
|
|
893
|
+
letters: '\u007A\u24E9\uFF5A\u017A\u1E91\u017C\u017E\u1E93\u1E95\u01B6\u0225\u0240\u2C6C\uA763',
|
|
894
|
+
},
|
|
895
|
+
];
|
|
896
|
+
/**
|
|
897
|
+
* Map of letters from diacritic variant to diacritless variant
|
|
898
|
+
* Contains lowercase and uppercase separatelly
|
|
899
|
+
*
|
|
900
|
+
* > "á" => "a"
|
|
901
|
+
* > "ě" => "e"
|
|
902
|
+
* > "Ă" => "A"
|
|
903
|
+
* > ...
|
|
904
|
+
*/
|
|
905
|
+
var DIACRITIC_VARIANTS_LETTERS = {};
|
|
906
|
+
// tslint:disable-next-line: prefer-for-of
|
|
907
|
+
for (var i = 0; i < defaultDiacriticsRemovalMap.length; i++) {
|
|
908
|
+
var letters = defaultDiacriticsRemovalMap[i].letters;
|
|
909
|
+
// tslint:disable-next-line: prefer-for-of
|
|
910
|
+
for (var j = 0; j < letters.length; j++) {
|
|
911
|
+
DIACRITIC_VARIANTS_LETTERS[letters[j]] = defaultDiacriticsRemovalMap[i].base;
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
// <- TODO: [🍓] Put to maker function to save execution time if not needed
|
|
915
|
+
/*
|
|
916
|
+
@see https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
|
|
917
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
918
|
+
you may not use this file except in compliance with the License.
|
|
919
|
+
You may obtain a copy of the License at
|
|
920
|
+
|
|
921
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
922
|
+
|
|
923
|
+
Unless required by applicable law or agreed to in writing, software
|
|
924
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
925
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
926
|
+
See the License for the specific language governing permissions and
|
|
927
|
+
limitations under the License.
|
|
928
|
+
*/
|
|
929
|
+
|
|
930
|
+
/**
|
|
931
|
+
*
|
|
932
|
+
*/
|
|
933
|
+
function removeDiacritics(input) {
|
|
934
|
+
/*eslint no-control-regex: "off"*/
|
|
935
|
+
return input.replace(/[^\u0000-\u007E]/g, function (a) {
|
|
936
|
+
return DIACRITIC_VARIANTS_LETTERS[a] || a;
|
|
937
|
+
});
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
/**
|
|
941
|
+
* Counts number of words in the text
|
|
942
|
+
*/
|
|
943
|
+
function countWords(text) {
|
|
944
|
+
text = text.replace(/[\p{Extended_Pictographic}]/gu, 'a');
|
|
945
|
+
text = removeDiacritics(text);
|
|
946
|
+
return text.split(/[^a-zа-я0-9]+/i).filter(function (word) { return word.length > 0; }).length;
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
/**
|
|
950
|
+
* Index of all counter functions
|
|
951
|
+
*/
|
|
952
|
+
var CountUtils = {
|
|
953
|
+
CHARACTERS: countCharacters,
|
|
954
|
+
WORDS: countWords,
|
|
955
|
+
SENTENCES: countSentences,
|
|
956
|
+
PARAGRAPHS: countParagraphs,
|
|
957
|
+
LINES: countLines,
|
|
958
|
+
PAGES: countPages,
|
|
959
|
+
};
|
|
960
|
+
|
|
961
|
+
/**
|
|
962
|
+
* Function checkExpectations will check if the expectations on given value are met
|
|
963
|
+
*
|
|
964
|
+
* Note: There are two simmilar functions:
|
|
965
|
+
* - `checkExpectations` which throws an error if the expectations are not met
|
|
966
|
+
* - `isPassingExpectations` which returns a boolean
|
|
967
|
+
*
|
|
968
|
+
* @throws {ExpectError} if the expectations are not met
|
|
969
|
+
* @returns {void} Nothing
|
|
970
|
+
*/
|
|
971
|
+
function checkExpectations(expectations, value) {
|
|
972
|
+
var e_1, _a;
|
|
973
|
+
try {
|
|
974
|
+
for (var _b = __values(Object.entries(expectations)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
975
|
+
var _d = __read(_c.value, 2), unit = _d[0], _e = _d[1], max = _e.max, min = _e.min;
|
|
976
|
+
var amount = CountUtils[unit.toUpperCase()](value);
|
|
977
|
+
if (min && amount < min) {
|
|
978
|
+
throw new ExpectError("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
|
|
979
|
+
} /* not else */
|
|
980
|
+
if (max && amount > max) {
|
|
981
|
+
throw new ExpectError("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
986
|
+
finally {
|
|
987
|
+
try {
|
|
988
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
989
|
+
}
|
|
990
|
+
finally { if (e_1) throw e_1.error; }
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
/**
|
|
994
|
+
* TODO: [💝] Unite object for expecting amount and format
|
|
995
|
+
*/
|
|
996
|
+
|
|
997
|
+
/**
|
|
998
|
+
* This error occurs during the parameter replacement in the template
|
|
999
|
+
*
|
|
1000
|
+
* Note: This is a kindof subtype of PromptbookExecutionError because it occurs during the execution of the pipeline
|
|
1001
|
+
*/
|
|
1002
|
+
var TemplateError = /** @class */ (function (_super) {
|
|
1003
|
+
__extends(TemplateError, _super);
|
|
1004
|
+
function TemplateError(message) {
|
|
1005
|
+
var _this = _super.call(this, message) || this;
|
|
1006
|
+
_this.name = 'TemplateError';
|
|
1007
|
+
Object.setPrototypeOf(_this, TemplateError.prototype);
|
|
1008
|
+
return _this;
|
|
1009
|
+
}
|
|
1010
|
+
return TemplateError;
|
|
1011
|
+
}(Error));
|
|
1012
|
+
|
|
1013
|
+
/**
|
|
1014
|
+
* Replaces parameters in template with values from parameters object
|
|
1015
|
+
*
|
|
1016
|
+
* @param template the template with parameters in {curly} braces
|
|
1017
|
+
* @param parameters the object with parameters
|
|
1018
|
+
* @returns the template with replaced parameters
|
|
1019
|
+
* @throws {TemplateError} if parameter is not defined, not closed, or not opened
|
|
1020
|
+
*
|
|
1021
|
+
* @private within the createPromptbookExecutor
|
|
1022
|
+
*/
|
|
1023
|
+
function replaceParameters(template, parameters) {
|
|
1024
|
+
var replacedTemplate = template;
|
|
1025
|
+
var match;
|
|
1026
|
+
var loopLimit = LOOP_LIMIT;
|
|
1027
|
+
var _loop_1 = function () {
|
|
1028
|
+
if (loopLimit-- < 0) {
|
|
1029
|
+
throw new UnexpectedError('Loop limit reached during parameters replacement in `replaceParameters`');
|
|
1030
|
+
}
|
|
1031
|
+
var precol = match.groups.precol;
|
|
1032
|
+
var parameterName = match.groups.parameterName;
|
|
1033
|
+
if (parameterName === '') {
|
|
1034
|
+
return "continue";
|
|
1035
|
+
}
|
|
1036
|
+
if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
|
|
1037
|
+
throw new TemplateError('Parameter is already opened or not closed');
|
|
1038
|
+
}
|
|
1039
|
+
if (parameters[parameterName] === undefined) {
|
|
1040
|
+
throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
|
|
1041
|
+
}
|
|
1042
|
+
var parameterValue = parameters[parameterName];
|
|
1043
|
+
if (parameterValue === undefined) {
|
|
1044
|
+
throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
|
|
1045
|
+
}
|
|
1046
|
+
parameterValue = parameterValue.toString();
|
|
1047
|
+
if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
|
|
1048
|
+
parameterValue = parameterValue
|
|
1049
|
+
.split('\n')
|
|
1050
|
+
.map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
|
|
1051
|
+
.join('\n');
|
|
1052
|
+
}
|
|
1053
|
+
replacedTemplate =
|
|
1054
|
+
replacedTemplate.substring(0, match.index + precol.length) +
|
|
1055
|
+
parameterValue +
|
|
1056
|
+
replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
|
|
1057
|
+
};
|
|
1058
|
+
while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
|
|
1059
|
+
.exec(replacedTemplate))) {
|
|
1060
|
+
_loop_1();
|
|
1061
|
+
}
|
|
1062
|
+
// [💫] Check if there are parameters that are not closed properly
|
|
1063
|
+
if (/{\w+$/.test(replacedTemplate)) {
|
|
1064
|
+
throw new TemplateError('Parameter is not closed');
|
|
1065
|
+
}
|
|
1066
|
+
// [💫] Check if there are parameters that are not opened properly
|
|
1067
|
+
if (/^\w+}/.test(replacedTemplate)) {
|
|
1068
|
+
throw new TemplateError('Parameter is not opened');
|
|
1069
|
+
}
|
|
1070
|
+
return replacedTemplate;
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
/**
|
|
1074
|
+
* Creates executor function from promptbook and execution tools.
|
|
1075
|
+
*
|
|
1076
|
+
* @returns The executor function
|
|
1077
|
+
* @throws {PromptbookLogicError} on logical error in the promptbook
|
|
1078
|
+
*/
|
|
1079
|
+
function createPromptbookExecutor(options) {
|
|
1080
|
+
var _this = this;
|
|
1081
|
+
var promptbook = options.promptbook, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
|
|
1082
|
+
var _b = settings.maxExecutionAttempts, maxExecutionAttempts = _b === void 0 ? 3 : _b;
|
|
1083
|
+
validatePromptbookJson(promptbook);
|
|
1084
|
+
var promptbookExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
|
|
1085
|
+
function executeSingleTemplate(currentTemplate) {
|
|
1086
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1087
|
+
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;
|
|
1088
|
+
var e_2, _j, e_4, _k, e_3, _l, _m;
|
|
1089
|
+
var _this = this;
|
|
1090
|
+
return __generator(this, function (_o) {
|
|
1091
|
+
switch (_o.label) {
|
|
1092
|
+
case 0:
|
|
1093
|
+
name = "promptbook-executor-frame-".concat(currentTemplate.name);
|
|
1094
|
+
title = currentTemplate.title;
|
|
1095
|
+
priority = promptbook.promptTemplates.length - promptbook.promptTemplates.indexOf(currentTemplate);
|
|
1096
|
+
if (!onProgress /* <- [3] */) return [3 /*break*/, 2]; /* <- [3] */
|
|
1097
|
+
return [4 /*yield*/, onProgress({
|
|
1098
|
+
name: name,
|
|
1099
|
+
title: title,
|
|
1100
|
+
isStarted: false,
|
|
1101
|
+
isDone: false,
|
|
1102
|
+
executionType: currentTemplate.executionType,
|
|
1103
|
+
parameterName: currentTemplate.resultingParameterName,
|
|
1104
|
+
parameterValue: null,
|
|
1105
|
+
// <- [3]
|
|
1106
|
+
})];
|
|
1107
|
+
case 1:
|
|
1108
|
+
_o.sent();
|
|
1109
|
+
_o.label = 2;
|
|
1110
|
+
case 2:
|
|
1111
|
+
result = null;
|
|
1112
|
+
resultString = null;
|
|
1113
|
+
expectError = null;
|
|
1114
|
+
maxAttempts = currentTemplate.executionType === 'PROMPT_DIALOG' ? Infinity : maxExecutionAttempts;
|
|
1115
|
+
jokers = currentTemplate.jokers || [];
|
|
1116
|
+
attempt = -jokers.length;
|
|
1117
|
+
_o.label = 3;
|
|
1118
|
+
case 3:
|
|
1119
|
+
if (!(attempt < maxAttempts)) return [3 /*break*/, 49];
|
|
1120
|
+
isJokerAttempt = attempt < 0;
|
|
1121
|
+
joker = jokers[jokers.length + attempt];
|
|
1122
|
+
if (isJokerAttempt && !joker) {
|
|
1123
|
+
throw new UnexpectedError("Joker not found in attempt ".concat(attempt));
|
|
1124
|
+
}
|
|
1125
|
+
result = null;
|
|
1126
|
+
resultString = null;
|
|
1127
|
+
expectError = null;
|
|
1128
|
+
if (isJokerAttempt) {
|
|
1129
|
+
if (typeof parametersToPass[joker] === 'undefined') {
|
|
1130
|
+
throw new PromptbookExecutionError("Joker parameter {".concat(joker, "} not defined"));
|
|
1131
|
+
}
|
|
1132
|
+
resultString = parametersToPass[joker];
|
|
1133
|
+
}
|
|
1134
|
+
_o.label = 4;
|
|
1135
|
+
case 4:
|
|
1136
|
+
_o.trys.push([4, 45, 46, 47]);
|
|
1137
|
+
if (!!isJokerAttempt) return [3 /*break*/, 27];
|
|
1138
|
+
_a = currentTemplate.executionType;
|
|
1139
|
+
switch (_a) {
|
|
1140
|
+
case 'SIMPLE_TEMPLATE': return [3 /*break*/, 5];
|
|
1141
|
+
case 'PROMPT_TEMPLATE': return [3 /*break*/, 6];
|
|
1142
|
+
case 'SCRIPT': return [3 /*break*/, 13];
|
|
1143
|
+
case 'PROMPT_DIALOG': return [3 /*break*/, 24];
|
|
1144
|
+
}
|
|
1145
|
+
return [3 /*break*/, 26];
|
|
1146
|
+
case 5:
|
|
1147
|
+
resultString = replaceParameters(currentTemplate.content, parametersToPass);
|
|
1148
|
+
return [3 /*break*/, 27];
|
|
1149
|
+
case 6:
|
|
1150
|
+
prompt = {
|
|
1151
|
+
title: currentTemplate.title,
|
|
1152
|
+
promptbookUrl: "".concat(promptbook.promptbookUrl
|
|
1153
|
+
? promptbook.promptbookUrl
|
|
1154
|
+
: 'anonymous' /* <- TODO: [🧠] How to deal with anonymous PROMPTBOOKs, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
|
|
1155
|
+
parameters: parametersToPass,
|
|
1156
|
+
content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
|
|
1157
|
+
modelRequirements: currentTemplate.modelRequirements,
|
|
1158
|
+
expectations: currentTemplate.expectations,
|
|
1159
|
+
expectFormat: currentTemplate.expectFormat,
|
|
1160
|
+
postprocessing: (currentTemplate.postprocessing || []).map(function (functionName) { return function (result) { return __awaiter(_this, void 0, void 0, function () {
|
|
1161
|
+
var errors, _a, _b, scriptTools, error_5, e_5_1;
|
|
1162
|
+
var e_5, _c;
|
|
1163
|
+
return __generator(this, function (_d) {
|
|
1164
|
+
switch (_d.label) {
|
|
1165
|
+
case 0:
|
|
1166
|
+
errors = [];
|
|
1167
|
+
_d.label = 1;
|
|
1168
|
+
case 1:
|
|
1169
|
+
_d.trys.push([1, 8, 9, 10]);
|
|
1170
|
+
_a = __values(tools.script), _b = _a.next();
|
|
1171
|
+
_d.label = 2;
|
|
1172
|
+
case 2:
|
|
1173
|
+
if (!!_b.done) return [3 /*break*/, 7];
|
|
1174
|
+
scriptTools = _b.value;
|
|
1175
|
+
_d.label = 3;
|
|
1176
|
+
case 3:
|
|
1177
|
+
_d.trys.push([3, 5, , 6]);
|
|
1178
|
+
return [4 /*yield*/, scriptTools.execute({
|
|
1179
|
+
scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
|
|
1180
|
+
script: "".concat(functionName, "(result)"),
|
|
1181
|
+
parameters: {
|
|
1182
|
+
result: result || '',
|
|
1183
|
+
// Note: No ...parametersToPass, because working with result only
|
|
1184
|
+
},
|
|
1185
|
+
})];
|
|
1186
|
+
case 4: return [2 /*return*/, _d.sent()];
|
|
1187
|
+
case 5:
|
|
1188
|
+
error_5 = _d.sent();
|
|
1189
|
+
if (!(error_5 instanceof Error)) {
|
|
1190
|
+
throw error_5;
|
|
1191
|
+
}
|
|
1192
|
+
errors.push(error_5);
|
|
1193
|
+
return [3 /*break*/, 6];
|
|
1194
|
+
case 6:
|
|
1195
|
+
_b = _a.next();
|
|
1196
|
+
return [3 /*break*/, 2];
|
|
1197
|
+
case 7: return [3 /*break*/, 10];
|
|
1198
|
+
case 8:
|
|
1199
|
+
e_5_1 = _d.sent();
|
|
1200
|
+
e_5 = { error: e_5_1 };
|
|
1201
|
+
return [3 /*break*/, 10];
|
|
1202
|
+
case 9:
|
|
1203
|
+
try {
|
|
1204
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
1205
|
+
}
|
|
1206
|
+
finally { if (e_5) throw e_5.error; }
|
|
1207
|
+
return [7 /*endfinally*/];
|
|
1208
|
+
case 10:
|
|
1209
|
+
if (errors.length === 0) {
|
|
1210
|
+
throw new PromptbookExecutionError('Postprocessing in LlmExecutionTools failed because no ScriptExecutionTools were provided');
|
|
1211
|
+
}
|
|
1212
|
+
else if (errors.length === 1) {
|
|
1213
|
+
throw errors[0];
|
|
1214
|
+
}
|
|
1215
|
+
else {
|
|
1216
|
+
throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n Postprocessing in LlmExecutionTools failed ".concat(errors.length, "x\n\n ").concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n\n')), "\n "); }));
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
});
|
|
1220
|
+
}); }; }),
|
|
1221
|
+
};
|
|
1222
|
+
_b = currentTemplate.modelRequirements.modelVariant;
|
|
1223
|
+
switch (_b) {
|
|
1224
|
+
case 'CHAT': return [3 /*break*/, 7];
|
|
1225
|
+
case 'COMPLETION': return [3 /*break*/, 9];
|
|
1226
|
+
}
|
|
1227
|
+
return [3 /*break*/, 11];
|
|
1228
|
+
case 7: return [4 /*yield*/, tools.llm.gptChat(prompt)];
|
|
1229
|
+
case 8:
|
|
1230
|
+
chatThread = _o.sent();
|
|
1231
|
+
// TODO: [🍬] Destroy chatThread
|
|
1232
|
+
result = chatThread;
|
|
1233
|
+
resultString = chatThread.content;
|
|
1234
|
+
return [3 /*break*/, 12];
|
|
1235
|
+
case 9: return [4 /*yield*/, tools.llm.gptComplete(prompt)];
|
|
1236
|
+
case 10:
|
|
1237
|
+
completionResult = _o.sent();
|
|
1238
|
+
result = completionResult;
|
|
1239
|
+
resultString = completionResult.content;
|
|
1240
|
+
return [3 /*break*/, 12];
|
|
1241
|
+
case 11: throw new PromptbookExecutionError("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
|
|
1242
|
+
case 12: return [3 /*break*/, 27];
|
|
1243
|
+
case 13:
|
|
1244
|
+
if (tools.script.length === 0) {
|
|
1245
|
+
throw new PromptbookExecutionError('No script execution tools are available');
|
|
1246
|
+
}
|
|
1247
|
+
if (!currentTemplate.contentLanguage) {
|
|
1248
|
+
throw new PromptbookExecutionError("Script language is not defined for prompt template \"".concat(currentTemplate.name, "\""));
|
|
1249
|
+
}
|
|
1250
|
+
// TODO: DRY [1]
|
|
1251
|
+
scriptExecutionErrors = [];
|
|
1252
|
+
_o.label = 14;
|
|
1253
|
+
case 14:
|
|
1254
|
+
_o.trys.push([14, 21, 22, 23]);
|
|
1255
|
+
_c = (e_2 = void 0, __values(tools.script)), _d = _c.next();
|
|
1256
|
+
_o.label = 15;
|
|
1257
|
+
case 15:
|
|
1258
|
+
if (!!_d.done) return [3 /*break*/, 20];
|
|
1259
|
+
scriptTools = _d.value;
|
|
1260
|
+
_o.label = 16;
|
|
1261
|
+
case 16:
|
|
1262
|
+
_o.trys.push([16, 18, , 19]);
|
|
1263
|
+
return [4 /*yield*/, scriptTools.execute({
|
|
1264
|
+
scriptLanguage: currentTemplate.contentLanguage,
|
|
1265
|
+
script: currentTemplate.content,
|
|
1266
|
+
parameters: parametersToPass,
|
|
1267
|
+
})];
|
|
1268
|
+
case 17:
|
|
1269
|
+
resultString = _o.sent();
|
|
1270
|
+
return [3 /*break*/, 20];
|
|
1271
|
+
case 18:
|
|
1272
|
+
error_2 = _o.sent();
|
|
1273
|
+
if (!(error_2 instanceof Error)) {
|
|
1274
|
+
throw error_2;
|
|
1275
|
+
}
|
|
1276
|
+
scriptExecutionErrors.push(error_2);
|
|
1277
|
+
return [3 /*break*/, 19];
|
|
1278
|
+
case 19:
|
|
1279
|
+
_d = _c.next();
|
|
1280
|
+
return [3 /*break*/, 15];
|
|
1281
|
+
case 20: return [3 /*break*/, 23];
|
|
1282
|
+
case 21:
|
|
1283
|
+
e_2_1 = _o.sent();
|
|
1284
|
+
e_2 = { error: e_2_1 };
|
|
1285
|
+
return [3 /*break*/, 23];
|
|
1286
|
+
case 22:
|
|
1287
|
+
try {
|
|
1288
|
+
if (_d && !_d.done && (_j = _c.return)) _j.call(_c);
|
|
1289
|
+
}
|
|
1290
|
+
finally { if (e_2) throw e_2.error; }
|
|
1291
|
+
return [7 /*endfinally*/];
|
|
1292
|
+
case 23:
|
|
1293
|
+
if (resultString !== null) {
|
|
1294
|
+
return [3 /*break*/, 27];
|
|
1295
|
+
}
|
|
1296
|
+
if (scriptExecutionErrors.length === 1) {
|
|
1297
|
+
throw scriptExecutionErrors[0];
|
|
1298
|
+
}
|
|
1299
|
+
else {
|
|
1300
|
+
throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n Script execution failed ".concat(scriptExecutionErrors.length, " times\n\n ").concat(block(scriptExecutionErrors
|
|
1301
|
+
.map(function (error) { return '- ' + error.message; })
|
|
1302
|
+
.join('\n\n')), "\n "); }));
|
|
1303
|
+
}
|
|
1304
|
+
case 24:
|
|
1305
|
+
if (tools.userInterface === undefined) {
|
|
1306
|
+
throw new PromptbookExecutionError('User interface tools are not available');
|
|
1307
|
+
}
|
|
1308
|
+
return [4 /*yield*/, tools.userInterface.promptDialog({
|
|
1309
|
+
promptTitle: currentTemplate.title,
|
|
1310
|
+
promptMessage: replaceParameters(currentTemplate.description || '', parametersToPass),
|
|
1311
|
+
defaultValue: replaceParameters(currentTemplate.content, parametersToPass),
|
|
1312
|
+
// TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
|
|
1313
|
+
placeholder: undefined,
|
|
1314
|
+
priority: priority,
|
|
1315
|
+
})];
|
|
1316
|
+
case 25:
|
|
1317
|
+
// TODO: [🌹] When making next attempt for `PROMPT DIALOG`, preserve the previous user input
|
|
1318
|
+
resultString = _o.sent();
|
|
1319
|
+
return [3 /*break*/, 27];
|
|
1320
|
+
case 26: throw new PromptbookExecutionError(
|
|
1321
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1322
|
+
"Unknown execution type \"".concat(currentTemplate.executionType, "\""));
|
|
1323
|
+
case 27:
|
|
1324
|
+
if (!(!isJokerAttempt && currentTemplate.postprocessing)) return [3 /*break*/, 44];
|
|
1325
|
+
_o.label = 28;
|
|
1326
|
+
case 28:
|
|
1327
|
+
_o.trys.push([28, 42, 43, 44]);
|
|
1328
|
+
_e = (e_4 = void 0, __values(currentTemplate.postprocessing)), _f = _e.next();
|
|
1329
|
+
_o.label = 29;
|
|
1330
|
+
case 29:
|
|
1331
|
+
if (!!_f.done) return [3 /*break*/, 41];
|
|
1332
|
+
functionName = _f.value;
|
|
1333
|
+
// TODO: DRY [1]
|
|
1334
|
+
scriptExecutionErrors = [];
|
|
1335
|
+
postprocessingError = null;
|
|
1336
|
+
_o.label = 30;
|
|
1337
|
+
case 30:
|
|
1338
|
+
_o.trys.push([30, 37, 38, 39]);
|
|
1339
|
+
_g = (e_3 = void 0, __values(tools.script)), _h = _g.next();
|
|
1340
|
+
_o.label = 31;
|
|
1341
|
+
case 31:
|
|
1342
|
+
if (!!_h.done) return [3 /*break*/, 36];
|
|
1343
|
+
scriptTools = _h.value;
|
|
1344
|
+
_o.label = 32;
|
|
1345
|
+
case 32:
|
|
1346
|
+
_o.trys.push([32, 34, , 35]);
|
|
1347
|
+
return [4 /*yield*/, scriptTools.execute({
|
|
1348
|
+
scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
|
|
1349
|
+
script: "".concat(functionName, "(resultString)"),
|
|
1350
|
+
parameters: {
|
|
1351
|
+
resultString: resultString || '',
|
|
1352
|
+
// Note: No ...parametersToPass, because working with result only
|
|
1353
|
+
},
|
|
1354
|
+
})];
|
|
1355
|
+
case 33:
|
|
1356
|
+
resultString = _o.sent();
|
|
1357
|
+
postprocessingError = null;
|
|
1358
|
+
return [3 /*break*/, 36];
|
|
1359
|
+
case 34:
|
|
1360
|
+
error_3 = _o.sent();
|
|
1361
|
+
if (!(error_3 instanceof Error)) {
|
|
1362
|
+
throw error_3;
|
|
1363
|
+
}
|
|
1364
|
+
postprocessingError = error_3;
|
|
1365
|
+
scriptExecutionErrors.push(error_3);
|
|
1366
|
+
return [3 /*break*/, 35];
|
|
1367
|
+
case 35:
|
|
1368
|
+
_h = _g.next();
|
|
1369
|
+
return [3 /*break*/, 31];
|
|
1370
|
+
case 36: return [3 /*break*/, 39];
|
|
1371
|
+
case 37:
|
|
1372
|
+
e_3_1 = _o.sent();
|
|
1373
|
+
e_3 = { error: e_3_1 };
|
|
1374
|
+
return [3 /*break*/, 39];
|
|
1375
|
+
case 38:
|
|
1376
|
+
try {
|
|
1377
|
+
if (_h && !_h.done && (_l = _g.return)) _l.call(_g);
|
|
1378
|
+
}
|
|
1379
|
+
finally { if (e_3) throw e_3.error; }
|
|
1380
|
+
return [7 /*endfinally*/];
|
|
1381
|
+
case 39:
|
|
1382
|
+
if (postprocessingError) {
|
|
1383
|
+
throw postprocessingError;
|
|
1384
|
+
}
|
|
1385
|
+
_o.label = 40;
|
|
1386
|
+
case 40:
|
|
1387
|
+
_f = _e.next();
|
|
1388
|
+
return [3 /*break*/, 29];
|
|
1389
|
+
case 41: return [3 /*break*/, 44];
|
|
1390
|
+
case 42:
|
|
1391
|
+
e_4_1 = _o.sent();
|
|
1392
|
+
e_4 = { error: e_4_1 };
|
|
1393
|
+
return [3 /*break*/, 44];
|
|
1394
|
+
case 43:
|
|
1395
|
+
try {
|
|
1396
|
+
if (_f && !_f.done && (_k = _e.return)) _k.call(_e);
|
|
1397
|
+
}
|
|
1398
|
+
finally { if (e_4) throw e_4.error; }
|
|
1399
|
+
return [7 /*endfinally*/];
|
|
1400
|
+
case 44:
|
|
1401
|
+
// TODO: [💝] Unite object for expecting amount and format
|
|
1402
|
+
if (currentTemplate.expectFormat) {
|
|
1403
|
+
if (currentTemplate.expectFormat === 'JSON') {
|
|
1404
|
+
if (!isValidJsonString(resultString || '')) {
|
|
1405
|
+
throw new ExpectError('Expected valid JSON string');
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
// TODO: [💝] Unite object for expecting amount and format
|
|
1410
|
+
if (currentTemplate.expectations) {
|
|
1411
|
+
checkExpectations(currentTemplate.expectations, resultString || '');
|
|
1412
|
+
}
|
|
1413
|
+
return [3 /*break*/, 49];
|
|
1414
|
+
case 45:
|
|
1415
|
+
error_4 = _o.sent();
|
|
1416
|
+
if (!(error_4 instanceof ExpectError)) {
|
|
1417
|
+
throw error_4;
|
|
1418
|
+
}
|
|
1419
|
+
expectError = error_4;
|
|
1420
|
+
return [3 /*break*/, 47];
|
|
1421
|
+
case 46:
|
|
1422
|
+
if (!isJokerAttempt &&
|
|
1423
|
+
currentTemplate.executionType === 'PROMPT_TEMPLATE' &&
|
|
1424
|
+
prompt
|
|
1425
|
+
// <- Note: [2] When some expected parameter is not defined, error will occur in replaceParameters
|
|
1426
|
+
// In that case we don’t want to make a report about it because it’s not a llm execution error
|
|
1427
|
+
) {
|
|
1428
|
+
// TODO: [🧠] Maybe put other executionTypes into report
|
|
1429
|
+
executionReport.promptExecutions.push({
|
|
1430
|
+
prompt: {
|
|
1431
|
+
title: currentTemplate.title /* <- Note: If title in promptbook contains emojis, pass it innto report */,
|
|
1432
|
+
content: prompt.content,
|
|
1433
|
+
modelRequirements: prompt.modelRequirements,
|
|
1434
|
+
expectations: prompt.expectations,
|
|
1435
|
+
expectFormat: prompt.expectFormat,
|
|
1436
|
+
// <- Note: Do want to pass ONLY wanted information to the report
|
|
1437
|
+
},
|
|
1438
|
+
result: result || undefined,
|
|
1439
|
+
error: expectError || undefined,
|
|
1440
|
+
});
|
|
1441
|
+
}
|
|
1442
|
+
return [7 /*endfinally*/];
|
|
1443
|
+
case 47:
|
|
1444
|
+
if (expectError !== null && attempt === maxAttempts - 1) {
|
|
1445
|
+
throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n LLM execution failed ".concat(maxExecutionAttempts, "x\n\n ---\n Last error ").concat((expectError === null || expectError === void 0 ? void 0 : expectError.name) || '', ":\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n\n Last result:\n ").concat(resultString, "\n ---\n "); }));
|
|
1446
|
+
}
|
|
1447
|
+
_o.label = 48;
|
|
1448
|
+
case 48:
|
|
1449
|
+
attempt++;
|
|
1450
|
+
return [3 /*break*/, 3];
|
|
1451
|
+
case 49:
|
|
1452
|
+
if (resultString === null) {
|
|
1453
|
+
throw new UnexpectedError('Something went wrong and prompt result is null');
|
|
1454
|
+
}
|
|
1455
|
+
if (onProgress /* <- [3] */) {
|
|
1456
|
+
onProgress({
|
|
1457
|
+
name: name,
|
|
1458
|
+
title: title,
|
|
1459
|
+
isStarted: true,
|
|
1460
|
+
isDone: true,
|
|
1461
|
+
executionType: currentTemplate.executionType,
|
|
1462
|
+
parameterName: currentTemplate.resultingParameterName,
|
|
1463
|
+
parameterValue: resultString,
|
|
1464
|
+
// <- [3]
|
|
1465
|
+
});
|
|
1466
|
+
}
|
|
1467
|
+
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));
|
|
1468
|
+
return [2 /*return*/];
|
|
1469
|
+
}
|
|
1470
|
+
});
|
|
1471
|
+
});
|
|
1472
|
+
}
|
|
1473
|
+
var parametersToPass, executionReport, resovedParameters_1, unresovedTemplates, resolving_1, loopLimit, _loop_1, error_1, usage_1, _a, _b, parameter, usage;
|
|
1474
|
+
var e_1, _c;
|
|
1475
|
+
return __generator(this, function (_d) {
|
|
1476
|
+
switch (_d.label) {
|
|
1477
|
+
case 0:
|
|
1478
|
+
parametersToPass = inputParameters;
|
|
1479
|
+
executionReport = {
|
|
1480
|
+
promptbookUrl: promptbook.promptbookUrl,
|
|
1481
|
+
title: promptbook.title,
|
|
1482
|
+
promptbookUsedVersion: PROMPTBOOK_VERSION,
|
|
1483
|
+
promptbookRequestedVersion: promptbook.promptbookVersion,
|
|
1484
|
+
description: promptbook.description,
|
|
1485
|
+
promptExecutions: [],
|
|
1486
|
+
};
|
|
1487
|
+
_d.label = 1;
|
|
1488
|
+
case 1:
|
|
1489
|
+
_d.trys.push([1, 6, , 7]);
|
|
1490
|
+
resovedParameters_1 = promptbook.parameters
|
|
1491
|
+
.filter(function (_a) {
|
|
1492
|
+
var isInput = _a.isInput;
|
|
1493
|
+
return isInput;
|
|
1494
|
+
})
|
|
1495
|
+
.map(function (_a) {
|
|
1496
|
+
var name = _a.name;
|
|
1497
|
+
return name;
|
|
1498
|
+
});
|
|
1499
|
+
unresovedTemplates = __spreadArray([], __read(promptbook.promptTemplates), false);
|
|
1500
|
+
resolving_1 = [];
|
|
1501
|
+
loopLimit = LOOP_LIMIT;
|
|
1502
|
+
_loop_1 = function () {
|
|
1503
|
+
var currentTemplate, work_1;
|
|
1504
|
+
return __generator(this, function (_e) {
|
|
1505
|
+
switch (_e.label) {
|
|
1506
|
+
case 0:
|
|
1507
|
+
if (loopLimit-- < 0) {
|
|
1508
|
+
throw new UnexpectedError('Loop limit reached during resolving parameters promptbook execution');
|
|
1509
|
+
}
|
|
1510
|
+
currentTemplate = unresovedTemplates.find(function (template) {
|
|
1511
|
+
return template.dependentParameterNames.every(function (name) { return resovedParameters_1.includes(name); });
|
|
1512
|
+
});
|
|
1513
|
+
if (!(!currentTemplate && resolving_1.length === 0)) return [3 /*break*/, 1];
|
|
1514
|
+
throw new UnexpectedError(spaceTrim("\n Can not resolve some parameters\n\n Note: This should be catched during validatePromptbookJson\n "));
|
|
1515
|
+
case 1:
|
|
1516
|
+
if (!!currentTemplate) return [3 /*break*/, 3];
|
|
1517
|
+
/* [5] */ return [4 /*yield*/, Promise.race(resolving_1)];
|
|
1518
|
+
case 2:
|
|
1519
|
+
/* [5] */ _e.sent();
|
|
1520
|
+
return [3 /*break*/, 4];
|
|
1521
|
+
case 3:
|
|
1522
|
+
unresovedTemplates = unresovedTemplates.filter(function (template) { return template !== currentTemplate; });
|
|
1523
|
+
work_1 = executeSingleTemplate(currentTemplate)
|
|
1524
|
+
.then(function () {
|
|
1525
|
+
resovedParameters_1 = __spreadArray(__spreadArray([], __read(resovedParameters_1), false), [currentTemplate.resultingParameterName], false);
|
|
1526
|
+
})
|
|
1527
|
+
.then(function () {
|
|
1528
|
+
resolving_1 = resolving_1.filter(function (w) { return w !== work_1; });
|
|
1529
|
+
});
|
|
1530
|
+
resolving_1.push(work_1);
|
|
1531
|
+
_e.label = 4;
|
|
1532
|
+
case 4: return [2 /*return*/];
|
|
1533
|
+
}
|
|
1534
|
+
});
|
|
1535
|
+
};
|
|
1536
|
+
_d.label = 2;
|
|
1537
|
+
case 2:
|
|
1538
|
+
if (!(unresovedTemplates.length > 0)) return [3 /*break*/, 4];
|
|
1539
|
+
return [5 /*yield**/, _loop_1()];
|
|
1540
|
+
case 3:
|
|
1541
|
+
_d.sent();
|
|
1542
|
+
return [3 /*break*/, 2];
|
|
1543
|
+
case 4: return [4 /*yield*/, Promise.all(resolving_1)];
|
|
1544
|
+
case 5:
|
|
1545
|
+
_d.sent();
|
|
1546
|
+
return [3 /*break*/, 7];
|
|
1547
|
+
case 6:
|
|
1548
|
+
error_1 = _d.sent();
|
|
1549
|
+
if (!(error_1 instanceof Error)) {
|
|
1550
|
+
throw error_1;
|
|
1551
|
+
}
|
|
1552
|
+
usage_1 = addUsage.apply(void 0, __spreadArray([], __read(executionReport.promptExecutions.map(function (_a) {
|
|
1553
|
+
var result = _a.result;
|
|
1554
|
+
return (result === null || result === void 0 ? void 0 : result.usage) || addUsage();
|
|
1555
|
+
})), false));
|
|
1556
|
+
return [2 /*return*/, {
|
|
1557
|
+
isSuccessful: false,
|
|
1558
|
+
errors: [error_1],
|
|
1559
|
+
usage: usage_1,
|
|
1560
|
+
executionReport: executionReport,
|
|
1561
|
+
outputParameters: parametersToPass,
|
|
1562
|
+
}];
|
|
1563
|
+
case 7:
|
|
1564
|
+
try {
|
|
1565
|
+
// Note: Filter ONLY output parameters
|
|
1566
|
+
for (_a = __values(promptbook.parameters), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
1567
|
+
parameter = _b.value;
|
|
1568
|
+
if (parameter.isOutput) {
|
|
1569
|
+
continue;
|
|
1570
|
+
}
|
|
1571
|
+
delete parametersToPass[parameter.name];
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1575
|
+
finally {
|
|
1576
|
+
try {
|
|
1577
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
1578
|
+
}
|
|
1579
|
+
finally { if (e_1) throw e_1.error; }
|
|
1580
|
+
}
|
|
1581
|
+
usage = addUsage.apply(void 0, __spreadArray([], __read(executionReport.promptExecutions.map(function (_a) {
|
|
1582
|
+
var result = _a.result;
|
|
1583
|
+
return (result === null || result === void 0 ? void 0 : result.usage) || addUsage();
|
|
1584
|
+
})), false));
|
|
1585
|
+
return [2 /*return*/, {
|
|
1586
|
+
isSuccessful: true,
|
|
1587
|
+
errors: [],
|
|
1588
|
+
usage: usage,
|
|
1589
|
+
executionReport: executionReport,
|
|
1590
|
+
outputParameters: parametersToPass,
|
|
1591
|
+
}];
|
|
1592
|
+
}
|
|
1593
|
+
});
|
|
1594
|
+
}); };
|
|
1595
|
+
return promptbookExecutor;
|
|
1596
|
+
}
|
|
1597
|
+
/**
|
|
1598
|
+
* TODO: [🧠] When not meet expectations in PROMPT_DIALOG, make some way to tell the user
|
|
1599
|
+
* TODO: [👧] Strongly type the executors to avoid need of remove nullables whtn noUncheckedIndexedAccess in tsconfig.json
|
|
1600
|
+
* Note: CreatePromptbookExecutorOptions are just connected to PromptbookExecutor so do not extract to types folder
|
|
1601
|
+
* TODO: [🧠][3] transparent = (report intermediate parameters) / opaque execution = (report only output parameters) progress reporting mode
|
|
1602
|
+
*/
|
|
1603
|
+
|
|
1604
|
+
/**
|
|
1605
|
+
* Makes first letter of a string uppercase
|
|
1606
|
+
*
|
|
1607
|
+
*/
|
|
1608
|
+
function capitalize(word) {
|
|
1609
|
+
return word.substring(0, 1).toUpperCase() + word.substring(1);
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
/**
|
|
1613
|
+
* Converts promptbook in JSON format to string format
|
|
1614
|
+
*
|
|
1615
|
+
* @param promptbookJson Promptbook in JSON format (.ptbk.json)
|
|
1616
|
+
* @returns Promptbook in string format (.ptbk.md)
|
|
1617
|
+
*/
|
|
1618
|
+
function promptbookJsonToString(promptbookJson) {
|
|
1619
|
+
var e_1, _a, e_2, _b, e_3, _c, e_4, _d, e_5, _e, e_6, _f;
|
|
1620
|
+
var title = promptbookJson.title, promptbookUrl = promptbookJson.promptbookUrl, promptbookVersion = promptbookJson.promptbookVersion, description = promptbookJson.description, parameters = promptbookJson.parameters, promptTemplates = promptbookJson.promptTemplates;
|
|
1621
|
+
var promptbookString = "# ".concat(title);
|
|
1622
|
+
if (description) {
|
|
1623
|
+
promptbookString += '\n\n';
|
|
1624
|
+
promptbookString += description;
|
|
776
1625
|
}
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
1626
|
+
// TODO:> const commands: Array<Command>
|
|
1627
|
+
var commands = [];
|
|
1628
|
+
if (promptbookUrl) {
|
|
1629
|
+
commands.push("PROMPTBOOK URL ".concat(promptbookUrl));
|
|
1630
|
+
}
|
|
1631
|
+
commands.push("PROMPTBOOK VERSION ".concat(promptbookVersion));
|
|
1632
|
+
promptbookString = prettifyMarkdown(promptbookString);
|
|
1633
|
+
try {
|
|
1634
|
+
for (var _g = __values(parameters.filter(function (_a) {
|
|
1635
|
+
var isInput = _a.isInput;
|
|
1636
|
+
return isInput;
|
|
1637
|
+
})), _h = _g.next(); !_h.done; _h = _g.next()) {
|
|
1638
|
+
var parameter = _h.value;
|
|
1639
|
+
commands.push("INPUT PARAMETER ".concat(promptTemplateParameterJsonToString(parameter)));
|
|
1640
|
+
}
|
|
1641
|
+
}
|
|
1642
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1643
|
+
finally {
|
|
1644
|
+
try {
|
|
1645
|
+
if (_h && !_h.done && (_a = _g.return)) _a.call(_g);
|
|
1646
|
+
}
|
|
1647
|
+
finally { if (e_1) throw e_1.error; }
|
|
1648
|
+
}
|
|
1649
|
+
try {
|
|
1650
|
+
for (var _j = __values(parameters.filter(function (_a) {
|
|
1651
|
+
var isOutput = _a.isOutput;
|
|
1652
|
+
return isOutput;
|
|
1653
|
+
})), _k = _j.next(); !_k.done; _k = _j.next()) {
|
|
1654
|
+
var parameter = _k.value;
|
|
1655
|
+
commands.push("OUTPUT PARAMETER ".concat(promptTemplateParameterJsonToString(parameter)));
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
1659
|
+
finally {
|
|
1660
|
+
try {
|
|
1661
|
+
if (_k && !_k.done && (_b = _j.return)) _b.call(_j);
|
|
1662
|
+
}
|
|
1663
|
+
finally { if (e_2) throw e_2.error; }
|
|
1664
|
+
}
|
|
1665
|
+
promptbookString += '\n\n';
|
|
1666
|
+
promptbookString += commands.map(function (command) { return "- ".concat(command); }).join('\n');
|
|
1667
|
+
try {
|
|
1668
|
+
for (var promptTemplates_1 = __values(promptTemplates), promptTemplates_1_1 = promptTemplates_1.next(); !promptTemplates_1_1.done; promptTemplates_1_1 = promptTemplates_1.next()) {
|
|
1669
|
+
var promptTemplate = promptTemplates_1_1.value;
|
|
1670
|
+
var
|
|
1671
|
+
/* Note: Not using:> name, */
|
|
1672
|
+
title_1 = promptTemplate.title, description_1 = promptTemplate.description,
|
|
1673
|
+
/* Note: dependentParameterNames, */
|
|
1674
|
+
jokers = promptTemplate.jokers, executionType = promptTemplate.executionType, content = promptTemplate.content, postprocessing = promptTemplate.postprocessing, expectations = promptTemplate.expectations, expectFormat = promptTemplate.expectFormat, resultingParameterName = promptTemplate.resultingParameterName;
|
|
1675
|
+
promptbookString += '\n\n';
|
|
1676
|
+
promptbookString += "## ".concat(title_1);
|
|
1677
|
+
if (description_1) {
|
|
1678
|
+
promptbookString += '\n\n';
|
|
1679
|
+
promptbookString += description_1;
|
|
1680
|
+
}
|
|
1681
|
+
// TODO:> const commands: Array<Command>
|
|
1682
|
+
var commands_1 = [];
|
|
1683
|
+
var contentLanguage = 'text';
|
|
1684
|
+
if (executionType === 'PROMPT_TEMPLATE') {
|
|
1685
|
+
var modelRequirements = promptTemplate.modelRequirements;
|
|
1686
|
+
var modelName = modelRequirements.modelName, modelVariant = modelRequirements.modelVariant;
|
|
1687
|
+
commands_1.push("EXECUTE PROMPT TEMPLATE");
|
|
1688
|
+
if (modelVariant) {
|
|
1689
|
+
commands_1.push("MODEL VARIANT ".concat(capitalize(modelVariant)));
|
|
1690
|
+
}
|
|
1691
|
+
if (modelName) {
|
|
1692
|
+
commands_1.push("MODEL NAME `".concat(modelName, "`"));
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
else if (executionType === 'SIMPLE_TEMPLATE') {
|
|
1696
|
+
commands_1.push("SIMPLE TEMPLATE");
|
|
1697
|
+
// Note: Nothing special here
|
|
1698
|
+
}
|
|
1699
|
+
else if (executionType === 'SCRIPT') {
|
|
1700
|
+
commands_1.push("EXECUTE SCRIPT");
|
|
1701
|
+
if (promptTemplate.contentLanguage) {
|
|
1702
|
+
contentLanguage = promptTemplate.contentLanguage;
|
|
1703
|
+
}
|
|
1704
|
+
else {
|
|
1705
|
+
contentLanguage = '';
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
else if (executionType === 'PROMPT_DIALOG') {
|
|
1709
|
+
commands_1.push("PROMPT DIALOG");
|
|
1710
|
+
// Note: Nothing special here
|
|
1711
|
+
}
|
|
1712
|
+
if (jokers) {
|
|
1713
|
+
try {
|
|
1714
|
+
for (var jokers_1 = (e_4 = void 0, __values(jokers)), jokers_1_1 = jokers_1.next(); !jokers_1_1.done; jokers_1_1 = jokers_1.next()) {
|
|
1715
|
+
var joker = jokers_1_1.value;
|
|
1716
|
+
commands_1.push("JOKER {".concat(joker, "}"));
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
1720
|
+
finally {
|
|
1721
|
+
try {
|
|
1722
|
+
if (jokers_1_1 && !jokers_1_1.done && (_d = jokers_1.return)) _d.call(jokers_1);
|
|
1723
|
+
}
|
|
1724
|
+
finally { if (e_4) throw e_4.error; }
|
|
1725
|
+
}
|
|
1726
|
+
} /* not else */
|
|
1727
|
+
if (postprocessing) {
|
|
1728
|
+
try {
|
|
1729
|
+
for (var postprocessing_1 = (e_5 = void 0, __values(postprocessing)), postprocessing_1_1 = postprocessing_1.next(); !postprocessing_1_1.done; postprocessing_1_1 = postprocessing_1.next()) {
|
|
1730
|
+
var postprocessingFunctionName = postprocessing_1_1.value;
|
|
1731
|
+
commands_1.push("POSTPROCESSING `".concat(postprocessingFunctionName, "`"));
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
1735
|
+
finally {
|
|
1736
|
+
try {
|
|
1737
|
+
if (postprocessing_1_1 && !postprocessing_1_1.done && (_e = postprocessing_1.return)) _e.call(postprocessing_1);
|
|
1738
|
+
}
|
|
1739
|
+
finally { if (e_5) throw e_5.error; }
|
|
1740
|
+
}
|
|
1741
|
+
} /* not else */
|
|
1742
|
+
if (expectations) {
|
|
1743
|
+
try {
|
|
1744
|
+
for (var _l = (e_6 = void 0, __values(Object.entries(expectations))), _m = _l.next(); !_m.done; _m = _l.next()) {
|
|
1745
|
+
var _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], min = _p.min, max = _p.max;
|
|
1746
|
+
if (min === max) {
|
|
1747
|
+
commands_1.push("EXPECT EXACTLY ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
|
|
1748
|
+
}
|
|
1749
|
+
else {
|
|
1750
|
+
if (min !== undefined) {
|
|
1751
|
+
commands_1.push("EXPECT MIN ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
|
|
1752
|
+
} /* not else */
|
|
1753
|
+
if (max !== undefined) {
|
|
1754
|
+
commands_1.push("EXPECT MAX ".concat(max, " ").concat(capitalize(unit + (max > 1 ? 's' : ''))));
|
|
1755
|
+
}
|
|
1756
|
+
}
|
|
1757
|
+
}
|
|
1758
|
+
}
|
|
1759
|
+
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
1760
|
+
finally {
|
|
1761
|
+
try {
|
|
1762
|
+
if (_m && !_m.done && (_f = _l.return)) _f.call(_l);
|
|
1763
|
+
}
|
|
1764
|
+
finally { if (e_6) throw e_6.error; }
|
|
1765
|
+
}
|
|
1766
|
+
} /* not else */
|
|
1767
|
+
if (expectFormat) {
|
|
1768
|
+
if (expectFormat === 'JSON') {
|
|
1769
|
+
// TODO: @deprecated remove
|
|
1770
|
+
commands_1.push("EXPECT JSON");
|
|
1771
|
+
}
|
|
1772
|
+
} /* not else */
|
|
1773
|
+
promptbookString += '\n\n';
|
|
1774
|
+
promptbookString += commands_1.map(function (command) { return "- ".concat(command); }).join('\n');
|
|
1775
|
+
promptbookString += '\n\n';
|
|
1776
|
+
promptbookString += '```' + contentLanguage;
|
|
1777
|
+
promptbookString += '\n';
|
|
1778
|
+
promptbookString += spaceTrim$1(content);
|
|
1779
|
+
// <- TODO: !!! Escape
|
|
1780
|
+
// <- TODO: [🧠] Some clear strategy how to spaceTrim the blocks
|
|
1781
|
+
promptbookString += '\n';
|
|
1782
|
+
promptbookString += '```';
|
|
1783
|
+
promptbookString += '\n\n';
|
|
1784
|
+
promptbookString += "`-> {".concat(resultingParameterName, "}`"); // <- TODO: !!! If the parameter here has description, add it and use promptTemplateParameterJsonToString
|
|
1785
|
+
}
|
|
1786
|
+
}
|
|
1787
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
1788
|
+
finally {
|
|
1789
|
+
try {
|
|
1790
|
+
if (promptTemplates_1_1 && !promptTemplates_1_1.done && (_c = promptTemplates_1.return)) _c.call(promptTemplates_1);
|
|
1791
|
+
}
|
|
1792
|
+
finally { if (e_3) throw e_3.error; }
|
|
1793
|
+
}
|
|
1794
|
+
return promptbookString;
|
|
1795
|
+
}
|
|
1796
|
+
/**
|
|
1797
|
+
* @private internal util of promptbookJsonToString
|
|
1798
|
+
*/
|
|
1799
|
+
function promptTemplateParameterJsonToString(promptTemplateParameterJson) {
|
|
1800
|
+
var name = promptTemplateParameterJson.name, description = promptTemplateParameterJson.description;
|
|
1801
|
+
var parameterString = "{".concat(name, "}");
|
|
1802
|
+
if (description) {
|
|
1803
|
+
parameterString = "".concat(parameterString, " ").concat(description);
|
|
1804
|
+
}
|
|
1805
|
+
return parameterString;
|
|
1806
|
+
}
|
|
1807
|
+
/**
|
|
1808
|
+
* TODO: Escape all
|
|
1809
|
+
*/
|
|
1810
|
+
|
|
1811
|
+
/**
|
|
1812
|
+
* This error indicates that promptbook not found in the library
|
|
1813
|
+
*/
|
|
1814
|
+
var PromptbookNotFoundError = /** @class */ (function (_super) {
|
|
1815
|
+
__extends(PromptbookNotFoundError, _super);
|
|
1816
|
+
function PromptbookNotFoundError(message) {
|
|
1817
|
+
var _this = _super.call(this, message) || this;
|
|
1818
|
+
_this.name = 'PromptbookNotFoundError';
|
|
1819
|
+
Object.setPrototypeOf(_this, PromptbookNotFoundError.prototype);
|
|
1820
|
+
return _this;
|
|
1821
|
+
}
|
|
1822
|
+
return PromptbookNotFoundError;
|
|
1823
|
+
}(Error));
|
|
1824
|
+
|
|
1825
|
+
/**
|
|
1826
|
+
* This error indicates errors in referencing promptbooks between each other
|
|
1827
|
+
*/
|
|
1828
|
+
var PromptbookReferenceError = /** @class */ (function (_super) {
|
|
1829
|
+
__extends(PromptbookReferenceError, _super);
|
|
1830
|
+
function PromptbookReferenceError(message) {
|
|
1831
|
+
var _this = _super.call(this, message) || this;
|
|
1832
|
+
_this.name = 'PromptbookReferenceError';
|
|
1833
|
+
Object.setPrototypeOf(_this, PromptbookReferenceError.prototype);
|
|
1834
|
+
return _this;
|
|
1835
|
+
}
|
|
1836
|
+
return PromptbookReferenceError;
|
|
1837
|
+
}(Error));
|
|
1838
|
+
|
|
1839
|
+
/**
|
|
1840
|
+
* Library of promptbooks that groups together promptbooks for an application.
|
|
1841
|
+
* This implementation is a very thin wrapper around the Array / Map of promptbooks.
|
|
1842
|
+
*
|
|
1843
|
+
* @see https://github.com/webgptorg/promptbook#promptbook-library
|
|
1844
|
+
*/
|
|
1845
|
+
var SimplePromptbookLibrary = /** @class */ (function () {
|
|
1846
|
+
/**
|
|
1847
|
+
* Constructs a promptbook library from promptbooks
|
|
1848
|
+
*
|
|
1849
|
+
* @param promptbooks !!!
|
|
1850
|
+
*
|
|
1851
|
+
* Note: During the construction logic of all promptbooks are validated
|
|
1852
|
+
* Note: It is not recommended to use this constructor directly, use `createPromptbookLibraryFromSources` *(or other variant)* instead
|
|
1853
|
+
*/
|
|
1854
|
+
function SimplePromptbookLibrary() {
|
|
1855
|
+
var e_1, _a;
|
|
1856
|
+
var promptbooks = [];
|
|
1857
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1858
|
+
promptbooks[_i] = arguments[_i];
|
|
1859
|
+
}
|
|
1860
|
+
this.library = new Map();
|
|
1861
|
+
try {
|
|
1862
|
+
for (var promptbooks_1 = __values(promptbooks), promptbooks_1_1 = promptbooks_1.next(); !promptbooks_1_1.done; promptbooks_1_1 = promptbooks_1.next()) {
|
|
1863
|
+
var promptbook = promptbooks_1_1.value;
|
|
1864
|
+
if (promptbook.promptbookUrl === undefined) {
|
|
1865
|
+
throw new PromptbookReferenceError(spaceTrim("\n Promptbook with name \"".concat(promptbook.title, "\" does not have defined URL\n\n Note: Promptbooks without URLs are called anonymous promptbooks\n They can be used as standalone promptbooks, but they cannot be referenced by other promptbooks\n And also they cannot be used in the promptbook library\n\n ")));
|
|
1866
|
+
}
|
|
1867
|
+
validatePromptbookJson(promptbook);
|
|
1868
|
+
// Note: [🦄]
|
|
1869
|
+
if (this.library.has(promptbook.promptbookUrl) &&
|
|
1870
|
+
promptbookJsonToString(promptbook) !==
|
|
1871
|
+
promptbookJsonToString(this.library.get(promptbook.promptbookUrl))) {
|
|
1872
|
+
throw new PromptbookReferenceError(spaceTrim("\n Promptbook with URL \"".concat(promptbook.promptbookUrl, "\" is already in the library\n\n Note: Promptbooks with the same URL are not allowed\n Note: Automatically check whether the promptbooks are the same BUT they are DIFFERENT\n\n ")));
|
|
1873
|
+
}
|
|
1874
|
+
this.library.set(promptbook.promptbookUrl, promptbook);
|
|
1875
|
+
}
|
|
1876
|
+
}
|
|
1877
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1878
|
+
finally {
|
|
1879
|
+
try {
|
|
1880
|
+
if (promptbooks_1_1 && !promptbooks_1_1.done && (_a = promptbooks_1.return)) _a.call(promptbooks_1);
|
|
1881
|
+
}
|
|
1882
|
+
finally { if (e_1) throw e_1.error; }
|
|
1883
|
+
}
|
|
1884
|
+
}
|
|
1885
|
+
/**
|
|
1886
|
+
* Gets all promptbooks in the library
|
|
1887
|
+
*/
|
|
1888
|
+
SimplePromptbookLibrary.prototype.listPromptbooks = function () {
|
|
1889
|
+
return Array.from(this.library.keys());
|
|
1890
|
+
};
|
|
1891
|
+
/**
|
|
1892
|
+
* Gets promptbook by its URL
|
|
1893
|
+
*
|
|
1894
|
+
* Note: This is not a direct fetching from the URL, but a lookup in the library
|
|
1895
|
+
*/
|
|
1896
|
+
SimplePromptbookLibrary.prototype.getPromptbookByUrl = function (url) {
|
|
1897
|
+
var _this = this;
|
|
1898
|
+
var promptbook = this.library.get(url);
|
|
1899
|
+
if (!promptbook) {
|
|
1900
|
+
if (this.listPromptbooks().length === 0) {
|
|
1901
|
+
throw new PromptbookNotFoundError(spaceTrim("\n Promptbook with url \"".concat(url, "\" not found\n\n No promptbooks available\n ")));
|
|
1902
|
+
}
|
|
1903
|
+
throw new PromptbookNotFoundError(spaceTrim(function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPromptbooks()
|
|
1904
|
+
.map(function (promptbookUrl) { return "- ".concat(promptbookUrl); })
|
|
1905
|
+
.join('\n')), "\n\n "); }));
|
|
1906
|
+
}
|
|
1907
|
+
return promptbook;
|
|
1908
|
+
};
|
|
1909
|
+
/**
|
|
1910
|
+
* Checks whether given prompt was defined in any promptbook in the library
|
|
1911
|
+
*/
|
|
1912
|
+
SimplePromptbookLibrary.prototype.isResponsibleForPrompt = function (prompt) {
|
|
1913
|
+
return true;
|
|
1914
|
+
};
|
|
1915
|
+
return SimplePromptbookLibrary;
|
|
1916
|
+
}());
|
|
1917
|
+
|
|
1918
|
+
/**
|
|
1919
|
+
* Creates PromptbookLibrary from array of PromptbookJson or PromptbookString
|
|
1920
|
+
*
|
|
1921
|
+
* Note: You can combine `PromptbookString` (`.ptbk.md`) with `PromptbookJson` BUT it is not recommended
|
|
1922
|
+
* Note: During the construction syntax and logic of all sources are validated
|
|
1923
|
+
*
|
|
1924
|
+
* @param promptbookSources
|
|
1925
|
+
* @returns PromptbookLibrary
|
|
1926
|
+
*/
|
|
1927
|
+
function createPromptbookLibraryFromSources() {
|
|
1928
|
+
var promptbookSources = [];
|
|
1929
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1930
|
+
promptbookSources[_i] = arguments[_i];
|
|
1931
|
+
}
|
|
1932
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1933
|
+
var promptbooks, promptbookSources_1, promptbookSources_1_1, source, promptbook, e_1_1;
|
|
1934
|
+
var e_1, _a;
|
|
1935
|
+
return __generator(this, function (_b) {
|
|
1936
|
+
switch (_b.label) {
|
|
1937
|
+
case 0:
|
|
1938
|
+
promptbooks = new Array();
|
|
1939
|
+
_b.label = 1;
|
|
1940
|
+
case 1:
|
|
1941
|
+
_b.trys.push([1, 8, 9, 10]);
|
|
1942
|
+
promptbookSources_1 = __values(promptbookSources), promptbookSources_1_1 = promptbookSources_1.next();
|
|
1943
|
+
_b.label = 2;
|
|
1944
|
+
case 2:
|
|
1945
|
+
if (!!promptbookSources_1_1.done) return [3 /*break*/, 7];
|
|
1946
|
+
source = promptbookSources_1_1.value;
|
|
1947
|
+
promptbook = void 0;
|
|
1948
|
+
if (!(typeof source === 'string')) return [3 /*break*/, 4];
|
|
1949
|
+
return [4 /*yield*/, promptbookStringToJson(source)];
|
|
1950
|
+
case 3:
|
|
1951
|
+
// Note: When directly creating from string, no need to validate the source
|
|
1952
|
+
// The validation is performed always before execution
|
|
1953
|
+
promptbook = _b.sent();
|
|
1954
|
+
return [3 /*break*/, 5];
|
|
1955
|
+
case 4:
|
|
1956
|
+
promptbook = source;
|
|
1957
|
+
_b.label = 5;
|
|
1958
|
+
case 5:
|
|
1959
|
+
promptbooks.push(promptbook);
|
|
1960
|
+
_b.label = 6;
|
|
1961
|
+
case 6:
|
|
1962
|
+
promptbookSources_1_1 = promptbookSources_1.next();
|
|
1963
|
+
return [3 /*break*/, 2];
|
|
1964
|
+
case 7: return [3 /*break*/, 10];
|
|
1965
|
+
case 8:
|
|
1966
|
+
e_1_1 = _b.sent();
|
|
1967
|
+
e_1 = { error: e_1_1 };
|
|
1968
|
+
return [3 /*break*/, 10];
|
|
1969
|
+
case 9:
|
|
1970
|
+
try {
|
|
1971
|
+
if (promptbookSources_1_1 && !promptbookSources_1_1.done && (_a = promptbookSources_1.return)) _a.call(promptbookSources_1);
|
|
1972
|
+
}
|
|
1973
|
+
finally { if (e_1) throw e_1.error; }
|
|
1974
|
+
return [7 /*endfinally*/];
|
|
1975
|
+
case 10: return [2 /*return*/, new (SimplePromptbookLibrary.bind.apply(SimplePromptbookLibrary, __spreadArray([void 0], __read(promptbooks), false)))()];
|
|
1976
|
+
}
|
|
1977
|
+
});
|
|
1978
|
+
});
|
|
1979
|
+
}
|
|
1980
|
+
/**
|
|
1981
|
+
* TODO: !!!! [🧠] Library precompilation and do not mix markdown and json promptbooks
|
|
1982
|
+
*/
|
|
1983
|
+
|
|
1984
|
+
/* tslint:disable */
|
|
1985
|
+
function normalizeToKebabCase(sentence) {
|
|
1986
|
+
var e_1, _a;
|
|
1987
|
+
sentence = removeDiacritics(sentence);
|
|
1988
|
+
var charType;
|
|
1989
|
+
var lastCharType = 'OTHER';
|
|
1990
|
+
var normalizedName = '';
|
|
1991
|
+
try {
|
|
1992
|
+
for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
|
|
1993
|
+
var char = sentence_1_1.value;
|
|
1994
|
+
var normalizedChar = void 0;
|
|
1995
|
+
if (/^[a-z]$/.test(char)) {
|
|
1996
|
+
charType = 'LOWERCASE';
|
|
1997
|
+
normalizedChar = char;
|
|
1998
|
+
}
|
|
1999
|
+
else if (/^[A-Z]$/.test(char)) {
|
|
2000
|
+
charType = 'UPPERCASE';
|
|
2001
|
+
normalizedChar = char.toLowerCase();
|
|
2002
|
+
}
|
|
2003
|
+
else if (/^[0-9]$/.test(char)) {
|
|
2004
|
+
charType = 'NUMBER';
|
|
2005
|
+
normalizedChar = char;
|
|
2006
|
+
}
|
|
2007
|
+
else if (/^\/$/.test(char)) {
|
|
2008
|
+
charType = 'SLASH';
|
|
2009
|
+
normalizedChar = char;
|
|
2010
|
+
}
|
|
2011
|
+
else {
|
|
2012
|
+
charType = 'OTHER';
|
|
2013
|
+
normalizedChar = '-';
|
|
2014
|
+
}
|
|
2015
|
+
if (charType !== lastCharType &&
|
|
2016
|
+
!(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
|
|
2017
|
+
!(lastCharType === 'NUMBER') &&
|
|
2018
|
+
!(charType === 'NUMBER')) {
|
|
2019
|
+
normalizedName += '-';
|
|
2020
|
+
}
|
|
2021
|
+
normalizedName += normalizedChar;
|
|
2022
|
+
lastCharType = charType;
|
|
2023
|
+
}
|
|
2024
|
+
}
|
|
2025
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2026
|
+
finally {
|
|
2027
|
+
try {
|
|
2028
|
+
if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
|
|
2029
|
+
}
|
|
2030
|
+
finally { if (e_1) throw e_1.error; }
|
|
2031
|
+
}
|
|
2032
|
+
normalizedName = normalizedName.split(/-+/g).join('-');
|
|
2033
|
+
normalizedName = normalizedName.split(/-?\/-?/g).join('/');
|
|
2034
|
+
normalizedName = normalizedName.replace(/^-/, '');
|
|
2035
|
+
normalizedName = normalizedName.replace(/-$/, '');
|
|
2036
|
+
return normalizedName;
|
|
2037
|
+
}
|
|
2038
|
+
|
|
2039
|
+
function prepareKnowledgeFromMarkdown(options) {
|
|
2040
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
2041
|
+
var content, llmTools, library, promptbook, executor, result, outputParameters, knowledgeRaw, knowledgeTextPieces, knowledge;
|
|
2042
|
+
var _this = this;
|
|
2043
|
+
return __generator(this, function (_a) {
|
|
2044
|
+
switch (_a.label) {
|
|
2045
|
+
case 0:
|
|
2046
|
+
content = options.content, llmTools = options.llmTools;
|
|
2047
|
+
return [4 /*yield*/, createPromptbookLibraryFromSources(
|
|
2048
|
+
/* !!!! ...(promptbookLibrary as Array<PromptbookJson>)*/ {
|
|
2049
|
+
title: 'Prepare Knowledge from Markdown',
|
|
2050
|
+
promptbookUrl: 'https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md',
|
|
2051
|
+
promptbookVersion: '0.59.0-5',
|
|
2052
|
+
parameters: [
|
|
2053
|
+
{ name: 'content', description: 'Markdown document content', isInput: true, isOutput: false },
|
|
2054
|
+
{ name: 'knowledge', description: 'The knowledge JSON object', isInput: false, isOutput: true },
|
|
2055
|
+
],
|
|
2056
|
+
promptTemplates: [
|
|
2057
|
+
{
|
|
2058
|
+
name: 'knowledge',
|
|
2059
|
+
title: 'Knowledge',
|
|
2060
|
+
dependentParameterNames: ['content'],
|
|
2061
|
+
executionType: 'PROMPT_TEMPLATE',
|
|
2062
|
+
modelRequirements: { modelVariant: 'CHAT', modelName: 'claude-3-opus-20240229' },
|
|
2063
|
+
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}',
|
|
2064
|
+
resultingParameterName: 'knowledge',
|
|
2065
|
+
},
|
|
2066
|
+
],
|
|
2067
|
+
knowledge: [],
|
|
2068
|
+
})];
|
|
2069
|
+
case 1:
|
|
2070
|
+
library = _a.sent();
|
|
2071
|
+
promptbook = library.getPromptbookByUrl('https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md');
|
|
2072
|
+
executor = createPromptbookExecutor({
|
|
2073
|
+
promptbook: promptbook,
|
|
2074
|
+
tools: {
|
|
2075
|
+
llm: llmTools,
|
|
2076
|
+
script: [
|
|
2077
|
+
/* <- TODO: Allow to just not define script tools */
|
|
2078
|
+
],
|
|
2079
|
+
},
|
|
2080
|
+
});
|
|
2081
|
+
return [4 /*yield*/, executor({ content: content })];
|
|
2082
|
+
case 2:
|
|
2083
|
+
result = _a.sent();
|
|
2084
|
+
assertsExecutionSuccessful(result);
|
|
2085
|
+
outputParameters = result.outputParameters;
|
|
2086
|
+
knowledgeRaw = outputParameters.knowledge;
|
|
2087
|
+
knowledgeTextPieces = (knowledgeRaw || '').split('\n---\n');
|
|
2088
|
+
return [4 /*yield*/, Promise.all(knowledgeTextPieces.map(function (knowledgeTextPiece, i) { return __awaiter(_this, void 0, void 0, function () {
|
|
2089
|
+
var name, title, content, keywords, index, sources;
|
|
2090
|
+
return __generator(this, function (_a) {
|
|
2091
|
+
name = "piece-".concat(i);
|
|
2092
|
+
title = spaceTrim$1(knowledgeTextPiece.substring(0, 100));
|
|
2093
|
+
content = spaceTrim$1(knowledgeTextPiece);
|
|
2094
|
+
keywords = [];
|
|
2095
|
+
index = [];
|
|
2096
|
+
sources = [];
|
|
2097
|
+
try {
|
|
2098
|
+
// TODO: !!!! Summarize name and title from the content
|
|
2099
|
+
title = spaceTrim$1(knowledgeTextPiece.substring(0, 30));
|
|
2100
|
+
name = normalizeToKebabCase(title);
|
|
2101
|
+
// TODO: !!!! Extract keywords via prompt
|
|
2102
|
+
// TODO: !!!! Index through LLM model
|
|
2103
|
+
// TODO: [🖖] !!!! Make system for sources and identification of sources
|
|
2104
|
+
}
|
|
2105
|
+
catch (error) {
|
|
2106
|
+
console.error(error);
|
|
2107
|
+
}
|
|
2108
|
+
return [2 /*return*/, {
|
|
2109
|
+
name: name,
|
|
2110
|
+
title: title,
|
|
2111
|
+
content: content,
|
|
2112
|
+
keywords: keywords,
|
|
2113
|
+
index: index,
|
|
2114
|
+
sources: sources,
|
|
2115
|
+
}];
|
|
2116
|
+
});
|
|
2117
|
+
}); }))];
|
|
2118
|
+
case 3:
|
|
2119
|
+
knowledge = _a.sent();
|
|
2120
|
+
return [2 /*return*/, knowledge];
|
|
2121
|
+
}
|
|
2122
|
+
});
|
|
2123
|
+
});
|
|
2124
|
+
}
|
|
2125
|
+
|
|
2126
|
+
/**
|
|
2127
|
+
* Supported script languages
|
|
2128
|
+
*/
|
|
2129
|
+
var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
|
|
2130
|
+
|
|
2131
|
+
/**
|
|
2132
|
+
* Computes the deepness of the markdown structure.
|
|
2133
|
+
*
|
|
2134
|
+
* @private within the library
|
|
2135
|
+
*/
|
|
2136
|
+
function countMarkdownStructureDeepness(markdownStructure) {
|
|
2137
|
+
var e_1, _a;
|
|
2138
|
+
var maxDeepness = 0;
|
|
2139
|
+
try {
|
|
2140
|
+
for (var _b = __values(markdownStructure.sections), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
2141
|
+
var section = _c.value;
|
|
2142
|
+
maxDeepness = Math.max(maxDeepness, countMarkdownStructureDeepness(section));
|
|
781
2143
|
}
|
|
782
|
-
return -number;
|
|
783
|
-
}
|
|
784
|
-
value = value.replace(/,/g, '.');
|
|
785
|
-
value = value.toUpperCase();
|
|
786
|
-
if (value === '') {
|
|
787
|
-
return 0;
|
|
788
|
-
}
|
|
789
|
-
if (value === '♾' || value.startsWith('INF')) {
|
|
790
|
-
return Infinity;
|
|
791
2144
|
}
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
if (denominator === 0) {
|
|
797
|
-
throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\" because denominator is zero"));
|
|
2145
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2146
|
+
finally {
|
|
2147
|
+
try {
|
|
2148
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
798
2149
|
}
|
|
799
|
-
|
|
800
|
-
}
|
|
801
|
-
if (/^(NAN|NULL|NONE|UNDEFINED|ZERO|NO.*)$/.test(value)) {
|
|
802
|
-
return 0;
|
|
2150
|
+
finally { if (e_1) throw e_1.error; }
|
|
803
2151
|
}
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
2152
|
+
return maxDeepness + 1;
|
|
2153
|
+
}
|
|
2154
|
+
|
|
2155
|
+
/**
|
|
2156
|
+
* Parse a markdown string into a MarkdownStructure object.
|
|
2157
|
+
*
|
|
2158
|
+
* Note: This function does work with code blocks
|
|
2159
|
+
* Note: This function does not work with markdown comments
|
|
2160
|
+
*
|
|
2161
|
+
* @param markdown The markdown string to parse.
|
|
2162
|
+
* @returns The MarkdownStructure object.
|
|
2163
|
+
*
|
|
2164
|
+
* @private within the library
|
|
2165
|
+
*/
|
|
2166
|
+
function markdownToMarkdownStructure(markdown) {
|
|
2167
|
+
var e_1, _a;
|
|
2168
|
+
var lines = markdown.split('\n');
|
|
2169
|
+
var root = { level: 0, title: '', contentLines: [], sections: [], parent: null };
|
|
2170
|
+
var current = root;
|
|
2171
|
+
var isInsideCodeBlock = false;
|
|
2172
|
+
try {
|
|
2173
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
2174
|
+
var line = lines_1_1.value;
|
|
2175
|
+
var headingMatch = line.match(/^(?<mark>#{1,6})\s(?<title>.*)/);
|
|
2176
|
+
if (isInsideCodeBlock || !headingMatch) {
|
|
2177
|
+
if (line.startsWith('```')) {
|
|
2178
|
+
isInsideCodeBlock = !isInsideCodeBlock;
|
|
2179
|
+
}
|
|
2180
|
+
current.contentLines.push(line);
|
|
2181
|
+
}
|
|
2182
|
+
else {
|
|
2183
|
+
var level = headingMatch.groups.mark.length;
|
|
2184
|
+
var title = headingMatch.groups.title.trim();
|
|
2185
|
+
var parent_1 = void 0;
|
|
2186
|
+
if (level > current.level) {
|
|
2187
|
+
// Note: Going deeper (next section is child of current)
|
|
2188
|
+
parent_1 = current;
|
|
2189
|
+
}
|
|
2190
|
+
else {
|
|
2191
|
+
// Note: Going up or staying at the same level (next section is sibling or parent or grandparent,... of current)
|
|
2192
|
+
parent_1 = current;
|
|
2193
|
+
var loopLimit = LOOP_LIMIT;
|
|
2194
|
+
while (parent_1.level !== level - 1) {
|
|
2195
|
+
if (loopLimit-- < 0) {
|
|
2196
|
+
throw new UnexpectedError('Loop limit reached during parsing of markdown structure in `markdownToMarkdownStructure`');
|
|
2197
|
+
}
|
|
2198
|
+
if (parent_1.parent === null /* <- Note: We are in root */) {
|
|
2199
|
+
// [🌻]
|
|
2200
|
+
throw new Error(spaceTrim("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
|
|
2201
|
+
}
|
|
2202
|
+
parent_1 = parent_1.parent;
|
|
2203
|
+
}
|
|
2204
|
+
}
|
|
2205
|
+
var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
|
|
2206
|
+
parent_1.sections.push(section);
|
|
2207
|
+
current = section;
|
|
2208
|
+
}
|
|
2209
|
+
}
|
|
807
2210
|
}
|
|
808
|
-
|
|
809
|
-
|
|
2211
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2212
|
+
finally {
|
|
2213
|
+
try {
|
|
2214
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
2215
|
+
}
|
|
2216
|
+
finally { if (e_1) throw e_1.error; }
|
|
810
2217
|
}
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
2218
|
+
if (root.sections.length === 1) {
|
|
2219
|
+
var markdownStructure = parsingMarkdownStructureToMarkdownStructure(root.sections[0]);
|
|
2220
|
+
return markdownStructure;
|
|
814
2221
|
}
|
|
815
|
-
|
|
2222
|
+
// [🌻]
|
|
2223
|
+
throw new Error('The markdown file must have exactly one top-level section.');
|
|
2224
|
+
// return root;
|
|
816
2225
|
}
|
|
817
2226
|
/**
|
|
818
|
-
*
|
|
2227
|
+
* @private
|
|
819
2228
|
*/
|
|
2229
|
+
function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
|
|
2230
|
+
var level = parsingMarkdownStructure.level, title = parsingMarkdownStructure.title, contentLines = parsingMarkdownStructure.contentLines, sections = parsingMarkdownStructure.sections;
|
|
2231
|
+
return {
|
|
2232
|
+
level: level,
|
|
2233
|
+
title: title,
|
|
2234
|
+
content: spaceTrim(contentLines.join('\n')),
|
|
2235
|
+
sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
|
|
2236
|
+
};
|
|
2237
|
+
}
|
|
820
2238
|
|
|
821
2239
|
/**
|
|
822
|
-
*
|
|
2240
|
+
* Utility function to extract all list items from markdown
|
|
823
2241
|
*
|
|
824
|
-
*
|
|
825
|
-
*
|
|
2242
|
+
* Note: It works with both ul and ol
|
|
2243
|
+
* Note: It omits list items in code blocks
|
|
2244
|
+
* Note: It flattens nested lists
|
|
2245
|
+
* Note: It can not work with html syntax and comments
|
|
826
2246
|
*
|
|
827
|
-
* @
|
|
2247
|
+
* @param markdown any valid markdown
|
|
2248
|
+
* @returns
|
|
828
2249
|
*/
|
|
829
|
-
function
|
|
2250
|
+
function extractAllListItemsFromMarkdown(markdown) {
|
|
830
2251
|
var e_1, _a;
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
var listItemParts = listItem
|
|
846
|
-
.split(' ')
|
|
847
|
-
.map(function (part) { return part.trim(); })
|
|
848
|
-
.filter(function (item) { return item !== ''; })
|
|
849
|
-
.filter(function (item) { return !/^PTBK$/i.test(item); })
|
|
850
|
-
.filter(function (item) { return !/^PROMPTBOOK$/i.test(item); })
|
|
851
|
-
.map(removeMarkdownFormatting);
|
|
852
|
-
if (type.startsWith('URL') ||
|
|
853
|
-
type.startsWith('PTBK_URL') ||
|
|
854
|
-
type.startsWith('PTBKURL') ||
|
|
855
|
-
type.startsWith('PROMPTBOOK_URL') ||
|
|
856
|
-
type.startsWith('PROMPTBOOKURL') ||
|
|
857
|
-
type.startsWith('HTTPS')) {
|
|
858
|
-
if (!(listItemParts.length === 2 || (listItemParts.length === 1 && type.startsWith('HTTPS')))) {
|
|
859
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n ")));
|
|
860
|
-
}
|
|
861
|
-
var promptbookUrlString = listItemParts.pop();
|
|
862
|
-
var promptbookUrl = new URL(promptbookUrlString);
|
|
863
|
-
if (promptbookUrl.protocol !== 'https:') {
|
|
864
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n Protocol must be HTTPS\n ")));
|
|
865
|
-
}
|
|
866
|
-
if (promptbookUrl.hash !== '') {
|
|
867
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n URL must not contain hash\n Hash is used for identification of the prompt template in the pipeline\n ")));
|
|
868
|
-
}
|
|
869
|
-
return {
|
|
870
|
-
type: 'PROMPTBOOK_URL',
|
|
871
|
-
promptbookUrl: promptbookUrl,
|
|
872
|
-
};
|
|
873
|
-
}
|
|
874
|
-
else if (type.startsWith('PROMPTBOOK_VERSION') || type.startsWith('PTBK_VERSION')) {
|
|
875
|
-
if (listItemParts.length !== 2) {
|
|
876
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_VERSION command:\n\n - ".concat(listItem, "\n ")));
|
|
2252
|
+
var lines = markdown.split('\n');
|
|
2253
|
+
var listItems = [];
|
|
2254
|
+
var isInCodeBlock = false;
|
|
2255
|
+
try {
|
|
2256
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
2257
|
+
var line = lines_1_1.value;
|
|
2258
|
+
var trimmedLine = line.trim();
|
|
2259
|
+
if (trimmedLine.startsWith('```')) {
|
|
2260
|
+
isInCodeBlock = !isInCodeBlock;
|
|
2261
|
+
}
|
|
2262
|
+
if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
|
|
2263
|
+
var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
|
|
2264
|
+
listItems.push(listItem);
|
|
2265
|
+
}
|
|
877
2266
|
}
|
|
878
|
-
var promptbookVersion = listItemParts.pop();
|
|
879
|
-
// TODO: Validate version
|
|
880
|
-
return {
|
|
881
|
-
type: 'PROMPTBOOK_VERSION',
|
|
882
|
-
promptbookVersion: promptbookVersion,
|
|
883
|
-
};
|
|
884
2267
|
}
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
var executionTypes = ExecutionTypes.filter(function (executionType) { return type.includes(executionType); });
|
|
890
|
-
if (executionTypes.length !== 1) {
|
|
891
|
-
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Unknown execution type in command:\n\n - ".concat(listItem, "\n\n Supported execution types are:\n ").concat(block(ExecutionTypes.join(', ')), "\n "); }));
|
|
2268
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2269
|
+
finally {
|
|
2270
|
+
try {
|
|
2271
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
892
2272
|
}
|
|
893
|
-
|
|
894
|
-
type: 'EXECUTE',
|
|
895
|
-
executionType: executionTypes[0],
|
|
896
|
-
};
|
|
2273
|
+
finally { if (e_1) throw e_1.error; }
|
|
897
2274
|
}
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
2275
|
+
return listItems;
|
|
2276
|
+
}
|
|
2277
|
+
|
|
2278
|
+
/**
|
|
2279
|
+
* Extracts all code blocks from markdown.
|
|
2280
|
+
*
|
|
2281
|
+
* Note: There are 3 simmilar function:
|
|
2282
|
+
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
2283
|
+
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
2284
|
+
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
2285
|
+
*
|
|
2286
|
+
* @param markdown any valid markdown
|
|
2287
|
+
* @returns code blocks with language and content
|
|
2288
|
+
*
|
|
2289
|
+
*/
|
|
2290
|
+
function extractAllBlocksFromMarkdown(markdown) {
|
|
2291
|
+
var e_1, _a;
|
|
2292
|
+
var codeBlocks = [];
|
|
2293
|
+
var lines = markdown.split('\n');
|
|
2294
|
+
var currentCodeBlock = null;
|
|
2295
|
+
try {
|
|
2296
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
2297
|
+
var line = lines_1_1.value;
|
|
2298
|
+
if (line.startsWith('```')) {
|
|
2299
|
+
var language = line.slice(3).trim() || null;
|
|
2300
|
+
if (currentCodeBlock === null) {
|
|
2301
|
+
currentCodeBlock = { language: language, content: '' };
|
|
2302
|
+
}
|
|
2303
|
+
else {
|
|
2304
|
+
if (language !== null) {
|
|
2305
|
+
// [🌻]
|
|
2306
|
+
throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
|
|
2307
|
+
}
|
|
2308
|
+
codeBlocks.push(currentCodeBlock);
|
|
2309
|
+
currentCodeBlock = null;
|
|
2310
|
+
}
|
|
914
2311
|
}
|
|
915
|
-
else {
|
|
916
|
-
|
|
2312
|
+
else if (currentCodeBlock !== null) {
|
|
2313
|
+
if (currentCodeBlock.content !== '') {
|
|
2314
|
+
currentCodeBlock.content += '\n';
|
|
2315
|
+
}
|
|
2316
|
+
currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
|
|
917
2317
|
}
|
|
918
2318
|
}
|
|
919
|
-
if (type.startsWith('MODEL_NAME')) {
|
|
920
|
-
return {
|
|
921
|
-
type: 'MODEL',
|
|
922
|
-
key: 'modelName',
|
|
923
|
-
value: listItemParts.pop(),
|
|
924
|
-
};
|
|
925
|
-
}
|
|
926
|
-
else {
|
|
927
|
-
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Unknown model key in command:\n\n - ".concat(listItem, "\n\n Supported model keys are:\n ").concat(block(['variant', 'name'].join(', ')), "\n\n Example:\n\n - MODEL VARIANT Chat\n - MODEL NAME gpt-4\n "); }));
|
|
928
|
-
}
|
|
929
2319
|
}
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
listItem.startsWith('> {') /* <- Note: This is a bit hack to parse return parameters defined at the end of each section */) {
|
|
935
|
-
var parametersMatch = listItem.match(/\{(?<parameterName>[a-z0-9_]+)\}[^\S\r\n]*(?<parameterDescription>.*)$/im);
|
|
936
|
-
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
937
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
|
|
938
|
-
}
|
|
939
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
940
|
-
var _b = parametersMatch.groups, parameterName = _b.parameterName, parameterDescription = _b.parameterDescription;
|
|
941
|
-
if (parameterDescription && parameterDescription.match(/\{(?<parameterName>[a-z0-9_]+)\}/im)) {
|
|
942
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Parameter {".concat(parameterName, "} can not contain another parameter in description:\n\n - ").concat(listItem, "\n ")));
|
|
943
|
-
}
|
|
944
|
-
var isInput = type.startsWith('INPUT');
|
|
945
|
-
var isOutput = type.startsWith('OUTPUT');
|
|
946
|
-
if (listItem.startsWith('> {')) {
|
|
947
|
-
isInput = false;
|
|
948
|
-
isOutput = false;
|
|
2320
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2321
|
+
finally {
|
|
2322
|
+
try {
|
|
2323
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
949
2324
|
}
|
|
950
|
-
|
|
951
|
-
type: 'PARAMETER',
|
|
952
|
-
parameterName: parameterName,
|
|
953
|
-
parameterDescription: parameterDescription.trim() || null,
|
|
954
|
-
isInput: isInput,
|
|
955
|
-
isOutput: isOutput,
|
|
956
|
-
};
|
|
2325
|
+
finally { if (e_1) throw e_1.error; }
|
|
957
2326
|
}
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
}
|
|
962
|
-
var parametersMatch = (listItemParts.pop() || '').match(/^\{(?<parameterName>[a-z0-9_]+)\}$/im);
|
|
963
|
-
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
964
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
|
|
965
|
-
}
|
|
966
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
967
|
-
var parameterName = parametersMatch.groups.parameterName;
|
|
968
|
-
return {
|
|
969
|
-
type: 'JOKER',
|
|
970
|
-
parameterName: parameterName,
|
|
971
|
-
};
|
|
2327
|
+
if (currentCodeBlock !== null) {
|
|
2328
|
+
// [🌻]
|
|
2329
|
+
throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
|
|
972
2330
|
}
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
2331
|
+
return codeBlocks;
|
|
2332
|
+
}
|
|
2333
|
+
|
|
2334
|
+
/**
|
|
2335
|
+
* Extracts exactly ONE code block from markdown.
|
|
2336
|
+
*
|
|
2337
|
+
* Note: If there are multiple or no code blocks the function throws an error
|
|
2338
|
+
*
|
|
2339
|
+
* Note: There are 3 simmilar function:
|
|
2340
|
+
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
2341
|
+
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
2342
|
+
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
2343
|
+
*
|
|
2344
|
+
* @param markdown any valid markdown
|
|
2345
|
+
* @returns code block with language and content
|
|
2346
|
+
*/
|
|
2347
|
+
function extractOneBlockFromMarkdown(markdown) {
|
|
2348
|
+
var codeBlocks = extractAllBlocksFromMarkdown(markdown);
|
|
2349
|
+
if (codeBlocks.length !== 1) {
|
|
2350
|
+
// TODO: Report more specific place where the error happened
|
|
2351
|
+
throw new Error(/* <- [🌻] */ 'There should be exactly one code block in the markdown');
|
|
982
2352
|
}
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
2353
|
+
return codeBlocks[0];
|
|
2354
|
+
}
|
|
2355
|
+
/***
|
|
2356
|
+
* TODO: [🍓][🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
|
|
2357
|
+
*/
|
|
2358
|
+
|
|
2359
|
+
/**
|
|
2360
|
+
* Creates a new set with all elements that are present in either set
|
|
2361
|
+
*/
|
|
2362
|
+
function union() {
|
|
2363
|
+
var e_1, _a, e_2, _b;
|
|
2364
|
+
var sets = [];
|
|
2365
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2366
|
+
sets[_i] = arguments[_i];
|
|
989
2367
|
}
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
var
|
|
994
|
-
var signRaw = listItemParts.shift();
|
|
995
|
-
if (/^exact/i.test(signRaw)) {
|
|
996
|
-
sign = 'EXACTLY';
|
|
997
|
-
}
|
|
998
|
-
else if (/^min/i.test(signRaw)) {
|
|
999
|
-
sign = 'MINIMUM';
|
|
1000
|
-
}
|
|
1001
|
-
else if (/^max/i.test(signRaw)) {
|
|
1002
|
-
sign = 'MAXIMUM';
|
|
1003
|
-
}
|
|
1004
|
-
else {
|
|
1005
|
-
throw new PromptbookSyntaxError("Invalid sign \"".concat(signRaw, "\", expected EXACTLY, MIN or MAX"));
|
|
1006
|
-
}
|
|
1007
|
-
var amountRaw = listItemParts.shift();
|
|
1008
|
-
var amount = parseNumber(amountRaw);
|
|
1009
|
-
if (amount < 0) {
|
|
1010
|
-
throw new PromptbookSyntaxError('Amount must be positive number or zero');
|
|
1011
|
-
}
|
|
1012
|
-
if (amount !== Math.floor(amount)) {
|
|
1013
|
-
throw new PromptbookSyntaxError('Amount must be whole number');
|
|
1014
|
-
}
|
|
1015
|
-
var unitRaw = listItemParts.shift();
|
|
1016
|
-
var unit = undefined;
|
|
2368
|
+
var union = new Set();
|
|
2369
|
+
try {
|
|
2370
|
+
for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
|
|
2371
|
+
var set = sets_1_1.value;
|
|
1017
2372
|
try {
|
|
1018
|
-
for (var
|
|
1019
|
-
var
|
|
1020
|
-
|
|
1021
|
-
existingUnitText = existingUnitText.substring(0, existingUnitText.length - 1);
|
|
1022
|
-
if (existingUnitText === 'CHARACTER') {
|
|
1023
|
-
existingUnitText = 'CHAR';
|
|
1024
|
-
}
|
|
1025
|
-
if (new RegExp("^".concat(existingUnitText.toLowerCase())).test(unitRaw.toLowerCase()) ||
|
|
1026
|
-
new RegExp("^".concat(unitRaw.toLowerCase())).test(existingUnitText.toLowerCase())) {
|
|
1027
|
-
if (unit !== undefined) {
|
|
1028
|
-
throw new PromptbookSyntaxError("Ambiguous unit \"".concat(unitRaw, "\""));
|
|
1029
|
-
}
|
|
1030
|
-
unit = existingUnit;
|
|
1031
|
-
}
|
|
2373
|
+
for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
2374
|
+
var item = _d.value;
|
|
2375
|
+
union.add(item);
|
|
1032
2376
|
}
|
|
1033
2377
|
}
|
|
1034
|
-
catch (
|
|
2378
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
1035
2379
|
finally {
|
|
1036
2380
|
try {
|
|
1037
|
-
if (
|
|
2381
|
+
if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
|
|
2382
|
+
}
|
|
2383
|
+
finally { if (e_2) throw e_2.error; }
|
|
2384
|
+
}
|
|
2385
|
+
}
|
|
2386
|
+
}
|
|
2387
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2388
|
+
finally {
|
|
2389
|
+
try {
|
|
2390
|
+
if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
|
|
2391
|
+
}
|
|
2392
|
+
finally { if (e_1) throw e_1.error; }
|
|
2393
|
+
}
|
|
2394
|
+
return union;
|
|
2395
|
+
}
|
|
2396
|
+
|
|
2397
|
+
/**
|
|
2398
|
+
* Parses the template and returns the list of all parameter names
|
|
2399
|
+
*
|
|
2400
|
+
* @param template the template with parameters in {curly} braces
|
|
2401
|
+
* @returns the list of parameter names
|
|
2402
|
+
*/
|
|
2403
|
+
function extractParameters(template) {
|
|
2404
|
+
var e_1, _a;
|
|
2405
|
+
var matches = template.matchAll(/{\w+}/g);
|
|
2406
|
+
var parameterNames = new Set();
|
|
2407
|
+
try {
|
|
2408
|
+
for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
|
|
2409
|
+
var match = matches_1_1.value;
|
|
2410
|
+
var parameterName = match[0].slice(1, -1);
|
|
2411
|
+
parameterNames.add(parameterName);
|
|
2412
|
+
}
|
|
2413
|
+
}
|
|
2414
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2415
|
+
finally {
|
|
2416
|
+
try {
|
|
2417
|
+
if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
|
|
2418
|
+
}
|
|
2419
|
+
finally { if (e_1) throw e_1.error; }
|
|
2420
|
+
}
|
|
2421
|
+
return parameterNames;
|
|
2422
|
+
}
|
|
2423
|
+
|
|
2424
|
+
/**
|
|
2425
|
+
* Parses the given script and returns the list of all used variables that are not defined in the script
|
|
2426
|
+
*
|
|
2427
|
+
* @param script from which to extract the variables
|
|
2428
|
+
* @returns the list of variable names
|
|
2429
|
+
* @throws {PromptbookSyntaxError} if the script is invalid
|
|
2430
|
+
*/
|
|
2431
|
+
function extractVariables(script) {
|
|
2432
|
+
var variables = new Set();
|
|
2433
|
+
script = "(()=>{".concat(script, "})()");
|
|
2434
|
+
try {
|
|
2435
|
+
for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
|
|
2436
|
+
try {
|
|
2437
|
+
eval(script);
|
|
2438
|
+
}
|
|
2439
|
+
catch (error) {
|
|
2440
|
+
if (!(error instanceof ReferenceError)) {
|
|
2441
|
+
throw error;
|
|
2442
|
+
}
|
|
2443
|
+
var undefinedName = error.message.split(' ')[0];
|
|
2444
|
+
/*
|
|
2445
|
+
Note: Remapping error
|
|
2446
|
+
From: [ReferenceError: thing is not defined],
|
|
2447
|
+
To: [Error: Parameter {thing} is not defined],
|
|
2448
|
+
*/
|
|
2449
|
+
if (!undefinedName) {
|
|
2450
|
+
throw error;
|
|
2451
|
+
}
|
|
2452
|
+
if (script.includes(undefinedName + '(')) {
|
|
2453
|
+
script = "const ".concat(undefinedName, " = ()=>'';") + script;
|
|
2454
|
+
}
|
|
2455
|
+
else {
|
|
2456
|
+
variables.add(undefinedName);
|
|
2457
|
+
script = "const ".concat(undefinedName, " = '';") + script;
|
|
1038
2458
|
}
|
|
1039
|
-
finally { if (e_1) throw e_1.error; }
|
|
1040
|
-
}
|
|
1041
|
-
if (unit === undefined) {
|
|
1042
|
-
throw new PromptbookSyntaxError("Invalid unit \"".concat(unitRaw, "\""));
|
|
1043
|
-
}
|
|
1044
|
-
return {
|
|
1045
|
-
type: 'EXPECT_AMOUNT',
|
|
1046
|
-
sign: sign,
|
|
1047
|
-
unit: unit,
|
|
1048
|
-
amount: amount,
|
|
1049
|
-
};
|
|
1050
|
-
}
|
|
1051
|
-
catch (error) {
|
|
1052
|
-
if (!(error instanceof Error)) {
|
|
1053
|
-
throw error;
|
|
1054
2459
|
}
|
|
1055
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid EXPECT command; ".concat(error.message, ":\n\n - ").concat(listItem, "\n ")));
|
|
1056
|
-
}
|
|
1057
|
-
/*
|
|
1058
|
-
} else if (type.startsWith('__________________')) {
|
|
1059
|
-
// <- [🥻] Insert here when making new command
|
|
1060
|
-
*/
|
|
1061
2460
|
}
|
|
1062
|
-
|
|
1063
|
-
|
|
2461
|
+
catch (error) {
|
|
2462
|
+
if (!(error instanceof Error)) {
|
|
2463
|
+
throw error;
|
|
2464
|
+
}
|
|
2465
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
|
|
1064
2466
|
}
|
|
2467
|
+
return variables;
|
|
1065
2468
|
}
|
|
2469
|
+
/**
|
|
2470
|
+
* TODO: [🔣] Support for multiple languages - python, java,...
|
|
2471
|
+
*/
|
|
1066
2472
|
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
{
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
}
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
}
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
}
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
}
|
|
1158
|
-
{
|
|
1159
|
-
{
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
},
|
|
1187
|
-
{
|
|
1188
|
-
base: 'c',
|
|
1189
|
-
letters: '\u0063\u24D2\uFF43\u0107\u0109\u010B\u010D\u00E7\u1E09\u0188\u023C\uA73F\u2184',
|
|
1190
|
-
},
|
|
1191
|
-
{
|
|
1192
|
-
base: 'd',
|
|
1193
|
-
letters: '\u0064\u24D3\uFF44\u1E0B\u010F\u1E0D\u1E11\u1E13\u1E0F\u0111\u018C\u0256\u0257\uA77A',
|
|
1194
|
-
},
|
|
1195
|
-
{ base: 'dz', letters: '\u01F3\u01C6' },
|
|
1196
|
-
{
|
|
1197
|
-
base: 'e',
|
|
1198
|
-
letters: '\u0065\u24D4\uFF45\u00E8\u00E9\u00EA\u1EC1\u1EBF\u1EC5\u1EC3\u1EBD\u0113\u1E15\u1E17\u0115\u0117\u00EB\u1EBB\u011B\u0205\u0207\u1EB9\u1EC7\u0229\u1E1D\u0119\u1E19\u1E1B\u0247\u025B\u01DD',
|
|
1199
|
-
},
|
|
1200
|
-
{ base: 'f', letters: '\u0066\u24D5\uFF46\u1E1F\u0192\uA77C' },
|
|
1201
|
-
{
|
|
1202
|
-
base: 'g',
|
|
1203
|
-
letters: '\u0067\u24D6\uFF47\u01F5\u011D\u1E21\u011F\u0121\u01E7\u0123\u01E5\u0260\uA7A1\u1D79\uA77F',
|
|
1204
|
-
},
|
|
1205
|
-
{
|
|
1206
|
-
base: 'h',
|
|
1207
|
-
letters: '\u0068\u24D7\uFF48\u0125\u1E23\u1E27\u021F\u1E25\u1E29\u1E2B\u1E96\u0127\u2C68\u2C76\u0265',
|
|
1208
|
-
},
|
|
1209
|
-
{ base: 'hv', letters: '\u0195' },
|
|
1210
|
-
{
|
|
1211
|
-
base: 'i',
|
|
1212
|
-
letters: '\u0069\u24D8\uFF49\u00EC\u00ED\u00EE\u0129\u012B\u012D\u00EF\u1E2F\u1EC9\u01D0\u0209\u020B\u1ECB\u012F\u1E2D\u0268\u0131',
|
|
1213
|
-
},
|
|
1214
|
-
{ base: 'j', letters: '\u006A\u24D9\uFF4A\u0135\u01F0\u0249' },
|
|
1215
|
-
{
|
|
1216
|
-
base: 'k',
|
|
1217
|
-
letters: '\u006B\u24DA\uFF4B\u1E31\u01E9\u1E33\u0137\u1E35\u0199\u2C6A\uA741\uA743\uA745\uA7A3',
|
|
1218
|
-
},
|
|
1219
|
-
{
|
|
1220
|
-
base: 'l',
|
|
1221
|
-
letters: '\u006C\u24DB\uFF4C\u0140\u013A\u013E\u1E37\u1E39\u013C\u1E3D\u1E3B\u017F\u0142\u019A\u026B\u2C61\uA749\uA781\uA747',
|
|
1222
|
-
},
|
|
1223
|
-
{ base: 'lj', letters: '\u01C9' },
|
|
1224
|
-
{ base: 'm', letters: '\u006D\u24DC\uFF4D\u1E3F\u1E41\u1E43\u0271\u026F' },
|
|
1225
|
-
{
|
|
1226
|
-
base: 'n',
|
|
1227
|
-
letters: '\u006E\u24DD\uFF4E\u01F9\u0144\u00F1\u1E45\u0148\u1E47\u0146\u1E4B\u1E49\u019E\u0272\u0149\uA791\uA7A5',
|
|
1228
|
-
},
|
|
1229
|
-
{ base: 'nj', letters: '\u01CC' },
|
|
1230
|
-
{
|
|
1231
|
-
base: 'o',
|
|
1232
|
-
letters: '\u006F\u24DE\uFF4F\u00F2\u00F3\u00F4\u1ED3\u1ED1\u1ED7\u1ED5\u00F5\u1E4D\u022D\u1E4F\u014D\u1E51\u1E53\u014F\u022F\u0231\u00F6\u022B\u1ECF\u0151\u01D2\u020D\u020F\u01A1\u1EDD\u1EDB\u1EE1\u1EDF\u1EE3\u1ECD\u1ED9\u01EB\u01ED\u00F8\u01FF\u0254\uA74B\uA74D\u0275',
|
|
1233
|
-
},
|
|
1234
|
-
{ base: 'oi', letters: '\u01A3' },
|
|
1235
|
-
{ base: 'ou', letters: '\u0223' },
|
|
1236
|
-
{ base: 'oo', letters: '\uA74F' },
|
|
1237
|
-
{
|
|
1238
|
-
base: 'p',
|
|
1239
|
-
letters: '\u0070\u24DF\uFF50\u1E55\u1E57\u01A5\u1D7D\uA751\uA753\uA755',
|
|
1240
|
-
},
|
|
1241
|
-
{ base: 'q', letters: '\u0071\u24E0\uFF51\u024B\uA757\uA759' },
|
|
1242
|
-
{
|
|
1243
|
-
base: 'r',
|
|
1244
|
-
letters: '\u0072\u24E1\uFF52\u0155\u1E59\u0159\u0211\u0213\u1E5B\u1E5D\u0157\u1E5F\u024D\u027D\uA75B\uA7A7\uA783',
|
|
1245
|
-
},
|
|
1246
|
-
{
|
|
1247
|
-
base: 's',
|
|
1248
|
-
letters: '\u0073\u24E2\uFF53\u00DF\u015B\u1E65\u015D\u1E61\u0161\u1E67\u1E63\u1E69\u0219\u015F\u023F\uA7A9\uA785\u1E9B',
|
|
1249
|
-
},
|
|
1250
|
-
{
|
|
1251
|
-
base: 't',
|
|
1252
|
-
letters: '\u0074\u24E3\uFF54\u1E6B\u1E97\u0165\u1E6D\u021B\u0163\u1E71\u1E6F\u0167\u01AD\u0288\u2C66\uA787',
|
|
1253
|
-
},
|
|
1254
|
-
{ base: 'tz', letters: '\uA729' },
|
|
1255
|
-
{
|
|
1256
|
-
base: 'u',
|
|
1257
|
-
letters: '\u0075\u24E4\uFF55\u00F9\u00FA\u00FB\u0169\u1E79\u016B\u1E7B\u016D\u00FC\u01DC\u01D8\u01D6\u01DA\u1EE7\u016F\u0171\u01D4\u0215\u0217\u01B0\u1EEB\u1EE9\u1EEF\u1EED\u1EF1\u1EE5\u1E73\u0173\u1E77\u1E75\u0289',
|
|
1258
|
-
},
|
|
1259
|
-
{ base: 'v', letters: '\u0076\u24E5\uFF56\u1E7D\u1E7F\u028B\uA75F\u028C' },
|
|
1260
|
-
{ base: 'vy', letters: '\uA761' },
|
|
1261
|
-
{
|
|
1262
|
-
base: 'w',
|
|
1263
|
-
letters: '\u0077\u24E6\uFF57\u1E81\u1E83\u0175\u1E87\u1E85\u1E98\u1E89\u2C73',
|
|
1264
|
-
},
|
|
1265
|
-
{ base: 'x', letters: '\u0078\u24E7\uFF58\u1E8B\u1E8D' },
|
|
1266
|
-
{
|
|
1267
|
-
base: 'y',
|
|
1268
|
-
letters: '\u0079\u24E8\uFF59\u1EF3\u00FD\u0177\u1EF9\u0233\u1E8F\u00FF\u1EF7\u1E99\u1EF5\u01B4\u024F\u1EFF',
|
|
1269
|
-
},
|
|
1270
|
-
{
|
|
1271
|
-
base: 'z',
|
|
1272
|
-
letters: '\u007A\u24E9\uFF5A\u017A\u1E91\u017C\u017E\u1E93\u1E95\u01B6\u0225\u0240\u2C6C\uA763',
|
|
1273
|
-
},
|
|
2473
|
+
/**
|
|
2474
|
+
* Parses the prompt template and returns the set of all used parameters
|
|
2475
|
+
*
|
|
2476
|
+
* @param promptTemplate the template with used parameters
|
|
2477
|
+
* @returns the set of parameter names
|
|
2478
|
+
* @throws {PromptbookSyntaxError} if the script is invalid
|
|
2479
|
+
*/
|
|
2480
|
+
function extractParametersFromPromptTemplate(promptTemplate) {
|
|
2481
|
+
var e_1, _a, e_2, _b;
|
|
2482
|
+
var parameterNames = new Set();
|
|
2483
|
+
try {
|
|
2484
|
+
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()) {
|
|
2485
|
+
var parameterName = _d.value;
|
|
2486
|
+
parameterNames.add(parameterName);
|
|
2487
|
+
}
|
|
2488
|
+
}
|
|
2489
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2490
|
+
finally {
|
|
2491
|
+
try {
|
|
2492
|
+
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
2493
|
+
}
|
|
2494
|
+
finally { if (e_1) throw e_1.error; }
|
|
2495
|
+
}
|
|
2496
|
+
if (promptTemplate.executionType === 'SCRIPT') {
|
|
2497
|
+
try {
|
|
2498
|
+
for (var _e = __values(extractVariables(promptTemplate.content)), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
2499
|
+
var parameterName = _f.value;
|
|
2500
|
+
parameterNames.add(parameterName);
|
|
2501
|
+
}
|
|
2502
|
+
}
|
|
2503
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
2504
|
+
finally {
|
|
2505
|
+
try {
|
|
2506
|
+
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
2507
|
+
}
|
|
2508
|
+
finally { if (e_2) throw e_2.error; }
|
|
2509
|
+
}
|
|
2510
|
+
}
|
|
2511
|
+
return parameterNames;
|
|
2512
|
+
}
|
|
2513
|
+
/**
|
|
2514
|
+
* TODO: [🔣] If script require contentLanguage
|
|
2515
|
+
*/
|
|
2516
|
+
|
|
2517
|
+
/* tslint:disable */
|
|
2518
|
+
/*
|
|
2519
|
+
TODO: Tests
|
|
2520
|
+
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: 'Moje tabule' })).toEqual('/VtG7sR9rRJqwNEdM2/Moje tabule');
|
|
2521
|
+
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: 'ěščřžžýáíúů' })).toEqual('/VtG7sR9rRJqwNEdM2/escrzyaieuu');
|
|
2522
|
+
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: ' ahoj ' })).toEqual('/VtG7sR9rRJqwNEdM2/ahoj');
|
|
2523
|
+
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: ' ahoj_ahojAhoj ahoj ' })).toEqual('/VtG7sR9rRJqwNEdM2/ahoj-ahoj-ahoj-ahoj');
|
|
2524
|
+
*/
|
|
2525
|
+
function normalizeTo_SCREAMING_CASE(sentence) {
|
|
2526
|
+
var e_1, _a;
|
|
2527
|
+
var charType;
|
|
2528
|
+
var lastCharType = 'OTHER';
|
|
2529
|
+
var normalizedName = '';
|
|
2530
|
+
try {
|
|
2531
|
+
for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
|
|
2532
|
+
var char = sentence_1_1.value;
|
|
2533
|
+
var normalizedChar = void 0;
|
|
2534
|
+
if (/^[a-z]$/.test(char)) {
|
|
2535
|
+
charType = 'LOWERCASE';
|
|
2536
|
+
normalizedChar = char.toUpperCase();
|
|
2537
|
+
}
|
|
2538
|
+
else if (/^[A-Z]$/.test(char)) {
|
|
2539
|
+
charType = 'UPPERCASE';
|
|
2540
|
+
normalizedChar = char;
|
|
2541
|
+
}
|
|
2542
|
+
else if (/^[0-9]$/.test(char)) {
|
|
2543
|
+
charType = 'NUMBER';
|
|
2544
|
+
normalizedChar = char;
|
|
2545
|
+
}
|
|
2546
|
+
else if (/^\/$/.test(char)) {
|
|
2547
|
+
charType = 'SLASH';
|
|
2548
|
+
normalizedChar = char;
|
|
2549
|
+
}
|
|
2550
|
+
else {
|
|
2551
|
+
charType = 'OTHER';
|
|
2552
|
+
normalizedChar = '_';
|
|
2553
|
+
}
|
|
2554
|
+
if (charType !== lastCharType &&
|
|
2555
|
+
!(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
|
|
2556
|
+
!(lastCharType === 'NUMBER') &&
|
|
2557
|
+
!(charType === 'NUMBER')) {
|
|
2558
|
+
normalizedName += '_';
|
|
2559
|
+
}
|
|
2560
|
+
normalizedName += normalizedChar;
|
|
2561
|
+
lastCharType = charType;
|
|
2562
|
+
}
|
|
2563
|
+
}
|
|
2564
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2565
|
+
finally {
|
|
2566
|
+
try {
|
|
2567
|
+
if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
|
|
2568
|
+
}
|
|
2569
|
+
finally { if (e_1) throw e_1.error; }
|
|
2570
|
+
}
|
|
2571
|
+
normalizedName = normalizedName.replace(/_+/g, '_');
|
|
2572
|
+
normalizedName = normalizedName.replace(/_?\/_?/g, '/');
|
|
2573
|
+
normalizedName = normalizedName.replace(/^_/, '');
|
|
2574
|
+
normalizedName = normalizedName.replace(/_$/, '');
|
|
2575
|
+
return normalizedName;
|
|
2576
|
+
}
|
|
2577
|
+
/**
|
|
2578
|
+
* TODO: [🌺] Use some intermediate util splitWords
|
|
2579
|
+
*/
|
|
2580
|
+
|
|
2581
|
+
/**
|
|
2582
|
+
* Execution type describes the way how the block is executed
|
|
2583
|
+
*
|
|
2584
|
+
* @see https://github.com/webgptorg/promptbook#execution-type
|
|
2585
|
+
*/
|
|
2586
|
+
var ExecutionTypes = [
|
|
2587
|
+
'PROMPT_TEMPLATE',
|
|
2588
|
+
'SIMPLE_TEMPLATE',
|
|
2589
|
+
'SCRIPT',
|
|
2590
|
+
'PROMPT_DIALOG',
|
|
2591
|
+
// <- [🥻] Insert here when making new command
|
|
1274
2592
|
];
|
|
2593
|
+
|
|
1275
2594
|
/**
|
|
1276
|
-
*
|
|
1277
|
-
|
|
2595
|
+
* Units of text measurement
|
|
2596
|
+
*/
|
|
2597
|
+
var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'LINES', 'PARAGRAPHS', 'PAGES'];
|
|
2598
|
+
/**
|
|
2599
|
+
* TODO: [💝] Unite object for expecting amount and format - remove expectFormat
|
|
2600
|
+
* TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
|
|
2601
|
+
* TODO: [👙][🧠] Just selecting gpt3 or gpt4 level of model
|
|
2602
|
+
*/
|
|
2603
|
+
|
|
2604
|
+
/**
|
|
2605
|
+
* Removes Markdown formatting tags from a string.
|
|
1278
2606
|
*
|
|
1279
|
-
*
|
|
1280
|
-
*
|
|
1281
|
-
* > "Ă" => "A"
|
|
1282
|
-
* > ...
|
|
2607
|
+
* @param {string} str - The string to remove Markdown tags from.
|
|
2608
|
+
* @returns {string} The input string with all Markdown tags removed.
|
|
1283
2609
|
*/
|
|
1284
|
-
|
|
1285
|
-
//
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
2610
|
+
function removeMarkdownFormatting(str) {
|
|
2611
|
+
// Remove bold formatting
|
|
2612
|
+
str = str.replace(/\*\*(.*?)\*\*/g, '$1');
|
|
2613
|
+
// Remove italic formatting
|
|
2614
|
+
str = str.replace(/\*(.*?)\*/g, '$1');
|
|
2615
|
+
// Remove code formatting
|
|
2616
|
+
str = str.replace(/`(.*?)`/g, '$1');
|
|
2617
|
+
return str;
|
|
2618
|
+
}
|
|
2619
|
+
|
|
2620
|
+
/**
|
|
2621
|
+
* Function parseNumber will parse number from string
|
|
2622
|
+
*
|
|
2623
|
+
* Unlike Number.parseInt, Number.parseFloat it will never ever result in NaN
|
|
2624
|
+
* Note: it also works only with decimal numbers
|
|
2625
|
+
*
|
|
2626
|
+
* @returns parsed number
|
|
2627
|
+
* @throws {PromptbookSyntaxError} if the value is not a number
|
|
2628
|
+
*
|
|
2629
|
+
* @private within the parseCommand
|
|
2630
|
+
*/
|
|
2631
|
+
function parseNumber(value) {
|
|
2632
|
+
var originalValue = value;
|
|
2633
|
+
if (typeof value === 'number') {
|
|
2634
|
+
value = value.toString(); // <- TODO: Maybe more efficient way to do this
|
|
2635
|
+
}
|
|
2636
|
+
if (typeof value !== 'string') {
|
|
2637
|
+
return 0;
|
|
2638
|
+
}
|
|
2639
|
+
value = value.trim();
|
|
2640
|
+
if (value.startsWith('+')) {
|
|
2641
|
+
return parseNumber(value.substring(1));
|
|
2642
|
+
}
|
|
2643
|
+
if (value.startsWith('-')) {
|
|
2644
|
+
var number = parseNumber(value.substring(1));
|
|
2645
|
+
if (number === 0) {
|
|
2646
|
+
return 0; // <- Note: To prevent -0
|
|
2647
|
+
}
|
|
2648
|
+
return -number;
|
|
2649
|
+
}
|
|
2650
|
+
value = value.replace(/,/g, '.');
|
|
2651
|
+
value = value.toUpperCase();
|
|
2652
|
+
if (value === '') {
|
|
2653
|
+
return 0;
|
|
2654
|
+
}
|
|
2655
|
+
if (value === '♾' || value.startsWith('INF')) {
|
|
2656
|
+
return Infinity;
|
|
2657
|
+
}
|
|
2658
|
+
if (value.includes('/')) {
|
|
2659
|
+
var _a = __read(value.split('/'), 2), numerator_ = _a[0], denominator_ = _a[1];
|
|
2660
|
+
var numerator = parseNumber(numerator_);
|
|
2661
|
+
var denominator = parseNumber(denominator_);
|
|
2662
|
+
if (denominator === 0) {
|
|
2663
|
+
throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\" because denominator is zero"));
|
|
2664
|
+
}
|
|
2665
|
+
return numerator / denominator;
|
|
2666
|
+
}
|
|
2667
|
+
if (/^(NAN|NULL|NONE|UNDEFINED|ZERO|NO.*)$/.test(value)) {
|
|
2668
|
+
return 0;
|
|
2669
|
+
}
|
|
2670
|
+
if (value.includes('E')) {
|
|
2671
|
+
var _b = __read(value.split('E'), 2), significand = _b[0], exponent = _b[1];
|
|
2672
|
+
return parseNumber(significand) * Math.pow(10, parseNumber(exponent));
|
|
2673
|
+
}
|
|
2674
|
+
if (!/^[0-9.]+$/.test(value) || value.split('.').length > 2) {
|
|
2675
|
+
throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\""));
|
|
2676
|
+
}
|
|
2677
|
+
var num = parseFloat(value);
|
|
2678
|
+
if (isNaN(num)) {
|
|
2679
|
+
throw new PromptbookSyntaxError("Unexpected NaN when parsing number from \"".concat(originalValue, "\""));
|
|
2680
|
+
}
|
|
2681
|
+
return num;
|
|
2682
|
+
}
|
|
2683
|
+
/**
|
|
2684
|
+
* TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
|
|
2685
|
+
*/
|
|
2686
|
+
|
|
2687
|
+
/**
|
|
2688
|
+
* Parses one line of ul/ol to command
|
|
2689
|
+
*
|
|
2690
|
+
* @returns parsed command object
|
|
2691
|
+
* @throws {PromptbookSyntaxError} if the command is invalid
|
|
2692
|
+
*
|
|
2693
|
+
* @private within the promptbookStringToJson
|
|
2694
|
+
*/
|
|
2695
|
+
function parseCommand(listItem) {
|
|
2696
|
+
var e_1, _a;
|
|
2697
|
+
if (listItem.includes('\n') || listItem.includes('\r')) {
|
|
2698
|
+
throw new PromptbookSyntaxError('Command can not contain new line characters:');
|
|
2699
|
+
}
|
|
2700
|
+
var type = listItem.trim();
|
|
2701
|
+
type = type.split('`').join('');
|
|
2702
|
+
type = type.split('"').join('');
|
|
2703
|
+
type = type.split("'").join('');
|
|
2704
|
+
type = type.split('~').join('');
|
|
2705
|
+
type = type.split('[').join('');
|
|
2706
|
+
type = type.split(']').join('');
|
|
2707
|
+
type = type.split('(').join('');
|
|
2708
|
+
type = type.split(')').join('');
|
|
2709
|
+
type = normalizeTo_SCREAMING_CASE(type);
|
|
2710
|
+
type = type.split('DIALOGUE').join('DIALOG');
|
|
2711
|
+
var listItemParts = listItem
|
|
2712
|
+
.split(' ')
|
|
2713
|
+
.map(function (part) { return part.trim(); })
|
|
2714
|
+
.filter(function (item) { return item !== ''; })
|
|
2715
|
+
.filter(function (item) { return !/^PTBK$/i.test(item); })
|
|
2716
|
+
.filter(function (item) { return !/^PROMPTBOOK$/i.test(item); })
|
|
2717
|
+
.map(removeMarkdownFormatting);
|
|
2718
|
+
if (type.startsWith('URL') ||
|
|
2719
|
+
type.startsWith('PTBK_URL') ||
|
|
2720
|
+
type.startsWith('PTBKURL') ||
|
|
2721
|
+
type.startsWith('PROMPTBOOK_URL') ||
|
|
2722
|
+
type.startsWith('PROMPTBOOKURL') ||
|
|
2723
|
+
type.startsWith('HTTPS')) {
|
|
2724
|
+
if (!(listItemParts.length === 2 || (listItemParts.length === 1 && type.startsWith('HTTPS')))) {
|
|
2725
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n ")));
|
|
2726
|
+
}
|
|
2727
|
+
var promptbookUrlString = listItemParts.pop();
|
|
2728
|
+
var promptbookUrl = new URL(promptbookUrlString);
|
|
2729
|
+
if (promptbookUrl.protocol !== 'https:') {
|
|
2730
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n Protocol must be HTTPS\n ")));
|
|
2731
|
+
}
|
|
2732
|
+
if (promptbookUrl.hash !== '') {
|
|
2733
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n URL must not contain hash\n Hash is used for identification of the prompt template in the pipeline\n ")));
|
|
2734
|
+
}
|
|
2735
|
+
return {
|
|
2736
|
+
type: 'PROMPTBOOK_URL',
|
|
2737
|
+
promptbookUrl: promptbookUrl,
|
|
2738
|
+
};
|
|
1291
2739
|
}
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
var normalizedName = '';
|
|
1326
|
-
try {
|
|
1327
|
-
for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
|
|
1328
|
-
var char = sentence_1_1.value;
|
|
1329
|
-
var normalizedChar = void 0;
|
|
1330
|
-
if (/^[a-z]$/.test(char)) {
|
|
1331
|
-
charType = 'LOWERCASE';
|
|
1332
|
-
normalizedChar = char;
|
|
2740
|
+
else if (type.startsWith('PROMPTBOOK_VERSION') || type.startsWith('PTBK_VERSION')) {
|
|
2741
|
+
if (listItemParts.length !== 2) {
|
|
2742
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_VERSION command:\n\n - ".concat(listItem, "\n ")));
|
|
2743
|
+
}
|
|
2744
|
+
var promptbookVersion = listItemParts.pop();
|
|
2745
|
+
// TODO: Validate version
|
|
2746
|
+
return {
|
|
2747
|
+
type: 'PROMPTBOOK_VERSION',
|
|
2748
|
+
promptbookVersion: promptbookVersion,
|
|
2749
|
+
};
|
|
2750
|
+
}
|
|
2751
|
+
else if (type.startsWith('EXECUTE') ||
|
|
2752
|
+
type.startsWith('EXEC') ||
|
|
2753
|
+
type.startsWith('PROMPT_DIALOG') ||
|
|
2754
|
+
type.startsWith('SIMPLE_TEMPLATE')) {
|
|
2755
|
+
var executionTypes = ExecutionTypes.filter(function (executionType) { return type.includes(executionType); });
|
|
2756
|
+
if (executionTypes.length !== 1) {
|
|
2757
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Unknown execution type in command:\n\n - ".concat(listItem, "\n\n Supported execution types are:\n ").concat(block(ExecutionTypes.join(', ')), "\n "); }));
|
|
2758
|
+
}
|
|
2759
|
+
return {
|
|
2760
|
+
type: 'EXECUTE',
|
|
2761
|
+
executionType: executionTypes[0],
|
|
2762
|
+
};
|
|
2763
|
+
}
|
|
2764
|
+
else if (type.startsWith('MODEL')) {
|
|
2765
|
+
// TODO: Make this more elegant and dynamically
|
|
2766
|
+
if (type.startsWith('MODEL_VARIANT')) {
|
|
2767
|
+
if (type === 'MODEL_VARIANT_CHAT') {
|
|
2768
|
+
return {
|
|
2769
|
+
type: 'MODEL',
|
|
2770
|
+
key: 'modelVariant',
|
|
2771
|
+
value: 'CHAT',
|
|
2772
|
+
};
|
|
1333
2773
|
}
|
|
1334
|
-
else if (
|
|
1335
|
-
|
|
1336
|
-
|
|
2774
|
+
else if (type === 'MODEL_VARIANT_COMPLETION') {
|
|
2775
|
+
return {
|
|
2776
|
+
type: 'MODEL',
|
|
2777
|
+
key: 'modelVariant',
|
|
2778
|
+
value: 'COMPLETION',
|
|
2779
|
+
};
|
|
1337
2780
|
}
|
|
1338
|
-
else
|
|
1339
|
-
|
|
1340
|
-
normalizedChar = char;
|
|
2781
|
+
else {
|
|
2782
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Unknown model variant in command:\n\n - ".concat(listItem, "\n\n Supported variants are:\n ").concat(block(['CHAT', 'COMPLETION'].join(', ')), "\n "); }));
|
|
1341
2783
|
}
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
2784
|
+
}
|
|
2785
|
+
if (type.startsWith('MODEL_NAME')) {
|
|
2786
|
+
return {
|
|
2787
|
+
type: 'MODEL',
|
|
2788
|
+
key: 'modelName',
|
|
2789
|
+
value: listItemParts.pop(),
|
|
2790
|
+
};
|
|
2791
|
+
}
|
|
2792
|
+
else {
|
|
2793
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Unknown model key in command:\n\n - ".concat(listItem, "\n\n Supported model keys are:\n ").concat(block(['variant', 'name'].join(', ')), "\n\n Example:\n\n - MODEL VARIANT Chat\n - MODEL NAME gpt-4\n "); }));
|
|
2794
|
+
}
|
|
2795
|
+
}
|
|
2796
|
+
else if (type.startsWith('PARAM') ||
|
|
2797
|
+
type.startsWith('INPUT_PARAM') ||
|
|
2798
|
+
type.startsWith('OUTPUT_PARAM') ||
|
|
2799
|
+
listItem.startsWith('{') ||
|
|
2800
|
+
listItem.startsWith('> {') /* <- Note: This is a bit hack to parse return parameters defined at the end of each section */) {
|
|
2801
|
+
var parametersMatch = listItem.match(/\{(?<parameterName>[a-z0-9_]+)\}[^\S\r\n]*(?<parameterDescription>.*)$/im);
|
|
2802
|
+
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
2803
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
|
|
2804
|
+
}
|
|
2805
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2806
|
+
var _b = parametersMatch.groups, parameterName = _b.parameterName, parameterDescription = _b.parameterDescription;
|
|
2807
|
+
if (parameterDescription && parameterDescription.match(/\{(?<parameterName>[a-z0-9_]+)\}/im)) {
|
|
2808
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Parameter {".concat(parameterName, "} can not contain another parameter in description:\n\n - ").concat(listItem, "\n ")));
|
|
2809
|
+
}
|
|
2810
|
+
var isInput = type.startsWith('INPUT');
|
|
2811
|
+
var isOutput = type.startsWith('OUTPUT');
|
|
2812
|
+
if (listItem.startsWith('> {')) {
|
|
2813
|
+
isInput = false;
|
|
2814
|
+
isOutput = false;
|
|
2815
|
+
}
|
|
2816
|
+
return {
|
|
2817
|
+
type: 'PARAMETER',
|
|
2818
|
+
parameterName: parameterName,
|
|
2819
|
+
parameterDescription: parameterDescription.trim() || null,
|
|
2820
|
+
isInput: isInput,
|
|
2821
|
+
isOutput: isOutput,
|
|
2822
|
+
};
|
|
2823
|
+
}
|
|
2824
|
+
else if (type.startsWith('JOKER')) {
|
|
2825
|
+
if (listItemParts.length !== 2) {
|
|
2826
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid JOKER command:\n\n - ".concat(listItem, "\n ")));
|
|
2827
|
+
}
|
|
2828
|
+
var parametersMatch = (listItemParts.pop() || '').match(/^\{(?<parameterName>[a-z0-9_]+)\}$/im);
|
|
2829
|
+
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
2830
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
|
|
2831
|
+
}
|
|
2832
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2833
|
+
var parameterName = parametersMatch.groups.parameterName;
|
|
2834
|
+
return {
|
|
2835
|
+
type: 'JOKER',
|
|
2836
|
+
parameterName: parameterName,
|
|
2837
|
+
};
|
|
2838
|
+
}
|
|
2839
|
+
else if (type.startsWith('POSTPROCESS') || type.startsWith('POST_PROCESS')) {
|
|
2840
|
+
if (listItemParts.length !== 2) {
|
|
2841
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid POSTPROCESSING command:\n\n - ".concat(listItem, "\n ")));
|
|
2842
|
+
}
|
|
2843
|
+
var functionName = listItemParts.pop();
|
|
2844
|
+
return {
|
|
2845
|
+
type: 'POSTPROCESS',
|
|
2846
|
+
functionName: functionName,
|
|
2847
|
+
};
|
|
2848
|
+
}
|
|
2849
|
+
else if (type.startsWith('EXPECT_JSON')) {
|
|
2850
|
+
return {
|
|
2851
|
+
type: 'EXPECT_FORMAT',
|
|
2852
|
+
format: 'JSON',
|
|
2853
|
+
};
|
|
2854
|
+
// [🥤]
|
|
2855
|
+
}
|
|
2856
|
+
else if (type.startsWith('EXPECT')) {
|
|
2857
|
+
try {
|
|
2858
|
+
listItemParts.shift();
|
|
2859
|
+
var sign = void 0;
|
|
2860
|
+
var signRaw = listItemParts.shift();
|
|
2861
|
+
if (/^exact/i.test(signRaw)) {
|
|
2862
|
+
sign = 'EXACTLY';
|
|
2863
|
+
}
|
|
2864
|
+
else if (/^min/i.test(signRaw)) {
|
|
2865
|
+
sign = 'MINIMUM';
|
|
2866
|
+
}
|
|
2867
|
+
else if (/^max/i.test(signRaw)) {
|
|
2868
|
+
sign = 'MAXIMUM';
|
|
1345
2869
|
}
|
|
1346
2870
|
else {
|
|
1347
|
-
|
|
1348
|
-
normalizedChar = '-';
|
|
2871
|
+
throw new PromptbookSyntaxError("Invalid sign \"".concat(signRaw, "\", expected EXACTLY, MIN or MAX"));
|
|
1349
2872
|
}
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
normalizedName += '-';
|
|
2873
|
+
var amountRaw = listItemParts.shift();
|
|
2874
|
+
var amount = parseNumber(amountRaw);
|
|
2875
|
+
if (amount < 0) {
|
|
2876
|
+
throw new PromptbookSyntaxError('Amount must be positive number or zero');
|
|
1355
2877
|
}
|
|
1356
|
-
|
|
1357
|
-
|
|
2878
|
+
if (amount !== Math.floor(amount)) {
|
|
2879
|
+
throw new PromptbookSyntaxError('Amount must be whole number');
|
|
2880
|
+
}
|
|
2881
|
+
var unitRaw = listItemParts.shift();
|
|
2882
|
+
var unit = undefined;
|
|
2883
|
+
try {
|
|
2884
|
+
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()) {
|
|
2885
|
+
var existingUnit = EXPECTATION_UNITS_1_1.value;
|
|
2886
|
+
var existingUnitText = existingUnit;
|
|
2887
|
+
existingUnitText = existingUnitText.substring(0, existingUnitText.length - 1);
|
|
2888
|
+
if (existingUnitText === 'CHARACTER') {
|
|
2889
|
+
existingUnitText = 'CHAR';
|
|
2890
|
+
}
|
|
2891
|
+
if (new RegExp("^".concat(existingUnitText.toLowerCase())).test(unitRaw.toLowerCase()) ||
|
|
2892
|
+
new RegExp("^".concat(unitRaw.toLowerCase())).test(existingUnitText.toLowerCase())) {
|
|
2893
|
+
if (unit !== undefined) {
|
|
2894
|
+
throw new PromptbookSyntaxError("Ambiguous unit \"".concat(unitRaw, "\""));
|
|
2895
|
+
}
|
|
2896
|
+
unit = existingUnit;
|
|
2897
|
+
}
|
|
2898
|
+
}
|
|
2899
|
+
}
|
|
2900
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2901
|
+
finally {
|
|
2902
|
+
try {
|
|
2903
|
+
if (EXPECTATION_UNITS_1_1 && !EXPECTATION_UNITS_1_1.done && (_a = EXPECTATION_UNITS_1.return)) _a.call(EXPECTATION_UNITS_1);
|
|
2904
|
+
}
|
|
2905
|
+
finally { if (e_1) throw e_1.error; }
|
|
2906
|
+
}
|
|
2907
|
+
if (unit === undefined) {
|
|
2908
|
+
throw new PromptbookSyntaxError("Invalid unit \"".concat(unitRaw, "\""));
|
|
2909
|
+
}
|
|
2910
|
+
return {
|
|
2911
|
+
type: 'EXPECT_AMOUNT',
|
|
2912
|
+
sign: sign,
|
|
2913
|
+
unit: unit,
|
|
2914
|
+
amount: amount,
|
|
2915
|
+
};
|
|
1358
2916
|
}
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
2917
|
+
catch (error) {
|
|
2918
|
+
if (!(error instanceof Error)) {
|
|
2919
|
+
throw error;
|
|
2920
|
+
}
|
|
2921
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid EXPECT command; ".concat(error.message, ":\n\n - ").concat(listItem, "\n ")));
|
|
1364
2922
|
}
|
|
1365
|
-
|
|
2923
|
+
/*
|
|
2924
|
+
} else if (type.startsWith('__________________')) {
|
|
2925
|
+
// <- [🥻] Insert here when making new command
|
|
2926
|
+
*/
|
|
2927
|
+
}
|
|
2928
|
+
else {
|
|
2929
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Unknown command:\n\n - ".concat(listItem, "\n\n Supported commands are:\n - PROMPTBOOK_URL <url>\n - PROMPTBOOK_VERSION <version>\n - EXECUTE PROMPT TEMPLATE\n - EXECUTE SIMPLE TEMPLATE\n - SIMPLE TEMPLATE\n - EXECUTE SCRIPT\n - EXECUTE PROMPT_DIALOG'\n - PROMPT_DIALOG'\n - MODEL NAME <name>\n - MODEL VARIANT <\"Chat\"|\"Completion\">\n - INPUT PARAM {<name>} <description>\n - OUTPUT PARAM {<name>} <description>\n - POSTPROCESS `{functionName}`\n - JOKER {<name>}\n - EXPECT JSON\n - EXPECT <\"Exactly\"|\"Min\"|\"Max\"> <number> <\"Chars\"|\"Words\"|\"Sentences\"|\"Paragraphs\"|\"Pages\">\n\n ")));
|
|
1366
2930
|
}
|
|
1367
|
-
normalizedName = normalizedName.split(/-+/g).join('-');
|
|
1368
|
-
normalizedName = normalizedName.split(/-?\/-?/g).join('/');
|
|
1369
|
-
normalizedName = normalizedName.replace(/^-/, '');
|
|
1370
|
-
normalizedName = normalizedName.replace(/-$/, '');
|
|
1371
|
-
return normalizedName;
|
|
1372
2931
|
}
|
|
1373
2932
|
|
|
1374
2933
|
/**
|
|
@@ -1427,7 +2986,10 @@ function promptbookStringToJson(promptbookString, options) {
|
|
|
1427
2986
|
knowledge: [],
|
|
1428
2987
|
};
|
|
1429
2988
|
if (!llmTools) return [3 /*break*/, 2];
|
|
1430
|
-
return [4 /*yield*/, prepareKnowledgeFromMarkdown(
|
|
2989
|
+
return [4 /*yield*/, prepareKnowledgeFromMarkdown({
|
|
2990
|
+
content: 'Roses are red, violets are blue, programmers use Promptbook, users too',
|
|
2991
|
+
llmTools: llmTools,
|
|
2992
|
+
})];
|
|
1431
2993
|
case 1:
|
|
1432
2994
|
knowledge = _e.sent();
|
|
1433
2995
|
console.info('!!!! knowledge', knowledge);
|