@promptbook/azure-openai 0.102.0-11 → 0.102.0-16

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 (30) hide show
  1. package/esm/index.es.js +916 -1
  2. package/esm/index.es.js.map +1 -1
  3. package/esm/typings/src/_packages/color.index.d.ts +2 -0
  4. package/esm/typings/src/_packages/components.index.d.ts +4 -0
  5. package/esm/typings/src/_packages/core.index.d.ts +6 -0
  6. package/esm/typings/src/_packages/utils.index.d.ts +2 -0
  7. package/esm/typings/src/book-components/Chat/Chat/ChatMessageItem.d.ts +5 -1
  8. package/esm/typings/src/book-components/Chat/Chat/ChatProps.d.ts +4 -0
  9. package/esm/typings/src/book-components/Chat/save/_common/ChatSaveFormatDefinition.d.ts +43 -5
  10. package/esm/typings/src/book-components/Chat/save/html/htmlSaveFormatDefinition.d.ts +6 -2
  11. package/esm/typings/src/book-components/Chat/save/index.d.ts +38 -8
  12. package/esm/typings/src/book-components/Chat/save/json/jsonSaveFormatDefinition.d.ts +6 -2
  13. package/esm/typings/src/book-components/Chat/save/markdown/mdSaveFormatDefinition.d.ts +5 -1
  14. package/esm/typings/src/book-components/Chat/save/pdf/pdfSaveFormatDefinition.d.ts +6 -2
  15. package/esm/typings/src/book-components/Chat/save/react/reactSaveFormatDefinition.d.ts +16 -0
  16. package/esm/typings/src/book-components/Chat/save/text/txtSaveFormatDefinition.d.ts +5 -1
  17. package/esm/typings/src/book-components/Chat/types/ChatParticipant.d.ts +1 -1
  18. package/esm/typings/src/book-components/icons/SaveIcon.d.ts +10 -0
  19. package/esm/typings/src/config.d.ts +26 -4
  20. package/esm/typings/src/utils/color/Color.d.ts +8 -0
  21. package/esm/typings/src/utils/color/operators/darken.d.ts +2 -1
  22. package/esm/typings/src/utils/color/operators/grayscale.d.ts +2 -1
  23. package/esm/typings/src/utils/color/operators/lighten.d.ts +2 -1
  24. package/esm/typings/src/utils/color/operators/mixWithColor.d.ts +2 -1
  25. package/esm/typings/src/utils/color/operators/saturate.d.ts +13 -0
  26. package/esm/typings/src/utils/serialization/serializeToPromptbookJavascript.d.ts +22 -0
  27. package/esm/typings/src/version.d.ts +1 -1
  28. package/package.json +2 -2
  29. package/umd/index.umd.js +916 -1
  30. package/umd/index.umd.js.map +1 -1
package/esm/index.es.js CHANGED
@@ -18,12 +18,901 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
18
18
  * @generated
19
19
  * @see https://github.com/webgptorg/promptbook
20
20
  */
21
- const PROMPTBOOK_ENGINE_VERSION = '0.102.0-11';
21
+ const PROMPTBOOK_ENGINE_VERSION = '0.102.0-16';
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
+ * @private util of `@promptbook/color`
29
+ * @de
30
+ */
31
+ class TakeChain {
32
+ constructor(value) {
33
+ this.value = value;
34
+ }
35
+ then(callback) {
36
+ const newValue = callback(this.value);
37
+ return take(newValue);
38
+ }
39
+ }
40
+
41
+ /**
42
+ * A function that takes an initial value and returns a proxy object with chainable methods.
43
+ *
44
+ * @param {*} initialValue - The initial value.
45
+ * @returns {Proxy<WithTake<TValue>>} - A proxy object with a `take` method.
46
+ *
47
+ * @private util of `@promptbook/color`
48
+ * @deprecated [🤡] Use some better functional library instead of `TakeChain`
49
+ */
50
+ function take(initialValue) {
51
+ if (initialValue instanceof TakeChain) {
52
+ return initialValue;
53
+ }
54
+ return new Proxy(new TakeChain(initialValue), {
55
+ get(target, property, receiver) {
56
+ if (Reflect.has(target, property)) {
57
+ return Reflect.get(target, property, receiver);
58
+ }
59
+ else if (Reflect.has(initialValue, property)) {
60
+ return Reflect.get(initialValue, property, receiver);
61
+ }
62
+ else {
63
+ return undefined;
64
+ }
65
+ },
66
+ });
67
+ }
68
+
69
+ /**
70
+ * 🎨 List of all 140 color names which are supported by CSS
71
+ *
72
+ * @public exported from `@promptbook/color`
73
+ */
74
+ const CSS_COLORS = {
75
+ transparent: 'rgba(0,0,0,0)',
76
+ aliceblue: '#f0f8ff',
77
+ antiquewhite: '#faebd7',
78
+ aqua: '#00ffff',
79
+ aquamarine: '#7fffd4',
80
+ azure: '#f0ffff',
81
+ beige: '#f5f5dc',
82
+ bisque: '#ffe4c4',
83
+ black: '#000000',
84
+ blanchedalmond: '#ffebcd',
85
+ blue: '#0000ff',
86
+ blueviolet: '#8a2be2',
87
+ brown: '#a52a2a',
88
+ burlywood: '#deb887',
89
+ cadetblue: '#5f9ea0',
90
+ chartreuse: '#7fff00',
91
+ chocolate: '#d2691e',
92
+ coral: '#ff7f50',
93
+ cornflowerblue: '#6495ed',
94
+ cornsilk: '#fff8dc',
95
+ crimson: '#dc143c',
96
+ cyan: '#00ffff',
97
+ darkblue: '#00008b',
98
+ darkcyan: '#008b8b',
99
+ darkgoldenrod: '#b8860b',
100
+ darkgray: '#a9a9a9',
101
+ darkgrey: '#a9a9a9',
102
+ darkgreen: '#006400',
103
+ darkkhaki: '#bdb76b',
104
+ darkmagenta: '#8b008b',
105
+ darkolivegreen: '#556b2f',
106
+ darkorange: '#ff8c00',
107
+ darkorchid: '#9932cc',
108
+ darkred: '#8b0000',
109
+ darksalmon: '#e9967a',
110
+ darkseagreen: '#8fbc8f',
111
+ darkslateblue: '#483d8b',
112
+ darkslategray: '#2f4f4f',
113
+ darkslategrey: '#2f4f4f',
114
+ darkturquoise: '#00ced1',
115
+ darkviolet: '#9400d3',
116
+ deeppink: '#ff1493',
117
+ deepskyblue: '#00bfff',
118
+ dimgray: '#696969',
119
+ dimgrey: '#696969',
120
+ dodgerblue: '#1e90ff',
121
+ firebrick: '#b22222',
122
+ floralwhite: '#fffaf0',
123
+ forestgreen: '#228b22',
124
+ fuchsia: '#ff00ff',
125
+ gainsboro: '#dcdcdc',
126
+ ghostwhite: '#f8f8ff',
127
+ gold: '#ffd700',
128
+ goldenrod: '#daa520',
129
+ gray: '#808080',
130
+ grey: '#808080',
131
+ green: '#008000',
132
+ greenyellow: '#adff2f',
133
+ honeydew: '#f0fff0',
134
+ hotpink: '#ff69b4',
135
+ indianred: '#cd5c5c',
136
+ indigo: '#4b0082',
137
+ ivory: '#fffff0',
138
+ khaki: '#f0e68c',
139
+ lavender: '#e6e6fa',
140
+ lavenderblush: '#fff0f5',
141
+ lawngreen: '#7cfc00',
142
+ lemonchiffon: '#fffacd',
143
+ lightblue: '#add8e6',
144
+ lightcoral: '#f08080',
145
+ lightcyan: '#e0ffff',
146
+ lightgoldenrodyellow: '#fafad2',
147
+ lightgray: '#d3d3d3',
148
+ lightgrey: '#d3d3d3',
149
+ lightgreen: '#90ee90',
150
+ lightpink: '#ffb6c1',
151
+ lightsalmon: '#ffa07a',
152
+ lightseagreen: '#20b2aa',
153
+ lightskyblue: '#87cefa',
154
+ lightslategray: '#778899',
155
+ lightslategrey: '#778899',
156
+ lightsteelblue: '#b0c4de',
157
+ lightyellow: '#ffffe0',
158
+ lime: '#00ff00',
159
+ limegreen: '#32cd32',
160
+ linen: '#faf0e6',
161
+ magenta: '#ff00ff',
162
+ maroon: '#800000',
163
+ mediumaquamarine: '#66cdaa',
164
+ mediumblue: '#0000cd',
165
+ mediumorchid: '#ba55d3',
166
+ mediumpurple: '#9370db',
167
+ mediumseagreen: '#3cb371',
168
+ mediumslateblue: '#7b68ee',
169
+ mediumspringgreen: '#00fa9a',
170
+ mediumturquoise: '#48d1cc',
171
+ mediumvioletred: '#c71585',
172
+ midnightblue: '#191970',
173
+ mintcream: '#f5fffa',
174
+ mistyrose: '#ffe4e1',
175
+ moccasin: '#ffe4b5',
176
+ navajowhite: '#ffdead',
177
+ navy: '#000080',
178
+ oldlace: '#fdf5e6',
179
+ olive: '#808000',
180
+ olivedrab: '#6b8e23',
181
+ orange: '#ffa500',
182
+ orangered: '#ff4500',
183
+ orchid: '#da70d6',
184
+ palegoldenrod: '#eee8aa',
185
+ palegreen: '#98fb98',
186
+ paleturquoise: '#afeeee',
187
+ palevioletred: '#db7093',
188
+ papayawhip: '#ffefd5',
189
+ peachpuff: '#ffdab9',
190
+ peru: '#cd853f',
191
+ pink: '#ffc0cb',
192
+ plum: '#dda0dd',
193
+ powderblue: '#b0e0e6',
194
+ purple: '#800080',
195
+ rebeccapurple: '#663399',
196
+ red: '#ff0000',
197
+ rosybrown: '#bc8f8f',
198
+ royalblue: '#4169e1',
199
+ saddlebrown: '#8b4513',
200
+ salmon: '#fa8072',
201
+ sandybrown: '#f4a460',
202
+ seagreen: '#2e8b57',
203
+ seashell: '#fff5ee',
204
+ sienna: '#a0522d',
205
+ silver: '#c0c0c0',
206
+ skyblue: '#87ceeb',
207
+ slateblue: '#6a5acd',
208
+ slategray: '#708090',
209
+ slategrey: '#708090',
210
+ snow: '#fffafa',
211
+ springgreen: '#00ff7f',
212
+ steelblue: '#4682b4',
213
+ tan: '#d2b48c',
214
+ teal: '#008080',
215
+ thistle: '#d8bfd8',
216
+ tomato: '#ff6347',
217
+ turquoise: '#40e0d0',
218
+ violet: '#ee82ee',
219
+ wheat: '#f5deb3',
220
+ white: '#ffffff',
221
+ whitesmoke: '#f5f5f5',
222
+ yellow: '#ffff00',
223
+ yellowgreen: '#9acd32',
224
+ };
225
+ /**
226
+ * Note: [💞] Ignore a discrepancy between file name and entity name
227
+ */
228
+
229
+ /**
230
+ * Validates that a channel value is a valid number within the range of 0 to 255.
231
+ * Throws an error if the value is not valid.
232
+ *
233
+ * @param channelName - The name of the channel being validated.
234
+ * @param value - The value of the channel to validate.
235
+ * @throws Will throw an error if the value is not a valid channel number.
236
+ *
237
+ * @private util of `@promptbook/color`
238
+ */
239
+ function checkChannelValue(channelName, value) {
240
+ if (typeof value !== 'number') {
241
+ throw new Error(`${channelName} channel value is not number but ${typeof value}`);
242
+ }
243
+ if (isNaN(value)) {
244
+ throw new Error(`${channelName} channel value is NaN`);
245
+ }
246
+ if (Math.round(value) !== value) {
247
+ throw new Error(`${channelName} channel is not whole number, it is ${value}`);
248
+ }
249
+ if (value < 0) {
250
+ throw new Error(`${channelName} channel is lower than 0, it is ${value}`);
251
+ }
252
+ if (value > 255) {
253
+ throw new Error(`${channelName} channel is greater than 255, it is ${value}`);
254
+ }
255
+ }
256
+ /**
257
+ * TODO: [🧠][🚓] Is/which combination it better to use asserts/check, validate or is utility function?
258
+ */
259
+
260
+ /**
261
+ * Color object represents an RGB color with alpha channel
262
+ *
263
+ * Note: There is no fromObject/toObject because the most logical way to serialize color is as a hex string (#009edd)
264
+ *
265
+ * @public exported from `@promptbook/color`
266
+ */
267
+ class Color {
268
+ /**
269
+ * Creates a new Color instance from miscellaneous formats
270
+ * - It can receive Color instance and just return the same instance
271
+ * - 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%)`
272
+ *
273
+ * 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
274
+ *
275
+ * @param color
276
+ * @returns Color object
277
+ */
278
+ static from(color) {
279
+ if (color instanceof Color) {
280
+ return take(color);
281
+ }
282
+ else if (Color.isColor(color)) {
283
+ return take(color);
284
+ }
285
+ else if (typeof color === 'string') {
286
+ return Color.fromString(color);
287
+ }
288
+ else {
289
+ console.error({ color });
290
+ throw new Error(`Can not create color from given object`);
291
+ }
292
+ }
293
+ /**
294
+ * Creates a new Color instance from miscellaneous string formats
295
+ *
296
+ * @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`,...
297
+ * @returns Color object
298
+ */
299
+ static fromString(color) {
300
+ if (CSS_COLORS[color]) {
301
+ return Color.fromString(CSS_COLORS[color]);
302
+ // -----
303
+ }
304
+ else if (Color.isHexColorString(color)) {
305
+ return Color.fromHex(color);
306
+ // -----
307
+ }
308
+ else if (/^hsl\(\s*(\d+)\s*,\s*(\d+(?:\.\d+)?%)\s*,\s*(\d+(?:\.\d+)?%)\)$/.test(color)) {
309
+ return Color.fromHsl(color);
310
+ // -----
311
+ }
312
+ else if (/^rgb\((\s*[0-9-.%]+\s*,?){3}\)$/.test(color)) {
313
+ // TODO: [0] Should be fromRgbString and fromRgbaString one or two functions
314
+ return Color.fromRgbString(color);
315
+ // -----
316
+ }
317
+ else if (/^rgba\((\s*[0-9-.%]+\s*,?){4}\)$/.test(color)) {
318
+ return Color.fromRgbaString(color);
319
+ // -----
320
+ }
321
+ else {
322
+ throw new Error(`Can not create a new Color instance from string "${color}".`);
323
+ }
324
+ }
325
+ /**
326
+ * Gets common color
327
+ *
328
+ * @param key as a css string like `midnightblue`
329
+ * @returns Color object
330
+ */
331
+ static get(key) {
332
+ if (!CSS_COLORS[key]) {
333
+ throw new Error(`"${key}" is not a common css color.`);
334
+ }
335
+ return Color.fromString(CSS_COLORS[key]);
336
+ }
337
+ /**
338
+ * Creates a new Color instance from average color of given image
339
+ *
340
+ * @param image as a source for example `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAA1JREFUGFdjYJh39z8ABJgCe/ZvAS4AAAAASUVORK5CYII=`
341
+ * @returns Color object
342
+ */
343
+ static async fromImage(image) {
344
+ return Color.fromHex(`#009edd`);
345
+ }
346
+ /**
347
+ * Creates a new Color instance from color in hex format
348
+ *
349
+ * @param color in hex for example `#009edd`, `009edd`, `#555`,...
350
+ * @returns Color object
351
+ */
352
+ static fromHex(hex) {
353
+ const hexOriginal = hex;
354
+ if (hex.startsWith('#')) {
355
+ hex = hex.substring(1);
356
+ }
357
+ if (hex.length === 3) {
358
+ return Color.fromHex3(hex);
359
+ }
360
+ if (hex.length === 6) {
361
+ return Color.fromHex6(hex);
362
+ }
363
+ if (hex.length === 8) {
364
+ return Color.fromHex8(hex);
365
+ }
366
+ throw new Error(`Can not parse color from hex string "${hexOriginal}"`);
367
+ }
368
+ /**
369
+ * Creates a new Color instance from color in hex format with 3 color digits (without alpha channel)
370
+ *
371
+ * @param color in hex for example `09d`
372
+ * @returns Color object
373
+ */
374
+ static fromHex3(hex) {
375
+ const r = parseInt(hex.substr(0, 1), 16) * 16;
376
+ const g = parseInt(hex.substr(1, 1), 16) * 16;
377
+ const b = parseInt(hex.substr(2, 1), 16) * 16;
378
+ return take(new Color(r, g, b));
379
+ }
380
+ /**
381
+ * Creates a new Color instance from color in hex format with 6 color digits (without alpha channel)
382
+ *
383
+ * @param color in hex for example `009edd`
384
+ * @returns Color object
385
+ */
386
+ static fromHex6(hex) {
387
+ const r = parseInt(hex.substr(0, 2), 16);
388
+ const g = parseInt(hex.substr(2, 2), 16);
389
+ const b = parseInt(hex.substr(4, 2), 16);
390
+ return take(new Color(r, g, b));
391
+ }
392
+ /**
393
+ * Creates a new Color instance from color in hex format with 8 color digits (with alpha channel)
394
+ *
395
+ * @param color in hex for example `009edd`
396
+ * @returns Color object
397
+ */
398
+ static fromHex8(hex) {
399
+ const r = parseInt(hex.substr(0, 2), 16);
400
+ const g = parseInt(hex.substr(2, 2), 16);
401
+ const b = parseInt(hex.substr(4, 2), 16);
402
+ const a = parseInt(hex.substr(6, 2), 16);
403
+ return take(new Color(r, g, b, a));
404
+ }
405
+ /**
406
+ * Creates a new Color instance from color in hsl format
407
+ *
408
+ * @param color as a hsl for example `hsl(197.1,100%,43.3%)`
409
+ * @returns Color object
410
+ */
411
+ static fromHsl(hsl) {
412
+ const match = hsl.match(/^hsl\(\s*([0-9.]+)\s*,\s*([0-9.]+)%\s*,\s*([0-9.]+)%\s*\)$/);
413
+ if (!match) {
414
+ throw new Error(`Invalid hsl string format: "${hsl}"`);
415
+ }
416
+ const h = parseFloat(match[1]);
417
+ const s = parseFloat(match[2]) / 100;
418
+ const l = parseFloat(match[3]) / 100;
419
+ // HSL to RGB conversion
420
+ const c = (1 - Math.abs(2 * l - 1)) * s;
421
+ const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
422
+ const m = l - c / 2;
423
+ let r1 = 0, g1 = 0, b1 = 0;
424
+ if (h >= 0 && h < 60) {
425
+ r1 = c;
426
+ g1 = x;
427
+ b1 = 0;
428
+ }
429
+ else if (h >= 60 && h < 120) {
430
+ r1 = x;
431
+ g1 = c;
432
+ b1 = 0;
433
+ }
434
+ else if (h >= 120 && h < 180) {
435
+ r1 = 0;
436
+ g1 = c;
437
+ b1 = x;
438
+ }
439
+ else if (h >= 180 && h < 240) {
440
+ r1 = 0;
441
+ g1 = x;
442
+ b1 = c;
443
+ }
444
+ else if (h >= 240 && h < 300) {
445
+ r1 = x;
446
+ g1 = 0;
447
+ b1 = c;
448
+ }
449
+ else if (h >= 300 && h < 360) {
450
+ r1 = c;
451
+ g1 = 0;
452
+ b1 = x;
453
+ }
454
+ const r = Math.round((r1 + m) * 255);
455
+ const g = Math.round((g1 + m) * 255);
456
+ const b = Math.round((b1 + m) * 255);
457
+ return take(new Color(r, g, b));
458
+ }
459
+ /**
460
+ * Creates a new Color instance from color in rgb format
461
+ *
462
+ * @param color as a rgb for example `rgb(0,158,221)`, `rgb(0%,62%,86.7%)`
463
+ * @returns Color object
464
+ */
465
+ static fromRgbString(rgb) {
466
+ const match = rgb.match(/^rgb\(\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*\)$/);
467
+ if (!match) {
468
+ throw new Error(`Invalid rgb string format: "${rgb}"`);
469
+ }
470
+ const parseChannel = (value) => {
471
+ if (value.endsWith('%')) {
472
+ // Percentage value
473
+ const percent = parseFloat(value);
474
+ return Math.round((percent / 100) * 255);
475
+ }
476
+ else {
477
+ // Numeric value
478
+ return Math.round(parseFloat(value));
479
+ }
480
+ };
481
+ const r = parseChannel(match[1]);
482
+ const g = parseChannel(match[2]);
483
+ const b = parseChannel(match[3]);
484
+ return take(new Color(r, g, b));
485
+ }
486
+ /**
487
+ * Creates a new Color instance from color in rbga format
488
+ *
489
+ * @param color as a rgba for example `rgba(0,158,221,0.5)`, `rgb(0%,62%,86.7%,50%)`
490
+ * @returns Color object
491
+ */
492
+ static fromRgbaString(rgba) {
493
+ const match = rgba.match(/^rgba\(\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*\)$/);
494
+ if (!match) {
495
+ throw new Error(`Invalid rgba string format: "${rgba}"`);
496
+ }
497
+ const parseChannel = (value) => {
498
+ if (value.endsWith('%')) {
499
+ const percent = parseFloat(value);
500
+ return Math.round((percent / 100) * 255);
501
+ }
502
+ else {
503
+ return Math.round(parseFloat(value));
504
+ }
505
+ };
506
+ const parseAlpha = (value) => {
507
+ if (value.endsWith('%')) {
508
+ const percent = parseFloat(value);
509
+ return Math.round((percent / 100) * 255);
510
+ }
511
+ else {
512
+ const alphaFloat = parseFloat(value);
513
+ // If alpha is between 0 and 1, treat as float
514
+ if (alphaFloat <= 1) {
515
+ return Math.round(alphaFloat * 255);
516
+ }
517
+ // Otherwise, treat as 0-255
518
+ return Math.round(alphaFloat);
519
+ }
520
+ };
521
+ const r = parseChannel(match[1]);
522
+ const g = parseChannel(match[2]);
523
+ const b = parseChannel(match[3]);
524
+ const a = parseAlpha(match[4]);
525
+ return take(new Color(r, g, b, a));
526
+ }
527
+ /**
528
+ * Creates a new Color for color channels values
529
+ *
530
+ * @param red number from 0 to 255
531
+ * @param green number from 0 to 255
532
+ * @param blue number from 0 to 255
533
+ * @param alpha number from 0 (transparent) to 255 (opaque = default)
534
+ * @returns Color object
535
+ */
536
+ static fromValues(red, green, blue, alpha = 255) {
537
+ return take(new Color(red, green, blue, alpha));
538
+ }
539
+ /**
540
+ * Checks if the given value is a valid Color object.
541
+ *
542
+ * @param {unknown} value - The value to check.
543
+ * @return {value is WithTake<Color>} Returns true if the value is a valid Color object, false otherwise.
544
+ */
545
+ static isColor(value) {
546
+ if (typeof value !== 'object') {
547
+ return false;
548
+ }
549
+ if (value === null) {
550
+ return false;
551
+ }
552
+ if (typeof value.red !== 'number' ||
553
+ typeof value.green !== 'number' ||
554
+ typeof value.blue !== 'number' ||
555
+ typeof value.alpha !== 'number') {
556
+ return false;
557
+ }
558
+ if (typeof value.then !== 'function') {
559
+ return false;
560
+ }
561
+ return true;
562
+ }
563
+ /**
564
+ * Checks if the given value is a valid hex color string
565
+ *
566
+ * @param value - value to check
567
+ * @returns true if the value is a valid hex color string (e.g., `#009edd`, `#fff`, etc.)
568
+ */
569
+ static isHexColorString(value) {
570
+ return typeof value === 'string' && /^#(?:[0-9a-fA-F]{3}){1,2}$/.test(value);
571
+ }
572
+ /**
573
+ * Creates new Color object
574
+ *
575
+ * Note: Consider using one of static methods like `from` or `fromString`
576
+ *
577
+ * @param red number from 0 to 255
578
+ * @param green number from 0 to 255
579
+ * @param blue number from 0 to 255
580
+ * @param alpha number from 0 (transparent) to 255 (opaque)
581
+ */
582
+ constructor(red, green, blue, alpha = 255) {
583
+ this.red = red;
584
+ this.green = green;
585
+ this.blue = blue;
586
+ this.alpha = alpha;
587
+ checkChannelValue('Red', red);
588
+ checkChannelValue('Green', green);
589
+ checkChannelValue('Blue', blue);
590
+ checkChannelValue('Alpha', alpha);
591
+ }
592
+ /**
593
+ * Shortcut for `red` property
594
+ * Number from 0 to 255
595
+ * @alias red
596
+ */
597
+ get r() {
598
+ return this.red;
599
+ }
600
+ /**
601
+ * Shortcut for `green` property
602
+ * Number from 0 to 255
603
+ * @alias green
604
+ */
605
+ get g() {
606
+ return this.green;
607
+ }
608
+ /**
609
+ * Shortcut for `blue` property
610
+ * Number from 0 to 255
611
+ * @alias blue
612
+ */
613
+ get b() {
614
+ return this.blue;
615
+ }
616
+ /**
617
+ * Shortcut for `alpha` property
618
+ * Number from 0 (transparent) to 255 (opaque)
619
+ * @alias alpha
620
+ */
621
+ get a() {
622
+ return this.alpha;
623
+ }
624
+ /**
625
+ * Shortcut for `alpha` property
626
+ * Number from 0 (transparent) to 255 (opaque)
627
+ * @alias alpha
628
+ */
629
+ get opacity() {
630
+ return this.alpha;
631
+ }
632
+ /**
633
+ * Shortcut for 1-`alpha` property
634
+ */
635
+ get transparency() {
636
+ return 255 - this.alpha;
637
+ }
638
+ clone() {
639
+ return take(new Color(this.red, this.green, this.blue, this.alpha));
640
+ }
641
+ toString() {
642
+ return this.toHex();
643
+ }
644
+ toHex() {
645
+ if (this.alpha === 255) {
646
+ return `#${this.red.toString(16).padStart(2, '0')}${this.green.toString(16).padStart(2, '0')}${this.blue
647
+ .toString(16)
648
+ .padStart(2, '0')}`;
649
+ }
650
+ else {
651
+ return `#${this.red.toString(16).padStart(2, '0')}${this.green.toString(16).padStart(2, '0')}${this.blue
652
+ .toString(16)
653
+ .padStart(2, '0')}${this.alpha.toString(16).padStart(2, '0')}`;
654
+ }
655
+ }
656
+ toRgb() {
657
+ if (this.alpha === 255) {
658
+ return `rgb(${this.red}, ${this.green}, ${this.blue})`;
659
+ }
660
+ else {
661
+ return `rgba(${this.red}, ${this.green}, ${this.blue}, ${Math.round((this.alpha / 255) * 100)}%)`;
662
+ }
663
+ }
664
+ toHsl() {
665
+ throw new Error(`Getting HSL is not implemented`);
666
+ }
667
+ }
668
+ /**
669
+ * TODO: [🥻] Split Color class and color type
670
+ * TODO: For each method a corresponding static method should be created
671
+ * Like clone can be done by color.clone() OR Color.clone(color)
672
+ * TODO: Probably as an independent LIB OR add to LIB xyzt (ask @roseckyj)
673
+ * TODO: !! Transfer back to Collboard (whole directory)
674
+ * TODO: Maybe [🏌️‍♂️] change ACRY toString => (toHex) toRgb when there will be toRgb and toRgba united
675
+ * TODO: Convert getters to methods - getters only for values
676
+ * TODO: Write tests
677
+ * TODO: Getters for alpha, opacity, transparency, r, b, g, h, s, l, a,...
678
+ * TODO: [0] Should be fromRgbString and fromRgbaString one or two functions + one or two regex
679
+ * TODO: Use rgb, rgba, hsl for testing and parsing with the same regex
680
+ * TODO: Regex for rgb, rgba, hsl does not support all options like deg, rad, turn,...
681
+ * TODO: Convolution matrix
682
+ * TODO: Maybe connect with textures
683
+ */
684
+
685
+ /**
686
+ * Converts HSL values to RGB values
687
+ *
688
+ * @param hue [0-1]
689
+ * @param saturation [0-1]
690
+ * @param lightness [0-1]
691
+ * @returns [red, green, blue] [0-255]
692
+ *
693
+ * @private util of `@promptbook/color`
694
+ */
695
+ function hslToRgb(hue, saturation, lightness) {
696
+ let red;
697
+ let green;
698
+ let blue;
699
+ if (saturation === 0) {
700
+ // achromatic
701
+ red = lightness;
702
+ green = lightness;
703
+ blue = lightness;
704
+ }
705
+ else {
706
+ // TODO: Extract to separate function
707
+ const hue2rgb = (p, q, t) => {
708
+ if (t < 0)
709
+ t += 1;
710
+ if (t > 1)
711
+ t -= 1;
712
+ if (t < 1 / 6)
713
+ return p + (q - p) * 6 * t;
714
+ if (t < 1 / 2)
715
+ return q;
716
+ if (t < 2 / 3)
717
+ return p + (q - p) * (2 / 3 - t) * 6;
718
+ return p;
719
+ };
720
+ const q = lightness < 0.5 ? lightness * (1 + saturation) : lightness + saturation - lightness * saturation;
721
+ const p = 2 * lightness - q;
722
+ red = hue2rgb(p, q, hue + 1 / 3);
723
+ green = hue2rgb(p, q, hue);
724
+ blue = hue2rgb(p, q, hue - 1 / 3);
725
+ }
726
+ return [Math.round(red * 255), Math.round(green * 255), Math.round(blue * 255)];
727
+ }
728
+ /**
729
+ * TODO: Properly name all used internal variables
730
+ */
731
+
732
+ /**
733
+ * Converts RGB values to HSL values
734
+ *
735
+ * @param red [0-255]
736
+ * @param green [0-255]
737
+ * @param blue [0-255]
738
+ * @returns [hue, saturation, lightness] [0-1]
739
+ *
740
+ * @private util of `@promptbook/color`
741
+ */
742
+ function rgbToHsl(red, green, blue) {
743
+ red /= 255;
744
+ green /= 255;
745
+ blue /= 255;
746
+ const max = Math.max(red, green, blue);
747
+ const min = Math.min(red, green, blue);
748
+ let hue;
749
+ let saturation;
750
+ const lightness = (max + min) / 2;
751
+ if (max === min) {
752
+ // achromatic
753
+ hue = 0;
754
+ saturation = 0;
755
+ }
756
+ else {
757
+ const d = max - min;
758
+ saturation = lightness > 0.5 ? d / (2 - max - min) : d / (max + min);
759
+ switch (max) {
760
+ case red:
761
+ hue = (green - blue) / d + (green < blue ? 6 : 0);
762
+ break;
763
+ case green:
764
+ hue = (blue - red) / d + 2;
765
+ break;
766
+ case blue:
767
+ hue = (red - green) / d + 4;
768
+ break;
769
+ default:
770
+ hue = 0;
771
+ }
772
+ hue /= 6;
773
+ }
774
+ return [hue, saturation, lightness];
775
+ }
776
+ /**
777
+ * TODO: Properly name all used internal variables
778
+ */
779
+
780
+ /**
781
+ * Makes color transformer which lighten the given color
782
+ *
783
+ * @param amount from 0 to 1
784
+ *
785
+ * @public exported from `@promptbook/color`
786
+ */
787
+ function lighten(amount) {
788
+ return ({ red, green, blue, alpha }) => {
789
+ const [h, s, lInitial] = rgbToHsl(red, green, blue);
790
+ let l = lInitial + amount;
791
+ l = Math.max(0, Math.min(l, 1)); // Replace lodash clamp with Math.max and Math.min
792
+ const [r, g, b] = hslToRgb(h, s, l);
793
+ return Color.fromValues(r, g, b, alpha);
794
+ };
795
+ }
796
+ /**
797
+ * TODO: Maybe implement by mix+hsl
798
+ */
799
+
800
+ /**
801
+ * Calculates distance between two colors
802
+ *
803
+ * @param color1 first color
804
+ * @param color2 second color
805
+ *
806
+ * Note: This function is inefficient. Use colorDistanceSquared instead if possible.
807
+ *
808
+ * @public exported from `@promptbook/color`
809
+ */
810
+ /**
811
+ * Calculates distance between two colors without square root
812
+ *
813
+ * @param color1 first color
814
+ * @param color2 second color
815
+ *
816
+ * @public exported from `@promptbook/color`
817
+ */
818
+ function colorDistanceSquared(color1, color2) {
819
+ const rmean = (color1.red + color2.red) / 2;
820
+ const r = color1.red - color2.red;
821
+ const g = color1.green - color2.green;
822
+ const b = color1.blue - color2.blue;
823
+ const weightR = 2 + rmean / 256;
824
+ const weightG = 4.0;
825
+ const weightB = 2 + (255 - rmean) / 256;
826
+ const distance = weightR * r * r + weightG * g * g + weightB * b * b;
827
+ return distance;
828
+ }
829
+
830
+ /**
831
+ * Makes color transformer which finds the nearest color from the given list
832
+ *
833
+ * @param colors array of colors to choose from
834
+ *
835
+ * @public exported from `@promptbook/color`
836
+ */
837
+ function nearest(...colors) {
838
+ return (color) => {
839
+ const distances = colors.map((c) => colorDistanceSquared(c, color));
840
+ const minDistance = Math.min(...distances);
841
+ const minIndex = distances.indexOf(minDistance);
842
+ const nearestColor = colors[minIndex];
843
+ return nearestColor;
844
+ };
845
+ }
846
+
847
+ /**
848
+ * Color transformer which returns the negative color
849
+ *
850
+ * @public exported from `@promptbook/color`
851
+ */
852
+ function negative(color) {
853
+ const r = 255 - color.red;
854
+ const g = 255 - color.green;
855
+ const b = 255 - color.blue;
856
+ return Color.fromValues(r, g, b, color.alpha);
857
+ }
858
+
859
+ /**
860
+ * Makes color transformer which finds the furthest color from the given list
861
+ *
862
+ * @param colors array of colors to choose from
863
+ *
864
+ * @public exported from `@promptbook/color`
865
+ */
866
+ function furthest(...colors) {
867
+ return (color) => {
868
+ const furthestColor = negative(nearest(...colors.map(negative))(color));
869
+ return furthestColor;
870
+ };
871
+ }
872
+ /**
873
+ * Makes color transformer which finds the best text color (black or white) for the given background color
874
+ *
875
+ * @public exported from `@promptbook/color`
876
+ */
877
+ furthest(Color.get('white'), Color.from('black'));
878
+
879
+ /**
880
+ * Makes color transformer which returns a grayscale version of the color
881
+ *
882
+ * @param amount from 0 to 1
883
+ *
884
+ * @public exported from `@promptbook/color`
885
+ */
886
+ function grayscale(amount) {
887
+ return ({ red, green, blue, alpha }) => {
888
+ const average = (red + green + blue) / 3;
889
+ red = Math.round(average * amount + red * (1 - amount));
890
+ green = Math.round(average * amount + green * (1 - amount));
891
+ blue = Math.round(average * amount + blue * (1 - amount));
892
+ return Color.fromValues(red, green, blue, alpha);
893
+ };
894
+ }
895
+
896
+ /**
897
+ * Makes color transformer which saturate the given color
898
+ *
899
+ * @param amount from -1 to 1
900
+ *
901
+ * @public exported from `@promptbook/color`
902
+ */
903
+ function saturate(amount) {
904
+ return ({ red, green, blue, alpha }) => {
905
+ const [h, sInitial, l] = rgbToHsl(red, green, blue);
906
+ let s = sInitial + amount;
907
+ s = Math.max(0, Math.min(s, 1));
908
+ const [r, g, b] = hslToRgb(h, s, l);
909
+ return Color.fromValues(r, g, b, alpha);
910
+ };
911
+ }
912
+ /**
913
+ * TODO: Maybe implement by mix+hsl
914
+ */
915
+
27
916
  /**
28
917
  * Name for the Promptbook
29
918
  *
@@ -44,6 +933,32 @@ const ADMIN_EMAIL = 'pavol@ptbk.io';
44
933
  * @public exported from `@promptbook/core`
45
934
  */
46
935
  const ADMIN_GITHUB_NAME = 'hejny';
936
+ // <- TODO: [🐊] Pick the best claim
937
+ /**
938
+ * Color of the Promptbook
939
+ *
940
+ * TODO: [🗽] Unite branding and make single place for it
941
+ *
942
+ * @public exported from `@promptbook/core`
943
+ */
944
+ const PROMPTBOOK_COLOR = Color.fromHex('#79EAFD');
945
+ // <- TODO: !!! How much is this adding to package size of @promptbook/core
946
+ /**
947
+ * Dark color of the Promptbook
948
+ *
949
+ * TODO: [🗽] Unite branding and make single place for it
950
+ *
951
+ * @public exported from `@promptbook/core`
952
+ */
953
+ PROMPTBOOK_COLOR.then(lighten(0.1)).then(saturate(0.9)).then(grayscale(0.9));
954
+ /**
955
+ * Color of the user (in chat)
956
+ *
957
+ * TODO: [🗽] Unite branding and make single place for it
958
+ *
959
+ * @public exported from `@promptbook/core`
960
+ */
961
+ Color.fromHex('#1D4ED8');
47
962
  // <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
48
963
  /**
49
964
  * The maximum number of iterations for a loops