@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/esm/index.es.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { capitalize, normalizeTo_SCREAMING_CASE, normalizeTo_PascalCase,
|
|
1
|
+
import { capitalize, normalizeTo_SCREAMING_CASE, normalizeTo_PascalCase, removeDiacritics } from 'n12';
|
|
2
2
|
import spaceTrim from 'spacetrim';
|
|
3
3
|
|
|
4
4
|
/*! *****************************************************************************
|
|
@@ -15,6 +15,22 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
15
15
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
16
16
|
PERFORMANCE OF THIS SOFTWARE.
|
|
17
17
|
***************************************************************************** */
|
|
18
|
+
/* global Reflect, Promise */
|
|
19
|
+
|
|
20
|
+
var extendStatics = function(d, b) {
|
|
21
|
+
extendStatics = Object.setPrototypeOf ||
|
|
22
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
23
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
24
|
+
return extendStatics(d, b);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function __extends(d, b) {
|
|
28
|
+
if (typeof b !== "function" && b !== null)
|
|
29
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
30
|
+
extendStatics(d, b);
|
|
31
|
+
function __() { this.constructor = d; }
|
|
32
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
33
|
+
}
|
|
18
34
|
|
|
19
35
|
var __assign = function() {
|
|
20
36
|
__assign = Object.assign || function __assign(t) {
|
|
@@ -737,12 +753,12 @@ function promptTemplatePipelineStringToJson(promptTemplatePipelineString) {
|
|
|
737
753
|
finally { if (e_1) throw e_1.error; }
|
|
738
754
|
}
|
|
739
755
|
var _loop_1 = function (section) {
|
|
740
|
-
var e_3, _e
|
|
756
|
+
var e_3, _e;
|
|
741
757
|
// TODO: Parse prompt template description (the content out of the codeblock and lists)
|
|
742
758
|
var templateModelRequirements = __assign({}, defaultModelRequirements);
|
|
743
|
-
var postprocessingCommands = [];
|
|
744
759
|
var listItems_3 = extractAllListItemsFromMarkdown(section.content);
|
|
745
760
|
var executionType = 'PROMPT_TEMPLATE';
|
|
761
|
+
var postprocessing = [];
|
|
746
762
|
var expectAmount = {};
|
|
747
763
|
var expectFormat = undefined;
|
|
748
764
|
var isExecutionTypeChanged = false;
|
|
@@ -766,7 +782,7 @@ function promptTemplatePipelineStringToJson(promptTemplatePipelineString) {
|
|
|
766
782
|
addParam(command);
|
|
767
783
|
break;
|
|
768
784
|
case 'POSTPROCESS':
|
|
769
|
-
|
|
785
|
+
postprocessing.push(command.functionName);
|
|
770
786
|
break;
|
|
771
787
|
case 'EXPECT_AMOUNT':
|
|
772
788
|
// eslint-disable-next-line no-case-declarations
|
|
@@ -803,7 +819,7 @@ function promptTemplatePipelineStringToJson(promptTemplatePipelineString) {
|
|
|
803
819
|
}
|
|
804
820
|
finally { if (e_3) throw e_3.error; }
|
|
805
821
|
}
|
|
806
|
-
var
|
|
822
|
+
var _f = extractOneBlockFromMarkdown(section.content), language = _f.language, content = _f.content;
|
|
807
823
|
if (executionType === 'SCRIPT') {
|
|
808
824
|
if (!language) {
|
|
809
825
|
throw new Error('You must specify the language of the script in the prompt template');
|
|
@@ -833,61 +849,25 @@ function promptTemplatePipelineStringToJson(promptTemplatePipelineString) {
|
|
|
833
849
|
if (description_1 === '') {
|
|
834
850
|
description_1 = undefined;
|
|
835
851
|
}
|
|
836
|
-
var getParameterName = function (i) {
|
|
837
|
-
var parameterName = postprocessingCommands.length <= i
|
|
838
|
-
? resultingParameterName
|
|
839
|
-
: normalizeTo_camelCase("".concat(resultingParameterName, " before ").concat(postprocessingCommands[i].functionName));
|
|
840
|
-
var isParameterDefined = ptbJson.parameters.some(function (parameter) { return parameter.name === parameterName; });
|
|
841
|
-
if (!isParameterDefined) {
|
|
842
|
-
var parameterDescription = "*(".concat(capitalize(section.title), " postprocessing ").concat(i + 1, "/").concat(postprocessingCommands.length, ")* {").concat(resultingParameterName, "} before `").concat(postprocessingCommands[i].functionName, "`");
|
|
843
|
-
addParam({
|
|
844
|
-
parameterName: parameterName,
|
|
845
|
-
parameterDescription: parameterDescription,
|
|
846
|
-
isInputParameter: false,
|
|
847
|
-
// TODO:> isPrivate: true,
|
|
848
|
-
});
|
|
849
|
-
}
|
|
850
|
-
return parameterName;
|
|
851
|
-
};
|
|
852
852
|
if (Object.keys(expectAmount).length === 0) {
|
|
853
853
|
expectAmount = undefined;
|
|
854
854
|
}
|
|
855
|
+
if (Object.keys(postprocessing).length === 0) {
|
|
856
|
+
postprocessing = undefined;
|
|
857
|
+
}
|
|
855
858
|
ptbJson.promptTemplates.push({
|
|
856
859
|
name: normalizeTo_PascalCase(section.title),
|
|
857
860
|
title: section.title,
|
|
858
861
|
description: description_1,
|
|
859
862
|
executionType: executionType,
|
|
863
|
+
postprocessing: postprocessing,
|
|
860
864
|
expectAmount: expectAmount,
|
|
861
865
|
expectFormat: expectFormat,
|
|
862
866
|
modelRequirements: templateModelRequirements,
|
|
863
867
|
contentLanguage: executionType === 'SCRIPT' ? language : undefined,
|
|
864
868
|
content: content,
|
|
865
|
-
resultingParameterName:
|
|
869
|
+
resultingParameterName: resultingParameterName,
|
|
866
870
|
});
|
|
867
|
-
try {
|
|
868
|
-
for (var _h = (e_4 = void 0, __values(postprocessingCommands.map(function (_a, i) {
|
|
869
|
-
var functionName = _a.functionName;
|
|
870
|
-
return [functionName, i];
|
|
871
|
-
}))), _j = _h.next(); !_j.done; _j = _h.next()) {
|
|
872
|
-
var _k = __read(_j.value, 2), functionName = _k[0], i = _k[1];
|
|
873
|
-
ptbJson.promptTemplates.push({
|
|
874
|
-
name: normalizeTo_PascalCase(section.title + ' Postprocessing ' + i),
|
|
875
|
-
title: "(".concat(i + 1, "/").concat(postprocessingCommands.length, ") ").concat(section.title, " postprocessing"),
|
|
876
|
-
description: "Postprocessing of section ".concat(section.title, " finally with resulting parameter {").concat(resultingParameterName, "}"),
|
|
877
|
-
executionType: 'SCRIPT',
|
|
878
|
-
contentLanguage: 'javascript',
|
|
879
|
-
content: "".concat(functionName, "(").concat(getParameterName(i), ")"),
|
|
880
|
-
resultingParameterName: getParameterName(i + 1),
|
|
881
|
-
});
|
|
882
|
-
}
|
|
883
|
-
}
|
|
884
|
-
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
885
|
-
finally {
|
|
886
|
-
try {
|
|
887
|
-
if (_j && !_j.done && (_f = _h.return)) _f.call(_h);
|
|
888
|
-
}
|
|
889
|
-
finally { if (e_4) throw e_4.error; }
|
|
890
|
-
}
|
|
891
871
|
};
|
|
892
872
|
try {
|
|
893
873
|
for (var _c = __values(markdownStructure.sections), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
@@ -908,9 +888,7 @@ function promptTemplatePipelineStringToJson(promptTemplatePipelineString) {
|
|
|
908
888
|
/**
|
|
909
889
|
* TODO: Report here line/column of error
|
|
910
890
|
* TODO: Use spaceTrim more effectively
|
|
911
|
-
* TODO:
|
|
912
|
-
* TODO: !!!! Allow to have non-immutable parameters - suffix them with fooPrevious3 -> fooPrevious2 -> fooPrevious1 -> foo
|
|
913
|
-
* This must work with other technial parameters
|
|
891
|
+
* TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
|
|
914
892
|
*/
|
|
915
893
|
|
|
916
894
|
/**
|
|
@@ -1041,7 +1019,7 @@ var PromptTemplatePipeline = /** @class */ (function () {
|
|
|
1041
1019
|
*/
|
|
1042
1020
|
PromptTemplatePipeline.fromJson = function (ptbkjson) {
|
|
1043
1021
|
validatePromptTemplatePipelineJson(ptbkjson);
|
|
1044
|
-
return new PromptTemplatePipeline(ptbkjson.ptbkUrl ? new URL(ptbkjson.ptbkUrl) : null, ptbkjson.title, ptbkjson.ptbkVersion, ptbkjson.
|
|
1022
|
+
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);
|
|
1045
1023
|
};
|
|
1046
1024
|
Object.defineProperty(PromptTemplatePipeline.prototype, "entryPromptTemplate", {
|
|
1047
1025
|
/**
|
|
@@ -1301,6 +1279,23 @@ function replaceParameters(template, parameters) {
|
|
|
1301
1279
|
return replacedTemplate;
|
|
1302
1280
|
}
|
|
1303
1281
|
|
|
1282
|
+
/**
|
|
1283
|
+
* This error occurs when some expectation is not met in the execution of the pipeline
|
|
1284
|
+
*/
|
|
1285
|
+
var ExpectError = /** @class */ (function (_super) {
|
|
1286
|
+
__extends(ExpectError, _super);
|
|
1287
|
+
function ExpectError(message) {
|
|
1288
|
+
var _this = _super.call(this, message) || this;
|
|
1289
|
+
_this.name = 'ExpectError';
|
|
1290
|
+
Object.setPrototypeOf(_this, ExpectError.prototype);
|
|
1291
|
+
return _this;
|
|
1292
|
+
}
|
|
1293
|
+
return ExpectError;
|
|
1294
|
+
}(Error));
|
|
1295
|
+
/**
|
|
1296
|
+
* TODO: [🧠] Should be this exported from the library
|
|
1297
|
+
*/
|
|
1298
|
+
|
|
1304
1299
|
/**
|
|
1305
1300
|
* Creates executor function from prompt template pipeline and execution tools.
|
|
1306
1301
|
*
|
|
@@ -1309,7 +1304,7 @@ function replaceParameters(template, parameters) {
|
|
|
1309
1304
|
function createPtpExecutor(options) {
|
|
1310
1305
|
var _this = this;
|
|
1311
1306
|
var ptp = options.ptp, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
|
|
1312
|
-
var _b = settings.
|
|
1307
|
+
var _b = settings.maxExecutionAttempts, maxExecutionAttempts = _b === void 0 ? 3 : _b;
|
|
1313
1308
|
var ptpExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
|
|
1314
1309
|
var parametersToPass, currentTemplate, executionReport, _loop_1, error_1;
|
|
1315
1310
|
var _a;
|
|
@@ -1330,10 +1325,10 @@ function createPtpExecutor(options) {
|
|
|
1330
1325
|
case 1:
|
|
1331
1326
|
_b.trys.push([1, 5, , 6]);
|
|
1332
1327
|
_loop_1 = function () {
|
|
1333
|
-
var resultingParameter, name_1, title, prompt_1, chatThread, completionResult, result, resultString,
|
|
1334
|
-
var e_2,
|
|
1335
|
-
return __generator(this, function (
|
|
1336
|
-
switch (
|
|
1328
|
+
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;
|
|
1329
|
+
var e_1, _q, e_3, _r, e_2, _s, e_4, _t, _u;
|
|
1330
|
+
return __generator(this, function (_v) {
|
|
1331
|
+
switch (_v.label) {
|
|
1337
1332
|
case 0:
|
|
1338
1333
|
resultingParameter = ptp.getResultingParameter(currentTemplate.name);
|
|
1339
1334
|
name_1 = "ptp-executor-frame-".concat(currentTemplate.name);
|
|
@@ -1349,47 +1344,46 @@ function createPtpExecutor(options) {
|
|
|
1349
1344
|
parameterValue: null,
|
|
1350
1345
|
})];
|
|
1351
1346
|
case 1:
|
|
1352
|
-
|
|
1353
|
-
|
|
1347
|
+
_v.sent();
|
|
1348
|
+
_v.label = 2;
|
|
1354
1349
|
case 2:
|
|
1355
1350
|
prompt_1 = void 0;
|
|
1356
1351
|
chatThread = void 0;
|
|
1357
1352
|
completionResult = void 0;
|
|
1358
1353
|
result = null;
|
|
1359
1354
|
resultString = null;
|
|
1360
|
-
|
|
1361
|
-
|
|
1355
|
+
expectError = null;
|
|
1356
|
+
attempt = 0;
|
|
1357
|
+
_v.label = 3;
|
|
1358
|
+
case 3:
|
|
1359
|
+
if (!(attempt < maxExecutionAttempts)) return [3 /*break*/, 48];
|
|
1360
|
+
result = null;
|
|
1361
|
+
resultString = null;
|
|
1362
|
+
expectError = null;
|
|
1363
|
+
_v.label = 4;
|
|
1364
|
+
case 4:
|
|
1365
|
+
_v.trys.push([4, 44, 45, 46]);
|
|
1362
1366
|
_c = currentTemplate.executionType;
|
|
1363
1367
|
switch (_c) {
|
|
1364
|
-
case 'SIMPLE_TEMPLATE': return [3 /*break*/,
|
|
1365
|
-
case 'PROMPT_TEMPLATE': return [3 /*break*/,
|
|
1366
|
-
case 'SCRIPT': return [3 /*break*/,
|
|
1367
|
-
case 'PROMPT_DIALOG': return [3 /*break*/,
|
|
1368
|
+
case 'SIMPLE_TEMPLATE': return [3 /*break*/, 5];
|
|
1369
|
+
case 'PROMPT_TEMPLATE': return [3 /*break*/, 6];
|
|
1370
|
+
case 'SCRIPT': return [3 /*break*/, 13];
|
|
1371
|
+
case 'PROMPT_DIALOG': return [3 /*break*/, 24];
|
|
1368
1372
|
}
|
|
1369
|
-
return [3 /*break*/,
|
|
1370
|
-
case
|
|
1373
|
+
return [3 /*break*/, 26];
|
|
1374
|
+
case 5:
|
|
1371
1375
|
resultString = replaceParameters(currentTemplate.content, parametersToPass);
|
|
1372
|
-
return [3 /*break*/,
|
|
1373
|
-
case
|
|
1376
|
+
return [3 /*break*/, 27];
|
|
1377
|
+
case 6:
|
|
1374
1378
|
prompt_1 = {
|
|
1375
1379
|
title: currentTemplate.title,
|
|
1376
1380
|
ptbkUrl: "".concat(ptp.ptbkUrl
|
|
1377
1381
|
? ptp.ptbkUrl.href
|
|
1378
|
-
: 'anonymous' /* <- [🧠]
|
|
1382
|
+
: 'anonymous' /* <- [🧠] !!! How to deal with anonymous PTPs, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
|
|
1379
1383
|
parameters: parametersToPass,
|
|
1380
|
-
content: replaceParameters(currentTemplate.content, parametersToPass)
|
|
1384
|
+
content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
|
|
1381
1385
|
modelRequirements: currentTemplate.modelRequirements,
|
|
1382
1386
|
};
|
|
1383
|
-
attempt = 0;
|
|
1384
|
-
_p.label = 5;
|
|
1385
|
-
case 5:
|
|
1386
|
-
if (!(attempt < maxNaturalExecutionAttempts)) return [3 /*break*/, 17];
|
|
1387
|
-
result = null;
|
|
1388
|
-
resultString = null;
|
|
1389
|
-
naturalExecutionError = null;
|
|
1390
|
-
_p.label = 6;
|
|
1391
|
-
case 6:
|
|
1392
|
-
_p.trys.push([6, 13, 14, 15]);
|
|
1393
1387
|
_d = currentTemplate.modelRequirements.modelVariant;
|
|
1394
1388
|
switch (_d) {
|
|
1395
1389
|
case 'CHAT': return [3 /*break*/, 7];
|
|
@@ -1398,131 +1392,71 @@ function createPtpExecutor(options) {
|
|
|
1398
1392
|
return [3 /*break*/, 11];
|
|
1399
1393
|
case 7: return [4 /*yield*/, tools.natural.gptChat(prompt_1)];
|
|
1400
1394
|
case 8:
|
|
1401
|
-
chatThread =
|
|
1395
|
+
chatThread = _v.sent();
|
|
1402
1396
|
// TODO: [🍬] Destroy chatThread
|
|
1403
1397
|
result = chatThread;
|
|
1404
1398
|
resultString = chatThread.content;
|
|
1405
1399
|
return [3 /*break*/, 12];
|
|
1406
1400
|
case 9: return [4 /*yield*/, tools.natural.gptComplete(prompt_1)];
|
|
1407
1401
|
case 10:
|
|
1408
|
-
completionResult =
|
|
1402
|
+
completionResult = _v.sent();
|
|
1409
1403
|
result = completionResult;
|
|
1410
1404
|
resultString = completionResult.content;
|
|
1411
1405
|
return [3 /*break*/, 12];
|
|
1412
1406
|
case 11: throw new Error("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
|
|
1413
|
-
case 12:
|
|
1414
|
-
// TODO: !!!!!! Here should postprocessing happen
|
|
1415
|
-
if (currentTemplate.expectFormat) {
|
|
1416
|
-
if (currentTemplate.expectFormat === 'JSON') {
|
|
1417
|
-
if (!isValidJsonString(resultString)) {
|
|
1418
|
-
throw new Error('Expected valid JSON string');
|
|
1419
|
-
}
|
|
1420
|
-
}
|
|
1421
|
-
}
|
|
1422
|
-
if (currentTemplate.expectAmount) {
|
|
1423
|
-
try {
|
|
1424
|
-
for (_e = (e_2 = void 0, __values(Object.entries(currentTemplate.expectAmount))), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
1425
|
-
_g = __read(_f.value, 2), unit = _g[0], _h = _g[1], max = _h.max, min = _h.min;
|
|
1426
|
-
amount = CountUtils[unit.toUpperCase()](resultString);
|
|
1427
|
-
if (min && amount < min) {
|
|
1428
|
-
throw new Error("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
|
|
1429
|
-
} /* not else */
|
|
1430
|
-
if (max && amount > max) {
|
|
1431
|
-
throw new Error("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
|
|
1432
|
-
}
|
|
1433
|
-
}
|
|
1434
|
-
}
|
|
1435
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
1436
|
-
finally {
|
|
1437
|
-
try {
|
|
1438
|
-
if (_f && !_f.done && (_l = _e.return)) _l.call(_e);
|
|
1439
|
-
}
|
|
1440
|
-
finally { if (e_2) throw e_2.error; }
|
|
1441
|
-
}
|
|
1442
|
-
}
|
|
1443
|
-
return [3 /*break*/, 15];
|
|
1407
|
+
case 12: return [3 /*break*/, 27];
|
|
1444
1408
|
case 13:
|
|
1445
|
-
error_2 = _p.sent();
|
|
1446
|
-
if (!(error_2 instanceof Error)) {
|
|
1447
|
-
throw error_2;
|
|
1448
|
-
}
|
|
1449
|
-
naturalExecutionError = error_2;
|
|
1450
|
-
return [3 /*break*/, 15];
|
|
1451
|
-
case 14:
|
|
1452
|
-
executionReport.promptExecutions.push({
|
|
1453
|
-
prompt: {
|
|
1454
|
-
title: prompt_1.title,
|
|
1455
|
-
content: prompt_1.content,
|
|
1456
|
-
modelRequirements: prompt_1.modelRequirements,
|
|
1457
|
-
// <- Note: Do want to pass ONLY wanted information to the report
|
|
1458
|
-
},
|
|
1459
|
-
result: result /* <- !!!!! Look what is exposed here and probbably also filter out */ ||
|
|
1460
|
-
undefined,
|
|
1461
|
-
error: naturalExecutionError || undefined,
|
|
1462
|
-
});
|
|
1463
|
-
return [7 /*endfinally*/];
|
|
1464
|
-
case 15:
|
|
1465
|
-
if (result !== null && naturalExecutionError === null) {
|
|
1466
|
-
return [3 /*break*/, 32];
|
|
1467
|
-
}
|
|
1468
|
-
_p.label = 16;
|
|
1469
|
-
case 16:
|
|
1470
|
-
attempt++;
|
|
1471
|
-
return [3 /*break*/, 5];
|
|
1472
|
-
case 17: throw new Error(spaceTrim(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 "); }));
|
|
1473
|
-
case 18:
|
|
1474
1409
|
if (tools.script.length === 0) {
|
|
1475
1410
|
throw new Error('No script execution tools are available');
|
|
1476
1411
|
}
|
|
1477
1412
|
if (!currentTemplate.contentLanguage) {
|
|
1478
1413
|
throw new Error("Script language is not defined for prompt template \"".concat(currentTemplate.name, "\""));
|
|
1479
1414
|
}
|
|
1415
|
+
// TODO: DRY [1]
|
|
1480
1416
|
scriptExecutionErrors = [];
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
_p.trys.push([21, 23, , 24]);
|
|
1417
|
+
_v.label = 14;
|
|
1418
|
+
case 14:
|
|
1419
|
+
_v.trys.push([14, 21, 22, 23]);
|
|
1420
|
+
_e = (e_1 = void 0, __values(tools.script)), _f = _e.next();
|
|
1421
|
+
_v.label = 15;
|
|
1422
|
+
case 15:
|
|
1423
|
+
if (!!_f.done) return [3 /*break*/, 20];
|
|
1424
|
+
scriptTools = _f.value;
|
|
1425
|
+
_v.label = 16;
|
|
1426
|
+
case 16:
|
|
1427
|
+
_v.trys.push([16, 18, , 19]);
|
|
1493
1428
|
return [4 /*yield*/, scriptTools.execute({
|
|
1494
1429
|
scriptLanguage: currentTemplate.contentLanguage,
|
|
1495
1430
|
script: currentTemplate.content,
|
|
1496
1431
|
parameters: parametersToPass,
|
|
1497
1432
|
})];
|
|
1498
|
-
case
|
|
1499
|
-
resultString =
|
|
1500
|
-
isScriptExecutionSuccessful = true;
|
|
1501
|
-
return [3 /*break*/, 25];
|
|
1502
|
-
case 23:
|
|
1503
|
-
error_3 = _p.sent();
|
|
1504
|
-
if (!(error_3 instanceof Error)) {
|
|
1505
|
-
throw error_3;
|
|
1506
|
-
}
|
|
1507
|
-
scriptExecutionErrors.push(error_3);
|
|
1508
|
-
return [3 /*break*/, 24];
|
|
1509
|
-
case 24:
|
|
1510
|
-
_k = _j.next();
|
|
1433
|
+
case 17:
|
|
1434
|
+
resultString = _v.sent();
|
|
1511
1435
|
return [3 /*break*/, 20];
|
|
1512
|
-
case
|
|
1513
|
-
|
|
1514
|
-
|
|
1436
|
+
case 18:
|
|
1437
|
+
error_2 = _v.sent();
|
|
1438
|
+
if (!(error_2 instanceof Error)) {
|
|
1439
|
+
throw error_2;
|
|
1440
|
+
}
|
|
1441
|
+
scriptExecutionErrors.push(error_2);
|
|
1442
|
+
return [3 /*break*/, 19];
|
|
1443
|
+
case 19:
|
|
1444
|
+
_f = _e.next();
|
|
1445
|
+
return [3 /*break*/, 15];
|
|
1446
|
+
case 20: return [3 /*break*/, 23];
|
|
1447
|
+
case 21:
|
|
1448
|
+
e_1_1 = _v.sent();
|
|
1515
1449
|
e_1 = { error: e_1_1 };
|
|
1516
|
-
return [3 /*break*/,
|
|
1517
|
-
case
|
|
1450
|
+
return [3 /*break*/, 23];
|
|
1451
|
+
case 22:
|
|
1518
1452
|
try {
|
|
1519
|
-
if (
|
|
1453
|
+
if (_f && !_f.done && (_q = _e.return)) _q.call(_e);
|
|
1520
1454
|
}
|
|
1521
1455
|
finally { if (e_1) throw e_1.error; }
|
|
1522
1456
|
return [7 /*endfinally*/];
|
|
1523
|
-
case
|
|
1524
|
-
if (
|
|
1525
|
-
return [3 /*break*/,
|
|
1457
|
+
case 23:
|
|
1458
|
+
if (resultString) {
|
|
1459
|
+
return [3 /*break*/, 27];
|
|
1526
1460
|
}
|
|
1527
1461
|
if (scriptExecutionErrors.length === 1) {
|
|
1528
1462
|
throw scriptExecutionErrors[0];
|
|
@@ -1530,19 +1464,149 @@ function createPtpExecutor(options) {
|
|
|
1530
1464
|
else {
|
|
1531
1465
|
throw new Error(spaceTrim(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 "); }));
|
|
1532
1466
|
}
|
|
1533
|
-
case
|
|
1467
|
+
case 24: return [4 /*yield*/, tools.userInterface.promptDialog({
|
|
1534
1468
|
prompt: replaceParameters(currentTemplate.description || '', parametersToPass),
|
|
1535
1469
|
defaultValue: replaceParameters(currentTemplate.content, parametersToPass),
|
|
1536
1470
|
// TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
|
|
1537
1471
|
placeholder: undefined,
|
|
1538
1472
|
})];
|
|
1539
|
-
case
|
|
1540
|
-
resultString =
|
|
1541
|
-
return [3 /*break*/,
|
|
1542
|
-
case
|
|
1473
|
+
case 25:
|
|
1474
|
+
resultString = _v.sent();
|
|
1475
|
+
return [3 /*break*/, 27];
|
|
1476
|
+
case 26:
|
|
1543
1477
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1544
1478
|
throw new Error("Unknown execution type \"".concat(currentTemplate.executionType, "\""));
|
|
1479
|
+
case 27:
|
|
1480
|
+
if (!currentTemplate.postprocessing) return [3 /*break*/, 43];
|
|
1481
|
+
_v.label = 28;
|
|
1482
|
+
case 28:
|
|
1483
|
+
_v.trys.push([28, 41, 42, 43]);
|
|
1484
|
+
_g = (e_3 = void 0, __values(currentTemplate.postprocessing)), _h = _g.next();
|
|
1485
|
+
_v.label = 29;
|
|
1486
|
+
case 29:
|
|
1487
|
+
if (!!_h.done) return [3 /*break*/, 40];
|
|
1488
|
+
functionName = _h.value;
|
|
1489
|
+
// TODO: DRY [1]
|
|
1490
|
+
scriptExecutionErrors = [];
|
|
1491
|
+
_v.label = 30;
|
|
1492
|
+
case 30:
|
|
1493
|
+
_v.trys.push([30, 37, 38, 39]);
|
|
1494
|
+
_j = (e_2 = void 0, __values(tools.script)), _k = _j.next();
|
|
1495
|
+
_v.label = 31;
|
|
1496
|
+
case 31:
|
|
1497
|
+
if (!!_k.done) return [3 /*break*/, 36];
|
|
1498
|
+
scriptTools = _k.value;
|
|
1499
|
+
_v.label = 32;
|
|
1545
1500
|
case 32:
|
|
1501
|
+
_v.trys.push([32, 34, , 35]);
|
|
1502
|
+
return [4 /*yield*/, scriptTools.execute({
|
|
1503
|
+
scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
|
|
1504
|
+
script: "".concat(functionName, "(resultString)"),
|
|
1505
|
+
parameters: __assign(__assign({}, parametersToPass), { resultString: resultString }),
|
|
1506
|
+
})];
|
|
1507
|
+
case 33:
|
|
1508
|
+
resultString = _v.sent();
|
|
1509
|
+
return [3 /*break*/, 36];
|
|
1510
|
+
case 34:
|
|
1511
|
+
error_3 = _v.sent();
|
|
1512
|
+
if (!(error_3 instanceof Error)) {
|
|
1513
|
+
throw error_3;
|
|
1514
|
+
}
|
|
1515
|
+
scriptExecutionErrors.push(error_3);
|
|
1516
|
+
return [3 /*break*/, 35];
|
|
1517
|
+
case 35:
|
|
1518
|
+
_k = _j.next();
|
|
1519
|
+
return [3 /*break*/, 31];
|
|
1520
|
+
case 36: return [3 /*break*/, 39];
|
|
1521
|
+
case 37:
|
|
1522
|
+
e_2_1 = _v.sent();
|
|
1523
|
+
e_2 = { error: e_2_1 };
|
|
1524
|
+
return [3 /*break*/, 39];
|
|
1525
|
+
case 38:
|
|
1526
|
+
try {
|
|
1527
|
+
if (_k && !_k.done && (_s = _j.return)) _s.call(_j);
|
|
1528
|
+
}
|
|
1529
|
+
finally { if (e_2) throw e_2.error; }
|
|
1530
|
+
return [7 /*endfinally*/];
|
|
1531
|
+
case 39:
|
|
1532
|
+
_h = _g.next();
|
|
1533
|
+
return [3 /*break*/, 29];
|
|
1534
|
+
case 40: return [3 /*break*/, 43];
|
|
1535
|
+
case 41:
|
|
1536
|
+
e_3_1 = _v.sent();
|
|
1537
|
+
e_3 = { error: e_3_1 };
|
|
1538
|
+
return [3 /*break*/, 43];
|
|
1539
|
+
case 42:
|
|
1540
|
+
try {
|
|
1541
|
+
if (_h && !_h.done && (_r = _g.return)) _r.call(_g);
|
|
1542
|
+
}
|
|
1543
|
+
finally { if (e_3) throw e_3.error; }
|
|
1544
|
+
return [7 /*endfinally*/];
|
|
1545
|
+
case 43:
|
|
1546
|
+
if (currentTemplate.expectFormat) {
|
|
1547
|
+
if (currentTemplate.expectFormat === 'JSON') {
|
|
1548
|
+
if (!isValidJsonString(resultString)) {
|
|
1549
|
+
throw new ExpectError('Expected valid JSON string');
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
if (currentTemplate.expectAmount) {
|
|
1554
|
+
try {
|
|
1555
|
+
for (_l = (e_4 = void 0, __values(Object.entries(currentTemplate.expectAmount))), _m = _l.next(); !_m.done; _m = _l.next()) {
|
|
1556
|
+
_o = __read(_m.value, 2), unit = _o[0], _p = _o[1], max = _p.max, min = _p.min;
|
|
1557
|
+
amount = CountUtils[unit.toUpperCase()](resultString);
|
|
1558
|
+
if (min && amount < min) {
|
|
1559
|
+
throw new ExpectError("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
|
|
1560
|
+
} /* not else */
|
|
1561
|
+
if (max && amount > max) {
|
|
1562
|
+
throw new ExpectError("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
1567
|
+
finally {
|
|
1568
|
+
try {
|
|
1569
|
+
if (_m && !_m.done && (_t = _l.return)) _t.call(_l);
|
|
1570
|
+
}
|
|
1571
|
+
finally { if (e_4) throw e_4.error; }
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
return [3 /*break*/, 46];
|
|
1575
|
+
case 44:
|
|
1576
|
+
error_4 = _v.sent();
|
|
1577
|
+
if (!(error_4 instanceof ExpectError)) {
|
|
1578
|
+
throw error_4;
|
|
1579
|
+
}
|
|
1580
|
+
expectError = error_4;
|
|
1581
|
+
return [3 /*break*/, 46];
|
|
1582
|
+
case 45:
|
|
1583
|
+
if (currentTemplate.executionType === 'PROMPT_TEMPLATE' &&
|
|
1584
|
+
prompt_1
|
|
1585
|
+
// <- Note: [2] When some expected parameter is not defined, error will occur in replaceParameters
|
|
1586
|
+
// In that case we don’t want to make a report about it because it’s not a natural execution error
|
|
1587
|
+
) {
|
|
1588
|
+
// TODO: [🧠] Maybe put other executionTypes into report
|
|
1589
|
+
executionReport.promptExecutions.push({
|
|
1590
|
+
prompt: {
|
|
1591
|
+
title: currentTemplate.title /* <- Note: If title in promptbook contains emojis, pass it innto report */,
|
|
1592
|
+
content: prompt_1.content,
|
|
1593
|
+
modelRequirements: prompt_1.modelRequirements,
|
|
1594
|
+
// <- Note: Do want to pass ONLY wanted information to the report
|
|
1595
|
+
},
|
|
1596
|
+
result: result || undefined,
|
|
1597
|
+
error: expectError || undefined,
|
|
1598
|
+
});
|
|
1599
|
+
}
|
|
1600
|
+
return [7 /*endfinally*/];
|
|
1601
|
+
case 46:
|
|
1602
|
+
if (result === null && attempt === maxExecutionAttempts) {
|
|
1603
|
+
throw new Error(spaceTrim(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 "); }));
|
|
1604
|
+
}
|
|
1605
|
+
_v.label = 47;
|
|
1606
|
+
case 47:
|
|
1607
|
+
attempt++;
|
|
1608
|
+
return [3 /*break*/, 3];
|
|
1609
|
+
case 48:
|
|
1546
1610
|
if (resultString === null) {
|
|
1547
1611
|
// <- TODO: [🥨] Make some NeverShouldHappenError
|
|
1548
1612
|
throw new Error('Something went wrong and prompt result is null');
|
|
@@ -1558,7 +1622,7 @@ function createPtpExecutor(options) {
|
|
|
1558
1622
|
parameterValue: resultString,
|
|
1559
1623
|
});
|
|
1560
1624
|
}
|
|
1561
|
-
parametersToPass = __assign(__assign({}, parametersToPass), (
|
|
1625
|
+
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));
|
|
1562
1626
|
currentTemplate = ptp.getFollowingPromptTemplate(currentTemplate.name);
|
|
1563
1627
|
return [2 /*return*/];
|
|
1564
1628
|
}
|