copilot-chat-widget 0.1.14 → 0.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
- (function(){"use strict";(()=>{if(typeof window>"u"||typeof document>"u"||window.__copilotWidgetLoaded)return;window.__copilotWidgetLoaded=!0;const S="http://localhost:3000",g=64,C=720,b=document.currentScript,y=t=>{if(!t?.src)return{};try{const s=new URL(t.src,window.location.href),o={};return s.searchParams.forEach((d,c)=>{o[c]=d}),o}catch(s){return console.warn("[CopilotChat] Failed to parse script query params:",s),{}}},T=t=>{document.readyState==="complete"||document.readyState==="interactive"?setTimeout(t,0):document.addEventListener("DOMContentLoaded",t)},x=()=>b||Array.from(document.querySelectorAll("script")).reverse().find(o=>o.dataset?.token||o.src&&o.src.includes("chat-widget"))||null,I=({iframeUrl:t,launcherIcon:s})=>{const o=document.querySelector("[data-copilot-widget-root]");o&&o.remove();const d=document.createElement("div");d.setAttribute("data-copilot-widget-root","true");const c=d.attachShadow({mode:"open"});document.body.appendChild(d);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:`${g}px`,height:`${g}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 n=document.createElement("div");n.setAttribute("data-copilot-widget-root","true"),Object.assign(n.style,{display:"none",position:"fixed",bottom:`${g+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 h=document.createElement("div"),i=document.createElement("div"),r=document.createElement("div");Object.assign(h.style,{position:"absolute",bottom:"-14px",right:"28px",width:"30px",height:"20px",pointerEvents:"none",display:"none",zIndex:"1"}),Object.assign(i.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(r.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"}),h.appendChild(i),h.appendChild(r);const u=document.createElement("div");Object.assign(u.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"}),c.appendChild(u);const a=document.createElement("iframe");a.src=t,a.title="Copilot chat widget",a.allow="clipboard-read; clipboard-write; microphone; camera; display-capture",a.setAttribute("scrolling","no"),Object.assign(a.style,{width:"100%",height:"100%",border:"none",display:"block",background:"transparent",overflow:"hidden"}),c.appendChild(u),u.appendChild(a),n.appendChild(h),n.appendChild(u),document.body.appendChild(e),document.body.appendChild(n);let l=!1;const f=()=>{l&&(l=!1,n.style.opacity="0",n.style.transform="scale(0.8) translateY(20px)",h.style.display="none",setTimeout(()=>{n.style.display="none"},250),e.style.transform="scale(1)")},O=w=>{const{data:p,source:v}=w||{};if(p?.type){if(p.type==="CART_CHECKOUT"&&v===a.contentWindow){console.log("[CopilotChat] Received checkout payload from widget:",p),showCheckoutToast("Checkout message received from chat widget. Check console for payload.");return}if(p.type==="WIDGET_READY"&&v===a.contentWindow){a.contentWindow.postMessage({type:"INIT_WIDGET"},"*");return}p.type==="CHAT_CLOSED"&&f()}};e.onclick=w=>{w.stopPropagation(),l=!l,l?(n.style.display="block",h.style.display="block",requestAnimationFrame(()=>{n.style.opacity="1",n.style.transform="scale(1) translateY(0)"})):f()},window.addEventListener("message",O),document.addEventListener("click",w=>{const p=w.target;p&&l&&!n.contains(p)&&p!==e&&f()});const m={close:f,open:()=>{l||e.click()}};return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.close=m.close,window.CopilotChat.open=m.open,window.CopilotChat.controls=m,m},k=t=>{console.error(`[CopilotChat] ${t}`)},U=()=>{const t=x();if(!t)return!1;const s=y(t),o=t.dataset||{},d=o.token||s.token,c=typeof window<"u"&&window.CopilotChatConfig&&window.CopilotChatConfig.token;return(d||c)&&o.autoload!=="false"},E=async(t={})=>{const s=t.scriptEl||x(),o=window.CopilotChatConfig||{},d=s?.dataset||{},c=y(s),n=(()=>{const i=t.token||o.token||d.token||c.token;if(i)return i;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(!n)return k("Missing token (provide via init config, window.CopilotChatConfig.token, or data-token attribute)."),null;const h=t.baseUrl||o.baseUrl||d.baseUrl||c.baseUrl||S;try{const i=await fetch(`${h}/api/chat-widget/config?token=${encodeURIComponent(n)}`,{credentials:"omit",mode:"cors"});if(!i.ok)throw new Error(`Server responded with ${i.status}`);const r=await i.json();if(!r?.iframeUrl||!r?.launcherIcon)throw new Error("Received incomplete widget configuration from server.");return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.init=(a={})=>{const l={iframeUrl:a.iframeUrl||r.iframeUrl,launcherIcon:a.launcherIcon||r.launcherIcon};return I(l)},window.CopilotChat.init(t)}catch(i){return k(i instanceof Error?i.message:"Unknown error during widget bootstrap."),null}};U()&&T(()=>{E()}),window.CopilotChat=window.CopilotChat||{},window.CopilotChat.load=E})()})();
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "copilot-chat-widget",
3
- "version": "0.1.14",
3
+ "version": "0.1.16",
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",
@@ -57,12 +57,47 @@
57
57
 
58
58
  return candidate || null;
59
59
  };
60
-
61
- const buildWidget = ({ iframeUrl, launcherIcon }) => {
62
- const existingRoot = document.querySelector("[data-copilot-widget-root]");
63
- if (existingRoot) {
64
- existingRoot.remove();
65
- }
60
+
61
+ const buildWidget = ({ iframeUrl, launcherIcon }) => {
62
+ const existingRoot = document.querySelector("[data-copilot-widget-root]");
63
+ if (existingRoot) {
64
+ existingRoot.remove();
65
+ }
66
+
67
+ const showCheckoutToast = (message) => {
68
+ const existingToast = document.querySelector("[data-copilot-checkout-toast]");
69
+ if (existingToast) {
70
+ existingToast.remove();
71
+ }
72
+
73
+ const toast = document.createElement("div");
74
+ toast.setAttribute("data-copilot-checkout-toast", "true");
75
+ toast.innerText = message;
76
+
77
+ Object.assign(toast.style, {
78
+ position: "fixed",
79
+ top: "20px",
80
+ left: "50%",
81
+ transform: "translateX(-50%)",
82
+ padding: "12px 16px",
83
+ background: "#0f172a",
84
+ color: "white",
85
+ borderRadius: "12px",
86
+ boxShadow: "0 8px 24px rgba(0,0,0,0.25)",
87
+ zIndex: 2147483647,
88
+ fontSize: "14px",
89
+ fontWeight: "600",
90
+ maxWidth: "420px",
91
+ lineHeight: "1.4",
92
+ textAlign: "center",
93
+ });
94
+
95
+ document.body.appendChild(toast);
96
+
97
+ setTimeout(() => {
98
+ toast.remove();
99
+ }, 4500);
100
+ };
66
101
 
67
102
  // ---------- Shadow DOM container ----------
68
103
  const containerHost = document.createElement("div");