@promptbook/utils 0.77.1 → 0.78.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/index.es.js +365 -500
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/core.index.d.ts +10 -0
- package/esm/typings/src/_packages/types.index.d.ts +4 -0
- package/esm/typings/src/_packages/utils.index.d.ts +4 -8
- package/esm/typings/src/commands/_common/types/CommandType.d.ts +17 -0
- package/esm/typings/src/config.d.ts +14 -0
- package/esm/typings/src/conversion/utils/extractParameterNamesFromTask.d.ts +1 -1
- package/esm/typings/src/conversion/utils/{extractVariables.d.ts → extractVariablesFromScript.d.ts} +2 -2
- package/esm/typings/src/conversion/utils/removePipelineCommand.d.ts +22 -0
- package/esm/typings/src/conversion/utils/{renameParameter.d.ts → renamePipelineParameter.d.ts} +3 -3
- package/esm/typings/src/errors/utils/getErrorReportUrl.d.ts +6 -0
- package/esm/typings/src/execution/execution-report/ExecutionReportString.d.ts +1 -1
- package/esm/typings/src/llm-providers/openai/openai-models.d.ts +1 -1
- package/esm/typings/src/pipeline/PipelineString.d.ts +1 -1
- package/esm/typings/src/utils/normalization/titleToName.test.d.ts +1 -0
- package/package.json +1 -1
- package/umd/index.umd.js +353 -490
- package/umd/index.umd.js.map +1 -1
- /package/esm/typings/src/conversion/utils/{extractVariables.test.d.ts → extractVariablesFromScript.test.d.ts} +0 -0
- /package/esm/typings/src/conversion/utils/{renameParameter.test.d.ts → removePipelineCommand.test.d.ts} +0 -0
- /package/esm/typings/src/conversion/utils/{titleToName.test.d.ts → renamePipelineParameter.test.d.ts} +0 -0
- /package/esm/typings/src/{conversion/utils → utils/normalization}/titleToName.d.ts +0 -0
package/umd/index.umd.js
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
*
|
|
21
21
|
* @see https://github.com/webgptorg/promptbook
|
|
22
22
|
*/
|
|
23
|
-
var PROMPTBOOK_ENGINE_VERSION = '0.
|
|
23
|
+
var PROMPTBOOK_ENGINE_VERSION = '0.78.1';
|
|
24
24
|
/**
|
|
25
25
|
* TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
|
|
26
26
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
@@ -145,6 +145,87 @@
|
|
|
145
145
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
+
/**
|
|
149
|
+
* Name for the Promptbook
|
|
150
|
+
*
|
|
151
|
+
* TODO: [🗽] Unite branding and make single place for it
|
|
152
|
+
*
|
|
153
|
+
* @public exported from `@promptbook/core`
|
|
154
|
+
*/
|
|
155
|
+
var NAME = "Promptbook";
|
|
156
|
+
/**
|
|
157
|
+
* Email of the responsible person
|
|
158
|
+
*
|
|
159
|
+
* @public exported from `@promptbook/core`
|
|
160
|
+
*/
|
|
161
|
+
var ADMIN_EMAIL = 'me@pavolhejny.com';
|
|
162
|
+
/**
|
|
163
|
+
* Name of the responsible person for the Promptbook on GitHub
|
|
164
|
+
*
|
|
165
|
+
* @public exported from `@promptbook/core`
|
|
166
|
+
*/
|
|
167
|
+
var ADMIN_GITHUB_NAME = 'hejny';
|
|
168
|
+
// <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
|
|
169
|
+
/**
|
|
170
|
+
* The maximum number of iterations for a loops
|
|
171
|
+
*
|
|
172
|
+
* @private within the repository - too low-level in comparison with other `MAX_...`
|
|
173
|
+
*/
|
|
174
|
+
var LOOP_LIMIT = 1000;
|
|
175
|
+
/**
|
|
176
|
+
* Nonce which is used for replacing things in strings
|
|
177
|
+
*
|
|
178
|
+
* @private within the repository
|
|
179
|
+
*/
|
|
180
|
+
var REPLACING_NONCE = 'u$k42k%!V2zo34w7Fu#@QUHYPW';
|
|
181
|
+
/**
|
|
182
|
+
* @@@
|
|
183
|
+
*
|
|
184
|
+
* @private within the repository
|
|
185
|
+
*/
|
|
186
|
+
var RESERVED_PARAMETER_MISSING_VALUE = 'MISSING-' + REPLACING_NONCE;
|
|
187
|
+
/**
|
|
188
|
+
* @@@
|
|
189
|
+
*
|
|
190
|
+
* @private within the repository
|
|
191
|
+
*/
|
|
192
|
+
var RESERVED_PARAMETER_RESTRICTED = 'RESTRICTED-' + REPLACING_NONCE;
|
|
193
|
+
// <- TODO: [🧜♂️]
|
|
194
|
+
/**
|
|
195
|
+
* @@@
|
|
196
|
+
*
|
|
197
|
+
* @public exported from `@promptbook/core`
|
|
198
|
+
*/
|
|
199
|
+
Object.freeze({
|
|
200
|
+
delimiter: ',',
|
|
201
|
+
quoteChar: '"',
|
|
202
|
+
newline: '\n',
|
|
203
|
+
skipEmptyLines: true,
|
|
204
|
+
});
|
|
205
|
+
/**
|
|
206
|
+
* TODO: Extract `constants.ts` from `config.ts`
|
|
207
|
+
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
208
|
+
* TODO: [🧠][🧜♂️] Maybe join remoteUrl and path into single value
|
|
209
|
+
*/
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Make error report URL for the given error
|
|
213
|
+
*
|
|
214
|
+
* @private !!!!!!
|
|
215
|
+
*/
|
|
216
|
+
function getErrorReportUrl(error) {
|
|
217
|
+
var report = {
|
|
218
|
+
title: "\uD83D\uDC1C Error report from ".concat(NAME),
|
|
219
|
+
body: spaceTrim__default["default"](function (block) { return "\n\n\n `".concat(error.name || 'Error', "` has occurred in the [").concat(NAME, "], please look into it @").concat(ADMIN_GITHUB_NAME, ".\n\n ```\n ").concat(block(error.message || '(no error message)'), "\n ```\n\n\n ## More info:\n\n - **Promptbook engine version:** ").concat(PROMPTBOOK_ENGINE_VERSION, "\n - **Book language version:** ").concat(BOOK_LANGUAGE_VERSION, "\n - **Time:** ").concat(new Date().toISOString(), "\n\n <details>\n <summary>Stack trace:</summary>\n\n ## Stack trace:\n\n ```stacktrace\n ").concat(block(error.stack || '(empty)'), "\n ```\n </details>\n\n "); }),
|
|
220
|
+
};
|
|
221
|
+
var reportUrl = new URL("https://github.com/webgptorg/promptbook/issues/new");
|
|
222
|
+
reportUrl.searchParams.set('labels', 'bug');
|
|
223
|
+
reportUrl.searchParams.set('assignees', ADMIN_GITHUB_NAME);
|
|
224
|
+
reportUrl.searchParams.set('title', report.title);
|
|
225
|
+
reportUrl.searchParams.set('body', report.body);
|
|
226
|
+
return reportUrl;
|
|
227
|
+
}
|
|
228
|
+
|
|
148
229
|
/**
|
|
149
230
|
* This error type indicates that the error should not happen and its last check before crashing with some other error
|
|
150
231
|
*
|
|
@@ -153,7 +234,7 @@
|
|
|
153
234
|
var UnexpectedError = /** @class */ (function (_super) {
|
|
154
235
|
__extends(UnexpectedError, _super);
|
|
155
236
|
function UnexpectedError(message) {
|
|
156
|
-
var _this = _super.call(this, spaceTrim$1.spaceTrim(function (block) { return "\n ".concat(block(message), "\n\n Note: This error should not happen.\n It's probbably a bug in the pipeline collection\n\n Please report issue:\n
|
|
237
|
+
var _this = _super.call(this, spaceTrim$1.spaceTrim(function (block) { return "\n ".concat(block(message), "\n\n Note: This error should not happen.\n It's probbably a bug in the pipeline collection\n\n Please report issue:\n ").concat(block(getErrorReportUrl(new Error(message)).href), "\n\n Or contact us on ").concat(ADMIN_EMAIL, "\n\n "); })) || this;
|
|
157
238
|
_this.name = 'UnexpectedError';
|
|
158
239
|
Object.setPrototypeOf(_this, UnexpectedError.prototype);
|
|
159
240
|
return _this;
|
|
@@ -225,6 +306,48 @@
|
|
|
225
306
|
* TODO: [🌺] Use some intermediate util splitWords
|
|
226
307
|
*/
|
|
227
308
|
|
|
309
|
+
/**
|
|
310
|
+
* Removes emojis from a string and fix whitespaces
|
|
311
|
+
*
|
|
312
|
+
* @param text with emojis
|
|
313
|
+
* @returns text without emojis
|
|
314
|
+
* @public exported from `@promptbook/utils`
|
|
315
|
+
*/
|
|
316
|
+
function removeEmojis(text) {
|
|
317
|
+
// Replace emojis (and also ZWJ sequence) with hyphens
|
|
318
|
+
text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
|
|
319
|
+
text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
|
|
320
|
+
text = text.replace(/(\p{Extended_Pictographic})(\u{200D}\p{Extended_Pictographic})*/gu, '$1');
|
|
321
|
+
text = text.replace(/\p{Extended_Pictographic}/gu, '');
|
|
322
|
+
return text;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Tests if given string is valid URL.
|
|
327
|
+
*
|
|
328
|
+
* Note: This does not check if the file exists only if the path is valid
|
|
329
|
+
* @public exported from `@promptbook/utils`
|
|
330
|
+
*/
|
|
331
|
+
function isValidFilePath(filename) {
|
|
332
|
+
if (typeof filename !== 'string') {
|
|
333
|
+
return false;
|
|
334
|
+
}
|
|
335
|
+
var filenameSlashes = filename.split('\\').join('/');
|
|
336
|
+
// Absolute Unix path: /hello.txt
|
|
337
|
+
if (/^(\/)/i.test(filenameSlashes)) {
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
// Absolute Windows path: /hello.txt
|
|
341
|
+
if (/^([A-Z]{1,2}:\/?)\//i.test(filenameSlashes)) {
|
|
342
|
+
return true;
|
|
343
|
+
}
|
|
344
|
+
// Relative path: ./hello.txt
|
|
345
|
+
if (/^(\.\.?\/)+/i.test(filenameSlashes)) {
|
|
346
|
+
return true;
|
|
347
|
+
}
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
|
|
228
351
|
/**
|
|
229
352
|
* Tests if given string is valid URL.
|
|
230
353
|
*
|
|
@@ -577,48 +700,6 @@
|
|
|
577
700
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
578
701
|
*/
|
|
579
702
|
|
|
580
|
-
/**
|
|
581
|
-
* Removes emojis from a string and fix whitespaces
|
|
582
|
-
*
|
|
583
|
-
* @param text with emojis
|
|
584
|
-
* @returns text without emojis
|
|
585
|
-
* @public exported from `@promptbook/utils`
|
|
586
|
-
*/
|
|
587
|
-
function removeEmojis(text) {
|
|
588
|
-
// Replace emojis (and also ZWJ sequence) with hyphens
|
|
589
|
-
text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
|
|
590
|
-
text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
|
|
591
|
-
text = text.replace(/(\p{Extended_Pictographic})(\u{200D}\p{Extended_Pictographic})*/gu, '$1');
|
|
592
|
-
text = text.replace(/\p{Extended_Pictographic}/gu, '');
|
|
593
|
-
return text;
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
/**
|
|
597
|
-
* Tests if given string is valid URL.
|
|
598
|
-
*
|
|
599
|
-
* Note: This does not check if the file exists only if the path is valid
|
|
600
|
-
* @public exported from `@promptbook/utils`
|
|
601
|
-
*/
|
|
602
|
-
function isValidFilePath(filename) {
|
|
603
|
-
if (typeof filename !== 'string') {
|
|
604
|
-
return false;
|
|
605
|
-
}
|
|
606
|
-
var filenameSlashes = filename.split('\\').join('/');
|
|
607
|
-
// Absolute Unix path: /hello.txt
|
|
608
|
-
if (/^(\/)/i.test(filenameSlashes)) {
|
|
609
|
-
return true;
|
|
610
|
-
}
|
|
611
|
-
// Absolute Windows path: /hello.txt
|
|
612
|
-
if (/^([A-Z]{1,2}:\/?)\//i.test(filenameSlashes)) {
|
|
613
|
-
return true;
|
|
614
|
-
}
|
|
615
|
-
// Relative path: ./hello.txt
|
|
616
|
-
if (/^(\.\.?\/)+/i.test(filenameSlashes)) {
|
|
617
|
-
return true;
|
|
618
|
-
}
|
|
619
|
-
return false;
|
|
620
|
-
}
|
|
621
|
-
|
|
622
703
|
/**
|
|
623
704
|
* @@@
|
|
624
705
|
*
|
|
@@ -707,34 +788,6 @@
|
|
|
707
788
|
* TODO: [🕌] When more than 2 functionalities, split into separate functions
|
|
708
789
|
*/
|
|
709
790
|
|
|
710
|
-
/**
|
|
711
|
-
* Parses the task and returns the list of all parameter names
|
|
712
|
-
*
|
|
713
|
-
* @param template the string template with parameters in {curly} braces
|
|
714
|
-
* @returns the list of parameter names
|
|
715
|
-
* @public exported from `@promptbook/utils`
|
|
716
|
-
*/
|
|
717
|
-
function extractParameterNames(template) {
|
|
718
|
-
var e_1, _a;
|
|
719
|
-
var matches = template.matchAll(/{\w+}/g);
|
|
720
|
-
var parameterNames = new Set();
|
|
721
|
-
try {
|
|
722
|
-
for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
|
|
723
|
-
var match = matches_1_1.value;
|
|
724
|
-
var parameterName = match[0].slice(1, -1);
|
|
725
|
-
parameterNames.add(parameterName);
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
729
|
-
finally {
|
|
730
|
-
try {
|
|
731
|
-
if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
|
|
732
|
-
}
|
|
733
|
-
finally { if (e_1) throw e_1.error; }
|
|
734
|
-
}
|
|
735
|
-
return parameterNames;
|
|
736
|
-
}
|
|
737
|
-
|
|
738
791
|
/**
|
|
739
792
|
* This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
|
|
740
793
|
*
|
|
@@ -760,10 +813,11 @@
|
|
|
760
813
|
* @param script from which to extract the variables
|
|
761
814
|
* @returns the list of variable names
|
|
762
815
|
* @throws {ParseError} if the script is invalid
|
|
763
|
-
* @public exported from `@promptbook/utils`
|
|
816
|
+
* @public exported from `@promptbook/utils` <- Note: [👖] This is usable elsewhere than in Promptbook, so keeping in utils
|
|
764
817
|
*/
|
|
765
|
-
function
|
|
818
|
+
function extractVariablesFromScript(script) {
|
|
766
819
|
var variables = new Set();
|
|
820
|
+
var originalScript = script;
|
|
767
821
|
script = "(()=>{".concat(script, "})()");
|
|
768
822
|
try {
|
|
769
823
|
for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
|
|
@@ -795,7 +849,9 @@
|
|
|
795
849
|
if (!(error instanceof Error)) {
|
|
796
850
|
throw error;
|
|
797
851
|
}
|
|
798
|
-
throw new ParseError(spaceTrim$1.spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.toString()), "}\n
|
|
852
|
+
throw new ParseError(spaceTrim$1.spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.toString()), "}\n\n\n Found variables:\n\n ").concat(Array.from(variables)
|
|
853
|
+
.map(function (variableName, i) { return "".concat(i + 1, ") ").concat(variableName); })
|
|
854
|
+
.join('\n'), "\n\n\n The script:\n\n ```javascript\n ").concat(block(originalScript), "\n ```\n "); }));
|
|
799
855
|
}
|
|
800
856
|
return variables;
|
|
801
857
|
}
|
|
@@ -804,211 +860,55 @@
|
|
|
804
860
|
*/
|
|
805
861
|
|
|
806
862
|
/**
|
|
807
|
-
*
|
|
863
|
+
* This error indicates problems parsing the format value
|
|
808
864
|
*
|
|
809
|
-
*
|
|
810
|
-
*
|
|
811
|
-
*
|
|
812
|
-
* @public exported from `@promptbook/
|
|
865
|
+
* For example, when the format value is not a valid JSON or CSV
|
|
866
|
+
* This is not thrown directly but in extended classes
|
|
867
|
+
*
|
|
868
|
+
* @public exported from `@promptbook/core`
|
|
813
869
|
*/
|
|
814
|
-
function
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
}
|
|
823
|
-
}
|
|
824
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
825
|
-
finally {
|
|
826
|
-
try {
|
|
827
|
-
if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
|
|
828
|
-
}
|
|
829
|
-
finally { if (e_1) throw e_1.error; }
|
|
830
|
-
}
|
|
831
|
-
if (taskType === 'SCRIPT_TASK') {
|
|
832
|
-
try {
|
|
833
|
-
for (var _g = __values(extractVariables(content)), _h = _g.next(); !_h.done; _h = _g.next()) {
|
|
834
|
-
var parameterName = _h.value;
|
|
835
|
-
parameterNames.add(parameterName);
|
|
836
|
-
}
|
|
837
|
-
}
|
|
838
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
839
|
-
finally {
|
|
840
|
-
try {
|
|
841
|
-
if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
|
|
842
|
-
}
|
|
843
|
-
finally { if (e_2) throw e_2.error; }
|
|
844
|
-
}
|
|
845
|
-
}
|
|
846
|
-
try {
|
|
847
|
-
for (var _j = __values(jokerParameterNames || []), _k = _j.next(); !_k.done; _k = _j.next()) {
|
|
848
|
-
var jokerName = _k.value;
|
|
849
|
-
parameterNames.add(jokerName);
|
|
850
|
-
}
|
|
851
|
-
}
|
|
852
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
853
|
-
finally {
|
|
854
|
-
try {
|
|
855
|
-
if (_k && !_k.done && (_c = _j.return)) _c.call(_j);
|
|
856
|
-
}
|
|
857
|
-
finally { if (e_3) throw e_3.error; }
|
|
858
|
-
}
|
|
859
|
-
parameterNames.delete('content');
|
|
860
|
-
// <- Note {websiteContent} is used in `preparedContent`
|
|
861
|
-
// Note: [🍭] Fixing dependent subparameterName from FOREACH command
|
|
862
|
-
if (foreach !== undefined) {
|
|
863
|
-
try {
|
|
864
|
-
for (var _l = __values(foreach.inputSubparameterNames), _m = _l.next(); !_m.done; _m = _l.next()) {
|
|
865
|
-
var subparameterName = _m.value;
|
|
866
|
-
if (parameterNames.has(subparameterName)) {
|
|
867
|
-
parameterNames.delete(subparameterName);
|
|
868
|
-
parameterNames.add(foreach.parameterName);
|
|
869
|
-
// <- TODO: [🚎] Warn/logic error when `subparameterName` not used
|
|
870
|
-
}
|
|
871
|
-
}
|
|
872
|
-
}
|
|
873
|
-
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
874
|
-
finally {
|
|
875
|
-
try {
|
|
876
|
-
if (_m && !_m.done && (_d = _l.return)) _d.call(_l);
|
|
877
|
-
}
|
|
878
|
-
finally { if (e_4) throw e_4.error; }
|
|
879
|
-
}
|
|
870
|
+
var AbstractFormatError = /** @class */ (function (_super) {
|
|
871
|
+
__extends(AbstractFormatError, _super);
|
|
872
|
+
// Note: To allow instanceof do not put here error `name`
|
|
873
|
+
// public readonly name = 'AbstractFormatError';
|
|
874
|
+
function AbstractFormatError(message) {
|
|
875
|
+
var _this = _super.call(this, message) || this;
|
|
876
|
+
Object.setPrototypeOf(_this, AbstractFormatError.prototype);
|
|
877
|
+
return _this;
|
|
880
878
|
}
|
|
881
|
-
return
|
|
882
|
-
}
|
|
879
|
+
return AbstractFormatError;
|
|
880
|
+
}(Error));
|
|
881
|
+
|
|
883
882
|
/**
|
|
884
|
-
*
|
|
883
|
+
* This error indicates problem with parsing of CSV
|
|
884
|
+
*
|
|
885
|
+
* @public exported from `@promptbook/core`
|
|
885
886
|
*/
|
|
887
|
+
var CsvFormatError = /** @class */ (function (_super) {
|
|
888
|
+
__extends(CsvFormatError, _super);
|
|
889
|
+
function CsvFormatError(message) {
|
|
890
|
+
var _this = _super.call(this, message) || this;
|
|
891
|
+
_this.name = 'CsvFormatError';
|
|
892
|
+
Object.setPrototypeOf(_this, CsvFormatError.prototype);
|
|
893
|
+
return _this;
|
|
894
|
+
}
|
|
895
|
+
return CsvFormatError;
|
|
896
|
+
}(AbstractFormatError));
|
|
886
897
|
|
|
887
898
|
/**
|
|
888
|
-
* This error indicates that the
|
|
899
|
+
* This error indicates that the pipeline collection cannot be propperly loaded
|
|
889
900
|
*
|
|
890
901
|
* @public exported from `@promptbook/core`
|
|
891
902
|
*/
|
|
892
|
-
var
|
|
893
|
-
__extends(
|
|
894
|
-
function
|
|
903
|
+
var CollectionError = /** @class */ (function (_super) {
|
|
904
|
+
__extends(CollectionError, _super);
|
|
905
|
+
function CollectionError(message) {
|
|
895
906
|
var _this = _super.call(this, message) || this;
|
|
896
|
-
_this.name = '
|
|
897
|
-
Object.setPrototypeOf(_this,
|
|
907
|
+
_this.name = 'CollectionError';
|
|
908
|
+
Object.setPrototypeOf(_this, CollectionError.prototype);
|
|
898
909
|
return _this;
|
|
899
910
|
}
|
|
900
|
-
return
|
|
901
|
-
}(Error));
|
|
902
|
-
|
|
903
|
-
/**
|
|
904
|
-
* Function `renameParameter` will find all usable parameters for given task
|
|
905
|
-
* In other words, it will find all parameters that are not used in the task itseld and all its dependencies
|
|
906
|
-
*
|
|
907
|
-
* @throws {PipelineLogicError} If the new parameter name is already used in the pipeline
|
|
908
|
-
* @public exported from `@promptbook/utils`
|
|
909
|
-
*/
|
|
910
|
-
function renameParameter(options) {
|
|
911
|
-
var e_1, _a, e_2, _b;
|
|
912
|
-
var pipeline = options.pipeline, oldParameterName = options.oldParameterName, newParameterName = options.newParameterName;
|
|
913
|
-
if (pipeline.parameters.some(function (parameter) { return parameter.name === newParameterName; })) {
|
|
914
|
-
throw new PipelineLogicError("Can not replace {".concat(oldParameterName, "} to {").concat(newParameterName, "} because {").concat(newParameterName, "} is already used in the pipeline"));
|
|
915
|
-
}
|
|
916
|
-
var renamedPipeline = __assign(__assign({}, pipeline), {
|
|
917
|
-
// <- TODO: [🪓] This should be without `as $PipelineJson`
|
|
918
|
-
parameters: __spreadArray([], __read(pipeline.parameters), false), tasks: __spreadArray([], __read(pipeline.tasks), false) });
|
|
919
|
-
try {
|
|
920
|
-
for (var _c = __values(renamedPipeline.parameters), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
921
|
-
var parameter = _d.value;
|
|
922
|
-
if (parameter.name !== oldParameterName) {
|
|
923
|
-
continue;
|
|
924
|
-
}
|
|
925
|
-
parameter.name = newParameterName;
|
|
926
|
-
}
|
|
927
|
-
}
|
|
928
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
929
|
-
finally {
|
|
930
|
-
try {
|
|
931
|
-
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
932
|
-
}
|
|
933
|
-
finally { if (e_1) throw e_1.error; }
|
|
934
|
-
}
|
|
935
|
-
try {
|
|
936
|
-
for (var _e = __values(renamedPipeline.tasks), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
937
|
-
var task = _f.value;
|
|
938
|
-
if (task.resultingParameterName === oldParameterName) {
|
|
939
|
-
task.resultingParameterName = newParameterName;
|
|
940
|
-
}
|
|
941
|
-
task.dependentParameterNames = task.dependentParameterNames.map(function (dependentParameterName) {
|
|
942
|
-
return dependentParameterName === oldParameterName ? newParameterName : dependentParameterName;
|
|
943
|
-
});
|
|
944
|
-
task.content = task.content.replace(new RegExp("{".concat(oldParameterName, "}"), 'g'), "{".concat(newParameterName, "}"));
|
|
945
|
-
task.title = task.title.replace(new RegExp("{".concat(oldParameterName, "}"), 'g'), "{".concat(newParameterName, "}"));
|
|
946
|
-
task.description =
|
|
947
|
-
task.description === undefined
|
|
948
|
-
? undefined
|
|
949
|
-
: task.description.replace(new RegExp("{".concat(oldParameterName, "}"), 'g'), "{".concat(newParameterName, "}"));
|
|
950
|
-
}
|
|
951
|
-
}
|
|
952
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
953
|
-
finally {
|
|
954
|
-
try {
|
|
955
|
-
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
956
|
-
}
|
|
957
|
-
finally { if (e_2) throw e_2.error; }
|
|
958
|
-
}
|
|
959
|
-
return renamedPipeline;
|
|
960
|
-
}
|
|
961
|
-
|
|
962
|
-
/**
|
|
963
|
-
* This error indicates problems parsing the format value
|
|
964
|
-
*
|
|
965
|
-
* For example, when the format value is not a valid JSON or CSV
|
|
966
|
-
* This is not thrown directly but in extended classes
|
|
967
|
-
*
|
|
968
|
-
* @public exported from `@promptbook/core`
|
|
969
|
-
*/
|
|
970
|
-
var AbstractFormatError = /** @class */ (function (_super) {
|
|
971
|
-
__extends(AbstractFormatError, _super);
|
|
972
|
-
// Note: To allow instanceof do not put here error `name`
|
|
973
|
-
// public readonly name = 'AbstractFormatError';
|
|
974
|
-
function AbstractFormatError(message) {
|
|
975
|
-
var _this = _super.call(this, message) || this;
|
|
976
|
-
Object.setPrototypeOf(_this, AbstractFormatError.prototype);
|
|
977
|
-
return _this;
|
|
978
|
-
}
|
|
979
|
-
return AbstractFormatError;
|
|
980
|
-
}(Error));
|
|
981
|
-
|
|
982
|
-
/**
|
|
983
|
-
* This error indicates problem with parsing of CSV
|
|
984
|
-
*
|
|
985
|
-
* @public exported from `@promptbook/core`
|
|
986
|
-
*/
|
|
987
|
-
var CsvFormatError = /** @class */ (function (_super) {
|
|
988
|
-
__extends(CsvFormatError, _super);
|
|
989
|
-
function CsvFormatError(message) {
|
|
990
|
-
var _this = _super.call(this, message) || this;
|
|
991
|
-
_this.name = 'CsvFormatError';
|
|
992
|
-
Object.setPrototypeOf(_this, CsvFormatError.prototype);
|
|
993
|
-
return _this;
|
|
994
|
-
}
|
|
995
|
-
return CsvFormatError;
|
|
996
|
-
}(AbstractFormatError));
|
|
997
|
-
|
|
998
|
-
/**
|
|
999
|
-
* This error indicates that the pipeline collection cannot be propperly loaded
|
|
1000
|
-
*
|
|
1001
|
-
* @public exported from `@promptbook/core`
|
|
1002
|
-
*/
|
|
1003
|
-
var CollectionError = /** @class */ (function (_super) {
|
|
1004
|
-
__extends(CollectionError, _super);
|
|
1005
|
-
function CollectionError(message) {
|
|
1006
|
-
var _this = _super.call(this, message) || this;
|
|
1007
|
-
_this.name = 'CollectionError';
|
|
1008
|
-
Object.setPrototypeOf(_this, CollectionError.prototype);
|
|
1009
|
-
return _this;
|
|
1010
|
-
}
|
|
1011
|
-
return CollectionError;
|
|
911
|
+
return CollectionError;
|
|
1012
912
|
}(Error));
|
|
1013
913
|
|
|
1014
914
|
/**
|
|
@@ -1142,6 +1042,22 @@
|
|
|
1142
1042
|
return PipelineExecutionError;
|
|
1143
1043
|
}(Error));
|
|
1144
1044
|
|
|
1045
|
+
/**
|
|
1046
|
+
* This error indicates that the promptbook object has valid syntax (=can be parsed) but contains logical errors (like circular dependencies)
|
|
1047
|
+
*
|
|
1048
|
+
* @public exported from `@promptbook/core`
|
|
1049
|
+
*/
|
|
1050
|
+
var PipelineLogicError = /** @class */ (function (_super) {
|
|
1051
|
+
__extends(PipelineLogicError, _super);
|
|
1052
|
+
function PipelineLogicError(message) {
|
|
1053
|
+
var _this = _super.call(this, message) || this;
|
|
1054
|
+
_this.name = 'PipelineLogicError';
|
|
1055
|
+
Object.setPrototypeOf(_this, PipelineLogicError.prototype);
|
|
1056
|
+
return _this;
|
|
1057
|
+
}
|
|
1058
|
+
return PipelineLogicError;
|
|
1059
|
+
}(Error));
|
|
1060
|
+
|
|
1145
1061
|
/**
|
|
1146
1062
|
* This error indicates errors in referencing promptbooks between each other
|
|
1147
1063
|
*
|
|
@@ -1762,240 +1678,32 @@
|
|
|
1762
1678
|
var spaceTrim = spaceTrim$1.spaceTrim;
|
|
1763
1679
|
|
|
1764
1680
|
/**
|
|
1765
|
-
*
|
|
1766
|
-
*
|
|
1767
|
-
* Note: `$` is used to indicate that this function is not a pure function - it mutates given object
|
|
1768
|
-
* Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
|
|
1681
|
+
* Parses the task and returns the list of all parameter names
|
|
1769
1682
|
*
|
|
1770
|
-
* @
|
|
1683
|
+
* @param template the string template with parameters in {curly} braces
|
|
1684
|
+
* @returns the list of parameter names
|
|
1771
1685
|
* @public exported from `@promptbook/utils`
|
|
1772
1686
|
*/
|
|
1773
|
-
function
|
|
1687
|
+
function extractParameterNames(template) {
|
|
1774
1688
|
var e_1, _a;
|
|
1775
|
-
var
|
|
1689
|
+
var matches = template.matchAll(/{\w+}/g);
|
|
1690
|
+
var parameterNames = new Set();
|
|
1776
1691
|
try {
|
|
1777
|
-
for (var
|
|
1778
|
-
var
|
|
1779
|
-
var
|
|
1780
|
-
|
|
1781
|
-
$deepFreeze(value);
|
|
1782
|
-
}
|
|
1692
|
+
for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
|
|
1693
|
+
var match = matches_1_1.value;
|
|
1694
|
+
var parameterName = match[0].slice(1, -1);
|
|
1695
|
+
parameterNames.add(parameterName);
|
|
1783
1696
|
}
|
|
1784
1697
|
}
|
|
1785
1698
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1786
1699
|
finally {
|
|
1787
1700
|
try {
|
|
1788
|
-
if (
|
|
1701
|
+
if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
|
|
1789
1702
|
}
|
|
1790
1703
|
finally { if (e_1) throw e_1.error; }
|
|
1791
1704
|
}
|
|
1792
|
-
return
|
|
1793
|
-
}
|
|
1794
|
-
/**
|
|
1795
|
-
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
1796
|
-
*/
|
|
1797
|
-
|
|
1798
|
-
/**
|
|
1799
|
-
* Checks if the value is [🚉] serializable as JSON
|
|
1800
|
-
* If not, throws an UnexpectedError with a rich error message and tracking
|
|
1801
|
-
*
|
|
1802
|
-
* - Almost all primitives are serializable BUT:
|
|
1803
|
-
* - `undefined` is not serializable
|
|
1804
|
-
* - `NaN` is not serializable
|
|
1805
|
-
* - Objects and arrays are serializable if all their properties are serializable
|
|
1806
|
-
* - Functions are not serializable
|
|
1807
|
-
* - Circular references are not serializable
|
|
1808
|
-
* - `Date` objects are not serializable
|
|
1809
|
-
* - `Map` and `Set` objects are not serializable
|
|
1810
|
-
* - `RegExp` objects are not serializable
|
|
1811
|
-
* - `Error` objects are not serializable
|
|
1812
|
-
* - `Symbol` objects are not serializable
|
|
1813
|
-
* - And much more...
|
|
1814
|
-
*
|
|
1815
|
-
* @throws UnexpectedError if the value is not serializable as JSON
|
|
1816
|
-
* @public exported from `@promptbook/utils`
|
|
1817
|
-
*/
|
|
1818
|
-
function checkSerializableAsJson(name, value) {
|
|
1819
|
-
var e_1, _a;
|
|
1820
|
-
if (value === undefined) {
|
|
1821
|
-
throw new UnexpectedError("".concat(name, " is undefined"));
|
|
1822
|
-
}
|
|
1823
|
-
else if (value === null) {
|
|
1824
|
-
return;
|
|
1825
|
-
}
|
|
1826
|
-
else if (typeof value === 'boolean') {
|
|
1827
|
-
return;
|
|
1828
|
-
}
|
|
1829
|
-
else if (typeof value === 'number' && !isNaN(value)) {
|
|
1830
|
-
return;
|
|
1831
|
-
}
|
|
1832
|
-
else if (typeof value === 'string') {
|
|
1833
|
-
return;
|
|
1834
|
-
}
|
|
1835
|
-
else if (typeof value === 'symbol') {
|
|
1836
|
-
throw new UnexpectedError("".concat(name, " is symbol"));
|
|
1837
|
-
}
|
|
1838
|
-
else if (typeof value === 'function') {
|
|
1839
|
-
throw new UnexpectedError("".concat(name, " is function"));
|
|
1840
|
-
}
|
|
1841
|
-
else if (typeof value === 'object' && Array.isArray(value)) {
|
|
1842
|
-
for (var i = 0; i < value.length; i++) {
|
|
1843
|
-
checkSerializableAsJson("".concat(name, "[").concat(i, "]"), value[i]);
|
|
1844
|
-
}
|
|
1845
|
-
}
|
|
1846
|
-
else if (typeof value === 'object') {
|
|
1847
|
-
if (value instanceof Date) {
|
|
1848
|
-
throw new UnexpectedError(spaceTrim__default["default"]("\n ".concat(name, " is Date\n\n Use `string_date_iso8601` instead\n ")));
|
|
1849
|
-
}
|
|
1850
|
-
else if (value instanceof Map) {
|
|
1851
|
-
throw new UnexpectedError("".concat(name, " is Map"));
|
|
1852
|
-
}
|
|
1853
|
-
else if (value instanceof Set) {
|
|
1854
|
-
throw new UnexpectedError("".concat(name, " is Set"));
|
|
1855
|
-
}
|
|
1856
|
-
else if (value instanceof RegExp) {
|
|
1857
|
-
throw new UnexpectedError("".concat(name, " is RegExp"));
|
|
1858
|
-
}
|
|
1859
|
-
else if (value instanceof Error) {
|
|
1860
|
-
throw new UnexpectedError(spaceTrim__default["default"]("\n ".concat(name, " is unserialized Error\n\n Use function `serializeError`\n ")));
|
|
1861
|
-
}
|
|
1862
|
-
else {
|
|
1863
|
-
try {
|
|
1864
|
-
for (var _b = __values(Object.entries(value)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
1865
|
-
var _d = __read(_c.value, 2), subName = _d[0], subValue = _d[1];
|
|
1866
|
-
if (subValue === undefined) {
|
|
1867
|
-
// Note: undefined in object is serializable - it is just omited
|
|
1868
|
-
continue;
|
|
1869
|
-
}
|
|
1870
|
-
checkSerializableAsJson("".concat(name, ".").concat(subName), subValue);
|
|
1871
|
-
}
|
|
1872
|
-
}
|
|
1873
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1874
|
-
finally {
|
|
1875
|
-
try {
|
|
1876
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
1877
|
-
}
|
|
1878
|
-
finally { if (e_1) throw e_1.error; }
|
|
1879
|
-
}
|
|
1880
|
-
try {
|
|
1881
|
-
JSON.stringify(value); // <- TODO: [0]
|
|
1882
|
-
}
|
|
1883
|
-
catch (error) {
|
|
1884
|
-
if (!(error instanceof Error)) {
|
|
1885
|
-
throw error;
|
|
1886
|
-
}
|
|
1887
|
-
throw new UnexpectedError(spaceTrim__default["default"](function (block) { return "\n ".concat(name, " is not serializable\n\n ").concat(block(error.toString()), "\n "); }));
|
|
1888
|
-
}
|
|
1889
|
-
/*
|
|
1890
|
-
TODO: [0] Is there some more elegant way to check circular references?
|
|
1891
|
-
const seen = new Set();
|
|
1892
|
-
const stack = [{ value }];
|
|
1893
|
-
while (stack.length > 0) {
|
|
1894
|
-
const { value } = stack.pop()!;
|
|
1895
|
-
if (typeof value === 'object' && value !== null) {
|
|
1896
|
-
if (seen.has(value)) {
|
|
1897
|
-
throw new UnexpectedError(`${name} has circular reference`);
|
|
1898
|
-
}
|
|
1899
|
-
seen.add(value);
|
|
1900
|
-
if (Array.isArray(value)) {
|
|
1901
|
-
stack.push(...value.map((value) => ({ value })));
|
|
1902
|
-
} else {
|
|
1903
|
-
stack.push(...Object.values(value).map((value) => ({ value })));
|
|
1904
|
-
}
|
|
1905
|
-
}
|
|
1906
|
-
}
|
|
1907
|
-
*/
|
|
1908
|
-
return;
|
|
1909
|
-
}
|
|
1910
|
-
}
|
|
1911
|
-
else {
|
|
1912
|
-
throw new UnexpectedError("".concat(name, " is unknown"));
|
|
1913
|
-
}
|
|
1914
|
-
}
|
|
1915
|
-
/**
|
|
1916
|
-
* TODO: [🧠][🛣] More elegant way to tracking than passing `name`
|
|
1917
|
-
* TODO: [🧠][main] !!! In-memory cache of same values to prevent multiple checks
|
|
1918
|
-
* Note: [🐠] This is how `checkSerializableAsJson` + `isSerializableAsJson` together can just retun true/false or rich error message
|
|
1919
|
-
*/
|
|
1920
|
-
|
|
1921
|
-
/**
|
|
1922
|
-
* @@@
|
|
1923
|
-
* @@@
|
|
1924
|
-
*
|
|
1925
|
-
* Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
|
|
1926
|
-
*
|
|
1927
|
-
* @param name - Name of the object for debugging purposes
|
|
1928
|
-
* @param objectValue - Object to be deeply frozen
|
|
1929
|
-
* @returns The same object as the input, but deeply frozen
|
|
1930
|
-
* @private this is in comparison to `deepFreeze` a more specific utility and maybe not very good practice to use without specific reason and considerations
|
|
1931
|
-
*/
|
|
1932
|
-
function $asDeeplyFrozenSerializableJson(name, objectValue) {
|
|
1933
|
-
checkSerializableAsJson(name, objectValue);
|
|
1934
|
-
return $deepFreeze(objectValue);
|
|
1705
|
+
return parameterNames;
|
|
1935
1706
|
}
|
|
1936
|
-
/**
|
|
1937
|
-
* TODO: [🧠][🛣] More elegant way to tracking than passing `name`
|
|
1938
|
-
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
1939
|
-
*/
|
|
1940
|
-
|
|
1941
|
-
// <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
|
|
1942
|
-
/**
|
|
1943
|
-
* The maximum number of iterations for a loops
|
|
1944
|
-
*
|
|
1945
|
-
* @private within the repository - too low-level in comparison with other `MAX_...`
|
|
1946
|
-
*/
|
|
1947
|
-
var LOOP_LIMIT = 1000;
|
|
1948
|
-
/**
|
|
1949
|
-
* Nonce which is used for replacing things in strings
|
|
1950
|
-
*
|
|
1951
|
-
* @private within the repository
|
|
1952
|
-
*/
|
|
1953
|
-
var REPLACING_NONCE = 'u$k42k%!V2zo34w7Fu#@QUHYPW';
|
|
1954
|
-
/**
|
|
1955
|
-
* The names of the parameters that are reserved for special purposes
|
|
1956
|
-
*
|
|
1957
|
-
* @public exported from `@promptbook/core`
|
|
1958
|
-
*/
|
|
1959
|
-
$asDeeplyFrozenSerializableJson('RESERVED_PARAMETER_NAMES', [
|
|
1960
|
-
'content',
|
|
1961
|
-
'context',
|
|
1962
|
-
'knowledge',
|
|
1963
|
-
'examples',
|
|
1964
|
-
'modelName',
|
|
1965
|
-
'currentDate',
|
|
1966
|
-
// <- TODO: list here all command names
|
|
1967
|
-
// <- TODO: Add more like 'date', 'modelName',...
|
|
1968
|
-
// <- TODO: Add [emoji] + instructions ACRY when adding new reserved parameter
|
|
1969
|
-
]);
|
|
1970
|
-
/**
|
|
1971
|
-
* @@@
|
|
1972
|
-
*
|
|
1973
|
-
* @private within the repository
|
|
1974
|
-
*/
|
|
1975
|
-
var RESERVED_PARAMETER_MISSING_VALUE = 'MISSING-' + REPLACING_NONCE;
|
|
1976
|
-
/**
|
|
1977
|
-
* @@@
|
|
1978
|
-
*
|
|
1979
|
-
* @private within the repository
|
|
1980
|
-
*/
|
|
1981
|
-
var RESERVED_PARAMETER_RESTRICTED = 'RESTRICTED-' + REPLACING_NONCE;
|
|
1982
|
-
// <- TODO: [🧜♂️]
|
|
1983
|
-
/**
|
|
1984
|
-
* @@@
|
|
1985
|
-
*
|
|
1986
|
-
* @public exported from `@promptbook/core`
|
|
1987
|
-
*/
|
|
1988
|
-
Object.freeze({
|
|
1989
|
-
delimiter: ',',
|
|
1990
|
-
quoteChar: '"',
|
|
1991
|
-
newline: '\n',
|
|
1992
|
-
skipEmptyLines: true,
|
|
1993
|
-
});
|
|
1994
|
-
/**
|
|
1995
|
-
* TODO: Extract `constants.ts` from `config.ts`
|
|
1996
|
-
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
1997
|
-
* TODO: [🧠][🧜♂️] Maybe join remoteUrl and path into single value
|
|
1998
|
-
*/
|
|
1999
1707
|
|
|
2000
1708
|
/**
|
|
2001
1709
|
* Replaces parameters in template with values from parameters object
|
|
@@ -2178,6 +1886,163 @@
|
|
|
2178
1886
|
return text;
|
|
2179
1887
|
}
|
|
2180
1888
|
|
|
1889
|
+
/**
|
|
1890
|
+
* @@@
|
|
1891
|
+
*
|
|
1892
|
+
* Note: `$` is used to indicate that this function is not a pure function - it mutates given object
|
|
1893
|
+
* Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
|
|
1894
|
+
*
|
|
1895
|
+
* @returns The same object as the input, but deeply frozen
|
|
1896
|
+
* @public exported from `@promptbook/utils`
|
|
1897
|
+
*/
|
|
1898
|
+
function $deepFreeze(objectValue) {
|
|
1899
|
+
var e_1, _a;
|
|
1900
|
+
var propertyNames = Object.getOwnPropertyNames(objectValue);
|
|
1901
|
+
try {
|
|
1902
|
+
for (var propertyNames_1 = __values(propertyNames), propertyNames_1_1 = propertyNames_1.next(); !propertyNames_1_1.done; propertyNames_1_1 = propertyNames_1.next()) {
|
|
1903
|
+
var propertyName = propertyNames_1_1.value;
|
|
1904
|
+
var value = objectValue[propertyName];
|
|
1905
|
+
if (value && typeof value === 'object') {
|
|
1906
|
+
$deepFreeze(value);
|
|
1907
|
+
}
|
|
1908
|
+
}
|
|
1909
|
+
}
|
|
1910
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1911
|
+
finally {
|
|
1912
|
+
try {
|
|
1913
|
+
if (propertyNames_1_1 && !propertyNames_1_1.done && (_a = propertyNames_1.return)) _a.call(propertyNames_1);
|
|
1914
|
+
}
|
|
1915
|
+
finally { if (e_1) throw e_1.error; }
|
|
1916
|
+
}
|
|
1917
|
+
return Object.freeze(objectValue);
|
|
1918
|
+
}
|
|
1919
|
+
/**
|
|
1920
|
+
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
1921
|
+
*/
|
|
1922
|
+
|
|
1923
|
+
/**
|
|
1924
|
+
* Checks if the value is [🚉] serializable as JSON
|
|
1925
|
+
* If not, throws an UnexpectedError with a rich error message and tracking
|
|
1926
|
+
*
|
|
1927
|
+
* - Almost all primitives are serializable BUT:
|
|
1928
|
+
* - `undefined` is not serializable
|
|
1929
|
+
* - `NaN` is not serializable
|
|
1930
|
+
* - Objects and arrays are serializable if all their properties are serializable
|
|
1931
|
+
* - Functions are not serializable
|
|
1932
|
+
* - Circular references are not serializable
|
|
1933
|
+
* - `Date` objects are not serializable
|
|
1934
|
+
* - `Map` and `Set` objects are not serializable
|
|
1935
|
+
* - `RegExp` objects are not serializable
|
|
1936
|
+
* - `Error` objects are not serializable
|
|
1937
|
+
* - `Symbol` objects are not serializable
|
|
1938
|
+
* - And much more...
|
|
1939
|
+
*
|
|
1940
|
+
* @throws UnexpectedError if the value is not serializable as JSON
|
|
1941
|
+
* @public exported from `@promptbook/utils`
|
|
1942
|
+
*/
|
|
1943
|
+
function checkSerializableAsJson(name, value) {
|
|
1944
|
+
var e_1, _a;
|
|
1945
|
+
if (value === undefined) {
|
|
1946
|
+
throw new UnexpectedError("".concat(name, " is undefined"));
|
|
1947
|
+
}
|
|
1948
|
+
else if (value === null) {
|
|
1949
|
+
return;
|
|
1950
|
+
}
|
|
1951
|
+
else if (typeof value === 'boolean') {
|
|
1952
|
+
return;
|
|
1953
|
+
}
|
|
1954
|
+
else if (typeof value === 'number' && !isNaN(value)) {
|
|
1955
|
+
return;
|
|
1956
|
+
}
|
|
1957
|
+
else if (typeof value === 'string') {
|
|
1958
|
+
return;
|
|
1959
|
+
}
|
|
1960
|
+
else if (typeof value === 'symbol') {
|
|
1961
|
+
throw new UnexpectedError("".concat(name, " is symbol"));
|
|
1962
|
+
}
|
|
1963
|
+
else if (typeof value === 'function') {
|
|
1964
|
+
throw new UnexpectedError("".concat(name, " is function"));
|
|
1965
|
+
}
|
|
1966
|
+
else if (typeof value === 'object' && Array.isArray(value)) {
|
|
1967
|
+
for (var i = 0; i < value.length; i++) {
|
|
1968
|
+
checkSerializableAsJson("".concat(name, "[").concat(i, "]"), value[i]);
|
|
1969
|
+
}
|
|
1970
|
+
}
|
|
1971
|
+
else if (typeof value === 'object') {
|
|
1972
|
+
if (value instanceof Date) {
|
|
1973
|
+
throw new UnexpectedError(spaceTrim__default["default"]("\n ".concat(name, " is Date\n\n Use `string_date_iso8601` instead\n ")));
|
|
1974
|
+
}
|
|
1975
|
+
else if (value instanceof Map) {
|
|
1976
|
+
throw new UnexpectedError("".concat(name, " is Map"));
|
|
1977
|
+
}
|
|
1978
|
+
else if (value instanceof Set) {
|
|
1979
|
+
throw new UnexpectedError("".concat(name, " is Set"));
|
|
1980
|
+
}
|
|
1981
|
+
else if (value instanceof RegExp) {
|
|
1982
|
+
throw new UnexpectedError("".concat(name, " is RegExp"));
|
|
1983
|
+
}
|
|
1984
|
+
else if (value instanceof Error) {
|
|
1985
|
+
throw new UnexpectedError(spaceTrim__default["default"]("\n ".concat(name, " is unserialized Error\n\n Use function `serializeError`\n ")));
|
|
1986
|
+
}
|
|
1987
|
+
else {
|
|
1988
|
+
try {
|
|
1989
|
+
for (var _b = __values(Object.entries(value)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
1990
|
+
var _d = __read(_c.value, 2), subName = _d[0], subValue = _d[1];
|
|
1991
|
+
if (subValue === undefined) {
|
|
1992
|
+
// Note: undefined in object is serializable - it is just omited
|
|
1993
|
+
continue;
|
|
1994
|
+
}
|
|
1995
|
+
checkSerializableAsJson("".concat(name, ".").concat(subName), subValue);
|
|
1996
|
+
}
|
|
1997
|
+
}
|
|
1998
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1999
|
+
finally {
|
|
2000
|
+
try {
|
|
2001
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
2002
|
+
}
|
|
2003
|
+
finally { if (e_1) throw e_1.error; }
|
|
2004
|
+
}
|
|
2005
|
+
try {
|
|
2006
|
+
JSON.stringify(value); // <- TODO: [0]
|
|
2007
|
+
}
|
|
2008
|
+
catch (error) {
|
|
2009
|
+
if (!(error instanceof Error)) {
|
|
2010
|
+
throw error;
|
|
2011
|
+
}
|
|
2012
|
+
throw new UnexpectedError(spaceTrim__default["default"](function (block) { return "\n ".concat(name, " is not serializable\n\n ").concat(block(error.toString()), "\n "); }));
|
|
2013
|
+
}
|
|
2014
|
+
/*
|
|
2015
|
+
TODO: [0] Is there some more elegant way to check circular references?
|
|
2016
|
+
const seen = new Set();
|
|
2017
|
+
const stack = [{ value }];
|
|
2018
|
+
while (stack.length > 0) {
|
|
2019
|
+
const { value } = stack.pop()!;
|
|
2020
|
+
if (typeof value === 'object' && value !== null) {
|
|
2021
|
+
if (seen.has(value)) {
|
|
2022
|
+
throw new UnexpectedError(`${name} has circular reference`);
|
|
2023
|
+
}
|
|
2024
|
+
seen.add(value);
|
|
2025
|
+
if (Array.isArray(value)) {
|
|
2026
|
+
stack.push(...value.map((value) => ({ value })));
|
|
2027
|
+
} else {
|
|
2028
|
+
stack.push(...Object.values(value).map((value) => ({ value })));
|
|
2029
|
+
}
|
|
2030
|
+
}
|
|
2031
|
+
}
|
|
2032
|
+
*/
|
|
2033
|
+
return;
|
|
2034
|
+
}
|
|
2035
|
+
}
|
|
2036
|
+
else {
|
|
2037
|
+
throw new UnexpectedError("".concat(name, " is unknown"));
|
|
2038
|
+
}
|
|
2039
|
+
}
|
|
2040
|
+
/**
|
|
2041
|
+
* TODO: [🧠][🛣] More elegant way to tracking than passing `name`
|
|
2042
|
+
* TODO: [🧠][main] !!! In-memory cache of same values to prevent multiple checks
|
|
2043
|
+
* Note: [🐠] This is how `checkSerializableAsJson` + `isSerializableAsJson` together can just retun true/false or rich error message
|
|
2044
|
+
*/
|
|
2045
|
+
|
|
2181
2046
|
/**
|
|
2182
2047
|
* @@@
|
|
2183
2048
|
*
|
|
@@ -2675,8 +2540,7 @@
|
|
|
2675
2540
|
exports.deserializeError = deserializeError;
|
|
2676
2541
|
exports.difference = difference;
|
|
2677
2542
|
exports.extractParameterNames = extractParameterNames;
|
|
2678
|
-
exports.
|
|
2679
|
-
exports.extractVariables = extractVariables;
|
|
2543
|
+
exports.extractVariablesFromScript = extractVariablesFromScript;
|
|
2680
2544
|
exports.forEachAsync = forEachAsync;
|
|
2681
2545
|
exports.intersection = intersection;
|
|
2682
2546
|
exports.isHostnameOnPrivateNetwork = isHostnameOnPrivateNetwork;
|
|
@@ -2706,7 +2570,6 @@
|
|
|
2706
2570
|
exports.removeDiacritics = removeDiacritics;
|
|
2707
2571
|
exports.removeEmojis = removeEmojis;
|
|
2708
2572
|
exports.removeQuotes = removeQuotes;
|
|
2709
|
-
exports.renameParameter = renameParameter;
|
|
2710
2573
|
exports.renderPromptbookMermaid = renderPromptbookMermaid;
|
|
2711
2574
|
exports.replaceParameters = replaceParameters;
|
|
2712
2575
|
exports.searchKeywords = searchKeywords;
|