brainerce 1.31.0 → 1.33.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.
- package/README.md +16 -2
- package/dist/bot/bootstrap.global.js +245 -51
- package/dist/bot/index.d.mts +57 -1
- package/dist/bot/index.d.ts +57 -1
- package/dist/bot/index.js +755 -146
- package/dist/bot/index.mjs +755 -146
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4865,12 +4865,26 @@ Add the store's AI shopping assistant with one line. All configuration (name, av
|
|
|
4865
4865
|
// or mount from the SDK (client-side only)
|
|
4866
4866
|
import { BrainerceBot } from 'brainerce/bot';
|
|
4867
4867
|
|
|
4868
|
-
const bot = await BrainerceBot.mount({
|
|
4868
|
+
const bot = await BrainerceBot.mount({
|
|
4869
|
+
connectionId: 'vc_abc123',
|
|
4870
|
+
// Optional: make cart adds (card buttons, the in-card variant picker, and
|
|
4871
|
+
// bot-initiated adds) go through YOUR cart so your header count stays in
|
|
4872
|
+
// sync. variantId is null for simple products. Return false to make the
|
|
4873
|
+
// widget fall back to navigating to the product page.
|
|
4874
|
+
onAddToCart: async ({ productId, variantId, quantity }) => {
|
|
4875
|
+
await client.smartAddToCart({ productId, variantId: variantId ?? undefined, quantity });
|
|
4876
|
+
return true;
|
|
4877
|
+
},
|
|
4878
|
+
});
|
|
4869
4879
|
// resolves to null when the bot is disabled — safe to call unconditionally
|
|
4870
4880
|
bot?.destroy(); // optional teardown
|
|
4871
4881
|
```
|
|
4872
4882
|
|
|
4873
|
-
The widget persists an anonymous session in `localStorage`, restores conversations on revisit, streams answers, shows product recommendation cards (
|
|
4883
|
+
The widget persists an anonymous session in `localStorage`, restores conversations on revisit, streams answers, and shows product recommendation cards (image, price, add-to-cart / view buttons). Multi-variant products get an **in-card variant picker** (attribute chips, live variant price/image) so shoppers can choose options and add without leaving the chat; shoppers can also just ask the assistant to add an item ("add the moka to my cart") and it happens through the same chain. A leave-a-message form lands in the merchant's Inquiries inbox, and zero-result searches feed the merchant's "unmet demand" analytics.
|
|
4884
|
+
|
|
4885
|
+
**Add to cart resolution** (never a dead button): the widget first calls your `onAddToCart` option; without one it dispatches a cancelable `brainerce:bot:add-to-cart` `CustomEvent` on `window` (`detail: { productId, variantId, quantity, connectionId }` — call `preventDefault()` after handling it); if nothing handles either, it navigates to the product page. Products too complex for in-chat picking (3+ attribute dimensions or 25+ variants) always navigate. Aside from your own cart handler, the widget is read-only by design — shoppers can never mutate the store through it.
|
|
4886
|
+
|
|
4887
|
+
Merchant-side display controls (Studio → Storefront Bot): chat size (compact / full screen / shopper's choice), auto-open, position, and whether shoppers may expand the window (`allowExpand`).
|
|
4874
4888
|
|
|
4875
4889
|
## Webhooks
|
|
4876
4890
|
|
|
@@ -1,57 +1,251 @@
|
|
|
1
|
-
"use strict";(()=>{var
|
|
1
|
+
"use strict";(()=>{var R="https://api.brainerce.com",O=new Set(["he","ar"]),S={en:{online:"Online",placeholder:"Ask anything\u2026",error:"Something went wrong \u2014 please try again.",leaveMessage:"Leave a message for the team",yourEmail:"Your email",yourMessage:"Your message",send:"Send",sent:"Thanks! The team will get back to you by email.",close:"Close",expand:"Expand",collapse:"Collapse",searching:"Searching the store\u2026",addToCart:"Add to cart",added:"Added",view:"View",chooseOptions:"View product",results:"From the store",poweredBy:"Powered by Brainerce",addFailed:"I couldn\u2019t add that to the cart \u2014 try the button on the product card."},he:{online:"\u05DE\u05D7\u05D5\u05D1\u05E8",placeholder:"\u05E9\u05D0\u05DC\u05D5 \u05D0\u05D5\u05EA\u05D9 \u05D4\u05DB\u05DC\u2026",error:"\u05DE\u05E9\u05D4\u05D5 \u05D4\u05E9\u05EA\u05D1\u05E9 \u2014 \u05E0\u05E1\u05D5 \u05E9\u05D5\u05D1.",leaveMessage:"\u05D4\u05E9\u05D0\u05D9\u05E8\u05D5 \u05D4\u05D5\u05D3\u05E2\u05D4 \u05DC\u05E6\u05D5\u05D5\u05EA",yourEmail:"\u05D4\u05D0\u05D9\u05DE\u05D9\u05D9\u05DC \u05E9\u05DC\u05DB\u05DD",yourMessage:"\u05D4\u05D4\u05D5\u05D3\u05E2\u05D4 \u05E9\u05DC\u05DB\u05DD",send:"\u05E9\u05DC\u05D9\u05D7\u05D4",sent:"\u05EA\u05D5\u05D3\u05D4! \u05D4\u05E6\u05D5\u05D5\u05EA \u05D9\u05D7\u05D6\u05D5\u05E8 \u05D0\u05DC\u05D9\u05DB\u05DD \u05D1\u05DE\u05D9\u05D9\u05DC.",close:"\u05E1\u05D2\u05D9\u05E8\u05D4",expand:"\u05D4\u05E8\u05D7\u05D1\u05D4",collapse:"\u05DB\u05D9\u05D5\u05D5\u05E5",searching:"\u05DE\u05D7\u05E4\u05E9 \u05D1\u05D7\u05E0\u05D5\u05EA\u2026",addToCart:"\u05D4\u05D5\u05E1\u05E4\u05D4 \u05DC\u05E1\u05DC",added:"\u05E0\u05D5\u05E1\u05E3",view:"\u05E6\u05E4\u05D9\u05D9\u05D4",chooseOptions:"\u05DC\u05E6\u05E4\u05D5\u05EA \u05D1\u05DE\u05D5\u05E6\u05E8",results:"\u05DE\u05EA\u05D5\u05DA \u05D4\u05D7\u05E0\u05D5\u05EA",poweredBy:"\u05DE\u05D5\u05E4\u05E2\u05DC \u05E2\u05DC \u05D9\u05D3\u05D9 Brainerce",addFailed:"\u05DC\u05D0 \u05D4\u05E6\u05DC\u05D7\u05EA\u05D9 \u05DC\u05D4\u05D5\u05E1\u05D9\u05E3 \u05DC\u05E1\u05DC \u2014 \u05E0\u05E1\u05D5 \u05D3\u05E8\u05DA \u05D4\u05DB\u05E4\u05EA\u05D5\u05E8 \u05D1\u05DB\u05E8\u05D8\u05D9\u05E1 \u05D4\u05DE\u05D5\u05E6\u05E8."}},H={chat:'<svg viewBox="0 0 24 24" fill="none"><path d="M12 3C7.03 3 3 6.58 3 11c0 2.04.86 3.9 2.28 5.32-.15 1.23-.62 2.39-1.1 3.21-.13.23.05.52.31.47 1.56-.27 3.07-.93 4.13-1.62A10.6 10.6 0 0 0 12 19c4.97 0 9-3.58 9-8s-4.03-8-9-8Z" fill="currentColor"/></svg>',close:'<svg viewBox="0 0 24 24" fill="none"><path d="M6 6l12 12M18 6L6 18" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',expand:'<svg viewBox="0 0 24 24" fill="none"><path d="M14 4h6v6M10 20H4v-6M20 4l-7 7M4 20l7-7" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg>',collapse:'<svg viewBox="0 0 24 24" fill="none"><path d="M20 10h-6V4M4 14h6v6M20 4l-6 6M4 20l6-6" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg>',mail:'<svg viewBox="0 0 24 24" fill="none"><rect x="3.5" y="5.5" width="17" height="13" rx="2.5" stroke="currentColor" stroke-width="1.7"/><path d="m4.5 7.5 7.5 5.5 7.5-5.5" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"/></svg>',send:'<svg viewBox="0 0 24 24" fill="none"><path d="M4.4 11.2 19 4.6c.7-.3 1.4.4 1.1 1.1l-6.6 14.6c-.3.7-1.3.6-1.5-.1l-1.7-5.4a1 1 0 0 0-.6-.6l-5.4-1.7c-.7-.2-.8-1.2-.1-1.5Z" fill="currentColor"/></svg>',cart:'<svg viewBox="0 0 24 24" fill="none"><path d="M3 4h2l2.4 11.2A2 2 0 0 0 9.36 17H17.5a2 2 0 0 0 1.95-1.55L21 8H6" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/><circle cx="10" cy="20.5" r="1.4" fill="currentColor"/><circle cx="17" cy="20.5" r="1.4" fill="currentColor"/></svg>',check:'<svg viewBox="0 0 24 24" fill="none"><path d="m5 12.5 4.5 4.5L19 7.5" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"/></svg>',arrow:'<svg viewBox="0 0 24 24" fill="none"><path d="M7 17 17 7M9 7h8v8" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg>',brand:'<svg viewBox="0 0 24 24"><defs><linearGradient id="bb-brand-g" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="#34d399"/><stop offset="1" stop-color="#8b5cf6"/></linearGradient></defs><rect x="1.5" y="1.5" width="21" height="21" rx="6" fill="url(#bb-brand-g)"/><text x="12" y="17" text-anchor="middle" font-family="ui-sans-serif,system-ui,sans-serif" font-size="14" font-weight="700" fill="#fff">B</text></svg>'},z=typeof DOMParser<"u"?new DOMParser:null;function m(b){let e=(H[b]??H.chat).replace("<svg ",'<svg xmlns="http://www.w3.org/2000/svg" '),t=z?.parseFromString(e,"image/svg+xml")?.documentElement;return!t||t.nodeName==="parsererror"?document.createTextNode(""):(t.setAttribute("aria-hidden","true"),t.setAttribute("class","bb-ic"),t)}function P(b){let e=new Uint8Array(16);crypto.getRandomValues(e);let n=btoa(String.fromCharCode(...e)).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"");return`${b}${n}`}function T(b){return/^\/(?!\/)/.test(b)||/^https?:\/\//i.test(b)}var $=/\*\*([^*\n]+)\*\*|\[([^\]\n]+)\]\(([^)\s]+)\)/g;function A(b,e){let n=0;$.lastIndex=0;for(let t=$.exec(e);t;t=$.exec(e)){if(t.index>n&&b.appendChild(document.createTextNode(e.slice(n,t.index))),t[1]!==void 0){let i=document.createElement("strong");i.textContent=t[1],b.appendChild(i)}else if(T(t[3])){let i=document.createElement("a");i.href=t[3],i.target="_blank",i.rel="noopener noreferrer",i.textContent=t[2],b.appendChild(i)}else b.appendChild(document.createTextNode(t[2]));n=t.index+t[0].length}n<e.length&&b.appendChild(document.createTextNode(e.slice(n)))}function U(b,e){b.replaceChildren();let n=e.split(`
|
|
2
|
+
`),t=null,i=null;for(let o of n){let r=/^\s*[-•*]\s+(.*)$/.exec(o);if(r){i=null,t||(t=document.createElement("ul"),b.appendChild(t));let d=document.createElement("li");A(d,r[1]),t.appendChild(d);continue}if(t=null,!o.trim()){i=null;continue}i?i.appendChild(document.createElement("br")):(i=document.createElement("p"),b.appendChild(i)),A(i,o)}}var M=class b{constructor(e){this.settings={enabled:!1};this.locale="en";this.sessionId=null;this.conversationId=null;this.busy=!1;this.opened=!1;this.expanded=!1;this.destroyed=!1;this.pendingText="";this.cardsRow=null;this.cardIds=new Set;this.prevBodyOverflow=null;this.connectionId=e.connectionId,this.baseUrl=(e.baseUrl||R).replace(/\/$/,""),this.storageKey=`brainerce-bot:${this.connectionId}`,this.onAddToCart=e.onAddToCart}static async mount(e){if(!e?.connectionId)return console.warn("[BrainerceBot] connectionId is required"),null;let n=new b(e);return await n.boot(e.target??document.body)?n:null}destroy(){this.destroyed=!0,this.prevBodyOverflow!==null&&(document.body.style.overflow=this.prevBodyOverflow,this.prevBodyOverflow=null),this.host?.remove()}async boot(e){try{let n=await fetch(`${this.baseUrl}/api/storefront-bot/${encodeURIComponent(this.connectionId)}/settings`);if(!n.ok)return!1;this.settings=await n.json()}catch{return!1}return this.settings.enabled?(this.locale=this.settings.languages?.[0]??"en",this.restoreIds(),this.render(e),this.settings.displayMode==="auto_open"&&setTimeout(()=>!this.destroyed&&this.open(),3e3),!0):!1}t(e){return(S[this.locale]??S.en)[e]??S.en[e]??e}restoreIds(){try{let e=localStorage.getItem(this.storageKey);if(e){let n=JSON.parse(e);this.sessionId=n.sessionId??null,this.conversationId=n.conversationId??null}}catch{}}persistIds(){try{localStorage.setItem(this.storageKey,JSON.stringify({sessionId:this.sessionId,conversationId:this.conversationId}))}catch{}}css(e,n,t){let i=this.settings.bubbleShape==="square",o=i?"14px":"22px",r=i?"8px":"15px",d="5px",a=this.settings.displayMode??"floating",l=a==="side_rail",s=a==="full_screen";return`
|
|
2
3
|
:host { all: initial; }
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0;
|
|
5
|
+
font-family: -apple-system, "SF Pro Text", "Segoe UI Variable Text", "Segoe UI", system-ui, "Helvetica Neue", sans-serif;
|
|
6
|
+
-webkit-font-smoothing: antialiased; }
|
|
7
|
+
button { font: inherit; cursor: pointer; background: none; border: none; color: inherit; }
|
|
8
|
+
.bb { position: fixed; bottom: 24px; ${t}: 24px; z-index: 2147483000; direction: ${n};
|
|
9
|
+
/* product-card density scales with the window size */
|
|
10
|
+
--bb-card-w: 148px; --bb-img-h: 84px; --bb-title-fs: 11.5px; --bb-price-fs: 12.5px; --bb-btn-h: 27px; }
|
|
11
|
+
/* .big = any large surface (expanded, full-screen mode, mobile takeover) */
|
|
12
|
+
.bb.big { --bb-card-w: 216px; --bb-img-h: 132px; --bb-title-fs: 13px; --bb-price-fs: 14px; --bb-btn-h: 33px; }
|
|
13
|
+
.bb.big .bb-msgs, .bb.big .bb-chips, .bb.big .bb-esc {
|
|
14
|
+
padding-inline: max(20px, calc((100% - 1340px) / 2)); }
|
|
15
|
+
.bb.big .bb-msgs { gap: 12px; padding-top: 22px; }
|
|
16
|
+
.bb.big .bb-msg { font-size: 14px; padding: 11px 15px; }
|
|
17
|
+
.bb.big .bb-header { padding: 15px 22px; }
|
|
18
|
+
/* Big-surface composer: a full-width message box with the send inside */
|
|
19
|
+
.bb.big .bb-composer { position: relative; padding: 16px 20px 20px; }
|
|
20
|
+
.bb.big .bb-input { min-height: 116px; border-radius: 18px; background: #fff;
|
|
21
|
+
border: 1px solid #e3e5ec; padding: 16px 20px 52px; font-size: 15px;
|
|
22
|
+
overflow-y: auto; box-shadow: 0 2px 8px -2px rgba(15,18,34,.06); }
|
|
23
|
+
.bb.big .bb-send { position: absolute; bottom: 34px; inset-inline-end: 36px;
|
|
24
|
+
width: 42px; height: 42px; }
|
|
25
|
+
.bb.big .bb-send .bb-ic { width: 19px; height: 19px; }
|
|
26
|
+
/* Backdrop for the large dialog: dim + blur the storefront behind it */
|
|
27
|
+
.bb-scrim { position: fixed; inset: 0; background: rgba(15,18,34,.35);
|
|
28
|
+
-webkit-backdrop-filter: blur(7px); backdrop-filter: blur(7px);
|
|
29
|
+
opacity: 0; pointer-events: none; transition: opacity .25s ease; }
|
|
30
|
+
.bb.open.big .bb-scrim { opacity: 1; pointer-events: auto; }
|
|
31
|
+
|
|
32
|
+
/* ---- launcher ------------------------------------------------------ */
|
|
5
33
|
.bb-launcher {
|
|
6
|
-
width:
|
|
7
|
-
|
|
8
|
-
border-radius: ${
|
|
9
|
-
box-shadow: 0
|
|
10
|
-
|
|
34
|
+
width: 58px; height: 58px; position: relative; display: flex; align-items: center;
|
|
35
|
+
justify-content: center; color: #fff; background: ${e};
|
|
36
|
+
border-radius: ${i?"16px":"999px"}; overflow: hidden;
|
|
37
|
+
box-shadow: 0 6px 16px -4px color-mix(in srgb, ${e} 55%, rgba(10,12,30,.4)), 0 2px 6px rgba(10,12,30,.18);
|
|
38
|
+
transition: transform .22s cubic-bezier(.34,1.56,.64,1), box-shadow .22s ease;
|
|
11
39
|
}
|
|
12
|
-
.bb-launcher:hover { transform: scale(1.
|
|
13
|
-
.bb-launcher
|
|
40
|
+
.bb-launcher:hover { transform: scale(1.07) translateY(-1px); }
|
|
41
|
+
.bb-launcher:active { transform: scale(.96); }
|
|
42
|
+
.bb-launcher img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover;
|
|
43
|
+
transition: opacity .18s ease, transform .25s ease; }
|
|
44
|
+
.bb-launcher .bb-ic { width: 26px; height: 26px; position: absolute;
|
|
45
|
+
transition: opacity .18s ease, transform .25s ease; }
|
|
46
|
+
.bb-launcher .bb-l-close { opacity: 0; transform: rotate(-90deg) scale(.6); }
|
|
47
|
+
.bb.open .bb-launcher .bb-l-chat, .bb.open .bb-launcher img { opacity: 0; transform: rotate(90deg) scale(.6); }
|
|
48
|
+
.bb.open .bb-launcher img { transform: scale(1.15); }
|
|
49
|
+
.bb.open .bb-launcher .bb-l-close { opacity: 1; transform: rotate(0) scale(1); }
|
|
50
|
+
|
|
51
|
+
/* ---- window -------------------------------------------------------- */
|
|
14
52
|
.bb-window {
|
|
15
|
-
position: absolute; bottom:
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
53
|
+
position: absolute; bottom: 74px; ${t}: 0;
|
|
54
|
+
width: 384px; max-width: calc(100vw - 32px);
|
|
55
|
+
height: min(620px, calc(100vh - 122px));
|
|
56
|
+
display: flex; flex-direction: column; background: #fff;
|
|
57
|
+
border-radius: ${o}; overflow: hidden;
|
|
58
|
+
box-shadow: 0 24px 64px -16px rgba(15,18,34,.28), 0 6px 20px -6px rgba(15,18,34,.14), 0 0 0 1px rgba(15,18,34,.05);
|
|
59
|
+
opacity: 0; transform: translateY(10px) scale(.97);
|
|
60
|
+
transform-origin: bottom ${t==="left"?"left":"right"};
|
|
61
|
+
pointer-events: none;
|
|
62
|
+
transition: opacity .2s ease, transform .24s cubic-bezier(.22,1.2,.36,1);
|
|
19
63
|
}
|
|
20
|
-
.bb
|
|
21
|
-
|
|
22
|
-
.bb-
|
|
23
|
-
|
|
24
|
-
|
|
64
|
+
.bb.open .bb-window { opacity: 1; transform: none; pointer-events: auto; }
|
|
65
|
+
/* Shopper-expanded = the large centered dialog (same as full-screen mode) */
|
|
66
|
+
.bb.expanded .bb-window { position: fixed;
|
|
67
|
+
inset: min(6vh, 60px) max(24px, calc((100vw - 1680px) / 2));
|
|
68
|
+
width: auto; max-width: none; height: auto; }
|
|
69
|
+
${l?`.bb-window { bottom: 0; top: auto; ${t}: 0; height: calc(100vh - 98px); border-end-start-radius: ${o}; }
|
|
70
|
+
.bb.expanded .bb-window { width: min(560px, calc(100vw - 40px)); }`:""}
|
|
71
|
+
${s?`.bb.open .bb-window { position: fixed;
|
|
72
|
+
inset: min(6vh, 60px) max(24px, calc((100vw - 1680px) / 2));
|
|
73
|
+
width: auto; max-width: none; height: auto; }
|
|
74
|
+
.bb.open .bb-launcher { opacity: 0; pointer-events: none; }`:""}
|
|
75
|
+
@media (max-width: 520px) {
|
|
76
|
+
.bb { bottom: 16px; ${t}: 16px; }
|
|
77
|
+
.bb.open .bb-window { position: fixed; inset: 0; width: 100%; max-width: none; height: 100%; max-height: none; border-radius: 0; }
|
|
78
|
+
.bb.open .bb-launcher { opacity: 0; pointer-events: none; }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/* ---- header -------------------------------------------------------- */
|
|
82
|
+
.bb-header { display: flex; align-items: center; gap: 11px; padding: 13px 16px;
|
|
83
|
+
background: #fff; border-bottom: 1px solid #eef0f4; flex-shrink: 0; }
|
|
84
|
+
.bb-avatar { width: 38px; height: 38px; position: relative; flex-shrink: 0;
|
|
85
|
+
border-radius: 999px; background: color-mix(in srgb, ${e} 14%, #fff);
|
|
86
|
+
color: ${e}; font-size: 15px; font-weight: 700;
|
|
87
|
+
display: flex; align-items: center; justify-content: center;
|
|
88
|
+
box-shadow: 0 0 0 2px #fff, 0 0 0 3.5px color-mix(in srgb, ${e} 35%, #fff); }
|
|
89
|
+
.bb-avatar img { width: 100%; height: 100%; border-radius: inherit; object-fit: cover; }
|
|
90
|
+
.bb-avatar::after { content: ''; position: absolute; bottom: -1px; inset-inline-end: -1px;
|
|
91
|
+
width: 10px; height: 10px; border-radius: 999px; background: #22c55e; border: 2px solid #fff; }
|
|
25
92
|
.bb-head-main { flex: 1; min-width: 0; }
|
|
26
|
-
.bb-name {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
.bb-
|
|
30
|
-
.bb-
|
|
31
|
-
.bb-
|
|
32
|
-
|
|
33
|
-
.bb-
|
|
34
|
-
.bb-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
.bb-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
.bb-
|
|
41
|
-
.bb-
|
|
42
|
-
.bb-
|
|
43
|
-
|
|
44
|
-
.bb-
|
|
45
|
-
|
|
46
|
-
.bb-
|
|
47
|
-
.bb-
|
|
48
|
-
.bb-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
.bb-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
.bb-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
93
|
+
.bb-name { display: block; font-size: 13.5px; font-weight: 650; color: #14161f;
|
|
94
|
+
letter-spacing: -.01em; line-height: 1.25;
|
|
95
|
+
white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
|
96
|
+
.bb-status { display: block; font-size: 11px; color: #8a8f9e; line-height: 1.3; }
|
|
97
|
+
.bb-actions { display: flex; align-items: center; gap: 2px; }
|
|
98
|
+
.bb-iconbtn { width: 30px; height: 30px; display: flex; align-items: center; justify-content: center;
|
|
99
|
+
border-radius: 8px; color: #9aa0ae; transition: background .12s ease, color .12s ease; }
|
|
100
|
+
.bb-iconbtn:hover { background: #f3f4f7; color: #4d5260; }
|
|
101
|
+
.bb-iconbtn .bb-ic { width: 17px; height: 17px; }
|
|
102
|
+
|
|
103
|
+
/* ---- messages ------------------------------------------------------ */
|
|
104
|
+
.bb-msgs { flex: 1; overflow-y: auto; overscroll-behavior: contain;
|
|
105
|
+
padding: 16px 14px 10px; display: flex; flex-direction: column; gap: 10px;
|
|
106
|
+
background: linear-gradient(color-mix(in srgb, ${e} 4%, #f7f8fa), #f7f8fa 140px); }
|
|
107
|
+
.bb-msgs::-webkit-scrollbar { width: 5px; }
|
|
108
|
+
.bb-msgs::-webkit-scrollbar-thumb { background: rgba(20,22,31,.12); border-radius: 99px; }
|
|
109
|
+
.bb-msg { max-width: 84%; padding: 9px 13px; font-size: 13px; line-height: 1.55;
|
|
110
|
+
word-break: break-word; animation: bb-in .2s ease both; unicode-bidi: plaintext; }
|
|
111
|
+
.bb-msg p + p, .bb-msg p + ul, .bb-msg ul + p { margin-top: 6px; }
|
|
112
|
+
.bb-msg ul { padding-inline-start: 18px; }
|
|
113
|
+
.bb-msg li { margin: 2px 0; }
|
|
114
|
+
.bb-msg a { color: ${e}; font-weight: 550; text-decoration: underline; text-underline-offset: 2px; }
|
|
115
|
+
.bb-msg.bot { align-self: flex-start; background: #fff; color: #232633;
|
|
116
|
+
border: 1px solid #eceef3; border-radius: ${r}; border-end-start-radius: ${d};
|
|
117
|
+
box-shadow: 0 1px 2px rgba(15,18,34,.04); }
|
|
118
|
+
.bb-msg.user { align-self: flex-end; background: ${e}; color: #fff;
|
|
119
|
+
border-radius: ${r}; border-end-end-radius: ${d};
|
|
120
|
+
box-shadow: 0 2px 6px -2px color-mix(in srgb, ${e} 50%, rgba(10,12,30,.3)); }
|
|
121
|
+
.bb-msg.err { align-self: flex-start; background: #fef2f2; color: #b91c1c;
|
|
122
|
+
border: 1px solid #fecaca; border-radius: ${r}; }
|
|
123
|
+
@keyframes bb-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
|
|
124
|
+
|
|
125
|
+
/* typing / tool status */
|
|
126
|
+
.bb-typing { align-self: flex-start; display: flex; align-items: center; gap: 8px;
|
|
127
|
+
background: #fff; border: 1px solid #eceef3; border-radius: ${r};
|
|
128
|
+
border-end-start-radius: ${d}; padding: 10px 13px; animation: bb-in .2s ease both; }
|
|
129
|
+
.bb-typing .bb-dots { display: flex; gap: 4px; }
|
|
130
|
+
.bb-typing .bb-dots i { width: 6px; height: 6px; border-radius: 99px;
|
|
131
|
+
background: color-mix(in srgb, ${e} 65%, #aab); animation: bb-bounce 1.2s ease-in-out infinite; }
|
|
132
|
+
.bb-typing .bb-dots i:nth-child(2) { animation-delay: .15s; }
|
|
133
|
+
.bb-typing .bb-dots i:nth-child(3) { animation-delay: .3s; }
|
|
134
|
+
.bb-typing .bb-tool { font-size: 11.5px; color: #8a8f9e; display: none; }
|
|
135
|
+
.bb-typing.searching .bb-tool { display: block; }
|
|
136
|
+
@keyframes bb-bounce { 0%, 60%, 100% { transform: none; opacity: .55; } 30% { transform: translateY(-4px); opacity: 1; } }
|
|
137
|
+
|
|
138
|
+
/* ---- product cards ------------------------------------------------- */
|
|
139
|
+
.bb-shelf { align-self: stretch; animation: bb-in .25s ease both; }
|
|
140
|
+
.bb-shelf-cap { font-size: 10.5px; font-weight: 650; letter-spacing: .07em; text-transform: uppercase;
|
|
141
|
+
color: #9aa0ae; padding: 2px 4px 6px; }
|
|
142
|
+
.bb-cards { display: flex; gap: 10px; overflow-x: auto; padding: 2px 2px 8px;
|
|
143
|
+
scroll-snap-type: x proximity; scrollbar-width: none; }
|
|
144
|
+
.bb-cards::-webkit-scrollbar { display: none; }
|
|
145
|
+
.bb-card { flex: 0 0 var(--bb-card-w); scroll-snap-align: start; background: #fff;
|
|
146
|
+
border: 1px solid #eceef3; border-radius: 12px; overflow: hidden;
|
|
147
|
+
display: flex; flex-direction: column;
|
|
148
|
+
box-shadow: 0 1px 2px rgba(15,18,34,.04);
|
|
149
|
+
transition: transform .16s ease, box-shadow .16s ease; }
|
|
150
|
+
.bb-card:hover { transform: translateY(-2px); box-shadow: 0 8px 20px -8px rgba(15,18,34,.18); }
|
|
151
|
+
.bb-card-img { display: flex; width: 100%; height: var(--bb-img-h); flex: 0 0 var(--bb-img-h); overflow: hidden; background: #f1f2f5; cursor: pointer; }
|
|
152
|
+
.bb-card-img img { width: 100%; height: 100%; object-fit: cover; display: block; }
|
|
153
|
+
.bb-card-img .bb-ic { width: 22px; height: 22px; color: #c6c9d4; margin: auto; }
|
|
154
|
+
.bb-card-body { padding: 8px 10px 10px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
|
|
155
|
+
.bb-card-title { font-size: var(--bb-title-fs); font-weight: 600; color: #1c1e29; line-height: 1.35;
|
|
156
|
+
letter-spacing: -.005em; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
|
|
157
|
+
overflow: hidden; min-height: 2.7em; cursor: pointer; unicode-bidi: plaintext; }
|
|
158
|
+
.bb-card-title:hover { color: ${e}; }
|
|
159
|
+
.bb-card-price { font-size: var(--bb-price-fs); font-weight: 700; color: #14161f;
|
|
160
|
+
font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
|
|
161
|
+
.bb-card-cta { display: flex; gap: 5px; margin-top: auto; }
|
|
162
|
+
.bb-btn { flex: 1; display: flex; align-items: center; justify-content: center; gap: 5px;
|
|
163
|
+
height: var(--bb-btn-h); border-radius: 8px; font-size: 11px; font-weight: 650;
|
|
164
|
+
white-space: nowrap; padding: 0 6px;
|
|
165
|
+
transition: filter .12s ease, background .12s ease, color .12s ease; }
|
|
166
|
+
.bb-btn .bb-ic { width: 13px; height: 13px; }
|
|
167
|
+
.bb-btn-add { background: ${e}; color: #fff; }
|
|
168
|
+
.bb-btn-add:hover { filter: brightness(1.08); }
|
|
169
|
+
.bb-btn-add[data-state="busy"] { opacity: .65; pointer-events: none; }
|
|
170
|
+
.bb-btn-add[data-state="done"] { background: #059669; pointer-events: none; }
|
|
171
|
+
.bb-btn-ghost { background: #fff; border: 1px solid #e3e5ec; color: #3c4150; flex: 0 0 auto; padding: 0 11px; }
|
|
172
|
+
.bb-btn-ghost:hover { background: #f6f7f9; border-color: #d5d8e1; }
|
|
173
|
+
.bb-btn-ghost.bb-wide { flex: 1; color: ${e}; border-color: color-mix(in srgb, ${e} 35%, #e3e5ec); }
|
|
174
|
+
.bb-btn-ghost.bb-wide:hover { background: color-mix(in srgb, ${e} 6%, #fff); }
|
|
175
|
+
.bb-btn:disabled { opacity: .45; pointer-events: none; }
|
|
176
|
+
|
|
177
|
+
/* in-card variant picker */
|
|
178
|
+
.bb-card.picking .bb-card-cta { display: none; }
|
|
179
|
+
.bb-pick { display: flex; flex-direction: column; gap: 6px; margin-top: 4px;
|
|
180
|
+
padding-top: 8px; border-top: 1px dashed #e9ebf0; position: relative; }
|
|
181
|
+
.bb-pick-close { position: absolute; top: 6px; inset-inline-end: 0;
|
|
182
|
+
width: 20px; height: 20px; display: flex; align-items: center; justify-content: center;
|
|
183
|
+
border-radius: 6px; color: #9aa0ae; }
|
|
184
|
+
.bb-pick-close:hover { background: #f3f4f7; color: #4d5260; }
|
|
185
|
+
.bb-pick-close .bb-ic { width: 11px; height: 11px; }
|
|
186
|
+
.bb-pick-key { font-size: 9.5px; font-weight: 700; text-transform: uppercase;
|
|
187
|
+
letter-spacing: .06em; color: #9aa0ae; unicode-bidi: plaintext; }
|
|
188
|
+
.bb-pick-vals { display: flex; flex-wrap: wrap; gap: 4px; }
|
|
189
|
+
.bb-chipv { border: 1px solid #e3e5ec; border-radius: 7px; padding: 3px 9px;
|
|
190
|
+
font-size: 11px; font-weight: 550; color: #3c4150; background: #fff;
|
|
191
|
+
transition: border-color .12s ease, background .12s ease, color .12s ease; }
|
|
192
|
+
.bb-chipv:hover { border-color: color-mix(in srgb, ${e} 50%, #e3e5ec); }
|
|
193
|
+
.bb-chipv.sel { background: ${e}; border-color: ${e}; color: #fff; }
|
|
194
|
+
.bb-chipv.off { opacity: .35; pointer-events: none; }
|
|
195
|
+
.bb-pick-foot { display: flex; align-items: center; justify-content: space-between;
|
|
196
|
+
gap: 6px; margin-top: 2px; }
|
|
197
|
+
.bb-pick-foot .bb-btn { flex: 0 0 auto; padding: 0 11px; }
|
|
198
|
+
.bb-pick-price { font-size: 12.5px; font-weight: 700; color: #14161f;
|
|
199
|
+
font-variant-numeric: tabular-nums; }
|
|
200
|
+
|
|
201
|
+
/* ---- starter chips -------------------------------------------------- */
|
|
202
|
+
.bb-chips { display: flex; flex-wrap: wrap; gap: 7px; padding: 4px 14px 12px; background: #f7f8fa; }
|
|
203
|
+
.bb-chip { border: 1px solid color-mix(in srgb, ${e} 30%, #e3e5ec);
|
|
204
|
+
color: color-mix(in srgb, ${e} 85%, #000); background: #fff; border-radius: 999px;
|
|
205
|
+
font-size: 12px; font-weight: 550; padding: 6px 13px;
|
|
206
|
+
transition: background .12s ease, transform .12s ease; }
|
|
207
|
+
.bb-chip:hover { background: color-mix(in srgb, ${e} 7%, #fff); transform: translateY(-1px); }
|
|
208
|
+
|
|
209
|
+
/* ---- escalation sheet ----------------------------------------------- */
|
|
210
|
+
.bb-esc { display: none; flex-direction: column; gap: 8px; padding: 12px 14px;
|
|
211
|
+
background: #fff; border-top: 1px solid #eef0f4; flex-shrink: 0; }
|
|
212
|
+
.bb-esc.open { display: flex; animation: bb-in .18s ease both; }
|
|
213
|
+
.bb-esc-title { font-size: 12px; font-weight: 650; color: #3c4150; }
|
|
214
|
+
.bb-esc input, .bb-esc textarea { border: 1px solid #e3e5ec; border-radius: 10px;
|
|
215
|
+
padding: 8px 11px; font-size: 12.5px; color: #1c1e29; outline: none; resize: none; background: #fbfbfd; }
|
|
216
|
+
.bb-esc input:focus, .bb-esc textarea:focus { border-color: color-mix(in srgb, ${e} 55%, #e3e5ec);
|
|
217
|
+
box-shadow: 0 0 0 3px color-mix(in srgb, ${e} 12%, transparent); background: #fff; }
|
|
218
|
+
.bb-esc-send { height: 33px; border-radius: 10px; background: ${e}; color: #fff;
|
|
219
|
+
font-size: 12.5px; font-weight: 650; }
|
|
220
|
+
.bb-esc-send:hover { filter: brightness(1.08); }
|
|
221
|
+
.bb-esc-note { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: #047857; font-weight: 550; }
|
|
222
|
+
.bb-esc-note .bb-ic { width: 15px; height: 15px; }
|
|
223
|
+
|
|
224
|
+
/* ---- composer ------------------------------------------------------- */
|
|
225
|
+
.bb-composer { display: flex; align-items: center; gap: 8px; padding: 11px 12px;
|
|
226
|
+
background: #fff; border-top: 1px solid #eef0f4; flex-shrink: 0; }
|
|
227
|
+
.bb-input { flex: 1; border: 1px solid transparent; outline: none; font-size: 13px; color: #1c1e29;
|
|
228
|
+
background: #f1f2f5; border-radius: 999px; padding: 9px 15px; min-width: 0;
|
|
229
|
+
resize: none; overflow-y: hidden; height: 38px; line-height: 1.45;
|
|
230
|
+
transition: border-color .12s ease, box-shadow .12s ease, background .12s ease; }
|
|
231
|
+
.bb-input::placeholder { color: #9aa0ae; }
|
|
232
|
+
.bb-input:focus { background: #fff; border-color: color-mix(in srgb, ${e} 55%, #e3e5ec);
|
|
233
|
+
box-shadow: 0 0 0 3px color-mix(in srgb, ${e} 12%, transparent); }
|
|
234
|
+
.bb-send { width: 36px; height: 36px; flex-shrink: 0; display: flex; align-items: center;
|
|
235
|
+
justify-content: center; border-radius: 999px; background: ${e}; color: #fff;
|
|
236
|
+
transition: transform .15s cubic-bezier(.34,1.56,.64,1), opacity .12s ease, filter .12s ease; }
|
|
237
|
+
.bb-send .bb-ic { width: 17px; height: 17px; ${n==="rtl"?"transform: scaleX(-1);":""} }
|
|
238
|
+
.bb-send:hover { filter: brightness(1.08); transform: scale(1.06); }
|
|
239
|
+
.bb-send:disabled { opacity: .4; pointer-events: none; }
|
|
240
|
+
|
|
241
|
+
/* Powered-by footer */
|
|
242
|
+
.bb-foot { flex-shrink: 0; display: flex; justify-content: center; align-items: center;
|
|
243
|
+
padding: 5px 12px 7px; background: #fff; }
|
|
244
|
+
.bb-foot a { display: inline-flex; align-items: center; gap: 5px; font-size: 10.5px;
|
|
245
|
+
font-weight: 500; color: #aab0bd; text-decoration: none; letter-spacing: .005em;
|
|
246
|
+
transition: color .12s ease; }
|
|
247
|
+
.bb-foot a:hover { color: #6b7280; }
|
|
248
|
+
.bb-foot .bb-ic { width: 13px; height: 13px; border-radius: 4px; }
|
|
249
|
+
`}render(e){let n=this.settings.accentColor||"#6366F1",t=O.has(this.locale)?"rtl":"ltr",i=this.settings.position==="start"?"left":"right",o=t==="rtl"?i==="left"?"right":"left":i,r=this.settings.displayName||"Assistant";this.host=document.createElement("div"),this.host.setAttribute("data-brainerce-bot",this.connectionId),this.root=this.host.attachShadow({mode:"open"});let d=document.createElement("style");d.textContent=this.css(n,t,o),this.root.appendChild(d);let a=document.createElement("div");a.className="bb",this.root.appendChild(a);let l=document.createElement("div");l.className="bb-scrim",l.addEventListener("click",()=>{this.expanded?this.toggleExpand():this.close()}),a.appendChild(l),this.windowEl=document.createElement("div"),this.windowEl.className="bb-window",a.appendChild(this.windowEl);let s=document.createElement("div");s.className="bb-header";let h=document.createElement("span");if(h.className="bb-avatar",this.settings.avatarUrl&&T(this.settings.avatarUrl)){let p=document.createElement("img");p.src=this.settings.avatarUrl,p.alt="",h.appendChild(p)}else h.appendChild(document.createTextNode(r.charAt(0).toUpperCase()));let u=document.createElement("span");u.className="bb-head-main";let g=document.createElement("span");g.className="bb-name",g.textContent=r;let w=document.createElement("span");w.className="bb-status",w.textContent=this.t("online"),u.appendChild(g),u.appendChild(w);let v=document.createElement("span");v.className="bb-actions",(this.settings.displayMode??"floating")!=="full_screen"&&this.settings.allowExpand!==!1&&(this.expandBtn=this.iconButton("expand",this.t("expand"),()=>this.toggleExpand()),v.appendChild(this.expandBtn)),v.appendChild(this.iconButton("mail",this.t("leaveMessage"),()=>this.toggleEscalation())),v.appendChild(this.iconButton("close",this.t("close"),()=>this.close())),s.appendChild(h),s.appendChild(u),s.appendChild(v),this.windowEl.appendChild(s),this.messagesEl=document.createElement("div"),this.messagesEl.className="bb-msgs",this.windowEl.appendChild(this.messagesEl),this.chipsEl=document.createElement("div"),this.chipsEl.className="bb-chips";for(let p of this.settings.starterQuestions??[]){let N=document.createElement("button");N.className="bb-chip",N.textContent=p,N.addEventListener("click",()=>this.send(p)),this.chipsEl.appendChild(N)}this.windowEl.appendChild(this.chipsEl);let c=document.createElement("div");c.className="bb-esc";let C=document.createElement("span");C.className="bb-esc-title",C.textContent=this.t("leaveMessage");let E=document.createElement("input");E.type="email",E.name="email",E.placeholder=this.t("yourEmail");let k=document.createElement("textarea");k.name="message",k.rows=2,k.placeholder=this.t("yourMessage");let I=document.createElement("button");I.type="button",I.className="bb-esc-send",I.textContent=this.t("send"),I.addEventListener("click",()=>this.submitEscalation(c)),c.appendChild(C),c.appendChild(E),c.appendChild(k),c.appendChild(I),this.windowEl.appendChild(c);let f=document.createElement("div");f.className="bb-composer",this.inputEl=document.createElement("textarea"),this.inputEl.className="bb-input",this.inputEl.rows=1,this.inputEl.placeholder=this.t("placeholder"),this.inputEl.addEventListener("keydown",p=>{p.key==="Enter"&&!p.shiftKey&&(p.preventDefault(),this.send(this.inputEl?.value??""))}),this.inputEl.addEventListener("input",()=>this.syncSendState()),this.sendBtn=document.createElement("button"),this.sendBtn.className="bb-send",this.sendBtn.disabled=!0,this.sendBtn.setAttribute("aria-label",this.t("send")),this.sendBtn.appendChild(m("send")),this.sendBtn.addEventListener("click",()=>this.send(this.inputEl?.value??"")),f.appendChild(this.inputEl),f.appendChild(this.sendBtn),this.windowEl.appendChild(f);let B=document.createElement("div");B.className="bb-foot";let x=document.createElement("a");if(x.href="https://brainerce.com",x.target="_blank",x.rel="noopener noreferrer",x.appendChild(m("brand")),x.appendChild(document.createTextNode(this.t("poweredBy"))),B.appendChild(x),this.windowEl.appendChild(B),this.launcherEl=document.createElement("button"),this.launcherEl.className="bb-launcher",this.launcherEl.setAttribute("aria-label",r),this.settings.avatarUrl&&T(this.settings.avatarUrl)){let p=document.createElement("img");p.src=this.settings.avatarUrl,p.alt="",this.launcherEl.appendChild(p)}else{let p=m("chat");p.classList.add("bb-l-chat"),this.launcherEl.appendChild(p)}let L=m("close");L.classList.add("bb-l-close"),this.launcherEl.appendChild(L),this.launcherEl.addEventListener("click",()=>this.opened?this.close():this.open()),a.appendChild(this.launcherEl),e.appendChild(this.host)}iconButton(e,n,t){let i=document.createElement("button");return i.className="bb-iconbtn",i.title=n,i.setAttribute("aria-label",n),i.appendChild(m(e)),i.addEventListener("click",t),i}syncSendState(){this.sendBtn&&(this.sendBtn.disabled=!(this.inputEl?.value??"").trim()||this.busy)}open(){if(!this.windowEl||this.opened)return;this.opened=!0;let e=this.root?.querySelector(".bb");e?.classList.add("open");let n=(this.settings.displayMode??"floating")==="full_screen"||window.innerWidth<=520;e?.classList.toggle("big",n||this.expanded),this.syncBodyScroll(),this.messagesEl&&this.messagesEl.childElementCount===0&&this.primeThread(),this.inputEl?.focus()}close(){this.opened=!1;let e=this.root?.querySelector(".bb");e?.classList.remove("open"),this.expanded||e?.classList.remove("big"),this.syncBodyScroll()}syncBodyScroll(){let e=this.opened&&!!this.root?.querySelector(".bb")?.classList.contains("big");e&&this.prevBodyOverflow===null?(this.prevBodyOverflow=document.body.style.overflow||"",document.body.style.overflow="hidden"):!e&&this.prevBodyOverflow!==null&&(document.body.style.overflow=this.prevBodyOverflow,this.prevBodyOverflow=null)}toggleExpand(){if(this.expanded=!this.expanded,this.root?.querySelector(".bb")?.classList.toggle("expanded",this.expanded),this.root?.querySelector(".bb")?.classList.toggle("big",this.expanded),this.syncBodyScroll(),this.expandBtn){this.expandBtn.replaceChildren(m(this.expanded?"collapse":"expand"));let e=this.t(this.expanded?"collapse":"expand");this.expandBtn.title=e,this.expandBtn.setAttribute("aria-label",e)}}async primeThread(){if(this.conversationId&&this.sessionId)try{let e=await fetch(`${this.baseUrl}/api/storefront-bot/${encodeURIComponent(this.connectionId)}/conversations/${encodeURIComponent(this.conversationId)}?limit=50`,{headers:{"X-Bot-Session":this.sessionId}});if(e.ok){let n=await e.json();for(let t of n.data){let i=this.appendMessage(t.role==="assistant"?"bot":"user","");U(i,t.content)}if(n.data.length>0){this.chipsEl?.remove();return}}else this.conversationId=null,this.sessionId=null,this.persistIds()}catch{}if(this.settings.greeting){let e=this.appendMessage("bot","");U(e,this.settings.greeting)}}async send(e){let n=e.trim();if(!n||this.busy)return;this.busy=!0,this.inputEl&&(this.inputEl.value=""),this.syncSendState(),this.chipsEl?.remove(),this.appendMessage("user",n);let t=this.appendTyping();this.pendingText="",this.cardsRow=null,this.cardIds=new Set;let i=null;try{let o=await fetch(`${this.baseUrl}/api/storefront-bot/${encodeURIComponent(this.connectionId)}/chat`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({message:n,turnId:P("trn_"),...this.conversationId?{conversationId:this.conversationId}:{},...this.sessionId?{anonymousSessionId:this.sessionId}:{},locale:this.locale})});if(!o.ok||!o.body)throw new Error(`chat failed (${o.status})`);let r=o.body.getReader(),d=new TextDecoder,a="";for(;;){let{value:l,done:s}=await r.read();if(s)break;a+=d.decode(l,{stream:!0});let h;for(;(h=a.indexOf(`
|
|
250
|
+
|
|
251
|
+
`))>=0;){let u=a.slice(0,h);if(a=a.slice(h+2),!u.startsWith("data: "))continue;let g;try{g=JSON.parse(u.slice(6))}catch{continue}i=this.handleFrame(g,t,i)}}i&&this.pendingText&&U(i,this.pendingText)}catch{this.appendMessage("err",this.t("error"))}finally{t.remove(),this.busy=!1,this.syncSendState()}}handleFrame(e,n,t){switch(e.type){case"connected":return this.conversationId=e.conversationId||this.conversationId,this.sessionId=e.anonymousSessionId||this.sessionId,this.persistIds(),t;case"token":return t||(n.remove(),t=this.appendMessage("bot","")),this.pendingText+=e.text,t.textContent=this.pendingText,this.scrollDown(),t;case"tool":return n.classList.toggle("searching",e.status==="running"),t;case"card":return this.appendCard(e.card),t;case"action":return this.handleAction(e),t;case"error":return this.appendMessage("err",e.message||this.t("error")),t;case"done":default:return t}}appendCard(e){if(!this.messagesEl||this.cardIds.has(e.productId))return;if(this.cardIds.add(e.productId),!this.cardsRow){let s=document.createElement("div");s.className="bb-shelf";let h=document.createElement("div");h.className="bb-shelf-cap",h.textContent=this.t("results"),this.cardsRow=document.createElement("div"),this.cardsRow.className="bb-cards",s.appendChild(h),s.appendChild(this.cardsRow),this.messagesEl.appendChild(s)}let n=T(e.url)?e.url:null,t=document.createElement("div");t.className="bb-card";let i=()=>{this.beacon(e.botRef),n&&(window.location.href=n)},o=document.createElement("span");if(o.className="bb-card-img",e.imageUrl&&T(e.imageUrl)){let s=document.createElement("img");s.src=e.imageUrl,s.alt="",s.loading="lazy",o.appendChild(s)}else o.appendChild(m("cart"));o.addEventListener("click",i),t.appendChild(o);let r=document.createElement("span");r.className="bb-card-body";let d=document.createElement("span");d.className="bb-card-title",d.textContent=e.title,d.addEventListener("click",i);let a=document.createElement("span");a.className="bb-card-price",a.textContent=e.price.formatted,r.appendChild(d),r.appendChild(a);let l=document.createElement("span");if(l.className="bb-card-cta",e.requiresOptions){let s=document.createElement("button");s.className="bb-btn bb-btn-add",s.setAttribute("aria-label",this.t("addToCart")),s.appendChild(m("cart")),e.variants?.length?s.addEventListener("click",()=>this.togglePicker(r,e,o)):s.addEventListener("click",i),l.appendChild(s)}else{let s=document.createElement("button");s.className="bb-btn bb-btn-add",s.appendChild(m("cart")),s.appendChild(document.createTextNode(this.t("addToCart"))),s.addEventListener("click",()=>void this.addToCart(e,s));let h=document.createElement("button");h.className="bb-btn bb-btn-ghost",h.appendChild(document.createTextNode(this.t("view"))),h.addEventListener("click",i),l.appendChild(s),l.appendChild(h)}r.appendChild(l),t.appendChild(r),this.cardsRow.appendChild(t),this.scrollDown()}togglePicker(e,n,t){let i=e.parentElement,o=e.querySelector(".bb-pick");if(o){o.remove(),i?.classList.remove("picking");return}i?.classList.add("picking");let r=n.variants??[],d=[];for(let u of r)for(let g of Object.keys(u.attributes))d.includes(g)||d.push(g);let a={},l=document.createElement("span");l.className="bb-pick";let s=()=>r.find(u=>d.every(g=>a[g]&&u.attributes[g]===a[g]))??null,h=()=>{l.replaceChildren();for(let c of d){let C=document.createElement("span");C.className="bb-pick-key",C.textContent=c;let E=document.createElement("span");E.className="bb-pick-vals";let k=new Set;for(let I of r){let f=I.attributes[c];if(!f||k.has(f))continue;k.add(f);let B=r.some(L=>L.attributes[c]===f&&d.every(p=>p===c||!a[p]||L.attributes[p]===a[p])),x=document.createElement("button");x.className=`bb-chipv${a[c]===f?" sel":""}${B?"":" off"}`,x.textContent=f,x.addEventListener("click",()=>{a[c]===f?delete a[c]:a[c]=f,h()}),E.appendChild(x)}l.appendChild(C),l.appendChild(E)}let u=document.createElement("button");u.className="bb-pick-close",u.setAttribute("aria-label",this.t("close")),u.appendChild(m("close")),u.addEventListener("click",()=>{l.remove(),i?.classList.remove("picking")}),l.appendChild(u);let g=s(),w=document.createElement("span");w.className="bb-pick-foot";let v=document.createElement("span");v.className="bb-pick-price",v.textContent=g?g.price.formatted:"";let y=document.createElement("button");if(y.className="bb-btn bb-btn-add",y.appendChild(m("cart")),y.appendChild(document.createTextNode(this.t("addToCart"))),g||(y.disabled=!0),y.addEventListener("click",()=>{let c=s();c&&this.addToCart(n,y,c.id)}),w.appendChild(v),w.appendChild(y),l.appendChild(w),g?.imageUrl&&T(g.imageUrl)){let c=t.querySelector("img");c&&(c.src=g.imageUrl)}this.scrollDown()};h(),e.appendChild(l),this.scrollDown()}async addToCart(e,n,t){this.beacon(e.botRef),n.dataset.state="busy",await this.dispatchAdd(e.productId,t??null)?(n.dataset.state="done",n.replaceChildren(m("check"),document.createTextNode(this.t("added"))),setTimeout(()=>{!this.destroyed&&n.isConnected&&(delete n.dataset.state,n.replaceChildren(m("cart"),document.createTextNode(this.t("addToCart"))))},2200)):(delete n.dataset.state,T(e.url)&&(window.location.href=e.url))}async dispatchAdd(e,n){try{if(this.onAddToCart)return await this.onAddToCart({productId:e,variantId:n,quantity:1})!==!1;let t=new CustomEvent("brainerce:bot:add-to-cart",{detail:{productId:e,variantId:n,quantity:1,connectionId:this.connectionId},cancelable:!0,bubbles:!0,composed:!0});return!window.dispatchEvent(t)}catch{return!1}}async handleAction(e){if(e.action!=="add_to_cart")return;e.botRef&&this.beacon(e.botRef),await this.dispatchAdd(e.productId,e.variantId)||this.appendMessage("err",this.t("addFailed"))}beacon(e){try{navigator.sendBeacon?.(`${this.baseUrl}/api/storefront-bot/attribution/click`,new Blob([JSON.stringify({botRef:e})],{type:"application/json"}))}catch{}}toggleEscalation(){this.root?.querySelector(".bb-esc")?.classList.toggle("open")}async submitEscalation(e){let n=e.querySelector('input[name="email"]')?.value.trim(),t=e.querySelector('textarea[name="message"]')?.value.trim();if(!(!n||!t||!this.conversationId||!this.sessionId))try{if((await fetch(`${this.baseUrl}/api/storefront-bot/${encodeURIComponent(this.connectionId)}/escalate`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:n,message:t,conversationId:this.conversationId,anonymousSessionId:this.sessionId,locale:this.locale})})).ok){let o=document.createElement("span");o.className="bb-esc-note",o.appendChild(m("check")),o.appendChild(document.createTextNode(this.t("sent"))),e.replaceChildren(o)}}catch{}}appendMessage(e,n){let t=document.createElement("div");return t.className=`bb-msg ${e}`,t.setAttribute("dir","auto"),t.textContent=n,this.messagesEl?.appendChild(t),this.scrollDown(),t}appendTyping(){let e=document.createElement("div");e.className="bb-typing";let n=document.createElement("span");n.className="bb-dots";for(let i=0;i<3;i++)n.appendChild(document.createElement("i"));let t=document.createElement("span");return t.className="bb-tool",t.textContent=this.t("searching"),e.appendChild(n),e.appendChild(t),this.messagesEl?.appendChild(e),this.scrollDown(),e}scrollDown(){this.messagesEl&&(this.messagesEl.scrollTop=this.messagesEl.scrollHeight)}};(()=>{let b=document.currentScript,e=b?.dataset.connectionId;if(!e){console.warn("[BrainerceBot] missing data-connection-id on the bot.js script tag");return}let n=b?.dataset.apiBase||void 0,t=()=>void M.mount({connectionId:e,baseUrl:n});document.readyState==="loading"?document.addEventListener("DOMContentLoaded",t,{once:!0}):t()})();})();
|
package/dist/bot/index.d.mts
CHANGED
|
@@ -11,6 +11,10 @@
|
|
|
11
11
|
* anonymousSessionId (localStorage). The server re-validates it on every call
|
|
12
12
|
* and silently replaces it when stale — the widget always adopts whatever ids
|
|
13
13
|
* arrive on the `connected` frame.
|
|
14
|
+
*
|
|
15
|
+
* Rendering safety: server/model strings NEVER pass through innerHTML. Text
|
|
16
|
+
* renders via textContent; the markdown-lite renderer builds DOM with
|
|
17
|
+
* createElement only; URLs pass the isSafeUrl gate before reaching href/src.
|
|
14
18
|
*/
|
|
15
19
|
interface BrainerceBotOptions {
|
|
16
20
|
/** Public vc_* connection id of the storefront. */
|
|
@@ -19,24 +23,43 @@ interface BrainerceBotOptions {
|
|
|
19
23
|
baseUrl?: string;
|
|
20
24
|
/** Mount target. Defaults to document.body. */
|
|
21
25
|
target?: HTMLElement;
|
|
26
|
+
/**
|
|
27
|
+
* Host-provided add-to-cart. When present the widget calls it instead of
|
|
28
|
+
* navigating, so the host's own cart state/UI stays in sync. Return (or
|
|
29
|
+
* resolve) false to signal failure — the widget then falls back to the PDP.
|
|
30
|
+
*/
|
|
31
|
+
onAddToCart?: (item: {
|
|
32
|
+
productId: string;
|
|
33
|
+
variantId?: string | null;
|
|
34
|
+
quantity: number;
|
|
35
|
+
}) => boolean | Promise<boolean>;
|
|
22
36
|
}
|
|
23
37
|
declare class BrainerceBot {
|
|
24
38
|
private readonly connectionId;
|
|
25
39
|
private readonly baseUrl;
|
|
26
40
|
private readonly storageKey;
|
|
41
|
+
private readonly onAddToCart?;
|
|
27
42
|
private host?;
|
|
28
43
|
private root?;
|
|
29
44
|
private windowEl?;
|
|
30
45
|
private messagesEl?;
|
|
31
46
|
private inputEl?;
|
|
47
|
+
private sendBtn?;
|
|
32
48
|
private chipsEl?;
|
|
49
|
+
private launcherEl?;
|
|
50
|
+
private expandBtn?;
|
|
33
51
|
private settings;
|
|
34
52
|
private locale;
|
|
35
53
|
private sessionId;
|
|
36
54
|
private conversationId;
|
|
37
55
|
private busy;
|
|
38
56
|
private opened;
|
|
57
|
+
private expanded;
|
|
39
58
|
private destroyed;
|
|
59
|
+
/** Per-turn streaming state. */
|
|
60
|
+
private pendingText;
|
|
61
|
+
private cardsRow;
|
|
62
|
+
private cardIds;
|
|
40
63
|
private constructor();
|
|
41
64
|
/** Boot the widget. Resolves to null when the bot is disabled server-side. */
|
|
42
65
|
static mount(options: BrainerceBotOptions): Promise<BrainerceBot | null>;
|
|
@@ -45,16 +68,49 @@ declare class BrainerceBot {
|
|
|
45
68
|
private t;
|
|
46
69
|
private restoreIds;
|
|
47
70
|
private persistIds;
|
|
71
|
+
private css;
|
|
48
72
|
private render;
|
|
73
|
+
private iconButton;
|
|
74
|
+
private syncSendState;
|
|
49
75
|
private open;
|
|
50
76
|
private close;
|
|
77
|
+
/**
|
|
78
|
+
* The large dialog owns the screen — the page behind must not scroll.
|
|
79
|
+
* Locks <body> while a big surface is open; restores on close/collapse.
|
|
80
|
+
*/
|
|
81
|
+
private prevBodyOverflow;
|
|
82
|
+
private syncBodyScroll;
|
|
83
|
+
private toggleExpand;
|
|
51
84
|
/** First open: restore the server thread, or show the greeting. */
|
|
52
85
|
private primeThread;
|
|
53
86
|
private send;
|
|
54
87
|
private handleFrame;
|
|
88
|
+
private appendCard;
|
|
89
|
+
/**
|
|
90
|
+
* In-card variant picker: one chip-row per attribute; a complete selection
|
|
91
|
+
* resolves to a variantId and becomes a real add-to-cart. Variant image and
|
|
92
|
+
* price update live. Built entirely with createElement (no innerHTML).
|
|
93
|
+
*/
|
|
94
|
+
private togglePicker;
|
|
95
|
+
/**
|
|
96
|
+
* Add-to-cart resolution chain — must never be a dead button:
|
|
97
|
+
* 1. host `onAddToCart` option (scaffolded stores: syncs their cart UI)
|
|
98
|
+
* 2. cancelable `brainerce:bot:add-to-cart` CustomEvent (custom embeds)
|
|
99
|
+
* 3. fallback: navigate to the product page
|
|
100
|
+
*/
|
|
101
|
+
private addToCart;
|
|
102
|
+
/**
|
|
103
|
+
* The host-cart chain shared by card buttons and bot-initiated actions:
|
|
104
|
+
* onAddToCart option -> cancelable CustomEvent. Returns whether a host
|
|
105
|
+
* took the add.
|
|
106
|
+
*/
|
|
107
|
+
private dispatchAdd;
|
|
108
|
+
/** Bot-initiated widget actions (the model called the addToCart tool). */
|
|
109
|
+
private handleAction;
|
|
110
|
+
/** The durable conversion signal — fire-and-forget, never blocks. */
|
|
111
|
+
private beacon;
|
|
55
112
|
private toggleEscalation;
|
|
56
113
|
private submitEscalation;
|
|
57
|
-
private appendCard;
|
|
58
114
|
private appendMessage;
|
|
59
115
|
private appendTyping;
|
|
60
116
|
private scrollDown;
|