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/README.md +94 -94
- package/dist/chat-widget.min.js +2 -2
- package/dist/index.cjs +2 -2
- package/dist/index.mjs +187 -219
- package/package.json +41 -40
- package/src/index.js +371 -425
- package/src/standalone.js +4 -4
package/src/index.js
CHANGED
|
@@ -1,425 +1,371 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
if (
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
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
|
-
};
|
|
403
|
-
|
|
404
|
-
const controls = window.CopilotChat.init(config);
|
|
405
|
-
return controls;
|
|
406
|
-
} catch (error) {
|
|
407
|
-
handleError(error instanceof Error ? error.message : "Unknown error during widget bootstrap.");
|
|
408
|
-
return null;
|
|
409
|
-
}
|
|
410
|
-
};
|
|
411
|
-
|
|
412
|
-
export const loadCopilotChatWidget = (config = {}) =>
|
|
413
|
-
new Promise((resolve) => {
|
|
414
|
-
if (typeof document === "undefined") {
|
|
415
|
-
resolve(null);
|
|
416
|
-
return;
|
|
417
|
-
}
|
|
418
|
-
ready(() => {
|
|
419
|
-
initCopilotChatWidget(config).then(resolve);
|
|
420
|
-
});
|
|
421
|
-
});
|
|
422
|
-
|
|
423
|
-
if (shouldAutoBootstrapFromScript()) {
|
|
424
|
-
loadCopilotChatWidget();
|
|
425
|
-
}
|
|
1
|
+
(function bootstrap() {
|
|
2
|
+
if (typeof window === "undefined") {
|
|
3
|
+
return;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
if (window.__copilotWidgetLoaded) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
window.__copilotWidgetLoaded = true;
|
|
10
|
+
|
|
11
|
+
const DEFAULT_BASE_URL = CHAT_WIDGET_BASE_URL;
|
|
12
|
+
const BUTTON_SIZE = 64;
|
|
13
|
+
const IFRAME_SIZE = 720;
|
|
14
|
+
const embeddingScript = document.currentScript;
|
|
15
|
+
const ENV_BASE_URL = CHAT_WIDGET_BASE_URL;
|
|
16
|
+
|
|
17
|
+
const readQueryConfig = (scriptEl) => {
|
|
18
|
+
if (!scriptEl?.src) return {};
|
|
19
|
+
try {
|
|
20
|
+
const url = new URL(scriptEl.src, window.location.href);
|
|
21
|
+
const params = {};
|
|
22
|
+
url.searchParams.forEach((value, key) => {
|
|
23
|
+
params[key] = value;
|
|
24
|
+
});
|
|
25
|
+
return params;
|
|
26
|
+
} catch (error) {
|
|
27
|
+
console.warn("[CopilotChat] Failed to parse script query params:", error);
|
|
28
|
+
return {};
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const ready = (fn) => {
|
|
33
|
+
if (document.readyState === "complete" || document.readyState === "interactive") {
|
|
34
|
+
setTimeout(fn, 0);
|
|
35
|
+
} else {
|
|
36
|
+
document.addEventListener("DOMContentLoaded", fn);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const normalizeBaseUrl = (url) => {
|
|
41
|
+
if (!url) return DEFAULT_BASE_URL;
|
|
42
|
+
return url.endsWith("/") ? url.slice(0, -1) : url;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const inferBaseUrlFromScript = (scriptEl) => {
|
|
46
|
+
if (!scriptEl?.src) return null;
|
|
47
|
+
try {
|
|
48
|
+
const srcUrl = new URL(scriptEl.src, window.location.href);
|
|
49
|
+
return `${srcUrl.protocol}//${srcUrl.host}`;
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.warn("[CopilotChat] Unable to infer base URL from script source.", error);
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const buildWidget = ({ iframeUrl, launcherIcon, theme = {} }) => {
|
|
57
|
+
const existingRoot = document.querySelector("[data-copilot-widget-root]");
|
|
58
|
+
if (existingRoot) {
|
|
59
|
+
existingRoot.remove();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const { accent = "linear-gradient(135deg, #0078ff, #00c6ff)" } = theme;
|
|
63
|
+
|
|
64
|
+
// ---------- Shadow DOM container ----------
|
|
65
|
+
const containerHost = document.createElement("div");
|
|
66
|
+
containerHost.setAttribute("data-copilot-widget-root", "true");
|
|
67
|
+
const shadow = containerHost.attachShadow({ mode: "open" });
|
|
68
|
+
document.body.appendChild(containerHost);
|
|
69
|
+
|
|
70
|
+
// ---------- Button ----------
|
|
71
|
+
const btn = document.createElement("button");
|
|
72
|
+
btn.type = "button";
|
|
73
|
+
btn.setAttribute("aria-label", "Open Copilot chat");
|
|
74
|
+
btn.innerHTML = `
|
|
75
|
+
<img
|
|
76
|
+
src="${launcherIcon}"
|
|
77
|
+
alt="Copilot chat launcher"
|
|
78
|
+
style="width: 38px; height: 38px; object-fit: contain; border-radius: 50%; pointer-events: none;"
|
|
79
|
+
/>
|
|
80
|
+
`;
|
|
81
|
+
|
|
82
|
+
Object.assign(btn.style, {
|
|
83
|
+
position: "fixed",
|
|
84
|
+
bottom: "24px",
|
|
85
|
+
right: "24px",
|
|
86
|
+
width: `${BUTTON_SIZE}px`,
|
|
87
|
+
height: `${BUTTON_SIZE}px`,
|
|
88
|
+
borderRadius: "50%",
|
|
89
|
+
border: "none",
|
|
90
|
+
background: accent,
|
|
91
|
+
color: "white",
|
|
92
|
+
cursor: "pointer",
|
|
93
|
+
zIndex: 999998,
|
|
94
|
+
display: "flex",
|
|
95
|
+
alignItems: "center",
|
|
96
|
+
justifyContent: "center",
|
|
97
|
+
boxShadow: "0 6px 14px rgba(0,0,0,0.25)",
|
|
98
|
+
transition: "all 0.25s ease",
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
btn.onmouseover = () => {
|
|
102
|
+
btn.style.transform = "scale(1.12)";
|
|
103
|
+
btn.style.boxShadow = "0 10px 25px rgba(0,0,0,0.3)";
|
|
104
|
+
};
|
|
105
|
+
btn.onmouseout = () => {
|
|
106
|
+
btn.style.transform = "scale(1)";
|
|
107
|
+
btn.style.boxShadow = "0 6px 14px rgba(0,0,0,0.25)";
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const container = document.createElement("div");
|
|
111
|
+
container.setAttribute("data-copilot-widget-root", "true");
|
|
112
|
+
Object.assign(container.style, {
|
|
113
|
+
display: "none",
|
|
114
|
+
position: "fixed",
|
|
115
|
+
bottom: `${BUTTON_SIZE + 36}px`,
|
|
116
|
+
right: "24px",
|
|
117
|
+
zIndex: "999999",
|
|
118
|
+
transformOrigin: "bottom right",
|
|
119
|
+
transform: "scale(0.8) translateY(20px)",
|
|
120
|
+
opacity: "0",
|
|
121
|
+
transition: "all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)",
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const arrow = document.createElement("div");
|
|
125
|
+
const arrowBorder = document.createElement("div");
|
|
126
|
+
const arrowFill = document.createElement("div");
|
|
127
|
+
|
|
128
|
+
Object.assign(arrow.style, {
|
|
129
|
+
position: "absolute",
|
|
130
|
+
bottom: "-14px",
|
|
131
|
+
right: "28px",
|
|
132
|
+
width: "30px",
|
|
133
|
+
height: "20px",
|
|
134
|
+
pointerEvents: "none",
|
|
135
|
+
display: "none",
|
|
136
|
+
zIndex: "1",
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
Object.assign(arrowBorder.style, {
|
|
140
|
+
position: "absolute",
|
|
141
|
+
bottom: "0",
|
|
142
|
+
left: "0",
|
|
143
|
+
right: "0",
|
|
144
|
+
margin: "0 auto",
|
|
145
|
+
width: "0",
|
|
146
|
+
height: "0",
|
|
147
|
+
borderLeft: "15px solid transparent",
|
|
148
|
+
borderRight: "15px solid transparent",
|
|
149
|
+
borderTop: "15px solid rgba(15,23,42,0.1)",
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
Object.assign(arrowFill.style, {
|
|
153
|
+
position: "absolute",
|
|
154
|
+
bottom: "2px",
|
|
155
|
+
left: "0",
|
|
156
|
+
right: "0",
|
|
157
|
+
margin: "0 auto",
|
|
158
|
+
width: "0",
|
|
159
|
+
height: "0",
|
|
160
|
+
borderLeft: "13px solid transparent",
|
|
161
|
+
borderRight: "13px solid transparent",
|
|
162
|
+
borderTop: "13px solid white",
|
|
163
|
+
boxShadow: "0 6px 16px rgba(15,23,42,0.12)",
|
|
164
|
+
borderRadius: "2px",
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
arrow.appendChild(arrowBorder);
|
|
168
|
+
arrow.appendChild(arrowFill);
|
|
169
|
+
|
|
170
|
+
const frameWrapper = document.createElement("div");
|
|
171
|
+
Object.assign(frameWrapper.style, {
|
|
172
|
+
width: `${IFRAME_SIZE}px`,
|
|
173
|
+
maxWidth: "calc(100vw - 48px)",
|
|
174
|
+
// keep height responsive to viewport to avoid outer scrollbars
|
|
175
|
+
height: `${Math.min(IFRAME_SIZE, Math.max(320, window.innerHeight - 140))}px`,
|
|
176
|
+
maxHeight: "calc(100vh - 150px)",
|
|
177
|
+
borderRadius: "20px",
|
|
178
|
+
background: "white",
|
|
179
|
+
border: "1px solid rgba(15,23,42,0.12)",
|
|
180
|
+
boxShadow: "0 18px 45px rgba(15,23,42,0.16)",
|
|
181
|
+
overflow: "hidden",
|
|
182
|
+
});
|
|
183
|
+
shadow.appendChild(frameWrapper);
|
|
184
|
+
|
|
185
|
+
const iframe = document.createElement("iframe");
|
|
186
|
+
iframe.src = iframeUrl;
|
|
187
|
+
iframe.title = "Copilot chat widget";
|
|
188
|
+
iframe.allow = "clipboard-read; clipboard-write; microphone; camera; display-capture";
|
|
189
|
+
iframe.setAttribute("scrolling", "no");
|
|
190
|
+
Object.assign(iframe.style, {
|
|
191
|
+
width: "100%",
|
|
192
|
+
height: "100%",
|
|
193
|
+
border: "none",
|
|
194
|
+
display: "block",
|
|
195
|
+
background: "transparent",
|
|
196
|
+
overflow: "hidden",
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
shadow.appendChild(frameWrapper);
|
|
200
|
+
frameWrapper.appendChild(iframe);
|
|
201
|
+
|
|
202
|
+
container.appendChild(arrow);
|
|
203
|
+
container.appendChild(frameWrapper);
|
|
204
|
+
document.body.appendChild(btn);
|
|
205
|
+
document.body.appendChild(container);
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
window.addEventListener("message", (event) => {
|
|
209
|
+
if (event.data?.type === "WIDGET_READY") {
|
|
210
|
+
iframe.contentWindow.postMessage(
|
|
211
|
+
{ type: "INIT_WIDGET" },
|
|
212
|
+
"*"
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
let isOpen = false;
|
|
218
|
+
|
|
219
|
+
const closeChat = () => {
|
|
220
|
+
if (!isOpen) return;
|
|
221
|
+
isOpen = false;
|
|
222
|
+
container.style.opacity = "0";
|
|
223
|
+
container.style.transform = "scale(0.8) translateY(20px)";
|
|
224
|
+
arrow.style.display = "none";
|
|
225
|
+
setTimeout(() => {
|
|
226
|
+
container.style.display = "none";
|
|
227
|
+
}, 250);
|
|
228
|
+
btn.style.transform = "scale(1)";
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
btn.onclick = (event) => {
|
|
232
|
+
event.stopPropagation();
|
|
233
|
+
isOpen = !isOpen;
|
|
234
|
+
if (isOpen) {
|
|
235
|
+
container.style.display = "block";
|
|
236
|
+
arrow.style.display = "block";
|
|
237
|
+
requestAnimationFrame(() => {
|
|
238
|
+
container.style.opacity = "1";
|
|
239
|
+
container.style.transform = "scale(1) translateY(0)";
|
|
240
|
+
});
|
|
241
|
+
} else {
|
|
242
|
+
closeChat();
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
window.addEventListener("message", (event) => {
|
|
247
|
+
if (event?.data?.type === "CHAT_CLOSED") {
|
|
248
|
+
closeChat();
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
document.addEventListener("click", (event) => {
|
|
253
|
+
const target = event.target;
|
|
254
|
+
if (!target) return;
|
|
255
|
+
if (isOpen && !container.contains(target) && target !== btn) {
|
|
256
|
+
closeChat();
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
const controls = {
|
|
261
|
+
close: closeChat,
|
|
262
|
+
open: () => {
|
|
263
|
+
if (!isOpen) {
|
|
264
|
+
btn.click();
|
|
265
|
+
}
|
|
266
|
+
},
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
window.CopilotChat = window.CopilotChat || {};
|
|
270
|
+
window.CopilotChat.close = controls.close;
|
|
271
|
+
window.CopilotChat.open = controls.open;
|
|
272
|
+
|
|
273
|
+
return controls;
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
const handleError = (message) => {
|
|
277
|
+
console.error(`[CopilotChat] ${message}`);
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
const resolveEmbeddingScript = () => {
|
|
281
|
+
if (embeddingScript) {
|
|
282
|
+
return embeddingScript;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const scripts = Array.from(document.querySelectorAll("script"));
|
|
286
|
+
const candidate = scripts
|
|
287
|
+
.reverse()
|
|
288
|
+
.find((script) => script.dataset?.token || script.src.includes("chat-widget"));
|
|
289
|
+
|
|
290
|
+
return candidate || null;
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
ready(async () => {
|
|
294
|
+
const scriptEl = resolveEmbeddingScript();
|
|
295
|
+
if (!scriptEl) {
|
|
296
|
+
handleError("Unable to locate the embedding script tag on the page.");
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const globalConfig = window.CopilotChatConfig || {};
|
|
301
|
+
const datasetConfig = scriptEl.dataset || {};
|
|
302
|
+
const queryConfig = readQueryConfig(scriptEl);
|
|
303
|
+
|
|
304
|
+
const token = globalConfig.token || datasetConfig.token || queryConfig.token;
|
|
305
|
+
if (!token) {
|
|
306
|
+
handleError("Missing token (provide via window.CopilotChatConfig.token or data-token attribute).");
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const baseUrl = normalizeBaseUrl(
|
|
311
|
+
ENV_BASE_URL ||
|
|
312
|
+
globalConfig.baseUrl ||
|
|
313
|
+
datasetConfig.baseUrl ||
|
|
314
|
+
queryConfig.baseUrl ||
|
|
315
|
+
inferBaseUrlFromScript(scriptEl)
|
|
316
|
+
);
|
|
317
|
+
const themeAccent =
|
|
318
|
+
(globalConfig.theme && globalConfig.theme.accent) ||
|
|
319
|
+
globalConfig.accent ||
|
|
320
|
+
datasetConfig.themeAccent ||
|
|
321
|
+
queryConfig.themeAccent;
|
|
322
|
+
|
|
323
|
+
try {
|
|
324
|
+
const response = await fetch(`${baseUrl}/api/chat-widget/config?token=${encodeURIComponent(token)}`, {
|
|
325
|
+
credentials: "omit",
|
|
326
|
+
mode: "cors",
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
if (!response.ok) {
|
|
330
|
+
throw new Error(`Server responded with ${response.status}`);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const config = await response.json();
|
|
334
|
+
if (!config?.iframeUrl || !config?.launcherIcon) {
|
|
335
|
+
throw new Error("Received incomplete widget configuration from server.");
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const baseConfig = {
|
|
339
|
+
iframeUrl: config.iframeUrl,
|
|
340
|
+
launcherIcon: config.launcherIcon,
|
|
341
|
+
theme: {
|
|
342
|
+
accent: themeAccent || config.theme?.accent || undefined,
|
|
343
|
+
},
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
window.CopilotChat = window.CopilotChat || {};
|
|
347
|
+
window.CopilotChat.init = (overrides = {}) => {
|
|
348
|
+
const mergedTheme = {
|
|
349
|
+
accent:
|
|
350
|
+
overrides.theme?.accent ||
|
|
351
|
+
overrides.accent ||
|
|
352
|
+
baseConfig.theme.accent ||
|
|
353
|
+
(typeof globalConfig.theme === "object" ? globalConfig.theme.accent : undefined) ||
|
|
354
|
+
globalConfig.accent,
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
const mergedConfig = {
|
|
358
|
+
iframeUrl: overrides.iframeUrl || baseConfig.iframeUrl,
|
|
359
|
+
launcherIcon: overrides.launcherIcon || baseConfig.launcherIcon,
|
|
360
|
+
theme: mergedTheme,
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
return buildWidget(mergedConfig);
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
window.CopilotChat.init(globalConfig);
|
|
367
|
+
} catch (error) {
|
|
368
|
+
handleError(error instanceof Error ? error.message : "Unknown error during widget bootstrap.");
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
})();
|