@ownhelp/widget 0.0.0-stage → 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/API_CONTRACT.md +199 -0
- package/LICENSE +21 -0
- package/README.md +110 -2
- package/dist/lib/index.js +725 -0
- package/dist/widget.js +190 -0
- package/package.json +60 -5
- package/types/index.d.ts +67 -0
|
@@ -0,0 +1,725 @@
|
|
|
1
|
+
import { jsxs as p, jsx as a, Fragment as xe } from "react/jsx-runtime";
|
|
2
|
+
import { useState as w, useRef as $, useCallback as J, useLayoutEffect as ve, useEffect as Z } from "react";
|
|
3
|
+
import { createPortal as ke } from "react-dom";
|
|
4
|
+
function ue(t) {
|
|
5
|
+
if (typeof t != "string" || t.length > 50) return;
|
|
6
|
+
const e = t.trim(), r = /^(\+?[0-9 ().-]+?)(?:\s*[xX#]\s*(\d+))?$/.exec(e);
|
|
7
|
+
if (!r || !/\d/.test(r[1])) return;
|
|
8
|
+
const n = r[1].replace(/[^+0-9]/g, "");
|
|
9
|
+
return { display: e, uri: `tel:${n}${r[2] ? `;ext=${r[2]}` : ""}` };
|
|
10
|
+
}
|
|
11
|
+
function ee(t) {
|
|
12
|
+
if (!t || t.available !== !0 || typeof t.token != "string" || !t.token.trim()) return;
|
|
13
|
+
const e = { available: !0, token: t.token };
|
|
14
|
+
typeof t.contact_email == "string" && /^[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/.test(t.contact_email) && (e.contactEmail = t.contact_email);
|
|
15
|
+
const r = ue(t.contact_phone);
|
|
16
|
+
return r && (e.contactPhone = r.display), e;
|
|
17
|
+
}
|
|
18
|
+
function Ce(t) {
|
|
19
|
+
const e = ee({
|
|
20
|
+
available: !0,
|
|
21
|
+
token: t?.token,
|
|
22
|
+
contact_email: t?.contactEmail,
|
|
23
|
+
contact_phone: t?.contactPhone
|
|
24
|
+
});
|
|
25
|
+
return {
|
|
26
|
+
email: e?.contactEmail ? `mailto:${encodeURIComponent(e.contactEmail)}` : void 0,
|
|
27
|
+
phone: ue(e?.contactPhone)?.uri
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function _e() {
|
|
31
|
+
let t = 0;
|
|
32
|
+
const e = /* @__PURE__ */ new Set();
|
|
33
|
+
return {
|
|
34
|
+
begin() {
|
|
35
|
+
const r = t, n = new AbortController();
|
|
36
|
+
return e.add(n), {
|
|
37
|
+
signal: n.signal,
|
|
38
|
+
abort: () => n.abort(),
|
|
39
|
+
isCurrent: () => r === t,
|
|
40
|
+
finish: () => e.delete(n)
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
reset() {
|
|
44
|
+
t += 1;
|
|
45
|
+
for (const r of e) r.abort();
|
|
46
|
+
e.clear();
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function Se(t) {
|
|
51
|
+
let e = "", r = "message", n = [];
|
|
52
|
+
function c() {
|
|
53
|
+
n.length > 0 && t({ event: r, data: n.join(`
|
|
54
|
+
`) }), r = "message", n = [];
|
|
55
|
+
}
|
|
56
|
+
function k(l) {
|
|
57
|
+
if (l === "") {
|
|
58
|
+
c();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (l.startsWith(":"))
|
|
62
|
+
return;
|
|
63
|
+
const i = l.indexOf(":"), s = i === -1 ? l : l.slice(0, i);
|
|
64
|
+
let d = i === -1 ? "" : l.slice(i + 1);
|
|
65
|
+
d.startsWith(" ") && (d = d.slice(1)), s === "event" ? r = d : s === "data" && n.push(d);
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
push(l) {
|
|
69
|
+
e += l;
|
|
70
|
+
let i;
|
|
71
|
+
for (; (i = e.search(/\r\n|\n|\r/)) !== -1; ) {
|
|
72
|
+
const s = e.startsWith(`\r
|
|
73
|
+
`, i);
|
|
74
|
+
if (!s && e[i] === "\r" && i === e.length - 1)
|
|
75
|
+
return;
|
|
76
|
+
const d = e.slice(0, i);
|
|
77
|
+
e = e.slice(i + (s ? 2 : 1)), k(d);
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
/** Call when the stream ends; dispatches nothing that was not terminated by a blank line. */
|
|
81
|
+
flush() {
|
|
82
|
+
e = "", r = "message", n = [];
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
class h extends Error {
|
|
87
|
+
constructor(e, r, n, c) {
|
|
88
|
+
super(r), this.name = "ChatError", this.code = e, this.status = n, this.retryAfterSec = c;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const Ee = 3e4;
|
|
92
|
+
function de(t) {
|
|
93
|
+
return `${String(t).replace(/\/+$/, "")}/api/chat`;
|
|
94
|
+
}
|
|
95
|
+
async function fe(t) {
|
|
96
|
+
let e = `http_${t.status}`, r = `Request failed (${t.status})`;
|
|
97
|
+
try {
|
|
98
|
+
const c = await t.json();
|
|
99
|
+
c && c.error && (e = c.error.code || e, r = c.error.message || r);
|
|
100
|
+
} catch {
|
|
101
|
+
}
|
|
102
|
+
const n = Number(t.headers && t.headers.get && t.headers.get("retry-after"));
|
|
103
|
+
return new h(e, r, t.status, Number.isFinite(n) && n > 0 ? n : void 0);
|
|
104
|
+
}
|
|
105
|
+
async function Ne({
|
|
106
|
+
apiUrl: t,
|
|
107
|
+
tenantId: e,
|
|
108
|
+
message: r,
|
|
109
|
+
sessionId: n,
|
|
110
|
+
signal: c,
|
|
111
|
+
onToken: k = () => {
|
|
112
|
+
},
|
|
113
|
+
idleTimeoutMs: l = Ee,
|
|
114
|
+
fetchImpl: i = (...s) => fetch(...s)
|
|
115
|
+
}) {
|
|
116
|
+
const s = new AbortController();
|
|
117
|
+
let d = !1, N = null;
|
|
118
|
+
const P = () => {
|
|
119
|
+
l && (clearTimeout(N), N = setTimeout(() => {
|
|
120
|
+
d = !0, s.abort();
|
|
121
|
+
}, l));
|
|
122
|
+
}, U = () => s.abort();
|
|
123
|
+
if (c) {
|
|
124
|
+
if (c.aborted)
|
|
125
|
+
throw new h("aborted", "Request cancelled");
|
|
126
|
+
c.addEventListener("abort", U, { once: !0 });
|
|
127
|
+
}
|
|
128
|
+
const q = (f) => f instanceof h ? f : d ? new h("timeout", "The assistant took too long to respond") : s.signal.aborted ? new h("aborted", "Request cancelled") : new h("network_error", "Could not reach the support service");
|
|
129
|
+
try {
|
|
130
|
+
P();
|
|
131
|
+
const f = await i(de(t), {
|
|
132
|
+
method: "POST",
|
|
133
|
+
headers: {
|
|
134
|
+
"Content-Type": "application/json",
|
|
135
|
+
Accept: "text/event-stream, application/json"
|
|
136
|
+
},
|
|
137
|
+
body: JSON.stringify({
|
|
138
|
+
tenant_id: e,
|
|
139
|
+
message: r,
|
|
140
|
+
session_id: n
|
|
141
|
+
}),
|
|
142
|
+
signal: s.signal
|
|
143
|
+
});
|
|
144
|
+
if (!f.ok)
|
|
145
|
+
throw await fe(f);
|
|
146
|
+
if (!(f.headers.get("content-type") || "").includes("text/event-stream") || !f.body) {
|
|
147
|
+
const b = await f.json(), g = String(b.answer ?? "");
|
|
148
|
+
g && k(g);
|
|
149
|
+
const y = ee(b.follow_up);
|
|
150
|
+
return { answer: g, sessionId: b.session_id ?? n, ...y ? { followUp: y } : {} };
|
|
151
|
+
}
|
|
152
|
+
let L = "", _ = !1, S, m = null;
|
|
153
|
+
const T = Se(({ event: b, data: g }) => {
|
|
154
|
+
let y = {};
|
|
155
|
+
try {
|
|
156
|
+
y = g ? JSON.parse(g) : {};
|
|
157
|
+
} catch {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (b === "token") {
|
|
161
|
+
const I = String(y.text ?? "");
|
|
162
|
+
L += I, k(I);
|
|
163
|
+
} else b === "error" ? m = new h(
|
|
164
|
+
y.code || "stream_error",
|
|
165
|
+
y.message || "The assistant ran into a problem"
|
|
166
|
+
) : b === "done" && (_ = !0, S = ee(y.follow_up));
|
|
167
|
+
}), j = f.body.getReader(), C = new TextDecoder();
|
|
168
|
+
for (; ; ) {
|
|
169
|
+
const { done: b, value: g } = await j.read();
|
|
170
|
+
if (b)
|
|
171
|
+
break;
|
|
172
|
+
if (P(), T.push(C.decode(g, { stream: !0 })), m)
|
|
173
|
+
throw await j.cancel().catch(() => {
|
|
174
|
+
}), m;
|
|
175
|
+
if (_) {
|
|
176
|
+
await j.cancel().catch(() => {
|
|
177
|
+
});
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (T.push(C.decode()), T.flush(), m)
|
|
182
|
+
throw m;
|
|
183
|
+
if (!_)
|
|
184
|
+
throw new h("stream_interrupted", "The response ended unexpectedly");
|
|
185
|
+
return { answer: L, sessionId: n, ...S ? { followUp: S } : {} };
|
|
186
|
+
} catch (f) {
|
|
187
|
+
throw q(f);
|
|
188
|
+
} finally {
|
|
189
|
+
clearTimeout(N), c && c.removeEventListener("abort", U);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
async function Te({
|
|
193
|
+
apiUrl: t,
|
|
194
|
+
tenantId: e,
|
|
195
|
+
sessionId: r,
|
|
196
|
+
followUpToken: n,
|
|
197
|
+
name: c,
|
|
198
|
+
email: k,
|
|
199
|
+
signal: l,
|
|
200
|
+
fetchImpl: i = (...s) => fetch(...s)
|
|
201
|
+
}) {
|
|
202
|
+
try {
|
|
203
|
+
if (l?.aborted) throw new h("aborted", "Request cancelled");
|
|
204
|
+
const s = await i(`${de(t)}/contact`, {
|
|
205
|
+
method: "POST",
|
|
206
|
+
headers: { "Content-Type": "application/json", Accept: "application/json" },
|
|
207
|
+
body: JSON.stringify({
|
|
208
|
+
tenant_id: e,
|
|
209
|
+
session_id: r,
|
|
210
|
+
follow_up_token: n,
|
|
211
|
+
name: c.trim(),
|
|
212
|
+
email: k.trim()
|
|
213
|
+
}),
|
|
214
|
+
signal: l
|
|
215
|
+
});
|
|
216
|
+
if (!s.ok) throw await fe(s);
|
|
217
|
+
if ((await s.json()).status !== "saved") throw new h("invalid_response", "Could not confirm save");
|
|
218
|
+
return { status: "saved" };
|
|
219
|
+
} catch (s) {
|
|
220
|
+
throw l?.aborted ? new h("aborted", "Request cancelled") : s instanceof h ? s : new h("network_error", "Could not save contact details");
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
const R = /* @__PURE__ */ new Map();
|
|
224
|
+
function pe() {
|
|
225
|
+
return typeof crypto < "u" && typeof crypto.randomUUID == "function" ? crypto.randomUUID() : `s_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 12)}`;
|
|
226
|
+
}
|
|
227
|
+
function we() {
|
|
228
|
+
try {
|
|
229
|
+
return typeof localStorage < "u" ? localStorage : null;
|
|
230
|
+
} catch {
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
function ae(t, e = we()) {
|
|
235
|
+
const r = `support_widget_session:${t}`;
|
|
236
|
+
if (R.has(r)) return R.get(r);
|
|
237
|
+
try {
|
|
238
|
+
const c = e ? e.getItem(r) : null;
|
|
239
|
+
if (c)
|
|
240
|
+
return R.set(r, c), c;
|
|
241
|
+
} catch {
|
|
242
|
+
}
|
|
243
|
+
if (R.has(r))
|
|
244
|
+
return R.get(r);
|
|
245
|
+
const n = pe();
|
|
246
|
+
R.set(r, n);
|
|
247
|
+
try {
|
|
248
|
+
e && e.setItem(r, n);
|
|
249
|
+
} catch {
|
|
250
|
+
}
|
|
251
|
+
return n;
|
|
252
|
+
}
|
|
253
|
+
function $e(t, e = we()) {
|
|
254
|
+
const r = `support_widget_session:${t}`, n = pe();
|
|
255
|
+
R.set(r, n);
|
|
256
|
+
try {
|
|
257
|
+
e?.setItem(r, n);
|
|
258
|
+
} catch {
|
|
259
|
+
}
|
|
260
|
+
return n;
|
|
261
|
+
}
|
|
262
|
+
const he = "The support assistant is temporarily unavailable. Please try again shortly.", se = "Something went wrong. Please try again.", ce = {
|
|
263
|
+
network_error: {
|
|
264
|
+
message: "We couldn't reach the support service. Please check your connection and try again.",
|
|
265
|
+
retryable: !0
|
|
266
|
+
},
|
|
267
|
+
timeout: { message: "The assistant took too long to respond. Please try again.", retryable: !0 },
|
|
268
|
+
stream_interrupted: {
|
|
269
|
+
message: "The connection was interrupted before the reply finished. Please try again.",
|
|
270
|
+
retryable: !0
|
|
271
|
+
},
|
|
272
|
+
generation_failed: { message: "The assistant ran into a problem. Please try again.", retryable: !0 },
|
|
273
|
+
stream_error: { message: "The assistant ran into a problem. Please try again.", retryable: !0 },
|
|
274
|
+
service_unavailable: { message: he, retryable: !0 },
|
|
275
|
+
message_too_long: {
|
|
276
|
+
message: "That message is too long. Please shorten it (2000 characters at most) and send it again.",
|
|
277
|
+
retryable: !1
|
|
278
|
+
},
|
|
279
|
+
invalid_request: { message: "That message couldn't be sent. Please edit it and try again.", retryable: !1 },
|
|
280
|
+
unknown_tenant: { message: "The support chat isn't available on this site.", retryable: !1 },
|
|
281
|
+
origin_not_allowed: { message: "The support chat isn't available on this site.", retryable: !1 }
|
|
282
|
+
};
|
|
283
|
+
function me(t) {
|
|
284
|
+
const e = t && t.code;
|
|
285
|
+
if (e === "rate_limited") {
|
|
286
|
+
const n = Number(t.retryAfterSec);
|
|
287
|
+
return { message: `You're sending messages too quickly. Please wait ${Number.isFinite(n) && n > 0 ? n > 90 ? `${Math.ceil(n / 60)} minutes` : `${Math.ceil(n)} seconds` : "a moment"} and try again.`, retryable: !0 };
|
|
288
|
+
}
|
|
289
|
+
if (e && ce[e])
|
|
290
|
+
return ce[e];
|
|
291
|
+
const r = Number(t && t.status);
|
|
292
|
+
return r >= 500 ? { message: he, retryable: !0 } : r >= 400 ? { message: se, retryable: !1 } : { message: se, retryable: !0 };
|
|
293
|
+
}
|
|
294
|
+
function Re(t) {
|
|
295
|
+
return me(t).message;
|
|
296
|
+
}
|
|
297
|
+
function Pe(t) {
|
|
298
|
+
return me(t).retryable;
|
|
299
|
+
}
|
|
300
|
+
const Ae = `
|
|
301
|
+
:host { all: initial; }
|
|
302
|
+
.cw-root {
|
|
303
|
+
--cw-accent: #1f6feb;
|
|
304
|
+
--cw-accent-text: #ffffff;
|
|
305
|
+
--cw-bg: #ffffff;
|
|
306
|
+
--cw-surface: #f3f4f6;
|
|
307
|
+
--cw-text: #111827;
|
|
308
|
+
--cw-muted: #6b7280;
|
|
309
|
+
--cw-border: #e5e7eb;
|
|
310
|
+
--cw-error: #b91c1c;
|
|
311
|
+
position: fixed;
|
|
312
|
+
bottom: 20px;
|
|
313
|
+
z-index: 2147483000;
|
|
314
|
+
font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
|
|
315
|
+
font-size: 14px;
|
|
316
|
+
line-height: 1.45;
|
|
317
|
+
color: var(--cw-text);
|
|
318
|
+
}
|
|
319
|
+
.cw-root.cw-right { right: 20px; }
|
|
320
|
+
.cw-root.cw-left { left: 20px; }
|
|
321
|
+
.cw-root *, .cw-root *::before, .cw-root *::after { box-sizing: border-box; }
|
|
322
|
+
|
|
323
|
+
@media (prefers-color-scheme: dark) {
|
|
324
|
+
.cw-root {
|
|
325
|
+
--cw-bg: #111827;
|
|
326
|
+
--cw-surface: #1f2937;
|
|
327
|
+
--cw-text: #f9fafb;
|
|
328
|
+
--cw-muted: #9ca3af;
|
|
329
|
+
--cw-border: #374151;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
.cw-launcher {
|
|
334
|
+
width: 56px;
|
|
335
|
+
height: 56px;
|
|
336
|
+
border: none;
|
|
337
|
+
border-radius: 50%;
|
|
338
|
+
background: var(--cw-accent);
|
|
339
|
+
color: var(--cw-accent-text);
|
|
340
|
+
cursor: pointer;
|
|
341
|
+
box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
|
|
342
|
+
display: flex;
|
|
343
|
+
align-items: center;
|
|
344
|
+
justify-content: center;
|
|
345
|
+
}
|
|
346
|
+
.cw-launcher:focus-visible, .cw-send:focus-visible, .cw-close:focus-visible, .cw-input:focus-visible {
|
|
347
|
+
outline: 2px solid var(--cw-accent);
|
|
348
|
+
outline-offset: 2px;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
.cw-panel {
|
|
352
|
+
width: 360px;
|
|
353
|
+
max-width: calc(100vw - 24px);
|
|
354
|
+
height: 520px;
|
|
355
|
+
max-height: calc(100vh - 40px);
|
|
356
|
+
background: var(--cw-bg);
|
|
357
|
+
border: 1px solid var(--cw-border);
|
|
358
|
+
border-radius: 14px;
|
|
359
|
+
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.28);
|
|
360
|
+
display: flex;
|
|
361
|
+
flex-direction: column;
|
|
362
|
+
overflow: hidden;
|
|
363
|
+
}
|
|
364
|
+
.cw-header {
|
|
365
|
+
display: flex;
|
|
366
|
+
align-items: center;
|
|
367
|
+
justify-content: space-between;
|
|
368
|
+
padding: 12px 14px;
|
|
369
|
+
background: var(--cw-accent);
|
|
370
|
+
color: var(--cw-accent-text);
|
|
371
|
+
font-weight: 600;
|
|
372
|
+
}
|
|
373
|
+
.cw-header > span { flex: 1; }
|
|
374
|
+
.cw-new-chat { border: 1px solid currentColor; border-radius: 6px; background: transparent; color: inherit; padding: 4px 7px; cursor: pointer; font: inherit; font-size: 12px; }
|
|
375
|
+
.cw-close {
|
|
376
|
+
background: transparent;
|
|
377
|
+
border: none;
|
|
378
|
+
color: inherit;
|
|
379
|
+
cursor: pointer;
|
|
380
|
+
font-size: 20px;
|
|
381
|
+
line-height: 1;
|
|
382
|
+
padding: 2px 6px;
|
|
383
|
+
}
|
|
384
|
+
.cw-messages {
|
|
385
|
+
flex: 1;
|
|
386
|
+
overflow-y: auto;
|
|
387
|
+
padding: 14px;
|
|
388
|
+
display: flex;
|
|
389
|
+
flex-direction: column;
|
|
390
|
+
gap: 10px;
|
|
391
|
+
}
|
|
392
|
+
.cw-msg {
|
|
393
|
+
max-width: 85%;
|
|
394
|
+
padding: 8px 12px;
|
|
395
|
+
border-radius: 12px;
|
|
396
|
+
white-space: pre-wrap;
|
|
397
|
+
word-wrap: break-word;
|
|
398
|
+
}
|
|
399
|
+
.cw-msg.cw-user {
|
|
400
|
+
align-self: flex-end;
|
|
401
|
+
background: var(--cw-accent);
|
|
402
|
+
color: var(--cw-accent-text);
|
|
403
|
+
border-bottom-right-radius: 4px;
|
|
404
|
+
}
|
|
405
|
+
.cw-msg.cw-assistant {
|
|
406
|
+
align-self: flex-start;
|
|
407
|
+
background: var(--cw-surface);
|
|
408
|
+
border-bottom-left-radius: 4px;
|
|
409
|
+
}
|
|
410
|
+
.cw-msg.cw-error {
|
|
411
|
+
color: var(--cw-error);
|
|
412
|
+
border: 1px solid var(--cw-error);
|
|
413
|
+
background: transparent;
|
|
414
|
+
}
|
|
415
|
+
.cw-retry {
|
|
416
|
+
margin-top: 6px;
|
|
417
|
+
background: transparent;
|
|
418
|
+
border: 1px solid currentColor;
|
|
419
|
+
color: inherit;
|
|
420
|
+
border-radius: 6px;
|
|
421
|
+
padding: 2px 8px;
|
|
422
|
+
cursor: pointer;
|
|
423
|
+
font-size: 12px;
|
|
424
|
+
}
|
|
425
|
+
.cw-typing::after {
|
|
426
|
+
content: "\\2026";
|
|
427
|
+
display: inline-block;
|
|
428
|
+
animation: cw-blink 1s steps(3, end) infinite;
|
|
429
|
+
}
|
|
430
|
+
@keyframes cw-blink { 50% { opacity: 0.25; } }
|
|
431
|
+
.cw-slow {
|
|
432
|
+
margin-left: 6px;
|
|
433
|
+
font-size: 12px;
|
|
434
|
+
opacity: 0.7;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
.cw-follow-up { border: 1px solid var(--cw-border); border-radius: 8px; padding: 10px; background: var(--cw-surface); }
|
|
438
|
+
.cw-follow-up p { margin: 6px 0; font-size: 12px; }
|
|
439
|
+
.cw-follow-up a { color: var(--cw-text); text-decoration: underline; }
|
|
440
|
+
.cw-contact-form { display: flex; flex-direction: column; gap: 8px; }
|
|
441
|
+
.cw-contact-form label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; }
|
|
442
|
+
.cw-contact-form .cw-send { padding: 9px; }
|
|
443
|
+
.cw-contact-form [role="alert"] { color: var(--cw-error); }
|
|
444
|
+
.cw-new-chat:focus-visible, .cw-contact-form input:focus-visible { outline: 2px solid currentColor; outline-offset: 2px; }
|
|
445
|
+
.cw-form {
|
|
446
|
+
display: flex;
|
|
447
|
+
gap: 8px;
|
|
448
|
+
padding: 10px;
|
|
449
|
+
border-top: 1px solid var(--cw-border);
|
|
450
|
+
}
|
|
451
|
+
.cw-input {
|
|
452
|
+
min-width: 0;
|
|
453
|
+
flex: 1;
|
|
454
|
+
border: 1px solid var(--cw-border);
|
|
455
|
+
background: var(--cw-bg);
|
|
456
|
+
color: var(--cw-text);
|
|
457
|
+
border-radius: 8px;
|
|
458
|
+
padding: 8px 10px;
|
|
459
|
+
font: inherit;
|
|
460
|
+
}
|
|
461
|
+
.cw-send {
|
|
462
|
+
border: none;
|
|
463
|
+
border-radius: 8px;
|
|
464
|
+
padding: 0 14px;
|
|
465
|
+
background: var(--cw-accent);
|
|
466
|
+
color: var(--cw-accent-text);
|
|
467
|
+
cursor: pointer;
|
|
468
|
+
font: inherit;
|
|
469
|
+
font-weight: 600;
|
|
470
|
+
}
|
|
471
|
+
.cw-send:disabled { opacity: 0.5; cursor: not-allowed; }
|
|
472
|
+
.cw-footer { padding: 0 10px 8px; font-size: 11px; color: var(--cw-muted); text-align: center; }
|
|
473
|
+
|
|
474
|
+
@media (prefers-reduced-motion: reduce) {
|
|
475
|
+
.cw-typing::after { animation: none; }
|
|
476
|
+
}
|
|
477
|
+
`, ie = 2e3, Le = 6e3;
|
|
478
|
+
let le = 0;
|
|
479
|
+
function V() {
|
|
480
|
+
return le += 1, `m${le}`;
|
|
481
|
+
}
|
|
482
|
+
function Ie({
|
|
483
|
+
apiUrl: t,
|
|
484
|
+
tenantId: e,
|
|
485
|
+
title: r = "Support",
|
|
486
|
+
greeting: n = "Hi! How can I help you today?",
|
|
487
|
+
position: c = "right",
|
|
488
|
+
accentColor: k,
|
|
489
|
+
defaultOpen: l = !1,
|
|
490
|
+
fetchImpl: i
|
|
491
|
+
}) {
|
|
492
|
+
const [s, d] = w(l), [N, P] = w(""), [U, q] = w(!1), [f, A] = w(!1), [L, _] = w(() => [
|
|
493
|
+
{ id: V(), role: "assistant", text: n, status: "done" }
|
|
494
|
+
]), S = $(null), m = $(null);
|
|
495
|
+
m.current || (m.current = _e());
|
|
496
|
+
const [T, j] = w(() => ae(e)), [C, b] = w(null), [g, y] = w(""), [I, X] = w(""), [z, D] = w("idle"), [te, W] = w(""), F = $(!1), H = $(!1), re = $(null), Y = $(null), K = $(""), ne = J((o) => {
|
|
497
|
+
m.current.reset(), S.current = null, H.current = !1, F.current = !1, K.current = "", j(o ? $e(e) : ae(e)), _([{ id: V(), role: "assistant", text: n, status: "done" }]), P(""), q(!1), A(!1), b(null), y(""), X(""), D("idle"), W(""), Y.current?.focus();
|
|
498
|
+
}, [e, n]);
|
|
499
|
+
ve(() => {
|
|
500
|
+
ne(!1);
|
|
501
|
+
}, [e, t]), Z(() => {
|
|
502
|
+
const o = re.current;
|
|
503
|
+
o && (o.scrollTop = o.scrollHeight);
|
|
504
|
+
}, [L, s]), Z(() => {
|
|
505
|
+
s && Y.current && Y.current.focus();
|
|
506
|
+
}, [s]), Z(() => () => m.current.reset(), []);
|
|
507
|
+
const M = J((o, u) => {
|
|
508
|
+
_((v) => v.map((x) => x.id === o ? { ...x, ...u } : x));
|
|
509
|
+
}, []), Q = J(
|
|
510
|
+
async (o) => {
|
|
511
|
+
const u = o.trim().slice(0, ie);
|
|
512
|
+
if (!u || H.current)
|
|
513
|
+
return;
|
|
514
|
+
H.current = !0, K.current = u;
|
|
515
|
+
const v = V();
|
|
516
|
+
_((E) => [
|
|
517
|
+
...E,
|
|
518
|
+
{ id: V(), role: "user", text: u, status: "done" },
|
|
519
|
+
{ id: v, role: "assistant", text: "", status: "streaming" }
|
|
520
|
+
]), P(""), q(!0);
|
|
521
|
+
const x = m.current.begin();
|
|
522
|
+
S.current = x;
|
|
523
|
+
let O = "";
|
|
524
|
+
A(!1);
|
|
525
|
+
const oe = setTimeout(() => {
|
|
526
|
+
x.isCurrent() && A(!0);
|
|
527
|
+
}, Le);
|
|
528
|
+
try {
|
|
529
|
+
const E = await Ne({
|
|
530
|
+
apiUrl: t,
|
|
531
|
+
tenantId: e,
|
|
532
|
+
message: u,
|
|
533
|
+
sessionId: T,
|
|
534
|
+
signal: x.signal,
|
|
535
|
+
fetchImpl: i,
|
|
536
|
+
onToken: (B) => {
|
|
537
|
+
x.isCurrent() && (clearTimeout(oe), A(!1), O += B, M(v, { text: O }));
|
|
538
|
+
}
|
|
539
|
+
});
|
|
540
|
+
if (!x.isCurrent()) return;
|
|
541
|
+
M(v, { status: "done" }), E.followUp && (b(E.followUp), W(""));
|
|
542
|
+
} catch (E) {
|
|
543
|
+
if (!x.isCurrent()) return;
|
|
544
|
+
if (E.code === "aborted")
|
|
545
|
+
M(v, { status: "done", text: O || "(stopped)" });
|
|
546
|
+
else {
|
|
547
|
+
const B = Re(E);
|
|
548
|
+
M(v, {
|
|
549
|
+
status: "error",
|
|
550
|
+
retryable: Pe(E),
|
|
551
|
+
text: O ? `${O}
|
|
552
|
+
|
|
553
|
+
${B}` : B
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
} finally {
|
|
557
|
+
clearTimeout(oe), x.finish(), x.isCurrent() && (S.current = null, H.current = !1, A(!1), q(!1));
|
|
558
|
+
}
|
|
559
|
+
},
|
|
560
|
+
[t, e, T, i, M]
|
|
561
|
+
), be = J(() => {
|
|
562
|
+
const o = K.current;
|
|
563
|
+
o && (_((u) => u.slice(0, -2)), Q(o));
|
|
564
|
+
}, [Q]), ge = (o) => {
|
|
565
|
+
o.preventDefault(), Q(N);
|
|
566
|
+
}, ye = async (o) => {
|
|
567
|
+
if (o.preventDefault(), !C || F.current || z === "saved") return;
|
|
568
|
+
F.current = !0;
|
|
569
|
+
const u = m.current.begin();
|
|
570
|
+
D("pending"), W("");
|
|
571
|
+
const v = setTimeout(() => u.abort(), 3e4);
|
|
572
|
+
try {
|
|
573
|
+
await Te({
|
|
574
|
+
apiUrl: t,
|
|
575
|
+
tenantId: e,
|
|
576
|
+
sessionId: T,
|
|
577
|
+
followUpToken: C.token,
|
|
578
|
+
name: g,
|
|
579
|
+
email: I,
|
|
580
|
+
signal: u.signal,
|
|
581
|
+
fetchImpl: i
|
|
582
|
+
}), u.isCurrent() && (D("saved"), y(""), X(""));
|
|
583
|
+
} catch {
|
|
584
|
+
u.isCurrent() && (D("error"), W("Your details could not be saved. Please try again or contact the business directly."));
|
|
585
|
+
} finally {
|
|
586
|
+
clearTimeout(v), u.finish(), u.isCurrent() && (F.current = !1);
|
|
587
|
+
}
|
|
588
|
+
}, G = Ce(C);
|
|
589
|
+
return /* @__PURE__ */ p("div", { className: `cw-root ${c === "left" ? "cw-left" : "cw-right"}`, style: k ? { "--cw-accent": k } : void 0, children: [
|
|
590
|
+
/* @__PURE__ */ a("style", { children: Ae }),
|
|
591
|
+
s ? /* @__PURE__ */ p("section", { className: "cw-panel", role: "dialog", "aria-label": r, children: [
|
|
592
|
+
/* @__PURE__ */ p("header", { className: "cw-header", children: [
|
|
593
|
+
/* @__PURE__ */ a("span", { children: r }),
|
|
594
|
+
/* @__PURE__ */ a("button", { type: "button", className: "cw-new-chat", onClick: () => ne(!0), children: "New chat" }),
|
|
595
|
+
/* @__PURE__ */ a(
|
|
596
|
+
"button",
|
|
597
|
+
{
|
|
598
|
+
type: "button",
|
|
599
|
+
className: "cw-close",
|
|
600
|
+
"aria-label": "Close chat",
|
|
601
|
+
onClick: () => d(!1),
|
|
602
|
+
children: "×"
|
|
603
|
+
}
|
|
604
|
+
)
|
|
605
|
+
] }),
|
|
606
|
+
/* @__PURE__ */ p("div", { className: "cw-messages", ref: re, role: "log", "aria-live": "polite", children: [
|
|
607
|
+
L.map((o, u) => {
|
|
608
|
+
const v = u === L.length - 1;
|
|
609
|
+
return /* @__PURE__ */ p(
|
|
610
|
+
"div",
|
|
611
|
+
{
|
|
612
|
+
className: `cw-msg cw-${o.role}${o.status === "error" ? " cw-error" : ""}`,
|
|
613
|
+
children: [
|
|
614
|
+
o.text,
|
|
615
|
+
o.status === "streaming" && !o.text ? /* @__PURE__ */ p(xe, { children: [
|
|
616
|
+
/* @__PURE__ */ a("span", { className: "cw-typing", "aria-label": "Assistant is typing" }),
|
|
617
|
+
f ? /* @__PURE__ */ a("span", { className: "cw-slow", children: "Still working on your answer…" }) : null
|
|
618
|
+
] }) : null,
|
|
619
|
+
o.status === "error" && o.retryable !== !1 && v && !U ? /* @__PURE__ */ a("div", { children: /* @__PURE__ */ a("button", { type: "button", className: "cw-retry", onClick: be, children: "Retry" }) }) : null
|
|
620
|
+
]
|
|
621
|
+
},
|
|
622
|
+
o.id
|
|
623
|
+
);
|
|
624
|
+
}),
|
|
625
|
+
C ? /* @__PURE__ */ p("section", { className: "cw-follow-up", "aria-label": "Email follow-up", children: [
|
|
626
|
+
/* @__PURE__ */ a("strong", { children: "Email follow-up" }),
|
|
627
|
+
/* @__PURE__ */ a("p", { children: "Email follow-up is not immediate. For time-sensitive help, contact the business directly." }),
|
|
628
|
+
G.phone ? /* @__PURE__ */ p("p", { children: [
|
|
629
|
+
"Call ",
|
|
630
|
+
/* @__PURE__ */ a("a", { href: G.phone, target: "_blank", rel: "noopener noreferrer", children: C.contactPhone })
|
|
631
|
+
] }) : null,
|
|
632
|
+
G.email ? /* @__PURE__ */ p("p", { children: [
|
|
633
|
+
"Email ",
|
|
634
|
+
/* @__PURE__ */ a("a", { href: G.email, target: "_blank", rel: "noopener noreferrer", children: C.contactEmail })
|
|
635
|
+
] }) : null,
|
|
636
|
+
z === "saved" ? /* @__PURE__ */ a("p", { role: "status", children: "Your details were saved for the team to review and follow up by email." }) : /* @__PURE__ */ p("form", { className: "cw-contact-form", onSubmit: ye, children: [
|
|
637
|
+
/* @__PURE__ */ a("p", { children: "Leave your name and email to request follow-up about this conversation." }),
|
|
638
|
+
/* @__PURE__ */ p("label", { children: [
|
|
639
|
+
"Name",
|
|
640
|
+
/* @__PURE__ */ a(
|
|
641
|
+
"input",
|
|
642
|
+
{
|
|
643
|
+
className: "cw-input",
|
|
644
|
+
name: "name",
|
|
645
|
+
autoComplete: "name",
|
|
646
|
+
required: !0,
|
|
647
|
+
maxLength: 100,
|
|
648
|
+
value: g,
|
|
649
|
+
disabled: z === "pending",
|
|
650
|
+
onChange: (o) => y(o.target.value)
|
|
651
|
+
}
|
|
652
|
+
)
|
|
653
|
+
] }),
|
|
654
|
+
/* @__PURE__ */ p("label", { children: [
|
|
655
|
+
"Email",
|
|
656
|
+
/* @__PURE__ */ a(
|
|
657
|
+
"input",
|
|
658
|
+
{
|
|
659
|
+
className: "cw-input",
|
|
660
|
+
name: "email",
|
|
661
|
+
type: "email",
|
|
662
|
+
autoComplete: "email",
|
|
663
|
+
required: !0,
|
|
664
|
+
maxLength: 254,
|
|
665
|
+
value: I,
|
|
666
|
+
disabled: z === "pending",
|
|
667
|
+
onChange: (o) => X(o.target.value)
|
|
668
|
+
}
|
|
669
|
+
)
|
|
670
|
+
] }),
|
|
671
|
+
te ? /* @__PURE__ */ a("p", { role: "alert", children: te }) : null,
|
|
672
|
+
/* @__PURE__ */ a("button", { className: "cw-send", type: "submit", disabled: z === "pending" || !g.trim() || !I.trim(), children: z === "pending" ? "Saving…" : "Request email follow-up" })
|
|
673
|
+
] })
|
|
674
|
+
] }) : null
|
|
675
|
+
] }),
|
|
676
|
+
/* @__PURE__ */ p("form", { className: "cw-form", onSubmit: ge, children: [
|
|
677
|
+
/* @__PURE__ */ a(
|
|
678
|
+
"input",
|
|
679
|
+
{
|
|
680
|
+
ref: Y,
|
|
681
|
+
className: "cw-input",
|
|
682
|
+
value: N,
|
|
683
|
+
maxLength: ie,
|
|
684
|
+
placeholder: "Type your question…",
|
|
685
|
+
"aria-label": "Your message",
|
|
686
|
+
onChange: (o) => P(o.target.value)
|
|
687
|
+
}
|
|
688
|
+
),
|
|
689
|
+
U ? /* @__PURE__ */ a(
|
|
690
|
+
"button",
|
|
691
|
+
{
|
|
692
|
+
type: "button",
|
|
693
|
+
className: "cw-send",
|
|
694
|
+
onClick: () => S.current?.abort(),
|
|
695
|
+
children: "Stop"
|
|
696
|
+
}
|
|
697
|
+
) : /* @__PURE__ */ a("button", { type: "submit", className: "cw-send", disabled: !N.trim(), children: "Send" })
|
|
698
|
+
] }),
|
|
699
|
+
/* @__PURE__ */ a("div", { className: "cw-footer", children: "AI assistant – answers may contain mistakes." })
|
|
700
|
+
] }) : /* @__PURE__ */ a(
|
|
701
|
+
"button",
|
|
702
|
+
{
|
|
703
|
+
type: "button",
|
|
704
|
+
className: "cw-launcher",
|
|
705
|
+
"aria-label": `Open ${r} chat`,
|
|
706
|
+
onClick: () => d(!0),
|
|
707
|
+
children: /* @__PURE__ */ a("svg", { width: "26", height: "26", viewBox: "0 0 24 24", fill: "currentColor", "aria-hidden": "true", children: /* @__PURE__ */ a("path", { d: "M4 3h16a2 2 0 0 1 2 2v11a2 2 0 0 1-2 2H9l-5 4v-4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z" }) })
|
|
708
|
+
}
|
|
709
|
+
)
|
|
710
|
+
] });
|
|
711
|
+
}
|
|
712
|
+
function Oe(t) {
|
|
713
|
+
const [e, r] = w(null), [n, c] = w(null);
|
|
714
|
+
return Z(() => {
|
|
715
|
+
e && c(e.shadowRoot || e.attachShadow({ mode: "open" }));
|
|
716
|
+
}, [e]), /* @__PURE__ */ a("div", { ref: r, "data-support-widget": t.tenantId, children: n ? ke(/* @__PURE__ */ a(Ie, { ...t }), n) : null });
|
|
717
|
+
}
|
|
718
|
+
export {
|
|
719
|
+
h as ChatError,
|
|
720
|
+
Oe as ChatWidget,
|
|
721
|
+
ae as getSessionId,
|
|
722
|
+
$e as newSessionId,
|
|
723
|
+
Ne as streamChat,
|
|
724
|
+
Te as submitContact
|
|
725
|
+
};
|