@promptbook/core 0.69.0-0 → 0.69.0-10
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 +18 -58
- package/esm/index.es.js +1900 -1169
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/core.index.d.ts +5 -1
- package/esm/typings/src/_packages/types.index.d.ts +8 -0
- package/esm/typings/src/_packages/utils.index.d.ts +4 -4
- package/esm/typings/src/commands/FOREACH/ForeachCommand.d.ts +2 -6
- package/esm/typings/src/commands/FOREACH/ForeachJson.d.ts +26 -0
- package/esm/typings/src/commands/FOREACH/foreachCommand.test.d.ts +3 -0
- package/esm/typings/src/commands/FOREACH/foreachCommandParser.d.ts +2 -1
- package/esm/typings/src/commands/_common/types/CommandParser.d.ts +4 -4
- package/esm/typings/src/config.d.ts +7 -1
- package/esm/typings/src/conversion/prettify/renderPipelineMermaidOptions.d.ts +3 -0
- package/esm/typings/src/conversion/utils/extractParameterNamesFromTemplate.d.ts +2 -1
- package/esm/typings/src/conversion/validation/validatePipeline.d.ts +5 -0
- package/esm/typings/src/execution/PipelineExecutorResult.d.ts +5 -3
- package/esm/typings/src/execution/createPipelineExecutor/$OngoingTemplateResult.d.ts +21 -0
- package/esm/typings/src/execution/createPipelineExecutor/00-CreatePipelineExecutorOptions.d.ts +20 -0
- package/esm/typings/src/execution/createPipelineExecutor/00-CreatePipelineExecutorSettings.d.ts +33 -0
- package/esm/typings/src/execution/createPipelineExecutor/00-createPipelineExecutor.d.ts +10 -0
- package/esm/typings/src/execution/createPipelineExecutor/10-executePipeline.d.ts +55 -0
- package/esm/typings/src/execution/createPipelineExecutor/20-executeTemplate.d.ts +62 -0
- package/esm/typings/src/execution/createPipelineExecutor/30-executeFormatCells.d.ts +21 -0
- package/esm/typings/src/execution/createPipelineExecutor/40-executeAttempts.d.ts +74 -0
- package/esm/typings/src/execution/createPipelineExecutor/filterJustOutputParameters.d.ts +34 -0
- package/esm/typings/src/execution/createPipelineExecutor/getContextForTemplate.d.ts +10 -0
- package/esm/typings/src/execution/createPipelineExecutor/getKnowledgeForTemplate.d.ts +27 -0
- package/esm/typings/src/execution/createPipelineExecutor/getReservedParametersForTemplate.d.ts +30 -0
- package/esm/typings/src/execution/createPipelineExecutor/getSamplesForTemplate.d.ts +10 -0
- package/esm/typings/src/execution/utils/checkExpectations.d.ts +2 -0
- package/esm/typings/src/execution/utils/usageToHuman.d.ts +3 -4
- package/esm/typings/src/formats/_common/FormatDefinition.d.ts +9 -12
- package/esm/typings/src/formats/_common/FormatSubvalueDefinition.d.ts +30 -0
- package/esm/typings/src/formats/csv/{ListFormatDefinition.d.ts → CsvFormatDefinition.d.ts} +3 -2
- package/esm/typings/src/formats/index.d.ts +1 -1
- package/esm/typings/src/formats/json/JsonFormatDefinition.d.ts +1 -1
- package/esm/typings/src/formats/text/TextFormatDefinition.d.ts +17 -0
- package/esm/typings/src/formats/xml/XmlFormatDefinition.d.ts +1 -1
- package/esm/typings/src/types/PipelineJson/ParameterJson.d.ts +1 -0
- package/esm/typings/src/types/PipelineJson/TemplateJsonCommon.d.ts +5 -0
- package/esm/typings/src/types/execution-report/ExecutionReportJson.d.ts +3 -0
- package/esm/typings/src/utils/expectation-counters/index.d.ts +3 -0
- package/esm/typings/src/utils/{extractParameterNames.d.ts → parameters/extractParameterNames.d.ts} +2 -2
- package/esm/typings/src/utils/parameters/mapAvailableToExpectedParameters.d.ts +27 -0
- package/esm/typings/src/utils/{replaceParameters.d.ts → parameters/replaceParameters.d.ts} +2 -2
- package/esm/typings/src/utils/validators/parameterName/validateParameterName.d.ts +10 -0
- package/esm/typings/src/utils/validators/parameterName/validateParameterName.test.d.ts +1 -0
- package/package.json +2 -1
- package/umd/index.umd.js +1904 -1172
- package/umd/index.umd.js.map +1 -1
- package/esm/typings/src/execution/createPipelineExecutor.d.ts +0 -72
- package/esm/typings/src/formats/list/ListFormatDefinition.d.ts +0 -16
- /package/esm/typings/src/utils/{extractParameterNames.test.d.ts → parameters/extractParameterNames.test.d.ts} +0 -0
- /package/esm/typings/src/{execution/utils/usageToHuman.test.d.ts → utils/parameters/mapAvailableToExpectedParameters.test.d.ts} +0 -0
- /package/esm/typings/src/utils/{replaceParameters.test.d.ts → parameters/replaceParameters.test.d.ts} +0 -0
package/umd/index.umd.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('spacetrim'), require('prettier'), require('prettier/parser-html'), require('waitasecond'), require('crypto-js/enc-hex'), require('crypto-js/sha256'), require('moment')) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports', 'spacetrim', 'prettier', 'prettier/parser-html', 'waitasecond', 'crypto-js/enc-hex', 'crypto-js/sha256', 'moment'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-core"] = {}, global.spaceTrim, global.prettier, global.parserHtml, global.waitasecond, global.hexEncoder, global.sha256, global.moment));
|
|
5
|
-
})(this, (function (exports, spaceTrim, prettier, parserHtml, waitasecond, hexEncoder, sha256, moment) { 'use strict';
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('spacetrim'), require('prettier'), require('prettier/parser-html'), require('waitasecond'), require('papaparse'), require('crypto-js/enc-hex'), require('crypto-js/sha256'), require('moment')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', 'spacetrim', 'prettier', 'prettier/parser-html', 'waitasecond', 'papaparse', 'crypto-js/enc-hex', 'crypto-js/sha256', 'moment'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-core"] = {}, global.spaceTrim, global.prettier, global.parserHtml, global.waitasecond, global.papaparse, global.hexEncoder, global.sha256, global.moment));
|
|
5
|
+
})(this, (function (exports, spaceTrim, prettier, parserHtml, waitasecond, papaparse, hexEncoder, sha256, moment) { 'use strict';
|
|
6
6
|
|
|
7
7
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
8
|
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
/**
|
|
17
17
|
* The version of the Promptbook library
|
|
18
18
|
*/
|
|
19
|
-
var PROMPTBOOK_VERSION = '0.
|
|
19
|
+
var PROMPTBOOK_VERSION = '0.69.0-9';
|
|
20
20
|
// TODO: !!!! List here all the versions and annotate + put into script
|
|
21
21
|
|
|
22
22
|
/*! *****************************************************************************
|
|
@@ -658,13 +658,13 @@
|
|
|
658
658
|
*
|
|
659
659
|
* @public exported from `@promptbook/core`
|
|
660
660
|
*/
|
|
661
|
-
var MAX_PARALLEL_COUNT = 5;
|
|
661
|
+
var MAX_PARALLEL_COUNT = 5; // <- TODO: [🤹♂️]
|
|
662
662
|
/**
|
|
663
663
|
* The maximum number of attempts to execute LLM task before giving up
|
|
664
664
|
*
|
|
665
665
|
* @public exported from `@promptbook/core`
|
|
666
666
|
*/
|
|
667
|
-
var MAX_EXECUTION_ATTEMPTS = 3;
|
|
667
|
+
var MAX_EXECUTION_ATTEMPTS = 3; // <- TODO: [🤹♂️]
|
|
668
668
|
/**
|
|
669
669
|
* The maximum length of the (generated) filename
|
|
670
670
|
*
|
|
@@ -715,6 +715,7 @@
|
|
|
715
715
|
'samples',
|
|
716
716
|
'modelName',
|
|
717
717
|
'currentDate',
|
|
718
|
+
// <- TODO: !!!!! list here all command names
|
|
718
719
|
// <- TODO: Add more like 'date', 'modelName',...
|
|
719
720
|
// <- TODO: Add [emoji] + instructions ACRY when adding new reserved parameter
|
|
720
721
|
]);
|
|
@@ -758,6 +759,15 @@
|
|
|
758
759
|
* @public exported from `@promptbook/core`
|
|
759
760
|
*/
|
|
760
761
|
var IS_VERBOSE = false;
|
|
762
|
+
/**
|
|
763
|
+
* @@@
|
|
764
|
+
*
|
|
765
|
+
* @private within the repository
|
|
766
|
+
*/
|
|
767
|
+
var IS_PIPELINE_LOGIC_VALIDATED = just(
|
|
768
|
+
/**/
|
|
769
|
+
// Note: In normal situations, we check the pipeline logic:
|
|
770
|
+
true);
|
|
761
771
|
/**
|
|
762
772
|
* TODO: [🧠][🧜♂️] Maybe join remoteUrl and path into single value
|
|
763
773
|
*/
|
|
@@ -967,6 +977,26 @@
|
|
|
967
977
|
* @public exported from `@promptbook/core`
|
|
968
978
|
*/
|
|
969
979
|
function validatePipeline(pipeline) {
|
|
980
|
+
if (IS_PIPELINE_LOGIC_VALIDATED) {
|
|
981
|
+
validatePipelineCore(pipeline);
|
|
982
|
+
}
|
|
983
|
+
else {
|
|
984
|
+
try {
|
|
985
|
+
validatePipelineCore(pipeline);
|
|
986
|
+
}
|
|
987
|
+
catch (error) {
|
|
988
|
+
if (!(error instanceof PipelineLogicError)) {
|
|
989
|
+
throw error;
|
|
990
|
+
}
|
|
991
|
+
console.error(spaceTrim.spaceTrim(function (block) { return "\n Pipeline is not valid but logic errors are temporarily disabled via `IS_PIPELINE_LOGIC_VALIDATED`\n\n ".concat(block(error.message), "\n "); }));
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
return pipeline;
|
|
995
|
+
}
|
|
996
|
+
/**
|
|
997
|
+
* @private internal function for `validatePipeline`
|
|
998
|
+
*/
|
|
999
|
+
function validatePipelineCore(pipeline) {
|
|
970
1000
|
// TODO: [🧠] Maybe test if promptbook is a promise and make specific error case for that
|
|
971
1001
|
var e_1, _a, e_2, _b, e_3, _c;
|
|
972
1002
|
var pipelineIdentification = (function () {
|
|
@@ -991,12 +1021,12 @@
|
|
|
991
1021
|
// TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
|
|
992
1022
|
if (!Array.isArray(pipeline.parameters)) {
|
|
993
1023
|
// TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
|
|
994
|
-
throw new ParseError(spaceTrim.spaceTrim(function (block) { return "\n
|
|
1024
|
+
throw new ParseError(spaceTrim.spaceTrim(function (block) { return "\n Pipeline is valid JSON but with wrong structure\n\n `PipelineJson.parameters` expected to be an array, but got ".concat(typeof pipeline.parameters, "\n\n ").concat(block(pipelineIdentification), "\n "); }));
|
|
995
1025
|
}
|
|
996
1026
|
// TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
|
|
997
1027
|
if (!Array.isArray(pipeline.templates)) {
|
|
998
1028
|
// TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
|
|
999
|
-
throw new ParseError(spaceTrim.spaceTrim(function (block) { return "\n
|
|
1029
|
+
throw new ParseError(spaceTrim.spaceTrim(function (block) { return "\n Pipeline is valid JSON but with wrong structure\n\n `PipelineJson.templates` expected to be an array, but got ".concat(typeof pipeline.templates, "\n\n ").concat(block(pipelineIdentification), "\n "); }));
|
|
1000
1030
|
}
|
|
1001
1031
|
var _loop_1 = function (parameter) {
|
|
1002
1032
|
if (parameter.isInput && parameter.isOutput) {
|
|
@@ -1165,9 +1195,9 @@
|
|
|
1165
1195
|
while (unresovedTemplates.length > 0) {
|
|
1166
1196
|
_loop_3();
|
|
1167
1197
|
}
|
|
1168
|
-
return pipeline;
|
|
1169
1198
|
}
|
|
1170
1199
|
/**
|
|
1200
|
+
* TODO: !!!!!! [🧞♀️] Do not allow joker + foreach
|
|
1171
1201
|
* TODO: [🧠] Work with promptbookVersion
|
|
1172
1202
|
* TODO: Use here some json-schema, Zod or something similar and change it to:
|
|
1173
1203
|
* > /**
|
|
@@ -1794,7 +1824,7 @@
|
|
|
1794
1824
|
});
|
|
1795
1825
|
}
|
|
1796
1826
|
|
|
1797
|
-
var PipelineCollection = [{title:"Prepare Knowledge from Markdown",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.
|
|
1827
|
+
var PipelineCollection = [{title:"Prepare Knowledge from Markdown",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.69.0-9",parameters:[{name:"knowledgeContent",description:"Markdown document content",isInput:true,isOutput:false},{name:"knowledgePieces",description:"The knowledge JSON object",isInput:false,isOutput:true}],templates:[{templateType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",content:"You are experienced data researcher, extract the important knowledge from the document.\n\n# Rules\n\n- Make pieces of information concise, clear, and easy to understand\n- One piece of information should be approximately 1 paragraph\n- Divide the paragraphs by markdown horizontal lines ---\n- Omit irrelevant information\n- Group redundant information\n- Write just extracted information, nothing else\n\n# The document\n\nTake information from this document:\n\n> {knowledgeContent}",resultingParameterName:"knowledgePieces",dependentParameterNames:["knowledgeContent"]}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-from-markdown.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-keywords.ptbk.md",promptbookVersion:"0.69.0-9",parameters:[{name:"knowledgePieceContent",description:"The content",isInput:true,isOutput:false},{name:"keywords",description:"Keywords separated by comma",isInput:false,isOutput:true}],templates:[{templateType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",content:"You are experienced data researcher, detect the important keywords in the document.\n\n# Rules\n\n- Write just keywords separated by comma\n\n# The document\n\nTake information from this document:\n\n> {knowledgePieceContent}",resultingParameterName:"keywords",dependentParameterNames:["knowledgePieceContent"]}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-keywords.ptbk.md"},{title:"Prepare Title",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-title.ptbk.md",promptbookVersion:"0.69.0-9",parameters:[{name:"knowledgePieceContent",description:"The content",isInput:true,isOutput:false},{name:"title",description:"The title of the document",isInput:false,isOutput:true}],templates:[{templateType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",content:"You are experienced content creator, write best title for the document.\n\n# Rules\n\n- Write just title, nothing else\n- Title should be concise and clear\n- Write maximum 5 words for the title\n\n# The document\n\n> {knowledgePieceContent}",resultingParameterName:"title",expectations:{words:{min:1,max:8}},dependentParameterNames:["knowledgePieceContent"]}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-title.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-persona.ptbk.md",promptbookVersion:"0.69.0-9",parameters:[{name:"availableModelNames",description:"List of available model names separated by comma (,)",isInput:true,isOutput:false},{name:"personaDescription",description:"Description of the persona",isInput:true,isOutput:false},{name:"modelRequirements",description:"Specific requirements for the model",isInput:false,isOutput:true}],templates:[{templateType:"PROMPT_TEMPLATE",name:"make-model-requirements",title:"Make modelRequirements",content:"You are experienced AI engineer, you need to create virtual assistant.\nWrite\n\n## Sample\n\n```json\n{\n\"modelName\": \"gpt-4o\",\n\"systemMessage\": \"You are experienced AI engineer and helpfull assistant.\",\n\"temperature\": 0.7\n}\n```\n\n## Instructions\n\n- Your output format is JSON object\n- Write just the JSON object, no other text should be present\n- It contains the following keys:\n - `modelName`: The name of the model to use\n - `systemMessage`: The system message to provide context to the model\n - `temperature`: The sampling temperature to use\n\n### Key `modelName`\n\nPick from the following models:\n\n- {availableModelNames}\n\n### Key `systemMessage`\n\nThe system message is used to communicate instructions or provide context to the model at the beginning of a conversation. It is displayed in a different format compared to user messages, helping the model understand its role in the conversation. The system message typically guides the model's behavior, sets the tone, or specifies desired output from the model. By utilizing the system message effectively, users can steer the model towards generating more accurate and relevant responses.\n\nFor example:\n\n> You are an experienced AI engineer and helpful assistant.\n\n> You are a friendly and knowledgeable chatbot.\n\n### Key `temperature`\n\nThe sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.\n\nYou can pick a value between 0 and 2. For example:\n\n- `0.1`: Low temperature, extremely conservative and deterministic\n- `0.5`: Medium temperature, balanced between conservative and creative\n- `1.0`: High temperature, creative and bit random\n- `1.5`: Very high temperature, extremely creative and often chaotic and unpredictable\n- `2.0`: Maximum temperature, completely random and unpredictable, for some extreme creative use cases\n\n# The assistant\n\nTake this description of the persona:\n\n> {personaDescription}",resultingParameterName:"modelRequirements",format:"JSON",dependentParameterNames:["availableModelNames","personaDescription"]}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-persona.ptbk.md"}];
|
|
1798
1828
|
|
|
1799
1829
|
var defaultDiacriticsRemovalMap = [
|
|
1800
1830
|
{
|
|
@@ -2323,113 +2353,35 @@
|
|
|
2323
2353
|
*/
|
|
2324
2354
|
|
|
2325
2355
|
/**
|
|
2326
|
-
*
|
|
2327
|
-
*
|
|
2328
|
-
* @param script from which to extract the variables
|
|
2329
|
-
* @returns the list of variable names
|
|
2330
|
-
* @throws {ParseError} if the script is invalid
|
|
2331
|
-
* @public exported from `@promptbook/utils`
|
|
2332
|
-
*/
|
|
2333
|
-
function extractVariables(script) {
|
|
2334
|
-
var variables = new Set();
|
|
2335
|
-
script = "(()=>{".concat(script, "})()");
|
|
2336
|
-
try {
|
|
2337
|
-
for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
|
|
2338
|
-
try {
|
|
2339
|
-
eval(script);
|
|
2340
|
-
}
|
|
2341
|
-
catch (error) {
|
|
2342
|
-
if (!(error instanceof ReferenceError)) {
|
|
2343
|
-
throw error;
|
|
2344
|
-
}
|
|
2345
|
-
var undefinedName = error.message.split(' ')[0];
|
|
2346
|
-
/*
|
|
2347
|
-
Note: Parsing the error
|
|
2348
|
-
[PipelineUrlError: thing is not defined]
|
|
2349
|
-
*/
|
|
2350
|
-
if (!undefinedName) {
|
|
2351
|
-
throw error;
|
|
2352
|
-
}
|
|
2353
|
-
if (script.includes(undefinedName + '(')) {
|
|
2354
|
-
script = "const ".concat(undefinedName, " = ()=>'';") + script;
|
|
2355
|
-
}
|
|
2356
|
-
else {
|
|
2357
|
-
variables.add(undefinedName);
|
|
2358
|
-
script = "const ".concat(undefinedName, " = '';") + script;
|
|
2359
|
-
}
|
|
2360
|
-
}
|
|
2361
|
-
}
|
|
2362
|
-
catch (error) {
|
|
2363
|
-
if (!(error instanceof Error)) {
|
|
2364
|
-
throw error;
|
|
2365
|
-
}
|
|
2366
|
-
throw new ParseError(spaceTrim.spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.toString()), "}\n "); }));
|
|
2367
|
-
}
|
|
2368
|
-
return variables;
|
|
2369
|
-
}
|
|
2370
|
-
/**
|
|
2371
|
-
* TODO: [🔣] Support for multiple languages - python, java,...
|
|
2372
|
-
*/
|
|
2373
|
-
|
|
2374
|
-
/**
|
|
2375
|
-
* Parses the template and returns the set of all used parameters
|
|
2356
|
+
* Determine if the pipeline is fully prepared
|
|
2376
2357
|
*
|
|
2377
|
-
* @
|
|
2378
|
-
* @returns the set of parameter names
|
|
2379
|
-
* @throws {ParseError} if the script is invalid
|
|
2380
|
-
* @public exported from `@promptbook/utils`
|
|
2358
|
+
* @public exported from `@promptbook/core`
|
|
2381
2359
|
*/
|
|
2382
|
-
function
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
for (var _d = __values(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameterNames(title)), false), __read(extractParameterNames(description || '')), false), __read(extractParameterNames(content)), false), __read(extractParameterNames(preparedContent || '')), false)), _e = _d.next(); !_e.done; _e = _d.next()) {
|
|
2388
|
-
var parameterName = _e.value;
|
|
2389
|
-
parameterNames.add(parameterName);
|
|
2390
|
-
}
|
|
2391
|
-
}
|
|
2392
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2393
|
-
finally {
|
|
2394
|
-
try {
|
|
2395
|
-
if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
|
|
2396
|
-
}
|
|
2397
|
-
finally { if (e_1) throw e_1.error; }
|
|
2398
|
-
}
|
|
2399
|
-
if (templateType === 'SCRIPT_TEMPLATE') {
|
|
2400
|
-
try {
|
|
2401
|
-
for (var _f = __values(extractVariables(content)), _g = _f.next(); !_g.done; _g = _f.next()) {
|
|
2402
|
-
var parameterName = _g.value;
|
|
2403
|
-
parameterNames.add(parameterName);
|
|
2404
|
-
}
|
|
2405
|
-
}
|
|
2406
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
2407
|
-
finally {
|
|
2408
|
-
try {
|
|
2409
|
-
if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
|
|
2410
|
-
}
|
|
2411
|
-
finally { if (e_2) throw e_2.error; }
|
|
2412
|
-
}
|
|
2413
|
-
}
|
|
2414
|
-
try {
|
|
2415
|
-
for (var _h = __values(jokerParameterNames || []), _j = _h.next(); !_j.done; _j = _h.next()) {
|
|
2416
|
-
var jokerName = _j.value;
|
|
2417
|
-
parameterNames.add(jokerName);
|
|
2418
|
-
}
|
|
2360
|
+
function isPipelinePrepared(pipeline) {
|
|
2361
|
+
// Note: Ignoring `pipeline.preparations` @@@
|
|
2362
|
+
// Note: Ignoring `pipeline.knowledgePieces` @@@
|
|
2363
|
+
if (!pipeline.personas.every(function (persona) { return persona.modelRequirements !== undefined; })) {
|
|
2364
|
+
return false;
|
|
2419
2365
|
}
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
try {
|
|
2423
|
-
if (_j && !_j.done && (_c = _h.return)) _c.call(_h);
|
|
2424
|
-
}
|
|
2425
|
-
finally { if (e_3) throw e_3.error; }
|
|
2366
|
+
if (!pipeline.knowledgeSources.every(function (knowledgeSource) { return knowledgeSource.preparationIds !== undefined; })) {
|
|
2367
|
+
return false;
|
|
2426
2368
|
}
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2369
|
+
/*
|
|
2370
|
+
TODO: [🧠][🍫] `templates` can not be determined if they are fully prepared SO ignoring them
|
|
2371
|
+
> if (!pipeline.templates.every(({ preparedContent }) => preparedContent === undefined)) {
|
|
2372
|
+
> return false;
|
|
2373
|
+
> }
|
|
2374
|
+
*/
|
|
2375
|
+
return true;
|
|
2430
2376
|
}
|
|
2431
2377
|
/**
|
|
2432
|
-
* TODO: [
|
|
2378
|
+
* TODO: [🔃] !!!!! If the pipeline was prepared with different version or different set of models, prepare it once again
|
|
2379
|
+
* TODO: [🐠] Maybe base this on `makeValidator`
|
|
2380
|
+
* TODO: [🧊] Pipeline can be partially prepared, this should return true ONLY if fully prepared
|
|
2381
|
+
* TODO: [🧿] Maybe do same process with same granularity and subfinctions as `preparePipeline`
|
|
2382
|
+
* - [🏍] ? Is context in each template
|
|
2383
|
+
* - [♨] Are samples prepared
|
|
2384
|
+
* - [♨] Are templates prepared
|
|
2433
2385
|
*/
|
|
2434
2386
|
|
|
2435
2387
|
/**
|
|
@@ -2449,27 +2401,6 @@
|
|
|
2449
2401
|
};
|
|
2450
2402
|
}
|
|
2451
2403
|
|
|
2452
|
-
/**
|
|
2453
|
-
* Function isValidJsonString will tell you if the string is valid JSON or not
|
|
2454
|
-
*
|
|
2455
|
-
* @public exported from `@promptbook/utils`
|
|
2456
|
-
*/
|
|
2457
|
-
function isValidJsonString(value /* <- [👨⚖️] */) {
|
|
2458
|
-
try {
|
|
2459
|
-
JSON.parse(value);
|
|
2460
|
-
return true;
|
|
2461
|
-
}
|
|
2462
|
-
catch (error) {
|
|
2463
|
-
if (!(error instanceof Error)) {
|
|
2464
|
-
throw error;
|
|
2465
|
-
}
|
|
2466
|
-
if (error.message.includes('Unexpected token')) {
|
|
2467
|
-
return false;
|
|
2468
|
-
}
|
|
2469
|
-
return false;
|
|
2470
|
-
}
|
|
2471
|
-
}
|
|
2472
|
-
|
|
2473
2404
|
/**
|
|
2474
2405
|
* Multiple LLM Execution Tools is a proxy server that uses multiple execution tools internally and exposes the executor interface externally.
|
|
2475
2406
|
*
|
|
@@ -2764,288 +2695,153 @@
|
|
|
2764
2695
|
*/
|
|
2765
2696
|
|
|
2766
2697
|
/**
|
|
2767
|
-
*
|
|
2698
|
+
* Takes an item or an array of items and returns an array of items
|
|
2768
2699
|
*
|
|
2769
|
-
*
|
|
2770
|
-
*
|
|
2771
|
-
*
|
|
2772
|
-
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
2773
|
-
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
2700
|
+
* 1) Any item except array and undefined returns array with that one item (also null)
|
|
2701
|
+
* 2) Undefined returns empty array
|
|
2702
|
+
* 3) Array returns itself
|
|
2774
2703
|
*
|
|
2775
|
-
* @
|
|
2776
|
-
* @returns code blocks with language and content
|
|
2777
|
-
* @throws {ParseError} if block is not closed properly
|
|
2778
|
-
* @public exported from `@promptbook/markdown-utils`
|
|
2704
|
+
* @private internal utility
|
|
2779
2705
|
*/
|
|
2780
|
-
function
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
2706
|
+
function arrayableToArray(input) {
|
|
2707
|
+
if (input === undefined) {
|
|
2708
|
+
return [];
|
|
2709
|
+
}
|
|
2710
|
+
if (input instanceof Array) {
|
|
2711
|
+
return input;
|
|
2712
|
+
}
|
|
2713
|
+
return [input];
|
|
2714
|
+
}
|
|
2715
|
+
|
|
2716
|
+
/**
|
|
2717
|
+
* Parses the given script and returns the list of all used variables that are not defined in the script
|
|
2718
|
+
*
|
|
2719
|
+
* @param script from which to extract the variables
|
|
2720
|
+
* @returns the list of variable names
|
|
2721
|
+
* @throws {ParseError} if the script is invalid
|
|
2722
|
+
* @public exported from `@promptbook/utils`
|
|
2723
|
+
*/
|
|
2724
|
+
function extractVariables(script) {
|
|
2725
|
+
var variables = new Set();
|
|
2726
|
+
script = "(()=>{".concat(script, "})()");
|
|
2787
2727
|
try {
|
|
2788
|
-
for (var
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
if (currentCodeBlock === null) {
|
|
2792
|
-
currentCodeBlock = { blockNotation: '>', language: null, content: '' };
|
|
2793
|
-
} /* not else */
|
|
2794
|
-
if (currentCodeBlock.blockNotation === '>') {
|
|
2795
|
-
if (currentCodeBlock.content !== '') {
|
|
2796
|
-
currentCodeBlock.content += '\n';
|
|
2797
|
-
}
|
|
2798
|
-
currentCodeBlock.content += line.slice(2);
|
|
2799
|
-
}
|
|
2800
|
-
}
|
|
2801
|
-
else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '>' /* <- Note: [0] */) {
|
|
2802
|
-
codeBlocks.push(currentCodeBlock);
|
|
2803
|
-
currentCodeBlock = null;
|
|
2728
|
+
for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
|
|
2729
|
+
try {
|
|
2730
|
+
eval(script);
|
|
2804
2731
|
}
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
if (currentCodeBlock === null) {
|
|
2809
|
-
currentCodeBlock = { blockNotation: '```', language: language, content: '' };
|
|
2732
|
+
catch (error) {
|
|
2733
|
+
if (!(error instanceof ReferenceError)) {
|
|
2734
|
+
throw error;
|
|
2810
2735
|
}
|
|
2811
|
-
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2736
|
+
var undefinedName = error.message.split(' ')[0];
|
|
2737
|
+
/*
|
|
2738
|
+
Note: Parsing the error
|
|
2739
|
+
[PipelineUrlError: thing is not defined]
|
|
2740
|
+
*/
|
|
2741
|
+
if (!undefinedName) {
|
|
2742
|
+
throw error;
|
|
2817
2743
|
}
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
|
|
2744
|
+
if (script.includes(undefinedName + '(')) {
|
|
2745
|
+
script = "const ".concat(undefinedName, " = ()=>'';") + script;
|
|
2746
|
+
}
|
|
2747
|
+
else {
|
|
2748
|
+
variables.add(undefinedName);
|
|
2749
|
+
script = "const ".concat(undefinedName, " = '';") + script;
|
|
2822
2750
|
}
|
|
2823
|
-
currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
|
|
2824
2751
|
}
|
|
2752
|
+
}
|
|
2753
|
+
catch (error) {
|
|
2754
|
+
if (!(error instanceof Error)) {
|
|
2755
|
+
throw error;
|
|
2825
2756
|
}
|
|
2757
|
+
throw new ParseError(spaceTrim.spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.toString()), "}\n "); }));
|
|
2826
2758
|
}
|
|
2827
|
-
|
|
2828
|
-
finally {
|
|
2829
|
-
try {
|
|
2830
|
-
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
2831
|
-
}
|
|
2832
|
-
finally { if (e_1) throw e_1.error; }
|
|
2833
|
-
}
|
|
2834
|
-
if (currentCodeBlock !== null) {
|
|
2835
|
-
throw new ParseError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
|
|
2836
|
-
}
|
|
2837
|
-
return codeBlocks;
|
|
2838
|
-
}
|
|
2839
|
-
/**
|
|
2840
|
-
* TODO: Maybe name for `blockNotation` instead of '```' and '>'
|
|
2841
|
-
*/
|
|
2842
|
-
|
|
2843
|
-
/**
|
|
2844
|
-
* Extracts extracts exactly one valid JSON code block
|
|
2845
|
-
*
|
|
2846
|
-
* - When given string is a valid JSON as it is, it just returns it
|
|
2847
|
-
* - When there is no JSON code block the function throws a `ParseError`
|
|
2848
|
-
* - When there are multiple JSON code blocks the function throws a `ParseError`
|
|
2849
|
-
*
|
|
2850
|
-
* Note: It is not important if marked as ```json BUT if it is VALID JSON
|
|
2851
|
-
* Note: There are multiple simmilar function:
|
|
2852
|
-
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
2853
|
-
* - `extractJsonBlock` extracts exactly one valid JSON code block
|
|
2854
|
-
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
2855
|
-
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
2856
|
-
*
|
|
2857
|
-
* @public exported from `@promptbook/markdown-utils`
|
|
2858
|
-
* @throws {ParseError} if there is no valid JSON block in the markdown
|
|
2859
|
-
*/
|
|
2860
|
-
function extractJsonBlock(markdown) {
|
|
2861
|
-
if (isValidJsonString(markdown)) {
|
|
2862
|
-
return markdown;
|
|
2863
|
-
}
|
|
2864
|
-
var codeBlocks = extractAllBlocksFromMarkdown(markdown);
|
|
2865
|
-
var jsonBlocks = codeBlocks.filter(function (_a) {
|
|
2866
|
-
var content = _a.content;
|
|
2867
|
-
return isValidJsonString(content);
|
|
2868
|
-
});
|
|
2869
|
-
if (jsonBlocks.length === 0) {
|
|
2870
|
-
throw new Error('There is no valid JSON block in the markdown');
|
|
2871
|
-
}
|
|
2872
|
-
if (jsonBlocks.length > 1) {
|
|
2873
|
-
throw new Error('There are multiple JSON code blocks in the markdown');
|
|
2874
|
-
}
|
|
2875
|
-
return jsonBlocks[0].content;
|
|
2876
|
-
}
|
|
2877
|
-
/**
|
|
2878
|
-
* TODO: Add some auto-healing logic + extract YAML, JSON5, TOML, etc.
|
|
2879
|
-
* TODO: [🏢] Make this logic part of `JsonFormatDefinition` or `isValidJsonString`
|
|
2880
|
-
*/
|
|
2881
|
-
|
|
2882
|
-
/**
|
|
2883
|
-
* Determine if the pipeline is fully prepared
|
|
2884
|
-
*
|
|
2885
|
-
* @public exported from `@promptbook/core`
|
|
2886
|
-
*/
|
|
2887
|
-
function isPipelinePrepared(pipeline) {
|
|
2888
|
-
// Note: Ignoring `pipeline.preparations` @@@
|
|
2889
|
-
// Note: Ignoring `pipeline.knowledgePieces` @@@
|
|
2890
|
-
if (!pipeline.personas.every(function (persona) { return persona.modelRequirements !== undefined; })) {
|
|
2891
|
-
return false;
|
|
2892
|
-
}
|
|
2893
|
-
if (!pipeline.knowledgeSources.every(function (knowledgeSource) { return knowledgeSource.preparationIds !== undefined; })) {
|
|
2894
|
-
return false;
|
|
2895
|
-
}
|
|
2896
|
-
/*
|
|
2897
|
-
TODO: [🧠][🍫] `templates` can not be determined if they are fully prepared SO ignoring them
|
|
2898
|
-
> if (!pipeline.templates.every(({ preparedContent }) => preparedContent === undefined)) {
|
|
2899
|
-
> return false;
|
|
2900
|
-
> }
|
|
2901
|
-
*/
|
|
2902
|
-
return true;
|
|
2903
|
-
}
|
|
2904
|
-
/**
|
|
2905
|
-
* TODO: [🔃] !!!!! If the pipeline was prepared with different version or different set of models, prepare it once again
|
|
2906
|
-
* TODO: [🐠] Maybe base this on `makeValidator`
|
|
2907
|
-
* TODO: [🧊] Pipeline can be partially prepared, this should return true ONLY if fully prepared
|
|
2908
|
-
* TODO: [🧿] Maybe do same process with same granularity and subfinctions as `preparePipeline`
|
|
2909
|
-
* - [🏍] ? Is context in each template
|
|
2910
|
-
* - [♨] Are samples prepared
|
|
2911
|
-
* - [♨] Are templates prepared
|
|
2912
|
-
*/
|
|
2913
|
-
|
|
2914
|
-
/**
|
|
2915
|
-
* Takes an item or an array of items and returns an array of items
|
|
2916
|
-
*
|
|
2917
|
-
* 1) Any item except array and undefined returns array with that one item (also null)
|
|
2918
|
-
* 2) Undefined returns empty array
|
|
2919
|
-
* 3) Array returns itself
|
|
2920
|
-
*
|
|
2921
|
-
* @private internal utility
|
|
2922
|
-
*/
|
|
2923
|
-
function arrayableToArray(input) {
|
|
2924
|
-
if (input === undefined) {
|
|
2925
|
-
return [];
|
|
2926
|
-
}
|
|
2927
|
-
if (input instanceof Array) {
|
|
2928
|
-
return input;
|
|
2929
|
-
}
|
|
2930
|
-
return [input];
|
|
2931
|
-
}
|
|
2932
|
-
|
|
2933
|
-
/**
|
|
2934
|
-
* Just says that the variable is not used but should be kept
|
|
2935
|
-
* No side effects.
|
|
2936
|
-
*
|
|
2937
|
-
* Note: It can be usefull for:
|
|
2938
|
-
*
|
|
2939
|
-
* 1) Suppressing eager optimization of unused imports
|
|
2940
|
-
* 2) Suppressing eslint errors of unused variables in the tests
|
|
2941
|
-
* 3) Keeping the type of the variable for type testing
|
|
2942
|
-
*
|
|
2943
|
-
* @param value any values
|
|
2944
|
-
* @returns void
|
|
2945
|
-
* @private within the repository
|
|
2946
|
-
*/
|
|
2947
|
-
function keepUnused() {
|
|
2948
|
-
var valuesToKeep = [];
|
|
2949
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2950
|
-
valuesToKeep[_i] = arguments[_i];
|
|
2951
|
-
}
|
|
2759
|
+
return variables;
|
|
2952
2760
|
}
|
|
2953
|
-
|
|
2954
2761
|
/**
|
|
2955
|
-
*
|
|
2956
|
-
* No side effects.
|
|
2957
|
-
*
|
|
2958
|
-
* Note: It can be usefull suppressing eslint errors of unused variables
|
|
2959
|
-
*
|
|
2960
|
-
* @param value any values
|
|
2961
|
-
* @returns void
|
|
2962
|
-
* @private within the repository
|
|
2762
|
+
* TODO: [🔣] Support for multiple languages - python, java,...
|
|
2963
2763
|
*/
|
|
2964
|
-
function TODO_USE() {
|
|
2965
|
-
var value = [];
|
|
2966
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2967
|
-
value[_i] = arguments[_i];
|
|
2968
|
-
}
|
|
2969
|
-
}
|
|
2970
2764
|
|
|
2971
2765
|
/**
|
|
2972
|
-
*
|
|
2766
|
+
* Parses the template and returns the set of all used parameters
|
|
2973
2767
|
*
|
|
2974
|
-
* @param template the template with parameters
|
|
2975
|
-
* @
|
|
2976
|
-
* @
|
|
2977
|
-
* @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
|
|
2768
|
+
* @param template the template with used parameters
|
|
2769
|
+
* @returns the set of parameter names
|
|
2770
|
+
* @throws {ParseError} if the script is invalid
|
|
2978
2771
|
* @public exported from `@promptbook/utils`
|
|
2979
2772
|
*/
|
|
2980
|
-
function
|
|
2981
|
-
var e_1, _a;
|
|
2773
|
+
function extractParameterNamesFromTemplate(template) {
|
|
2774
|
+
var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
|
|
2775
|
+
var title = template.title, description = template.description, templateType = template.templateType, content = template.content, preparedContent = template.preparedContent, jokerParameterNames = template.jokerParameterNames, foreach = template.foreach;
|
|
2776
|
+
var parameterNames = new Set();
|
|
2982
2777
|
try {
|
|
2983
|
-
for (var
|
|
2984
|
-
var
|
|
2985
|
-
|
|
2986
|
-
throw new UnexpectedError("Parameter {".concat(parameterName, "} has missing value"));
|
|
2987
|
-
}
|
|
2988
|
-
else if (parameterValue === RESERVED_PARAMETER_RESTRICTED) {
|
|
2989
|
-
// TODO: [🍵]
|
|
2990
|
-
throw new UnexpectedError("Parameter {".concat(parameterName, "} is restricted to use"));
|
|
2991
|
-
}
|
|
2778
|
+
for (var _e = __values(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameterNames(title)), false), __read(extractParameterNames(description || '')), false), __read(extractParameterNames(content)), false), __read(extractParameterNames(preparedContent || '')), false)), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
2779
|
+
var parameterName = _f.value;
|
|
2780
|
+
parameterNames.add(parameterName);
|
|
2992
2781
|
}
|
|
2993
2782
|
}
|
|
2994
2783
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2995
2784
|
finally {
|
|
2996
2785
|
try {
|
|
2997
|
-
if (
|
|
2786
|
+
if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
|
|
2998
2787
|
}
|
|
2999
2788
|
finally { if (e_1) throw e_1.error; }
|
|
3000
2789
|
}
|
|
3001
|
-
|
|
3002
|
-
|
|
3003
|
-
|
|
3004
|
-
|
|
3005
|
-
|
|
3006
|
-
|
|
2790
|
+
if (templateType === 'SCRIPT_TEMPLATE') {
|
|
2791
|
+
try {
|
|
2792
|
+
for (var _g = __values(extractVariables(content)), _h = _g.next(); !_h.done; _h = _g.next()) {
|
|
2793
|
+
var parameterName = _h.value;
|
|
2794
|
+
parameterNames.add(parameterName);
|
|
2795
|
+
}
|
|
3007
2796
|
}
|
|
3008
|
-
|
|
3009
|
-
|
|
3010
|
-
|
|
3011
|
-
|
|
2797
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
2798
|
+
finally {
|
|
2799
|
+
try {
|
|
2800
|
+
if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
|
|
2801
|
+
}
|
|
2802
|
+
finally { if (e_2) throw e_2.error; }
|
|
3012
2803
|
}
|
|
3013
|
-
|
|
3014
|
-
|
|
2804
|
+
}
|
|
2805
|
+
try {
|
|
2806
|
+
for (var _j = __values(jokerParameterNames || []), _k = _j.next(); !_k.done; _k = _j.next()) {
|
|
2807
|
+
var jokerName = _k.value;
|
|
2808
|
+
parameterNames.add(jokerName);
|
|
3015
2809
|
}
|
|
3016
|
-
|
|
3017
|
-
|
|
2810
|
+
}
|
|
2811
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
2812
|
+
finally {
|
|
2813
|
+
try {
|
|
2814
|
+
if (_k && !_k.done && (_c = _j.return)) _c.call(_j);
|
|
3018
2815
|
}
|
|
3019
|
-
|
|
3020
|
-
|
|
3021
|
-
|
|
2816
|
+
finally { if (e_3) throw e_3.error; }
|
|
2817
|
+
}
|
|
2818
|
+
parameterNames.delete('content');
|
|
2819
|
+
// <- Note {websiteContent} is used in `preparedContent`
|
|
2820
|
+
// Note: [🍭] Fixing dependent subparameterName from FOREACH command
|
|
2821
|
+
if (foreach !== undefined) {
|
|
2822
|
+
try {
|
|
2823
|
+
for (var _l = __values(foreach.subparameterNames), _m = _l.next(); !_m.done; _m = _l.next()) {
|
|
2824
|
+
var subparameterName = _m.value;
|
|
2825
|
+
if (parameterNames.has(subparameterName)) {
|
|
2826
|
+
parameterNames.delete(subparameterName);
|
|
2827
|
+
parameterNames.add(foreach.parameterName);
|
|
2828
|
+
// <- TODO: [🚎] Warn/logic error when `subparameterName` not used
|
|
2829
|
+
}
|
|
2830
|
+
}
|
|
3022
2831
|
}
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
.
|
|
3027
|
-
|
|
3028
|
-
|
|
2832
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
2833
|
+
finally {
|
|
2834
|
+
try {
|
|
2835
|
+
if (_m && !_m.done && (_d = _l.return)) _d.call(_l);
|
|
2836
|
+
}
|
|
2837
|
+
finally { if (e_4) throw e_4.error; }
|
|
3029
2838
|
}
|
|
3030
|
-
replacedTemplate =
|
|
3031
|
-
replacedTemplate.substring(0, match.index + precol.length) +
|
|
3032
|
-
parameterValue +
|
|
3033
|
-
replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
|
|
3034
|
-
};
|
|
3035
|
-
while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
|
|
3036
|
-
.exec(replacedTemplate))) {
|
|
3037
|
-
_loop_1();
|
|
3038
2839
|
}
|
|
3039
|
-
|
|
3040
|
-
if (/{\w+$/.test(replacedTemplate)) {
|
|
3041
|
-
throw new PipelineExecutionError('Parameter is not closed');
|
|
3042
|
-
}
|
|
3043
|
-
// [💫] Check if there are parameters that are not opened properly
|
|
3044
|
-
if (/^\w+}/.test(replacedTemplate)) {
|
|
3045
|
-
throw new PipelineExecutionError('Parameter is not opened');
|
|
3046
|
-
}
|
|
3047
|
-
return replacedTemplate;
|
|
2840
|
+
return parameterNames;
|
|
3048
2841
|
}
|
|
2842
|
+
/**
|
|
2843
|
+
* TODO: [🔣] If script require contentLanguage
|
|
2844
|
+
*/
|
|
3049
2845
|
|
|
3050
2846
|
/**
|
|
3051
2847
|
* Create difference set of two sets.
|
|
@@ -3123,117 +2919,348 @@
|
|
|
3123
2919
|
}
|
|
3124
2920
|
|
|
3125
2921
|
/**
|
|
3126
|
-
*
|
|
2922
|
+
* Just marks a place of place where should be something implemented
|
|
2923
|
+
* No side effects.
|
|
3127
2924
|
*
|
|
3128
|
-
*
|
|
3129
|
-
*/
|
|
3130
|
-
function countCharacters(text) {
|
|
3131
|
-
// Remove null characters
|
|
3132
|
-
text = text.replace(/\0/g, '');
|
|
3133
|
-
// Replace emojis (and also ZWJ sequence) with hyphens
|
|
3134
|
-
text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
|
|
3135
|
-
text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
|
|
3136
|
-
text = text.replace(/\p{Extended_Pictographic}(\u{200D}\p{Extended_Pictographic})*/gu, '-');
|
|
3137
|
-
return text.length;
|
|
3138
|
-
}
|
|
3139
|
-
|
|
3140
|
-
/**
|
|
3141
|
-
* Counts number of lines in the text
|
|
2925
|
+
* Note: It can be usefull suppressing eslint errors of unused variables
|
|
3142
2926
|
*
|
|
3143
|
-
* @
|
|
2927
|
+
* @param value any values
|
|
2928
|
+
* @returns void
|
|
2929
|
+
* @private within the repository
|
|
3144
2930
|
*/
|
|
3145
|
-
function
|
|
3146
|
-
|
|
3147
|
-
|
|
2931
|
+
function TODO_USE() {
|
|
2932
|
+
var value = [];
|
|
2933
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2934
|
+
value[_i] = arguments[_i];
|
|
3148
2935
|
}
|
|
3149
|
-
return text.split('\n').length;
|
|
3150
2936
|
}
|
|
3151
2937
|
|
|
3152
2938
|
/**
|
|
3153
|
-
*
|
|
2939
|
+
* Definition for CSV spreadsheet
|
|
3154
2940
|
*
|
|
3155
|
-
* @public exported from `@promptbook/
|
|
2941
|
+
* @public exported from `@promptbook/core`
|
|
2942
|
+
* <- TODO: [🏢] Export from package `@promptbook/csv`
|
|
2943
|
+
*/
|
|
2944
|
+
var CsvFormatDefinition = {
|
|
2945
|
+
formatName: 'CSV',
|
|
2946
|
+
aliases: ['SPREADSHEET', 'TABLE'],
|
|
2947
|
+
isValid: function (value, schema) {
|
|
2948
|
+
TODO_USE(value /* <- TODO: Use value here */);
|
|
2949
|
+
TODO_USE(schema /* <- TODO: Use schema here */);
|
|
2950
|
+
return true;
|
|
2951
|
+
},
|
|
2952
|
+
canBeValid: function (partialValue, schema) {
|
|
2953
|
+
TODO_USE(partialValue /* <- TODO: Use partialValue here */);
|
|
2954
|
+
TODO_USE(schema /* <- TODO: Use schema here */);
|
|
2955
|
+
return true;
|
|
2956
|
+
},
|
|
2957
|
+
heal: function (value, schema) {
|
|
2958
|
+
TODO_USE(value /* <- TODO: Use partialValue here */);
|
|
2959
|
+
TODO_USE(schema /* <- TODO: Use schema here */);
|
|
2960
|
+
throw new Error('Not implemented');
|
|
2961
|
+
},
|
|
2962
|
+
subvalueDefinitions: [
|
|
2963
|
+
{
|
|
2964
|
+
subvalueName: 'ROW',
|
|
2965
|
+
mapValues: function (value, mapCallback) {
|
|
2966
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
2967
|
+
var csv, mappedData;
|
|
2968
|
+
return __generator(this, function (_a) {
|
|
2969
|
+
switch (_a.label) {
|
|
2970
|
+
case 0:
|
|
2971
|
+
csv = papaparse.parse(value, {
|
|
2972
|
+
header: true,
|
|
2973
|
+
delimiter: ',',
|
|
2974
|
+
quoteChar: '"',
|
|
2975
|
+
newline: '\r\n',
|
|
2976
|
+
skipEmptyLines: true,
|
|
2977
|
+
// encoding: 'utf8',
|
|
2978
|
+
// <- TODO: !!!!!! DEFAULT_CSV_OPTIONS
|
|
2979
|
+
// <- TODO: [🧠] How to define parsing options for formats, its different concept than schema
|
|
2980
|
+
});
|
|
2981
|
+
if (csv.errors.length !== 0) {
|
|
2982
|
+
throw new ParseError(// <- TODO: !!!!!! Split PipelineParseError and FormatParseError -> CsvParseError
|
|
2983
|
+
spaceTrim__default["default"](function (block) { return "\n CSV parsing error\n\n ".concat(block(csv.errors.map(function (error) { return error.message; }).join('\n\n')), "\n "); }));
|
|
2984
|
+
}
|
|
2985
|
+
return [4 /*yield*/, Promise.all(csv.data.map(function (row, index) { /*not await */ return mapCallback(row, index); }))];
|
|
2986
|
+
case 1:
|
|
2987
|
+
mappedData = _a.sent();
|
|
2988
|
+
return [2 /*return*/, papaparse.unparse(mappedData, {
|
|
2989
|
+
header: true,
|
|
2990
|
+
delimiter: ',',
|
|
2991
|
+
quoteChar: '"',
|
|
2992
|
+
newline: '\r\n',
|
|
2993
|
+
skipEmptyLines: true,
|
|
2994
|
+
// encoding: 'utf8',
|
|
2995
|
+
// <- TODO: !!!!!! DEFAULT_CSV_OPTIONS
|
|
2996
|
+
// <- TODO: [🧠] How to define parsing options for formats, its different concept than schema
|
|
2997
|
+
})];
|
|
2998
|
+
}
|
|
2999
|
+
});
|
|
3000
|
+
});
|
|
3001
|
+
},
|
|
3002
|
+
},
|
|
3003
|
+
{
|
|
3004
|
+
subvalueName: 'CELL',
|
|
3005
|
+
mapValues: function (value, mapCallback) {
|
|
3006
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
3007
|
+
var csv, mappedData;
|
|
3008
|
+
var _this = this;
|
|
3009
|
+
return __generator(this, function (_a) {
|
|
3010
|
+
switch (_a.label) {
|
|
3011
|
+
case 0:
|
|
3012
|
+
csv = papaparse.parse(value, {
|
|
3013
|
+
header: true,
|
|
3014
|
+
delimiter: ',',
|
|
3015
|
+
quoteChar: '"',
|
|
3016
|
+
newline: '\r\n',
|
|
3017
|
+
skipEmptyLines: true,
|
|
3018
|
+
// encoding: 'utf8',
|
|
3019
|
+
// <- TODO: !!!!!! DEFAULT_CSV_OPTIONS
|
|
3020
|
+
// <- TODO: [🧠] How to define parsing options for formats, its different concept than schema
|
|
3021
|
+
});
|
|
3022
|
+
if (csv.errors.length !== 0) {
|
|
3023
|
+
throw new ParseError(// <- TODO: !!!!!! Split PipelineParseError and FormatParseError -> CsvParseError
|
|
3024
|
+
spaceTrim__default["default"](function (block) { return "\n CSV parsing error\n\n ".concat(block(csv.errors.map(function (error) { return error.message; }).join('\n\n')), "\n "); }));
|
|
3025
|
+
}
|
|
3026
|
+
return [4 /*yield*/, Promise.all(csv.data.map(function (row, rowIndex) { return __awaiter(_this, void 0, void 0, function () {
|
|
3027
|
+
var _this = this;
|
|
3028
|
+
return __generator(this, function (_a) {
|
|
3029
|
+
return [2 /*return*/, /* not await */ Promise.all(Object.entries(row).map(function (_a, columnIndex) {
|
|
3030
|
+
var _b = __read(_a, 2), key = _b[0], value = _b[1];
|
|
3031
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
3032
|
+
var index;
|
|
3033
|
+
var _c;
|
|
3034
|
+
return __generator(this, function (_d) {
|
|
3035
|
+
index = rowIndex * Object.keys(row).length + columnIndex;
|
|
3036
|
+
return [2 /*return*/, /* not await */ mapCallback((_c = {}, _c[key] = value, _c), index)];
|
|
3037
|
+
});
|
|
3038
|
+
});
|
|
3039
|
+
}))];
|
|
3040
|
+
});
|
|
3041
|
+
}); }))];
|
|
3042
|
+
case 1:
|
|
3043
|
+
mappedData = _a.sent();
|
|
3044
|
+
return [2 /*return*/, papaparse.unparse(mappedData, {
|
|
3045
|
+
header: true,
|
|
3046
|
+
delimiter: ',',
|
|
3047
|
+
quoteChar: '"',
|
|
3048
|
+
newline: '\r\n',
|
|
3049
|
+
skipEmptyLines: true,
|
|
3050
|
+
// encoding: 'utf8',
|
|
3051
|
+
// <- TODO: !!!!!! DEFAULT_CSV_OPTIONS
|
|
3052
|
+
// <- TODO: [🧠] How to define parsing options for formats, its different concept than schema
|
|
3053
|
+
})];
|
|
3054
|
+
}
|
|
3055
|
+
});
|
|
3056
|
+
});
|
|
3057
|
+
},
|
|
3058
|
+
},
|
|
3059
|
+
],
|
|
3060
|
+
};
|
|
3061
|
+
/**
|
|
3062
|
+
* TODO: [🍓] In `CsvFormatDefinition` implement simple `isValid`
|
|
3063
|
+
* TODO: [🍓] In `CsvFormatDefinition` implement partial `canBeValid`
|
|
3064
|
+
* TODO: [🍓] In `CsvFormatDefinition` implement `heal
|
|
3065
|
+
* TODO: [🍓] In `CsvFormatDefinition` implement `subvalueDefinitions`
|
|
3066
|
+
* TODO: [🏢] Allow to expect something inside CSV objects and other formats
|
|
3156
3067
|
*/
|
|
3157
|
-
function countPages(text) {
|
|
3158
|
-
var sentencesPerPage = 5; // Assuming each page has 5 sentences
|
|
3159
|
-
var sentences = text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
|
|
3160
|
-
var pageCount = Math.ceil(sentences.length / sentencesPerPage);
|
|
3161
|
-
return pageCount;
|
|
3162
|
-
}
|
|
3163
3068
|
|
|
3164
3069
|
/**
|
|
3165
|
-
*
|
|
3070
|
+
* Function isValidJsonString will tell you if the string is valid JSON or not
|
|
3166
3071
|
*
|
|
3167
3072
|
* @public exported from `@promptbook/utils`
|
|
3168
3073
|
*/
|
|
3169
|
-
function
|
|
3170
|
-
|
|
3074
|
+
function isValidJsonString(value /* <- [👨⚖️] */) {
|
|
3075
|
+
try {
|
|
3076
|
+
JSON.parse(value);
|
|
3077
|
+
return true;
|
|
3078
|
+
}
|
|
3079
|
+
catch (error) {
|
|
3080
|
+
if (!(error instanceof Error)) {
|
|
3081
|
+
throw error;
|
|
3082
|
+
}
|
|
3083
|
+
if (error.message.includes('Unexpected token')) {
|
|
3084
|
+
return false;
|
|
3085
|
+
}
|
|
3086
|
+
return false;
|
|
3087
|
+
}
|
|
3171
3088
|
}
|
|
3172
3089
|
|
|
3173
3090
|
/**
|
|
3174
|
-
*
|
|
3091
|
+
* Definition for JSON format
|
|
3175
3092
|
*
|
|
3176
|
-
* @
|
|
3093
|
+
* @private still in development [🏢]
|
|
3177
3094
|
*/
|
|
3178
|
-
|
|
3179
|
-
|
|
3180
|
-
|
|
3095
|
+
var JsonFormatDefinition = {
|
|
3096
|
+
formatName: 'JSON',
|
|
3097
|
+
mimeType: 'application/json',
|
|
3098
|
+
isValid: function (value, schema) {
|
|
3099
|
+
TODO_USE(schema /* <- TODO: Use schema here */);
|
|
3100
|
+
return isValidJsonString(value);
|
|
3101
|
+
},
|
|
3102
|
+
canBeValid: function (partialValue, schema) {
|
|
3103
|
+
TODO_USE(partialValue /* <- TODO: Use partialValue here */);
|
|
3104
|
+
TODO_USE(schema /* <- TODO: Use schema here */);
|
|
3105
|
+
return true;
|
|
3106
|
+
},
|
|
3107
|
+
heal: function (value, schema) {
|
|
3108
|
+
TODO_USE(value /* <- TODO: Use partialValue here */);
|
|
3109
|
+
TODO_USE(schema /* <- TODO: Use schema here */);
|
|
3110
|
+
throw new Error('Not implemented');
|
|
3111
|
+
},
|
|
3112
|
+
subvalueDefinitions: [],
|
|
3113
|
+
};
|
|
3181
3114
|
/**
|
|
3182
|
-
*
|
|
3183
|
-
*
|
|
3184
|
-
*
|
|
3115
|
+
* TODO: [🧠] Maybe propper instance of object
|
|
3116
|
+
* TODO: [0] Make string_serialized_json
|
|
3117
|
+
* TODO: [1] Make type for JSON Schema
|
|
3118
|
+
* TODO: [🧠] What to use for validating JSONs - JSON Schema, ZoD, typescript types/interfaces,...?
|
|
3119
|
+
* TODO: [🍓] In `JsonFormatDefinition` implement simple `isValid`
|
|
3120
|
+
* TODO: [🍓] In `JsonFormatDefinition` implement partial `canBeValid`
|
|
3121
|
+
* TODO: [🍓] In `JsonFormatDefinition` implement `heal
|
|
3122
|
+
* TODO: [🍓] In `JsonFormatDefinition` implement `subvalueDefinitions`
|
|
3123
|
+
* TODO: [🏢] Allow to expect something inside JSON objects and other formats
|
|
3185
3124
|
*/
|
|
3186
|
-
function countSentences(text) {
|
|
3187
|
-
return splitIntoSentences(text).length;
|
|
3188
|
-
}
|
|
3189
3125
|
|
|
3190
3126
|
/**
|
|
3191
|
-
*
|
|
3127
|
+
* Definition for any text - this will be always valid
|
|
3192
3128
|
*
|
|
3193
|
-
*
|
|
3129
|
+
* Note: This is not useful for validation, but for splitting and mapping with `subvalueDefinitions`
|
|
3130
|
+
*
|
|
3131
|
+
* @public exported from `@promptbook/core`
|
|
3132
|
+
*/
|
|
3133
|
+
var TextFormatDefinition = {
|
|
3134
|
+
formatName: 'TEXT',
|
|
3135
|
+
isValid: function (value) {
|
|
3136
|
+
return typeof value === 'string';
|
|
3137
|
+
},
|
|
3138
|
+
canBeValid: function (partialValue) {
|
|
3139
|
+
return typeof partialValue === 'string';
|
|
3140
|
+
},
|
|
3141
|
+
heal: function () {
|
|
3142
|
+
throw new UnexpectedError('It does not make sense to call `TextFormatDefinition.heal`');
|
|
3143
|
+
},
|
|
3144
|
+
subvalueDefinitions: [
|
|
3145
|
+
{
|
|
3146
|
+
subvalueName: 'LINE',
|
|
3147
|
+
mapValues: function (value, mapCallback) {
|
|
3148
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
3149
|
+
var lines, mappedLines;
|
|
3150
|
+
return __generator(this, function (_a) {
|
|
3151
|
+
switch (_a.label) {
|
|
3152
|
+
case 0:
|
|
3153
|
+
lines = value.split('\n');
|
|
3154
|
+
return [4 /*yield*/, Promise.all(lines.map(function (lineContent, lineNumber) {
|
|
3155
|
+
// TODO: [🧠] Maybe option to skip empty line
|
|
3156
|
+
/* not await */ return mapCallback({
|
|
3157
|
+
lineContent: lineContent,
|
|
3158
|
+
// TODO: [🧠] Maybe also put here `lineNumber`
|
|
3159
|
+
}, lineNumber);
|
|
3160
|
+
}))];
|
|
3161
|
+
case 1:
|
|
3162
|
+
mappedLines = _a.sent();
|
|
3163
|
+
return [2 /*return*/, mappedLines.join('\n')];
|
|
3164
|
+
}
|
|
3165
|
+
});
|
|
3166
|
+
});
|
|
3167
|
+
},
|
|
3168
|
+
},
|
|
3169
|
+
// <- TODO: [🧠][🤠] Here should be all words, characters, lines, paragraphs, pages aviable as subvalues
|
|
3170
|
+
],
|
|
3171
|
+
};
|
|
3172
|
+
/**
|
|
3173
|
+
* TODO: [🧠][🤠] Here should be all words, characters, lines, paragraphs, pages aviable as subvalues
|
|
3174
|
+
* TODO: [🍓] In `TextFormatDefinition` implement simple `isValid`
|
|
3175
|
+
* TODO: [🍓] In `TextFormatDefinition` implement partial `canBeValid`
|
|
3176
|
+
* TODO: [🍓] In `TextFormatDefinition` implement `heal
|
|
3177
|
+
* TODO: [🍓] In `TextFormatDefinition` implement `subvalueDefinitions`
|
|
3178
|
+
* TODO: [🏢] Allow to expect something inside each item of list and other formats
|
|
3194
3179
|
*/
|
|
3195
|
-
function countWords(text) {
|
|
3196
|
-
text = text.replace(/[\p{Extended_Pictographic}]/gu, 'a');
|
|
3197
|
-
text = removeDiacritics(text);
|
|
3198
|
-
return text.split(/[^a-zа-я0-9]+/i).filter(function (word) { return word.length > 0; }).length;
|
|
3199
|
-
}
|
|
3200
3180
|
|
|
3201
3181
|
/**
|
|
3202
|
-
*
|
|
3182
|
+
* Definition for XML format
|
|
3203
3183
|
*
|
|
3204
|
-
* @
|
|
3184
|
+
* @private still in development [🏢]
|
|
3205
3185
|
*/
|
|
3206
|
-
var
|
|
3207
|
-
|
|
3208
|
-
|
|
3209
|
-
|
|
3210
|
-
|
|
3211
|
-
|
|
3212
|
-
|
|
3186
|
+
var XmlFormatDefinition = {
|
|
3187
|
+
formatName: 'XML',
|
|
3188
|
+
mimeType: 'application/xml',
|
|
3189
|
+
isValid: function (value, schema) {
|
|
3190
|
+
TODO_USE(value /* <- TODO: Use value here */);
|
|
3191
|
+
TODO_USE(schema /* <- TODO: Use schema here */);
|
|
3192
|
+
return true;
|
|
3193
|
+
},
|
|
3194
|
+
canBeValid: function (partialValue, schema) {
|
|
3195
|
+
TODO_USE(partialValue /* <- TODO: Use partialValue here */);
|
|
3196
|
+
TODO_USE(schema /* <- TODO: Use schema here */);
|
|
3197
|
+
return true;
|
|
3198
|
+
},
|
|
3199
|
+
heal: function (value, schema) {
|
|
3200
|
+
TODO_USE(value /* <- TODO: Use partialValue here */);
|
|
3201
|
+
TODO_USE(schema /* <- TODO: Use schema here */);
|
|
3202
|
+
throw new Error('Not implemented');
|
|
3203
|
+
},
|
|
3204
|
+
subvalueDefinitions: [],
|
|
3213
3205
|
};
|
|
3206
|
+
/**
|
|
3207
|
+
* TODO: [🧠] Maybe propper instance of object
|
|
3208
|
+
* TODO: [0] Make string_serialized_xml
|
|
3209
|
+
* TODO: [1] Make type for XML Schema
|
|
3210
|
+
* TODO: [🧠] What to use for validating XMLs - XSD,...
|
|
3211
|
+
* TODO: [🍓] In `XmlFormatDefinition` implement simple `isValid`
|
|
3212
|
+
* TODO: [🍓] In `XmlFormatDefinition` implement partial `canBeValid`
|
|
3213
|
+
* TODO: [🍓] In `XmlFormatDefinition` implement `heal
|
|
3214
|
+
* TODO: [🍓] In `XmlFormatDefinition` implement `subvalueDefinitions`
|
|
3215
|
+
* TODO: [🏢] Allow to expect something inside XML and other formats
|
|
3216
|
+
*/
|
|
3214
3217
|
|
|
3215
3218
|
/**
|
|
3216
|
-
*
|
|
3219
|
+
* Definitions for all formats supported by Promptbook
|
|
3217
3220
|
*
|
|
3218
|
-
*
|
|
3219
|
-
|
|
3220
|
-
|
|
3221
|
+
* @private internal index of `...` <- TODO [🏢]
|
|
3222
|
+
*/
|
|
3223
|
+
var FORMAT_DEFINITIONS = [
|
|
3224
|
+
JsonFormatDefinition,
|
|
3225
|
+
XmlFormatDefinition,
|
|
3226
|
+
TextFormatDefinition,
|
|
3227
|
+
CsvFormatDefinition,
|
|
3228
|
+
];
|
|
3229
|
+
|
|
3230
|
+
/**
|
|
3231
|
+
* Maps available parameters to expected parameters
|
|
3221
3232
|
*
|
|
3222
|
-
*
|
|
3223
|
-
*
|
|
3224
|
-
*
|
|
3233
|
+
* The strategy is:
|
|
3234
|
+
* 1) @@@
|
|
3235
|
+
* 2) @@@
|
|
3236
|
+
*
|
|
3237
|
+
* @throws {PipelineExecutionError} @@@
|
|
3238
|
+
* @private within the repository used in `createPipelineExecutor`
|
|
3225
3239
|
*/
|
|
3226
|
-
function
|
|
3240
|
+
function mapAvailableToExpectedParameters(options) {
|
|
3227
3241
|
var e_1, _a;
|
|
3242
|
+
var expectedParameters = options.expectedParameters, availableParameters = options.availableParameters;
|
|
3243
|
+
var availableParametersNames = new Set(Object.keys(availableParameters));
|
|
3244
|
+
var expectedParameterNames = new Set(Object.keys(expectedParameters));
|
|
3245
|
+
var mappedParameters = {};
|
|
3228
3246
|
try {
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
var
|
|
3232
|
-
|
|
3233
|
-
|
|
3234
|
-
|
|
3235
|
-
|
|
3236
|
-
|
|
3247
|
+
// Phase 1️⃣: Matching mapping
|
|
3248
|
+
for (var _b = __values(Array.from(union(availableParametersNames, expectedParameterNames))), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
3249
|
+
var parameterName = _c.value;
|
|
3250
|
+
// Situation: Parameter is available and expected
|
|
3251
|
+
if (availableParametersNames.has(parameterName) && expectedParameterNames.has(parameterName)) {
|
|
3252
|
+
mappedParameters[parameterName] = availableParameters[parameterName];
|
|
3253
|
+
// <- Note: [👩👩👧] Maybe detect parameter collision here?
|
|
3254
|
+
availableParametersNames.delete(parameterName);
|
|
3255
|
+
expectedParameterNames.delete(parameterName);
|
|
3256
|
+
}
|
|
3257
|
+
// Situation: Parameter is available but NOT expected
|
|
3258
|
+
else if (availableParametersNames.has(parameterName) && !expectedParameterNames.has(parameterName)) {
|
|
3259
|
+
// [🐱👤] Do not pass this parameter to prompt - Maybe use it non-matching mapping
|
|
3260
|
+
}
|
|
3261
|
+
// Situation: Parameter is NOT available BUT expected
|
|
3262
|
+
else if (!availableParametersNames.has(parameterName) && expectedParameterNames.has(parameterName)) {
|
|
3263
|
+
// Do nothing here - this will be maybe fixed in the non-matching mapping
|
|
3237
3264
|
}
|
|
3238
3265
|
}
|
|
3239
3266
|
}
|
|
@@ -3244,622 +3271,1145 @@
|
|
|
3244
3271
|
}
|
|
3245
3272
|
finally { if (e_1) throw e_1.error; }
|
|
3246
3273
|
}
|
|
3274
|
+
if (expectedParameterNames.size === 0) {
|
|
3275
|
+
// Note: [👨👨👧] Now we can freeze `mappedParameters` to prevent @@@
|
|
3276
|
+
Object.freeze(mappedParameters);
|
|
3277
|
+
return mappedParameters;
|
|
3278
|
+
}
|
|
3279
|
+
// Phase 2️⃣: Non-matching mapping
|
|
3280
|
+
if (expectedParameterNames.size !== availableParametersNames.size) {
|
|
3281
|
+
throw new PipelineExecutionError(spaceTrim__default["default"](function (block) { return "\n Can not map available parameters to expected parameters\n\n Mapped parameters:\n ".concat(block(Object.keys(mappedParameters)
|
|
3282
|
+
.map(function (parameterName) { return "- {".concat(parameterName, "}"); })
|
|
3283
|
+
.join('\n')), "\n\n Expected parameters which can not be mapped:\n ").concat(block(Array.from(expectedParameterNames)
|
|
3284
|
+
.map(function (parameterName) { return "- {".concat(parameterName, "}"); })
|
|
3285
|
+
.join('\n')), "\n\n Remaining available parameters:\n ").concat(block(Array.from(availableParametersNames)
|
|
3286
|
+
.map(function (parameterName) { return "- {".concat(parameterName, "}"); })
|
|
3287
|
+
.join('\n')), "\n\n "); }));
|
|
3288
|
+
}
|
|
3289
|
+
var expectedParameterNamesArray = Array.from(expectedParameterNames);
|
|
3290
|
+
var availableParametersNamesArray = Array.from(availableParametersNames);
|
|
3291
|
+
for (var i = 0; i < expectedParameterNames.size; i++) {
|
|
3292
|
+
mappedParameters[expectedParameterNamesArray[i]] = availableParameters[availableParametersNamesArray[i]];
|
|
3293
|
+
}
|
|
3294
|
+
// Note: [👨👨👧] Now we can freeze `mappedParameters` to prevent @@@
|
|
3295
|
+
Object.freeze(mappedParameters);
|
|
3296
|
+
return mappedParameters;
|
|
3247
3297
|
}
|
|
3298
|
+
|
|
3248
3299
|
/**
|
|
3249
|
-
*
|
|
3300
|
+
* Extracts all code blocks from markdown.
|
|
3250
3301
|
*
|
|
3251
|
-
* Note: There are
|
|
3252
|
-
* - `
|
|
3253
|
-
* - `
|
|
3302
|
+
* Note: There are multiple simmilar function:
|
|
3303
|
+
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
3304
|
+
* - `extractJsonBlock` extracts exactly one valid JSON code block
|
|
3305
|
+
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
3306
|
+
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
3254
3307
|
*
|
|
3255
|
-
* @
|
|
3256
|
-
* @
|
|
3308
|
+
* @param markdown any valid markdown
|
|
3309
|
+
* @returns code blocks with language and content
|
|
3310
|
+
* @throws {ParseError} if block is not closed properly
|
|
3311
|
+
* @public exported from `@promptbook/markdown-utils`
|
|
3257
3312
|
*/
|
|
3258
|
-
function
|
|
3313
|
+
function extractAllBlocksFromMarkdown(markdown) {
|
|
3314
|
+
var e_1, _a;
|
|
3315
|
+
var codeBlocks = [];
|
|
3316
|
+
var lines = markdown.split('\n');
|
|
3317
|
+
// Note: [0] Ensure that the last block notated by gt > will be closed
|
|
3318
|
+
lines.push('');
|
|
3319
|
+
var currentCodeBlock = null;
|
|
3259
3320
|
try {
|
|
3260
|
-
|
|
3261
|
-
|
|
3321
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
3322
|
+
var line = lines_1_1.value;
|
|
3323
|
+
if (line.startsWith('> ') || line === '>') {
|
|
3324
|
+
if (currentCodeBlock === null) {
|
|
3325
|
+
currentCodeBlock = { blockNotation: '>', language: null, content: '' };
|
|
3326
|
+
} /* not else */
|
|
3327
|
+
if (currentCodeBlock.blockNotation === '>') {
|
|
3328
|
+
if (currentCodeBlock.content !== '') {
|
|
3329
|
+
currentCodeBlock.content += '\n';
|
|
3330
|
+
}
|
|
3331
|
+
currentCodeBlock.content += line.slice(2);
|
|
3332
|
+
}
|
|
3333
|
+
}
|
|
3334
|
+
else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '>' /* <- Note: [0] */) {
|
|
3335
|
+
codeBlocks.push(currentCodeBlock);
|
|
3336
|
+
currentCodeBlock = null;
|
|
3337
|
+
}
|
|
3338
|
+
/* not else */
|
|
3339
|
+
if (line.startsWith('```')) {
|
|
3340
|
+
var language = line.slice(3).trim() || null;
|
|
3341
|
+
if (currentCodeBlock === null) {
|
|
3342
|
+
currentCodeBlock = { blockNotation: '```', language: language, content: '' };
|
|
3343
|
+
}
|
|
3344
|
+
else {
|
|
3345
|
+
if (language !== null) {
|
|
3346
|
+
throw new ParseError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
|
|
3347
|
+
}
|
|
3348
|
+
codeBlocks.push(currentCodeBlock);
|
|
3349
|
+
currentCodeBlock = null;
|
|
3350
|
+
}
|
|
3351
|
+
}
|
|
3352
|
+
else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '```') {
|
|
3353
|
+
if (currentCodeBlock.content !== '') {
|
|
3354
|
+
currentCodeBlock.content += '\n';
|
|
3355
|
+
}
|
|
3356
|
+
currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
|
|
3357
|
+
}
|
|
3358
|
+
}
|
|
3262
3359
|
}
|
|
3263
|
-
catch (
|
|
3264
|
-
|
|
3265
|
-
|
|
3360
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
3361
|
+
finally {
|
|
3362
|
+
try {
|
|
3363
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
3266
3364
|
}
|
|
3267
|
-
|
|
3365
|
+
finally { if (e_1) throw e_1.error; }
|
|
3268
3366
|
}
|
|
3367
|
+
if (currentCodeBlock !== null) {
|
|
3368
|
+
throw new ParseError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
|
|
3369
|
+
}
|
|
3370
|
+
return codeBlocks;
|
|
3269
3371
|
}
|
|
3270
3372
|
/**
|
|
3271
|
-
* TODO:
|
|
3373
|
+
* TODO: Maybe name for `blockNotation` instead of '```' and '>'
|
|
3272
3374
|
*/
|
|
3273
3375
|
|
|
3274
3376
|
/**
|
|
3275
|
-
*
|
|
3377
|
+
* Extracts extracts exactly one valid JSON code block
|
|
3276
3378
|
*
|
|
3277
|
-
*
|
|
3278
|
-
*
|
|
3279
|
-
*
|
|
3379
|
+
* - When given string is a valid JSON as it is, it just returns it
|
|
3380
|
+
* - When there is no JSON code block the function throws a `ParseError`
|
|
3381
|
+
* - When there are multiple JSON code blocks the function throws a `ParseError`
|
|
3382
|
+
*
|
|
3383
|
+
* Note: It is not important if marked as ```json BUT if it is VALID JSON
|
|
3384
|
+
* Note: There are multiple simmilar function:
|
|
3385
|
+
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
3386
|
+
* - `extractJsonBlock` extracts exactly one valid JSON code block
|
|
3387
|
+
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
3388
|
+
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
3389
|
+
*
|
|
3390
|
+
* @public exported from `@promptbook/markdown-utils`
|
|
3391
|
+
* @throws {ParseError} if there is no valid JSON block in the markdown
|
|
3280
3392
|
*/
|
|
3281
|
-
function
|
|
3282
|
-
|
|
3283
|
-
|
|
3284
|
-
var _b = settings.maxExecutionAttempts, maxExecutionAttempts = _b === void 0 ? MAX_EXECUTION_ATTEMPTS : _b, _c = settings.maxParallelCount, maxParallelCount = _c === void 0 ? MAX_PARALLEL_COUNT : _c, _d = settings.isVerbose, isVerbose = _d === void 0 ? IS_VERBOSE : _d, _e = settings.isNotPreparedWarningSupressed, isNotPreparedWarningSupressed = _e === void 0 ? false : _e;
|
|
3285
|
-
validatePipeline(pipeline);
|
|
3286
|
-
var pipelineIdentification = (function () {
|
|
3287
|
-
// Note: This is a 😐 implementation of [🚞]
|
|
3288
|
-
var _ = [];
|
|
3289
|
-
if (pipeline.sourceFile !== undefined) {
|
|
3290
|
-
_.push("File: ".concat(pipeline.sourceFile));
|
|
3291
|
-
}
|
|
3292
|
-
if (pipeline.pipelineUrl !== undefined) {
|
|
3293
|
-
_.push("Url: ".concat(pipeline.pipelineUrl));
|
|
3294
|
-
}
|
|
3295
|
-
return _.join('\n');
|
|
3296
|
-
})();
|
|
3297
|
-
var llmTools = joinLlmExecutionTools.apply(void 0, __spreadArray([], __read(arrayableToArray(tools.llm)), false));
|
|
3298
|
-
var preparedPipeline;
|
|
3299
|
-
if (isPipelinePrepared(pipeline)) {
|
|
3300
|
-
preparedPipeline = pipeline;
|
|
3301
|
-
}
|
|
3302
|
-
else if (isNotPreparedWarningSupressed !== true) {
|
|
3303
|
-
console.warn(spaceTrim.spaceTrim(function (block) { return "\n Pipeline is not prepared\n\n ".concat(block(pipelineIdentification), "\n\n It will be prepared ad-hoc before the first execution and **returned as `preparedPipeline` in `PipelineExecutorResult`**\n But it is recommended to prepare the pipeline during collection preparation\n\n @see more at https://ptbk.io/prepare-pipeline\n "); }));
|
|
3393
|
+
function extractJsonBlock(markdown) {
|
|
3394
|
+
if (isValidJsonString(markdown)) {
|
|
3395
|
+
return markdown;
|
|
3304
3396
|
}
|
|
3305
|
-
var
|
|
3306
|
-
|
|
3307
|
-
|
|
3308
|
-
|
|
3309
|
-
|
|
3310
|
-
|
|
3311
|
-
|
|
3312
|
-
|
|
3313
|
-
|
|
3397
|
+
var codeBlocks = extractAllBlocksFromMarkdown(markdown);
|
|
3398
|
+
var jsonBlocks = codeBlocks.filter(function (_a) {
|
|
3399
|
+
var content = _a.content;
|
|
3400
|
+
return isValidJsonString(content);
|
|
3401
|
+
});
|
|
3402
|
+
if (jsonBlocks.length === 0) {
|
|
3403
|
+
throw new Error('There is no valid JSON block in the markdown');
|
|
3404
|
+
}
|
|
3405
|
+
if (jsonBlocks.length > 1) {
|
|
3406
|
+
throw new Error('There are multiple JSON code blocks in the markdown');
|
|
3407
|
+
}
|
|
3408
|
+
return jsonBlocks[0].content;
|
|
3409
|
+
}
|
|
3410
|
+
/**
|
|
3411
|
+
* TODO: Add some auto-healing logic + extract YAML, JSON5, TOML, etc.
|
|
3412
|
+
* TODO: [🏢] Make this logic part of `JsonFormatDefinition` or `isValidJsonString`
|
|
3413
|
+
*/
|
|
3414
|
+
|
|
3415
|
+
/**
|
|
3416
|
+
* Just says that the variable is not used but should be kept
|
|
3417
|
+
* No side effects.
|
|
3418
|
+
*
|
|
3419
|
+
* Note: It can be usefull for:
|
|
3420
|
+
*
|
|
3421
|
+
* 1) Suppressing eager optimization of unused imports
|
|
3422
|
+
* 2) Suppressing eslint errors of unused variables in the tests
|
|
3423
|
+
* 3) Keeping the type of the variable for type testing
|
|
3424
|
+
*
|
|
3425
|
+
* @param value any values
|
|
3426
|
+
* @returns void
|
|
3427
|
+
* @private within the repository
|
|
3428
|
+
*/
|
|
3429
|
+
function keepUnused() {
|
|
3430
|
+
var valuesToKeep = [];
|
|
3431
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
3432
|
+
valuesToKeep[_i] = arguments[_i];
|
|
3433
|
+
}
|
|
3434
|
+
}
|
|
3435
|
+
|
|
3436
|
+
/**
|
|
3437
|
+
* Replaces parameters in template with values from parameters object
|
|
3438
|
+
*
|
|
3439
|
+
* @param template the template with parameters in {curly} braces
|
|
3440
|
+
* @param parameters the object with parameters
|
|
3441
|
+
* @returns the template with replaced parameters
|
|
3442
|
+
* @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
|
|
3443
|
+
* @public exported from `@promptbook/utils`
|
|
3444
|
+
*/
|
|
3445
|
+
function replaceParameters(template, parameters) {
|
|
3446
|
+
var e_1, _a;
|
|
3447
|
+
try {
|
|
3448
|
+
for (var _b = __values(Object.entries(parameters)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
3449
|
+
var _d = __read(_c.value, 2), parameterName = _d[0], parameterValue = _d[1];
|
|
3450
|
+
if (parameterValue === RESERVED_PARAMETER_MISSING_VALUE) {
|
|
3451
|
+
throw new UnexpectedError("Parameter {".concat(parameterName, "} has missing value"));
|
|
3452
|
+
}
|
|
3453
|
+
else if (parameterValue === RESERVED_PARAMETER_RESTRICTED) {
|
|
3454
|
+
// TODO: [🍵]
|
|
3455
|
+
throw new UnexpectedError("Parameter {".concat(parameterName, "} is restricted to use"));
|
|
3456
|
+
}
|
|
3314
3457
|
}
|
|
3315
|
-
|
|
3316
|
-
|
|
3317
|
-
|
|
3318
|
-
|
|
3319
|
-
|
|
3320
|
-
return [2 /*return*/, preparedPipeline.knowledgePieces.map(function (_a) {
|
|
3321
|
-
var content = _a.content;
|
|
3322
|
-
return "- ".concat(content);
|
|
3323
|
-
}).join('\n')];
|
|
3324
|
-
});
|
|
3325
|
-
});
|
|
3458
|
+
}
|
|
3459
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
3460
|
+
finally {
|
|
3461
|
+
try {
|
|
3462
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
3326
3463
|
}
|
|
3327
|
-
|
|
3328
|
-
|
|
3329
|
-
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
|
|
3334
|
-
|
|
3464
|
+
finally { if (e_1) throw e_1.error; }
|
|
3465
|
+
}
|
|
3466
|
+
var replacedTemplate = template;
|
|
3467
|
+
var match;
|
|
3468
|
+
var loopLimit = LOOP_LIMIT;
|
|
3469
|
+
var _loop_1 = function () {
|
|
3470
|
+
if (loopLimit-- < 0) {
|
|
3471
|
+
throw new LimitReachedError('Loop limit reached during parameters replacement in `replaceParameters`');
|
|
3335
3472
|
}
|
|
3336
|
-
|
|
3337
|
-
|
|
3338
|
-
|
|
3339
|
-
|
|
3340
|
-
|
|
3341
|
-
|
|
3342
|
-
|
|
3343
|
-
|
|
3344
|
-
|
|
3345
|
-
|
|
3346
|
-
|
|
3347
|
-
|
|
3348
|
-
|
|
3349
|
-
|
|
3350
|
-
|
|
3351
|
-
|
|
3352
|
-
|
|
3353
|
-
|
|
3354
|
-
|
|
3355
|
-
|
|
3356
|
-
|
|
3357
|
-
|
|
3358
|
-
|
|
3359
|
-
|
|
3360
|
-
|
|
3361
|
-
|
|
3362
|
-
|
|
3363
|
-
|
|
3364
|
-
|
|
3365
|
-
|
|
3366
|
-
|
|
3367
|
-
|
|
3368
|
-
|
|
3369
|
-
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
|
|
3473
|
+
var precol = match.groups.precol;
|
|
3474
|
+
var parameterName = match.groups.parameterName;
|
|
3475
|
+
if (parameterName === '') {
|
|
3476
|
+
return "continue";
|
|
3477
|
+
}
|
|
3478
|
+
if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
|
|
3479
|
+
throw new PipelineExecutionError('Parameter is already opened or not closed');
|
|
3480
|
+
}
|
|
3481
|
+
if (parameters[parameterName] === undefined) {
|
|
3482
|
+
throw new PipelineExecutionError("Parameter {".concat(parameterName, "} is not defined"));
|
|
3483
|
+
}
|
|
3484
|
+
var parameterValue = parameters[parameterName];
|
|
3485
|
+
if (parameterValue === undefined) {
|
|
3486
|
+
throw new PipelineExecutionError("Parameter {".concat(parameterName, "} is not defined"));
|
|
3487
|
+
}
|
|
3488
|
+
parameterValue = parameterValue.toString();
|
|
3489
|
+
if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
|
|
3490
|
+
parameterValue = parameterValue
|
|
3491
|
+
.split('\n')
|
|
3492
|
+
.map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
|
|
3493
|
+
.join('\n');
|
|
3494
|
+
}
|
|
3495
|
+
replacedTemplate =
|
|
3496
|
+
replacedTemplate.substring(0, match.index + precol.length) +
|
|
3497
|
+
parameterValue +
|
|
3498
|
+
replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
|
|
3499
|
+
};
|
|
3500
|
+
while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
|
|
3501
|
+
.exec(replacedTemplate))) {
|
|
3502
|
+
_loop_1();
|
|
3503
|
+
}
|
|
3504
|
+
// [💫] Check if there are parameters that are not closed properly
|
|
3505
|
+
if (/{\w+$/.test(replacedTemplate)) {
|
|
3506
|
+
throw new PipelineExecutionError('Parameter is not closed');
|
|
3507
|
+
}
|
|
3508
|
+
// [💫] Check if there are parameters that are not opened properly
|
|
3509
|
+
if (/^\w+}/.test(replacedTemplate)) {
|
|
3510
|
+
throw new PipelineExecutionError('Parameter is not opened');
|
|
3511
|
+
}
|
|
3512
|
+
return replacedTemplate;
|
|
3513
|
+
}
|
|
3514
|
+
|
|
3515
|
+
/**
|
|
3516
|
+
* Counts number of characters in the text
|
|
3517
|
+
*
|
|
3518
|
+
* @public exported from `@promptbook/utils`
|
|
3519
|
+
*/
|
|
3520
|
+
function countCharacters(text) {
|
|
3521
|
+
// Remove null characters
|
|
3522
|
+
text = text.replace(/\0/g, '');
|
|
3523
|
+
// Replace emojis (and also ZWJ sequence) with hyphens
|
|
3524
|
+
text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
|
|
3525
|
+
text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
|
|
3526
|
+
text = text.replace(/\p{Extended_Pictographic}(\u{200D}\p{Extended_Pictographic})*/gu, '-');
|
|
3527
|
+
return text.length;
|
|
3528
|
+
}
|
|
3529
|
+
|
|
3530
|
+
/**
|
|
3531
|
+
* Counts number of lines in the text
|
|
3532
|
+
*
|
|
3533
|
+
* @public exported from `@promptbook/utils`
|
|
3534
|
+
*/
|
|
3535
|
+
function countLines(text) {
|
|
3536
|
+
if (text === '') {
|
|
3537
|
+
return 0;
|
|
3538
|
+
}
|
|
3539
|
+
return text.split('\n').length;
|
|
3540
|
+
}
|
|
3541
|
+
|
|
3542
|
+
/**
|
|
3543
|
+
* Counts number of pages in the text
|
|
3544
|
+
*
|
|
3545
|
+
* @public exported from `@promptbook/utils`
|
|
3546
|
+
*/
|
|
3547
|
+
function countPages(text) {
|
|
3548
|
+
var sentencesPerPage = 5; // Assuming each page has 5 sentences
|
|
3549
|
+
var sentences = text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
|
|
3550
|
+
var pageCount = Math.ceil(sentences.length / sentencesPerPage);
|
|
3551
|
+
return pageCount;
|
|
3552
|
+
}
|
|
3553
|
+
|
|
3554
|
+
/**
|
|
3555
|
+
* Counts number of paragraphs in the text
|
|
3556
|
+
*
|
|
3557
|
+
* @public exported from `@promptbook/utils`
|
|
3558
|
+
*/
|
|
3559
|
+
function countParagraphs(text) {
|
|
3560
|
+
return text.split(/\n\s*\n/).filter(function (paragraph) { return paragraph.trim() !== ''; }).length;
|
|
3561
|
+
}
|
|
3562
|
+
|
|
3563
|
+
/**
|
|
3564
|
+
* Split text into sentences
|
|
3565
|
+
*
|
|
3566
|
+
* @public exported from `@promptbook/utils`
|
|
3567
|
+
*/
|
|
3568
|
+
function splitIntoSentences(text) {
|
|
3569
|
+
return text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
|
|
3570
|
+
}
|
|
3571
|
+
/**
|
|
3572
|
+
* Counts number of sentences in the text
|
|
3573
|
+
*
|
|
3574
|
+
* @public exported from `@promptbook/utils`
|
|
3575
|
+
*/
|
|
3576
|
+
function countSentences(text) {
|
|
3577
|
+
return splitIntoSentences(text).length;
|
|
3578
|
+
}
|
|
3579
|
+
|
|
3580
|
+
/**
|
|
3581
|
+
* Counts number of words in the text
|
|
3582
|
+
*
|
|
3583
|
+
* @public exported from `@promptbook/utils`
|
|
3584
|
+
*/
|
|
3585
|
+
function countWords(text) {
|
|
3586
|
+
text = text.replace(/[\p{Extended_Pictographic}]/gu, 'a');
|
|
3587
|
+
text = removeDiacritics(text);
|
|
3588
|
+
return text.split(/[^a-zа-я0-9]+/i).filter(function (word) { return word.length > 0; }).length;
|
|
3589
|
+
}
|
|
3590
|
+
|
|
3591
|
+
/**
|
|
3592
|
+
* Index of all counter functions
|
|
3593
|
+
*
|
|
3594
|
+
* @public exported from `@promptbook/utils`
|
|
3595
|
+
*/
|
|
3596
|
+
var CountUtils = {
|
|
3597
|
+
CHARACTERS: countCharacters,
|
|
3598
|
+
WORDS: countWords,
|
|
3599
|
+
SENTENCES: countSentences,
|
|
3600
|
+
PARAGRAPHS: countParagraphs,
|
|
3601
|
+
LINES: countLines,
|
|
3602
|
+
PAGES: countPages,
|
|
3603
|
+
};
|
|
3604
|
+
/**
|
|
3605
|
+
* TODO: [🧠][🤠] This should be probbably as part of `TextFormatDefinition`
|
|
3606
|
+
*/
|
|
3607
|
+
|
|
3608
|
+
/**
|
|
3609
|
+
* Function checkExpectations will check if the expectations on given value are met
|
|
3610
|
+
*
|
|
3611
|
+
* Note: There are two simmilar functions:
|
|
3612
|
+
* - `checkExpectations` which throws an error if the expectations are not met
|
|
3613
|
+
* - `isPassingExpectations` which returns a boolean
|
|
3614
|
+
*
|
|
3615
|
+
* @throws {ExpectError} if the expectations are not met
|
|
3616
|
+
* @returns {void} Nothing
|
|
3617
|
+
* @private internal function of `createPipelineExecutor`
|
|
3618
|
+
*/
|
|
3619
|
+
function checkExpectations(expectations, value) {
|
|
3620
|
+
var e_1, _a;
|
|
3621
|
+
try {
|
|
3622
|
+
for (var _b = __values(Object.entries(expectations)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
3623
|
+
var _d = __read(_c.value, 2), unit = _d[0], _e = _d[1], max = _e.max, min = _e.min;
|
|
3624
|
+
var amount = CountUtils[unit.toUpperCase()](value);
|
|
3625
|
+
if (min && amount < min) {
|
|
3626
|
+
throw new ExpectError("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
|
|
3627
|
+
} /* not else */
|
|
3628
|
+
if (max && amount > max) {
|
|
3629
|
+
throw new ExpectError("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
|
|
3630
|
+
}
|
|
3631
|
+
}
|
|
3632
|
+
}
|
|
3633
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
3634
|
+
finally {
|
|
3635
|
+
try {
|
|
3636
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
3637
|
+
}
|
|
3638
|
+
finally { if (e_1) throw e_1.error; }
|
|
3639
|
+
}
|
|
3640
|
+
}
|
|
3641
|
+
/**
|
|
3642
|
+
* Function checkExpectations will check if the expectations on given value are met
|
|
3643
|
+
*
|
|
3644
|
+
* Note: There are two simmilar functions:
|
|
3645
|
+
* - `checkExpectations` which throws an error if the expectations are not met
|
|
3646
|
+
* - `isPassingExpectations` which returns a boolean
|
|
3647
|
+
*
|
|
3648
|
+
* @returns {boolean} True if the expectations are met
|
|
3649
|
+
* @public exported from `@promptbook/core`
|
|
3650
|
+
*/
|
|
3651
|
+
function isPassingExpectations(expectations, value) {
|
|
3652
|
+
try {
|
|
3653
|
+
checkExpectations(expectations, value);
|
|
3654
|
+
return true;
|
|
3655
|
+
}
|
|
3656
|
+
catch (error) {
|
|
3657
|
+
if (!(error instanceof ExpectError)) {
|
|
3658
|
+
throw error;
|
|
3384
3659
|
}
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
3392
|
-
|
|
3393
|
-
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
|
|
3399
|
-
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
|
|
3407
|
-
|
|
3408
|
-
|
|
3409
|
-
|
|
3410
|
-
|
|
3411
|
-
|
|
3412
|
-
|
|
3413
|
-
|
|
3414
|
-
|
|
3415
|
-
|
|
3416
|
-
|
|
3417
|
-
|
|
3418
|
-
|
|
3419
|
-
|
|
3420
|
-
|
|
3421
|
-
|
|
3422
|
-
|
|
3423
|
-
|
|
3424
|
-
|
|
3425
|
-
|
|
3426
|
-
|
|
3427
|
-
|
|
3428
|
-
|
|
3429
|
-
|
|
3430
|
-
|
|
3431
|
-
|
|
3432
|
-
|
|
3433
|
-
|
|
3434
|
-
|
|
3435
|
-
|
|
3436
|
-
|
|
3437
|
-
|
|
3438
|
-
|
|
3439
|
-
|
|
3440
|
-
|
|
3441
|
-
|
|
3442
|
-
|
|
3443
|
-
|
|
3444
|
-
|
|
3445
|
-
|
|
3446
|
-
|
|
3447
|
-
|
|
3448
|
-
|
|
3449
|
-
|
|
3450
|
-
|
|
3451
|
-
|
|
3452
|
-
|
|
3453
|
-
|
|
3454
|
-
|
|
3455
|
-
|
|
3456
|
-
|
|
3457
|
-
|
|
3458
|
-
|
|
3459
|
-
|
|
3460
|
-
|
|
3461
|
-
|
|
3462
|
-
|
|
3463
|
-
|
|
3464
|
-
|
|
3465
|
-
|
|
3466
|
-
|
|
3467
|
-
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
|
|
3477
|
-
|
|
3478
|
-
|
|
3479
|
-
|
|
3480
|
-
|
|
3481
|
-
|
|
3482
|
-
|
|
3483
|
-
|
|
3484
|
-
|
|
3485
|
-
|
|
3486
|
-
|
|
3487
|
-
|
|
3488
|
-
|
|
3489
|
-
|
|
3490
|
-
|
|
3491
|
-
|
|
3492
|
-
|
|
3493
|
-
|
|
3494
|
-
|
|
3495
|
-
|
|
3496
|
-
|
|
3497
|
-
|
|
3498
|
-
|
|
3499
|
-
|
|
3500
|
-
|
|
3501
|
-
|
|
3502
|
-
|
|
3503
|
-
|
|
3504
|
-
|
|
3505
|
-
|
|
3506
|
-
|
|
3507
|
-
|
|
3508
|
-
|
|
3509
|
-
|
|
3510
|
-
|
|
3511
|
-
|
|
3512
|
-
|
|
3513
|
-
|
|
3514
|
-
|
|
3515
|
-
|
|
3516
|
-
|
|
3517
|
-
|
|
3518
|
-
|
|
3519
|
-
|
|
3520
|
-
|
|
3521
|
-
|
|
3522
|
-
|
|
3523
|
-
|
|
3524
|
-
|
|
3525
|
-
|
|
3526
|
-
|
|
3527
|
-
|
|
3528
|
-
|
|
3529
|
-
|
|
3530
|
-
|
|
3531
|
-
|
|
3532
|
-
|
|
3533
|
-
|
|
3534
|
-
|
|
3535
|
-
|
|
3536
|
-
|
|
3537
|
-
|
|
3538
|
-
|
|
3539
|
-
|
|
3540
|
-
|
|
3541
|
-
|
|
3542
|
-
|
|
3543
|
-
|
|
3544
|
-
|
|
3545
|
-
|
|
3546
|
-
|
|
3547
|
-
|
|
3548
|
-
|
|
3549
|
-
|
|
3550
|
-
|
|
3551
|
-
|
|
3552
|
-
|
|
3553
|
-
|
|
3554
|
-
|
|
3555
|
-
|
|
3556
|
-
|
|
3557
|
-
|
|
3558
|
-
|
|
3559
|
-
|
|
3560
|
-
|
|
3561
|
-
|
|
3562
|
-
|
|
3563
|
-
|
|
3564
|
-
|
|
3565
|
-
|
|
3566
|
-
|
|
3567
|
-
|
|
3568
|
-
|
|
3569
|
-
|
|
3570
|
-
|
|
3571
|
-
|
|
3572
|
-
|
|
3573
|
-
|
|
3574
|
-
|
|
3575
|
-
|
|
3576
|
-
|
|
3577
|
-
|
|
3578
|
-
|
|
3579
|
-
|
|
3580
|
-
|
|
3581
|
-
|
|
3582
|
-
|
|
3583
|
-
|
|
3584
|
-
|
|
3585
|
-
|
|
3586
|
-
|
|
3587
|
-
|
|
3588
|
-
|
|
3589
|
-
|
|
3590
|
-
|
|
3591
|
-
|
|
3592
|
-
|
|
3593
|
-
|
|
3594
|
-
|
|
3595
|
-
|
|
3596
|
-
|
|
3597
|
-
|
|
3598
|
-
|
|
3599
|
-
|
|
3600
|
-
|
|
3601
|
-
|
|
3602
|
-
|
|
3603
|
-
|
|
3604
|
-
|
|
3605
|
-
|
|
3606
|
-
|
|
3607
|
-
|
|
3608
|
-
|
|
3609
|
-
|
|
3610
|
-
|
|
3611
|
-
|
|
3612
|
-
|
|
3613
|
-
|
|
3614
|
-
|
|
3615
|
-
|
|
3616
|
-
|
|
3617
|
-
|
|
3618
|
-
|
|
3619
|
-
|
|
3620
|
-
|
|
3621
|
-
|
|
3622
|
-
|
|
3623
|
-
|
|
3624
|
-
|
|
3625
|
-
|
|
3626
|
-
|
|
3627
|
-
|
|
3628
|
-
|
|
3629
|
-
|
|
3630
|
-
|
|
3631
|
-
|
|
3632
|
-
|
|
3633
|
-
|
|
3634
|
-
|
|
3635
|
-
|
|
3636
|
-
|
|
3637
|
-
|
|
3638
|
-
|
|
3639
|
-
|
|
3640
|
-
|
|
3641
|
-
|
|
3642
|
-
|
|
3643
|
-
|
|
3644
|
-
|
|
3645
|
-
|
|
3646
|
-
|
|
3647
|
-
|
|
3648
|
-
|
|
3649
|
-
|
|
3650
|
-
|
|
3651
|
-
|
|
3652
|
-
|
|
3653
|
-
|
|
3654
|
-
|
|
3655
|
-
|
|
3656
|
-
|
|
3657
|
-
|
|
3658
|
-
|
|
3659
|
-
|
|
3660
|
-
|
|
3661
|
-
|
|
3662
|
-
|
|
3663
|
-
|
|
3664
|
-
|
|
3665
|
-
|
|
3666
|
-
|
|
3667
|
-
|
|
3668
|
-
|
|
3669
|
-
|
|
3670
|
-
|
|
3671
|
-
|
|
3672
|
-
|
|
3673
|
-
|
|
3674
|
-
|
|
3675
|
-
|
|
3676
|
-
|
|
3677
|
-
if (error_3 instanceof UnexpectedError) {
|
|
3678
|
-
throw error_3;
|
|
3679
|
-
}
|
|
3680
|
-
postprocessingError = error_3;
|
|
3681
|
-
scriptPipelineExecutionErrors.push(error_3);
|
|
3682
|
-
return [3 /*break*/, 34];
|
|
3683
|
-
case 34:
|
|
3684
|
-
_r = _q.next();
|
|
3685
|
-
return [3 /*break*/, 30];
|
|
3686
|
-
case 35: return [3 /*break*/, 38];
|
|
3687
|
-
case 36:
|
|
3688
|
-
e_6_1 = _v.sent();
|
|
3689
|
-
e_6 = { error: e_6_1 };
|
|
3690
|
-
return [3 /*break*/, 38];
|
|
3691
|
-
case 37:
|
|
3692
|
-
try {
|
|
3693
|
-
if (_r && !_r.done && (_u = _q.return)) _u.call(_q);
|
|
3694
|
-
}
|
|
3695
|
-
finally { if (e_6) throw e_6.error; }
|
|
3696
|
-
return [7 /*endfinally*/];
|
|
3697
|
-
case 38:
|
|
3698
|
-
if (postprocessingError) {
|
|
3699
|
-
throw postprocessingError;
|
|
3700
|
-
}
|
|
3701
|
-
_v.label = 39;
|
|
3702
|
-
case 39:
|
|
3703
|
-
_p = _o.next();
|
|
3704
|
-
return [3 /*break*/, 28];
|
|
3705
|
-
case 40: return [3 /*break*/, 43];
|
|
3706
|
-
case 41:
|
|
3707
|
-
e_7_1 = _v.sent();
|
|
3708
|
-
e_7 = { error: e_7_1 };
|
|
3709
|
-
return [3 /*break*/, 43];
|
|
3710
|
-
case 42:
|
|
3711
|
-
try {
|
|
3712
|
-
if (_p && !_p.done && (_t = _o.return)) _t.call(_o);
|
|
3713
|
-
}
|
|
3714
|
-
finally { if (e_7) throw e_7.error; }
|
|
3715
|
-
return [7 /*endfinally*/];
|
|
3716
|
-
case 43:
|
|
3717
|
-
// TODO: [💝] Unite object for expecting amount and format
|
|
3718
|
-
if (currentTemplate.format) {
|
|
3719
|
-
if (currentTemplate.format === 'JSON') {
|
|
3720
|
-
if (!isValidJsonString(resultString || '')) {
|
|
3721
|
-
// TODO: [🏢] Do more universally via `FormatDefinition`
|
|
3722
|
-
try {
|
|
3723
|
-
resultString = extractJsonBlock(resultString || '');
|
|
3724
|
-
}
|
|
3725
|
-
catch (error) {
|
|
3726
|
-
keepUnused(error);
|
|
3727
|
-
throw new ExpectError(spaceTrim.spaceTrim(function (block) { return "\n Expected valid JSON string\n\n ".concat(block(
|
|
3728
|
-
/*<- Note: No need for `pipelineIdentification`, it will be catched and added later */ ''), "\n "); }));
|
|
3729
|
-
}
|
|
3730
|
-
}
|
|
3660
|
+
return false;
|
|
3661
|
+
}
|
|
3662
|
+
}
|
|
3663
|
+
/**
|
|
3664
|
+
* TODO: [💝] Unite object for expecting amount and format
|
|
3665
|
+
* TODO: [🧠][🤠] This should be part of `TextFormatDefinition`
|
|
3666
|
+
* Note: [💝] and [🤠] are interconnected together
|
|
3667
|
+
*/
|
|
3668
|
+
|
|
3669
|
+
/**
|
|
3670
|
+
* @@@
|
|
3671
|
+
*
|
|
3672
|
+
* @private internal utility of `createPipelineExecutor`
|
|
3673
|
+
*/
|
|
3674
|
+
function executeAttempts(options) {
|
|
3675
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
3676
|
+
var jokerParameterNames, priority, maxAttempts, preparedContent, parameters, template, preparedPipeline, tools, llmTools, settings, $executionReport, pipelineIdentification, maxExecutionAttempts, $ongoingTemplateResult, _loop_1, attempt, state_1;
|
|
3677
|
+
return __generator(this, function (_a) {
|
|
3678
|
+
switch (_a.label) {
|
|
3679
|
+
case 0:
|
|
3680
|
+
jokerParameterNames = options.jokerParameterNames, priority = options.priority, maxAttempts = options.maxAttempts, preparedContent = options.preparedContent, parameters = options.parameters, template = options.template, preparedPipeline = options.preparedPipeline, tools = options.tools, llmTools = options.llmTools, settings = options.settings, $executionReport = options.$executionReport, pipelineIdentification = options.pipelineIdentification;
|
|
3681
|
+
maxExecutionAttempts = settings.maxExecutionAttempts;
|
|
3682
|
+
$ongoingTemplateResult = {
|
|
3683
|
+
$result: null,
|
|
3684
|
+
$resultString: null,
|
|
3685
|
+
$expectError: null,
|
|
3686
|
+
$scriptPipelineExecutionErrors: [],
|
|
3687
|
+
};
|
|
3688
|
+
_loop_1 = function (attempt) {
|
|
3689
|
+
var isJokerAttempt, jokerParameterName, _b, modelRequirements, _c, _d, _e, _f, _g, _h, scriptTools, _j, error_1, e_1_1, _k, _l, _m, functionName, postprocessingError, _o, _p, scriptTools, _q, error_2, e_2_1, e_3_1, error_3;
|
|
3690
|
+
var e_1, _r, e_3, _s, e_2, _t;
|
|
3691
|
+
return __generator(this, function (_u) {
|
|
3692
|
+
switch (_u.label) {
|
|
3693
|
+
case 0:
|
|
3694
|
+
isJokerAttempt = attempt < 0;
|
|
3695
|
+
jokerParameterName = jokerParameterNames[jokerParameterNames.length + attempt];
|
|
3696
|
+
// TODO: [🧠] !!!!!! JOKERS, EXPECTATIONS, POSTPROCESSING and FOREACH
|
|
3697
|
+
if (isJokerAttempt && !jokerParameterName) {
|
|
3698
|
+
throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Joker not found in attempt ".concat(attempt, "\n\n ").concat(block(pipelineIdentification), "\n "); }));
|
|
3699
|
+
}
|
|
3700
|
+
$ongoingTemplateResult.$result = null;
|
|
3701
|
+
$ongoingTemplateResult.$resultString = null;
|
|
3702
|
+
$ongoingTemplateResult.$expectError = null;
|
|
3703
|
+
if (isJokerAttempt) {
|
|
3704
|
+
if (parameters[jokerParameterName] === undefined) {
|
|
3705
|
+
throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Joker parameter {".concat(jokerParameterName, "} not defined\n\n ").concat(block(pipelineIdentification), "\n "); }));
|
|
3706
|
+
// <- TODO: This is maybe `PipelineLogicError` which should be detected in `validatePipeline` and here just thrown as `UnexpectedError`
|
|
3707
|
+
}
|
|
3708
|
+
else {
|
|
3709
|
+
$ongoingTemplateResult.$resultString = parameters[jokerParameterName];
|
|
3710
|
+
}
|
|
3711
|
+
}
|
|
3712
|
+
_u.label = 1;
|
|
3713
|
+
case 1:
|
|
3714
|
+
_u.trys.push([1, 44, 45, 46]);
|
|
3715
|
+
if (!!isJokerAttempt) return [3 /*break*/, 26];
|
|
3716
|
+
_b = template.templateType;
|
|
3717
|
+
switch (_b) {
|
|
3718
|
+
case 'SIMPLE_TEMPLATE': return [3 /*break*/, 2];
|
|
3719
|
+
case 'PROMPT_TEMPLATE': return [3 /*break*/, 3];
|
|
3720
|
+
case 'SCRIPT_TEMPLATE': return [3 /*break*/, 12];
|
|
3721
|
+
case 'DIALOG_TEMPLATE': return [3 /*break*/, 23];
|
|
3722
|
+
}
|
|
3723
|
+
return [3 /*break*/, 25];
|
|
3724
|
+
case 2:
|
|
3725
|
+
$ongoingTemplateResult.$resultString = replaceParameters(preparedContent, parameters);
|
|
3726
|
+
return [3 /*break*/, 26];
|
|
3727
|
+
case 3:
|
|
3728
|
+
modelRequirements = __assign(__assign({ modelVariant: 'CHAT' }, (preparedPipeline.defaultModelRequirements || {})), (template.modelRequirements || {}));
|
|
3729
|
+
$ongoingTemplateResult.$prompt = {
|
|
3730
|
+
title: template.title,
|
|
3731
|
+
pipelineUrl: "".concat(preparedPipeline.pipelineUrl
|
|
3732
|
+
? preparedPipeline.pipelineUrl
|
|
3733
|
+
: 'anonymous' /* <- TODO: [🧠] How to deal with anonymous pipelines, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(template.name),
|
|
3734
|
+
parameters: parameters,
|
|
3735
|
+
content: preparedContent,
|
|
3736
|
+
modelRequirements: modelRequirements,
|
|
3737
|
+
expectations: __assign(__assign({}, (preparedPipeline.personas.find(function (_a) {
|
|
3738
|
+
var name = _a.name;
|
|
3739
|
+
return name === template.personaName;
|
|
3740
|
+
}) ||
|
|
3741
|
+
{})), template.expectations),
|
|
3742
|
+
format: template.format,
|
|
3743
|
+
postprocessingFunctionNames: template.postprocessingFunctionNames,
|
|
3744
|
+
}; // <- TODO: Not very good type guard
|
|
3745
|
+
_c = modelRequirements.modelVariant;
|
|
3746
|
+
switch (_c) {
|
|
3747
|
+
case 'CHAT': return [3 /*break*/, 4];
|
|
3748
|
+
case 'COMPLETION': return [3 /*break*/, 6];
|
|
3749
|
+
case 'EMBEDDING': return [3 /*break*/, 8];
|
|
3750
|
+
}
|
|
3751
|
+
return [3 /*break*/, 10];
|
|
3752
|
+
case 4:
|
|
3753
|
+
_d = $ongoingTemplateResult;
|
|
3754
|
+
return [4 /*yield*/, llmTools.callChatModel($deepFreeze($ongoingTemplateResult.$prompt))];
|
|
3755
|
+
case 5:
|
|
3756
|
+
_d.$chatResult = _u.sent();
|
|
3757
|
+
// TODO: [🍬] Destroy chatThread
|
|
3758
|
+
$ongoingTemplateResult.$result = $ongoingTemplateResult.$chatResult;
|
|
3759
|
+
$ongoingTemplateResult.$resultString = $ongoingTemplateResult.$chatResult.content;
|
|
3760
|
+
return [3 /*break*/, 11];
|
|
3761
|
+
case 6:
|
|
3762
|
+
_e = $ongoingTemplateResult;
|
|
3763
|
+
return [4 /*yield*/, llmTools.callCompletionModel($deepFreeze($ongoingTemplateResult.$prompt))];
|
|
3764
|
+
case 7:
|
|
3765
|
+
_e.$completionResult = _u.sent();
|
|
3766
|
+
$ongoingTemplateResult.$result = $ongoingTemplateResult.$completionResult;
|
|
3767
|
+
$ongoingTemplateResult.$resultString =
|
|
3768
|
+
$ongoingTemplateResult.$completionResult.content;
|
|
3769
|
+
return [3 /*break*/, 11];
|
|
3770
|
+
case 8:
|
|
3771
|
+
// TODO: [🧠] This is weird, embedding model can not be used such a way in the pipeline
|
|
3772
|
+
_f = $ongoingTemplateResult;
|
|
3773
|
+
return [4 /*yield*/, llmTools.callEmbeddingModel($deepFreeze($ongoingTemplateResult.$prompt))];
|
|
3774
|
+
case 9:
|
|
3775
|
+
// TODO: [🧠] This is weird, embedding model can not be used such a way in the pipeline
|
|
3776
|
+
_f.$embeddingResult = _u.sent();
|
|
3777
|
+
$ongoingTemplateResult.$result = $ongoingTemplateResult.$embeddingResult;
|
|
3778
|
+
$ongoingTemplateResult.$resultString =
|
|
3779
|
+
$ongoingTemplateResult.$embeddingResult.content.join(',');
|
|
3780
|
+
return [3 /*break*/, 11];
|
|
3781
|
+
case 10: throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Unknown model variant \"".concat(template.modelRequirements.modelVariant, "\"\n\n ").concat(block(pipelineIdentification), "\n\n "); }));
|
|
3782
|
+
case 11: return [3 /*break*/, 26];
|
|
3783
|
+
case 12:
|
|
3784
|
+
if (arrayableToArray(tools.script).length === 0) {
|
|
3785
|
+
throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n No script execution tools are available\n\n ".concat(block(pipelineIdentification), "\n "); }));
|
|
3786
|
+
}
|
|
3787
|
+
if (!template.contentLanguage) {
|
|
3788
|
+
throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Script language is not defined for SCRIPT TEMPLATE \"".concat(template.name, "\"\n\n ").concat(block(pipelineIdentification), "\n "); }));
|
|
3789
|
+
}
|
|
3790
|
+
_u.label = 13;
|
|
3791
|
+
case 13:
|
|
3792
|
+
_u.trys.push([13, 20, 21, 22]);
|
|
3793
|
+
_g = (e_1 = void 0, __values(arrayableToArray(tools.script))), _h = _g.next();
|
|
3794
|
+
_u.label = 14;
|
|
3795
|
+
case 14:
|
|
3796
|
+
if (!!_h.done) return [3 /*break*/, 19];
|
|
3797
|
+
scriptTools = _h.value;
|
|
3798
|
+
_u.label = 15;
|
|
3799
|
+
case 15:
|
|
3800
|
+
_u.trys.push([15, 17, , 18]);
|
|
3801
|
+
_j = $ongoingTemplateResult;
|
|
3802
|
+
return [4 /*yield*/, scriptTools.execute($deepFreeze({
|
|
3803
|
+
scriptLanguage: template.contentLanguage,
|
|
3804
|
+
script: preparedContent,
|
|
3805
|
+
parameters: parameters,
|
|
3806
|
+
}))];
|
|
3807
|
+
case 16:
|
|
3808
|
+
_j.$resultString = _u.sent();
|
|
3809
|
+
return [3 /*break*/, 19];
|
|
3810
|
+
case 17:
|
|
3811
|
+
error_1 = _u.sent();
|
|
3812
|
+
if (!(error_1 instanceof Error)) {
|
|
3813
|
+
throw error_1;
|
|
3814
|
+
}
|
|
3815
|
+
if (error_1 instanceof UnexpectedError) {
|
|
3816
|
+
throw error_1;
|
|
3817
|
+
}
|
|
3818
|
+
$ongoingTemplateResult.$scriptPipelineExecutionErrors.push(error_1);
|
|
3819
|
+
return [3 /*break*/, 18];
|
|
3820
|
+
case 18:
|
|
3821
|
+
_h = _g.next();
|
|
3822
|
+
return [3 /*break*/, 14];
|
|
3823
|
+
case 19: return [3 /*break*/, 22];
|
|
3824
|
+
case 20:
|
|
3825
|
+
e_1_1 = _u.sent();
|
|
3826
|
+
e_1 = { error: e_1_1 };
|
|
3827
|
+
return [3 /*break*/, 22];
|
|
3828
|
+
case 21:
|
|
3829
|
+
try {
|
|
3830
|
+
if (_h && !_h.done && (_r = _g.return)) _r.call(_g);
|
|
3831
|
+
}
|
|
3832
|
+
finally { if (e_1) throw e_1.error; }
|
|
3833
|
+
return [7 /*endfinally*/];
|
|
3834
|
+
case 22:
|
|
3835
|
+
if ($ongoingTemplateResult.$resultString !== null) {
|
|
3836
|
+
return [3 /*break*/, 26];
|
|
3837
|
+
}
|
|
3838
|
+
if ($ongoingTemplateResult.$scriptPipelineExecutionErrors.length === 1) {
|
|
3839
|
+
throw $ongoingTemplateResult.$scriptPipelineExecutionErrors[0];
|
|
3840
|
+
}
|
|
3841
|
+
else {
|
|
3842
|
+
throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Script execution failed ".concat($ongoingTemplateResult.$scriptPipelineExecutionErrors.length, "x\n\n ").concat(block(pipelineIdentification), "\n\n ").concat(block($ongoingTemplateResult.$scriptPipelineExecutionErrors
|
|
3843
|
+
.map(function (error) { return '- ' + error.message; })
|
|
3844
|
+
.join('\n\n')), "\n "); }));
|
|
3845
|
+
}
|
|
3846
|
+
case 23:
|
|
3847
|
+
if (tools.userInterface === undefined) {
|
|
3848
|
+
throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n User interface tools are not available\n\n ".concat(block(pipelineIdentification), "\n "); }));
|
|
3849
|
+
}
|
|
3850
|
+
// TODO: [🌹] When making next attempt for `DIALOG TEMPLATE`, preserve the previous user input
|
|
3851
|
+
_k = $ongoingTemplateResult;
|
|
3852
|
+
return [4 /*yield*/, tools.userInterface.promptDialog($deepFreeze({
|
|
3853
|
+
promptTitle: template.title,
|
|
3854
|
+
promptMessage: replaceParameters(template.description || '', parameters),
|
|
3855
|
+
defaultValue: replaceParameters(preparedContent, parameters),
|
|
3856
|
+
// TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
|
|
3857
|
+
placeholder: undefined,
|
|
3858
|
+
priority: priority,
|
|
3859
|
+
}))];
|
|
3860
|
+
case 24:
|
|
3861
|
+
// TODO: [🌹] When making next attempt for `DIALOG TEMPLATE`, preserve the previous user input
|
|
3862
|
+
_k.$resultString = _u.sent();
|
|
3863
|
+
return [3 /*break*/, 26];
|
|
3864
|
+
case 25: throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Unknown execution type \"".concat(template.templateType, "\"\n\n ").concat(block(pipelineIdentification), "\n "); }));
|
|
3865
|
+
case 26:
|
|
3866
|
+
if (!(!isJokerAttempt && template.postprocessingFunctionNames)) return [3 /*break*/, 43];
|
|
3867
|
+
_u.label = 27;
|
|
3868
|
+
case 27:
|
|
3869
|
+
_u.trys.push([27, 41, 42, 43]);
|
|
3870
|
+
_l = (e_3 = void 0, __values(template.postprocessingFunctionNames)), _m = _l.next();
|
|
3871
|
+
_u.label = 28;
|
|
3872
|
+
case 28:
|
|
3873
|
+
if (!!_m.done) return [3 /*break*/, 40];
|
|
3874
|
+
functionName = _m.value;
|
|
3875
|
+
postprocessingError = null;
|
|
3876
|
+
_u.label = 29;
|
|
3877
|
+
case 29:
|
|
3878
|
+
_u.trys.push([29, 36, 37, 38]);
|
|
3879
|
+
_o = (e_2 = void 0, __values(arrayableToArray(tools.script))), _p = _o.next();
|
|
3880
|
+
_u.label = 30;
|
|
3881
|
+
case 30:
|
|
3882
|
+
if (!!_p.done) return [3 /*break*/, 35];
|
|
3883
|
+
scriptTools = _p.value;
|
|
3884
|
+
_u.label = 31;
|
|
3885
|
+
case 31:
|
|
3886
|
+
_u.trys.push([31, 33, , 34]);
|
|
3887
|
+
_q = $ongoingTemplateResult;
|
|
3888
|
+
return [4 /*yield*/, scriptTools.execute({
|
|
3889
|
+
scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
|
|
3890
|
+
script: "".concat(functionName, "(resultString)"),
|
|
3891
|
+
parameters: {
|
|
3892
|
+
resultString: $ongoingTemplateResult.$resultString || '',
|
|
3893
|
+
// Note: No ...parametersForTemplate, because working with result only
|
|
3894
|
+
},
|
|
3895
|
+
})];
|
|
3896
|
+
case 32:
|
|
3897
|
+
_q.$resultString = _u.sent();
|
|
3898
|
+
postprocessingError = null;
|
|
3899
|
+
return [3 /*break*/, 35];
|
|
3900
|
+
case 33:
|
|
3901
|
+
error_2 = _u.sent();
|
|
3902
|
+
if (!(error_2 instanceof Error)) {
|
|
3903
|
+
throw error_2;
|
|
3904
|
+
}
|
|
3905
|
+
if (error_2 instanceof UnexpectedError) {
|
|
3906
|
+
throw error_2;
|
|
3907
|
+
}
|
|
3908
|
+
postprocessingError = error_2;
|
|
3909
|
+
$ongoingTemplateResult.$scriptPipelineExecutionErrors.push(error_2);
|
|
3910
|
+
return [3 /*break*/, 34];
|
|
3911
|
+
case 34:
|
|
3912
|
+
_p = _o.next();
|
|
3913
|
+
return [3 /*break*/, 30];
|
|
3914
|
+
case 35: return [3 /*break*/, 38];
|
|
3915
|
+
case 36:
|
|
3916
|
+
e_2_1 = _u.sent();
|
|
3917
|
+
e_2 = { error: e_2_1 };
|
|
3918
|
+
return [3 /*break*/, 38];
|
|
3919
|
+
case 37:
|
|
3920
|
+
try {
|
|
3921
|
+
if (_p && !_p.done && (_t = _o.return)) _t.call(_o);
|
|
3922
|
+
}
|
|
3923
|
+
finally { if (e_2) throw e_2.error; }
|
|
3924
|
+
return [7 /*endfinally*/];
|
|
3925
|
+
case 38:
|
|
3926
|
+
if (postprocessingError) {
|
|
3927
|
+
throw postprocessingError;
|
|
3928
|
+
}
|
|
3929
|
+
_u.label = 39;
|
|
3930
|
+
case 39:
|
|
3931
|
+
_m = _l.next();
|
|
3932
|
+
return [3 /*break*/, 28];
|
|
3933
|
+
case 40: return [3 /*break*/, 43];
|
|
3934
|
+
case 41:
|
|
3935
|
+
e_3_1 = _u.sent();
|
|
3936
|
+
e_3 = { error: e_3_1 };
|
|
3937
|
+
return [3 /*break*/, 43];
|
|
3938
|
+
case 42:
|
|
3939
|
+
try {
|
|
3940
|
+
if (_m && !_m.done && (_s = _l.return)) _s.call(_l);
|
|
3941
|
+
}
|
|
3942
|
+
finally { if (e_3) throw e_3.error; }
|
|
3943
|
+
return [7 /*endfinally*/];
|
|
3944
|
+
case 43:
|
|
3945
|
+
// TODO: [💝] Unite object for expecting amount and format
|
|
3946
|
+
if (template.format) {
|
|
3947
|
+
if (template.format === 'JSON') {
|
|
3948
|
+
if (!isValidJsonString($ongoingTemplateResult.$resultString || '')) {
|
|
3949
|
+
// TODO: [🏢] Do more universally via `FormatDefinition`
|
|
3950
|
+
try {
|
|
3951
|
+
$ongoingTemplateResult.$resultString = extractJsonBlock($ongoingTemplateResult.$resultString || '');
|
|
3731
3952
|
}
|
|
3732
|
-
|
|
3733
|
-
|
|
3953
|
+
catch (error) {
|
|
3954
|
+
keepUnused(error);
|
|
3955
|
+
throw new ExpectError(spaceTrim.spaceTrim(function (block) { return "\n Expected valid JSON string\n\n ".concat(block(
|
|
3956
|
+
/*<- Note: No need for `pipelineIdentification`, it will be catched and added later */ ''), "\n "); }));
|
|
3734
3957
|
}
|
|
3735
3958
|
}
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
|
|
3740
|
-
|
|
3741
|
-
|
|
3742
|
-
|
|
3743
|
-
|
|
3744
|
-
|
|
3745
|
-
|
|
3746
|
-
|
|
3747
|
-
|
|
3748
|
-
|
|
3749
|
-
|
|
3750
|
-
|
|
3751
|
-
|
|
3752
|
-
|
|
3753
|
-
|
|
3754
|
-
|
|
3755
|
-
|
|
3756
|
-
|
|
3757
|
-
|
|
3758
|
-
|
|
3759
|
-
|
|
3760
|
-
|
|
3761
|
-
|
|
3762
|
-
|
|
3763
|
-
|
|
3764
|
-
|
|
3765
|
-
|
|
3766
|
-
|
|
3767
|
-
|
|
3768
|
-
|
|
3959
|
+
}
|
|
3960
|
+
else {
|
|
3961
|
+
throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Unknown format \"".concat(template.format, "\"\n\n ").concat(block(pipelineIdentification), "\n "); }));
|
|
3962
|
+
}
|
|
3963
|
+
}
|
|
3964
|
+
// TODO: [💝] Unite object for expecting amount and format
|
|
3965
|
+
if (template.expectations) {
|
|
3966
|
+
checkExpectations(template.expectations, $ongoingTemplateResult.$resultString || '');
|
|
3967
|
+
}
|
|
3968
|
+
return [2 /*return*/, "break-attempts"];
|
|
3969
|
+
case 44:
|
|
3970
|
+
error_3 = _u.sent();
|
|
3971
|
+
if (!(error_3 instanceof ExpectError)) {
|
|
3972
|
+
throw error_3;
|
|
3973
|
+
}
|
|
3974
|
+
$ongoingTemplateResult.$expectError = error_3;
|
|
3975
|
+
return [3 /*break*/, 46];
|
|
3976
|
+
case 45:
|
|
3977
|
+
if (!isJokerAttempt &&
|
|
3978
|
+
template.templateType === 'PROMPT_TEMPLATE' &&
|
|
3979
|
+
$ongoingTemplateResult.$prompt
|
|
3980
|
+
// <- Note: [2] When some expected parameter is not defined, error will occur in replaceParameters
|
|
3981
|
+
// In that case we don’t want to make a report about it because it’s not a llm execution error
|
|
3982
|
+
) {
|
|
3983
|
+
// TODO: [🧠] Maybe put other templateTypes into report
|
|
3984
|
+
$executionReport.promptExecutions.push({
|
|
3985
|
+
prompt: __assign({}, $ongoingTemplateResult.$prompt),
|
|
3986
|
+
result: $ongoingTemplateResult.$result || undefined,
|
|
3987
|
+
error: $ongoingTemplateResult.$expectError === null
|
|
3988
|
+
? undefined
|
|
3989
|
+
: serializeError($ongoingTemplateResult.$expectError),
|
|
3990
|
+
});
|
|
3991
|
+
}
|
|
3992
|
+
return [7 /*endfinally*/];
|
|
3993
|
+
case 46:
|
|
3994
|
+
if ($ongoingTemplateResult.$expectError !== null && attempt === maxAttempts - 1) {
|
|
3995
|
+
throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) {
|
|
3996
|
+
var _a, _b, _c;
|
|
3997
|
+
return "\n LLM execution failed ".concat(maxExecutionAttempts, "x\n\n ").concat(block(pipelineIdentification), "\n\n ---\n The Prompt:\n ").concat(block((((_a = $ongoingTemplateResult.$prompt) === null || _a === void 0 ? void 0 : _a.content) || '')
|
|
3998
|
+
.split('\n')
|
|
3999
|
+
.map(function (line) { return "> ".concat(line); })
|
|
4000
|
+
.join('\n')), "\n\n Last error ").concat(((_b = $ongoingTemplateResult.$expectError) === null || _b === void 0 ? void 0 : _b.name) || '', ":\n ").concat(block((((_c = $ongoingTemplateResult.$expectError) === null || _c === void 0 ? void 0 : _c.message) || '')
|
|
4001
|
+
.split('\n')
|
|
4002
|
+
.map(function (line) { return "> ".concat(line); })
|
|
4003
|
+
.join('\n')), "\n\n Last result:\n ").concat(block($ongoingTemplateResult.$resultString === null
|
|
4004
|
+
? 'null'
|
|
4005
|
+
: $ongoingTemplateResult.$resultString
|
|
3769
4006
|
.split('\n')
|
|
3770
4007
|
.map(function (line) { return "> ".concat(line); })
|
|
3771
|
-
.join('\n')), "\n
|
|
3772
|
-
|
|
3773
|
-
: resultString
|
|
3774
|
-
.split('\n')
|
|
3775
|
-
.map(function (line) { return "> ".concat(line); })
|
|
3776
|
-
.join('\n')), "\n ---\n "); }));
|
|
3777
|
-
}
|
|
3778
|
-
return [2 /*return*/];
|
|
4008
|
+
.join('\n')), "\n ---\n ");
|
|
4009
|
+
}));
|
|
3779
4010
|
}
|
|
3780
|
-
|
|
3781
|
-
};
|
|
3782
|
-
attempt = -jokerParameterNames.length;
|
|
3783
|
-
_h.label = 4;
|
|
3784
|
-
case 4:
|
|
3785
|
-
if (!(attempt < maxAttempts)) return [3 /*break*/, 7];
|
|
3786
|
-
return [5 /*yield**/, _loop_5(attempt)];
|
|
3787
|
-
case 5:
|
|
3788
|
-
state_2 = _h.sent();
|
|
3789
|
-
switch (state_2) {
|
|
3790
|
-
case "break-attempts": return [3 /*break*/, 7];
|
|
3791
|
-
}
|
|
3792
|
-
_h.label = 6;
|
|
3793
|
-
case 6:
|
|
3794
|
-
attempt++;
|
|
3795
|
-
return [3 /*break*/, 4];
|
|
3796
|
-
case 7:
|
|
3797
|
-
if (resultString === null) {
|
|
3798
|
-
throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Something went wrong and prompt result is null\n\n ".concat(block(pipelineIdentification), "\n "); }));
|
|
4011
|
+
return [2 /*return*/];
|
|
3799
4012
|
}
|
|
3800
|
-
|
|
3801
|
-
|
|
3802
|
-
|
|
3803
|
-
|
|
3804
|
-
|
|
3805
|
-
|
|
3806
|
-
|
|
3807
|
-
|
|
3808
|
-
|
|
3809
|
-
|
|
3810
|
-
|
|
3811
|
-
if (isReturned) {
|
|
3812
|
-
throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Can not call `onProgress` after pipeline execution is finished \uD83C\uDF4E\n\n ".concat(block(pipelineIdentification), "\n\n ").concat(block(JSON.stringify(progress_2, null, 4)
|
|
3813
|
-
.split('\n')
|
|
3814
|
-
.map(function (line) { return "> ".concat(line); })
|
|
3815
|
-
.join('\n')), "\n\n "); }));
|
|
3816
|
-
}
|
|
3817
|
-
return [4 /*yield*/, onProgress(progress_2)];
|
|
3818
|
-
case 8:
|
|
3819
|
-
_h.sent();
|
|
3820
|
-
_h.label = 9;
|
|
3821
|
-
case 9:
|
|
3822
|
-
parametersToPass = Object.freeze(__assign(__assign({}, parametersToPass), (_g = {}, _g[currentTemplate.resultingParameterName] = resultString /* <- Note: Not need to detect parameter collision here because pipeline checks logic consistency during construction */, _g)));
|
|
3823
|
-
return [2 /*return*/];
|
|
4013
|
+
});
|
|
4014
|
+
};
|
|
4015
|
+
attempt = -jokerParameterNames.length;
|
|
4016
|
+
_a.label = 1;
|
|
4017
|
+
case 1:
|
|
4018
|
+
if (!(attempt < maxAttempts)) return [3 /*break*/, 4];
|
|
4019
|
+
return [5 /*yield**/, _loop_1(attempt)];
|
|
4020
|
+
case 2:
|
|
4021
|
+
state_1 = _a.sent();
|
|
4022
|
+
switch (state_1) {
|
|
4023
|
+
case "break-attempts": return [3 /*break*/, 4];
|
|
3824
4024
|
}
|
|
3825
|
-
|
|
3826
|
-
|
|
4025
|
+
_a.label = 3;
|
|
4026
|
+
case 3:
|
|
4027
|
+
attempt++;
|
|
4028
|
+
return [3 /*break*/, 1];
|
|
4029
|
+
case 4:
|
|
4030
|
+
if ($ongoingTemplateResult.$resultString === null) {
|
|
4031
|
+
throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Something went wrong and prompt result is null\n\n ".concat(block(pipelineIdentification), "\n "); }));
|
|
4032
|
+
}
|
|
4033
|
+
return [2 /*return*/, $ongoingTemplateResult.$resultString];
|
|
4034
|
+
}
|
|
4035
|
+
});
|
|
4036
|
+
});
|
|
4037
|
+
}
|
|
4038
|
+
/**
|
|
4039
|
+
* TODO: Break into smaller functions
|
|
4040
|
+
*/
|
|
4041
|
+
|
|
4042
|
+
/**
|
|
4043
|
+
* @@@
|
|
4044
|
+
*
|
|
4045
|
+
* @private internal utility of `createPipelineExecutor`
|
|
4046
|
+
*/
|
|
4047
|
+
function executeFormatCells(options) {
|
|
4048
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
4049
|
+
var template, jokerParameterNames, parameters, priority, pipelineIdentification, parameterValue, formatDefinition, subvalueDefinition, resultString;
|
|
4050
|
+
var _this = this;
|
|
4051
|
+
return __generator(this, function (_a) {
|
|
4052
|
+
switch (_a.label) {
|
|
4053
|
+
case 0:
|
|
4054
|
+
template = options.template, jokerParameterNames = options.jokerParameterNames, parameters = options.parameters, priority = options.priority, pipelineIdentification = options.pipelineIdentification;
|
|
4055
|
+
if (template.foreach === undefined) {
|
|
4056
|
+
return [2 /*return*/, /* not await */ executeAttempts(options)];
|
|
4057
|
+
}
|
|
4058
|
+
if (jokerParameterNames.length !== 0) {
|
|
4059
|
+
throw new UnexpectedError(spaceTrim__default["default"](function (block) { return "\n JOKER parameters are not supported together with FOREACH command\n\n [\uD83E\uDDDE\u200D\u2640\uFE0F] This should be prevented in `validatePipeline`\n\n ".concat(block(pipelineIdentification), "\n "); }));
|
|
4060
|
+
}
|
|
4061
|
+
parameterValue = parameters[template.foreach.parameterName] || '';
|
|
4062
|
+
formatDefinition = FORMAT_DEFINITIONS.find(function (formatDefinition) {
|
|
4063
|
+
return __spreadArray([formatDefinition.formatName], __read((formatDefinition.aliases || [])), false).includes(template.foreach.formatName);
|
|
4064
|
+
});
|
|
4065
|
+
if (formatDefinition === undefined) {
|
|
4066
|
+
throw new UnexpectedError(
|
|
4067
|
+
// <- TODO: [🧠][🧐] Should be formats fixed per promptbook version or behave as plugins (=> change UnexpectedError)
|
|
4068
|
+
spaceTrim__default["default"](function (block) { return "\n Unsupported format \"".concat(template.foreach.formatName, "\"\n\n Available formats:\n ").concat(block(FORMAT_DEFINITIONS.map(function (formatDefinition) { return formatDefinition.formatName; })
|
|
4069
|
+
.map(function (formatName) { return "- ".concat(formatName); })
|
|
4070
|
+
.join('\n')), "\n\n [\u26F7] This should never happen because format name should be validated during parsing\n\n ").concat(block(pipelineIdentification), "\n "); }));
|
|
4071
|
+
}
|
|
4072
|
+
subvalueDefinition = formatDefinition.subvalueDefinitions.find(function (subvalueDefinition) {
|
|
4073
|
+
return __spreadArray([subvalueDefinition.subvalueName], __read((subvalueDefinition.aliases || [])), false).includes(template.foreach.cellName);
|
|
4074
|
+
});
|
|
4075
|
+
if (subvalueDefinition === undefined) {
|
|
4076
|
+
throw new UnexpectedError(
|
|
4077
|
+
// <- TODO: [🧠][🧐] Should be formats fixed per promptbook version or behave as plugins (=> change UnexpectedError)
|
|
4078
|
+
spaceTrim__default["default"](function (block) { return "\n Unsupported cell name \"".concat(template.foreach.cellName, "\" for format \"").concat(template.foreach.formatName, "\"\n\n Available cell names for format \"").concat(formatDefinition.formatName, "\":\n ").concat(block(formatDefinition.subvalueDefinitions
|
|
4079
|
+
.map(function (subvalueDefinition) { return subvalueDefinition.subvalueName; })
|
|
4080
|
+
.map(function (subvalueName) { return "- ".concat(subvalueName); })
|
|
4081
|
+
.join('\n')), "\n\n [\u26F7] This should never happen because cell name should be validated during parsing\n\n ").concat(block(pipelineIdentification), "\n "); }));
|
|
4082
|
+
}
|
|
4083
|
+
return [4 /*yield*/, subvalueDefinition.mapValues(parameterValue, function (subparameters, index) { return __awaiter(_this, void 0, void 0, function () {
|
|
4084
|
+
var mappedParameters, allSubparameters, subresultString;
|
|
4085
|
+
return __generator(this, function (_a) {
|
|
4086
|
+
switch (_a.label) {
|
|
4087
|
+
case 0:
|
|
4088
|
+
try {
|
|
4089
|
+
mappedParameters = mapAvailableToExpectedParameters({
|
|
4090
|
+
expectedParameters: Object.fromEntries(template.foreach.subparameterNames.map(function (subparameterName) { return [subparameterName, null]; })),
|
|
4091
|
+
availableParameters: subparameters,
|
|
4092
|
+
});
|
|
4093
|
+
}
|
|
4094
|
+
catch (error) {
|
|
4095
|
+
if (!(error instanceof PipelineExecutionError)) {
|
|
4096
|
+
throw error;
|
|
4097
|
+
}
|
|
4098
|
+
throw new PipelineExecutionError(spaceTrim__default["default"](function (block) { return "\n ".concat(error.message, "\n\n This is error in FOREACH command\n You have probbably passed wrong data to pipeline or wrong data was generated which are processed by FOREACH command\n\n ").concat(block(pipelineIdentification), "\n "); }));
|
|
4099
|
+
}
|
|
4100
|
+
allSubparameters = __assign(__assign({}, parameters), mappedParameters);
|
|
4101
|
+
// Note: [👨👨👧] Now we can freeze `subparameters` because we are sure that all and only used parameters are defined and are not going to be changed
|
|
4102
|
+
Object.freeze(allSubparameters);
|
|
4103
|
+
return [4 /*yield*/, executeAttempts(__assign(__assign({}, options), { priority: priority + index, parameters: allSubparameters, pipelineIdentification: pipelineIdentification }))];
|
|
4104
|
+
case 1:
|
|
4105
|
+
subresultString = _a.sent();
|
|
4106
|
+
return [2 /*return*/, subresultString];
|
|
4107
|
+
}
|
|
4108
|
+
});
|
|
4109
|
+
}); })];
|
|
4110
|
+
case 1:
|
|
4111
|
+
resultString = _a.sent();
|
|
4112
|
+
return [2 /*return*/, resultString];
|
|
4113
|
+
}
|
|
4114
|
+
});
|
|
4115
|
+
});
|
|
4116
|
+
}
|
|
4117
|
+
/**
|
|
4118
|
+
* TODO: !!!!!! Make pipelineIdentification more precise
|
|
4119
|
+
* TODO: !!!!!! How FOREACH execution looks in the report
|
|
4120
|
+
* TODO: [🧠][🦥] Better (less confusing) name for "cell" / "subvalue" / "subparameter"
|
|
4121
|
+
* TODO: []
|
|
4122
|
+
*/
|
|
4123
|
+
|
|
4124
|
+
/**
|
|
4125
|
+
* @@@
|
|
4126
|
+
*
|
|
4127
|
+
* @private internal utility of `createPipelineExecutor`
|
|
4128
|
+
*/
|
|
4129
|
+
function getContextForTemplate(template) {
|
|
4130
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
4131
|
+
return __generator(this, function (_a) {
|
|
4132
|
+
TODO_USE(template);
|
|
4133
|
+
return [2 /*return*/, RESERVED_PARAMETER_MISSING_VALUE /* <- TODO: [🏍] Implement */];
|
|
4134
|
+
});
|
|
4135
|
+
});
|
|
4136
|
+
}
|
|
4137
|
+
|
|
4138
|
+
/**
|
|
4139
|
+
* @@@
|
|
4140
|
+
*
|
|
4141
|
+
* @private internal utility of `createPipelineExecutor`
|
|
4142
|
+
*/
|
|
4143
|
+
function getKnowledgeForTemplate(options) {
|
|
4144
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
4145
|
+
var preparedPipeline, template;
|
|
4146
|
+
return __generator(this, function (_a) {
|
|
4147
|
+
preparedPipeline = options.preparedPipeline, template = options.template;
|
|
4148
|
+
// TODO: [♨] Implement Better - use real index and keyword search from `template` and {samples}
|
|
4149
|
+
TODO_USE(template);
|
|
4150
|
+
return [2 /*return*/, preparedPipeline.knowledgePieces.map(function (_a) {
|
|
4151
|
+
var content = _a.content;
|
|
4152
|
+
return "- ".concat(content);
|
|
4153
|
+
}).join('\n')];
|
|
4154
|
+
});
|
|
4155
|
+
});
|
|
4156
|
+
}
|
|
4157
|
+
|
|
4158
|
+
/**
|
|
4159
|
+
* @@@
|
|
4160
|
+
*
|
|
4161
|
+
* @private internal utility of `createPipelineExecutor`
|
|
4162
|
+
*/
|
|
4163
|
+
function getSamplesForTemplate(template) {
|
|
4164
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
4165
|
+
return __generator(this, function (_a) {
|
|
4166
|
+
// TODO: [♨] Implement Better - use real index and keyword search
|
|
4167
|
+
TODO_USE(template);
|
|
4168
|
+
return [2 /*return*/, RESERVED_PARAMETER_MISSING_VALUE /* <- TODO: [♨] Implement */];
|
|
4169
|
+
});
|
|
4170
|
+
});
|
|
4171
|
+
}
|
|
4172
|
+
|
|
4173
|
+
/**
|
|
4174
|
+
* @@@
|
|
4175
|
+
*
|
|
4176
|
+
* @private internal utility of `createPipelineExecutor`
|
|
4177
|
+
*/
|
|
4178
|
+
function getReservedParametersForTemplate(options) {
|
|
4179
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
4180
|
+
var preparedPipeline, template, pipelineIdentification, context, knowledge, samples, currentDate, modelName, reservedParameters, _loop_1, RESERVED_PARAMETER_NAMES_1, RESERVED_PARAMETER_NAMES_1_1, parameterName;
|
|
4181
|
+
var e_1, _a;
|
|
4182
|
+
return __generator(this, function (_b) {
|
|
4183
|
+
switch (_b.label) {
|
|
4184
|
+
case 0:
|
|
4185
|
+
preparedPipeline = options.preparedPipeline, template = options.template, pipelineIdentification = options.pipelineIdentification;
|
|
4186
|
+
return [4 /*yield*/, getContextForTemplate(template)];
|
|
4187
|
+
case 1:
|
|
4188
|
+
context = _b.sent();
|
|
4189
|
+
return [4 /*yield*/, getKnowledgeForTemplate({ preparedPipeline: preparedPipeline, template: template })];
|
|
4190
|
+
case 2:
|
|
4191
|
+
knowledge = _b.sent();
|
|
4192
|
+
return [4 /*yield*/, getSamplesForTemplate(template)];
|
|
4193
|
+
case 3:
|
|
4194
|
+
samples = _b.sent();
|
|
4195
|
+
currentDate = new Date().toISOString();
|
|
4196
|
+
modelName = RESERVED_PARAMETER_MISSING_VALUE;
|
|
4197
|
+
reservedParameters = {
|
|
4198
|
+
content: RESERVED_PARAMETER_RESTRICTED,
|
|
4199
|
+
context: context,
|
|
4200
|
+
knowledge: knowledge,
|
|
4201
|
+
samples: samples,
|
|
4202
|
+
currentDate: currentDate,
|
|
4203
|
+
modelName: modelName,
|
|
4204
|
+
};
|
|
4205
|
+
_loop_1 = function (parameterName) {
|
|
4206
|
+
if (reservedParameters[parameterName] === undefined) {
|
|
4207
|
+
throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Reserved parameter {".concat(parameterName, "} is not defined\n\n ").concat(block(pipelineIdentification), "\n "); }));
|
|
4208
|
+
}
|
|
4209
|
+
};
|
|
4210
|
+
try {
|
|
4211
|
+
// Note: Doublecheck that ALL reserved parameters are defined:
|
|
4212
|
+
for (RESERVED_PARAMETER_NAMES_1 = __values(RESERVED_PARAMETER_NAMES), RESERVED_PARAMETER_NAMES_1_1 = RESERVED_PARAMETER_NAMES_1.next(); !RESERVED_PARAMETER_NAMES_1_1.done; RESERVED_PARAMETER_NAMES_1_1 = RESERVED_PARAMETER_NAMES_1.next()) {
|
|
4213
|
+
parameterName = RESERVED_PARAMETER_NAMES_1_1.value;
|
|
4214
|
+
_loop_1(parameterName);
|
|
4215
|
+
}
|
|
4216
|
+
}
|
|
4217
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
4218
|
+
finally {
|
|
4219
|
+
try {
|
|
4220
|
+
if (RESERVED_PARAMETER_NAMES_1_1 && !RESERVED_PARAMETER_NAMES_1_1.done && (_a = RESERVED_PARAMETER_NAMES_1.return)) _a.call(RESERVED_PARAMETER_NAMES_1);
|
|
4221
|
+
}
|
|
4222
|
+
finally { if (e_1) throw e_1.error; }
|
|
4223
|
+
}
|
|
4224
|
+
return [2 /*return*/, reservedParameters];
|
|
4225
|
+
}
|
|
4226
|
+
});
|
|
4227
|
+
});
|
|
4228
|
+
}
|
|
4229
|
+
|
|
4230
|
+
/**
|
|
4231
|
+
* @@@
|
|
4232
|
+
*
|
|
4233
|
+
* @private internal utility of `createPipelineExecutor`
|
|
4234
|
+
*/
|
|
4235
|
+
function executeTemplate(options) {
|
|
4236
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
4237
|
+
var currentTemplate, preparedPipeline, parametersToPass, tools, llmTools, onProgress, settings, $executionReport, pipelineIdentification, maxExecutionAttempts, name, title, priority, usedParameterNames, dependentParameterNames, definedParameters, _a, _b, _c, definedParameterNames, parameters, _loop_1, _d, _e, parameterName, maxAttempts, jokerParameterNames, preparedContent, resultString;
|
|
4238
|
+
var e_1, _f, _g;
|
|
4239
|
+
return __generator(this, function (_h) {
|
|
4240
|
+
switch (_h.label) {
|
|
4241
|
+
case 0:
|
|
4242
|
+
currentTemplate = options.currentTemplate, preparedPipeline = options.preparedPipeline, parametersToPass = options.parametersToPass, tools = options.tools, llmTools = options.llmTools, onProgress = options.onProgress, settings = options.settings, $executionReport = options.$executionReport, pipelineIdentification = options.pipelineIdentification;
|
|
4243
|
+
maxExecutionAttempts = settings.maxExecutionAttempts;
|
|
4244
|
+
name = "pipeline-executor-frame-".concat(currentTemplate.name);
|
|
4245
|
+
title = currentTemplate.title;
|
|
4246
|
+
priority = preparedPipeline.templates.length - preparedPipeline.templates.indexOf(currentTemplate);
|
|
4247
|
+
return [4 /*yield*/, onProgress({
|
|
4248
|
+
name: name,
|
|
4249
|
+
title: title,
|
|
4250
|
+
isStarted: false,
|
|
4251
|
+
isDone: false,
|
|
4252
|
+
templateType: currentTemplate.templateType,
|
|
4253
|
+
parameterName: currentTemplate.resultingParameterName,
|
|
4254
|
+
parameterValue: null,
|
|
4255
|
+
// <- [🍸]
|
|
4256
|
+
})];
|
|
4257
|
+
case 1:
|
|
4258
|
+
_h.sent();
|
|
4259
|
+
usedParameterNames = extractParameterNamesFromTemplate(currentTemplate);
|
|
4260
|
+
dependentParameterNames = new Set(currentTemplate.dependentParameterNames);
|
|
4261
|
+
// TODO: [👩🏾🤝👩🏻] Use here `mapAvailableToExpectedParameters`
|
|
4262
|
+
if (union(difference(usedParameterNames, dependentParameterNames), difference(dependentParameterNames, usedParameterNames)).size !== 0) {
|
|
4263
|
+
throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Dependent parameters are not consistent with used parameters:\n\n Dependent parameters:\n ".concat(Array.from(dependentParameterNames)
|
|
4264
|
+
.map(function (name) { return "{".concat(name, "}"); })
|
|
4265
|
+
.join(', '), "\n\n Used parameters:\n ").concat(Array.from(usedParameterNames)
|
|
4266
|
+
.map(function (name) { return "{".concat(name, "}"); })
|
|
4267
|
+
.join(', '), "\n\n ").concat(block(pipelineIdentification), "\n\n "); }));
|
|
4268
|
+
}
|
|
4269
|
+
_b = (_a = Object).freeze;
|
|
4270
|
+
_c = [{}];
|
|
4271
|
+
return [4 /*yield*/, getReservedParametersForTemplate({
|
|
4272
|
+
preparedPipeline: preparedPipeline,
|
|
4273
|
+
template: currentTemplate,
|
|
4274
|
+
pipelineIdentification: pipelineIdentification,
|
|
4275
|
+
})];
|
|
4276
|
+
case 2:
|
|
4277
|
+
definedParameters = _b.apply(_a, [__assign.apply(void 0, [__assign.apply(void 0, _c.concat([(_h.sent())])), parametersToPass])]);
|
|
4278
|
+
definedParameterNames = new Set(Object.keys(definedParameters));
|
|
4279
|
+
parameters = {};
|
|
4280
|
+
_loop_1 = function (parameterName) {
|
|
4281
|
+
// Situation: Parameter is defined and used
|
|
4282
|
+
if (definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
|
|
4283
|
+
parameters[parameterName] = definedParameters[parameterName];
|
|
4284
|
+
}
|
|
4285
|
+
// Situation: Parameter is defined but NOT used
|
|
4286
|
+
else if (definedParameterNames.has(parameterName) && !usedParameterNames.has(parameterName)) ;
|
|
4287
|
+
// Situation: Parameter is NOT defined BUT used
|
|
4288
|
+
else if (!definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
|
|
4289
|
+
// Houston, we have a problem
|
|
4290
|
+
// Note: Checking part is also done in `validatePipeline`, but it’s good to doublecheck
|
|
4291
|
+
throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Parameter {".concat(parameterName, "} is NOT defined\n BUT used in template \"").concat(currentTemplate.title || currentTemplate.name, "\"\n\n This should be catched in `validatePipeline`\n\n ").concat(block(pipelineIdentification), "\n\n "); }));
|
|
4292
|
+
}
|
|
4293
|
+
};
|
|
4294
|
+
try {
|
|
4295
|
+
// Note: [2] Check that all used parameters are defined and removing unused parameters for this template
|
|
4296
|
+
// TODO: [👩🏾🤝👩🏻] Use here `mapAvailableToExpectedParameters`
|
|
4297
|
+
for (_d = __values(Array.from(union(definedParameterNames, usedParameterNames, dependentParameterNames))), _e = _d.next(); !_e.done; _e = _d.next()) {
|
|
4298
|
+
parameterName = _e.value;
|
|
4299
|
+
_loop_1(parameterName);
|
|
4300
|
+
}
|
|
4301
|
+
}
|
|
4302
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
4303
|
+
finally {
|
|
4304
|
+
try {
|
|
4305
|
+
if (_e && !_e.done && (_f = _d.return)) _f.call(_d);
|
|
4306
|
+
}
|
|
4307
|
+
finally { if (e_1) throw e_1.error; }
|
|
4308
|
+
}
|
|
4309
|
+
// Note: [👨👨👧] Now we can freeze `parameters` because we are sure that all and only used parameters are defined and are not going to be changed
|
|
4310
|
+
Object.freeze(parameters);
|
|
4311
|
+
maxAttempts = currentTemplate.templateType === 'DIALOG_TEMPLATE' ? Infinity : maxExecutionAttempts;
|
|
4312
|
+
jokerParameterNames = currentTemplate.jokerParameterNames || [];
|
|
4313
|
+
preparedContent = (currentTemplate.preparedContent || '{content}')
|
|
4314
|
+
.split('{content}')
|
|
4315
|
+
.join(currentTemplate.content);
|
|
4316
|
+
return [4 /*yield*/, executeFormatCells({
|
|
4317
|
+
jokerParameterNames: jokerParameterNames,
|
|
4318
|
+
priority: priority,
|
|
4319
|
+
maxAttempts: maxAttempts,
|
|
4320
|
+
preparedContent: preparedContent,
|
|
4321
|
+
parameters: parameters,
|
|
4322
|
+
template: currentTemplate,
|
|
4323
|
+
preparedPipeline: preparedPipeline,
|
|
4324
|
+
tools: tools,
|
|
4325
|
+
llmTools: llmTools,
|
|
4326
|
+
settings: settings,
|
|
4327
|
+
$executionReport: $executionReport,
|
|
4328
|
+
pipelineIdentification: pipelineIdentification,
|
|
4329
|
+
})];
|
|
4330
|
+
case 3:
|
|
4331
|
+
resultString = _h.sent();
|
|
4332
|
+
return [4 /*yield*/, onProgress({
|
|
4333
|
+
name: name,
|
|
4334
|
+
title: title,
|
|
4335
|
+
isStarted: true,
|
|
4336
|
+
isDone: true,
|
|
4337
|
+
templateType: currentTemplate.templateType,
|
|
4338
|
+
parameterName: currentTemplate.resultingParameterName,
|
|
4339
|
+
parameterValue: resultString,
|
|
4340
|
+
// <- [🍸]
|
|
4341
|
+
})];
|
|
4342
|
+
case 4:
|
|
4343
|
+
_h.sent();
|
|
4344
|
+
return [2 /*return*/, Object.freeze((_g = {},
|
|
4345
|
+
_g[currentTemplate.resultingParameterName] =
|
|
4346
|
+
// <- Note: [👩👩👧] No need to detect parameter collision here because pipeline checks logic consistency during construction
|
|
4347
|
+
resultString,
|
|
4348
|
+
_g))];
|
|
4349
|
+
}
|
|
4350
|
+
});
|
|
4351
|
+
});
|
|
4352
|
+
}
|
|
4353
|
+
/**
|
|
4354
|
+
* TODO: [🤹♂️]
|
|
4355
|
+
*/
|
|
4356
|
+
|
|
4357
|
+
/**
|
|
4358
|
+
* @@@
|
|
4359
|
+
*
|
|
4360
|
+
* @private internal utility of `createPipelineExecutor`
|
|
4361
|
+
*/
|
|
4362
|
+
function filterJustOutputParameters(options) {
|
|
4363
|
+
var e_1, _a;
|
|
4364
|
+
var preparedPipeline = options.preparedPipeline, parametersToPass = options.parametersToPass, $warnings = options.$warnings, pipelineIdentification = options.pipelineIdentification;
|
|
4365
|
+
var outputParameters = {};
|
|
4366
|
+
var _loop_1 = function (parameter) {
|
|
4367
|
+
if (parametersToPass[parameter.name] === undefined) {
|
|
4368
|
+
// [4]
|
|
4369
|
+
$warnings.push(new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Parameter {".concat(parameter.name, "} should be an output parameter, but it was not generated during pipeline execution\n\n ").concat(block(pipelineIdentification), "\n "); })));
|
|
4370
|
+
return "continue";
|
|
4371
|
+
}
|
|
4372
|
+
outputParameters[parameter.name] = parametersToPass[parameter.name] || '';
|
|
4373
|
+
};
|
|
4374
|
+
try {
|
|
4375
|
+
// Note: Filter ONLY output parameters
|
|
4376
|
+
// TODO: [👩🏾🤝👩🏻] Maybe use here `mapAvailableToExpectedParameters`
|
|
4377
|
+
for (var _b = __values(preparedPipeline.parameters.filter(function (_a) {
|
|
4378
|
+
var isOutput = _a.isOutput;
|
|
4379
|
+
return isOutput;
|
|
4380
|
+
})), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
4381
|
+
var parameter = _c.value;
|
|
4382
|
+
_loop_1(parameter);
|
|
3827
4383
|
}
|
|
3828
|
-
|
|
3829
|
-
|
|
3830
|
-
|
|
3831
|
-
|
|
3832
|
-
|
|
3833
|
-
// [4]
|
|
3834
|
-
warnings.push(new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Parameter {".concat(parameter.name, "} should be an output parameter, but it was not generated during pipeline execution\n\n ").concat(block(pipelineIdentification), "\n "); })));
|
|
3835
|
-
return "continue";
|
|
3836
|
-
}
|
|
3837
|
-
outputParameters[parameter.name] = parametersToPass[parameter.name] || '';
|
|
3838
|
-
};
|
|
3839
|
-
try {
|
|
3840
|
-
// Note: Filter ONLY output parameters
|
|
3841
|
-
for (var _b = __values(preparedPipeline.parameters.filter(function (_a) {
|
|
3842
|
-
var isOutput = _a.isOutput;
|
|
3843
|
-
return isOutput;
|
|
3844
|
-
})), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
3845
|
-
var parameter = _c.value;
|
|
3846
|
-
_loop_6(parameter);
|
|
3847
|
-
}
|
|
3848
|
-
}
|
|
3849
|
-
catch (e_8_1) { e_8 = { error: e_8_1 }; }
|
|
3850
|
-
finally {
|
|
3851
|
-
try {
|
|
3852
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
3853
|
-
}
|
|
3854
|
-
finally { if (e_8) throw e_8.error; }
|
|
3855
|
-
}
|
|
3856
|
-
return outputParameters;
|
|
4384
|
+
}
|
|
4385
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
4386
|
+
finally {
|
|
4387
|
+
try {
|
|
4388
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
3857
4389
|
}
|
|
3858
|
-
|
|
4390
|
+
finally { if (e_1) throw e_1.error; }
|
|
4391
|
+
}
|
|
4392
|
+
return outputParameters;
|
|
4393
|
+
}
|
|
4394
|
+
|
|
4395
|
+
/**
|
|
4396
|
+
* @@@
|
|
4397
|
+
*
|
|
4398
|
+
* Note: This is not a `PipelineExecutor` (which is binded with one exact pipeline), but a utility function of `createPipelineExecutor` which creates `PipelineExecutor`
|
|
4399
|
+
*
|
|
4400
|
+
* @private internal utility of `createPipelineExecutor`
|
|
4401
|
+
*/
|
|
4402
|
+
function executePipeline(options) {
|
|
4403
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
4404
|
+
var inputParameters, tools, onProgress, pipeline, setPreparedPipeline, pipelineIdentification, settings, maxParallelCount, isVerbose, preparedPipeline, llmTools, errors, warnings, executionReport, isReturned, _a, _b, parameter, e_1_1, _loop_1, _c, _d, parameterName, state_1, e_2_1, parametersToPass, resovedParameterNames_1, unresovedTemplates_1, resolving_1, loopLimit, _loop_2, error_1, usage_1, outputParameters_1, usage, outputParameters;
|
|
3859
4405
|
var e_1, _e, e_2, _f;
|
|
3860
4406
|
return __generator(this, function (_g) {
|
|
3861
4407
|
switch (_g.label) {
|
|
3862
4408
|
case 0:
|
|
4409
|
+
inputParameters = options.inputParameters, tools = options.tools, onProgress = options.onProgress, pipeline = options.pipeline, setPreparedPipeline = options.setPreparedPipeline, pipelineIdentification = options.pipelineIdentification, settings = options.settings;
|
|
4410
|
+
maxParallelCount = settings.maxParallelCount, isVerbose = settings.isVerbose;
|
|
4411
|
+
preparedPipeline = options.preparedPipeline;
|
|
4412
|
+
llmTools = joinLlmExecutionTools.apply(void 0, __spreadArray([], __read(arrayableToArray(tools.llm)), false));
|
|
3863
4413
|
if (!(preparedPipeline === undefined)) return [3 /*break*/, 2];
|
|
3864
4414
|
return [4 /*yield*/, preparePipeline(pipeline, {
|
|
3865
4415
|
llmTools: llmTools,
|
|
@@ -3868,6 +4418,7 @@
|
|
|
3868
4418
|
})];
|
|
3869
4419
|
case 1:
|
|
3870
4420
|
preparedPipeline = _g.sent();
|
|
4421
|
+
setPreparedPipeline(preparedPipeline);
|
|
3871
4422
|
_g.label = 2;
|
|
3872
4423
|
case 2:
|
|
3873
4424
|
errors = [];
|
|
@@ -3937,7 +4488,7 @@
|
|
|
3937
4488
|
return name === parameterName;
|
|
3938
4489
|
});
|
|
3939
4490
|
if (!(parameter === undefined)) return [3 /*break*/, 1];
|
|
3940
|
-
warnings.push(new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n
|
|
4491
|
+
warnings.push(new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Extra parameter {".concat(parameterName, "} is being passed which is not part of the pipeline.\n\n ").concat(block(pipelineIdentification), "\n "); })));
|
|
3941
4492
|
return [3 /*break*/, 4];
|
|
3942
4493
|
case 1:
|
|
3943
4494
|
if (!(parameter.isInput === false)) return [3 /*break*/, 4];
|
|
@@ -3949,10 +4500,10 @@
|
|
|
3949
4500
|
// Note: Wait a short time to prevent race conditions
|
|
3950
4501
|
_h.sent();
|
|
3951
4502
|
_h.label = 3;
|
|
3952
|
-
case 3: return [2 /*return*/, { value: $asDeeplyFrozenSerializableJson(spaceTrim.spaceTrim(function (block) { return "\n
|
|
4503
|
+
case 3: return [2 /*return*/, { value: $asDeeplyFrozenSerializableJson(spaceTrim.spaceTrim(function (block) { return "\n Unuccessful PipelineExecutorResult (with extra parameter {".concat(parameter.name, "}) PipelineExecutorResult\n\n ").concat(block(pipelineIdentification), "\n "); }), {
|
|
3953
4504
|
isSuccessful: false,
|
|
3954
4505
|
errors: __spreadArray([
|
|
3955
|
-
new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n
|
|
4506
|
+
new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Parameter {".concat(parameter.name, "} is passed as input parameter but it is not input\n\n ").concat(block(pipelineIdentification), "\n "); }))
|
|
3956
4507
|
], __read(errors), false).map(serializeError),
|
|
3957
4508
|
warnings: warnings.map(serializeError),
|
|
3958
4509
|
executionReport: executionReport,
|
|
@@ -4016,7 +4567,7 @@
|
|
|
4016
4567
|
case 0:
|
|
4017
4568
|
if (loopLimit-- < 0) {
|
|
4018
4569
|
// Note: Really UnexpectedError not LimitReachedError - this should be catched during validatePipeline
|
|
4019
|
-
throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n
|
|
4570
|
+
throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Loop limit reached during resolving parameters pipeline execution\n\n ".concat(block(pipelineIdentification), "\n "); }));
|
|
4020
4571
|
}
|
|
4021
4572
|
currentTemplate = unresovedTemplates_1.find(function (template) {
|
|
4022
4573
|
return template.dependentParameterNames.every(function (name) {
|
|
@@ -4026,29 +4577,52 @@
|
|
|
4026
4577
|
if (!(!currentTemplate && resolving_1.length === 0)) return [3 /*break*/, 1];
|
|
4027
4578
|
throw new UnexpectedError(
|
|
4028
4579
|
// TODO: [🐎] DRY
|
|
4029
|
-
spaceTrim.spaceTrim(function (block) { return "\n
|
|
4580
|
+
spaceTrim.spaceTrim(function (block) { return "\n Can not resolve some parameters:\n\n ".concat(block(pipelineIdentification), "\n\n Can not resolve:\n ").concat(block(unresovedTemplates_1
|
|
4030
4581
|
.map(function (_a) {
|
|
4031
4582
|
var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
|
|
4032
4583
|
return "- Parameter {".concat(resultingParameterName, "} which depends on ").concat(dependentParameterNames
|
|
4033
4584
|
.map(function (dependentParameterName) { return "{".concat(dependentParameterName, "}"); })
|
|
4034
4585
|
.join(' and '));
|
|
4035
4586
|
})
|
|
4036
|
-
.join('\n')), "\n\n
|
|
4587
|
+
.join('\n')), "\n\n Resolved:\n ").concat(block(resovedParameterNames_1.map(function (name) { return "- Parameter {".concat(name, "}"); }).join('\n')), "\n\n Note: This should be catched in `validatePipeline`\n "); }));
|
|
4037
4588
|
case 1:
|
|
4038
4589
|
if (!!currentTemplate) return [3 /*break*/, 3];
|
|
4039
|
-
/* [
|
|
4590
|
+
/* [🤹♂️] */ return [4 /*yield*/, Promise.race(resolving_1)];
|
|
4040
4591
|
case 2:
|
|
4041
|
-
/* [
|
|
4592
|
+
/* [🤹♂️] */ _j.sent();
|
|
4042
4593
|
return [3 /*break*/, 4];
|
|
4043
4594
|
case 3:
|
|
4044
4595
|
unresovedTemplates_1 = unresovedTemplates_1.filter(function (template) { return template !== currentTemplate; });
|
|
4045
|
-
work_1 =
|
|
4046
|
-
|
|
4596
|
+
work_1 = executeTemplate({
|
|
4597
|
+
currentTemplate: currentTemplate,
|
|
4598
|
+
preparedPipeline: preparedPipeline,
|
|
4599
|
+
parametersToPass: parametersToPass,
|
|
4600
|
+
tools: tools,
|
|
4601
|
+
llmTools: llmTools,
|
|
4602
|
+
onProgress: function (progress) {
|
|
4603
|
+
if (isReturned) {
|
|
4604
|
+
throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Can not call `onProgress` after pipeline execution is finished\n\n ".concat(block(pipelineIdentification), "\n\n ").concat(block(JSON.stringify(progress, null, 4)
|
|
4605
|
+
.split('\n')
|
|
4606
|
+
.map(function (line) { return "> ".concat(line); })
|
|
4607
|
+
.join('\n')), "\n "); }));
|
|
4608
|
+
}
|
|
4609
|
+
if (onProgress) {
|
|
4610
|
+
onProgress(progress);
|
|
4611
|
+
}
|
|
4612
|
+
},
|
|
4613
|
+
settings: settings,
|
|
4614
|
+
$executionReport: executionReport,
|
|
4615
|
+
pipelineIdentification: pipelineIdentification,
|
|
4616
|
+
})
|
|
4617
|
+
.then(function (newParametersToPass) {
|
|
4618
|
+
parametersToPass = __assign(__assign({}, newParametersToPass), parametersToPass);
|
|
4047
4619
|
resovedParameterNames_1 = __spreadArray(__spreadArray([], __read(resovedParameterNames_1), false), [currentTemplate.resultingParameterName], false);
|
|
4048
4620
|
})
|
|
4049
4621
|
.then(function () {
|
|
4050
4622
|
resolving_1 = resolving_1.filter(function (w) { return w !== work_1; });
|
|
4051
4623
|
});
|
|
4624
|
+
// <- Note: Errors are catched here [3]
|
|
4625
|
+
// TODO: BUT if in multiple templates are errors, only the first one is catched so maybe we should catch errors here and save them to errors array here
|
|
4052
4626
|
resolving_1.push(work_1);
|
|
4053
4627
|
_j.label = 4;
|
|
4054
4628
|
case 4: return [2 /*return*/];
|
|
@@ -4075,7 +4649,12 @@
|
|
|
4075
4649
|
var result = _a.result;
|
|
4076
4650
|
return (result === null || result === void 0 ? void 0 : result.usage) || ZERO_USAGE;
|
|
4077
4651
|
})), false));
|
|
4078
|
-
outputParameters_1 = filterJustOutputParameters(
|
|
4652
|
+
outputParameters_1 = filterJustOutputParameters({
|
|
4653
|
+
preparedPipeline: preparedPipeline,
|
|
4654
|
+
parametersToPass: parametersToPass,
|
|
4655
|
+
$warnings: warnings,
|
|
4656
|
+
pipelineIdentification: pipelineIdentification,
|
|
4657
|
+
});
|
|
4079
4658
|
isReturned = true;
|
|
4080
4659
|
if (!(onProgress !== undefined)) return [3 /*break*/, 27];
|
|
4081
4660
|
// Note: Wait a short time to prevent race conditions
|
|
@@ -4098,7 +4677,12 @@
|
|
|
4098
4677
|
var result = _a.result;
|
|
4099
4678
|
return (result === null || result === void 0 ? void 0 : result.usage) || ZERO_USAGE;
|
|
4100
4679
|
})), false));
|
|
4101
|
-
outputParameters = filterJustOutputParameters(
|
|
4680
|
+
outputParameters = filterJustOutputParameters({
|
|
4681
|
+
preparedPipeline: preparedPipeline,
|
|
4682
|
+
parametersToPass: parametersToPass,
|
|
4683
|
+
$warnings: warnings,
|
|
4684
|
+
pipelineIdentification: pipelineIdentification,
|
|
4685
|
+
});
|
|
4102
4686
|
isReturned = true;
|
|
4103
4687
|
if (!(onProgress !== undefined)) return [3 /*break*/, 30];
|
|
4104
4688
|
// Note: Wait a short time to prevent race conditions
|
|
@@ -4118,22 +4702,62 @@
|
|
|
4118
4702
|
})];
|
|
4119
4703
|
}
|
|
4120
4704
|
});
|
|
4121
|
-
});
|
|
4122
|
-
return pipelineExecutor;
|
|
4705
|
+
});
|
|
4123
4706
|
}
|
|
4707
|
+
|
|
4124
4708
|
/**
|
|
4125
|
-
*
|
|
4126
|
-
*
|
|
4127
|
-
*
|
|
4128
|
-
*
|
|
4129
|
-
*
|
|
4130
|
-
* TODO: [🧠] When not meet expectations in DIALOG_TEMPLATE, make some way to tell the user
|
|
4131
|
-
* TODO: [👧] Strongly type the executors to avoid need of remove nullables whtn noUncheckedIndexedAccess in tsconfig.json
|
|
4132
|
-
* Note: CreatePipelineExecutorOptions are just connected to PipelineExecutor so do not extract to types folder
|
|
4133
|
-
* TODO: [🧠][3] transparent = (report intermediate parameters) / opaque execution = (report only output parameters) progress reporting mode
|
|
4134
|
-
* TODO: [🛠] Actions, instruments (and maybe knowledge) => Functions and tools
|
|
4135
|
-
* TODO: [🧠][💷] `assertsExecutionSuccessful` should be the method of `PipelineExecutor` result BUT maybe NOT to preserve pure JSON object
|
|
4709
|
+
* Creates executor function from pipeline and execution tools.
|
|
4710
|
+
*
|
|
4711
|
+
* @returns The executor function
|
|
4712
|
+
* @throws {PipelineLogicError} on logical error in the pipeline
|
|
4713
|
+
* @public exported from `@promptbook/core`
|
|
4136
4714
|
*/
|
|
4715
|
+
function createPipelineExecutor(options) {
|
|
4716
|
+
var _this = this;
|
|
4717
|
+
var pipeline = options.pipeline, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
|
|
4718
|
+
var _b = settings.maxExecutionAttempts, maxExecutionAttempts = _b === void 0 ? MAX_EXECUTION_ATTEMPTS : _b, _c = settings.maxParallelCount, maxParallelCount = _c === void 0 ? MAX_PARALLEL_COUNT : _c, _d = settings.isVerbose, isVerbose = _d === void 0 ? IS_VERBOSE : _d, _e = settings.isNotPreparedWarningSupressed, isNotPreparedWarningSupressed = _e === void 0 ? false : _e;
|
|
4719
|
+
validatePipeline(pipeline);
|
|
4720
|
+
var pipelineIdentification = (function () {
|
|
4721
|
+
// Note: This is a 😐 implementation of [🚞]
|
|
4722
|
+
var _ = [];
|
|
4723
|
+
if (pipeline.sourceFile !== undefined) {
|
|
4724
|
+
_.push("File: ".concat(pipeline.sourceFile));
|
|
4725
|
+
}
|
|
4726
|
+
if (pipeline.pipelineUrl !== undefined) {
|
|
4727
|
+
_.push("Url: ".concat(pipeline.pipelineUrl));
|
|
4728
|
+
}
|
|
4729
|
+
return _.join('\n');
|
|
4730
|
+
})();
|
|
4731
|
+
var preparedPipeline;
|
|
4732
|
+
if (isPipelinePrepared(pipeline)) {
|
|
4733
|
+
preparedPipeline = pipeline;
|
|
4734
|
+
}
|
|
4735
|
+
else if (isNotPreparedWarningSupressed !== true) {
|
|
4736
|
+
console.warn(spaceTrim.spaceTrim(function (block) { return "\n Pipeline is not prepared\n\n ".concat(block(pipelineIdentification), "\n\n It will be prepared ad-hoc before the first execution and **returned as `preparedPipeline` in `PipelineExecutorResult`**\n But it is recommended to prepare the pipeline during collection preparation\n\n @see more at https://ptbk.io/prepare-pipeline\n "); }));
|
|
4737
|
+
}
|
|
4738
|
+
var pipelineExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
|
|
4739
|
+
return __generator(this, function (_a) {
|
|
4740
|
+
return [2 /*return*/, executePipeline({
|
|
4741
|
+
pipeline: pipeline,
|
|
4742
|
+
preparedPipeline: preparedPipeline,
|
|
4743
|
+
setPreparedPipeline: function (newPreparedPipeline) {
|
|
4744
|
+
preparedPipeline = newPreparedPipeline;
|
|
4745
|
+
},
|
|
4746
|
+
inputParameters: inputParameters,
|
|
4747
|
+
tools: tools,
|
|
4748
|
+
onProgress: onProgress,
|
|
4749
|
+
pipelineIdentification: pipelineIdentification,
|
|
4750
|
+
settings: {
|
|
4751
|
+
maxExecutionAttempts: maxExecutionAttempts,
|
|
4752
|
+
maxParallelCount: maxParallelCount,
|
|
4753
|
+
isVerbose: isVerbose,
|
|
4754
|
+
isNotPreparedWarningSupressed: isNotPreparedWarningSupressed,
|
|
4755
|
+
},
|
|
4756
|
+
})];
|
|
4757
|
+
});
|
|
4758
|
+
}); };
|
|
4759
|
+
return pipelineExecutor;
|
|
4760
|
+
}
|
|
4137
4761
|
|
|
4138
4762
|
/**
|
|
4139
4763
|
* @@@
|
|
@@ -5269,6 +5893,145 @@
|
|
|
5269
5893
|
* TODO: [🌺] Use some intermediate util splitWords
|
|
5270
5894
|
*/
|
|
5271
5895
|
|
|
5896
|
+
/**
|
|
5897
|
+
* @@@
|
|
5898
|
+
*
|
|
5899
|
+
* @param text @@@
|
|
5900
|
+
* @param _isFirstLetterCapital @@@
|
|
5901
|
+
* @returns @@@
|
|
5902
|
+
* @example 'helloWorld'
|
|
5903
|
+
* @example 'iLovePromptbook'
|
|
5904
|
+
* @public exported from `@promptbook/utils`
|
|
5905
|
+
*/
|
|
5906
|
+
function normalizeTo_camelCase(text, _isFirstLetterCapital) {
|
|
5907
|
+
var e_1, _a;
|
|
5908
|
+
if (_isFirstLetterCapital === void 0) { _isFirstLetterCapital = false; }
|
|
5909
|
+
var charType;
|
|
5910
|
+
var lastCharType = null;
|
|
5911
|
+
var normalizedName = '';
|
|
5912
|
+
try {
|
|
5913
|
+
for (var text_1 = __values(text), text_1_1 = text_1.next(); !text_1_1.done; text_1_1 = text_1.next()) {
|
|
5914
|
+
var char = text_1_1.value;
|
|
5915
|
+
var normalizedChar = void 0;
|
|
5916
|
+
if (/^[a-z]$/.test(char)) {
|
|
5917
|
+
charType = 'LOWERCASE';
|
|
5918
|
+
normalizedChar = char;
|
|
5919
|
+
}
|
|
5920
|
+
else if (/^[A-Z]$/.test(char)) {
|
|
5921
|
+
charType = 'UPPERCASE';
|
|
5922
|
+
normalizedChar = char.toLowerCase();
|
|
5923
|
+
}
|
|
5924
|
+
else if (/^[0-9]$/.test(char)) {
|
|
5925
|
+
charType = 'NUMBER';
|
|
5926
|
+
normalizedChar = char;
|
|
5927
|
+
}
|
|
5928
|
+
else {
|
|
5929
|
+
charType = 'OTHER';
|
|
5930
|
+
normalizedChar = '';
|
|
5931
|
+
}
|
|
5932
|
+
if (!lastCharType) {
|
|
5933
|
+
if (_isFirstLetterCapital) {
|
|
5934
|
+
normalizedChar = normalizedChar.toUpperCase(); //TODO: DRY
|
|
5935
|
+
}
|
|
5936
|
+
}
|
|
5937
|
+
else if (charType !== lastCharType &&
|
|
5938
|
+
!(charType === 'LOWERCASE' && lastCharType === 'UPPERCASE') &&
|
|
5939
|
+
!(lastCharType === 'NUMBER') &&
|
|
5940
|
+
!(charType === 'NUMBER')) {
|
|
5941
|
+
normalizedChar = normalizedChar.toUpperCase(); //TODO: [🌺] DRY
|
|
5942
|
+
}
|
|
5943
|
+
normalizedName += normalizedChar;
|
|
5944
|
+
lastCharType = charType;
|
|
5945
|
+
}
|
|
5946
|
+
}
|
|
5947
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
5948
|
+
finally {
|
|
5949
|
+
try {
|
|
5950
|
+
if (text_1_1 && !text_1_1.done && (_a = text_1.return)) _a.call(text_1);
|
|
5951
|
+
}
|
|
5952
|
+
finally { if (e_1) throw e_1.error; }
|
|
5953
|
+
}
|
|
5954
|
+
return normalizedName;
|
|
5955
|
+
}
|
|
5956
|
+
/**
|
|
5957
|
+
* TODO: [🌺] Use some intermediate util splitWords
|
|
5958
|
+
*/
|
|
5959
|
+
|
|
5960
|
+
/**
|
|
5961
|
+
* Function `validateParameterName` will @@@
|
|
5962
|
+
*
|
|
5963
|
+
* @param parameterName @@@
|
|
5964
|
+
* @returns @@@
|
|
5965
|
+
* @throws {ParseError} @@@
|
|
5966
|
+
* @private within the repository
|
|
5967
|
+
*/
|
|
5968
|
+
function validateParameterName(parameterName) {
|
|
5969
|
+
var e_1, _a;
|
|
5970
|
+
var rawParameterName = parameterName;
|
|
5971
|
+
try {
|
|
5972
|
+
for (var _b = __values([
|
|
5973
|
+
['`', '`'],
|
|
5974
|
+
['{', '}'],
|
|
5975
|
+
['[', ']'],
|
|
5976
|
+
['(', ')'],
|
|
5977
|
+
['<', '>'],
|
|
5978
|
+
]), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
5979
|
+
var _d = __read(_c.value, 2), start = _d[0], end = _d[1];
|
|
5980
|
+
if (parameterName.substring(0, 1) === start &&
|
|
5981
|
+
parameterName.substring(parameterName.length - 1, parameterName.length) === end
|
|
5982
|
+
// <- TODO: More universal that 1 character
|
|
5983
|
+
) {
|
|
5984
|
+
parameterName = parameterName.substring(1, parameterName.length - 1);
|
|
5985
|
+
// <- TODO: More universal that 1 character
|
|
5986
|
+
}
|
|
5987
|
+
}
|
|
5988
|
+
}
|
|
5989
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
5990
|
+
finally {
|
|
5991
|
+
try {
|
|
5992
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
5993
|
+
}
|
|
5994
|
+
finally { if (e_1) throw e_1.error; }
|
|
5995
|
+
}
|
|
5996
|
+
// TODO: [🐠] Following try-catch block should be part of common validators logic
|
|
5997
|
+
try {
|
|
5998
|
+
/*
|
|
5999
|
+
Note: We don't need to check for spaces because we are going to normalize the parameter name to camelCase
|
|
6000
|
+
if (parameterName.includes(' ')) {
|
|
6001
|
+
throw new ParseError(`Parameter name cannot contain spaces`);
|
|
6002
|
+
}
|
|
6003
|
+
*/
|
|
6004
|
+
if (parameterName.includes('.')) {
|
|
6005
|
+
throw new ParseError("Parameter name cannot contain dots");
|
|
6006
|
+
}
|
|
6007
|
+
if (parameterName.includes('/') || parameterName.includes('\\')) {
|
|
6008
|
+
throw new ParseError("Parameter name cannot contain slashes");
|
|
6009
|
+
}
|
|
6010
|
+
if (parameterName.includes('(') ||
|
|
6011
|
+
parameterName.includes(')') ||
|
|
6012
|
+
parameterName.includes('{') ||
|
|
6013
|
+
parameterName.includes('}') ||
|
|
6014
|
+
parameterName.includes('[') ||
|
|
6015
|
+
parameterName.includes(']')) {
|
|
6016
|
+
throw new ParseError("Parameter name cannot contain braces");
|
|
6017
|
+
}
|
|
6018
|
+
parameterName = normalizeTo_camelCase(parameterName);
|
|
6019
|
+
if (parameterName === '') {
|
|
6020
|
+
throw new ParseError("Parameter name cannot be empty");
|
|
6021
|
+
}
|
|
6022
|
+
if (RESERVED_PARAMETER_NAMES.includes(parameterName)) {
|
|
6023
|
+
throw new ParseError("{".concat(parameterName, "} is a reserved parameter name"));
|
|
6024
|
+
}
|
|
6025
|
+
}
|
|
6026
|
+
catch (error) {
|
|
6027
|
+
if (!(error instanceof ParseError)) {
|
|
6028
|
+
throw error;
|
|
6029
|
+
}
|
|
6030
|
+
throw new ParseError(spaceTrim__default["default"](function (block) { return "\n ".concat(block(error.message), "\n\n Tried to validate parameter name:\n ").concat(block(rawParameterName), "\n "); }));
|
|
6031
|
+
}
|
|
6032
|
+
return parameterName;
|
|
6033
|
+
}
|
|
6034
|
+
|
|
5272
6035
|
/**
|
|
5273
6036
|
* Parses the foreach command
|
|
5274
6037
|
*
|
|
@@ -5303,62 +6066,66 @@
|
|
|
5303
6066
|
* Example usages of the FOREACH command
|
|
5304
6067
|
*/
|
|
5305
6068
|
examples: [
|
|
5306
|
-
'FOREACH
|
|
5307
|
-
'FOR
|
|
5308
|
-
'EACH
|
|
6069
|
+
'FOREACH Text Line `{customers}` -> `{customer}`',
|
|
6070
|
+
'FOR Csv Row `{customers}` -> `{firstName}`, `{lastName}`',
|
|
6071
|
+
'EACH Csv Cell `{customers}` -> `{cell}`',
|
|
5309
6072
|
// <- TODO: [🍭] !!!!!! More
|
|
5310
6073
|
],
|
|
5311
6074
|
/**
|
|
5312
6075
|
* Parses the FOREACH command
|
|
5313
6076
|
*/
|
|
5314
6077
|
parse: function (input) {
|
|
5315
|
-
var args = input.args
|
|
6078
|
+
var args = input.args;
|
|
5316
6079
|
var formatName = normalizeTo_SCREAMING_CASE(args[0] || '');
|
|
5317
6080
|
var cellName = normalizeTo_SCREAMING_CASE(args[1] || '');
|
|
5318
|
-
var
|
|
5319
|
-
var
|
|
5320
|
-
|
|
5321
|
-
|
|
5322
|
-
|
|
5323
|
-
|
|
5324
|
-
].includes(formatName)) {
|
|
6081
|
+
var parameterNameWrapped = args[2];
|
|
6082
|
+
var assignSign = args[3];
|
|
6083
|
+
var formatDefinition = FORMAT_DEFINITIONS.find(function (formatDefinition) {
|
|
6084
|
+
return __spreadArray([formatDefinition.formatName], __read((formatDefinition.aliases || [])), false).includes(formatName);
|
|
6085
|
+
});
|
|
6086
|
+
if (formatDefinition === undefined) {
|
|
5325
6087
|
console.info({ args: args, formatName: formatName });
|
|
5326
|
-
throw new
|
|
6088
|
+
throw new ParseError(spaceTrim__default["default"](function (block) { return "\n Unsupported format \"".concat(formatName, "\"\n\n Available formats:\n ").concat(block(FORMAT_DEFINITIONS.map(function (formatDefinition) { return formatDefinition.formatName; })
|
|
6089
|
+
.map(function (formatName) { return "- ".concat(formatName); })
|
|
6090
|
+
.join('\n')), "\n "); }));
|
|
5327
6091
|
// <- TODO: [🏢] List all supported format names
|
|
5328
6092
|
}
|
|
5329
|
-
|
|
5330
|
-
|
|
5331
|
-
|
|
5332
|
-
|
|
5333
|
-
'CELL',
|
|
5334
|
-
// <- TODO: [🏢] Unhardcode format cekks
|
|
5335
|
-
].includes(cellName)) {
|
|
6093
|
+
var subvalueDefinition = formatDefinition.subvalueDefinitions.find(function (subvalueDefinition) {
|
|
6094
|
+
return __spreadArray([subvalueDefinition.subvalueName], __read((subvalueDefinition.aliases || [])), false).includes(cellName);
|
|
6095
|
+
});
|
|
6096
|
+
if (subvalueDefinition === undefined) {
|
|
5336
6097
|
console.info({ args: args, cellName: cellName });
|
|
5337
|
-
throw new
|
|
6098
|
+
throw new ParseError(spaceTrim__default["default"](function (block) { return "\n Unsupported cell name \"".concat(cellName, "\" for format \"").concat(formatName, "\"\n\n Available cell names for format \"").concat(formatDefinition.formatName, "\":\n ").concat(block(formatDefinition.subvalueDefinitions
|
|
6099
|
+
.map(function (subvalueDefinition) { return subvalueDefinition.subvalueName; })
|
|
6100
|
+
.map(function (subvalueName) { return "- ".concat(subvalueName); })
|
|
6101
|
+
.join('\n')), "\n "); }));
|
|
5338
6102
|
// <- TODO: [🏢] List all supported cell names for the format
|
|
5339
6103
|
}
|
|
5340
6104
|
if (assignSign !== '->') {
|
|
5341
6105
|
console.info({ args: args, assignSign: assignSign });
|
|
5342
|
-
throw new
|
|
5343
|
-
}
|
|
5344
|
-
|
|
5345
|
-
if (
|
|
5346
|
-
|
|
5347
|
-
|
|
5348
|
-
|
|
5349
|
-
|
|
5350
|
-
|
|
5351
|
-
|
|
5352
|
-
|
|
5353
|
-
|
|
5354
|
-
|
|
5355
|
-
|
|
6106
|
+
throw new ParseError("FOREACH command must have '->' to assign the value to the parameter");
|
|
6107
|
+
}
|
|
6108
|
+
// TODO: !!!!!! Replace with propper parameter name validation `validateParameterName`
|
|
6109
|
+
if ((parameterNameWrapped === null || parameterNameWrapped === void 0 ? void 0 : parameterNameWrapped.substring(0, 1)) !== '{' ||
|
|
6110
|
+
(parameterNameWrapped === null || parameterNameWrapped === void 0 ? void 0 : parameterNameWrapped.substring(parameterNameWrapped.length - 1, parameterNameWrapped.length)) !== '}') {
|
|
6111
|
+
console.info({ args: args, parameterNameWrapped: parameterNameWrapped }, parameterNameWrapped === null || parameterNameWrapped === void 0 ? void 0 : parameterNameWrapped.substring(0, 1), parameterNameWrapped === null || parameterNameWrapped === void 0 ? void 0 : parameterNameWrapped.substring(parameterNameWrapped.length - 1, parameterNameWrapped.length));
|
|
6112
|
+
throw new ParseError("!!!!!! 1 Here will be error (with rules and precise error) from validateParameterName");
|
|
6113
|
+
}
|
|
6114
|
+
var parameterName = parameterNameWrapped.substring(1, parameterNameWrapped.length - 1);
|
|
6115
|
+
var subparameterNames = args
|
|
6116
|
+
.slice(4)
|
|
6117
|
+
.map(function (parameterName) { return parameterName.split(',').join(' ').trim(); })
|
|
6118
|
+
.filter(function (parameterName) { return parameterName !== ''; })
|
|
6119
|
+
.map(validateParameterName);
|
|
6120
|
+
if (subparameterNames.length === 0) {
|
|
6121
|
+
throw new ParseError("FOREACH command must have at least one subparameter");
|
|
5356
6122
|
}
|
|
5357
6123
|
return {
|
|
5358
6124
|
type: 'FOREACH',
|
|
5359
6125
|
formatName: formatName,
|
|
5360
6126
|
cellName: cellName,
|
|
5361
6127
|
parameterName: parameterName,
|
|
6128
|
+
subparameterNames: subparameterNames,
|
|
5362
6129
|
};
|
|
5363
6130
|
},
|
|
5364
6131
|
/**
|
|
@@ -5367,8 +6134,12 @@
|
|
|
5367
6134
|
* Note: `$` is used to indicate that this function mutates given `templateJson`
|
|
5368
6135
|
*/
|
|
5369
6136
|
$applyToTemplateJson: function (command, $templateJson, $pipelineJson) {
|
|
5370
|
-
|
|
5371
|
-
//
|
|
6137
|
+
var formatName = command.formatName, cellName = command.cellName, parameterName = command.parameterName, subparameterNames = command.subparameterNames;
|
|
6138
|
+
// TODO: !!!!!! Detect double use
|
|
6139
|
+
// TODO: !!!!!! Detect usage with JOKER and don't allow it
|
|
6140
|
+
$templateJson.foreach = { formatName: formatName, cellName: cellName, parameterName: parameterName, subparameterNames: subparameterNames };
|
|
6141
|
+
keepUnused($pipelineJson); // <- TODO: !!!!!! BUT Maybe register subparameter from foreach into parameters of the pipeline
|
|
6142
|
+
// Note: [🍭] FOREACH apply has some sideeffects on different places in codebase
|
|
5372
6143
|
},
|
|
5373
6144
|
/**
|
|
5374
6145
|
* Converts the FOREACH command back to string
|
|
@@ -5377,8 +6148,7 @@
|
|
|
5377
6148
|
*/
|
|
5378
6149
|
stringify: function (command) {
|
|
5379
6150
|
keepUnused(command);
|
|
5380
|
-
return "";
|
|
5381
|
-
// <- TODO: [🍭] !!!!!! Implement
|
|
6151
|
+
return "---"; // <- TODO: [🛋] Implement
|
|
5382
6152
|
},
|
|
5383
6153
|
/**
|
|
5384
6154
|
* Reads the FOREACH command from the `TemplateJson`
|
|
@@ -5387,12 +6157,12 @@
|
|
|
5387
6157
|
*/
|
|
5388
6158
|
takeFromTemplateJson: function ($templateJson) {
|
|
5389
6159
|
keepUnused($templateJson);
|
|
5390
|
-
|
|
5391
|
-
// <- TODO: [🍭] !!!!!! Implement
|
|
6160
|
+
throw new NotYetImplementedError("[\uD83D\uDECB] Not implemented yet"); // <- TODO: [🛋] Implement
|
|
5392
6161
|
},
|
|
5393
6162
|
};
|
|
5394
6163
|
/**
|
|
5395
|
-
* TODO: !!!!!!
|
|
6164
|
+
* TODO: !!!!!! Remove console logs
|
|
6165
|
+
* TODO: [🧠][🦥] Better (less confusing) name for "cell" / "subvalue" / "subparameter"
|
|
5396
6166
|
* TODO: [🍭] !!!!!! Make .ptbk.md file with examples of the FOREACH command and also with wrong parsing and logic
|
|
5397
6167
|
*/
|
|
5398
6168
|
|
|
@@ -5503,6 +6273,7 @@
|
|
|
5503
6273
|
*/
|
|
5504
6274
|
parse: function (input) {
|
|
5505
6275
|
var args = input.args;
|
|
6276
|
+
// TODO: !!!!!! Replace with propper parameter name validation `validateParameterName`
|
|
5506
6277
|
var parametersMatch = (args.pop() || '').match(/^\{(?<parameterName>[a-z0-9_]+)\}$/im);
|
|
5507
6278
|
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
5508
6279
|
throw new ParseError("Invalid joker");
|
|
@@ -5632,6 +6403,7 @@
|
|
|
5632
6403
|
if ($pipelineJson.defaultModelRequirements[command.key] !== undefined) {
|
|
5633
6404
|
if ($pipelineJson.defaultModelRequirements[command.key] === command.value) {
|
|
5634
6405
|
console.warn("Multiple commands `MODEL ".concat(command.key, " ").concat(command.value, "` in the pipeline head"));
|
|
6406
|
+
// <- TODO: [🚎] Some better way how to get warnings from pipeline parsing / logic
|
|
5635
6407
|
}
|
|
5636
6408
|
else {
|
|
5637
6409
|
throw new ParseError(spaceTrim__default["default"]("\n Redefinition of MODEL `".concat(command.key, "` in the pipeline head\n\n You have used:\n - MODEL ").concat(command.key, " ").concat($pipelineJson.defaultModelRequirements[command.key], "\n - MODEL ").concat(command.key, " ").concat(command.value, "\n ")));
|
|
@@ -7267,70 +8039,6 @@
|
|
|
7267
8039
|
* TODO: [🏛] This can be part of markdown builder
|
|
7268
8040
|
*/
|
|
7269
8041
|
|
|
7270
|
-
/**
|
|
7271
|
-
* @@@
|
|
7272
|
-
*
|
|
7273
|
-
* @param text @@@
|
|
7274
|
-
* @param _isFirstLetterCapital @@@
|
|
7275
|
-
* @returns @@@
|
|
7276
|
-
* @example 'helloWorld'
|
|
7277
|
-
* @example 'iLovePromptbook'
|
|
7278
|
-
* @public exported from `@promptbook/utils`
|
|
7279
|
-
*/
|
|
7280
|
-
function normalizeTo_camelCase(text, _isFirstLetterCapital) {
|
|
7281
|
-
var e_1, _a;
|
|
7282
|
-
if (_isFirstLetterCapital === void 0) { _isFirstLetterCapital = false; }
|
|
7283
|
-
var charType;
|
|
7284
|
-
var lastCharType = null;
|
|
7285
|
-
var normalizedName = '';
|
|
7286
|
-
try {
|
|
7287
|
-
for (var text_1 = __values(text), text_1_1 = text_1.next(); !text_1_1.done; text_1_1 = text_1.next()) {
|
|
7288
|
-
var char = text_1_1.value;
|
|
7289
|
-
var normalizedChar = void 0;
|
|
7290
|
-
if (/^[a-z]$/.test(char)) {
|
|
7291
|
-
charType = 'LOWERCASE';
|
|
7292
|
-
normalizedChar = char;
|
|
7293
|
-
}
|
|
7294
|
-
else if (/^[A-Z]$/.test(char)) {
|
|
7295
|
-
charType = 'UPPERCASE';
|
|
7296
|
-
normalizedChar = char.toLowerCase();
|
|
7297
|
-
}
|
|
7298
|
-
else if (/^[0-9]$/.test(char)) {
|
|
7299
|
-
charType = 'NUMBER';
|
|
7300
|
-
normalizedChar = char;
|
|
7301
|
-
}
|
|
7302
|
-
else {
|
|
7303
|
-
charType = 'OTHER';
|
|
7304
|
-
normalizedChar = '';
|
|
7305
|
-
}
|
|
7306
|
-
if (!lastCharType) {
|
|
7307
|
-
if (_isFirstLetterCapital) {
|
|
7308
|
-
normalizedChar = normalizedChar.toUpperCase(); //TODO: DRY
|
|
7309
|
-
}
|
|
7310
|
-
}
|
|
7311
|
-
else if (charType !== lastCharType &&
|
|
7312
|
-
!(charType === 'LOWERCASE' && lastCharType === 'UPPERCASE') &&
|
|
7313
|
-
!(lastCharType === 'NUMBER') &&
|
|
7314
|
-
!(charType === 'NUMBER')) {
|
|
7315
|
-
normalizedChar = normalizedChar.toUpperCase(); //TODO: [🌺] DRY
|
|
7316
|
-
}
|
|
7317
|
-
normalizedName += normalizedChar;
|
|
7318
|
-
lastCharType = charType;
|
|
7319
|
-
}
|
|
7320
|
-
}
|
|
7321
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
7322
|
-
finally {
|
|
7323
|
-
try {
|
|
7324
|
-
if (text_1_1 && !text_1_1.done && (_a = text_1.return)) _a.call(text_1);
|
|
7325
|
-
}
|
|
7326
|
-
finally { if (e_1) throw e_1.error; }
|
|
7327
|
-
}
|
|
7328
|
-
return normalizedName;
|
|
7329
|
-
}
|
|
7330
|
-
/**
|
|
7331
|
-
* TODO: [🌺] Use some intermediate util splitWords
|
|
7332
|
-
*/
|
|
7333
|
-
|
|
7334
8042
|
/**
|
|
7335
8043
|
* Creates a Mermaid graph based on the promptbook
|
|
7336
8044
|
*
|
|
@@ -7387,6 +8095,9 @@
|
|
|
7387
8095
|
return promptbookMermaid;
|
|
7388
8096
|
}
|
|
7389
8097
|
/**
|
|
8098
|
+
* TODO: !!!!!! FOREACH in mermaid graph
|
|
8099
|
+
* TODO: !!!!!! Knowledge in mermaid graph
|
|
8100
|
+
* TODO: !!!!!! Personas in mermaid graph
|
|
7390
8101
|
* TODO: Maybe use some Mermaid package instead of string templating
|
|
7391
8102
|
* TODO: [🕌] When more than 2 functionalities, split into separate functions
|
|
7392
8103
|
*/
|
|
@@ -7528,21 +8239,40 @@
|
|
|
7528
8239
|
* @public exported from `@promptbook/core`
|
|
7529
8240
|
*/
|
|
7530
8241
|
function usageToHuman(usage) {
|
|
7531
|
-
var
|
|
8242
|
+
var reportItems = [];
|
|
7532
8243
|
var uncertainNumberToHuman = function (_a) {
|
|
7533
8244
|
var value = _a.value, isUncertain = _a.isUncertain;
|
|
7534
8245
|
return "".concat(isUncertain ? 'approximately ' : '').concat(Math.round(value * 100) / 100);
|
|
7535
8246
|
};
|
|
7536
|
-
|
|
7537
|
-
|
|
7538
|
-
|
|
8247
|
+
if (usage.price.value > 0.1
|
|
8248
|
+
// <- TODO: [🍓][🧞♂️][👩🏽🤝🧑🏻] Configure negligible value - default value to config + value to `UsageToHumanSettings`
|
|
8249
|
+
) {
|
|
8250
|
+
reportItems.push("Cost ".concat(uncertainNumberToHuman(usage.price), " USD"));
|
|
8251
|
+
}
|
|
8252
|
+
else {
|
|
8253
|
+
reportItems.push("Negligible cost");
|
|
8254
|
+
}
|
|
8255
|
+
var worktime = usageToWorktime(usage);
|
|
8256
|
+
if (worktime.value > 0.5
|
|
8257
|
+
// <- TODO: [🍓][🧞♂️][👩🏽🤝🧑🏻]
|
|
8258
|
+
) {
|
|
8259
|
+
reportItems.push("Saved ".concat(uncertainNumberToHuman(usageToWorktime(usage)), " hours of human time"));
|
|
8260
|
+
// TODO: [🍓][🧞♂️] Show minutes, seconds, days NOT 0.1 hours
|
|
8261
|
+
}
|
|
8262
|
+
if (usage.output.charactersCount.value > 0) {
|
|
8263
|
+
reportItems.push("Written ".concat(uncertainNumberToHuman(usage.output.charactersCount), " characters"));
|
|
8264
|
+
}
|
|
8265
|
+
if (reportItems.length === 0) {
|
|
8266
|
+
// Note: For negligible usage, we report at least something
|
|
8267
|
+
reportItems.push('Negligible');
|
|
8268
|
+
}
|
|
8269
|
+
return spaceTrim__default["default"](function (block) { return "\n Usage:\n ".concat(block(reportItems.map(function (item) { return "- ".concat(item); }).join('\n')), "\n "); });
|
|
7539
8270
|
}
|
|
7540
8271
|
/**
|
|
7541
|
-
* TODO: Use "$1" not "1 USD"
|
|
7542
|
-
* TODO: Use markdown formatting like "Cost approximately **$1**"
|
|
7543
|
-
* TODO: Report in minutes, seconds, days NOT 0.1 hours
|
|
8272
|
+
* TODO: [🍓][🧞♂️] Use "$1" not "1 USD"
|
|
8273
|
+
* TODO: [🍓][🧞♂️] Use markdown formatting like "Cost approximately **$1**"
|
|
8274
|
+
* TODO: [🍓][🧞♂️] Report in minutes, seconds, days NOT 0.1 hours
|
|
7544
8275
|
* TODO: [🧠] Maybe make from `uncertainNumberToHuman` separate exported utility
|
|
7545
|
-
* TODO: When negligible usage, report "Negligible" or just don't report it
|
|
7546
8276
|
* TODO: [🧠] Maybe use "~" instead of "approximately"
|
|
7547
8277
|
* TODO: [🏛] Maybe make some markdown builder
|
|
7548
8278
|
*/
|
|
@@ -8486,6 +9216,7 @@
|
|
|
8486
9216
|
exports.CLAIM = CLAIM;
|
|
8487
9217
|
exports.CallbackInterfaceTools = CallbackInterfaceTools;
|
|
8488
9218
|
exports.CollectionError = CollectionError;
|
|
9219
|
+
exports.CsvFormatDefinition = CsvFormatDefinition;
|
|
8489
9220
|
exports.DEFAULT_REMOTE_URL = DEFAULT_REMOTE_URL;
|
|
8490
9221
|
exports.DEFAULT_REMOTE_URL_PATH = DEFAULT_REMOTE_URL_PATH;
|
|
8491
9222
|
exports.ERRORS = ERRORS;
|
|
@@ -8514,6 +9245,7 @@
|
|
|
8514
9245
|
exports.PrefixStorage = PrefixStorage;
|
|
8515
9246
|
exports.RESERVED_PARAMETER_NAMES = RESERVED_PARAMETER_NAMES;
|
|
8516
9247
|
exports.TemplateTypes = TemplateTypes;
|
|
9248
|
+
exports.TextFormatDefinition = TextFormatDefinition;
|
|
8517
9249
|
exports.UnexpectedError = UnexpectedError;
|
|
8518
9250
|
exports.ZERO_USAGE = ZERO_USAGE;
|
|
8519
9251
|
exports._AnthropicClaudeMetadataRegistration = _AnthropicClaudeMetadataRegistration;
|