@promptbook/core 0.44.0-9 โ 0.44.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/README.md +740 -2
- package/esm/index.es.js +164 -44
- package/esm/index.es.js.map +1 -1
- package/esm/typings/_packages/utils.index.d.ts +4 -2
- package/esm/typings/execution/plugins/natural-execution-tools/mocked/fakeTextToExpectations.d.ts +6 -2
- package/esm/typings/execution/plugins/script-execution-tools/javascript/JavascriptExecutionToolsOptions.d.ts +5 -1
- package/esm/typings/execution/plugins/script-execution-tools/javascript/utils/unknownToString.d.ts +7 -0
- package/esm/typings/types/Prompt.d.ts +5 -0
- package/esm/typings/types/PromptbookJson/PromptTemplateJson.d.ts +1 -0
- package/package.json +3 -3
- package/umd/index.umd.js +204 -88
- package/umd/index.umd.js.map +1 -1
- package/umd/typings/_packages/utils.index.d.ts +4 -2
- package/umd/typings/execution/plugins/natural-execution-tools/mocked/fakeTextToExpectations.d.ts +6 -2
- package/umd/typings/execution/plugins/script-execution-tools/javascript/JavascriptExecutionToolsOptions.d.ts +5 -1
- package/umd/typings/execution/plugins/script-execution-tools/javascript/utils/unknownToString.d.ts +7 -0
- package/umd/typings/types/Prompt.d.ts +5 -0
- package/umd/typings/types/PromptbookJson/PromptTemplateJson.d.ts +1 -0
package/umd/index.umd.js
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
2
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('spacetrim'), require('prettier'), require('prettier/parser-html'), require('moment'), require('lorem-ipsum')) :
|
|
3
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.
|
|
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);
|
|
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.
|
|
@@ -133,7 +129,7 @@
|
|
|
133
129
|
var UnexpectedError = /** @class */ (function (_super) {
|
|
134
130
|
__extends(UnexpectedError, _super);
|
|
135
131
|
function UnexpectedError(message) {
|
|
136
|
-
var _this = _super.call(this,
|
|
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;
|
|
137
133
|
_this.name = 'UnexpectedError';
|
|
138
134
|
Object.setPrototypeOf(_this, UnexpectedError.prototype);
|
|
139
135
|
return _this;
|
|
@@ -148,7 +144,7 @@
|
|
|
148
144
|
* @returns {string} The input string with all comments removed.
|
|
149
145
|
*/
|
|
150
146
|
function removeContentComments(content) {
|
|
151
|
-
return
|
|
147
|
+
return spacetrim.spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
|
|
152
148
|
}
|
|
153
149
|
|
|
154
150
|
/**
|
|
@@ -1123,7 +1119,7 @@
|
|
|
1123
1119
|
}
|
|
1124
1120
|
if (parent_1.parent === null /* <- Note: We are in root */) {
|
|
1125
1121
|
// [๐ป]
|
|
1126
|
-
throw new Error(
|
|
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 "));
|
|
1127
1123
|
}
|
|
1128
1124
|
parent_1 = parent_1.parent;
|
|
1129
1125
|
}
|
|
@@ -1157,7 +1153,7 @@
|
|
|
1157
1153
|
return {
|
|
1158
1154
|
level: level,
|
|
1159
1155
|
title: title,
|
|
1160
|
-
content:
|
|
1156
|
+
content: spacetrim.spaceTrim(contentLines.join('\n')),
|
|
1161
1157
|
sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
|
|
1162
1158
|
};
|
|
1163
1159
|
}
|
|
@@ -1165,7 +1161,7 @@
|
|
|
1165
1161
|
/**
|
|
1166
1162
|
* The version of the Promptbook library
|
|
1167
1163
|
*/
|
|
1168
|
-
var PROMPTBOOK_VERSION = '0.44.0-
|
|
1164
|
+
var PROMPTBOOK_VERSION = '0.44.0-23';
|
|
1169
1165
|
|
|
1170
1166
|
/**
|
|
1171
1167
|
* Parses the given script and returns the list of all used variables that are not defined in the script
|
|
@@ -1210,7 +1206,7 @@
|
|
|
1210
1206
|
if (!(error instanceof Error)) {
|
|
1211
1207
|
throw error;
|
|
1212
1208
|
}
|
|
1213
|
-
throw new PromptbookSyntaxError(
|
|
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 "); }));
|
|
1214
1210
|
}
|
|
1215
1211
|
return variables;
|
|
1216
1212
|
}
|
|
@@ -1275,15 +1271,15 @@
|
|
|
1275
1271
|
type.startsWith('PROMPTBOOKURL') ||
|
|
1276
1272
|
type.startsWith('HTTPS')) {
|
|
1277
1273
|
if (!(listItemParts.length === 2 || (listItemParts.length === 1 && type.startsWith('HTTPS')))) {
|
|
1278
|
-
throw new PromptbookSyntaxError(
|
|
1274
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n ")));
|
|
1279
1275
|
}
|
|
1280
1276
|
var promptbookUrlString = listItemParts.pop();
|
|
1281
1277
|
var promptbookUrl = new URL(promptbookUrlString);
|
|
1282
1278
|
if (promptbookUrl.protocol !== 'https:') {
|
|
1283
|
-
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 ")));
|
|
1284
1280
|
}
|
|
1285
1281
|
if (promptbookUrl.hash !== '') {
|
|
1286
|
-
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 ")));
|
|
1287
1283
|
}
|
|
1288
1284
|
return {
|
|
1289
1285
|
type: 'PROMPTBOOK_URL',
|
|
@@ -1292,7 +1288,7 @@
|
|
|
1292
1288
|
}
|
|
1293
1289
|
else if (type.startsWith('PROMPTBOOK_VERSION') || type.startsWith('PTBK_VERSION')) {
|
|
1294
1290
|
if (listItemParts.length !== 2) {
|
|
1295
|
-
throw new PromptbookSyntaxError(
|
|
1291
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid PROMPTBOOK_VERSION command:\n\n - ".concat(listItem, "\n ")));
|
|
1296
1292
|
}
|
|
1297
1293
|
var promptbookVersion = listItemParts.pop();
|
|
1298
1294
|
// TODO: Validate version
|
|
@@ -1307,7 +1303,7 @@
|
|
|
1307
1303
|
type.startsWith('SIMPLE_TEMPLATE')) {
|
|
1308
1304
|
var executionTypes = ExecutionTypes.filter(function (executionType) { return type.includes(executionType); });
|
|
1309
1305
|
if (executionTypes.length !== 1) {
|
|
1310
|
-
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 "); }));
|
|
1311
1307
|
}
|
|
1312
1308
|
return {
|
|
1313
1309
|
type: 'EXECUTE',
|
|
@@ -1332,7 +1328,7 @@
|
|
|
1332
1328
|
};
|
|
1333
1329
|
}
|
|
1334
1330
|
else {
|
|
1335
|
-
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 "); }));
|
|
1336
1332
|
}
|
|
1337
1333
|
}
|
|
1338
1334
|
if (type.startsWith('MODEL_NAME')) {
|
|
@@ -1343,7 +1339,7 @@
|
|
|
1343
1339
|
};
|
|
1344
1340
|
}
|
|
1345
1341
|
else {
|
|
1346
|
-
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 "); }));
|
|
1347
1343
|
}
|
|
1348
1344
|
}
|
|
1349
1345
|
else if (type.startsWith('PARAM') ||
|
|
@@ -1353,12 +1349,12 @@
|
|
|
1353
1349
|
listItem.startsWith('> {') /* <- Note: This is a bit hack to parse return parameters defined at the end of each section */) {
|
|
1354
1350
|
var parametersMatch = listItem.match(/\{(?<parameterName>[a-z0-9_]+)\}[^\S\r\n]*(?<parameterDescription>.*)$/im);
|
|
1355
1351
|
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
1356
|
-
throw new PromptbookSyntaxError(
|
|
1352
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
|
|
1357
1353
|
}
|
|
1358
1354
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1359
1355
|
var _b = parametersMatch.groups, parameterName = _b.parameterName, parameterDescription = _b.parameterDescription;
|
|
1360
1356
|
if (parameterDescription && parameterDescription.match(/\{(?<parameterName>[a-z0-9_]+)\}/im)) {
|
|
1361
|
-
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 ")));
|
|
1362
1358
|
}
|
|
1363
1359
|
var isInput = type.startsWith('INPUT');
|
|
1364
1360
|
var isOutput = type.startsWith('OUTPUT');
|
|
@@ -1376,11 +1372,11 @@
|
|
|
1376
1372
|
}
|
|
1377
1373
|
else if (type.startsWith('JOKER')) {
|
|
1378
1374
|
if (listItemParts.length !== 2) {
|
|
1379
|
-
throw new PromptbookSyntaxError(
|
|
1375
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid JOKER command:\n\n - ".concat(listItem, "\n ")));
|
|
1380
1376
|
}
|
|
1381
1377
|
var parametersMatch = (listItemParts.pop() || '').match(/^\{(?<parameterName>[a-z0-9_]+)\}$/im);
|
|
1382
1378
|
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
1383
|
-
throw new PromptbookSyntaxError(
|
|
1379
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
|
|
1384
1380
|
}
|
|
1385
1381
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1386
1382
|
var parameterName = parametersMatch.groups.parameterName;
|
|
@@ -1391,7 +1387,7 @@
|
|
|
1391
1387
|
}
|
|
1392
1388
|
else if (type.startsWith('POSTPROCESS') || type.startsWith('POST_PROCESS')) {
|
|
1393
1389
|
if (listItemParts.length !== 2) {
|
|
1394
|
-
throw new PromptbookSyntaxError(
|
|
1390
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid POSTPROCESSING command:\n\n - ".concat(listItem, "\n ")));
|
|
1395
1391
|
}
|
|
1396
1392
|
var functionName = listItemParts.pop();
|
|
1397
1393
|
return {
|
|
@@ -1471,7 +1467,7 @@
|
|
|
1471
1467
|
if (!(error instanceof Error)) {
|
|
1472
1468
|
throw error;
|
|
1473
1469
|
}
|
|
1474
|
-
throw new PromptbookSyntaxError(
|
|
1470
|
+
throw new PromptbookSyntaxError(spacetrim.spaceTrim("\n Invalid EXPECT command; ".concat(error.message, ":\n\n - ").concat(listItem, "\n ")));
|
|
1475
1471
|
}
|
|
1476
1472
|
/*
|
|
1477
1473
|
} else if (type.startsWith('__________________')) {
|
|
@@ -1479,7 +1475,7 @@
|
|
|
1479
1475
|
*/
|
|
1480
1476
|
}
|
|
1481
1477
|
else {
|
|
1482
|
-
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 ")));
|
|
1483
1479
|
}
|
|
1484
1480
|
}
|
|
1485
1481
|
|
|
@@ -1515,7 +1511,7 @@
|
|
|
1515
1511
|
existingParameter.description &&
|
|
1516
1512
|
existingParameter.description !== parameterDescription &&
|
|
1517
1513
|
parameterDescription) {
|
|
1518
|
-
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 "); }));
|
|
1519
1515
|
}
|
|
1520
1516
|
if (existingParameter) {
|
|
1521
1517
|
if (parameterDescription) {
|
|
@@ -1536,7 +1532,7 @@
|
|
|
1536
1532
|
var markdownStructure = markdownToMarkdownStructure(promptbookString);
|
|
1537
1533
|
var markdownStructureDeepness = countMarkdownStructureDeepness(markdownStructure);
|
|
1538
1534
|
if (markdownStructureDeepness !== 2) {
|
|
1539
|
-
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 ")));
|
|
1540
1536
|
}
|
|
1541
1537
|
promptbookJson.title = markdownStructure.title;
|
|
1542
1538
|
// TODO: [1] DRY description
|
|
@@ -1545,7 +1541,7 @@
|
|
|
1545
1541
|
description = description.split(/^```.*^```/gms).join('');
|
|
1546
1542
|
//Note: Remove lists and return statement
|
|
1547
1543
|
description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
|
|
1548
|
-
description =
|
|
1544
|
+
description = spacetrim.spaceTrim(description);
|
|
1549
1545
|
if (description === '') {
|
|
1550
1546
|
description = undefined;
|
|
1551
1547
|
}
|
|
@@ -1660,13 +1656,13 @@
|
|
|
1660
1656
|
throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
|
|
1661
1657
|
}
|
|
1662
1658
|
else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
|
|
1663
|
-
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 "); }));
|
|
1664
1660
|
}
|
|
1665
1661
|
}
|
|
1666
1662
|
var lastLine = section.content.split('\n').pop();
|
|
1667
1663
|
var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
|
|
1668
1664
|
if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
|
|
1669
|
-
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(
|
|
1670
1666
|
// TODO: Show code of invalid sections each time + DRY
|
|
1671
1667
|
section.content
|
|
1672
1668
|
.split('\n')
|
|
@@ -1680,7 +1676,7 @@
|
|
|
1680
1676
|
description_1 = description_1.split(/^```.*^```/gms).join('');
|
|
1681
1677
|
//Note: Remove lists and return statement
|
|
1682
1678
|
description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
|
|
1683
|
-
description_1 =
|
|
1679
|
+
description_1 = spacetrim.spaceTrim(description_1);
|
|
1684
1680
|
if (description_1 === '') {
|
|
1685
1681
|
description_1 = undefined;
|
|
1686
1682
|
}
|
|
@@ -1828,12 +1824,12 @@
|
|
|
1828
1824
|
if (!parameter.isInput &&
|
|
1829
1825
|
!parameter.isOutput &&
|
|
1830
1826
|
!promptbook.promptTemplates.some(function (template) { return template.dependentParameterNames.includes(parameter.name); })) {
|
|
1831
|
-
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 ")));
|
|
1832
1828
|
}
|
|
1833
1829
|
// Note: Testing that parameter is either input or result of some template
|
|
1834
1830
|
if (!parameter.isInput &&
|
|
1835
1831
|
!promptbook.promptTemplates.some(function (template) { return template.resultingParameterName === parameter.name; })) {
|
|
1836
|
-
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 ")));
|
|
1837
1833
|
}
|
|
1838
1834
|
};
|
|
1839
1835
|
try {
|
|
@@ -1868,7 +1864,7 @@
|
|
|
1868
1864
|
if (template.executionType === 'PROMPT_TEMPLATE' &&
|
|
1869
1865
|
(template.modelRequirements.modelVariant === undefined ||
|
|
1870
1866
|
template.modelRequirements.modelName === undefined)) {
|
|
1871
|
-
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 ")));
|
|
1872
1868
|
}
|
|
1873
1869
|
if (template.jokers && template.jokers.length > 0) {
|
|
1874
1870
|
if (!template.expectFormat &&
|
|
@@ -1943,7 +1939,7 @@
|
|
|
1943
1939
|
return template.dependentParameterNames.every(function (name) { return resovedParameters.includes(name); });
|
|
1944
1940
|
});
|
|
1945
1941
|
if (currentlyResovedTemplates.length === 0) {
|
|
1946
|
-
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
|
|
1947
1943
|
.map(function (_a) {
|
|
1948
1944
|
var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
|
|
1949
1945
|
return "- {".concat(resultingParameterName, "} depends on ").concat(dependentParameterNames
|
|
@@ -1990,6 +1986,7 @@
|
|
|
1990
1986
|
return __awaiter(this, void 0, void 0, function () {
|
|
1991
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;
|
|
1992
1988
|
var e_2, _j, e_4, _k, e_3, _l, _m;
|
|
1989
|
+
var _this = this;
|
|
1993
1990
|
return __generator(this, function (_o) {
|
|
1994
1991
|
switch (_o.label) {
|
|
1995
1992
|
case 0:
|
|
@@ -2059,6 +2056,67 @@
|
|
|
2059
2056
|
content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
|
|
2060
2057
|
modelRequirements: currentTemplate.modelRequirements,
|
|
2061
2058
|
expectations: currentTemplate.expectations,
|
|
2059
|
+
postprocessing: (currentTemplate.postprocessing || []).map(function (functionName) { return function (result) { return __awaiter(_this, void 0, void 0, function () {
|
|
2060
|
+
var errors, _a, _b, scriptTools, error_5, e_5_1;
|
|
2061
|
+
var e_5, _c;
|
|
2062
|
+
return __generator(this, function (_d) {
|
|
2063
|
+
switch (_d.label) {
|
|
2064
|
+
case 0:
|
|
2065
|
+
errors = [];
|
|
2066
|
+
_d.label = 1;
|
|
2067
|
+
case 1:
|
|
2068
|
+
_d.trys.push([1, 8, 9, 10]);
|
|
2069
|
+
_a = __values(tools.script), _b = _a.next();
|
|
2070
|
+
_d.label = 2;
|
|
2071
|
+
case 2:
|
|
2072
|
+
if (!!_b.done) return [3 /*break*/, 7];
|
|
2073
|
+
scriptTools = _b.value;
|
|
2074
|
+
_d.label = 3;
|
|
2075
|
+
case 3:
|
|
2076
|
+
_d.trys.push([3, 5, , 6]);
|
|
2077
|
+
return [4 /*yield*/, scriptTools.execute({
|
|
2078
|
+
scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
|
|
2079
|
+
script: "".concat(functionName, "(result)"),
|
|
2080
|
+
parameters: {
|
|
2081
|
+
result: result || '',
|
|
2082
|
+
// Note: No ...parametersToPass, because working with result only
|
|
2083
|
+
},
|
|
2084
|
+
})];
|
|
2085
|
+
case 4: return [2 /*return*/, _d.sent()];
|
|
2086
|
+
case 5:
|
|
2087
|
+
error_5 = _d.sent();
|
|
2088
|
+
if (!(error_5 instanceof Error)) {
|
|
2089
|
+
throw error_5;
|
|
2090
|
+
}
|
|
2091
|
+
errors.push(error_5);
|
|
2092
|
+
return [3 /*break*/, 6];
|
|
2093
|
+
case 6:
|
|
2094
|
+
_b = _a.next();
|
|
2095
|
+
return [3 /*break*/, 2];
|
|
2096
|
+
case 7: return [3 /*break*/, 10];
|
|
2097
|
+
case 8:
|
|
2098
|
+
e_5_1 = _d.sent();
|
|
2099
|
+
e_5 = { error: e_5_1 };
|
|
2100
|
+
return [3 /*break*/, 10];
|
|
2101
|
+
case 9:
|
|
2102
|
+
try {
|
|
2103
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
2104
|
+
}
|
|
2105
|
+
finally { if (e_5) throw e_5.error; }
|
|
2106
|
+
return [7 /*endfinally*/];
|
|
2107
|
+
case 10:
|
|
2108
|
+
if (errors.length === 0) {
|
|
2109
|
+
throw new PromptbookExecutionError('Postprocessing in NaturalExecutionTools failed because no ScriptExecutionTools were provided');
|
|
2110
|
+
}
|
|
2111
|
+
else if (errors.length === 1) {
|
|
2112
|
+
throw errors[0];
|
|
2113
|
+
}
|
|
2114
|
+
else {
|
|
2115
|
+
throw new PromptbookExecutionError(spacetrim.spaceTrim(function (block) { return "\n Postprocessing in NaturalExecutionTools failed ".concat(errors.length, "x\n\n ").concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n\n')), "\n "); }));
|
|
2116
|
+
}
|
|
2117
|
+
}
|
|
2118
|
+
});
|
|
2119
|
+
}); }; }),
|
|
2062
2120
|
};
|
|
2063
2121
|
_b = currentTemplate.modelRequirements.modelVariant;
|
|
2064
2122
|
switch (_b) {
|
|
@@ -2131,14 +2189,14 @@
|
|
|
2131
2189
|
finally { if (e_2) throw e_2.error; }
|
|
2132
2190
|
return [7 /*endfinally*/];
|
|
2133
2191
|
case 23:
|
|
2134
|
-
if (resultString) {
|
|
2192
|
+
if (resultString !== null) {
|
|
2135
2193
|
return [3 /*break*/, 27];
|
|
2136
2194
|
}
|
|
2137
2195
|
if (scriptExecutionErrors.length === 1) {
|
|
2138
2196
|
throw scriptExecutionErrors[0];
|
|
2139
2197
|
}
|
|
2140
2198
|
else {
|
|
2141
|
-
throw new PromptbookExecutionError(
|
|
2199
|
+
throw new PromptbookExecutionError(spacetrim.spaceTrim(function (block) { return "\n Script execution failed ".concat(scriptExecutionErrors.length, " times\n\n ").concat(block(scriptExecutionErrors
|
|
2142
2200
|
.map(function (error) { return '- ' + error.message; })
|
|
2143
2201
|
.join('\n\n')), "\n "); }));
|
|
2144
2202
|
}
|
|
@@ -2184,7 +2242,10 @@
|
|
|
2184
2242
|
return [4 /*yield*/, scriptTools.execute({
|
|
2185
2243
|
scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
|
|
2186
2244
|
script: "".concat(functionName, "(resultString)"),
|
|
2187
|
-
parameters:
|
|
2245
|
+
parameters: {
|
|
2246
|
+
resultString: resultString || '',
|
|
2247
|
+
// Note: No ...parametersToPass, because working with result only
|
|
2248
|
+
},
|
|
2188
2249
|
})];
|
|
2189
2250
|
case 33:
|
|
2190
2251
|
resultString = _o.sent();
|
|
@@ -2275,7 +2336,7 @@
|
|
|
2275
2336
|
return [7 /*endfinally*/];
|
|
2276
2337
|
case 47:
|
|
2277
2338
|
if (expectError !== null && attempt === maxAttempts - 1) {
|
|
2278
|
-
throw new PromptbookExecutionError(
|
|
2339
|
+
throw new PromptbookExecutionError(spacetrim.spaceTrim(function (block) { return "\n Natural execution failed ".concat(maxExecutionAttempts, "x\n\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 ---\n "); }));
|
|
2279
2340
|
}
|
|
2280
2341
|
_o.label = 48;
|
|
2281
2342
|
case 48:
|
|
@@ -2344,7 +2405,7 @@
|
|
|
2344
2405
|
return template.dependentParameterNames.every(function (name) { return resovedParameters_1.includes(name); });
|
|
2345
2406
|
});
|
|
2346
2407
|
if (!(!currentTemplate && resolving_1.length === 0)) return [3 /*break*/, 1];
|
|
2347
|
-
throw new UnexpectedError(
|
|
2408
|
+
throw new UnexpectedError(spacetrim.spaceTrim("\n Can not resolve some parameters\n\n Note: This should be catched during validatePromptbookJson\n "));
|
|
2348
2409
|
case 1:
|
|
2349
2410
|
if (!!currentTemplate) return [3 /*break*/, 3];
|
|
2350
2411
|
/* [5] */ return [4 /*yield*/, Promise.race(resolving_1)];
|
|
@@ -2450,7 +2511,7 @@
|
|
|
2450
2511
|
console.info('๐ฌ Mocked gptChat call');
|
|
2451
2512
|
}
|
|
2452
2513
|
return [2 /*return*/, {
|
|
2453
|
-
content:
|
|
2514
|
+
content: spacetrim.spaceTrim(function (block) { return "\n You said:\n ".concat(block(prompt.content), "\n "); }),
|
|
2454
2515
|
model: 'mocked-echo',
|
|
2455
2516
|
timing: {
|
|
2456
2517
|
start: getCurrentIsoDate(),
|
|
@@ -2479,7 +2540,7 @@
|
|
|
2479
2540
|
console.info('๐ Mocked gptComplete call');
|
|
2480
2541
|
}
|
|
2481
2542
|
return [2 /*return*/, {
|
|
2482
|
-
content:
|
|
2543
|
+
content: spacetrim.spaceTrim(function (block) { return "\n ".concat(block(prompt.content), "\n And so on...\n "); }),
|
|
2483
2544
|
model: 'mocked-echo',
|
|
2484
2545
|
timing: {
|
|
2485
2546
|
start: getCurrentIsoDate(),
|
|
@@ -2507,29 +2568,73 @@
|
|
|
2507
2568
|
/**
|
|
2508
2569
|
* Gets the expectations and creates a fake text that meets the expectations
|
|
2509
2570
|
*
|
|
2571
|
+
* Note: You can provide postprocessing functions to modify the text before checking the expectations
|
|
2572
|
+
* The result will be the text BEFORE the postprocessing
|
|
2573
|
+
*
|
|
2510
2574
|
* @private internal util for MockedFackedNaturalExecutionTools
|
|
2511
2575
|
*/
|
|
2512
|
-
function $fakeTextToExpectations(expectations) {
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2576
|
+
function $fakeTextToExpectations(expectations, postprocessing) {
|
|
2577
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
2578
|
+
var lorem, loremText, text, loopLimit, textToCheck, _a, _b, func, e_1_1;
|
|
2579
|
+
var e_1, _c;
|
|
2580
|
+
return __generator(this, function (_d) {
|
|
2581
|
+
switch (_d.label) {
|
|
2582
|
+
case 0:
|
|
2583
|
+
lorem = new loremIpsum.LoremIpsum({
|
|
2584
|
+
wordsPerSentence: { min: 5, max: 15 },
|
|
2585
|
+
sentencesPerParagraph: { min: 5, max: 15 },
|
|
2586
|
+
});
|
|
2587
|
+
loremText = '';
|
|
2588
|
+
text = '';
|
|
2589
|
+
loopLimit = CHARACTER_LOOP_LIMIT;
|
|
2590
|
+
_d.label = 1;
|
|
2591
|
+
case 1:
|
|
2592
|
+
if (!(loopLimit-- > 0)) return [3 /*break*/, 11];
|
|
2593
|
+
textToCheck = text;
|
|
2594
|
+
_d.label = 2;
|
|
2595
|
+
case 2:
|
|
2596
|
+
_d.trys.push([2, 7, 8, 9]);
|
|
2597
|
+
_a = (e_1 = void 0, __values(postprocessing || [])), _b = _a.next();
|
|
2598
|
+
_d.label = 3;
|
|
2599
|
+
case 3:
|
|
2600
|
+
if (!!_b.done) return [3 /*break*/, 6];
|
|
2601
|
+
func = _b.value;
|
|
2602
|
+
return [4 /*yield*/, func(textToCheck)];
|
|
2603
|
+
case 4:
|
|
2604
|
+
textToCheck = _d.sent();
|
|
2605
|
+
_d.label = 5;
|
|
2606
|
+
case 5:
|
|
2607
|
+
_b = _a.next();
|
|
2608
|
+
return [3 /*break*/, 3];
|
|
2609
|
+
case 6: return [3 /*break*/, 9];
|
|
2610
|
+
case 7:
|
|
2611
|
+
e_1_1 = _d.sent();
|
|
2612
|
+
e_1 = { error: e_1_1 };
|
|
2613
|
+
return [3 /*break*/, 9];
|
|
2614
|
+
case 8:
|
|
2615
|
+
try {
|
|
2616
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
2617
|
+
}
|
|
2618
|
+
finally { if (e_1) throw e_1.error; }
|
|
2619
|
+
return [7 /*endfinally*/];
|
|
2620
|
+
case 9:
|
|
2621
|
+
if (isPassingExpectations(expectations, textToCheck)) {
|
|
2622
|
+
return [2 /*return*/, text]; // <- Note: Returning the text because the postprocessing
|
|
2623
|
+
}
|
|
2624
|
+
if (loremText === '') {
|
|
2625
|
+
loremText = lorem.generateParagraphs(1) + '\n\n';
|
|
2626
|
+
}
|
|
2627
|
+
text += loremText.substring(0, 1);
|
|
2628
|
+
loremText = loremText.substring(1);
|
|
2629
|
+
_d.label = 10;
|
|
2630
|
+
case 10: return [3 /*break*/, 1];
|
|
2631
|
+
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 "); }));
|
|
2632
|
+
}
|
|
2633
|
+
});
|
|
2516
2634
|
});
|
|
2517
|
-
var loremText = '';
|
|
2518
|
-
var text = '';
|
|
2519
|
-
for (var loopLimit = CHARACTER_LOOP_LIMIT; loopLimit-- > 0;) {
|
|
2520
|
-
if (isPassingExpectations(expectations, text)) {
|
|
2521
|
-
return text;
|
|
2522
|
-
}
|
|
2523
|
-
if (loremText === '') {
|
|
2524
|
-
loremText = lorem.generateParagraphs(1) + '\n\n';
|
|
2525
|
-
}
|
|
2526
|
-
text += loremText.substring(0, 1);
|
|
2527
|
-
loremText = loremText.substring(1);
|
|
2528
|
-
}
|
|
2529
|
-
throw new Error(spaceTrim__default["default"](function (block) { return "\n Can not generate fake text to met the expectations\n\n Loop limit reached\n The expectations:\n ".concat(block(JSON.stringify(expectations, null, 4)), "\n\n The draft text:\n ").concat(block(text), "\n\n "); }));
|
|
2530
2635
|
}
|
|
2531
2636
|
/**
|
|
2532
|
-
* TODO: Implement better
|
|
2637
|
+
* TODO: Implement better - create FakeLLM from this
|
|
2533
2638
|
* TODO: [๐] Unite object for expecting amount and format - use here also a format
|
|
2534
2639
|
*/
|
|
2535
2640
|
|
|
@@ -2545,29 +2650,40 @@
|
|
|
2545
2650
|
*/
|
|
2546
2651
|
MockedFackedNaturalExecutionTools.prototype.gptChat = function (prompt) {
|
|
2547
2652
|
return __awaiter(this, void 0, void 0, function () {
|
|
2653
|
+
var content, result;
|
|
2548
2654
|
return __generator(this, function (_a) {
|
|
2549
|
-
|
|
2550
|
-
|
|
2655
|
+
switch (_a.label) {
|
|
2656
|
+
case 0:
|
|
2657
|
+
if (this.options.isVerbose) {
|
|
2658
|
+
console.info('๐ฌ Mocked faked prompt', prompt);
|
|
2659
|
+
}
|
|
2660
|
+
return [4 /*yield*/, $fakeTextToExpectations(prompt.expectations || {
|
|
2661
|
+
sentences: { min: 1, max: 1 },
|
|
2662
|
+
}, prompt.postprocessing)];
|
|
2663
|
+
case 1:
|
|
2664
|
+
content = _a.sent();
|
|
2665
|
+
result = {
|
|
2666
|
+
content: content,
|
|
2667
|
+
model: 'mocked-facked',
|
|
2668
|
+
timing: {
|
|
2669
|
+
start: getCurrentIsoDate(),
|
|
2670
|
+
complete: getCurrentIsoDate(),
|
|
2671
|
+
},
|
|
2672
|
+
usage: {
|
|
2673
|
+
price: 0,
|
|
2674
|
+
inputTokens: 0,
|
|
2675
|
+
outputTokens: 0,
|
|
2676
|
+
},
|
|
2677
|
+
rawResponse: {
|
|
2678
|
+
note: 'This is mocked echo',
|
|
2679
|
+
},
|
|
2680
|
+
// <- [๐คนโโ๏ธ]
|
|
2681
|
+
};
|
|
2682
|
+
if (this.options.isVerbose) {
|
|
2683
|
+
console.info('๐ฌ Mocked faked result', result);
|
|
2684
|
+
}
|
|
2685
|
+
return [2 /*return*/, result];
|
|
2551
2686
|
}
|
|
2552
|
-
return [2 /*return*/, {
|
|
2553
|
-
content: $fakeTextToExpectations(prompt.expectations || {
|
|
2554
|
-
sentences: { min: 1, max: 1 },
|
|
2555
|
-
}),
|
|
2556
|
-
model: 'mocked-facked',
|
|
2557
|
-
timing: {
|
|
2558
|
-
start: getCurrentIsoDate(),
|
|
2559
|
-
complete: getCurrentIsoDate(),
|
|
2560
|
-
},
|
|
2561
|
-
usage: {
|
|
2562
|
-
price: 0,
|
|
2563
|
-
inputTokens: 0,
|
|
2564
|
-
outputTokens: 0,
|
|
2565
|
-
},
|
|
2566
|
-
rawResponse: {
|
|
2567
|
-
note: 'This is mocked echo',
|
|
2568
|
-
},
|
|
2569
|
-
// <- [๐คนโโ๏ธ]
|
|
2570
|
-
}];
|
|
2571
2687
|
});
|
|
2572
2688
|
});
|
|
2573
2689
|
};
|
|
@@ -2604,7 +2720,7 @@
|
|
|
2604
2720
|
case 1:
|
|
2605
2721
|
answer = _a.sent();
|
|
2606
2722
|
if (this.options.isVerbose) {
|
|
2607
|
-
console.info(
|
|
2723
|
+
console.info(spacetrim.spaceTrim(function (block) { return "\n \uD83D\uDCD6 ".concat(block(options.promptTitle), "\n \uD83D\uDC64 ").concat(block(answer), "\n "); }));
|
|
2608
2724
|
}
|
|
2609
2725
|
return [2 /*return*/, answer];
|
|
2610
2726
|
}
|
|
@@ -2631,9 +2747,9 @@
|
|
|
2631
2747
|
return __awaiter(this, void 0, void 0, function () {
|
|
2632
2748
|
var answer;
|
|
2633
2749
|
return __generator(this, function (_a) {
|
|
2634
|
-
answer = window.prompt(
|
|
2750
|
+
answer = window.prompt(spacetrim.spaceTrim(function (block) { return "\n ".concat(block(options.promptTitle), "\n\n ").concat(block(options.promptMessage), "\n "); }));
|
|
2635
2751
|
if (this.options.isVerbose) {
|
|
2636
|
-
console.info(
|
|
2752
|
+
console.info(spacetrim.spaceTrim(function (block) { return "\n \uD83D\uDCD6 ".concat(block(options.promptTitle), "\n \uD83D\uDC64 ").concat(block(answer || '๐ซ User cancelled prompt'), "\n "); }));
|
|
2637
2753
|
}
|
|
2638
2754
|
if (answer === null) {
|
|
2639
2755
|
throw new PromptbookExecutionError('User cancelled prompt');
|
|
@@ -2698,7 +2814,7 @@
|
|
|
2698
2814
|
for (var promptbooks_1 = __values(promptbooks), promptbooks_1_1 = promptbooks_1.next(); !promptbooks_1_1.done; promptbooks_1_1 = promptbooks_1.next()) {
|
|
2699
2815
|
var promptbook = promptbooks_1_1.value;
|
|
2700
2816
|
if (promptbook.promptbookUrl === undefined) {
|
|
2701
|
-
throw new PromptbookReferenceError(
|
|
2817
|
+
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 ")));
|
|
2702
2818
|
}
|
|
2703
2819
|
validatePromptbookJson(promptbook);
|
|
2704
2820
|
this.library.set(promptbook.promptbookUrl, promptbook);
|
|
@@ -2727,7 +2843,7 @@
|
|
|
2727
2843
|
var _this = this;
|
|
2728
2844
|
var promptbook = this.library.get(url);
|
|
2729
2845
|
if (!promptbook) {
|
|
2730
|
-
throw new PromptbookNotFoundError(
|
|
2846
|
+
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()
|
|
2731
2847
|
.map(function (promptbookUrl) { return "- ".concat(promptbookUrl); })
|
|
2732
2848
|
.join('\n')), "\n\n "); }));
|
|
2733
2849
|
}
|
|
@@ -2878,7 +2994,7 @@
|
|
|
2878
2994
|
case 0:
|
|
2879
2995
|
if (!!predicate(url)) return [3 /*break*/, 2];
|
|
2880
2996
|
_a = PromptbookNotFoundError.bind;
|
|
2881
|
-
return [4 /*yield*/,
|
|
2997
|
+
return [4 /*yield*/, spacetrim.spaceTrim(function (block) { return __awaiter(_this, void 0, void 0, function () {
|
|
2882
2998
|
var _a, _b, _c, _d, _e, _f;
|
|
2883
2999
|
return __generator(this, function (_g) {
|
|
2884
3000
|
switch (_g.label) {
|