copilot-chat-widget 0.1.18 → 0.1.20
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 +16 -27
- package/dist/chat-widget.min.js +3 -3
- package/dist/index.cjs +3 -3
- package/dist/index.mjs +50 -57
- package/package.json +1 -1
- package/src/index.d.ts +3 -5
- package/src/index.js +13 -31
- package/src/widget-runtime.js +22 -79
package/README.md
CHANGED
|
@@ -13,13 +13,10 @@ yarn add copilot-chat-widget
|
|
|
13
13
|
```js
|
|
14
14
|
import { loadCopilotChatWidget } from "copilot-chat-widget";
|
|
15
15
|
|
|
16
|
-
loadCopilotChatWidget({
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
});
|
|
21
|
-
```
|
|
22
|
-
`loadCopilotChatWidget` injects the standalone script (`chat-widget.min.js`) and bootstraps the widget using the provided `token`.
|
|
16
|
+
loadCopilotChatWidget({
|
|
17
|
+
});
|
|
18
|
+
```
|
|
19
|
+
`loadCopilotChatWidget` fetches widget config from `/api/chat-widget/config` and mounts the chat bubble.
|
|
23
20
|
|
|
24
21
|
### React/Next.js example
|
|
25
22
|
```jsx
|
|
@@ -27,12 +24,10 @@ import { useEffect } from "react";
|
|
|
27
24
|
import { loadCopilotChatWidget } from "copilot-chat-widget";
|
|
28
25
|
|
|
29
26
|
export default function Page() {
|
|
30
|
-
useEffect(() => {
|
|
31
|
-
loadCopilotChatWidget({
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
});
|
|
35
|
-
}, []);
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
loadCopilotChatWidget({
|
|
29
|
+
});
|
|
30
|
+
}, []);
|
|
36
31
|
|
|
37
32
|
return <main>Your page content</main>;
|
|
38
33
|
}
|
|
@@ -40,20 +35,14 @@ export default function Page() {
|
|
|
40
35
|
|
|
41
36
|
## Usage (script tag)
|
|
42
37
|
```html
|
|
43
|
-
<script
|
|
44
|
-
src="https://cdn.example.com/chat-widget.min.js"
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
- `
|
|
51
|
-
- `data-autoload="false"`: if you want to insert the script but call `window.CopilotChat.init()` manually.
|
|
52
|
-
|
|
53
|
-
## Configuration
|
|
54
|
-
- `token` (string, required): widget token.
|
|
55
|
-
- `baseUrl` (string): backend host serving `/api/chat-widget/config`. Needed when the embedding page is on a different origin. If omitted, the widget uses `WIDGET_BASE_URL` (injected at build) then falls back to the script/page origin.
|
|
56
|
-
- `autoload` (boolean): set `false` to defer bootstrap and call `window.CopilotChat.load()` yourself.
|
|
38
|
+
<script
|
|
39
|
+
src="https://cdn.example.com/chat-widget.min.js"
|
|
40
|
+
></script>
|
|
41
|
+
```
|
|
42
|
+
- `data-autoload="false"`: if you want to insert the script but call `window.CopilotChat.init()` manually.
|
|
43
|
+
|
|
44
|
+
## Configuration
|
|
45
|
+
- `autoload` (boolean): set `false` to defer bootstrap and call `window.CopilotChat.load()` yourself.
|
|
57
46
|
|
|
58
47
|
Runtime controls (after script loads):
|
|
59
48
|
```js
|
package/dist/chat-widget.min.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
(function(){"use strict";(()=>{if(typeof window>"u"||typeof document>"u"||window.__copilotWidgetLoaded)return;window.__copilotWidgetLoaded=!0;const
|
|
1
|
+
(function(){"use strict";(()=>{if(typeof window>"u"||typeof document>"u"||window.__copilotWidgetLoaded)return;window.__copilotWidgetLoaded=!0;const S="http://localhost:3000",b=64,f=720,x=document.currentScript,k=e=>{document.readyState==="complete"||document.readyState==="interactive"?setTimeout(e,0):document.addEventListener("DOMContentLoaded",e)},y=()=>x||Array.from(document.querySelectorAll("script")).reverse().find(o=>o.dataset?.token||o.src&&o.src.includes("chat-widget"))||null,I=({iframeUrl:e,launcherIcon:p})=>{const o=document.querySelector("[data-copilot-widget-root]");o&&o.remove();const c=d=>{const i=document.querySelector("[data-copilot-checkout-toast]");i&&i.remove();const a=document.createElement("div");a.setAttribute("data-copilot-checkout-toast","true"),a.innerText=d,Object.assign(a.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(a),setTimeout(()=>{a.remove()},4500)},m=document.createElement("div");m.setAttribute("data-copilot-widget-root","true");const h=m.attachShadow({mode:"open"});document.body.appendChild(m);const t=document.createElement("button");t.type="button",t.setAttribute("aria-label","Open Copilot chat"),t.innerHTML=`
|
|
2
2
|
<img
|
|
3
|
-
src="${
|
|
3
|
+
src="${p}"
|
|
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(
|
|
7
|
+
`,Object.assign(t.style,{position:"fixed",bottom:"24px",right:"24px",width:`${b}px`,height:`${b}px`,borderRadius:"50%",border:"none",background:"linear-gradient(135deg, #0078ff, #00c6ff)",color:"white",cursor:"pointer",zIndex:999998,display:"flex",alignItems:"center",justifyContent:"center",boxShadow:"0 6px 14px rgba(0,0,0,0.25)",transition:"all 0.25s ease"}),t.onmouseover=()=>{t.style.transform="scale(1.12)",t.style.boxShadow="0 10px 25px rgba(0,0,0,0.3)"},t.onmouseout=()=>{t.style.transform="scale(1)",t.style.boxShadow="0 6px 14px rgba(0,0,0,0.25)"};const n=document.createElement("div");n.setAttribute("data-copilot-widget-root","true"),Object.assign(n.style,{display:"none",position:"fixed",bottom:`${b+36}px`,right:"24px",zIndex:"999999",transformOrigin:"bottom right",transform:"scale(0.8) translateY(20px)",opacity:"0",transition:"all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)"});const l=document.createElement("div"),E=document.createElement("div"),v=document.createElement("div");Object.assign(l.style,{position:"absolute",bottom:"-14px",right:"28px",width:"30px",height:"20px",pointerEvents:"none",display:"none",zIndex:"1"}),Object.assign(E.style,{position:"absolute",bottom:"0",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"15px solid transparent",borderRight:"15px solid transparent",borderTop:"15px solid rgba(15,23,42,0.1)"}),Object.assign(v.style,{position:"absolute",bottom:"2px",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"13px solid transparent",borderRight:"13px solid transparent",borderTop:"13px solid white",boxShadow:"0 6px 16px rgba(15,23,42,0.12)",borderRadius:"2px"}),l.appendChild(E),l.appendChild(v);const u=document.createElement("div");Object.assign(u.style,{width:`${f}px`,maxWidth:"calc(100vw - 48px)",height:`${Math.min(f,Math.max(320,window.innerHeight-140))}px`,maxHeight:"calc(100vh - 150px)",borderRadius:"20px",background:"white",border:"1px solid rgba(15,23,42,0.12)",boxShadow:"0 18px 45px rgba(15,23,42,0.16)",overflow:"hidden"}),h.appendChild(u);const r=document.createElement("iframe");r.src=e,r.title="Copilot chat widget",r.allow="clipboard-read; clipboard-write; microphone; camera; display-capture",r.setAttribute("scrolling","no"),Object.assign(r.style,{width:"100%",height:"100%",border:"none",display:"block",background:"transparent",overflow:"hidden"}),h.appendChild(u),u.appendChild(r),n.appendChild(l),n.appendChild(u),document.body.appendChild(t),document.body.appendChild(n);let s=!1;const w=()=>{s&&(s=!1,n.style.opacity="0",n.style.transform="scale(0.8) translateY(20px)",l.style.display="none",setTimeout(()=>{n.style.display="none"},250),t.style.transform="scale(1)")},O=d=>{const{data:i,source:a}=d||{};if(i?.type){if(i.type==="CART_CHECKOUT"&&a===r.contentWindow){console.log("[CopilotChat] Received checkout payload from widget:",i),c("Checkout message received from chat widget. Check console for payload.");return}if(i.type==="WIDGET_READY"&&a===r.contentWindow){r.contentWindow.postMessage({type:"INIT_WIDGET"},"*");return}i.type==="CHAT_CLOSED"&&w()}};t.onclick=d=>{d.stopPropagation(),s=!s,s?(n.style.display="block",l.style.display="block",requestAnimationFrame(()=>{n.style.opacity="1",n.style.transform="scale(1) translateY(0)"})):w()},window.addEventListener("message",O),document.addEventListener("click",d=>{const i=d.target;i&&s&&!n.contains(i)&&i!==t&&w()});const g={close:w,open:()=>{s||t.click()}};return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.close=g.close,window.CopilotChat.open=g.open,window.CopilotChat.controls=g,g},T=e=>{console.error(`[CopilotChat] ${e}`)},A=()=>{const e=y();return e?(e.dataset||{}).autoload!=="false":!1},C=async(e={})=>{e.scriptEl||y();const p=S;try{const o=await fetch(`${p}/api/chat-widget/config`,{credentials:"omit",mode:"cors"});if(!o.ok)throw new Error(`Server responded with ${o.status}`);const c=await o.json();if(!c?.iframeUrl||!c?.launcherIcon)throw new Error("Received incomplete widget configuration from server.");return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.init=(h={})=>{const t={iframeUrl:h.iframeUrl||c.iframeUrl,launcherIcon:h.launcherIcon||c.launcherIcon};return I(t)},window.CopilotChat.init(e)}catch(o){return T(o instanceof Error?o.message:"Unknown error during widget bootstrap."),null}};A()&&k(()=>{C()}),window.CopilotChat=window.CopilotChat||{},window.CopilotChat.load=C})()})();
|
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const E="http://localhost:3000",m=64,y=720,k=({iframeUrl:b,launcherIcon:p})=>{const h=document.querySelector("[data-copilot-widget-root]");h&&h.remove();const n=d=>{const o=document.querySelector("[data-copilot-checkout-toast]");o&&o.remove();const a=document.createElement("div");a.setAttribute("data-copilot-checkout-toast","true"),a.innerText=d,Object.assign(a.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(a),setTimeout(()=>{a.remove()},4500)},r=document.createElement("div");r.setAttribute("data-copilot-widget-root","true");const w=r.attachShadow({mode:"open"});document.body.appendChild(r);const t=document.createElement("button");t.type="button",t.setAttribute("aria-label","Open Copilot chat"),t.innerHTML=`
|
|
2
2
|
<img
|
|
3
|
-
src="${
|
|
3
|
+
src="${p}"
|
|
4
4
|
alt="Copilot chat launcher"
|
|
5
5
|
style="width: 38px; height: 38px; object-fit: contain; border-radius: 50%; pointer-events: none;"
|
|
6
6
|
/>
|
|
7
|
-
`,Object.assign(t.style,{position:"fixed",bottom:"24px",right:"24px",width:`${
|
|
7
|
+
`,Object.assign(t.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"}),t.onmouseover=()=>{t.style.transform="scale(1.12)",t.style.boxShadow="0 10px 25px rgba(0,0,0,0.3)"},t.onmouseout=()=>{t.style.transform="scale(1)",t.style.boxShadow="0 6px 14px rgba(0,0,0,0.25)"};const e=document.createElement("div");e.setAttribute("data-copilot-widget-root","true"),Object.assign(e.style,{display:"none",position:"fixed",bottom:`${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 l=document.createElement("div"),x=document.createElement("div"),f=document.createElement("div");Object.assign(l.style,{position:"absolute",bottom:"-14px",right:"28px",width:"30px",height:"20px",pointerEvents:"none",display:"none",zIndex:"1"}),Object.assign(x.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(f.style,{position:"absolute",bottom:"2px",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"13px solid transparent",borderRight:"13px solid transparent",borderTop:"13px solid white",boxShadow:"0 6px 16px rgba(15,23,42,0.12)",borderRadius:"2px"}),l.appendChild(x),l.appendChild(f);const c=document.createElement("div");Object.assign(c.style,{width:`${y}px`,maxWidth:"calc(100vw - 48px)",height:`${Math.min(y,Math.max(320,window.innerHeight-140))}px`,maxHeight:"calc(100vh - 150px)",borderRadius:"20px",background:"white",border:"1px solid rgba(15,23,42,0.12)",boxShadow:"0 18px 45px rgba(15,23,42,0.16)",overflow:"hidden"}),w.appendChild(c);const i=document.createElement("iframe");i.src=b,i.title="Copilot chat widget",i.allow="clipboard-read; clipboard-write; microphone; camera; display-capture",i.setAttribute("scrolling","no"),Object.assign(i.style,{width:"100%",height:"100%",border:"none",display:"block",background:"transparent",overflow:"hidden"}),w.appendChild(c),c.appendChild(i),e.appendChild(l),e.appendChild(c),document.body.appendChild(t),document.body.appendChild(e);let s=!1;const u=()=>{s&&(s=!1,e.style.opacity="0",e.style.transform="scale(0.8) translateY(20px)",l.style.display="none",setTimeout(()=>{e.style.display="none"},250),t.style.transform="scale(1)")},v=d=>{const{data:o,source:a}=d||{};if(o?.type){if(o.type==="CART_CHECKOUT"&&a===i.contentWindow){console.log("[CopilotChat] Received checkout payload from widget:",o),n("Checkout message received from chat widget. Check console for payload.");return}if(o.type==="WIDGET_READY"&&a===i.contentWindow){i.contentWindow.postMessage({type:"INIT_WIDGET"},"*");return}o.type==="CHAT_CLOSED"&&u()}};window.addEventListener("message",v),t.onclick=d=>{d.stopPropagation(),s=!s,s?(e.style.display="block",l.style.display="block",requestAnimationFrame(()=>{e.style.opacity="1",e.style.transform="scale(1) translateY(0)"})):u()},document.addEventListener("click",d=>{const o=d.target;o&&s&&!e.contains(o)&&o!==t&&u()});const g={close:u,open:()=>{s||t.click()}};return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.close=g.close,window.CopilotChat.open=g.open,window.CopilotChat.controls=g,g};async function C(b={}){if(typeof window>"u"||typeof document>"u")return null;const p=E,h=`${p.replace(/\/$/,"")}/api/chat-widget/config`;try{const n=await fetch(h,{credentials:"omit",mode:"cors"});if(!n.ok)throw new Error(`Server responded with ${n.status}`);const r=await n.json();if(!r?.iframeUrl||!r?.launcherIcon)throw new Error("Received incomplete widget configuration from server.");return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.baseUrl=p,k({iframeUrl:r.iframeUrl,launcherIcon:r.launcherIcon})}catch(n){return console.error("[CopilotChat] Error during widget bootstrap:",n instanceof Error?n.message:n),null}}exports.default=C;exports.loadCopilotChatWidget=C;
|
package/dist/index.mjs
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
const
|
|
1
|
+
const y = "http://localhost:3000";
|
|
2
|
+
const C = ({ iframeUrl: g, launcherIcon: p }) => {
|
|
3
|
+
const h = document.querySelector("[data-copilot-widget-root]");
|
|
4
|
+
h && h.remove();
|
|
5
|
+
const n = (d) => {
|
|
6
6
|
const o = document.querySelector("[data-copilot-checkout-toast]");
|
|
7
7
|
o && o.remove();
|
|
8
|
-
const
|
|
9
|
-
|
|
8
|
+
const a = document.createElement("div");
|
|
9
|
+
a.setAttribute("data-copilot-checkout-toast", "true"), a.innerText = d, Object.assign(a.style, {
|
|
10
10
|
position: "fixed",
|
|
11
11
|
top: "20px",
|
|
12
12
|
left: "50%",
|
|
@@ -22,17 +22,17 @@ const y = ({ iframeUrl: h, launcherIcon: u }) => {
|
|
|
22
22
|
maxWidth: "420px",
|
|
23
23
|
lineHeight: "1.4",
|
|
24
24
|
textAlign: "center"
|
|
25
|
-
}), document.body.appendChild(
|
|
26
|
-
|
|
25
|
+
}), document.body.appendChild(a), setTimeout(() => {
|
|
26
|
+
a.remove();
|
|
27
27
|
}, 4500);
|
|
28
|
-
},
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
document.body.appendChild(
|
|
28
|
+
}, r = document.createElement("div");
|
|
29
|
+
r.setAttribute("data-copilot-widget-root", "true");
|
|
30
|
+
const b = r.attachShadow({ mode: "open" });
|
|
31
|
+
document.body.appendChild(r);
|
|
32
32
|
const t = document.createElement("button");
|
|
33
33
|
t.type = "button", t.setAttribute("aria-label", "Open Copilot chat"), t.innerHTML = `
|
|
34
34
|
<img
|
|
35
|
-
src="${
|
|
35
|
+
src="${p}"
|
|
36
36
|
alt="Copilot chat launcher"
|
|
37
37
|
style="width: 38px; height: 38px; object-fit: contain; border-radius: 50%; pointer-events: none;"
|
|
38
38
|
/>
|
|
@@ -70,7 +70,7 @@ const y = ({ iframeUrl: h, launcherIcon: u }) => {
|
|
|
70
70
|
opacity: "0",
|
|
71
71
|
transition: "all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)"
|
|
72
72
|
});
|
|
73
|
-
const l = document.createElement("div"),
|
|
73
|
+
const l = document.createElement("div"), w = document.createElement("div"), x = document.createElement("div");
|
|
74
74
|
Object.assign(l.style, {
|
|
75
75
|
position: "absolute",
|
|
76
76
|
bottom: "-14px",
|
|
@@ -80,7 +80,7 @@ const y = ({ iframeUrl: h, launcherIcon: u }) => {
|
|
|
80
80
|
pointerEvents: "none",
|
|
81
81
|
display: "none",
|
|
82
82
|
zIndex: "1"
|
|
83
|
-
}), Object.assign(
|
|
83
|
+
}), Object.assign(w.style, {
|
|
84
84
|
position: "absolute",
|
|
85
85
|
bottom: "0",
|
|
86
86
|
left: "0",
|
|
@@ -91,7 +91,7 @@ const y = ({ iframeUrl: h, launcherIcon: u }) => {
|
|
|
91
91
|
borderLeft: "15px solid transparent",
|
|
92
92
|
borderRight: "15px solid transparent",
|
|
93
93
|
borderTop: "15px solid rgba(15,23,42,0.1)"
|
|
94
|
-
}), Object.assign(
|
|
94
|
+
}), Object.assign(x.style, {
|
|
95
95
|
position: "absolute",
|
|
96
96
|
bottom: "2px",
|
|
97
97
|
left: "0",
|
|
@@ -104,9 +104,9 @@ const y = ({ iframeUrl: h, launcherIcon: u }) => {
|
|
|
104
104
|
borderTop: "13px solid white",
|
|
105
105
|
boxShadow: "0 6px 16px rgba(15,23,42,0.12)",
|
|
106
106
|
borderRadius: "2px"
|
|
107
|
-
}), l.appendChild(
|
|
108
|
-
const
|
|
109
|
-
Object.assign(
|
|
107
|
+
}), l.appendChild(w), l.appendChild(x);
|
|
108
|
+
const c = document.createElement("div");
|
|
109
|
+
Object.assign(c.style, {
|
|
110
110
|
width: "720px",
|
|
111
111
|
maxWidth: "calc(100vw - 48px)",
|
|
112
112
|
height: `${Math.min(720, Math.max(320, window.innerHeight - 140))}px`,
|
|
@@ -116,75 +116,68 @@ const y = ({ iframeUrl: h, launcherIcon: u }) => {
|
|
|
116
116
|
border: "1px solid rgba(15,23,42,0.12)",
|
|
117
117
|
boxShadow: "0 18px 45px rgba(15,23,42,0.16)",
|
|
118
118
|
overflow: "hidden"
|
|
119
|
-
}),
|
|
119
|
+
}), b.appendChild(c);
|
|
120
120
|
const i = document.createElement("iframe");
|
|
121
|
-
i.src =
|
|
121
|
+
i.src = g, i.title = "Copilot chat widget", i.allow = "clipboard-read; clipboard-write; microphone; camera; display-capture", i.setAttribute("scrolling", "no"), Object.assign(i.style, {
|
|
122
122
|
width: "100%",
|
|
123
123
|
height: "100%",
|
|
124
124
|
border: "none",
|
|
125
125
|
display: "block",
|
|
126
126
|
background: "transparent",
|
|
127
127
|
overflow: "hidden"
|
|
128
|
-
}),
|
|
129
|
-
let
|
|
130
|
-
const
|
|
131
|
-
|
|
128
|
+
}), b.appendChild(c), c.appendChild(i), e.appendChild(l), e.appendChild(c), document.body.appendChild(t), document.body.appendChild(e);
|
|
129
|
+
let s = !1;
|
|
130
|
+
const u = () => {
|
|
131
|
+
s && (s = !1, e.style.opacity = "0", e.style.transform = "scale(0.8) translateY(20px)", l.style.display = "none", setTimeout(() => {
|
|
132
132
|
e.style.display = "none";
|
|
133
133
|
}, 250), t.style.transform = "scale(1)");
|
|
134
|
-
},
|
|
135
|
-
const { data: o, source:
|
|
134
|
+
}, f = (d) => {
|
|
135
|
+
const { data: o, source: a } = d || {};
|
|
136
136
|
if (o?.type) {
|
|
137
|
-
if (o.type === "CART_CHECKOUT" &&
|
|
138
|
-
console.log("[CopilotChat] Received checkout payload from widget:", o),
|
|
137
|
+
if (o.type === "CART_CHECKOUT" && a === i.contentWindow) {
|
|
138
|
+
console.log("[CopilotChat] Received checkout payload from widget:", o), n("Checkout message received from chat widget. Check console for payload.");
|
|
139
139
|
return;
|
|
140
140
|
}
|
|
141
|
-
if (o.type === "WIDGET_READY" &&
|
|
141
|
+
if (o.type === "WIDGET_READY" && a === i.contentWindow) {
|
|
142
142
|
i.contentWindow.postMessage({ type: "INIT_WIDGET" }, "*");
|
|
143
143
|
return;
|
|
144
144
|
}
|
|
145
|
-
o.type === "CHAT_CLOSED" &&
|
|
145
|
+
o.type === "CHAT_CLOSED" && u();
|
|
146
146
|
}
|
|
147
147
|
};
|
|
148
|
-
window.addEventListener("message",
|
|
149
|
-
|
|
148
|
+
window.addEventListener("message", f), t.onclick = (d) => {
|
|
149
|
+
d.stopPropagation(), s = !s, s ? (e.style.display = "block", l.style.display = "block", requestAnimationFrame(() => {
|
|
150
150
|
e.style.opacity = "1", e.style.transform = "scale(1) translateY(0)";
|
|
151
|
-
})) :
|
|
152
|
-
}, document.addEventListener("click", (
|
|
153
|
-
const o =
|
|
154
|
-
o &&
|
|
151
|
+
})) : u();
|
|
152
|
+
}, document.addEventListener("click", (d) => {
|
|
153
|
+
const o = d.target;
|
|
154
|
+
o && s && !e.contains(o) && o !== t && u();
|
|
155
155
|
});
|
|
156
|
-
const
|
|
157
|
-
close:
|
|
156
|
+
const m = {
|
|
157
|
+
close: u,
|
|
158
158
|
open: () => {
|
|
159
|
-
|
|
159
|
+
s || t.click();
|
|
160
160
|
}
|
|
161
161
|
};
|
|
162
|
-
return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.close =
|
|
162
|
+
return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.close = m.close, window.CopilotChat.open = m.open, window.CopilotChat.controls = m, m;
|
|
163
163
|
};
|
|
164
|
-
async function E(
|
|
164
|
+
async function E(g = {}) {
|
|
165
165
|
if (typeof window > "u" || typeof document > "u")
|
|
166
166
|
return null;
|
|
167
|
-
const
|
|
168
|
-
if (!d)
|
|
169
|
-
return console.error(
|
|
170
|
-
"[CopilotChat] Missing token (provide via loadCopilotChatWidget({ token }) or window.CopilotChatConfig.token)."
|
|
171
|
-
), null;
|
|
172
|
-
const g = h.baseUrl || u.baseUrl || C, c = `${g.replace(/\/$/, "")}/api/chat-widget/config?token=${encodeURIComponent(
|
|
173
|
-
d
|
|
174
|
-
)}`;
|
|
167
|
+
const p = y, h = `${p.replace(/\/$/, "")}/api/chat-widget/config`;
|
|
175
168
|
try {
|
|
176
|
-
const n = await fetch(
|
|
169
|
+
const n = await fetch(h, {
|
|
177
170
|
credentials: "omit",
|
|
178
171
|
mode: "cors"
|
|
179
172
|
});
|
|
180
173
|
if (!n.ok)
|
|
181
174
|
throw new Error(`Server responded with ${n.status}`);
|
|
182
|
-
const
|
|
183
|
-
if (!
|
|
175
|
+
const r = await n.json();
|
|
176
|
+
if (!r?.iframeUrl || !r?.launcherIcon)
|
|
184
177
|
throw new Error("Received incomplete widget configuration from server.");
|
|
185
|
-
return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.
|
|
186
|
-
iframeUrl:
|
|
187
|
-
launcherIcon:
|
|
178
|
+
return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.baseUrl = p, C({
|
|
179
|
+
iframeUrl: r.iframeUrl,
|
|
180
|
+
launcherIcon: r.launcherIcon
|
|
188
181
|
});
|
|
189
182
|
} catch (n) {
|
|
190
183
|
return console.error(
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
export interface CopilotChatOptions {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
baseUrl?: string;
|
|
5
|
-
}
|
|
1
|
+
export interface CopilotChatOptions {
|
|
2
|
+
autoload?: boolean;
|
|
3
|
+
}
|
|
6
4
|
|
|
7
5
|
export function loadCopilotChatWidget(options: CopilotChatOptions): unknown;
|
|
8
6
|
export default loadCopilotChatWidget;
|
package/src/index.js
CHANGED
|
@@ -265,32 +265,17 @@ const buildWidget = ({ iframeUrl, launcherIcon }) => {
|
|
|
265
265
|
return controls;
|
|
266
266
|
};
|
|
267
267
|
|
|
268
|
-
export async function loadCopilotChatWidget(options = {}) {
|
|
269
|
-
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
270
|
-
return null;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
const globalConfig = window.CopilotChatConfig || {};
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
(typeof window !== "undefined"
|
|
278
|
-
|
|
279
|
-
: null);
|
|
280
|
-
|
|
281
|
-
if (!token) {
|
|
282
|
-
console.error(
|
|
283
|
-
"[CopilotChat] Missing token (provide via loadCopilotChatWidget({ token }) or window.CopilotChatConfig.token)."
|
|
284
|
-
);
|
|
285
|
-
return null;
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
const baseUrl =
|
|
289
|
-
options.baseUrl ||
|
|
290
|
-
globalConfig.baseUrl ||
|
|
291
|
-
BUILD_BASE_URL ||
|
|
292
|
-
(typeof window !== "undefined" && window.location?.origin) ||
|
|
293
|
-
null;
|
|
268
|
+
export async function loadCopilotChatWidget(options = {}) {
|
|
269
|
+
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const globalConfig = window.CopilotChatConfig || {};
|
|
274
|
+
|
|
275
|
+
const baseUrl =
|
|
276
|
+
BUILD_BASE_URL ||
|
|
277
|
+
(typeof window !== "undefined" && window.location?.origin) ||
|
|
278
|
+
null;
|
|
294
279
|
|
|
295
280
|
if (!baseUrl) {
|
|
296
281
|
console.error(
|
|
@@ -299,9 +284,7 @@ export async function loadCopilotChatWidget(options = {}) {
|
|
|
299
284
|
return null;
|
|
300
285
|
}
|
|
301
286
|
|
|
302
|
-
const url = `${baseUrl.replace(/\/$/, "")}/api/chat-widget/config
|
|
303
|
-
token
|
|
304
|
-
)}`;
|
|
287
|
+
const url = `${baseUrl.replace(/\/$/, "")}/api/chat-widget/config`;
|
|
305
288
|
|
|
306
289
|
try {
|
|
307
290
|
const response = await fetch(url, {
|
|
@@ -319,8 +302,7 @@ export async function loadCopilotChatWidget(options = {}) {
|
|
|
319
302
|
}
|
|
320
303
|
|
|
321
304
|
window.CopilotChat = window.CopilotChat || {};
|
|
322
|
-
window.CopilotChat.
|
|
323
|
-
window.CopilotChat.baseUrl = baseUrl;
|
|
305
|
+
window.CopilotChat.baseUrl = baseUrl;
|
|
324
306
|
|
|
325
307
|
return buildWidget({
|
|
326
308
|
iframeUrl: remoteConfig.iframeUrl,
|
package/src/widget-runtime.js
CHANGED
|
@@ -11,22 +11,7 @@
|
|
|
11
11
|
const IFRAME_SIZE = 720;
|
|
12
12
|
const embeddingScript = document.currentScript;
|
|
13
13
|
|
|
14
|
-
const
|
|
15
|
-
if (!scriptEl?.src) return {};
|
|
16
|
-
try {
|
|
17
|
-
const url = new URL(scriptEl.src, window.location.href);
|
|
18
|
-
const params = {};
|
|
19
|
-
url.searchParams.forEach((value, key) => {
|
|
20
|
-
params[key] = value;
|
|
21
|
-
});
|
|
22
|
-
return params;
|
|
23
|
-
} catch (error) {
|
|
24
|
-
console.warn("[CopilotChat] Failed to parse script query params:", error);
|
|
25
|
-
return {};
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const ready = (fn) => {
|
|
14
|
+
const ready = (fn) => {
|
|
30
15
|
if (document.readyState === "complete" || document.readyState === "interactive") {
|
|
31
16
|
setTimeout(fn, 0);
|
|
32
17
|
} else {
|
|
@@ -322,75 +307,33 @@
|
|
|
322
307
|
console.error(`[CopilotChat] ${message}`);
|
|
323
308
|
};
|
|
324
309
|
|
|
325
|
-
const shouldAutoBootstrapFromScript = () => {
|
|
326
|
-
const scriptEl = resolveEmbeddingScript();
|
|
327
|
-
if (!scriptEl) return false;
|
|
328
|
-
const
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
const
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
const resolveToken = () => {
|
|
342
|
-
const fromConfig = config.token || globalConfig.token || datasetConfig.token || queryConfig.token;
|
|
343
|
-
if (fromConfig) return fromConfig;
|
|
344
|
-
|
|
345
|
-
try {
|
|
346
|
-
const stored = window.localStorage.getItem("copilotChatToken");
|
|
347
|
-
if (stored) return stored;
|
|
348
|
-
} catch (error) {
|
|
349
|
-
// ignore storage errors
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
if (typeof window !== "undefined" && typeof window.prompt === "function") {
|
|
353
|
-
const entered = window.prompt("Enter your Copilot Chat token:");
|
|
354
|
-
if (entered) {
|
|
355
|
-
try {
|
|
356
|
-
window.localStorage.setItem("copilotChatToken", entered);
|
|
357
|
-
} catch (error) {
|
|
358
|
-
// ignore storage errors
|
|
359
|
-
}
|
|
360
|
-
return entered;
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
return null;
|
|
365
|
-
};
|
|
366
|
-
|
|
367
|
-
const token = resolveToken();
|
|
368
|
-
|
|
369
|
-
if (!token) {
|
|
370
|
-
handleError("Missing token (provide via init config, window.CopilotChatConfig.token, or data-token attribute).");
|
|
371
|
-
return null;
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
const baseUrl =
|
|
375
|
-
config.baseUrl ||
|
|
376
|
-
globalConfig.baseUrl ||
|
|
377
|
-
datasetConfig.baseUrl ||
|
|
378
|
-
queryConfig.baseUrl ||
|
|
379
|
-
BUILD_BASE_URL ||
|
|
380
|
-
inferBaseUrlFromScript(scriptEl) ||
|
|
381
|
-
(typeof window !== "undefined" && window.location?.origin) ||
|
|
382
|
-
null;
|
|
310
|
+
const shouldAutoBootstrapFromScript = () => {
|
|
311
|
+
const scriptEl = resolveEmbeddingScript();
|
|
312
|
+
if (!scriptEl) return false;
|
|
313
|
+
const dataset = scriptEl.dataset || {};
|
|
314
|
+
return dataset.autoload !== "false";
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
const initCopilotChatWidget = async (config = {}) => {
|
|
318
|
+
const scriptEl = config.scriptEl || resolveEmbeddingScript();
|
|
319
|
+
const globalConfig = window.CopilotChatConfig || {};
|
|
320
|
+
|
|
321
|
+
const baseUrl =
|
|
322
|
+
BUILD_BASE_URL ||
|
|
323
|
+
inferBaseUrlFromScript(scriptEl) ||
|
|
324
|
+
(typeof window !== "undefined" && window.location?.origin) ||
|
|
325
|
+
null;
|
|
383
326
|
|
|
384
327
|
if (!baseUrl) {
|
|
385
328
|
handleError("Unable to resolve base URL from embedding script or window.location.");
|
|
386
329
|
return null;
|
|
387
330
|
}
|
|
388
331
|
|
|
389
|
-
try {
|
|
390
|
-
const response = await fetch(`${baseUrl}/api/chat-widget/config
|
|
391
|
-
credentials: "omit",
|
|
392
|
-
mode: "cors",
|
|
393
|
-
});
|
|
332
|
+
try {
|
|
333
|
+
const response = await fetch(`${baseUrl}/api/chat-widget/config`, {
|
|
334
|
+
credentials: "omit",
|
|
335
|
+
mode: "cors",
|
|
336
|
+
});
|
|
394
337
|
|
|
395
338
|
if (!response.ok) {
|
|
396
339
|
throw new Error(`Server responded with ${response.status}`);
|