@promptbook/utils 0.78.0-0 → 0.78.3

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.2';
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++)
@@ -739,20 +821,32 @@ function extractVariablesFromScript(script) {
739
821
  if (!(error instanceof ReferenceError)) {
740
822
  throw error;
741
823
  }
742
- var undefinedName = error.message.split(' ')[0];
743
824
  /*
744
825
  Note: Parsing the error
826
+ 🌟 Most devices:
745
827
  [PipelineUrlError: thing is not defined]
828
+
829
+ 🍏 iPhone`s Safari:
830
+ [PipelineUrlError: Can't find variable: thing]
746
831
  */
747
- if (!undefinedName) {
832
+ var variableName = undefined;
833
+ if (error.message.startsWith("Can't")) {
834
+ // 🍏 Case
835
+ variableName = error.message.split(' ').pop();
836
+ }
837
+ else {
838
+ // 🌟 Case
839
+ variableName = error.message.split(' ').shift();
840
+ }
841
+ if (variableName === undefined) {
748
842
  throw error;
749
843
  }
750
- if (script.includes(undefinedName + '(')) {
751
- script = "const ".concat(undefinedName, " = ()=>'';") + script;
844
+ if (script.includes(variableName + '(')) {
845
+ script = "const ".concat(variableName, " = ()=>'';") + script;
752
846
  }
753
847
  else {
754
- variables.add(undefinedName);
755
- script = "const ".concat(undefinedName, " = '';") + script;
848
+ variables.add(variableName);
849
+ script = "const ".concat(variableName, " = '';") + script;
756
850
  }
757
851
  }
758
852
  }
@@ -760,7 +854,9 @@ function extractVariablesFromScript(script) {
760
854
  if (!(error instanceof Error)) {
761
855
  throw error;
762
856
  }
763
- throw new ParseError(spaceTrim$1(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.toString()), "}\n "); }));
857
+ 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)
858
+ .map(function (variableName, i) { return "".concat(i + 1, ") ").concat(variableName); })
859
+ .join('\n'), "\n\n\n The script:\n\n ```javascript\n ").concat(block(originalScript), "\n ```\n "); }));
764
860
  }
765
861
  return variables;
766
862
  }
@@ -895,7 +991,7 @@ var LimitReachedError = /** @class */ (function (_super) {
895
991
  var MissingToolsError = /** @class */ (function (_super) {
896
992
  __extends(MissingToolsError, _super);
897
993
  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;
994
+ 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
995
  _this.name = 'MissingToolsError';
900
996
  Object.setPrototypeOf(_this, MissingToolsError.prototype);
901
997
  return _this;
@@ -927,7 +1023,7 @@ var NotFoundError = /** @class */ (function (_super) {
927
1023
  var NotYetImplementedError = /** @class */ (function (_super) {
928
1024
  __extends(NotYetImplementedError, _super);
929
1025
  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;
1026
+ 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
1027
  _this.name = 'NotYetImplementedError';
932
1028
  Object.setPrototypeOf(_this, NotYetImplementedError.prototype);
933
1029
  return _this;
@@ -1060,7 +1156,7 @@ function deserializeError(error) {
1060
1156
  function serializeError(error) {
1061
1157
  var name = error.name, message = error.message, stack = error.stack;
1062
1158
  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 "); }));
1159
+ 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
1160
  }
1065
1161
  return {
1066
1162
  name: name,
@@ -1584,7 +1680,7 @@ function searchKeywords(haystack, needle) {
1584
1680
  * @public exported from `@promptbook/utils`
1585
1681
  * @see https://github.com/hejny/spacetrim#usage
1586
1682
  */
1587
- var spaceTrim = spaceTrim$1;
1683
+ var spaceTrim = spaceTrim$2;
1588
1684
 
1589
1685
  /**
1590
1686
  * Parses the task and returns the list of all parameter names
@@ -1614,242 +1710,6 @@ function extractParameterNames(template) {
1614
1710
  return parameterNames;
1615
1711
  }
1616
1712
 
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
1713
  /**
1854
1714
  * Replaces parameters in template with values from parameters object
1855
1715
  *
@@ -2031,6 +1891,163 @@ function removeQuotes(text) {
2031
1891
  return text;
2032
1892
  }
2033
1893
 
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
+
2034
2051
  /**
2035
2052
  * @@@
2036
2053
  *
@@ -2241,13 +2258,13 @@ function union() {
2241
2258
  * @public exported from `@promptbook/utils`
2242
2259
  */
2243
2260
  function trimCodeBlock(value) {
2244
- value = spaceTrim$1(value);
2261
+ value = spaceTrim$2(value);
2245
2262
  if (!/^```[a-z]*(.*)```$/is.test(value)) {
2246
2263
  return value;
2247
2264
  }
2248
2265
  value = value.replace(/^```[a-z]*/i, '');
2249
2266
  value = value.replace(/```$/i, '');
2250
- value = spaceTrim$1(value);
2267
+ value = spaceTrim$2(value);
2251
2268
  return value;
2252
2269
  }
2253
2270
 
@@ -2260,9 +2277,9 @@ function trimCodeBlock(value) {
2260
2277
  * @public exported from `@promptbook/utils`
2261
2278
  */
2262
2279
  function trimEndOfCodeBlock(value) {
2263
- value = spaceTrim$1(value);
2280
+ value = spaceTrim$2(value);
2264
2281
  value = value.replace(/```$/g, '');
2265
- value = spaceTrim$1(value);
2282
+ value = spaceTrim$2(value);
2266
2283
  return value;
2267
2284
  }
2268
2285
 
@@ -2284,7 +2301,7 @@ function unwrapResult(text, options) {
2284
2301
  var trimmedText = text;
2285
2302
  // Remove leading and trailing spaces and newlines
2286
2303
  if (isTrimmed) {
2287
- trimmedText = spaceTrim$1(trimmedText);
2304
+ trimmedText = spaceTrim$2(trimmedText);
2288
2305
  }
2289
2306
  var processedText = trimmedText;
2290
2307
  if (isIntroduceSentenceRemoved) {
@@ -2293,7 +2310,7 @@ function unwrapResult(text, options) {
2293
2310
  // Remove the introduce sentence and quotes by replacing it with an empty string
2294
2311
  processedText = processedText.replace(introduceSentenceRegex, '');
2295
2312
  }
2296
- processedText = spaceTrim$1(processedText);
2313
+ processedText = spaceTrim$2(processedText);
2297
2314
  }
2298
2315
  if (processedText.length < 3) {
2299
2316
  return trimmedText;