@promptbook/utils 0.103.0-55 → 0.103.0-56

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
@@ -1,4 +1,4 @@
1
- import spaceTrim$1, { spaceTrim as spaceTrim$2 } from 'spacetrim';
1
+ import spaceTrim$2, { spaceTrim as spaceTrim$1 } from 'spacetrim';
2
2
  import { basename } from 'path';
3
3
  import { randomBytes } from 'crypto';
4
4
  import { SHA256 } from 'crypto-js';
@@ -18,12 +18,23 @@ const BOOK_LANGUAGE_VERSION = '2.0.0';
18
18
  * @generated
19
19
  * @see https://github.com/webgptorg/promptbook
20
20
  */
21
- const PROMPTBOOK_ENGINE_VERSION = '0.103.0-55';
21
+ const PROMPTBOOK_ENGINE_VERSION = '0.103.0-56';
22
22
  /**
23
23
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
24
24
  * Note: [💞] Ignore a discrepancy between file name and entity name
25
25
  */
26
26
 
27
+ /**
28
+ * Trims string from all 4 sides
29
+ *
30
+ * Note: This is a re-exported function from the `spacetrim` package which is
31
+ * Developed by same author @hejny as this package
32
+ *
33
+ * @public exported from `@promptbook/utils`
34
+ * @see https://github.com/hejny/spacetrim#usage
35
+ */
36
+ const spaceTrim = spaceTrim$1;
37
+
27
38
  /**
28
39
  * @private util of `@promptbook/color`
29
40
  * @de
@@ -72,6 +83,7 @@ function take(initialValue) {
72
83
  * @public exported from `@promptbook/color`
73
84
  */
74
85
  const CSS_COLORS = {
86
+ promptbook: '#79EAFD',
75
87
  transparent: 'rgba(0,0,0,0)',
76
88
  aliceblue: '#f0f8ff',
77
89
  antiquewhite: '#faebd7',
@@ -287,6 +299,28 @@ class Color {
287
299
  throw new Error(`Can not create color from given object`);
288
300
  }
289
301
  }
302
+ /**
303
+ * Creates a new Color instance from miscellaneous formats
304
+ * It just does not throw error when it fails, it returns PROMPTBOOK_COLOR instead
305
+ *
306
+ * @param color
307
+ * @returns Color object
308
+ */
309
+ static fromSafe(color) {
310
+ try {
311
+ return Color.from(color);
312
+ }
313
+ catch (error) {
314
+ // <- Note: Can not use `assertsError(error)` here because it causes circular dependency
315
+ console.warn(spaceTrim((block) => `
316
+ Color.fromSafe error:
317
+ ${block(error.message)}
318
+
319
+ Returning default PROMPTBOOK_COLOR.
320
+ `));
321
+ return Color.fromString('promptbook');
322
+ }
323
+ }
290
324
  /**
291
325
  * Creates a new Color instance from miscellaneous string formats
292
326
  *
@@ -876,7 +910,7 @@ const ADMIN_GITHUB_NAME = 'hejny';
876
910
  *
877
911
  * @public exported from `@promptbook/core`
878
912
  */
879
- const PROMPTBOOK_COLOR = Color.fromHex('#79EAFD');
913
+ const PROMPTBOOK_COLOR = Color.fromString('promptbook');
880
914
  // <- TODO: [🧠][🈵] Using `Color` here increases the package size approx 3kb, maybe remove it
881
915
  /**
882
916
  * Colors for syntax highlighting in the `<BookEditor/>`
@@ -1012,7 +1046,7 @@ function $deepFreeze(objectValue) {
1012
1046
  function getErrorReportUrl(error) {
1013
1047
  const report = {
1014
1048
  title: `🐜 Error report from ${NAME}`,
1015
- body: spaceTrim$1((block) => `
1049
+ body: spaceTrim$2((block) => `
1016
1050
 
1017
1051
 
1018
1052
  \`${error.name || 'Error'}\` has occurred in the [${NAME}], please look into it @${ADMIN_GITHUB_NAME}.
@@ -1055,7 +1089,7 @@ function getErrorReportUrl(error) {
1055
1089
  */
1056
1090
  class UnexpectedError extends Error {
1057
1091
  constructor(message) {
1058
- super(spaceTrim$2((block) => `
1092
+ super(spaceTrim$1((block) => `
1059
1093
  ${block(message)}
1060
1094
 
1061
1095
  Note: This error should not happen.
@@ -1081,7 +1115,7 @@ class WrappedError extends Error {
1081
1115
  constructor(whatWasThrown) {
1082
1116
  const tag = `[🤮]`;
1083
1117
  console.error(tag, whatWasThrown);
1084
- super(spaceTrim$2(`
1118
+ super(spaceTrim$1(`
1085
1119
  Non-Error object was thrown
1086
1120
 
1087
1121
  Note: Look for ${tag} in the console for more details
@@ -1170,7 +1204,7 @@ function checkSerializableAsJson(options) {
1170
1204
  }
1171
1205
  else if (typeof value === 'object') {
1172
1206
  if (value instanceof Date) {
1173
- throw new UnexpectedError(spaceTrim$1((block) => `
1207
+ throw new UnexpectedError(spaceTrim$2((block) => `
1174
1208
  \`${name}\` is Date
1175
1209
 
1176
1210
  Use \`string_date_iso8601\` instead
@@ -1189,7 +1223,7 @@ function checkSerializableAsJson(options) {
1189
1223
  throw new UnexpectedError(`${name} is RegExp`);
1190
1224
  }
1191
1225
  else if (value instanceof Error) {
1192
- throw new UnexpectedError(spaceTrim$1((block) => `
1226
+ throw new UnexpectedError(spaceTrim$2((block) => `
1193
1227
  \`${name}\` is unserialized Error
1194
1228
 
1195
1229
  Use function \`serializeError\`
@@ -1212,7 +1246,7 @@ function checkSerializableAsJson(options) {
1212
1246
  }
1213
1247
  catch (error) {
1214
1248
  assertsError(error);
1215
- throw new UnexpectedError(spaceTrim$1((block) => `
1249
+ throw new UnexpectedError(spaceTrim$2((block) => `
1216
1250
  \`${name}\` is not serializable
1217
1251
 
1218
1252
  ${block(error.stack || error.message)}
@@ -1244,7 +1278,7 @@ function checkSerializableAsJson(options) {
1244
1278
  }
1245
1279
  }
1246
1280
  else {
1247
- throw new UnexpectedError(spaceTrim$1((block) => `
1281
+ throw new UnexpectedError(spaceTrim$2((block) => `
1248
1282
  \`${name}\` is unknown type
1249
1283
 
1250
1284
  Additional message for \`${name}\`:
@@ -1928,7 +1962,7 @@ function renderPromptbookMermaid(pipelineJson, options) {
1928
1962
  .filter(([MERMAID_NAME]) => (inputAndIntermediateParametersMermaid + outputParametersMermaid).includes(MERMAID_NAME))
1929
1963
  .map(([MERMAID_NAME, title]) => `${MERMAID_NAME}((${title})):::${MERMAID_NAME}`)
1930
1964
  .join('\n');
1931
- const promptbookMermaid = spaceTrim$2((block) => `
1965
+ const promptbookMermaid = spaceTrim$1((block) => `
1932
1966
 
1933
1967
  %% 🔮 Tip: Open this on GitHub or in the VSCode website to see the Mermaid graph visually
1934
1968
 
@@ -2104,7 +2138,7 @@ class LimitReachedError extends Error {
2104
2138
  */
2105
2139
  class MissingToolsError extends Error {
2106
2140
  constructor(message) {
2107
- super(spaceTrim$2((block) => `
2141
+ super(spaceTrim$1((block) => `
2108
2142
  ${block(message)}
2109
2143
 
2110
2144
  Note: You have probably forgot to provide some tools for pipeline execution or preparation
@@ -2148,7 +2182,7 @@ class NotFoundError extends Error {
2148
2182
  */
2149
2183
  class NotYetImplementedError extends Error {
2150
2184
  constructor(message) {
2151
- super(spaceTrim$2((block) => `
2185
+ super(spaceTrim$1((block) => `
2152
2186
  ${block(message)}
2153
2187
 
2154
2188
  Note: This feature is not implemented yet but it will be soon.
@@ -2333,7 +2367,7 @@ function deserializeError(error) {
2333
2367
  message = `${name}: ${message}`;
2334
2368
  }
2335
2369
  if (stack !== undefined && stack !== '') {
2336
- message = spaceTrim$1((block) => `
2370
+ message = spaceTrim$2((block) => `
2337
2371
  ${block(message)}
2338
2372
 
2339
2373
  Original stack trace:
@@ -2354,7 +2388,7 @@ function serializeError(error) {
2354
2388
  const { name, message, stack } = error;
2355
2389
  const { id } = error;
2356
2390
  if (!Object.keys(ALL_ERRORS).includes(name)) {
2357
- console.error(spaceTrim$1((block) => `
2391
+ console.error(spaceTrim$2((block) => `
2358
2392
 
2359
2393
  Cannot serialize error with name "${name}"
2360
2394
 
@@ -2460,7 +2494,7 @@ function jsonParse(value) {
2460
2494
  }
2461
2495
  else if (typeof value !== 'string') {
2462
2496
  console.error('Can not parse JSON from non-string value.', { text: value });
2463
- throw new Error(spaceTrim$1(`
2497
+ throw new Error(spaceTrim$2(`
2464
2498
  Can not parse JSON from non-string value.
2465
2499
 
2466
2500
  The value type: ${typeof value}
@@ -2474,7 +2508,7 @@ function jsonParse(value) {
2474
2508
  if (!(error instanceof Error)) {
2475
2509
  throw error;
2476
2510
  }
2477
- throw new Error(spaceTrim$1((block) => `
2511
+ throw new Error(spaceTrim$2((block) => `
2478
2512
  ${block(error.message)}
2479
2513
 
2480
2514
  The expected JSON text:
@@ -2674,7 +2708,7 @@ function templateParameters(template, parameters) {
2674
2708
  */
2675
2709
  function prompt(strings, ...values) {
2676
2710
  if (values.length === 0) {
2677
- return spaceTrim$1(strings.join(''));
2711
+ return spaceTrim$2(strings.join(''));
2678
2712
  }
2679
2713
  const stringsWithHiddenParameters = strings.map((stringsItem) =>
2680
2714
  // TODO: [0] DRY
@@ -2685,7 +2719,7 @@ function prompt(strings, ...values) {
2685
2719
  let pipelineString = stringsWithHiddenParameters.reduce((result, stringsItem, i) => placeholderParameterNames[i] === undefined
2686
2720
  ? `${result}${stringsItem}`
2687
2721
  : `${result}${stringsItem}{${placeholderParameterNames[i]}}`, '');
2688
- pipelineString = spaceTrim$1(pipelineString);
2722
+ pipelineString = spaceTrim$2(pipelineString);
2689
2723
  try {
2690
2724
  pipelineString = templateParameters(pipelineString, parameters);
2691
2725
  }
@@ -2694,7 +2728,7 @@ function prompt(strings, ...values) {
2694
2728
  throw error;
2695
2729
  }
2696
2730
  console.error({ pipelineString, parameters, placeholderParameterNames, error });
2697
- throw new UnexpectedError(spaceTrim$1((block) => `
2731
+ throw new UnexpectedError(spaceTrim$2((block) => `
2698
2732
  Internal error in prompt template literal
2699
2733
 
2700
2734
  ${block(JSON.stringify({ strings, values }, null, 4))}}
@@ -2981,7 +3015,7 @@ function $getCurrentDate() {
2981
3015
  * @public exported from `@promptbook/utils`
2982
3016
  */
2983
3017
  function computeHash(value) {
2984
- return SHA256(hexEncoder.parse(spaceTrim$1(valueToString(value)))).toString( /* hex */);
3018
+ return SHA256(hexEncoder.parse(spaceTrim$2(valueToString(value)))).toString( /* hex */);
2985
3019
  }
2986
3020
  /**
2987
3021
  * TODO: [🥬][🥬] Use this ACRY
@@ -3221,7 +3255,7 @@ function nameToUriParts(name) {
3221
3255
  * @public exported from `@promptbook/utils`
3222
3256
  */
3223
3257
  function normalizeMessageText(text) {
3224
- return spaceTrim$2(text);
3258
+ return spaceTrim$1(text);
3225
3259
  }
3226
3260
 
3227
3261
  /**
@@ -3371,7 +3405,7 @@ function unwrapResult(text, options) {
3371
3405
  let trimmedText = text;
3372
3406
  // Remove leading and trailing spaces and newlines
3373
3407
  if (isTrimmed) {
3374
- trimmedText = spaceTrim$2(trimmedText);
3408
+ trimmedText = spaceTrim$1(trimmedText);
3375
3409
  }
3376
3410
  let processedText = trimmedText;
3377
3411
  if (isIntroduceSentenceRemoved) {
@@ -3380,7 +3414,7 @@ function unwrapResult(text, options) {
3380
3414
  // Remove the introduce sentence and quotes by replacing it with an empty string
3381
3415
  processedText = processedText.replace(introduceSentenceRegex, '');
3382
3416
  }
3383
- processedText = spaceTrim$2(processedText);
3417
+ processedText = spaceTrim$1(processedText);
3384
3418
  }
3385
3419
  if (processedText.length < 3) {
3386
3420
  return trimmedText;
@@ -3424,17 +3458,6 @@ function unwrapResult(text, options) {
3424
3458
  * TODO: [🧠] Should this also unwrap the (parenthesis)
3425
3459
  */
3426
3460
 
3427
- /**
3428
- * Trims string from all 4 sides
3429
- *
3430
- * Note: This is a re-exported function from the `spacetrim` package which is
3431
- * Developed by same author @hejny as this package
3432
- *
3433
- * @public exported from `@promptbook/utils`
3434
- * @see https://github.com/hejny/spacetrim#usage
3435
- */
3436
- const spaceTrim = spaceTrim$2;
3437
-
3438
3461
  /**
3439
3462
  * Parses the task and returns the list of all parameter names
3440
3463
  *
@@ -3572,7 +3595,7 @@ function serializeToPromptbookJavascript(value) {
3572
3595
  imports.push(`import { Color } from '@promptbook/color';`);
3573
3596
  }
3574
3597
  else if (typeof value === 'string') {
3575
- const trimmed = spaceTrim$1(value);
3598
+ const trimmed = spaceTrim$2(value);
3576
3599
  if (trimmed.includes('\n')) {
3577
3600
  // Multiline string -> use `spaceTrim`
3578
3601
  serializedValue = `spaceTrim(\`\n${value.replace(/`/g, '\\`')}\n\`)`;