copilot-chat-widget 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +94 -0
- package/dist/chat-widget.min.js +7 -0
- package/dist/index.cjs +7 -0
- package/dist/index.mjs +230 -0
- package/package.json +40 -0
- package/src/index.js +425 -0
- package/src/standalone.js +4 -0
package/README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# Copilot Chat Widget (NPM)
|
|
2
|
+
|
|
3
|
+
Widget chat Copilot nay co the nhung bang 2 cach:
|
|
4
|
+
|
|
5
|
+
1. Qua NPM (khuyen nghi cho SPA/Next.js/React/Vue).
|
|
6
|
+
2. Qua the `<script>` (tuong thich nguoc voi cach dang dung hien tai).
|
|
7
|
+
|
|
8
|
+
## Cai dat
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install copilot-chat-widget
|
|
12
|
+
# hoac
|
|
13
|
+
yarn add copilot-chat-widget
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Su dung voi NPM
|
|
17
|
+
|
|
18
|
+
### Vanilla JS hoac framework bat ky
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
import { loadCopilotChatWidget } from "copilot-chat-widget";
|
|
22
|
+
|
|
23
|
+
loadCopilotChatWidget({
|
|
24
|
+
token: "YOUR_WIDGET_TOKEN"
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`loadCopilotChatWidget` se doi DOM san san (DOMContentLoaded) roi tu khoi dong widget. Neu ban muon tu kiem soat, co the goi thang `initCopilotChatWidget` sau khi DOM san:
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
import { initCopilotChatWidget } from "copilot-chat-widget";
|
|
32
|
+
|
|
33
|
+
await initCopilotChatWidget({
|
|
34
|
+
token: "YOUR_WIDGET_TOKEN"
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### React/Next.js
|
|
39
|
+
|
|
40
|
+
```jsx
|
|
41
|
+
import { useEffect } from "react";
|
|
42
|
+
import { loadCopilotChatWidget } from "copilot-chat-widget";
|
|
43
|
+
|
|
44
|
+
export default function Page() {
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
loadCopilotChatWidget({
|
|
47
|
+
token: process.env.NEXT_PUBLIC_CHAT_WIDGET_TOKEN
|
|
48
|
+
});
|
|
49
|
+
}, []);
|
|
50
|
+
|
|
51
|
+
return <main>Your page content</main>;
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Su dung qua the `<script>` (giu nguyen hanh vi cu)
|
|
56
|
+
|
|
57
|
+
```html
|
|
58
|
+
<script
|
|
59
|
+
src="https://cdn.example.com/chat-widget.min.js"
|
|
60
|
+
data-token="YOUR_WIDGET_TOKEN"
|
|
61
|
+
></script>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
- `data-token`: bat buoc.
|
|
65
|
+
- `data-autoload="false"`: neu muon chen script nhung tu goi `window.CopilotChat.init()` sau.
|
|
66
|
+
|
|
67
|
+
## API cau hinh
|
|
68
|
+
|
|
69
|
+
- `token` (string, bat buoc): ma token widget.
|
|
70
|
+
- `force` (boolean): cho phep khoi dong lai widget neu da co instance truoc do.
|
|
71
|
+
|
|
72
|
+
Khi da khoi dong, co the goi:
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
window.CopilotChat.open(); // mo widget
|
|
76
|
+
window.CopilotChat.close(); // dong widget
|
|
77
|
+
window.CopilotChat.init(); // khoi tao lai (neu can) voi cau hinh mac dinh
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Build va publish package
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
cd webui/widget
|
|
84
|
+
npm install
|
|
85
|
+
npm run build # tao dist/index.mjs, dist/index.cjs va dist/chat-widget.min.js
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Sau khi kiem tra output trong `webui/widget/dist`, co the publish len NPM:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
npm publish --access public
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Neu muon doi ten package cho phu hop scope cong ty, sua truong `name` trong `webui/widget/package.json` truoc khi publish.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
(function(g){"use strict";const E=typeof document<"u"?document.currentScript:null,v=t=>{if(!t?.src)return{};try{const n=new URL(t.src,typeof window<"u"?window.location.href:void 0),i={};return n.searchParams.forEach((l,d)=>{i[d]=l}),i}catch(n){return console.warn("[CopilotChat] Failed to parse script query params:",n),{}}},T=t=>{typeof document>"u"||(document.readyState==="complete"||document.readyState==="interactive"?setTimeout(t,0):document.addEventListener("DOMContentLoaded",t))},I=t=>"http://localhost:3000",O=({iframeUrl:t,launcherIcon:n})=>{const i=document.querySelector("[data-copilot-widget-root]");i&&i.remove();const l=u=>{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=u,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)},d=document.createElement("div");d.setAttribute("data-copilot-widget-root","true");const f=d.attachShadow({mode:"open"});document.body.appendChild(d);const o=document.createElement("button");o.type="button",o.setAttribute("aria-label","Open Copilot chat"),o.innerHTML=`
|
|
2
|
+
<img
|
|
3
|
+
src="${n}"
|
|
4
|
+
alt="Copilot chat launcher"
|
|
5
|
+
style="width: 38px; height: 38px; object-fit: contain; border-radius: 50%; pointer-events: none;"
|
|
6
|
+
/>
|
|
7
|
+
`,Object.assign(o.style,{position:"fixed",bottom:"24px",right:"24px",width:"64px",height:"64px",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"}),o.onmouseover=()=>{o.style.transform="scale(1.12)",o.style.boxShadow="0 10px 25px rgba(0,0,0,0.3)"},o.onmouseout=()=>{o.style.transform="scale(1)",o.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:"100px",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 r=document.createElement("div"),m=document.createElement("div"),x=document.createElement("div");Object.assign(r.style,{position:"absolute",bottom:"-14px",right:"28px",width:"30px",height:"20px",pointerEvents:"none",display:"none",zIndex:"1"}),Object.assign(m.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"}),r.appendChild(m),r.appendChild(x);const c=document.createElement("div");Object.assign(c.style,{width:"720px",maxWidth:"calc(100vw - 48px)",height:`${Math.min(720,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"}),f.appendChild(c);const s=document.createElement("iframe");s.src=t,s.title="Copilot chat widget",s.allow="clipboard-read; clipboard-write; microphone; camera; display-capture",s.setAttribute("scrolling","no"),Object.assign(s.style,{width:"100%",height:"100%",border:"none",display:"block",background:"transparent",overflow:"hidden"}),f.appendChild(c),c.appendChild(s),e.appendChild(r),e.appendChild(c),document.body.appendChild(o),document.body.appendChild(e);let p=!1;const b=()=>{p&&(p=!1,e.style.opacity="0",e.style.transform="scale(0.8) translateY(20px)",r.style.display="none",setTimeout(()=>{e.style.display="none"},250),o.style.transform="scale(1)")},R=u=>{const{data:a,source:h}=u||{};if(a?.type){if(a.type==="CART_CHECKOUT"&&h===s.contentWindow){console.log("[CopilotChat] Received checkout payload from widget:",a),l("Checkout message received from chat widget. Check console for payload.");return}a.type==="CHAT_CLOSED"&&b()}};window.addEventListener("message",R),o.onclick=u=>{u.stopPropagation(),p=!p,p?(e.style.display="block",r.style.display="block",requestAnimationFrame(()=>{e.style.opacity="1",e.style.transform="scale(1) translateY(0)"})):b()},document.addEventListener("click",u=>{const a=u.target;a&&p&&!e.contains(a)&&a!==o&&b()});const C={close:b,open:()=>{p||o.click()}};return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.close=C.close,window.CopilotChat.open=C.open,window.CopilotChat.controls=C,C},w=t=>{console.error(`[CopilotChat] ${t}`)},y=()=>E||(typeof document>"u"?null:Array.from(document.querySelectorAll("script")).reverse().find(i=>i.dataset?.token||i.src&&i.src.includes("chat-widget"))||null),U=()=>{if(typeof window>"u"||typeof document>"u")return!1;const t=y();if(!t)return!1;const n=v(t),i=t.dataset||{},l=i.token||n.token,d=typeof window<"u"&&window.CopilotChatConfig&&window.CopilotChatConfig.token;return(l||d)&&i.autoload!=="false"},k=async(t={})=>{if(typeof window>"u"||typeof document>"u")return w("Window or document is not available. This widget runs in browsers only."),null;if(window.__copilotWidgetLoaded&&!t.force)return window.CopilotChat?.controls||null;window.__copilotWidgetLoaded=!0;const n=t.scriptEl||y(),i=window.CopilotChatConfig||{},l=n?.dataset||{},d=v(n),f=t.token||i.token||l.token||d.token;if(!f)return w("Missing token (provide via init config, window.CopilotChatConfig.token, or data-token attribute)."),null;const o=I();if(!o)return w("Unable to resolve base URL from embedding script or window.location."),null;try{const e=await fetch(`${o}/api/chat-widget/config?token=${encodeURIComponent(f)}`,{credentials:"omit",mode:"cors"});if(!e.ok)throw new Error(`Server responded with ${e.status}`);const r=await e.json();if(!r?.iframeUrl||!r?.launcherIcon)throw new Error("Received incomplete widget configuration from server.");const m={iframeUrl:r.iframeUrl,launcherIcon:r.launcherIcon};return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.init=(c={})=>{const s={iframeUrl:c.iframeUrl||m.iframeUrl,launcherIcon:c.launcherIcon||m.launcherIcon};return O(s)},window.CopilotChat.init(t)}catch(e){return w(e instanceof Error?e.message:"Unknown error during widget bootstrap."),null}},S=(t={})=>new Promise(n=>{if(typeof document>"u"){n(null);return}T(()=>{k(t).then(n)})});U()&&S(),g.initCopilotChatWidget=k,g.loadCopilotChatWidget=S,g.resolveEmbeddingScript=y,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})})(this.CopilotChatWidget=this.CopilotChatWidget||{});
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=64,v=720,k=typeof document<"u"?document.currentScript:null,E=t=>{if(!t?.src)return{};try{const n=new URL(t.src,typeof window<"u"?window.location.href:void 0),i={};return n.searchParams.forEach((l,d)=>{i[d]=l}),i}catch(n){return console.warn("[CopilotChat] Failed to parse script query params:",n),{}}},I=t=>{typeof document>"u"||(document.readyState==="complete"||document.readyState==="interactive"?setTimeout(t,0):document.addEventListener("DOMContentLoaded",t))},U=t=>"http://localhost:3000",R=({iframeUrl:t,launcherIcon:n})=>{const i=document.querySelector("[data-copilot-widget-root]");i&&i.remove();const l=u=>{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=u,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)},d=document.createElement("div");d.setAttribute("data-copilot-widget-root","true");const f=d.attachShadow({mode:"open"});document.body.appendChild(d);const o=document.createElement("button");o.type="button",o.setAttribute("aria-label","Open Copilot chat"),o.innerHTML=`
|
|
2
|
+
<img
|
|
3
|
+
src="${n}"
|
|
4
|
+
alt="Copilot chat launcher"
|
|
5
|
+
style="width: 38px; height: 38px; object-fit: contain; border-radius: 50%; pointer-events: none;"
|
|
6
|
+
/>
|
|
7
|
+
`,Object.assign(o.style,{position:"fixed",bottom:"24px",right:"24px",width:`${y}px`,height:`${y}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"}),o.onmouseover=()=>{o.style.transform="scale(1.12)",o.style.boxShadow="0 10px 25px rgba(0,0,0,0.3)"},o.onmouseout=()=>{o.style.transform="scale(1)",o.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:`${y+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 r=document.createElement("div"),m=document.createElement("div"),C=document.createElement("div");Object.assign(r.style,{position:"absolute",bottom:"-14px",right:"28px",width:"30px",height:"20px",pointerEvents:"none",display:"none",zIndex:"1"}),Object.assign(m.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(C.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"}),r.appendChild(m),r.appendChild(C);const c=document.createElement("div");Object.assign(c.style,{width:`${v}px`,maxWidth:"calc(100vw - 48px)",height:`${Math.min(v,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"}),f.appendChild(c);const s=document.createElement("iframe");s.src=t,s.title="Copilot chat widget",s.allow="clipboard-read; clipboard-write; microphone; camera; display-capture",s.setAttribute("scrolling","no"),Object.assign(s.style,{width:"100%",height:"100%",border:"none",display:"block",background:"transparent",overflow:"hidden"}),f.appendChild(c),c.appendChild(s),e.appendChild(r),e.appendChild(c),document.body.appendChild(o),document.body.appendChild(e);let p=!1;const w=()=>{p&&(p=!1,e.style.opacity="0",e.style.transform="scale(0.8) translateY(20px)",r.style.display="none",setTimeout(()=>{e.style.display="none"},250),o.style.transform="scale(1)")},O=u=>{const{data:a,source:h}=u||{};if(a?.type){if(a.type==="CART_CHECKOUT"&&h===s.contentWindow){console.log("[CopilotChat] Received checkout payload from widget:",a),l("Checkout message received from chat widget. Check console for payload.");return}a.type==="CHAT_CLOSED"&&w()}};window.addEventListener("message",O),o.onclick=u=>{u.stopPropagation(),p=!p,p?(e.style.display="block",r.style.display="block",requestAnimationFrame(()=>{e.style.opacity="1",e.style.transform="scale(1) translateY(0)"})):w()},document.addEventListener("click",u=>{const a=u.target;a&&p&&!e.contains(a)&&a!==o&&w()});const g={close:w,open:()=>{p||o.click()}};return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.close=g.close,window.CopilotChat.open=g.open,window.CopilotChat.controls=g,g},b=t=>{console.error(`[CopilotChat] ${t}`)},x=()=>k||(typeof document>"u"?null:Array.from(document.querySelectorAll("script")).reverse().find(i=>i.dataset?.token||i.src&&i.src.includes("chat-widget"))||null),W=()=>{if(typeof window>"u"||typeof document>"u")return!1;const t=x();if(!t)return!1;const n=E(t),i=t.dataset||{},l=i.token||n.token,d=typeof window<"u"&&window.CopilotChatConfig&&window.CopilotChatConfig.token;return(l||d)&&i.autoload!=="false"},S=async(t={})=>{if(typeof window>"u"||typeof document>"u")return b("Window or document is not available. This widget runs in browsers only."),null;if(window.__copilotWidgetLoaded&&!t.force)return window.CopilotChat?.controls||null;window.__copilotWidgetLoaded=!0;const n=t.scriptEl||x(),i=window.CopilotChatConfig||{},l=n?.dataset||{},d=E(n),f=t.token||i.token||l.token||d.token;if(!f)return b("Missing token (provide via init config, window.CopilotChatConfig.token, or data-token attribute)."),null;const o=U();if(!o)return b("Unable to resolve base URL from embedding script or window.location."),null;try{const e=await fetch(`${o}/api/chat-widget/config?token=${encodeURIComponent(f)}`,{credentials:"omit",mode:"cors"});if(!e.ok)throw new Error(`Server responded with ${e.status}`);const r=await e.json();if(!r?.iframeUrl||!r?.launcherIcon)throw new Error("Received incomplete widget configuration from server.");const m={iframeUrl:r.iframeUrl,launcherIcon:r.launcherIcon};return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.init=(c={})=>{const s={iframeUrl:c.iframeUrl||m.iframeUrl,launcherIcon:c.launcherIcon||m.launcherIcon};return R(s)},window.CopilotChat.init(t)}catch(e){return b(e instanceof Error?e.message:"Unknown error during widget bootstrap."),null}},T=(t={})=>new Promise(n=>{if(typeof document>"u"){n(null);return}I(()=>{S(t).then(n)})});W()&&T();exports.initCopilotChatWidget=S;exports.loadCopilotChatWidget=T;exports.resolveEmbeddingScript=x;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
const y = typeof document < "u" ? document.currentScript : null, x = (t) => {
|
|
2
|
+
if (!t?.src) return {};
|
|
3
|
+
try {
|
|
4
|
+
const n = new URL(t.src, typeof window < "u" ? window.location.href : void 0), i = {};
|
|
5
|
+
return n.searchParams.forEach((l, s) => {
|
|
6
|
+
i[s] = l;
|
|
7
|
+
}), i;
|
|
8
|
+
} catch (n) {
|
|
9
|
+
return console.warn("[CopilotChat] Failed to parse script query params:", n), {};
|
|
10
|
+
}
|
|
11
|
+
}, v = (t) => {
|
|
12
|
+
typeof document > "u" || (document.readyState === "complete" || document.readyState === "interactive" ? setTimeout(t, 0) : document.addEventListener("DOMContentLoaded", t));
|
|
13
|
+
}, S = (t) => "http://localhost:3000", T = ({ iframeUrl: t, launcherIcon: n }) => {
|
|
14
|
+
const i = document.querySelector("[data-copilot-widget-root]");
|
|
15
|
+
i && i.remove();
|
|
16
|
+
const l = (u) => {
|
|
17
|
+
const a = document.querySelector("[data-copilot-checkout-toast]");
|
|
18
|
+
a && a.remove();
|
|
19
|
+
const h = document.createElement("div");
|
|
20
|
+
h.setAttribute("data-copilot-checkout-toast", "true"), h.innerText = u, Object.assign(h.style, {
|
|
21
|
+
position: "fixed",
|
|
22
|
+
top: "20px",
|
|
23
|
+
left: "50%",
|
|
24
|
+
transform: "translateX(-50%)",
|
|
25
|
+
padding: "12px 16px",
|
|
26
|
+
background: "#0f172a",
|
|
27
|
+
color: "white",
|
|
28
|
+
borderRadius: "12px",
|
|
29
|
+
boxShadow: "0 8px 24px rgba(0,0,0,0.25)",
|
|
30
|
+
zIndex: 2147483647,
|
|
31
|
+
fontSize: "14px",
|
|
32
|
+
fontWeight: "600",
|
|
33
|
+
maxWidth: "420px",
|
|
34
|
+
lineHeight: "1.4",
|
|
35
|
+
textAlign: "center"
|
|
36
|
+
}), document.body.appendChild(h), setTimeout(() => {
|
|
37
|
+
h.remove();
|
|
38
|
+
}, 4500);
|
|
39
|
+
}, s = document.createElement("div");
|
|
40
|
+
s.setAttribute("data-copilot-widget-root", "true");
|
|
41
|
+
const f = s.attachShadow({ mode: "open" });
|
|
42
|
+
document.body.appendChild(s);
|
|
43
|
+
const o = document.createElement("button");
|
|
44
|
+
o.type = "button", o.setAttribute("aria-label", "Open Copilot chat"), o.innerHTML = `
|
|
45
|
+
<img
|
|
46
|
+
src="${n}"
|
|
47
|
+
alt="Copilot chat launcher"
|
|
48
|
+
style="width: 38px; height: 38px; object-fit: contain; border-radius: 50%; pointer-events: none;"
|
|
49
|
+
/>
|
|
50
|
+
`, Object.assign(o.style, {
|
|
51
|
+
position: "fixed",
|
|
52
|
+
bottom: "24px",
|
|
53
|
+
right: "24px",
|
|
54
|
+
width: "64px",
|
|
55
|
+
height: "64px",
|
|
56
|
+
borderRadius: "50%",
|
|
57
|
+
border: "none",
|
|
58
|
+
background: "linear-gradient(135deg, #0078ff, #00c6ff)",
|
|
59
|
+
color: "white",
|
|
60
|
+
cursor: "pointer",
|
|
61
|
+
zIndex: 999998,
|
|
62
|
+
display: "flex",
|
|
63
|
+
alignItems: "center",
|
|
64
|
+
justifyContent: "center",
|
|
65
|
+
boxShadow: "0 6px 14px rgba(0,0,0,0.25)",
|
|
66
|
+
transition: "all 0.25s ease"
|
|
67
|
+
}), o.onmouseover = () => {
|
|
68
|
+
o.style.transform = "scale(1.12)", o.style.boxShadow = "0 10px 25px rgba(0,0,0,0.3)";
|
|
69
|
+
}, o.onmouseout = () => {
|
|
70
|
+
o.style.transform = "scale(1)", o.style.boxShadow = "0 6px 14px rgba(0,0,0,0.25)";
|
|
71
|
+
};
|
|
72
|
+
const e = document.createElement("div");
|
|
73
|
+
e.setAttribute("data-copilot-widget-root", "true"), Object.assign(e.style, {
|
|
74
|
+
display: "none",
|
|
75
|
+
position: "fixed",
|
|
76
|
+
bottom: "100px",
|
|
77
|
+
right: "24px",
|
|
78
|
+
zIndex: "999999",
|
|
79
|
+
transformOrigin: "bottom right",
|
|
80
|
+
transform: "scale(0.8) translateY(20px)",
|
|
81
|
+
opacity: "0",
|
|
82
|
+
transition: "all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)"
|
|
83
|
+
});
|
|
84
|
+
const r = document.createElement("div"), m = document.createElement("div"), C = document.createElement("div");
|
|
85
|
+
Object.assign(r.style, {
|
|
86
|
+
position: "absolute",
|
|
87
|
+
bottom: "-14px",
|
|
88
|
+
right: "28px",
|
|
89
|
+
width: "30px",
|
|
90
|
+
height: "20px",
|
|
91
|
+
pointerEvents: "none",
|
|
92
|
+
display: "none",
|
|
93
|
+
zIndex: "1"
|
|
94
|
+
}), Object.assign(m.style, {
|
|
95
|
+
position: "absolute",
|
|
96
|
+
bottom: "0",
|
|
97
|
+
left: "0",
|
|
98
|
+
right: "0",
|
|
99
|
+
margin: "0 auto",
|
|
100
|
+
width: "0",
|
|
101
|
+
height: "0",
|
|
102
|
+
borderLeft: "15px solid transparent",
|
|
103
|
+
borderRight: "15px solid transparent",
|
|
104
|
+
borderTop: "15px solid rgba(15,23,42,0.1)"
|
|
105
|
+
}), Object.assign(C.style, {
|
|
106
|
+
position: "absolute",
|
|
107
|
+
bottom: "2px",
|
|
108
|
+
left: "0",
|
|
109
|
+
right: "0",
|
|
110
|
+
margin: "0 auto",
|
|
111
|
+
width: "0",
|
|
112
|
+
height: "0",
|
|
113
|
+
borderLeft: "13px solid transparent",
|
|
114
|
+
borderRight: "13px solid transparent",
|
|
115
|
+
borderTop: "13px solid white",
|
|
116
|
+
boxShadow: "0 6px 16px rgba(15,23,42,0.12)",
|
|
117
|
+
borderRadius: "2px"
|
|
118
|
+
}), r.appendChild(m), r.appendChild(C);
|
|
119
|
+
const c = document.createElement("div");
|
|
120
|
+
Object.assign(c.style, {
|
|
121
|
+
width: "720px",
|
|
122
|
+
maxWidth: "calc(100vw - 48px)",
|
|
123
|
+
// keep height responsive to viewport to avoid outer scrollbars
|
|
124
|
+
height: `${Math.min(720, Math.max(320, window.innerHeight - 140))}px`,
|
|
125
|
+
maxHeight: "calc(100vh - 150px)",
|
|
126
|
+
borderRadius: "20px",
|
|
127
|
+
background: "white",
|
|
128
|
+
border: "1px solid rgba(15,23,42,0.12)",
|
|
129
|
+
boxShadow: "0 18px 45px rgba(15,23,42,0.16)",
|
|
130
|
+
overflow: "hidden"
|
|
131
|
+
}), f.appendChild(c);
|
|
132
|
+
const d = document.createElement("iframe");
|
|
133
|
+
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, {
|
|
134
|
+
width: "100%",
|
|
135
|
+
height: "100%",
|
|
136
|
+
border: "none",
|
|
137
|
+
display: "block",
|
|
138
|
+
background: "transparent",
|
|
139
|
+
overflow: "hidden"
|
|
140
|
+
}), f.appendChild(c), c.appendChild(d), e.appendChild(r), e.appendChild(c), document.body.appendChild(o), document.body.appendChild(e);
|
|
141
|
+
let p = !1;
|
|
142
|
+
const w = () => {
|
|
143
|
+
p && (p = !1, e.style.opacity = "0", e.style.transform = "scale(0.8) translateY(20px)", r.style.display = "none", setTimeout(() => {
|
|
144
|
+
e.style.display = "none";
|
|
145
|
+
}, 250), o.style.transform = "scale(1)");
|
|
146
|
+
}, k = (u) => {
|
|
147
|
+
const { data: a, source: h } = u || {};
|
|
148
|
+
if (a?.type) {
|
|
149
|
+
if (a.type === "CART_CHECKOUT" && h === d.contentWindow) {
|
|
150
|
+
console.log("[CopilotChat] Received checkout payload from widget:", a), l("Checkout message received from chat widget. Check console for payload.");
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
a.type === "CHAT_CLOSED" && w();
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
window.addEventListener("message", k), o.onclick = (u) => {
|
|
157
|
+
u.stopPropagation(), p = !p, p ? (e.style.display = "block", r.style.display = "block", requestAnimationFrame(() => {
|
|
158
|
+
e.style.opacity = "1", e.style.transform = "scale(1) translateY(0)";
|
|
159
|
+
})) : w();
|
|
160
|
+
}, document.addEventListener("click", (u) => {
|
|
161
|
+
const a = u.target;
|
|
162
|
+
a && p && !e.contains(a) && a !== o && w();
|
|
163
|
+
});
|
|
164
|
+
const g = {
|
|
165
|
+
close: w,
|
|
166
|
+
open: () => {
|
|
167
|
+
p || o.click();
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.close = g.close, window.CopilotChat.open = g.open, window.CopilotChat.controls = g, g;
|
|
171
|
+
}, b = (t) => {
|
|
172
|
+
console.error(`[CopilotChat] ${t}`);
|
|
173
|
+
}, E = () => y || (typeof document > "u" ? null : Array.from(document.querySelectorAll("script")).reverse().find((i) => i.dataset?.token || i.src && i.src.includes("chat-widget")) || null), I = () => {
|
|
174
|
+
if (typeof window > "u" || typeof document > "u")
|
|
175
|
+
return !1;
|
|
176
|
+
const t = E();
|
|
177
|
+
if (!t) return !1;
|
|
178
|
+
const n = x(t), i = t.dataset || {}, l = i.token || n.token, s = typeof window < "u" && window.CopilotChatConfig && window.CopilotChatConfig.token;
|
|
179
|
+
return (l || s) && i.autoload !== "false";
|
|
180
|
+
}, O = async (t = {}) => {
|
|
181
|
+
if (typeof window > "u" || typeof document > "u")
|
|
182
|
+
return b("Window or document is not available. This widget runs in browsers only."), null;
|
|
183
|
+
if (window.__copilotWidgetLoaded && !t.force)
|
|
184
|
+
return window.CopilotChat?.controls || null;
|
|
185
|
+
window.__copilotWidgetLoaded = !0;
|
|
186
|
+
const n = t.scriptEl || E(), i = window.CopilotChatConfig || {}, l = n?.dataset || {}, s = x(n), f = t.token || i.token || l.token || s.token;
|
|
187
|
+
if (!f)
|
|
188
|
+
return b("Missing token (provide via init config, window.CopilotChatConfig.token, or data-token attribute)."), null;
|
|
189
|
+
const o = S();
|
|
190
|
+
if (!o)
|
|
191
|
+
return b("Unable to resolve base URL from embedding script or window.location."), null;
|
|
192
|
+
try {
|
|
193
|
+
const e = await fetch(`${o}/api/chat-widget/config?token=${encodeURIComponent(f)}`, {
|
|
194
|
+
credentials: "omit",
|
|
195
|
+
mode: "cors"
|
|
196
|
+
});
|
|
197
|
+
if (!e.ok)
|
|
198
|
+
throw new Error(`Server responded with ${e.status}`);
|
|
199
|
+
const r = await e.json();
|
|
200
|
+
if (!r?.iframeUrl || !r?.launcherIcon)
|
|
201
|
+
throw new Error("Received incomplete widget configuration from server.");
|
|
202
|
+
const m = {
|
|
203
|
+
iframeUrl: r.iframeUrl,
|
|
204
|
+
launcherIcon: r.launcherIcon
|
|
205
|
+
};
|
|
206
|
+
return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.init = (c = {}) => {
|
|
207
|
+
const d = {
|
|
208
|
+
iframeUrl: c.iframeUrl || m.iframeUrl,
|
|
209
|
+
launcherIcon: c.launcherIcon || m.launcherIcon
|
|
210
|
+
};
|
|
211
|
+
return T(d);
|
|
212
|
+
}, window.CopilotChat.init(t);
|
|
213
|
+
} catch (e) {
|
|
214
|
+
return b(e instanceof Error ? e.message : "Unknown error during widget bootstrap."), null;
|
|
215
|
+
}
|
|
216
|
+
}, U = (t = {}) => new Promise((n) => {
|
|
217
|
+
if (typeof document > "u") {
|
|
218
|
+
n(null);
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
v(() => {
|
|
222
|
+
O(t).then(n);
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
I() && U();
|
|
226
|
+
export {
|
|
227
|
+
O as initCopilotChatWidget,
|
|
228
|
+
U as loadCopilotChatWidget,
|
|
229
|
+
E as resolveEmbeddingScript
|
|
230
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "copilot-chat-widget",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Embeddable Copilot chat widget that can be loaded via NPM or a script tag.",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"browser": "./dist/chat-widget.min.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"browser": "./dist/chat-widget.min.js",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.cjs"
|
|
13
|
+
},
|
|
14
|
+
"./dist/chat-widget.min.js": "./dist/chat-widget.min.js"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"src"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "vite build --config ./vite.config.mjs",
|
|
22
|
+
"prepare": "npm run build"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"chat",
|
|
26
|
+
"widget",
|
|
27
|
+
"copilot",
|
|
28
|
+
"agentic",
|
|
29
|
+
"embed"
|
|
30
|
+
],
|
|
31
|
+
"author": "",
|
|
32
|
+
"license": "UNLICENSED",
|
|
33
|
+
"sideEffects": true,
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=18"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"vite": "^7.1.12"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
const BUTTON_SIZE = 64;
|
|
2
|
+
const IFRAME_SIZE = 720;
|
|
3
|
+
const embeddingScript = typeof document !== "undefined" ? document.currentScript : null;
|
|
4
|
+
|
|
5
|
+
const readQueryConfig = (scriptEl) => {
|
|
6
|
+
if (!scriptEl?.src) return {};
|
|
7
|
+
try {
|
|
8
|
+
const url = new URL(scriptEl.src, typeof window !== "undefined" ? window.location.href : undefined);
|
|
9
|
+
const params = {};
|
|
10
|
+
url.searchParams.forEach((value, key) => {
|
|
11
|
+
params[key] = value;
|
|
12
|
+
});
|
|
13
|
+
return params;
|
|
14
|
+
} catch (error) {
|
|
15
|
+
console.warn("[CopilotChat] Failed to parse script query params:", error);
|
|
16
|
+
return {};
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const ready = (fn) => {
|
|
21
|
+
if (typeof document === "undefined") return;
|
|
22
|
+
if (document.readyState === "complete" || document.readyState === "interactive") {
|
|
23
|
+
setTimeout(fn, 0);
|
|
24
|
+
} else {
|
|
25
|
+
document.addEventListener("DOMContentLoaded", fn);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const inferBaseUrlFromScript = (scriptEl) => {
|
|
30
|
+
if (!scriptEl?.src || typeof window === "undefined") return null;
|
|
31
|
+
try {
|
|
32
|
+
const srcUrl = new URL(scriptEl.src, window.location.href);
|
|
33
|
+
return `${srcUrl.protocol}//${srcUrl.host}`;
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.warn("[CopilotChat] Unable to infer base URL from script source.", error);
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const resolveBaseUrl = (scriptEl) => {
|
|
41
|
+
// Use the build-time defined base URL from VITE_CHAT_WIDGET_BASE_URL
|
|
42
|
+
if (typeof CHAT_WIDGET_BASE_URL !== "undefined") {
|
|
43
|
+
return CHAT_WIDGET_BASE_URL;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Fallback to inferred URL from script
|
|
47
|
+
const inferred = inferBaseUrlFromScript(scriptEl);
|
|
48
|
+
if (inferred) return inferred;
|
|
49
|
+
|
|
50
|
+
// Final fallback to window location
|
|
51
|
+
if (typeof window !== "undefined" && window.location?.origin) {
|
|
52
|
+
return window.location.origin;
|
|
53
|
+
}
|
|
54
|
+
return null;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const buildWidget = ({ iframeUrl, launcherIcon }) => {
|
|
58
|
+
const existingRoot = document.querySelector("[data-copilot-widget-root]");
|
|
59
|
+
if (existingRoot) {
|
|
60
|
+
existingRoot.remove();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const showCheckoutToast = (message) => {
|
|
64
|
+
const existingToast = document.querySelector("[data-copilot-checkout-toast]");
|
|
65
|
+
if (existingToast) {
|
|
66
|
+
existingToast.remove();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const toast = document.createElement("div");
|
|
70
|
+
toast.setAttribute("data-copilot-checkout-toast", "true");
|
|
71
|
+
toast.innerText = message;
|
|
72
|
+
|
|
73
|
+
Object.assign(toast.style, {
|
|
74
|
+
position: "fixed",
|
|
75
|
+
top: "20px",
|
|
76
|
+
left: "50%",
|
|
77
|
+
transform: "translateX(-50%)",
|
|
78
|
+
padding: "12px 16px",
|
|
79
|
+
background: "#0f172a",
|
|
80
|
+
color: "white",
|
|
81
|
+
borderRadius: "12px",
|
|
82
|
+
boxShadow: "0 8px 24px rgba(0,0,0,0.25)",
|
|
83
|
+
zIndex: 2147483647,
|
|
84
|
+
fontSize: "14px",
|
|
85
|
+
fontWeight: "600",
|
|
86
|
+
maxWidth: "420px",
|
|
87
|
+
lineHeight: "1.4",
|
|
88
|
+
textAlign: "center",
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
document.body.appendChild(toast);
|
|
92
|
+
|
|
93
|
+
setTimeout(() => {
|
|
94
|
+
toast.remove();
|
|
95
|
+
}, 4500);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// ---------- Shadow DOM container ----------
|
|
99
|
+
const containerHost = document.createElement("div");
|
|
100
|
+
containerHost.setAttribute("data-copilot-widget-root", "true");
|
|
101
|
+
const shadow = containerHost.attachShadow({ mode: "open" });
|
|
102
|
+
document.body.appendChild(containerHost);
|
|
103
|
+
|
|
104
|
+
// ---------- Button ----------
|
|
105
|
+
const btn = document.createElement("button");
|
|
106
|
+
btn.type = "button";
|
|
107
|
+
btn.setAttribute("aria-label", "Open Copilot chat");
|
|
108
|
+
btn.innerHTML = `
|
|
109
|
+
<img
|
|
110
|
+
src="${launcherIcon}"
|
|
111
|
+
alt="Copilot chat launcher"
|
|
112
|
+
style="width: 38px; height: 38px; object-fit: contain; border-radius: 50%; pointer-events: none;"
|
|
113
|
+
/>
|
|
114
|
+
`;
|
|
115
|
+
|
|
116
|
+
Object.assign(btn.style, {
|
|
117
|
+
position: "fixed",
|
|
118
|
+
bottom: "24px",
|
|
119
|
+
right: "24px",
|
|
120
|
+
width: `${BUTTON_SIZE}px`,
|
|
121
|
+
height: `${BUTTON_SIZE}px`,
|
|
122
|
+
borderRadius: "50%",
|
|
123
|
+
border: "none",
|
|
124
|
+
background: "linear-gradient(135deg, #0078ff, #00c6ff)",
|
|
125
|
+
color: "white",
|
|
126
|
+
cursor: "pointer",
|
|
127
|
+
zIndex: 999998,
|
|
128
|
+
display: "flex",
|
|
129
|
+
alignItems: "center",
|
|
130
|
+
justifyContent: "center",
|
|
131
|
+
boxShadow: "0 6px 14px rgba(0,0,0,0.25)",
|
|
132
|
+
transition: "all 0.25s ease",
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
btn.onmouseover = () => {
|
|
136
|
+
btn.style.transform = "scale(1.12)";
|
|
137
|
+
btn.style.boxShadow = "0 10px 25px rgba(0,0,0,0.3)";
|
|
138
|
+
};
|
|
139
|
+
btn.onmouseout = () => {
|
|
140
|
+
btn.style.transform = "scale(1)";
|
|
141
|
+
btn.style.boxShadow = "0 6px 14px rgba(0,0,0,0.25)";
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const container = document.createElement("div");
|
|
145
|
+
container.setAttribute("data-copilot-widget-root", "true");
|
|
146
|
+
Object.assign(container.style, {
|
|
147
|
+
display: "none",
|
|
148
|
+
position: "fixed",
|
|
149
|
+
bottom: `${BUTTON_SIZE + 36}px`,
|
|
150
|
+
right: "24px",
|
|
151
|
+
zIndex: "999999",
|
|
152
|
+
transformOrigin: "bottom right",
|
|
153
|
+
transform: "scale(0.8) translateY(20px)",
|
|
154
|
+
opacity: "0",
|
|
155
|
+
transition: "all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)",
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const arrow = document.createElement("div");
|
|
159
|
+
const arrowBorder = document.createElement("div");
|
|
160
|
+
const arrowFill = document.createElement("div");
|
|
161
|
+
|
|
162
|
+
Object.assign(arrow.style, {
|
|
163
|
+
position: "absolute",
|
|
164
|
+
bottom: "-14px",
|
|
165
|
+
right: "28px",
|
|
166
|
+
width: "30px",
|
|
167
|
+
height: "20px",
|
|
168
|
+
pointerEvents: "none",
|
|
169
|
+
display: "none",
|
|
170
|
+
zIndex: "1",
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
Object.assign(arrowBorder.style, {
|
|
174
|
+
position: "absolute",
|
|
175
|
+
bottom: "0",
|
|
176
|
+
left: "0",
|
|
177
|
+
right: "0",
|
|
178
|
+
margin: "0 auto",
|
|
179
|
+
width: "0",
|
|
180
|
+
height: "0",
|
|
181
|
+
borderLeft: "15px solid transparent",
|
|
182
|
+
borderRight: "15px solid transparent",
|
|
183
|
+
borderTop: "15px solid rgba(15,23,42,0.1)",
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
Object.assign(arrowFill.style, {
|
|
187
|
+
position: "absolute",
|
|
188
|
+
bottom: "2px",
|
|
189
|
+
left: "0",
|
|
190
|
+
right: "0",
|
|
191
|
+
margin: "0 auto",
|
|
192
|
+
width: "0",
|
|
193
|
+
height: "0",
|
|
194
|
+
borderLeft: "13px solid transparent",
|
|
195
|
+
borderRight: "13px solid transparent",
|
|
196
|
+
borderTop: "13px solid white",
|
|
197
|
+
boxShadow: "0 6px 16px rgba(15,23,42,0.12)",
|
|
198
|
+
borderRadius: "2px",
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
arrow.appendChild(arrowBorder);
|
|
202
|
+
arrow.appendChild(arrowFill);
|
|
203
|
+
|
|
204
|
+
const frameWrapper = document.createElement("div");
|
|
205
|
+
Object.assign(frameWrapper.style, {
|
|
206
|
+
width: `${IFRAME_SIZE}px`,
|
|
207
|
+
maxWidth: "calc(100vw - 48px)",
|
|
208
|
+
// keep height responsive to viewport to avoid outer scrollbars
|
|
209
|
+
height: `${Math.min(IFRAME_SIZE, Math.max(320, window.innerHeight - 140))}px`,
|
|
210
|
+
maxHeight: "calc(100vh - 150px)",
|
|
211
|
+
borderRadius: "20px",
|
|
212
|
+
background: "white",
|
|
213
|
+
border: "1px solid rgba(15,23,42,0.12)",
|
|
214
|
+
boxShadow: "0 18px 45px rgba(15,23,42,0.16)",
|
|
215
|
+
overflow: "hidden",
|
|
216
|
+
});
|
|
217
|
+
shadow.appendChild(frameWrapper);
|
|
218
|
+
|
|
219
|
+
const iframe = document.createElement("iframe");
|
|
220
|
+
iframe.src = iframeUrl;
|
|
221
|
+
iframe.title = "Copilot chat widget";
|
|
222
|
+
iframe.allow = "clipboard-read; clipboard-write; microphone; camera; display-capture";
|
|
223
|
+
iframe.setAttribute("scrolling", "no");
|
|
224
|
+
Object.assign(iframe.style, {
|
|
225
|
+
width: "100%",
|
|
226
|
+
height: "100%",
|
|
227
|
+
border: "none",
|
|
228
|
+
display: "block",
|
|
229
|
+
background: "transparent",
|
|
230
|
+
overflow: "hidden",
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
shadow.appendChild(frameWrapper);
|
|
234
|
+
frameWrapper.appendChild(iframe);
|
|
235
|
+
|
|
236
|
+
container.appendChild(arrow);
|
|
237
|
+
container.appendChild(frameWrapper);
|
|
238
|
+
document.body.appendChild(btn);
|
|
239
|
+
document.body.appendChild(container);
|
|
240
|
+
|
|
241
|
+
let isOpen = false;
|
|
242
|
+
|
|
243
|
+
const closeChat = () => {
|
|
244
|
+
if (!isOpen) return;
|
|
245
|
+
isOpen = false;
|
|
246
|
+
container.style.opacity = "0";
|
|
247
|
+
container.style.transform = "scale(0.8) translateY(20px)";
|
|
248
|
+
arrow.style.display = "none";
|
|
249
|
+
setTimeout(() => {
|
|
250
|
+
container.style.display = "none";
|
|
251
|
+
}, 250);
|
|
252
|
+
btn.style.transform = "scale(1)";
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
const handleWidgetMessage = (event) => {
|
|
256
|
+
const { data, source } = event || {};
|
|
257
|
+
if (!data?.type) return;
|
|
258
|
+
|
|
259
|
+
if (data.type === "CART_CHECKOUT" && source === iframe.contentWindow) {
|
|
260
|
+
console.log("[CopilotChat] Received checkout payload from widget:", data);
|
|
261
|
+
showCheckoutToast("Checkout message received from chat widget. Check console for payload.");
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (data.type === "CHAT_CLOSED") {
|
|
266
|
+
closeChat();
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
window.addEventListener("message", handleWidgetMessage);
|
|
271
|
+
|
|
272
|
+
btn.onclick = (event) => {
|
|
273
|
+
event.stopPropagation();
|
|
274
|
+
isOpen = !isOpen;
|
|
275
|
+
if (isOpen) {
|
|
276
|
+
container.style.display = "block";
|
|
277
|
+
arrow.style.display = "block";
|
|
278
|
+
requestAnimationFrame(() => {
|
|
279
|
+
container.style.opacity = "1";
|
|
280
|
+
container.style.transform = "scale(1) translateY(0)";
|
|
281
|
+
});
|
|
282
|
+
} else {
|
|
283
|
+
closeChat();
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
document.addEventListener("click", (event) => {
|
|
288
|
+
const target = event.target;
|
|
289
|
+
if (!target) return;
|
|
290
|
+
if (isOpen && !container.contains(target) && target !== btn) {
|
|
291
|
+
closeChat();
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
const controls = {
|
|
296
|
+
close: closeChat,
|
|
297
|
+
open: () => {
|
|
298
|
+
if (!isOpen) {
|
|
299
|
+
btn.click();
|
|
300
|
+
}
|
|
301
|
+
},
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
window.CopilotChat = window.CopilotChat || {};
|
|
305
|
+
window.CopilotChat.close = controls.close;
|
|
306
|
+
window.CopilotChat.open = controls.open;
|
|
307
|
+
window.CopilotChat.controls = controls;
|
|
308
|
+
|
|
309
|
+
return controls;
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
const handleError = (message) => {
|
|
313
|
+
console.error(`[CopilotChat] ${message}`);
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
export const resolveEmbeddingScript = () => {
|
|
317
|
+
if (embeddingScript) {
|
|
318
|
+
return embeddingScript;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (typeof document === "undefined") return null;
|
|
322
|
+
|
|
323
|
+
const scripts = Array.from(document.querySelectorAll("script"));
|
|
324
|
+
const candidate = scripts
|
|
325
|
+
.reverse()
|
|
326
|
+
.find((script) => script.dataset?.token || (script.src && script.src.includes("chat-widget")));
|
|
327
|
+
|
|
328
|
+
return candidate || null;
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
const shouldAutoBootstrapFromScript = () => {
|
|
332
|
+
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
333
|
+
return false;
|
|
334
|
+
}
|
|
335
|
+
const scriptEl = resolveEmbeddingScript();
|
|
336
|
+
if (!scriptEl) return false;
|
|
337
|
+
const queryConfig = readQueryConfig(scriptEl);
|
|
338
|
+
const dataset = scriptEl.dataset || {};
|
|
339
|
+
const hasTokenHint = dataset.token || queryConfig.token;
|
|
340
|
+
const hasGlobalToken = typeof window !== "undefined" && window.CopilotChatConfig && window.CopilotChatConfig.token;
|
|
341
|
+
return (hasTokenHint || hasGlobalToken) && dataset.autoload !== "false";
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
export const initCopilotChatWidget = async (config = {}) => {
|
|
345
|
+
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
346
|
+
handleError("Window or document is not available. This widget runs in browsers only.");
|
|
347
|
+
return null;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (window.__copilotWidgetLoaded && !config.force) {
|
|
351
|
+
return window.CopilotChat?.controls || null;
|
|
352
|
+
}
|
|
353
|
+
window.__copilotWidgetLoaded = true;
|
|
354
|
+
|
|
355
|
+
const scriptEl = config.scriptEl || resolveEmbeddingScript();
|
|
356
|
+
const globalConfig = window.CopilotChatConfig || {};
|
|
357
|
+
const datasetConfig = scriptEl?.dataset || {};
|
|
358
|
+
const queryConfig = readQueryConfig(scriptEl);
|
|
359
|
+
|
|
360
|
+
const token = config.token || globalConfig.token || datasetConfig.token || queryConfig.token;
|
|
361
|
+
|
|
362
|
+
if (!token) {
|
|
363
|
+
handleError("Missing token (provide via init config, window.CopilotChatConfig.token, or data-token attribute).");
|
|
364
|
+
return null;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const baseUrl = resolveBaseUrl(scriptEl);
|
|
368
|
+
|
|
369
|
+
if (!baseUrl) {
|
|
370
|
+
handleError("Unable to resolve base URL from embedding script or window.location.");
|
|
371
|
+
return null;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
try {
|
|
375
|
+
const response = await fetch(`${baseUrl}/api/chat-widget/config?token=${encodeURIComponent(token)}`, {
|
|
376
|
+
credentials: "omit",
|
|
377
|
+
mode: "cors",
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
if (!response.ok) {
|
|
381
|
+
throw new Error(`Server responded with ${response.status}`);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const remoteConfig = await response.json();
|
|
385
|
+
if (!remoteConfig?.iframeUrl || !remoteConfig?.launcherIcon) {
|
|
386
|
+
throw new Error("Received incomplete widget configuration from server.");
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const baseConfig = {
|
|
390
|
+
iframeUrl: remoteConfig.iframeUrl,
|
|
391
|
+
launcherIcon: remoteConfig.launcherIcon,
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
window.CopilotChat = window.CopilotChat || {};
|
|
395
|
+
window.CopilotChat.init = (overrides = {}) => {
|
|
396
|
+
const mergedConfig = {
|
|
397
|
+
iframeUrl: overrides.iframeUrl || baseConfig.iframeUrl,
|
|
398
|
+
launcherIcon: overrides.launcherIcon || baseConfig.launcherIcon,
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
return buildWidget(mergedConfig);
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
const controls = window.CopilotChat.init(config);
|
|
405
|
+
return controls;
|
|
406
|
+
} catch (error) {
|
|
407
|
+
handleError(error instanceof Error ? error.message : "Unknown error during widget bootstrap.");
|
|
408
|
+
return null;
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
export const loadCopilotChatWidget = (config = {}) =>
|
|
413
|
+
new Promise((resolve) => {
|
|
414
|
+
if (typeof document === "undefined") {
|
|
415
|
+
resolve(null);
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
ready(() => {
|
|
419
|
+
initCopilotChatWidget(config).then(resolve);
|
|
420
|
+
});
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
if (shouldAutoBootstrapFromScript()) {
|
|
424
|
+
loadCopilotChatWidget();
|
|
425
|
+
}
|