@promptbook/node 0.92.0-10 → 0.92.0-12

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.
Files changed (24) hide show
  1. package/esm/index.es.js +255 -233
  2. package/esm/index.es.js.map +1 -1
  3. package/esm/typings/src/_packages/browser.index.d.ts +2 -0
  4. package/esm/typings/src/_packages/core.index.d.ts +6 -4
  5. package/esm/typings/src/_packages/types.index.d.ts +2 -2
  6. package/esm/typings/src/execution/PipelineExecutorResult.d.ts +3 -1
  7. package/esm/typings/src/execution/createPipelineExecutor/computeCosineSimilarity.d.ts +13 -0
  8. package/esm/typings/src/execution/utils/checkExpectations.d.ts +1 -1
  9. package/esm/typings/src/formats/_common/{FormatDefinition.d.ts → FormatParser.d.ts} +3 -3
  10. package/esm/typings/src/formats/_common/{FormatSubvalueDefinition.d.ts → FormatSubvalueParser.d.ts} +1 -1
  11. package/esm/typings/src/formats/csv/CsvFormatParser.d.ts +17 -0
  12. package/esm/typings/src/formats/index.d.ts +2 -2
  13. package/esm/typings/src/formats/json/{JsonFormatDefinition.d.ts → JsonFormatParser.d.ts} +6 -6
  14. package/esm/typings/src/formats/text/{TextFormatDefinition.d.ts → TextFormatParser.d.ts} +7 -7
  15. package/esm/typings/src/formats/xml/XmlFormatParser.d.ts +19 -0
  16. package/esm/typings/src/postprocessing/utils/extractJsonBlock.d.ts +1 -1
  17. package/esm/typings/src/storage/local-storage/getIndexedDbStorage.d.ts +10 -0
  18. package/esm/typings/src/storage/local-storage/utils/makePromptbookStorageFromIndexedDb.d.ts +7 -0
  19. package/esm/typings/src/utils/expectation-counters/index.d.ts +1 -1
  20. package/package.json +2 -2
  21. package/umd/index.umd.js +258 -236
  22. package/umd/index.umd.js.map +1 -1
  23. package/esm/typings/src/formats/csv/CsvFormatDefinition.d.ts +0 -17
  24. package/esm/typings/src/formats/xml/XmlFormatDefinition.d.ts +0 -19
package/esm/index.es.js CHANGED
@@ -5,8 +5,8 @@ import spaceTrim, { spaceTrim as spaceTrim$1 } from 'spacetrim';
5
5
  import JSZip from 'jszip';
6
6
  import { format } from 'prettier';
7
7
  import parserHtml from 'prettier/parser-html';
8
- import { Subject } from 'rxjs';
9
8
  import { randomBytes } from 'crypto';
9
+ import { Subject } from 'rxjs';
10
10
  import { forTime } from 'waitasecond';
11
11
  import { parse, unparse } from 'papaparse';
12
12
  import hexEncoder from 'crypto-js/enc-hex';
@@ -30,7 +30,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
30
30
  * @generated
31
31
  * @see https://github.com/webgptorg/promptbook
32
32
  */
33
- const PROMPTBOOK_ENGINE_VERSION = '0.92.0-10';
33
+ const PROMPTBOOK_ENGINE_VERSION = '0.92.0-12';
34
34
  /**
35
35
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
36
36
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -1688,70 +1688,6 @@ function isPipelinePrepared(pipeline) {
1688
1688
  * - [♨] Are tasks prepared
1689
1689
  */
1690
1690
 
1691
- /**
1692
- * Generates random token
1693
- *
1694
- * Note: This function is cryptographically secure (it uses crypto.randomBytes internally)
1695
- *
1696
- * @private internal helper function
1697
- * @returns secure random token
1698
- */
1699
- function $randomToken(randomness) {
1700
- return randomBytes(randomness).toString('hex');
1701
- }
1702
- /**
1703
- * TODO: Maybe use nanoid instead https://github.com/ai/nanoid
1704
- */
1705
-
1706
- /**
1707
- * Recursively converts JSON strings to JSON objects
1708
-
1709
- * @public exported from `@promptbook/utils`
1710
- */
1711
- function jsonStringsToJsons(object) {
1712
- if (object === null) {
1713
- return object;
1714
- }
1715
- if (Array.isArray(object)) {
1716
- return object.map(jsonStringsToJsons);
1717
- }
1718
- if (typeof object !== 'object') {
1719
- return object;
1720
- }
1721
- const newObject = { ...object };
1722
- for (const [key, value] of Object.entries(object)) {
1723
- if (typeof value === 'string' && isValidJsonString(value)) {
1724
- newObject[key] = jsonParse(value);
1725
- }
1726
- else {
1727
- newObject[key] = jsonStringsToJsons(value);
1728
- }
1729
- }
1730
- return newObject;
1731
- }
1732
- /**
1733
- * TODO: Type the return type correctly
1734
- */
1735
-
1736
- /**
1737
- * This error indicates errors during the execution of the pipeline
1738
- *
1739
- * @public exported from `@promptbook/core`
1740
- */
1741
- class PipelineExecutionError extends Error {
1742
- constructor(message) {
1743
- // Added id parameter
1744
- super(message);
1745
- this.name = 'PipelineExecutionError';
1746
- // TODO: [🐙] DRY - Maybe $randomId
1747
- this.id = `error-${$randomToken(8 /* <- TODO: To global config + Use Base58 to avoid simmilar char conflicts */)}`;
1748
- Object.setPrototypeOf(this, PipelineExecutionError.prototype);
1749
- }
1750
- }
1751
- /**
1752
- * TODO: [🧠][🌂] Add id to all errors
1753
- */
1754
-
1755
1691
  /**
1756
1692
  * This error indicates problems parsing the format value
1757
1693
  *
@@ -1886,6 +1822,40 @@ class NotYetImplementedError extends Error {
1886
1822
  }
1887
1823
  }
1888
1824
 
1825
+ /**
1826
+ * Generates random token
1827
+ *
1828
+ * Note: This function is cryptographically secure (it uses crypto.randomBytes internally)
1829
+ *
1830
+ * @private internal helper function
1831
+ * @returns secure random token
1832
+ */
1833
+ function $randomToken(randomness) {
1834
+ return randomBytes(randomness).toString('hex');
1835
+ }
1836
+ /**
1837
+ * TODO: Maybe use nanoid instead https://github.com/ai/nanoid
1838
+ */
1839
+
1840
+ /**
1841
+ * This error indicates errors during the execution of the pipeline
1842
+ *
1843
+ * @public exported from `@promptbook/core`
1844
+ */
1845
+ class PipelineExecutionError extends Error {
1846
+ constructor(message) {
1847
+ // Added id parameter
1848
+ super(message);
1849
+ this.name = 'PipelineExecutionError';
1850
+ // TODO: [🐙] DRY - Maybe $randomId
1851
+ this.id = `error-${$randomToken(8 /* <- TODO: To global config + Use Base58 to avoid simmilar char conflicts */)}`;
1852
+ Object.setPrototypeOf(this, PipelineExecutionError.prototype);
1853
+ }
1854
+ }
1855
+ /**
1856
+ * TODO: [🧠][🌂] Add id to all errors
1857
+ */
1858
+
1889
1859
  /**
1890
1860
  * Error thrown when a fetch request fails
1891
1861
  *
@@ -1961,6 +1931,65 @@ const ALL_ERRORS = {
1961
1931
  * Note: [💞] Ignore a discrepancy between file name and entity name
1962
1932
  */
1963
1933
 
1934
+ /**
1935
+ * Serializes an error into a [🚉] JSON-serializable object
1936
+ *
1937
+ * @public exported from `@promptbook/utils`
1938
+ */
1939
+ function serializeError(error) {
1940
+ const { name, message, stack } = error;
1941
+ const { id } = error;
1942
+ if (!Object.keys(ALL_ERRORS).includes(name)) {
1943
+ console.error(spaceTrim((block) => `
1944
+
1945
+ Cannot serialize error with name "${name}"
1946
+
1947
+ Authors of Promptbook probably forgot to add this error into the list of errors:
1948
+ https://github.com/webgptorg/promptbook/blob/main/src/errors/0-index.ts
1949
+
1950
+
1951
+ ${block(stack || message)}
1952
+
1953
+ `));
1954
+ }
1955
+ return {
1956
+ name: name,
1957
+ message,
1958
+ stack,
1959
+ id, // Include id in the serialized object
1960
+ };
1961
+ }
1962
+
1963
+ /**
1964
+ * Recursively converts JSON strings to JSON objects
1965
+
1966
+ * @public exported from `@promptbook/utils`
1967
+ */
1968
+ function jsonStringsToJsons(object) {
1969
+ if (object === null) {
1970
+ return object;
1971
+ }
1972
+ if (Array.isArray(object)) {
1973
+ return object.map(jsonStringsToJsons);
1974
+ }
1975
+ if (typeof object !== 'object') {
1976
+ return object;
1977
+ }
1978
+ const newObject = { ...object };
1979
+ for (const [key, value] of Object.entries(object)) {
1980
+ if (typeof value === 'string' && isValidJsonString(value)) {
1981
+ newObject[key] = jsonParse(value);
1982
+ }
1983
+ else {
1984
+ newObject[key] = jsonStringsToJsons(value);
1985
+ }
1986
+ }
1987
+ return newObject;
1988
+ }
1989
+ /**
1990
+ * TODO: Type the return type correctly
1991
+ */
1992
+
1964
1993
  /**
1965
1994
  * Deserializes the error object
1966
1995
  *
@@ -2127,33 +2156,72 @@ function createTask(options) {
2127
2156
  */
2128
2157
 
2129
2158
  /**
2130
- * Serializes an error into a [🚉] JSON-serializable object
2159
+ * Represents the uncertain value
2131
2160
  *
2132
- * @public exported from `@promptbook/utils`
2161
+ * @public exported from `@promptbook/core`
2162
+ */
2163
+ const ZERO_VALUE = $deepFreeze({ value: 0 });
2164
+ /**
2165
+ * Represents the uncertain value
2166
+ *
2167
+ * @public exported from `@promptbook/core`
2168
+ */
2169
+ const UNCERTAIN_ZERO_VALUE = $deepFreeze({ value: 0, isUncertain: true });
2170
+ /**
2171
+ * Represents the usage with no resources consumed
2172
+ *
2173
+ * @public exported from `@promptbook/core`
2174
+ */
2175
+ const ZERO_USAGE = $deepFreeze({
2176
+ price: ZERO_VALUE,
2177
+ input: {
2178
+ tokensCount: ZERO_VALUE,
2179
+ charactersCount: ZERO_VALUE,
2180
+ wordsCount: ZERO_VALUE,
2181
+ sentencesCount: ZERO_VALUE,
2182
+ linesCount: ZERO_VALUE,
2183
+ paragraphsCount: ZERO_VALUE,
2184
+ pagesCount: ZERO_VALUE,
2185
+ },
2186
+ output: {
2187
+ tokensCount: ZERO_VALUE,
2188
+ charactersCount: ZERO_VALUE,
2189
+ wordsCount: ZERO_VALUE,
2190
+ sentencesCount: ZERO_VALUE,
2191
+ linesCount: ZERO_VALUE,
2192
+ paragraphsCount: ZERO_VALUE,
2193
+ pagesCount: ZERO_VALUE,
2194
+ },
2195
+ });
2196
+ /**
2197
+ * Represents the usage with unknown resources consumed
2198
+ *
2199
+ * @public exported from `@promptbook/core`
2200
+ */
2201
+ const UNCERTAIN_USAGE = $deepFreeze({
2202
+ price: UNCERTAIN_ZERO_VALUE,
2203
+ input: {
2204
+ tokensCount: UNCERTAIN_ZERO_VALUE,
2205
+ charactersCount: UNCERTAIN_ZERO_VALUE,
2206
+ wordsCount: UNCERTAIN_ZERO_VALUE,
2207
+ sentencesCount: UNCERTAIN_ZERO_VALUE,
2208
+ linesCount: UNCERTAIN_ZERO_VALUE,
2209
+ paragraphsCount: UNCERTAIN_ZERO_VALUE,
2210
+ pagesCount: UNCERTAIN_ZERO_VALUE,
2211
+ },
2212
+ output: {
2213
+ tokensCount: UNCERTAIN_ZERO_VALUE,
2214
+ charactersCount: UNCERTAIN_ZERO_VALUE,
2215
+ wordsCount: UNCERTAIN_ZERO_VALUE,
2216
+ sentencesCount: UNCERTAIN_ZERO_VALUE,
2217
+ linesCount: UNCERTAIN_ZERO_VALUE,
2218
+ paragraphsCount: UNCERTAIN_ZERO_VALUE,
2219
+ pagesCount: UNCERTAIN_ZERO_VALUE,
2220
+ },
2221
+ });
2222
+ /**
2223
+ * Note: [💞] Ignore a discrepancy between file name and entity name
2133
2224
  */
2134
- function serializeError(error) {
2135
- const { name, message, stack } = error;
2136
- const { id } = error;
2137
- if (!Object.keys(ALL_ERRORS).includes(name)) {
2138
- console.error(spaceTrim((block) => `
2139
-
2140
- Cannot serialize error with name "${name}"
2141
-
2142
- Authors of Promptbook probably forgot to add this error into the list of errors:
2143
- https://github.com/webgptorg/promptbook/blob/main/src/errors/0-index.ts
2144
-
2145
-
2146
- ${block(stack || message)}
2147
-
2148
- `));
2149
- }
2150
- return {
2151
- name: name,
2152
- message,
2153
- stack,
2154
- id, // Include id in the serialized object
2155
- };
2156
- }
2157
2225
 
2158
2226
  /**
2159
2227
  * Format either small or big number
@@ -2236,74 +2304,6 @@ function valueToString(value) {
2236
2304
  }
2237
2305
  }
2238
2306
 
2239
- /**
2240
- * Represents the uncertain value
2241
- *
2242
- * @public exported from `@promptbook/core`
2243
- */
2244
- const ZERO_VALUE = $deepFreeze({ value: 0 });
2245
- /**
2246
- * Represents the uncertain value
2247
- *
2248
- * @public exported from `@promptbook/core`
2249
- */
2250
- const UNCERTAIN_ZERO_VALUE = $deepFreeze({ value: 0, isUncertain: true });
2251
- /**
2252
- * Represents the usage with no resources consumed
2253
- *
2254
- * @public exported from `@promptbook/core`
2255
- */
2256
- const ZERO_USAGE = $deepFreeze({
2257
- price: ZERO_VALUE,
2258
- input: {
2259
- tokensCount: ZERO_VALUE,
2260
- charactersCount: ZERO_VALUE,
2261
- wordsCount: ZERO_VALUE,
2262
- sentencesCount: ZERO_VALUE,
2263
- linesCount: ZERO_VALUE,
2264
- paragraphsCount: ZERO_VALUE,
2265
- pagesCount: ZERO_VALUE,
2266
- },
2267
- output: {
2268
- tokensCount: ZERO_VALUE,
2269
- charactersCount: ZERO_VALUE,
2270
- wordsCount: ZERO_VALUE,
2271
- sentencesCount: ZERO_VALUE,
2272
- linesCount: ZERO_VALUE,
2273
- paragraphsCount: ZERO_VALUE,
2274
- pagesCount: ZERO_VALUE,
2275
- },
2276
- });
2277
- /**
2278
- * Represents the usage with unknown resources consumed
2279
- *
2280
- * @public exported from `@promptbook/core`
2281
- */
2282
- $deepFreeze({
2283
- price: UNCERTAIN_ZERO_VALUE,
2284
- input: {
2285
- tokensCount: UNCERTAIN_ZERO_VALUE,
2286
- charactersCount: UNCERTAIN_ZERO_VALUE,
2287
- wordsCount: UNCERTAIN_ZERO_VALUE,
2288
- sentencesCount: UNCERTAIN_ZERO_VALUE,
2289
- linesCount: UNCERTAIN_ZERO_VALUE,
2290
- paragraphsCount: UNCERTAIN_ZERO_VALUE,
2291
- pagesCount: UNCERTAIN_ZERO_VALUE,
2292
- },
2293
- output: {
2294
- tokensCount: UNCERTAIN_ZERO_VALUE,
2295
- charactersCount: UNCERTAIN_ZERO_VALUE,
2296
- wordsCount: UNCERTAIN_ZERO_VALUE,
2297
- sentencesCount: UNCERTAIN_ZERO_VALUE,
2298
- linesCount: UNCERTAIN_ZERO_VALUE,
2299
- paragraphsCount: UNCERTAIN_ZERO_VALUE,
2300
- pagesCount: UNCERTAIN_ZERO_VALUE,
2301
- },
2302
- });
2303
- /**
2304
- * Note: [💞] Ignore a discrepancy between file name and entity name
2305
- */
2306
-
2307
2307
  /**
2308
2308
  * Function `addUsage` will add multiple usages into one
2309
2309
  *
@@ -2521,6 +2521,24 @@ const MANDATORY_CSV_SETTINGS = Object.freeze({
2521
2521
  // encoding: 'utf-8',
2522
2522
  });
2523
2523
 
2524
+ /**
2525
+ * Converts a CSV string into an object
2526
+ *
2527
+ * Note: This is wrapper around `papaparse.parse()` with better autohealing
2528
+ *
2529
+ * @private - for now until `@promptbook/csv` is released
2530
+ */
2531
+ function csvParse(value /* <- TODO: string_csv */, settings, schema /* <- TODO: Make CSV Schemas */) {
2532
+ settings = { ...settings, ...MANDATORY_CSV_SETTINGS };
2533
+ // Note: Autoheal invalid '\n' characters
2534
+ if (settings.newline && !settings.newline.includes('\r') && value.includes('\r')) {
2535
+ console.warn('CSV string contains carriage return characters, but in the CSV settings the `newline` setting does not include them. Autohealing the CSV string.');
2536
+ value = value.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
2537
+ }
2538
+ const csv = parse(value, settings);
2539
+ return csv;
2540
+ }
2541
+
2524
2542
  /**
2525
2543
  * Function to check if a string is valid CSV
2526
2544
  *
@@ -2543,31 +2561,13 @@ function isValidCsvString(value) {
2543
2561
  }
2544
2562
  }
2545
2563
 
2546
- /**
2547
- * Converts a CSV string into an object
2548
- *
2549
- * Note: This is wrapper around `papaparse.parse()` with better autohealing
2550
- *
2551
- * @private - for now until `@promptbook/csv` is released
2552
- */
2553
- function csvParse(value /* <- TODO: string_csv */, settings, schema /* <- TODO: Make CSV Schemas */) {
2554
- settings = { ...settings, ...MANDATORY_CSV_SETTINGS };
2555
- // Note: Autoheal invalid '\n' characters
2556
- if (settings.newline && !settings.newline.includes('\r') && value.includes('\r')) {
2557
- console.warn('CSV string contains carriage return characters, but in the CSV settings the `newline` setting does not include them. Autohealing the CSV string.');
2558
- value = value.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
2559
- }
2560
- const csv = parse(value, settings);
2561
- return csv;
2562
- }
2563
-
2564
2564
  /**
2565
2565
  * Definition for CSV spreadsheet
2566
2566
  *
2567
2567
  * @public exported from `@promptbook/core`
2568
2568
  * <- TODO: [🏢] Export from package `@promptbook/csv`
2569
2569
  */
2570
- const CsvFormatDefinition = {
2570
+ const CsvFormatParser = {
2571
2571
  formatName: 'CSV',
2572
2572
  aliases: ['SPREADSHEET', 'TABLE'],
2573
2573
  isValid(value, settings, schema) {
@@ -2579,7 +2579,7 @@ const CsvFormatDefinition = {
2579
2579
  heal(value, settings, schema) {
2580
2580
  throw new Error('Not implemented');
2581
2581
  },
2582
- subvalueDefinitions: [
2582
+ subvalueParsers: [
2583
2583
  {
2584
2584
  subvalueName: 'ROW',
2585
2585
  async mapValues(value, outputParameterName, settings, mapCallback) {
@@ -2640,10 +2640,10 @@ const CsvFormatDefinition = {
2640
2640
  ],
2641
2641
  };
2642
2642
  /**
2643
- * TODO: [🍓] In `CsvFormatDefinition` implement simple `isValid`
2644
- * TODO: [🍓] In `CsvFormatDefinition` implement partial `canBeValid`
2645
- * TODO: [🍓] In `CsvFormatDefinition` implement `heal
2646
- * TODO: [🍓] In `CsvFormatDefinition` implement `subvalueDefinitions`
2643
+ * TODO: [🍓] In `CsvFormatParser` implement simple `isValid`
2644
+ * TODO: [🍓] In `CsvFormatParser` implement partial `canBeValid`
2645
+ * TODO: [🍓] In `CsvFormatParser` implement `heal
2646
+ * TODO: [🍓] In `CsvFormatParser` implement `subvalueParsers`
2647
2647
  * TODO: [🏢] Allow to expect something inside CSV objects and other formats
2648
2648
  */
2649
2649
 
@@ -2652,7 +2652,7 @@ const CsvFormatDefinition = {
2652
2652
  *
2653
2653
  * @private still in development [🏢]
2654
2654
  */
2655
- const JsonFormatDefinition = {
2655
+ const JsonFormatParser = {
2656
2656
  formatName: 'JSON',
2657
2657
  mimeType: 'application/json',
2658
2658
  isValid(value, settings, schema) {
@@ -2664,28 +2664,28 @@ const JsonFormatDefinition = {
2664
2664
  heal(value, settings, schema) {
2665
2665
  throw new Error('Not implemented');
2666
2666
  },
2667
- subvalueDefinitions: [],
2667
+ subvalueParsers: [],
2668
2668
  };
2669
2669
  /**
2670
2670
  * TODO: [🧠] Maybe propper instance of object
2671
2671
  * TODO: [0] Make string_serialized_json
2672
2672
  * TODO: [1] Make type for JSON Settings and Schema
2673
2673
  * TODO: [🧠] What to use for validating JSONs - JSON Schema, ZoD, typescript types/interfaces,...?
2674
- * TODO: [🍓] In `JsonFormatDefinition` implement simple `isValid`
2675
- * TODO: [🍓] In `JsonFormatDefinition` implement partial `canBeValid`
2676
- * TODO: [🍓] In `JsonFormatDefinition` implement `heal
2677
- * TODO: [🍓] In `JsonFormatDefinition` implement `subvalueDefinitions`
2674
+ * TODO: [🍓] In `JsonFormatParser` implement simple `isValid`
2675
+ * TODO: [🍓] In `JsonFormatParser` implement partial `canBeValid`
2676
+ * TODO: [🍓] In `JsonFormatParser` implement `heal
2677
+ * TODO: [🍓] In `JsonFormatParser` implement `subvalueParsers`
2678
2678
  * TODO: [🏢] Allow to expect something inside JSON objects and other formats
2679
2679
  */
2680
2680
 
2681
2681
  /**
2682
2682
  * Definition for any text - this will be always valid
2683
2683
  *
2684
- * Note: This is not useful for validation, but for splitting and mapping with `subvalueDefinitions`
2684
+ * Note: This is not useful for validation, but for splitting and mapping with `subvalueParsers`
2685
2685
  *
2686
2686
  * @public exported from `@promptbook/core`
2687
2687
  */
2688
- const TextFormatDefinition = {
2688
+ const TextFormatParser = {
2689
2689
  formatName: 'TEXT',
2690
2690
  isValid(value) {
2691
2691
  return typeof value === 'string';
@@ -2694,9 +2694,9 @@ const TextFormatDefinition = {
2694
2694
  return typeof partialValue === 'string';
2695
2695
  },
2696
2696
  heal() {
2697
- throw new UnexpectedError('It does not make sense to call `TextFormatDefinition.heal`');
2697
+ throw new UnexpectedError('It does not make sense to call `TextFormatParser.heal`');
2698
2698
  },
2699
- subvalueDefinitions: [
2699
+ subvalueParsers: [
2700
2700
  {
2701
2701
  subvalueName: 'LINE',
2702
2702
  async mapValues(value, outputParameterName, settings, mapCallback) {
@@ -2716,10 +2716,10 @@ const TextFormatDefinition = {
2716
2716
  /**
2717
2717
  * TODO: [1] Make type for XML Text and Schema
2718
2718
  * TODO: [🧠][🤠] Here should be all words, characters, lines, paragraphs, pages available as subvalues
2719
- * TODO: [🍓] In `TextFormatDefinition` implement simple `isValid`
2720
- * TODO: [🍓] In `TextFormatDefinition` implement partial `canBeValid`
2721
- * TODO: [🍓] In `TextFormatDefinition` implement `heal
2722
- * TODO: [🍓] In `TextFormatDefinition` implement `subvalueDefinitions`
2719
+ * TODO: [🍓] In `TextFormatParser` implement simple `isValid`
2720
+ * TODO: [🍓] In `TextFormatParser` implement partial `canBeValid`
2721
+ * TODO: [🍓] In `TextFormatParser` implement `heal
2722
+ * TODO: [🍓] In `TextFormatParser` implement `subvalueParsers`
2723
2723
  * TODO: [🏢] Allow to expect something inside each item of list and other formats
2724
2724
  */
2725
2725
 
@@ -2752,7 +2752,7 @@ function isValidXmlString(value) {
2752
2752
  *
2753
2753
  * @private still in development [🏢]
2754
2754
  */
2755
- const XmlFormatDefinition = {
2755
+ const XmlFormatParser = {
2756
2756
  formatName: 'XML',
2757
2757
  mimeType: 'application/xml',
2758
2758
  isValid(value, settings, schema) {
@@ -2764,17 +2764,17 @@ const XmlFormatDefinition = {
2764
2764
  heal(value, settings, schema) {
2765
2765
  throw new Error('Not implemented');
2766
2766
  },
2767
- subvalueDefinitions: [],
2767
+ subvalueParsers: [],
2768
2768
  };
2769
2769
  /**
2770
2770
  * TODO: [🧠] Maybe propper instance of object
2771
2771
  * TODO: [0] Make string_serialized_xml
2772
2772
  * TODO: [1] Make type for XML Settings and Schema
2773
2773
  * TODO: [🧠] What to use for validating XMLs - XSD,...
2774
- * TODO: [🍓] In `XmlFormatDefinition` implement simple `isValid`
2775
- * TODO: [🍓] In `XmlFormatDefinition` implement partial `canBeValid`
2776
- * TODO: [🍓] In `XmlFormatDefinition` implement `heal
2777
- * TODO: [🍓] In `XmlFormatDefinition` implement `subvalueDefinitions`
2774
+ * TODO: [🍓] In `XmlFormatParser` implement simple `isValid`
2775
+ * TODO: [🍓] In `XmlFormatParser` implement partial `canBeValid`
2776
+ * TODO: [🍓] In `XmlFormatParser` implement `heal
2777
+ * TODO: [🍓] In `XmlFormatParser` implement `subvalueParsers`
2778
2778
  * TODO: [🏢] Allow to expect something inside XML and other formats
2779
2779
  */
2780
2780
 
@@ -2783,12 +2783,7 @@ const XmlFormatDefinition = {
2783
2783
  *
2784
2784
  * @private internal index of `...` <- TODO [🏢]
2785
2785
  */
2786
- const FORMAT_DEFINITIONS = [
2787
- JsonFormatDefinition,
2788
- XmlFormatDefinition,
2789
- TextFormatDefinition,
2790
- CsvFormatDefinition,
2791
- ];
2786
+ const FORMAT_DEFINITIONS = [JsonFormatParser, XmlFormatParser, TextFormatParser, CsvFormatParser];
2792
2787
  /**
2793
2788
  * Note: [💞] Ignore a discrepancy between file name and entity name
2794
2789
  */
@@ -3150,7 +3145,7 @@ function extractJsonBlock(markdown) {
3150
3145
  }
3151
3146
  /**
3152
3147
  * TODO: Add some auto-healing logic + extract YAML, JSON5, TOML, etc.
3153
- * TODO: [🏢] Make this logic part of `JsonFormatDefinition` or `isValidJsonString`
3148
+ * TODO: [🏢] Make this logic part of `JsonFormatParser` or `isValidJsonString`
3154
3149
  */
3155
3150
 
3156
3151
  /**
@@ -3649,7 +3644,7 @@ const CountUtils = {
3649
3644
  PAGES: countPages,
3650
3645
  };
3651
3646
  /**
3652
- * TODO: [🧠][🤠] This should be probbably as part of `TextFormatDefinition`
3647
+ * TODO: [🧠][🤠] This should be probbably as part of `TextFormatParser`
3653
3648
  * Note: [💞] Ignore a discrepancy between file name and entity name
3654
3649
  */
3655
3650
 
@@ -3677,7 +3672,7 @@ function checkExpectations(expectations, value) {
3677
3672
  }
3678
3673
  /**
3679
3674
  * TODO: [💝] Unite object for expecting amount and format
3680
- * TODO: [🧠][🤠] This should be part of `TextFormatDefinition`
3675
+ * TODO: [🧠][🤠] This should be part of `TextFormatParser`
3681
3676
  * Note: [💝] and [🤠] are interconnected together
3682
3677
  */
3683
3678
 
@@ -3905,7 +3900,7 @@ async function executeAttempts(options) {
3905
3900
  if (task.format) {
3906
3901
  if (task.format === 'JSON') {
3907
3902
  if (!isValidJsonString($ongoingTaskResult.$resultString || '')) {
3908
- // TODO: [🏢] Do more universally via `FormatDefinition`
3903
+ // TODO: [🏢] Do more universally via `FormatParser`
3909
3904
  try {
3910
3905
  $ongoingTaskResult.$resultString = extractJsonBlock($ongoingTaskResult.$resultString || '');
3911
3906
  }
@@ -4043,16 +4038,16 @@ async function executeFormatSubvalues(options) {
4043
4038
  ${block(pipelineIdentification)}
4044
4039
  `));
4045
4040
  }
4046
- const subvalueDefinition = formatDefinition.subvalueDefinitions.find((subvalueDefinition) => [subvalueDefinition.subvalueName, ...(subvalueDefinition.aliases || [])].includes(task.foreach.subformatName));
4047
- if (subvalueDefinition === undefined) {
4041
+ const subvalueParser = formatDefinition.subvalueParsers.find((subvalueParser) => [subvalueParser.subvalueName, ...(subvalueParser.aliases || [])].includes(task.foreach.subformatName));
4042
+ if (subvalueParser === undefined) {
4048
4043
  throw new UnexpectedError(
4049
4044
  // <- TODO: [🧠][🧐] Should be formats fixed per promptbook version or behave as plugins (=> change UnexpectedError)
4050
4045
  spaceTrim((block) => `
4051
4046
  Unsupported subformat name "${task.foreach.subformatName}" for format "${task.foreach.formatName}"
4052
4047
 
4053
4048
  Available subformat names for format "${formatDefinition.formatName}":
4054
- ${block(formatDefinition.subvalueDefinitions
4055
- .map((subvalueDefinition) => subvalueDefinition.subvalueName)
4049
+ ${block(formatDefinition.subvalueParsers
4050
+ .map((subvalueParser) => subvalueParser.subvalueName)
4056
4051
  .map((subvalueName) => `- ${subvalueName}`)
4057
4052
  .join('\n'))}
4058
4053
 
@@ -4066,7 +4061,7 @@ async function executeFormatSubvalues(options) {
4066
4061
  formatSettings = csvSettings;
4067
4062
  // <- TODO: [🤹‍♂️] More universal, make simmilar pattern for other formats for example \n vs \r\n in text
4068
4063
  }
4069
- const resultString = await subvalueDefinition.mapValues(parameterValue, task.foreach.outputSubparameterName, formatSettings, async (subparameters, index) => {
4064
+ const resultString = await subvalueParser.mapValues(parameterValue, task.foreach.outputSubparameterName, formatSettings, async (subparameters, index) => {
4070
4065
  let mappedParameters;
4071
4066
  // TODO: [🤹‍♂️][🪂] Limit to N concurrent executions
4072
4067
  // TODO: When done [🐚] Report progress also for each subvalue here
@@ -4128,6 +4123,27 @@ async function getExamplesForTask(task) {
4128
4123
  return RESERVED_PARAMETER_MISSING_VALUE /* <- TODO: [♨] Implement */;
4129
4124
  }
4130
4125
 
4126
+ /**
4127
+ * Computes the cosine similarity between two embedding vectors
4128
+ *
4129
+ * Note: This is helping function for RAG (retrieval-augmented generation)
4130
+ *
4131
+ * @param embeddingVector1
4132
+ * @param embeddingVector2
4133
+ * @returns Cosine similarity between the two vectors
4134
+ *
4135
+ * @public exported from `@promptbook/core`
4136
+ */
4137
+ function computeCosineSimilarity(embeddingVector1, embeddingVector2) {
4138
+ if (embeddingVector1.length !== embeddingVector2.length) {
4139
+ throw new TypeError('Embedding vectors must have the same length');
4140
+ }
4141
+ const dotProduct = embeddingVector1.reduce((sum, value, index) => sum + value * embeddingVector2[index], 0);
4142
+ const magnitude1 = Math.sqrt(embeddingVector1.reduce((sum, value) => sum + value * value, 0));
4143
+ const magnitude2 = Math.sqrt(embeddingVector2.reduce((sum, value) => sum + value * value, 0));
4144
+ return 1 - dotProduct / (magnitude1 * magnitude2);
4145
+ }
4146
+
4131
4147
  /**
4132
4148
  * @@@
4133
4149
  *
@@ -4154,7 +4170,7 @@ async function getKnowledgeForTask(options) {
4154
4170
  },
4155
4171
  content: task.content,
4156
4172
  parameters: {
4157
- /* !!!!!!!! */
4173
+ /* !!!! */
4158
4174
  },
4159
4175
  };
4160
4176
  const taskEmbeddingResult = await llmTools.callEmbeddingModel(taskEmbeddingPrompt);
@@ -4189,16 +4205,6 @@ async function getKnowledgeForTask(options) {
4189
4205
  return knowledgePiecesLimited.map(({ content }) => `- ${content}`).join('\n');
4190
4206
  // <- TODO: [🧠] Some smart aggregation of knowledge pieces, single-line vs multi-line vs mixed
4191
4207
  }
4192
- // TODO: !!!!!! Annotate + to new file
4193
- function computeCosineSimilarity(embeddingVector1, embeddingVector2) {
4194
- if (embeddingVector1.length !== embeddingVector2.length) {
4195
- throw new TypeError('Embedding vectors must have the same length');
4196
- }
4197
- const dotProduct = embeddingVector1.reduce((sum, value, index) => sum + value * embeddingVector2[index], 0);
4198
- const magnitude1 = Math.sqrt(embeddingVector1.reduce((sum, value) => sum + value * value, 0));
4199
- const magnitude2 = Math.sqrt(embeddingVector2.reduce((sum, value) => sum + value * value, 0));
4200
- return 1 - dotProduct / (magnitude1 * magnitude2);
4201
- }
4202
4208
  /**
4203
4209
  * TODO: !!!! Verify if this is working
4204
4210
  * TODO: [♨] Implement Better - use keyword search
@@ -4701,6 +4707,22 @@ function createPipelineExecutor(options) {
4701
4707
  cacheDirname,
4702
4708
  intermediateFilesStrategy,
4703
4709
  isAutoInstalled,
4710
+ }).catch((error) => {
4711
+ assertsError(error);
4712
+ return exportJson({
4713
+ name: 'pipelineExecutorResult',
4714
+ message: `Unuccessful PipelineExecutorResult, last catch`,
4715
+ order: [],
4716
+ value: {
4717
+ isSuccessful: false,
4718
+ errors: [serializeError(error)],
4719
+ warnings: [],
4720
+ usage: UNCERTAIN_USAGE,
4721
+ executionReport: null,
4722
+ outputParameters: {},
4723
+ preparedPipeline,
4724
+ },
4725
+ });
4704
4726
  });
4705
4727
  };
4706
4728
  const pipelineExecutor = (inputParameters) => createTask({
@@ -6685,14 +6707,14 @@ const foreachCommandParser = {
6685
6707
  `));
6686
6708
  // <- TODO: [🏢] List all supported format names
6687
6709
  }
6688
- const subvalueDefinition = formatDefinition.subvalueDefinitions.find((subvalueDefinition) => [subvalueDefinition.subvalueName, ...(subvalueDefinition.aliases || [])].includes(subformatName));
6689
- if (subvalueDefinition === undefined) {
6710
+ const subvalueParser = formatDefinition.subvalueParsers.find((subvalueParser) => [subvalueParser.subvalueName, ...(subvalueParser.aliases || [])].includes(subformatName));
6711
+ if (subvalueParser === undefined) {
6690
6712
  throw new ParseError(spaceTrim((block) => `
6691
6713
  Unsupported subformat name "${subformatName}" for format "${formatName}"
6692
6714
 
6693
6715
  Available subformat names for format "${formatDefinition.formatName}":
6694
- ${block(formatDefinition.subvalueDefinitions
6695
- .map((subvalueDefinition) => subvalueDefinition.subvalueName)
6716
+ ${block(formatDefinition.subvalueParsers
6717
+ .map((subvalueParser) => subvalueParser.subvalueName)
6696
6718
  .map((subvalueName) => `- ${subvalueName}`)
6697
6719
  .join('\n'))}
6698
6720
  `));