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