@cognitiondesk/widget 1.2.2 → 1.2.3
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/react.es.js +12 -5
- package/dist/react.umd.cjs +1 -1
- package/dist/widget.es.js +12 -5
- package/dist/widget.umd.cjs +1 -1
- package/package.json +1 -1
- package/src/widget.js +22 -7
package/dist/react.es.js
CHANGED
|
@@ -326,7 +326,9 @@ let D = class {
|
|
|
326
326
|
const s = document.createElement("style");
|
|
327
327
|
s.textContent = z, this._shadow.appendChild(s), this._buildDOM(), this._applyConfig(), this._syncViewportMetrics(), this._bindViewportMetrics(), this._bindEvents(), this._cfg.welcomeMessage && this._appendMessage("assistant", this._cfg.welcomeMessage, !1);
|
|
328
328
|
};
|
|
329
|
-
return this._cfg.widgetId ? this._fetchWidgetConfig().then(() =>
|
|
329
|
+
return this._cfg.widgetId ? this._fetchWidgetConfig().then((i) => {
|
|
330
|
+
i !== !1 && e();
|
|
331
|
+
}).catch(() => e()) : (e(), Promise.resolve(this));
|
|
330
332
|
}
|
|
331
333
|
/**
|
|
332
334
|
* Programmatically update settings at runtime.
|
|
@@ -346,7 +348,8 @@ let D = class {
|
|
|
346
348
|
* and wants them reflected immediately without reloading the page.
|
|
347
349
|
*/
|
|
348
350
|
async refreshConfig() {
|
|
349
|
-
this._cfg.widgetId
|
|
351
|
+
if (!this._cfg.widgetId) return;
|
|
352
|
+
await this._fetchWidgetConfig() === !1 ? this.unmount() : this._applyConfig();
|
|
350
353
|
}
|
|
351
354
|
// ── Config internals ────────────────────────────────────────────────────────
|
|
352
355
|
/**
|
|
@@ -366,14 +369,16 @@ let D = class {
|
|
|
366
369
|
const t = `${this._cfg.backendUrl}/platforms/web-widget/public/${this._cfg.widgetId}`;
|
|
367
370
|
try {
|
|
368
371
|
const i = await fetch(t);
|
|
369
|
-
if (
|
|
372
|
+
if (i.status === 404 || i.status === 403) return !1;
|
|
373
|
+
if (!i.ok) return !0;
|
|
370
374
|
const { config: s } = await i.json();
|
|
371
375
|
if (!s) return;
|
|
372
376
|
const a = (n, o) => {
|
|
373
377
|
o != null && !this._userSet(n) && (this._cfg[n] = o);
|
|
374
378
|
};
|
|
375
|
-
a("botName", s.botName), a("welcomeMessage", s.welcomeMessage), a("primaryColor", s.primaryColor), a("secondaryColor", s.secondaryColor), a("placeholder", s.placeholder), a("theme", s.theme), a("position", s.position), a("style", s.style), a("size", s.size), s.assistantId && !this._cfg.assistantId && (this._cfg.assistantId = s.assistantId), (e = s.avatar) != null && e.value && !this._userSet("botEmoji") && (this._cfg.botEmoji = s.avatar.value), s.rateLimiting && (this._cfg.rateLimiting = { ...this._cfg.rateLimiting, ...s.rateLimiting }), this._cfg.overrideSettings && this._applyOverrides(this._cfg.overrideSettings);
|
|
379
|
+
return a("botName", s.botName), a("welcomeMessage", s.welcomeMessage), a("primaryColor", s.primaryColor), a("secondaryColor", s.secondaryColor), a("placeholder", s.placeholder), a("theme", s.theme), a("position", s.position), a("style", s.style), a("size", s.size), s.assistantId && !this._cfg.assistantId && (this._cfg.assistantId = s.assistantId), (e = s.avatar) != null && e.value && !this._userSet("botEmoji") && (this._cfg.botEmoji = s.avatar.value), s.rateLimiting && (this._cfg.rateLimiting = { ...this._cfg.rateLimiting, ...s.rateLimiting }), this._cfg.overrideSettings && this._applyOverrides(this._cfg.overrideSettings), !0;
|
|
376
380
|
} catch {
|
|
381
|
+
return !0;
|
|
377
382
|
}
|
|
378
383
|
}
|
|
379
384
|
/**
|
|
@@ -403,7 +408,9 @@ let D = class {
|
|
|
403
408
|
* Uses a debounce flag to avoid concurrent fetches.
|
|
404
409
|
*/
|
|
405
410
|
_silentRefresh() {
|
|
406
|
-
!this._cfg.widgetId || this._refreshPending || (this._refreshPending = !0, this._fetchWidgetConfig().then(() =>
|
|
411
|
+
!this._cfg.widgetId || this._refreshPending || (this._refreshPending = !0, this._fetchWidgetConfig().then((t) => {
|
|
412
|
+
t === !1 ? this.unmount() : this._applyConfig();
|
|
413
|
+
}).catch(() => {
|
|
407
414
|
}).finally(() => {
|
|
408
415
|
this._refreshPending = !1;
|
|
409
416
|
}));
|
package/dist/react.umd.cjs
CHANGED
|
@@ -244,7 +244,7 @@
|
|
|
244
244
|
font-size: 16px;
|
|
245
245
|
}
|
|
246
246
|
}
|
|
247
|
-
`,y={chat:'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>',close:'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>',send:'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/></svg>'};function P(){return"cd_"+Math.random().toString(36).slice(2)+Date.now().toString(36)}let M=class{constructor(t={}){var e,i,s,a,n;if(!t.apiKey)throw new Error("[CognitionDesk] apiKey is required");this._overrides=new Set(t.overrideSettings?Object.keys(t.overrideSettings):[]),this._cfg={apiKey:t.apiKey,widgetId:t.widgetId||null,assistantId:t.assistantId||null,backendUrl:t.backendUrl||H,primaryColor:t.primaryColor||"#2563eb",theme:t.theme||"light",botName:t.botName||"AI Assistant",botEmoji:t.botEmoji||"🤖",welcomeMessage:t.welcomeMessage||"Hello! How can I help you today?",placeholder:t.placeholder||"Type a message…",position:t.position||"bottom-right",streaming:t.streaming!==!1,rateLimiting:{enabled:((e=t.rateLimiting)==null?void 0:e.enabled)!==!1,maxMessagesPerSession:((i=t.rateLimiting)==null?void 0:i.maxMessagesPerSession)??0,maxMessagesPerMinute:((s=t.rateLimiting)==null?void 0:s.maxMessagesPerMinute)??0,limitReachedMessage:((a=t.rateLimiting)==null?void 0:a.limitReachedMessage)||"You've reached the message limit for this session.",rateLimitMessage:((n=t.rateLimiting)==null?void 0:n.rateLimitMessage)||"You're sending messages too quickly. Please wait a moment."},overrideSettings:t.overrideSettings||null},t.overrideSettings&&this._applyOverrides(t.overrideSettings),this._sessionId=P(),this._messageCount=0,this._minuteCount=0,this._minuteStart=Date.now(),this._messages=[],this._open=!1,this._loading=!1,this._container=null,this._shadow=null,this._panel=null,this._messagesEl=null,this._textarea=null,this._sendBtn=null,this._viewportHandler=null,this._refreshPending=!1}mount(t){const e=()=>{const i=t||document.body;this._container=document.createElement("div"),this._container.setAttribute("data-cognitiondesk",""),i.appendChild(this._container),this._shadow=this._container.attachShadow({mode:"open"});const s=document.createElement("style");s.textContent=T,this._shadow.appendChild(s),this._buildDOM(),this._applyConfig(),this._syncViewportMetrics(),this._bindViewportMetrics(),this._bindEvents(),this._cfg.welcomeMessage&&this._appendMessage("assistant",this._cfg.welcomeMessage,!1)};return this._cfg.widgetId?this._fetchWidgetConfig().then(
|
|
247
|
+
`,y={chat:'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>',close:'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>',send:'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/></svg>'};function P(){return"cd_"+Math.random().toString(36).slice(2)+Date.now().toString(36)}let M=class{constructor(t={}){var e,i,s,a,n;if(!t.apiKey)throw new Error("[CognitionDesk] apiKey is required");this._overrides=new Set(t.overrideSettings?Object.keys(t.overrideSettings):[]),this._cfg={apiKey:t.apiKey,widgetId:t.widgetId||null,assistantId:t.assistantId||null,backendUrl:t.backendUrl||H,primaryColor:t.primaryColor||"#2563eb",theme:t.theme||"light",botName:t.botName||"AI Assistant",botEmoji:t.botEmoji||"🤖",welcomeMessage:t.welcomeMessage||"Hello! How can I help you today?",placeholder:t.placeholder||"Type a message…",position:t.position||"bottom-right",streaming:t.streaming!==!1,rateLimiting:{enabled:((e=t.rateLimiting)==null?void 0:e.enabled)!==!1,maxMessagesPerSession:((i=t.rateLimiting)==null?void 0:i.maxMessagesPerSession)??0,maxMessagesPerMinute:((s=t.rateLimiting)==null?void 0:s.maxMessagesPerMinute)??0,limitReachedMessage:((a=t.rateLimiting)==null?void 0:a.limitReachedMessage)||"You've reached the message limit for this session.",rateLimitMessage:((n=t.rateLimiting)==null?void 0:n.rateLimitMessage)||"You're sending messages too quickly. Please wait a moment."},overrideSettings:t.overrideSettings||null},t.overrideSettings&&this._applyOverrides(t.overrideSettings),this._sessionId=P(),this._messageCount=0,this._minuteCount=0,this._minuteStart=Date.now(),this._messages=[],this._open=!1,this._loading=!1,this._container=null,this._shadow=null,this._panel=null,this._messagesEl=null,this._textarea=null,this._sendBtn=null,this._viewportHandler=null,this._refreshPending=!1}mount(t){const e=()=>{const i=t||document.body;this._container=document.createElement("div"),this._container.setAttribute("data-cognitiondesk",""),i.appendChild(this._container),this._shadow=this._container.attachShadow({mode:"open"});const s=document.createElement("style");s.textContent=T,this._shadow.appendChild(s),this._buildDOM(),this._applyConfig(),this._syncViewportMetrics(),this._bindViewportMetrics(),this._bindEvents(),this._cfg.welcomeMessage&&this._appendMessage("assistant",this._cfg.welcomeMessage,!1)};return this._cfg.widgetId?this._fetchWidgetConfig().then(i=>{i!==!1&&e()}).catch(()=>e()):(e(),Promise.resolve(this))}updateSettings(t){if(!t||typeof t!="object")return;const e={...this._cfg.overrideSettings||{},...t};this._cfg.overrideSettings=e,this._overrides=new Set(Object.keys(e)),this._applyOverrides(e),this._applyConfig()}async refreshConfig(){if(!this._cfg.widgetId)return;await this._fetchWidgetConfig()===!1?this.unmount():this._applyConfig()}_userSet(t){return this._overrides.has(t)}async _fetchWidgetConfig(){var e;const t=`${this._cfg.backendUrl}/platforms/web-widget/public/${this._cfg.widgetId}`;try{const i=await fetch(t);if(i.status===404||i.status===403)return!1;if(!i.ok)return!0;const{config:s}=await i.json();if(!s)return;const a=(n,o)=>{o!=null&&!this._userSet(n)&&(this._cfg[n]=o)};return a("botName",s.botName),a("welcomeMessage",s.welcomeMessage),a("primaryColor",s.primaryColor),a("secondaryColor",s.secondaryColor),a("placeholder",s.placeholder),a("theme",s.theme),a("position",s.position),a("style",s.style),a("size",s.size),s.assistantId&&!this._cfg.assistantId&&(this._cfg.assistantId=s.assistantId),(e=s.avatar)!=null&&e.value&&!this._userSet("botEmoji")&&(this._cfg.botEmoji=s.avatar.value),s.rateLimiting&&(this._cfg.rateLimiting={...this._cfg.rateLimiting,...s.rateLimiting}),this._cfg.overrideSettings&&this._applyOverrides(this._cfg.overrideSettings),!0}catch{return!0}}_applyOverrides(t){for(const[e,i]of Object.entries(t))e==="rateLimiting"&&typeof i=="object"?this._cfg.rateLimiting={...this._cfg.rateLimiting,...i}:this._cfg[e]=i}_applyConfig(){var s,a,n,o,d,c,l;const t=this._cfg.theme==="dark"||this._cfg.theme==="auto"&&((s=window.matchMedia)==null?void 0:s.call(window,"(prefers-color-scheme: dark)").matches);(a=this._root)==null||a.classList.toggle("cd-dark",t),(n=this._root)==null||n.style.setProperty("--cd-primary",this._cfg.primaryColor),(o=this._panel)==null||o.style.setProperty("--cd-primary",this._cfg.primaryColor),(d=this._toggleBtn)==null||d.style.setProperty("--cd-primary",this._cfg.primaryColor);const e=(c=this._shadow)==null?void 0:c.querySelector(".cd-header-avatar");e&&(e.textContent=this._cfg.botEmoji);const i=(l=this._shadow)==null?void 0:l.querySelector(".cd-header-name");i&&(i.textContent=this._cfg.botName),this._textarea&&(this._textarea.placeholder=this._cfg.placeholder)}_silentRefresh(){!this._cfg.widgetId||this._refreshPending||(this._refreshPending=!0,this._fetchWidgetConfig().then(t=>{t===!1?this.unmount():this._applyConfig()}).catch(()=>{}).finally(()=>{this._refreshPending=!1}))}unmount(){this._unbindViewportMetrics(),this._container&&(this._container.remove(),this._container=null)}open(){var t,e;this._open=!0,this._syncViewportMetrics(),(t=this._panel)==null||t.classList.add("open"),(e=this._textarea)==null||e.focus(),this._silentRefresh()}close(){var t;this._open=!1,(t=this._panel)==null||t.classList.remove("open")}toggle(){this._open?this.close():this.open()}clearHistory(){this._messages=[],this._messagesEl&&(this._messagesEl.innerHTML=""),this._cfg.welcomeMessage&&this._appendMessage("assistant",this._cfg.welcomeMessage)}_buildDOM(){const t=document.createElement("div");t.className="cd-root";const e=document.createElement("button");e.className="cd-widget-btn",e.innerHTML=y.chat,e.setAttribute("aria-label","Open chat"),e.style.setProperty("--cd-primary",this._cfg.primaryColor),this._toggleBtn=e;const i=document.createElement("div");i.className="cd-panel",this._panel=i;const s=document.createElement("div");s.className="cd-header",s.innerHTML=`
|
|
248
248
|
<div class="cd-header-avatar">${this._cfg.botEmoji}</div>
|
|
249
249
|
<div class="cd-header-info">
|
|
250
250
|
<div class="cd-header-name">${this._escHtml(this._cfg.botName)}</div>
|
package/dist/widget.es.js
CHANGED
|
@@ -324,7 +324,9 @@ class m {
|
|
|
324
324
|
const s = document.createElement("style");
|
|
325
325
|
s.textContent = _, this._shadow.appendChild(s), this._buildDOM(), this._applyConfig(), this._syncViewportMetrics(), this._bindViewportMetrics(), this._bindEvents(), this._cfg.welcomeMessage && this._appendMessage("assistant", this._cfg.welcomeMessage, !1);
|
|
326
326
|
};
|
|
327
|
-
return this._cfg.widgetId ? this._fetchWidgetConfig().then(() =>
|
|
327
|
+
return this._cfg.widgetId ? this._fetchWidgetConfig().then((a) => {
|
|
328
|
+
a !== !1 && t();
|
|
329
|
+
}).catch(() => t()) : (t(), Promise.resolve(this));
|
|
328
330
|
}
|
|
329
331
|
/**
|
|
330
332
|
* Programmatically update settings at runtime.
|
|
@@ -344,7 +346,8 @@ class m {
|
|
|
344
346
|
* and wants them reflected immediately without reloading the page.
|
|
345
347
|
*/
|
|
346
348
|
async refreshConfig() {
|
|
347
|
-
this._cfg.widgetId
|
|
349
|
+
if (!this._cfg.widgetId) return;
|
|
350
|
+
await this._fetchWidgetConfig() === !1 ? this.unmount() : this._applyConfig();
|
|
348
351
|
}
|
|
349
352
|
// ── Config internals ────────────────────────────────────────────────────────
|
|
350
353
|
/**
|
|
@@ -364,14 +367,16 @@ class m {
|
|
|
364
367
|
const e = `${this._cfg.backendUrl}/platforms/web-widget/public/${this._cfg.widgetId}`;
|
|
365
368
|
try {
|
|
366
369
|
const a = await fetch(e);
|
|
367
|
-
if (
|
|
370
|
+
if (a.status === 404 || a.status === 403) return !1;
|
|
371
|
+
if (!a.ok) return !0;
|
|
368
372
|
const { config: s } = await a.json();
|
|
369
373
|
if (!s) return;
|
|
370
374
|
const i = (r, n) => {
|
|
371
375
|
n != null && !this._userSet(r) && (this._cfg[r] = n);
|
|
372
376
|
};
|
|
373
|
-
i("botName", s.botName), i("welcomeMessage", s.welcomeMessage), i("primaryColor", s.primaryColor), i("secondaryColor", s.secondaryColor), i("placeholder", s.placeholder), i("theme", s.theme), i("position", s.position), i("style", s.style), i("size", s.size), s.assistantId && !this._cfg.assistantId && (this._cfg.assistantId = s.assistantId), (t = s.avatar) != null && t.value && !this._userSet("botEmoji") && (this._cfg.botEmoji = s.avatar.value), s.rateLimiting && (this._cfg.rateLimiting = { ...this._cfg.rateLimiting, ...s.rateLimiting }), this._cfg.overrideSettings && this._applyOverrides(this._cfg.overrideSettings);
|
|
377
|
+
return i("botName", s.botName), i("welcomeMessage", s.welcomeMessage), i("primaryColor", s.primaryColor), i("secondaryColor", s.secondaryColor), i("placeholder", s.placeholder), i("theme", s.theme), i("position", s.position), i("style", s.style), i("size", s.size), s.assistantId && !this._cfg.assistantId && (this._cfg.assistantId = s.assistantId), (t = s.avatar) != null && t.value && !this._userSet("botEmoji") && (this._cfg.botEmoji = s.avatar.value), s.rateLimiting && (this._cfg.rateLimiting = { ...this._cfg.rateLimiting, ...s.rateLimiting }), this._cfg.overrideSettings && this._applyOverrides(this._cfg.overrideSettings), !0;
|
|
374
378
|
} catch {
|
|
379
|
+
return !0;
|
|
375
380
|
}
|
|
376
381
|
}
|
|
377
382
|
/**
|
|
@@ -401,7 +406,9 @@ class m {
|
|
|
401
406
|
* Uses a debounce flag to avoid concurrent fetches.
|
|
402
407
|
*/
|
|
403
408
|
_silentRefresh() {
|
|
404
|
-
!this._cfg.widgetId || this._refreshPending || (this._refreshPending = !0, this._fetchWidgetConfig().then(() =>
|
|
409
|
+
!this._cfg.widgetId || this._refreshPending || (this._refreshPending = !0, this._fetchWidgetConfig().then((e) => {
|
|
410
|
+
e === !1 ? this.unmount() : this._applyConfig();
|
|
411
|
+
}).catch(() => {
|
|
405
412
|
}).finally(() => {
|
|
406
413
|
this._refreshPending = !1;
|
|
407
414
|
}));
|
package/dist/widget.umd.cjs
CHANGED
|
@@ -244,7 +244,7 @@
|
|
|
244
244
|
font-size: 16px;
|
|
245
245
|
}
|
|
246
246
|
}
|
|
247
|
-
`,m={chat:'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>',close:'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>',send:'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/></svg>'};function v(){return"cd_"+Math.random().toString(36).slice(2)+Date.now().toString(36)}class g{constructor(e={}){var t,i,s,a,r;if(!e.apiKey)throw new Error("[CognitionDesk] apiKey is required");this._overrides=new Set(e.overrideSettings?Object.keys(e.overrideSettings):[]),this._cfg={apiKey:e.apiKey,widgetId:e.widgetId||null,assistantId:e.assistantId||null,backendUrl:e.backendUrl||h,primaryColor:e.primaryColor||"#2563eb",theme:e.theme||"light",botName:e.botName||"AI Assistant",botEmoji:e.botEmoji||"🤖",welcomeMessage:e.welcomeMessage||"Hello! How can I help you today?",placeholder:e.placeholder||"Type a message…",position:e.position||"bottom-right",streaming:e.streaming!==!1,rateLimiting:{enabled:((t=e.rateLimiting)==null?void 0:t.enabled)!==!1,maxMessagesPerSession:((i=e.rateLimiting)==null?void 0:i.maxMessagesPerSession)??0,maxMessagesPerMinute:((s=e.rateLimiting)==null?void 0:s.maxMessagesPerMinute)??0,limitReachedMessage:((a=e.rateLimiting)==null?void 0:a.limitReachedMessage)||"You've reached the message limit for this session.",rateLimitMessage:((r=e.rateLimiting)==null?void 0:r.rateLimitMessage)||"You're sending messages too quickly. Please wait a moment."},overrideSettings:e.overrideSettings||null},e.overrideSettings&&this._applyOverrides(e.overrideSettings),this._sessionId=v(),this._messageCount=0,this._minuteCount=0,this._minuteStart=Date.now(),this._messages=[],this._open=!1,this._loading=!1,this._container=null,this._shadow=null,this._panel=null,this._messagesEl=null,this._textarea=null,this._sendBtn=null,this._viewportHandler=null,this._refreshPending=!1}mount(e){const t=()=>{const i=e||document.body;this._container=document.createElement("div"),this._container.setAttribute("data-cognitiondesk",""),i.appendChild(this._container),this._shadow=this._container.attachShadow({mode:"open"});const s=document.createElement("style");s.textContent=b,this._shadow.appendChild(s),this._buildDOM(),this._applyConfig(),this._syncViewportMetrics(),this._bindViewportMetrics(),this._bindEvents(),this._cfg.welcomeMessage&&this._appendMessage("assistant",this._cfg.welcomeMessage,!1)};return this._cfg.widgetId?this._fetchWidgetConfig().then(
|
|
247
|
+
`,m={chat:'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>',close:'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>',send:'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/></svg>'};function v(){return"cd_"+Math.random().toString(36).slice(2)+Date.now().toString(36)}class g{constructor(e={}){var t,i,s,a,r;if(!e.apiKey)throw new Error("[CognitionDesk] apiKey is required");this._overrides=new Set(e.overrideSettings?Object.keys(e.overrideSettings):[]),this._cfg={apiKey:e.apiKey,widgetId:e.widgetId||null,assistantId:e.assistantId||null,backendUrl:e.backendUrl||h,primaryColor:e.primaryColor||"#2563eb",theme:e.theme||"light",botName:e.botName||"AI Assistant",botEmoji:e.botEmoji||"🤖",welcomeMessage:e.welcomeMessage||"Hello! How can I help you today?",placeholder:e.placeholder||"Type a message…",position:e.position||"bottom-right",streaming:e.streaming!==!1,rateLimiting:{enabled:((t=e.rateLimiting)==null?void 0:t.enabled)!==!1,maxMessagesPerSession:((i=e.rateLimiting)==null?void 0:i.maxMessagesPerSession)??0,maxMessagesPerMinute:((s=e.rateLimiting)==null?void 0:s.maxMessagesPerMinute)??0,limitReachedMessage:((a=e.rateLimiting)==null?void 0:a.limitReachedMessage)||"You've reached the message limit for this session.",rateLimitMessage:((r=e.rateLimiting)==null?void 0:r.rateLimitMessage)||"You're sending messages too quickly. Please wait a moment."},overrideSettings:e.overrideSettings||null},e.overrideSettings&&this._applyOverrides(e.overrideSettings),this._sessionId=v(),this._messageCount=0,this._minuteCount=0,this._minuteStart=Date.now(),this._messages=[],this._open=!1,this._loading=!1,this._container=null,this._shadow=null,this._panel=null,this._messagesEl=null,this._textarea=null,this._sendBtn=null,this._viewportHandler=null,this._refreshPending=!1}mount(e){const t=()=>{const i=e||document.body;this._container=document.createElement("div"),this._container.setAttribute("data-cognitiondesk",""),i.appendChild(this._container),this._shadow=this._container.attachShadow({mode:"open"});const s=document.createElement("style");s.textContent=b,this._shadow.appendChild(s),this._buildDOM(),this._applyConfig(),this._syncViewportMetrics(),this._bindViewportMetrics(),this._bindEvents(),this._cfg.welcomeMessage&&this._appendMessage("assistant",this._cfg.welcomeMessage,!1)};return this._cfg.widgetId?this._fetchWidgetConfig().then(i=>{i!==!1&&t()}).catch(()=>t()):(t(),Promise.resolve(this))}updateSettings(e){if(!e||typeof e!="object")return;const t={...this._cfg.overrideSettings||{},...e};this._cfg.overrideSettings=t,this._overrides=new Set(Object.keys(t)),this._applyOverrides(t),this._applyConfig()}async refreshConfig(){if(!this._cfg.widgetId)return;await this._fetchWidgetConfig()===!1?this.unmount():this._applyConfig()}_userSet(e){return this._overrides.has(e)}async _fetchWidgetConfig(){var t;const e=`${this._cfg.backendUrl}/platforms/web-widget/public/${this._cfg.widgetId}`;try{const i=await fetch(e);if(i.status===404||i.status===403)return!1;if(!i.ok)return!0;const{config:s}=await i.json();if(!s)return;const a=(r,n)=>{n!=null&&!this._userSet(r)&&(this._cfg[r]=n)};return a("botName",s.botName),a("welcomeMessage",s.welcomeMessage),a("primaryColor",s.primaryColor),a("secondaryColor",s.secondaryColor),a("placeholder",s.placeholder),a("theme",s.theme),a("position",s.position),a("style",s.style),a("size",s.size),s.assistantId&&!this._cfg.assistantId&&(this._cfg.assistantId=s.assistantId),(t=s.avatar)!=null&&t.value&&!this._userSet("botEmoji")&&(this._cfg.botEmoji=s.avatar.value),s.rateLimiting&&(this._cfg.rateLimiting={...this._cfg.rateLimiting,...s.rateLimiting}),this._cfg.overrideSettings&&this._applyOverrides(this._cfg.overrideSettings),!0}catch{return!0}}_applyOverrides(e){for(const[t,i]of Object.entries(e))t==="rateLimiting"&&typeof i=="object"?this._cfg.rateLimiting={...this._cfg.rateLimiting,...i}:this._cfg[t]=i}_applyConfig(){var s,a,r,n,o,d,l;const e=this._cfg.theme==="dark"||this._cfg.theme==="auto"&&((s=window.matchMedia)==null?void 0:s.call(window,"(prefers-color-scheme: dark)").matches);(a=this._root)==null||a.classList.toggle("cd-dark",e),(r=this._root)==null||r.style.setProperty("--cd-primary",this._cfg.primaryColor),(n=this._panel)==null||n.style.setProperty("--cd-primary",this._cfg.primaryColor),(o=this._toggleBtn)==null||o.style.setProperty("--cd-primary",this._cfg.primaryColor);const t=(d=this._shadow)==null?void 0:d.querySelector(".cd-header-avatar");t&&(t.textContent=this._cfg.botEmoji);const i=(l=this._shadow)==null?void 0:l.querySelector(".cd-header-name");i&&(i.textContent=this._cfg.botName),this._textarea&&(this._textarea.placeholder=this._cfg.placeholder)}_silentRefresh(){!this._cfg.widgetId||this._refreshPending||(this._refreshPending=!0,this._fetchWidgetConfig().then(e=>{e===!1?this.unmount():this._applyConfig()}).catch(()=>{}).finally(()=>{this._refreshPending=!1}))}unmount(){this._unbindViewportMetrics(),this._container&&(this._container.remove(),this._container=null)}open(){var e,t;this._open=!0,this._syncViewportMetrics(),(e=this._panel)==null||e.classList.add("open"),(t=this._textarea)==null||t.focus(),this._silentRefresh()}close(){var e;this._open=!1,(e=this._panel)==null||e.classList.remove("open")}toggle(){this._open?this.close():this.open()}clearHistory(){this._messages=[],this._messagesEl&&(this._messagesEl.innerHTML=""),this._cfg.welcomeMessage&&this._appendMessage("assistant",this._cfg.welcomeMessage)}_buildDOM(){const e=document.createElement("div");e.className="cd-root";const t=document.createElement("button");t.className="cd-widget-btn",t.innerHTML=m.chat,t.setAttribute("aria-label","Open chat"),t.style.setProperty("--cd-primary",this._cfg.primaryColor),this._toggleBtn=t;const i=document.createElement("div");i.className="cd-panel",this._panel=i;const s=document.createElement("div");s.className="cd-header",s.innerHTML=`
|
|
248
248
|
<div class="cd-header-avatar">${this._cfg.botEmoji}</div>
|
|
249
249
|
<div class="cd-header-info">
|
|
250
250
|
<div class="cd-header-name">${this._escHtml(this._cfg.botName)}</div>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cognitiondesk/widget",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"description": "Embed a CognitionDesk AI chat widget into any website. Authenticate with an API key from your CognitionDesk dashboard.",
|
|
5
5
|
"keywords": ["chat", "widget", "ai", "cognitiondesk", "chatbot", "react", "streaming", "mounaji"],
|
|
6
6
|
"license": "MIT",
|
package/src/widget.js
CHANGED
|
@@ -387,8 +387,8 @@ export default class CognitionDeskWidget {
|
|
|
387
387
|
|
|
388
388
|
if (this._cfg.widgetId) {
|
|
389
389
|
return this._fetchWidgetConfig()
|
|
390
|
-
.then(()
|
|
391
|
-
.catch(() => _mount());
|
|
390
|
+
.then(active => { if (active !== false) _mount(); })
|
|
391
|
+
.catch(() => _mount()); // network error — mount with local config
|
|
392
392
|
}
|
|
393
393
|
|
|
394
394
|
_mount();
|
|
@@ -419,8 +419,12 @@ export default class CognitionDeskWidget {
|
|
|
419
419
|
*/
|
|
420
420
|
async refreshConfig() {
|
|
421
421
|
if (!this._cfg.widgetId) return;
|
|
422
|
-
await this._fetchWidgetConfig();
|
|
423
|
-
|
|
422
|
+
const active = await this._fetchWidgetConfig();
|
|
423
|
+
if (active === false) {
|
|
424
|
+
this.unmount();
|
|
425
|
+
} else {
|
|
426
|
+
this._applyConfig();
|
|
427
|
+
}
|
|
424
428
|
}
|
|
425
429
|
|
|
426
430
|
// ── Config internals ────────────────────────────────────────────────────────
|
|
@@ -442,7 +446,9 @@ export default class CognitionDeskWidget {
|
|
|
442
446
|
const url = `${this._cfg.backendUrl}/platforms/web-widget/public/${this._cfg.widgetId}`;
|
|
443
447
|
try {
|
|
444
448
|
const res = await fetch(url);
|
|
445
|
-
|
|
449
|
+
// Widget deleted or disabled — signal caller to unmount
|
|
450
|
+
if (res.status === 404 || res.status === 403) return false;
|
|
451
|
+
if (!res.ok) return true; // other HTTP error — keep existing config
|
|
446
452
|
const { config } = await res.json();
|
|
447
453
|
if (!config) return;
|
|
448
454
|
|
|
@@ -480,8 +486,10 @@ export default class CognitionDeskWidget {
|
|
|
480
486
|
if (this._cfg.overrideSettings) {
|
|
481
487
|
this._applyOverrides(this._cfg.overrideSettings);
|
|
482
488
|
}
|
|
489
|
+
|
|
490
|
+
return true; // config applied successfully
|
|
483
491
|
} catch {
|
|
484
|
-
//
|
|
492
|
+
return true; // network error — keep current config, stay mounted
|
|
485
493
|
}
|
|
486
494
|
}
|
|
487
495
|
|
|
@@ -533,7 +541,14 @@ export default class CognitionDeskWidget {
|
|
|
533
541
|
if (!this._cfg.widgetId || this._refreshPending) return;
|
|
534
542
|
this._refreshPending = true;
|
|
535
543
|
this._fetchWidgetConfig()
|
|
536
|
-
.then(
|
|
544
|
+
.then(active => {
|
|
545
|
+
if (active === false) {
|
|
546
|
+
// Widget was deleted or disabled — remove it from the page silently
|
|
547
|
+
this.unmount();
|
|
548
|
+
} else {
|
|
549
|
+
this._applyConfig();
|
|
550
|
+
}
|
|
551
|
+
})
|
|
537
552
|
.catch(() => {})
|
|
538
553
|
.finally(() => { this._refreshPending = false; });
|
|
539
554
|
}
|