copilot-chat-widget 0.1.17 → 0.1.19

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 CHANGED
@@ -1,63 +1,56 @@
1
- # Copilot Chat Widget
2
-
3
- Embeddable chat widget that can be added via NPM or a script tag.
4
-
5
- ## Installation
6
- ```bash
7
- npm install copilot-chat-widget
8
- # or
9
- yarn add copilot-chat-widget
10
- ```
11
-
12
- ## Usage (NPM import)
13
- ```js
14
- import { loadCopilotChatWidget } from "copilot-chat-widget";
15
-
1
+ # Copilot Chat Widget
2
+
3
+ Embeddable chat widget that can be added via NPM or a script tag.
4
+
5
+ ## Installation
6
+ ```bash
7
+ npm install copilot-chat-widget
8
+ # or
9
+ yarn add copilot-chat-widget
10
+ ```
11
+
12
+ ## Usage (NPM import)
13
+ ```js
14
+ import { loadCopilotChatWidget } from "copilot-chat-widget";
15
+
16
16
  loadCopilotChatWidget({
17
- token: "YOUR_WIDGET_TOKEN",
18
17
  // optional: baseUrl if backend is on a different origin
19
18
  // baseUrl: "https://your-backend-domain"
20
19
  });
21
20
  ```
22
- `loadCopilotChatWidget` injects the standalone script (`chat-widget.min.js`) and bootstraps the widget using the provided `token`.
23
-
24
- ### React/Next.js example
25
- ```jsx
26
- import { useEffect } from "react";
27
- import { loadCopilotChatWidget } from "copilot-chat-widget";
28
-
29
- export default function Page() {
21
+ `loadCopilotChatWidget` fetches widget config from `/api/chat-widget/config` and mounts the chat bubble.
22
+
23
+ ### React/Next.js example
24
+ ```jsx
25
+ import { useEffect } from "react";
26
+ import { loadCopilotChatWidget } from "copilot-chat-widget";
27
+
28
+ export default function Page() {
30
29
  useEffect(() => {
31
30
  loadCopilotChatWidget({
32
- token: process.env.NEXT_PUBLIC_CHAT_WIDGET_TOKEN,
33
31
  baseUrl: process.env.NEXT_PUBLIC_WIDGET_BASE_URL // optional override if backend is another origin
34
32
  });
35
33
  }, []);
36
-
37
- return <main>Your page content</main>;
38
- }
39
- ```
40
-
41
- ## Usage (script tag)
42
- ```html
34
+
35
+ return <main>Your page content</main>;
36
+ }
37
+ ```
38
+
39
+ ## Usage (script tag)
40
+ ```html
43
41
  <script
44
42
  src="https://cdn.example.com/chat-widget.min.js"
45
- data-token="YOUR_WIDGET_TOKEN"
46
- data-base-url="https://your-backend-domain" <!-- optional if backend is another origin -->
47
43
  ></script>
48
44
  ```
49
- - `data-token`: required.
50
- - `data-base-url`: optional override; if omitted, the widget uses the build-time default `WIDGET_BASE_URL`, then falls back to the script/page origin.
51
45
  - `data-autoload="false"`: if you want to insert the script but call `window.CopilotChat.init()` manually.
52
-
46
+
53
47
  ## Configuration
54
- - `token` (string, required): widget token.
55
48
  - `baseUrl` (string): backend host serving `/api/chat-widget/config`. Needed when the embedding page is on a different origin. If omitted, the widget uses `WIDGET_BASE_URL` (injected at build) then falls back to the script/page origin.
56
49
  - `autoload` (boolean): set `false` to defer bootstrap and call `window.CopilotChat.load()` yourself.
57
-
58
- Runtime controls (after script loads):
59
- ```js
60
- window.CopilotChat.open(); // open widget
61
- window.CopilotChat.close(); // close widget
62
- window.CopilotChat.load(); // initialize if autoload=false
63
- ```
50
+
51
+ Runtime controls (after script loads):
52
+ ```js
53
+ window.CopilotChat.open(); // open widget
54
+ window.CopilotChat.close(); // close widget
55
+ window.CopilotChat.load(); // initialize if autoload=false
56
+ ```
@@ -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,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=`
1
+ (function(){"use strict";(()=>{if(typeof window>"u"||typeof document>"u"||window.__copilotWidgetLoaded)return;window.__copilotWidgetLoaded=!0;const S="http://localhost:3000",g=64,f=720,x=document.currentScript,k=e=>{document.readyState==="complete"||document.readyState==="interactive"?setTimeout(e,0):document.addEventListener("DOMContentLoaded",e)},C=()=>x||Array.from(document.querySelectorAll("script")).reverse().find(r=>r.dataset?.token||r.src&&r.src.includes("chat-widget"))||null,I=({iframeUrl:e,launcherIcon:h})=>{const r=document.querySelector("[data-copilot-widget-root]");r&&r.remove();const a=l=>{const n=document.querySelector("[data-copilot-checkout-toast]");n&&n.remove();const d=document.createElement("div");d.setAttribute("data-copilot-checkout-toast","true"),d.innerText=l,Object.assign(d.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(d),setTimeout(()=>{d.remove()},4500)},s=document.createElement("div");s.setAttribute("data-copilot-widget-root","true");const b=s.attachShadow({mode:"open"});document.body.appendChild(s);const t=document.createElement("button");t.type="button",t.setAttribute("aria-label","Open Copilot chat"),t.innerHTML=`
2
2
  <img
3
- src="${s}"
3
+ src="${h}"
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:`${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})()})();
7
+ `,Object.assign(t.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"}),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 o=document.createElement("div");o.setAttribute("data-copilot-widget-root","true"),Object.assign(o.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 p=document.createElement("div"),E=document.createElement("div"),v=document.createElement("div");Object.assign(p.style,{position:"absolute",bottom:"-14px",right:"28px",width:"30px",height:"20px",pointerEvents:"none",display:"none",zIndex:"1"}),Object.assign(E.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(v.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"}),p.appendChild(E),p.appendChild(v);const u=document.createElement("div");Object.assign(u.style,{width:`${f}px`,maxWidth:"calc(100vw - 48px)",height:`${Math.min(f,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"}),b.appendChild(u);const i=document.createElement("iframe");i.src=e,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"}),b.appendChild(u),u.appendChild(i),o.appendChild(p),o.appendChild(u),document.body.appendChild(t),document.body.appendChild(o);let c=!1;const m=()=>{c&&(c=!1,o.style.opacity="0",o.style.transform="scale(0.8) translateY(20px)",p.style.display="none",setTimeout(()=>{o.style.display="none"},250),t.style.transform="scale(1)")},O=l=>{const{data:n,source:d}=l||{};if(n?.type){if(n.type==="CART_CHECKOUT"&&d===i.contentWindow){console.log("[CopilotChat] Received checkout payload from widget:",n),a("Checkout message received from chat widget. Check console for payload.");return}if(n.type==="WIDGET_READY"&&d===i.contentWindow){i.contentWindow.postMessage({type:"INIT_WIDGET"},"*");return}n.type==="CHAT_CLOSED"&&m()}};t.onclick=l=>{l.stopPropagation(),c=!c,c?(o.style.display="block",p.style.display="block",requestAnimationFrame(()=>{o.style.opacity="1",o.style.transform="scale(1) translateY(0)"})):m()},window.addEventListener("message",O),document.addEventListener("click",l=>{const n=l.target;n&&c&&!o.contains(n)&&n!==t&&m()});const w={close:m,open:()=>{c||t.click()}};return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.close=w.close,window.CopilotChat.open=w.open,window.CopilotChat.controls=w,w},T=e=>{console.error(`[CopilotChat] ${e}`)},A=()=>{const e=C();return e?(e.dataset||{}).autoload!=="false":!1},y=async(e={})=>{e.scriptEl||C();const h=window.CopilotChatConfig||{},r=e.baseUrl||h.baseUrl||S;try{const a=await fetch(`${r}/api/chat-widget/config`,{credentials:"omit",mode:"cors"});if(!a.ok)throw new Error(`Server responded with ${a.status}`);const s=await a.json();if(!s?.iframeUrl||!s?.launcherIcon)throw new Error("Received incomplete widget configuration from server.");return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.init=(t={})=>{const o={iframeUrl:t.iframeUrl||s.iframeUrl,launcherIcon:t.launcherIcon||s.launcherIcon};return I(o)},window.CopilotChat.init(e)}catch(a){return T(a instanceof Error?a.message:"Unknown error during widget bootstrap."),null}};A()&&k(()=>{y()}),window.CopilotChat=window.CopilotChat||{},window.CopilotChat.load=y})()})();
package/dist/index.cjs CHANGED
@@ -1,7 +1,7 @@
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=`
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const E="http://localhost:3000",w=64,y=720,k=({iframeUrl:g,launcherIcon:b})=>{const c=document.querySelector("[data-copilot-widget-root]");c&&c.remove();const m=d=>{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=d,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)},n=document.createElement("div");n.setAttribute("data-copilot-widget-root","true");const a=n.attachShadow({mode:"open"});document.body.appendChild(n);const t=document.createElement("button");t.type="button",t.setAttribute("aria-label","Open Copilot chat"),t.innerHTML=`
2
2
  <img
3
- src="${u}"
3
+ src="${b}"
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 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;
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:`${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"}),a.appendChild(p);const i=document.createElement("iframe");i.src=g,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"}),a.appendChild(p),p.appendChild(i),e.appendChild(l),e.appendChild(p),document.body.appendChild(t),document.body.appendChild(e);let s=!1;const h=()=>{s&&(s=!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=d=>{const{data:o,source:r}=d||{};if(o?.type){if(o.type==="CART_CHECKOUT"&&r===i.contentWindow){console.log("[CopilotChat] Received checkout payload from widget:",o),m("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"&&h()}};window.addEventListener("message",v),t.onclick=d=>{d.stopPropagation(),s=!s,s?(e.style.display="block",l.style.display="block",requestAnimationFrame(()=>{e.style.opacity="1",e.style.transform="scale(1) translateY(0)"})):h()},document.addEventListener("click",d=>{const o=d.target;o&&s&&!e.contains(o)&&o!==t&&h()});const u={close:h,open:()=>{s||t.click()}};return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.close=u.close,window.CopilotChat.open=u.open,window.CopilotChat.controls=u,u};async function C(g={}){if(typeof window>"u"||typeof document>"u")return null;const b=window.CopilotChatConfig||{},c=g.baseUrl||b.baseUrl||E,m=`${c.replace(/\/$/,"")}/api/chat-widget/config`;try{const n=await fetch(m,{credentials:"omit",mode:"cors"});if(!n.ok)throw new Error(`Server responded with ${n.status}`);const a=await n.json();if(!a?.iframeUrl||!a?.launcherIcon)throw new Error("Received incomplete widget configuration from server.");return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.baseUrl=c,k({iframeUrl:a.iframeUrl,launcherIcon:a.launcherIcon})}catch(n){return console.error("[CopilotChat] Error during widget bootstrap:",n instanceof Error?n.message:n),null}}exports.default=C;exports.loadCopilotChatWidget=C;
package/dist/index.mjs CHANGED
@@ -1,12 +1,12 @@
1
- const C = "http://localhost:3000";
2
- const y = ({ iframeUrl: h, launcherIcon: u }) => {
3
- const d = document.querySelector("[data-copilot-widget-root]");
4
- d && d.remove();
5
- const g = (s) => {
1
+ const y = "http://localhost:3000";
2
+ const C = ({ iframeUrl: g, launcherIcon: b }) => {
3
+ const c = document.querySelector("[data-copilot-widget-root]");
4
+ c && c.remove();
5
+ const m = (d) => {
6
6
  const o = document.querySelector("[data-copilot-checkout-toast]");
7
7
  o && o.remove();
8
8
  const r = document.createElement("div");
9
- r.setAttribute("data-copilot-checkout-toast", "true"), r.innerText = s, Object.assign(r.style, {
9
+ r.setAttribute("data-copilot-checkout-toast", "true"), r.innerText = d, Object.assign(r.style, {
10
10
  position: "fixed",
11
11
  top: "20px",
12
12
  left: "50%",
@@ -25,14 +25,14 @@ const y = ({ iframeUrl: h, launcherIcon: u }) => {
25
25
  }), document.body.appendChild(r), setTimeout(() => {
26
26
  r.remove();
27
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);
28
+ }, n = document.createElement("div");
29
+ n.setAttribute("data-copilot-widget-root", "true");
30
+ const a = n.attachShadow({ mode: "open" });
31
+ document.body.appendChild(n);
32
32
  const t = document.createElement("button");
33
33
  t.type = "button", t.setAttribute("aria-label", "Open Copilot chat"), t.innerHTML = `
34
34
  <img
35
- src="${u}"
35
+ src="${b}"
36
36
  alt="Copilot chat launcher"
37
37
  style="width: 38px; height: 38px; object-fit: contain; border-radius: 50%; pointer-events: none;"
38
38
  />
@@ -70,7 +70,7 @@ const y = ({ iframeUrl: h, launcherIcon: u }) => {
70
70
  opacity: "0",
71
71
  transition: "all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)"
72
72
  });
73
- const l = document.createElement("div"), b = document.createElement("div"), f = document.createElement("div");
73
+ const l = document.createElement("div"), w = document.createElement("div"), x = document.createElement("div");
74
74
  Object.assign(l.style, {
75
75
  position: "absolute",
76
76
  bottom: "-14px",
@@ -80,7 +80,7 @@ const y = ({ iframeUrl: h, launcherIcon: u }) => {
80
80
  pointerEvents: "none",
81
81
  display: "none",
82
82
  zIndex: "1"
83
- }), Object.assign(b.style, {
83
+ }), Object.assign(w.style, {
84
84
  position: "absolute",
85
85
  bottom: "0",
86
86
  left: "0",
@@ -91,7 +91,7 @@ const y = ({ iframeUrl: h, launcherIcon: u }) => {
91
91
  borderLeft: "15px solid transparent",
92
92
  borderRight: "15px solid transparent",
93
93
  borderTop: "15px solid rgba(15,23,42,0.1)"
94
- }), Object.assign(f.style, {
94
+ }), Object.assign(x.style, {
95
95
  position: "absolute",
96
96
  bottom: "2px",
97
97
  left: "0",
@@ -104,7 +104,7 @@ const y = ({ iframeUrl: h, launcherIcon: u }) => {
104
104
  borderTop: "13px solid white",
105
105
  boxShadow: "0 6px 16px rgba(15,23,42,0.12)",
106
106
  borderRadius: "2px"
107
- }), l.appendChild(b), l.appendChild(f);
107
+ }), l.appendChild(w), l.appendChild(x);
108
108
  const p = document.createElement("div");
109
109
  Object.assign(p.style, {
110
110
  width: "720px",
@@ -116,75 +116,68 @@ const y = ({ iframeUrl: h, launcherIcon: u }) => {
116
116
  border: "1px solid rgba(15,23,42,0.12)",
117
117
  boxShadow: "0 18px 45px rgba(15,23,42,0.16)",
118
118
  overflow: "hidden"
119
- }), n.appendChild(p);
119
+ }), a.appendChild(p);
120
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, {
121
+ i.src = g, i.title = "Copilot chat widget", i.allow = "clipboard-read; clipboard-write; microphone; camera; display-capture", i.setAttribute("scrolling", "no"), Object.assign(i.style, {
122
122
  width: "100%",
123
123
  height: "100%",
124
124
  border: "none",
125
125
  display: "block",
126
126
  background: "transparent",
127
127
  overflow: "hidden"
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(() => {
128
+ }), a.appendChild(p), p.appendChild(i), e.appendChild(l), e.appendChild(p), document.body.appendChild(t), document.body.appendChild(e);
129
+ let s = !1;
130
+ const h = () => {
131
+ s && (s = !1, e.style.opacity = "0", e.style.transform = "scale(0.8) translateY(20px)", l.style.display = "none", setTimeout(() => {
132
132
  e.style.display = "none";
133
133
  }, 250), t.style.transform = "scale(1)");
134
- }, x = (s) => {
135
- const { data: o, source: r } = s || {};
134
+ }, f = (d) => {
135
+ const { data: o, source: r } = d || {};
136
136
  if (o?.type) {
137
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.");
138
+ console.log("[CopilotChat] Received checkout payload from widget:", o), m("Checkout message received from chat widget. Check console for payload.");
139
139
  return;
140
140
  }
141
141
  if (o.type === "WIDGET_READY" && r === i.contentWindow) {
142
142
  i.contentWindow.postMessage({ type: "INIT_WIDGET" }, "*");
143
143
  return;
144
144
  }
145
- o.type === "CHAT_CLOSED" && m();
145
+ o.type === "CHAT_CLOSED" && h();
146
146
  }
147
147
  };
148
- window.addEventListener("message", x), t.onclick = (s) => {
149
- s.stopPropagation(), a = !a, a ? (e.style.display = "block", l.style.display = "block", requestAnimationFrame(() => {
148
+ window.addEventListener("message", f), t.onclick = (d) => {
149
+ d.stopPropagation(), s = !s, s ? (e.style.display = "block", l.style.display = "block", requestAnimationFrame(() => {
150
150
  e.style.opacity = "1", e.style.transform = "scale(1) translateY(0)";
151
- })) : m();
152
- }, document.addEventListener("click", (s) => {
153
- const o = s.target;
154
- o && a && !e.contains(o) && o !== t && m();
151
+ })) : h();
152
+ }, document.addEventListener("click", (d) => {
153
+ const o = d.target;
154
+ o && s && !e.contains(o) && o !== t && h();
155
155
  });
156
- const w = {
157
- close: m,
156
+ const u = {
157
+ close: h,
158
158
  open: () => {
159
- a || t.click();
159
+ s || t.click();
160
160
  }
161
161
  };
162
- return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.close = w.close, window.CopilotChat.open = w.open, window.CopilotChat.controls = w, w;
162
+ return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.close = u.close, window.CopilotChat.open = u.open, window.CopilotChat.controls = u, u;
163
163
  };
164
- async function E(h = {}) {
164
+ async function E(g = {}) {
165
165
  if (typeof window > "u" || typeof document > "u")
166
166
  return null;
167
- const u = window.CopilotChatConfig || {}, d = h.token || u.token || typeof window < "u" && window.localStorage?.getItem("copilotChatToken") || null;
168
- if (!d)
169
- return console.error(
170
- "[CopilotChat] Missing token (provide via loadCopilotChatWidget({ token }) or window.CopilotChatConfig.token)."
171
- ), null;
172
- const g = h.baseUrl || u.baseUrl || C, c = `${g.replace(/\/$/, "")}/api/chat-widget/config?token=${encodeURIComponent(
173
- d
174
- )}`;
167
+ const b = window.CopilotChatConfig || {}, c = g.baseUrl || b.baseUrl || y, m = `${c.replace(/\/$/, "")}/api/chat-widget/config`;
175
168
  try {
176
- const n = await fetch(c, {
169
+ const n = await fetch(m, {
177
170
  credentials: "omit",
178
171
  mode: "cors"
179
172
  });
180
173
  if (!n.ok)
181
174
  throw new Error(`Server responded with ${n.status}`);
182
- const t = await n.json();
183
- if (!t?.iframeUrl || !t?.launcherIcon)
175
+ const a = await n.json();
176
+ if (!a?.iframeUrl || !a?.launcherIcon)
184
177
  throw new Error("Received incomplete widget configuration from server.");
185
- return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.token = d, window.CopilotChat.baseUrl = g, y({
186
- iframeUrl: t.iframeUrl,
187
- launcherIcon: t.launcherIcon
178
+ return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.baseUrl = c, C({
179
+ iframeUrl: a.iframeUrl,
180
+ launcherIcon: a.launcherIcon
188
181
  });
189
182
  } catch (n) {
190
183
  return console.error(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "copilot-chat-widget",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
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.d.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  export interface CopilotChatOptions {
2
- token: string;
3
2
  autoload?: boolean;
4
3
  baseUrl?: string;
5
4
  }
6
-
7
- export function loadCopilotChatWidget(options: CopilotChatOptions): unknown;
8
- export default loadCopilotChatWidget;
5
+
6
+ export function loadCopilotChatWidget(options: CopilotChatOptions): unknown;
7
+ export default loadCopilotChatWidget;