@promptbook/core 0.44.0-1 โ 0.44.0-10
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 +788 -653
- package/esm/index.es.js.map +1 -1
- package/esm/typings/_packages/core.index.d.ts +2 -1
- package/esm/typings/_packages/utils.index.d.ts +3 -2
- package/esm/typings/config.d.ts +4 -0
- package/esm/typings/execution/plugins/natural-execution-tools/mocked/MockedEchoNaturalExecutionTools.d.ts +1 -1
- package/esm/typings/execution/plugins/natural-execution-tools/mocked/MockedFackedNaturalExecutionTools.d.ts +19 -0
- package/esm/typings/execution/plugins/natural-execution-tools/mocked/fakeTextToExpectations.d.ts +11 -0
- package/esm/typings/execution/plugins/natural-execution-tools/mocked/fakeTextToExpectations.test.d.ts +1 -0
- package/esm/typings/execution/plugins/natural-execution-tools/mocked/faked-completion.test.d.ts +1 -0
- package/esm/typings/execution/utils/checkExpectations.d.ts +25 -0
- package/esm/typings/execution/utils/checkExpectations.test.d.ts +1 -0
- package/esm/typings/types/Prompt.d.ts +8 -0
- package/esm/typings/types/PromptbookJson/PromptTemplateJson.d.ts +13 -4
- package/package.json +2 -1
- package/umd/index.umd.js +791 -656
- package/umd/index.umd.js.map +1 -1
- package/umd/typings/_packages/core.index.d.ts +2 -1
- package/umd/typings/_packages/utils.index.d.ts +3 -2
- package/umd/typings/config.d.ts +4 -0
- package/umd/typings/execution/plugins/natural-execution-tools/mocked/MockedEchoNaturalExecutionTools.d.ts +1 -1
- package/umd/typings/execution/plugins/natural-execution-tools/mocked/MockedFackedNaturalExecutionTools.d.ts +19 -0
- package/umd/typings/execution/plugins/natural-execution-tools/mocked/fakeTextToExpectations.d.ts +11 -0
- package/umd/typings/execution/plugins/natural-execution-tools/mocked/fakeTextToExpectations.test.d.ts +1 -0
- package/umd/typings/execution/plugins/natural-execution-tools/mocked/faked-completion.test.d.ts +1 -0
- package/umd/typings/execution/utils/checkExpectations.d.ts +25 -0
- package/umd/typings/execution/utils/checkExpectations.test.d.ts +1 -0
- package/umd/typings/types/Prompt.d.ts +8 -0
- package/umd/typings/types/PromptbookJson/PromptTemplateJson.d.ts +13 -4
package/umd/index.umd.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('spacetrim'), require('prettier'), require('prettier/parser-html'), require('moment')) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports', 'spacetrim', 'prettier', 'prettier/parser-html', 'moment'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-core"] = {}, global.spaceTrim));
|
|
5
|
-
})(this, (function (exports, spaceTrim) { 'use strict';
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('spacetrim'), require('prettier'), require('prettier/parser-html'), require('moment'), require('lorem-ipsum')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', 'spacetrim', 'prettier', 'prettier/parser-html', 'moment', 'lorem-ipsum'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-core"] = {}, global.spaceTrim, null, null, null, global.loremIpsum));
|
|
5
|
+
})(this, (function (exports, spaceTrim, prettier, parserHtml, moment, loremIpsum) { 'use strict';
|
|
6
6
|
|
|
7
7
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
8
|
|
|
@@ -127,85 +127,6 @@
|
|
|
127
127
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
-
/**
|
|
131
|
-
* This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
|
|
132
|
-
*/
|
|
133
|
-
var PromptbookSyntaxError = /** @class */ (function (_super) {
|
|
134
|
-
__extends(PromptbookSyntaxError, _super);
|
|
135
|
-
function PromptbookSyntaxError(message) {
|
|
136
|
-
var _this = _super.call(this, message) || this;
|
|
137
|
-
_this.name = 'PromptbookSyntaxError';
|
|
138
|
-
Object.setPrototypeOf(_this, PromptbookSyntaxError.prototype);
|
|
139
|
-
return _this;
|
|
140
|
-
}
|
|
141
|
-
return PromptbookSyntaxError;
|
|
142
|
-
}(Error));
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Supported script languages
|
|
146
|
-
*/
|
|
147
|
-
var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Parses the template and returns the list of all parameter names
|
|
151
|
-
*
|
|
152
|
-
* @param template the template with parameters in {curly} braces
|
|
153
|
-
* @returns the list of parameter names
|
|
154
|
-
*
|
|
155
|
-
* @private within the library
|
|
156
|
-
*/
|
|
157
|
-
function extractParameters(template) {
|
|
158
|
-
var e_1, _a;
|
|
159
|
-
var matches = template.matchAll(/{\w+}/g);
|
|
160
|
-
var parameterNames = [];
|
|
161
|
-
try {
|
|
162
|
-
for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
|
|
163
|
-
var match = matches_1_1.value;
|
|
164
|
-
var parameterName = match[0].slice(1, -1);
|
|
165
|
-
if (!parameterNames.includes(parameterName)) {
|
|
166
|
-
parameterNames.push(parameterName);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
171
|
-
finally {
|
|
172
|
-
try {
|
|
173
|
-
if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
|
|
174
|
-
}
|
|
175
|
-
finally { if (e_1) throw e_1.error; }
|
|
176
|
-
}
|
|
177
|
-
return parameterNames;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Computes the deepness of the markdown structure.
|
|
182
|
-
*
|
|
183
|
-
* @private within the library
|
|
184
|
-
*/
|
|
185
|
-
function countMarkdownStructureDeepness(markdownStructure) {
|
|
186
|
-
var e_1, _a;
|
|
187
|
-
var maxDeepness = 0;
|
|
188
|
-
try {
|
|
189
|
-
for (var _b = __values(markdownStructure.sections), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
190
|
-
var section = _c.value;
|
|
191
|
-
maxDeepness = Math.max(maxDeepness, countMarkdownStructureDeepness(section));
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
195
|
-
finally {
|
|
196
|
-
try {
|
|
197
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
198
|
-
}
|
|
199
|
-
finally { if (e_1) throw e_1.error; }
|
|
200
|
-
}
|
|
201
|
-
return maxDeepness + 1;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* The maximum number of iterations for a loops
|
|
206
|
-
*/
|
|
207
|
-
var LOOP_LIMIT = 1000;
|
|
208
|
-
|
|
209
130
|
/**
|
|
210
131
|
* This error type indicates that the error should not happen and its last check before crashing with some other error
|
|
211
132
|
*/
|
|
@@ -220,218 +141,6 @@
|
|
|
220
141
|
return UnexpectedError;
|
|
221
142
|
}(Error));
|
|
222
143
|
|
|
223
|
-
/**
|
|
224
|
-
* Parse a markdown string into a MarkdownStructure object.
|
|
225
|
-
*
|
|
226
|
-
* Note: This function does work with code blocks
|
|
227
|
-
* Note: This function does not work with markdown comments
|
|
228
|
-
*
|
|
229
|
-
* @param markdown The markdown string to parse.
|
|
230
|
-
* @returns The MarkdownStructure object.
|
|
231
|
-
*
|
|
232
|
-
* @private within the library
|
|
233
|
-
*/
|
|
234
|
-
function markdownToMarkdownStructure(markdown) {
|
|
235
|
-
var e_1, _a;
|
|
236
|
-
var lines = markdown.split('\n');
|
|
237
|
-
var root = { level: 0, title: '', contentLines: [], sections: [], parent: null };
|
|
238
|
-
var current = root;
|
|
239
|
-
var isInsideCodeBlock = false;
|
|
240
|
-
try {
|
|
241
|
-
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
242
|
-
var line = lines_1_1.value;
|
|
243
|
-
var headingMatch = line.match(/^(?<mark>#{1,6})\s(?<title>.*)/);
|
|
244
|
-
if (isInsideCodeBlock || !headingMatch) {
|
|
245
|
-
if (line.startsWith('```')) {
|
|
246
|
-
isInsideCodeBlock = !isInsideCodeBlock;
|
|
247
|
-
}
|
|
248
|
-
current.contentLines.push(line);
|
|
249
|
-
}
|
|
250
|
-
else {
|
|
251
|
-
var level = headingMatch.groups.mark.length;
|
|
252
|
-
var title = headingMatch.groups.title.trim();
|
|
253
|
-
var parent_1 = void 0;
|
|
254
|
-
if (level > current.level) {
|
|
255
|
-
// Note: Going deeper (next section is child of current)
|
|
256
|
-
parent_1 = current;
|
|
257
|
-
}
|
|
258
|
-
else {
|
|
259
|
-
// Note: Going up or staying at the same level (next section is sibling or parent or grandparent,... of current)
|
|
260
|
-
parent_1 = current;
|
|
261
|
-
var loopLimit = LOOP_LIMIT;
|
|
262
|
-
while (parent_1.level !== level - 1) {
|
|
263
|
-
if (loopLimit-- < 0) {
|
|
264
|
-
throw new UnexpectedError('Loop limit reached during parsing of markdown structure in `markdownToMarkdownStructure`');
|
|
265
|
-
}
|
|
266
|
-
if (parent_1.parent === null /* <- Note: We are in root */) {
|
|
267
|
-
// [๐ป]
|
|
268
|
-
throw new Error(spaceTrim__default["default"]("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
|
|
269
|
-
}
|
|
270
|
-
parent_1 = parent_1.parent;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
|
|
274
|
-
parent_1.sections.push(section);
|
|
275
|
-
current = section;
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
280
|
-
finally {
|
|
281
|
-
try {
|
|
282
|
-
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
283
|
-
}
|
|
284
|
-
finally { if (e_1) throw e_1.error; }
|
|
285
|
-
}
|
|
286
|
-
if (root.sections.length === 1) {
|
|
287
|
-
var markdownStructure = parsingMarkdownStructureToMarkdownStructure(root.sections[0]);
|
|
288
|
-
return markdownStructure;
|
|
289
|
-
}
|
|
290
|
-
// [๐ป]
|
|
291
|
-
throw new Error('The markdown file must have exactly one top-level section.');
|
|
292
|
-
// return root;
|
|
293
|
-
}
|
|
294
|
-
/**
|
|
295
|
-
* @private
|
|
296
|
-
*/
|
|
297
|
-
function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
|
|
298
|
-
var level = parsingMarkdownStructure.level, title = parsingMarkdownStructure.title, contentLines = parsingMarkdownStructure.contentLines, sections = parsingMarkdownStructure.sections;
|
|
299
|
-
return {
|
|
300
|
-
level: level,
|
|
301
|
-
title: title,
|
|
302
|
-
content: spaceTrim__default["default"](contentLines.join('\n')),
|
|
303
|
-
sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* Utility function to extract all list items from markdown
|
|
309
|
-
*
|
|
310
|
-
* Note: It works with both ul and ol
|
|
311
|
-
* Note: It omits list items in code blocks
|
|
312
|
-
* Note: It flattens nested lists
|
|
313
|
-
* Note: It can not work with html syntax and comments
|
|
314
|
-
*
|
|
315
|
-
* @param markdown any valid markdown
|
|
316
|
-
* @returns
|
|
317
|
-
*/
|
|
318
|
-
function extractAllListItemsFromMarkdown(markdown) {
|
|
319
|
-
var e_1, _a;
|
|
320
|
-
var lines = markdown.split('\n');
|
|
321
|
-
var listItems = [];
|
|
322
|
-
var isInCodeBlock = false;
|
|
323
|
-
try {
|
|
324
|
-
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
325
|
-
var line = lines_1_1.value;
|
|
326
|
-
var trimmedLine = line.trim();
|
|
327
|
-
if (trimmedLine.startsWith('```')) {
|
|
328
|
-
isInCodeBlock = !isInCodeBlock;
|
|
329
|
-
}
|
|
330
|
-
if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
|
|
331
|
-
var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
|
|
332
|
-
listItems.push(listItem);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
337
|
-
finally {
|
|
338
|
-
try {
|
|
339
|
-
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
340
|
-
}
|
|
341
|
-
finally { if (e_1) throw e_1.error; }
|
|
342
|
-
}
|
|
343
|
-
return listItems;
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
/**
|
|
347
|
-
* Makes first letter of a string uppercase
|
|
348
|
-
*
|
|
349
|
-
*/
|
|
350
|
-
function capitalize(word) {
|
|
351
|
-
return word.substring(0, 1).toUpperCase() + word.substring(1);
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
/**
|
|
355
|
-
* Extracts all code blocks from markdown.
|
|
356
|
-
*
|
|
357
|
-
* Note: There are 3 simmilar function:
|
|
358
|
-
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
359
|
-
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
360
|
-
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
361
|
-
*
|
|
362
|
-
* @param markdown any valid markdown
|
|
363
|
-
* @returns code blocks with language and content
|
|
364
|
-
*
|
|
365
|
-
*/
|
|
366
|
-
function extractAllBlocksFromMarkdown(markdown) {
|
|
367
|
-
var e_1, _a;
|
|
368
|
-
var codeBlocks = [];
|
|
369
|
-
var lines = markdown.split('\n');
|
|
370
|
-
var currentCodeBlock = null;
|
|
371
|
-
try {
|
|
372
|
-
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
373
|
-
var line = lines_1_1.value;
|
|
374
|
-
if (line.startsWith('```')) {
|
|
375
|
-
var language = line.slice(3).trim() || null;
|
|
376
|
-
if (currentCodeBlock === null) {
|
|
377
|
-
currentCodeBlock = { language: language, content: '' };
|
|
378
|
-
}
|
|
379
|
-
else {
|
|
380
|
-
if (language !== null) {
|
|
381
|
-
// [๐ป]
|
|
382
|
-
throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
|
|
383
|
-
}
|
|
384
|
-
codeBlocks.push(currentCodeBlock);
|
|
385
|
-
currentCodeBlock = null;
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
else if (currentCodeBlock !== null) {
|
|
389
|
-
if (currentCodeBlock.content !== '') {
|
|
390
|
-
currentCodeBlock.content += '\n';
|
|
391
|
-
}
|
|
392
|
-
currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
397
|
-
finally {
|
|
398
|
-
try {
|
|
399
|
-
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
400
|
-
}
|
|
401
|
-
finally { if (e_1) throw e_1.error; }
|
|
402
|
-
}
|
|
403
|
-
if (currentCodeBlock !== null) {
|
|
404
|
-
// [๐ป]
|
|
405
|
-
throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
|
|
406
|
-
}
|
|
407
|
-
return codeBlocks;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
/**
|
|
411
|
-
* Extracts exactly ONE code block from markdown.
|
|
412
|
-
*
|
|
413
|
-
* Note: If there are multiple or no code blocks the function throws an error
|
|
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 block with language and content
|
|
422
|
-
*/
|
|
423
|
-
function extractOneBlockFromMarkdown(markdown) {
|
|
424
|
-
var codeBlocks = extractAllBlocksFromMarkdown(markdown);
|
|
425
|
-
if (codeBlocks.length !== 1) {
|
|
426
|
-
// TODO: Report more specific place where the error happened
|
|
427
|
-
throw new Error(/* <- [๐ป] */ 'There should be exactly one code block in the markdown');
|
|
428
|
-
}
|
|
429
|
-
return codeBlocks[0];
|
|
430
|
-
}
|
|
431
|
-
/***
|
|
432
|
-
* TODO: [๐ป] !!! Decide of this is internal util, external util OR validator/postprocessor
|
|
433
|
-
*/
|
|
434
|
-
|
|
435
144
|
/**
|
|
436
145
|
* Removes HTML or Markdown comments from a string.
|
|
437
146
|
*
|
|
@@ -443,94 +152,18 @@
|
|
|
443
152
|
}
|
|
444
153
|
|
|
445
154
|
/**
|
|
446
|
-
*
|
|
447
|
-
*/
|
|
448
|
-
var PROMPTBOOK_VERSION = '0.44.0-0';
|
|
449
|
-
|
|
450
|
-
/**
|
|
451
|
-
* Parses the given script and returns the list of all used variables that are not defined in the script
|
|
452
|
-
*
|
|
453
|
-
* @param script from which to extract the variables
|
|
454
|
-
* @returns the list of variable names
|
|
455
|
-
* @throws {PromptbookSyntaxError} if the script is invalid
|
|
456
|
-
*
|
|
457
|
-
* @private within the promptbookStringToJson
|
|
155
|
+
* This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
|
|
458
156
|
*/
|
|
459
|
-
function
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
}
|
|
467
|
-
catch (error) {
|
|
468
|
-
if (!(error instanceof ReferenceError)) {
|
|
469
|
-
throw error;
|
|
470
|
-
}
|
|
471
|
-
var undefinedName = error.message.split(' ')[0];
|
|
472
|
-
/*
|
|
473
|
-
Note: Remapping error
|
|
474
|
-
From: [ReferenceError: thing is not defined],
|
|
475
|
-
To: [Error: Parameter {thing} is not defined],
|
|
476
|
-
*/
|
|
477
|
-
if (!undefinedName) {
|
|
478
|
-
throw error;
|
|
479
|
-
}
|
|
480
|
-
if (script.includes(undefinedName + '(')) {
|
|
481
|
-
script = "const ".concat(undefinedName, " = ()=>'';") + script;
|
|
482
|
-
}
|
|
483
|
-
else {
|
|
484
|
-
variables.push(undefinedName);
|
|
485
|
-
script = "const ".concat(undefinedName, " = '';") + script;
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
catch (error) {
|
|
490
|
-
if (!(error instanceof Error)) {
|
|
491
|
-
throw error;
|
|
492
|
-
}
|
|
493
|
-
throw new PromptbookSyntaxError(spaceTrim__default["default"](function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
|
|
157
|
+
var PromptbookSyntaxError = /** @class */ (function (_super) {
|
|
158
|
+
__extends(PromptbookSyntaxError, _super);
|
|
159
|
+
function PromptbookSyntaxError(message) {
|
|
160
|
+
var _this = _super.call(this, message) || this;
|
|
161
|
+
_this.name = 'PromptbookSyntaxError';
|
|
162
|
+
Object.setPrototypeOf(_this, PromptbookSyntaxError.prototype);
|
|
163
|
+
return _this;
|
|
494
164
|
}
|
|
495
|
-
return
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
/**
|
|
499
|
-
* Execution type describes the way how the block is executed
|
|
500
|
-
*
|
|
501
|
-
* @see https://github.com/webgptorg/promptbook#execution-type
|
|
502
|
-
*/
|
|
503
|
-
var ExecutionTypes = [
|
|
504
|
-
'PROMPT_TEMPLATE',
|
|
505
|
-
'SIMPLE_TEMPLATE',
|
|
506
|
-
'SCRIPT',
|
|
507
|
-
'PROMPT_DIALOG',
|
|
508
|
-
// <- [๐ฅป] Insert here when making new command
|
|
509
|
-
];
|
|
510
|
-
|
|
511
|
-
/**
|
|
512
|
-
* Units of text measurement
|
|
513
|
-
*/
|
|
514
|
-
var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'PARAGRAPHS', 'LINES', 'PAGES'];
|
|
515
|
-
/**
|
|
516
|
-
* TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
|
|
517
|
-
*/
|
|
518
|
-
|
|
519
|
-
/**
|
|
520
|
-
* Removes Markdown formatting tags from a string.
|
|
521
|
-
*
|
|
522
|
-
* @param {string} str - The string to remove Markdown tags from.
|
|
523
|
-
* @returns {string} The input string with all Markdown tags removed.
|
|
524
|
-
*/
|
|
525
|
-
function removeMarkdownFormatting(str) {
|
|
526
|
-
// Remove bold formatting
|
|
527
|
-
str = str.replace(/\*\*(.*?)\*\*/g, '$1');
|
|
528
|
-
// Remove italic formatting
|
|
529
|
-
str = str.replace(/\*(.*?)\*/g, '$1');
|
|
530
|
-
// Remove code formatting
|
|
531
|
-
str = str.replace(/`(.*?)`/g, '$1');
|
|
532
|
-
return str;
|
|
533
|
-
}
|
|
165
|
+
return PromptbookSyntaxError;
|
|
166
|
+
}(Error));
|
|
534
167
|
|
|
535
168
|
/**
|
|
536
169
|
* Function parseNumber will parse number from string
|
|
@@ -591,103 +224,44 @@
|
|
|
591
224
|
}
|
|
592
225
|
var num = parseFloat(value);
|
|
593
226
|
if (isNaN(num)) {
|
|
594
|
-
throw new PromptbookSyntaxError("Unexpected NaN when parsing number from \"".concat(originalValue, "\""));
|
|
595
|
-
}
|
|
596
|
-
return num;
|
|
597
|
-
}
|
|
598
|
-
/**
|
|
599
|
-
* TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
|
|
600
|
-
*/
|
|
601
|
-
|
|
602
|
-
/**
|
|
603
|
-
* This error indicates errors during the execution of the promptbook
|
|
604
|
-
*/
|
|
605
|
-
var PromptbookExecutionError = /** @class */ (function (_super) {
|
|
606
|
-
__extends(PromptbookExecutionError, _super);
|
|
607
|
-
function PromptbookExecutionError(message) {
|
|
608
|
-
var _this = _super.call(this, message) || this;
|
|
609
|
-
_this.name = 'PromptbookExecutionError';
|
|
610
|
-
Object.setPrototypeOf(_this, PromptbookExecutionError.prototype);
|
|
611
|
-
return _this;
|
|
612
|
-
}
|
|
613
|
-
return PromptbookExecutionError;
|
|
614
|
-
}(Error));
|
|
615
|
-
|
|
616
|
-
/**
|
|
617
|
-
* This error occurs during the parameter replacement in the template
|
|
618
|
-
*
|
|
619
|
-
* Note: This is a kindof subtype of PromptbookExecutionError because it occurs during the execution of the pipeline
|
|
620
|
-
*/
|
|
621
|
-
var TemplateError = /** @class */ (function (_super) {
|
|
622
|
-
__extends(TemplateError, _super);
|
|
623
|
-
function TemplateError(message) {
|
|
624
|
-
var _this = _super.call(this, message) || this;
|
|
625
|
-
_this.name = 'TemplateError';
|
|
626
|
-
Object.setPrototypeOf(_this, TemplateError.prototype);
|
|
627
|
-
return _this;
|
|
628
|
-
}
|
|
629
|
-
return TemplateError;
|
|
630
|
-
}(Error));
|
|
631
|
-
|
|
632
|
-
/**
|
|
633
|
-
* Replaces parameters in template with values from parameters object
|
|
634
|
-
*
|
|
635
|
-
* @param template the template with parameters in {curly} braces
|
|
636
|
-
* @param parameters the object with parameters
|
|
637
|
-
* @returns the template with replaced parameters
|
|
638
|
-
* @throws {TemplateError} if parameter is not defined, not closed, or not opened
|
|
639
|
-
*
|
|
640
|
-
* @private within the createPromptbookExecutor
|
|
641
|
-
*/
|
|
642
|
-
function replaceParameters(template, parameters) {
|
|
643
|
-
var replacedTemplate = template;
|
|
644
|
-
var match;
|
|
645
|
-
var loopLimit = LOOP_LIMIT;
|
|
646
|
-
var _loop_1 = function () {
|
|
647
|
-
if (loopLimit-- < 0) {
|
|
648
|
-
throw new UnexpectedError('Loop limit reached during parameters replacement in `replaceParameters`');
|
|
649
|
-
}
|
|
650
|
-
var precol = match.groups.precol;
|
|
651
|
-
var parameterName = match.groups.parameterName;
|
|
652
|
-
if (parameterName === '') {
|
|
653
|
-
return "continue";
|
|
654
|
-
}
|
|
655
|
-
if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
|
|
656
|
-
throw new TemplateError('Parameter is already opened or not closed');
|
|
657
|
-
}
|
|
658
|
-
if (parameters[parameterName] === undefined) {
|
|
659
|
-
throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
|
|
660
|
-
}
|
|
661
|
-
var parameterValue = parameters[parameterName];
|
|
662
|
-
if (parameterValue === undefined) {
|
|
663
|
-
throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
|
|
664
|
-
}
|
|
665
|
-
parameterValue = parameterValue.toString();
|
|
666
|
-
if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
|
|
667
|
-
parameterValue = parameterValue
|
|
668
|
-
.split('\n')
|
|
669
|
-
.map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
|
|
670
|
-
.join('\n');
|
|
671
|
-
}
|
|
672
|
-
replacedTemplate =
|
|
673
|
-
replacedTemplate.substring(0, match.index + precol.length) +
|
|
674
|
-
parameterValue +
|
|
675
|
-
replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
|
|
676
|
-
};
|
|
677
|
-
while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
|
|
678
|
-
.exec(replacedTemplate))) {
|
|
679
|
-
_loop_1();
|
|
680
|
-
}
|
|
681
|
-
// [๐ซ] Check if there are parameters that are not closed properly
|
|
682
|
-
if (/{\w+$/.test(replacedTemplate)) {
|
|
683
|
-
throw new TemplateError('Parameter is not closed');
|
|
684
|
-
}
|
|
685
|
-
// [๐ซ] Check if there are parameters that are not opened properly
|
|
686
|
-
if (/^\w+}/.test(replacedTemplate)) {
|
|
687
|
-
throw new TemplateError('Parameter is not opened');
|
|
227
|
+
throw new PromptbookSyntaxError("Unexpected NaN when parsing number from \"".concat(originalValue, "\""));
|
|
688
228
|
}
|
|
689
|
-
return
|
|
229
|
+
return num;
|
|
690
230
|
}
|
|
231
|
+
/**
|
|
232
|
+
* TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
|
|
233
|
+
*/
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* This error indicates errors during the execution of the promptbook
|
|
237
|
+
*/
|
|
238
|
+
var PromptbookExecutionError = /** @class */ (function (_super) {
|
|
239
|
+
__extends(PromptbookExecutionError, _super);
|
|
240
|
+
function PromptbookExecutionError(message) {
|
|
241
|
+
var _this = _super.call(this, message) || this;
|
|
242
|
+
_this.name = 'PromptbookExecutionError';
|
|
243
|
+
Object.setPrototypeOf(_this, PromptbookExecutionError.prototype);
|
|
244
|
+
return _this;
|
|
245
|
+
}
|
|
246
|
+
return PromptbookExecutionError;
|
|
247
|
+
}(Error));
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* This error occurs when some expectation is not met in the execution of the pipeline
|
|
251
|
+
*
|
|
252
|
+
* @private Always catched and rethrown as `PromptbookExecutionError`
|
|
253
|
+
* Note: This is a kindof subtype of PromptbookExecutionError
|
|
254
|
+
*/
|
|
255
|
+
var ExpectError = /** @class */ (function (_super) {
|
|
256
|
+
__extends(ExpectError, _super);
|
|
257
|
+
function ExpectError(message) {
|
|
258
|
+
var _this = _super.call(this, message) || this;
|
|
259
|
+
_this.name = 'ExpectError';
|
|
260
|
+
Object.setPrototypeOf(_this, ExpectError.prototype);
|
|
261
|
+
return _this;
|
|
262
|
+
}
|
|
263
|
+
return ExpectError;
|
|
264
|
+
}(Error));
|
|
691
265
|
|
|
692
266
|
/**
|
|
693
267
|
* Counts number of characters in the text
|
|
@@ -969,70 +543,356 @@
|
|
|
969
543
|
defaultDiacriticsRemovalMap[i].base;
|
|
970
544
|
}
|
|
971
545
|
}
|
|
972
|
-
// <- TODO: !!!! Put to maker function
|
|
973
|
-
/*
|
|
974
|
-
@see https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
|
|
975
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
976
|
-
you may not use this file except in compliance with the License.
|
|
977
|
-
You may obtain a copy of the License at
|
|
978
|
-
|
|
979
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
980
|
-
|
|
981
|
-
Unless required by applicable law or agreed to in writing, software
|
|
982
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
983
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
984
|
-
See the License for the specific language governing permissions and
|
|
985
|
-
limitations under the License.
|
|
986
|
-
*/
|
|
546
|
+
// <- TODO: !!!! Put to maker function
|
|
547
|
+
/*
|
|
548
|
+
@see https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
|
|
549
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
550
|
+
you may not use this file except in compliance with the License.
|
|
551
|
+
You may obtain a copy of the License at
|
|
552
|
+
|
|
553
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
554
|
+
|
|
555
|
+
Unless required by applicable law or agreed to in writing, software
|
|
556
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
557
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
558
|
+
See the License for the specific language governing permissions and
|
|
559
|
+
limitations under the License.
|
|
560
|
+
*/
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
*
|
|
564
|
+
*/
|
|
565
|
+
function removeDiacritics(input) {
|
|
566
|
+
/*eslint no-control-regex: "off"*/
|
|
567
|
+
return input.replace(/[^\u0000-\u007E]/g, function (a) {
|
|
568
|
+
return DIACRITIC_VARIANTS_LETTERS[a] || a;
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* Counts number of words in the text
|
|
574
|
+
*/
|
|
575
|
+
function countWords(text) {
|
|
576
|
+
text = text.replace(/[\p{Extended_Pictographic}]/gu, 'a');
|
|
577
|
+
text = removeDiacritics(text);
|
|
578
|
+
return text.split(/[^a-zะฐ-ั0-9]+/i).filter(function (word) { return word.length > 0; }).length;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* Index of all counter functions
|
|
583
|
+
*/
|
|
584
|
+
var CountUtils = {
|
|
585
|
+
CHARACTERS: countCharacters,
|
|
586
|
+
WORDS: countWords,
|
|
587
|
+
SENTENCES: countSentences,
|
|
588
|
+
PARAGRAPHS: countParagraphs,
|
|
589
|
+
LINES: countLines,
|
|
590
|
+
PAGES: countPages,
|
|
591
|
+
};
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* Function checkExpectations will check if the expectations on given value are met
|
|
595
|
+
*
|
|
596
|
+
* Note: There are two simmilar functions:
|
|
597
|
+
* - `checkExpectations` which throws an error if the expectations are not met
|
|
598
|
+
* - `isPassingExpectations` which returns a boolean
|
|
599
|
+
*
|
|
600
|
+
* @throws {ExpectError} if the expectations are not met
|
|
601
|
+
* @returns {void} Nothing
|
|
602
|
+
*/
|
|
603
|
+
function checkExpectations(expectations, value) {
|
|
604
|
+
var e_1, _a;
|
|
605
|
+
try {
|
|
606
|
+
for (var _b = __values(Object.entries(expectations)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
607
|
+
var _d = __read(_c.value, 2), unit = _d[0], _e = _d[1], max = _e.max, min = _e.min;
|
|
608
|
+
var amount = CountUtils[unit.toUpperCase()](value);
|
|
609
|
+
if (min && amount < min) {
|
|
610
|
+
throw new ExpectError("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
|
|
611
|
+
} /* not else */
|
|
612
|
+
if (max && amount > max) {
|
|
613
|
+
throw new ExpectError("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
618
|
+
finally {
|
|
619
|
+
try {
|
|
620
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
621
|
+
}
|
|
622
|
+
finally { if (e_1) throw e_1.error; }
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* Function checkExpectations will check if the expectations on given value are met
|
|
627
|
+
*
|
|
628
|
+
* Note: There are two simmilar functions:
|
|
629
|
+
* - `checkExpectations` which throws an error if the expectations are not met
|
|
630
|
+
* - `isPassingExpectations` which returns a boolean
|
|
631
|
+
*
|
|
632
|
+
* @returns {boolean} True if the expectations are met
|
|
633
|
+
*/
|
|
634
|
+
function isPassingExpectations(expectations, value) {
|
|
635
|
+
try {
|
|
636
|
+
checkExpectations(expectations, value);
|
|
637
|
+
return true;
|
|
638
|
+
}
|
|
639
|
+
catch (error) {
|
|
640
|
+
if (!(error instanceof ExpectError)) {
|
|
641
|
+
throw error;
|
|
642
|
+
}
|
|
643
|
+
return false;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* TODO: [๐] Unite object for expecting amount and format
|
|
648
|
+
*/
|
|
649
|
+
|
|
650
|
+
/**
|
|
651
|
+
* The maximum number of iterations for a loops
|
|
652
|
+
*/
|
|
653
|
+
var LOOP_LIMIT = 1000;
|
|
654
|
+
/**
|
|
655
|
+
* The maximum number of iterations for a loops which adds characters one by one
|
|
656
|
+
*/
|
|
657
|
+
var CHARACTER_LOOP_LIMIT = 100000;
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* This error occurs during the parameter replacement in the template
|
|
661
|
+
*
|
|
662
|
+
* Note: This is a kindof subtype of PromptbookExecutionError because it occurs during the execution of the pipeline
|
|
663
|
+
*/
|
|
664
|
+
var TemplateError = /** @class */ (function (_super) {
|
|
665
|
+
__extends(TemplateError, _super);
|
|
666
|
+
function TemplateError(message) {
|
|
667
|
+
var _this = _super.call(this, message) || this;
|
|
668
|
+
_this.name = 'TemplateError';
|
|
669
|
+
Object.setPrototypeOf(_this, TemplateError.prototype);
|
|
670
|
+
return _this;
|
|
671
|
+
}
|
|
672
|
+
return TemplateError;
|
|
673
|
+
}(Error));
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* Replaces parameters in template with values from parameters object
|
|
677
|
+
*
|
|
678
|
+
* @param template the template with parameters in {curly} braces
|
|
679
|
+
* @param parameters the object with parameters
|
|
680
|
+
* @returns the template with replaced parameters
|
|
681
|
+
* @throws {TemplateError} if parameter is not defined, not closed, or not opened
|
|
682
|
+
*
|
|
683
|
+
* @private within the createPromptbookExecutor
|
|
684
|
+
*/
|
|
685
|
+
function replaceParameters(template, parameters) {
|
|
686
|
+
var replacedTemplate = template;
|
|
687
|
+
var match;
|
|
688
|
+
var loopLimit = LOOP_LIMIT;
|
|
689
|
+
var _loop_1 = function () {
|
|
690
|
+
if (loopLimit-- < 0) {
|
|
691
|
+
throw new UnexpectedError('Loop limit reached during parameters replacement in `replaceParameters`');
|
|
692
|
+
}
|
|
693
|
+
var precol = match.groups.precol;
|
|
694
|
+
var parameterName = match.groups.parameterName;
|
|
695
|
+
if (parameterName === '') {
|
|
696
|
+
return "continue";
|
|
697
|
+
}
|
|
698
|
+
if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
|
|
699
|
+
throw new TemplateError('Parameter is already opened or not closed');
|
|
700
|
+
}
|
|
701
|
+
if (parameters[parameterName] === undefined) {
|
|
702
|
+
throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
|
|
703
|
+
}
|
|
704
|
+
var parameterValue = parameters[parameterName];
|
|
705
|
+
if (parameterValue === undefined) {
|
|
706
|
+
throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
|
|
707
|
+
}
|
|
708
|
+
parameterValue = parameterValue.toString();
|
|
709
|
+
if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
|
|
710
|
+
parameterValue = parameterValue
|
|
711
|
+
.split('\n')
|
|
712
|
+
.map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
|
|
713
|
+
.join('\n');
|
|
714
|
+
}
|
|
715
|
+
replacedTemplate =
|
|
716
|
+
replacedTemplate.substring(0, match.index + precol.length) +
|
|
717
|
+
parameterValue +
|
|
718
|
+
replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
|
|
719
|
+
};
|
|
720
|
+
while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
|
|
721
|
+
.exec(replacedTemplate))) {
|
|
722
|
+
_loop_1();
|
|
723
|
+
}
|
|
724
|
+
// [๐ซ] Check if there are parameters that are not closed properly
|
|
725
|
+
if (/{\w+$/.test(replacedTemplate)) {
|
|
726
|
+
throw new TemplateError('Parameter is not closed');
|
|
727
|
+
}
|
|
728
|
+
// [๐ซ] Check if there are parameters that are not opened properly
|
|
729
|
+
if (/^\w+}/.test(replacedTemplate)) {
|
|
730
|
+
throw new TemplateError('Parameter is not opened');
|
|
731
|
+
}
|
|
732
|
+
return replacedTemplate;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/**
|
|
736
|
+
* Function isValidJsonString will tell you if the string is valid JSON or not
|
|
737
|
+
*/
|
|
738
|
+
function isValidJsonString(value) {
|
|
739
|
+
try {
|
|
740
|
+
JSON.parse(value);
|
|
741
|
+
return true;
|
|
742
|
+
}
|
|
743
|
+
catch (error) {
|
|
744
|
+
if (!(error instanceof Error)) {
|
|
745
|
+
throw error;
|
|
746
|
+
}
|
|
747
|
+
if (error.message.includes('Unexpected token')) {
|
|
748
|
+
return false;
|
|
749
|
+
}
|
|
750
|
+
return false;
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* Makes first letter of a string uppercase
|
|
756
|
+
*
|
|
757
|
+
*/
|
|
758
|
+
function capitalize(word) {
|
|
759
|
+
return word.substring(0, 1).toUpperCase() + word.substring(1);
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
/**
|
|
763
|
+
* Extracts all code blocks from markdown.
|
|
764
|
+
*
|
|
765
|
+
* Note: There are 3 simmilar function:
|
|
766
|
+
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
767
|
+
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
768
|
+
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
769
|
+
*
|
|
770
|
+
* @param markdown any valid markdown
|
|
771
|
+
* @returns code blocks with language and content
|
|
772
|
+
*
|
|
773
|
+
*/
|
|
774
|
+
function extractAllBlocksFromMarkdown(markdown) {
|
|
775
|
+
var e_1, _a;
|
|
776
|
+
var codeBlocks = [];
|
|
777
|
+
var lines = markdown.split('\n');
|
|
778
|
+
var currentCodeBlock = null;
|
|
779
|
+
try {
|
|
780
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
781
|
+
var line = lines_1_1.value;
|
|
782
|
+
if (line.startsWith('```')) {
|
|
783
|
+
var language = line.slice(3).trim() || null;
|
|
784
|
+
if (currentCodeBlock === null) {
|
|
785
|
+
currentCodeBlock = { language: language, content: '' };
|
|
786
|
+
}
|
|
787
|
+
else {
|
|
788
|
+
if (language !== null) {
|
|
789
|
+
// [๐ป]
|
|
790
|
+
throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
|
|
791
|
+
}
|
|
792
|
+
codeBlocks.push(currentCodeBlock);
|
|
793
|
+
currentCodeBlock = null;
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
else if (currentCodeBlock !== null) {
|
|
797
|
+
if (currentCodeBlock.content !== '') {
|
|
798
|
+
currentCodeBlock.content += '\n';
|
|
799
|
+
}
|
|
800
|
+
currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
805
|
+
finally {
|
|
806
|
+
try {
|
|
807
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
808
|
+
}
|
|
809
|
+
finally { if (e_1) throw e_1.error; }
|
|
810
|
+
}
|
|
811
|
+
if (currentCodeBlock !== null) {
|
|
812
|
+
// [๐ป]
|
|
813
|
+
throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
|
|
814
|
+
}
|
|
815
|
+
return codeBlocks;
|
|
816
|
+
}
|
|
987
817
|
|
|
988
818
|
/**
|
|
819
|
+
* Utility function to extract all list items from markdown
|
|
820
|
+
*
|
|
821
|
+
* Note: It works with both ul and ol
|
|
822
|
+
* Note: It omits list items in code blocks
|
|
823
|
+
* Note: It flattens nested lists
|
|
824
|
+
* Note: It can not work with html syntax and comments
|
|
989
825
|
*
|
|
826
|
+
* @param markdown any valid markdown
|
|
827
|
+
* @returns
|
|
990
828
|
*/
|
|
991
|
-
function
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
829
|
+
function extractAllListItemsFromMarkdown(markdown) {
|
|
830
|
+
var e_1, _a;
|
|
831
|
+
var lines = markdown.split('\n');
|
|
832
|
+
var listItems = [];
|
|
833
|
+
var isInCodeBlock = false;
|
|
834
|
+
try {
|
|
835
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
836
|
+
var line = lines_1_1.value;
|
|
837
|
+
var trimmedLine = line.trim();
|
|
838
|
+
if (trimmedLine.startsWith('```')) {
|
|
839
|
+
isInCodeBlock = !isInCodeBlock;
|
|
840
|
+
}
|
|
841
|
+
if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
|
|
842
|
+
var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
|
|
843
|
+
listItems.push(listItem);
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
848
|
+
finally {
|
|
849
|
+
try {
|
|
850
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
851
|
+
}
|
|
852
|
+
finally { if (e_1) throw e_1.error; }
|
|
853
|
+
}
|
|
854
|
+
return listItems;
|
|
996
855
|
}
|
|
997
856
|
|
|
998
857
|
/**
|
|
999
|
-
*
|
|
858
|
+
* Extracts exactly ONE code block from markdown.
|
|
859
|
+
*
|
|
860
|
+
* Note: If there are multiple or no code blocks the function throws an error
|
|
861
|
+
*
|
|
862
|
+
* Note: There are 3 simmilar function:
|
|
863
|
+
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
864
|
+
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
865
|
+
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
866
|
+
*
|
|
867
|
+
* @param markdown any valid markdown
|
|
868
|
+
* @returns code block with language and content
|
|
1000
869
|
*/
|
|
1001
|
-
function
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
870
|
+
function extractOneBlockFromMarkdown(markdown) {
|
|
871
|
+
var codeBlocks = extractAllBlocksFromMarkdown(markdown);
|
|
872
|
+
if (codeBlocks.length !== 1) {
|
|
873
|
+
// TODO: Report more specific place where the error happened
|
|
874
|
+
throw new Error(/* <- [๐ป] */ 'There should be exactly one code block in the markdown');
|
|
875
|
+
}
|
|
876
|
+
return codeBlocks[0];
|
|
1005
877
|
}
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
* Index of all counter functions
|
|
878
|
+
/***
|
|
879
|
+
* TODO: [๐ป] !!! Decide of this is internal util, external util OR validator/postprocessor
|
|
1009
880
|
*/
|
|
1010
|
-
var CountUtils = {
|
|
1011
|
-
CHARACTERS: countCharacters,
|
|
1012
|
-
WORDS: countWords,
|
|
1013
|
-
SENTENCES: countSentences,
|
|
1014
|
-
PARAGRAPHS: countParagraphs,
|
|
1015
|
-
LINES: countLines,
|
|
1016
|
-
PAGES: countPages,
|
|
1017
|
-
};
|
|
1018
881
|
|
|
1019
882
|
/**
|
|
1020
|
-
*
|
|
883
|
+
* Removes Markdown formatting tags from a string.
|
|
884
|
+
*
|
|
885
|
+
* @param {string} str - The string to remove Markdown tags from.
|
|
886
|
+
* @returns {string} The input string with all Markdown tags removed.
|
|
1021
887
|
*/
|
|
1022
|
-
function
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
}
|
|
1031
|
-
if (error.message.includes('Unexpected token')) {
|
|
1032
|
-
return false;
|
|
1033
|
-
}
|
|
1034
|
-
return false;
|
|
1035
|
-
}
|
|
888
|
+
function removeMarkdownFormatting(str) {
|
|
889
|
+
// Remove bold formatting
|
|
890
|
+
str = str.replace(/\*\*(.*?)\*\*/g, '$1');
|
|
891
|
+
// Remove italic formatting
|
|
892
|
+
str = str.replace(/\*(.*?)\*/g, '$1');
|
|
893
|
+
// Remove code formatting
|
|
894
|
+
str = str.replace(/`(.*?)`/g, '$1');
|
|
895
|
+
return str;
|
|
1036
896
|
}
|
|
1037
897
|
|
|
1038
898
|
/* tslint:disable */
|
|
@@ -1085,79 +945,298 @@
|
|
|
1085
945
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1086
946
|
finally {
|
|
1087
947
|
try {
|
|
1088
|
-
if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
|
|
948
|
+
if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
|
|
949
|
+
}
|
|
950
|
+
finally { if (e_1) throw e_1.error; }
|
|
951
|
+
}
|
|
952
|
+
normalizedName = normalizedName.replace(/_+/g, '_');
|
|
953
|
+
normalizedName = normalizedName.replace(/_?\/_?/g, '/');
|
|
954
|
+
normalizedName = normalizedName.replace(/^_/, '');
|
|
955
|
+
normalizedName = normalizedName.replace(/_$/, '');
|
|
956
|
+
return normalizedName;
|
|
957
|
+
}
|
|
958
|
+
/**
|
|
959
|
+
* TODO: [๐บ] Use some intermediate util splitWords
|
|
960
|
+
*/
|
|
961
|
+
|
|
962
|
+
/* tslint:disable */
|
|
963
|
+
function normalizeTo_camelCase(sentence, __firstLetterCapital) {
|
|
964
|
+
var e_1, _a;
|
|
965
|
+
if (__firstLetterCapital === void 0) { __firstLetterCapital = false; }
|
|
966
|
+
var charType;
|
|
967
|
+
var lastCharType = null;
|
|
968
|
+
var normalizedName = '';
|
|
969
|
+
try {
|
|
970
|
+
for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
|
|
971
|
+
var char = sentence_1_1.value;
|
|
972
|
+
var normalizedChar = void 0;
|
|
973
|
+
if (/^[a-z]$/.test(char)) {
|
|
974
|
+
charType = 'LOWERCASE';
|
|
975
|
+
normalizedChar = char;
|
|
976
|
+
}
|
|
977
|
+
else if (/^[A-Z]$/.test(char)) {
|
|
978
|
+
charType = 'UPPERCASE';
|
|
979
|
+
normalizedChar = char.toLowerCase();
|
|
980
|
+
}
|
|
981
|
+
else if (/^[0-9]$/.test(char)) {
|
|
982
|
+
charType = 'NUMBER';
|
|
983
|
+
normalizedChar = char;
|
|
984
|
+
}
|
|
985
|
+
else {
|
|
986
|
+
charType = 'OTHER';
|
|
987
|
+
normalizedChar = '';
|
|
988
|
+
}
|
|
989
|
+
if (!lastCharType) {
|
|
990
|
+
if (__firstLetterCapital) {
|
|
991
|
+
normalizedChar = normalizedChar.toUpperCase(); //TODO: DRY
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
else if (charType !== lastCharType &&
|
|
995
|
+
!(charType === 'LOWERCASE' && lastCharType === 'UPPERCASE') &&
|
|
996
|
+
!(lastCharType === 'NUMBER') &&
|
|
997
|
+
!(charType === 'NUMBER')) {
|
|
998
|
+
normalizedChar = normalizedChar.toUpperCase(); //TODO: [๐บ] DRY
|
|
999
|
+
}
|
|
1000
|
+
normalizedName += normalizedChar;
|
|
1001
|
+
lastCharType = charType;
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1005
|
+
finally {
|
|
1006
|
+
try {
|
|
1007
|
+
if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
|
|
1008
|
+
}
|
|
1009
|
+
finally { if (e_1) throw e_1.error; }
|
|
1010
|
+
}
|
|
1011
|
+
return normalizedName;
|
|
1012
|
+
}
|
|
1013
|
+
/**
|
|
1014
|
+
* TODO: [๐บ] Use some intermediate util splitWords
|
|
1015
|
+
*/
|
|
1016
|
+
|
|
1017
|
+
function normalizeTo_PascalCase(sentence) {
|
|
1018
|
+
return normalizeTo_camelCase(sentence, true);
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
/**
|
|
1022
|
+
* Supported script languages
|
|
1023
|
+
*/
|
|
1024
|
+
var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
|
|
1025
|
+
|
|
1026
|
+
/**
|
|
1027
|
+
* Parses the template and returns the list of all parameter names
|
|
1028
|
+
*
|
|
1029
|
+
* @param template the template with parameters in {curly} braces
|
|
1030
|
+
* @returns the list of parameter names
|
|
1031
|
+
*
|
|
1032
|
+
* @private within the library
|
|
1033
|
+
*/
|
|
1034
|
+
function extractParameters(template) {
|
|
1035
|
+
var e_1, _a;
|
|
1036
|
+
var matches = template.matchAll(/{\w+}/g);
|
|
1037
|
+
var parameterNames = [];
|
|
1038
|
+
try {
|
|
1039
|
+
for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
|
|
1040
|
+
var match = matches_1_1.value;
|
|
1041
|
+
var parameterName = match[0].slice(1, -1);
|
|
1042
|
+
if (!parameterNames.includes(parameterName)) {
|
|
1043
|
+
parameterNames.push(parameterName);
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1048
|
+
finally {
|
|
1049
|
+
try {
|
|
1050
|
+
if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
|
|
1051
|
+
}
|
|
1052
|
+
finally { if (e_1) throw e_1.error; }
|
|
1053
|
+
}
|
|
1054
|
+
return parameterNames;
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
/**
|
|
1058
|
+
* Computes the deepness of the markdown structure.
|
|
1059
|
+
*
|
|
1060
|
+
* @private within the library
|
|
1061
|
+
*/
|
|
1062
|
+
function countMarkdownStructureDeepness(markdownStructure) {
|
|
1063
|
+
var e_1, _a;
|
|
1064
|
+
var maxDeepness = 0;
|
|
1065
|
+
try {
|
|
1066
|
+
for (var _b = __values(markdownStructure.sections), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
1067
|
+
var section = _c.value;
|
|
1068
|
+
maxDeepness = Math.max(maxDeepness, countMarkdownStructureDeepness(section));
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1072
|
+
finally {
|
|
1073
|
+
try {
|
|
1074
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
1089
1075
|
}
|
|
1090
1076
|
finally { if (e_1) throw e_1.error; }
|
|
1091
1077
|
}
|
|
1092
|
-
|
|
1093
|
-
normalizedName = normalizedName.replace(/_?\/_?/g, '/');
|
|
1094
|
-
normalizedName = normalizedName.replace(/^_/, '');
|
|
1095
|
-
normalizedName = normalizedName.replace(/_$/, '');
|
|
1096
|
-
return normalizedName;
|
|
1078
|
+
return maxDeepness + 1;
|
|
1097
1079
|
}
|
|
1080
|
+
|
|
1098
1081
|
/**
|
|
1099
|
-
*
|
|
1082
|
+
* Parse a markdown string into a MarkdownStructure object.
|
|
1083
|
+
*
|
|
1084
|
+
* Note: This function does work with code blocks
|
|
1085
|
+
* Note: This function does not work with markdown comments
|
|
1086
|
+
*
|
|
1087
|
+
* @param markdown The markdown string to parse.
|
|
1088
|
+
* @returns The MarkdownStructure object.
|
|
1089
|
+
*
|
|
1090
|
+
* @private within the library
|
|
1100
1091
|
*/
|
|
1101
|
-
|
|
1102
|
-
/* tslint:disable */
|
|
1103
|
-
function normalizeTo_camelCase(sentence, __firstLetterCapital) {
|
|
1092
|
+
function markdownToMarkdownStructure(markdown) {
|
|
1104
1093
|
var e_1, _a;
|
|
1105
|
-
|
|
1106
|
-
var
|
|
1107
|
-
var
|
|
1108
|
-
var
|
|
1094
|
+
var lines = markdown.split('\n');
|
|
1095
|
+
var root = { level: 0, title: '', contentLines: [], sections: [], parent: null };
|
|
1096
|
+
var current = root;
|
|
1097
|
+
var isInsideCodeBlock = false;
|
|
1109
1098
|
try {
|
|
1110
|
-
for (var
|
|
1111
|
-
var
|
|
1112
|
-
var
|
|
1113
|
-
if (
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
charType = 'UPPERCASE';
|
|
1119
|
-
normalizedChar = char.toLowerCase();
|
|
1120
|
-
}
|
|
1121
|
-
else if (/^[0-9]$/.test(char)) {
|
|
1122
|
-
charType = 'NUMBER';
|
|
1123
|
-
normalizedChar = char;
|
|
1099
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
1100
|
+
var line = lines_1_1.value;
|
|
1101
|
+
var headingMatch = line.match(/^(?<mark>#{1,6})\s(?<title>.*)/);
|
|
1102
|
+
if (isInsideCodeBlock || !headingMatch) {
|
|
1103
|
+
if (line.startsWith('```')) {
|
|
1104
|
+
isInsideCodeBlock = !isInsideCodeBlock;
|
|
1105
|
+
}
|
|
1106
|
+
current.contentLines.push(line);
|
|
1124
1107
|
}
|
|
1125
1108
|
else {
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1109
|
+
var level = headingMatch.groups.mark.length;
|
|
1110
|
+
var title = headingMatch.groups.title.trim();
|
|
1111
|
+
var parent_1 = void 0;
|
|
1112
|
+
if (level > current.level) {
|
|
1113
|
+
// Note: Going deeper (next section is child of current)
|
|
1114
|
+
parent_1 = current;
|
|
1132
1115
|
}
|
|
1116
|
+
else {
|
|
1117
|
+
// Note: Going up or staying at the same level (next section is sibling or parent or grandparent,... of current)
|
|
1118
|
+
parent_1 = current;
|
|
1119
|
+
var loopLimit = LOOP_LIMIT;
|
|
1120
|
+
while (parent_1.level !== level - 1) {
|
|
1121
|
+
if (loopLimit-- < 0) {
|
|
1122
|
+
throw new UnexpectedError('Loop limit reached during parsing of markdown structure in `markdownToMarkdownStructure`');
|
|
1123
|
+
}
|
|
1124
|
+
if (parent_1.parent === null /* <- Note: We are in root */) {
|
|
1125
|
+
// [๐ป]
|
|
1126
|
+
throw new Error(spaceTrim__default["default"]("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
|
|
1127
|
+
}
|
|
1128
|
+
parent_1 = parent_1.parent;
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
|
|
1132
|
+
parent_1.sections.push(section);
|
|
1133
|
+
current = section;
|
|
1133
1134
|
}
|
|
1134
|
-
else if (charType !== lastCharType &&
|
|
1135
|
-
!(charType === 'LOWERCASE' && lastCharType === 'UPPERCASE') &&
|
|
1136
|
-
!(lastCharType === 'NUMBER') &&
|
|
1137
|
-
!(charType === 'NUMBER')) {
|
|
1138
|
-
normalizedChar = normalizedChar.toUpperCase(); //TODO: [๐บ] DRY
|
|
1139
|
-
}
|
|
1140
|
-
normalizedName += normalizedChar;
|
|
1141
|
-
lastCharType = charType;
|
|
1142
1135
|
}
|
|
1143
1136
|
}
|
|
1144
1137
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1145
1138
|
finally {
|
|
1146
1139
|
try {
|
|
1147
|
-
if (
|
|
1140
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
1148
1141
|
}
|
|
1149
1142
|
finally { if (e_1) throw e_1.error; }
|
|
1150
1143
|
}
|
|
1151
|
-
|
|
1144
|
+
if (root.sections.length === 1) {
|
|
1145
|
+
var markdownStructure = parsingMarkdownStructureToMarkdownStructure(root.sections[0]);
|
|
1146
|
+
return markdownStructure;
|
|
1147
|
+
}
|
|
1148
|
+
// [๐ป]
|
|
1149
|
+
throw new Error('The markdown file must have exactly one top-level section.');
|
|
1150
|
+
// return root;
|
|
1152
1151
|
}
|
|
1153
1152
|
/**
|
|
1154
|
-
*
|
|
1153
|
+
* @private
|
|
1155
1154
|
*/
|
|
1155
|
+
function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
|
|
1156
|
+
var level = parsingMarkdownStructure.level, title = parsingMarkdownStructure.title, contentLines = parsingMarkdownStructure.contentLines, sections = parsingMarkdownStructure.sections;
|
|
1157
|
+
return {
|
|
1158
|
+
level: level,
|
|
1159
|
+
title: title,
|
|
1160
|
+
content: spaceTrim__default["default"](contentLines.join('\n')),
|
|
1161
|
+
sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
|
|
1162
|
+
};
|
|
1163
|
+
}
|
|
1156
1164
|
|
|
1157
|
-
|
|
1158
|
-
|
|
1165
|
+
/**
|
|
1166
|
+
* The version of the Promptbook library
|
|
1167
|
+
*/
|
|
1168
|
+
var PROMPTBOOK_VERSION = '0.44.0-9';
|
|
1169
|
+
|
|
1170
|
+
/**
|
|
1171
|
+
* Parses the given script and returns the list of all used variables that are not defined in the script
|
|
1172
|
+
*
|
|
1173
|
+
* @param script from which to extract the variables
|
|
1174
|
+
* @returns the list of variable names
|
|
1175
|
+
* @throws {PromptbookSyntaxError} if the script is invalid
|
|
1176
|
+
*
|
|
1177
|
+
* @private within the promptbookStringToJson
|
|
1178
|
+
*/
|
|
1179
|
+
function extractVariables(script) {
|
|
1180
|
+
var variables = [];
|
|
1181
|
+
script = "(()=>{".concat(script, "})()");
|
|
1182
|
+
try {
|
|
1183
|
+
for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
|
|
1184
|
+
try {
|
|
1185
|
+
eval(script);
|
|
1186
|
+
}
|
|
1187
|
+
catch (error) {
|
|
1188
|
+
if (!(error instanceof ReferenceError)) {
|
|
1189
|
+
throw error;
|
|
1190
|
+
}
|
|
1191
|
+
var undefinedName = error.message.split(' ')[0];
|
|
1192
|
+
/*
|
|
1193
|
+
Note: Remapping error
|
|
1194
|
+
From: [ReferenceError: thing is not defined],
|
|
1195
|
+
To: [Error: Parameter {thing} is not defined],
|
|
1196
|
+
*/
|
|
1197
|
+
if (!undefinedName) {
|
|
1198
|
+
throw error;
|
|
1199
|
+
}
|
|
1200
|
+
if (script.includes(undefinedName + '(')) {
|
|
1201
|
+
script = "const ".concat(undefinedName, " = ()=>'';") + script;
|
|
1202
|
+
}
|
|
1203
|
+
else {
|
|
1204
|
+
variables.push(undefinedName);
|
|
1205
|
+
script = "const ".concat(undefinedName, " = '';") + script;
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
catch (error) {
|
|
1210
|
+
if (!(error instanceof Error)) {
|
|
1211
|
+
throw error;
|
|
1212
|
+
}
|
|
1213
|
+
throw new PromptbookSyntaxError(spaceTrim__default["default"](function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
|
|
1214
|
+
}
|
|
1215
|
+
return variables;
|
|
1159
1216
|
}
|
|
1160
1217
|
|
|
1218
|
+
/**
|
|
1219
|
+
* Execution type describes the way how the block is executed
|
|
1220
|
+
*
|
|
1221
|
+
* @see https://github.com/webgptorg/promptbook#execution-type
|
|
1222
|
+
*/
|
|
1223
|
+
var ExecutionTypes = [
|
|
1224
|
+
'PROMPT_TEMPLATE',
|
|
1225
|
+
'SIMPLE_TEMPLATE',
|
|
1226
|
+
'SCRIPT',
|
|
1227
|
+
'PROMPT_DIALOG',
|
|
1228
|
+
// <- [๐ฅป] Insert here when making new command
|
|
1229
|
+
];
|
|
1230
|
+
|
|
1231
|
+
/**
|
|
1232
|
+
* Units of text measurement
|
|
1233
|
+
*/
|
|
1234
|
+
var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'PARAGRAPHS', 'LINES', 'PAGES'];
|
|
1235
|
+
/**
|
|
1236
|
+
* TODO: [๐] Unite object for expecting amount and format - remove expectFormat
|
|
1237
|
+
* TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
|
|
1238
|
+
*/
|
|
1239
|
+
|
|
1161
1240
|
/**
|
|
1162
1241
|
* Parses one line of ul/ol to command
|
|
1163
1242
|
*
|
|
@@ -1651,7 +1730,7 @@
|
|
|
1651
1730
|
executionType: executionType,
|
|
1652
1731
|
jokers: jokers,
|
|
1653
1732
|
postprocessing: postprocessing,
|
|
1654
|
-
|
|
1733
|
+
expectations: expectAmount,
|
|
1655
1734
|
expectFormat: expectFormat,
|
|
1656
1735
|
modelRequirements: templateModelRequirements,
|
|
1657
1736
|
contentLanguage: executionType === 'SCRIPT' ? language : undefined,
|
|
@@ -1793,7 +1872,7 @@
|
|
|
1793
1872
|
}
|
|
1794
1873
|
if (template.jokers && template.jokers.length > 0) {
|
|
1795
1874
|
if (!template.expectFormat &&
|
|
1796
|
-
!template.
|
|
1875
|
+
!template.expectations /* <- TODO: Require at least 1 -> min <- expectation to use jokers */) {
|
|
1797
1876
|
throw new PromptbookLogicError("Joker parameters are used for {".concat(template.resultingParameterName, "} but no expectations are defined"));
|
|
1798
1877
|
}
|
|
1799
1878
|
try {
|
|
@@ -1812,9 +1891,9 @@
|
|
|
1812
1891
|
finally { if (e_3) throw e_3.error; }
|
|
1813
1892
|
}
|
|
1814
1893
|
}
|
|
1815
|
-
if (template.
|
|
1894
|
+
if (template.expectations) {
|
|
1816
1895
|
try {
|
|
1817
|
-
for (var _l = (e_4 = void 0, __values(Object.entries(template.
|
|
1896
|
+
for (var _l = (e_4 = void 0, __values(Object.entries(template.expectations))), _m = _l.next(); !_m.done; _m = _l.next()) {
|
|
1818
1897
|
var _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], min = _p.min, max = _p.max;
|
|
1819
1898
|
if (min !== undefined && max !== undefined && min > max) {
|
|
1820
1899
|
throw new PromptbookLogicError("Min expectation (=".concat(min, ") of ").concat(unit, " is higher than max expectation (=").concat(max, ")"));
|
|
@@ -1895,23 +1974,6 @@
|
|
|
1895
1974
|
* > ex port function validatePromptbookJson(promptbook: unknown): asserts promptbook is PromptbookJson {
|
|
1896
1975
|
*/
|
|
1897
1976
|
|
|
1898
|
-
/**
|
|
1899
|
-
* This error occurs when some expectation is not met in the execution of the pipeline
|
|
1900
|
-
*
|
|
1901
|
-
* @private Always catched and rethrown as `PromptbookExecutionError`
|
|
1902
|
-
* Note: This is a kindof subtype of PromptbookExecutionError
|
|
1903
|
-
*/
|
|
1904
|
-
var ExpectError = /** @class */ (function (_super) {
|
|
1905
|
-
__extends(ExpectError, _super);
|
|
1906
|
-
function ExpectError(message) {
|
|
1907
|
-
var _this = _super.call(this, message) || this;
|
|
1908
|
-
_this.name = 'ExpectError';
|
|
1909
|
-
Object.setPrototypeOf(_this, ExpectError.prototype);
|
|
1910
|
-
return _this;
|
|
1911
|
-
}
|
|
1912
|
-
return ExpectError;
|
|
1913
|
-
}(Error));
|
|
1914
|
-
|
|
1915
1977
|
/**
|
|
1916
1978
|
* Creates executor function from promptbook and execution tools.
|
|
1917
1979
|
*
|
|
@@ -1926,10 +1988,10 @@
|
|
|
1926
1988
|
var promptbookExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
|
|
1927
1989
|
function executeSingleTemplate(currentTemplate) {
|
|
1928
1990
|
return __awaiter(this, void 0, void 0, function () {
|
|
1929
|
-
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,
|
|
1930
|
-
var e_2,
|
|
1931
|
-
return __generator(this, function (
|
|
1932
|
-
switch (
|
|
1991
|
+
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;
|
|
1992
|
+
var e_2, _j, e_4, _k, e_3, _l, _m;
|
|
1993
|
+
return __generator(this, function (_o) {
|
|
1994
|
+
switch (_o.label) {
|
|
1933
1995
|
case 0:
|
|
1934
1996
|
name = "promptbook-executor-frame-".concat(currentTemplate.name);
|
|
1935
1997
|
title = currentTemplate.title;
|
|
@@ -1946,8 +2008,8 @@
|
|
|
1946
2008
|
// <- [3]
|
|
1947
2009
|
})];
|
|
1948
2010
|
case 1:
|
|
1949
|
-
|
|
1950
|
-
|
|
2011
|
+
_o.sent();
|
|
2012
|
+
_o.label = 2;
|
|
1951
2013
|
case 2:
|
|
1952
2014
|
result = null;
|
|
1953
2015
|
resultString = null;
|
|
@@ -1955,7 +2017,7 @@
|
|
|
1955
2017
|
maxAttempts = currentTemplate.executionType === 'PROMPT_DIALOG' ? Infinity : maxExecutionAttempts;
|
|
1956
2018
|
jokers = currentTemplate.jokers || [];
|
|
1957
2019
|
attempt = -jokers.length;
|
|
1958
|
-
|
|
2020
|
+
_o.label = 3;
|
|
1959
2021
|
case 3:
|
|
1960
2022
|
if (!(attempt < maxAttempts)) return [3 /*break*/, 49];
|
|
1961
2023
|
isJokerAttempt = attempt < 0;
|
|
@@ -1972,9 +2034,9 @@
|
|
|
1972
2034
|
}
|
|
1973
2035
|
resultString = parametersToPass[joker];
|
|
1974
2036
|
}
|
|
1975
|
-
|
|
2037
|
+
_o.label = 4;
|
|
1976
2038
|
case 4:
|
|
1977
|
-
|
|
2039
|
+
_o.trys.push([4, 45, 46, 47]);
|
|
1978
2040
|
if (!!isJokerAttempt) return [3 /*break*/, 27];
|
|
1979
2041
|
_a = currentTemplate.executionType;
|
|
1980
2042
|
switch (_a) {
|
|
@@ -1996,6 +2058,7 @@
|
|
|
1996
2058
|
parameters: parametersToPass,
|
|
1997
2059
|
content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
|
|
1998
2060
|
modelRequirements: currentTemplate.modelRequirements,
|
|
2061
|
+
expectations: currentTemplate.expectations,
|
|
1999
2062
|
};
|
|
2000
2063
|
_b = currentTemplate.modelRequirements.modelVariant;
|
|
2001
2064
|
switch (_b) {
|
|
@@ -2005,14 +2068,14 @@
|
|
|
2005
2068
|
return [3 /*break*/, 11];
|
|
2006
2069
|
case 7: return [4 /*yield*/, tools.natural.gptChat(prompt)];
|
|
2007
2070
|
case 8:
|
|
2008
|
-
chatThread =
|
|
2071
|
+
chatThread = _o.sent();
|
|
2009
2072
|
// TODO: [๐ฌ] Destroy chatThread
|
|
2010
2073
|
result = chatThread;
|
|
2011
2074
|
resultString = chatThread.content;
|
|
2012
2075
|
return [3 /*break*/, 12];
|
|
2013
2076
|
case 9: return [4 /*yield*/, tools.natural.gptComplete(prompt)];
|
|
2014
2077
|
case 10:
|
|
2015
|
-
completionResult =
|
|
2078
|
+
completionResult = _o.sent();
|
|
2016
2079
|
result = completionResult;
|
|
2017
2080
|
resultString = completionResult.content;
|
|
2018
2081
|
return [3 /*break*/, 12];
|
|
@@ -2027,27 +2090,27 @@
|
|
|
2027
2090
|
}
|
|
2028
2091
|
// TODO: DRY [1]
|
|
2029
2092
|
scriptExecutionErrors = [];
|
|
2030
|
-
|
|
2093
|
+
_o.label = 14;
|
|
2031
2094
|
case 14:
|
|
2032
|
-
|
|
2095
|
+
_o.trys.push([14, 21, 22, 23]);
|
|
2033
2096
|
_c = (e_2 = void 0, __values(tools.script)), _d = _c.next();
|
|
2034
|
-
|
|
2097
|
+
_o.label = 15;
|
|
2035
2098
|
case 15:
|
|
2036
2099
|
if (!!_d.done) return [3 /*break*/, 20];
|
|
2037
2100
|
scriptTools = _d.value;
|
|
2038
|
-
|
|
2101
|
+
_o.label = 16;
|
|
2039
2102
|
case 16:
|
|
2040
|
-
|
|
2103
|
+
_o.trys.push([16, 18, , 19]);
|
|
2041
2104
|
return [4 /*yield*/, scriptTools.execute({
|
|
2042
2105
|
scriptLanguage: currentTemplate.contentLanguage,
|
|
2043
2106
|
script: currentTemplate.content,
|
|
2044
2107
|
parameters: parametersToPass,
|
|
2045
2108
|
})];
|
|
2046
2109
|
case 17:
|
|
2047
|
-
resultString =
|
|
2110
|
+
resultString = _o.sent();
|
|
2048
2111
|
return [3 /*break*/, 20];
|
|
2049
2112
|
case 18:
|
|
2050
|
-
error_2 =
|
|
2113
|
+
error_2 = _o.sent();
|
|
2051
2114
|
if (!(error_2 instanceof Error)) {
|
|
2052
2115
|
throw error_2;
|
|
2053
2116
|
}
|
|
@@ -2058,12 +2121,12 @@
|
|
|
2058
2121
|
return [3 /*break*/, 15];
|
|
2059
2122
|
case 20: return [3 /*break*/, 23];
|
|
2060
2123
|
case 21:
|
|
2061
|
-
e_2_1 =
|
|
2124
|
+
e_2_1 = _o.sent();
|
|
2062
2125
|
e_2 = { error: e_2_1 };
|
|
2063
2126
|
return [3 /*break*/, 23];
|
|
2064
2127
|
case 22:
|
|
2065
2128
|
try {
|
|
2066
|
-
if (_d && !_d.done && (
|
|
2129
|
+
if (_d && !_d.done && (_j = _c.return)) _j.call(_c);
|
|
2067
2130
|
}
|
|
2068
2131
|
finally { if (e_2) throw e_2.error; }
|
|
2069
2132
|
return [7 /*endfinally*/];
|
|
@@ -2089,46 +2152,46 @@
|
|
|
2089
2152
|
})];
|
|
2090
2153
|
case 25:
|
|
2091
2154
|
// TODO: [๐น] When making next attempt for `PROMPT DIALOG`, preserve the previous user input
|
|
2092
|
-
resultString =
|
|
2155
|
+
resultString = _o.sent();
|
|
2093
2156
|
return [3 /*break*/, 27];
|
|
2094
2157
|
case 26: throw new PromptbookExecutionError(
|
|
2095
2158
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2096
2159
|
"Unknown execution type \"".concat(currentTemplate.executionType, "\""));
|
|
2097
2160
|
case 27:
|
|
2098
2161
|
if (!(!isJokerAttempt && currentTemplate.postprocessing)) return [3 /*break*/, 44];
|
|
2099
|
-
|
|
2162
|
+
_o.label = 28;
|
|
2100
2163
|
case 28:
|
|
2101
|
-
|
|
2164
|
+
_o.trys.push([28, 42, 43, 44]);
|
|
2102
2165
|
_e = (e_4 = void 0, __values(currentTemplate.postprocessing)), _f = _e.next();
|
|
2103
|
-
|
|
2166
|
+
_o.label = 29;
|
|
2104
2167
|
case 29:
|
|
2105
2168
|
if (!!_f.done) return [3 /*break*/, 41];
|
|
2106
2169
|
functionName = _f.value;
|
|
2107
2170
|
// TODO: DRY [1]
|
|
2108
2171
|
scriptExecutionErrors = [];
|
|
2109
2172
|
postprocessingError = null;
|
|
2110
|
-
|
|
2173
|
+
_o.label = 30;
|
|
2111
2174
|
case 30:
|
|
2112
|
-
|
|
2175
|
+
_o.trys.push([30, 37, 38, 39]);
|
|
2113
2176
|
_g = (e_3 = void 0, __values(tools.script)), _h = _g.next();
|
|
2114
|
-
|
|
2177
|
+
_o.label = 31;
|
|
2115
2178
|
case 31:
|
|
2116
2179
|
if (!!_h.done) return [3 /*break*/, 36];
|
|
2117
2180
|
scriptTools = _h.value;
|
|
2118
|
-
|
|
2181
|
+
_o.label = 32;
|
|
2119
2182
|
case 32:
|
|
2120
|
-
|
|
2183
|
+
_o.trys.push([32, 34, , 35]);
|
|
2121
2184
|
return [4 /*yield*/, scriptTools.execute({
|
|
2122
2185
|
scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
|
|
2123
2186
|
script: "".concat(functionName, "(resultString)"),
|
|
2124
2187
|
parameters: __assign(__assign({}, parametersToPass), { resultString: resultString || '' }),
|
|
2125
2188
|
})];
|
|
2126
2189
|
case 33:
|
|
2127
|
-
resultString =
|
|
2190
|
+
resultString = _o.sent();
|
|
2128
2191
|
postprocessingError = null;
|
|
2129
2192
|
return [3 /*break*/, 36];
|
|
2130
2193
|
case 34:
|
|
2131
|
-
error_3 =
|
|
2194
|
+
error_3 = _o.sent();
|
|
2132
2195
|
if (!(error_3 instanceof Error)) {
|
|
2133
2196
|
throw error_3;
|
|
2134
2197
|
}
|
|
@@ -2140,12 +2203,12 @@
|
|
|
2140
2203
|
return [3 /*break*/, 31];
|
|
2141
2204
|
case 36: return [3 /*break*/, 39];
|
|
2142
2205
|
case 37:
|
|
2143
|
-
e_3_1 =
|
|
2206
|
+
e_3_1 = _o.sent();
|
|
2144
2207
|
e_3 = { error: e_3_1 };
|
|
2145
2208
|
return [3 /*break*/, 39];
|
|
2146
2209
|
case 38:
|
|
2147
2210
|
try {
|
|
2148
|
-
if (_h && !_h.done && (
|
|
2211
|
+
if (_h && !_h.done && (_l = _g.return)) _l.call(_g);
|
|
2149
2212
|
}
|
|
2150
2213
|
finally { if (e_3) throw e_3.error; }
|
|
2151
2214
|
return [7 /*endfinally*/];
|
|
@@ -2153,22 +2216,23 @@
|
|
|
2153
2216
|
if (postprocessingError) {
|
|
2154
2217
|
throw postprocessingError;
|
|
2155
2218
|
}
|
|
2156
|
-
|
|
2219
|
+
_o.label = 40;
|
|
2157
2220
|
case 40:
|
|
2158
2221
|
_f = _e.next();
|
|
2159
2222
|
return [3 /*break*/, 29];
|
|
2160
2223
|
case 41: return [3 /*break*/, 44];
|
|
2161
2224
|
case 42:
|
|
2162
|
-
e_4_1 =
|
|
2225
|
+
e_4_1 = _o.sent();
|
|
2163
2226
|
e_4 = { error: e_4_1 };
|
|
2164
2227
|
return [3 /*break*/, 44];
|
|
2165
2228
|
case 43:
|
|
2166
2229
|
try {
|
|
2167
|
-
if (_f && !_f.done && (
|
|
2230
|
+
if (_f && !_f.done && (_k = _e.return)) _k.call(_e);
|
|
2168
2231
|
}
|
|
2169
2232
|
finally { if (e_4) throw e_4.error; }
|
|
2170
2233
|
return [7 /*endfinally*/];
|
|
2171
2234
|
case 44:
|
|
2235
|
+
// TODO: [๐] Unite object for expecting amount and format
|
|
2172
2236
|
if (currentTemplate.expectFormat) {
|
|
2173
2237
|
if (currentTemplate.expectFormat === 'JSON') {
|
|
2174
2238
|
if (!isValidJsonString(resultString || '')) {
|
|
@@ -2176,30 +2240,13 @@
|
|
|
2176
2240
|
}
|
|
2177
2241
|
}
|
|
2178
2242
|
}
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
_l = __read(_k.value, 2), unit = _l[0], _m = _l[1], max = _m.max, min = _m.min;
|
|
2183
|
-
amount = CountUtils[unit.toUpperCase()](resultString || '');
|
|
2184
|
-
if (min && amount < min) {
|
|
2185
|
-
throw new ExpectError("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
|
|
2186
|
-
} /* not else */
|
|
2187
|
-
if (max && amount > max) {
|
|
2188
|
-
throw new ExpectError("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
|
|
2189
|
-
}
|
|
2190
|
-
}
|
|
2191
|
-
}
|
|
2192
|
-
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
2193
|
-
finally {
|
|
2194
|
-
try {
|
|
2195
|
-
if (_k && !_k.done && (_r = _j.return)) _r.call(_j);
|
|
2196
|
-
}
|
|
2197
|
-
finally { if (e_5) throw e_5.error; }
|
|
2198
|
-
}
|
|
2243
|
+
// TODO: [๐] Unite object for expecting amount and format
|
|
2244
|
+
if (currentTemplate.expectations) {
|
|
2245
|
+
checkExpectations(currentTemplate.expectations, resultString || '');
|
|
2199
2246
|
}
|
|
2200
2247
|
return [3 /*break*/, 49];
|
|
2201
2248
|
case 45:
|
|
2202
|
-
error_4 =
|
|
2249
|
+
error_4 = _o.sent();
|
|
2203
2250
|
if (!(error_4 instanceof ExpectError)) {
|
|
2204
2251
|
throw error_4;
|
|
2205
2252
|
}
|
|
@@ -2218,6 +2265,7 @@
|
|
|
2218
2265
|
title: currentTemplate.title /* <- Note: If title in promptbook contains emojis, pass it innto report */,
|
|
2219
2266
|
content: prompt.content,
|
|
2220
2267
|
modelRequirements: prompt.modelRequirements,
|
|
2268
|
+
expectations: prompt.expectations,
|
|
2221
2269
|
// <- Note: Do want to pass ONLY wanted information to the report
|
|
2222
2270
|
},
|
|
2223
2271
|
result: result || undefined,
|
|
@@ -2227,9 +2275,9 @@
|
|
|
2227
2275
|
return [7 /*endfinally*/];
|
|
2228
2276
|
case 47:
|
|
2229
2277
|
if (expectError !== null && attempt === maxAttempts - 1) {
|
|
2230
|
-
throw new PromptbookExecutionError(spaceTrim__default["default"](function (block) { return "\n Natural execution failed ".concat(maxExecutionAttempts, "x\n\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n "); }));
|
|
2278
|
+
throw new PromptbookExecutionError(spaceTrim__default["default"](function (block) { return "\n Natural execution failed ".concat(maxExecutionAttempts, "x\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 "); }));
|
|
2231
2279
|
}
|
|
2232
|
-
|
|
2280
|
+
_o.label = 48;
|
|
2233
2281
|
case 48:
|
|
2234
2282
|
attempt++;
|
|
2235
2283
|
return [3 /*break*/, 3];
|
|
@@ -2249,7 +2297,7 @@
|
|
|
2249
2297
|
// <- [3]
|
|
2250
2298
|
});
|
|
2251
2299
|
}
|
|
2252
|
-
parametersToPass = __assign(__assign({}, parametersToPass), (
|
|
2300
|
+
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));
|
|
2253
2301
|
return [2 /*return*/];
|
|
2254
2302
|
}
|
|
2255
2303
|
});
|
|
@@ -2453,8 +2501,94 @@
|
|
|
2453
2501
|
return MockedEchoNaturalExecutionTools;
|
|
2454
2502
|
}());
|
|
2455
2503
|
/**
|
|
2456
|
-
* TODO: Allow in spaceTrim: nesting with > ${block(prompt.request)}
|
|
2504
|
+
* TODO: Allow in spaceTrim: nesting with > ${block(prompt.request)}, same as replace params
|
|
2505
|
+
*/
|
|
2506
|
+
|
|
2507
|
+
/**
|
|
2508
|
+
* Gets the expectations and creates a fake text that meets the expectations
|
|
2509
|
+
*
|
|
2510
|
+
* @private internal util for MockedFackedNaturalExecutionTools
|
|
2511
|
+
*/
|
|
2512
|
+
function $fakeTextToExpectations(expectations) {
|
|
2513
|
+
var lorem = new loremIpsum.LoremIpsum({
|
|
2514
|
+
wordsPerSentence: { min: 5, max: 15 },
|
|
2515
|
+
sentencesPerParagraph: { min: 5, max: 15 },
|
|
2516
|
+
});
|
|
2517
|
+
var loremText = '';
|
|
2518
|
+
var text = '';
|
|
2519
|
+
for (var loopLimit = CHARACTER_LOOP_LIMIT; loopLimit-- > 0;) {
|
|
2520
|
+
if (isPassingExpectations(expectations, text)) {
|
|
2521
|
+
return text;
|
|
2522
|
+
}
|
|
2523
|
+
if (loremText === '') {
|
|
2524
|
+
loremText = lorem.generateParagraphs(1) + '\n\n';
|
|
2525
|
+
}
|
|
2526
|
+
text += loremText.substring(0, 1);
|
|
2527
|
+
loremText = loremText.substring(1);
|
|
2528
|
+
}
|
|
2529
|
+
throw new Error(spaceTrim__default["default"](function (block) { return "\n Can not generate fake text to met the expectations\n\n Loop limit reached\n The expectations:\n ".concat(block(JSON.stringify(expectations, null, 4)), "\n\n The draft text:\n ").concat(block(text), "\n\n "); }));
|
|
2530
|
+
}
|
|
2531
|
+
/**
|
|
2532
|
+
* TODO: Implement better
|
|
2533
|
+
* TODO: [๐] Unite object for expecting amount and format - use here also a format
|
|
2534
|
+
*/
|
|
2535
|
+
|
|
2536
|
+
/**
|
|
2537
|
+
* Mocked execution Tools for just faking expected responses for testing purposes
|
|
2457
2538
|
*/
|
|
2539
|
+
var MockedFackedNaturalExecutionTools = /** @class */ (function () {
|
|
2540
|
+
function MockedFackedNaturalExecutionTools(options) {
|
|
2541
|
+
this.options = options;
|
|
2542
|
+
}
|
|
2543
|
+
/**
|
|
2544
|
+
* Fakes chat model
|
|
2545
|
+
*/
|
|
2546
|
+
MockedFackedNaturalExecutionTools.prototype.gptChat = function (prompt) {
|
|
2547
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
2548
|
+
var content, result;
|
|
2549
|
+
return __generator(this, function (_a) {
|
|
2550
|
+
if (this.options.isVerbose) {
|
|
2551
|
+
console.info('๐ฌ Mocked faked prompt', prompt);
|
|
2552
|
+
}
|
|
2553
|
+
content = $fakeTextToExpectations(prompt.expectations || {
|
|
2554
|
+
sentences: { min: 1, max: 1 },
|
|
2555
|
+
});
|
|
2556
|
+
result = {
|
|
2557
|
+
content: content,
|
|
2558
|
+
model: 'mocked-facked',
|
|
2559
|
+
timing: {
|
|
2560
|
+
start: getCurrentIsoDate(),
|
|
2561
|
+
complete: getCurrentIsoDate(),
|
|
2562
|
+
},
|
|
2563
|
+
usage: {
|
|
2564
|
+
price: 0,
|
|
2565
|
+
inputTokens: 0,
|
|
2566
|
+
outputTokens: 0,
|
|
2567
|
+
},
|
|
2568
|
+
rawResponse: {
|
|
2569
|
+
note: 'This is mocked echo',
|
|
2570
|
+
},
|
|
2571
|
+
// <- [๐คนโโ๏ธ]
|
|
2572
|
+
};
|
|
2573
|
+
if (this.options.isVerbose) {
|
|
2574
|
+
console.info('๐ฌ Mocked faked result', result);
|
|
2575
|
+
}
|
|
2576
|
+
return [2 /*return*/, result];
|
|
2577
|
+
});
|
|
2578
|
+
});
|
|
2579
|
+
};
|
|
2580
|
+
/**
|
|
2581
|
+
* Fakes completion model
|
|
2582
|
+
*/
|
|
2583
|
+
MockedFackedNaturalExecutionTools.prototype.gptComplete = function (prompt) {
|
|
2584
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
2585
|
+
return __generator(this, function (_a) {
|
|
2586
|
+
return [2 /*return*/, this.gptChat(prompt)];
|
|
2587
|
+
});
|
|
2588
|
+
});
|
|
2589
|
+
};
|
|
2590
|
+
return MockedFackedNaturalExecutionTools;
|
|
2591
|
+
}());
|
|
2458
2592
|
|
|
2459
2593
|
/**
|
|
2460
2594
|
* Delagates the user interaction to a async callback function
|
|
@@ -2802,6 +2936,7 @@
|
|
|
2802
2936
|
exports.CallbackInterfaceTools = CallbackInterfaceTools;
|
|
2803
2937
|
exports.ExecutionTypes = ExecutionTypes;
|
|
2804
2938
|
exports.MockedEchoNaturalExecutionTools = MockedEchoNaturalExecutionTools;
|
|
2939
|
+
exports.MockedFackedNaturalExecutionTools = MockedFackedNaturalExecutionTools;
|
|
2805
2940
|
exports.PROMPTBOOK_VERSION = PROMPTBOOK_VERSION;
|
|
2806
2941
|
exports.SimplePromptInterfaceTools = SimplePromptInterfaceTools;
|
|
2807
2942
|
exports.SimplePromptbookLibrary = SimplePromptbookLibrary;
|