@promptbook/pdf 0.92.0-11 → 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 +221 -199
  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 +221 -199
  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/umd/index.umd.js CHANGED
@@ -25,7 +25,7 @@
25
25
  * @generated
26
26
  * @see https://github.com/webgptorg/promptbook
27
27
  */
28
- const PROMPTBOOK_ENGINE_VERSION = '0.92.0-11';
28
+ const PROMPTBOOK_ENGINE_VERSION = '0.92.0-12';
29
29
  /**
30
30
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
31
31
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -2136,75 +2136,6 @@
2136
2136
  * - [♨] Are tasks prepared
2137
2137
  */
2138
2138
 
2139
- /**
2140
- * Converts a JavaScript Object Notation (JSON) string into an object.
2141
- *
2142
- * Note: This is wrapper around `JSON.parse()` with better error and type handling
2143
- *
2144
- * @public exported from `@promptbook/utils`
2145
- */
2146
- function jsonParse(value) {
2147
- if (value === undefined) {
2148
- throw new Error(`Can not parse JSON from undefined value.`);
2149
- }
2150
- else if (typeof value !== 'string') {
2151
- console.error('Can not parse JSON from non-string value.', { text: value });
2152
- throw new Error(spaceTrim__default["default"](`
2153
- Can not parse JSON from non-string value.
2154
-
2155
- The value type: ${typeof value}
2156
- See more in console.
2157
- `));
2158
- }
2159
- try {
2160
- return JSON.parse(value);
2161
- }
2162
- catch (error) {
2163
- if (!(error instanceof Error)) {
2164
- throw error;
2165
- }
2166
- throw new Error(spaceTrim__default["default"]((block) => `
2167
- ${block(error.message)}
2168
-
2169
- The JSON text:
2170
- ${block(value)}
2171
- `));
2172
- }
2173
- }
2174
- /**
2175
- * TODO: !!!! Use in Promptbook.studio
2176
- */
2177
-
2178
- /**
2179
- * Recursively converts JSON strings to JSON objects
2180
-
2181
- * @public exported from `@promptbook/utils`
2182
- */
2183
- function jsonStringsToJsons(object) {
2184
- if (object === null) {
2185
- return object;
2186
- }
2187
- if (Array.isArray(object)) {
2188
- return object.map(jsonStringsToJsons);
2189
- }
2190
- if (typeof object !== 'object') {
2191
- return object;
2192
- }
2193
- const newObject = { ...object };
2194
- for (const [key, value] of Object.entries(object)) {
2195
- if (typeof value === 'string' && isValidJsonString(value)) {
2196
- newObject[key] = jsonParse(value);
2197
- }
2198
- else {
2199
- newObject[key] = jsonStringsToJsons(value);
2200
- }
2201
- }
2202
- return newObject;
2203
- }
2204
- /**
2205
- * TODO: Type the return type correctly
2206
- */
2207
-
2208
2139
  /**
2209
2140
  * This error indicates problems parsing the format value
2210
2141
  *
@@ -2388,6 +2319,104 @@
2388
2319
  * Note: [💞] Ignore a discrepancy between file name and entity name
2389
2320
  */
2390
2321
 
2322
+ /**
2323
+ * Serializes an error into a [🚉] JSON-serializable object
2324
+ *
2325
+ * @public exported from `@promptbook/utils`
2326
+ */
2327
+ function serializeError(error) {
2328
+ const { name, message, stack } = error;
2329
+ const { id } = error;
2330
+ if (!Object.keys(ALL_ERRORS).includes(name)) {
2331
+ console.error(spaceTrim__default["default"]((block) => `
2332
+
2333
+ Cannot serialize error with name "${name}"
2334
+
2335
+ Authors of Promptbook probably forgot to add this error into the list of errors:
2336
+ https://github.com/webgptorg/promptbook/blob/main/src/errors/0-index.ts
2337
+
2338
+
2339
+ ${block(stack || message)}
2340
+
2341
+ `));
2342
+ }
2343
+ return {
2344
+ name: name,
2345
+ message,
2346
+ stack,
2347
+ id, // Include id in the serialized object
2348
+ };
2349
+ }
2350
+
2351
+ /**
2352
+ * Converts a JavaScript Object Notation (JSON) string into an object.
2353
+ *
2354
+ * Note: This is wrapper around `JSON.parse()` with better error and type handling
2355
+ *
2356
+ * @public exported from `@promptbook/utils`
2357
+ */
2358
+ function jsonParse(value) {
2359
+ if (value === undefined) {
2360
+ throw new Error(`Can not parse JSON from undefined value.`);
2361
+ }
2362
+ else if (typeof value !== 'string') {
2363
+ console.error('Can not parse JSON from non-string value.', { text: value });
2364
+ throw new Error(spaceTrim__default["default"](`
2365
+ Can not parse JSON from non-string value.
2366
+
2367
+ The value type: ${typeof value}
2368
+ See more in console.
2369
+ `));
2370
+ }
2371
+ try {
2372
+ return JSON.parse(value);
2373
+ }
2374
+ catch (error) {
2375
+ if (!(error instanceof Error)) {
2376
+ throw error;
2377
+ }
2378
+ throw new Error(spaceTrim__default["default"]((block) => `
2379
+ ${block(error.message)}
2380
+
2381
+ The JSON text:
2382
+ ${block(value)}
2383
+ `));
2384
+ }
2385
+ }
2386
+ /**
2387
+ * TODO: !!!! Use in Promptbook.studio
2388
+ */
2389
+
2390
+ /**
2391
+ * Recursively converts JSON strings to JSON objects
2392
+
2393
+ * @public exported from `@promptbook/utils`
2394
+ */
2395
+ function jsonStringsToJsons(object) {
2396
+ if (object === null) {
2397
+ return object;
2398
+ }
2399
+ if (Array.isArray(object)) {
2400
+ return object.map(jsonStringsToJsons);
2401
+ }
2402
+ if (typeof object !== 'object') {
2403
+ return object;
2404
+ }
2405
+ const newObject = { ...object };
2406
+ for (const [key, value] of Object.entries(object)) {
2407
+ if (typeof value === 'string' && isValidJsonString(value)) {
2408
+ newObject[key] = jsonParse(value);
2409
+ }
2410
+ else {
2411
+ newObject[key] = jsonStringsToJsons(value);
2412
+ }
2413
+ }
2414
+ return newObject;
2415
+ }
2416
+ /**
2417
+ * TODO: Type the return type correctly
2418
+ */
2419
+
2391
2420
  /**
2392
2421
  * Deserializes the error object
2393
2422
  *
@@ -2553,64 +2582,6 @@
2553
2582
  * TODO: [🐚] Split into more files and make `PrepareTask` & `RemoteTask` + split the function
2554
2583
  */
2555
2584
 
2556
- /**
2557
- * Serializes an error into a [🚉] JSON-serializable object
2558
- *
2559
- * @public exported from `@promptbook/utils`
2560
- */
2561
- function serializeError(error) {
2562
- const { name, message, stack } = error;
2563
- const { id } = error;
2564
- if (!Object.keys(ALL_ERRORS).includes(name)) {
2565
- console.error(spaceTrim__default["default"]((block) => `
2566
-
2567
- Cannot serialize error with name "${name}"
2568
-
2569
- Authors of Promptbook probably forgot to add this error into the list of errors:
2570
- https://github.com/webgptorg/promptbook/blob/main/src/errors/0-index.ts
2571
-
2572
-
2573
- ${block(stack || message)}
2574
-
2575
- `));
2576
- }
2577
- return {
2578
- name: name,
2579
- message,
2580
- stack,
2581
- id, // Include id in the serialized object
2582
- };
2583
- }
2584
-
2585
- /**
2586
- * Async version of Array.forEach
2587
- *
2588
- * @param array - Array to iterate over
2589
- * @param options - Options for the function
2590
- * @param callbackfunction - Function to call for each item
2591
- * @public exported from `@promptbook/utils`
2592
- * @deprecated [🪂] Use queues instead
2593
- */
2594
- async function forEachAsync(array, options, callbackfunction) {
2595
- const { maxParallelCount = Infinity } = options;
2596
- let index = 0;
2597
- let runningTasks = [];
2598
- const tasks = [];
2599
- for (const item of array) {
2600
- const currentIndex = index++;
2601
- const task = callbackfunction(item, currentIndex, array);
2602
- tasks.push(task);
2603
- runningTasks.push(task);
2604
- /* not await */ Promise.resolve(task).then(() => {
2605
- runningTasks = runningTasks.filter((t) => t !== task);
2606
- });
2607
- if (maxParallelCount < runningTasks.length) {
2608
- await Promise.race(runningTasks);
2609
- }
2610
- }
2611
- await Promise.all(tasks);
2612
- }
2613
-
2614
2585
  /**
2615
2586
  * Represents the uncertain value
2616
2587
  *
@@ -2654,7 +2625,7 @@
2654
2625
  *
2655
2626
  * @public exported from `@promptbook/core`
2656
2627
  */
2657
- $deepFreeze({
2628
+ const UNCERTAIN_USAGE = $deepFreeze({
2658
2629
  price: UNCERTAIN_ZERO_VALUE,
2659
2630
  input: {
2660
2631
  tokensCount: UNCERTAIN_ZERO_VALUE,
@@ -2679,6 +2650,35 @@
2679
2650
  * Note: [💞] Ignore a discrepancy between file name and entity name
2680
2651
  */
2681
2652
 
2653
+ /**
2654
+ * Async version of Array.forEach
2655
+ *
2656
+ * @param array - Array to iterate over
2657
+ * @param options - Options for the function
2658
+ * @param callbackfunction - Function to call for each item
2659
+ * @public exported from `@promptbook/utils`
2660
+ * @deprecated [🪂] Use queues instead
2661
+ */
2662
+ async function forEachAsync(array, options, callbackfunction) {
2663
+ const { maxParallelCount = Infinity } = options;
2664
+ let index = 0;
2665
+ let runningTasks = [];
2666
+ const tasks = [];
2667
+ for (const item of array) {
2668
+ const currentIndex = index++;
2669
+ const task = callbackfunction(item, currentIndex, array);
2670
+ tasks.push(task);
2671
+ runningTasks.push(task);
2672
+ /* not await */ Promise.resolve(task).then(() => {
2673
+ runningTasks = runningTasks.filter((t) => t !== task);
2674
+ });
2675
+ if (maxParallelCount < runningTasks.length) {
2676
+ await Promise.race(runningTasks);
2677
+ }
2678
+ }
2679
+ await Promise.all(tasks);
2680
+ }
2681
+
2682
2682
  /**
2683
2683
  * Function `addUsage` will add multiple usages into one
2684
2684
  *
@@ -4086,6 +4086,24 @@
4086
4086
  // encoding: 'utf-8',
4087
4087
  });
4088
4088
 
4089
+ /**
4090
+ * Converts a CSV string into an object
4091
+ *
4092
+ * Note: This is wrapper around `papaparse.parse()` with better autohealing
4093
+ *
4094
+ * @private - for now until `@promptbook/csv` is released
4095
+ */
4096
+ function csvParse(value /* <- TODO: string_csv */, settings, schema /* <- TODO: Make CSV Schemas */) {
4097
+ settings = { ...settings, ...MANDATORY_CSV_SETTINGS };
4098
+ // Note: Autoheal invalid '\n' characters
4099
+ if (settings.newline && !settings.newline.includes('\r') && value.includes('\r')) {
4100
+ console.warn('CSV string contains carriage return characters, but in the CSV settings the `newline` setting does not include them. Autohealing the CSV string.');
4101
+ value = value.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
4102
+ }
4103
+ const csv = papaparse.parse(value, settings);
4104
+ return csv;
4105
+ }
4106
+
4089
4107
  /**
4090
4108
  * Function to check if a string is valid CSV
4091
4109
  *
@@ -4108,31 +4126,13 @@
4108
4126
  }
4109
4127
  }
4110
4128
 
4111
- /**
4112
- * Converts a CSV string into an object
4113
- *
4114
- * Note: This is wrapper around `papaparse.parse()` with better autohealing
4115
- *
4116
- * @private - for now until `@promptbook/csv` is released
4117
- */
4118
- function csvParse(value /* <- TODO: string_csv */, settings, schema /* <- TODO: Make CSV Schemas */) {
4119
- settings = { ...settings, ...MANDATORY_CSV_SETTINGS };
4120
- // Note: Autoheal invalid '\n' characters
4121
- if (settings.newline && !settings.newline.includes('\r') && value.includes('\r')) {
4122
- console.warn('CSV string contains carriage return characters, but in the CSV settings the `newline` setting does not include them. Autohealing the CSV string.');
4123
- value = value.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
4124
- }
4125
- const csv = papaparse.parse(value, settings);
4126
- return csv;
4127
- }
4128
-
4129
4129
  /**
4130
4130
  * Definition for CSV spreadsheet
4131
4131
  *
4132
4132
  * @public exported from `@promptbook/core`
4133
4133
  * <- TODO: [🏢] Export from package `@promptbook/csv`
4134
4134
  */
4135
- const CsvFormatDefinition = {
4135
+ const CsvFormatParser = {
4136
4136
  formatName: 'CSV',
4137
4137
  aliases: ['SPREADSHEET', 'TABLE'],
4138
4138
  isValid(value, settings, schema) {
@@ -4144,7 +4144,7 @@
4144
4144
  heal(value, settings, schema) {
4145
4145
  throw new Error('Not implemented');
4146
4146
  },
4147
- subvalueDefinitions: [
4147
+ subvalueParsers: [
4148
4148
  {
4149
4149
  subvalueName: 'ROW',
4150
4150
  async mapValues(value, outputParameterName, settings, mapCallback) {
@@ -4205,10 +4205,10 @@
4205
4205
  ],
4206
4206
  };
4207
4207
  /**
4208
- * TODO: [🍓] In `CsvFormatDefinition` implement simple `isValid`
4209
- * TODO: [🍓] In `CsvFormatDefinition` implement partial `canBeValid`
4210
- * TODO: [🍓] In `CsvFormatDefinition` implement `heal
4211
- * TODO: [🍓] In `CsvFormatDefinition` implement `subvalueDefinitions`
4208
+ * TODO: [🍓] In `CsvFormatParser` implement simple `isValid`
4209
+ * TODO: [🍓] In `CsvFormatParser` implement partial `canBeValid`
4210
+ * TODO: [🍓] In `CsvFormatParser` implement `heal
4211
+ * TODO: [🍓] In `CsvFormatParser` implement `subvalueParsers`
4212
4212
  * TODO: [🏢] Allow to expect something inside CSV objects and other formats
4213
4213
  */
4214
4214
 
@@ -4217,7 +4217,7 @@
4217
4217
  *
4218
4218
  * @private still in development [🏢]
4219
4219
  */
4220
- const JsonFormatDefinition = {
4220
+ const JsonFormatParser = {
4221
4221
  formatName: 'JSON',
4222
4222
  mimeType: 'application/json',
4223
4223
  isValid(value, settings, schema) {
@@ -4229,28 +4229,28 @@
4229
4229
  heal(value, settings, schema) {
4230
4230
  throw new Error('Not implemented');
4231
4231
  },
4232
- subvalueDefinitions: [],
4232
+ subvalueParsers: [],
4233
4233
  };
4234
4234
  /**
4235
4235
  * TODO: [🧠] Maybe propper instance of object
4236
4236
  * TODO: [0] Make string_serialized_json
4237
4237
  * TODO: [1] Make type for JSON Settings and Schema
4238
4238
  * TODO: [🧠] What to use for validating JSONs - JSON Schema, ZoD, typescript types/interfaces,...?
4239
- * TODO: [🍓] In `JsonFormatDefinition` implement simple `isValid`
4240
- * TODO: [🍓] In `JsonFormatDefinition` implement partial `canBeValid`
4241
- * TODO: [🍓] In `JsonFormatDefinition` implement `heal
4242
- * TODO: [🍓] In `JsonFormatDefinition` implement `subvalueDefinitions`
4239
+ * TODO: [🍓] In `JsonFormatParser` implement simple `isValid`
4240
+ * TODO: [🍓] In `JsonFormatParser` implement partial `canBeValid`
4241
+ * TODO: [🍓] In `JsonFormatParser` implement `heal
4242
+ * TODO: [🍓] In `JsonFormatParser` implement `subvalueParsers`
4243
4243
  * TODO: [🏢] Allow to expect something inside JSON objects and other formats
4244
4244
  */
4245
4245
 
4246
4246
  /**
4247
4247
  * Definition for any text - this will be always valid
4248
4248
  *
4249
- * Note: This is not useful for validation, but for splitting and mapping with `subvalueDefinitions`
4249
+ * Note: This is not useful for validation, but for splitting and mapping with `subvalueParsers`
4250
4250
  *
4251
4251
  * @public exported from `@promptbook/core`
4252
4252
  */
4253
- const TextFormatDefinition = {
4253
+ const TextFormatParser = {
4254
4254
  formatName: 'TEXT',
4255
4255
  isValid(value) {
4256
4256
  return typeof value === 'string';
@@ -4259,9 +4259,9 @@
4259
4259
  return typeof partialValue === 'string';
4260
4260
  },
4261
4261
  heal() {
4262
- throw new UnexpectedError('It does not make sense to call `TextFormatDefinition.heal`');
4262
+ throw new UnexpectedError('It does not make sense to call `TextFormatParser.heal`');
4263
4263
  },
4264
- subvalueDefinitions: [
4264
+ subvalueParsers: [
4265
4265
  {
4266
4266
  subvalueName: 'LINE',
4267
4267
  async mapValues(value, outputParameterName, settings, mapCallback) {
@@ -4281,10 +4281,10 @@
4281
4281
  /**
4282
4282
  * TODO: [1] Make type for XML Text and Schema
4283
4283
  * TODO: [🧠][🤠] Here should be all words, characters, lines, paragraphs, pages available as subvalues
4284
- * TODO: [🍓] In `TextFormatDefinition` implement simple `isValid`
4285
- * TODO: [🍓] In `TextFormatDefinition` implement partial `canBeValid`
4286
- * TODO: [🍓] In `TextFormatDefinition` implement `heal
4287
- * TODO: [🍓] In `TextFormatDefinition` implement `subvalueDefinitions`
4284
+ * TODO: [🍓] In `TextFormatParser` implement simple `isValid`
4285
+ * TODO: [🍓] In `TextFormatParser` implement partial `canBeValid`
4286
+ * TODO: [🍓] In `TextFormatParser` implement `heal
4287
+ * TODO: [🍓] In `TextFormatParser` implement `subvalueParsers`
4288
4288
  * TODO: [🏢] Allow to expect something inside each item of list and other formats
4289
4289
  */
4290
4290
 
@@ -4317,7 +4317,7 @@
4317
4317
  *
4318
4318
  * @private still in development [🏢]
4319
4319
  */
4320
- const XmlFormatDefinition = {
4320
+ const XmlFormatParser = {
4321
4321
  formatName: 'XML',
4322
4322
  mimeType: 'application/xml',
4323
4323
  isValid(value, settings, schema) {
@@ -4329,17 +4329,17 @@
4329
4329
  heal(value, settings, schema) {
4330
4330
  throw new Error('Not implemented');
4331
4331
  },
4332
- subvalueDefinitions: [],
4332
+ subvalueParsers: [],
4333
4333
  };
4334
4334
  /**
4335
4335
  * TODO: [🧠] Maybe propper instance of object
4336
4336
  * TODO: [0] Make string_serialized_xml
4337
4337
  * TODO: [1] Make type for XML Settings and Schema
4338
4338
  * TODO: [🧠] What to use for validating XMLs - XSD,...
4339
- * TODO: [🍓] In `XmlFormatDefinition` implement simple `isValid`
4340
- * TODO: [🍓] In `XmlFormatDefinition` implement partial `canBeValid`
4341
- * TODO: [🍓] In `XmlFormatDefinition` implement `heal
4342
- * TODO: [🍓] In `XmlFormatDefinition` implement `subvalueDefinitions`
4339
+ * TODO: [🍓] In `XmlFormatParser` implement simple `isValid`
4340
+ * TODO: [🍓] In `XmlFormatParser` implement partial `canBeValid`
4341
+ * TODO: [🍓] In `XmlFormatParser` implement `heal
4342
+ * TODO: [🍓] In `XmlFormatParser` implement `subvalueParsers`
4343
4343
  * TODO: [🏢] Allow to expect something inside XML and other formats
4344
4344
  */
4345
4345
 
@@ -4348,12 +4348,7 @@
4348
4348
  *
4349
4349
  * @private internal index of `...` <- TODO [🏢]
4350
4350
  */
4351
- const FORMAT_DEFINITIONS = [
4352
- JsonFormatDefinition,
4353
- XmlFormatDefinition,
4354
- TextFormatDefinition,
4355
- CsvFormatDefinition,
4356
- ];
4351
+ const FORMAT_DEFINITIONS = [JsonFormatParser, XmlFormatParser, TextFormatParser, CsvFormatParser];
4357
4352
  /**
4358
4353
  * Note: [💞] Ignore a discrepancy between file name and entity name
4359
4354
  */
@@ -4523,7 +4518,7 @@
4523
4518
  }
4524
4519
  /**
4525
4520
  * TODO: Add some auto-healing logic + extract YAML, JSON5, TOML, etc.
4526
- * TODO: [🏢] Make this logic part of `JsonFormatDefinition` or `isValidJsonString`
4521
+ * TODO: [🏢] Make this logic part of `JsonFormatParser` or `isValidJsonString`
4527
4522
  */
4528
4523
 
4529
4524
  /**
@@ -4725,7 +4720,7 @@
4725
4720
  PAGES: countPages,
4726
4721
  };
4727
4722
  /**
4728
- * TODO: [🧠][🤠] This should be probbably as part of `TextFormatDefinition`
4723
+ * TODO: [🧠][🤠] This should be probbably as part of `TextFormatParser`
4729
4724
  * Note: [💞] Ignore a discrepancy between file name and entity name
4730
4725
  */
4731
4726
 
@@ -4753,7 +4748,7 @@
4753
4748
  }
4754
4749
  /**
4755
4750
  * TODO: [💝] Unite object for expecting amount and format
4756
- * TODO: [🧠][🤠] This should be part of `TextFormatDefinition`
4751
+ * TODO: [🧠][🤠] This should be part of `TextFormatParser`
4757
4752
  * Note: [💝] and [🤠] are interconnected together
4758
4753
  */
4759
4754
 
@@ -4981,7 +4976,7 @@
4981
4976
  if (task.format) {
4982
4977
  if (task.format === 'JSON') {
4983
4978
  if (!isValidJsonString($ongoingTaskResult.$resultString || '')) {
4984
- // TODO: [🏢] Do more universally via `FormatDefinition`
4979
+ // TODO: [🏢] Do more universally via `FormatParser`
4985
4980
  try {
4986
4981
  $ongoingTaskResult.$resultString = extractJsonBlock($ongoingTaskResult.$resultString || '');
4987
4982
  }
@@ -5119,16 +5114,16 @@
5119
5114
  ${block(pipelineIdentification)}
5120
5115
  `));
5121
5116
  }
5122
- const subvalueDefinition = formatDefinition.subvalueDefinitions.find((subvalueDefinition) => [subvalueDefinition.subvalueName, ...(subvalueDefinition.aliases || [])].includes(task.foreach.subformatName));
5123
- if (subvalueDefinition === undefined) {
5117
+ const subvalueParser = formatDefinition.subvalueParsers.find((subvalueParser) => [subvalueParser.subvalueName, ...(subvalueParser.aliases || [])].includes(task.foreach.subformatName));
5118
+ if (subvalueParser === undefined) {
5124
5119
  throw new UnexpectedError(
5125
5120
  // <- TODO: [🧠][🧐] Should be formats fixed per promptbook version or behave as plugins (=> change UnexpectedError)
5126
5121
  spaceTrim__default["default"]((block) => `
5127
5122
  Unsupported subformat name "${task.foreach.subformatName}" for format "${task.foreach.formatName}"
5128
5123
 
5129
5124
  Available subformat names for format "${formatDefinition.formatName}":
5130
- ${block(formatDefinition.subvalueDefinitions
5131
- .map((subvalueDefinition) => subvalueDefinition.subvalueName)
5125
+ ${block(formatDefinition.subvalueParsers
5126
+ .map((subvalueParser) => subvalueParser.subvalueName)
5132
5127
  .map((subvalueName) => `- ${subvalueName}`)
5133
5128
  .join('\n'))}
5134
5129
 
@@ -5142,7 +5137,7 @@
5142
5137
  formatSettings = csvSettings;
5143
5138
  // <- TODO: [🤹‍♂️] More universal, make simmilar pattern for other formats for example \n vs \r\n in text
5144
5139
  }
5145
- const resultString = await subvalueDefinition.mapValues(parameterValue, task.foreach.outputSubparameterName, formatSettings, async (subparameters, index) => {
5140
+ const resultString = await subvalueParser.mapValues(parameterValue, task.foreach.outputSubparameterName, formatSettings, async (subparameters, index) => {
5146
5141
  let mappedParameters;
5147
5142
  // TODO: [🤹‍♂️][🪂] Limit to N concurrent executions
5148
5143
  // TODO: When done [🐚] Report progress also for each subvalue here
@@ -5204,6 +5199,27 @@
5204
5199
  return RESERVED_PARAMETER_MISSING_VALUE /* <- TODO: [♨] Implement */;
5205
5200
  }
5206
5201
 
5202
+ /**
5203
+ * Computes the cosine similarity between two embedding vectors
5204
+ *
5205
+ * Note: This is helping function for RAG (retrieval-augmented generation)
5206
+ *
5207
+ * @param embeddingVector1
5208
+ * @param embeddingVector2
5209
+ * @returns Cosine similarity between the two vectors
5210
+ *
5211
+ * @public exported from `@promptbook/core`
5212
+ */
5213
+ function computeCosineSimilarity(embeddingVector1, embeddingVector2) {
5214
+ if (embeddingVector1.length !== embeddingVector2.length) {
5215
+ throw new TypeError('Embedding vectors must have the same length');
5216
+ }
5217
+ const dotProduct = embeddingVector1.reduce((sum, value, index) => sum + value * embeddingVector2[index], 0);
5218
+ const magnitude1 = Math.sqrt(embeddingVector1.reduce((sum, value) => sum + value * value, 0));
5219
+ const magnitude2 = Math.sqrt(embeddingVector2.reduce((sum, value) => sum + value * value, 0));
5220
+ return 1 - dotProduct / (magnitude1 * magnitude2);
5221
+ }
5222
+
5207
5223
  /**
5208
5224
  * @@@
5209
5225
  *
@@ -5230,7 +5246,7 @@
5230
5246
  },
5231
5247
  content: task.content,
5232
5248
  parameters: {
5233
- /* !!!!!!!! */
5249
+ /* !!!! */
5234
5250
  },
5235
5251
  };
5236
5252
  const taskEmbeddingResult = await llmTools.callEmbeddingModel(taskEmbeddingPrompt);
@@ -5265,16 +5281,6 @@
5265
5281
  return knowledgePiecesLimited.map(({ content }) => `- ${content}`).join('\n');
5266
5282
  // <- TODO: [🧠] Some smart aggregation of knowledge pieces, single-line vs multi-line vs mixed
5267
5283
  }
5268
- // TODO: !!!!!! Annotate + to new file
5269
- function computeCosineSimilarity(embeddingVector1, embeddingVector2) {
5270
- if (embeddingVector1.length !== embeddingVector2.length) {
5271
- throw new TypeError('Embedding vectors must have the same length');
5272
- }
5273
- const dotProduct = embeddingVector1.reduce((sum, value, index) => sum + value * embeddingVector2[index], 0);
5274
- const magnitude1 = Math.sqrt(embeddingVector1.reduce((sum, value) => sum + value * value, 0));
5275
- const magnitude2 = Math.sqrt(embeddingVector2.reduce((sum, value) => sum + value * value, 0));
5276
- return 1 - dotProduct / (magnitude1 * magnitude2);
5277
- }
5278
5284
  /**
5279
5285
  * TODO: !!!! Verify if this is working
5280
5286
  * TODO: [♨] Implement Better - use keyword search
@@ -5777,6 +5783,22 @@
5777
5783
  cacheDirname,
5778
5784
  intermediateFilesStrategy,
5779
5785
  isAutoInstalled,
5786
+ }).catch((error) => {
5787
+ assertsError(error);
5788
+ return exportJson({
5789
+ name: 'pipelineExecutorResult',
5790
+ message: `Unuccessful PipelineExecutorResult, last catch`,
5791
+ order: [],
5792
+ value: {
5793
+ isSuccessful: false,
5794
+ errors: [serializeError(error)],
5795
+ warnings: [],
5796
+ usage: UNCERTAIN_USAGE,
5797
+ executionReport: null,
5798
+ outputParameters: {},
5799
+ preparedPipeline,
5800
+ },
5801
+ });
5780
5802
  });
5781
5803
  };
5782
5804
  const pipelineExecutor = (inputParameters) => createTask({