@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/README.md CHANGED
@@ -23,10 +23,6 @@
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
-
30
26
  ## 📦 Package `@promptbook/utils`
31
27
 
32
28
  - Promptbooks are [divided into several](#-packages) packages, all are published from [single monorepo](https://github.com/webgptorg/promptbook).
package/esm/index.es.js CHANGED
@@ -1,4 +1,4 @@
1
- import spaceTrim$2, { spaceTrim as spaceTrim$1 } from 'spacetrim';
1
+ import spaceTrim$1, { spaceTrim as spaceTrim$2 } from 'spacetrim';
2
2
  import { basename } from 'path';
3
3
 
4
4
  // ⚠️ WARNING: This code has been generated so that any manual changes will be overwritten
@@ -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.77.1';
16
+ var PROMPTBOOK_ENGINE_VERSION = '0.78.1';
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
@@ -138,6 +138,87 @@ function __spreadArray(to, from, pack) {
138
138
  return to.concat(ar || Array.prototype.slice.call(from));
139
139
  }
140
140
 
141
+ /**
142
+ * Name for the Promptbook
143
+ *
144
+ * TODO: [🗽] Unite branding and make single place for it
145
+ *
146
+ * @public exported from `@promptbook/core`
147
+ */
148
+ var NAME = "Promptbook";
149
+ /**
150
+ * Email of the responsible person
151
+ *
152
+ * @public exported from `@promptbook/core`
153
+ */
154
+ var ADMIN_EMAIL = 'me@pavolhejny.com';
155
+ /**
156
+ * Name of the responsible person for the Promptbook on GitHub
157
+ *
158
+ * @public exported from `@promptbook/core`
159
+ */
160
+ var ADMIN_GITHUB_NAME = 'hejny';
161
+ // <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
162
+ /**
163
+ * The maximum number of iterations for a loops
164
+ *
165
+ * @private within the repository - too low-level in comparison with other `MAX_...`
166
+ */
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
+ // <- TODO: [🧜‍♂️]
187
+ /**
188
+ * @@@
189
+ *
190
+ * @public exported from `@promptbook/core`
191
+ */
192
+ Object.freeze({
193
+ delimiter: ',',
194
+ quoteChar: '"',
195
+ newline: '\n',
196
+ skipEmptyLines: true,
197
+ });
198
+ /**
199
+ * TODO: Extract `constants.ts` from `config.ts`
200
+ * Note: [💞] Ignore a discrepancy between file name and entity name
201
+ * TODO: [🧠][🧜‍♂️] Maybe join remoteUrl and path into single value
202
+ */
203
+
204
+ /**
205
+ * Make error report URL for the given error
206
+ *
207
+ * @private !!!!!!
208
+ */
209
+ function getErrorReportUrl(error) {
210
+ var report = {
211
+ title: "\uD83D\uDC1C Error report from ".concat(NAME),
212
+ body: spaceTrim$1(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 "); }),
213
+ };
214
+ var reportUrl = new URL("https://github.com/webgptorg/promptbook/issues/new");
215
+ reportUrl.searchParams.set('labels', 'bug');
216
+ reportUrl.searchParams.set('assignees', ADMIN_GITHUB_NAME);
217
+ reportUrl.searchParams.set('title', report.title);
218
+ reportUrl.searchParams.set('body', report.body);
219
+ return reportUrl;
220
+ }
221
+
141
222
  /**
142
223
  * This error type indicates that the error should not happen and its last check before crashing with some other error
143
224
  *
@@ -146,7 +227,7 @@ function __spreadArray(to, from, pack) {
146
227
  var UnexpectedError = /** @class */ (function (_super) {
147
228
  __extends(UnexpectedError, _super);
148
229
  function UnexpectedError(message) {
149
- var _this = _super.call(this, spaceTrim$1(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;
230
+ var _this = _super.call(this, spaceTrim$2(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;
150
231
  _this.name = 'UnexpectedError';
151
232
  Object.setPrototypeOf(_this, UnexpectedError.prototype);
152
233
  return _this;
@@ -660,7 +741,7 @@ function renderPromptbookMermaid(pipelineJson, options) {
660
741
  }
661
742
  return task.name || normalizeTo_camelCase('task-' + titleToName(task.title));
662
743
  };
663
- var promptbookMermaid = spaceTrim$1(function (block) { return "\n\n %% \uD83D\uDD2E Tip: Open this on GitHub or in the VSCode website to see the Mermaid graph visually\n\n flowchart LR\n subgraph \"".concat(pipelineJson.title, "\"\n\n direction TB\n\n input((Input)):::input\n ").concat(block(pipelineJson.tasks
744
+ var promptbookMermaid = spaceTrim$2(function (block) { return "\n\n %% \uD83D\uDD2E Tip: Open this on GitHub or in the VSCode website to see the Mermaid graph visually\n\n flowchart LR\n subgraph \"".concat(pipelineJson.title, "\"\n\n direction TB\n\n input((Input)):::input\n ").concat(block(pipelineJson.tasks
664
745
  .flatMap(function (_a) {
665
746
  var title = _a.title, dependentParameterNames = _a.dependentParameterNames, resultingParameterName = _a.resultingParameterName;
666
747
  return __spreadArray([
@@ -729,6 +810,7 @@ var ParseError = /** @class */ (function (_super) {
729
810
  */
730
811
  function extractVariablesFromScript(script) {
731
812
  var variables = new Set();
813
+ var originalScript = script;
732
814
  script = "(()=>{".concat(script, "})()");
733
815
  try {
734
816
  for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
@@ -760,7 +842,9 @@ function extractVariablesFromScript(script) {
760
842
  if (!(error instanceof Error)) {
761
843
  throw error;
762
844
  }
763
- throw new ParseError(spaceTrim$1(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.toString()), "}\n "); }));
845
+ throw new ParseError(spaceTrim$2(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)
846
+ .map(function (variableName, i) { return "".concat(i + 1, ") ").concat(variableName); })
847
+ .join('\n'), "\n\n\n The script:\n\n ```javascript\n ").concat(block(originalScript), "\n ```\n "); }));
764
848
  }
765
849
  return variables;
766
850
  }
@@ -895,7 +979,7 @@ var LimitReachedError = /** @class */ (function (_super) {
895
979
  var MissingToolsError = /** @class */ (function (_super) {
896
980
  __extends(MissingToolsError, _super);
897
981
  function MissingToolsError(message) {
898
- var _this = _super.call(this, spaceTrim$1(function (block) { return "\n ".concat(block(message), "\n\n Note: You have probbably forgot to provide some tools for pipeline execution or preparation\n\n "); })) || this;
982
+ var _this = _super.call(this, spaceTrim$2(function (block) { return "\n ".concat(block(message), "\n\n Note: You have probbably forgot to provide some tools for pipeline execution or preparation\n\n "); })) || this;
899
983
  _this.name = 'MissingToolsError';
900
984
  Object.setPrototypeOf(_this, MissingToolsError.prototype);
901
985
  return _this;
@@ -927,7 +1011,7 @@ var NotFoundError = /** @class */ (function (_super) {
927
1011
  var NotYetImplementedError = /** @class */ (function (_super) {
928
1012
  __extends(NotYetImplementedError, _super);
929
1013
  function NotYetImplementedError(message) {
930
- var _this = _super.call(this, spaceTrim$1(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;
1014
+ var _this = _super.call(this, spaceTrim$2(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;
931
1015
  _this.name = 'NotYetImplementedError';
932
1016
  Object.setPrototypeOf(_this, NotYetImplementedError.prototype);
933
1017
  return _this;
@@ -1060,7 +1144,7 @@ function deserializeError(error) {
1060
1144
  function serializeError(error) {
1061
1145
  var name = error.name, message = error.message, stack = error.stack;
1062
1146
  if (!Object.keys(ALL_ERRORS).includes(name)) {
1063
- console.error(spaceTrim$2(function (block) { return "\n \n Cannot serialize error with name \"".concat(name, "\"\n\n ").concat(block(stack || message), "\n \n "); }));
1147
+ console.error(spaceTrim$1(function (block) { return "\n \n Cannot serialize error with name \"".concat(name, "\"\n\n ").concat(block(stack || message), "\n \n "); }));
1064
1148
  }
1065
1149
  return {
1066
1150
  name: name,
@@ -1584,7 +1668,7 @@ function searchKeywords(haystack, needle) {
1584
1668
  * @public exported from `@promptbook/utils`
1585
1669
  * @see https://github.com/hejny/spacetrim#usage
1586
1670
  */
1587
- var spaceTrim = spaceTrim$1;
1671
+ var spaceTrim = spaceTrim$2;
1588
1672
 
1589
1673
  /**
1590
1674
  * Parses the task and returns the list of all parameter names
@@ -1614,242 +1698,6 @@ function extractParameterNames(template) {
1614
1698
  return parameterNames;
1615
1699
  }
1616
1700
 
1617
- /**
1618
- * @@@
1619
- *
1620
- * Note: `$` is used to indicate that this function is not a pure function - it mutates given object
1621
- * Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
1622
- *
1623
- * @returns The same object as the input, but deeply frozen
1624
- * @public exported from `@promptbook/utils`
1625
- */
1626
- function $deepFreeze(objectValue) {
1627
- var e_1, _a;
1628
- var propertyNames = Object.getOwnPropertyNames(objectValue);
1629
- try {
1630
- for (var propertyNames_1 = __values(propertyNames), propertyNames_1_1 = propertyNames_1.next(); !propertyNames_1_1.done; propertyNames_1_1 = propertyNames_1.next()) {
1631
- var propertyName = propertyNames_1_1.value;
1632
- var value = objectValue[propertyName];
1633
- if (value && typeof value === 'object') {
1634
- $deepFreeze(value);
1635
- }
1636
- }
1637
- }
1638
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
1639
- finally {
1640
- try {
1641
- if (propertyNames_1_1 && !propertyNames_1_1.done && (_a = propertyNames_1.return)) _a.call(propertyNames_1);
1642
- }
1643
- finally { if (e_1) throw e_1.error; }
1644
- }
1645
- return Object.freeze(objectValue);
1646
- }
1647
- /**
1648
- * TODO: [🧠] Is there a way how to meaningfully test this utility
1649
- */
1650
-
1651
- /**
1652
- * Checks if the value is [🚉] serializable as JSON
1653
- * If not, throws an UnexpectedError with a rich error message and tracking
1654
- *
1655
- * - Almost all primitives are serializable BUT:
1656
- * - `undefined` is not serializable
1657
- * - `NaN` is not serializable
1658
- * - Objects and arrays are serializable if all their properties are serializable
1659
- * - Functions are not serializable
1660
- * - Circular references are not serializable
1661
- * - `Date` objects are not serializable
1662
- * - `Map` and `Set` objects are not serializable
1663
- * - `RegExp` objects are not serializable
1664
- * - `Error` objects are not serializable
1665
- * - `Symbol` objects are not serializable
1666
- * - And much more...
1667
- *
1668
- * @throws UnexpectedError if the value is not serializable as JSON
1669
- * @public exported from `@promptbook/utils`
1670
- */
1671
- function checkSerializableAsJson(name, value) {
1672
- var e_1, _a;
1673
- if (value === undefined) {
1674
- throw new UnexpectedError("".concat(name, " is undefined"));
1675
- }
1676
- else if (value === null) {
1677
- return;
1678
- }
1679
- else if (typeof value === 'boolean') {
1680
- return;
1681
- }
1682
- else if (typeof value === 'number' && !isNaN(value)) {
1683
- return;
1684
- }
1685
- else if (typeof value === 'string') {
1686
- return;
1687
- }
1688
- else if (typeof value === 'symbol') {
1689
- throw new UnexpectedError("".concat(name, " is symbol"));
1690
- }
1691
- else if (typeof value === 'function') {
1692
- throw new UnexpectedError("".concat(name, " is function"));
1693
- }
1694
- else if (typeof value === 'object' && Array.isArray(value)) {
1695
- for (var i = 0; i < value.length; i++) {
1696
- checkSerializableAsJson("".concat(name, "[").concat(i, "]"), value[i]);
1697
- }
1698
- }
1699
- else if (typeof value === 'object') {
1700
- if (value instanceof Date) {
1701
- throw new UnexpectedError(spaceTrim$2("\n ".concat(name, " is Date\n\n Use `string_date_iso8601` instead\n ")));
1702
- }
1703
- else if (value instanceof Map) {
1704
- throw new UnexpectedError("".concat(name, " is Map"));
1705
- }
1706
- else if (value instanceof Set) {
1707
- throw new UnexpectedError("".concat(name, " is Set"));
1708
- }
1709
- else if (value instanceof RegExp) {
1710
- throw new UnexpectedError("".concat(name, " is RegExp"));
1711
- }
1712
- else if (value instanceof Error) {
1713
- throw new UnexpectedError(spaceTrim$2("\n ".concat(name, " is unserialized Error\n\n Use function `serializeError`\n ")));
1714
- }
1715
- else {
1716
- try {
1717
- for (var _b = __values(Object.entries(value)), _c = _b.next(); !_c.done; _c = _b.next()) {
1718
- var _d = __read(_c.value, 2), subName = _d[0], subValue = _d[1];
1719
- if (subValue === undefined) {
1720
- // Note: undefined in object is serializable - it is just omited
1721
- continue;
1722
- }
1723
- checkSerializableAsJson("".concat(name, ".").concat(subName), subValue);
1724
- }
1725
- }
1726
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
1727
- finally {
1728
- try {
1729
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1730
- }
1731
- finally { if (e_1) throw e_1.error; }
1732
- }
1733
- try {
1734
- JSON.stringify(value); // <- TODO: [0]
1735
- }
1736
- catch (error) {
1737
- if (!(error instanceof Error)) {
1738
- throw error;
1739
- }
1740
- throw new UnexpectedError(spaceTrim$2(function (block) { return "\n ".concat(name, " is not serializable\n\n ").concat(block(error.toString()), "\n "); }));
1741
- }
1742
- /*
1743
- TODO: [0] Is there some more elegant way to check circular references?
1744
- const seen = new Set();
1745
- const stack = [{ value }];
1746
- while (stack.length > 0) {
1747
- const { value } = stack.pop()!;
1748
- if (typeof value === 'object' && value !== null) {
1749
- if (seen.has(value)) {
1750
- throw new UnexpectedError(`${name} has circular reference`);
1751
- }
1752
- seen.add(value);
1753
- if (Array.isArray(value)) {
1754
- stack.push(...value.map((value) => ({ value })));
1755
- } else {
1756
- stack.push(...Object.values(value).map((value) => ({ value })));
1757
- }
1758
- }
1759
- }
1760
- */
1761
- return;
1762
- }
1763
- }
1764
- else {
1765
- throw new UnexpectedError("".concat(name, " is unknown"));
1766
- }
1767
- }
1768
- /**
1769
- * TODO: [🧠][🛣] More elegant way to tracking than passing `name`
1770
- * TODO: [🧠][main] !!! In-memory cache of same values to prevent multiple checks
1771
- * Note: [🐠] This is how `checkSerializableAsJson` + `isSerializableAsJson` together can just retun true/false or rich error message
1772
- */
1773
-
1774
- /**
1775
- * @@@
1776
- * @@@
1777
- *
1778
- * Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
1779
- *
1780
- * @param name - Name of the object for debugging purposes
1781
- * @param objectValue - Object to be deeply frozen
1782
- * @returns The same object as the input, but deeply frozen
1783
- * @private this is in comparison to `deepFreeze` a more specific utility and maybe not very good practice to use without specific reason and considerations
1784
- */
1785
- function $asDeeplyFrozenSerializableJson(name, objectValue) {
1786
- checkSerializableAsJson(name, objectValue);
1787
- return $deepFreeze(objectValue);
1788
- }
1789
- /**
1790
- * TODO: [🧠][🛣] More elegant way to tracking than passing `name`
1791
- * TODO: [🧠] Is there a way how to meaningfully test this utility
1792
- */
1793
-
1794
- // <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
1795
- /**
1796
- * The maximum number of iterations for a loops
1797
- *
1798
- * @private within the repository - too low-level in comparison with other `MAX_...`
1799
- */
1800
- var LOOP_LIMIT = 1000;
1801
- /**
1802
- * Nonce which is used for replacing things in strings
1803
- *
1804
- * @private within the repository
1805
- */
1806
- var REPLACING_NONCE = 'u$k42k%!V2zo34w7Fu#@QUHYPW';
1807
- /**
1808
- * The names of the parameters that are reserved for special purposes
1809
- *
1810
- * @public exported from `@promptbook/core`
1811
- */
1812
- $asDeeplyFrozenSerializableJson('RESERVED_PARAMETER_NAMES', [
1813
- 'content',
1814
- 'context',
1815
- 'knowledge',
1816
- 'examples',
1817
- 'modelName',
1818
- 'currentDate',
1819
- // <- TODO: list here all command names
1820
- // <- TODO: Add more like 'date', 'modelName',...
1821
- // <- TODO: Add [emoji] + instructions ACRY when adding new reserved parameter
1822
- ]);
1823
- /**
1824
- * @@@
1825
- *
1826
- * @private within the repository
1827
- */
1828
- var RESERVED_PARAMETER_MISSING_VALUE = 'MISSING-' + REPLACING_NONCE;
1829
- /**
1830
- * @@@
1831
- *
1832
- * @private within the repository
1833
- */
1834
- var RESERVED_PARAMETER_RESTRICTED = 'RESTRICTED-' + REPLACING_NONCE;
1835
- // <- TODO: [🧜‍♂️]
1836
- /**
1837
- * @@@
1838
- *
1839
- * @public exported from `@promptbook/core`
1840
- */
1841
- Object.freeze({
1842
- delimiter: ',',
1843
- quoteChar: '"',
1844
- newline: '\n',
1845
- skipEmptyLines: true,
1846
- });
1847
- /**
1848
- * TODO: Extract `constants.ts` from `config.ts`
1849
- * Note: [💞] Ignore a discrepancy between file name and entity name
1850
- * TODO: [🧠][🧜‍♂️] Maybe join remoteUrl and path into single value
1851
- */
1852
-
1853
1701
  /**
1854
1702
  * Replaces parameters in template with values from parameters object
1855
1703
  *
@@ -2031,6 +1879,163 @@ function removeQuotes(text) {
2031
1879
  return text;
2032
1880
  }
2033
1881
 
1882
+ /**
1883
+ * @@@
1884
+ *
1885
+ * Note: `$` is used to indicate that this function is not a pure function - it mutates given object
1886
+ * Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
1887
+ *
1888
+ * @returns The same object as the input, but deeply frozen
1889
+ * @public exported from `@promptbook/utils`
1890
+ */
1891
+ function $deepFreeze(objectValue) {
1892
+ var e_1, _a;
1893
+ var propertyNames = Object.getOwnPropertyNames(objectValue);
1894
+ try {
1895
+ for (var propertyNames_1 = __values(propertyNames), propertyNames_1_1 = propertyNames_1.next(); !propertyNames_1_1.done; propertyNames_1_1 = propertyNames_1.next()) {
1896
+ var propertyName = propertyNames_1_1.value;
1897
+ var value = objectValue[propertyName];
1898
+ if (value && typeof value === 'object') {
1899
+ $deepFreeze(value);
1900
+ }
1901
+ }
1902
+ }
1903
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1904
+ finally {
1905
+ try {
1906
+ if (propertyNames_1_1 && !propertyNames_1_1.done && (_a = propertyNames_1.return)) _a.call(propertyNames_1);
1907
+ }
1908
+ finally { if (e_1) throw e_1.error; }
1909
+ }
1910
+ return Object.freeze(objectValue);
1911
+ }
1912
+ /**
1913
+ * TODO: [🧠] Is there a way how to meaningfully test this utility
1914
+ */
1915
+
1916
+ /**
1917
+ * Checks if the value is [🚉] serializable as JSON
1918
+ * If not, throws an UnexpectedError with a rich error message and tracking
1919
+ *
1920
+ * - Almost all primitives are serializable BUT:
1921
+ * - `undefined` is not serializable
1922
+ * - `NaN` is not serializable
1923
+ * - Objects and arrays are serializable if all their properties are serializable
1924
+ * - Functions are not serializable
1925
+ * - Circular references are not serializable
1926
+ * - `Date` objects are not serializable
1927
+ * - `Map` and `Set` objects are not serializable
1928
+ * - `RegExp` objects are not serializable
1929
+ * - `Error` objects are not serializable
1930
+ * - `Symbol` objects are not serializable
1931
+ * - And much more...
1932
+ *
1933
+ * @throws UnexpectedError if the value is not serializable as JSON
1934
+ * @public exported from `@promptbook/utils`
1935
+ */
1936
+ function checkSerializableAsJson(name, value) {
1937
+ var e_1, _a;
1938
+ if (value === undefined) {
1939
+ throw new UnexpectedError("".concat(name, " is undefined"));
1940
+ }
1941
+ else if (value === null) {
1942
+ return;
1943
+ }
1944
+ else if (typeof value === 'boolean') {
1945
+ return;
1946
+ }
1947
+ else if (typeof value === 'number' && !isNaN(value)) {
1948
+ return;
1949
+ }
1950
+ else if (typeof value === 'string') {
1951
+ return;
1952
+ }
1953
+ else if (typeof value === 'symbol') {
1954
+ throw new UnexpectedError("".concat(name, " is symbol"));
1955
+ }
1956
+ else if (typeof value === 'function') {
1957
+ throw new UnexpectedError("".concat(name, " is function"));
1958
+ }
1959
+ else if (typeof value === 'object' && Array.isArray(value)) {
1960
+ for (var i = 0; i < value.length; i++) {
1961
+ checkSerializableAsJson("".concat(name, "[").concat(i, "]"), value[i]);
1962
+ }
1963
+ }
1964
+ else if (typeof value === 'object') {
1965
+ if (value instanceof Date) {
1966
+ throw new UnexpectedError(spaceTrim$1("\n ".concat(name, " is Date\n\n Use `string_date_iso8601` instead\n ")));
1967
+ }
1968
+ else if (value instanceof Map) {
1969
+ throw new UnexpectedError("".concat(name, " is Map"));
1970
+ }
1971
+ else if (value instanceof Set) {
1972
+ throw new UnexpectedError("".concat(name, " is Set"));
1973
+ }
1974
+ else if (value instanceof RegExp) {
1975
+ throw new UnexpectedError("".concat(name, " is RegExp"));
1976
+ }
1977
+ else if (value instanceof Error) {
1978
+ throw new UnexpectedError(spaceTrim$1("\n ".concat(name, " is unserialized Error\n\n Use function `serializeError`\n ")));
1979
+ }
1980
+ else {
1981
+ try {
1982
+ for (var _b = __values(Object.entries(value)), _c = _b.next(); !_c.done; _c = _b.next()) {
1983
+ var _d = __read(_c.value, 2), subName = _d[0], subValue = _d[1];
1984
+ if (subValue === undefined) {
1985
+ // Note: undefined in object is serializable - it is just omited
1986
+ continue;
1987
+ }
1988
+ checkSerializableAsJson("".concat(name, ".").concat(subName), subValue);
1989
+ }
1990
+ }
1991
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1992
+ finally {
1993
+ try {
1994
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1995
+ }
1996
+ finally { if (e_1) throw e_1.error; }
1997
+ }
1998
+ try {
1999
+ JSON.stringify(value); // <- TODO: [0]
2000
+ }
2001
+ catch (error) {
2002
+ if (!(error instanceof Error)) {
2003
+ throw error;
2004
+ }
2005
+ throw new UnexpectedError(spaceTrim$1(function (block) { return "\n ".concat(name, " is not serializable\n\n ").concat(block(error.toString()), "\n "); }));
2006
+ }
2007
+ /*
2008
+ TODO: [0] Is there some more elegant way to check circular references?
2009
+ const seen = new Set();
2010
+ const stack = [{ value }];
2011
+ while (stack.length > 0) {
2012
+ const { value } = stack.pop()!;
2013
+ if (typeof value === 'object' && value !== null) {
2014
+ if (seen.has(value)) {
2015
+ throw new UnexpectedError(`${name} has circular reference`);
2016
+ }
2017
+ seen.add(value);
2018
+ if (Array.isArray(value)) {
2019
+ stack.push(...value.map((value) => ({ value })));
2020
+ } else {
2021
+ stack.push(...Object.values(value).map((value) => ({ value })));
2022
+ }
2023
+ }
2024
+ }
2025
+ */
2026
+ return;
2027
+ }
2028
+ }
2029
+ else {
2030
+ throw new UnexpectedError("".concat(name, " is unknown"));
2031
+ }
2032
+ }
2033
+ /**
2034
+ * TODO: [🧠][🛣] More elegant way to tracking than passing `name`
2035
+ * TODO: [🧠][main] !!! In-memory cache of same values to prevent multiple checks
2036
+ * Note: [🐠] This is how `checkSerializableAsJson` + `isSerializableAsJson` together can just retun true/false or rich error message
2037
+ */
2038
+
2034
2039
  /**
2035
2040
  * @@@
2036
2041
  *
@@ -2241,13 +2246,13 @@ function union() {
2241
2246
  * @public exported from `@promptbook/utils`
2242
2247
  */
2243
2248
  function trimCodeBlock(value) {
2244
- value = spaceTrim$1(value);
2249
+ value = spaceTrim$2(value);
2245
2250
  if (!/^```[a-z]*(.*)```$/is.test(value)) {
2246
2251
  return value;
2247
2252
  }
2248
2253
  value = value.replace(/^```[a-z]*/i, '');
2249
2254
  value = value.replace(/```$/i, '');
2250
- value = spaceTrim$1(value);
2255
+ value = spaceTrim$2(value);
2251
2256
  return value;
2252
2257
  }
2253
2258
 
@@ -2260,9 +2265,9 @@ function trimCodeBlock(value) {
2260
2265
  * @public exported from `@promptbook/utils`
2261
2266
  */
2262
2267
  function trimEndOfCodeBlock(value) {
2263
- value = spaceTrim$1(value);
2268
+ value = spaceTrim$2(value);
2264
2269
  value = value.replace(/```$/g, '');
2265
- value = spaceTrim$1(value);
2270
+ value = spaceTrim$2(value);
2266
2271
  return value;
2267
2272
  }
2268
2273
 
@@ -2284,7 +2289,7 @@ function unwrapResult(text, options) {
2284
2289
  var trimmedText = text;
2285
2290
  // Remove leading and trailing spaces and newlines
2286
2291
  if (isTrimmed) {
2287
- trimmedText = spaceTrim$1(trimmedText);
2292
+ trimmedText = spaceTrim$2(trimmedText);
2288
2293
  }
2289
2294
  var processedText = trimmedText;
2290
2295
  if (isIntroduceSentenceRemoved) {
@@ -2293,7 +2298,7 @@ function unwrapResult(text, options) {
2293
2298
  // Remove the introduce sentence and quotes by replacing it with an empty string
2294
2299
  processedText = processedText.replace(introduceSentenceRegex, '');
2295
2300
  }
2296
- processedText = spaceTrim$1(processedText);
2301
+ processedText = spaceTrim$2(processedText);
2297
2302
  }
2298
2303
  if (processedText.length < 3) {
2299
2304
  return trimmedText;