@promptbook/core 0.26.0 → 0.27.0-0
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 +245 -181
- package/esm/index.es.js.map +1 -1
- package/esm/typings/conversion/parseCommand.test.d.ts +1 -2
- package/esm/typings/conversion/promptTemplatePipelineStringToJson.d.ts +1 -3
- package/esm/typings/execution/ExpectError.d.ts +10 -0
- package/esm/typings/execution/createPtpExecutor.d.ts +2 -2
- package/esm/typings/execution/plugins/natural-execution-tools/openai/OpenAiExecutionTools.d.ts +1 -1
- package/esm/typings/types/PromptTemplatePipelineJson/PromptTemplateJson.d.ts +22 -14
- package/package.json +1 -1
- package/umd/index.umd.js +244 -180
- package/umd/index.umd.js.map +1 -1
- package/umd/typings/conversion/parseCommand.test.d.ts +1 -2
- package/umd/typings/conversion/promptTemplatePipelineStringToJson.d.ts +1 -3
- package/umd/typings/execution/ExpectError.d.ts +10 -0
- package/umd/typings/execution/createPtpExecutor.d.ts +2 -2
- package/umd/typings/execution/plugins/natural-execution-tools/openai/OpenAiExecutionTools.d.ts +1 -1
- package/umd/typings/types/PromptTemplatePipelineJson/PromptTemplateJson.d.ts +22 -14
package/umd/index.umd.js
CHANGED
|
@@ -22,6 +22,22 @@
|
|
|
22
22
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
23
23
|
PERFORMANCE OF THIS SOFTWARE.
|
|
24
24
|
***************************************************************************** */
|
|
25
|
+
/* global Reflect, Promise */
|
|
26
|
+
|
|
27
|
+
var extendStatics = function(d, b) {
|
|
28
|
+
extendStatics = Object.setPrototypeOf ||
|
|
29
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
30
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
31
|
+
return extendStatics(d, b);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
function __extends(d, b) {
|
|
35
|
+
if (typeof b !== "function" && b !== null)
|
|
36
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
37
|
+
extendStatics(d, b);
|
|
38
|
+
function __() { this.constructor = d; }
|
|
39
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
40
|
+
}
|
|
25
41
|
|
|
26
42
|
var __assign = function() {
|
|
27
43
|
__assign = Object.assign || function __assign(t) {
|
|
@@ -744,12 +760,12 @@
|
|
|
744
760
|
finally { if (e_1) throw e_1.error; }
|
|
745
761
|
}
|
|
746
762
|
var _loop_1 = function (section) {
|
|
747
|
-
var e_3, _e
|
|
763
|
+
var e_3, _e;
|
|
748
764
|
// TODO: Parse prompt template description (the content out of the codeblock and lists)
|
|
749
765
|
var templateModelRequirements = __assign({}, defaultModelRequirements);
|
|
750
|
-
var postprocessingCommands = [];
|
|
751
766
|
var listItems_3 = extractAllListItemsFromMarkdown(section.content);
|
|
752
767
|
var executionType = 'PROMPT_TEMPLATE';
|
|
768
|
+
var postprocessing = [];
|
|
753
769
|
var expectAmount = {};
|
|
754
770
|
var expectFormat = undefined;
|
|
755
771
|
var isExecutionTypeChanged = false;
|
|
@@ -773,7 +789,7 @@
|
|
|
773
789
|
addParam(command);
|
|
774
790
|
break;
|
|
775
791
|
case 'POSTPROCESS':
|
|
776
|
-
|
|
792
|
+
postprocessing.push(command.functionName);
|
|
777
793
|
break;
|
|
778
794
|
case 'EXPECT_AMOUNT':
|
|
779
795
|
// eslint-disable-next-line no-case-declarations
|
|
@@ -810,7 +826,7 @@
|
|
|
810
826
|
}
|
|
811
827
|
finally { if (e_3) throw e_3.error; }
|
|
812
828
|
}
|
|
813
|
-
var
|
|
829
|
+
var _f = extractOneBlockFromMarkdown(section.content), language = _f.language, content = _f.content;
|
|
814
830
|
if (executionType === 'SCRIPT') {
|
|
815
831
|
if (!language) {
|
|
816
832
|
throw new Error('You must specify the language of the script in the prompt template');
|
|
@@ -840,61 +856,25 @@
|
|
|
840
856
|
if (description_1 === '') {
|
|
841
857
|
description_1 = undefined;
|
|
842
858
|
}
|
|
843
|
-
var getParameterName = function (i) {
|
|
844
|
-
var parameterName = postprocessingCommands.length <= i
|
|
845
|
-
? resultingParameterName
|
|
846
|
-
: n12.normalizeTo_camelCase("".concat(resultingParameterName, " before ").concat(postprocessingCommands[i].functionName));
|
|
847
|
-
var isParameterDefined = ptbJson.parameters.some(function (parameter) { return parameter.name === parameterName; });
|
|
848
|
-
if (!isParameterDefined) {
|
|
849
|
-
var parameterDescription = "*(".concat(n12.capitalize(section.title), " postprocessing ").concat(i + 1, "/").concat(postprocessingCommands.length, ")* {").concat(resultingParameterName, "} before `").concat(postprocessingCommands[i].functionName, "`");
|
|
850
|
-
addParam({
|
|
851
|
-
parameterName: parameterName,
|
|
852
|
-
parameterDescription: parameterDescription,
|
|
853
|
-
isInputParameter: false,
|
|
854
|
-
// TODO:> isPrivate: true,
|
|
855
|
-
});
|
|
856
|
-
}
|
|
857
|
-
return parameterName;
|
|
858
|
-
};
|
|
859
859
|
if (Object.keys(expectAmount).length === 0) {
|
|
860
860
|
expectAmount = undefined;
|
|
861
861
|
}
|
|
862
|
+
if (Object.keys(postprocessing).length === 0) {
|
|
863
|
+
postprocessing = undefined;
|
|
864
|
+
}
|
|
862
865
|
ptbJson.promptTemplates.push({
|
|
863
866
|
name: n12.normalizeTo_PascalCase(section.title),
|
|
864
867
|
title: section.title,
|
|
865
868
|
description: description_1,
|
|
866
869
|
executionType: executionType,
|
|
870
|
+
postprocessing: postprocessing,
|
|
867
871
|
expectAmount: expectAmount,
|
|
868
872
|
expectFormat: expectFormat,
|
|
869
873
|
modelRequirements: templateModelRequirements,
|
|
870
874
|
contentLanguage: executionType === 'SCRIPT' ? language : undefined,
|
|
871
875
|
content: content,
|
|
872
|
-
resultingParameterName:
|
|
876
|
+
resultingParameterName: resultingParameterName,
|
|
873
877
|
});
|
|
874
|
-
try {
|
|
875
|
-
for (var _h = (e_4 = void 0, __values(postprocessingCommands.map(function (_a, i) {
|
|
876
|
-
var functionName = _a.functionName;
|
|
877
|
-
return [functionName, i];
|
|
878
|
-
}))), _j = _h.next(); !_j.done; _j = _h.next()) {
|
|
879
|
-
var _k = __read(_j.value, 2), functionName = _k[0], i = _k[1];
|
|
880
|
-
ptbJson.promptTemplates.push({
|
|
881
|
-
name: n12.normalizeTo_PascalCase(section.title + ' Postprocessing ' + i),
|
|
882
|
-
title: "(".concat(i + 1, "/").concat(postprocessingCommands.length, ") ").concat(section.title, " postprocessing"),
|
|
883
|
-
description: "Postprocessing of section ".concat(section.title, " finally with resulting parameter {").concat(resultingParameterName, "}"),
|
|
884
|
-
executionType: 'SCRIPT',
|
|
885
|
-
contentLanguage: 'javascript',
|
|
886
|
-
content: "".concat(functionName, "(").concat(getParameterName(i), ")"),
|
|
887
|
-
resultingParameterName: getParameterName(i + 1),
|
|
888
|
-
});
|
|
889
|
-
}
|
|
890
|
-
}
|
|
891
|
-
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
892
|
-
finally {
|
|
893
|
-
try {
|
|
894
|
-
if (_j && !_j.done && (_f = _h.return)) _f.call(_h);
|
|
895
|
-
}
|
|
896
|
-
finally { if (e_4) throw e_4.error; }
|
|
897
|
-
}
|
|
898
878
|
};
|
|
899
879
|
try {
|
|
900
880
|
for (var _c = __values(markdownStructure.sections), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
@@ -915,9 +895,7 @@
|
|
|
915
895
|
/**
|
|
916
896
|
* TODO: Report here line/column of error
|
|
917
897
|
* TODO: Use spaceTrim more effectively
|
|
918
|
-
* TODO:
|
|
919
|
-
* TODO: !!!! Allow to have non-immutable parameters - suffix them with fooPrevious3 -> fooPrevious2 -> fooPrevious1 -> foo
|
|
920
|
-
* This must work with other technial parameters
|
|
898
|
+
* TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
|
|
921
899
|
*/
|
|
922
900
|
|
|
923
901
|
/**
|
|
@@ -1048,7 +1026,7 @@
|
|
|
1048
1026
|
*/
|
|
1049
1027
|
PromptTemplatePipeline.fromJson = function (ptbkjson) {
|
|
1050
1028
|
validatePromptTemplatePipelineJson(ptbkjson);
|
|
1051
|
-
return new PromptTemplatePipeline(ptbkjson.ptbkUrl ? new URL(ptbkjson.ptbkUrl) : null, ptbkjson.title, ptbkjson.ptbkVersion, ptbkjson.
|
|
1029
|
+
return new PromptTemplatePipeline(ptbkjson.ptbkUrl ? new URL(ptbkjson.ptbkUrl) : null, ptbkjson.title, ptbkjson.ptbkVersion, ptbkjson.description || null, Object.fromEntries(ptbkjson.parameters.map(function (parameter) { return [parameter.name, parameter]; })), ptbkjson.promptTemplates);
|
|
1052
1030
|
};
|
|
1053
1031
|
Object.defineProperty(PromptTemplatePipeline.prototype, "entryPromptTemplate", {
|
|
1054
1032
|
/**
|
|
@@ -1308,6 +1286,23 @@
|
|
|
1308
1286
|
return replacedTemplate;
|
|
1309
1287
|
}
|
|
1310
1288
|
|
|
1289
|
+
/**
|
|
1290
|
+
* This error occurs when some expectation is not met in the execution of the pipeline
|
|
1291
|
+
*/
|
|
1292
|
+
var ExpectError = /** @class */ (function (_super) {
|
|
1293
|
+
__extends(ExpectError, _super);
|
|
1294
|
+
function ExpectError(message) {
|
|
1295
|
+
var _this = _super.call(this, message) || this;
|
|
1296
|
+
_this.name = 'ExpectError';
|
|
1297
|
+
Object.setPrototypeOf(_this, ExpectError.prototype);
|
|
1298
|
+
return _this;
|
|
1299
|
+
}
|
|
1300
|
+
return ExpectError;
|
|
1301
|
+
}(Error));
|
|
1302
|
+
/**
|
|
1303
|
+
* TODO: [🧠] Should be this exported from the library
|
|
1304
|
+
*/
|
|
1305
|
+
|
|
1311
1306
|
/**
|
|
1312
1307
|
* Creates executor function from prompt template pipeline and execution tools.
|
|
1313
1308
|
*
|
|
@@ -1316,7 +1311,7 @@
|
|
|
1316
1311
|
function createPtpExecutor(options) {
|
|
1317
1312
|
var _this = this;
|
|
1318
1313
|
var ptp = options.ptp, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
|
|
1319
|
-
var _b = settings.
|
|
1314
|
+
var _b = settings.maxExecutionAttempts, maxExecutionAttempts = _b === void 0 ? 3 : _b;
|
|
1320
1315
|
var ptpExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
|
|
1321
1316
|
var parametersToPass, currentTemplate, executionReport, _loop_1, error_1;
|
|
1322
1317
|
var _a;
|
|
@@ -1337,10 +1332,10 @@
|
|
|
1337
1332
|
case 1:
|
|
1338
1333
|
_b.trys.push([1, 5, , 6]);
|
|
1339
1334
|
_loop_1 = function () {
|
|
1340
|
-
var resultingParameter, name_1, title, prompt_1, chatThread, completionResult, result, resultString,
|
|
1341
|
-
var e_2,
|
|
1342
|
-
return __generator(this, function (
|
|
1343
|
-
switch (
|
|
1335
|
+
var resultingParameter, name_1, title, prompt_1, chatThread, completionResult, result, resultString, expectError, scriptExecutionErrors, attempt, _c, _d, _e, _f, scriptTools, error_2, e_1_1, _g, _h, functionName, _j, _k, scriptTools, error_3, e_2_1, e_3_1, _l, _m, _o, unit, _p, max, min, amount, error_4;
|
|
1336
|
+
var e_1, _q, e_3, _r, e_2, _s, e_4, _t, _u;
|
|
1337
|
+
return __generator(this, function (_v) {
|
|
1338
|
+
switch (_v.label) {
|
|
1344
1339
|
case 0:
|
|
1345
1340
|
resultingParameter = ptp.getResultingParameter(currentTemplate.name);
|
|
1346
1341
|
name_1 = "ptp-executor-frame-".concat(currentTemplate.name);
|
|
@@ -1356,47 +1351,46 @@
|
|
|
1356
1351
|
parameterValue: null,
|
|
1357
1352
|
})];
|
|
1358
1353
|
case 1:
|
|
1359
|
-
|
|
1360
|
-
|
|
1354
|
+
_v.sent();
|
|
1355
|
+
_v.label = 2;
|
|
1361
1356
|
case 2:
|
|
1362
1357
|
prompt_1 = void 0;
|
|
1363
1358
|
chatThread = void 0;
|
|
1364
1359
|
completionResult = void 0;
|
|
1365
1360
|
result = null;
|
|
1366
1361
|
resultString = null;
|
|
1367
|
-
|
|
1368
|
-
|
|
1362
|
+
expectError = null;
|
|
1363
|
+
attempt = 0;
|
|
1364
|
+
_v.label = 3;
|
|
1365
|
+
case 3:
|
|
1366
|
+
if (!(attempt < maxExecutionAttempts)) return [3 /*break*/, 48];
|
|
1367
|
+
result = null;
|
|
1368
|
+
resultString = null;
|
|
1369
|
+
expectError = null;
|
|
1370
|
+
_v.label = 4;
|
|
1371
|
+
case 4:
|
|
1372
|
+
_v.trys.push([4, 44, 45, 46]);
|
|
1369
1373
|
_c = currentTemplate.executionType;
|
|
1370
1374
|
switch (_c) {
|
|
1371
|
-
case 'SIMPLE_TEMPLATE': return [3 /*break*/,
|
|
1372
|
-
case 'PROMPT_TEMPLATE': return [3 /*break*/,
|
|
1373
|
-
case 'SCRIPT': return [3 /*break*/,
|
|
1374
|
-
case 'PROMPT_DIALOG': return [3 /*break*/,
|
|
1375
|
+
case 'SIMPLE_TEMPLATE': return [3 /*break*/, 5];
|
|
1376
|
+
case 'PROMPT_TEMPLATE': return [3 /*break*/, 6];
|
|
1377
|
+
case 'SCRIPT': return [3 /*break*/, 13];
|
|
1378
|
+
case 'PROMPT_DIALOG': return [3 /*break*/, 24];
|
|
1375
1379
|
}
|
|
1376
|
-
return [3 /*break*/,
|
|
1377
|
-
case
|
|
1380
|
+
return [3 /*break*/, 26];
|
|
1381
|
+
case 5:
|
|
1378
1382
|
resultString = replaceParameters(currentTemplate.content, parametersToPass);
|
|
1379
|
-
return [3 /*break*/,
|
|
1380
|
-
case
|
|
1383
|
+
return [3 /*break*/, 27];
|
|
1384
|
+
case 6:
|
|
1381
1385
|
prompt_1 = {
|
|
1382
1386
|
title: currentTemplate.title,
|
|
1383
1387
|
ptbkUrl: "".concat(ptp.ptbkUrl
|
|
1384
1388
|
? ptp.ptbkUrl.href
|
|
1385
|
-
: 'anonymous' /* <- [🧠]
|
|
1389
|
+
: 'anonymous' /* <- [🧠] !!! How to deal with anonymous PTPs, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
|
|
1386
1390
|
parameters: parametersToPass,
|
|
1387
|
-
content: replaceParameters(currentTemplate.content, parametersToPass)
|
|
1391
|
+
content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
|
|
1388
1392
|
modelRequirements: currentTemplate.modelRequirements,
|
|
1389
1393
|
};
|
|
1390
|
-
attempt = 0;
|
|
1391
|
-
_p.label = 5;
|
|
1392
|
-
case 5:
|
|
1393
|
-
if (!(attempt < maxNaturalExecutionAttempts)) return [3 /*break*/, 17];
|
|
1394
|
-
result = null;
|
|
1395
|
-
resultString = null;
|
|
1396
|
-
naturalExecutionError = null;
|
|
1397
|
-
_p.label = 6;
|
|
1398
|
-
case 6:
|
|
1399
|
-
_p.trys.push([6, 13, 14, 15]);
|
|
1400
1394
|
_d = currentTemplate.modelRequirements.modelVariant;
|
|
1401
1395
|
switch (_d) {
|
|
1402
1396
|
case 'CHAT': return [3 /*break*/, 7];
|
|
@@ -1405,131 +1399,71 @@
|
|
|
1405
1399
|
return [3 /*break*/, 11];
|
|
1406
1400
|
case 7: return [4 /*yield*/, tools.natural.gptChat(prompt_1)];
|
|
1407
1401
|
case 8:
|
|
1408
|
-
chatThread =
|
|
1402
|
+
chatThread = _v.sent();
|
|
1409
1403
|
// TODO: [🍬] Destroy chatThread
|
|
1410
1404
|
result = chatThread;
|
|
1411
1405
|
resultString = chatThread.content;
|
|
1412
1406
|
return [3 /*break*/, 12];
|
|
1413
1407
|
case 9: return [4 /*yield*/, tools.natural.gptComplete(prompt_1)];
|
|
1414
1408
|
case 10:
|
|
1415
|
-
completionResult =
|
|
1409
|
+
completionResult = _v.sent();
|
|
1416
1410
|
result = completionResult;
|
|
1417
1411
|
resultString = completionResult.content;
|
|
1418
1412
|
return [3 /*break*/, 12];
|
|
1419
1413
|
case 11: throw new Error("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
|
|
1420
|
-
case 12:
|
|
1421
|
-
// TODO: !!!!!! Here should postprocessing happen
|
|
1422
|
-
if (currentTemplate.expectFormat) {
|
|
1423
|
-
if (currentTemplate.expectFormat === 'JSON') {
|
|
1424
|
-
if (!isValidJsonString(resultString)) {
|
|
1425
|
-
throw new Error('Expected valid JSON string');
|
|
1426
|
-
}
|
|
1427
|
-
}
|
|
1428
|
-
}
|
|
1429
|
-
if (currentTemplate.expectAmount) {
|
|
1430
|
-
try {
|
|
1431
|
-
for (_e = (e_2 = void 0, __values(Object.entries(currentTemplate.expectAmount))), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
1432
|
-
_g = __read(_f.value, 2), unit = _g[0], _h = _g[1], max = _h.max, min = _h.min;
|
|
1433
|
-
amount = CountUtils[unit.toUpperCase()](resultString);
|
|
1434
|
-
if (min && amount < min) {
|
|
1435
|
-
throw new Error("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
|
|
1436
|
-
} /* not else */
|
|
1437
|
-
if (max && amount > max) {
|
|
1438
|
-
throw new Error("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
|
|
1439
|
-
}
|
|
1440
|
-
}
|
|
1441
|
-
}
|
|
1442
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
1443
|
-
finally {
|
|
1444
|
-
try {
|
|
1445
|
-
if (_f && !_f.done && (_l = _e.return)) _l.call(_e);
|
|
1446
|
-
}
|
|
1447
|
-
finally { if (e_2) throw e_2.error; }
|
|
1448
|
-
}
|
|
1449
|
-
}
|
|
1450
|
-
return [3 /*break*/, 15];
|
|
1414
|
+
case 12: return [3 /*break*/, 27];
|
|
1451
1415
|
case 13:
|
|
1452
|
-
error_2 = _p.sent();
|
|
1453
|
-
if (!(error_2 instanceof Error)) {
|
|
1454
|
-
throw error_2;
|
|
1455
|
-
}
|
|
1456
|
-
naturalExecutionError = error_2;
|
|
1457
|
-
return [3 /*break*/, 15];
|
|
1458
|
-
case 14:
|
|
1459
|
-
executionReport.promptExecutions.push({
|
|
1460
|
-
prompt: {
|
|
1461
|
-
title: prompt_1.title,
|
|
1462
|
-
content: prompt_1.content,
|
|
1463
|
-
modelRequirements: prompt_1.modelRequirements,
|
|
1464
|
-
// <- Note: Do want to pass ONLY wanted information to the report
|
|
1465
|
-
},
|
|
1466
|
-
result: result /* <- !!!!! Look what is exposed here and probbably also filter out */ ||
|
|
1467
|
-
undefined,
|
|
1468
|
-
error: naturalExecutionError || undefined,
|
|
1469
|
-
});
|
|
1470
|
-
return [7 /*endfinally*/];
|
|
1471
|
-
case 15:
|
|
1472
|
-
if (result !== null && naturalExecutionError === null) {
|
|
1473
|
-
return [3 /*break*/, 32];
|
|
1474
|
-
}
|
|
1475
|
-
_p.label = 16;
|
|
1476
|
-
case 16:
|
|
1477
|
-
attempt++;
|
|
1478
|
-
return [3 /*break*/, 5];
|
|
1479
|
-
case 17: throw new Error(spaceTrim__default["default"](function (block) { return "\n Natural execution failed ".concat(settings.maxNaturalExecutionAttempts, "x\n\n ").concat(block((naturalExecutionError === null || naturalExecutionError === void 0 ? void 0 : naturalExecutionError.message) || ''), "\n "); }));
|
|
1480
|
-
case 18:
|
|
1481
1416
|
if (tools.script.length === 0) {
|
|
1482
1417
|
throw new Error('No script execution tools are available');
|
|
1483
1418
|
}
|
|
1484
1419
|
if (!currentTemplate.contentLanguage) {
|
|
1485
1420
|
throw new Error("Script language is not defined for prompt template \"".concat(currentTemplate.name, "\""));
|
|
1486
1421
|
}
|
|
1422
|
+
// TODO: DRY [1]
|
|
1487
1423
|
scriptExecutionErrors = [];
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
_p.trys.push([21, 23, , 24]);
|
|
1424
|
+
_v.label = 14;
|
|
1425
|
+
case 14:
|
|
1426
|
+
_v.trys.push([14, 21, 22, 23]);
|
|
1427
|
+
_e = (e_1 = void 0, __values(tools.script)), _f = _e.next();
|
|
1428
|
+
_v.label = 15;
|
|
1429
|
+
case 15:
|
|
1430
|
+
if (!!_f.done) return [3 /*break*/, 20];
|
|
1431
|
+
scriptTools = _f.value;
|
|
1432
|
+
_v.label = 16;
|
|
1433
|
+
case 16:
|
|
1434
|
+
_v.trys.push([16, 18, , 19]);
|
|
1500
1435
|
return [4 /*yield*/, scriptTools.execute({
|
|
1501
1436
|
scriptLanguage: currentTemplate.contentLanguage,
|
|
1502
1437
|
script: currentTemplate.content,
|
|
1503
1438
|
parameters: parametersToPass,
|
|
1504
1439
|
})];
|
|
1505
|
-
case
|
|
1506
|
-
resultString =
|
|
1507
|
-
isScriptExecutionSuccessful = true;
|
|
1508
|
-
return [3 /*break*/, 25];
|
|
1509
|
-
case 23:
|
|
1510
|
-
error_3 = _p.sent();
|
|
1511
|
-
if (!(error_3 instanceof Error)) {
|
|
1512
|
-
throw error_3;
|
|
1513
|
-
}
|
|
1514
|
-
scriptExecutionErrors.push(error_3);
|
|
1515
|
-
return [3 /*break*/, 24];
|
|
1516
|
-
case 24:
|
|
1517
|
-
_k = _j.next();
|
|
1440
|
+
case 17:
|
|
1441
|
+
resultString = _v.sent();
|
|
1518
1442
|
return [3 /*break*/, 20];
|
|
1519
|
-
case
|
|
1520
|
-
|
|
1521
|
-
|
|
1443
|
+
case 18:
|
|
1444
|
+
error_2 = _v.sent();
|
|
1445
|
+
if (!(error_2 instanceof Error)) {
|
|
1446
|
+
throw error_2;
|
|
1447
|
+
}
|
|
1448
|
+
scriptExecutionErrors.push(error_2);
|
|
1449
|
+
return [3 /*break*/, 19];
|
|
1450
|
+
case 19:
|
|
1451
|
+
_f = _e.next();
|
|
1452
|
+
return [3 /*break*/, 15];
|
|
1453
|
+
case 20: return [3 /*break*/, 23];
|
|
1454
|
+
case 21:
|
|
1455
|
+
e_1_1 = _v.sent();
|
|
1522
1456
|
e_1 = { error: e_1_1 };
|
|
1523
|
-
return [3 /*break*/,
|
|
1524
|
-
case
|
|
1457
|
+
return [3 /*break*/, 23];
|
|
1458
|
+
case 22:
|
|
1525
1459
|
try {
|
|
1526
|
-
if (
|
|
1460
|
+
if (_f && !_f.done && (_q = _e.return)) _q.call(_e);
|
|
1527
1461
|
}
|
|
1528
1462
|
finally { if (e_1) throw e_1.error; }
|
|
1529
1463
|
return [7 /*endfinally*/];
|
|
1530
|
-
case
|
|
1531
|
-
if (
|
|
1532
|
-
return [3 /*break*/,
|
|
1464
|
+
case 23:
|
|
1465
|
+
if (resultString) {
|
|
1466
|
+
return [3 /*break*/, 27];
|
|
1533
1467
|
}
|
|
1534
1468
|
if (scriptExecutionErrors.length === 1) {
|
|
1535
1469
|
throw scriptExecutionErrors[0];
|
|
@@ -1537,19 +1471,149 @@
|
|
|
1537
1471
|
else {
|
|
1538
1472
|
throw new Error(spaceTrim__default["default"](function (block) { return "\n Script execution failed ".concat(scriptExecutionErrors.length, " times\n\n ").concat(block(scriptExecutionErrors.map(function (error) { return '- ' + error.message; }).join('\n\n')), "\n "); }));
|
|
1539
1473
|
}
|
|
1540
|
-
case
|
|
1474
|
+
case 24: return [4 /*yield*/, tools.userInterface.promptDialog({
|
|
1541
1475
|
prompt: replaceParameters(currentTemplate.description || '', parametersToPass),
|
|
1542
1476
|
defaultValue: replaceParameters(currentTemplate.content, parametersToPass),
|
|
1543
1477
|
// TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
|
|
1544
1478
|
placeholder: undefined,
|
|
1545
1479
|
})];
|
|
1546
|
-
case
|
|
1547
|
-
resultString =
|
|
1548
|
-
return [3 /*break*/,
|
|
1549
|
-
case
|
|
1480
|
+
case 25:
|
|
1481
|
+
resultString = _v.sent();
|
|
1482
|
+
return [3 /*break*/, 27];
|
|
1483
|
+
case 26:
|
|
1550
1484
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1551
1485
|
throw new Error("Unknown execution type \"".concat(currentTemplate.executionType, "\""));
|
|
1486
|
+
case 27:
|
|
1487
|
+
if (!currentTemplate.postprocessing) return [3 /*break*/, 43];
|
|
1488
|
+
_v.label = 28;
|
|
1489
|
+
case 28:
|
|
1490
|
+
_v.trys.push([28, 41, 42, 43]);
|
|
1491
|
+
_g = (e_3 = void 0, __values(currentTemplate.postprocessing)), _h = _g.next();
|
|
1492
|
+
_v.label = 29;
|
|
1493
|
+
case 29:
|
|
1494
|
+
if (!!_h.done) return [3 /*break*/, 40];
|
|
1495
|
+
functionName = _h.value;
|
|
1496
|
+
// TODO: DRY [1]
|
|
1497
|
+
scriptExecutionErrors = [];
|
|
1498
|
+
_v.label = 30;
|
|
1499
|
+
case 30:
|
|
1500
|
+
_v.trys.push([30, 37, 38, 39]);
|
|
1501
|
+
_j = (e_2 = void 0, __values(tools.script)), _k = _j.next();
|
|
1502
|
+
_v.label = 31;
|
|
1503
|
+
case 31:
|
|
1504
|
+
if (!!_k.done) return [3 /*break*/, 36];
|
|
1505
|
+
scriptTools = _k.value;
|
|
1506
|
+
_v.label = 32;
|
|
1552
1507
|
case 32:
|
|
1508
|
+
_v.trys.push([32, 34, , 35]);
|
|
1509
|
+
return [4 /*yield*/, scriptTools.execute({
|
|
1510
|
+
scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
|
|
1511
|
+
script: "".concat(functionName, "(resultString)"),
|
|
1512
|
+
parameters: __assign(__assign({}, parametersToPass), { resultString: resultString }),
|
|
1513
|
+
})];
|
|
1514
|
+
case 33:
|
|
1515
|
+
resultString = _v.sent();
|
|
1516
|
+
return [3 /*break*/, 36];
|
|
1517
|
+
case 34:
|
|
1518
|
+
error_3 = _v.sent();
|
|
1519
|
+
if (!(error_3 instanceof Error)) {
|
|
1520
|
+
throw error_3;
|
|
1521
|
+
}
|
|
1522
|
+
scriptExecutionErrors.push(error_3);
|
|
1523
|
+
return [3 /*break*/, 35];
|
|
1524
|
+
case 35:
|
|
1525
|
+
_k = _j.next();
|
|
1526
|
+
return [3 /*break*/, 31];
|
|
1527
|
+
case 36: return [3 /*break*/, 39];
|
|
1528
|
+
case 37:
|
|
1529
|
+
e_2_1 = _v.sent();
|
|
1530
|
+
e_2 = { error: e_2_1 };
|
|
1531
|
+
return [3 /*break*/, 39];
|
|
1532
|
+
case 38:
|
|
1533
|
+
try {
|
|
1534
|
+
if (_k && !_k.done && (_s = _j.return)) _s.call(_j);
|
|
1535
|
+
}
|
|
1536
|
+
finally { if (e_2) throw e_2.error; }
|
|
1537
|
+
return [7 /*endfinally*/];
|
|
1538
|
+
case 39:
|
|
1539
|
+
_h = _g.next();
|
|
1540
|
+
return [3 /*break*/, 29];
|
|
1541
|
+
case 40: return [3 /*break*/, 43];
|
|
1542
|
+
case 41:
|
|
1543
|
+
e_3_1 = _v.sent();
|
|
1544
|
+
e_3 = { error: e_3_1 };
|
|
1545
|
+
return [3 /*break*/, 43];
|
|
1546
|
+
case 42:
|
|
1547
|
+
try {
|
|
1548
|
+
if (_h && !_h.done && (_r = _g.return)) _r.call(_g);
|
|
1549
|
+
}
|
|
1550
|
+
finally { if (e_3) throw e_3.error; }
|
|
1551
|
+
return [7 /*endfinally*/];
|
|
1552
|
+
case 43:
|
|
1553
|
+
if (currentTemplate.expectFormat) {
|
|
1554
|
+
if (currentTemplate.expectFormat === 'JSON') {
|
|
1555
|
+
if (!isValidJsonString(resultString)) {
|
|
1556
|
+
throw new ExpectError('Expected valid JSON string');
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1560
|
+
if (currentTemplate.expectAmount) {
|
|
1561
|
+
try {
|
|
1562
|
+
for (_l = (e_4 = void 0, __values(Object.entries(currentTemplate.expectAmount))), _m = _l.next(); !_m.done; _m = _l.next()) {
|
|
1563
|
+
_o = __read(_m.value, 2), unit = _o[0], _p = _o[1], max = _p.max, min = _p.min;
|
|
1564
|
+
amount = CountUtils[unit.toUpperCase()](resultString);
|
|
1565
|
+
if (min && amount < min) {
|
|
1566
|
+
throw new ExpectError("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
|
|
1567
|
+
} /* not else */
|
|
1568
|
+
if (max && amount > max) {
|
|
1569
|
+
throw new ExpectError("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
}
|
|
1573
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
1574
|
+
finally {
|
|
1575
|
+
try {
|
|
1576
|
+
if (_m && !_m.done && (_t = _l.return)) _t.call(_l);
|
|
1577
|
+
}
|
|
1578
|
+
finally { if (e_4) throw e_4.error; }
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
return [3 /*break*/, 46];
|
|
1582
|
+
case 44:
|
|
1583
|
+
error_4 = _v.sent();
|
|
1584
|
+
if (!(error_4 instanceof ExpectError)) {
|
|
1585
|
+
throw error_4;
|
|
1586
|
+
}
|
|
1587
|
+
expectError = error_4;
|
|
1588
|
+
return [3 /*break*/, 46];
|
|
1589
|
+
case 45:
|
|
1590
|
+
if (currentTemplate.executionType === 'PROMPT_TEMPLATE' &&
|
|
1591
|
+
prompt_1
|
|
1592
|
+
// <- Note: [2] When some expected parameter is not defined, error will occur in replaceParameters
|
|
1593
|
+
// In that case we don’t want to make a report about it because it’s not a natural execution error
|
|
1594
|
+
) {
|
|
1595
|
+
// TODO: [🧠] Maybe put other executionTypes into report
|
|
1596
|
+
executionReport.promptExecutions.push({
|
|
1597
|
+
prompt: {
|
|
1598
|
+
title: currentTemplate.title /* <- Note: If title in promptbook contains emojis, pass it innto report */,
|
|
1599
|
+
content: prompt_1.content,
|
|
1600
|
+
modelRequirements: prompt_1.modelRequirements,
|
|
1601
|
+
// <- Note: Do want to pass ONLY wanted information to the report
|
|
1602
|
+
},
|
|
1603
|
+
result: result || undefined,
|
|
1604
|
+
error: expectError || undefined,
|
|
1605
|
+
});
|
|
1606
|
+
}
|
|
1607
|
+
return [7 /*endfinally*/];
|
|
1608
|
+
case 46:
|
|
1609
|
+
if (result === null && attempt === maxExecutionAttempts) {
|
|
1610
|
+
throw new Error(spaceTrim__default["default"](function (block) { return "\n Natural execution failed ".concat(settings.maxExecutionAttempts, "x\n\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n "); }));
|
|
1611
|
+
}
|
|
1612
|
+
_v.label = 47;
|
|
1613
|
+
case 47:
|
|
1614
|
+
attempt++;
|
|
1615
|
+
return [3 /*break*/, 3];
|
|
1616
|
+
case 48:
|
|
1553
1617
|
if (resultString === null) {
|
|
1554
1618
|
// <- TODO: [🥨] Make some NeverShouldHappenError
|
|
1555
1619
|
throw new Error('Something went wrong and prompt result is null');
|
|
@@ -1565,7 +1629,7 @@
|
|
|
1565
1629
|
parameterValue: resultString,
|
|
1566
1630
|
});
|
|
1567
1631
|
}
|
|
1568
|
-
parametersToPass = __assign(__assign({}, parametersToPass), (
|
|
1632
|
+
parametersToPass = __assign(__assign({}, parametersToPass), (_u = {}, _u[resultingParameter.name] = resultString /* <- Note: Not need to detect parameter collision here because PromptTemplatePipeline checks logic consistency during construction */, _u));
|
|
1569
1633
|
currentTemplate = ptp.getFollowingPromptTemplate(currentTemplate.name);
|
|
1570
1634
|
return [2 /*return*/];
|
|
1571
1635
|
}
|