@promptbook/openai 0.102.0-8 → 0.102.0

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 (35) hide show
  1. package/README.md +0 -4
  2. package/esm/index.es.js +916 -1
  3. package/esm/index.es.js.map +1 -1
  4. package/esm/typings/src/_packages/color.index.d.ts +2 -0
  5. package/esm/typings/src/_packages/components.index.d.ts +4 -0
  6. package/esm/typings/src/_packages/core.index.d.ts +6 -0
  7. package/esm/typings/src/_packages/utils.index.d.ts +2 -0
  8. package/esm/typings/src/book-components/Chat/Chat/ChatMessageItem.d.ts +9 -1
  9. package/esm/typings/src/book-components/Chat/Chat/ChatProps.d.ts +4 -0
  10. package/esm/typings/src/book-components/Chat/save/_common/ChatSaveFormatDefinition.d.ts +43 -5
  11. package/esm/typings/src/book-components/Chat/save/html/htmlSaveFormatDefinition.d.ts +9 -2
  12. package/esm/typings/src/book-components/Chat/save/index.d.ts +38 -8
  13. package/esm/typings/src/book-components/Chat/save/json/jsonSaveFormatDefinition.d.ts +6 -2
  14. package/esm/typings/src/book-components/Chat/save/markdown/mdSaveFormatDefinition.d.ts +5 -1
  15. package/esm/typings/src/book-components/Chat/save/pdf/pdfSaveFormatDefinition.d.ts +6 -2
  16. package/esm/typings/src/book-components/Chat/save/react/reactSaveFormatDefinition.d.ts +16 -0
  17. package/esm/typings/src/book-components/Chat/save/text/txtSaveFormatDefinition.d.ts +5 -1
  18. package/esm/typings/src/book-components/Chat/types/ChatMessage.d.ts +1 -1
  19. package/esm/typings/src/book-components/Chat/types/ChatParticipant.d.ts +1 -1
  20. package/esm/typings/src/book-components/Chat/utils/exportChatHistory.d.ts +1 -1
  21. package/esm/typings/src/book-components/icons/SaveIcon.d.ts +10 -0
  22. package/esm/typings/src/config.d.ts +26 -4
  23. package/esm/typings/src/utils/color/Color.d.ts +8 -0
  24. package/esm/typings/src/utils/color/operators/darken.d.ts +2 -1
  25. package/esm/typings/src/utils/color/operators/grayscale.d.ts +2 -1
  26. package/esm/typings/src/utils/color/operators/lighten.d.ts +2 -1
  27. package/esm/typings/src/utils/color/operators/mixWithColor.d.ts +2 -1
  28. package/esm/typings/src/utils/color/operators/saturate.d.ts +13 -0
  29. package/esm/typings/src/utils/markdown/prettifyMarkdown.d.ts +2 -0
  30. package/esm/typings/src/utils/serialization/serializeToPromptbookJavascript.d.ts +22 -0
  31. package/esm/typings/src/version.d.ts +1 -1
  32. package/package.json +2 -2
  33. package/umd/index.umd.js +916 -1
  34. package/umd/index.umd.js.map +1 -1
  35. package/esm/typings/src/utils/markdown/prettifyMarkdown.test.d.ts +0 -1
package/esm/index.es.js CHANGED
@@ -19,7 +19,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
19
19
  * @generated
20
20
  * @see https://github.com/webgptorg/promptbook
21
21
  */
22
- const PROMPTBOOK_ENGINE_VERSION = '0.102.0-8';
22
+ const PROMPTBOOK_ENGINE_VERSION = '0.102.0';
23
23
  /**
24
24
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
25
25
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -229,6 +229,895 @@ function $getCurrentDate() {
229
229
  return new Date().toISOString();
230
230
  }
231
231
 
232
+ /**
233
+ * @private util of `@promptbook/color`
234
+ * @de
235
+ */
236
+ class TakeChain {
237
+ constructor(value) {
238
+ this.value = value;
239
+ }
240
+ then(callback) {
241
+ const newValue = callback(this.value);
242
+ return take(newValue);
243
+ }
244
+ }
245
+
246
+ /**
247
+ * A function that takes an initial value and returns a proxy object with chainable methods.
248
+ *
249
+ * @param {*} initialValue - The initial value.
250
+ * @returns {Proxy<WithTake<TValue>>} - A proxy object with a `take` method.
251
+ *
252
+ * @private util of `@promptbook/color`
253
+ * @deprecated [🤡] Use some better functional library instead of `TakeChain`
254
+ */
255
+ function take(initialValue) {
256
+ if (initialValue instanceof TakeChain) {
257
+ return initialValue;
258
+ }
259
+ return new Proxy(new TakeChain(initialValue), {
260
+ get(target, property, receiver) {
261
+ if (Reflect.has(target, property)) {
262
+ return Reflect.get(target, property, receiver);
263
+ }
264
+ else if (Reflect.has(initialValue, property)) {
265
+ return Reflect.get(initialValue, property, receiver);
266
+ }
267
+ else {
268
+ return undefined;
269
+ }
270
+ },
271
+ });
272
+ }
273
+
274
+ /**
275
+ * 🎨 List of all 140 color names which are supported by CSS
276
+ *
277
+ * @public exported from `@promptbook/color`
278
+ */
279
+ const CSS_COLORS = {
280
+ transparent: 'rgba(0,0,0,0)',
281
+ aliceblue: '#f0f8ff',
282
+ antiquewhite: '#faebd7',
283
+ aqua: '#00ffff',
284
+ aquamarine: '#7fffd4',
285
+ azure: '#f0ffff',
286
+ beige: '#f5f5dc',
287
+ bisque: '#ffe4c4',
288
+ black: '#000000',
289
+ blanchedalmond: '#ffebcd',
290
+ blue: '#0000ff',
291
+ blueviolet: '#8a2be2',
292
+ brown: '#a52a2a',
293
+ burlywood: '#deb887',
294
+ cadetblue: '#5f9ea0',
295
+ chartreuse: '#7fff00',
296
+ chocolate: '#d2691e',
297
+ coral: '#ff7f50',
298
+ cornflowerblue: '#6495ed',
299
+ cornsilk: '#fff8dc',
300
+ crimson: '#dc143c',
301
+ cyan: '#00ffff',
302
+ darkblue: '#00008b',
303
+ darkcyan: '#008b8b',
304
+ darkgoldenrod: '#b8860b',
305
+ darkgray: '#a9a9a9',
306
+ darkgrey: '#a9a9a9',
307
+ darkgreen: '#006400',
308
+ darkkhaki: '#bdb76b',
309
+ darkmagenta: '#8b008b',
310
+ darkolivegreen: '#556b2f',
311
+ darkorange: '#ff8c00',
312
+ darkorchid: '#9932cc',
313
+ darkred: '#8b0000',
314
+ darksalmon: '#e9967a',
315
+ darkseagreen: '#8fbc8f',
316
+ darkslateblue: '#483d8b',
317
+ darkslategray: '#2f4f4f',
318
+ darkslategrey: '#2f4f4f',
319
+ darkturquoise: '#00ced1',
320
+ darkviolet: '#9400d3',
321
+ deeppink: '#ff1493',
322
+ deepskyblue: '#00bfff',
323
+ dimgray: '#696969',
324
+ dimgrey: '#696969',
325
+ dodgerblue: '#1e90ff',
326
+ firebrick: '#b22222',
327
+ floralwhite: '#fffaf0',
328
+ forestgreen: '#228b22',
329
+ fuchsia: '#ff00ff',
330
+ gainsboro: '#dcdcdc',
331
+ ghostwhite: '#f8f8ff',
332
+ gold: '#ffd700',
333
+ goldenrod: '#daa520',
334
+ gray: '#808080',
335
+ grey: '#808080',
336
+ green: '#008000',
337
+ greenyellow: '#adff2f',
338
+ honeydew: '#f0fff0',
339
+ hotpink: '#ff69b4',
340
+ indianred: '#cd5c5c',
341
+ indigo: '#4b0082',
342
+ ivory: '#fffff0',
343
+ khaki: '#f0e68c',
344
+ lavender: '#e6e6fa',
345
+ lavenderblush: '#fff0f5',
346
+ lawngreen: '#7cfc00',
347
+ lemonchiffon: '#fffacd',
348
+ lightblue: '#add8e6',
349
+ lightcoral: '#f08080',
350
+ lightcyan: '#e0ffff',
351
+ lightgoldenrodyellow: '#fafad2',
352
+ lightgray: '#d3d3d3',
353
+ lightgrey: '#d3d3d3',
354
+ lightgreen: '#90ee90',
355
+ lightpink: '#ffb6c1',
356
+ lightsalmon: '#ffa07a',
357
+ lightseagreen: '#20b2aa',
358
+ lightskyblue: '#87cefa',
359
+ lightslategray: '#778899',
360
+ lightslategrey: '#778899',
361
+ lightsteelblue: '#b0c4de',
362
+ lightyellow: '#ffffe0',
363
+ lime: '#00ff00',
364
+ limegreen: '#32cd32',
365
+ linen: '#faf0e6',
366
+ magenta: '#ff00ff',
367
+ maroon: '#800000',
368
+ mediumaquamarine: '#66cdaa',
369
+ mediumblue: '#0000cd',
370
+ mediumorchid: '#ba55d3',
371
+ mediumpurple: '#9370db',
372
+ mediumseagreen: '#3cb371',
373
+ mediumslateblue: '#7b68ee',
374
+ mediumspringgreen: '#00fa9a',
375
+ mediumturquoise: '#48d1cc',
376
+ mediumvioletred: '#c71585',
377
+ midnightblue: '#191970',
378
+ mintcream: '#f5fffa',
379
+ mistyrose: '#ffe4e1',
380
+ moccasin: '#ffe4b5',
381
+ navajowhite: '#ffdead',
382
+ navy: '#000080',
383
+ oldlace: '#fdf5e6',
384
+ olive: '#808000',
385
+ olivedrab: '#6b8e23',
386
+ orange: '#ffa500',
387
+ orangered: '#ff4500',
388
+ orchid: '#da70d6',
389
+ palegoldenrod: '#eee8aa',
390
+ palegreen: '#98fb98',
391
+ paleturquoise: '#afeeee',
392
+ palevioletred: '#db7093',
393
+ papayawhip: '#ffefd5',
394
+ peachpuff: '#ffdab9',
395
+ peru: '#cd853f',
396
+ pink: '#ffc0cb',
397
+ plum: '#dda0dd',
398
+ powderblue: '#b0e0e6',
399
+ purple: '#800080',
400
+ rebeccapurple: '#663399',
401
+ red: '#ff0000',
402
+ rosybrown: '#bc8f8f',
403
+ royalblue: '#4169e1',
404
+ saddlebrown: '#8b4513',
405
+ salmon: '#fa8072',
406
+ sandybrown: '#f4a460',
407
+ seagreen: '#2e8b57',
408
+ seashell: '#fff5ee',
409
+ sienna: '#a0522d',
410
+ silver: '#c0c0c0',
411
+ skyblue: '#87ceeb',
412
+ slateblue: '#6a5acd',
413
+ slategray: '#708090',
414
+ slategrey: '#708090',
415
+ snow: '#fffafa',
416
+ springgreen: '#00ff7f',
417
+ steelblue: '#4682b4',
418
+ tan: '#d2b48c',
419
+ teal: '#008080',
420
+ thistle: '#d8bfd8',
421
+ tomato: '#ff6347',
422
+ turquoise: '#40e0d0',
423
+ violet: '#ee82ee',
424
+ wheat: '#f5deb3',
425
+ white: '#ffffff',
426
+ whitesmoke: '#f5f5f5',
427
+ yellow: '#ffff00',
428
+ yellowgreen: '#9acd32',
429
+ };
430
+ /**
431
+ * Note: [💞] Ignore a discrepancy between file name and entity name
432
+ */
433
+
434
+ /**
435
+ * Validates that a channel value is a valid number within the range of 0 to 255.
436
+ * Throws an error if the value is not valid.
437
+ *
438
+ * @param channelName - The name of the channel being validated.
439
+ * @param value - The value of the channel to validate.
440
+ * @throws Will throw an error if the value is not a valid channel number.
441
+ *
442
+ * @private util of `@promptbook/color`
443
+ */
444
+ function checkChannelValue(channelName, value) {
445
+ if (typeof value !== 'number') {
446
+ throw new Error(`${channelName} channel value is not number but ${typeof value}`);
447
+ }
448
+ if (isNaN(value)) {
449
+ throw new Error(`${channelName} channel value is NaN`);
450
+ }
451
+ if (Math.round(value) !== value) {
452
+ throw new Error(`${channelName} channel is not whole number, it is ${value}`);
453
+ }
454
+ if (value < 0) {
455
+ throw new Error(`${channelName} channel is lower than 0, it is ${value}`);
456
+ }
457
+ if (value > 255) {
458
+ throw new Error(`${channelName} channel is greater than 255, it is ${value}`);
459
+ }
460
+ }
461
+ /**
462
+ * TODO: [🧠][🚓] Is/which combination it better to use asserts/check, validate or is utility function?
463
+ */
464
+
465
+ /**
466
+ * Color object represents an RGB color with alpha channel
467
+ *
468
+ * Note: There is no fromObject/toObject because the most logical way to serialize color is as a hex string (#009edd)
469
+ *
470
+ * @public exported from `@promptbook/color`
471
+ */
472
+ class Color {
473
+ /**
474
+ * Creates a new Color instance from miscellaneous formats
475
+ * - It can receive Color instance and just return the same instance
476
+ * - It can receive color in string format for example `#009edd`, `rgb(0,158,221)`, `rgb(0%,62%,86.7%)`, `hsl(197.1,100%,43.3%)`
477
+ *
478
+ * Note: This is not including fromImage because detecting color from an image is heavy task which requires async stuff and we cannot safely determine with overloading if return value will be a promise
479
+ *
480
+ * @param color
481
+ * @returns Color object
482
+ */
483
+ static from(color) {
484
+ if (color instanceof Color) {
485
+ return take(color);
486
+ }
487
+ else if (Color.isColor(color)) {
488
+ return take(color);
489
+ }
490
+ else if (typeof color === 'string') {
491
+ return Color.fromString(color);
492
+ }
493
+ else {
494
+ console.error({ color });
495
+ throw new Error(`Can not create color from given object`);
496
+ }
497
+ }
498
+ /**
499
+ * Creates a new Color instance from miscellaneous string formats
500
+ *
501
+ * @param color as a string for example `#009edd`, `rgb(0,158,221)`, `rgb(0%,62%,86.7%)`, `hsl(197.1,100%,43.3%)`, `red`, `darkgrey`,...
502
+ * @returns Color object
503
+ */
504
+ static fromString(color) {
505
+ if (CSS_COLORS[color]) {
506
+ return Color.fromString(CSS_COLORS[color]);
507
+ // -----
508
+ }
509
+ else if (Color.isHexColorString(color)) {
510
+ return Color.fromHex(color);
511
+ // -----
512
+ }
513
+ else if (/^hsl\(\s*(\d+)\s*,\s*(\d+(?:\.\d+)?%)\s*,\s*(\d+(?:\.\d+)?%)\)$/.test(color)) {
514
+ return Color.fromHsl(color);
515
+ // -----
516
+ }
517
+ else if (/^rgb\((\s*[0-9-.%]+\s*,?){3}\)$/.test(color)) {
518
+ // TODO: [0] Should be fromRgbString and fromRgbaString one or two functions
519
+ return Color.fromRgbString(color);
520
+ // -----
521
+ }
522
+ else if (/^rgba\((\s*[0-9-.%]+\s*,?){4}\)$/.test(color)) {
523
+ return Color.fromRgbaString(color);
524
+ // -----
525
+ }
526
+ else {
527
+ throw new Error(`Can not create a new Color instance from string "${color}".`);
528
+ }
529
+ }
530
+ /**
531
+ * Gets common color
532
+ *
533
+ * @param key as a css string like `midnightblue`
534
+ * @returns Color object
535
+ */
536
+ static get(key) {
537
+ if (!CSS_COLORS[key]) {
538
+ throw new Error(`"${key}" is not a common css color.`);
539
+ }
540
+ return Color.fromString(CSS_COLORS[key]);
541
+ }
542
+ /**
543
+ * Creates a new Color instance from average color of given image
544
+ *
545
+ * @param image as a source for example `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAA1JREFUGFdjYJh39z8ABJgCe/ZvAS4AAAAASUVORK5CYII=`
546
+ * @returns Color object
547
+ */
548
+ static async fromImage(image) {
549
+ return Color.fromHex(`#009edd`);
550
+ }
551
+ /**
552
+ * Creates a new Color instance from color in hex format
553
+ *
554
+ * @param color in hex for example `#009edd`, `009edd`, `#555`,...
555
+ * @returns Color object
556
+ */
557
+ static fromHex(hex) {
558
+ const hexOriginal = hex;
559
+ if (hex.startsWith('#')) {
560
+ hex = hex.substring(1);
561
+ }
562
+ if (hex.length === 3) {
563
+ return Color.fromHex3(hex);
564
+ }
565
+ if (hex.length === 6) {
566
+ return Color.fromHex6(hex);
567
+ }
568
+ if (hex.length === 8) {
569
+ return Color.fromHex8(hex);
570
+ }
571
+ throw new Error(`Can not parse color from hex string "${hexOriginal}"`);
572
+ }
573
+ /**
574
+ * Creates a new Color instance from color in hex format with 3 color digits (without alpha channel)
575
+ *
576
+ * @param color in hex for example `09d`
577
+ * @returns Color object
578
+ */
579
+ static fromHex3(hex) {
580
+ const r = parseInt(hex.substr(0, 1), 16) * 16;
581
+ const g = parseInt(hex.substr(1, 1), 16) * 16;
582
+ const b = parseInt(hex.substr(2, 1), 16) * 16;
583
+ return take(new Color(r, g, b));
584
+ }
585
+ /**
586
+ * Creates a new Color instance from color in hex format with 6 color digits (without alpha channel)
587
+ *
588
+ * @param color in hex for example `009edd`
589
+ * @returns Color object
590
+ */
591
+ static fromHex6(hex) {
592
+ const r = parseInt(hex.substr(0, 2), 16);
593
+ const g = parseInt(hex.substr(2, 2), 16);
594
+ const b = parseInt(hex.substr(4, 2), 16);
595
+ return take(new Color(r, g, b));
596
+ }
597
+ /**
598
+ * Creates a new Color instance from color in hex format with 8 color digits (with alpha channel)
599
+ *
600
+ * @param color in hex for example `009edd`
601
+ * @returns Color object
602
+ */
603
+ static fromHex8(hex) {
604
+ const r = parseInt(hex.substr(0, 2), 16);
605
+ const g = parseInt(hex.substr(2, 2), 16);
606
+ const b = parseInt(hex.substr(4, 2), 16);
607
+ const a = parseInt(hex.substr(6, 2), 16);
608
+ return take(new Color(r, g, b, a));
609
+ }
610
+ /**
611
+ * Creates a new Color instance from color in hsl format
612
+ *
613
+ * @param color as a hsl for example `hsl(197.1,100%,43.3%)`
614
+ * @returns Color object
615
+ */
616
+ static fromHsl(hsl) {
617
+ const match = hsl.match(/^hsl\(\s*([0-9.]+)\s*,\s*([0-9.]+)%\s*,\s*([0-9.]+)%\s*\)$/);
618
+ if (!match) {
619
+ throw new Error(`Invalid hsl string format: "${hsl}"`);
620
+ }
621
+ const h = parseFloat(match[1]);
622
+ const s = parseFloat(match[2]) / 100;
623
+ const l = parseFloat(match[3]) / 100;
624
+ // HSL to RGB conversion
625
+ const c = (1 - Math.abs(2 * l - 1)) * s;
626
+ const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
627
+ const m = l - c / 2;
628
+ let r1 = 0, g1 = 0, b1 = 0;
629
+ if (h >= 0 && h < 60) {
630
+ r1 = c;
631
+ g1 = x;
632
+ b1 = 0;
633
+ }
634
+ else if (h >= 60 && h < 120) {
635
+ r1 = x;
636
+ g1 = c;
637
+ b1 = 0;
638
+ }
639
+ else if (h >= 120 && h < 180) {
640
+ r1 = 0;
641
+ g1 = c;
642
+ b1 = x;
643
+ }
644
+ else if (h >= 180 && h < 240) {
645
+ r1 = 0;
646
+ g1 = x;
647
+ b1 = c;
648
+ }
649
+ else if (h >= 240 && h < 300) {
650
+ r1 = x;
651
+ g1 = 0;
652
+ b1 = c;
653
+ }
654
+ else if (h >= 300 && h < 360) {
655
+ r1 = c;
656
+ g1 = 0;
657
+ b1 = x;
658
+ }
659
+ const r = Math.round((r1 + m) * 255);
660
+ const g = Math.round((g1 + m) * 255);
661
+ const b = Math.round((b1 + m) * 255);
662
+ return take(new Color(r, g, b));
663
+ }
664
+ /**
665
+ * Creates a new Color instance from color in rgb format
666
+ *
667
+ * @param color as a rgb for example `rgb(0,158,221)`, `rgb(0%,62%,86.7%)`
668
+ * @returns Color object
669
+ */
670
+ static fromRgbString(rgb) {
671
+ const match = rgb.match(/^rgb\(\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*\)$/);
672
+ if (!match) {
673
+ throw new Error(`Invalid rgb string format: "${rgb}"`);
674
+ }
675
+ const parseChannel = (value) => {
676
+ if (value.endsWith('%')) {
677
+ // Percentage value
678
+ const percent = parseFloat(value);
679
+ return Math.round((percent / 100) * 255);
680
+ }
681
+ else {
682
+ // Numeric value
683
+ return Math.round(parseFloat(value));
684
+ }
685
+ };
686
+ const r = parseChannel(match[1]);
687
+ const g = parseChannel(match[2]);
688
+ const b = parseChannel(match[3]);
689
+ return take(new Color(r, g, b));
690
+ }
691
+ /**
692
+ * Creates a new Color instance from color in rbga format
693
+ *
694
+ * @param color as a rgba for example `rgba(0,158,221,0.5)`, `rgb(0%,62%,86.7%,50%)`
695
+ * @returns Color object
696
+ */
697
+ static fromRgbaString(rgba) {
698
+ const match = rgba.match(/^rgba\(\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*\)$/);
699
+ if (!match) {
700
+ throw new Error(`Invalid rgba string format: "${rgba}"`);
701
+ }
702
+ const parseChannel = (value) => {
703
+ if (value.endsWith('%')) {
704
+ const percent = parseFloat(value);
705
+ return Math.round((percent / 100) * 255);
706
+ }
707
+ else {
708
+ return Math.round(parseFloat(value));
709
+ }
710
+ };
711
+ const parseAlpha = (value) => {
712
+ if (value.endsWith('%')) {
713
+ const percent = parseFloat(value);
714
+ return Math.round((percent / 100) * 255);
715
+ }
716
+ else {
717
+ const alphaFloat = parseFloat(value);
718
+ // If alpha is between 0 and 1, treat as float
719
+ if (alphaFloat <= 1) {
720
+ return Math.round(alphaFloat * 255);
721
+ }
722
+ // Otherwise, treat as 0-255
723
+ return Math.round(alphaFloat);
724
+ }
725
+ };
726
+ const r = parseChannel(match[1]);
727
+ const g = parseChannel(match[2]);
728
+ const b = parseChannel(match[3]);
729
+ const a = parseAlpha(match[4]);
730
+ return take(new Color(r, g, b, a));
731
+ }
732
+ /**
733
+ * Creates a new Color for color channels values
734
+ *
735
+ * @param red number from 0 to 255
736
+ * @param green number from 0 to 255
737
+ * @param blue number from 0 to 255
738
+ * @param alpha number from 0 (transparent) to 255 (opaque = default)
739
+ * @returns Color object
740
+ */
741
+ static fromValues(red, green, blue, alpha = 255) {
742
+ return take(new Color(red, green, blue, alpha));
743
+ }
744
+ /**
745
+ * Checks if the given value is a valid Color object.
746
+ *
747
+ * @param {unknown} value - The value to check.
748
+ * @return {value is WithTake<Color>} Returns true if the value is a valid Color object, false otherwise.
749
+ */
750
+ static isColor(value) {
751
+ if (typeof value !== 'object') {
752
+ return false;
753
+ }
754
+ if (value === null) {
755
+ return false;
756
+ }
757
+ if (typeof value.red !== 'number' ||
758
+ typeof value.green !== 'number' ||
759
+ typeof value.blue !== 'number' ||
760
+ typeof value.alpha !== 'number') {
761
+ return false;
762
+ }
763
+ if (typeof value.then !== 'function') {
764
+ return false;
765
+ }
766
+ return true;
767
+ }
768
+ /**
769
+ * Checks if the given value is a valid hex color string
770
+ *
771
+ * @param value - value to check
772
+ * @returns true if the value is a valid hex color string (e.g., `#009edd`, `#fff`, etc.)
773
+ */
774
+ static isHexColorString(value) {
775
+ return typeof value === 'string' && /^#(?:[0-9a-fA-F]{3}){1,2}$/.test(value);
776
+ }
777
+ /**
778
+ * Creates new Color object
779
+ *
780
+ * Note: Consider using one of static methods like `from` or `fromString`
781
+ *
782
+ * @param red number from 0 to 255
783
+ * @param green number from 0 to 255
784
+ * @param blue number from 0 to 255
785
+ * @param alpha number from 0 (transparent) to 255 (opaque)
786
+ */
787
+ constructor(red, green, blue, alpha = 255) {
788
+ this.red = red;
789
+ this.green = green;
790
+ this.blue = blue;
791
+ this.alpha = alpha;
792
+ checkChannelValue('Red', red);
793
+ checkChannelValue('Green', green);
794
+ checkChannelValue('Blue', blue);
795
+ checkChannelValue('Alpha', alpha);
796
+ }
797
+ /**
798
+ * Shortcut for `red` property
799
+ * Number from 0 to 255
800
+ * @alias red
801
+ */
802
+ get r() {
803
+ return this.red;
804
+ }
805
+ /**
806
+ * Shortcut for `green` property
807
+ * Number from 0 to 255
808
+ * @alias green
809
+ */
810
+ get g() {
811
+ return this.green;
812
+ }
813
+ /**
814
+ * Shortcut for `blue` property
815
+ * Number from 0 to 255
816
+ * @alias blue
817
+ */
818
+ get b() {
819
+ return this.blue;
820
+ }
821
+ /**
822
+ * Shortcut for `alpha` property
823
+ * Number from 0 (transparent) to 255 (opaque)
824
+ * @alias alpha
825
+ */
826
+ get a() {
827
+ return this.alpha;
828
+ }
829
+ /**
830
+ * Shortcut for `alpha` property
831
+ * Number from 0 (transparent) to 255 (opaque)
832
+ * @alias alpha
833
+ */
834
+ get opacity() {
835
+ return this.alpha;
836
+ }
837
+ /**
838
+ * Shortcut for 1-`alpha` property
839
+ */
840
+ get transparency() {
841
+ return 255 - this.alpha;
842
+ }
843
+ clone() {
844
+ return take(new Color(this.red, this.green, this.blue, this.alpha));
845
+ }
846
+ toString() {
847
+ return this.toHex();
848
+ }
849
+ toHex() {
850
+ if (this.alpha === 255) {
851
+ return `#${this.red.toString(16).padStart(2, '0')}${this.green.toString(16).padStart(2, '0')}${this.blue
852
+ .toString(16)
853
+ .padStart(2, '0')}`;
854
+ }
855
+ else {
856
+ return `#${this.red.toString(16).padStart(2, '0')}${this.green.toString(16).padStart(2, '0')}${this.blue
857
+ .toString(16)
858
+ .padStart(2, '0')}${this.alpha.toString(16).padStart(2, '0')}`;
859
+ }
860
+ }
861
+ toRgb() {
862
+ if (this.alpha === 255) {
863
+ return `rgb(${this.red}, ${this.green}, ${this.blue})`;
864
+ }
865
+ else {
866
+ return `rgba(${this.red}, ${this.green}, ${this.blue}, ${Math.round((this.alpha / 255) * 100)}%)`;
867
+ }
868
+ }
869
+ toHsl() {
870
+ throw new Error(`Getting HSL is not implemented`);
871
+ }
872
+ }
873
+ /**
874
+ * TODO: [🥻] Split Color class and color type
875
+ * TODO: For each method a corresponding static method should be created
876
+ * Like clone can be done by color.clone() OR Color.clone(color)
877
+ * TODO: Probably as an independent LIB OR add to LIB xyzt (ask @roseckyj)
878
+ * TODO: !! Transfer back to Collboard (whole directory)
879
+ * TODO: Maybe [🏌️‍♂️] change ACRY toString => (toHex) toRgb when there will be toRgb and toRgba united
880
+ * TODO: Convert getters to methods - getters only for values
881
+ * TODO: Write tests
882
+ * TODO: Getters for alpha, opacity, transparency, r, b, g, h, s, l, a,...
883
+ * TODO: [0] Should be fromRgbString and fromRgbaString one or two functions + one or two regex
884
+ * TODO: Use rgb, rgba, hsl for testing and parsing with the same regex
885
+ * TODO: Regex for rgb, rgba, hsl does not support all options like deg, rad, turn,...
886
+ * TODO: Convolution matrix
887
+ * TODO: Maybe connect with textures
888
+ */
889
+
890
+ /**
891
+ * Converts HSL values to RGB values
892
+ *
893
+ * @param hue [0-1]
894
+ * @param saturation [0-1]
895
+ * @param lightness [0-1]
896
+ * @returns [red, green, blue] [0-255]
897
+ *
898
+ * @private util of `@promptbook/color`
899
+ */
900
+ function hslToRgb(hue, saturation, lightness) {
901
+ let red;
902
+ let green;
903
+ let blue;
904
+ if (saturation === 0) {
905
+ // achromatic
906
+ red = lightness;
907
+ green = lightness;
908
+ blue = lightness;
909
+ }
910
+ else {
911
+ // TODO: Extract to separate function
912
+ const hue2rgb = (p, q, t) => {
913
+ if (t < 0)
914
+ t += 1;
915
+ if (t > 1)
916
+ t -= 1;
917
+ if (t < 1 / 6)
918
+ return p + (q - p) * 6 * t;
919
+ if (t < 1 / 2)
920
+ return q;
921
+ if (t < 2 / 3)
922
+ return p + (q - p) * (2 / 3 - t) * 6;
923
+ return p;
924
+ };
925
+ const q = lightness < 0.5 ? lightness * (1 + saturation) : lightness + saturation - lightness * saturation;
926
+ const p = 2 * lightness - q;
927
+ red = hue2rgb(p, q, hue + 1 / 3);
928
+ green = hue2rgb(p, q, hue);
929
+ blue = hue2rgb(p, q, hue - 1 / 3);
930
+ }
931
+ return [Math.round(red * 255), Math.round(green * 255), Math.round(blue * 255)];
932
+ }
933
+ /**
934
+ * TODO: Properly name all used internal variables
935
+ */
936
+
937
+ /**
938
+ * Converts RGB values to HSL values
939
+ *
940
+ * @param red [0-255]
941
+ * @param green [0-255]
942
+ * @param blue [0-255]
943
+ * @returns [hue, saturation, lightness] [0-1]
944
+ *
945
+ * @private util of `@promptbook/color`
946
+ */
947
+ function rgbToHsl(red, green, blue) {
948
+ red /= 255;
949
+ green /= 255;
950
+ blue /= 255;
951
+ const max = Math.max(red, green, blue);
952
+ const min = Math.min(red, green, blue);
953
+ let hue;
954
+ let saturation;
955
+ const lightness = (max + min) / 2;
956
+ if (max === min) {
957
+ // achromatic
958
+ hue = 0;
959
+ saturation = 0;
960
+ }
961
+ else {
962
+ const d = max - min;
963
+ saturation = lightness > 0.5 ? d / (2 - max - min) : d / (max + min);
964
+ switch (max) {
965
+ case red:
966
+ hue = (green - blue) / d + (green < blue ? 6 : 0);
967
+ break;
968
+ case green:
969
+ hue = (blue - red) / d + 2;
970
+ break;
971
+ case blue:
972
+ hue = (red - green) / d + 4;
973
+ break;
974
+ default:
975
+ hue = 0;
976
+ }
977
+ hue /= 6;
978
+ }
979
+ return [hue, saturation, lightness];
980
+ }
981
+ /**
982
+ * TODO: Properly name all used internal variables
983
+ */
984
+
985
+ /**
986
+ * Makes color transformer which lighten the given color
987
+ *
988
+ * @param amount from 0 to 1
989
+ *
990
+ * @public exported from `@promptbook/color`
991
+ */
992
+ function lighten(amount) {
993
+ return ({ red, green, blue, alpha }) => {
994
+ const [h, s, lInitial] = rgbToHsl(red, green, blue);
995
+ let l = lInitial + amount;
996
+ l = Math.max(0, Math.min(l, 1)); // Replace lodash clamp with Math.max and Math.min
997
+ const [r, g, b] = hslToRgb(h, s, l);
998
+ return Color.fromValues(r, g, b, alpha);
999
+ };
1000
+ }
1001
+ /**
1002
+ * TODO: Maybe implement by mix+hsl
1003
+ */
1004
+
1005
+ /**
1006
+ * Calculates distance between two colors
1007
+ *
1008
+ * @param color1 first color
1009
+ * @param color2 second color
1010
+ *
1011
+ * Note: This function is inefficient. Use colorDistanceSquared instead if possible.
1012
+ *
1013
+ * @public exported from `@promptbook/color`
1014
+ */
1015
+ /**
1016
+ * Calculates distance between two colors without square root
1017
+ *
1018
+ * @param color1 first color
1019
+ * @param color2 second color
1020
+ *
1021
+ * @public exported from `@promptbook/color`
1022
+ */
1023
+ function colorDistanceSquared(color1, color2) {
1024
+ const rmean = (color1.red + color2.red) / 2;
1025
+ const r = color1.red - color2.red;
1026
+ const g = color1.green - color2.green;
1027
+ const b = color1.blue - color2.blue;
1028
+ const weightR = 2 + rmean / 256;
1029
+ const weightG = 4.0;
1030
+ const weightB = 2 + (255 - rmean) / 256;
1031
+ const distance = weightR * r * r + weightG * g * g + weightB * b * b;
1032
+ return distance;
1033
+ }
1034
+
1035
+ /**
1036
+ * Makes color transformer which finds the nearest color from the given list
1037
+ *
1038
+ * @param colors array of colors to choose from
1039
+ *
1040
+ * @public exported from `@promptbook/color`
1041
+ */
1042
+ function nearest(...colors) {
1043
+ return (color) => {
1044
+ const distances = colors.map((c) => colorDistanceSquared(c, color));
1045
+ const minDistance = Math.min(...distances);
1046
+ const minIndex = distances.indexOf(minDistance);
1047
+ const nearestColor = colors[minIndex];
1048
+ return nearestColor;
1049
+ };
1050
+ }
1051
+
1052
+ /**
1053
+ * Color transformer which returns the negative color
1054
+ *
1055
+ * @public exported from `@promptbook/color`
1056
+ */
1057
+ function negative(color) {
1058
+ const r = 255 - color.red;
1059
+ const g = 255 - color.green;
1060
+ const b = 255 - color.blue;
1061
+ return Color.fromValues(r, g, b, color.alpha);
1062
+ }
1063
+
1064
+ /**
1065
+ * Makes color transformer which finds the furthest color from the given list
1066
+ *
1067
+ * @param colors array of colors to choose from
1068
+ *
1069
+ * @public exported from `@promptbook/color`
1070
+ */
1071
+ function furthest(...colors) {
1072
+ return (color) => {
1073
+ const furthestColor = negative(nearest(...colors.map(negative))(color));
1074
+ return furthestColor;
1075
+ };
1076
+ }
1077
+ /**
1078
+ * Makes color transformer which finds the best text color (black or white) for the given background color
1079
+ *
1080
+ * @public exported from `@promptbook/color`
1081
+ */
1082
+ furthest(Color.get('white'), Color.from('black'));
1083
+
1084
+ /**
1085
+ * Makes color transformer which returns a grayscale version of the color
1086
+ *
1087
+ * @param amount from 0 to 1
1088
+ *
1089
+ * @public exported from `@promptbook/color`
1090
+ */
1091
+ function grayscale(amount) {
1092
+ return ({ red, green, blue, alpha }) => {
1093
+ const average = (red + green + blue) / 3;
1094
+ red = Math.round(average * amount + red * (1 - amount));
1095
+ green = Math.round(average * amount + green * (1 - amount));
1096
+ blue = Math.round(average * amount + blue * (1 - amount));
1097
+ return Color.fromValues(red, green, blue, alpha);
1098
+ };
1099
+ }
1100
+
1101
+ /**
1102
+ * Makes color transformer which saturate the given color
1103
+ *
1104
+ * @param amount from -1 to 1
1105
+ *
1106
+ * @public exported from `@promptbook/color`
1107
+ */
1108
+ function saturate(amount) {
1109
+ return ({ red, green, blue, alpha }) => {
1110
+ const [h, sInitial, l] = rgbToHsl(red, green, blue);
1111
+ let s = sInitial + amount;
1112
+ s = Math.max(0, Math.min(s, 1));
1113
+ const [r, g, b] = hslToRgb(h, s, l);
1114
+ return Color.fromValues(r, g, b, alpha);
1115
+ };
1116
+ }
1117
+ /**
1118
+ * TODO: Maybe implement by mix+hsl
1119
+ */
1120
+
232
1121
  /**
233
1122
  * Name for the Promptbook
234
1123
  *
@@ -249,6 +1138,32 @@ const ADMIN_EMAIL = 'pavol@ptbk.io';
249
1138
  * @public exported from `@promptbook/core`
250
1139
  */
251
1140
  const ADMIN_GITHUB_NAME = 'hejny';
1141
+ // <- TODO: [🐊] Pick the best claim
1142
+ /**
1143
+ * Color of the Promptbook
1144
+ *
1145
+ * TODO: [🗽] Unite branding and make single place for it
1146
+ *
1147
+ * @public exported from `@promptbook/core`
1148
+ */
1149
+ const PROMPTBOOK_COLOR = Color.fromHex('#79EAFD');
1150
+ // <- TODO: [🧠] Using `Color` here increases the package size approx 3kb, maybe remove it
1151
+ /**
1152
+ * Dark color of the Promptbook
1153
+ *
1154
+ * TODO: [🗽] Unite branding and make single place for it
1155
+ *
1156
+ * @public exported from `@promptbook/core`
1157
+ */
1158
+ PROMPTBOOK_COLOR.then(lighten(0.1)).then(saturate(0.9)).then(grayscale(0.9));
1159
+ /**
1160
+ * Color of the user (in chat)
1161
+ *
1162
+ * TODO: [🗽] Unite branding and make single place for it
1163
+ *
1164
+ * @public exported from `@promptbook/core`
1165
+ */
1166
+ Color.fromHex('#1D4ED8');
252
1167
  // <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
253
1168
  /**
254
1169
  * The maximum number of iterations for a loops