@idevconn/ai-chat-client 0.1.2

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,706 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/web-component/index.ts
31
+ var web_component_exports = {};
32
+ __export(web_component_exports, {
33
+ AiChatElement: () => AiChatElement
34
+ });
35
+ module.exports = __toCommonJS(web_component_exports);
36
+ var import_react6 = __toESM(require("react"));
37
+ var import_client = require("react-dom/client");
38
+
39
+ // src/react/AiChat.tsx
40
+ var import_react5 = require("react");
41
+
42
+ // src/hooks/use-chat.ts
43
+ var import_react2 = require("react");
44
+
45
+ // src/hooks/use-sse-stream.ts
46
+ var import_react = require("react");
47
+ function useSseStream(options) {
48
+ const [isStreaming, setIsStreaming] = (0, import_react.useState)(false);
49
+ const abortRef = (0, import_react.useRef)(null);
50
+ const abort = (0, import_react.useCallback)(() => {
51
+ abortRef.current?.abort();
52
+ abortRef.current = null;
53
+ setIsStreaming(false);
54
+ }, []);
55
+ const streamMessage = (0, import_react.useCallback)(
56
+ async (sessionId, message, onToken, onDone, onError) => {
57
+ abort();
58
+ const controller = new AbortController();
59
+ abortRef.current = controller;
60
+ setIsStreaming(true);
61
+ try {
62
+ const response = await fetch(options.endpoint, {
63
+ method: "POST",
64
+ headers: {
65
+ "Content-Type": "application/json",
66
+ ...options.headers
67
+ },
68
+ body: JSON.stringify({ sessionId, message }),
69
+ signal: controller.signal
70
+ });
71
+ if (!response.ok) {
72
+ onError(`Server error: ${response.status}`);
73
+ setIsStreaming(false);
74
+ return;
75
+ }
76
+ const reader = response.body?.getReader();
77
+ if (!reader) {
78
+ onError("No response body");
79
+ setIsStreaming(false);
80
+ return;
81
+ }
82
+ const decoder = new TextDecoder();
83
+ let buffer = "";
84
+ while (true) {
85
+ const { done, value } = await reader.read();
86
+ if (done) break;
87
+ buffer += decoder.decode(value, { stream: true });
88
+ const lines = buffer.split("\n");
89
+ buffer = lines.pop() || "";
90
+ for (const line of lines) {
91
+ if (!line.startsWith("data: ")) continue;
92
+ const data = line.slice(6).trim();
93
+ if (!data || data === "[DONE]") continue;
94
+ try {
95
+ const event = JSON.parse(data);
96
+ switch (event.type) {
97
+ case "token":
98
+ if (event.content) onToken(event.content);
99
+ break;
100
+ case "done":
101
+ onDone();
102
+ setIsStreaming(false);
103
+ return;
104
+ case "error":
105
+ onError(event.error || "Unknown error");
106
+ setIsStreaming(false);
107
+ return;
108
+ }
109
+ } catch {
110
+ }
111
+ }
112
+ }
113
+ onDone();
114
+ } catch (error) {
115
+ if (error.name !== "AbortError") {
116
+ onError(error.message || "Connection failed");
117
+ }
118
+ }
119
+ setIsStreaming(false);
120
+ },
121
+ [options.endpoint, options.headers, abort]
122
+ );
123
+ return { isStreaming, streamMessage, abort };
124
+ }
125
+
126
+ // src/lib/utils.ts
127
+ function generateId() {
128
+ return crypto.randomUUID?.() || Math.random().toString(36).slice(2);
129
+ }
130
+
131
+ // src/hooks/use-chat.ts
132
+ function useChat(config) {
133
+ const [messages, setMessages] = (0, import_react2.useState)([]);
134
+ const sessionIdRef = (0, import_react2.useRef)(generateId());
135
+ const headers = {};
136
+ if (config.apiKey) headers["x-api-key"] = config.apiKey;
137
+ if (config.token) headers["Authorization"] = `Bearer ${config.token}`;
138
+ const { isStreaming, streamMessage } = useSseStream({
139
+ endpoint: config.endpoint,
140
+ headers
141
+ });
142
+ const sendMessage = (0, import_react2.useCallback)(
143
+ (text) => {
144
+ if (!text.trim() || isStreaming) return;
145
+ const userMsg = {
146
+ id: generateId(),
147
+ role: "user",
148
+ content: text,
149
+ timestamp: Date.now()
150
+ };
151
+ const assistantMsg = {
152
+ id: generateId(),
153
+ role: "assistant",
154
+ content: "",
155
+ timestamp: Date.now(),
156
+ streaming: true
157
+ };
158
+ setMessages((prev) => [...prev, userMsg, assistantMsg]);
159
+ streamMessage(
160
+ sessionIdRef.current,
161
+ text,
162
+ (token) => {
163
+ setMessages((prev) => {
164
+ const updated = [...prev];
165
+ const last = updated[updated.length - 1];
166
+ if (last && last.role === "assistant") {
167
+ updated[updated.length - 1] = {
168
+ ...last,
169
+ content: last.content + token
170
+ };
171
+ }
172
+ return updated;
173
+ });
174
+ },
175
+ () => {
176
+ setMessages((prev) => {
177
+ const updated = [...prev];
178
+ const last = updated[updated.length - 1];
179
+ if (last && last.role === "assistant") {
180
+ updated[updated.length - 1] = {
181
+ ...last,
182
+ streaming: false
183
+ };
184
+ }
185
+ return updated;
186
+ });
187
+ },
188
+ (error) => {
189
+ setMessages((prev) => {
190
+ const updated = [...prev];
191
+ const last = updated[updated.length - 1];
192
+ if (last && last.role === "assistant") {
193
+ updated[updated.length - 1] = {
194
+ ...last,
195
+ content: `Error: ${error}`,
196
+ streaming: false
197
+ };
198
+ }
199
+ return updated;
200
+ });
201
+ }
202
+ );
203
+ },
204
+ [isStreaming, streamMessage]
205
+ );
206
+ const clearMessages = (0, import_react2.useCallback)(() => {
207
+ setMessages([]);
208
+ sessionIdRef.current = generateId();
209
+ }, []);
210
+ return { messages, isStreaming, sendMessage, clearMessages };
211
+ }
212
+
213
+ // src/react/ChatFab.tsx
214
+ var import_jsx_runtime = require("react/jsx-runtime");
215
+ function ChatFab({
216
+ isOpen,
217
+ onClick,
218
+ primaryColor,
219
+ position = "bottom-right"
220
+ }) {
221
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
222
+ "button",
223
+ {
224
+ onClick,
225
+ style: {
226
+ position: "fixed",
227
+ zIndex: 9999,
228
+ bottom: "16px",
229
+ right: position === "bottom-right" ? "16px" : "auto",
230
+ left: position === "bottom-left" ? "16px" : "auto",
231
+ display: "flex",
232
+ alignItems: "center",
233
+ justifyContent: "center",
234
+ width: "56px",
235
+ height: "56px",
236
+ borderRadius: "50%",
237
+ border: "none",
238
+ cursor: "pointer",
239
+ boxShadow: "0 4px 12px rgba(0,0,0,0.15)",
240
+ backgroundColor: primaryColor || "#6366f1",
241
+ transition: "transform 0.2s"
242
+ },
243
+ "aria-label": isOpen ? "Close chat" : "Open chat",
244
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
245
+ "svg",
246
+ {
247
+ style: { width: "24px", height: "24px", color: "white" },
248
+ fill: "none",
249
+ viewBox: "0 0 24 24",
250
+ stroke: "currentColor",
251
+ strokeWidth: 2,
252
+ children: isOpen ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("path", { strokeLinecap: "round", strokeLinejoin: "round", d: "M6 18L18 6M6 6l12 12" }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
253
+ "path",
254
+ {
255
+ strokeLinecap: "round",
256
+ strokeLinejoin: "round",
257
+ d: "M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z"
258
+ }
259
+ )
260
+ }
261
+ )
262
+ }
263
+ );
264
+ }
265
+
266
+ // src/react/ChatHeader.tsx
267
+ var import_jsx_runtime2 = require("react/jsx-runtime");
268
+ function ChatHeader({ botName, botAvatar, primaryColor, onClose }) {
269
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
270
+ "div",
271
+ {
272
+ style: {
273
+ display: "flex",
274
+ alignItems: "center",
275
+ justifyContent: "space-between",
276
+ padding: "12px 16px",
277
+ borderBottom: "1px solid #e5e7eb",
278
+ backgroundColor: primaryColor || "#6366f1"
279
+ },
280
+ children: [
281
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: { display: "flex", alignItems: "center", gap: "8px" }, children: [
282
+ botAvatar && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { style: { fontSize: "18px" }, children: botAvatar }),
283
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { style: { color: "white", fontWeight: 500, fontSize: "14px" }, children: botName || "AI Assistant" })
284
+ ] }),
285
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
286
+ "button",
287
+ {
288
+ onClick: onClose,
289
+ style: {
290
+ background: "transparent",
291
+ border: "none",
292
+ cursor: "pointer",
293
+ padding: "4px",
294
+ borderRadius: "4px",
295
+ color: "rgba(255,255,255,0.8)"
296
+ },
297
+ "aria-label": "Close chat",
298
+ children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
299
+ "svg",
300
+ {
301
+ style: { width: "20px", height: "20px" },
302
+ fill: "none",
303
+ viewBox: "0 0 24 24",
304
+ stroke: "currentColor",
305
+ strokeWidth: 2,
306
+ children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("path", { strokeLinecap: "round", strokeLinejoin: "round", d: "M6 18L18 6M6 6l12 12" })
307
+ }
308
+ )
309
+ }
310
+ )
311
+ ]
312
+ }
313
+ );
314
+ }
315
+
316
+ // src/react/MessageList.tsx
317
+ var import_react3 = require("react");
318
+
319
+ // src/react/UserMessage.tsx
320
+ var import_jsx_runtime3 = require("react/jsx-runtime");
321
+ function UserMessage({ content }) {
322
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { display: "flex", justifyContent: "flex-end", marginBottom: "12px" }, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
323
+ "div",
324
+ {
325
+ style: {
326
+ maxWidth: "80%",
327
+ borderRadius: "8px",
328
+ padding: "8px 16px",
329
+ backgroundColor: "#6366f1",
330
+ color: "white",
331
+ fontSize: "14px"
332
+ },
333
+ children: content
334
+ }
335
+ ) });
336
+ }
337
+
338
+ // src/react/BotMessage.tsx
339
+ var import_jsx_runtime4 = require("react/jsx-runtime");
340
+ function BotMessage({ content, streaming }) {
341
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { style: { display: "flex", justifyContent: "flex-start", marginBottom: "12px" }, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
342
+ "div",
343
+ {
344
+ style: {
345
+ maxWidth: "80%",
346
+ borderRadius: "8px",
347
+ padding: "8px 16px",
348
+ backgroundColor: "#f3f4f6",
349
+ color: "#1f2937",
350
+ fontSize: "14px"
351
+ },
352
+ children: [
353
+ content || streaming && "...",
354
+ streaming && content && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
355
+ "span",
356
+ {
357
+ style: {
358
+ display: "inline-block",
359
+ width: "6px",
360
+ height: "16px",
361
+ marginLeft: "2px",
362
+ backgroundColor: "#9ca3af",
363
+ animation: "pulse 1s infinite"
364
+ }
365
+ }
366
+ )
367
+ ]
368
+ }
369
+ ) });
370
+ }
371
+
372
+ // src/react/MessageList.tsx
373
+ var import_jsx_runtime5 = require("react/jsx-runtime");
374
+ function MessageList({ messages }) {
375
+ const bottomRef = (0, import_react3.useRef)(null);
376
+ (0, import_react3.useEffect)(() => {
377
+ bottomRef.current?.scrollIntoView({ behavior: "smooth" });
378
+ }, [messages]);
379
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { flex: 1, overflowY: "auto", padding: "16px" }, children: [
380
+ messages.map(
381
+ (msg) => msg.role === "user" ? /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(UserMessage, { content: msg.content }, msg.id) : /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(BotMessage, { content: msg.content, streaming: msg.streaming }, msg.id)
382
+ ),
383
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { ref: bottomRef })
384
+ ] });
385
+ }
386
+
387
+ // src/react/EmptyState.tsx
388
+ var import_jsx_runtime6 = require("react/jsx-runtime");
389
+ function EmptyState({ welcomeMessage, starterTopics, onTopicClick }) {
390
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(
391
+ "div",
392
+ {
393
+ style: {
394
+ display: "flex",
395
+ flexDirection: "column",
396
+ alignItems: "center",
397
+ justifyContent: "center",
398
+ height: "100%",
399
+ padding: "24px",
400
+ textAlign: "center"
401
+ },
402
+ children: [
403
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("p", { style: { color: "#6b7280", fontSize: "14px", marginBottom: "24px" }, children: welcomeMessage || "Hi! How can I help you today?" }),
404
+ starterTopics && starterTopics.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
405
+ "div",
406
+ {
407
+ style: {
408
+ display: "flex",
409
+ flexDirection: "column",
410
+ gap: "8px",
411
+ width: "100%",
412
+ maxWidth: "280px"
413
+ },
414
+ children: starterTopics.map((topic, i) => /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(
415
+ "button",
416
+ {
417
+ onClick: () => onTopicClick(topic.message),
418
+ style: {
419
+ display: "flex",
420
+ alignItems: "center",
421
+ gap: "8px",
422
+ padding: "12px 16px",
423
+ textAlign: "left",
424
+ fontSize: "14px",
425
+ borderRadius: "8px",
426
+ border: "1px solid #e5e7eb",
427
+ backgroundColor: "white",
428
+ cursor: "pointer"
429
+ },
430
+ children: [
431
+ topic.icon && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { children: topic.icon }),
432
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { children: [
433
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { style: { color: "#1f2937", fontWeight: 500 }, children: topic.label }),
434
+ topic.description && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
435
+ "span",
436
+ {
437
+ style: {
438
+ display: "block",
439
+ fontSize: "12px",
440
+ color: "#6b7280",
441
+ marginTop: "2px"
442
+ },
443
+ children: topic.description
444
+ }
445
+ )
446
+ ] })
447
+ ]
448
+ },
449
+ i
450
+ ))
451
+ }
452
+ )
453
+ ]
454
+ }
455
+ );
456
+ }
457
+
458
+ // src/react/ChatInput.tsx
459
+ var import_react4 = require("react");
460
+ var import_jsx_runtime7 = require("react/jsx-runtime");
461
+ function ChatInput({ onSend, disabled, placeholder }) {
462
+ const [value, setValue] = (0, import_react4.useState)("");
463
+ const handleSend = (0, import_react4.useCallback)(() => {
464
+ if (!value.trim() || disabled) return;
465
+ onSend(value.trim());
466
+ setValue("");
467
+ }, [value, disabled, onSend]);
468
+ const handleKeyDown = (0, import_react4.useCallback)(
469
+ (e) => {
470
+ if (e.key === "Enter" && !e.shiftKey) {
471
+ e.preventDefault();
472
+ handleSend();
473
+ }
474
+ },
475
+ [handleSend]
476
+ );
477
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(
478
+ "div",
479
+ {
480
+ style: {
481
+ display: "flex",
482
+ alignItems: "center",
483
+ gap: "8px",
484
+ padding: "12px",
485
+ borderTop: "1px solid #e5e7eb"
486
+ },
487
+ children: [
488
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
489
+ "input",
490
+ {
491
+ type: "text",
492
+ value,
493
+ onChange: (e) => setValue(e.target.value),
494
+ onKeyDown: handleKeyDown,
495
+ disabled,
496
+ placeholder: placeholder || "Type your message...",
497
+ style: {
498
+ flex: 1,
499
+ padding: "8px 12px",
500
+ fontSize: "14px",
501
+ border: "1px solid #e5e7eb",
502
+ borderRadius: "8px",
503
+ outline: "none",
504
+ opacity: disabled ? 0.5 : 1
505
+ }
506
+ }
507
+ ),
508
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
509
+ "button",
510
+ {
511
+ onClick: handleSend,
512
+ disabled: disabled || !value.trim(),
513
+ style: {
514
+ display: "flex",
515
+ alignItems: "center",
516
+ justifyContent: "center",
517
+ width: "36px",
518
+ height: "36px",
519
+ borderRadius: "8px",
520
+ border: "none",
521
+ cursor: disabled || !value.trim() ? "not-allowed" : "pointer",
522
+ backgroundColor: "#6366f1",
523
+ opacity: disabled || !value.trim() ? 0.5 : 1
524
+ },
525
+ "aria-label": "Send message",
526
+ children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
527
+ "svg",
528
+ {
529
+ style: { width: "16px", height: "16px", color: "white" },
530
+ fill: "none",
531
+ viewBox: "0 0 24 24",
532
+ stroke: "currentColor",
533
+ strokeWidth: 2,
534
+ children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("path", { strokeLinecap: "round", strokeLinejoin: "round", d: "M12 19l9 2-9-18-9 18 9-2zm0 0v-8" })
535
+ }
536
+ )
537
+ }
538
+ )
539
+ ]
540
+ }
541
+ );
542
+ }
543
+
544
+ // src/react/ChatPanel.tsx
545
+ var import_jsx_runtime8 = require("react/jsx-runtime");
546
+ function ChatPanel({ config, messages, isStreaming, onSend, onClose }) {
547
+ const position = config.theme?.position || "bottom-right";
548
+ return /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(
549
+ "div",
550
+ {
551
+ style: {
552
+ position: "fixed",
553
+ zIndex: 9999,
554
+ bottom: "80px",
555
+ right: position === "bottom-right" ? "16px" : "auto",
556
+ left: position === "bottom-left" ? "16px" : "auto",
557
+ width: config.theme?.width || "380px",
558
+ height: config.theme?.height || "560px",
559
+ borderRadius: config.theme?.borderRadius || "12px",
560
+ backgroundColor: "#ffffff",
561
+ boxShadow: "0 25px 50px -12px rgba(0,0,0,0.25)",
562
+ border: "1px solid #e5e7eb",
563
+ display: "flex",
564
+ flexDirection: "column",
565
+ overflow: "hidden"
566
+ },
567
+ children: [
568
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
569
+ ChatHeader,
570
+ {
571
+ botName: config.botName,
572
+ botAvatar: config.botAvatar,
573
+ primaryColor: config.theme?.primaryColor,
574
+ onClose
575
+ }
576
+ ),
577
+ messages.length === 0 ? /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
578
+ EmptyState,
579
+ {
580
+ welcomeMessage: config.welcomeMessage,
581
+ starterTopics: config.starterTopics,
582
+ onTopicClick: onSend
583
+ }
584
+ ) : /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(MessageList, { messages }),
585
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(ChatInput, { onSend, disabled: isStreaming, placeholder: config.placeholder })
586
+ ]
587
+ }
588
+ );
589
+ }
590
+
591
+ // src/react/AiChat.tsx
592
+ var import_jsx_runtime9 = require("react/jsx-runtime");
593
+ var _isOpen = false;
594
+ var listeners = /* @__PURE__ */ new Set();
595
+ function setOpen(value) {
596
+ _isOpen = value;
597
+ listeners.forEach((l) => l());
598
+ }
599
+ function subscribe(listener) {
600
+ listeners.add(listener);
601
+ return () => listeners.delete(listener);
602
+ }
603
+ function getSnapshot() {
604
+ return _isOpen;
605
+ }
606
+ function AiChat(props) {
607
+ const isOpen = (0, import_react5.useSyncExternalStore)(subscribe, getSnapshot);
608
+ const { messages, isStreaming, sendMessage } = useChat(props);
609
+ const toggle = (0, import_react5.useCallback)((e) => {
610
+ e.preventDefault();
611
+ e.stopPropagation();
612
+ setOpen(!_isOpen);
613
+ }, []);
614
+ return /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(import_jsx_runtime9.Fragment, { children: [
615
+ isOpen && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
616
+ ChatPanel,
617
+ {
618
+ config: props,
619
+ messages,
620
+ isStreaming,
621
+ onSend: sendMessage,
622
+ onClose: () => setOpen(false)
623
+ }
624
+ ),
625
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
626
+ ChatFab,
627
+ {
628
+ isOpen,
629
+ onClick: toggle,
630
+ primaryColor: props.theme?.primaryColor,
631
+ position: props.theme?.position
632
+ }
633
+ )
634
+ ] });
635
+ }
636
+
637
+ // src/web-component/index.ts
638
+ var AiChatElement = class extends HTMLElement {
639
+ root = null;
640
+ shadowContainer = null;
641
+ static get observedAttributes() {
642
+ return [
643
+ "endpoint",
644
+ "api-key",
645
+ "token",
646
+ "theme",
647
+ "starter-topics",
648
+ "welcome-message",
649
+ "bot-name"
650
+ ];
651
+ }
652
+ connectedCallback() {
653
+ const shadow = this.attachShadow({ mode: "open" });
654
+ this.shadowContainer = document.createElement("div");
655
+ shadow.appendChild(this.shadowContainer);
656
+ this.root = (0, import_client.createRoot)(this.shadowContainer);
657
+ this.render();
658
+ }
659
+ disconnectedCallback() {
660
+ this.root?.unmount();
661
+ this.root = null;
662
+ }
663
+ attributeChangedCallback() {
664
+ this.render();
665
+ }
666
+ render() {
667
+ if (!this.root) return;
668
+ const config = this.buildConfig();
669
+ this.root.render(import_react6.default.createElement(AiChat, config));
670
+ }
671
+ buildConfig() {
672
+ const config = {
673
+ endpoint: this.getAttribute("endpoint") || "/api/v1/chat"
674
+ };
675
+ const apiKey = this.getAttribute("api-key");
676
+ if (apiKey) config.apiKey = apiKey;
677
+ const token = this.getAttribute("token");
678
+ if (token) config.token = token;
679
+ const welcomeMessage = this.getAttribute("welcome-message");
680
+ if (welcomeMessage) config.welcomeMessage = welcomeMessage;
681
+ const botName = this.getAttribute("bot-name");
682
+ if (botName) config.botName = botName;
683
+ const themeAttr = this.getAttribute("theme");
684
+ if (themeAttr) {
685
+ try {
686
+ config.theme = JSON.parse(themeAttr);
687
+ } catch {
688
+ }
689
+ }
690
+ const topicsAttr = this.getAttribute("starter-topics");
691
+ if (topicsAttr) {
692
+ try {
693
+ config.starterTopics = JSON.parse(topicsAttr);
694
+ } catch {
695
+ }
696
+ }
697
+ return config;
698
+ }
699
+ };
700
+ if (typeof window !== "undefined" && !customElements.get("ai-chat")) {
701
+ customElements.define("ai-chat", AiChatElement);
702
+ }
703
+ // Annotate the CommonJS export names for ESM import in node:
704
+ 0 && (module.exports = {
705
+ AiChatElement
706
+ });