@promptbook/utils 0.78.0-0 → 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/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.77.1';
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 https://github.com/webgptorg/promptbook/issues\n\n Or contact us on me@pavolhejny.com\n\n "); })) || this;
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;
@@ -736,6 +817,7 @@
736
817
  */
737
818
  function extractVariablesFromScript(script) {
738
819
  var variables = new Set();
820
+ var originalScript = script;
739
821
  script = "(()=>{".concat(script, "})()");
740
822
  try {
741
823
  for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
@@ -767,7 +849,9 @@
767
849
  if (!(error instanceof Error)) {
768
850
  throw error;
769
851
  }
770
- 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 "); }));
771
855
  }
772
856
  return variables;
773
857
  }
@@ -1621,242 +1705,6 @@
1621
1705
  return parameterNames;
1622
1706
  }
1623
1707
 
1624
- /**
1625
- * @@@
1626
- *
1627
- * Note: `$` is used to indicate that this function is not a pure function - it mutates given object
1628
- * Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
1629
- *
1630
- * @returns The same object as the input, but deeply frozen
1631
- * @public exported from `@promptbook/utils`
1632
- */
1633
- function $deepFreeze(objectValue) {
1634
- var e_1, _a;
1635
- var propertyNames = Object.getOwnPropertyNames(objectValue);
1636
- try {
1637
- for (var propertyNames_1 = __values(propertyNames), propertyNames_1_1 = propertyNames_1.next(); !propertyNames_1_1.done; propertyNames_1_1 = propertyNames_1.next()) {
1638
- var propertyName = propertyNames_1_1.value;
1639
- var value = objectValue[propertyName];
1640
- if (value && typeof value === 'object') {
1641
- $deepFreeze(value);
1642
- }
1643
- }
1644
- }
1645
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
1646
- finally {
1647
- try {
1648
- if (propertyNames_1_1 && !propertyNames_1_1.done && (_a = propertyNames_1.return)) _a.call(propertyNames_1);
1649
- }
1650
- finally { if (e_1) throw e_1.error; }
1651
- }
1652
- return Object.freeze(objectValue);
1653
- }
1654
- /**
1655
- * TODO: [🧠] Is there a way how to meaningfully test this utility
1656
- */
1657
-
1658
- /**
1659
- * Checks if the value is [🚉] serializable as JSON
1660
- * If not, throws an UnexpectedError with a rich error message and tracking
1661
- *
1662
- * - Almost all primitives are serializable BUT:
1663
- * - `undefined` is not serializable
1664
- * - `NaN` is not serializable
1665
- * - Objects and arrays are serializable if all their properties are serializable
1666
- * - Functions are not serializable
1667
- * - Circular references are not serializable
1668
- * - `Date` objects are not serializable
1669
- * - `Map` and `Set` objects are not serializable
1670
- * - `RegExp` objects are not serializable
1671
- * - `Error` objects are not serializable
1672
- * - `Symbol` objects are not serializable
1673
- * - And much more...
1674
- *
1675
- * @throws UnexpectedError if the value is not serializable as JSON
1676
- * @public exported from `@promptbook/utils`
1677
- */
1678
- function checkSerializableAsJson(name, value) {
1679
- var e_1, _a;
1680
- if (value === undefined) {
1681
- throw new UnexpectedError("".concat(name, " is undefined"));
1682
- }
1683
- else if (value === null) {
1684
- return;
1685
- }
1686
- else if (typeof value === 'boolean') {
1687
- return;
1688
- }
1689
- else if (typeof value === 'number' && !isNaN(value)) {
1690
- return;
1691
- }
1692
- else if (typeof value === 'string') {
1693
- return;
1694
- }
1695
- else if (typeof value === 'symbol') {
1696
- throw new UnexpectedError("".concat(name, " is symbol"));
1697
- }
1698
- else if (typeof value === 'function') {
1699
- throw new UnexpectedError("".concat(name, " is function"));
1700
- }
1701
- else if (typeof value === 'object' && Array.isArray(value)) {
1702
- for (var i = 0; i < value.length; i++) {
1703
- checkSerializableAsJson("".concat(name, "[").concat(i, "]"), value[i]);
1704
- }
1705
- }
1706
- else if (typeof value === 'object') {
1707
- if (value instanceof Date) {
1708
- throw new UnexpectedError(spaceTrim__default["default"]("\n ".concat(name, " is Date\n\n Use `string_date_iso8601` instead\n ")));
1709
- }
1710
- else if (value instanceof Map) {
1711
- throw new UnexpectedError("".concat(name, " is Map"));
1712
- }
1713
- else if (value instanceof Set) {
1714
- throw new UnexpectedError("".concat(name, " is Set"));
1715
- }
1716
- else if (value instanceof RegExp) {
1717
- throw new UnexpectedError("".concat(name, " is RegExp"));
1718
- }
1719
- else if (value instanceof Error) {
1720
- throw new UnexpectedError(spaceTrim__default["default"]("\n ".concat(name, " is unserialized Error\n\n Use function `serializeError`\n ")));
1721
- }
1722
- else {
1723
- try {
1724
- for (var _b = __values(Object.entries(value)), _c = _b.next(); !_c.done; _c = _b.next()) {
1725
- var _d = __read(_c.value, 2), subName = _d[0], subValue = _d[1];
1726
- if (subValue === undefined) {
1727
- // Note: undefined in object is serializable - it is just omited
1728
- continue;
1729
- }
1730
- checkSerializableAsJson("".concat(name, ".").concat(subName), subValue);
1731
- }
1732
- }
1733
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
1734
- finally {
1735
- try {
1736
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1737
- }
1738
- finally { if (e_1) throw e_1.error; }
1739
- }
1740
- try {
1741
- JSON.stringify(value); // <- TODO: [0]
1742
- }
1743
- catch (error) {
1744
- if (!(error instanceof Error)) {
1745
- throw error;
1746
- }
1747
- throw new UnexpectedError(spaceTrim__default["default"](function (block) { return "\n ".concat(name, " is not serializable\n\n ").concat(block(error.toString()), "\n "); }));
1748
- }
1749
- /*
1750
- TODO: [0] Is there some more elegant way to check circular references?
1751
- const seen = new Set();
1752
- const stack = [{ value }];
1753
- while (stack.length > 0) {
1754
- const { value } = stack.pop()!;
1755
- if (typeof value === 'object' && value !== null) {
1756
- if (seen.has(value)) {
1757
- throw new UnexpectedError(`${name} has circular reference`);
1758
- }
1759
- seen.add(value);
1760
- if (Array.isArray(value)) {
1761
- stack.push(...value.map((value) => ({ value })));
1762
- } else {
1763
- stack.push(...Object.values(value).map((value) => ({ value })));
1764
- }
1765
- }
1766
- }
1767
- */
1768
- return;
1769
- }
1770
- }
1771
- else {
1772
- throw new UnexpectedError("".concat(name, " is unknown"));
1773
- }
1774
- }
1775
- /**
1776
- * TODO: [🧠][🛣] More elegant way to tracking than passing `name`
1777
- * TODO: [🧠][main] !!! In-memory cache of same values to prevent multiple checks
1778
- * Note: [🐠] This is how `checkSerializableAsJson` + `isSerializableAsJson` together can just retun true/false or rich error message
1779
- */
1780
-
1781
- /**
1782
- * @@@
1783
- * @@@
1784
- *
1785
- * Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
1786
- *
1787
- * @param name - Name of the object for debugging purposes
1788
- * @param objectValue - Object to be deeply frozen
1789
- * @returns The same object as the input, but deeply frozen
1790
- * @private this is in comparison to `deepFreeze` a more specific utility and maybe not very good practice to use without specific reason and considerations
1791
- */
1792
- function $asDeeplyFrozenSerializableJson(name, objectValue) {
1793
- checkSerializableAsJson(name, objectValue);
1794
- return $deepFreeze(objectValue);
1795
- }
1796
- /**
1797
- * TODO: [🧠][🛣] More elegant way to tracking than passing `name`
1798
- * TODO: [🧠] Is there a way how to meaningfully test this utility
1799
- */
1800
-
1801
- // <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
1802
- /**
1803
- * The maximum number of iterations for a loops
1804
- *
1805
- * @private within the repository - too low-level in comparison with other `MAX_...`
1806
- */
1807
- var LOOP_LIMIT = 1000;
1808
- /**
1809
- * Nonce which is used for replacing things in strings
1810
- *
1811
- * @private within the repository
1812
- */
1813
- var REPLACING_NONCE = 'u$k42k%!V2zo34w7Fu#@QUHYPW';
1814
- /**
1815
- * The names of the parameters that are reserved for special purposes
1816
- *
1817
- * @public exported from `@promptbook/core`
1818
- */
1819
- $asDeeplyFrozenSerializableJson('RESERVED_PARAMETER_NAMES', [
1820
- 'content',
1821
- 'context',
1822
- 'knowledge',
1823
- 'examples',
1824
- 'modelName',
1825
- 'currentDate',
1826
- // <- TODO: list here all command names
1827
- // <- TODO: Add more like 'date', 'modelName',...
1828
- // <- TODO: Add [emoji] + instructions ACRY when adding new reserved parameter
1829
- ]);
1830
- /**
1831
- * @@@
1832
- *
1833
- * @private within the repository
1834
- */
1835
- var RESERVED_PARAMETER_MISSING_VALUE = 'MISSING-' + REPLACING_NONCE;
1836
- /**
1837
- * @@@
1838
- *
1839
- * @private within the repository
1840
- */
1841
- var RESERVED_PARAMETER_RESTRICTED = 'RESTRICTED-' + REPLACING_NONCE;
1842
- // <- TODO: [🧜‍♂️]
1843
- /**
1844
- * @@@
1845
- *
1846
- * @public exported from `@promptbook/core`
1847
- */
1848
- Object.freeze({
1849
- delimiter: ',',
1850
- quoteChar: '"',
1851
- newline: '\n',
1852
- skipEmptyLines: true,
1853
- });
1854
- /**
1855
- * TODO: Extract `constants.ts` from `config.ts`
1856
- * Note: [💞] Ignore a discrepancy between file name and entity name
1857
- * TODO: [🧠][🧜‍♂️] Maybe join remoteUrl and path into single value
1858
- */
1859
-
1860
1708
  /**
1861
1709
  * Replaces parameters in template with values from parameters object
1862
1710
  *
@@ -2038,6 +1886,163 @@
2038
1886
  return text;
2039
1887
  }
2040
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
+
2041
2046
  /**
2042
2047
  * @@@
2043
2048
  *