@promptbook/utils 0.66.0 → 0.67.0-1
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 +428 -137
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/core.index.d.ts +4 -4
- package/esm/typings/src/_packages/types.index.d.ts +7 -1
- package/esm/typings/src/_packages/utils.index.d.ts +14 -8
- package/esm/typings/src/commands/EXPECT/ExpectFormatCommand.d.ts +2 -0
- package/esm/typings/src/errors/{ReferenceError.d.ts → PipelineUrlError.d.ts} +2 -2
- package/esm/typings/src/errors/index.d.ts +27 -0
- package/esm/typings/src/errors/utils/ErrorJson.d.ts +20 -0
- package/esm/typings/src/errors/utils/deserializeError.d.ts +7 -0
- package/esm/typings/src/errors/utils/deserializeError.test.d.ts +1 -0
- package/esm/typings/src/errors/utils/serializeError.d.ts +7 -0
- package/esm/typings/src/errors/utils/serializeError.test.d.ts +1 -0
- package/esm/typings/src/execution/ExecutionTools.d.ts +4 -1
- package/esm/typings/src/execution/PipelineExecutor.d.ts +1 -47
- package/esm/typings/src/execution/PipelineExecutorResult.d.ts +49 -0
- package/esm/typings/src/execution/PromptResult.d.ts +4 -4
- package/esm/typings/src/execution/PromptResultUsage.d.ts +4 -0
- package/esm/typings/src/execution/UncertainNumber.d.ts +1 -0
- package/esm/typings/src/execution/assertsExecutionSuccessful.d.ts +2 -2
- package/esm/typings/src/llm-providers/_common/utils/cache/CacheItem.d.ts +0 -1
- package/esm/typings/src/llm-providers/mocked/$fakeTextToExpectations.d.ts +2 -2
- package/esm/typings/src/llm-providers/mocked/MockedFackedLlmExecutionTools.d.ts +3 -3
- package/esm/typings/src/llm-providers/remote/RemoteLlmExecutionTools.d.ts +1 -0
- package/esm/typings/src/llm-providers/remote/interfaces/PromptbookServer_Error.d.ts +2 -6
- package/esm/typings/src/llm-providers/remote/startRemoteServer.d.ts +1 -0
- package/esm/typings/src/personas/preparePersona.d.ts +1 -0
- package/esm/typings/src/prepare/isPipelinePrepared.d.ts +1 -0
- package/esm/typings/src/scripting/javascript/JavascriptExecutionToolsOptions.d.ts +2 -2
- package/esm/typings/src/storage/_common/PromptbookStorage.d.ts +1 -1
- package/esm/typings/src/types/ModelRequirements.d.ts +5 -5
- package/esm/typings/src/types/PipelineJson/Expectations.d.ts +3 -1
- package/esm/typings/src/types/PipelineJson/KnowledgePieceJson.d.ts +2 -0
- package/esm/typings/src/types/PipelineJson/KnowledgeSourceJson.d.ts +4 -0
- package/esm/typings/src/types/PipelineJson/LlmTemplateJson.d.ts +2 -0
- package/esm/typings/src/types/PipelineJson/PersonaJson.d.ts +4 -0
- package/esm/typings/src/types/PipelineJson/PipelineJson.d.ts +2 -0
- package/esm/typings/src/types/PipelineJson/PromptDialogJson.d.ts +1 -0
- package/esm/typings/src/types/PipelineJson/PromptTemplateJson.d.ts +2 -0
- package/esm/typings/src/types/PipelineJson/PromptTemplateJsonCommon.d.ts +2 -2
- package/esm/typings/src/types/PipelineJson/PromptTemplateParameterJson.d.ts +2 -0
- package/esm/typings/src/types/PipelineJson/ScriptJson.d.ts +1 -0
- package/esm/typings/src/types/PipelineJson/SimpleTemplateJson.d.ts +1 -0
- package/esm/typings/src/types/Prompt.d.ts +7 -7
- package/esm/typings/src/types/ScriptLanguage.d.ts +2 -0
- package/esm/typings/src/types/execution-report/ExecutionPromptReportJson.d.ts +24 -0
- package/esm/typings/src/types/execution-report/ExecutionReportJson.d.ts +3 -20
- package/esm/typings/src/types/typeAliases.d.ts +7 -0
- package/esm/typings/src/utils/environment/$getGlobalScope.d.ts +1 -4
- package/esm/typings/src/utils/serialization/$asDeeplyFrozenSerializableJson.d.ts +17 -0
- package/esm/typings/src/utils/{deepFreeze.d.ts → serialization/$deepFreeze.d.ts} +0 -10
- package/esm/typings/src/utils/serialization/checkSerializableAsJson.d.ts +27 -0
- package/esm/typings/src/utils/{clonePipeline.d.ts → serialization/clonePipeline.d.ts} +1 -1
- package/esm/typings/src/utils/serialization/isSerializableAsJson.d.ts +24 -0
- package/esm/typings/src/utils/serialization/isSerializableAsJson.test.d.ts +1 -0
- package/package.json +1 -1
- package/umd/index.umd.js +431 -137
- package/umd/index.umd.js.map +1 -1
- package/esm/typings/src/errors/VersionMismatchError.d.ts +0 -10
- /package/esm/typings/src/utils/{deepClone.d.ts → serialization/deepClone.d.ts} +0 -0
package/umd/index.umd.js
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
/**
|
|
13
13
|
* The version of the Promptbook library
|
|
14
14
|
*/
|
|
15
|
-
var PROMPTBOOK_VERSION = '0.
|
|
15
|
+
var PROMPTBOOK_VERSION = '0.67.0-0';
|
|
16
16
|
// TODO: !!!! List here all the versions and annotate + put into script
|
|
17
17
|
|
|
18
18
|
/*! *****************************************************************************
|
|
@@ -704,7 +704,7 @@
|
|
|
704
704
|
var undefinedName = error.message.split(' ')[0];
|
|
705
705
|
/*
|
|
706
706
|
Note: Parsing the error
|
|
707
|
-
[
|
|
707
|
+
[PipelineUrlError: thing is not defined]
|
|
708
708
|
*/
|
|
709
709
|
if (!undefinedName) {
|
|
710
710
|
throw error;
|
|
@@ -722,7 +722,7 @@
|
|
|
722
722
|
if (!(error instanceof Error)) {
|
|
723
723
|
throw error;
|
|
724
724
|
}
|
|
725
|
-
throw new ParsingError(spaceTrim.spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.
|
|
725
|
+
throw new ParsingError(spaceTrim.spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.toString()), "}\n "); }));
|
|
726
726
|
}
|
|
727
727
|
return variables;
|
|
728
728
|
}
|
|
@@ -864,6 +864,167 @@
|
|
|
864
864
|
return renamedPipeline;
|
|
865
865
|
}
|
|
866
866
|
|
|
867
|
+
/**
|
|
868
|
+
* This error indicates that the pipeline collection cannot be propperly loaded
|
|
869
|
+
*
|
|
870
|
+
* @public exported from `@promptbook/core`
|
|
871
|
+
*/
|
|
872
|
+
var CollectionError = /** @class */ (function (_super) {
|
|
873
|
+
__extends(CollectionError, _super);
|
|
874
|
+
function CollectionError(message) {
|
|
875
|
+
var _this = _super.call(this, message) || this;
|
|
876
|
+
_this.name = 'CollectionError';
|
|
877
|
+
Object.setPrototypeOf(_this, CollectionError.prototype);
|
|
878
|
+
return _this;
|
|
879
|
+
}
|
|
880
|
+
return CollectionError;
|
|
881
|
+
}(Error));
|
|
882
|
+
|
|
883
|
+
/**
|
|
884
|
+
* This error type indicates that you try to use a feature that is not available in the current environment
|
|
885
|
+
*
|
|
886
|
+
* @public exported from `@promptbook/core`
|
|
887
|
+
*/
|
|
888
|
+
var EnvironmentMismatchError = /** @class */ (function (_super) {
|
|
889
|
+
__extends(EnvironmentMismatchError, _super);
|
|
890
|
+
function EnvironmentMismatchError(message) {
|
|
891
|
+
var _this = _super.call(this, message) || this;
|
|
892
|
+
_this.name = 'EnvironmentMismatchError';
|
|
893
|
+
Object.setPrototypeOf(_this, EnvironmentMismatchError.prototype);
|
|
894
|
+
return _this;
|
|
895
|
+
}
|
|
896
|
+
return EnvironmentMismatchError;
|
|
897
|
+
}(Error));
|
|
898
|
+
|
|
899
|
+
/**
|
|
900
|
+
* This error type indicates that some limit was reached
|
|
901
|
+
*
|
|
902
|
+
* @public exported from `@promptbook/core`
|
|
903
|
+
*/
|
|
904
|
+
var LimitReachedError = /** @class */ (function (_super) {
|
|
905
|
+
__extends(LimitReachedError, _super);
|
|
906
|
+
function LimitReachedError(message) {
|
|
907
|
+
var _this = _super.call(this, message) || this;
|
|
908
|
+
_this.name = 'LimitReachedError';
|
|
909
|
+
Object.setPrototypeOf(_this, LimitReachedError.prototype);
|
|
910
|
+
return _this;
|
|
911
|
+
}
|
|
912
|
+
return LimitReachedError;
|
|
913
|
+
}(Error));
|
|
914
|
+
|
|
915
|
+
/**
|
|
916
|
+
* This error indicates that promptbook not found in the collection
|
|
917
|
+
*
|
|
918
|
+
* @public exported from `@promptbook/core`
|
|
919
|
+
*/
|
|
920
|
+
var NotFoundError = /** @class */ (function (_super) {
|
|
921
|
+
__extends(NotFoundError, _super);
|
|
922
|
+
function NotFoundError(message) {
|
|
923
|
+
var _this = _super.call(this, message) || this;
|
|
924
|
+
_this.name = 'NotFoundError';
|
|
925
|
+
Object.setPrototypeOf(_this, NotFoundError.prototype);
|
|
926
|
+
return _this;
|
|
927
|
+
}
|
|
928
|
+
return NotFoundError;
|
|
929
|
+
}(Error));
|
|
930
|
+
|
|
931
|
+
/**
|
|
932
|
+
* This error type indicates that some part of the code is not implemented yet
|
|
933
|
+
*
|
|
934
|
+
* @public exported from `@promptbook/core`
|
|
935
|
+
*/
|
|
936
|
+
var NotYetImplementedError = /** @class */ (function (_super) {
|
|
937
|
+
__extends(NotYetImplementedError, _super);
|
|
938
|
+
function NotYetImplementedError(message) {
|
|
939
|
+
var _this = _super.call(this, spaceTrim.spaceTrim(function (block) { return "\n ".concat(block(message), "\n\n Note: This feature is not implemented yet but it will be soon.\n\n If you want speed up the implementation or just read more, look here:\n https://github.com/webgptorg/promptbook\n\n Or contact us on me@pavolhejny.com\n\n "); })) || this;
|
|
940
|
+
_this.name = 'NotYetImplementedError';
|
|
941
|
+
Object.setPrototypeOf(_this, NotYetImplementedError.prototype);
|
|
942
|
+
return _this;
|
|
943
|
+
}
|
|
944
|
+
return NotYetImplementedError;
|
|
945
|
+
}(Error));
|
|
946
|
+
|
|
947
|
+
/**
|
|
948
|
+
* This error indicates errors during the execution of the pipeline
|
|
949
|
+
*
|
|
950
|
+
* @public exported from `@promptbook/core`
|
|
951
|
+
*/
|
|
952
|
+
var PipelineExecutionError = /** @class */ (function (_super) {
|
|
953
|
+
__extends(PipelineExecutionError, _super);
|
|
954
|
+
function PipelineExecutionError(message) {
|
|
955
|
+
var _this = _super.call(this, message) || this;
|
|
956
|
+
_this.name = 'PipelineExecutionError';
|
|
957
|
+
Object.setPrototypeOf(_this, PipelineExecutionError.prototype);
|
|
958
|
+
return _this;
|
|
959
|
+
}
|
|
960
|
+
return PipelineExecutionError;
|
|
961
|
+
}(Error));
|
|
962
|
+
|
|
963
|
+
/**
|
|
964
|
+
* This error indicates errors in referencing promptbooks between each other
|
|
965
|
+
*
|
|
966
|
+
* @public exported from `@promptbook/core`
|
|
967
|
+
*/
|
|
968
|
+
var PipelineUrlError = /** @class */ (function (_super) {
|
|
969
|
+
__extends(PipelineUrlError, _super);
|
|
970
|
+
function PipelineUrlError(message) {
|
|
971
|
+
var _this = _super.call(this, message) || this;
|
|
972
|
+
_this.name = 'PipelineUrlError';
|
|
973
|
+
Object.setPrototypeOf(_this, PipelineUrlError.prototype);
|
|
974
|
+
return _this;
|
|
975
|
+
}
|
|
976
|
+
return PipelineUrlError;
|
|
977
|
+
}(Error));
|
|
978
|
+
|
|
979
|
+
/**
|
|
980
|
+
* Index of all custom errors
|
|
981
|
+
*
|
|
982
|
+
* @public exported from `@promptbook/core`
|
|
983
|
+
*/
|
|
984
|
+
var ERRORS = {
|
|
985
|
+
CollectionError: CollectionError,
|
|
986
|
+
EnvironmentMismatchError: EnvironmentMismatchError,
|
|
987
|
+
LimitReachedError: LimitReachedError,
|
|
988
|
+
NotFoundError: NotFoundError,
|
|
989
|
+
NotYetImplementedError: NotYetImplementedError,
|
|
990
|
+
ParsingError: ParsingError,
|
|
991
|
+
PipelineExecutionError: PipelineExecutionError,
|
|
992
|
+
PipelineLogicError: PipelineLogicError,
|
|
993
|
+
PipelineUrlError: PipelineUrlError,
|
|
994
|
+
UnexpectedError: UnexpectedError,
|
|
995
|
+
// TODO: [🪑]> VersionMismatchError,
|
|
996
|
+
};
|
|
997
|
+
|
|
998
|
+
/**
|
|
999
|
+
* Deserializes the error object
|
|
1000
|
+
*
|
|
1001
|
+
* @public exported from `@promptbook/utils`
|
|
1002
|
+
*/
|
|
1003
|
+
function deserializeError(error) {
|
|
1004
|
+
if (error.name === 'Error') {
|
|
1005
|
+
return new Error(error.message);
|
|
1006
|
+
}
|
|
1007
|
+
var CustomError = ERRORS[error.name];
|
|
1008
|
+
return new CustomError(error.message);
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
/**
|
|
1012
|
+
* Serializes an error into a [🚉] JSON-serializable object
|
|
1013
|
+
*
|
|
1014
|
+
* @public exported from `@promptbook/utils`
|
|
1015
|
+
*/
|
|
1016
|
+
function serializeError(error) {
|
|
1017
|
+
var name = error.name, message = error.message, stack = error.stack;
|
|
1018
|
+
if (!__spreadArray(['Error'], __read(Object.keys(ERRORS)), false).includes(name)) {
|
|
1019
|
+
throw new UnexpectedError(spaceTrim__default["default"](function (block) { return "\n \n Cannot serialize error with name \"".concat(name, "\"\n\n ").concat(block(stack || message), "\n \n "); }));
|
|
1020
|
+
}
|
|
1021
|
+
return {
|
|
1022
|
+
name: name,
|
|
1023
|
+
message: message,
|
|
1024
|
+
stack: stack,
|
|
1025
|
+
};
|
|
1026
|
+
}
|
|
1027
|
+
|
|
867
1028
|
/**
|
|
868
1029
|
* Async version of Array.forEach
|
|
869
1030
|
*
|
|
@@ -1101,106 +1262,6 @@
|
|
|
1101
1262
|
return new Date().toISOString();
|
|
1102
1263
|
}
|
|
1103
1264
|
|
|
1104
|
-
/**
|
|
1105
|
-
* @@@
|
|
1106
|
-
*
|
|
1107
|
-
* Note: It is usefull @@@
|
|
1108
|
-
*
|
|
1109
|
-
* @param pipeline
|
|
1110
|
-
* @public exported from `@promptbook/utils`
|
|
1111
|
-
*/
|
|
1112
|
-
function clonePipeline(pipeline) {
|
|
1113
|
-
// Note: Not using spread operator (...) because @@@
|
|
1114
|
-
var pipelineUrl = pipeline.pipelineUrl, sourceFile = pipeline.sourceFile, title = pipeline.title, promptbookVersion = pipeline.promptbookVersion, description = pipeline.description, parameters = pipeline.parameters, promptTemplates = pipeline.promptTemplates, knowledgeSources = pipeline.knowledgeSources, knowledgePieces = pipeline.knowledgePieces, personas = pipeline.personas, preparations = pipeline.preparations;
|
|
1115
|
-
return {
|
|
1116
|
-
pipelineUrl: pipelineUrl,
|
|
1117
|
-
sourceFile: sourceFile,
|
|
1118
|
-
title: title,
|
|
1119
|
-
promptbookVersion: promptbookVersion,
|
|
1120
|
-
description: description,
|
|
1121
|
-
parameters: parameters,
|
|
1122
|
-
promptTemplates: promptTemplates,
|
|
1123
|
-
knowledgeSources: knowledgeSources,
|
|
1124
|
-
knowledgePieces: knowledgePieces,
|
|
1125
|
-
personas: personas,
|
|
1126
|
-
preparations: preparations,
|
|
1127
|
-
};
|
|
1128
|
-
}
|
|
1129
|
-
/**
|
|
1130
|
-
* TODO: [🍙] Make some standart order of json properties
|
|
1131
|
-
*/
|
|
1132
|
-
|
|
1133
|
-
/**
|
|
1134
|
-
* @@@
|
|
1135
|
-
*
|
|
1136
|
-
* @public exported from `@promptbook/utils`
|
|
1137
|
-
*/
|
|
1138
|
-
function deepClone(objectValue) {
|
|
1139
|
-
return JSON.parse(JSON.stringify(objectValue));
|
|
1140
|
-
/*
|
|
1141
|
-
TODO: [🧠] Is there a better implementation?
|
|
1142
|
-
> const propertyNames = Object.getOwnPropertyNames(objectValue);
|
|
1143
|
-
> for (const propertyName of propertyNames) {
|
|
1144
|
-
> const value = (objectValue as really_any)[propertyName];
|
|
1145
|
-
> if (value && typeof value === 'object') {
|
|
1146
|
-
> deepClone(value);
|
|
1147
|
-
> }
|
|
1148
|
-
> }
|
|
1149
|
-
> return Object.assign({}, objectValue);
|
|
1150
|
-
*/
|
|
1151
|
-
}
|
|
1152
|
-
/**
|
|
1153
|
-
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
1154
|
-
*/
|
|
1155
|
-
|
|
1156
|
-
/**
|
|
1157
|
-
* @@@
|
|
1158
|
-
*
|
|
1159
|
-
* Note: `$` is used to indicate that this function is not a pure function - it mutates given object
|
|
1160
|
-
* Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
|
|
1161
|
-
*
|
|
1162
|
-
* @returns The same object as the input, but deeply frozen
|
|
1163
|
-
* @public exported from `@promptbook/utils`
|
|
1164
|
-
*/
|
|
1165
|
-
function $deepFreeze(objectValue) {
|
|
1166
|
-
var e_1, _a;
|
|
1167
|
-
var propertyNames = Object.getOwnPropertyNames(objectValue);
|
|
1168
|
-
try {
|
|
1169
|
-
for (var propertyNames_1 = __values(propertyNames), propertyNames_1_1 = propertyNames_1.next(); !propertyNames_1_1.done; propertyNames_1_1 = propertyNames_1.next()) {
|
|
1170
|
-
var propertyName = propertyNames_1_1.value;
|
|
1171
|
-
var value = objectValue[propertyName];
|
|
1172
|
-
if (value && typeof value === 'object') {
|
|
1173
|
-
$deepFreeze(value);
|
|
1174
|
-
}
|
|
1175
|
-
}
|
|
1176
|
-
}
|
|
1177
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1178
|
-
finally {
|
|
1179
|
-
try {
|
|
1180
|
-
if (propertyNames_1_1 && !propertyNames_1_1.done && (_a = propertyNames_1.return)) _a.call(propertyNames_1);
|
|
1181
|
-
}
|
|
1182
|
-
finally { if (e_1) throw e_1.error; }
|
|
1183
|
-
}
|
|
1184
|
-
return Object.freeze(objectValue);
|
|
1185
|
-
}
|
|
1186
|
-
/**
|
|
1187
|
-
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
1188
|
-
*/
|
|
1189
|
-
|
|
1190
|
-
/**
|
|
1191
|
-
* @@@
|
|
1192
|
-
*
|
|
1193
|
-
* Note: `$` is used to indicate that this function is not a pure function - it access global scope
|
|
1194
|
-
*
|
|
1195
|
-
* @public exported from `@promptbook/utils`
|
|
1196
|
-
*/
|
|
1197
|
-
function $getGlobalScope() {
|
|
1198
|
-
return Function('return this')();
|
|
1199
|
-
}
|
|
1200
|
-
/***
|
|
1201
|
-
* TODO: !!!!! Make private and promptbook registry from this
|
|
1202
|
-
*/
|
|
1203
|
-
|
|
1204
1265
|
/**
|
|
1205
1266
|
* Detects if the code is running in a browser environment in main thread (Not in a web worker)
|
|
1206
1267
|
*
|
|
@@ -1664,6 +1725,183 @@
|
|
|
1664
1725
|
return text;
|
|
1665
1726
|
}
|
|
1666
1727
|
|
|
1728
|
+
/**
|
|
1729
|
+
* @@@
|
|
1730
|
+
*
|
|
1731
|
+
* Note: `$` is used to indicate that this function is not a pure function - it mutates given object
|
|
1732
|
+
* Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
|
|
1733
|
+
*
|
|
1734
|
+
* @returns The same object as the input, but deeply frozen
|
|
1735
|
+
* @public exported from `@promptbook/utils`
|
|
1736
|
+
*/
|
|
1737
|
+
function $deepFreeze(objectValue) {
|
|
1738
|
+
var e_1, _a;
|
|
1739
|
+
var propertyNames = Object.getOwnPropertyNames(objectValue);
|
|
1740
|
+
try {
|
|
1741
|
+
for (var propertyNames_1 = __values(propertyNames), propertyNames_1_1 = propertyNames_1.next(); !propertyNames_1_1.done; propertyNames_1_1 = propertyNames_1.next()) {
|
|
1742
|
+
var propertyName = propertyNames_1_1.value;
|
|
1743
|
+
var value = objectValue[propertyName];
|
|
1744
|
+
if (value && typeof value === 'object') {
|
|
1745
|
+
$deepFreeze(value);
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
}
|
|
1749
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1750
|
+
finally {
|
|
1751
|
+
try {
|
|
1752
|
+
if (propertyNames_1_1 && !propertyNames_1_1.done && (_a = propertyNames_1.return)) _a.call(propertyNames_1);
|
|
1753
|
+
}
|
|
1754
|
+
finally { if (e_1) throw e_1.error; }
|
|
1755
|
+
}
|
|
1756
|
+
return Object.freeze(objectValue);
|
|
1757
|
+
}
|
|
1758
|
+
/**
|
|
1759
|
+
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
1760
|
+
*/
|
|
1761
|
+
|
|
1762
|
+
/**
|
|
1763
|
+
* Checks if the value is [🚉] serializable as JSON
|
|
1764
|
+
* If not, throws an UnexpectedError with a rich error message and tracking
|
|
1765
|
+
*
|
|
1766
|
+
* - Almost all primitives are serializable BUT:
|
|
1767
|
+
* - `undefined` is not serializable
|
|
1768
|
+
* - `NaN` is not serializable
|
|
1769
|
+
* - Objects and arrays are serializable if all their properties are serializable
|
|
1770
|
+
* - Functions are not serializable
|
|
1771
|
+
* - Circular references are not serializable
|
|
1772
|
+
* - `Date` objects are not serializable
|
|
1773
|
+
* - `Map` and `Set` objects are not serializable
|
|
1774
|
+
* - `RegExp` objects are not serializable
|
|
1775
|
+
* - `Error` objects are not serializable
|
|
1776
|
+
* - `Symbol` objects are not serializable
|
|
1777
|
+
* - And much more...
|
|
1778
|
+
*
|
|
1779
|
+
* @throws UnexpectedError if the value is not serializable as JSON
|
|
1780
|
+
* @public exported from `@promptbook/utils`
|
|
1781
|
+
*/
|
|
1782
|
+
function checkSerializableAsJson(name, value) {
|
|
1783
|
+
var e_1, _a;
|
|
1784
|
+
if (value === undefined) {
|
|
1785
|
+
throw new UnexpectedError("".concat(name, " is undefined"));
|
|
1786
|
+
}
|
|
1787
|
+
else if (value === null) {
|
|
1788
|
+
return;
|
|
1789
|
+
}
|
|
1790
|
+
else if (typeof value === 'boolean') {
|
|
1791
|
+
return;
|
|
1792
|
+
}
|
|
1793
|
+
else if (typeof value === 'number' && !isNaN(value)) {
|
|
1794
|
+
return;
|
|
1795
|
+
}
|
|
1796
|
+
else if (typeof value === 'string') {
|
|
1797
|
+
return;
|
|
1798
|
+
}
|
|
1799
|
+
else if (typeof value === 'symbol') {
|
|
1800
|
+
throw new UnexpectedError("".concat(name, " is symbol"));
|
|
1801
|
+
}
|
|
1802
|
+
else if (typeof value === 'function') {
|
|
1803
|
+
throw new UnexpectedError("".concat(name, " is function"));
|
|
1804
|
+
}
|
|
1805
|
+
else if (typeof value === 'object' && Array.isArray(value)) {
|
|
1806
|
+
for (var i = 0; i < value.length; i++) {
|
|
1807
|
+
checkSerializableAsJson("".concat(name, "[").concat(i, "]"), value[i]);
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
else if (typeof value === 'object') {
|
|
1811
|
+
if (value instanceof Date) {
|
|
1812
|
+
throw new UnexpectedError(spaceTrim__default["default"]("\n ".concat(name, " is Date\n\n Use `string_date_iso8601` instead\n ")));
|
|
1813
|
+
}
|
|
1814
|
+
else if (value instanceof Map) {
|
|
1815
|
+
throw new UnexpectedError("".concat(name, " is Map"));
|
|
1816
|
+
}
|
|
1817
|
+
else if (value instanceof Set) {
|
|
1818
|
+
throw new UnexpectedError("".concat(name, " is Set"));
|
|
1819
|
+
}
|
|
1820
|
+
else if (value instanceof RegExp) {
|
|
1821
|
+
throw new UnexpectedError("".concat(name, " is RegExp"));
|
|
1822
|
+
}
|
|
1823
|
+
else if (value instanceof Error) {
|
|
1824
|
+
throw new UnexpectedError(spaceTrim__default["default"]("\n ".concat(name, " is unserialized Error\n\n Use function `serializeError`\n ")));
|
|
1825
|
+
}
|
|
1826
|
+
else {
|
|
1827
|
+
try {
|
|
1828
|
+
for (var _b = __values(Object.entries(value)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
1829
|
+
var _d = __read(_c.value, 2), subName = _d[0], subValue = _d[1];
|
|
1830
|
+
if (subValue === undefined) {
|
|
1831
|
+
// Note: undefined in object is serializable - it is just omited
|
|
1832
|
+
continue;
|
|
1833
|
+
}
|
|
1834
|
+
checkSerializableAsJson("".concat(name, ".").concat(subName), subValue);
|
|
1835
|
+
}
|
|
1836
|
+
}
|
|
1837
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1838
|
+
finally {
|
|
1839
|
+
try {
|
|
1840
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
1841
|
+
}
|
|
1842
|
+
finally { if (e_1) throw e_1.error; }
|
|
1843
|
+
}
|
|
1844
|
+
try {
|
|
1845
|
+
JSON.stringify(value); // <- TODO: [0]
|
|
1846
|
+
}
|
|
1847
|
+
catch (error) {
|
|
1848
|
+
if (!(error instanceof Error)) {
|
|
1849
|
+
throw error;
|
|
1850
|
+
}
|
|
1851
|
+
throw new UnexpectedError(spaceTrim__default["default"](function (block) { return "\n ".concat(name, " is not serializable\n\n ").concat(block(error.toString()), "\n "); }));
|
|
1852
|
+
}
|
|
1853
|
+
/*
|
|
1854
|
+
TODO: [0] Is there some more elegant way to check circular references?
|
|
1855
|
+
const seen = new Set();
|
|
1856
|
+
const stack = [{ value }];
|
|
1857
|
+
while (stack.length > 0) {
|
|
1858
|
+
const { value } = stack.pop()!;
|
|
1859
|
+
if (typeof value === 'object' && value !== null) {
|
|
1860
|
+
if (seen.has(value)) {
|
|
1861
|
+
throw new UnexpectedError(`${name} has circular reference`);
|
|
1862
|
+
}
|
|
1863
|
+
seen.add(value);
|
|
1864
|
+
if (Array.isArray(value)) {
|
|
1865
|
+
stack.push(...value.map((value) => ({ value })));
|
|
1866
|
+
} else {
|
|
1867
|
+
stack.push(...Object.values(value).map((value) => ({ value })));
|
|
1868
|
+
}
|
|
1869
|
+
}
|
|
1870
|
+
}
|
|
1871
|
+
*/
|
|
1872
|
+
return;
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
else {
|
|
1876
|
+
throw new UnexpectedError("".concat(name, " is unknown"));
|
|
1877
|
+
}
|
|
1878
|
+
}
|
|
1879
|
+
/**
|
|
1880
|
+
* TODO: [🧠][🛣] More elegant way to tracking than passing `name`
|
|
1881
|
+
* TODO: [🧠] !!! In-memory cache of same values to prevent multiple checks
|
|
1882
|
+
* Note: [🐠] This is how `checkSerializableAsJson` + `isSerializableAsJson` together can just retun true/false or rich error message
|
|
1883
|
+
*/
|
|
1884
|
+
|
|
1885
|
+
/**
|
|
1886
|
+
* @@@
|
|
1887
|
+
* @@@
|
|
1888
|
+
*
|
|
1889
|
+
* Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
|
|
1890
|
+
*
|
|
1891
|
+
* @param name - Name of the object for debugging purposes
|
|
1892
|
+
* @param objectValue - Object to be deeply frozen
|
|
1893
|
+
* @returns The same object as the input, but deeply frozen
|
|
1894
|
+
* @private this is in comparison to `deepFreeze` a more specific utility and maybe not very good practice to use without specific reason and considerations
|
|
1895
|
+
*/
|
|
1896
|
+
function $asDeeplyFrozenSerializableJson(name, objectValue) {
|
|
1897
|
+
checkSerializableAsJson(name, objectValue);
|
|
1898
|
+
return $deepFreeze(objectValue);
|
|
1899
|
+
}
|
|
1900
|
+
/**
|
|
1901
|
+
* TODO: [🧠][🛣] More elegant way to tracking than passing `name`
|
|
1902
|
+
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
1903
|
+
*/
|
|
1904
|
+
|
|
1667
1905
|
// <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
|
|
1668
1906
|
/**
|
|
1669
1907
|
* The maximum number of iterations for a loops
|
|
@@ -1682,7 +1920,7 @@
|
|
|
1682
1920
|
*
|
|
1683
1921
|
* @public exported from `@promptbook/core`
|
|
1684
1922
|
*/
|
|
1685
|
-
$
|
|
1923
|
+
$asDeeplyFrozenSerializableJson('RESERVED_PARAMETER_NAMES', [
|
|
1686
1924
|
'content',
|
|
1687
1925
|
'context',
|
|
1688
1926
|
'knowledge',
|
|
@@ -1708,38 +1946,6 @@
|
|
|
1708
1946
|
* TODO: [🧠][🧜♂️] Maybe join remoteUrl and path into single value
|
|
1709
1947
|
*/
|
|
1710
1948
|
|
|
1711
|
-
/**
|
|
1712
|
-
* This error type indicates that some limit was reached
|
|
1713
|
-
*
|
|
1714
|
-
* @public exported from `@promptbook/core`
|
|
1715
|
-
*/
|
|
1716
|
-
var LimitReachedError = /** @class */ (function (_super) {
|
|
1717
|
-
__extends(LimitReachedError, _super);
|
|
1718
|
-
function LimitReachedError(message) {
|
|
1719
|
-
var _this = _super.call(this, message) || this;
|
|
1720
|
-
_this.name = 'LimitReachedError';
|
|
1721
|
-
Object.setPrototypeOf(_this, LimitReachedError.prototype);
|
|
1722
|
-
return _this;
|
|
1723
|
-
}
|
|
1724
|
-
return LimitReachedError;
|
|
1725
|
-
}(Error));
|
|
1726
|
-
|
|
1727
|
-
/**
|
|
1728
|
-
* This error indicates errors during the execution of the pipeline
|
|
1729
|
-
*
|
|
1730
|
-
* @public exported from `@promptbook/core`
|
|
1731
|
-
*/
|
|
1732
|
-
var PipelineExecutionError = /** @class */ (function (_super) {
|
|
1733
|
-
__extends(PipelineExecutionError, _super);
|
|
1734
|
-
function PipelineExecutionError(message) {
|
|
1735
|
-
var _this = _super.call(this, message) || this;
|
|
1736
|
-
_this.name = 'PipelineExecutionError';
|
|
1737
|
-
Object.setPrototypeOf(_this, PipelineExecutionError.prototype);
|
|
1738
|
-
return _this;
|
|
1739
|
-
}
|
|
1740
|
-
return PipelineExecutionError;
|
|
1741
|
-
}(Error));
|
|
1742
|
-
|
|
1743
1949
|
/**
|
|
1744
1950
|
* Replaces parameters in template with values from parameters object
|
|
1745
1951
|
*
|
|
@@ -1819,6 +2025,91 @@
|
|
|
1819
2025
|
return replacedTemplate;
|
|
1820
2026
|
}
|
|
1821
2027
|
|
|
2028
|
+
/**
|
|
2029
|
+
* @@@
|
|
2030
|
+
*
|
|
2031
|
+
* Note: It is usefull @@@
|
|
2032
|
+
*
|
|
2033
|
+
* @param pipeline
|
|
2034
|
+
* @public exported from `@promptbook/utils`
|
|
2035
|
+
*/
|
|
2036
|
+
function clonePipeline(pipeline) {
|
|
2037
|
+
// Note: Not using spread operator (...) because @@@
|
|
2038
|
+
var pipelineUrl = pipeline.pipelineUrl, sourceFile = pipeline.sourceFile, title = pipeline.title, promptbookVersion = pipeline.promptbookVersion, description = pipeline.description, parameters = pipeline.parameters, promptTemplates = pipeline.promptTemplates, knowledgeSources = pipeline.knowledgeSources, knowledgePieces = pipeline.knowledgePieces, personas = pipeline.personas, preparations = pipeline.preparations;
|
|
2039
|
+
return {
|
|
2040
|
+
pipelineUrl: pipelineUrl,
|
|
2041
|
+
sourceFile: sourceFile,
|
|
2042
|
+
title: title,
|
|
2043
|
+
promptbookVersion: promptbookVersion,
|
|
2044
|
+
description: description,
|
|
2045
|
+
parameters: parameters,
|
|
2046
|
+
promptTemplates: promptTemplates,
|
|
2047
|
+
knowledgeSources: knowledgeSources,
|
|
2048
|
+
knowledgePieces: knowledgePieces,
|
|
2049
|
+
personas: personas,
|
|
2050
|
+
preparations: preparations,
|
|
2051
|
+
};
|
|
2052
|
+
}
|
|
2053
|
+
/**
|
|
2054
|
+
* TODO: [🍙] Make some standart order of json properties
|
|
2055
|
+
*/
|
|
2056
|
+
|
|
2057
|
+
/**
|
|
2058
|
+
* @@@
|
|
2059
|
+
*
|
|
2060
|
+
* @public exported from `@promptbook/utils`
|
|
2061
|
+
*/
|
|
2062
|
+
function deepClone(objectValue) {
|
|
2063
|
+
return JSON.parse(JSON.stringify(objectValue));
|
|
2064
|
+
/*
|
|
2065
|
+
TODO: [🧠] Is there a better implementation?
|
|
2066
|
+
> const propertyNames = Object.getOwnPropertyNames(objectValue);
|
|
2067
|
+
> for (const propertyName of propertyNames) {
|
|
2068
|
+
> const value = (objectValue as really_any)[propertyName];
|
|
2069
|
+
> if (value && typeof value === 'object') {
|
|
2070
|
+
> deepClone(value);
|
|
2071
|
+
> }
|
|
2072
|
+
> }
|
|
2073
|
+
> return Object.assign({}, objectValue);
|
|
2074
|
+
*/
|
|
2075
|
+
}
|
|
2076
|
+
/**
|
|
2077
|
+
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
2078
|
+
*/
|
|
2079
|
+
|
|
2080
|
+
/**
|
|
2081
|
+
* Tests if the value is [🚉] serializable as JSON
|
|
2082
|
+
*
|
|
2083
|
+
* - Almost all primitives are serializable BUT:
|
|
2084
|
+
* - `undefined` is not serializable
|
|
2085
|
+
* - `NaN` is not serializable
|
|
2086
|
+
* - Objects and arrays are serializable if all their properties are serializable
|
|
2087
|
+
* - Functions are not serializable
|
|
2088
|
+
* - Circular references are not serializable
|
|
2089
|
+
* - `Date` objects are not serializable
|
|
2090
|
+
* - `Map` and `Set` objects are not serializable
|
|
2091
|
+
* - `RegExp` objects are not serializable
|
|
2092
|
+
* - `Error` objects are not serializable
|
|
2093
|
+
* - `Symbol` objects are not serializable
|
|
2094
|
+
* - And much more...
|
|
2095
|
+
*
|
|
2096
|
+
*
|
|
2097
|
+
* @public exported from `@promptbook/utils`
|
|
2098
|
+
*/
|
|
2099
|
+
function isSerializableAsJson(value) {
|
|
2100
|
+
try {
|
|
2101
|
+
checkSerializableAsJson('', value);
|
|
2102
|
+
return true;
|
|
2103
|
+
}
|
|
2104
|
+
catch (error) {
|
|
2105
|
+
return false;
|
|
2106
|
+
}
|
|
2107
|
+
}
|
|
2108
|
+
/**
|
|
2109
|
+
* TODO: [🧠] !!! In-memory cache of same values to prevent multiple checks
|
|
2110
|
+
* TODO: [🧠][💺] Can be done this on type-level?
|
|
2111
|
+
*/
|
|
2112
|
+
|
|
1822
2113
|
/**
|
|
1823
2114
|
* Create difference set of two sets.
|
|
1824
2115
|
*
|
|
@@ -2260,7 +2551,6 @@
|
|
|
2260
2551
|
|
|
2261
2552
|
exports.$currentDate = $currentDate;
|
|
2262
2553
|
exports.$deepFreeze = $deepFreeze;
|
|
2263
|
-
exports.$getGlobalScope = $getGlobalScope;
|
|
2264
2554
|
exports.$isRunningInBrowser = $isRunningInBrowser;
|
|
2265
2555
|
exports.$isRunningInNode = $isRunningInNode;
|
|
2266
2556
|
exports.$isRunningInWebWorker = $isRunningInWebWorker;
|
|
@@ -2269,6 +2559,7 @@
|
|
|
2269
2559
|
exports.DIACRITIC_VARIANTS_LETTERS = DIACRITIC_VARIANTS_LETTERS;
|
|
2270
2560
|
exports.PROMPTBOOK_VERSION = PROMPTBOOK_VERSION;
|
|
2271
2561
|
exports.capitalize = capitalize;
|
|
2562
|
+
exports.checkSerializableAsJson = checkSerializableAsJson;
|
|
2272
2563
|
exports.clonePipeline = clonePipeline;
|
|
2273
2564
|
exports.countCharacters = countCharacters;
|
|
2274
2565
|
exports.countLines = countLines;
|
|
@@ -2278,6 +2569,7 @@
|
|
|
2278
2569
|
exports.countWords = countWords;
|
|
2279
2570
|
exports.decapitalize = decapitalize;
|
|
2280
2571
|
exports.deepClone = deepClone;
|
|
2572
|
+
exports.deserializeError = deserializeError;
|
|
2281
2573
|
exports.difference = difference;
|
|
2282
2574
|
exports.extractBlock = extractBlock;
|
|
2283
2575
|
exports.extractParameterNames = extractParameterNames;
|
|
@@ -2286,6 +2578,7 @@
|
|
|
2286
2578
|
exports.forEachAsync = forEachAsync;
|
|
2287
2579
|
exports.intersection = intersection;
|
|
2288
2580
|
exports.isHostnameOnPrivateNetwork = isHostnameOnPrivateNetwork;
|
|
2581
|
+
exports.isSerializableAsJson = isSerializableAsJson;
|
|
2289
2582
|
exports.isUrlOnPrivateNetwork = isUrlOnPrivateNetwork;
|
|
2290
2583
|
exports.isValidEmail = isValidEmail;
|
|
2291
2584
|
exports.isValidFilePath = isValidFilePath;
|
|
@@ -2315,6 +2608,7 @@
|
|
|
2315
2608
|
exports.renderPromptbookMermaid = renderPromptbookMermaid;
|
|
2316
2609
|
exports.replaceParameters = replaceParameters;
|
|
2317
2610
|
exports.searchKeywords = searchKeywords;
|
|
2611
|
+
exports.serializeError = serializeError;
|
|
2318
2612
|
exports.splitIntoSentences = splitIntoSentences;
|
|
2319
2613
|
exports.titleToName = titleToName;
|
|
2320
2614
|
exports.trimCodeBlock = trimCodeBlock;
|