@promptbook/core 0.44.0-4 → 0.44.0-5
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 +210 -161
- package/esm/index.es.js.map +1 -1
- package/esm/typings/_packages/utils.index.d.ts +3 -2
- package/esm/typings/execution/plugins/natural-execution-tools/mocked/fakeTextToExpectations.d.ts +4 -0
- package/esm/typings/execution/plugins/natural-execution-tools/mocked/fakeTextToExpectations.test.d.ts +1 -0
- package/esm/typings/execution/utils/checkExpectations.d.ts +25 -0
- package/esm/typings/execution/utils/checkExpectations.test.d.ts +1 -0
- package/esm/typings/types/PromptbookJson/PromptTemplateJson.d.ts +1 -0
- package/package.json +1 -1
- package/umd/index.umd.js +210 -161
- package/umd/index.umd.js.map +1 -1
- package/umd/typings/_packages/utils.index.d.ts +3 -2
- package/umd/typings/execution/plugins/natural-execution-tools/mocked/fakeTextToExpectations.d.ts +4 -0
- package/umd/typings/execution/plugins/natural-execution-tools/mocked/fakeTextToExpectations.test.d.ts +1 -0
- package/umd/typings/execution/utils/checkExpectations.d.ts +25 -0
- package/umd/typings/execution/utils/checkExpectations.test.d.ts +1 -0
- package/umd/typings/types/PromptbookJson/PromptTemplateJson.d.ts +1 -0
package/umd/index.umd.js
CHANGED
|
@@ -247,86 +247,22 @@
|
|
|
247
247
|
}(Error));
|
|
248
248
|
|
|
249
249
|
/**
|
|
250
|
-
*
|
|
251
|
-
*/
|
|
252
|
-
var LOOP_LIMIT = 1000;
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* This error occurs during the parameter replacement in the template
|
|
250
|
+
* This error occurs when some expectation is not met in the execution of the pipeline
|
|
256
251
|
*
|
|
257
|
-
*
|
|
252
|
+
* @private Always catched and rethrown as `PromptbookExecutionError`
|
|
253
|
+
* Note: This is a kindof subtype of PromptbookExecutionError
|
|
258
254
|
*/
|
|
259
|
-
var
|
|
260
|
-
__extends(
|
|
261
|
-
function
|
|
255
|
+
var ExpectError = /** @class */ (function (_super) {
|
|
256
|
+
__extends(ExpectError, _super);
|
|
257
|
+
function ExpectError(message) {
|
|
262
258
|
var _this = _super.call(this, message) || this;
|
|
263
|
-
_this.name = '
|
|
264
|
-
Object.setPrototypeOf(_this,
|
|
259
|
+
_this.name = 'ExpectError';
|
|
260
|
+
Object.setPrototypeOf(_this, ExpectError.prototype);
|
|
265
261
|
return _this;
|
|
266
262
|
}
|
|
267
|
-
return
|
|
263
|
+
return ExpectError;
|
|
268
264
|
}(Error));
|
|
269
265
|
|
|
270
|
-
/**
|
|
271
|
-
* Replaces parameters in template with values from parameters object
|
|
272
|
-
*
|
|
273
|
-
* @param template the template with parameters in {curly} braces
|
|
274
|
-
* @param parameters the object with parameters
|
|
275
|
-
* @returns the template with replaced parameters
|
|
276
|
-
* @throws {TemplateError} if parameter is not defined, not closed, or not opened
|
|
277
|
-
*
|
|
278
|
-
* @private within the createPromptbookExecutor
|
|
279
|
-
*/
|
|
280
|
-
function replaceParameters(template, parameters) {
|
|
281
|
-
var replacedTemplate = template;
|
|
282
|
-
var match;
|
|
283
|
-
var loopLimit = LOOP_LIMIT;
|
|
284
|
-
var _loop_1 = function () {
|
|
285
|
-
if (loopLimit-- < 0) {
|
|
286
|
-
throw new UnexpectedError('Loop limit reached during parameters replacement in `replaceParameters`');
|
|
287
|
-
}
|
|
288
|
-
var precol = match.groups.precol;
|
|
289
|
-
var parameterName = match.groups.parameterName;
|
|
290
|
-
if (parameterName === '') {
|
|
291
|
-
return "continue";
|
|
292
|
-
}
|
|
293
|
-
if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
|
|
294
|
-
throw new TemplateError('Parameter is already opened or not closed');
|
|
295
|
-
}
|
|
296
|
-
if (parameters[parameterName] === undefined) {
|
|
297
|
-
throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
|
|
298
|
-
}
|
|
299
|
-
var parameterValue = parameters[parameterName];
|
|
300
|
-
if (parameterValue === undefined) {
|
|
301
|
-
throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
|
|
302
|
-
}
|
|
303
|
-
parameterValue = parameterValue.toString();
|
|
304
|
-
if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
|
|
305
|
-
parameterValue = parameterValue
|
|
306
|
-
.split('\n')
|
|
307
|
-
.map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
|
|
308
|
-
.join('\n');
|
|
309
|
-
}
|
|
310
|
-
replacedTemplate =
|
|
311
|
-
replacedTemplate.substring(0, match.index + precol.length) +
|
|
312
|
-
parameterValue +
|
|
313
|
-
replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
|
|
314
|
-
};
|
|
315
|
-
while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
|
|
316
|
-
.exec(replacedTemplate))) {
|
|
317
|
-
_loop_1();
|
|
318
|
-
}
|
|
319
|
-
// [💫] Check if there are parameters that are not closed properly
|
|
320
|
-
if (/{\w+$/.test(replacedTemplate)) {
|
|
321
|
-
throw new TemplateError('Parameter is not closed');
|
|
322
|
-
}
|
|
323
|
-
// [💫] Check if there are parameters that are not opened properly
|
|
324
|
-
if (/^\w+}/.test(replacedTemplate)) {
|
|
325
|
-
throw new TemplateError('Parameter is not opened');
|
|
326
|
-
}
|
|
327
|
-
return replacedTemplate;
|
|
328
|
-
}
|
|
329
|
-
|
|
330
266
|
/**
|
|
331
267
|
* Counts number of characters in the text
|
|
332
268
|
*/
|
|
@@ -654,6 +590,144 @@
|
|
|
654
590
|
PAGES: countPages,
|
|
655
591
|
};
|
|
656
592
|
|
|
593
|
+
/**
|
|
594
|
+
* Function checkExpectations will check if the expectations on given value are met
|
|
595
|
+
*
|
|
596
|
+
* Note: There are two simmilar functions:
|
|
597
|
+
* - `checkExpectations` which throws an error if the expectations are not met
|
|
598
|
+
* - `isPassingExpectations` which returns a boolean
|
|
599
|
+
*
|
|
600
|
+
* @throws {ExpectError} if the expectations are not met
|
|
601
|
+
* @returns {void} Nothing
|
|
602
|
+
*/
|
|
603
|
+
function checkExpectations(expectations, value) {
|
|
604
|
+
var e_1, _a;
|
|
605
|
+
try {
|
|
606
|
+
for (var _b = __values(Object.entries(expectations)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
607
|
+
var _d = __read(_c.value, 2), unit = _d[0], _e = _d[1], max = _e.max, min = _e.min;
|
|
608
|
+
var amount = CountUtils[unit.toUpperCase()](value);
|
|
609
|
+
if (min && amount < min) {
|
|
610
|
+
throw new ExpectError("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
|
|
611
|
+
} /* not else */
|
|
612
|
+
if (max && amount > max) {
|
|
613
|
+
throw new ExpectError("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
618
|
+
finally {
|
|
619
|
+
try {
|
|
620
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
621
|
+
}
|
|
622
|
+
finally { if (e_1) throw e_1.error; }
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* Function checkExpectations will check if the expectations on given value are met
|
|
627
|
+
*
|
|
628
|
+
* Note: There are two simmilar functions:
|
|
629
|
+
* - `checkExpectations` which throws an error if the expectations are not met
|
|
630
|
+
* - `isPassingExpectations` which returns a boolean
|
|
631
|
+
*
|
|
632
|
+
* @returns {boolean} True if the expectations are met
|
|
633
|
+
*/
|
|
634
|
+
function isPassingExpectations(expectations, value) {
|
|
635
|
+
try {
|
|
636
|
+
checkExpectations(expectations, value);
|
|
637
|
+
return true;
|
|
638
|
+
}
|
|
639
|
+
catch (error) {
|
|
640
|
+
if (!(error instanceof ExpectError)) {
|
|
641
|
+
throw error;
|
|
642
|
+
}
|
|
643
|
+
return false;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* TODO: [💝] Unite object for expecting amount and format
|
|
648
|
+
*/
|
|
649
|
+
|
|
650
|
+
/**
|
|
651
|
+
* The maximum number of iterations for a loops
|
|
652
|
+
*/
|
|
653
|
+
var LOOP_LIMIT = 1000;
|
|
654
|
+
|
|
655
|
+
/**
|
|
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
|
+
|
|
657
731
|
/**
|
|
658
732
|
* Function isValidJsonString will tell you if the string is valid JSON or not
|
|
659
733
|
*/
|
|
@@ -1087,7 +1161,7 @@
|
|
|
1087
1161
|
/**
|
|
1088
1162
|
* The version of the Promptbook library
|
|
1089
1163
|
*/
|
|
1090
|
-
var PROMPTBOOK_VERSION = '0.44.0-
|
|
1164
|
+
var PROMPTBOOK_VERSION = '0.44.0-4';
|
|
1091
1165
|
|
|
1092
1166
|
/**
|
|
1093
1167
|
* Parses the given script and returns the list of all used variables that are not defined in the script
|
|
@@ -1155,6 +1229,7 @@
|
|
|
1155
1229
|
*/
|
|
1156
1230
|
var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'PARAGRAPHS', 'LINES', 'PAGES'];
|
|
1157
1231
|
/**
|
|
1232
|
+
* TODO: [💝] Unite object for expecting amount and format - remove expectFormat
|
|
1158
1233
|
* TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
|
|
1159
1234
|
*/
|
|
1160
1235
|
|
|
@@ -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) {
|
|
@@ -2006,14 +2064,14 @@
|
|
|
2006
2064
|
return [3 /*break*/, 11];
|
|
2007
2065
|
case 7: return [4 /*yield*/, tools.natural.gptChat(prompt)];
|
|
2008
2066
|
case 8:
|
|
2009
|
-
chatThread =
|
|
2067
|
+
chatThread = _o.sent();
|
|
2010
2068
|
// TODO: [🍬] Destroy chatThread
|
|
2011
2069
|
result = chatThread;
|
|
2012
2070
|
resultString = chatThread.content;
|
|
2013
2071
|
return [3 /*break*/, 12];
|
|
2014
2072
|
case 9: return [4 /*yield*/, tools.natural.gptComplete(prompt)];
|
|
2015
2073
|
case 10:
|
|
2016
|
-
completionResult =
|
|
2074
|
+
completionResult = _o.sent();
|
|
2017
2075
|
result = completionResult;
|
|
2018
2076
|
resultString = completionResult.content;
|
|
2019
2077
|
return [3 /*break*/, 12];
|
|
@@ -2028,27 +2086,27 @@
|
|
|
2028
2086
|
}
|
|
2029
2087
|
// TODO: DRY [1]
|
|
2030
2088
|
scriptExecutionErrors = [];
|
|
2031
|
-
|
|
2089
|
+
_o.label = 14;
|
|
2032
2090
|
case 14:
|
|
2033
|
-
|
|
2091
|
+
_o.trys.push([14, 21, 22, 23]);
|
|
2034
2092
|
_c = (e_2 = void 0, __values(tools.script)), _d = _c.next();
|
|
2035
|
-
|
|
2093
|
+
_o.label = 15;
|
|
2036
2094
|
case 15:
|
|
2037
2095
|
if (!!_d.done) return [3 /*break*/, 20];
|
|
2038
2096
|
scriptTools = _d.value;
|
|
2039
|
-
|
|
2097
|
+
_o.label = 16;
|
|
2040
2098
|
case 16:
|
|
2041
|
-
|
|
2099
|
+
_o.trys.push([16, 18, , 19]);
|
|
2042
2100
|
return [4 /*yield*/, scriptTools.execute({
|
|
2043
2101
|
scriptLanguage: currentTemplate.contentLanguage,
|
|
2044
2102
|
script: currentTemplate.content,
|
|
2045
2103
|
parameters: parametersToPass,
|
|
2046
2104
|
})];
|
|
2047
2105
|
case 17:
|
|
2048
|
-
resultString =
|
|
2106
|
+
resultString = _o.sent();
|
|
2049
2107
|
return [3 /*break*/, 20];
|
|
2050
2108
|
case 18:
|
|
2051
|
-
error_2 =
|
|
2109
|
+
error_2 = _o.sent();
|
|
2052
2110
|
if (!(error_2 instanceof Error)) {
|
|
2053
2111
|
throw error_2;
|
|
2054
2112
|
}
|
|
@@ -2059,12 +2117,12 @@
|
|
|
2059
2117
|
return [3 /*break*/, 15];
|
|
2060
2118
|
case 20: return [3 /*break*/, 23];
|
|
2061
2119
|
case 21:
|
|
2062
|
-
e_2_1 =
|
|
2120
|
+
e_2_1 = _o.sent();
|
|
2063
2121
|
e_2 = { error: e_2_1 };
|
|
2064
2122
|
return [3 /*break*/, 23];
|
|
2065
2123
|
case 22:
|
|
2066
2124
|
try {
|
|
2067
|
-
if (_d && !_d.done && (
|
|
2125
|
+
if (_d && !_d.done && (_j = _c.return)) _j.call(_c);
|
|
2068
2126
|
}
|
|
2069
2127
|
finally { if (e_2) throw e_2.error; }
|
|
2070
2128
|
return [7 /*endfinally*/];
|
|
@@ -2090,46 +2148,46 @@
|
|
|
2090
2148
|
})];
|
|
2091
2149
|
case 25:
|
|
2092
2150
|
// TODO: [🌹] When making next attempt for `PROMPT DIALOG`, preserve the previous user input
|
|
2093
|
-
resultString =
|
|
2151
|
+
resultString = _o.sent();
|
|
2094
2152
|
return [3 /*break*/, 27];
|
|
2095
2153
|
case 26: throw new PromptbookExecutionError(
|
|
2096
2154
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2097
2155
|
"Unknown execution type \"".concat(currentTemplate.executionType, "\""));
|
|
2098
2156
|
case 27:
|
|
2099
2157
|
if (!(!isJokerAttempt && currentTemplate.postprocessing)) return [3 /*break*/, 44];
|
|
2100
|
-
|
|
2158
|
+
_o.label = 28;
|
|
2101
2159
|
case 28:
|
|
2102
|
-
|
|
2160
|
+
_o.trys.push([28, 42, 43, 44]);
|
|
2103
2161
|
_e = (e_4 = void 0, __values(currentTemplate.postprocessing)), _f = _e.next();
|
|
2104
|
-
|
|
2162
|
+
_o.label = 29;
|
|
2105
2163
|
case 29:
|
|
2106
2164
|
if (!!_f.done) return [3 /*break*/, 41];
|
|
2107
2165
|
functionName = _f.value;
|
|
2108
2166
|
// TODO: DRY [1]
|
|
2109
2167
|
scriptExecutionErrors = [];
|
|
2110
2168
|
postprocessingError = null;
|
|
2111
|
-
|
|
2169
|
+
_o.label = 30;
|
|
2112
2170
|
case 30:
|
|
2113
|
-
|
|
2171
|
+
_o.trys.push([30, 37, 38, 39]);
|
|
2114
2172
|
_g = (e_3 = void 0, __values(tools.script)), _h = _g.next();
|
|
2115
|
-
|
|
2173
|
+
_o.label = 31;
|
|
2116
2174
|
case 31:
|
|
2117
2175
|
if (!!_h.done) return [3 /*break*/, 36];
|
|
2118
2176
|
scriptTools = _h.value;
|
|
2119
|
-
|
|
2177
|
+
_o.label = 32;
|
|
2120
2178
|
case 32:
|
|
2121
|
-
|
|
2179
|
+
_o.trys.push([32, 34, , 35]);
|
|
2122
2180
|
return [4 /*yield*/, scriptTools.execute({
|
|
2123
2181
|
scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
|
|
2124
2182
|
script: "".concat(functionName, "(resultString)"),
|
|
2125
2183
|
parameters: __assign(__assign({}, parametersToPass), { resultString: resultString || '' }),
|
|
2126
2184
|
})];
|
|
2127
2185
|
case 33:
|
|
2128
|
-
resultString =
|
|
2186
|
+
resultString = _o.sent();
|
|
2129
2187
|
postprocessingError = null;
|
|
2130
2188
|
return [3 /*break*/, 36];
|
|
2131
2189
|
case 34:
|
|
2132
|
-
error_3 =
|
|
2190
|
+
error_3 = _o.sent();
|
|
2133
2191
|
if (!(error_3 instanceof Error)) {
|
|
2134
2192
|
throw error_3;
|
|
2135
2193
|
}
|
|
@@ -2141,12 +2199,12 @@
|
|
|
2141
2199
|
return [3 /*break*/, 31];
|
|
2142
2200
|
case 36: return [3 /*break*/, 39];
|
|
2143
2201
|
case 37:
|
|
2144
|
-
e_3_1 =
|
|
2202
|
+
e_3_1 = _o.sent();
|
|
2145
2203
|
e_3 = { error: e_3_1 };
|
|
2146
2204
|
return [3 /*break*/, 39];
|
|
2147
2205
|
case 38:
|
|
2148
2206
|
try {
|
|
2149
|
-
if (_h && !_h.done && (
|
|
2207
|
+
if (_h && !_h.done && (_l = _g.return)) _l.call(_g);
|
|
2150
2208
|
}
|
|
2151
2209
|
finally { if (e_3) throw e_3.error; }
|
|
2152
2210
|
return [7 /*endfinally*/];
|
|
@@ -2154,22 +2212,23 @@
|
|
|
2154
2212
|
if (postprocessingError) {
|
|
2155
2213
|
throw postprocessingError;
|
|
2156
2214
|
}
|
|
2157
|
-
|
|
2215
|
+
_o.label = 40;
|
|
2158
2216
|
case 40:
|
|
2159
2217
|
_f = _e.next();
|
|
2160
2218
|
return [3 /*break*/, 29];
|
|
2161
2219
|
case 41: return [3 /*break*/, 44];
|
|
2162
2220
|
case 42:
|
|
2163
|
-
e_4_1 =
|
|
2221
|
+
e_4_1 = _o.sent();
|
|
2164
2222
|
e_4 = { error: e_4_1 };
|
|
2165
2223
|
return [3 /*break*/, 44];
|
|
2166
2224
|
case 43:
|
|
2167
2225
|
try {
|
|
2168
|
-
if (_f && !_f.done && (
|
|
2226
|
+
if (_f && !_f.done && (_k = _e.return)) _k.call(_e);
|
|
2169
2227
|
}
|
|
2170
2228
|
finally { if (e_4) throw e_4.error; }
|
|
2171
2229
|
return [7 /*endfinally*/];
|
|
2172
2230
|
case 44:
|
|
2231
|
+
// TODO: [💝] Unite object for expecting amount and format
|
|
2173
2232
|
if (currentTemplate.expectFormat) {
|
|
2174
2233
|
if (currentTemplate.expectFormat === 'JSON') {
|
|
2175
2234
|
if (!isValidJsonString(resultString || '')) {
|
|
@@ -2177,30 +2236,13 @@
|
|
|
2177
2236
|
}
|
|
2178
2237
|
}
|
|
2179
2238
|
}
|
|
2239
|
+
// TODO: [💝] Unite object for expecting amount and format
|
|
2180
2240
|
if (currentTemplate.expectations) {
|
|
2181
|
-
|
|
2182
|
-
for (_j = (e_5 = void 0, __values(Object.entries(currentTemplate.expectations))), _k = _j.next(); !_k.done; _k = _j.next()) {
|
|
2183
|
-
_l = __read(_k.value, 2), unit = _l[0], _m = _l[1], max = _m.max, min = _m.min;
|
|
2184
|
-
amount = CountUtils[unit.toUpperCase()](resultString || '');
|
|
2185
|
-
if (min && amount < min) {
|
|
2186
|
-
throw new ExpectError("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
|
|
2187
|
-
} /* not else */
|
|
2188
|
-
if (max && amount > max) {
|
|
2189
|
-
throw new ExpectError("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
|
|
2190
|
-
}
|
|
2191
|
-
}
|
|
2192
|
-
}
|
|
2193
|
-
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
2194
|
-
finally {
|
|
2195
|
-
try {
|
|
2196
|
-
if (_k && !_k.done && (_r = _j.return)) _r.call(_j);
|
|
2197
|
-
}
|
|
2198
|
-
finally { if (e_5) throw e_5.error; }
|
|
2199
|
-
}
|
|
2241
|
+
checkExpectations(currentTemplate.expectations, resultString || '');
|
|
2200
2242
|
}
|
|
2201
2243
|
return [3 /*break*/, 49];
|
|
2202
2244
|
case 45:
|
|
2203
|
-
error_4 =
|
|
2245
|
+
error_4 = _o.sent();
|
|
2204
2246
|
if (!(error_4 instanceof ExpectError)) {
|
|
2205
2247
|
throw error_4;
|
|
2206
2248
|
}
|
|
@@ -2231,7 +2273,7 @@
|
|
|
2231
2273
|
if (expectError !== null && attempt === maxAttempts - 1) {
|
|
2232
2274
|
throw new PromptbookExecutionError(spaceTrim__default["default"](function (block) { return "\n Natural execution failed ".concat(maxExecutionAttempts, "x\n\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n "); }));
|
|
2233
2275
|
}
|
|
2234
|
-
|
|
2276
|
+
_o.label = 48;
|
|
2235
2277
|
case 48:
|
|
2236
2278
|
attempt++;
|
|
2237
2279
|
return [3 /*break*/, 3];
|
|
@@ -2251,7 +2293,7 @@
|
|
|
2251
2293
|
// <- [3]
|
|
2252
2294
|
});
|
|
2253
2295
|
}
|
|
2254
|
-
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));
|
|
2255
2297
|
return [2 /*return*/];
|
|
2256
2298
|
}
|
|
2257
2299
|
});
|
|
@@ -2464,19 +2506,26 @@
|
|
|
2464
2506
|
* @private internal util for MockedFackedNaturalExecutionTools
|
|
2465
2507
|
*/
|
|
2466
2508
|
function $fakeTextToExpectations(expectations) {
|
|
2467
|
-
var lorem = new loremIpsum.LoremIpsum({
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2509
|
+
var lorem = new loremIpsum.LoremIpsum({});
|
|
2510
|
+
var loremText = '';
|
|
2511
|
+
var text = '';
|
|
2512
|
+
for (var loopLimit = LOOP_LIMIT; loopLimit-- > 0;) {
|
|
2513
|
+
if (isPassingExpectations(expectations, text)) {
|
|
2514
|
+
return text;
|
|
2515
|
+
}
|
|
2516
|
+
if (loremText === '') {
|
|
2517
|
+
loremText = lorem.generateParagraphs(2);
|
|
2518
|
+
}
|
|
2519
|
+
text += loremText.substring(0, 1);
|
|
2520
|
+
loremText = loremText.substring(1);
|
|
2521
|
+
console.log(text);
|
|
2522
|
+
}
|
|
2523
|
+
throw new Error('Can not generate fake text to met the expectations; Loop limit reached');
|
|
2479
2524
|
}
|
|
2525
|
+
/**
|
|
2526
|
+
* TODO: Implement better
|
|
2527
|
+
* TODO: [💝] Unite object for expecting amount and format - use here also a format
|
|
2528
|
+
*/
|
|
2480
2529
|
|
|
2481
2530
|
/**
|
|
2482
2531
|
* Mocked execution Tools for just faking expected responses for testing purposes
|