@promptbook/core 0.44.0-1 โ 0.44.0-11
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 +940 -756
- 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 +15 -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/plugins/script-execution-tools/javascript/JavascriptExecutionToolsOptions.d.ts +5 -1
- 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 +13 -0
- package/esm/typings/types/PromptbookJson/PromptTemplateJson.d.ts +14 -4
- package/package.json +2 -1
- package/umd/index.umd.js +983 -803
- 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 +15 -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/plugins/script-execution-tools/javascript/JavascriptExecutionToolsOptions.d.ts +5 -1
- 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 +13 -0
- package/umd/typings/types/PromptbookJson/PromptTemplateJson.d.ts +14 -4
package/umd/index.umd.js
CHANGED
|
@@ -1,12 +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.
|
|
5
|
-
})(this, (function (exports,
|
|
6
|
-
|
|
7
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
-
|
|
9
|
-
var spaceTrim__default = /*#__PURE__*/_interopDefaultLegacy(spaceTrim);
|
|
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';
|
|
10
6
|
|
|
11
7
|
/*! *****************************************************************************
|
|
12
8
|
Copyright (c) Microsoft Corporation.
|
|
@@ -127,6 +123,30 @@
|
|
|
127
123
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
128
124
|
}
|
|
129
125
|
|
|
126
|
+
/**
|
|
127
|
+
* This error type indicates that the error should not happen and its last check before crashing with some other error
|
|
128
|
+
*/
|
|
129
|
+
var UnexpectedError = /** @class */ (function (_super) {
|
|
130
|
+
__extends(UnexpectedError, _super);
|
|
131
|
+
function UnexpectedError(message) {
|
|
132
|
+
var _this = _super.call(this, spacetrim.spaceTrim(function (block) { return "\n ".concat(block(message), "\n\n Note: This error should not happen.\n It's probbably a bug in the promptbook library\n\n Please report issue:\n https://github.com/webgptorg/promptbook/issues\n\n Or contact us on me@pavolhejny.com\n\n "); })) || this;
|
|
133
|
+
_this.name = 'UnexpectedError';
|
|
134
|
+
Object.setPrototypeOf(_this, UnexpectedError.prototype);
|
|
135
|
+
return _this;
|
|
136
|
+
}
|
|
137
|
+
return UnexpectedError;
|
|
138
|
+
}(Error));
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Removes HTML or Markdown comments from a string.
|
|
142
|
+
*
|
|
143
|
+
* @param {string} content - The string to remove comments from.
|
|
144
|
+
* @returns {string} The input string with all comments removed.
|
|
145
|
+
*/
|
|
146
|
+
function removeContentComments(content) {
|
|
147
|
+
return spacetrim.spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
|
|
148
|
+
}
|
|
149
|
+
|
|
130
150
|
/**
|
|
131
151
|
* This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
|
|
132
152
|
*/
|
|
@@ -142,604 +162,154 @@
|
|
|
142
162
|
}(Error));
|
|
143
163
|
|
|
144
164
|
/**
|
|
145
|
-
*
|
|
146
|
-
*/
|
|
147
|
-
var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Parses the template and returns the list of all parameter names
|
|
165
|
+
* Function parseNumber will parse number from string
|
|
151
166
|
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
167
|
+
* Unlike Number.parseInt, Number.parseFloat it will never ever result in NaN
|
|
168
|
+
* Note: it also works only with decimal numbers
|
|
154
169
|
*
|
|
155
|
-
* @
|
|
170
|
+
* @returns parsed number
|
|
171
|
+
* @throws {PromptbookSyntaxError} if the value is not a number
|
|
172
|
+
*
|
|
173
|
+
* @private within the parseCommand
|
|
156
174
|
*/
|
|
157
|
-
function
|
|
158
|
-
var
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
}
|
|
175
|
+
function parseNumber(value) {
|
|
176
|
+
var originalValue = value;
|
|
177
|
+
if (typeof value === 'number') {
|
|
178
|
+
value = value.toString(); // <- TODO: Maybe more efficient way to do this
|
|
169
179
|
}
|
|
170
|
-
|
|
171
|
-
|
|
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; }
|
|
180
|
+
if (typeof value !== 'string') {
|
|
181
|
+
return 0;
|
|
176
182
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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));
|
|
183
|
+
value = value.trim();
|
|
184
|
+
if (value.startsWith('+')) {
|
|
185
|
+
return parseNumber(value.substring(1));
|
|
186
|
+
}
|
|
187
|
+
if (value.startsWith('-')) {
|
|
188
|
+
var number = parseNumber(value.substring(1));
|
|
189
|
+
if (number === 0) {
|
|
190
|
+
return 0; // <- Note: To prevent -0
|
|
192
191
|
}
|
|
192
|
+
return -number;
|
|
193
193
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
194
|
+
value = value.replace(/,/g, '.');
|
|
195
|
+
value = value.toUpperCase();
|
|
196
|
+
if (value === '') {
|
|
197
|
+
return 0;
|
|
198
|
+
}
|
|
199
|
+
if (value === 'โพ' || value.startsWith('INF')) {
|
|
200
|
+
return Infinity;
|
|
201
|
+
}
|
|
202
|
+
if (value.includes('/')) {
|
|
203
|
+
var _a = __read(value.split('/'), 2), numerator_ = _a[0], denominator_ = _a[1];
|
|
204
|
+
var numerator = parseNumber(numerator_);
|
|
205
|
+
var denominator = parseNumber(denominator_);
|
|
206
|
+
if (denominator === 0) {
|
|
207
|
+
throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\" because denominator is zero"));
|
|
198
208
|
}
|
|
199
|
-
|
|
209
|
+
return numerator / denominator;
|
|
200
210
|
}
|
|
201
|
-
|
|
211
|
+
if (/^(NAN|NULL|NONE|UNDEFINED|ZERO|NO.*)$/.test(value)) {
|
|
212
|
+
return 0;
|
|
213
|
+
}
|
|
214
|
+
if (value.includes('E')) {
|
|
215
|
+
var _b = __read(value.split('E'), 2), significand = _b[0], exponent = _b[1];
|
|
216
|
+
return parseNumber(significand) * Math.pow(10, parseNumber(exponent));
|
|
217
|
+
}
|
|
218
|
+
if (!/^[0-9.]+$/.test(value) || value.split('.').length > 2) {
|
|
219
|
+
throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\""));
|
|
220
|
+
}
|
|
221
|
+
var num = parseFloat(value);
|
|
222
|
+
if (isNaN(num)) {
|
|
223
|
+
throw new PromptbookSyntaxError("Unexpected NaN when parsing number from \"".concat(originalValue, "\""));
|
|
224
|
+
}
|
|
225
|
+
return num;
|
|
202
226
|
}
|
|
203
|
-
|
|
204
227
|
/**
|
|
205
|
-
*
|
|
228
|
+
* TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
|
|
206
229
|
*/
|
|
207
|
-
var LOOP_LIMIT = 1000;
|
|
208
230
|
|
|
209
231
|
/**
|
|
210
|
-
* This error
|
|
232
|
+
* This error indicates errors during the execution of the promptbook
|
|
211
233
|
*/
|
|
212
|
-
var
|
|
213
|
-
__extends(
|
|
214
|
-
function
|
|
215
|
-
var _this = _super.call(this,
|
|
216
|
-
_this.name = '
|
|
217
|
-
Object.setPrototypeOf(_this,
|
|
234
|
+
var PromptbookExecutionError = /** @class */ (function (_super) {
|
|
235
|
+
__extends(PromptbookExecutionError, _super);
|
|
236
|
+
function PromptbookExecutionError(message) {
|
|
237
|
+
var _this = _super.call(this, message) || this;
|
|
238
|
+
_this.name = 'PromptbookExecutionError';
|
|
239
|
+
Object.setPrototypeOf(_this, PromptbookExecutionError.prototype);
|
|
218
240
|
return _this;
|
|
219
241
|
}
|
|
220
|
-
return
|
|
242
|
+
return PromptbookExecutionError;
|
|
221
243
|
}(Error));
|
|
222
244
|
|
|
223
245
|
/**
|
|
224
|
-
*
|
|
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.
|
|
246
|
+
* This error occurs when some expectation is not met in the execution of the pipeline
|
|
231
247
|
*
|
|
232
|
-
* @private
|
|
248
|
+
* @private Always catched and rethrown as `PromptbookExecutionError`
|
|
249
|
+
* Note: This is a kindof subtype of PromptbookExecutionError
|
|
233
250
|
*/
|
|
234
|
-
function
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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; }
|
|
251
|
+
var ExpectError = /** @class */ (function (_super) {
|
|
252
|
+
__extends(ExpectError, _super);
|
|
253
|
+
function ExpectError(message) {
|
|
254
|
+
var _this = _super.call(this, message) || this;
|
|
255
|
+
_this.name = 'ExpectError';
|
|
256
|
+
Object.setPrototypeOf(_this, ExpectError.prototype);
|
|
257
|
+
return _this;
|
|
285
258
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
259
|
+
return ExpectError;
|
|
260
|
+
}(Error));
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Counts number of characters in the text
|
|
264
|
+
*/
|
|
265
|
+
function countCharacters(text) {
|
|
266
|
+
// Remove null characters
|
|
267
|
+
text = text.replace(/\0/g, '');
|
|
268
|
+
// Replace emojis (and also ZWJ sequence) with hyphens
|
|
269
|
+
text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
|
|
270
|
+
text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
|
|
271
|
+
text = text.replace(/\p{Extended_Pictographic}(\u{200D}\p{Extended_Pictographic})*/gu, '-');
|
|
272
|
+
return text.length;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Counts number of lines in the text
|
|
277
|
+
*/
|
|
278
|
+
function countLines(text) {
|
|
279
|
+
if (text === '') {
|
|
280
|
+
return 0;
|
|
289
281
|
}
|
|
290
|
-
|
|
291
|
-
throw new Error('The markdown file must have exactly one top-level section.');
|
|
292
|
-
// return root;
|
|
282
|
+
return text.split('\n').length;
|
|
293
283
|
}
|
|
284
|
+
|
|
294
285
|
/**
|
|
295
|
-
*
|
|
286
|
+
* Counts number of pages in the text
|
|
296
287
|
*/
|
|
297
|
-
function
|
|
298
|
-
var
|
|
299
|
-
return
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
content: spaceTrim__default["default"](contentLines.join('\n')),
|
|
303
|
-
sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
|
|
304
|
-
};
|
|
288
|
+
function countPages(text) {
|
|
289
|
+
var sentencesPerPage = 5; // Assuming each page has 5 sentences
|
|
290
|
+
var sentences = text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
|
|
291
|
+
var pageCount = Math.ceil(sentences.length / sentencesPerPage);
|
|
292
|
+
return pageCount;
|
|
305
293
|
}
|
|
306
294
|
|
|
307
295
|
/**
|
|
308
|
-
*
|
|
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
|
-
/**
|
|
436
|
-
* Removes HTML or Markdown comments from a string.
|
|
437
|
-
*
|
|
438
|
-
* @param {string} content - The string to remove comments from.
|
|
439
|
-
* @returns {string} The input string with all comments removed.
|
|
296
|
+
* Counts number of paragraphs in the text
|
|
440
297
|
*/
|
|
441
|
-
function
|
|
442
|
-
return
|
|
298
|
+
function countParagraphs(text) {
|
|
299
|
+
return text.split(/\n\s*\n/).filter(function (paragraph) { return paragraph.trim() !== ''; }).length;
|
|
443
300
|
}
|
|
444
301
|
|
|
445
302
|
/**
|
|
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
|
|
303
|
+
* Split text into sentences
|
|
458
304
|
*/
|
|
459
|
-
function
|
|
460
|
-
|
|
461
|
-
script = "(()=>{".concat(script, "})()");
|
|
462
|
-
try {
|
|
463
|
-
for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
|
|
464
|
-
try {
|
|
465
|
-
eval(script);
|
|
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 "); }));
|
|
494
|
-
}
|
|
495
|
-
return variables;
|
|
305
|
+
function splitIntoSentences(text) {
|
|
306
|
+
return text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
|
|
496
307
|
}
|
|
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
308
|
/**
|
|
520
|
-
*
|
|
521
|
-
*
|
|
522
|
-
* @param {string} str - The string to remove Markdown tags from.
|
|
523
|
-
* @returns {string} The input string with all Markdown tags removed.
|
|
309
|
+
* Counts number of sentences in the text
|
|
524
310
|
*/
|
|
525
|
-
function
|
|
526
|
-
|
|
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
|
-
}
|
|
534
|
-
|
|
535
|
-
/**
|
|
536
|
-
* Function parseNumber will parse number from string
|
|
537
|
-
*
|
|
538
|
-
* Unlike Number.parseInt, Number.parseFloat it will never ever result in NaN
|
|
539
|
-
* Note: it also works only with decimal numbers
|
|
540
|
-
*
|
|
541
|
-
* @returns parsed number
|
|
542
|
-
* @throws {PromptbookSyntaxError} if the value is not a number
|
|
543
|
-
*
|
|
544
|
-
* @private within the parseCommand
|
|
545
|
-
*/
|
|
546
|
-
function parseNumber(value) {
|
|
547
|
-
var originalValue = value;
|
|
548
|
-
if (typeof value === 'number') {
|
|
549
|
-
value = value.toString(); // <- TODO: Maybe more efficient way to do this
|
|
550
|
-
}
|
|
551
|
-
if (typeof value !== 'string') {
|
|
552
|
-
return 0;
|
|
553
|
-
}
|
|
554
|
-
value = value.trim();
|
|
555
|
-
if (value.startsWith('+')) {
|
|
556
|
-
return parseNumber(value.substring(1));
|
|
557
|
-
}
|
|
558
|
-
if (value.startsWith('-')) {
|
|
559
|
-
var number = parseNumber(value.substring(1));
|
|
560
|
-
if (number === 0) {
|
|
561
|
-
return 0; // <- Note: To prevent -0
|
|
562
|
-
}
|
|
563
|
-
return -number;
|
|
564
|
-
}
|
|
565
|
-
value = value.replace(/,/g, '.');
|
|
566
|
-
value = value.toUpperCase();
|
|
567
|
-
if (value === '') {
|
|
568
|
-
return 0;
|
|
569
|
-
}
|
|
570
|
-
if (value === 'โพ' || value.startsWith('INF')) {
|
|
571
|
-
return Infinity;
|
|
572
|
-
}
|
|
573
|
-
if (value.includes('/')) {
|
|
574
|
-
var _a = __read(value.split('/'), 2), numerator_ = _a[0], denominator_ = _a[1];
|
|
575
|
-
var numerator = parseNumber(numerator_);
|
|
576
|
-
var denominator = parseNumber(denominator_);
|
|
577
|
-
if (denominator === 0) {
|
|
578
|
-
throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\" because denominator is zero"));
|
|
579
|
-
}
|
|
580
|
-
return numerator / denominator;
|
|
581
|
-
}
|
|
582
|
-
if (/^(NAN|NULL|NONE|UNDEFINED|ZERO|NO.*)$/.test(value)) {
|
|
583
|
-
return 0;
|
|
584
|
-
}
|
|
585
|
-
if (value.includes('E')) {
|
|
586
|
-
var _b = __read(value.split('E'), 2), significand = _b[0], exponent = _b[1];
|
|
587
|
-
return parseNumber(significand) * Math.pow(10, parseNumber(exponent));
|
|
588
|
-
}
|
|
589
|
-
if (!/^[0-9.]+$/.test(value) || value.split('.').length > 2) {
|
|
590
|
-
throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\""));
|
|
591
|
-
}
|
|
592
|
-
var num = parseFloat(value);
|
|
593
|
-
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');
|
|
688
|
-
}
|
|
689
|
-
return replacedTemplate;
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
/**
|
|
693
|
-
* Counts number of characters in the text
|
|
694
|
-
*/
|
|
695
|
-
function countCharacters(text) {
|
|
696
|
-
// Remove null characters
|
|
697
|
-
text = text.replace(/\0/g, '');
|
|
698
|
-
// Replace emojis (and also ZWJ sequence) with hyphens
|
|
699
|
-
text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
|
|
700
|
-
text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
|
|
701
|
-
text = text.replace(/\p{Extended_Pictographic}(\u{200D}\p{Extended_Pictographic})*/gu, '-');
|
|
702
|
-
return text.length;
|
|
703
|
-
}
|
|
704
|
-
|
|
705
|
-
/**
|
|
706
|
-
* Counts number of lines in the text
|
|
707
|
-
*/
|
|
708
|
-
function countLines(text) {
|
|
709
|
-
if (text === '') {
|
|
710
|
-
return 0;
|
|
711
|
-
}
|
|
712
|
-
return text.split('\n').length;
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
/**
|
|
716
|
-
* Counts number of pages in the text
|
|
717
|
-
*/
|
|
718
|
-
function countPages(text) {
|
|
719
|
-
var sentencesPerPage = 5; // Assuming each page has 5 sentences
|
|
720
|
-
var sentences = text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
|
|
721
|
-
var pageCount = Math.ceil(sentences.length / sentencesPerPage);
|
|
722
|
-
return pageCount;
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
/**
|
|
726
|
-
* Counts number of paragraphs in the text
|
|
727
|
-
*/
|
|
728
|
-
function countParagraphs(text) {
|
|
729
|
-
return text.split(/\n\s*\n/).filter(function (paragraph) { return paragraph.trim() !== ''; }).length;
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
/**
|
|
733
|
-
* Split text into sentences
|
|
734
|
-
*/
|
|
735
|
-
function splitIntoSentences(text) {
|
|
736
|
-
return text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
|
|
737
|
-
}
|
|
738
|
-
/**
|
|
739
|
-
* Counts number of sentences in the text
|
|
740
|
-
*/
|
|
741
|
-
function countSentences(text) {
|
|
742
|
-
return splitIntoSentences(text).length;
|
|
311
|
+
function countSentences(text) {
|
|
312
|
+
return splitIntoSentences(text).length;
|
|
743
313
|
}
|
|
744
314
|
|
|
745
315
|
var defaultDiacriticsRemovalMap = [
|
|
@@ -969,70 +539,356 @@
|
|
|
969
539
|
defaultDiacriticsRemovalMap[i].base;
|
|
970
540
|
}
|
|
971
541
|
}
|
|
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
|
-
*/
|
|
542
|
+
// <- TODO: !!!! Put to maker function
|
|
543
|
+
/*
|
|
544
|
+
@see https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
|
|
545
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
546
|
+
you may not use this file except in compliance with the License.
|
|
547
|
+
You may obtain a copy of the License at
|
|
548
|
+
|
|
549
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
550
|
+
|
|
551
|
+
Unless required by applicable law or agreed to in writing, software
|
|
552
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
553
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
554
|
+
See the License for the specific language governing permissions and
|
|
555
|
+
limitations under the License.
|
|
556
|
+
*/
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
*
|
|
560
|
+
*/
|
|
561
|
+
function removeDiacritics(input) {
|
|
562
|
+
/*eslint no-control-regex: "off"*/
|
|
563
|
+
return input.replace(/[^\u0000-\u007E]/g, function (a) {
|
|
564
|
+
return DIACRITIC_VARIANTS_LETTERS[a] || a;
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* Counts number of words in the text
|
|
570
|
+
*/
|
|
571
|
+
function countWords(text) {
|
|
572
|
+
text = text.replace(/[\p{Extended_Pictographic}]/gu, 'a');
|
|
573
|
+
text = removeDiacritics(text);
|
|
574
|
+
return text.split(/[^a-zะฐ-ั0-9]+/i).filter(function (word) { return word.length > 0; }).length;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* Index of all counter functions
|
|
579
|
+
*/
|
|
580
|
+
var CountUtils = {
|
|
581
|
+
CHARACTERS: countCharacters,
|
|
582
|
+
WORDS: countWords,
|
|
583
|
+
SENTENCES: countSentences,
|
|
584
|
+
PARAGRAPHS: countParagraphs,
|
|
585
|
+
LINES: countLines,
|
|
586
|
+
PAGES: countPages,
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Function checkExpectations will check if the expectations on given value are met
|
|
591
|
+
*
|
|
592
|
+
* Note: There are two simmilar functions:
|
|
593
|
+
* - `checkExpectations` which throws an error if the expectations are not met
|
|
594
|
+
* - `isPassingExpectations` which returns a boolean
|
|
595
|
+
*
|
|
596
|
+
* @throws {ExpectError} if the expectations are not met
|
|
597
|
+
* @returns {void} Nothing
|
|
598
|
+
*/
|
|
599
|
+
function checkExpectations(expectations, value) {
|
|
600
|
+
var e_1, _a;
|
|
601
|
+
try {
|
|
602
|
+
for (var _b = __values(Object.entries(expectations)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
603
|
+
var _d = __read(_c.value, 2), unit = _d[0], _e = _d[1], max = _e.max, min = _e.min;
|
|
604
|
+
var amount = CountUtils[unit.toUpperCase()](value);
|
|
605
|
+
if (min && amount < min) {
|
|
606
|
+
throw new ExpectError("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
|
|
607
|
+
} /* not else */
|
|
608
|
+
if (max && amount > max) {
|
|
609
|
+
throw new ExpectError("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
614
|
+
finally {
|
|
615
|
+
try {
|
|
616
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
617
|
+
}
|
|
618
|
+
finally { if (e_1) throw e_1.error; }
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* Function checkExpectations will check if the expectations on given value are met
|
|
623
|
+
*
|
|
624
|
+
* Note: There are two simmilar functions:
|
|
625
|
+
* - `checkExpectations` which throws an error if the expectations are not met
|
|
626
|
+
* - `isPassingExpectations` which returns a boolean
|
|
627
|
+
*
|
|
628
|
+
* @returns {boolean} True if the expectations are met
|
|
629
|
+
*/
|
|
630
|
+
function isPassingExpectations(expectations, value) {
|
|
631
|
+
try {
|
|
632
|
+
checkExpectations(expectations, value);
|
|
633
|
+
return true;
|
|
634
|
+
}
|
|
635
|
+
catch (error) {
|
|
636
|
+
if (!(error instanceof ExpectError)) {
|
|
637
|
+
throw error;
|
|
638
|
+
}
|
|
639
|
+
return false;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* TODO: [๐] Unite object for expecting amount and format
|
|
644
|
+
*/
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* The maximum number of iterations for a loops
|
|
648
|
+
*/
|
|
649
|
+
var LOOP_LIMIT = 1000;
|
|
650
|
+
/**
|
|
651
|
+
* The maximum number of iterations for a loops which adds characters one by one
|
|
652
|
+
*/
|
|
653
|
+
var CHARACTER_LOOP_LIMIT = 100000;
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* This error occurs during the parameter replacement in the template
|
|
657
|
+
*
|
|
658
|
+
* Note: This is a kindof subtype of PromptbookExecutionError because it occurs during the execution of the pipeline
|
|
659
|
+
*/
|
|
660
|
+
var TemplateError = /** @class */ (function (_super) {
|
|
661
|
+
__extends(TemplateError, _super);
|
|
662
|
+
function TemplateError(message) {
|
|
663
|
+
var _this = _super.call(this, message) || this;
|
|
664
|
+
_this.name = 'TemplateError';
|
|
665
|
+
Object.setPrototypeOf(_this, TemplateError.prototype);
|
|
666
|
+
return _this;
|
|
667
|
+
}
|
|
668
|
+
return TemplateError;
|
|
669
|
+
}(Error));
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* Replaces parameters in template with values from parameters object
|
|
673
|
+
*
|
|
674
|
+
* @param template the template with parameters in {curly} braces
|
|
675
|
+
* @param parameters the object with parameters
|
|
676
|
+
* @returns the template with replaced parameters
|
|
677
|
+
* @throws {TemplateError} if parameter is not defined, not closed, or not opened
|
|
678
|
+
*
|
|
679
|
+
* @private within the createPromptbookExecutor
|
|
680
|
+
*/
|
|
681
|
+
function replaceParameters(template, parameters) {
|
|
682
|
+
var replacedTemplate = template;
|
|
683
|
+
var match;
|
|
684
|
+
var loopLimit = LOOP_LIMIT;
|
|
685
|
+
var _loop_1 = function () {
|
|
686
|
+
if (loopLimit-- < 0) {
|
|
687
|
+
throw new UnexpectedError('Loop limit reached during parameters replacement in `replaceParameters`');
|
|
688
|
+
}
|
|
689
|
+
var precol = match.groups.precol;
|
|
690
|
+
var parameterName = match.groups.parameterName;
|
|
691
|
+
if (parameterName === '') {
|
|
692
|
+
return "continue";
|
|
693
|
+
}
|
|
694
|
+
if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
|
|
695
|
+
throw new TemplateError('Parameter is already opened or not closed');
|
|
696
|
+
}
|
|
697
|
+
if (parameters[parameterName] === undefined) {
|
|
698
|
+
throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
|
|
699
|
+
}
|
|
700
|
+
var parameterValue = parameters[parameterName];
|
|
701
|
+
if (parameterValue === undefined) {
|
|
702
|
+
throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
|
|
703
|
+
}
|
|
704
|
+
parameterValue = parameterValue.toString();
|
|
705
|
+
if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
|
|
706
|
+
parameterValue = parameterValue
|
|
707
|
+
.split('\n')
|
|
708
|
+
.map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
|
|
709
|
+
.join('\n');
|
|
710
|
+
}
|
|
711
|
+
replacedTemplate =
|
|
712
|
+
replacedTemplate.substring(0, match.index + precol.length) +
|
|
713
|
+
parameterValue +
|
|
714
|
+
replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
|
|
715
|
+
};
|
|
716
|
+
while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
|
|
717
|
+
.exec(replacedTemplate))) {
|
|
718
|
+
_loop_1();
|
|
719
|
+
}
|
|
720
|
+
// [๐ซ] Check if there are parameters that are not closed properly
|
|
721
|
+
if (/{\w+$/.test(replacedTemplate)) {
|
|
722
|
+
throw new TemplateError('Parameter is not closed');
|
|
723
|
+
}
|
|
724
|
+
// [๐ซ] Check if there are parameters that are not opened properly
|
|
725
|
+
if (/^\w+}/.test(replacedTemplate)) {
|
|
726
|
+
throw new TemplateError('Parameter is not opened');
|
|
727
|
+
}
|
|
728
|
+
return replacedTemplate;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* Function isValidJsonString will tell you if the string is valid JSON or not
|
|
733
|
+
*/
|
|
734
|
+
function isValidJsonString(value) {
|
|
735
|
+
try {
|
|
736
|
+
JSON.parse(value);
|
|
737
|
+
return true;
|
|
738
|
+
}
|
|
739
|
+
catch (error) {
|
|
740
|
+
if (!(error instanceof Error)) {
|
|
741
|
+
throw error;
|
|
742
|
+
}
|
|
743
|
+
if (error.message.includes('Unexpected token')) {
|
|
744
|
+
return false;
|
|
745
|
+
}
|
|
746
|
+
return false;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* Makes first letter of a string uppercase
|
|
752
|
+
*
|
|
753
|
+
*/
|
|
754
|
+
function capitalize(word) {
|
|
755
|
+
return word.substring(0, 1).toUpperCase() + word.substring(1);
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/**
|
|
759
|
+
* Extracts all code blocks from markdown.
|
|
760
|
+
*
|
|
761
|
+
* Note: There are 3 simmilar function:
|
|
762
|
+
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
763
|
+
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
764
|
+
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
765
|
+
*
|
|
766
|
+
* @param markdown any valid markdown
|
|
767
|
+
* @returns code blocks with language and content
|
|
768
|
+
*
|
|
769
|
+
*/
|
|
770
|
+
function extractAllBlocksFromMarkdown(markdown) {
|
|
771
|
+
var e_1, _a;
|
|
772
|
+
var codeBlocks = [];
|
|
773
|
+
var lines = markdown.split('\n');
|
|
774
|
+
var currentCodeBlock = null;
|
|
775
|
+
try {
|
|
776
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
777
|
+
var line = lines_1_1.value;
|
|
778
|
+
if (line.startsWith('```')) {
|
|
779
|
+
var language = line.slice(3).trim() || null;
|
|
780
|
+
if (currentCodeBlock === null) {
|
|
781
|
+
currentCodeBlock = { language: language, content: '' };
|
|
782
|
+
}
|
|
783
|
+
else {
|
|
784
|
+
if (language !== null) {
|
|
785
|
+
// [๐ป]
|
|
786
|
+
throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
|
|
787
|
+
}
|
|
788
|
+
codeBlocks.push(currentCodeBlock);
|
|
789
|
+
currentCodeBlock = null;
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
else if (currentCodeBlock !== null) {
|
|
793
|
+
if (currentCodeBlock.content !== '') {
|
|
794
|
+
currentCodeBlock.content += '\n';
|
|
795
|
+
}
|
|
796
|
+
currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
801
|
+
finally {
|
|
802
|
+
try {
|
|
803
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
804
|
+
}
|
|
805
|
+
finally { if (e_1) throw e_1.error; }
|
|
806
|
+
}
|
|
807
|
+
if (currentCodeBlock !== null) {
|
|
808
|
+
// [๐ป]
|
|
809
|
+
throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
|
|
810
|
+
}
|
|
811
|
+
return codeBlocks;
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
/**
|
|
815
|
+
* Utility function to extract all list items from markdown
|
|
816
|
+
*
|
|
817
|
+
* Note: It works with both ul and ol
|
|
818
|
+
* Note: It omits list items in code blocks
|
|
819
|
+
* Note: It flattens nested lists
|
|
820
|
+
* Note: It can not work with html syntax and comments
|
|
821
|
+
*
|
|
822
|
+
* @param markdown any valid markdown
|
|
823
|
+
* @returns
|
|
824
|
+
*/
|
|
825
|
+
function extractAllListItemsFromMarkdown(markdown) {
|
|
826
|
+
var e_1, _a;
|
|
827
|
+
var lines = markdown.split('\n');
|
|
828
|
+
var listItems = [];
|
|
829
|
+
var isInCodeBlock = false;
|
|
830
|
+
try {
|
|
831
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
832
|
+
var line = lines_1_1.value;
|
|
833
|
+
var trimmedLine = line.trim();
|
|
834
|
+
if (trimmedLine.startsWith('```')) {
|
|
835
|
+
isInCodeBlock = !isInCodeBlock;
|
|
836
|
+
}
|
|
837
|
+
if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
|
|
838
|
+
var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
|
|
839
|
+
listItems.push(listItem);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
844
|
+
finally {
|
|
845
|
+
try {
|
|
846
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
847
|
+
}
|
|
848
|
+
finally { if (e_1) throw e_1.error; }
|
|
849
|
+
}
|
|
850
|
+
return listItems;
|
|
851
|
+
}
|
|
987
852
|
|
|
988
853
|
/**
|
|
854
|
+
* Extracts exactly ONE code block from markdown.
|
|
989
855
|
*
|
|
856
|
+
* Note: If there are multiple or no code blocks the function throws an error
|
|
857
|
+
*
|
|
858
|
+
* Note: There are 3 simmilar function:
|
|
859
|
+
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
860
|
+
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
861
|
+
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
862
|
+
*
|
|
863
|
+
* @param markdown any valid markdown
|
|
864
|
+
* @returns code block with language and content
|
|
990
865
|
*/
|
|
991
|
-
function
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
/**
|
|
999
|
-
* Counts number of words in the text
|
|
1000
|
-
*/
|
|
1001
|
-
function countWords(text) {
|
|
1002
|
-
text = text.replace(/[\p{Extended_Pictographic}]/gu, 'a');
|
|
1003
|
-
text = removeDiacritics(text);
|
|
1004
|
-
return text.split(/[^a-zะฐ-ั0-9]+/i).filter(function (word) { return word.length > 0; }).length;
|
|
866
|
+
function extractOneBlockFromMarkdown(markdown) {
|
|
867
|
+
var codeBlocks = extractAllBlocksFromMarkdown(markdown);
|
|
868
|
+
if (codeBlocks.length !== 1) {
|
|
869
|
+
// TODO: Report more specific place where the error happened
|
|
870
|
+
throw new Error(/* <- [๐ป] */ 'There should be exactly one code block in the markdown');
|
|
871
|
+
}
|
|
872
|
+
return codeBlocks[0];
|
|
1005
873
|
}
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
* Index of all counter functions
|
|
874
|
+
/***
|
|
875
|
+
* TODO: [๐ป] !!! Decide of this is internal util, external util OR validator/postprocessor
|
|
1009
876
|
*/
|
|
1010
|
-
var CountUtils = {
|
|
1011
|
-
CHARACTERS: countCharacters,
|
|
1012
|
-
WORDS: countWords,
|
|
1013
|
-
SENTENCES: countSentences,
|
|
1014
|
-
PARAGRAPHS: countParagraphs,
|
|
1015
|
-
LINES: countLines,
|
|
1016
|
-
PAGES: countPages,
|
|
1017
|
-
};
|
|
1018
877
|
|
|
1019
878
|
/**
|
|
1020
|
-
*
|
|
879
|
+
* Removes Markdown formatting tags from a string.
|
|
880
|
+
*
|
|
881
|
+
* @param {string} str - The string to remove Markdown tags from.
|
|
882
|
+
* @returns {string} The input string with all Markdown tags removed.
|
|
1021
883
|
*/
|
|
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
|
-
}
|
|
884
|
+
function removeMarkdownFormatting(str) {
|
|
885
|
+
// Remove bold formatting
|
|
886
|
+
str = str.replace(/\*\*(.*?)\*\*/g, '$1');
|
|
887
|
+
// Remove italic formatting
|
|
888
|
+
str = str.replace(/\*(.*?)\*/g, '$1');
|
|
889
|
+
// Remove code formatting
|
|
890
|
+
str = str.replace(/`(.*?)`/g, '$1');
|
|
891
|
+
return str;
|
|
1036
892
|
}
|
|
1037
893
|
|
|
1038
894
|
/* tslint:disable */
|
|
@@ -1069,94 +925,313 @@
|
|
|
1069
925
|
normalizedChar = char;
|
|
1070
926
|
}
|
|
1071
927
|
else {
|
|
1072
|
-
charType = 'OTHER';
|
|
1073
|
-
normalizedChar = '_';
|
|
1074
|
-
}
|
|
1075
|
-
if (charType !== lastCharType &&
|
|
1076
|
-
!(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
|
|
1077
|
-
!(lastCharType === 'NUMBER') &&
|
|
1078
|
-
!(charType === 'NUMBER')) {
|
|
1079
|
-
normalizedName += '_';
|
|
928
|
+
charType = 'OTHER';
|
|
929
|
+
normalizedChar = '_';
|
|
930
|
+
}
|
|
931
|
+
if (charType !== lastCharType &&
|
|
932
|
+
!(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
|
|
933
|
+
!(lastCharType === 'NUMBER') &&
|
|
934
|
+
!(charType === 'NUMBER')) {
|
|
935
|
+
normalizedName += '_';
|
|
936
|
+
}
|
|
937
|
+
normalizedName += normalizedChar;
|
|
938
|
+
lastCharType = charType;
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
942
|
+
finally {
|
|
943
|
+
try {
|
|
944
|
+
if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
|
|
945
|
+
}
|
|
946
|
+
finally { if (e_1) throw e_1.error; }
|
|
947
|
+
}
|
|
948
|
+
normalizedName = normalizedName.replace(/_+/g, '_');
|
|
949
|
+
normalizedName = normalizedName.replace(/_?\/_?/g, '/');
|
|
950
|
+
normalizedName = normalizedName.replace(/^_/, '');
|
|
951
|
+
normalizedName = normalizedName.replace(/_$/, '');
|
|
952
|
+
return normalizedName;
|
|
953
|
+
}
|
|
954
|
+
/**
|
|
955
|
+
* TODO: [๐บ] Use some intermediate util splitWords
|
|
956
|
+
*/
|
|
957
|
+
|
|
958
|
+
/* tslint:disable */
|
|
959
|
+
function normalizeTo_camelCase(sentence, __firstLetterCapital) {
|
|
960
|
+
var e_1, _a;
|
|
961
|
+
if (__firstLetterCapital === void 0) { __firstLetterCapital = false; }
|
|
962
|
+
var charType;
|
|
963
|
+
var lastCharType = null;
|
|
964
|
+
var normalizedName = '';
|
|
965
|
+
try {
|
|
966
|
+
for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
|
|
967
|
+
var char = sentence_1_1.value;
|
|
968
|
+
var normalizedChar = void 0;
|
|
969
|
+
if (/^[a-z]$/.test(char)) {
|
|
970
|
+
charType = 'LOWERCASE';
|
|
971
|
+
normalizedChar = char;
|
|
972
|
+
}
|
|
973
|
+
else if (/^[A-Z]$/.test(char)) {
|
|
974
|
+
charType = 'UPPERCASE';
|
|
975
|
+
normalizedChar = char.toLowerCase();
|
|
976
|
+
}
|
|
977
|
+
else if (/^[0-9]$/.test(char)) {
|
|
978
|
+
charType = 'NUMBER';
|
|
979
|
+
normalizedChar = char;
|
|
980
|
+
}
|
|
981
|
+
else {
|
|
982
|
+
charType = 'OTHER';
|
|
983
|
+
normalizedChar = '';
|
|
984
|
+
}
|
|
985
|
+
if (!lastCharType) {
|
|
986
|
+
if (__firstLetterCapital) {
|
|
987
|
+
normalizedChar = normalizedChar.toUpperCase(); //TODO: DRY
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
else if (charType !== lastCharType &&
|
|
991
|
+
!(charType === 'LOWERCASE' && lastCharType === 'UPPERCASE') &&
|
|
992
|
+
!(lastCharType === 'NUMBER') &&
|
|
993
|
+
!(charType === 'NUMBER')) {
|
|
994
|
+
normalizedChar = normalizedChar.toUpperCase(); //TODO: [๐บ] DRY
|
|
995
|
+
}
|
|
996
|
+
normalizedName += normalizedChar;
|
|
997
|
+
lastCharType = charType;
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1001
|
+
finally {
|
|
1002
|
+
try {
|
|
1003
|
+
if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
|
|
1004
|
+
}
|
|
1005
|
+
finally { if (e_1) throw e_1.error; }
|
|
1006
|
+
}
|
|
1007
|
+
return normalizedName;
|
|
1008
|
+
}
|
|
1009
|
+
/**
|
|
1010
|
+
* TODO: [๐บ] Use some intermediate util splitWords
|
|
1011
|
+
*/
|
|
1012
|
+
|
|
1013
|
+
function normalizeTo_PascalCase(sentence) {
|
|
1014
|
+
return normalizeTo_camelCase(sentence, true);
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
/**
|
|
1018
|
+
* Supported script languages
|
|
1019
|
+
*/
|
|
1020
|
+
var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
|
|
1021
|
+
|
|
1022
|
+
/**
|
|
1023
|
+
* Parses the template and returns the list of all parameter names
|
|
1024
|
+
*
|
|
1025
|
+
* @param template the template with parameters in {curly} braces
|
|
1026
|
+
* @returns the list of parameter names
|
|
1027
|
+
*
|
|
1028
|
+
* @private within the library
|
|
1029
|
+
*/
|
|
1030
|
+
function extractParameters(template) {
|
|
1031
|
+
var e_1, _a;
|
|
1032
|
+
var matches = template.matchAll(/{\w+}/g);
|
|
1033
|
+
var parameterNames = [];
|
|
1034
|
+
try {
|
|
1035
|
+
for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
|
|
1036
|
+
var match = matches_1_1.value;
|
|
1037
|
+
var parameterName = match[0].slice(1, -1);
|
|
1038
|
+
if (!parameterNames.includes(parameterName)) {
|
|
1039
|
+
parameterNames.push(parameterName);
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1044
|
+
finally {
|
|
1045
|
+
try {
|
|
1046
|
+
if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
|
|
1047
|
+
}
|
|
1048
|
+
finally { if (e_1) throw e_1.error; }
|
|
1049
|
+
}
|
|
1050
|
+
return parameterNames;
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
/**
|
|
1054
|
+
* Computes the deepness of the markdown structure.
|
|
1055
|
+
*
|
|
1056
|
+
* @private within the library
|
|
1057
|
+
*/
|
|
1058
|
+
function countMarkdownStructureDeepness(markdownStructure) {
|
|
1059
|
+
var e_1, _a;
|
|
1060
|
+
var maxDeepness = 0;
|
|
1061
|
+
try {
|
|
1062
|
+
for (var _b = __values(markdownStructure.sections), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
1063
|
+
var section = _c.value;
|
|
1064
|
+
maxDeepness = Math.max(maxDeepness, countMarkdownStructureDeepness(section));
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1068
|
+
finally {
|
|
1069
|
+
try {
|
|
1070
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
1071
|
+
}
|
|
1072
|
+
finally { if (e_1) throw e_1.error; }
|
|
1073
|
+
}
|
|
1074
|
+
return maxDeepness + 1;
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
/**
|
|
1078
|
+
* Parse a markdown string into a MarkdownStructure object.
|
|
1079
|
+
*
|
|
1080
|
+
* Note: This function does work with code blocks
|
|
1081
|
+
* Note: This function does not work with markdown comments
|
|
1082
|
+
*
|
|
1083
|
+
* @param markdown The markdown string to parse.
|
|
1084
|
+
* @returns The MarkdownStructure object.
|
|
1085
|
+
*
|
|
1086
|
+
* @private within the library
|
|
1087
|
+
*/
|
|
1088
|
+
function markdownToMarkdownStructure(markdown) {
|
|
1089
|
+
var e_1, _a;
|
|
1090
|
+
var lines = markdown.split('\n');
|
|
1091
|
+
var root = { level: 0, title: '', contentLines: [], sections: [], parent: null };
|
|
1092
|
+
var current = root;
|
|
1093
|
+
var isInsideCodeBlock = false;
|
|
1094
|
+
try {
|
|
1095
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
1096
|
+
var line = lines_1_1.value;
|
|
1097
|
+
var headingMatch = line.match(/^(?<mark>#{1,6})\s(?<title>.*)/);
|
|
1098
|
+
if (isInsideCodeBlock || !headingMatch) {
|
|
1099
|
+
if (line.startsWith('```')) {
|
|
1100
|
+
isInsideCodeBlock = !isInsideCodeBlock;
|
|
1101
|
+
}
|
|
1102
|
+
current.contentLines.push(line);
|
|
1103
|
+
}
|
|
1104
|
+
else {
|
|
1105
|
+
var level = headingMatch.groups.mark.length;
|
|
1106
|
+
var title = headingMatch.groups.title.trim();
|
|
1107
|
+
var parent_1 = void 0;
|
|
1108
|
+
if (level > current.level) {
|
|
1109
|
+
// Note: Going deeper (next section is child of current)
|
|
1110
|
+
parent_1 = current;
|
|
1111
|
+
}
|
|
1112
|
+
else {
|
|
1113
|
+
// Note: Going up or staying at the same level (next section is sibling or parent or grandparent,... of current)
|
|
1114
|
+
parent_1 = current;
|
|
1115
|
+
var loopLimit = LOOP_LIMIT;
|
|
1116
|
+
while (parent_1.level !== level - 1) {
|
|
1117
|
+
if (loopLimit-- < 0) {
|
|
1118
|
+
throw new UnexpectedError('Loop limit reached during parsing of markdown structure in `markdownToMarkdownStructure`');
|
|
1119
|
+
}
|
|
1120
|
+
if (parent_1.parent === null /* <- Note: We are in root */) {
|
|
1121
|
+
// [๐ป]
|
|
1122
|
+
throw new Error(spacetrim.spaceTrim("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
|
|
1123
|
+
}
|
|
1124
|
+
parent_1 = parent_1.parent;
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
|
|
1128
|
+
parent_1.sections.push(section);
|
|
1129
|
+
current = section;
|
|
1080
1130
|
}
|
|
1081
|
-
normalizedName += normalizedChar;
|
|
1082
|
-
lastCharType = charType;
|
|
1083
1131
|
}
|
|
1084
1132
|
}
|
|
1085
1133
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1086
1134
|
finally {
|
|
1087
1135
|
try {
|
|
1088
|
-
if (
|
|
1136
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
1089
1137
|
}
|
|
1090
1138
|
finally { if (e_1) throw e_1.error; }
|
|
1091
1139
|
}
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1140
|
+
if (root.sections.length === 1) {
|
|
1141
|
+
var markdownStructure = parsingMarkdownStructureToMarkdownStructure(root.sections[0]);
|
|
1142
|
+
return markdownStructure;
|
|
1143
|
+
}
|
|
1144
|
+
// [๐ป]
|
|
1145
|
+
throw new Error('The markdown file must have exactly one top-level section.');
|
|
1146
|
+
// return root;
|
|
1097
1147
|
}
|
|
1098
1148
|
/**
|
|
1099
|
-
*
|
|
1149
|
+
* @private
|
|
1100
1150
|
*/
|
|
1151
|
+
function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
|
|
1152
|
+
var level = parsingMarkdownStructure.level, title = parsingMarkdownStructure.title, contentLines = parsingMarkdownStructure.contentLines, sections = parsingMarkdownStructure.sections;
|
|
1153
|
+
return {
|
|
1154
|
+
level: level,
|
|
1155
|
+
title: title,
|
|
1156
|
+
content: spacetrim.spaceTrim(contentLines.join('\n')),
|
|
1157
|
+
sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
|
|
1158
|
+
};
|
|
1159
|
+
}
|
|
1101
1160
|
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1161
|
+
/**
|
|
1162
|
+
* The version of the Promptbook library
|
|
1163
|
+
*/
|
|
1164
|
+
var PROMPTBOOK_VERSION = '0.44.0-10';
|
|
1165
|
+
|
|
1166
|
+
/**
|
|
1167
|
+
* Parses the given script and returns the list of all used variables that are not defined in the script
|
|
1168
|
+
*
|
|
1169
|
+
* @param script from which to extract the variables
|
|
1170
|
+
* @returns the list of variable names
|
|
1171
|
+
* @throws {PromptbookSyntaxError} if the script is invalid
|
|
1172
|
+
*
|
|
1173
|
+
* @private within the promptbookStringToJson
|
|
1174
|
+
*/
|
|
1175
|
+
function extractVariables(script) {
|
|
1176
|
+
var variables = [];
|
|
1177
|
+
script = "(()=>{".concat(script, "})()");
|
|
1109
1178
|
try {
|
|
1110
|
-
for (var
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
if (/^[a-z]$/.test(char)) {
|
|
1114
|
-
charType = 'LOWERCASE';
|
|
1115
|
-
normalizedChar = char;
|
|
1116
|
-
}
|
|
1117
|
-
else if (/^[A-Z]$/.test(char)) {
|
|
1118
|
-
charType = 'UPPERCASE';
|
|
1119
|
-
normalizedChar = char.toLowerCase();
|
|
1120
|
-
}
|
|
1121
|
-
else if (/^[0-9]$/.test(char)) {
|
|
1122
|
-
charType = 'NUMBER';
|
|
1123
|
-
normalizedChar = char;
|
|
1124
|
-
}
|
|
1125
|
-
else {
|
|
1126
|
-
charType = 'OTHER';
|
|
1127
|
-
normalizedChar = '';
|
|
1179
|
+
for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
|
|
1180
|
+
try {
|
|
1181
|
+
eval(script);
|
|
1128
1182
|
}
|
|
1129
|
-
|
|
1130
|
-
if (
|
|
1131
|
-
|
|
1183
|
+
catch (error) {
|
|
1184
|
+
if (!(error instanceof ReferenceError)) {
|
|
1185
|
+
throw error;
|
|
1186
|
+
}
|
|
1187
|
+
var undefinedName = error.message.split(' ')[0];
|
|
1188
|
+
/*
|
|
1189
|
+
Note: Remapping error
|
|
1190
|
+
From: [ReferenceError: thing is not defined],
|
|
1191
|
+
To: [Error: Parameter {thing} is not defined],
|
|
1192
|
+
*/
|
|
1193
|
+
if (!undefinedName) {
|
|
1194
|
+
throw error;
|
|
1195
|
+
}
|
|
1196
|
+
if (script.includes(undefinedName + '(')) {
|
|
1197
|
+
script = "const ".concat(undefinedName, " = ()=>'';") + script;
|
|
1198
|
+
}
|
|
1199
|
+
else {
|
|
1200
|
+
variables.push(undefinedName);
|
|
1201
|
+
script = "const ".concat(undefinedName, " = '';") + script;
|
|
1132
1202
|
}
|
|
1133
1203
|
}
|
|
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
|
-
}
|
|
1143
1204
|
}
|
|
1144
|
-
catch (
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
|
|
1205
|
+
catch (error) {
|
|
1206
|
+
if (!(error instanceof Error)) {
|
|
1207
|
+
throw error;
|
|
1148
1208
|
}
|
|
1149
|
-
|
|
1209
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
|
|
1150
1210
|
}
|
|
1151
|
-
return
|
|
1211
|
+
return variables;
|
|
1152
1212
|
}
|
|
1213
|
+
|
|
1153
1214
|
/**
|
|
1154
|
-
*
|
|
1215
|
+
* Execution type describes the way how the block is executed
|
|
1216
|
+
*
|
|
1217
|
+
* @see https://github.com/webgptorg/promptbook#execution-type
|
|
1155
1218
|
*/
|
|
1219
|
+
var ExecutionTypes = [
|
|
1220
|
+
'PROMPT_TEMPLATE',
|
|
1221
|
+
'SIMPLE_TEMPLATE',
|
|
1222
|
+
'SCRIPT',
|
|
1223
|
+
'PROMPT_DIALOG',
|
|
1224
|
+
// <- [๐ฅป] Insert here when making new command
|
|
1225
|
+
];
|
|
1156
1226
|
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1227
|
+
/**
|
|
1228
|
+
* Units of text measurement
|
|
1229
|
+
*/
|
|
1230
|
+
var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'PARAGRAPHS', 'LINES', 'PAGES'];
|
|
1231
|
+
/**
|
|
1232
|
+
* TODO: [๐] Unite object for expecting amount and format - remove expectFormat
|
|
1233
|
+
* TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
|
|
1234
|
+
*/
|
|
1160
1235
|
|
|
1161
1236
|
/**
|
|
1162
1237
|
* Parses one line of ul/ol to command
|
|
@@ -1196,15 +1271,15 @@
|
|
|
1196
1271
|
type.startsWith('PROMPTBOOKURL') ||
|
|
1197
1272
|
type.startsWith('HTTPS')) {
|
|
1198
1273
|
if (!(listItemParts.length === 2 || (listItemParts.length === 1 && type.startsWith('HTTPS')))) {
|
|
1199
|
-
throw new PromptbookSyntaxError(
|
|
1274
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n ")));
|
|
1200
1275
|
}
|
|
1201
1276
|
var promptbookUrlString = listItemParts.pop();
|
|
1202
1277
|
var promptbookUrl = new URL(promptbookUrlString);
|
|
1203
1278
|
if (promptbookUrl.protocol !== 'https:') {
|
|
1204
|
-
throw new PromptbookSyntaxError(
|
|
1279
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n Protocol must be HTTPS\n ")));
|
|
1205
1280
|
}
|
|
1206
1281
|
if (promptbookUrl.hash !== '') {
|
|
1207
|
-
throw new PromptbookSyntaxError(
|
|
1282
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n URL must not contain hash\n Hash is used for identification of the prompt template in the pipeline\n ")));
|
|
1208
1283
|
}
|
|
1209
1284
|
return {
|
|
1210
1285
|
type: 'PROMPTBOOK_URL',
|
|
@@ -1213,7 +1288,7 @@
|
|
|
1213
1288
|
}
|
|
1214
1289
|
else if (type.startsWith('PROMPTBOOK_VERSION') || type.startsWith('PTBK_VERSION')) {
|
|
1215
1290
|
if (listItemParts.length !== 2) {
|
|
1216
|
-
throw new PromptbookSyntaxError(
|
|
1291
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid PROMPTBOOK_VERSION command:\n\n - ".concat(listItem, "\n ")));
|
|
1217
1292
|
}
|
|
1218
1293
|
var promptbookVersion = listItemParts.pop();
|
|
1219
1294
|
// TODO: Validate version
|
|
@@ -1228,7 +1303,7 @@
|
|
|
1228
1303
|
type.startsWith('SIMPLE_TEMPLATE')) {
|
|
1229
1304
|
var executionTypes = ExecutionTypes.filter(function (executionType) { return type.includes(executionType); });
|
|
1230
1305
|
if (executionTypes.length !== 1) {
|
|
1231
|
-
throw new PromptbookSyntaxError(
|
|
1306
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim(function (block) { return "\n Unknown execution type in command:\n\n - ".concat(listItem, "\n\n Supported execution types are:\n ").concat(block(ExecutionTypes.join(', ')), "\n "); }));
|
|
1232
1307
|
}
|
|
1233
1308
|
return {
|
|
1234
1309
|
type: 'EXECUTE',
|
|
@@ -1253,7 +1328,7 @@
|
|
|
1253
1328
|
};
|
|
1254
1329
|
}
|
|
1255
1330
|
else {
|
|
1256
|
-
throw new PromptbookSyntaxError(
|
|
1331
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim(function (block) { return "\n Unknown model variant in command:\n\n - ".concat(listItem, "\n\n Supported variants are:\n ").concat(block(['CHAT', 'COMPLETION'].join(', ')), "\n "); }));
|
|
1257
1332
|
}
|
|
1258
1333
|
}
|
|
1259
1334
|
if (type.startsWith('MODEL_NAME')) {
|
|
@@ -1264,7 +1339,7 @@
|
|
|
1264
1339
|
};
|
|
1265
1340
|
}
|
|
1266
1341
|
else {
|
|
1267
|
-
throw new PromptbookSyntaxError(
|
|
1342
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim(function (block) { return "\n Unknown model key in command:\n\n - ".concat(listItem, "\n\n Supported model keys are:\n ").concat(block(['variant', 'name'].join(', ')), "\n\n Example:\n\n - MODEL VARIANT Chat\n - MODEL NAME gpt-4\n "); }));
|
|
1268
1343
|
}
|
|
1269
1344
|
}
|
|
1270
1345
|
else if (type.startsWith('PARAM') ||
|
|
@@ -1274,12 +1349,12 @@
|
|
|
1274
1349
|
listItem.startsWith('> {') /* <- Note: This is a bit hack to parse return parameters defined at the end of each section */) {
|
|
1275
1350
|
var parametersMatch = listItem.match(/\{(?<parameterName>[a-z0-9_]+)\}[^\S\r\n]*(?<parameterDescription>.*)$/im);
|
|
1276
1351
|
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
1277
|
-
throw new PromptbookSyntaxError(
|
|
1352
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
|
|
1278
1353
|
}
|
|
1279
1354
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1280
1355
|
var _b = parametersMatch.groups, parameterName = _b.parameterName, parameterDescription = _b.parameterDescription;
|
|
1281
1356
|
if (parameterDescription && parameterDescription.match(/\{(?<parameterName>[a-z0-9_]+)\}/im)) {
|
|
1282
|
-
throw new PromptbookSyntaxError(
|
|
1357
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Parameter {".concat(parameterName, "} can not contain another parameter in description:\n\n - ").concat(listItem, "\n ")));
|
|
1283
1358
|
}
|
|
1284
1359
|
var isInput = type.startsWith('INPUT');
|
|
1285
1360
|
var isOutput = type.startsWith('OUTPUT');
|
|
@@ -1297,11 +1372,11 @@
|
|
|
1297
1372
|
}
|
|
1298
1373
|
else if (type.startsWith('JOKER')) {
|
|
1299
1374
|
if (listItemParts.length !== 2) {
|
|
1300
|
-
throw new PromptbookSyntaxError(
|
|
1375
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid JOKER command:\n\n - ".concat(listItem, "\n ")));
|
|
1301
1376
|
}
|
|
1302
1377
|
var parametersMatch = (listItemParts.pop() || '').match(/^\{(?<parameterName>[a-z0-9_]+)\}$/im);
|
|
1303
1378
|
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
1304
|
-
throw new PromptbookSyntaxError(
|
|
1379
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
|
|
1305
1380
|
}
|
|
1306
1381
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1307
1382
|
var parameterName = parametersMatch.groups.parameterName;
|
|
@@ -1312,7 +1387,7 @@
|
|
|
1312
1387
|
}
|
|
1313
1388
|
else if (type.startsWith('POSTPROCESS') || type.startsWith('POST_PROCESS')) {
|
|
1314
1389
|
if (listItemParts.length !== 2) {
|
|
1315
|
-
throw new PromptbookSyntaxError(
|
|
1390
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid POSTPROCESSING command:\n\n - ".concat(listItem, "\n ")));
|
|
1316
1391
|
}
|
|
1317
1392
|
var functionName = listItemParts.pop();
|
|
1318
1393
|
return {
|
|
@@ -1392,7 +1467,7 @@
|
|
|
1392
1467
|
if (!(error instanceof Error)) {
|
|
1393
1468
|
throw error;
|
|
1394
1469
|
}
|
|
1395
|
-
throw new PromptbookSyntaxError(
|
|
1470
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid EXPECT command; ".concat(error.message, ":\n\n - ").concat(listItem, "\n ")));
|
|
1396
1471
|
}
|
|
1397
1472
|
/*
|
|
1398
1473
|
} else if (type.startsWith('__________________')) {
|
|
@@ -1400,7 +1475,7 @@
|
|
|
1400
1475
|
*/
|
|
1401
1476
|
}
|
|
1402
1477
|
else {
|
|
1403
|
-
throw new PromptbookSyntaxError(
|
|
1478
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Unknown command:\n\n - ".concat(listItem, "\n\n Supported commands are:\n - PROMPTBOOK_URL <url>\n - PROMPTBOOK_VERSION <version>\n - EXECUTE PROMPT TEMPLATE\n - EXECUTE SIMPLE TEMPLATE\n - SIMPLE TEMPLATE\n - EXECUTE SCRIPT\n - EXECUTE PROMPT_DIALOG'\n - PROMPT_DIALOG'\n - MODEL NAME <name>\n - MODEL VARIANT <\"Chat\"|\"Completion\">\n - INPUT PARAM {<name>} <description>\n - OUTPUT PARAM {<name>} <description>\n - POSTPROCESS `{functionName}`\n - JOKER {<name>}\n - EXPECT JSON\n - EXPECT <\"Exactly\"|\"Min\"|\"Max\"> <number> <\"Chars\"|\"Words\"|\"Sentences\"|\"Paragraphs\"|\"Pages\">\n\n ")));
|
|
1404
1479
|
}
|
|
1405
1480
|
}
|
|
1406
1481
|
|
|
@@ -1436,7 +1511,7 @@
|
|
|
1436
1511
|
existingParameter.description &&
|
|
1437
1512
|
existingParameter.description !== parameterDescription &&
|
|
1438
1513
|
parameterDescription) {
|
|
1439
|
-
throw new PromptbookSyntaxError(
|
|
1514
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim(function (block) { return "\n Parameter {".concat(parameterName, "} is defined multiple times with different description.\n\n First definition:\n ").concat(block(existingParameter.description || '[undefined]'), "\n\n Second definition:\n ").concat(block(parameterDescription || '[undefined]'), "\n "); }));
|
|
1440
1515
|
}
|
|
1441
1516
|
if (existingParameter) {
|
|
1442
1517
|
if (parameterDescription) {
|
|
@@ -1457,7 +1532,7 @@
|
|
|
1457
1532
|
var markdownStructure = markdownToMarkdownStructure(promptbookString);
|
|
1458
1533
|
var markdownStructureDeepness = countMarkdownStructureDeepness(markdownStructure);
|
|
1459
1534
|
if (markdownStructureDeepness !== 2) {
|
|
1460
|
-
throw new PromptbookSyntaxError(
|
|
1535
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid markdown structure.\n The markdown must have exactly 2 levels of headings (one top-level section and one section for each template).\n Now it has ".concat(markdownStructureDeepness, " levels of headings.\n ")));
|
|
1461
1536
|
}
|
|
1462
1537
|
promptbookJson.title = markdownStructure.title;
|
|
1463
1538
|
// TODO: [1] DRY description
|
|
@@ -1466,7 +1541,7 @@
|
|
|
1466
1541
|
description = description.split(/^```.*^```/gms).join('');
|
|
1467
1542
|
//Note: Remove lists and return statement
|
|
1468
1543
|
description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
|
|
1469
|
-
description =
|
|
1544
|
+
description = spacetrim.spaceTrim(description);
|
|
1470
1545
|
if (description === '') {
|
|
1471
1546
|
description = undefined;
|
|
1472
1547
|
}
|
|
@@ -1581,13 +1656,13 @@
|
|
|
1581
1656
|
throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
|
|
1582
1657
|
}
|
|
1583
1658
|
else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
|
|
1584
|
-
throw new PromptbookSyntaxError(
|
|
1659
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim(function (block) { return "\n Script language ".concat(language, " is not supported.\n\n Supported languages are:\n ").concat(block(SUPPORTED_SCRIPT_LANGUAGES.join(', ')), "\n\n "); }));
|
|
1585
1660
|
}
|
|
1586
1661
|
}
|
|
1587
1662
|
var lastLine = section.content.split('\n').pop();
|
|
1588
1663
|
var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
|
|
1589
1664
|
if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
|
|
1590
|
-
throw new PromptbookSyntaxError(
|
|
1665
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
|
|
1591
1666
|
// TODO: Show code of invalid sections each time + DRY
|
|
1592
1667
|
section.content
|
|
1593
1668
|
.split('\n')
|
|
@@ -1601,7 +1676,7 @@
|
|
|
1601
1676
|
description_1 = description_1.split(/^```.*^```/gms).join('');
|
|
1602
1677
|
//Note: Remove lists and return statement
|
|
1603
1678
|
description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
|
|
1604
|
-
description_1 =
|
|
1679
|
+
description_1 = spacetrim.spaceTrim(description_1);
|
|
1605
1680
|
if (description_1 === '') {
|
|
1606
1681
|
description_1 = undefined;
|
|
1607
1682
|
}
|
|
@@ -1651,7 +1726,7 @@
|
|
|
1651
1726
|
executionType: executionType,
|
|
1652
1727
|
jokers: jokers,
|
|
1653
1728
|
postprocessing: postprocessing,
|
|
1654
|
-
|
|
1729
|
+
expectations: expectAmount,
|
|
1655
1730
|
expectFormat: expectFormat,
|
|
1656
1731
|
modelRequirements: templateModelRequirements,
|
|
1657
1732
|
contentLanguage: executionType === 'SCRIPT' ? language : undefined,
|
|
@@ -1749,12 +1824,12 @@
|
|
|
1749
1824
|
if (!parameter.isInput &&
|
|
1750
1825
|
!parameter.isOutput &&
|
|
1751
1826
|
!promptbook.promptTemplates.some(function (template) { return template.dependentParameterNames.includes(parameter.name); })) {
|
|
1752
|
-
throw new PromptbookLogicError(
|
|
1827
|
+
throw new PromptbookLogicError(spacetrim.spaceTrim("\n Parameter {".concat(parameter.name, "} is created but not used\n\n You can declare {").concat(parameter.name, "} as output parameter by adding in the header:\n - OUTPUT PARAMETER `{").concat(parameter.name, "}` ").concat(parameter.description || '', "\n\n ")));
|
|
1753
1828
|
}
|
|
1754
1829
|
// Note: Testing that parameter is either input or result of some template
|
|
1755
1830
|
if (!parameter.isInput &&
|
|
1756
1831
|
!promptbook.promptTemplates.some(function (template) { return template.resultingParameterName === parameter.name; })) {
|
|
1757
|
-
throw new PromptbookLogicError(
|
|
1832
|
+
throw new PromptbookLogicError(spacetrim.spaceTrim("\n Parameter {".concat(parameter.name, "} is declared but not defined\n\n You can do one of these:\n - Remove declaration of {").concat(parameter.name, "}\n - Add prompt template that results in -> {").concat(parameter.name, "}\n\n ")));
|
|
1758
1833
|
}
|
|
1759
1834
|
};
|
|
1760
1835
|
try {
|
|
@@ -1789,11 +1864,11 @@
|
|
|
1789
1864
|
if (template.executionType === 'PROMPT_TEMPLATE' &&
|
|
1790
1865
|
(template.modelRequirements.modelVariant === undefined ||
|
|
1791
1866
|
template.modelRequirements.modelName === undefined)) {
|
|
1792
|
-
throw new PromptbookLogicError(
|
|
1867
|
+
throw new PromptbookLogicError(spacetrim.spaceTrim("\n\n You must specify MODEL VARIANT and MODEL NAME in the prompt template \"".concat(template.title, "\"\n\n For example:\n - MODEL VARIANT Chat\n - MODEL NAME `gpt-4-1106-preview`\n\n ")));
|
|
1793
1868
|
}
|
|
1794
1869
|
if (template.jokers && template.jokers.length > 0) {
|
|
1795
1870
|
if (!template.expectFormat &&
|
|
1796
|
-
!template.
|
|
1871
|
+
!template.expectations /* <- TODO: Require at least 1 -> min <- expectation to use jokers */) {
|
|
1797
1872
|
throw new PromptbookLogicError("Joker parameters are used for {".concat(template.resultingParameterName, "} but no expectations are defined"));
|
|
1798
1873
|
}
|
|
1799
1874
|
try {
|
|
@@ -1812,9 +1887,9 @@
|
|
|
1812
1887
|
finally { if (e_3) throw e_3.error; }
|
|
1813
1888
|
}
|
|
1814
1889
|
}
|
|
1815
|
-
if (template.
|
|
1890
|
+
if (template.expectations) {
|
|
1816
1891
|
try {
|
|
1817
|
-
for (var _l = (e_4 = void 0, __values(Object.entries(template.
|
|
1892
|
+
for (var _l = (e_4 = void 0, __values(Object.entries(template.expectations))), _m = _l.next(); !_m.done; _m = _l.next()) {
|
|
1818
1893
|
var _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], min = _p.min, max = _p.max;
|
|
1819
1894
|
if (min !== undefined && max !== undefined && min > max) {
|
|
1820
1895
|
throw new PromptbookLogicError("Min expectation (=".concat(min, ") of ").concat(unit, " is higher than max expectation (=").concat(max, ")"));
|
|
@@ -1864,7 +1939,7 @@
|
|
|
1864
1939
|
return template.dependentParameterNames.every(function (name) { return resovedParameters.includes(name); });
|
|
1865
1940
|
});
|
|
1866
1941
|
if (currentlyResovedTemplates.length === 0) {
|
|
1867
|
-
throw new PromptbookLogicError(
|
|
1942
|
+
throw new PromptbookLogicError(spacetrim.spaceTrim(function (block) { return "\n\n Can not resolve some parameters\n It may be circular dependencies\n\n Can not resolve:\n ".concat(block(unresovedTemplates
|
|
1868
1943
|
.map(function (_a) {
|
|
1869
1944
|
var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
|
|
1870
1945
|
return "- {".concat(resultingParameterName, "} depends on ").concat(dependentParameterNames
|
|
@@ -1895,23 +1970,6 @@
|
|
|
1895
1970
|
* > ex port function validatePromptbookJson(promptbook: unknown): asserts promptbook is PromptbookJson {
|
|
1896
1971
|
*/
|
|
1897
1972
|
|
|
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
1973
|
/**
|
|
1916
1974
|
* Creates executor function from promptbook and execution tools.
|
|
1917
1975
|
*
|
|
@@ -1926,10 +1984,10 @@
|
|
|
1926
1984
|
var promptbookExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
|
|
1927
1985
|
function executeSingleTemplate(currentTemplate) {
|
|
1928
1986
|
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 (
|
|
1987
|
+
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;
|
|
1988
|
+
var e_2, _j, e_4, _k, e_3, _l, _m;
|
|
1989
|
+
return __generator(this, function (_o) {
|
|
1990
|
+
switch (_o.label) {
|
|
1933
1991
|
case 0:
|
|
1934
1992
|
name = "promptbook-executor-frame-".concat(currentTemplate.name);
|
|
1935
1993
|
title = currentTemplate.title;
|
|
@@ -1946,8 +2004,8 @@
|
|
|
1946
2004
|
// <- [3]
|
|
1947
2005
|
})];
|
|
1948
2006
|
case 1:
|
|
1949
|
-
|
|
1950
|
-
|
|
2007
|
+
_o.sent();
|
|
2008
|
+
_o.label = 2;
|
|
1951
2009
|
case 2:
|
|
1952
2010
|
result = null;
|
|
1953
2011
|
resultString = null;
|
|
@@ -1955,7 +2013,7 @@
|
|
|
1955
2013
|
maxAttempts = currentTemplate.executionType === 'PROMPT_DIALOG' ? Infinity : maxExecutionAttempts;
|
|
1956
2014
|
jokers = currentTemplate.jokers || [];
|
|
1957
2015
|
attempt = -jokers.length;
|
|
1958
|
-
|
|
2016
|
+
_o.label = 3;
|
|
1959
2017
|
case 3:
|
|
1960
2018
|
if (!(attempt < maxAttempts)) return [3 /*break*/, 49];
|
|
1961
2019
|
isJokerAttempt = attempt < 0;
|
|
@@ -1972,9 +2030,9 @@
|
|
|
1972
2030
|
}
|
|
1973
2031
|
resultString = parametersToPass[joker];
|
|
1974
2032
|
}
|
|
1975
|
-
|
|
2033
|
+
_o.label = 4;
|
|
1976
2034
|
case 4:
|
|
1977
|
-
|
|
2035
|
+
_o.trys.push([4, 45, 46, 47]);
|
|
1978
2036
|
if (!!isJokerAttempt) return [3 /*break*/, 27];
|
|
1979
2037
|
_a = currentTemplate.executionType;
|
|
1980
2038
|
switch (_a) {
|
|
@@ -1996,6 +2054,7 @@
|
|
|
1996
2054
|
parameters: parametersToPass,
|
|
1997
2055
|
content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
|
|
1998
2056
|
modelRequirements: currentTemplate.modelRequirements,
|
|
2057
|
+
expectations: currentTemplate.expectations,
|
|
1999
2058
|
};
|
|
2000
2059
|
_b = currentTemplate.modelRequirements.modelVariant;
|
|
2001
2060
|
switch (_b) {
|
|
@@ -2005,14 +2064,14 @@
|
|
|
2005
2064
|
return [3 /*break*/, 11];
|
|
2006
2065
|
case 7: return [4 /*yield*/, tools.natural.gptChat(prompt)];
|
|
2007
2066
|
case 8:
|
|
2008
|
-
chatThread =
|
|
2067
|
+
chatThread = _o.sent();
|
|
2009
2068
|
// TODO: [๐ฌ] Destroy chatThread
|
|
2010
2069
|
result = chatThread;
|
|
2011
2070
|
resultString = chatThread.content;
|
|
2012
2071
|
return [3 /*break*/, 12];
|
|
2013
2072
|
case 9: return [4 /*yield*/, tools.natural.gptComplete(prompt)];
|
|
2014
2073
|
case 10:
|
|
2015
|
-
completionResult =
|
|
2074
|
+
completionResult = _o.sent();
|
|
2016
2075
|
result = completionResult;
|
|
2017
2076
|
resultString = completionResult.content;
|
|
2018
2077
|
return [3 /*break*/, 12];
|
|
@@ -2027,27 +2086,27 @@
|
|
|
2027
2086
|
}
|
|
2028
2087
|
// TODO: DRY [1]
|
|
2029
2088
|
scriptExecutionErrors = [];
|
|
2030
|
-
|
|
2089
|
+
_o.label = 14;
|
|
2031
2090
|
case 14:
|
|
2032
|
-
|
|
2091
|
+
_o.trys.push([14, 21, 22, 23]);
|
|
2033
2092
|
_c = (e_2 = void 0, __values(tools.script)), _d = _c.next();
|
|
2034
|
-
|
|
2093
|
+
_o.label = 15;
|
|
2035
2094
|
case 15:
|
|
2036
2095
|
if (!!_d.done) return [3 /*break*/, 20];
|
|
2037
2096
|
scriptTools = _d.value;
|
|
2038
|
-
|
|
2097
|
+
_o.label = 16;
|
|
2039
2098
|
case 16:
|
|
2040
|
-
|
|
2099
|
+
_o.trys.push([16, 18, , 19]);
|
|
2041
2100
|
return [4 /*yield*/, scriptTools.execute({
|
|
2042
2101
|
scriptLanguage: currentTemplate.contentLanguage,
|
|
2043
2102
|
script: currentTemplate.content,
|
|
2044
2103
|
parameters: parametersToPass,
|
|
2045
2104
|
})];
|
|
2046
2105
|
case 17:
|
|
2047
|
-
resultString =
|
|
2106
|
+
resultString = _o.sent();
|
|
2048
2107
|
return [3 /*break*/, 20];
|
|
2049
2108
|
case 18:
|
|
2050
|
-
error_2 =
|
|
2109
|
+
error_2 = _o.sent();
|
|
2051
2110
|
if (!(error_2 instanceof Error)) {
|
|
2052
2111
|
throw error_2;
|
|
2053
2112
|
}
|
|
@@ -2058,12 +2117,12 @@
|
|
|
2058
2117
|
return [3 /*break*/, 15];
|
|
2059
2118
|
case 20: return [3 /*break*/, 23];
|
|
2060
2119
|
case 21:
|
|
2061
|
-
e_2_1 =
|
|
2120
|
+
e_2_1 = _o.sent();
|
|
2062
2121
|
e_2 = { error: e_2_1 };
|
|
2063
2122
|
return [3 /*break*/, 23];
|
|
2064
2123
|
case 22:
|
|
2065
2124
|
try {
|
|
2066
|
-
if (_d && !_d.done && (
|
|
2125
|
+
if (_d && !_d.done && (_j = _c.return)) _j.call(_c);
|
|
2067
2126
|
}
|
|
2068
2127
|
finally { if (e_2) throw e_2.error; }
|
|
2069
2128
|
return [7 /*endfinally*/];
|
|
@@ -2075,7 +2134,7 @@
|
|
|
2075
2134
|
throw scriptExecutionErrors[0];
|
|
2076
2135
|
}
|
|
2077
2136
|
else {
|
|
2078
|
-
throw new PromptbookExecutionError(
|
|
2137
|
+
throw new PromptbookExecutionError(spacetrim.spaceTrim(function (block) { return "\n Script execution failed ".concat(scriptExecutionErrors.length, " times\n\n ").concat(block(scriptExecutionErrors
|
|
2079
2138
|
.map(function (error) { return '- ' + error.message; })
|
|
2080
2139
|
.join('\n\n')), "\n "); }));
|
|
2081
2140
|
}
|
|
@@ -2089,46 +2148,46 @@
|
|
|
2089
2148
|
})];
|
|
2090
2149
|
case 25:
|
|
2091
2150
|
// TODO: [๐น] When making next attempt for `PROMPT DIALOG`, preserve the previous user input
|
|
2092
|
-
resultString =
|
|
2151
|
+
resultString = _o.sent();
|
|
2093
2152
|
return [3 /*break*/, 27];
|
|
2094
2153
|
case 26: throw new PromptbookExecutionError(
|
|
2095
2154
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2096
2155
|
"Unknown execution type \"".concat(currentTemplate.executionType, "\""));
|
|
2097
2156
|
case 27:
|
|
2098
2157
|
if (!(!isJokerAttempt && currentTemplate.postprocessing)) return [3 /*break*/, 44];
|
|
2099
|
-
|
|
2158
|
+
_o.label = 28;
|
|
2100
2159
|
case 28:
|
|
2101
|
-
|
|
2160
|
+
_o.trys.push([28, 42, 43, 44]);
|
|
2102
2161
|
_e = (e_4 = void 0, __values(currentTemplate.postprocessing)), _f = _e.next();
|
|
2103
|
-
|
|
2162
|
+
_o.label = 29;
|
|
2104
2163
|
case 29:
|
|
2105
2164
|
if (!!_f.done) return [3 /*break*/, 41];
|
|
2106
2165
|
functionName = _f.value;
|
|
2107
2166
|
// TODO: DRY [1]
|
|
2108
2167
|
scriptExecutionErrors = [];
|
|
2109
2168
|
postprocessingError = null;
|
|
2110
|
-
|
|
2169
|
+
_o.label = 30;
|
|
2111
2170
|
case 30:
|
|
2112
|
-
|
|
2171
|
+
_o.trys.push([30, 37, 38, 39]);
|
|
2113
2172
|
_g = (e_3 = void 0, __values(tools.script)), _h = _g.next();
|
|
2114
|
-
|
|
2173
|
+
_o.label = 31;
|
|
2115
2174
|
case 31:
|
|
2116
2175
|
if (!!_h.done) return [3 /*break*/, 36];
|
|
2117
2176
|
scriptTools = _h.value;
|
|
2118
|
-
|
|
2177
|
+
_o.label = 32;
|
|
2119
2178
|
case 32:
|
|
2120
|
-
|
|
2179
|
+
_o.trys.push([32, 34, , 35]);
|
|
2121
2180
|
return [4 /*yield*/, scriptTools.execute({
|
|
2122
2181
|
scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
|
|
2123
2182
|
script: "".concat(functionName, "(resultString)"),
|
|
2124
2183
|
parameters: __assign(__assign({}, parametersToPass), { resultString: resultString || '' }),
|
|
2125
2184
|
})];
|
|
2126
2185
|
case 33:
|
|
2127
|
-
resultString =
|
|
2186
|
+
resultString = _o.sent();
|
|
2128
2187
|
postprocessingError = null;
|
|
2129
2188
|
return [3 /*break*/, 36];
|
|
2130
2189
|
case 34:
|
|
2131
|
-
error_3 =
|
|
2190
|
+
error_3 = _o.sent();
|
|
2132
2191
|
if (!(error_3 instanceof Error)) {
|
|
2133
2192
|
throw error_3;
|
|
2134
2193
|
}
|
|
@@ -2140,12 +2199,12 @@
|
|
|
2140
2199
|
return [3 /*break*/, 31];
|
|
2141
2200
|
case 36: return [3 /*break*/, 39];
|
|
2142
2201
|
case 37:
|
|
2143
|
-
e_3_1 =
|
|
2202
|
+
e_3_1 = _o.sent();
|
|
2144
2203
|
e_3 = { error: e_3_1 };
|
|
2145
2204
|
return [3 /*break*/, 39];
|
|
2146
2205
|
case 38:
|
|
2147
2206
|
try {
|
|
2148
|
-
if (_h && !_h.done && (
|
|
2207
|
+
if (_h && !_h.done && (_l = _g.return)) _l.call(_g);
|
|
2149
2208
|
}
|
|
2150
2209
|
finally { if (e_3) throw e_3.error; }
|
|
2151
2210
|
return [7 /*endfinally*/];
|
|
@@ -2153,22 +2212,23 @@
|
|
|
2153
2212
|
if (postprocessingError) {
|
|
2154
2213
|
throw postprocessingError;
|
|
2155
2214
|
}
|
|
2156
|
-
|
|
2215
|
+
_o.label = 40;
|
|
2157
2216
|
case 40:
|
|
2158
2217
|
_f = _e.next();
|
|
2159
2218
|
return [3 /*break*/, 29];
|
|
2160
2219
|
case 41: return [3 /*break*/, 44];
|
|
2161
2220
|
case 42:
|
|
2162
|
-
e_4_1 =
|
|
2221
|
+
e_4_1 = _o.sent();
|
|
2163
2222
|
e_4 = { error: e_4_1 };
|
|
2164
2223
|
return [3 /*break*/, 44];
|
|
2165
2224
|
case 43:
|
|
2166
2225
|
try {
|
|
2167
|
-
if (_f && !_f.done && (
|
|
2226
|
+
if (_f && !_f.done && (_k = _e.return)) _k.call(_e);
|
|
2168
2227
|
}
|
|
2169
2228
|
finally { if (e_4) throw e_4.error; }
|
|
2170
2229
|
return [7 /*endfinally*/];
|
|
2171
2230
|
case 44:
|
|
2231
|
+
// TODO: [๐] Unite object for expecting amount and format
|
|
2172
2232
|
if (currentTemplate.expectFormat) {
|
|
2173
2233
|
if (currentTemplate.expectFormat === 'JSON') {
|
|
2174
2234
|
if (!isValidJsonString(resultString || '')) {
|
|
@@ -2176,30 +2236,13 @@
|
|
|
2176
2236
|
}
|
|
2177
2237
|
}
|
|
2178
2238
|
}
|
|
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
|
-
}
|
|
2239
|
+
// TODO: [๐] Unite object for expecting amount and format
|
|
2240
|
+
if (currentTemplate.expectations) {
|
|
2241
|
+
checkExpectations(currentTemplate.expectations, resultString || '');
|
|
2199
2242
|
}
|
|
2200
2243
|
return [3 /*break*/, 49];
|
|
2201
2244
|
case 45:
|
|
2202
|
-
error_4 =
|
|
2245
|
+
error_4 = _o.sent();
|
|
2203
2246
|
if (!(error_4 instanceof ExpectError)) {
|
|
2204
2247
|
throw error_4;
|
|
2205
2248
|
}
|
|
@@ -2218,6 +2261,7 @@
|
|
|
2218
2261
|
title: currentTemplate.title /* <- Note: If title in promptbook contains emojis, pass it innto report */,
|
|
2219
2262
|
content: prompt.content,
|
|
2220
2263
|
modelRequirements: prompt.modelRequirements,
|
|
2264
|
+
expectations: prompt.expectations,
|
|
2221
2265
|
// <- Note: Do want to pass ONLY wanted information to the report
|
|
2222
2266
|
},
|
|
2223
2267
|
result: result || undefined,
|
|
@@ -2227,9 +2271,9 @@
|
|
|
2227
2271
|
return [7 /*endfinally*/];
|
|
2228
2272
|
case 47:
|
|
2229
2273
|
if (expectError !== null && attempt === maxAttempts - 1) {
|
|
2230
|
-
throw new PromptbookExecutionError(
|
|
2274
|
+
throw new PromptbookExecutionError(spacetrim.spaceTrim(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
2275
|
}
|
|
2232
|
-
|
|
2276
|
+
_o.label = 48;
|
|
2233
2277
|
case 48:
|
|
2234
2278
|
attempt++;
|
|
2235
2279
|
return [3 /*break*/, 3];
|
|
@@ -2249,7 +2293,7 @@
|
|
|
2249
2293
|
// <- [3]
|
|
2250
2294
|
});
|
|
2251
2295
|
}
|
|
2252
|
-
parametersToPass = __assign(__assign({}, parametersToPass), (
|
|
2296
|
+
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
2297
|
return [2 /*return*/];
|
|
2254
2298
|
}
|
|
2255
2299
|
});
|
|
@@ -2296,7 +2340,7 @@
|
|
|
2296
2340
|
return template.dependentParameterNames.every(function (name) { return resovedParameters_1.includes(name); });
|
|
2297
2341
|
});
|
|
2298
2342
|
if (!(!currentTemplate && resolving_1.length === 0)) return [3 /*break*/, 1];
|
|
2299
|
-
throw new UnexpectedError(
|
|
2343
|
+
throw new UnexpectedError(spacetrim.spaceTrim("\n Can not resolve some parameters\n\n Note: This should be catched during validatePromptbookJson\n "));
|
|
2300
2344
|
case 1:
|
|
2301
2345
|
if (!!currentTemplate) return [3 /*break*/, 3];
|
|
2302
2346
|
/* [5] */ return [4 /*yield*/, Promise.race(resolving_1)];
|
|
@@ -2402,7 +2446,7 @@
|
|
|
2402
2446
|
console.info('๐ฌ Mocked gptChat call');
|
|
2403
2447
|
}
|
|
2404
2448
|
return [2 /*return*/, {
|
|
2405
|
-
content:
|
|
2449
|
+
content: spacetrim.spaceTrim(function (block) { return "\n You said:\n ".concat(block(prompt.content), "\n "); }),
|
|
2406
2450
|
model: 'mocked-echo',
|
|
2407
2451
|
timing: {
|
|
2408
2452
|
start: getCurrentIsoDate(),
|
|
@@ -2431,7 +2475,7 @@
|
|
|
2431
2475
|
console.info('๐ Mocked gptComplete call');
|
|
2432
2476
|
}
|
|
2433
2477
|
return [2 /*return*/, {
|
|
2434
|
-
content:
|
|
2478
|
+
content: spacetrim.spaceTrim(function (block) { return "\n ".concat(block(prompt.content), "\n And so on...\n "); }),
|
|
2435
2479
|
model: 'mocked-echo',
|
|
2436
2480
|
timing: {
|
|
2437
2481
|
start: getCurrentIsoDate(),
|
|
@@ -2453,9 +2497,144 @@
|
|
|
2453
2497
|
return MockedEchoNaturalExecutionTools;
|
|
2454
2498
|
}());
|
|
2455
2499
|
/**
|
|
2456
|
-
* TODO: Allow in spaceTrim: nesting with > ${block(prompt.request)}
|
|
2500
|
+
* TODO: Allow in spaceTrim: nesting with > ${block(prompt.request)}, same as replace params
|
|
2501
|
+
*/
|
|
2502
|
+
|
|
2503
|
+
/**
|
|
2504
|
+
* Gets the expectations and creates a fake text that meets the expectations
|
|
2505
|
+
*
|
|
2506
|
+
* Note: You can provide postprocessing functions to modify the text before checking the expectations
|
|
2507
|
+
* The result will be the text BEFORE the postprocessing
|
|
2508
|
+
*
|
|
2509
|
+
* @private internal util for MockedFackedNaturalExecutionTools
|
|
2510
|
+
*/
|
|
2511
|
+
function $fakeTextToExpectations(expectations, postprocessing) {
|
|
2512
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
2513
|
+
var lorem, loremText, text, loopLimit, textToCheck, _a, _b, func, e_1_1;
|
|
2514
|
+
var e_1, _c;
|
|
2515
|
+
return __generator(this, function (_d) {
|
|
2516
|
+
switch (_d.label) {
|
|
2517
|
+
case 0:
|
|
2518
|
+
lorem = new loremIpsum.LoremIpsum({
|
|
2519
|
+
wordsPerSentence: { min: 5, max: 15 },
|
|
2520
|
+
sentencesPerParagraph: { min: 5, max: 15 },
|
|
2521
|
+
});
|
|
2522
|
+
loremText = '';
|
|
2523
|
+
text = '';
|
|
2524
|
+
loopLimit = CHARACTER_LOOP_LIMIT;
|
|
2525
|
+
_d.label = 1;
|
|
2526
|
+
case 1:
|
|
2527
|
+
if (!(loopLimit-- > 0)) return [3 /*break*/, 11];
|
|
2528
|
+
textToCheck = text;
|
|
2529
|
+
_d.label = 2;
|
|
2530
|
+
case 2:
|
|
2531
|
+
_d.trys.push([2, 7, 8, 9]);
|
|
2532
|
+
_a = (e_1 = void 0, __values(postprocessing || [])), _b = _a.next();
|
|
2533
|
+
_d.label = 3;
|
|
2534
|
+
case 3:
|
|
2535
|
+
if (!!_b.done) return [3 /*break*/, 6];
|
|
2536
|
+
func = _b.value;
|
|
2537
|
+
return [4 /*yield*/, func(textToCheck)];
|
|
2538
|
+
case 4:
|
|
2539
|
+
textToCheck = _d.sent();
|
|
2540
|
+
_d.label = 5;
|
|
2541
|
+
case 5:
|
|
2542
|
+
_b = _a.next();
|
|
2543
|
+
return [3 /*break*/, 3];
|
|
2544
|
+
case 6: return [3 /*break*/, 9];
|
|
2545
|
+
case 7:
|
|
2546
|
+
e_1_1 = _d.sent();
|
|
2547
|
+
e_1 = { error: e_1_1 };
|
|
2548
|
+
return [3 /*break*/, 9];
|
|
2549
|
+
case 8:
|
|
2550
|
+
try {
|
|
2551
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
2552
|
+
}
|
|
2553
|
+
finally { if (e_1) throw e_1.error; }
|
|
2554
|
+
return [7 /*endfinally*/];
|
|
2555
|
+
case 9:
|
|
2556
|
+
if (isPassingExpectations(expectations, textToCheck)) {
|
|
2557
|
+
return [2 /*return*/, text]; // <- Note: Returning the text because the postprocessing
|
|
2558
|
+
}
|
|
2559
|
+
if (loremText === '') {
|
|
2560
|
+
loremText = lorem.generateParagraphs(1) + '\n\n';
|
|
2561
|
+
}
|
|
2562
|
+
text += loremText.substring(0, 1);
|
|
2563
|
+
loremText = loremText.substring(1);
|
|
2564
|
+
_d.label = 10;
|
|
2565
|
+
case 10: return [3 /*break*/, 1];
|
|
2566
|
+
case 11: throw new Error(spacetrim.spaceTrim(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 "); }));
|
|
2567
|
+
}
|
|
2568
|
+
});
|
|
2569
|
+
});
|
|
2570
|
+
}
|
|
2571
|
+
/**
|
|
2572
|
+
* TODO: Implement better - create FakeLLM from this
|
|
2573
|
+
* TODO: [๐] Unite object for expecting amount and format - use here also a format
|
|
2457
2574
|
*/
|
|
2458
2575
|
|
|
2576
|
+
/**
|
|
2577
|
+
* Mocked execution Tools for just faking expected responses for testing purposes
|
|
2578
|
+
*/
|
|
2579
|
+
var MockedFackedNaturalExecutionTools = /** @class */ (function () {
|
|
2580
|
+
function MockedFackedNaturalExecutionTools(options) {
|
|
2581
|
+
this.options = options;
|
|
2582
|
+
}
|
|
2583
|
+
/**
|
|
2584
|
+
* Fakes chat model
|
|
2585
|
+
*/
|
|
2586
|
+
MockedFackedNaturalExecutionTools.prototype.gptChat = function (prompt) {
|
|
2587
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
2588
|
+
var content, result;
|
|
2589
|
+
return __generator(this, function (_a) {
|
|
2590
|
+
switch (_a.label) {
|
|
2591
|
+
case 0:
|
|
2592
|
+
if (this.options.isVerbose) {
|
|
2593
|
+
console.info('๐ฌ Mocked faked prompt', prompt);
|
|
2594
|
+
}
|
|
2595
|
+
return [4 /*yield*/, $fakeTextToExpectations(prompt.expectations || {
|
|
2596
|
+
sentences: { min: 1, max: 1 },
|
|
2597
|
+
}, prompt.postprocessing)];
|
|
2598
|
+
case 1:
|
|
2599
|
+
content = _a.sent();
|
|
2600
|
+
result = {
|
|
2601
|
+
content: content,
|
|
2602
|
+
model: 'mocked-facked',
|
|
2603
|
+
timing: {
|
|
2604
|
+
start: getCurrentIsoDate(),
|
|
2605
|
+
complete: getCurrentIsoDate(),
|
|
2606
|
+
},
|
|
2607
|
+
usage: {
|
|
2608
|
+
price: 0,
|
|
2609
|
+
inputTokens: 0,
|
|
2610
|
+
outputTokens: 0,
|
|
2611
|
+
},
|
|
2612
|
+
rawResponse: {
|
|
2613
|
+
note: 'This is mocked echo',
|
|
2614
|
+
},
|
|
2615
|
+
// <- [๐คนโโ๏ธ]
|
|
2616
|
+
};
|
|
2617
|
+
if (this.options.isVerbose) {
|
|
2618
|
+
console.info('๐ฌ Mocked faked result', result);
|
|
2619
|
+
}
|
|
2620
|
+
return [2 /*return*/, result];
|
|
2621
|
+
}
|
|
2622
|
+
});
|
|
2623
|
+
});
|
|
2624
|
+
};
|
|
2625
|
+
/**
|
|
2626
|
+
* Fakes completion model
|
|
2627
|
+
*/
|
|
2628
|
+
MockedFackedNaturalExecutionTools.prototype.gptComplete = function (prompt) {
|
|
2629
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
2630
|
+
return __generator(this, function (_a) {
|
|
2631
|
+
return [2 /*return*/, this.gptChat(prompt)];
|
|
2632
|
+
});
|
|
2633
|
+
});
|
|
2634
|
+
};
|
|
2635
|
+
return MockedFackedNaturalExecutionTools;
|
|
2636
|
+
}());
|
|
2637
|
+
|
|
2459
2638
|
/**
|
|
2460
2639
|
* Delagates the user interaction to a async callback function
|
|
2461
2640
|
* You need to provide your own implementation of this callback function and its bind to UI.
|
|
@@ -2476,7 +2655,7 @@
|
|
|
2476
2655
|
case 1:
|
|
2477
2656
|
answer = _a.sent();
|
|
2478
2657
|
if (this.options.isVerbose) {
|
|
2479
|
-
console.info(
|
|
2658
|
+
console.info(spacetrim.spaceTrim(function (block) { return "\n \uD83D\uDCD6 ".concat(block(options.promptTitle), "\n \uD83D\uDC64 ").concat(block(answer), "\n "); }));
|
|
2480
2659
|
}
|
|
2481
2660
|
return [2 /*return*/, answer];
|
|
2482
2661
|
}
|
|
@@ -2503,9 +2682,9 @@
|
|
|
2503
2682
|
return __awaiter(this, void 0, void 0, function () {
|
|
2504
2683
|
var answer;
|
|
2505
2684
|
return __generator(this, function (_a) {
|
|
2506
|
-
answer = window.prompt(
|
|
2685
|
+
answer = window.prompt(spacetrim.spaceTrim(function (block) { return "\n ".concat(block(options.promptTitle), "\n\n ").concat(block(options.promptMessage), "\n "); }));
|
|
2507
2686
|
if (this.options.isVerbose) {
|
|
2508
|
-
console.info(
|
|
2687
|
+
console.info(spacetrim.spaceTrim(function (block) { return "\n \uD83D\uDCD6 ".concat(block(options.promptTitle), "\n \uD83D\uDC64 ").concat(block(answer || '๐ซ User cancelled prompt'), "\n "); }));
|
|
2509
2688
|
}
|
|
2510
2689
|
if (answer === null) {
|
|
2511
2690
|
throw new PromptbookExecutionError('User cancelled prompt');
|
|
@@ -2570,7 +2749,7 @@
|
|
|
2570
2749
|
for (var promptbooks_1 = __values(promptbooks), promptbooks_1_1 = promptbooks_1.next(); !promptbooks_1_1.done; promptbooks_1_1 = promptbooks_1.next()) {
|
|
2571
2750
|
var promptbook = promptbooks_1_1.value;
|
|
2572
2751
|
if (promptbook.promptbookUrl === undefined) {
|
|
2573
|
-
throw new PromptbookReferenceError(
|
|
2752
|
+
throw new PromptbookReferenceError(spacetrim.spaceTrim("\n Promptbook with name \"".concat(promptbook.title, "\" does not have defined URL\n\n Note: Promptbooks without URLs are called anonymous promptbooks\n They can be used as standalone promptbooks, but they cannot be referenced by other promptbooks\n And also they cannot be used in the promptbook library\n\n ")));
|
|
2574
2753
|
}
|
|
2575
2754
|
validatePromptbookJson(promptbook);
|
|
2576
2755
|
this.library.set(promptbook.promptbookUrl, promptbook);
|
|
@@ -2599,7 +2778,7 @@
|
|
|
2599
2778
|
var _this = this;
|
|
2600
2779
|
var promptbook = this.library.get(url);
|
|
2601
2780
|
if (!promptbook) {
|
|
2602
|
-
throw new PromptbookNotFoundError(
|
|
2781
|
+
throw new PromptbookNotFoundError(spacetrim.spaceTrim(function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPromptbooks()
|
|
2603
2782
|
.map(function (promptbookUrl) { return "- ".concat(promptbookUrl); })
|
|
2604
2783
|
.join('\n')), "\n\n "); }));
|
|
2605
2784
|
}
|
|
@@ -2750,7 +2929,7 @@
|
|
|
2750
2929
|
case 0:
|
|
2751
2930
|
if (!!predicate(url)) return [3 /*break*/, 2];
|
|
2752
2931
|
_a = PromptbookNotFoundError.bind;
|
|
2753
|
-
return [4 /*yield*/,
|
|
2932
|
+
return [4 /*yield*/, spacetrim.spaceTrim(function (block) { return __awaiter(_this, void 0, void 0, function () {
|
|
2754
2933
|
var _a, _b, _c, _d, _e, _f;
|
|
2755
2934
|
return __generator(this, function (_g) {
|
|
2756
2935
|
switch (_g.label) {
|
|
@@ -2802,6 +2981,7 @@
|
|
|
2802
2981
|
exports.CallbackInterfaceTools = CallbackInterfaceTools;
|
|
2803
2982
|
exports.ExecutionTypes = ExecutionTypes;
|
|
2804
2983
|
exports.MockedEchoNaturalExecutionTools = MockedEchoNaturalExecutionTools;
|
|
2984
|
+
exports.MockedFackedNaturalExecutionTools = MockedFackedNaturalExecutionTools;
|
|
2805
2985
|
exports.PROMPTBOOK_VERSION = PROMPTBOOK_VERSION;
|
|
2806
2986
|
exports.SimplePromptInterfaceTools = SimplePromptInterfaceTools;
|
|
2807
2987
|
exports.SimplePromptbookLibrary = SimplePromptbookLibrary;
|