copilot-chat-widget 0.1.29 → 0.1.30

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.
@@ -1,7 +1,7 @@
1
- (function(){"use strict";(()=>{if(typeof window>"u"||typeof document>"u"||window.__copilotWidgetLoaded)return;window.__copilotWidgetLoaded=!0;const T="http://localhost:3000",b=64,x=720,y=document.currentScript,k=t=>{if(!t?.src)return{};try{const s=new URL(t.src,window.location.href),n={};return s.searchParams.forEach((l,c)=>{n[c]=l}),n}catch(s){return console.warn("[CopilotChat] Failed to parse script query params:",s),{}}},I=t=>{document.readyState==="complete"||document.readyState==="interactive"?setTimeout(t,0):document.addEventListener("DOMContentLoaded",t)},E=()=>y||Array.from(document.querySelectorAll("script")).reverse().find(n=>n.dataset?.token||n.src&&n.src.includes("chat-widget"))||null,U=({iframeUrl:t,launcherIcon:s})=>{const n=document.querySelector("[data-copilot-widget-root]");n&&n.remove();const l=w=>{const a=document.querySelector("[data-copilot-checkout-toast]");a&&a.remove();const h=document.createElement("div");h.setAttribute("data-copilot-checkout-toast","true"),h.innerText=w,Object.assign(h.style,{position:"fixed",top:"20px",left:"50%",transform:"translateX(-50%)",padding:"12px 16px",background:"#0f172a",color:"white",borderRadius:"12px",boxShadow:"0 8px 24px rgba(0,0,0,0.25)",zIndex:2147483647,fontSize:"14px",fontWeight:"600",maxWidth:"420px",lineHeight:"1.4",textAlign:"center"}),document.body.appendChild(h),setTimeout(()=>{h.remove()},4500)},c=document.createElement("div");c.setAttribute("data-copilot-widget-root","true");const C=c.attachShadow({mode:"open"});document.body.appendChild(c);const e=document.createElement("button");e.type="button",e.setAttribute("aria-label","Open Copilot chat"),e.innerHTML=`
1
+ (function(){"use strict";(()=>{if(typeof window>"u"||typeof document>"u"||window.__copilotWidgetLoaded)return;window.__copilotWidgetLoaded=!0;const T="http://localhost:3000",b=64,y=720,x=document.currentScript,k=t=>{if(!t?.src)return{};try{const s=new URL(t.src,window.location.href),n={};return s.searchParams.forEach((l,c)=>{n[c]=l}),n}catch(s){return console.warn("[CopilotChat] Failed to parse script query params:",s),{}}},I=t=>{document.readyState==="complete"||document.readyState==="interactive"?setTimeout(t,0):document.addEventListener("DOMContentLoaded",t)},v=()=>x||Array.from(document.querySelectorAll("script")).reverse().find(n=>n.dataset?.token||n.src&&n.src.includes("chat-widget"))||null,U=({iframeUrl:t,launcherIcon:s})=>{const n=document.querySelector("[data-copilot-widget-root]");n&&n.remove();const l=w=>{const a=document.querySelector("[data-copilot-checkout-toast]");a&&a.remove();const h=document.createElement("div");h.setAttribute("data-copilot-checkout-toast","true"),h.innerText=w,Object.assign(h.style,{position:"fixed",top:"20px",left:"50%",transform:"translateX(-50%)",padding:"12px 16px",background:"#0f172a",color:"white",borderRadius:"12px",boxShadow:"0 8px 24px rgba(0,0,0,0.25)",zIndex:2147483647,fontSize:"14px",fontWeight:"600",maxWidth:"420px",lineHeight:"1.4",textAlign:"center"}),document.body.appendChild(h),setTimeout(()=>{h.remove()},4500)},c=document.createElement("div");c.setAttribute("data-copilot-widget-root","true");const C=c.attachShadow({mode:"open"});document.body.appendChild(c);const e=document.createElement("button");e.type="button",e.setAttribute("aria-label","Open Copilot chat"),e.innerHTML=`
2
2
  <img
3
3
  src="${s}"
4
4
  alt="Copilot chat launcher"
5
5
  style="width: 38px; height: 38px; object-fit: contain; border-radius: 50%; pointer-events: none;"
6
6
  />
7
- `,Object.assign(e.style,{position:"fixed",bottom:"24px",right:"24px",width:`${b}px`,height:`${b}px`,borderRadius:"50%",border:"none",background:"linear-gradient(135deg, #0078ff, #00c6ff)",color:"white",cursor:"pointer",zIndex:999998,display:"flex",alignItems:"center",justifyContent:"center",boxShadow:"0 6px 14px rgba(0,0,0,0.25)",transition:"all 0.25s ease"}),e.onmouseover=()=>{e.style.transform="scale(1.12)",e.style.boxShadow="0 10px 25px rgba(0,0,0,0.3)"},e.onmouseout=()=>{e.style.transform="scale(1)",e.style.boxShadow="0 6px 14px rgba(0,0,0,0.25)"};const i=document.createElement("div");i.setAttribute("data-copilot-widget-root","true"),Object.assign(i.style,{display:"none",position:"fixed",bottom:`${b+36}px`,right:"24px",zIndex:"999999",transformOrigin:"bottom right",transform:"scale(0.8) translateY(20px)",opacity:"0",transition:"all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)"});const o=document.createElement("div"),r=document.createElement("div"),m=document.createElement("div");Object.assign(o.style,{position:"absolute",bottom:"-14px",right:"28px",width:"30px",height:"20px",pointerEvents:"none",display:"none",zIndex:"1"}),Object.assign(r.style,{position:"absolute",bottom:"0",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"15px solid transparent",borderRight:"15px solid transparent",borderTop:"15px solid rgba(15,23,42,0.1)"}),Object.assign(m.style,{position:"absolute",bottom:"2px",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"13px solid transparent",borderRight:"13px solid transparent",borderTop:"13px solid white",boxShadow:"0 6px 16px rgba(15,23,42,0.12)",borderRadius:"2px"}),o.appendChild(r),o.appendChild(m);const p=document.createElement("div");Object.assign(p.style,{width:`${x}px`,maxWidth:"calc(100vw - 48px)",height:`${Math.min(x,Math.max(320,window.innerHeight-140))}px`,maxHeight:"calc(100vh - 150px)",borderRadius:"20px",background:"white",border:"1px solid rgba(15,23,42,0.12)",boxShadow:"0 18px 45px rgba(15,23,42,0.16)",overflow:"hidden"}),C.appendChild(p);const d=document.createElement("iframe");d.src=t,d.title="Copilot chat widget",d.allow="clipboard-read; clipboard-write; microphone; camera; display-capture",d.setAttribute("scrolling","no"),Object.assign(d.style,{width:"100%",height:"100%",border:"none",display:"block",background:"transparent",overflow:"hidden"}),C.appendChild(p),p.appendChild(d),i.appendChild(o),i.appendChild(p),document.body.appendChild(e),document.body.appendChild(i);let u=!1;const f=()=>{u&&(u=!1,i.style.opacity="0",i.style.transform="scale(0.8) translateY(20px)",o.style.display="none",setTimeout(()=>{i.style.display="none"},250),e.style.transform="scale(1)")},O=w=>{const{data:a,source:h}=w||{};if(a?.type){if(a.type==="CART_CHECKOUT"&&h===d.contentWindow){console.log("[CopilotChat] Received checkout payload from widget:",a),l("Data:");return}if(a.type==="WIDGET_READY"&&h===d.contentWindow){d.contentWindow.postMessage({type:"INIT_WIDGET"},"*");return}a.type==="CHAT_CLOSED"&&f()}};e.onclick=w=>{w.stopPropagation(),u=!u,u?(i.style.display="block",o.style.display="block",requestAnimationFrame(()=>{i.style.opacity="1",i.style.transform="scale(1) translateY(0)"})):f()},window.addEventListener("message",O),document.addEventListener("click",w=>{const a=w.target;a&&u&&!i.contains(a)&&a!==e&&f()});const g={close:f,open:()=>{u||e.click()}};return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.close=g.close,window.CopilotChat.open=g.open,window.CopilotChat.controls=g,g},v=t=>{console.error(`[CopilotChat] ${t}`)},A=()=>{const t=E();if(!t)return!1;const s=k(t),n=t.dataset||{},l=n.token||s.token,c=typeof window<"u"&&window.CopilotChatConfig&&window.CopilotChatConfig.token;return(l||c)&&n.autoload!=="false"},S=async(t={})=>{const s=t.scriptEl||E(),n=window.CopilotChatConfig||{},l=s?.dataset||{},c=k(s),e=(()=>{const o=t.token||n.token||l.token||c.token;if(o)return o;try{const r=window.localStorage.getItem("copilotChatToken");if(r)return r}catch{}if(typeof window<"u"&&typeof window.prompt=="function"){const r=window.prompt("Enter your Copilot Chat token:");if(r){try{window.localStorage.setItem("copilotChatToken",r)}catch{}return r}}return null})();if(!e)return v("Missing token (provide via init config, window.CopilotChatConfig.token, or data-token attribute)."),null;const i=t.baseUrl||n.baseUrl||l.baseUrl||c.baseUrl||T;try{const o=await fetch(`${i}/api/chat-widget/config?token=${encodeURIComponent(e)}`,{credentials:"omit",mode:"cors"});if(!o.ok)throw new Error(`Server responded with ${o.status}`);const r=await o.json();if(!r?.iframeUrl||!r?.launcherIcon)throw new Error("Received incomplete widget configuration from server.");return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.init=(p={})=>{const d={iframeUrl:p.iframeUrl||r.iframeUrl,launcherIcon:p.launcherIcon||r.launcherIcon};return U(d)},window.CopilotChat.init(t)}catch(o){return v(o instanceof Error?o.message:"Unknown error during widget bootstrap."),null}};A()&&I(()=>{S()}),window.CopilotChat=window.CopilotChat||{},window.CopilotChat.load=S})()})();
7
+ `,Object.assign(e.style,{position:"fixed",bottom:"24px",right:"24px",width:`${b}px`,height:`${b}px`,borderRadius:"50%",border:"none",background:"linear-gradient(135deg, #0078ff, #00c6ff)",color:"white",cursor:"pointer",zIndex:999998,display:"flex",alignItems:"center",justifyContent:"center",boxShadow:"0 6px 14px rgba(0,0,0,0.25)",transition:"all 0.25s ease"}),e.onmouseover=()=>{e.style.transform="scale(1.12)",e.style.boxShadow="0 10px 25px rgba(0,0,0,0.3)"},e.onmouseout=()=>{e.style.transform="scale(1)",e.style.boxShadow="0 6px 14px rgba(0,0,0,0.25)"};const i=document.createElement("div");i.setAttribute("data-copilot-widget-root","true"),Object.assign(i.style,{display:"none",position:"fixed",bottom:`${b+36}px`,right:"24px",zIndex:"999999",transformOrigin:"bottom right",transform:"scale(0.8) translateY(20px)",opacity:"0",transition:"all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)"});const o=document.createElement("div"),r=document.createElement("div"),m=document.createElement("div");Object.assign(o.style,{position:"absolute",bottom:"-14px",right:"28px",width:"30px",height:"20px",pointerEvents:"none",display:"none",zIndex:"1"}),Object.assign(r.style,{position:"absolute",bottom:"0",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"15px solid transparent",borderRight:"15px solid transparent",borderTop:"15px solid rgba(15,23,42,0.1)"}),Object.assign(m.style,{position:"absolute",bottom:"2px",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"13px solid transparent",borderRight:"13px solid transparent",borderTop:"13px solid white",boxShadow:"0 6px 16px rgba(15,23,42,0.12)",borderRadius:"2px"}),o.appendChild(r),o.appendChild(m);const p=document.createElement("div");Object.assign(p.style,{width:`${y}px`,maxWidth:"calc(100vw - 48px)",height:`${Math.min(y,Math.max(320,window.innerHeight-140))}px`,maxHeight:"calc(100vh - 150px)",borderRadius:"20px",background:"white",border:"1px solid rgba(15,23,42,0.12)",boxShadow:"0 18px 45px rgba(15,23,42,0.16)",overflow:"hidden"}),C.appendChild(p);const d=document.createElement("iframe");d.src=t,d.title="Copilot chat widget",d.allow="clipboard-read; clipboard-write; microphone; camera; display-capture",d.setAttribute("scrolling","no"),Object.assign(d.style,{width:"100%",height:"100%",border:"none",display:"block",background:"transparent",overflow:"hidden"}),C.appendChild(p),p.appendChild(d),i.appendChild(o),i.appendChild(p),document.body.appendChild(e),document.body.appendChild(i);let u=!1;const f=()=>{u&&(u=!1,i.style.opacity="0",i.style.transform="scale(0.8) translateY(20px)",o.style.display="none",setTimeout(()=>{i.style.display="none"},250),e.style.transform="scale(1)")},O=w=>{const{data:a,source:h}=w||{};if(a?.type){if(a.type==="CART_CHECKOUT"&&h===d.contentWindow){console.log("[CopilotChat] Received checkout payload from widget:",a),l("Checkout message received from chat widget. Check console for payload.");return}if(a.type==="WIDGET_READY"&&h===d.contentWindow){d.contentWindow.postMessage({type:"INIT_WIDGET"},"*");return}a.type==="CHAT_CLOSED"&&f()}};e.onclick=w=>{w.stopPropagation(),u=!u,u?(i.style.display="block",o.style.display="block",requestAnimationFrame(()=>{i.style.opacity="1",i.style.transform="scale(1) translateY(0)"})):f()},window.addEventListener("message",O),document.addEventListener("click",w=>{const a=w.target;a&&u&&!i.contains(a)&&a!==e&&f()});const g={close:f,open:()=>{u||e.click()}};return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.close=g.close,window.CopilotChat.open=g.open,window.CopilotChat.controls=g,g},E=t=>{console.error(`[CopilotChat] ${t}`)},A=()=>{const t=v();if(!t)return!1;const s=k(t),n=t.dataset||{},l=n.token||s.token,c=typeof window<"u"&&window.CopilotChatConfig&&window.CopilotChatConfig.token;return(l||c)&&n.autoload!=="false"},S=async(t={})=>{const s=t.scriptEl||v(),n=window.CopilotChatConfig||{},l=s?.dataset||{},c=k(s),e=(()=>{const o=t.token||n.token||l.token||c.token;if(o)return o;try{const r=window.localStorage.getItem("copilotChatToken");if(r)return r}catch{}if(typeof window<"u"&&typeof window.prompt=="function"){const r=window.prompt("Enter your Copilot Chat token:");if(r){try{window.localStorage.setItem("copilotChatToken",r)}catch{}return r}}return null})();if(!e)return E("Missing token (provide via init config, window.CopilotChatConfig.token, or data-token attribute)."),null;const i=t.baseUrl||n.baseUrl||l.baseUrl||c.baseUrl||T;try{const o=await fetch(`${i}/api/chat-widget/config?token=${encodeURIComponent(e)}`,{credentials:"omit",mode:"cors"});if(!o.ok)throw new Error(`Server responded with ${o.status}`);const r=await o.json();if(!r?.iframeUrl||!r?.launcherIcon)throw new Error("Received incomplete widget configuration from server.");return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.init=(p={})=>{const d={iframeUrl:p.iframeUrl||r.iframeUrl,launcherIcon:p.launcherIcon||r.launcherIcon};return U(d)},window.CopilotChat.init(t)}catch(o){return E(o instanceof Error?o.message:"Unknown error during widget bootstrap."),null}};A()&&I(()=>{S()}),window.CopilotChat=window.CopilotChat||{},window.CopilotChat.load=S})()})();
package/dist/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
- "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const v="http://localhost:3000",w=64,C=720,E=({iframeUrl:c,launcherIcon:p})=>{const r=document.querySelector("[data-copilot-widget-root]");r&&r.remove();const a=document.createElement("div");a.setAttribute("data-copilot-widget-root","true");const h=a.attachShadow({mode:"open"});document.body.appendChild(a);const t=document.createElement("button");t.type="button",t.setAttribute("aria-label","Open Copilot chat"),t.innerHTML=`
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const k="http://localhost:3000",w=64,C=720,E=({iframeUrl:h,launcherIcon:u})=>{const d=document.querySelector("[data-copilot-widget-root]");d&&d.remove();const g=s=>{const o=document.querySelector("[data-copilot-checkout-toast]");o&&o.remove();const r=document.createElement("div");r.setAttribute("data-copilot-checkout-toast","true"),r.innerText=s,Object.assign(r.style,{position:"fixed",top:"20px",left:"50%",transform:"translateX(-50%)",padding:"12px 16px",background:"#0f172a",color:"white",borderRadius:"12px",boxShadow:"0 8px 24px rgba(0,0,0,0.25)",zIndex:2147483647,fontSize:"14px",fontWeight:"600",maxWidth:"420px",lineHeight:"1.4",textAlign:"center"}),document.body.appendChild(r),setTimeout(()=>{r.remove()},4500)},c=document.createElement("div");c.setAttribute("data-copilot-widget-root","true");const n=c.attachShadow({mode:"open"});document.body.appendChild(c);const t=document.createElement("button");t.type="button",t.setAttribute("aria-label","Open Copilot chat"),t.innerHTML=`
2
2
  <img
3
- src="${p}"
3
+ src="${u}"
4
4
  alt="Copilot chat launcher"
5
5
  style="width: 38px; height: 38px; object-fit: contain; border-radius: 50%; pointer-events: none;"
6
6
  />
7
- `,Object.assign(t.style,{position:"fixed",bottom:"24px",right:"24px",width:`${w}px`,height:`${w}px`,borderRadius:"50%",border:"none",background:"linear-gradient(135deg, #0078ff, #00c6ff)",color:"white",cursor:"pointer",zIndex:999998,display:"flex",alignItems:"center",justifyContent:"center",boxShadow:"0 6px 14px rgba(0,0,0,0.25)",transition:"all 0.25s ease"}),t.onmouseover=()=>{t.style.transform="scale(1.12)",t.style.boxShadow="0 10px 25px rgba(0,0,0,0.3)"},t.onmouseout=()=>{t.style.transform="scale(1)",t.style.boxShadow="0 6px 14px rgba(0,0,0,0.25)"};const e=document.createElement("div");e.setAttribute("data-copilot-widget-root","true"),Object.assign(e.style,{display:"none",position:"fixed",bottom:`${w+36}px`,right:"24px",zIndex:"999999",transformOrigin:"bottom right",transform:"scale(0.8) translateY(20px)",opacity:"0",transition:"all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)"});const s=document.createElement("div"),b=document.createElement("div"),m=document.createElement("div");Object.assign(s.style,{position:"absolute",bottom:"-14px",right:"28px",width:"30px",height:"20px",pointerEvents:"none",display:"none",zIndex:"1"}),Object.assign(b.style,{position:"absolute",bottom:"0",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"15px solid transparent",borderRight:"15px solid transparent",borderTop:"15px solid rgba(15,23,42,0.1)"}),Object.assign(m.style,{position:"absolute",bottom:"2px",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"13px solid transparent",borderRight:"13px solid transparent",borderTop:"13px solid white",boxShadow:"0 6px 16px rgba(15,23,42,0.12)",borderRadius:"2px"}),s.appendChild(b),s.appendChild(m);const l=document.createElement("div");Object.assign(l.style,{width:`${C}px`,maxWidth:"calc(100vw - 48px)",height:`${Math.min(C,Math.max(320,window.innerHeight-140))}px`,maxHeight:"calc(100vh - 150px)",borderRadius:"20px",background:"white",border:"1px solid rgba(15,23,42,0.12)",boxShadow:"0 18px 45px rgba(15,23,42,0.16)",overflow:"hidden"}),h.appendChild(l);const o=document.createElement("iframe");o.src=c,o.title="Copilot chat widget",o.allow="clipboard-read; clipboard-write; microphone; camera; display-capture",o.setAttribute("scrolling","no"),Object.assign(o.style,{width:"100%",height:"100%",border:"none",display:"block",background:"transparent",overflow:"hidden"}),h.appendChild(l),l.appendChild(o),e.appendChild(s),e.appendChild(l),document.body.appendChild(t),document.body.appendChild(e);let i=!1;const u=()=>{i&&(i=!1,e.style.opacity="0",e.style.transform="scale(0.8) translateY(20px)",s.style.display="none",setTimeout(()=>{e.style.display="none"},250),t.style.transform="scale(1)")},y=d=>{const{data:n,source:f}=d||{};if(n?.type){if(n.type==="CART_CHECKOUT"&&f===o.contentWindow){console.log("[CopilotChat] Received checkout payload from widget:",n);return}if(n.type==="WIDGET_READY"&&f===o.contentWindow){o.contentWindow.postMessage({type:"INIT_WIDGET"},"*");return}n.type==="CHAT_CLOSED"&&u()}};window.addEventListener("message",y),t.onclick=d=>{d.stopPropagation(),i=!i,i?(e.style.display="block",s.style.display="block",requestAnimationFrame(()=>{e.style.opacity="1",e.style.transform="scale(1) translateY(0)"})):u()},document.addEventListener("click",d=>{const n=d.target;n&&i&&!e.contains(n)&&n!==t&&u()});const g={close:u,open:()=>{i||t.click()}};return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.close=g.close,window.CopilotChat.open=g.open,window.CopilotChat.controls=g,g};async function x(c={}){if(typeof window>"u"||typeof document>"u")return null;const p=window.CopilotChatConfig||{},r=c.token||p.token||typeof window<"u"&&window.localStorage?.getItem("copilotChatToken")||null;if(!r)return console.error("[CopilotChat] Missing token (provide via loadCopilotChatWidget({ token }) or window.CopilotChatConfig.token)."),null;const a=c.baseUrl||p.baseUrl||v,h=`${a.replace(/\/$/,"")}/api/chat-widget/config?token=${encodeURIComponent(r)}`;try{const t=await fetch(h,{credentials:"omit",mode:"cors"});if(!t.ok)throw new Error(`Server responded with ${t.status}`);const e=await t.json();if(!e?.iframeUrl||!e?.launcherIcon)throw new Error("Received incomplete widget configuration from server.");return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.token=r,window.CopilotChat.baseUrl=a,E({iframeUrl:e.iframeUrl,launcherIcon:e.launcherIcon})}catch(t){return console.error("[CopilotChat] Error during widget bootstrap:",t instanceof Error?t.message:t),null}}exports.default=x;exports.loadCopilotChatWidget=x;
7
+ `,Object.assign(t.style,{position:"fixed",bottom:"24px",right:"24px",width:`${w}px`,height:`${w}px`,borderRadius:"50%",border:"none",background:"linear-gradient(135deg, #0078ff, #00c6ff)",color:"white",cursor:"pointer",zIndex:999998,display:"flex",alignItems:"center",justifyContent:"center",boxShadow:"0 6px 14px rgba(0,0,0,0.25)",transition:"all 0.25s ease"}),t.onmouseover=()=>{t.style.transform="scale(1.12)",t.style.boxShadow="0 10px 25px rgba(0,0,0,0.3)"},t.onmouseout=()=>{t.style.transform="scale(1)",t.style.boxShadow="0 6px 14px rgba(0,0,0,0.25)"};const e=document.createElement("div");e.setAttribute("data-copilot-widget-root","true"),Object.assign(e.style,{display:"none",position:"fixed",bottom:`${w+36}px`,right:"24px",zIndex:"999999",transformOrigin:"bottom right",transform:"scale(0.8) translateY(20px)",opacity:"0",transition:"all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)"});const l=document.createElement("div"),f=document.createElement("div"),x=document.createElement("div");Object.assign(l.style,{position:"absolute",bottom:"-14px",right:"28px",width:"30px",height:"20px",pointerEvents:"none",display:"none",zIndex:"1"}),Object.assign(f.style,{position:"absolute",bottom:"0",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"15px solid transparent",borderRight:"15px solid transparent",borderTop:"15px solid rgba(15,23,42,0.1)"}),Object.assign(x.style,{position:"absolute",bottom:"2px",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"13px solid transparent",borderRight:"13px solid transparent",borderTop:"13px solid white",boxShadow:"0 6px 16px rgba(15,23,42,0.12)",borderRadius:"2px"}),l.appendChild(f),l.appendChild(x);const p=document.createElement("div");Object.assign(p.style,{width:`${C}px`,maxWidth:"calc(100vw - 48px)",height:`${Math.min(C,Math.max(320,window.innerHeight-140))}px`,maxHeight:"calc(100vh - 150px)",borderRadius:"20px",background:"white",border:"1px solid rgba(15,23,42,0.12)",boxShadow:"0 18px 45px rgba(15,23,42,0.16)",overflow:"hidden"}),n.appendChild(p);const i=document.createElement("iframe");i.src=h,i.title="Copilot chat widget",i.allow="clipboard-read; clipboard-write; microphone; camera; display-capture",i.setAttribute("scrolling","no"),Object.assign(i.style,{width:"100%",height:"100%",border:"none",display:"block",background:"transparent",overflow:"hidden"}),n.appendChild(p),p.appendChild(i),e.appendChild(l),e.appendChild(p),document.body.appendChild(t),document.body.appendChild(e);let a=!1;const m=()=>{a&&(a=!1,e.style.opacity="0",e.style.transform="scale(0.8) translateY(20px)",l.style.display="none",setTimeout(()=>{e.style.display="none"},250),t.style.transform="scale(1)")},v=s=>{const{data:o,source:r}=s||{};if(o?.type){if(o.type==="CART_CHECKOUT"&&r===i.contentWindow){console.log("[CopilotChat] Received checkout payload from widget:",o),g("Checkout message received from chat widget. Check console for payload.");return}if(o.type==="WIDGET_READY"&&r===i.contentWindow){i.contentWindow.postMessage({type:"INIT_WIDGET"},"*");return}o.type==="CHAT_CLOSED"&&m()}};window.addEventListener("message",v),t.onclick=s=>{s.stopPropagation(),a=!a,a?(e.style.display="block",l.style.display="block",requestAnimationFrame(()=>{e.style.opacity="1",e.style.transform="scale(1) translateY(0)"})):m()},document.addEventListener("click",s=>{const o=s.target;o&&a&&!e.contains(o)&&o!==t&&m()});const b={close:m,open:()=>{a||t.click()}};return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.close=b.close,window.CopilotChat.open=b.open,window.CopilotChat.controls=b,b};async function y(h={}){if(typeof window>"u"||typeof document>"u")return null;const u=window.CopilotChatConfig||{},d=h.token||u.token||typeof window<"u"&&window.localStorage?.getItem("copilotChatToken")||null;if(!d)return console.error("[CopilotChat] Missing token (provide via loadCopilotChatWidget({ token }) or window.CopilotChatConfig.token)."),null;const g=h.baseUrl||u.baseUrl||k,c=`${g.replace(/\/$/,"")}/api/chat-widget/config?token=${encodeURIComponent(d)}`;try{const n=await fetch(c,{credentials:"omit",mode:"cors"});if(!n.ok)throw new Error(`Server responded with ${n.status}`);const t=await n.json();if(!t?.iframeUrl||!t?.launcherIcon)throw new Error("Received incomplete widget configuration from server.");return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.token=d,window.CopilotChat.baseUrl=g,E({iframeUrl:t.iframeUrl,launcherIcon:t.launcherIcon})}catch(n){return console.error("[CopilotChat] Error during widget bootstrap:",n instanceof Error?n.message:n),null}}exports.default=y;exports.loadCopilotChatWidget=y;
package/dist/index.mjs CHANGED
@@ -1,15 +1,38 @@
1
1
  const C = "http://localhost:3000";
2
- const x = ({ iframeUrl: c, launcherIcon: p }) => {
3
- const r = document.querySelector("[data-copilot-widget-root]");
4
- r && r.remove();
5
- const a = document.createElement("div");
6
- a.setAttribute("data-copilot-widget-root", "true");
7
- const h = a.attachShadow({ mode: "open" });
8
- document.body.appendChild(a);
2
+ const y = ({ iframeUrl: h, launcherIcon: u }) => {
3
+ const d = document.querySelector("[data-copilot-widget-root]");
4
+ d && d.remove();
5
+ const g = (s) => {
6
+ const o = document.querySelector("[data-copilot-checkout-toast]");
7
+ o && o.remove();
8
+ const r = document.createElement("div");
9
+ r.setAttribute("data-copilot-checkout-toast", "true"), r.innerText = s, Object.assign(r.style, {
10
+ position: "fixed",
11
+ top: "20px",
12
+ left: "50%",
13
+ transform: "translateX(-50%)",
14
+ padding: "12px 16px",
15
+ background: "#0f172a",
16
+ color: "white",
17
+ borderRadius: "12px",
18
+ boxShadow: "0 8px 24px rgba(0,0,0,0.25)",
19
+ zIndex: 2147483647,
20
+ fontSize: "14px",
21
+ fontWeight: "600",
22
+ maxWidth: "420px",
23
+ lineHeight: "1.4",
24
+ textAlign: "center"
25
+ }), document.body.appendChild(r), setTimeout(() => {
26
+ r.remove();
27
+ }, 4500);
28
+ }, c = document.createElement("div");
29
+ c.setAttribute("data-copilot-widget-root", "true");
30
+ const n = c.attachShadow({ mode: "open" });
31
+ document.body.appendChild(c);
9
32
  const t = document.createElement("button");
10
33
  t.type = "button", t.setAttribute("aria-label", "Open Copilot chat"), t.innerHTML = `
11
34
  <img
12
- src="${p}"
35
+ src="${u}"
13
36
  alt="Copilot chat launcher"
14
37
  style="width: 38px; height: 38px; object-fit: contain; border-radius: 50%; pointer-events: none;"
15
38
  />
@@ -47,8 +70,8 @@ const x = ({ iframeUrl: c, launcherIcon: p }) => {
47
70
  opacity: "0",
48
71
  transition: "all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)"
49
72
  });
50
- const s = document.createElement("div"), g = document.createElement("div"), b = document.createElement("div");
51
- Object.assign(s.style, {
73
+ const l = document.createElement("div"), b = document.createElement("div"), f = document.createElement("div");
74
+ Object.assign(l.style, {
52
75
  position: "absolute",
53
76
  bottom: "-14px",
54
77
  right: "28px",
@@ -57,7 +80,7 @@ const x = ({ iframeUrl: c, launcherIcon: p }) => {
57
80
  pointerEvents: "none",
58
81
  display: "none",
59
82
  zIndex: "1"
60
- }), Object.assign(g.style, {
83
+ }), Object.assign(b.style, {
61
84
  position: "absolute",
62
85
  bottom: "0",
63
86
  left: "0",
@@ -68,7 +91,7 @@ const x = ({ iframeUrl: c, launcherIcon: p }) => {
68
91
  borderLeft: "15px solid transparent",
69
92
  borderRight: "15px solid transparent",
70
93
  borderTop: "15px solid rgba(15,23,42,0.1)"
71
- }), Object.assign(b.style, {
94
+ }), Object.assign(f.style, {
72
95
  position: "absolute",
73
96
  bottom: "2px",
74
97
  left: "0",
@@ -81,9 +104,9 @@ const x = ({ iframeUrl: c, launcherIcon: p }) => {
81
104
  borderTop: "13px solid white",
82
105
  boxShadow: "0 6px 16px rgba(15,23,42,0.12)",
83
106
  borderRadius: "2px"
84
- }), s.appendChild(g), s.appendChild(b);
85
- const l = document.createElement("div");
86
- Object.assign(l.style, {
107
+ }), l.appendChild(b), l.appendChild(f);
108
+ const p = document.createElement("div");
109
+ Object.assign(p.style, {
87
110
  width: "720px",
88
111
  maxWidth: "calc(100vw - 48px)",
89
112
  height: `${Math.min(720, Math.max(320, window.innerHeight - 140))}px`,
@@ -93,84 +116,84 @@ const x = ({ iframeUrl: c, launcherIcon: p }) => {
93
116
  border: "1px solid rgba(15,23,42,0.12)",
94
117
  boxShadow: "0 18px 45px rgba(15,23,42,0.16)",
95
118
  overflow: "hidden"
96
- }), h.appendChild(l);
97
- const o = document.createElement("iframe");
98
- o.src = c, o.title = "Copilot chat widget", o.allow = "clipboard-read; clipboard-write; microphone; camera; display-capture", o.setAttribute("scrolling", "no"), Object.assign(o.style, {
119
+ }), n.appendChild(p);
120
+ const i = document.createElement("iframe");
121
+ i.src = h, i.title = "Copilot chat widget", i.allow = "clipboard-read; clipboard-write; microphone; camera; display-capture", i.setAttribute("scrolling", "no"), Object.assign(i.style, {
99
122
  width: "100%",
100
123
  height: "100%",
101
124
  border: "none",
102
125
  display: "block",
103
126
  background: "transparent",
104
127
  overflow: "hidden"
105
- }), h.appendChild(l), l.appendChild(o), e.appendChild(s), e.appendChild(l), document.body.appendChild(t), document.body.appendChild(e);
106
- let i = !1;
107
- const u = () => {
108
- i && (i = !1, e.style.opacity = "0", e.style.transform = "scale(0.8) translateY(20px)", s.style.display = "none", setTimeout(() => {
128
+ }), n.appendChild(p), p.appendChild(i), e.appendChild(l), e.appendChild(p), document.body.appendChild(t), document.body.appendChild(e);
129
+ let a = !1;
130
+ const m = () => {
131
+ a && (a = !1, e.style.opacity = "0", e.style.transform = "scale(0.8) translateY(20px)", l.style.display = "none", setTimeout(() => {
109
132
  e.style.display = "none";
110
133
  }, 250), t.style.transform = "scale(1)");
111
- }, f = (d) => {
112
- const { data: n, source: m } = d || {};
113
- if (n?.type) {
114
- if (n.type === "CART_CHECKOUT" && m === o.contentWindow) {
115
- console.log("[CopilotChat] Received checkout payload from widget:", n);
134
+ }, x = (s) => {
135
+ const { data: o, source: r } = s || {};
136
+ if (o?.type) {
137
+ if (o.type === "CART_CHECKOUT" && r === i.contentWindow) {
138
+ console.log("[CopilotChat] Received checkout payload from widget:", o), g("Checkout message received from chat widget. Check console for payload.");
116
139
  return;
117
140
  }
118
- if (n.type === "WIDGET_READY" && m === o.contentWindow) {
119
- o.contentWindow.postMessage({ type: "INIT_WIDGET" }, "*");
141
+ if (o.type === "WIDGET_READY" && r === i.contentWindow) {
142
+ i.contentWindow.postMessage({ type: "INIT_WIDGET" }, "*");
120
143
  return;
121
144
  }
122
- n.type === "CHAT_CLOSED" && u();
145
+ o.type === "CHAT_CLOSED" && m();
123
146
  }
124
147
  };
125
- window.addEventListener("message", f), t.onclick = (d) => {
126
- d.stopPropagation(), i = !i, i ? (e.style.display = "block", s.style.display = "block", requestAnimationFrame(() => {
148
+ window.addEventListener("message", x), t.onclick = (s) => {
149
+ s.stopPropagation(), a = !a, a ? (e.style.display = "block", l.style.display = "block", requestAnimationFrame(() => {
127
150
  e.style.opacity = "1", e.style.transform = "scale(1) translateY(0)";
128
- })) : u();
129
- }, document.addEventListener("click", (d) => {
130
- const n = d.target;
131
- n && i && !e.contains(n) && n !== t && u();
151
+ })) : m();
152
+ }, document.addEventListener("click", (s) => {
153
+ const o = s.target;
154
+ o && a && !e.contains(o) && o !== t && m();
132
155
  });
133
156
  const w = {
134
- close: u,
157
+ close: m,
135
158
  open: () => {
136
- i || t.click();
159
+ a || t.click();
137
160
  }
138
161
  };
139
162
  return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.close = w.close, window.CopilotChat.open = w.open, window.CopilotChat.controls = w, w;
140
163
  };
141
- async function y(c = {}) {
164
+ async function E(h = {}) {
142
165
  if (typeof window > "u" || typeof document > "u")
143
166
  return null;
144
- const p = window.CopilotChatConfig || {}, r = c.token || p.token || typeof window < "u" && window.localStorage?.getItem("copilotChatToken") || null;
145
- if (!r)
167
+ const u = window.CopilotChatConfig || {}, d = h.token || u.token || typeof window < "u" && window.localStorage?.getItem("copilotChatToken") || null;
168
+ if (!d)
146
169
  return console.error(
147
170
  "[CopilotChat] Missing token (provide via loadCopilotChatWidget({ token }) or window.CopilotChatConfig.token)."
148
171
  ), null;
149
- const a = c.baseUrl || p.baseUrl || C, h = `${a.replace(/\/$/, "")}/api/chat-widget/config?token=${encodeURIComponent(
150
- r
172
+ const g = h.baseUrl || u.baseUrl || C, c = `${g.replace(/\/$/, "")}/api/chat-widget/config?token=${encodeURIComponent(
173
+ d
151
174
  )}`;
152
175
  try {
153
- const t = await fetch(h, {
176
+ const n = await fetch(c, {
154
177
  credentials: "omit",
155
178
  mode: "cors"
156
179
  });
157
- if (!t.ok)
158
- throw new Error(`Server responded with ${t.status}`);
159
- const e = await t.json();
160
- if (!e?.iframeUrl || !e?.launcherIcon)
180
+ if (!n.ok)
181
+ throw new Error(`Server responded with ${n.status}`);
182
+ const t = await n.json();
183
+ if (!t?.iframeUrl || !t?.launcherIcon)
161
184
  throw new Error("Received incomplete widget configuration from server.");
162
- return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.token = r, window.CopilotChat.baseUrl = a, x({
163
- iframeUrl: e.iframeUrl,
164
- launcherIcon: e.launcherIcon
185
+ return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.token = d, window.CopilotChat.baseUrl = g, y({
186
+ iframeUrl: t.iframeUrl,
187
+ launcherIcon: t.launcherIcon
165
188
  });
166
- } catch (t) {
189
+ } catch (n) {
167
190
  return console.error(
168
191
  "[CopilotChat] Error during widget bootstrap:",
169
- t instanceof Error ? t.message : t
192
+ n instanceof Error ? n.message : n
170
193
  ), null;
171
194
  }
172
195
  }
173
196
  export {
174
- y as default,
175
- y as loadCopilotChatWidget
197
+ E as default,
198
+ E as loadCopilotChatWidget
176
199
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "copilot-chat-widget",
3
- "version": "0.1.29",
3
+ "version": "0.1.30",
4
4
  "description": "Embeddable Copilot chat widget that can be loaded via NPM or a script tag.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
package/src/index.js CHANGED
@@ -209,6 +209,7 @@ const buildWidget = ({ iframeUrl, launcherIcon }) => {
209
209
 
210
210
  if (data.type === "CART_CHECKOUT" && source === iframe.contentWindow) {
211
211
  console.log("[CopilotChat] Received checkout payload from widget:", data);
212
+ showCheckoutToast("Checkout message received from chat widget. Check console for payload.");
212
213
  return;
213
214
  }
214
215
 
@@ -27,10 +27,7 @@
27
27
  };
28
28
 
29
29
  const ready = (fn) => {
30
- if (
31
- document.readyState === "complete" ||
32
- document.readyState === "interactive"
33
- ) {
30
+ if (document.readyState === "complete" || document.readyState === "interactive") {
34
31
  setTimeout(fn, 0);
35
32
  } else {
36
33
  document.addEventListener("DOMContentLoaded", fn);
@@ -43,10 +40,7 @@
43
40
  const srcUrl = new URL(scriptEl.src, window.location.href);
44
41
  return `${srcUrl.protocol}//${srcUrl.host}`;
45
42
  } catch (error) {
46
- console.warn(
47
- "[CopilotChat] Unable to infer base URL from script source.",
48
- error
49
- );
43
+ console.warn("[CopilotChat] Unable to infer base URL from script source.", error);
50
44
  return null;
51
45
  }
52
46
  };
@@ -59,11 +53,7 @@
59
53
  const scripts = Array.from(document.querySelectorAll("script"));
60
54
  const candidate = scripts
61
55
  .reverse()
62
- .find(
63
- (script) =>
64
- script.dataset?.token ||
65
- (script.src && script.src.includes("chat-widget"))
66
- );
56
+ .find((script) => script.dataset?.token || (script.src && script.src.includes("chat-widget")));
67
57
 
68
58
  return candidate || null;
69
59
  };
@@ -75,9 +65,7 @@
75
65
  }
76
66
 
77
67
  const showCheckoutToast = (message) => {
78
- const existingToast = document.querySelector(
79
- "[data-copilot-checkout-toast]"
80
- );
68
+ const existingToast = document.querySelector("[data-copilot-checkout-toast]");
81
69
  if (existingToast) {
82
70
  existingToast.remove();
83
71
  }
@@ -235,8 +223,7 @@
235
223
  const iframe = document.createElement("iframe");
236
224
  iframe.src = iframeUrl;
237
225
  iframe.title = "Copilot chat widget";
238
- iframe.allow =
239
- "clipboard-read; clipboard-write; microphone; camera; display-capture";
226
+ iframe.allow = "clipboard-read; clipboard-write; microphone; camera; display-capture";
240
227
  iframe.setAttribute("scrolling", "no");
241
228
  Object.assign(iframe.style, {
242
229
  width: "100%",
@@ -274,12 +261,8 @@
274
261
  if (!data?.type) return;
275
262
 
276
263
  if (data.type === "CART_CHECKOUT" && source === iframe.contentWindow) {
277
- console.log(
278
- "[CopilotChat] Received checkout payload from widget:",
279
- data
280
- );
281
- showCheckoutToast("Data:", data);
282
-
264
+ console.log("[CopilotChat] Received checkout payload from widget:", data);
265
+ showCheckoutToast("Checkout message received from chat widget. Check console for payload.");
283
266
  return;
284
267
  }
285
268
 
@@ -345,10 +328,7 @@
345
328
  const queryConfig = readQueryConfig(scriptEl);
346
329
  const dataset = scriptEl.dataset || {};
347
330
  const hasTokenHint = dataset.token || queryConfig.token;
348
- const hasGlobalToken =
349
- typeof window !== "undefined" &&
350
- window.CopilotChatConfig &&
351
- window.CopilotChatConfig.token;
331
+ const hasGlobalToken = typeof window !== "undefined" && window.CopilotChatConfig && window.CopilotChatConfig.token;
352
332
  return (hasTokenHint || hasGlobalToken) && dataset.autoload !== "false";
353
333
  };
354
334
 
@@ -359,11 +339,7 @@
359
339
  const queryConfig = readQueryConfig(scriptEl);
360
340
 
361
341
  const resolveToken = () => {
362
- const fromConfig =
363
- config.token ||
364
- globalConfig.token ||
365
- datasetConfig.token ||
366
- queryConfig.token;
342
+ const fromConfig = config.token || globalConfig.token || datasetConfig.token || queryConfig.token;
367
343
  if (fromConfig) return fromConfig;
368
344
 
369
345
  try {
@@ -373,10 +349,7 @@
373
349
  // ignore storage errors
374
350
  }
375
351
 
376
- if (
377
- typeof window !== "undefined" &&
378
- typeof window.prompt === "function"
379
- ) {
352
+ if (typeof window !== "undefined" && typeof window.prompt === "function") {
380
353
  const entered = window.prompt("Enter your Copilot Chat token:");
381
354
  if (entered) {
382
355
  try {
@@ -394,9 +367,7 @@
394
367
  const token = resolveToken();
395
368
 
396
369
  if (!token) {
397
- handleError(
398
- "Missing token (provide via init config, window.CopilotChatConfig.token, or data-token attribute)."
399
- );
370
+ handleError("Missing token (provide via init config, window.CopilotChatConfig.token, or data-token attribute).");
400
371
  return null;
401
372
  }
402
373
 
@@ -411,20 +382,15 @@
411
382
  null;
412
383
 
413
384
  if (!baseUrl) {
414
- handleError(
415
- "Unable to resolve base URL from embedding script or window.location."
416
- );
385
+ handleError("Unable to resolve base URL from embedding script or window.location.");
417
386
  return null;
418
387
  }
419
388
 
420
389
  try {
421
- const response = await fetch(
422
- `${baseUrl}/api/chat-widget/config?token=${encodeURIComponent(token)}`,
423
- {
424
- credentials: "omit",
425
- mode: "cors",
426
- }
427
- );
390
+ const response = await fetch(`${baseUrl}/api/chat-widget/config?token=${encodeURIComponent(token)}`, {
391
+ credentials: "omit",
392
+ mode: "cors",
393
+ });
428
394
 
429
395
  if (!response.ok) {
430
396
  throw new Error(`Server responded with ${response.status}`);
@@ -432,9 +398,7 @@
432
398
 
433
399
  const remoteConfig = await response.json();
434
400
  if (!remoteConfig?.iframeUrl || !remoteConfig?.launcherIcon) {
435
- throw new Error(
436
- "Received incomplete widget configuration from server."
437
- );
401
+ throw new Error("Received incomplete widget configuration from server.");
438
402
  }
439
403
 
440
404
  window.CopilotChat = window.CopilotChat || {};
@@ -450,11 +414,7 @@
450
414
  const controls = window.CopilotChat.init(config);
451
415
  return controls;
452
416
  } catch (error) {
453
- handleError(
454
- error instanceof Error
455
- ? error.message
456
- : "Unknown error during widget bootstrap."
457
- );
417
+ handleError(error instanceof Error ? error.message : "Unknown error during widget bootstrap.");
458
418
  return null;
459
419
  }
460
420
  };