@promptbook/utils 0.78.4 → 0.80.0-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/esm/index.es.js +278 -202
- package/esm/index.es.js.map +1 -1
- package/esm/typings/books/index.d.ts +6 -6
- package/esm/typings/src/_packages/core.index.d.ts +4 -2
- package/esm/typings/src/_packages/types.index.d.ts +10 -0
- package/esm/typings/src/_packages/utils.index.d.ts +4 -0
- package/esm/typings/src/cli/cli-commands/runInteractiveChatbot.d.ts +32 -0
- package/esm/typings/src/commands/_common/types/CommandParser.d.ts +5 -2
- package/esm/typings/src/config.d.ts +0 -25
- package/esm/typings/src/constants.d.ts +35 -0
- package/esm/typings/src/conversion/pipelineJsonToString.d.ts +1 -0
- package/esm/typings/src/conversion/pipelineStringToJsonSync.d.ts +1 -0
- package/esm/typings/src/formfactors/generator/GeneratorFormfactorDefinition.d.ts +6 -1
- package/esm/typings/src/formfactors/index.d.ts +12 -2
- package/esm/typings/src/formfactors/matcher/MatcherFormfactorDefinition.d.ts +6 -1
- package/esm/typings/src/high-level-abstractions/_common/HighLevelAbstraction.d.ts +20 -0
- package/esm/typings/src/high-level-abstractions/implicit-formfactor/ImplicitFormfactorHla.d.ts +10 -0
- package/esm/typings/src/high-level-abstractions/index.d.ts +44 -0
- package/esm/typings/src/high-level-abstractions/quick-chatbot/QuickChatbotHla.d.ts +10 -0
- package/esm/typings/src/llm-providers/remote/RemoteLlmExecutionTools.d.ts +1 -1
- package/esm/typings/src/llm-providers/remote/startRemoteServer.d.ts +1 -1
- package/esm/typings/src/prepare/prepareTasks.d.ts +1 -0
- package/esm/typings/src/types/typeAliases.d.ts +1 -1
- package/esm/typings/src/utils/normalization/orderJson.d.ts +21 -0
- package/esm/typings/src/utils/normalization/orderJson.test.d.ts +4 -0
- package/esm/typings/src/utils/organization/keepTypeImported.d.ts +9 -0
- package/esm/typings/src/utils/serialization/$deepFreeze.d.ts +1 -1
- package/esm/typings/src/utils/serialization/checkSerializableAsJson.d.ts +20 -2
- package/esm/typings/src/utils/serialization/deepClone.test.d.ts +1 -0
- package/esm/typings/src/utils/serialization/exportJson.d.ts +29 -0
- package/esm/typings/src/utils/serialization/isSerializableAsJson.d.ts +2 -1
- package/package.json +3 -2
- package/umd/index.umd.js +279 -201
- package/umd/index.umd.js.map +1 -1
- package/esm/typings/src/utils/serialization/$asDeeplyFrozenSerializableJson.d.ts +0 -17
package/README.md
CHANGED
|
@@ -23,6 +23,10 @@
|
|
|
23
23
|
|
|
24
24
|
|
|
25
25
|
|
|
26
|
+
<blockquote style="color: #ff8811">
|
|
27
|
+
<b>⚠ Warning:</b> This is a pre-release version of the library. It is not yet ready for production use. Please look at <a href="https://www.npmjs.com/package/@promptbook/core?activeTab=versions">latest stable release</a>.
|
|
28
|
+
</blockquote>
|
|
29
|
+
|
|
26
30
|
## 📦 Package `@promptbook/utils`
|
|
27
31
|
|
|
28
32
|
- Promptbooks are [divided into several](#-packages) packages, all are published from [single monorepo](https://github.com/webgptorg/promptbook).
|
package/esm/index.es.js
CHANGED
|
@@ -13,7 +13,7 @@ var BOOK_LANGUAGE_VERSION = '1.0.0';
|
|
|
13
13
|
*
|
|
14
14
|
* @see https://github.com/webgptorg/promptbook
|
|
15
15
|
*/
|
|
16
|
-
var PROMPTBOOK_ENGINE_VERSION = '0.
|
|
16
|
+
var PROMPTBOOK_ENGINE_VERSION = '0.79.0';
|
|
17
17
|
/**
|
|
18
18
|
* TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
|
|
19
19
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
@@ -165,24 +165,6 @@ var ADMIN_GITHUB_NAME = 'hejny';
|
|
|
165
165
|
* @private within the repository - too low-level in comparison with other `MAX_...`
|
|
166
166
|
*/
|
|
167
167
|
var LOOP_LIMIT = 1000;
|
|
168
|
-
/**
|
|
169
|
-
* Nonce which is used for replacing things in strings
|
|
170
|
-
*
|
|
171
|
-
* @private within the repository
|
|
172
|
-
*/
|
|
173
|
-
var REPLACING_NONCE = 'u$k42k%!V2zo34w7Fu#@QUHYPW';
|
|
174
|
-
/**
|
|
175
|
-
* @@@
|
|
176
|
-
*
|
|
177
|
-
* @private within the repository
|
|
178
|
-
*/
|
|
179
|
-
var RESERVED_PARAMETER_MISSING_VALUE = 'MISSING-' + REPLACING_NONCE;
|
|
180
|
-
/**
|
|
181
|
-
* @@@
|
|
182
|
-
*
|
|
183
|
-
* @private within the repository
|
|
184
|
-
*/
|
|
185
|
-
var RESERVED_PARAMETER_RESTRICTED = 'RESTRICTED-' + REPLACING_NONCE;
|
|
186
168
|
// <- TODO: [🧜♂️]
|
|
187
169
|
/**
|
|
188
170
|
* @@@
|
|
@@ -196,7 +178,6 @@ Object.freeze({
|
|
|
196
178
|
skipEmptyLines: true,
|
|
197
179
|
});
|
|
198
180
|
/**
|
|
199
|
-
* TODO: Extract `constants.ts` from `config.ts`
|
|
200
181
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
201
182
|
* TODO: [🧠][🧜♂️] Maybe join remoteUrl and path into single value
|
|
202
183
|
*/
|
|
@@ -1605,6 +1586,18 @@ function normalizeWhitespaces(sentence) {
|
|
|
1605
1586
|
return sentence.replace(/\s+/gs, ' ').trim();
|
|
1606
1587
|
}
|
|
1607
1588
|
|
|
1589
|
+
/**
|
|
1590
|
+
* Orders JSON object by keys
|
|
1591
|
+
*
|
|
1592
|
+
* @returns The same type of object as the input re-ordered
|
|
1593
|
+
* @public exported from `@promptbook/utils`
|
|
1594
|
+
*/
|
|
1595
|
+
function orderJson(options) {
|
|
1596
|
+
var value = options.value, order = options.order;
|
|
1597
|
+
var orderedValue = __assign(__assign({}, (order === undefined ? {} : Object.fromEntries(order.map(function (key) { return [key, undefined]; })))), value);
|
|
1598
|
+
return orderedValue;
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1608
1601
|
/**
|
|
1609
1602
|
* Parses keywords from any object and recursively walks through
|
|
1610
1603
|
*
|
|
@@ -1710,6 +1703,268 @@ function extractParameterNames(template) {
|
|
|
1710
1703
|
return parameterNames;
|
|
1711
1704
|
}
|
|
1712
1705
|
|
|
1706
|
+
/**
|
|
1707
|
+
* Freezes the given object and all its nested objects recursively
|
|
1708
|
+
*
|
|
1709
|
+
* Note: `$` is used to indicate that this function is not a pure function - it mutates given object
|
|
1710
|
+
* Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
|
|
1711
|
+
*
|
|
1712
|
+
* @returns The same object as the input, but deeply frozen
|
|
1713
|
+
* @public exported from `@promptbook/utils`
|
|
1714
|
+
*/
|
|
1715
|
+
function $deepFreeze(objectValue) {
|
|
1716
|
+
var e_1, _a;
|
|
1717
|
+
if (Array.isArray(objectValue)) {
|
|
1718
|
+
return Object.freeze(objectValue.map(function (item) { return $deepFreeze(item); }));
|
|
1719
|
+
}
|
|
1720
|
+
var propertyNames = Object.getOwnPropertyNames(objectValue);
|
|
1721
|
+
try {
|
|
1722
|
+
for (var propertyNames_1 = __values(propertyNames), propertyNames_1_1 = propertyNames_1.next(); !propertyNames_1_1.done; propertyNames_1_1 = propertyNames_1.next()) {
|
|
1723
|
+
var propertyName = propertyNames_1_1.value;
|
|
1724
|
+
var value = objectValue[propertyName];
|
|
1725
|
+
if (value && typeof value === 'object') {
|
|
1726
|
+
$deepFreeze(value);
|
|
1727
|
+
}
|
|
1728
|
+
}
|
|
1729
|
+
}
|
|
1730
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1731
|
+
finally {
|
|
1732
|
+
try {
|
|
1733
|
+
if (propertyNames_1_1 && !propertyNames_1_1.done && (_a = propertyNames_1.return)) _a.call(propertyNames_1);
|
|
1734
|
+
}
|
|
1735
|
+
finally { if (e_1) throw e_1.error; }
|
|
1736
|
+
}
|
|
1737
|
+
Object.freeze(objectValue);
|
|
1738
|
+
return objectValue;
|
|
1739
|
+
}
|
|
1740
|
+
/**
|
|
1741
|
+
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
1742
|
+
*/
|
|
1743
|
+
|
|
1744
|
+
/**
|
|
1745
|
+
* Checks if the value is [🚉] serializable as JSON
|
|
1746
|
+
* If not, throws an UnexpectedError with a rich error message and tracking
|
|
1747
|
+
*
|
|
1748
|
+
* - Almost all primitives are serializable BUT:
|
|
1749
|
+
* - `undefined` is not serializable
|
|
1750
|
+
* - `NaN` is not serializable
|
|
1751
|
+
* - Objects and arrays are serializable if all their properties are serializable
|
|
1752
|
+
* - Functions are not serializable
|
|
1753
|
+
* - Circular references are not serializable
|
|
1754
|
+
* - `Date` objects are not serializable
|
|
1755
|
+
* - `Map` and `Set` objects are not serializable
|
|
1756
|
+
* - `RegExp` objects are not serializable
|
|
1757
|
+
* - `Error` objects are not serializable
|
|
1758
|
+
* - `Symbol` objects are not serializable
|
|
1759
|
+
* - And much more...
|
|
1760
|
+
*
|
|
1761
|
+
* @throws UnexpectedError if the value is not serializable as JSON
|
|
1762
|
+
* @public exported from `@promptbook/utils`
|
|
1763
|
+
*/
|
|
1764
|
+
function checkSerializableAsJson(options) {
|
|
1765
|
+
var e_1, _a;
|
|
1766
|
+
var value = options.value, name = options.name, message = options.message;
|
|
1767
|
+
if (value === undefined) {
|
|
1768
|
+
throw new UnexpectedError("".concat(name, " is undefined"));
|
|
1769
|
+
}
|
|
1770
|
+
else if (value === null) {
|
|
1771
|
+
return;
|
|
1772
|
+
}
|
|
1773
|
+
else if (typeof value === 'boolean') {
|
|
1774
|
+
return;
|
|
1775
|
+
}
|
|
1776
|
+
else if (typeof value === 'number' && !isNaN(value)) {
|
|
1777
|
+
return;
|
|
1778
|
+
}
|
|
1779
|
+
else if (typeof value === 'string') {
|
|
1780
|
+
return;
|
|
1781
|
+
}
|
|
1782
|
+
else if (typeof value === 'symbol') {
|
|
1783
|
+
throw new UnexpectedError("".concat(name, " is symbol"));
|
|
1784
|
+
}
|
|
1785
|
+
else if (typeof value === 'function') {
|
|
1786
|
+
throw new UnexpectedError("".concat(name, " is function"));
|
|
1787
|
+
}
|
|
1788
|
+
else if (typeof value === 'object' && Array.isArray(value)) {
|
|
1789
|
+
for (var i = 0; i < value.length; i++) {
|
|
1790
|
+
checkSerializableAsJson({ name: "".concat(name, "[").concat(i, "]"), value: value[i], message: message });
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
else if (typeof value === 'object') {
|
|
1794
|
+
if (value instanceof Date) {
|
|
1795
|
+
throw new UnexpectedError(spaceTrim$1(function (block) { return "\n `".concat(name, "` is Date\n\n Use `string_date_iso8601` instead\n\n Additional message for `").concat(name, "`:\n ").concat(block(message || '(nothing)'), "\n "); }));
|
|
1796
|
+
}
|
|
1797
|
+
else if (value instanceof Map) {
|
|
1798
|
+
throw new UnexpectedError("".concat(name, " is Map"));
|
|
1799
|
+
}
|
|
1800
|
+
else if (value instanceof Set) {
|
|
1801
|
+
throw new UnexpectedError("".concat(name, " is Set"));
|
|
1802
|
+
}
|
|
1803
|
+
else if (value instanceof RegExp) {
|
|
1804
|
+
throw new UnexpectedError("".concat(name, " is RegExp"));
|
|
1805
|
+
}
|
|
1806
|
+
else if (value instanceof Error) {
|
|
1807
|
+
throw new UnexpectedError(spaceTrim$1(function (block) { return "\n `".concat(name, "` is unserialized Error\n\n Use function `serializeError`\n\n Additional message for `").concat(name, "`:\n ").concat(block(message || '(nothing)'), "\n\n "); }));
|
|
1808
|
+
}
|
|
1809
|
+
else {
|
|
1810
|
+
try {
|
|
1811
|
+
for (var _b = __values(Object.entries(value)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
1812
|
+
var _d = __read(_c.value, 2), subName = _d[0], subValue = _d[1];
|
|
1813
|
+
if (subValue === undefined) {
|
|
1814
|
+
// Note: undefined in object is serializable - it is just omited
|
|
1815
|
+
continue;
|
|
1816
|
+
}
|
|
1817
|
+
checkSerializableAsJson({ name: "".concat(name, ".").concat(subName), value: subValue, message: message });
|
|
1818
|
+
}
|
|
1819
|
+
}
|
|
1820
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1821
|
+
finally {
|
|
1822
|
+
try {
|
|
1823
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
1824
|
+
}
|
|
1825
|
+
finally { if (e_1) throw e_1.error; }
|
|
1826
|
+
}
|
|
1827
|
+
try {
|
|
1828
|
+
JSON.stringify(value); // <- TODO: [0]
|
|
1829
|
+
}
|
|
1830
|
+
catch (error) {
|
|
1831
|
+
if (!(error instanceof Error)) {
|
|
1832
|
+
throw error;
|
|
1833
|
+
}
|
|
1834
|
+
throw new UnexpectedError(spaceTrim$1(function (block) { return "\n `".concat(name, "` is not serializable\n\n ").concat(block(error.toString()), "\n\n Additional message for `").concat(name, "`:\n ").concat(block(message || '(nothing)'), "\n "); }));
|
|
1835
|
+
}
|
|
1836
|
+
/*
|
|
1837
|
+
TODO: [0] Is there some more elegant way to check circular references?
|
|
1838
|
+
const seen = new Set();
|
|
1839
|
+
const stack = [{ value }];
|
|
1840
|
+
while (stack.length > 0) {
|
|
1841
|
+
const { value } = stack.pop()!;
|
|
1842
|
+
if (typeof value === 'object' && value !== null) {
|
|
1843
|
+
if (seen.has(value)) {
|
|
1844
|
+
throw new UnexpectedError(`${name} has circular reference`);
|
|
1845
|
+
}
|
|
1846
|
+
seen.add(value);
|
|
1847
|
+
if (Array.isArray(value)) {
|
|
1848
|
+
stack.push(...value.map((value) => ({ value })));
|
|
1849
|
+
} else {
|
|
1850
|
+
stack.push(...Object.values(value).map((value) => ({ value })));
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
*/
|
|
1855
|
+
return;
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
else {
|
|
1859
|
+
throw new UnexpectedError(spaceTrim$1(function (block) { return "\n `".concat(name, "` is unknown type\n\n Additional message for `").concat(name, "`:\n ").concat(block(message || '(nothing)'), "\n "); }));
|
|
1860
|
+
}
|
|
1861
|
+
}
|
|
1862
|
+
/**
|
|
1863
|
+
* TODO: Can be return type more type-safe? like `asserts options.value is JsonValue`
|
|
1864
|
+
* TODO: [🧠][main] !!! In-memory cache of same values to prevent multiple checks
|
|
1865
|
+
* Note: [🐠] This is how `checkSerializableAsJson` + `isSerializableAsJson` together can just retun true/false or rich error message
|
|
1866
|
+
*/
|
|
1867
|
+
|
|
1868
|
+
/**
|
|
1869
|
+
* @@@
|
|
1870
|
+
*
|
|
1871
|
+
* @public exported from `@promptbook/utils`
|
|
1872
|
+
*/
|
|
1873
|
+
function deepClone(objectValue) {
|
|
1874
|
+
return JSON.parse(JSON.stringify(objectValue));
|
|
1875
|
+
/*
|
|
1876
|
+
!!!!!!!!
|
|
1877
|
+
TODO: [🧠] Is there a better implementation?
|
|
1878
|
+
> const propertyNames = Object.getOwnPropertyNames(objectValue);
|
|
1879
|
+
> for (const propertyName of propertyNames) {
|
|
1880
|
+
> const value = (objectValue as really_any)[propertyName];
|
|
1881
|
+
> if (value && typeof value === 'object') {
|
|
1882
|
+
> deepClone(value);
|
|
1883
|
+
> }
|
|
1884
|
+
> }
|
|
1885
|
+
> return Object.assign({}, objectValue);
|
|
1886
|
+
*/
|
|
1887
|
+
}
|
|
1888
|
+
/**
|
|
1889
|
+
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
1890
|
+
*/
|
|
1891
|
+
|
|
1892
|
+
/**
|
|
1893
|
+
* Utility to export a JSON object from a function
|
|
1894
|
+
*
|
|
1895
|
+
* 1) Checks if the value is serializable as JSON
|
|
1896
|
+
* 2) Makes a deep clone of the object
|
|
1897
|
+
* 2) Orders the object properties
|
|
1898
|
+
* 2) Deeply freezes the cloned object
|
|
1899
|
+
*
|
|
1900
|
+
* Note: This function does not mutates the given object
|
|
1901
|
+
*
|
|
1902
|
+
* @returns The same type of object as the input but read-only and re-ordered
|
|
1903
|
+
* @public exported from `@promptbook/utils`
|
|
1904
|
+
*/
|
|
1905
|
+
function exportJson(options) {
|
|
1906
|
+
var name = options.name, value = options.value, order = options.order, message = options.message;
|
|
1907
|
+
checkSerializableAsJson({ name: name, value: value, message: message });
|
|
1908
|
+
var orderedValue =
|
|
1909
|
+
// TODO: Fix error "Type instantiation is excessively deep and possibly infinite."
|
|
1910
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
1911
|
+
// @ts-ignore
|
|
1912
|
+
order === undefined
|
|
1913
|
+
? deepClone(value)
|
|
1914
|
+
: orderJson({
|
|
1915
|
+
value: value,
|
|
1916
|
+
// <- Note: checkSerializableAsJson asserts that the value is serializable as JSON
|
|
1917
|
+
order: order,
|
|
1918
|
+
});
|
|
1919
|
+
$deepFreeze(orderedValue);
|
|
1920
|
+
return orderedValue;
|
|
1921
|
+
}
|
|
1922
|
+
/**
|
|
1923
|
+
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
1924
|
+
*/
|
|
1925
|
+
|
|
1926
|
+
/**
|
|
1927
|
+
* Nonce which is used for replacing things in strings
|
|
1928
|
+
*
|
|
1929
|
+
* @private within the repository
|
|
1930
|
+
*/
|
|
1931
|
+
var REPLACING_NONCE = 'u$k42k%!V2zo34w7Fu#@QUHYPW';
|
|
1932
|
+
/**
|
|
1933
|
+
* @@@
|
|
1934
|
+
*
|
|
1935
|
+
* @private within the repository
|
|
1936
|
+
*/
|
|
1937
|
+
var RESERVED_PARAMETER_MISSING_VALUE = 'MISSING-' + REPLACING_NONCE;
|
|
1938
|
+
/**
|
|
1939
|
+
* @@@
|
|
1940
|
+
*
|
|
1941
|
+
* @private within the repository
|
|
1942
|
+
*/
|
|
1943
|
+
var RESERVED_PARAMETER_RESTRICTED = 'RESTRICTED-' + REPLACING_NONCE;
|
|
1944
|
+
/**
|
|
1945
|
+
* The names of the parameters that are reserved for special purposes
|
|
1946
|
+
*
|
|
1947
|
+
* @public exported from `@promptbook/core`
|
|
1948
|
+
*/
|
|
1949
|
+
exportJson({
|
|
1950
|
+
name: 'RESERVED_PARAMETER_NAMES',
|
|
1951
|
+
message: "The names of the parameters that are reserved for special purposes",
|
|
1952
|
+
value: [
|
|
1953
|
+
'content',
|
|
1954
|
+
'context',
|
|
1955
|
+
'knowledge',
|
|
1956
|
+
'examples',
|
|
1957
|
+
'modelName',
|
|
1958
|
+
'currentDate',
|
|
1959
|
+
// <- TODO: list here all command names
|
|
1960
|
+
// <- TODO: Add more like 'date', 'modelName',...
|
|
1961
|
+
// <- TODO: Add [emoji] + instructions ACRY when adding new reserved parameter
|
|
1962
|
+
],
|
|
1963
|
+
});
|
|
1964
|
+
/**
|
|
1965
|
+
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
1966
|
+
*/
|
|
1967
|
+
|
|
1713
1968
|
/**
|
|
1714
1969
|
* Replaces parameters in template with values from parameters object
|
|
1715
1970
|
*
|
|
@@ -1891,163 +2146,6 @@ function removeQuotes(text) {
|
|
|
1891
2146
|
return text;
|
|
1892
2147
|
}
|
|
1893
2148
|
|
|
1894
|
-
/**
|
|
1895
|
-
* @@@
|
|
1896
|
-
*
|
|
1897
|
-
* Note: `$` is used to indicate that this function is not a pure function - it mutates given object
|
|
1898
|
-
* Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
|
|
1899
|
-
*
|
|
1900
|
-
* @returns The same object as the input, but deeply frozen
|
|
1901
|
-
* @public exported from `@promptbook/utils`
|
|
1902
|
-
*/
|
|
1903
|
-
function $deepFreeze(objectValue) {
|
|
1904
|
-
var e_1, _a;
|
|
1905
|
-
var propertyNames = Object.getOwnPropertyNames(objectValue);
|
|
1906
|
-
try {
|
|
1907
|
-
for (var propertyNames_1 = __values(propertyNames), propertyNames_1_1 = propertyNames_1.next(); !propertyNames_1_1.done; propertyNames_1_1 = propertyNames_1.next()) {
|
|
1908
|
-
var propertyName = propertyNames_1_1.value;
|
|
1909
|
-
var value = objectValue[propertyName];
|
|
1910
|
-
if (value && typeof value === 'object') {
|
|
1911
|
-
$deepFreeze(value);
|
|
1912
|
-
}
|
|
1913
|
-
}
|
|
1914
|
-
}
|
|
1915
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1916
|
-
finally {
|
|
1917
|
-
try {
|
|
1918
|
-
if (propertyNames_1_1 && !propertyNames_1_1.done && (_a = propertyNames_1.return)) _a.call(propertyNames_1);
|
|
1919
|
-
}
|
|
1920
|
-
finally { if (e_1) throw e_1.error; }
|
|
1921
|
-
}
|
|
1922
|
-
return Object.freeze(objectValue);
|
|
1923
|
-
}
|
|
1924
|
-
/**
|
|
1925
|
-
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
1926
|
-
*/
|
|
1927
|
-
|
|
1928
|
-
/**
|
|
1929
|
-
* Checks if the value is [🚉] serializable as JSON
|
|
1930
|
-
* If not, throws an UnexpectedError with a rich error message and tracking
|
|
1931
|
-
*
|
|
1932
|
-
* - Almost all primitives are serializable BUT:
|
|
1933
|
-
* - `undefined` is not serializable
|
|
1934
|
-
* - `NaN` is not serializable
|
|
1935
|
-
* - Objects and arrays are serializable if all their properties are serializable
|
|
1936
|
-
* - Functions are not serializable
|
|
1937
|
-
* - Circular references are not serializable
|
|
1938
|
-
* - `Date` objects are not serializable
|
|
1939
|
-
* - `Map` and `Set` objects are not serializable
|
|
1940
|
-
* - `RegExp` objects are not serializable
|
|
1941
|
-
* - `Error` objects are not serializable
|
|
1942
|
-
* - `Symbol` objects are not serializable
|
|
1943
|
-
* - And much more...
|
|
1944
|
-
*
|
|
1945
|
-
* @throws UnexpectedError if the value is not serializable as JSON
|
|
1946
|
-
* @public exported from `@promptbook/utils`
|
|
1947
|
-
*/
|
|
1948
|
-
function checkSerializableAsJson(name, value) {
|
|
1949
|
-
var e_1, _a;
|
|
1950
|
-
if (value === undefined) {
|
|
1951
|
-
throw new UnexpectedError("".concat(name, " is undefined"));
|
|
1952
|
-
}
|
|
1953
|
-
else if (value === null) {
|
|
1954
|
-
return;
|
|
1955
|
-
}
|
|
1956
|
-
else if (typeof value === 'boolean') {
|
|
1957
|
-
return;
|
|
1958
|
-
}
|
|
1959
|
-
else if (typeof value === 'number' && !isNaN(value)) {
|
|
1960
|
-
return;
|
|
1961
|
-
}
|
|
1962
|
-
else if (typeof value === 'string') {
|
|
1963
|
-
return;
|
|
1964
|
-
}
|
|
1965
|
-
else if (typeof value === 'symbol') {
|
|
1966
|
-
throw new UnexpectedError("".concat(name, " is symbol"));
|
|
1967
|
-
}
|
|
1968
|
-
else if (typeof value === 'function') {
|
|
1969
|
-
throw new UnexpectedError("".concat(name, " is function"));
|
|
1970
|
-
}
|
|
1971
|
-
else if (typeof value === 'object' && Array.isArray(value)) {
|
|
1972
|
-
for (var i = 0; i < value.length; i++) {
|
|
1973
|
-
checkSerializableAsJson("".concat(name, "[").concat(i, "]"), value[i]);
|
|
1974
|
-
}
|
|
1975
|
-
}
|
|
1976
|
-
else if (typeof value === 'object') {
|
|
1977
|
-
if (value instanceof Date) {
|
|
1978
|
-
throw new UnexpectedError(spaceTrim$1("\n ".concat(name, " is Date\n\n Use `string_date_iso8601` instead\n ")));
|
|
1979
|
-
}
|
|
1980
|
-
else if (value instanceof Map) {
|
|
1981
|
-
throw new UnexpectedError("".concat(name, " is Map"));
|
|
1982
|
-
}
|
|
1983
|
-
else if (value instanceof Set) {
|
|
1984
|
-
throw new UnexpectedError("".concat(name, " is Set"));
|
|
1985
|
-
}
|
|
1986
|
-
else if (value instanceof RegExp) {
|
|
1987
|
-
throw new UnexpectedError("".concat(name, " is RegExp"));
|
|
1988
|
-
}
|
|
1989
|
-
else if (value instanceof Error) {
|
|
1990
|
-
throw new UnexpectedError(spaceTrim$1("\n ".concat(name, " is unserialized Error\n\n Use function `serializeError`\n ")));
|
|
1991
|
-
}
|
|
1992
|
-
else {
|
|
1993
|
-
try {
|
|
1994
|
-
for (var _b = __values(Object.entries(value)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
1995
|
-
var _d = __read(_c.value, 2), subName = _d[0], subValue = _d[1];
|
|
1996
|
-
if (subValue === undefined) {
|
|
1997
|
-
// Note: undefined in object is serializable - it is just omited
|
|
1998
|
-
continue;
|
|
1999
|
-
}
|
|
2000
|
-
checkSerializableAsJson("".concat(name, ".").concat(subName), subValue);
|
|
2001
|
-
}
|
|
2002
|
-
}
|
|
2003
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2004
|
-
finally {
|
|
2005
|
-
try {
|
|
2006
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
2007
|
-
}
|
|
2008
|
-
finally { if (e_1) throw e_1.error; }
|
|
2009
|
-
}
|
|
2010
|
-
try {
|
|
2011
|
-
JSON.stringify(value); // <- TODO: [0]
|
|
2012
|
-
}
|
|
2013
|
-
catch (error) {
|
|
2014
|
-
if (!(error instanceof Error)) {
|
|
2015
|
-
throw error;
|
|
2016
|
-
}
|
|
2017
|
-
throw new UnexpectedError(spaceTrim$1(function (block) { return "\n ".concat(name, " is not serializable\n\n ").concat(block(error.toString()), "\n "); }));
|
|
2018
|
-
}
|
|
2019
|
-
/*
|
|
2020
|
-
TODO: [0] Is there some more elegant way to check circular references?
|
|
2021
|
-
const seen = new Set();
|
|
2022
|
-
const stack = [{ value }];
|
|
2023
|
-
while (stack.length > 0) {
|
|
2024
|
-
const { value } = stack.pop()!;
|
|
2025
|
-
if (typeof value === 'object' && value !== null) {
|
|
2026
|
-
if (seen.has(value)) {
|
|
2027
|
-
throw new UnexpectedError(`${name} has circular reference`);
|
|
2028
|
-
}
|
|
2029
|
-
seen.add(value);
|
|
2030
|
-
if (Array.isArray(value)) {
|
|
2031
|
-
stack.push(...value.map((value) => ({ value })));
|
|
2032
|
-
} else {
|
|
2033
|
-
stack.push(...Object.values(value).map((value) => ({ value })));
|
|
2034
|
-
}
|
|
2035
|
-
}
|
|
2036
|
-
}
|
|
2037
|
-
*/
|
|
2038
|
-
return;
|
|
2039
|
-
}
|
|
2040
|
-
}
|
|
2041
|
-
else {
|
|
2042
|
-
throw new UnexpectedError("".concat(name, " is unknown"));
|
|
2043
|
-
}
|
|
2044
|
-
}
|
|
2045
|
-
/**
|
|
2046
|
-
* TODO: [🧠][🛣] More elegant way to tracking than passing `name`
|
|
2047
|
-
* TODO: [🧠][main] !!! In-memory cache of same values to prevent multiple checks
|
|
2048
|
-
* Note: [🐠] This is how `checkSerializableAsJson` + `isSerializableAsJson` together can just retun true/false or rich error message
|
|
2049
|
-
*/
|
|
2050
|
-
|
|
2051
2149
|
/**
|
|
2052
2150
|
* @@@
|
|
2053
2151
|
*
|
|
@@ -2078,29 +2176,7 @@ function clonePipeline(pipeline) {
|
|
|
2078
2176
|
* TODO: [🍙] Make some standard order of json properties
|
|
2079
2177
|
*/
|
|
2080
2178
|
|
|
2081
|
-
|
|
2082
|
-
* @@@
|
|
2083
|
-
*
|
|
2084
|
-
* @public exported from `@promptbook/utils`
|
|
2085
|
-
*/
|
|
2086
|
-
function deepClone(objectValue) {
|
|
2087
|
-
return JSON.parse(JSON.stringify(objectValue));
|
|
2088
|
-
/*
|
|
2089
|
-
TODO: [🧠] Is there a better implementation?
|
|
2090
|
-
> const propertyNames = Object.getOwnPropertyNames(objectValue);
|
|
2091
|
-
> for (const propertyName of propertyNames) {
|
|
2092
|
-
> const value = (objectValue as really_any)[propertyName];
|
|
2093
|
-
> if (value && typeof value === 'object') {
|
|
2094
|
-
> deepClone(value);
|
|
2095
|
-
> }
|
|
2096
|
-
> }
|
|
2097
|
-
> return Object.assign({}, objectValue);
|
|
2098
|
-
*/
|
|
2099
|
-
}
|
|
2100
|
-
/**
|
|
2101
|
-
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
2102
|
-
*/
|
|
2103
|
-
|
|
2179
|
+
// <- TODO: !!!!!!! Auto convert to type `import { ... } from 'type-fest';`
|
|
2104
2180
|
/**
|
|
2105
2181
|
* Tests if the value is [🚉] serializable as JSON
|
|
2106
2182
|
*
|
|
@@ -2122,7 +2198,7 @@ function deepClone(objectValue) {
|
|
|
2122
2198
|
*/
|
|
2123
2199
|
function isSerializableAsJson(value) {
|
|
2124
2200
|
try {
|
|
2125
|
-
checkSerializableAsJson(
|
|
2201
|
+
checkSerializableAsJson({ value: value });
|
|
2126
2202
|
return true;
|
|
2127
2203
|
}
|
|
2128
2204
|
catch (error) {
|
|
@@ -2518,5 +2594,5 @@ function isValidUuid(value) {
|
|
|
2518
2594
|
return /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i.test(value);
|
|
2519
2595
|
}
|
|
2520
2596
|
|
|
2521
|
-
export { $deepFreeze, $getCurrentDate, $isRunningInBrowser, $isRunningInJest, $isRunningInNode, $isRunningInWebWorker, $randomSeed, BOOK_LANGUAGE_VERSION, CHARACTERS_PER_STANDARD_LINE, CountUtils, DIACRITIC_VARIANTS_LETTERS, LINES_PER_STANDARD_PAGE, PROMPTBOOK_ENGINE_VERSION, capitalize, checkSerializableAsJson, clonePipeline, countCharacters, countLines, countPages, countParagraphs, countSentences, countWords, decapitalize, deepClone, deserializeError, difference, extractParameterNames, extractVariablesFromScript, forEachAsync, intersection, isHostnameOnPrivateNetwork, isSerializableAsJson, isUrlOnPrivateNetwork, isValidEmail, isValidFilePath, isValidJavascriptName, isValidJsonString, isValidKeyword, isValidPipelineUrl, isValidPromptbookVersion, isValidSemanticVersion, isValidUrl, isValidUuid, nameToUriPart, nameToUriParts, normalizeToKebabCase, normalizeTo_PascalCase, normalizeTo_SCREAMING_CASE, normalizeTo_camelCase, normalizeTo_snake_case, normalizeWhitespaces, parseKeywords, parseKeywordsFromString, parseNumber, removeDiacritics, removeEmojis, removeQuotes, renderPromptbookMermaid, replaceParameters, searchKeywords, serializeError, spaceTrim, splitIntoSentences, titleToName, trimCodeBlock, trimEndOfCodeBlock, union, unwrapResult };
|
|
2597
|
+
export { $deepFreeze, $getCurrentDate, $isRunningInBrowser, $isRunningInJest, $isRunningInNode, $isRunningInWebWorker, $randomSeed, BOOK_LANGUAGE_VERSION, CHARACTERS_PER_STANDARD_LINE, CountUtils, DIACRITIC_VARIANTS_LETTERS, LINES_PER_STANDARD_PAGE, PROMPTBOOK_ENGINE_VERSION, capitalize, checkSerializableAsJson, clonePipeline, countCharacters, countLines, countPages, countParagraphs, countSentences, countWords, decapitalize, deepClone, deserializeError, difference, exportJson, extractParameterNames, extractVariablesFromScript, forEachAsync, intersection, isHostnameOnPrivateNetwork, isSerializableAsJson, isUrlOnPrivateNetwork, isValidEmail, isValidFilePath, isValidJavascriptName, isValidJsonString, isValidKeyword, isValidPipelineUrl, isValidPromptbookVersion, isValidSemanticVersion, isValidUrl, isValidUuid, nameToUriPart, nameToUriParts, normalizeToKebabCase, normalizeTo_PascalCase, normalizeTo_SCREAMING_CASE, normalizeTo_camelCase, normalizeTo_snake_case, normalizeWhitespaces, orderJson, parseKeywords, parseKeywordsFromString, parseNumber, removeDiacritics, removeEmojis, removeQuotes, renderPromptbookMermaid, replaceParameters, searchKeywords, serializeError, spaceTrim, splitIntoSentences, titleToName, trimCodeBlock, trimEndOfCodeBlock, union, unwrapResult };
|
|
2522
2598
|
//# sourceMappingURL=index.es.js.map
|