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