@xapp/chat-widget 1.96.2 → 1.96.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -68,6 +68,522 @@ function isActionBarPaymentButton(button) {
68
68
  return button.type === "payment";
69
69
  }
70
70
 
71
+ var buildChatWidgetChannel$1 = {};
72
+
73
+ var color = {};
74
+
75
+ /*! Copyright (c) 2026, XAPP AI */
76
+ Object.defineProperty(color, "__esModule", { value: true });
77
+ color.parseRgb = parseRgb;
78
+ color.invertColorHEX = invertColorHEX;
79
+ color.invertColorRgb = invertColorRgb;
80
+ color.invertColor = invertColor;
81
+ color.isSupportedColor = isSupportedColor;
82
+ color.usableColorOr = usableColorOr;
83
+ color.readableTextOn = readableTextOn;
84
+ color.darkenColor = darkenColor;
85
+ /**
86
+ * Colour maths for building a widget theme from a business's own branding.
87
+ *
88
+ * These lived in stentor-client, which meant the only way to work out a
89
+ * readable text colour was to be running in a browser. Studio needs the same
90
+ * answers when it builds an app server-side for a get.xapp.ai signup, and two
91
+ * implementations of "which colour reads on this background" would drift into
92
+ * two different-looking widgets.
93
+ *
94
+ * The inputs are colours scraped off a customer's website, so they are whatever
95
+ * their designer chose — any hex length, rgb or rgba, or nothing at all.
96
+ */
97
+ /** Left-pads a hex component so "f" becomes "0f". */
98
+ function padZero(value, length = 2) {
99
+ const zeros = new Array(length).join("0");
100
+ return (zeros + value).slice(-length);
101
+ }
102
+ function parseRgb(colorString) {
103
+ const rgba = /^rgba?\((\d+),\s*(\d+),\s*(\d+),\s*([\d.]+)\)$/;
104
+ const rgb = /^rgb?\((\d+),\s*(\d+),\s*(\d+)\)$/;
105
+ const withAlpha = colorString === null || colorString === void 0 ? void 0 : colorString.match(rgba);
106
+ if (withAlpha) {
107
+ return {
108
+ r: parseInt(withAlpha[1], 10),
109
+ g: parseInt(withAlpha[2], 10),
110
+ b: parseInt(withAlpha[3], 10),
111
+ a: parseFloat(withAlpha[4])
112
+ };
113
+ }
114
+ const plain = colorString === null || colorString === void 0 ? void 0 : colorString.match(rgb);
115
+ if (plain) {
116
+ return { r: parseInt(plain[1], 10), g: parseInt(plain[2], 10), b: parseInt(plain[3], 10) };
117
+ }
118
+ return null;
119
+ }
120
+ /**
121
+ * Whether text should be black or white to read on this background.
122
+ *
123
+ * The weighting is perceived brightness, not an average: the eye is far more
124
+ * sensitive to green than to blue, so a pure blue is dark to us even though its
125
+ * numeric average is mid-range.
126
+ *
127
+ * @see http://stackoverflow.com/a/3943023/112731
128
+ */
129
+ function isLight(r, g, b) {
130
+ return r * 0.299 + g * 0.587 + b * 0.114 > 186;
131
+ }
132
+ /**
133
+ * Inverts a hex colour, or returns black/white for contrast when `bw` is set.
134
+ *
135
+ * Inverting is rarely what you want behind text — a mid-tone inverts to another
136
+ * mid-tone and neither reads. `bw` is the contrast answer.
137
+ */
138
+ function invertColorHEX(hex, bw) {
139
+ if (!hex) {
140
+ return undefined;
141
+ }
142
+ let value = hex.indexOf("#") === 0 ? hex.slice(1) : hex;
143
+ if (value.length === 3) {
144
+ value = value[0] + value[0] + value[1] + value[1] + value[2] + value[2];
145
+ }
146
+ if (value.length !== 6) {
147
+ throw new Error("Invalid HEX color.");
148
+ }
149
+ const r = parseInt(value.slice(0, 2), 16);
150
+ const g = parseInt(value.slice(2, 4), 16);
151
+ const b = parseInt(value.slice(4, 6), 16);
152
+ if (bw) {
153
+ return isLight(r, g, b) ? "#000000" : "#FFFFFF";
154
+ }
155
+ return `#${padZero((255 - r).toString(16))}${padZero((255 - g).toString(16))}${padZero((255 - b).toString(16))}`;
156
+ }
157
+ /** As invertColorHEX, for rgb/rgba input. */
158
+ function invertColorRgb(rgb, bw) {
159
+ const parsed = parseRgb(rgb);
160
+ if (!parsed) {
161
+ return rgb;
162
+ }
163
+ const { r, g, b } = parsed;
164
+ if (bw) {
165
+ return isLight(r, g, b) ? "rgba(0, 0, 0, 1)" : "rgba(255, 255, 255, 1)";
166
+ }
167
+ return `rgba(${255 - r}, ${255 - g}, ${255 - b}, 1)`;
168
+ }
169
+ /** Inverts whichever colour notation was given. */
170
+ function invertColor(color, bw) {
171
+ if (!color) {
172
+ return undefined;
173
+ }
174
+ return color.startsWith("#") ? invertColorHEX(color, bw) : invertColorRgb(color, bw);
175
+ }
176
+ /**
177
+ * Whether we can work out what text reads on this colour.
178
+ *
179
+ * Hex and rgb/rgba only. A CSS named colour like `orange`, or `hsl(...)`, is
180
+ * perfectly valid CSS — we simply cannot compute its brightness, and a
181
+ * background whose contrast we cannot compute is one we must not use.
182
+ */
183
+ function isSupportedColor(color) {
184
+ if (!color) {
185
+ return false;
186
+ }
187
+ if (color.startsWith("#")) {
188
+ const value = color.slice(1);
189
+ return (value.length === 3 || value.length === 6) && /^[0-9a-f]+$/i.test(value);
190
+ }
191
+ return parseRgb(color) !== null;
192
+ }
193
+ /**
194
+ * The colour to theme with: the one given, or the fallback when we cannot work
195
+ * out what text reads on it.
196
+ *
197
+ * Every builder needs this and each was doing it by hand, which is how the
198
+ * chat widget ended up guarded and the other two not. A background whose
199
+ * contrast cannot be computed is one we must not use — the foreground would be
200
+ * a guess paired with an unknown, and on a dark unparseable colour that is
201
+ * invisible text.
202
+ */
203
+ function usableColorOr(color, fallback) {
204
+ return isSupportedColor(color) ? color : fallback;
205
+ }
206
+ /**
207
+ * The text colour that will read on this background: black or white, never
208
+ * anything else.
209
+ *
210
+ * That guarantee is the whole point. An earlier version leaned on invertColor,
211
+ * whose rgb path returns its input unchanged when it cannot parse it — so
212
+ * `readableTextOn("orange")` answered `"orange"`, and a header themed from a
213
+ * scraped named colour got text in exactly the background colour. Invisible,
214
+ * and by the one function meant to prevent it.
215
+ *
216
+ * Unparseable input falls back to black. That is a guess, so callers who
217
+ * control the background should check `isSupportedColor` first and use a
218
+ * colour whose contrast can actually be computed, rather than pairing a
219
+ * guessed foreground with an unknown background.
220
+ */
221
+ function readableTextOn(background) {
222
+ if (!isSupportedColor(background)) {
223
+ return "#000000";
224
+ }
225
+ const rgb = background.startsWith("#") ? hexToRgb(background) : parseRgb(background);
226
+ if (!rgb) {
227
+ return "#000000";
228
+ }
229
+ return isLight(rgb.r, rgb.g, rgb.b) ? "#000000" : "#FFFFFF";
230
+ }
231
+ function hexToRgb(hex) {
232
+ let value = hex.slice(1);
233
+ if (value.length === 3) {
234
+ value = value[0] + value[0] + value[1] + value[1] + value[2] + value[2];
235
+ }
236
+ if (value.length !== 6) {
237
+ return null;
238
+ }
239
+ return {
240
+ r: parseInt(value.slice(0, 2), 16),
241
+ g: parseInt(value.slice(2, 4), 16),
242
+ b: parseInt(value.slice(4, 6), 16)
243
+ };
244
+ }
245
+ /**
246
+ * A shade of the input colour, darkened by `amount` percent (negative
247
+ * lightens). Ported alongside the rest so the form widget's theme does not
248
+ * need its own copy of it.
249
+ */
250
+ function darkenColor(colorIn, amount) {
251
+ if (!colorIn) {
252
+ return colorIn;
253
+ }
254
+ let r, g, b, a;
255
+ let colorOut;
256
+ try {
257
+ // Parse the color format
258
+ if (colorIn.startsWith('#')) {
259
+ // Hex format
260
+ const hexMatch = colorIn.match(/\w\w/g);
261
+ if (hexMatch) {
262
+ [r, g, b] = hexMatch.map(x => parseInt(x, 16));
263
+ }
264
+ }
265
+ else if (colorIn.startsWith('rgb')) {
266
+ // RGB or RGBA format
267
+ const rgba = parseRgb(colorIn);
268
+ if (rgba) {
269
+ ({ r, g, b, a } = rgba);
270
+ }
271
+ }
272
+ if (r === undefined || g === undefined || b === undefined) {
273
+ console.error(`Unsupported color format: ${colorIn}`);
274
+ return colorIn;
275
+ }
276
+ if (a !== undefined) {
277
+ // Adjust luminance for RGBA
278
+ // Luminance is on the 0-255 scale the channels are, so the clamp
279
+ // has to be too. Bounding it to 0-1 collapsed it to 1 for every
280
+ // real colour, which made the ratio tiny and crushed the result to
281
+ // near-black instead of darkening it: rgba(20, 52, 204, 0.8) at 20%
282
+ // came back rgba(0, 1, 3, 1).
283
+ const luminance = 0.3 * r + 0.59 * g + 0.11 * b;
284
+ let newLuminance = luminance * (1 - amount / 100);
285
+ newLuminance = Math.max(0, Math.min(255, newLuminance));
286
+ const ratio = newLuminance / (luminance || 1); // no division by zero
287
+ r = Math.round(r * ratio);
288
+ g = Math.round(g * ratio);
289
+ b = Math.round(b * ratio);
290
+ // Adjust alpha
291
+ a = a + amount / 100;
292
+ a = Math.max(0, Math.min(1, a)); // within 0 and 1
293
+ colorOut = `rgba(${r}, ${g}, ${b}, ${a})`;
294
+ }
295
+ else {
296
+ // Adjust the color for RGB or Hex
297
+ if (amount > 0) {
298
+ r = Math.max(0, r - amount);
299
+ g = Math.max(0, g - amount);
300
+ b = Math.max(0, b - amount);
301
+ }
302
+ else {
303
+ amount = Math.abs(amount); // positive for lightening
304
+ r = Math.min(255, r + amount);
305
+ g = Math.min(255, g + amount);
306
+ b = Math.min(255, b + amount);
307
+ }
308
+ colorOut = `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`;
309
+ }
310
+ }
311
+ catch (e) {
312
+ console.error(`Unsupported color format: ${colorIn} - Error: ${e}`);
313
+ }
314
+ return colorOut;
315
+ }
316
+
317
+ Object.defineProperty(buildChatWidgetChannel$1, "__esModule", { value: true });
318
+ buildChatWidgetChannel$1.buildChatWidgetChannel = buildChatWidgetChannel;
319
+ buildChatWidgetChannel$1.buildChatTheme = buildChatTheme;
320
+ const color_1 = color;
321
+ /** Fallback when a crawl found no branding. Deliberately neutral. */
322
+ const DEFAULT_PRIMARY = "#1AA7DC";
323
+ function buildChatWidgetChannel(props) {
324
+ const { appId, apiUrl, mcpUrl, formWidgetKey, phoneNumber } = props;
325
+ // Only a colour whose contrast can actually be computed, so the background
326
+ // and the text on it are always a matched pair rather than one scraped
327
+ // value and one guess.
328
+ const primaryColor = (0, color_1.usableColorOr)(props.primaryColor, DEFAULT_PRIMARY);
329
+ return {
330
+ type: "chat-widget",
331
+ // MCP by default. The widget runs on a public page, so homeowner mode
332
+ // — which is unauthenticated and carries the appId in the url — is the
333
+ // only one that can work there; business_owner needs a token, and a
334
+ // token on a public page is not a token.
335
+ connection: {
336
+ type: "mcp",
337
+ serverUrl: `${mcpUrl}/${appId}`,
338
+ mcp: {
339
+ mode: "homeowner"
340
+ }
341
+ },
342
+ theme: buildChatTheme(primaryColor, props.secondaryColor),
343
+ actionBar: buildActionBar({ apiUrl, formWidgetKey, phoneNumber })
344
+ };
345
+ }
346
+ /**
347
+ * The dock of buttons that replaces the plain chat bubble.
348
+ *
349
+ * On by default, which is the part worth explaining: chat is not on the default
350
+ * tier, but the action bar is how someone reaches booking and the phone, and
351
+ * those are what the business is paying for.
352
+ *
353
+ * Order is deliberate. Booking is the thing we are selling, so it comes first;
354
+ * calling is the fallback for people who will not use a form; chat is last
355
+ * because it is the least likely to be enabled.
356
+ */
357
+ function buildActionBar(props) {
358
+ const { apiUrl, formWidgetKey, phoneNumber } = props;
359
+ const buttons = [];
360
+ if (formWidgetKey) {
361
+ buttons.push({
362
+ id: "book",
363
+ type: "schedule",
364
+ label: "Book",
365
+ formConfig: {
366
+ api: {
367
+ url: apiUrl,
368
+ key: formWidgetKey
369
+ }
370
+ }
371
+ });
372
+ }
373
+ if (phoneNumber) {
374
+ buttons.push({
375
+ id: "call",
376
+ type: "phone",
377
+ label: "Call",
378
+ phoneNumber
379
+ });
380
+ }
381
+ buttons.push({
382
+ id: "chat",
383
+ type: "chat",
384
+ label: "Chat"
385
+ });
386
+ return {
387
+ enabled: true,
388
+ position: "right",
389
+ buttons
390
+ };
391
+ }
392
+ /**
393
+ * The theme a new chat widget starts with, from the business's own colour.
394
+ *
395
+ * A faithful port of stentor-client's buildDefaultChatTheme, deliberately: the
396
+ * point of moving it is that onboarding can call this instead of its own copy,
397
+ * and that migration should not restyle every widget created afterwards. A
398
+ * thinner theme would have quietly dropped the CTA, message, input, footer and
399
+ * button styling that new widgets have today.
400
+ *
401
+ * The one change is how the contrast colour is derived — see below.
402
+ */
403
+ function buildChatTheme(primaryColor, secondaryColor) {
404
+ const fontFamily = "RobotoRegular";
405
+ // secondaryColor is the colour of text sitting ON primaryColor — header
406
+ // title, subtitle, footer branding. Left unset it is computed, which is
407
+ // what makes the theme readable against whatever colour was scraped off
408
+ // the business's own site.
409
+ if (!secondaryColor) {
410
+ secondaryColor = (0, color_1.readableTextOn)(primaryColor);
411
+ }
412
+ return {
413
+ zIndex: "10000",
414
+ border: {
415
+ color: primaryColor,
416
+ radius: "10px",
417
+ },
418
+ carousel: {
419
+ button: {
420
+ color: primaryColor,
421
+ },
422
+ subtitle: {
423
+ color: "#979797",
424
+ fontFamily,
425
+ fontSize: "12px",
426
+ fontStyle: "normal",
427
+ fontWeight: "normal",
428
+ },
429
+ title: {
430
+ color: "#000",
431
+ fontFamily,
432
+ fontSize: "14px",
433
+ fontStyle: "normal",
434
+ fontWeight: "bold",
435
+ },
436
+ },
437
+ chatButton: {
438
+ background: primaryColor,
439
+ margin: {
440
+ bottom: "10px",
441
+ left: "10px",
442
+ right: "10px",
443
+ top: "10px",
444
+ },
445
+ },
446
+ content: {
447
+ background: "#ffffff",
448
+ },
449
+ cta: {
450
+ background: "#e1e2e1",
451
+ text: {
452
+ // Use a dark color that contrasts well with the light grey background
453
+ // instead of primaryColor which may not have sufficient contrast
454
+ color: "#333333",
455
+ fontFamily,
456
+ fontSize: "14px",
457
+ },
458
+ },
459
+ footer: {
460
+ background: primaryColor,
461
+ border: {
462
+ color: primaryColor,
463
+ radius: "0px",
464
+ width: "0px",
465
+ },
466
+ branding: {
467
+ text: {
468
+ color: "#ffffff",
469
+ fontFamily,
470
+ fontSize: "14px",
471
+ fontWeight: "normal",
472
+ fontStyle: "normal",
473
+ },
474
+ },
475
+ },
476
+ header: {
477
+ background: primaryColor,
478
+ avatarBackground: "transparent",
479
+ border: {
480
+ color: primaryColor,
481
+ radius: "0px",
482
+ width: "0px",
483
+ },
484
+ text: {
485
+ color: secondaryColor,
486
+ fontFamily,
487
+ fontSize: "16px",
488
+ fontWeight: "normal",
489
+ fontStyle: "normal",
490
+ },
491
+ subtitle: {
492
+ color: secondaryColor,
493
+ fontFamily,
494
+ fontSize: "10px",
495
+ fontWeight: "normal",
496
+ fontStyle: "normal",
497
+ },
498
+ },
499
+ input: {
500
+ background: "#ffffff",
501
+ border: {
502
+ radius: "20px",
503
+ },
504
+ text: {
505
+ color: "#000000",
506
+ fontFamily,
507
+ fontSize: "14px",
508
+ },
509
+ placeholder: {
510
+ color: "#e1e2e1",
511
+ fontFamily,
512
+ fontSize: "14px",
513
+ fontWeight: "normal",
514
+ fontStyle: "normal",
515
+ },
516
+ },
517
+ textTypingStatus: {
518
+ text: {
519
+ color: "#bfbfbf",
520
+ fontFamily,
521
+ fontSize: "14px",
522
+ fontWeight: "normal",
523
+ fontStyle: "normal",
524
+ },
525
+ },
526
+ menu: {
527
+ item: {
528
+ height: "24px",
529
+ text: {
530
+ fontSize: "14px",
531
+ },
532
+ },
533
+ },
534
+ menuButton: {
535
+ color: "#ffffff",
536
+ },
537
+ messages: {
538
+ maxWidth: "400px",
539
+ mine: {
540
+ bubbleColor: "#f2f2f2",
541
+ text: {
542
+ color: "#454545",
543
+ fontFamily,
544
+ fontSize: "18px",
545
+ fontStyle: "normal",
546
+ fontWeight: "normal",
547
+ },
548
+ },
549
+ others: {
550
+ bubbleColor: "#e1e2e1",
551
+ text: {
552
+ color: "#262726",
553
+ fontFamily,
554
+ fontSize: "18px",
555
+ fontStyle: "normal",
556
+ fontWeight: "normal",
557
+ },
558
+ },
559
+ padding: {
560
+ bottom: "0px",
561
+ left: "20px",
562
+ right: "16px",
563
+ top: "16px",
564
+ },
565
+ },
566
+ refreshButton: {
567
+ color: secondaryColor,
568
+ },
569
+ minimizeButton: {
570
+ color: secondaryColor,
571
+ },
572
+ cancelButton: {
573
+ color: secondaryColor,
574
+ },
575
+ primaryColor: primaryColor,
576
+ sendButton: {
577
+ color: "#505050",
578
+ width: "20px",
579
+ },
580
+ size: {
581
+ width: "500px",
582
+ height: "700px",
583
+ },
584
+ };
585
+ }
586
+
71
587
  var ChannelData = {};
72
588
 
73
589
  Object.defineProperty(ChannelData, "__esModule", { value: true });
@@ -133,7 +649,9 @@ function isOpenURLMenuItem(item) {
133
649
  Object.defineProperty(exports, "__esModule", { value: true });
134
650
  /*! Copyright (c) 2021, XAPPmedia */
135
651
  __exportStar(ActionBar$1, exports);
652
+ __exportStar(buildChatWidgetChannel$1, exports);
136
653
  __exportStar(ChannelData, exports);
654
+ __exportStar(color, exports);
137
655
  __exportStar(Constants, exports);
138
656
  __exportStar(Types, exports);
139
657
  __exportStar(UserInfo, exports);