sagedesk 1.0.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.
@@ -0,0 +1,1370 @@
1
+ 'use client';
2
+
3
+ // src/react/SageDeskWidget.tsx
4
+ import {
5
+ useState as useState2,
6
+ useRef as useRef2,
7
+ useEffect as useEffect2,
8
+ useCallback as useCallback2
9
+ } from "react";
10
+ import { createPortal } from "react-dom";
11
+
12
+ // src/react/useSageDesk.ts
13
+ import { useState, useEffect, useReducer, useCallback, useRef, useMemo } from "react";
14
+
15
+ // src/core/embedder.ts
16
+ var XENOVA_IDS = {
17
+ "all-MiniLM-L6-v2": "Xenova/all-MiniLM-L6-v2",
18
+ "bge-small-en-v1-5": "Xenova/bge-small-en-v1.5",
19
+ "paraphrase-multilingual-MiniLM-L12-v2": "Xenova/paraphrase-multilingual-MiniLM-L12-v2",
20
+ "all-mpnet-base-v2": "Xenova/all-mpnet-base-v2"
21
+ };
22
+ var _EmbedderRuntime = class _EmbedderRuntime {
23
+ constructor() {
24
+ this._ready = false;
25
+ this._failed = false;
26
+ }
27
+ async load(model = "all-MiniLM-L6-v2") {
28
+ if (this._ready) return;
29
+ if (this._failed) throw new Error("EmbedderRuntime previously failed to load");
30
+ if (_EmbedderRuntime._loadingPromise) {
31
+ await _EmbedderRuntime._loadingPromise;
32
+ this._ready = true;
33
+ return;
34
+ }
35
+ const modelId = XENOVA_IDS[model];
36
+ _EmbedderRuntime._loadingPromise = (async () => {
37
+ try {
38
+ const { pipeline } = await import("@huggingface/transformers");
39
+ _EmbedderRuntime._pipelineInstance = await pipeline(
40
+ "feature-extraction",
41
+ modelId,
42
+ { dtype: "q8" }
43
+ );
44
+ } catch (err) {
45
+ _EmbedderRuntime._loadingPromise = null;
46
+ _EmbedderRuntime._pipelineInstance = null;
47
+ throw err;
48
+ }
49
+ })();
50
+ try {
51
+ await _EmbedderRuntime._loadingPromise;
52
+ this._ready = true;
53
+ } catch (err) {
54
+ this._failed = true;
55
+ throw err;
56
+ }
57
+ }
58
+ async embed(text) {
59
+ if (!_EmbedderRuntime._pipelineInstance) {
60
+ await this.load();
61
+ }
62
+ try {
63
+ const output = await _EmbedderRuntime._pipelineInstance(text, {
64
+ pooling: "mean",
65
+ normalize: true
66
+ });
67
+ return output.data;
68
+ } catch (err) {
69
+ throw new Error(`Embedding failed: ${String(err)}`);
70
+ }
71
+ }
72
+ get isReady() {
73
+ return this._ready;
74
+ }
75
+ get hasFailed() {
76
+ return this._failed;
77
+ }
78
+ /** @internal */
79
+ static _reset() {
80
+ _EmbedderRuntime._pipelineInstance = null;
81
+ _EmbedderRuntime._loadingPromise = null;
82
+ }
83
+ };
84
+ // Module-level singleton so the WASM model is loaded at most once per page,
85
+ // regardless of how many widget instances exist.
86
+ _EmbedderRuntime._pipelineInstance = null;
87
+ _EmbedderRuntime._loadingPromise = null;
88
+ var EmbedderRuntime = _EmbedderRuntime;
89
+
90
+ // src/core/search.ts
91
+ function dotProduct(a, b) {
92
+ if (a.length !== b.length) {
93
+ throw new Error(`Vector dimension mismatch: query(${a.length}) vs index(${b.length})`);
94
+ }
95
+ let dot = 0;
96
+ for (let i = 0; i < a.length; i++) dot += a[i] * b[i];
97
+ return dot;
98
+ }
99
+ function search(queryVector, index, topK = 3, minScore = 0.42) {
100
+ const results = [];
101
+ for (const chunk of index) {
102
+ const score = dotProduct(queryVector, chunk.vector384);
103
+ if (score < minScore) continue;
104
+ if (results.length < topK) {
105
+ results.push({ chunk, score });
106
+ results.sort((a, b) => b.score - a.score);
107
+ } else if (score > results[topK - 1].score) {
108
+ results[topK - 1] = { chunk, score };
109
+ results.sort((a, b) => b.score - a.score);
110
+ }
111
+ }
112
+ return results;
113
+ }
114
+ function keywordSearch(query, index, topK = 3) {
115
+ const terms = query.toLowerCase().split(/\s+/).filter((w) => w.length > 2).map((w) => w.replace(/[^a-z0-9]/g, ""));
116
+ if (terms.length === 0) return [];
117
+ const results = [];
118
+ for (const chunk of index) {
119
+ const chunkLower = chunk.textLower || chunk.text.toLowerCase();
120
+ const matchCount = terms.filter((t) => chunkLower.includes(t)).length;
121
+ const score = matchCount / terms.length;
122
+ if (score <= 0) continue;
123
+ if (results.length < topK) {
124
+ results.push({ chunk, score });
125
+ results.sort((a, b) => b.score - a.score);
126
+ } else if (score > results[topK - 1].score) {
127
+ results[topK - 1] = { chunk, score };
128
+ results.sort((a, b) => b.score - a.score);
129
+ }
130
+ }
131
+ return results;
132
+ }
133
+ async function loadIndex(url) {
134
+ const res = await fetch(url);
135
+ if (!res.ok) {
136
+ throw new Error(`Failed to fetch index (HTTP ${res.status}): ${url}`);
137
+ }
138
+ const data = await res.json();
139
+ const chunks = Array.isArray(data) ? data : data.chunks;
140
+ for (const chunk of chunks) {
141
+ chunk.textLower = chunk.text.toLowerCase();
142
+ if (Array.isArray(chunk.vector384)) {
143
+ chunk.vector384 = new Float32Array(chunk.vector384);
144
+ }
145
+ if (Array.isArray(chunk.vector768)) {
146
+ chunk.vector768 = new Float32Array(chunk.vector768);
147
+ }
148
+ }
149
+ return chunks;
150
+ }
151
+
152
+ // src/core/retriever.ts
153
+ async function fetchIndex(url) {
154
+ try {
155
+ return await loadIndex(url);
156
+ } catch (err) {
157
+ throw new Error(`Could not load knowledge index: ${String(err)}`);
158
+ }
159
+ }
160
+ async function retrieve(text, index, embedder, config) {
161
+ const topK = config?.topK ?? 3;
162
+ const minScore = config?.minScore ?? 0.42;
163
+ if (embedder.isReady) {
164
+ try {
165
+ const vector = await embedder.embed(text);
166
+ const results2 = search(vector, index, topK, minScore);
167
+ return { results: results2, mode: "vector" };
168
+ } catch {
169
+ }
170
+ }
171
+ const results = keywordSearch(text, index, topK);
172
+ return { results, mode: "keyword" };
173
+ }
174
+
175
+ // src/core/renderer.ts
176
+ function buildAnswer(results) {
177
+ if (results.length === 0) return "";
178
+ const seen = /* @__PURE__ */ new Set();
179
+ const parts = [];
180
+ for (const r of results) {
181
+ if (!seen.has(r.chunk.sourceId)) {
182
+ seen.add(r.chunk.sourceId);
183
+ parts.push(r.chunk.text);
184
+ }
185
+ }
186
+ return parts.join("\n\n");
187
+ }
188
+ function extractChips(index, override) {
189
+ if (override && override.length > 0) return override.slice(0, 5);
190
+ const chips = [];
191
+ const seenText = /* @__PURE__ */ new Set();
192
+ const seenSource = /* @__PURE__ */ new Set();
193
+ for (const chunk of index) {
194
+ if (chips.length >= 5) break;
195
+ if (chunk.sourceId) {
196
+ if (seenSource.has(chunk.sourceId)) continue;
197
+ seenSource.add(chunk.sourceId);
198
+ }
199
+ const candidate = chunk.question ?? extractFirstSentence(chunk.text);
200
+ if (candidate && !seenText.has(candidate)) {
201
+ seenText.add(candidate);
202
+ chips.push(candidate);
203
+ }
204
+ }
205
+ return chips;
206
+ }
207
+ function extractFirstSentence(text) {
208
+ const match = text.match(/^[^\n.!?]{10,80}[.!?\n]?/);
209
+ if (!match) return text.slice(0, 60);
210
+ return match[0].trim();
211
+ }
212
+
213
+ // src/core/fallback.ts
214
+ var DEFAULT_POOL = [
215
+ "That one's a bit outside what I have notes on right now. Feel free to reach out directly and I'll make sure you get a proper answer.",
216
+ "Hmm, I don't have a great answer for that one yet. You're welcome to get in touch and a real person will help.",
217
+ "I want to give you the right answer, not a guess. If you reach out through the contact page someone will follow up with you."
218
+ ];
219
+ var rotationIndex = 0;
220
+ function getFallback(config) {
221
+ const pool = config.fallbackPool && config.fallbackPool.length > 0 ? config.fallbackPool : config.fallback ? [config.fallback] : DEFAULT_POOL;
222
+ const message = pool[rotationIndex % pool.length];
223
+ rotationIndex = (rotationIndex + 1) % pool.length;
224
+ if (config.contactUrl) {
225
+ return `${message} You can reach us at: ${config.contactUrl}`;
226
+ }
227
+ return message;
228
+ }
229
+
230
+ // src/react/useSageDesk.ts
231
+ var initialState = {
232
+ messages: [],
233
+ isOpen: false,
234
+ isTyping: false,
235
+ engineStatus: "idle",
236
+ engineError: null,
237
+ hasSentMessage: false
238
+ };
239
+ function reducer(state, action) {
240
+ switch (action.type) {
241
+ case "OPEN":
242
+ return { ...state, isOpen: true };
243
+ case "CLOSE":
244
+ return { ...state, isOpen: false };
245
+ case "ADD_MESSAGE":
246
+ return { ...state, messages: [...state.messages, action.payload] };
247
+ case "SET_TYPING":
248
+ return { ...state, isTyping: action.payload };
249
+ case "SET_ENGINE_STATUS":
250
+ return {
251
+ ...state,
252
+ engineStatus: action.payload.status,
253
+ engineError: action.payload.error ?? null
254
+ };
255
+ case "MARK_SENT":
256
+ return { ...state, hasSentMessage: true };
257
+ default:
258
+ return state;
259
+ }
260
+ }
261
+ function useSageDesk(config) {
262
+ const [state, dispatch] = useReducer(reducer, initialState);
263
+ const engineStatusRef = useRef("idle");
264
+ engineStatusRef.current = state.engineStatus;
265
+ const indexRef = useRef(null);
266
+ const embedderRef = useRef(null);
267
+ const engineStartedRef = useRef(false);
268
+ const msgCounterRef = useRef(0);
269
+ const [chips, setChips] = useState([]);
270
+ const makeId = () => `msg-${++msgCounterRef.current}`;
271
+ const addMessage = useCallback(
272
+ (msg) => {
273
+ dispatch({
274
+ type: "ADD_MESSAGE",
275
+ payload: { ...msg, id: makeId(), timestamp: /* @__PURE__ */ new Date() }
276
+ });
277
+ },
278
+ []
279
+ );
280
+ const startEngine = useCallback(async () => {
281
+ if (engineStartedRef.current) return;
282
+ engineStartedRef.current = true;
283
+ dispatch({ type: "SET_ENGINE_STATUS", payload: { status: "loading-index" } });
284
+ try {
285
+ indexRef.current = await fetchIndex(config.indexUrl);
286
+ } catch (err) {
287
+ console.warn("[sagedesk] Failed to load knowledge index from", config.indexUrl, "-", err);
288
+ dispatch({
289
+ type: "SET_ENGINE_STATUS",
290
+ payload: { status: "error-index", error: String(err) }
291
+ });
292
+ addMessage({
293
+ role: "bot",
294
+ text: "I'm having trouble loading right now. Please try again in a moment."
295
+ });
296
+ return;
297
+ }
298
+ setChips(extractChips(indexRef.current, config.agent.suggestedChips));
299
+ dispatch({ type: "SET_ENGINE_STATUS", payload: { status: "loading-model" } });
300
+ try {
301
+ embedderRef.current = new EmbedderRuntime();
302
+ await embedderRef.current.load(config.agent.model);
303
+ dispatch({ type: "SET_ENGINE_STATUS", payload: { status: "ready" } });
304
+ } catch (err) {
305
+ console.warn("[sagedesk] WASM model failed to load, falling back to keyword search -", err);
306
+ embedderRef.current = new EmbedderRuntime();
307
+ dispatch({ type: "SET_ENGINE_STATUS", payload: { status: "degraded" } });
308
+ }
309
+ }, [config.indexUrl, config.agent.suggestedChips, addMessage]);
310
+ const greetingShownRef = useRef(false);
311
+ const open = useCallback(() => {
312
+ dispatch({ type: "OPEN" });
313
+ if (!greetingShownRef.current) {
314
+ greetingShownRef.current = true;
315
+ addMessage({
316
+ role: "bot",
317
+ text: config.agent.greeting ?? "Hey, how can I help you today?"
318
+ });
319
+ }
320
+ startEngine();
321
+ }, [config.agent.greeting, addMessage, startEngine]);
322
+ const close = useCallback(() => {
323
+ dispatch({ type: "CLOSE" });
324
+ }, []);
325
+ const waitForEngine = useCallback(() => {
326
+ return new Promise((resolve) => {
327
+ const check = () => {
328
+ const s = engineStatusRef.current;
329
+ if (s === "ready" || s === "degraded" || s === "error-index" || s === "error-model") {
330
+ resolve();
331
+ } else {
332
+ setTimeout(check, 100);
333
+ }
334
+ };
335
+ check();
336
+ });
337
+ }, []);
338
+ const submit = useCallback(
339
+ async (text) => {
340
+ const trimmed = text.trim();
341
+ if (!trimmed) return;
342
+ const typingStart = Date.now();
343
+ dispatch({ type: "MARK_SENT" });
344
+ addMessage({ role: "user", text: trimmed });
345
+ dispatch({ type: "SET_TYPING", payload: true });
346
+ const currentStatus = engineStatusRef.current;
347
+ if (currentStatus !== "ready" && currentStatus !== "degraded" && currentStatus !== "error-index" && currentStatus !== "error-model") {
348
+ await waitForEngine();
349
+ }
350
+ let botText;
351
+ let isFallback = false;
352
+ let mode = "keyword";
353
+ if (!indexRef.current) {
354
+ botText = getFallback(config.agent);
355
+ isFallback = true;
356
+ } else {
357
+ try {
358
+ const res = await retrieve(
359
+ trimmed,
360
+ indexRef.current,
361
+ embedderRef.current,
362
+ config.search
363
+ );
364
+ mode = res.mode;
365
+ if (res.results.length > 0) {
366
+ botText = buildAnswer(res.results);
367
+ } else {
368
+ botText = getFallback(config.agent);
369
+ isFallback = true;
370
+ }
371
+ } catch (err) {
372
+ console.warn("[sagedesk] Query failed, showing fallback -", err);
373
+ botText = getFallback(config.agent);
374
+ isFallback = true;
375
+ }
376
+ }
377
+ const elapsed = Date.now() - typingStart;
378
+ const delayBase = mode === "keyword" || isFallback ? 800 : 3e3;
379
+ const minTypingMs = delayBase + Math.random() * 2e3;
380
+ const remaining = minTypingMs - elapsed;
381
+ if (remaining > 0) await new Promise((r) => setTimeout(r, remaining));
382
+ dispatch({ type: "SET_TYPING", payload: false });
383
+ addMessage({ role: "bot", text: botText, isFallback });
384
+ },
385
+ [addMessage, waitForEngine, config.agent, config.search]
386
+ );
387
+ useEffect(() => {
388
+ if (!state.isOpen) return;
389
+ const handler = (e) => {
390
+ if (e.key === "Escape") close();
391
+ };
392
+ document.addEventListener("keydown", handler);
393
+ return () => document.removeEventListener("keydown", handler);
394
+ }, [state.isOpen, close]);
395
+ const activeChips = useMemo(() => {
396
+ const askedTexts = new Set(
397
+ state.messages.filter((m) => m.role === "user").map((m) => m.text.toLowerCase().trim())
398
+ );
399
+ return chips.filter((chip) => !askedTexts.has(chip.toLowerCase().trim()));
400
+ }, [chips, state.messages]);
401
+ return { state, chips: activeChips, open, close, submit };
402
+ }
403
+
404
+ // src/react/SageDeskWidget.tsx
405
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
406
+ var STYLE_ID = "sagedesk-widget-styles";
407
+ var SHARED = `
408
+ @keyframes sd-bounce {
409
+ 0%, 60%, 100% { transform: translateY(0); }
410
+ 30% { transform: translateY(-5px); }
411
+ }
412
+ @keyframes sd-panel-open {
413
+ from { transform: scale(0.94) translateY(6px); opacity: 0; }
414
+ to { transform: scale(1) translateY(0); opacity: 1; }
415
+ }
416
+ @keyframes sd-panel-close {
417
+ from { transform: scale(1) translateY(0); opacity: 1; }
418
+ to { transform: scale(0.94) translateY(6px); opacity: 0; }
419
+ }
420
+ .sd-r-opening { animation: sd-panel-open 200ms cubic-bezier(0.34,1.56,0.64,1) both; }
421
+ .sd-r-closing { animation: sd-panel-close 150ms ease-in both; }
422
+ .sd-r-dot-1 { animation: sd-bounce 1.2s ease-in-out infinite; }
423
+ .sd-r-dot-2 { animation: sd-bounce 1.2s ease-in-out 0.2s infinite; }
424
+ .sd-r-dot-3 { animation: sd-bounce 1.2s ease-in-out 0.4s infinite; }
425
+ .sd-r-scrollable {
426
+ flex-wrap: nowrap !important;
427
+ overflow-x: auto !important;
428
+ scrollbar-width: none !important;
429
+ -ms-overflow-style: none !important;
430
+ }
431
+ .sd-r-scrollable::-webkit-scrollbar { display: none !important; }
432
+ .sd-r-scrollable > * { flex-shrink: 0 !important; }
433
+ @media (max-width: 420px) {
434
+ .sd-r-panel {
435
+ bottom: 0 !important; right: 0 !important; left: 0 !important;
436
+ width: auto !important; max-width: 100% !important;
437
+ border-radius: 20px 20px 0 0 !important;
438
+ max-height: 85vh !important;
439
+ transform-origin: bottom center !important;
440
+ }
441
+ }
442
+ `;
443
+ var THEME_CSS = {
444
+ classic: `${SHARED}
445
+ .sd-r-trigger:hover { transform: scale(1.06) !important; }
446
+ .sd-r-close-btn:hover { background: rgba(255,255,255,0.22) !important; }
447
+ .sd-r-chip:hover { opacity: 0.8; }
448
+ .sd-r-send:hover { opacity: 0.85; }
449
+ .sd-r-send:active { transform: scale(0.95); }
450
+ .sd-r-input:focus { outline: none; }
451
+ .sd-r-input::placeholder { color: #a8a8b0; }
452
+ `,
453
+ light: `${SHARED}
454
+ .sd-r-trigger-light:hover { box-shadow: 0 16px 32px -10px rgba(40,30,90,0.24),0 2px 10px rgba(40,30,90,0.08) !important; }
455
+ .sd-r-chip-light:hover { opacity: 0.75; }
456
+ .sd-r-send:hover { opacity: 0.85; }
457
+ .sd-r-send:active { transform: scale(0.95); }
458
+ .sd-r-input:focus { outline: none; }
459
+ .sd-r-input::placeholder { color: #a8a89e; }
460
+ `,
461
+ dark: `${SHARED}
462
+ @keyframes sd-blink { 0%,100%{opacity:1} 50%{opacity:0} }
463
+ .sd-r-cursor { animation: sd-blink 1s infinite; }
464
+ .sd-r-trigger:hover { transform: scale(1.04) !important; }
465
+ .sd-r-prompt:hover { background: rgba(255,255,255,0.07) !important; }
466
+ .sd-r-send:hover { opacity: 0.85; }
467
+ .sd-r-send:active { transform: scale(0.95); }
468
+ .sd-r-input:focus { outline: none; }
469
+ .sd-r-input::placeholder { color: rgba(255,255,255,0.35); }
470
+ `
471
+ };
472
+ function injectStyles(theme) {
473
+ if (typeof document === "undefined") return;
474
+ const id = `${STYLE_ID}-${theme}`;
475
+ if (document.getElementById(id)) return;
476
+ const style = document.createElement("style");
477
+ style.id = id;
478
+ style.textContent = THEME_CSS[theme];
479
+ document.head.prepend(style);
480
+ }
481
+ var IconChat = ({ size = 22 }) => /* @__PURE__ */ jsx("svg", { width: size, height: size, viewBox: "0 0 24 24", fill: "none", "aria-hidden": "true", children: /* @__PURE__ */ jsx(
482
+ "path",
483
+ {
484
+ d: "M5 6.5A2.5 2.5 0 017.5 4h9A2.5 2.5 0 0119 6.5v7A2.5 2.5 0 0116.5 16H11l-4 3.5V16H7.5A2.5 2.5 0 015 13.5v-7z",
485
+ stroke: "currentColor",
486
+ strokeWidth: "1.6",
487
+ strokeLinejoin: "round"
488
+ }
489
+ ) });
490
+ var IconSend = ({ size = 14 }) => /* @__PURE__ */ jsx("svg", { width: size, height: size, viewBox: "0 0 24 24", fill: "none", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { d: "M4 12L20 4l-4 16-4-7-8-1z", stroke: "currentColor", strokeWidth: "1.6", strokeLinejoin: "round" }) });
491
+ var IconClose = ({ size = 14 }) => /* @__PURE__ */ jsx("svg", { width: size, height: size, viewBox: "0 0 24 24", fill: "none", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { d: "M6 6l12 12M18 6L6 18", stroke: "currentColor", strokeWidth: "1.6", strokeLinecap: "round" }) });
492
+ var IconBot = ({ size = 22 }) => /* @__PURE__ */ jsxs("svg", { width: size, height: size, viewBox: "0 0 24 24", fill: "none", "aria-hidden": "true", children: [
493
+ /* @__PURE__ */ jsx("rect", { x: "4", y: "8", width: "16", height: "12", rx: "3", stroke: "currentColor", strokeWidth: "1.6" }),
494
+ /* @__PURE__ */ jsx("circle", { cx: "9", cy: "13.5", r: "1.5", fill: "currentColor" }),
495
+ /* @__PURE__ */ jsx("circle", { cx: "15", cy: "13.5", r: "1.5", fill: "currentColor" }),
496
+ /* @__PURE__ */ jsx("path", { d: "M9.5 16.5h5", stroke: "currentColor", strokeWidth: "1.6", strokeLinecap: "round" }),
497
+ /* @__PURE__ */ jsx("path", { d: "M12 8V5", stroke: "currentColor", strokeWidth: "1.6", strokeLinecap: "round" }),
498
+ /* @__PURE__ */ jsx("circle", { cx: "12", cy: "4", r: "1.2", fill: "currentColor" })
499
+ ] });
500
+ var IconPerson = ({ size = 18 }) => /* @__PURE__ */ jsxs("svg", { width: size, height: size, viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "1.6", strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: [
501
+ /* @__PURE__ */ jsx("path", { d: "M12 12.5a4 4 0 100-8 4 4 0 000 8z" }),
502
+ /* @__PURE__ */ jsx("path", { d: "M5 20.5c0-3.5 3.13-6 7-6s7 2.5 7 6" })
503
+ ] });
504
+ var PoweredBy = ({ dark = false }) => /* @__PURE__ */ jsxs("div", { style: {
505
+ fontSize: "11px",
506
+ color: dark ? "rgba(255,255,255,0.35)" : "#a8a8b0",
507
+ display: "flex",
508
+ alignItems: "center",
509
+ justifyContent: "center",
510
+ gap: "4px"
511
+ }, children: [
512
+ "Powered by",
513
+ " ",
514
+ /* @__PURE__ */ jsx(
515
+ "a",
516
+ {
517
+ href: "https://github.com/mzeeshanwahid/sagedesk",
518
+ target: "_blank",
519
+ rel: "noopener noreferrer",
520
+ style: {
521
+ color: dark ? "rgba(255,255,255,0.7)" : "#5a5a64",
522
+ fontWeight: 500,
523
+ textDecoration: "none"
524
+ },
525
+ children: "sagedesk"
526
+ }
527
+ )
528
+ ] });
529
+ function ClassicMessageBubble({ msg, accent }) {
530
+ const isBot = msg.role === "bot";
531
+ return /* @__PURE__ */ jsxs("div", { style: { display: "flex", flexDirection: "column", alignItems: isBot ? "flex-start" : "flex-end", gap: "4px" }, children: [
532
+ msg.isFallback && /* @__PURE__ */ jsx("p", { style: { fontSize: "11px", fontWeight: 500, color: "#9b9aa3", margin: 0, padding: "0 4px", fontFamily: "inherit" }, children: "Not sure about that one" }),
533
+ /* @__PURE__ */ jsx("div", { style: {
534
+ maxWidth: "82%",
535
+ padding: "10px 14px",
536
+ fontSize: "14px",
537
+ lineHeight: 1.5,
538
+ borderRadius: isBot ? "16px 16px 16px 6px" : "16px 16px 6px 16px",
539
+ background: isBot ? "#fff" : accent,
540
+ color: isBot ? "#1a1a2e" : "#fff",
541
+ border: isBot ? "1px solid rgba(20,20,40,0.06)" : "none",
542
+ boxShadow: isBot ? "0 1px 2px rgba(20,20,40,0.04)" : `0 6px 16px -6px color-mix(in oklab, ${accent} 60%, transparent)`,
543
+ whiteSpace: "pre-wrap",
544
+ wordBreak: "break-word",
545
+ fontFamily: "inherit"
546
+ }, children: msg.text }),
547
+ /* @__PURE__ */ jsx("span", { style: {
548
+ fontSize: "11px",
549
+ color: "#a8a8b0",
550
+ marginTop: "2px",
551
+ padding: isBot ? "0 0 0 4px" : "0 4px 0 0",
552
+ fontVariantNumeric: "tabular-nums",
553
+ fontFamily: "inherit"
554
+ }, children: "just now" })
555
+ ] });
556
+ }
557
+ function ClassicTypingIndicator() {
558
+ const dot = { width: 6, height: 6, borderRadius: "50%", background: "#c8c8ce", display: "inline-block" };
559
+ return /* @__PURE__ */ jsx("div", { style: { display: "flex", flexDirection: "column", alignItems: "flex-start", gap: "4px" }, children: /* @__PURE__ */ jsxs("div", { style: {
560
+ padding: "10px 14px",
561
+ borderRadius: "16px 16px 16px 6px",
562
+ background: "#fff",
563
+ border: "1px solid rgba(20,20,40,0.06)",
564
+ boxShadow: "0 1px 2px rgba(20,20,40,0.04)",
565
+ display: "flex",
566
+ alignItems: "center",
567
+ gap: "4px"
568
+ }, children: [
569
+ /* @__PURE__ */ jsx("span", { style: dot, className: "sd-r-dot-1" }),
570
+ /* @__PURE__ */ jsx("span", { style: dot, className: "sd-r-dot-2" }),
571
+ /* @__PURE__ */ jsx("span", { style: dot, className: "sd-r-dot-3" })
572
+ ] }) });
573
+ }
574
+ function LightMessageBubble({ msg, accent, agentName }) {
575
+ const isBot = msg.role === "bot";
576
+ if (isBot) {
577
+ return /* @__PURE__ */ jsxs("div", { style: { display: "flex", gap: "10px" }, children: [
578
+ /* @__PURE__ */ jsx("div", { style: {
579
+ width: 28,
580
+ height: 28,
581
+ borderRadius: "50%",
582
+ background: `linear-gradient(135deg, ${accent}, color-mix(in oklab, ${accent} 60%, #fff))`,
583
+ flexShrink: 0,
584
+ marginTop: "2px"
585
+ } }),
586
+ /* @__PURE__ */ jsxs("div", { style: { flex: 1, minWidth: 0 }, children: [
587
+ /* @__PURE__ */ jsxs("div", { style: { display: "flex", alignItems: "baseline", gap: "8px", marginBottom: "4px" }, children: [
588
+ /* @__PURE__ */ jsx("span", { style: { fontSize: "13px", fontWeight: 600, color: "#1a1a2e", fontFamily: "inherit" }, children: agentName }),
589
+ /* @__PURE__ */ jsx("span", { style: { fontSize: "11px", color: "#a8a89e", fontVariantNumeric: "tabular-nums", fontFamily: "inherit" }, children: "just now" })
590
+ ] }),
591
+ msg.isFallback && /* @__PURE__ */ jsx("p", { style: { fontSize: "11px", color: "#9b9aa3", margin: "0 0 4px", fontFamily: "inherit" }, children: "Not sure about that one" }),
592
+ /* @__PURE__ */ jsx("div", { style: { fontSize: "14px", lineHeight: 1.55, color: "#2a2a36", fontFamily: "inherit", whiteSpace: "pre-wrap", wordBreak: "break-word" }, children: msg.text })
593
+ ] })
594
+ ] });
595
+ }
596
+ return /* @__PURE__ */ jsx("div", { style: { display: "flex", justifyContent: "flex-end" }, children: /* @__PURE__ */ jsx("div", { style: {
597
+ maxWidth: "78%",
598
+ padding: "11px 15px",
599
+ fontSize: "14px",
600
+ lineHeight: 1.5,
601
+ borderRadius: "16px 16px 4px 16px",
602
+ background: `color-mix(in oklab, ${accent} 10%, white)`,
603
+ color: accent,
604
+ border: `1px solid color-mix(in oklab, ${accent} 22%, transparent)`,
605
+ fontWeight: 500,
606
+ whiteSpace: "pre-wrap",
607
+ wordBreak: "break-word",
608
+ fontFamily: "inherit"
609
+ }, children: msg.text }) });
610
+ }
611
+ function LightTypingIndicator({ accent }) {
612
+ const dot = { width: 6, height: 6, borderRadius: "50%", background: "#c4c4be", display: "inline-block" };
613
+ return /* @__PURE__ */ jsxs("div", { style: { display: "flex", gap: "10px" }, children: [
614
+ /* @__PURE__ */ jsx("div", { style: {
615
+ width: 28,
616
+ height: 28,
617
+ borderRadius: "50%",
618
+ background: `linear-gradient(135deg, ${accent}, color-mix(in oklab, ${accent} 60%, #fff))`,
619
+ flexShrink: 0,
620
+ marginTop: "2px"
621
+ } }),
622
+ /* @__PURE__ */ jsxs("div", { style: {
623
+ padding: "10px 14px",
624
+ borderRadius: "16px 16px 16px 4px",
625
+ background: "#fff",
626
+ border: "1px solid rgba(20,20,40,0.07)",
627
+ boxShadow: "0 1px 2px rgba(20,20,40,0.04)",
628
+ display: "inline-flex",
629
+ alignItems: "center",
630
+ gap: "4px"
631
+ }, children: [
632
+ /* @__PURE__ */ jsx("span", { style: dot, className: "sd-r-dot-1" }),
633
+ /* @__PURE__ */ jsx("span", { style: dot, className: "sd-r-dot-2" }),
634
+ /* @__PURE__ */ jsx("span", { style: dot, className: "sd-r-dot-3" })
635
+ ] })
636
+ ] });
637
+ }
638
+ function DarkMessageBubble({ msg, accent }) {
639
+ const isBot = msg.role === "bot";
640
+ return /* @__PURE__ */ jsxs("div", { style: {
641
+ maxWidth: "85%",
642
+ padding: "12px 14px",
643
+ fontSize: "14px",
644
+ lineHeight: isBot ? 1.6 : 1.5,
645
+ borderRadius: isBot ? "14px 14px 14px 6px" : "14px 14px 6px 14px",
646
+ background: isBot ? "rgba(255,255,255,0.05)" : `linear-gradient(135deg, ${accent}, color-mix(in oklab, ${accent} 75%, #1a1340))`,
647
+ border: isBot ? "1px solid rgba(255,255,255,0.06)" : "none",
648
+ color: isBot ? "rgba(255,255,255,0.92)" : "#fff",
649
+ alignSelf: isBot ? "flex-start" : "flex-end",
650
+ boxShadow: isBot ? "none" : `0 8px 20px -8px color-mix(in oklab, ${accent} 70%, transparent)`,
651
+ whiteSpace: "pre-wrap",
652
+ wordBreak: "break-word",
653
+ fontFamily: "inherit"
654
+ }, children: [
655
+ msg.isFallback && /* @__PURE__ */ jsx("span", { style: { fontSize: "11px", color: "rgba(255,255,255,0.5)", display: "block", marginBottom: "4px" }, children: "Not sure about that one" }),
656
+ msg.text
657
+ ] });
658
+ }
659
+ function DarkTypingIndicator() {
660
+ const dot = { width: 6, height: 6, borderRadius: "50%", background: "rgba(255,255,255,0.4)", display: "inline-block" };
661
+ return /* @__PURE__ */ jsxs("div", { style: {
662
+ maxWidth: "85%",
663
+ padding: "12px 14px",
664
+ borderRadius: "14px 14px 14px 6px",
665
+ background: "rgba(255,255,255,0.05)",
666
+ border: "1px solid rgba(255,255,255,0.06)",
667
+ alignSelf: "flex-start",
668
+ display: "flex",
669
+ alignItems: "center",
670
+ gap: "4px"
671
+ }, children: [
672
+ /* @__PURE__ */ jsx("span", { style: dot, className: "sd-r-dot-1" }),
673
+ /* @__PURE__ */ jsx("span", { style: dot, className: "sd-r-dot-2" }),
674
+ /* @__PURE__ */ jsx("span", { style: dot, className: "sd-r-dot-3" })
675
+ ] });
676
+ }
677
+ function renderClassic(p) {
678
+ const {
679
+ agent,
680
+ state,
681
+ chips,
682
+ accent,
683
+ isLeft,
684
+ inputValue,
685
+ setInputValue,
686
+ handleClose,
687
+ handleSubmit,
688
+ handleKeyDown,
689
+ isClosing,
690
+ panelClass,
691
+ showChips,
692
+ showPoweredBy,
693
+ threadRef,
694
+ inputRef,
695
+ triggerRef,
696
+ open,
697
+ submit
698
+ } = p;
699
+ const side = isLeft ? "left" : "right";
700
+ const triggerStyle = {
701
+ position: "fixed",
702
+ bottom: "28px",
703
+ [side]: "28px",
704
+ width: "56px",
705
+ height: "56px",
706
+ borderRadius: "50%",
707
+ background: `linear-gradient(135deg, ${accent} 0%, color-mix(in oklab, ${accent} 78%, #1a1340) 100%)`,
708
+ border: "none",
709
+ color: "#fff",
710
+ cursor: "pointer",
711
+ display: "grid",
712
+ placeItems: "center",
713
+ zIndex: 9999,
714
+ padding: 0,
715
+ transition: "transform 150ms ease",
716
+ boxShadow: `0 14px 28px -6px color-mix(in oklab, ${accent} 55%, transparent), 0 4px 10px rgba(40,30,90,0.15)`
717
+ };
718
+ const panelStyle = {
719
+ position: "fixed",
720
+ bottom: "96px",
721
+ [side]: "28px",
722
+ width: "380px",
723
+ height: "580px",
724
+ maxHeight: "580px",
725
+ borderRadius: "20px",
726
+ background: "#ffffff",
727
+ boxShadow: "0 1px 0 rgba(20,20,40,0.04), 0 24px 48px -16px rgba(40,30,90,0.22), 0 4px 14px rgba(40,30,90,0.08)",
728
+ border: "1px solid rgba(20,20,40,0.04)",
729
+ display: "flex",
730
+ flexDirection: "column",
731
+ overflow: "hidden",
732
+ zIndex: 9999,
733
+ transformOrigin: isLeft ? "bottom left" : "bottom right"
734
+ };
735
+ const headerGrad = `linear-gradient(135deg, ${accent} 0%, color-mix(in oklab, ${accent} 78%, #1a1340) 100%)`;
736
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
737
+ /* @__PURE__ */ jsx(
738
+ "button",
739
+ {
740
+ ref: triggerRef,
741
+ style: triggerStyle,
742
+ className: "sd-r-trigger",
743
+ onClick: state.isOpen ? handleClose : open,
744
+ "aria-label": "Open support chat",
745
+ "aria-expanded": state.isOpen,
746
+ children: /* @__PURE__ */ jsx(IconChat, { size: 22 })
747
+ }
748
+ ),
749
+ (state.isOpen || isClosing) && /* @__PURE__ */ jsxs("div", { style: panelStyle, className: `sd-r-panel ${panelClass}`, role: "dialog", "aria-label": agent.name, children: [
750
+ /* @__PURE__ */ jsxs("div", { style: { padding: "18px 20px", background: headerGrad, color: "#fff", display: "flex", alignItems: "center", gap: "12px", flexShrink: 0 }, children: [
751
+ /* @__PURE__ */ jsxs("div", { style: { position: "relative", width: 40, height: 40 }, children: [
752
+ /* @__PURE__ */ jsx("div", { style: { width: 40, height: 40, borderRadius: "50%", background: "rgba(255,255,255,0.18)", display: "grid", placeItems: "center", overflow: "hidden" }, children: agent.avatarUrl ? /* @__PURE__ */ jsx("img", { src: agent.avatarUrl, alt: agent.name, style: { width: "100%", height: "100%", objectFit: "cover" } }) : /* @__PURE__ */ jsx(IconPerson, { size: 20 }) }),
753
+ /* @__PURE__ */ jsx("div", { style: { position: "absolute", right: -1, bottom: -1, width: 12, height: 12, borderRadius: "50%", background: "#22c55e", boxShadow: `0 0 0 2.5px ${accent}` } })
754
+ ] }),
755
+ /* @__PURE__ */ jsxs("div", { style: { flex: 1, minWidth: 0 }, children: [
756
+ /* @__PURE__ */ jsx("div", { style: { fontSize: "15px", fontWeight: 600, letterSpacing: "-0.01em" }, children: agent.name }),
757
+ /* @__PURE__ */ jsxs("div", { style: { fontSize: "12px", opacity: 0.85, display: "flex", alignItems: "center", gap: "6px", marginTop: "3px" }, children: [
758
+ /* @__PURE__ */ jsx("span", { style: { width: 6, height: 6, borderRadius: "50%", background: "#4ade80", boxShadow: "0 0 0 2px rgba(74,222,128,0.25)" } }),
759
+ "Typically replies in under a minute"
760
+ ] })
761
+ ] }),
762
+ /* @__PURE__ */ jsx("button", { className: "sd-r-close-btn", onClick: handleClose, "aria-label": "Close chat", style: {
763
+ background: "rgba(255,255,255,0.14)",
764
+ border: "none",
765
+ color: "#fff",
766
+ width: 30,
767
+ height: 30,
768
+ borderRadius: 8,
769
+ cursor: "pointer",
770
+ display: "grid",
771
+ placeItems: "center",
772
+ padding: 0,
773
+ transition: "background 120ms"
774
+ }, children: /* @__PURE__ */ jsx(IconClose, { size: 14 }) })
775
+ ] }),
776
+ /* @__PURE__ */ jsxs("div", { ref: threadRef, role: "log", "aria-live": "polite", "aria-label": "Chat messages", style: {
777
+ flex: 1,
778
+ padding: "22px 18px 18px",
779
+ overflowY: "auto",
780
+ background: "#fbfbfa",
781
+ display: "flex",
782
+ flexDirection: "column",
783
+ gap: "18px",
784
+ scrollbarWidth: "thin",
785
+ overscrollBehavior: "contain"
786
+ }, children: [
787
+ state.messages.map((msg) => /* @__PURE__ */ jsx(ClassicMessageBubble, { msg, accent }, msg.id)),
788
+ state.isTyping && /* @__PURE__ */ jsx(ClassicTypingIndicator, {})
789
+ ] }),
790
+ showChips && /* @__PURE__ */ jsxs("div", { style: { padding: state.hasSentMessage ? "0 0 16px" : "0 18px 16px", background: "#fbfbfa", flexShrink: 0 }, children: [
791
+ /* @__PURE__ */ jsx("div", { style: {
792
+ fontSize: "11px",
793
+ color: "#9b9aa3",
794
+ letterSpacing: "0.06em",
795
+ textTransform: "uppercase",
796
+ fontWeight: 500,
797
+ paddingLeft: state.hasSentMessage ? 18 : 4,
798
+ marginBottom: "6px"
799
+ }, children: "Suggested" }),
800
+ /* @__PURE__ */ jsx("div", { className: state.hasSentMessage ? "sd-r-scrollable" : "", style: {
801
+ display: "flex",
802
+ gap: "6px",
803
+ flexWrap: state.hasSentMessage ? "nowrap" : "wrap",
804
+ padding: state.hasSentMessage ? "0 18px" : 0
805
+ }, children: chips.map((chip) => /* @__PURE__ */ jsx("button", { className: "sd-r-chip", onClick: () => {
806
+ setInputValue("");
807
+ submit(chip);
808
+ }, style: {
809
+ fontSize: "12.5px",
810
+ padding: "7px 12px",
811
+ borderRadius: "999px",
812
+ background: "#fff",
813
+ border: `1px solid color-mix(in oklab, ${accent} 24%, transparent)`,
814
+ color: accent,
815
+ cursor: "pointer",
816
+ fontWeight: 500,
817
+ fontFamily: "inherit",
818
+ letterSpacing: "-0.005em"
819
+ }, children: chip }, chip)) })
820
+ ] }),
821
+ /* @__PURE__ */ jsxs("div", { style: { padding: "14px 14px 16px", borderTop: "1px solid rgba(20,20,40,0.06)", background: "#fff", flexShrink: 0 }, children: [
822
+ /* @__PURE__ */ jsxs("div", { style: {
823
+ display: "flex",
824
+ alignItems: "center",
825
+ gap: "6px",
826
+ background: "#f5f4f0",
827
+ borderRadius: "12px",
828
+ padding: "5px 6px 5px 14px",
829
+ border: `1px solid ${inputValue ? `color-mix(in oklab, ${accent} 30%, transparent)` : "transparent"}`,
830
+ boxShadow: inputValue ? `0 0 0 4px color-mix(in oklab, ${accent} 12%, transparent)` : "none",
831
+ transition: "border-color .15s, box-shadow .15s"
832
+ }, children: [
833
+ /* @__PURE__ */ jsx(
834
+ "input",
835
+ {
836
+ ref: inputRef,
837
+ value: inputValue,
838
+ onChange: (e) => setInputValue(e.target.value),
839
+ onKeyDown: handleKeyDown,
840
+ placeholder: "Write a message\u2026",
841
+ className: "sd-r-input",
842
+ "aria-label": "Type your question",
843
+ autoComplete: "off",
844
+ style: { flex: 1, background: "transparent", border: "none", outline: "none", fontSize: "14px", padding: "8px 0", fontFamily: "inherit", color: "#1a1a2e" }
845
+ }
846
+ ),
847
+ /* @__PURE__ */ jsx("button", { className: "sd-r-send", onClick: handleSubmit, "aria-label": "Send message", style: {
848
+ width: 32,
849
+ height: 32,
850
+ borderRadius: 8,
851
+ padding: 0,
852
+ background: inputValue ? accent : `color-mix(in oklab, ${accent} 22%, #e5e3dc)`,
853
+ border: "none",
854
+ color: "#fff",
855
+ cursor: "pointer",
856
+ display: "grid",
857
+ placeItems: "center",
858
+ transition: "background .15s"
859
+ }, children: /* @__PURE__ */ jsx(IconSend, { size: 14 }) })
860
+ ] }),
861
+ showPoweredBy && /* @__PURE__ */ jsx("div", { style: { marginTop: "10px" }, children: /* @__PURE__ */ jsx(PoweredBy, {}) })
862
+ ] })
863
+ ] })
864
+ ] });
865
+ }
866
+ function renderLight(p) {
867
+ const {
868
+ agent,
869
+ state,
870
+ chips,
871
+ accent,
872
+ isLeft,
873
+ inputValue,
874
+ setInputValue,
875
+ handleClose,
876
+ handleSubmit,
877
+ handleKeyDown,
878
+ isClosing,
879
+ panelClass,
880
+ showChips,
881
+ showPoweredBy,
882
+ threadRef,
883
+ inputRef,
884
+ triggerRef,
885
+ open,
886
+ submit
887
+ } = p;
888
+ const side = isLeft ? "left" : "right";
889
+ const triggerStyle = {
890
+ position: "fixed",
891
+ bottom: "28px",
892
+ [side]: "28px",
893
+ height: "52px",
894
+ padding: "0 8px 0 20px",
895
+ borderRadius: "999px",
896
+ background: "#fdfcf9",
897
+ border: "1px solid rgba(20,20,40,0.08)",
898
+ cursor: "pointer",
899
+ display: "flex",
900
+ alignItems: "center",
901
+ gap: "14px",
902
+ boxShadow: "0 12px 26px -8px rgba(40,30,90,0.18), 0 2px 8px rgba(40,30,90,0.06)",
903
+ zIndex: 9999,
904
+ fontFamily: "inherit",
905
+ transition: "box-shadow 150ms ease"
906
+ };
907
+ const panelStyle = {
908
+ position: "fixed",
909
+ bottom: "92px",
910
+ [side]: "28px",
911
+ width: "400px",
912
+ height: "580px",
913
+ maxHeight: "580px",
914
+ borderRadius: "22px",
915
+ background: "#fdfcf9",
916
+ boxShadow: "0 1px 0 rgba(20,20,40,0.04), 0 30px 60px -20px rgba(40,30,90,0.18), 0 6px 16px rgba(40,30,90,0.06)",
917
+ border: "1px solid rgba(20,20,40,0.05)",
918
+ display: "flex",
919
+ flexDirection: "column",
920
+ overflow: "hidden",
921
+ zIndex: 9999,
922
+ transformOrigin: isLeft ? "bottom left" : "bottom right"
923
+ };
924
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
925
+ /* @__PURE__ */ jsxs(
926
+ "button",
927
+ {
928
+ ref: triggerRef,
929
+ style: triggerStyle,
930
+ className: "sd-r-trigger-light",
931
+ onClick: state.isOpen ? handleClose : open,
932
+ "aria-label": "Open support chat",
933
+ "aria-expanded": state.isOpen,
934
+ children: [
935
+ /* @__PURE__ */ jsxs("span", { style: { display: "flex", flexDirection: "column", alignItems: "flex-start", lineHeight: 1.2 }, children: [
936
+ /* @__PURE__ */ jsx("span", { style: { fontSize: "14px", fontWeight: 600, color: "#1a1a2e" }, children: "Chat with us" }),
937
+ /* @__PURE__ */ jsx("span", { style: { fontSize: "11px", color: "#9b9aa3", marginTop: "2px" }, children: "We typically reply in 1m" })
938
+ ] }),
939
+ /* @__PURE__ */ jsx("span", { style: { width: "36px", height: "36px", borderRadius: "50%", background: accent, color: "#fff", display: "grid", placeItems: "center", flexShrink: 0 }, children: /* @__PURE__ */ jsx(IconChat, { size: 18 }) })
940
+ ]
941
+ }
942
+ ),
943
+ (state.isOpen || isClosing) && /* @__PURE__ */ jsxs("div", { style: panelStyle, className: `sd-r-panel ${panelClass}`, role: "dialog", "aria-label": agent.name, children: [
944
+ /* @__PURE__ */ jsxs("div", { style: { padding: "18px 20px 14px", borderBottom: "1px solid rgba(20,20,40,0.05)", display: "flex", alignItems: "center", gap: "12px", background: "#fdfcf9", flexShrink: 0 }, children: [
945
+ /* @__PURE__ */ jsx("div", { style: {
946
+ width: 36,
947
+ height: 36,
948
+ borderRadius: "50%",
949
+ flexShrink: 0,
950
+ background: `linear-gradient(135deg, ${accent}, color-mix(in oklab, ${accent} 55%, #fff))`,
951
+ display: "grid",
952
+ placeItems: "center",
953
+ overflow: "hidden",
954
+ color: "#fff"
955
+ }, children: agent.avatarUrl ? /* @__PURE__ */ jsx("img", { src: agent.avatarUrl, alt: agent.name, style: { width: "100%", height: "100%", objectFit: "cover" } }) : /* @__PURE__ */ jsx(IconPerson, { size: 16 }) }),
956
+ /* @__PURE__ */ jsxs("div", { style: { flex: 1, minWidth: 0 }, children: [
957
+ /* @__PURE__ */ jsx("div", { style: { fontSize: "14.5px", fontWeight: 600, color: "#1a1a2e", letterSpacing: "-0.01em" }, children: agent.name }),
958
+ /* @__PURE__ */ jsxs("div", { style: { fontSize: "12px", color: "#7a7a82", display: "flex", alignItems: "center", gap: "6px", marginTop: "2px" }, children: [
959
+ /* @__PURE__ */ jsx("span", { style: { width: 6, height: 6, borderRadius: "50%", background: "#22c55e" } }),
960
+ "Online \xB7 replies in under a minute"
961
+ ] })
962
+ ] }),
963
+ /* @__PURE__ */ jsx("button", { onClick: handleClose, "aria-label": "Close chat", style: {
964
+ background: "transparent",
965
+ border: "none",
966
+ color: "#9b9aa3",
967
+ width: 30,
968
+ height: 30,
969
+ borderRadius: 8,
970
+ cursor: "pointer",
971
+ display: "grid",
972
+ placeItems: "center",
973
+ padding: 0
974
+ }, children: /* @__PURE__ */ jsx(IconClose, { size: 16 }) })
975
+ ] }),
976
+ /* @__PURE__ */ jsxs("div", { ref: threadRef, role: "log", "aria-live": "polite", "aria-label": "Chat messages", style: {
977
+ flex: 1,
978
+ padding: "22px 20px 16px",
979
+ overflowY: "auto",
980
+ display: "flex",
981
+ flexDirection: "column",
982
+ gap: "22px",
983
+ scrollbarWidth: "thin",
984
+ overscrollBehavior: "contain"
985
+ }, children: [
986
+ state.messages.map((msg) => /* @__PURE__ */ jsx(LightMessageBubble, { msg, accent, agentName: agent.name }, msg.id)),
987
+ state.isTyping && /* @__PURE__ */ jsx(LightTypingIndicator, { accent })
988
+ ] }),
989
+ showChips && /* @__PURE__ */ jsx("div", { className: state.hasSentMessage ? "sd-r-scrollable" : "", style: {
990
+ padding: "0 20px 14px",
991
+ display: "flex",
992
+ flexWrap: state.hasSentMessage ? "nowrap" : "wrap",
993
+ gap: "6px",
994
+ flexShrink: 0
995
+ }, children: chips.map((chip) => /* @__PURE__ */ jsx("button", { className: "sd-r-chip-light", onClick: () => {
996
+ setInputValue("");
997
+ submit(chip);
998
+ }, style: {
999
+ fontSize: "12px",
1000
+ padding: "5px 10px",
1001
+ borderRadius: "6px",
1002
+ background: "#f4f3ee",
1003
+ border: "none",
1004
+ color: "#5a5a64",
1005
+ cursor: "pointer",
1006
+ fontFamily: "inherit",
1007
+ fontWeight: 500
1008
+ }, children: chip }, chip)) }),
1009
+ /* @__PURE__ */ jsxs("div", { style: { padding: "14px", borderTop: "1px solid rgba(20,20,40,0.05)", background: "#fdfcf9", flexShrink: 0 }, children: [
1010
+ /* @__PURE__ */ jsxs("div", { style: {
1011
+ background: "#fff",
1012
+ border: `1px solid ${inputValue ? `color-mix(in oklab, ${accent} 40%, transparent)` : "rgba(20,20,40,0.1)"}`,
1013
+ borderRadius: "14px",
1014
+ padding: "10px 12px",
1015
+ transition: "border-color .15s, box-shadow .15s",
1016
+ boxShadow: inputValue ? `0 0 0 4px color-mix(in oklab, ${accent} 12%, transparent)` : "none"
1017
+ }, children: [
1018
+ /* @__PURE__ */ jsx(
1019
+ "input",
1020
+ {
1021
+ ref: inputRef,
1022
+ value: inputValue,
1023
+ onChange: (e) => setInputValue(e.target.value),
1024
+ onKeyDown: handleKeyDown,
1025
+ placeholder: "Write a message\u2026",
1026
+ className: "sd-r-input",
1027
+ "aria-label": "Type your question",
1028
+ autoComplete: "off",
1029
+ style: { width: "100%", background: "transparent", border: "none", outline: "none", fontSize: "14px", fontFamily: "inherit", color: "#1a1a2e" }
1030
+ }
1031
+ ),
1032
+ /* @__PURE__ */ jsxs("div", { style: { display: "flex", alignItems: "center", marginTop: "8px", gap: "6px" }, children: [
1033
+ /* @__PURE__ */ jsx("div", { style: { flex: 1 } }),
1034
+ /* @__PURE__ */ jsx("button", { className: "sd-r-send", onClick: handleSubmit, "aria-label": "Send message", style: {
1035
+ width: 28,
1036
+ height: 28,
1037
+ borderRadius: 7,
1038
+ padding: 0,
1039
+ background: inputValue ? accent : "#e8e7e0",
1040
+ color: inputValue ? "#fff" : "#a8a89e",
1041
+ border: "none",
1042
+ cursor: "pointer",
1043
+ display: "grid",
1044
+ placeItems: "center",
1045
+ transition: "background .15s"
1046
+ }, children: /* @__PURE__ */ jsx(IconSend, { size: 13 }) })
1047
+ ] })
1048
+ ] }),
1049
+ showPoweredBy && /* @__PURE__ */ jsx("div", { style: { marginTop: "10px" }, children: /* @__PURE__ */ jsx(PoweredBy, {}) })
1050
+ ] })
1051
+ ] })
1052
+ ] });
1053
+ }
1054
+ function renderDark(p) {
1055
+ const {
1056
+ agent,
1057
+ state,
1058
+ chips,
1059
+ accent,
1060
+ isLeft,
1061
+ inputValue,
1062
+ setInputValue,
1063
+ handleClose,
1064
+ handleSubmit,
1065
+ handleKeyDown,
1066
+ isClosing,
1067
+ panelClass,
1068
+ showChips,
1069
+ showPoweredBy,
1070
+ threadRef,
1071
+ inputRef,
1072
+ triggerRef,
1073
+ open,
1074
+ submit
1075
+ } = p;
1076
+ const side = isLeft ? "left" : "right";
1077
+ const triggerStyle = {
1078
+ position: "fixed",
1079
+ bottom: "28px",
1080
+ [side]: "28px",
1081
+ width: "60px",
1082
+ height: "60px",
1083
+ borderRadius: "50%",
1084
+ background: `linear-gradient(135deg, color-mix(in oklab, ${accent} 70%, #1a1340), #1a1a2e)`,
1085
+ border: "1px solid rgba(255,255,255,0.12)",
1086
+ color: "#fff",
1087
+ cursor: "pointer",
1088
+ display: "grid",
1089
+ placeItems: "center",
1090
+ zIndex: 9999,
1091
+ padding: 0,
1092
+ transition: "transform 150ms ease",
1093
+ boxShadow: "0 16px 32px -8px rgba(0,0,0,0.5), inset 0 1px 0 rgba(255,255,255,0.1)"
1094
+ };
1095
+ const glowStyle = {
1096
+ position: "fixed",
1097
+ bottom: "18px",
1098
+ [side]: "18px",
1099
+ width: "80px",
1100
+ height: "80px",
1101
+ borderRadius: "50%",
1102
+ background: `radial-gradient(circle, color-mix(in oklab, ${accent} 50%, transparent), transparent 70%)`,
1103
+ filter: "blur(10px)",
1104
+ pointerEvents: "none",
1105
+ zIndex: 9998
1106
+ };
1107
+ const panelStyle = {
1108
+ position: "fixed",
1109
+ bottom: "100px",
1110
+ [side]: "28px",
1111
+ width: "380px",
1112
+ height: "580px",
1113
+ maxHeight: "580px",
1114
+ borderRadius: "22px",
1115
+ background: "rgba(18, 16, 32, 0.86)",
1116
+ backdropFilter: "blur(40px) saturate(180%)",
1117
+ WebkitBackdropFilter: "blur(40px) saturate(180%)",
1118
+ boxShadow: "0 30px 60px -20px rgba(0,0,0,0.4), 0 4px 14px rgba(0,0,0,0.2), inset 0 1px 0 rgba(255,255,255,0.06)",
1119
+ border: "1px solid rgba(255,255,255,0.08)",
1120
+ display: "flex",
1121
+ flexDirection: "column",
1122
+ overflow: "hidden",
1123
+ zIndex: 9999,
1124
+ color: "#fff",
1125
+ transformOrigin: isLeft ? "bottom left" : "bottom right"
1126
+ };
1127
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
1128
+ /* @__PURE__ */ jsx("div", { style: glowStyle, "aria-hidden": "true" }),
1129
+ /* @__PURE__ */ jsx(
1130
+ "button",
1131
+ {
1132
+ ref: triggerRef,
1133
+ style: triggerStyle,
1134
+ className: "sd-r-trigger",
1135
+ onClick: state.isOpen ? handleClose : open,
1136
+ "aria-label": "Open support chat",
1137
+ "aria-expanded": state.isOpen,
1138
+ children: /* @__PURE__ */ jsx(IconBot, { size: 22 })
1139
+ }
1140
+ ),
1141
+ (state.isOpen || isClosing) && /* @__PURE__ */ jsxs("div", { style: panelStyle, className: `sd-r-panel ${panelClass}`, role: "dialog", "aria-label": agent.name, children: [
1142
+ /* @__PURE__ */ jsx("div", { "aria-hidden": "true", style: {
1143
+ position: "absolute",
1144
+ top: 0,
1145
+ left: 0,
1146
+ right: 0,
1147
+ height: 200,
1148
+ pointerEvents: "none",
1149
+ background: `radial-gradient(ellipse 80% 100% at 50% 0%, color-mix(in oklab, ${accent} 40%, transparent) 0%, transparent 70%)`
1150
+ } }),
1151
+ /* @__PURE__ */ jsxs("div", { style: { padding: "20px 20px 16px", display: "flex", alignItems: "center", gap: "12px", position: "relative", zIndex: 1, flexShrink: 0 }, children: [
1152
+ /* @__PURE__ */ jsxs("div", { style: { position: "relative" }, children: [
1153
+ /* @__PURE__ */ jsx("div", { style: {
1154
+ width: 38,
1155
+ height: 38,
1156
+ borderRadius: "50%",
1157
+ display: "grid",
1158
+ placeItems: "center",
1159
+ overflow: "hidden",
1160
+ background: `linear-gradient(135deg, ${accent}, color-mix(in oklab, ${accent} 50%, #fff))`,
1161
+ boxShadow: `0 0 24px color-mix(in oklab, ${accent} 50%, transparent)`
1162
+ }, children: agent.avatarUrl ? /* @__PURE__ */ jsx("img", { src: agent.avatarUrl, alt: agent.name, style: { width: "100%", height: "100%", objectFit: "cover" } }) : /* @__PURE__ */ jsx(IconBot, { size: 16 }) }),
1163
+ /* @__PURE__ */ jsx("div", { style: { position: "absolute", right: -2, bottom: -2, width: 12, height: 12, borderRadius: "50%", background: "#22c55e", boxShadow: "0 0 0 2.5px rgba(18,16,32,1)" } })
1164
+ ] }),
1165
+ /* @__PURE__ */ jsxs("div", { style: { flex: 1 }, children: [
1166
+ /* @__PURE__ */ jsx("div", { style: { fontSize: "15px", fontWeight: 600, letterSpacing: "-0.01em" }, children: agent.name }),
1167
+ /* @__PURE__ */ jsxs("div", { style: { fontSize: "12px", color: "rgba(255,255,255,0.55)", marginTop: "3px", display: "flex", alignItems: "center", gap: "6px" }, children: [
1168
+ /* @__PURE__ */ jsx("span", { style: { width: 5, height: 5, borderRadius: "50%", background: "#4ade80", boxShadow: "0 0 6px #4ade80" } }),
1169
+ "Trained on this site \xB7 always on"
1170
+ ] })
1171
+ ] }),
1172
+ /* @__PURE__ */ jsx("button", { onClick: handleClose, "aria-label": "Close chat", style: {
1173
+ background: "rgba(255,255,255,0.06)",
1174
+ border: "1px solid rgba(255,255,255,0.08)",
1175
+ color: "rgba(255,255,255,0.7)",
1176
+ width: 30,
1177
+ height: 30,
1178
+ borderRadius: 8,
1179
+ cursor: "pointer",
1180
+ display: "grid",
1181
+ placeItems: "center",
1182
+ padding: 0
1183
+ }, children: /* @__PURE__ */ jsx(IconClose, { size: 14 }) })
1184
+ ] }),
1185
+ /* @__PURE__ */ jsxs("div", { ref: threadRef, role: "log", "aria-live": "polite", "aria-label": "Chat messages", style: {
1186
+ flex: 1,
1187
+ padding: "8px 20px 16px",
1188
+ overflowY: "auto",
1189
+ display: "flex",
1190
+ flexDirection: "column",
1191
+ gap: "14px",
1192
+ position: "relative",
1193
+ zIndex: 1,
1194
+ scrollbarWidth: "thin",
1195
+ overscrollBehavior: "contain"
1196
+ }, children: [
1197
+ state.messages.map((msg) => /* @__PURE__ */ jsx(DarkMessageBubble, { msg, accent }, msg.id)),
1198
+ state.isTyping && /* @__PURE__ */ jsx(DarkTypingIndicator, {})
1199
+ ] }),
1200
+ showChips && /* @__PURE__ */ jsxs("div", { style: { padding: state.hasSentMessage ? "0 0 16px" : "0 20px 16px", position: "relative", zIndex: 1, flexShrink: 0 }, children: [
1201
+ !state.hasSentMessage && /* @__PURE__ */ jsx("div", { style: { fontSize: "11px", color: "rgba(255,255,255,0.4)", letterSpacing: "0.08em", textTransform: "uppercase", fontWeight: 500, marginBottom: "8px" }, children: "Try asking" }),
1202
+ /* @__PURE__ */ jsx("div", { className: state.hasSentMessage ? "sd-r-scrollable" : "", style: {
1203
+ display: "flex",
1204
+ flexDirection: state.hasSentMessage ? "row" : "column",
1205
+ gap: "6px",
1206
+ padding: state.hasSentMessage ? "0 20px" : 0
1207
+ }, children: chips.map((chip) => /* @__PURE__ */ jsxs("button", { className: "sd-r-prompt", onClick: () => {
1208
+ setInputValue("");
1209
+ submit(chip);
1210
+ }, style: {
1211
+ textAlign: "left",
1212
+ padding: "10px 14px",
1213
+ borderRadius: "10px",
1214
+ background: "rgba(255,255,255,0.04)",
1215
+ border: "1px solid rgba(255,255,255,0.07)",
1216
+ color: "rgba(255,255,255,0.85)",
1217
+ cursor: "pointer",
1218
+ fontSize: "13px",
1219
+ fontFamily: "inherit",
1220
+ display: "flex",
1221
+ alignItems: "center",
1222
+ gap: "8px",
1223
+ transition: "background .12s",
1224
+ width: state.hasSentMessage ? "auto" : "100%",
1225
+ whiteSpace: state.hasSentMessage ? "nowrap" : "normal"
1226
+ }, children: [
1227
+ /* @__PURE__ */ jsx("svg", { width: "12", height: "12", viewBox: "0 0 24 24", fill: "none", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { d: "M9 6l6 6-6 6", stroke: "currentColor", strokeWidth: "1.6", strokeLinecap: "round", strokeLinejoin: "round" }) }),
1228
+ chip
1229
+ ] }, chip)) })
1230
+ ] }),
1231
+ /* @__PURE__ */ jsxs("div", { style: { padding: "14px", borderTop: "1px solid rgba(255,255,255,0.06)", position: "relative", zIndex: 1, flexShrink: 0 }, children: [
1232
+ /* @__PURE__ */ jsxs("div", { style: {
1233
+ display: "flex",
1234
+ alignItems: "center",
1235
+ gap: "8px",
1236
+ background: "rgba(255,255,255,0.05)",
1237
+ border: `1px solid ${inputValue ? `color-mix(in oklab, ${accent} 60%, transparent)` : "rgba(255,255,255,0.08)"}`,
1238
+ borderRadius: "12px",
1239
+ padding: "4px 4px 4px 14px",
1240
+ transition: "border-color .15s"
1241
+ }, children: [
1242
+ /* @__PURE__ */ jsx(
1243
+ "input",
1244
+ {
1245
+ ref: inputRef,
1246
+ value: inputValue,
1247
+ onChange: (e) => setInputValue(e.target.value),
1248
+ onKeyDown: handleKeyDown,
1249
+ placeholder: "Write a message\u2026",
1250
+ className: "sd-r-input",
1251
+ "aria-label": "Type your question",
1252
+ autoComplete: "off",
1253
+ style: { flex: 1, background: "transparent", border: "none", outline: "none", fontSize: "14px", padding: "9px 0", fontFamily: "inherit", color: "#fff" }
1254
+ }
1255
+ ),
1256
+ /* @__PURE__ */ jsx("button", { className: "sd-r-send", onClick: handleSubmit, "aria-label": "Send message", style: {
1257
+ width: 32,
1258
+ height: 32,
1259
+ borderRadius: 9,
1260
+ padding: 0,
1261
+ border: "none",
1262
+ cursor: "pointer",
1263
+ background: inputValue ? `linear-gradient(135deg, ${accent}, color-mix(in oklab, ${accent} 60%, #fff))` : "rgba(255,255,255,0.08)",
1264
+ color: inputValue ? "#fff" : "rgba(255,255,255,0.4)",
1265
+ display: "grid",
1266
+ placeItems: "center",
1267
+ transition: "background .15s",
1268
+ boxShadow: inputValue ? `0 4px 12px -2px color-mix(in oklab, ${accent} 60%, transparent)` : "none"
1269
+ }, children: /* @__PURE__ */ jsx(IconSend, { size: 14 }) })
1270
+ ] }),
1271
+ showPoweredBy && /* @__PURE__ */ jsx("div", { style: { marginTop: "10px" }, children: /* @__PURE__ */ jsx(PoweredBy, { dark: true }) })
1272
+ ] })
1273
+ ] })
1274
+ ] });
1275
+ }
1276
+ function SageDeskWidget({ indexUrl, agent, search: search2 }) {
1277
+ if (!indexUrl) {
1278
+ throw new Error(
1279
+ '[sagedesk] Required prop "indexUrl" is missing. Run `npx sagedesk build` and pass the output path, e.g. indexUrl="/support-index.json".'
1280
+ );
1281
+ }
1282
+ if (!agent?.name) {
1283
+ throw new Error('[sagedesk] Required prop "agent.name" is missing.');
1284
+ }
1285
+ const config = { indexUrl, agent, search: search2 };
1286
+ const { state, chips, open, close, submit } = useSageDesk(config);
1287
+ const theme = agent.theme ?? "classic";
1288
+ const accent = agent.accentColor ?? "#534AB7";
1289
+ const position = agent.position ?? "bottom-right";
1290
+ const isLeft = position === "bottom-left";
1291
+ const [inputValue, setInputValue] = useState2("");
1292
+ const [isClosing, setIsClosing] = useState2(false);
1293
+ const threadRef = useRef2(null);
1294
+ const inputRef = useRef2(null);
1295
+ const triggerRef = useRef2(null);
1296
+ const [mounted, setMounted] = useState2(false);
1297
+ useEffect2(() => {
1298
+ if (!indexUrl.startsWith("/") && !indexUrl.startsWith("http")) {
1299
+ console.warn(
1300
+ `[sagedesk] indexUrl "${indexUrl}" looks like a relative path. It should start with "/" so it resolves correctly from any page.`
1301
+ );
1302
+ }
1303
+ setMounted(true);
1304
+ injectStyles(theme);
1305
+ }, []);
1306
+ useEffect2(() => {
1307
+ if (threadRef.current) {
1308
+ threadRef.current.scrollTop = threadRef.current.scrollHeight;
1309
+ }
1310
+ }, [state.messages, state.isTyping]);
1311
+ useEffect2(() => {
1312
+ if (state.isOpen) {
1313
+ setTimeout(() => inputRef.current?.focus(), 50);
1314
+ }
1315
+ }, [state.isOpen]);
1316
+ const handleClose = useCallback2(() => {
1317
+ setIsClosing(true);
1318
+ setTimeout(() => {
1319
+ setIsClosing(false);
1320
+ close();
1321
+ triggerRef.current?.focus();
1322
+ }, 150);
1323
+ }, [close]);
1324
+ const handleSubmit = useCallback2(() => {
1325
+ const text = inputValue.trim();
1326
+ if (!text) return;
1327
+ setInputValue("");
1328
+ submit(text);
1329
+ }, [inputValue, submit]);
1330
+ const handleKeyDown = useCallback2(
1331
+ (e) => {
1332
+ if (e.key === "Enter" && !e.shiftKey) {
1333
+ e.preventDefault();
1334
+ handleSubmit();
1335
+ }
1336
+ },
1337
+ [handleSubmit]
1338
+ );
1339
+ if (!mounted || typeof document === "undefined") return null;
1340
+ const showPoweredBy = agent.poweredBy !== false;
1341
+ const showChips = chips.length > 0;
1342
+ const panelClass = isClosing ? "sd-r-closing" : state.isOpen ? "sd-r-opening" : "";
1343
+ const props = {
1344
+ agent,
1345
+ state,
1346
+ chips,
1347
+ accent,
1348
+ isLeft,
1349
+ inputValue,
1350
+ setInputValue,
1351
+ handleClose,
1352
+ handleSubmit,
1353
+ handleKeyDown,
1354
+ isClosing,
1355
+ panelClass,
1356
+ showChips,
1357
+ showPoweredBy,
1358
+ threadRef,
1359
+ inputRef,
1360
+ triggerRef,
1361
+ open,
1362
+ submit
1363
+ };
1364
+ const content = theme === "dark" ? renderDark(props) : theme === "light" ? renderLight(props) : renderClassic(props);
1365
+ return createPortal(content, document.body);
1366
+ }
1367
+ export {
1368
+ SageDeskWidget
1369
+ };
1370
+ //# sourceMappingURL=SageDeskWidget-P3H2VJR5.js.map