copilot-chat-widget 0.1.0 → 0.1.1

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/dist/index.mjs CHANGED
@@ -1,230 +1,198 @@
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]");
1
+ (function() {
2
+ if (typeof window > "u" || window.__copilotWidgetLoaded)
3
+ return;
4
+ window.__copilotWidgetLoaded = !0;
5
+ const b = 64, C = 720, E = document.currentScript, v = "http://localhost:3000", k = (e) => {
6
+ if (!e?.src) return {};
7
+ try {
8
+ const n = new URL(e.src, window.location.href), r = {};
9
+ return n.searchParams.forEach((a, h) => {
10
+ r[h] = a;
11
+ }), r;
12
+ } catch (n) {
13
+ return console.warn("[CopilotChat] Failed to parse script query params:", n), {};
14
+ }
15
+ }, I = (e) => {
16
+ document.readyState === "complete" || document.readyState === "interactive" ? setTimeout(e, 0) : document.addEventListener("DOMContentLoaded", e);
17
+ }, S = (e) => e.endsWith("/") ? e.slice(0, -1) : e, A = ({ iframeUrl: e, launcherIcon: n, theme: r = {} }) => {
18
+ const a = document.querySelector("[data-copilot-widget-root]");
18
19
  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 = `
20
+ const { accent: h = "linear-gradient(135deg, #0078ff, #00c6ff)" } = r, m = document.createElement("div");
21
+ m.setAttribute("data-copilot-widget-root", "true");
22
+ const u = m.attachShadow({ mode: "open" });
23
+ document.body.appendChild(m);
24
+ const t = document.createElement("button");
25
+ t.type = "button", t.setAttribute("aria-label", "Open Copilot chat"), t.innerHTML = `
45
26
  <img
46
27
  src="${n}"
47
28
  alt="Copilot chat launcher"
48
29
  style="width: 38px; height: 38px; object-fit: contain; border-radius: 50%; pointer-events: none;"
49
30
  />
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;
31
+ `, Object.assign(t.style, {
32
+ position: "fixed",
33
+ bottom: "24px",
34
+ right: "24px",
35
+ width: `${b}px`,
36
+ height: `${b}px`,
37
+ borderRadius: "50%",
38
+ border: "none",
39
+ background: h,
40
+ color: "white",
41
+ cursor: "pointer",
42
+ zIndex: 999998,
43
+ display: "flex",
44
+ alignItems: "center",
45
+ justifyContent: "center",
46
+ boxShadow: "0 6px 14px rgba(0,0,0,0.25)",
47
+ transition: "all 0.25s ease"
48
+ }), t.onmouseover = () => {
49
+ t.style.transform = "scale(1.12)", t.style.boxShadow = "0 10px 25px rgba(0,0,0,0.3)";
50
+ }, t.onmouseout = () => {
51
+ t.style.transform = "scale(1)", t.style.boxShadow = "0 6px 14px rgba(0,0,0,0.25)";
52
+ };
53
+ const o = document.createElement("div");
54
+ o.setAttribute("data-copilot-widget-root", "true"), Object.assign(o.style, {
55
+ display: "none",
56
+ position: "fixed",
57
+ bottom: `${b + 36}px`,
58
+ right: "24px",
59
+ zIndex: "999999",
60
+ transformOrigin: "bottom right",
61
+ transform: "scale(0.8) translateY(20px)",
62
+ opacity: "0",
63
+ transition: "all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)"
64
+ });
65
+ const i = document.createElement("div"), c = document.createElement("div"), g = document.createElement("div");
66
+ Object.assign(i.style, {
67
+ position: "absolute",
68
+ bottom: "-14px",
69
+ right: "28px",
70
+ width: "30px",
71
+ height: "20px",
72
+ pointerEvents: "none",
73
+ display: "none",
74
+ zIndex: "1"
75
+ }), Object.assign(c.style, {
76
+ position: "absolute",
77
+ bottom: "0",
78
+ left: "0",
79
+ right: "0",
80
+ margin: "0 auto",
81
+ width: "0",
82
+ height: "0",
83
+ borderLeft: "15px solid transparent",
84
+ borderRight: "15px solid transparent",
85
+ borderTop: "15px solid rgba(15,23,42,0.1)"
86
+ }), Object.assign(g.style, {
87
+ position: "absolute",
88
+ bottom: "2px",
89
+ left: "0",
90
+ right: "0",
91
+ margin: "0 auto",
92
+ width: "0",
93
+ height: "0",
94
+ borderLeft: "13px solid transparent",
95
+ borderRight: "13px solid transparent",
96
+ borderTop: "13px solid white",
97
+ boxShadow: "0 6px 16px rgba(15,23,42,0.12)",
98
+ borderRadius: "2px"
99
+ }), i.appendChild(c), i.appendChild(g);
100
+ const s = document.createElement("div");
101
+ Object.assign(s.style, {
102
+ width: `${C}px`,
103
+ maxWidth: "calc(100vw - 48px)",
104
+ // keep height responsive to viewport to avoid outer scrollbars
105
+ height: `${Math.min(C, Math.max(320, window.innerHeight - 140))}px`,
106
+ maxHeight: "calc(100vh - 150px)",
107
+ borderRadius: "20px",
108
+ background: "white",
109
+ border: "1px solid rgba(15,23,42,0.12)",
110
+ boxShadow: "0 18px 45px rgba(15,23,42,0.16)",
111
+ overflow: "hidden"
112
+ }), u.appendChild(s);
113
+ const d = document.createElement("iframe");
114
+ d.src = e, d.title = "Copilot chat widget", d.allow = "clipboard-read; clipboard-write; microphone; camera; display-capture", d.setAttribute("scrolling", "no"), Object.assign(d.style, {
115
+ width: "100%",
116
+ height: "100%",
117
+ border: "none",
118
+ display: "block",
119
+ background: "transparent",
120
+ overflow: "hidden"
121
+ }), u.appendChild(s), s.appendChild(d), o.appendChild(i), o.appendChild(s), document.body.appendChild(t), document.body.appendChild(o), window.addEventListener("message", (p) => {
122
+ p.data?.type === "WIDGET_READY" && d.contentWindow.postMessage(
123
+ { type: "INIT_WIDGET" },
124
+ "*"
125
+ );
126
+ });
127
+ let l = !1;
128
+ const w = () => {
129
+ l && (l = !1, o.style.opacity = "0", o.style.transform = "scale(0.8) translateY(20px)", i.style.display = "none", setTimeout(() => {
130
+ o.style.display = "none";
131
+ }, 250), t.style.transform = "scale(1)");
132
+ };
133
+ t.onclick = (p) => {
134
+ p.stopPropagation(), l = !l, l ? (o.style.display = "block", i.style.display = "block", requestAnimationFrame(() => {
135
+ o.style.opacity = "1", o.style.transform = "scale(1) translateY(0)";
136
+ })) : w();
137
+ }, window.addEventListener("message", (p) => {
138
+ p?.data?.type === "CHAT_CLOSED" && w();
139
+ }), document.addEventListener("click", (p) => {
140
+ const x = p.target;
141
+ x && l && !o.contains(x) && x !== t && w();
142
+ });
143
+ const y = {
144
+ close: w,
145
+ open: () => {
146
+ l || t.click();
152
147
  }
153
- a.type === "CHAT_CLOSED" && w();
148
+ };
149
+ return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.close = y.close, window.CopilotChat.open = y.open, y;
150
+ }, f = (e) => {
151
+ console.error(`[CopilotChat] ${e}`);
152
+ }, U = () => E || Array.from(document.querySelectorAll("script")).reverse().find((r) => r.dataset?.token || r.src.includes("chat-widget")) || null;
153
+ I(async () => {
154
+ const e = U();
155
+ if (!e) {
156
+ f("Unable to locate the embedding script tag on the page.");
157
+ return;
154
158
  }
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();
159
+ const n = window.CopilotChatConfig || {}, r = e.dataset || {}, a = k(e), h = n.token || r.token || a.token;
160
+ if (!h) {
161
+ f("Missing token (provide via window.CopilotChatConfig.token or data-token attribute).");
162
+ return;
168
163
  }
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
164
+ const m = S(
165
+ v
166
+ ), u = n.theme && n.theme.accent || n.accent || r.themeAccent || a.themeAccent;
167
+ try {
168
+ const t = await fetch(`${m}/api/chat-widget/config?token=${encodeURIComponent(h)}`, {
169
+ credentials: "omit",
170
+ mode: "cors"
171
+ });
172
+ if (!t.ok)
173
+ throw new Error(`Server responded with ${t.status}`);
174
+ const o = await t.json();
175
+ if (!o?.iframeUrl || !o?.launcherIcon)
176
+ throw new Error("Received incomplete widget configuration from server.");
177
+ const i = {
178
+ iframeUrl: o.iframeUrl,
179
+ launcherIcon: o.launcherIcon,
180
+ theme: {
181
+ accent: u || o.theme?.accent || void 0
182
+ }
210
183
  };
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);
184
+ window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.init = (c = {}) => {
185
+ const g = {
186
+ accent: c.theme?.accent || c.accent || i.theme.accent || (typeof n.theme == "object" ? n.theme.accent : void 0) || n.accent
187
+ }, s = {
188
+ iframeUrl: c.iframeUrl || i.iframeUrl,
189
+ launcherIcon: c.launcherIcon || i.launcherIcon,
190
+ theme: g
191
+ };
192
+ return A(s);
193
+ }, window.CopilotChat.init(n);
194
+ } catch (t) {
195
+ f(t instanceof Error ? t.message : "Unknown error during widget bootstrap.");
196
+ }
223
197
  });
224
- });
225
- I() && U();
226
- export {
227
- O as initCopilotChatWidget,
228
- U as loadCopilotChatWidget,
229
- E as resolveEmbeddingScript
230
- };
198
+ })();
package/package.json CHANGED
@@ -1,40 +1,41 @@
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
- }
1
+ {
2
+ "name": "copilot-chat-widget",
3
+ "version": "0.1.1",
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.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
+ }