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