@promptbook/documents 0.89.0-11 โ†’ 0.89.0-14

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/esm/index.es.js CHANGED
@@ -28,7 +28,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
28
28
  * @generated
29
29
  * @see https://github.com/webgptorg/promptbook
30
30
  */
31
- const PROMPTBOOK_ENGINE_VERSION = '0.89.0-11';
31
+ const PROMPTBOOK_ENGINE_VERSION = '0.89.0-14';
32
32
  /**
33
33
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
34
34
  * Note: [๐Ÿ’ž] Ignore a discrepancy between file name and entity name
@@ -123,6 +123,7 @@ const VALUE_STRINGS = {
123
123
  infinity: '(infinity; โˆž)',
124
124
  negativeInfinity: '(negative infinity; -โˆž)',
125
125
  unserializable: '(unserializable value)',
126
+ circular: '(circular JSON)',
126
127
  };
127
128
  /**
128
129
  * Small number limit
@@ -1070,6 +1071,56 @@ class ParseError extends Error {
1070
1071
  * TODO: Maybe split `ParseError` and `ApplyError`
1071
1072
  */
1072
1073
 
1074
+ /**
1075
+ * This error type indicates that somewhere in the code non-Error object was thrown and it was wrapped into the `WrappedError`
1076
+ *
1077
+ * @public exported from `@promptbook/core`
1078
+ */
1079
+ class WrappedError extends Error {
1080
+ constructor(whatWasThrown) {
1081
+ const tag = `[๐Ÿคฎ]`;
1082
+ console.error(tag, whatWasThrown);
1083
+ super(spaceTrim((block) => `
1084
+ ${ /* Fixing tests !!! block(valueToString(whatWasThrown)) */block(`non-Error object was thrown`)}
1085
+
1086
+ Note: Look for ${tag} in the console for more details
1087
+ !!! Note: \`WrappedError\` indicates that somewhere in the code non-Error object was thrown and it was wrapped
1088
+
1089
+ Please report issue on ${ADMIN_EMAIL}
1090
+
1091
+ `));
1092
+ this.name = 'WrappedError';
1093
+ Object.setPrototypeOf(this, WrappedError.prototype);
1094
+ }
1095
+ }
1096
+
1097
+ /**
1098
+ * !!!@@@
1099
+ *
1100
+ * @param whatWasThrown !!!@@@
1101
+ * @returns !!!@@@
1102
+ *
1103
+ * @private within the repository
1104
+ */
1105
+ function assertsError(whatWasThrown) {
1106
+ // Case 1: !!!@@@
1107
+ if (whatWasThrown instanceof WrappedError) {
1108
+ const wrappedError = whatWasThrown;
1109
+ throw wrappedError;
1110
+ }
1111
+ // Case 2: !!!@@@
1112
+ if (whatWasThrown instanceof UnexpectedError) {
1113
+ const unexpectedError = whatWasThrown;
1114
+ throw unexpectedError;
1115
+ }
1116
+ // Case 3: !!!@@@
1117
+ if (whatWasThrown instanceof Error) {
1118
+ return;
1119
+ }
1120
+ // Case 4: !!!@@@
1121
+ throw new WrappedError(whatWasThrown);
1122
+ }
1123
+
1073
1124
  /**
1074
1125
  * Function isValidJsonString will tell you if the string is valid JSON or not
1075
1126
  *
@@ -1081,9 +1132,7 @@ function isValidJsonString(value /* <- [๐Ÿ‘จโ€โš–๏ธ] */) {
1081
1132
  return true;
1082
1133
  }
1083
1134
  catch (error) {
1084
- if (!(error instanceof Error)) {
1085
- throw error;
1086
- }
1135
+ assertsError(error);
1087
1136
  if (error.message.includes('Unexpected token')) {
1088
1137
  return false;
1089
1138
  }
@@ -1436,9 +1485,7 @@ function checkSerializableAsJson(options) {
1436
1485
  JSON.stringify(value); // <- TODO: [0]
1437
1486
  }
1438
1487
  catch (error) {
1439
- if (!(error instanceof Error)) {
1440
- throw error;
1441
- }
1488
+ assertsError(error);
1442
1489
  throw new UnexpectedError(spaceTrim$1((block) => `
1443
1490
  \`${name}\` is not serializable
1444
1491
 
@@ -2420,7 +2467,10 @@ const PROMPTBOOK_ERRORS = {
2420
2467
  PipelineExecutionError,
2421
2468
  PipelineLogicError,
2422
2469
  PipelineUrlError,
2470
+ AuthenticationError,
2471
+ PromptbookFetchError,
2423
2472
  UnexpectedError,
2473
+ WrappedError,
2424
2474
  // TODO: [๐Ÿช‘]> VersionMismatchError,
2425
2475
  };
2426
2476
  /**
@@ -2437,8 +2487,6 @@ const COMMON_JAVASCRIPT_ERRORS = {
2437
2487
  TypeError,
2438
2488
  URIError,
2439
2489
  AggregateError,
2440
- AuthenticationError,
2441
- PromptbookFetchError,
2442
2490
  /*
2443
2491
  Note: Not widely supported
2444
2492
  > InternalError,
@@ -2572,6 +2620,7 @@ function createTask(options) {
2572
2620
  partialResultSubject.next(executionResult);
2573
2621
  }
2574
2622
  catch (error) {
2623
+ assertsError(error);
2575
2624
  status = 'ERROR';
2576
2625
  errors.push(error);
2577
2626
  partialResultSubject.error(error);
@@ -2963,14 +3012,15 @@ class MultipleLlmExecutionTools {
2963
3012
  }
2964
3013
  }
2965
3014
  catch (error) {
2966
- if (!(error instanceof Error) || error instanceof UnexpectedError) {
3015
+ assertsError(error);
3016
+ if (error instanceof UnexpectedError) {
2967
3017
  throw error;
2968
3018
  }
2969
3019
  errors.push({ llmExecutionTools, error });
2970
3020
  }
2971
3021
  }
2972
3022
  if (errors.length === 1) {
2973
- throw errors[0];
3023
+ throw errors[0].error;
2974
3024
  }
2975
3025
  else if (errors.length > 1) {
2976
3026
  throw new PipelineExecutionError(
@@ -3415,9 +3465,7 @@ const promptbookFetch = async (urlOrRequest, init) => {
3415
3465
  return await fetch(urlOrRequest, init);
3416
3466
  }
3417
3467
  catch (error) {
3418
- if (!(error instanceof Error)) {
3419
- throw error;
3420
- }
3468
+ assertsError(error);
3421
3469
  let url;
3422
3470
  if (typeof urlOrRequest === 'string') {
3423
3471
  url = urlOrRequest;
@@ -3648,9 +3696,7 @@ async function prepareKnowledgePieces(knowledgeSources, tools, options) {
3648
3696
  knowledgePreparedUnflatten[index] = pieces;
3649
3697
  }
3650
3698
  catch (error) {
3651
- if (!(error instanceof Error)) {
3652
- throw error;
3653
- }
3699
+ assertsError(error);
3654
3700
  console.warn(error);
3655
3701
  // <- TODO: [๐Ÿฎ] Some standard way how to transform errors into warnings and how to handle non-critical fails during the tasks
3656
3702
  }
@@ -3942,13 +3988,19 @@ function valueToString(value) {
3942
3988
  return value.toISOString();
3943
3989
  }
3944
3990
  else {
3945
- return JSON.stringify(value);
3991
+ try {
3992
+ return JSON.stringify(value);
3993
+ }
3994
+ catch (error) {
3995
+ if (error instanceof TypeError && error.message.includes('circular structure')) {
3996
+ return VALUE_STRINGS.circular;
3997
+ }
3998
+ throw error;
3999
+ }
3946
4000
  }
3947
4001
  }
3948
4002
  catch (error) {
3949
- if (!(error instanceof Error)) {
3950
- throw error;
3951
- }
4003
+ assertsError(error);
3952
4004
  console.error(error);
3953
4005
  return VALUE_STRINGS.unserializable;
3954
4006
  }
@@ -4005,9 +4057,7 @@ function extractVariablesFromJavascript(script) {
4005
4057
  }
4006
4058
  }
4007
4059
  catch (error) {
4008
- if (!(error instanceof Error)) {
4009
- throw error;
4010
- }
4060
+ assertsError(error);
4011
4061
  throw new ParseError(spaceTrim((block) => `
4012
4062
  Can not extract variables from the script
4013
4063
  ${block(error.stack || error.message)}
@@ -4872,9 +4922,7 @@ async function executeAttempts(options) {
4872
4922
  break scripts;
4873
4923
  }
4874
4924
  catch (error) {
4875
- if (!(error instanceof Error)) {
4876
- throw error;
4877
- }
4925
+ assertsError(error);
4878
4926
  if (error instanceof UnexpectedError) {
4879
4927
  throw error;
4880
4928
  }
@@ -4944,9 +4992,7 @@ async function executeAttempts(options) {
4944
4992
  break scripts;
4945
4993
  }
4946
4994
  catch (error) {
4947
- if (!(error instanceof Error)) {
4948
- throw error;
4949
- }
4995
+ assertsError(error);
4950
4996
  if (error instanceof UnexpectedError) {
4951
4997
  throw error;
4952
4998
  }
@@ -5567,9 +5613,7 @@ async function executePipeline(options) {
5567
5613
  await Promise.all(resolving);
5568
5614
  }
5569
5615
  catch (error /* <- Note: [3] */) {
5570
- if (!(error instanceof Error)) {
5571
- throw error;
5572
- }
5616
+ assertsError(error);
5573
5617
  // Note: No need to rethrow UnexpectedError
5574
5618
  // if (error instanceof UnexpectedError) {
5575
5619
  // Note: Count usage, [๐Ÿง ] Maybe put to separate function executionReportJsonToUsage + DRY [๐Ÿคนโ€โ™‚๏ธ]