aizek-chatbot 1.0.28 → 1.0.29
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/package.json +1 -1
- package/dist/index.cjs +0 -1142
- package/dist/index.cjs.map +0 -1
- package/dist/index.css +0 -1852
- package/dist/index.css.map +0 -1
- package/dist/index.d.mts +0 -50
- package/dist/index.d.ts +0 -50
- package/dist/index.mjs +0 -1134
- package/dist/index.mjs.map +0 -1
package/dist/index.mjs
DELETED
|
@@ -1,1134 +0,0 @@
|
|
|
1
|
-
import { useRef, useState, useEffect } from 'react';
|
|
2
|
-
import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
|
|
3
|
-
import ReactMarkdown from 'react-markdown';
|
|
4
|
-
import remarkGfm from 'remark-gfm';
|
|
5
|
-
import sanitizeHtml from 'sanitize-html';
|
|
6
|
-
|
|
7
|
-
// src/utils/global.ts
|
|
8
|
-
var SESSION_TTL_MS = 60 * 60 * 1e3;
|
|
9
|
-
var MAX_SESSIONS = 3;
|
|
10
|
-
var DEVICE_KEY = "aizek_device_id_v1";
|
|
11
|
-
var ACTIVE_SESSION_KEY = "aizek_active_session_v1";
|
|
12
|
-
var LOCAL_SESSIONS_KEY = "aizek_sessions_v1";
|
|
13
|
-
function validateHeaders(headers, authConfig, opts = {}) {
|
|
14
|
-
if (headers && (!authConfig || Object.keys(authConfig).length === 0)) {
|
|
15
|
-
return {
|
|
16
|
-
isValid: false,
|
|
17
|
-
missingKeys: [],
|
|
18
|
-
extraKeys: [],
|
|
19
|
-
emptyValueKeys: [],
|
|
20
|
-
warning: "Auth config bo\u015F ya da tan\u0131ms\u0131z, header do\u011Frulamas\u0131 yap\u0131lam\u0131yor."
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
const { caseSensitive = false, allowExtra = false } = opts;
|
|
24
|
-
const normalize = (s) => caseSensitive ? s : s.toLowerCase();
|
|
25
|
-
const headerEntries = Object.entries(headers).map(([k, v]) => [normalize(k), v.trim()]);
|
|
26
|
-
const authEntries = Object.entries(authConfig).map(([k, v]) => [normalize(k), v.trim()]);
|
|
27
|
-
const headerKeys = headerEntries.map(([k]) => k);
|
|
28
|
-
const authKeys = authEntries.map(([k]) => k);
|
|
29
|
-
const requiredSet = new Set(authKeys);
|
|
30
|
-
const missingKeys = authKeys.filter((k) => !headerKeys.includes(k));
|
|
31
|
-
const extraKeys = headerKeys.filter((k) => !requiredSet.has(k));
|
|
32
|
-
const hasAllRequired = missingKeys.length === 0;
|
|
33
|
-
const hasExtraKeys = extraKeys.length > 0 && !allowExtra;
|
|
34
|
-
const emptyValueKeys = authKeys.filter((k) => {
|
|
35
|
-
const val = headerEntries.find(([key]) => key === k)?.[1];
|
|
36
|
-
return !val || val.length === 0;
|
|
37
|
-
});
|
|
38
|
-
const isValid = hasAllRequired && !hasExtraKeys && emptyValueKeys.length === 0;
|
|
39
|
-
return { isValid, missingKeys, extraKeys, emptyValueKeys };
|
|
40
|
-
}
|
|
41
|
-
function getOrCreateDeviceId() {
|
|
42
|
-
if (typeof window === "undefined") return "";
|
|
43
|
-
const existing = localStorage.getItem(DEVICE_KEY);
|
|
44
|
-
if (existing) return existing;
|
|
45
|
-
const id = crypto.randomUUID();
|
|
46
|
-
localStorage.setItem(DEVICE_KEY, id);
|
|
47
|
-
return id;
|
|
48
|
-
}
|
|
49
|
-
function now() {
|
|
50
|
-
return Date.now();
|
|
51
|
-
}
|
|
52
|
-
function readStoredSessions() {
|
|
53
|
-
try {
|
|
54
|
-
const raw = localStorage.getItem(LOCAL_SESSIONS_KEY);
|
|
55
|
-
const parsed = JSON.parse(raw ?? "[]");
|
|
56
|
-
if (!Array.isArray(parsed)) return [];
|
|
57
|
-
return parsed.filter((x) => x && typeof x.sessionId === "string" && typeof x.lastActive === "number").slice(0, MAX_SESSIONS);
|
|
58
|
-
} catch {
|
|
59
|
-
return [];
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
function writeStoredSessions(list) {
|
|
63
|
-
localStorage.setItem(LOCAL_SESSIONS_KEY, JSON.stringify(list.slice(0, MAX_SESSIONS)));
|
|
64
|
-
}
|
|
65
|
-
function purgeExpiredLocalSessions(setActiveSessionIdState) {
|
|
66
|
-
const list = readStoredSessions();
|
|
67
|
-
const filtered = list.filter((s) => now() - s.lastActive <= SESSION_TTL_MS);
|
|
68
|
-
if (filtered.length !== list.length) writeStoredSessions(filtered);
|
|
69
|
-
const active = getActiveSessionId();
|
|
70
|
-
if (active && !filtered.some((s) => s.sessionId === active)) {
|
|
71
|
-
setActiveSessionId(null);
|
|
72
|
-
setActiveSessionIdState(null);
|
|
73
|
-
}
|
|
74
|
-
return filtered;
|
|
75
|
-
}
|
|
76
|
-
function getActiveSessionId() {
|
|
77
|
-
return localStorage.getItem(ACTIVE_SESSION_KEY);
|
|
78
|
-
}
|
|
79
|
-
function setActiveSessionId(id) {
|
|
80
|
-
if (!id) localStorage.removeItem(ACTIVE_SESSION_KEY);
|
|
81
|
-
else localStorage.setItem(ACTIVE_SESSION_KEY, id);
|
|
82
|
-
}
|
|
83
|
-
function touchSession(sessionId) {
|
|
84
|
-
const list = readStoredSessions();
|
|
85
|
-
const next = list.map((s) => s.sessionId === sessionId ? { ...s, lastActive: now() } : s);
|
|
86
|
-
if (!next.some((s) => s.sessionId === sessionId)) next.unshift({ sessionId, lastActive: now() });
|
|
87
|
-
writeStoredSessions(next.slice(0, MAX_SESSIONS));
|
|
88
|
-
}
|
|
89
|
-
function upsertSessionsFromServer(serverSessionIds, setSessions) {
|
|
90
|
-
const local = readStoredSessions();
|
|
91
|
-
const map = new Map(local.map((s) => [s.sessionId, s.lastActive]));
|
|
92
|
-
const merged = serverSessionIds.slice(0, MAX_SESSIONS).map((sid) => ({
|
|
93
|
-
sessionId: sid,
|
|
94
|
-
lastActive: map.get(sid) ?? now()
|
|
95
|
-
}));
|
|
96
|
-
writeStoredSessions(merged);
|
|
97
|
-
setSessions(merged.map((x) => x.sessionId));
|
|
98
|
-
return merged;
|
|
99
|
-
}
|
|
100
|
-
var HeaderAlert = ({ headerValidation, showAlert, setShowAlert }) => {
|
|
101
|
-
if (!headerValidation || !showAlert) return null;
|
|
102
|
-
const { isValid, missingKeys, extraKeys, emptyValueKeys, warning } = headerValidation;
|
|
103
|
-
if (isValid && missingKeys.length === 0 && extraKeys.length === 0 && emptyValueKeys.length === 0 && !warning) {
|
|
104
|
-
return null;
|
|
105
|
-
}
|
|
106
|
-
const hasErrors = missingKeys.length > 0 || emptyValueKeys.length > 0;
|
|
107
|
-
const hasWarnings = extraKeys.length > 0 || !!warning;
|
|
108
|
-
const alertType = hasErrors ? "error" : "warning";
|
|
109
|
-
const getAlertIcon = () => {
|
|
110
|
-
if (hasErrors) {
|
|
111
|
-
return /* @__PURE__ */ jsx("svg", { width: "20", height: "20", viewBox: "0 0 24 24", fill: "currentColor", children: /* @__PURE__ */ jsx("path", { d: "M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z" }) });
|
|
112
|
-
}
|
|
113
|
-
return /* @__PURE__ */ jsx("svg", { width: "20", height: "20", viewBox: "0 0 24 24", fill: "currentColor", children: /* @__PURE__ */ jsx("path", { d: "M1 21h22L12 2 1 21zm12-3h-2v-2h2v2zm0-4h-2v-4h2v4z" }) });
|
|
114
|
-
};
|
|
115
|
-
return /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsxs("div", { className: `alert-container ${alertType}`, children: [
|
|
116
|
-
/* @__PURE__ */ jsx("div", { className: "alert-icon-container", children: getAlertIcon() }),
|
|
117
|
-
/* @__PURE__ */ jsxs("div", { className: "alert-content", children: [
|
|
118
|
-
/* @__PURE__ */ jsx("h4", { className: "alert-title", children: hasErrors ? "Header Do\u011Frulama Hatas\u0131" : "Header Uyar\u0131s\u0131" }),
|
|
119
|
-
/* @__PURE__ */ jsx("p", { className: "alert-message", children: hasErrors && hasWarnings ? "Header yap\u0131land\u0131rman\u0131zda hatalar ve uyar\u0131lar bulundu." : hasErrors ? "Header yap\u0131land\u0131rman\u0131zda hatalar bulundu." : "Header yap\u0131land\u0131rman\u0131zda fazla anahtarlar bulundu." }),
|
|
120
|
-
missingKeys.length > 0 && /* @__PURE__ */ jsxs("div", { children: [
|
|
121
|
-
/* @__PURE__ */ jsx("strong", { children: "Eksik Header'lar:" }),
|
|
122
|
-
/* @__PURE__ */ jsx("ul", { className: "alert-list", children: missingKeys.map((key, index) => /* @__PURE__ */ jsxs("li", { className: "alert-list-item", children: [
|
|
123
|
-
/* @__PURE__ */ jsx("span", { children: "\u2022" }),
|
|
124
|
-
/* @__PURE__ */ jsx("code", { children: key })
|
|
125
|
-
] }, index)) })
|
|
126
|
-
] }),
|
|
127
|
-
emptyValueKeys.length > 0 && /* @__PURE__ */ jsxs("div", { children: [
|
|
128
|
-
/* @__PURE__ */ jsx("strong", { children: "Bo\u015F De\u011Ferli Header'lar:" }),
|
|
129
|
-
/* @__PURE__ */ jsx("ul", { className: "alert-list", children: emptyValueKeys.map((key, index) => /* @__PURE__ */ jsxs("li", { className: "alert-list-item", children: [
|
|
130
|
-
/* @__PURE__ */ jsx("span", { children: "\u2022" }),
|
|
131
|
-
/* @__PURE__ */ jsx("code", { children: key }),
|
|
132
|
-
/* @__PURE__ */ jsx("span", { children: "(de\u011Fer bo\u015F olamaz)" })
|
|
133
|
-
] }, index)) })
|
|
134
|
-
] }),
|
|
135
|
-
extraKeys.length > 0 && /* @__PURE__ */ jsxs("div", { children: [
|
|
136
|
-
/* @__PURE__ */ jsx("strong", { children: "Fazla Header'lar:" }),
|
|
137
|
-
/* @__PURE__ */ jsx("ul", { className: "alert-list", children: extraKeys.map((key, index) => /* @__PURE__ */ jsxs("li", { className: "alert-list-item", children: [
|
|
138
|
-
/* @__PURE__ */ jsx("span", { children: "\u2022" }),
|
|
139
|
-
/* @__PURE__ */ jsx("code", { children: key })
|
|
140
|
-
] }, index)) })
|
|
141
|
-
] }),
|
|
142
|
-
warning && /* @__PURE__ */ jsxs("div", { children: [
|
|
143
|
-
/* @__PURE__ */ jsx("strong", { children: "Uyar\u0131:" }),
|
|
144
|
-
/* @__PURE__ */ jsx("p", { className: "alert-message", children: warning })
|
|
145
|
-
] })
|
|
146
|
-
] }),
|
|
147
|
-
/* @__PURE__ */ jsx(
|
|
148
|
-
"button",
|
|
149
|
-
{
|
|
150
|
-
onClick: () => setShowAlert(false),
|
|
151
|
-
className: "alert-close-button",
|
|
152
|
-
"aria-label": "Uyar\u0131y\u0131 kapat",
|
|
153
|
-
children: /* @__PURE__ */ jsx("svg", { width: "16", height: "16", viewBox: "0 0 24 24", fill: "currentColor", children: /* @__PURE__ */ jsx("path", { d: "M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z" }) })
|
|
154
|
-
}
|
|
155
|
-
)
|
|
156
|
-
] }) });
|
|
157
|
-
};
|
|
158
|
-
var LoadingSpinner = () => {
|
|
159
|
-
return /* @__PURE__ */ jsx("div", { className: "loading-spinner" });
|
|
160
|
-
};
|
|
161
|
-
var ChatInput = ({ isLoading, placeholder, handleSendMessage }) => {
|
|
162
|
-
const [message, setMessage] = useState("");
|
|
163
|
-
const textareaRef = useRef(null);
|
|
164
|
-
const handleSubmit = (e) => {
|
|
165
|
-
e.preventDefault();
|
|
166
|
-
if (message.trim() && !isLoading) {
|
|
167
|
-
handleSendMessage(message.trim());
|
|
168
|
-
setMessage("");
|
|
169
|
-
if (textareaRef.current) {
|
|
170
|
-
textareaRef.current.style.height = "auto";
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
const handleKeyDown = (e) => {
|
|
175
|
-
if (e.key === "Enter" && !e.shiftKey) {
|
|
176
|
-
e.preventDefault();
|
|
177
|
-
handleSubmit(e);
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
const handleInputChange = (e) => {
|
|
181
|
-
setMessage(e.target.value);
|
|
182
|
-
const textarea = e.target;
|
|
183
|
-
textarea.style.height = "auto";
|
|
184
|
-
textarea.style.height = Math.min(textarea.scrollHeight, 120) + "px";
|
|
185
|
-
};
|
|
186
|
-
return /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, className: "input-container", children: [
|
|
187
|
-
/* @__PURE__ */ jsx(
|
|
188
|
-
"textarea",
|
|
189
|
-
{
|
|
190
|
-
ref: textareaRef,
|
|
191
|
-
value: message,
|
|
192
|
-
onChange: handleInputChange,
|
|
193
|
-
onKeyDown: handleKeyDown,
|
|
194
|
-
placeholder,
|
|
195
|
-
disabled: isLoading,
|
|
196
|
-
className: "textarea"
|
|
197
|
-
}
|
|
198
|
-
),
|
|
199
|
-
/* @__PURE__ */ jsx(
|
|
200
|
-
"button",
|
|
201
|
-
{
|
|
202
|
-
type: "submit",
|
|
203
|
-
disabled: isLoading || !message.trim(),
|
|
204
|
-
className: "send-button",
|
|
205
|
-
children: isLoading ? /* @__PURE__ */ jsx(LoadingSpinner, {}) : /* @__PURE__ */ jsx("svg", { width: "20", height: "20", viewBox: "0 0 24 24", fill: "currentColor", children: /* @__PURE__ */ jsx("path", { d: "M2.01 21L23 12 2.01 3 2 10l15 2-15 2z" }) })
|
|
206
|
-
}
|
|
207
|
-
)
|
|
208
|
-
] });
|
|
209
|
-
};
|
|
210
|
-
var GenericUIRenderer = ({ uiData, onInteraction }) => {
|
|
211
|
-
const containerRef = useRef(null);
|
|
212
|
-
if (!uiData || !uiData.components) return null;
|
|
213
|
-
const collectFormValues = () => {
|
|
214
|
-
if (!containerRef.current) return {};
|
|
215
|
-
const formData = {};
|
|
216
|
-
const inputs = containerRef.current.querySelectorAll("input, textarea, select");
|
|
217
|
-
inputs.forEach((element) => {
|
|
218
|
-
const name = element.getAttribute("name");
|
|
219
|
-
if (name) {
|
|
220
|
-
if (element instanceof HTMLInputElement && element.type === "checkbox") {
|
|
221
|
-
formData[name] = element.checked;
|
|
222
|
-
} else if (element instanceof HTMLInputElement && element.type === "radio") {
|
|
223
|
-
if (element.checked) {
|
|
224
|
-
formData[name] = element.value;
|
|
225
|
-
}
|
|
226
|
-
} else {
|
|
227
|
-
formData[name] = element.value;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
return formData;
|
|
232
|
-
};
|
|
233
|
-
const renderComponent = (comp) => {
|
|
234
|
-
switch (comp.type) {
|
|
235
|
-
case "text":
|
|
236
|
-
return /* @__PURE__ */ jsx("p", { className: "gen-ui-text", children: comp.value ?? comp.label }, comp.id);
|
|
237
|
-
case "input":
|
|
238
|
-
return /* @__PURE__ */ jsxs("div", { className: "gen-ui-input-wrapper", children: [
|
|
239
|
-
comp.label && /* @__PURE__ */ jsx("label", { className: "gen-ui-input-label", children: comp.label }),
|
|
240
|
-
comp.fieldType === "textarea" ? /* @__PURE__ */ jsx(
|
|
241
|
-
"textarea",
|
|
242
|
-
{
|
|
243
|
-
name: comp.id,
|
|
244
|
-
placeholder: comp.placeholder,
|
|
245
|
-
className: "gen-ui-textarea"
|
|
246
|
-
}
|
|
247
|
-
) : /* @__PURE__ */ jsx(
|
|
248
|
-
"input",
|
|
249
|
-
{
|
|
250
|
-
name: comp.id,
|
|
251
|
-
type: comp.fieldType || "text",
|
|
252
|
-
placeholder: comp.placeholder,
|
|
253
|
-
className: "gen-ui-input"
|
|
254
|
-
}
|
|
255
|
-
)
|
|
256
|
-
] }, comp.id);
|
|
257
|
-
case "select":
|
|
258
|
-
return /* @__PURE__ */ jsxs("div", { className: "gen-ui-select-wrapper", children: [
|
|
259
|
-
comp.label && /* @__PURE__ */ jsx("label", { className: "gen-ui-select-label", children: comp.label }),
|
|
260
|
-
/* @__PURE__ */ jsx("select", { name: comp.id, className: "gen-ui-select", children: comp.options?.map((opt) => /* @__PURE__ */ jsx("option", { value: opt, children: opt }, opt)) })
|
|
261
|
-
] }, comp.id);
|
|
262
|
-
case "button":
|
|
263
|
-
return /* @__PURE__ */ jsx(
|
|
264
|
-
"button",
|
|
265
|
-
{
|
|
266
|
-
onClick: () => {
|
|
267
|
-
const formValues = collectFormValues();
|
|
268
|
-
onInteraction({
|
|
269
|
-
action: comp.label,
|
|
270
|
-
buttonId: comp.buttonType,
|
|
271
|
-
formData: formValues
|
|
272
|
-
});
|
|
273
|
-
},
|
|
274
|
-
className: "gen-ui-button",
|
|
275
|
-
children: comp.label
|
|
276
|
-
},
|
|
277
|
-
comp.id
|
|
278
|
-
);
|
|
279
|
-
case "card":
|
|
280
|
-
return /* @__PURE__ */ jsxs("div", { className: "gen-ui-card", children: [
|
|
281
|
-
comp.label && /* @__PURE__ */ jsx("h2", { className: "gen-ui-card-title", children: comp.label }),
|
|
282
|
-
comp.value && /* @__PURE__ */ jsx("p", { className: "gen-ui-card-content", children: comp.value })
|
|
283
|
-
] }, comp.id);
|
|
284
|
-
case "list":
|
|
285
|
-
return /* @__PURE__ */ jsx("ul", { className: "gen-ui-list", children: comp.items?.map((item) => /* @__PURE__ */ jsx("li", { className: "gen-ui-list-item", children: item.label ?? String(item.value ?? "") }, item.id)) }, comp.id);
|
|
286
|
-
case "image":
|
|
287
|
-
return /* @__PURE__ */ jsx(
|
|
288
|
-
"img",
|
|
289
|
-
{
|
|
290
|
-
src: comp.url,
|
|
291
|
-
alt: comp.label || "",
|
|
292
|
-
className: "gen-ui-image"
|
|
293
|
-
},
|
|
294
|
-
comp.id
|
|
295
|
-
);
|
|
296
|
-
case "link":
|
|
297
|
-
return /* @__PURE__ */ jsx(
|
|
298
|
-
"a",
|
|
299
|
-
{
|
|
300
|
-
href: comp.url,
|
|
301
|
-
className: "gen-ui-link",
|
|
302
|
-
children: comp.label || comp.url
|
|
303
|
-
},
|
|
304
|
-
comp.id
|
|
305
|
-
);
|
|
306
|
-
case "table":
|
|
307
|
-
return /* @__PURE__ */ jsx("div", { className: "gen-ui-table-wrapper", children: /* @__PURE__ */ jsxs("table", { className: "gen-ui-table", children: [
|
|
308
|
-
comp.columns && /* @__PURE__ */ jsx("thead", { className: "gen-ui-table-header", children: /* @__PURE__ */ jsx("tr", { children: comp.columns.map((col) => /* @__PURE__ */ jsx("th", { className: "gen-ui-table-th", children: col }, col)) }) }),
|
|
309
|
-
comp.rows && /* @__PURE__ */ jsx("tbody", { className: "gen-ui-table-body", children: comp.rows.map((row, ridx) => /* @__PURE__ */ jsx("tr", { children: row.map((cell, cidx) => /* @__PURE__ */ jsx("td", { className: "gen-ui-table-td", children: cell }, cidx)) }, ridx)) })
|
|
310
|
-
] }) }, comp.id);
|
|
311
|
-
case "form":
|
|
312
|
-
return /* @__PURE__ */ jsx("form", { className: "gen-ui-form", children: comp.items?.map((field) => renderComponent(field)) }, comp.id);
|
|
313
|
-
default:
|
|
314
|
-
return null;
|
|
315
|
-
}
|
|
316
|
-
};
|
|
317
|
-
return /* @__PURE__ */ jsxs("div", { className: "generative-ui-container", ref: containerRef, children: [
|
|
318
|
-
(uiData.title || uiData.description) && /* @__PURE__ */ jsxs("div", { className: "generative-ui-header", children: [
|
|
319
|
-
uiData.title && /* @__PURE__ */ jsx("h1", { className: "generative-ui-title", children: uiData.title }),
|
|
320
|
-
uiData.description && /* @__PURE__ */ jsx("p", { className: "generative-ui-description", children: uiData.description })
|
|
321
|
-
] }),
|
|
322
|
-
uiData.components.map((comp) => renderComponent(comp))
|
|
323
|
-
] });
|
|
324
|
-
};
|
|
325
|
-
var MessageBubble = ({ message, onAction }) => {
|
|
326
|
-
console.log("MESSAGE", message);
|
|
327
|
-
const isUser = message.role === "user";
|
|
328
|
-
const approval = message.role === "approval";
|
|
329
|
-
if (approval) {
|
|
330
|
-
return /* @__PURE__ */ jsx(Fragment, {});
|
|
331
|
-
}
|
|
332
|
-
return /* @__PURE__ */ jsxs("div", { className: `message-container ${isUser ? "user" : "assistant"}`, children: [
|
|
333
|
-
/* @__PURE__ */ jsx("div", { className: `message-bubble ${isUser ? "user" : "assistant"}`, children: isUser ? message.text && /* @__PURE__ */ jsx("div", { className: "markdown-content", children: message.text }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
334
|
-
message.text && /* @__PURE__ */ jsx("div", { className: "markdown-content", children: /* @__PURE__ */ jsx(ReactMarkdown, { remarkPlugins: [remarkGfm], children: message.text }) }),
|
|
335
|
-
message.ui && /* @__PURE__ */ jsx(
|
|
336
|
-
GenericUIRenderer,
|
|
337
|
-
{
|
|
338
|
-
uiData: message.ui,
|
|
339
|
-
onInteraction: (event) => {
|
|
340
|
-
console.log("event", event);
|
|
341
|
-
if (event.buttonId === "submit") {
|
|
342
|
-
onAction(JSON.stringify(event.formData), true);
|
|
343
|
-
} else {
|
|
344
|
-
onAction(event.action, true);
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
)
|
|
349
|
-
] }) }),
|
|
350
|
-
/* @__PURE__ */ jsx("div", { className: `message-time ${isUser ? "user" : "assistant"}`, children: message.timestamp.toLocaleTimeString("tr-TR", {
|
|
351
|
-
hour: "2-digit",
|
|
352
|
-
minute: "2-digit"
|
|
353
|
-
}) })
|
|
354
|
-
] });
|
|
355
|
-
};
|
|
356
|
-
var TypingDots = () => {
|
|
357
|
-
const [dots, setDots] = useState("");
|
|
358
|
-
useEffect(() => {
|
|
359
|
-
const interval = setInterval(() => {
|
|
360
|
-
setDots((prev) => {
|
|
361
|
-
if (prev === "...") return "";
|
|
362
|
-
return prev + ".";
|
|
363
|
-
});
|
|
364
|
-
}, 500);
|
|
365
|
-
return () => clearInterval(interval);
|
|
366
|
-
}, []);
|
|
367
|
-
return /* @__PURE__ */ jsx("div", { className: "message-container assistant", children: /* @__PURE__ */ jsx("div", { className: "message-bubble assistant", children: /* @__PURE__ */ jsx("div", { className: "message-typing-indicator", children: /* @__PURE__ */ jsx("span", { children: dots }) }) }) });
|
|
368
|
-
};
|
|
369
|
-
|
|
370
|
-
// src/utils/chatbot.ts
|
|
371
|
-
var extractUIJsonFromText = (text) => {
|
|
372
|
-
const regex = /```ui-component([\s\S]*?)```/g;
|
|
373
|
-
let cleaned = text;
|
|
374
|
-
let match;
|
|
375
|
-
let uiData = null;
|
|
376
|
-
while ((match = regex.exec(text)) !== null) {
|
|
377
|
-
const block = match[1].trim();
|
|
378
|
-
try {
|
|
379
|
-
const parsed = JSON.parse(block);
|
|
380
|
-
if (parsed && parsed.components && Array.isArray(parsed.components)) {
|
|
381
|
-
parsed.components = parsed.components.map((comp, index) => {
|
|
382
|
-
if (!comp.id || typeof comp.id !== "string" || !comp.id.trim()) {
|
|
383
|
-
comp.id = `ui-comp-${index}-${Date.now()}`;
|
|
384
|
-
}
|
|
385
|
-
if (comp.type === "button") {
|
|
386
|
-
if (!comp.buttonType) {
|
|
387
|
-
comp.buttonType = "click";
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
if (comp.type === "input") {
|
|
391
|
-
if (!comp.fieldType) {
|
|
392
|
-
comp.fieldType = "text";
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
if (comp.type === "select") {
|
|
396
|
-
if (comp.options && Array.isArray(comp.options)) {
|
|
397
|
-
const firstOption = comp.options[0];
|
|
398
|
-
if (typeof firstOption === "object" && firstOption !== null) {
|
|
399
|
-
if ("label" in firstOption || "value" in firstOption) {
|
|
400
|
-
comp.options = comp.options.map(
|
|
401
|
-
(opt) => opt.label || opt.value || String(opt)
|
|
402
|
-
);
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
if (comp.type === "table") {
|
|
408
|
-
if (comp.columns && Array.isArray(comp.columns)) {
|
|
409
|
-
const firstCol = comp.columns[0];
|
|
410
|
-
if (typeof firstCol === "object" && firstCol !== null) {
|
|
411
|
-
comp.columns = comp.columns.map(
|
|
412
|
-
(col) => col.label || col.id || String(col)
|
|
413
|
-
);
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
if (comp.rows && Array.isArray(comp.rows)) {
|
|
417
|
-
const firstRow = comp.rows[0];
|
|
418
|
-
if (typeof firstRow === "object" && !Array.isArray(firstRow)) {
|
|
419
|
-
comp.rows = comp.rows.map((row) => {
|
|
420
|
-
if (Array.isArray(row)) return row;
|
|
421
|
-
return Object.values(row).map((val) => {
|
|
422
|
-
if (typeof val === "object" && val !== null) {
|
|
423
|
-
if (val.type === "image" && val.src) return val.src;
|
|
424
|
-
if (val.value !== void 0) return String(val.value);
|
|
425
|
-
}
|
|
426
|
-
return String(val || "");
|
|
427
|
-
});
|
|
428
|
-
});
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
if (comp.type === "form" && comp.items && Array.isArray(comp.items)) {
|
|
433
|
-
comp.items = comp.items.map((field, fieldIndex) => {
|
|
434
|
-
if (!field.id || !field.id.trim()) {
|
|
435
|
-
field.id = `field-${fieldIndex}-${Date.now()}`;
|
|
436
|
-
}
|
|
437
|
-
if (field.optional !== void 0) {
|
|
438
|
-
field.requiredField = !field.optional;
|
|
439
|
-
delete field.optional;
|
|
440
|
-
}
|
|
441
|
-
if (field.required !== void 0) {
|
|
442
|
-
field.requiredField = field.required;
|
|
443
|
-
delete field.required;
|
|
444
|
-
}
|
|
445
|
-
if (field.type === "input" && !field.fieldType) {
|
|
446
|
-
field.fieldType = "text";
|
|
447
|
-
}
|
|
448
|
-
if (field.type === "select" && field.options && Array.isArray(field.options)) {
|
|
449
|
-
const firstOpt = field.options[0];
|
|
450
|
-
if (typeof firstOpt === "object" && firstOpt !== null) {
|
|
451
|
-
field.options = field.options.map(
|
|
452
|
-
(opt) => opt.label || opt.value || String(opt)
|
|
453
|
-
);
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
return field;
|
|
457
|
-
});
|
|
458
|
-
}
|
|
459
|
-
if (comp.type === "list" && comp.items && Array.isArray(comp.items)) {
|
|
460
|
-
comp.items = comp.items.map((item, itemIndex) => {
|
|
461
|
-
if (typeof item === "string") {
|
|
462
|
-
return {
|
|
463
|
-
id: `list-item-${itemIndex}-${Date.now()}`,
|
|
464
|
-
type: "text",
|
|
465
|
-
value: item,
|
|
466
|
-
label: item
|
|
467
|
-
};
|
|
468
|
-
}
|
|
469
|
-
if (!item.id || !item.id.trim()) {
|
|
470
|
-
item.id = `list-item-${itemIndex}-${Date.now()}`;
|
|
471
|
-
}
|
|
472
|
-
return item;
|
|
473
|
-
});
|
|
474
|
-
}
|
|
475
|
-
return comp;
|
|
476
|
-
});
|
|
477
|
-
uiData = parsed;
|
|
478
|
-
}
|
|
479
|
-
cleaned = cleaned.replace(match[0], "").trim();
|
|
480
|
-
break;
|
|
481
|
-
} catch (e) {
|
|
482
|
-
console.error("Invalid ui-component JSON:", e, block);
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
return {
|
|
486
|
-
cleanedText: cleaned,
|
|
487
|
-
uiData
|
|
488
|
-
};
|
|
489
|
-
};
|
|
490
|
-
var AizekChatBot = ({
|
|
491
|
-
clientId,
|
|
492
|
-
headers,
|
|
493
|
-
onMounted,
|
|
494
|
-
onReady,
|
|
495
|
-
onOpen,
|
|
496
|
-
onClose,
|
|
497
|
-
onMessage,
|
|
498
|
-
onToolCall,
|
|
499
|
-
onDisconnect
|
|
500
|
-
}) => {
|
|
501
|
-
const messagesEndRef = useRef(null);
|
|
502
|
-
const [config, setConfig] = useState();
|
|
503
|
-
const [messages, setMessages] = useState([]);
|
|
504
|
-
const [isLoading, setIsLoading] = useState(false);
|
|
505
|
-
const [isConfigLoading, setIsConfigLoading] = useState(true);
|
|
506
|
-
const [isOpen, setIsOpen] = useState(false);
|
|
507
|
-
const [headerValidation, setHeaderValidation] = useState(null);
|
|
508
|
-
const [showAlert, setShowAlert] = useState(true);
|
|
509
|
-
const [sessions, setSessions] = useState([]);
|
|
510
|
-
const [activeSessionId, setActiveSessionIdState] = useState(null);
|
|
511
|
-
const [activeTab, setActiveTab] = useState("home");
|
|
512
|
-
const [messageView, setMessageView] = useState("list");
|
|
513
|
-
const PROXY_BASE_URL = "https://proxy.aizek.ai/api";
|
|
514
|
-
const createNewSession = async () => {
|
|
515
|
-
const deviceId = getOrCreateDeviceId();
|
|
516
|
-
if (sessions.length >= MAX_SESSIONS) {
|
|
517
|
-
throw new Error(`You can open up to ${MAX_SESSIONS} sessions.`);
|
|
518
|
-
}
|
|
519
|
-
const res = await fetch(`${PROXY_BASE_URL}/aizek-sessions/new?clientId=${clientId}`, {
|
|
520
|
-
method: "POST",
|
|
521
|
-
headers: {
|
|
522
|
-
"Content-Type": "application/json",
|
|
523
|
-
"x-device-id": deviceId,
|
|
524
|
-
"x-alternate": JSON.stringify(headers)
|
|
525
|
-
}
|
|
526
|
-
});
|
|
527
|
-
const data = await res.json();
|
|
528
|
-
if (!data.success) throw new Error(data.message || "session create failed");
|
|
529
|
-
const sid = data.data.sessionId;
|
|
530
|
-
const updatedSessions = data.data.sessions ?? [sid];
|
|
531
|
-
upsertSessionsFromServer(updatedSessions, setSessions);
|
|
532
|
-
setActiveSessionIdState(sid);
|
|
533
|
-
setActiveSessionId(sid);
|
|
534
|
-
setActiveTab("messages");
|
|
535
|
-
setMessageView("detail");
|
|
536
|
-
touchSession(sid);
|
|
537
|
-
setMessages([]);
|
|
538
|
-
return sid;
|
|
539
|
-
};
|
|
540
|
-
const loadConfig = async () => {
|
|
541
|
-
try {
|
|
542
|
-
setIsConfigLoading(true);
|
|
543
|
-
const deviceId = getOrCreateDeviceId();
|
|
544
|
-
purgeExpiredLocalSessions(setActiveSessionIdState);
|
|
545
|
-
const localActive = getActiveSessionId();
|
|
546
|
-
const response = await fetch(`${PROXY_BASE_URL}/aizek-connect?clientId=${clientId}`, {
|
|
547
|
-
method: "POST",
|
|
548
|
-
headers: {
|
|
549
|
-
"Content-Type": "application/json",
|
|
550
|
-
"x-device-id": deviceId,
|
|
551
|
-
"x-alternate": JSON.stringify(headers)
|
|
552
|
-
}
|
|
553
|
-
});
|
|
554
|
-
const data = await response.json();
|
|
555
|
-
if (!data.success) {
|
|
556
|
-
setIsOpen(false);
|
|
557
|
-
return;
|
|
558
|
-
}
|
|
559
|
-
setIsOpen(!!data.data.widget_config.initial_open);
|
|
560
|
-
setConfig(data.data);
|
|
561
|
-
const serverSessions = data.data.sessions ?? [];
|
|
562
|
-
const merged = upsertSessionsFromServer(serverSessions, setSessions);
|
|
563
|
-
if (merged.length === 0) {
|
|
564
|
-
const newSid = await createNewSession();
|
|
565
|
-
setActiveSessionIdState(newSid);
|
|
566
|
-
setActiveSessionId(newSid);
|
|
567
|
-
return;
|
|
568
|
-
}
|
|
569
|
-
const mergedIds = merged.map((s) => s.sessionId);
|
|
570
|
-
const nextActive = (localActive && mergedIds.includes(localActive) ? localActive : null) ?? mergedIds[0] ?? null;
|
|
571
|
-
setActiveSessionIdState(nextActive);
|
|
572
|
-
setActiveSessionId(nextActive);
|
|
573
|
-
if (headers) {
|
|
574
|
-
const validationResult = validateHeaders(headers, data.data.auth_config, {
|
|
575
|
-
allowExtra: false,
|
|
576
|
-
caseSensitive: true
|
|
577
|
-
});
|
|
578
|
-
setHeaderValidation(validationResult);
|
|
579
|
-
}
|
|
580
|
-
onReady?.({ config: { ...data.data } });
|
|
581
|
-
} catch (error) {
|
|
582
|
-
console.error("Failed to load chat widget config:", error);
|
|
583
|
-
} finally {
|
|
584
|
-
setIsConfigLoading(false);
|
|
585
|
-
}
|
|
586
|
-
};
|
|
587
|
-
const getHistoryMessageBySessionId = async (sid) => {
|
|
588
|
-
try {
|
|
589
|
-
const deviceId = getOrCreateDeviceId();
|
|
590
|
-
const response = await fetch(`${PROXY_BASE_URL}/aizek-messages`, {
|
|
591
|
-
method: "GET",
|
|
592
|
-
headers: {
|
|
593
|
-
"Content-Type": "application/json",
|
|
594
|
-
"x-device-id": deviceId,
|
|
595
|
-
"x-session-id": sid,
|
|
596
|
-
"x-alternate": JSON.stringify(headers)
|
|
597
|
-
}
|
|
598
|
-
});
|
|
599
|
-
const data = await response.json();
|
|
600
|
-
if (!data.success) {
|
|
601
|
-
throw new Error(data.message || "Failed to fetch messages");
|
|
602
|
-
}
|
|
603
|
-
const historyMessages = [];
|
|
604
|
-
if (data.data?.messages && Array.isArray(data.data.messages)) {
|
|
605
|
-
for (const msg of data.data.messages) {
|
|
606
|
-
let textContent = "";
|
|
607
|
-
let uiData = null;
|
|
608
|
-
if (typeof msg.content === "string") {
|
|
609
|
-
textContent = msg.content;
|
|
610
|
-
} else if (Array.isArray(msg.content)) {
|
|
611
|
-
const textParts = [];
|
|
612
|
-
for (const item of msg.content) {
|
|
613
|
-
if (item.type === "text" && item.text) {
|
|
614
|
-
textParts.push(item.text);
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
textContent = textParts.join("\n");
|
|
618
|
-
}
|
|
619
|
-
if (textContent) {
|
|
620
|
-
const extracted = extractUIJsonFromText(textContent);
|
|
621
|
-
textContent = extracted.cleanedText;
|
|
622
|
-
uiData = extracted.uiData;
|
|
623
|
-
}
|
|
624
|
-
if (textContent.trim() || uiData) {
|
|
625
|
-
historyMessages.push({
|
|
626
|
-
text: textContent.trim() || void 0,
|
|
627
|
-
ui: uiData,
|
|
628
|
-
role: msg.role === "user" ? "user" : msg.role === "assistant" ? "assistant" : "user",
|
|
629
|
-
timestamp: /* @__PURE__ */ new Date()
|
|
630
|
-
});
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
return historyMessages;
|
|
635
|
-
} catch (error) {
|
|
636
|
-
console.error("Error fetching message history:", error);
|
|
637
|
-
return [];
|
|
638
|
-
}
|
|
639
|
-
};
|
|
640
|
-
useEffect(() => {
|
|
641
|
-
onMounted?.();
|
|
642
|
-
loadConfig();
|
|
643
|
-
}, []);
|
|
644
|
-
useEffect(() => {
|
|
645
|
-
const t = setInterval(() => {
|
|
646
|
-
purgeExpiredLocalSessions(setActiveSessionIdState);
|
|
647
|
-
}, 1e3);
|
|
648
|
-
return () => clearInterval(t);
|
|
649
|
-
}, []);
|
|
650
|
-
useEffect(() => {
|
|
651
|
-
if (typeof config?.widget_config.initial_open === "boolean") {
|
|
652
|
-
const open = config.widget_config.initial_open;
|
|
653
|
-
setIsOpen(open);
|
|
654
|
-
if (open) onOpen?.();
|
|
655
|
-
else onClose?.();
|
|
656
|
-
}
|
|
657
|
-
}, [config?.widget_config.initial_open]);
|
|
658
|
-
useEffect(() => {
|
|
659
|
-
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
|
|
660
|
-
}, [messages]);
|
|
661
|
-
useEffect(() => {
|
|
662
|
-
if (activeSessionId && !isConfigLoading) {
|
|
663
|
-
getHistoryMessageBySessionId(activeSessionId).then((historyMessages) => {
|
|
664
|
-
setMessages(historyMessages);
|
|
665
|
-
}).catch((error) => {
|
|
666
|
-
console.error("Failed to load message history:", error);
|
|
667
|
-
});
|
|
668
|
-
} else if (!activeSessionId) {
|
|
669
|
-
setMessages([]);
|
|
670
|
-
}
|
|
671
|
-
}, [activeSessionId, isConfigLoading]);
|
|
672
|
-
const addMessage = (payload) => {
|
|
673
|
-
const newMessage = {
|
|
674
|
-
text: payload.text,
|
|
675
|
-
ui: payload.ui,
|
|
676
|
-
role: payload.role,
|
|
677
|
-
timestamp: /* @__PURE__ */ new Date()
|
|
678
|
-
};
|
|
679
|
-
onMessage?.(newMessage);
|
|
680
|
-
setMessages((prev) => [...prev, newMessage]);
|
|
681
|
-
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
|
|
682
|
-
return newMessage;
|
|
683
|
-
};
|
|
684
|
-
const sendMessage = async (message, approval) => {
|
|
685
|
-
if (!message.trim() || isLoading) return;
|
|
686
|
-
setMessages((prev) => [
|
|
687
|
-
...prev,
|
|
688
|
-
{ text: message, ui: void 0, role: approval ? "approval" : "user", timestamp: /* @__PURE__ */ new Date() }
|
|
689
|
-
]);
|
|
690
|
-
setIsLoading(true);
|
|
691
|
-
try {
|
|
692
|
-
const deviceId = getOrCreateDeviceId();
|
|
693
|
-
purgeExpiredLocalSessions(setActiveSessionIdState);
|
|
694
|
-
let sid = activeSessionId;
|
|
695
|
-
if (!sid) sid = await createNewSession();
|
|
696
|
-
const response = await fetch(`${PROXY_BASE_URL}/aizek-chat?clientId=${clientId}`, {
|
|
697
|
-
method: "POST",
|
|
698
|
-
headers: {
|
|
699
|
-
"Content-Type": "application/json",
|
|
700
|
-
"x-device-id": deviceId,
|
|
701
|
-
"x-session-id": sid,
|
|
702
|
-
"x-alternate": JSON.stringify(headers)
|
|
703
|
-
},
|
|
704
|
-
body: JSON.stringify({ message })
|
|
705
|
-
});
|
|
706
|
-
if (response.status === 401) {
|
|
707
|
-
const local = readStoredSessions().filter((s) => s.sessionId !== sid);
|
|
708
|
-
writeStoredSessions(local);
|
|
709
|
-
setSessions(local.map((x) => x.sessionId));
|
|
710
|
-
setActiveSessionId(null);
|
|
711
|
-
setActiveSessionIdState(null);
|
|
712
|
-
const newSid = await createNewSession();
|
|
713
|
-
const retry = await fetch(`${PROXY_BASE_URL}/aizek-chat?clientId=${clientId}`, {
|
|
714
|
-
method: "POST",
|
|
715
|
-
headers: {
|
|
716
|
-
"Content-Type": "application/json",
|
|
717
|
-
"x-device-id": deviceId,
|
|
718
|
-
"x-session-id": newSid,
|
|
719
|
-
"x-alternate": JSON.stringify(headers)
|
|
720
|
-
},
|
|
721
|
-
body: JSON.stringify({ message })
|
|
722
|
-
});
|
|
723
|
-
if (!retry.ok) throw new Error(`HTTP error ${retry.status}`);
|
|
724
|
-
const retryJson = await retry.json();
|
|
725
|
-
const text = JSON.stringify(retryJson.data);
|
|
726
|
-
const { cleanedText, uiData } = extractUIJsonFromText(text);
|
|
727
|
-
addMessage({ text: cleanedText, ui: uiData, role: "assistant" });
|
|
728
|
-
touchSession(newSid);
|
|
729
|
-
return;
|
|
730
|
-
}
|
|
731
|
-
if (!response.ok) {
|
|
732
|
-
throw new Error(`HTTP error ${response.status}`);
|
|
733
|
-
}
|
|
734
|
-
if (!response.body) {
|
|
735
|
-
throw new Error("Streaming desteklenmiyor (response.body yok)");
|
|
736
|
-
}
|
|
737
|
-
const reader = response.body.getReader();
|
|
738
|
-
const decoder = new TextDecoder();
|
|
739
|
-
let buffer = "";
|
|
740
|
-
while (true) {
|
|
741
|
-
const { value, done } = await reader.read();
|
|
742
|
-
if (done) break;
|
|
743
|
-
buffer += decoder.decode(value, { stream: true });
|
|
744
|
-
const chunks = buffer.split("\n\n");
|
|
745
|
-
buffer = chunks.pop() ?? "";
|
|
746
|
-
for (const rawChunk of chunks) {
|
|
747
|
-
const line = rawChunk.split("\n").find((l) => l.startsWith("data:"));
|
|
748
|
-
if (!line) continue;
|
|
749
|
-
const jsonStr = line.replace(/^data:\s*/, "").trim();
|
|
750
|
-
if (!jsonStr) continue;
|
|
751
|
-
const event = JSON.parse(jsonStr);
|
|
752
|
-
if (event.type === "assistant_text" && event.content) {
|
|
753
|
-
const { cleanedText, uiData } = extractUIJsonFromText(event.content);
|
|
754
|
-
addMessage({
|
|
755
|
-
text: cleanedText,
|
|
756
|
-
ui: uiData,
|
|
757
|
-
role: "assistant"
|
|
758
|
-
});
|
|
759
|
-
}
|
|
760
|
-
if (event.type === "assistant_tool_result" && event.content) {
|
|
761
|
-
const toolInfoParsed = JSON.parse(event.content);
|
|
762
|
-
onToolCall?.(toolInfoParsed);
|
|
763
|
-
}
|
|
764
|
-
if (event.type === "error" && event.content) {
|
|
765
|
-
const { cleanedText, uiData } = extractUIJsonFromText(event.content);
|
|
766
|
-
addMessage({
|
|
767
|
-
text: cleanedText,
|
|
768
|
-
ui: uiData,
|
|
769
|
-
role: "assistant"
|
|
770
|
-
});
|
|
771
|
-
}
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
touchSession(sid);
|
|
775
|
-
} catch (error) {
|
|
776
|
-
console.error("Error sending message:", error);
|
|
777
|
-
addMessage({ text: "Sorry, something went wrong. Please try again.", role: "assistant" });
|
|
778
|
-
} finally {
|
|
779
|
-
setIsLoading(false);
|
|
780
|
-
}
|
|
781
|
-
};
|
|
782
|
-
const disconnectActiveSession = async () => {
|
|
783
|
-
try {
|
|
784
|
-
const deviceId = getOrCreateDeviceId();
|
|
785
|
-
const sid = activeSessionId;
|
|
786
|
-
if (!sid) return;
|
|
787
|
-
await fetch(`${PROXY_BASE_URL}/aizek-disconnect?clientId=${clientId}`, {
|
|
788
|
-
method: "POST",
|
|
789
|
-
headers: {
|
|
790
|
-
"Content-Type": "application/json",
|
|
791
|
-
"x-device-id": deviceId,
|
|
792
|
-
"x-session-id": sid,
|
|
793
|
-
"x-alternate": JSON.stringify(headers)
|
|
794
|
-
},
|
|
795
|
-
body: JSON.stringify({ sessionId: sid })
|
|
796
|
-
});
|
|
797
|
-
const nextStored = readStoredSessions().filter((s) => s.sessionId !== sid);
|
|
798
|
-
writeStoredSessions(nextStored);
|
|
799
|
-
const nextSessions = nextStored.map((x) => x.sessionId);
|
|
800
|
-
setSessions(nextSessions);
|
|
801
|
-
const nextActive = nextSessions[0] ?? null;
|
|
802
|
-
setActiveSessionIdState(nextActive);
|
|
803
|
-
setActiveSessionId(nextActive);
|
|
804
|
-
setMessages([]);
|
|
805
|
-
onDisconnect?.();
|
|
806
|
-
} catch (e) {
|
|
807
|
-
console.error(e);
|
|
808
|
-
}
|
|
809
|
-
};
|
|
810
|
-
const handleSelectSession = (sid) => {
|
|
811
|
-
setActiveSessionIdState(sid);
|
|
812
|
-
setActiveSessionId(sid);
|
|
813
|
-
setActiveTab("messages");
|
|
814
|
-
setMessageView("detail");
|
|
815
|
-
};
|
|
816
|
-
const getSessionLabel = (sid) => {
|
|
817
|
-
const idx = sessions.indexOf(sid);
|
|
818
|
-
return idx >= 0 ? `Chat ${idx + 1}` : "Chat";
|
|
819
|
-
};
|
|
820
|
-
const toggleChat = () => {
|
|
821
|
-
const newIsOpen = !isOpen;
|
|
822
|
-
setIsOpen(newIsOpen);
|
|
823
|
-
if (newIsOpen) onOpen?.();
|
|
824
|
-
else onClose?.();
|
|
825
|
-
};
|
|
826
|
-
const clean = sanitizeHtml(config?.widget_config.welcome_message ?? "", {
|
|
827
|
-
allowedTags: ["b", "i", "em", "strong", "a", "p", "br"],
|
|
828
|
-
allowedAttributes: {
|
|
829
|
-
a: ["href", "target", "rel"]
|
|
830
|
-
}
|
|
831
|
-
});
|
|
832
|
-
return /* @__PURE__ */ jsx(Fragment, { children: isConfigLoading ? /* @__PURE__ */ jsx(
|
|
833
|
-
"button",
|
|
834
|
-
{
|
|
835
|
-
className: "floating-button bottom-right button-sizes medium loading-state",
|
|
836
|
-
style: { background: "#4f46e5" },
|
|
837
|
-
"aria-label": "Loading",
|
|
838
|
-
children: /* @__PURE__ */ jsx("div", { className: "loading-spinner" })
|
|
839
|
-
}
|
|
840
|
-
) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
841
|
-
isOpen && /* @__PURE__ */ jsx("div", { className: `overlay floating-chat-overlay ${isOpen ? "is-open" : ""}`, onClick: toggleChat }),
|
|
842
|
-
/* @__PURE__ */ jsx(
|
|
843
|
-
"div",
|
|
844
|
-
{
|
|
845
|
-
className: `chat-container ${config?.widget_config.button_position} ${isOpen ? "is-open" : ""}`,
|
|
846
|
-
style: { width: config?.widget_config.chat_width, height: config?.widget_config.chat_height },
|
|
847
|
-
children: /* @__PURE__ */ jsxs("div", { className: "chatbot-container", children: [
|
|
848
|
-
/* @__PURE__ */ jsxs("div", { className: "header", style: { background: config?.widget_config.header_background }, children: [
|
|
849
|
-
/* @__PURE__ */ jsx("div", { className: "logo-container", children: config?.widget_config.company_logo ? config?.widget_config.company_logo.startsWith("http") || config?.widget_config.company_logo.startsWith("data:") ? /* @__PURE__ */ jsx("img", { src: config?.widget_config.company_logo, alt: "Company Logo", className: "logo-image" }) : /* @__PURE__ */ jsx("span", { className: "logo-text", children: config?.widget_config.company_logo }) : "\u{1F916}" }),
|
|
850
|
-
/* @__PURE__ */ jsxs("div", { children: [
|
|
851
|
-
/* @__PURE__ */ jsx("h3", { className: "company-name", children: config?.widget_config.company_name }),
|
|
852
|
-
/* @__PURE__ */ jsx("p", { className: "status-text", children: isLoading ? "Typing..." : "Online" })
|
|
853
|
-
] })
|
|
854
|
-
] }),
|
|
855
|
-
/* @__PURE__ */ jsxs("div", { className: "chat-content", children: [
|
|
856
|
-
activeTab === "home" && /* @__PURE__ */ jsxs("div", { className: "home-panel", children: [
|
|
857
|
-
/* @__PURE__ */ jsx("p", { className: "eyebrow", children: "Welcome" }),
|
|
858
|
-
/* @__PURE__ */ jsx("h3", { className: "panel-title", children: config?.widget_config.company_name }),
|
|
859
|
-
/* @__PURE__ */ jsx("p", { className: "panel-subtitle", children: "Ask anything. We keep your history and respond instantly." }),
|
|
860
|
-
/* @__PURE__ */ jsxs("div", { className: "home-actions", children: [
|
|
861
|
-
/* @__PURE__ */ jsx(
|
|
862
|
-
"button",
|
|
863
|
-
{
|
|
864
|
-
className: "primary-button",
|
|
865
|
-
onClick: async () => {
|
|
866
|
-
try {
|
|
867
|
-
await createNewSession();
|
|
868
|
-
} catch (e) {
|
|
869
|
-
console.error(e);
|
|
870
|
-
}
|
|
871
|
-
},
|
|
872
|
-
disabled: isLoading || sessions.length >= MAX_SESSIONS,
|
|
873
|
-
children: "Start a new conversation"
|
|
874
|
-
}
|
|
875
|
-
),
|
|
876
|
-
/* @__PURE__ */ jsx(
|
|
877
|
-
"button",
|
|
878
|
-
{
|
|
879
|
-
className: "ghost-button",
|
|
880
|
-
onClick: () => {
|
|
881
|
-
setActiveTab("messages");
|
|
882
|
-
setMessageView("list");
|
|
883
|
-
},
|
|
884
|
-
children: "View conversations"
|
|
885
|
-
}
|
|
886
|
-
)
|
|
887
|
-
] })
|
|
888
|
-
] }),
|
|
889
|
-
activeTab === "messages" && /* @__PURE__ */ jsx(Fragment, { children: messageView === "list" ? /* @__PURE__ */ jsxs("div", { className: "conversation-list", children: [
|
|
890
|
-
/* @__PURE__ */ jsxs("div", { className: "list-header", children: [
|
|
891
|
-
/* @__PURE__ */ jsxs("div", { children: [
|
|
892
|
-
/* @__PURE__ */ jsx("p", { className: "eyebrow", children: "Conversations" }),
|
|
893
|
-
/* @__PURE__ */ jsx("h4", { className: "panel-title", children: "Inbox" })
|
|
894
|
-
] }),
|
|
895
|
-
/* @__PURE__ */ jsx(
|
|
896
|
-
"button",
|
|
897
|
-
{
|
|
898
|
-
className: "session-new-button",
|
|
899
|
-
onClick: async () => {
|
|
900
|
-
try {
|
|
901
|
-
await createNewSession();
|
|
902
|
-
} catch (e) {
|
|
903
|
-
console.error(e);
|
|
904
|
-
}
|
|
905
|
-
},
|
|
906
|
-
disabled: sessions.length >= MAX_SESSIONS,
|
|
907
|
-
children: /* @__PURE__ */ jsx("span", { children: "+ New" })
|
|
908
|
-
}
|
|
909
|
-
)
|
|
910
|
-
] }),
|
|
911
|
-
sessions.length === 0 ? /* @__PURE__ */ jsxs("div", { className: "empty-list", children: [
|
|
912
|
-
/* @__PURE__ */ jsx("div", { className: "empty-state-icon", children: "\u{1F4AC}" }),
|
|
913
|
-
/* @__PURE__ */ jsx("h4", { className: "empty-state-title", children: "No conversations yet" }),
|
|
914
|
-
/* @__PURE__ */ jsx("p", { className: "empty-state-description", children: "Start a new conversation to see it appear here." })
|
|
915
|
-
] }) : /* @__PURE__ */ jsx("div", { className: "conversation-items", children: sessions.map((sid) => /* @__PURE__ */ jsxs(
|
|
916
|
-
"button",
|
|
917
|
-
{
|
|
918
|
-
className: `conversation-item ${sid === activeSessionId ? "active" : ""}`,
|
|
919
|
-
onClick: () => handleSelectSession(sid),
|
|
920
|
-
children: [
|
|
921
|
-
/* @__PURE__ */ jsxs("div", { className: "conversation-meta", children: [
|
|
922
|
-
/* @__PURE__ */ jsx("p", { className: "conversation-title", children: getSessionLabel(sid) }),
|
|
923
|
-
/* @__PURE__ */ jsxs("p", { className: "conversation-sub", children: [
|
|
924
|
-
"Session ID: ",
|
|
925
|
-
sid.slice(0, 8),
|
|
926
|
-
"..."
|
|
927
|
-
] })
|
|
928
|
-
] }),
|
|
929
|
-
/* @__PURE__ */ jsx("span", { className: "conversation-pill", children: "Open" })
|
|
930
|
-
]
|
|
931
|
-
},
|
|
932
|
-
sid
|
|
933
|
-
)) })
|
|
934
|
-
] }) : /* @__PURE__ */ jsxs("div", { className: "conversation-detail", children: [
|
|
935
|
-
/* @__PURE__ */ jsxs("div", { className: "detail-header", children: [
|
|
936
|
-
/* @__PURE__ */ jsx("div", { className: "detail-header-left", children: /* @__PURE__ */ jsx("button", { className: "icon-button", onClick: () => setMessageView("list"), "aria-label": "Back", children: /* @__PURE__ */ jsx("svg", { width: "18", height: "18", viewBox: "0 0 24 24", fill: "none", children: /* @__PURE__ */ jsx(
|
|
937
|
-
"path",
|
|
938
|
-
{
|
|
939
|
-
d: "M15 18l-6-6 6-6",
|
|
940
|
-
stroke: "currentColor",
|
|
941
|
-
strokeWidth: "2",
|
|
942
|
-
strokeLinecap: "round",
|
|
943
|
-
strokeLinejoin: "round"
|
|
944
|
-
}
|
|
945
|
-
) }) }) }),
|
|
946
|
-
/* @__PURE__ */ jsxs("div", { className: "detail-header-center", children: [
|
|
947
|
-
/* @__PURE__ */ jsx("div", { className: "detail-avatar", "aria-hidden": "true", children: /* @__PURE__ */ jsx("svg", { width: "18", height: "18", viewBox: "0 0 24 24", fill: "none", children: /* @__PURE__ */ jsx(
|
|
948
|
-
"path",
|
|
949
|
-
{
|
|
950
|
-
d: "M12 3c-4.418 0-8 3.134-8 7 0 2.382 1.362 4.486 3.5 5.737V21l4.07-2.13c.14.008.283.013.43.013 4.418 0 8-3.134 8-7s-3.582-7-8-7z",
|
|
951
|
-
stroke: "currentColor",
|
|
952
|
-
strokeWidth: "2",
|
|
953
|
-
strokeLinejoin: "round"
|
|
954
|
-
}
|
|
955
|
-
) }) }),
|
|
956
|
-
/* @__PURE__ */ jsxs("div", { className: "detail-title", children: [
|
|
957
|
-
/* @__PURE__ */ jsxs("div", { className: "detail-title-row", children: [
|
|
958
|
-
/* @__PURE__ */ jsx("strong", { className: "detail-title-text", children: activeSessionId ? getSessionLabel(activeSessionId) : "Chat" }),
|
|
959
|
-
/* @__PURE__ */ jsx("span", { className: `status-dot ${isLoading ? "typing" : "online"}`, "aria-hidden": "true" })
|
|
960
|
-
] }),
|
|
961
|
-
/* @__PURE__ */ jsx("p", { className: "detail-subtitle", children: isLoading ? "Yaz\u0131yor\u2026" : "Online" })
|
|
962
|
-
] })
|
|
963
|
-
] }),
|
|
964
|
-
/* @__PURE__ */ jsxs("div", { className: "detail-header-right", children: [
|
|
965
|
-
/* @__PURE__ */ jsx(
|
|
966
|
-
"button",
|
|
967
|
-
{
|
|
968
|
-
className: "icon-button",
|
|
969
|
-
onClick: () => {
|
|
970
|
-
setActiveTab("info");
|
|
971
|
-
setMessageView("list");
|
|
972
|
-
},
|
|
973
|
-
"aria-label": "Bilgi",
|
|
974
|
-
children: /* @__PURE__ */ jsxs("svg", { width: "18", height: "18", viewBox: "0 0 24 24", fill: "none", children: [
|
|
975
|
-
/* @__PURE__ */ jsx(
|
|
976
|
-
"path",
|
|
977
|
-
{
|
|
978
|
-
d: "M12 17v-6",
|
|
979
|
-
stroke: "currentColor",
|
|
980
|
-
strokeWidth: "2",
|
|
981
|
-
strokeLinecap: "round"
|
|
982
|
-
}
|
|
983
|
-
),
|
|
984
|
-
/* @__PURE__ */ jsx(
|
|
985
|
-
"path",
|
|
986
|
-
{
|
|
987
|
-
d: "M12 8h.01",
|
|
988
|
-
stroke: "currentColor",
|
|
989
|
-
strokeWidth: "2.5",
|
|
990
|
-
strokeLinecap: "round"
|
|
991
|
-
}
|
|
992
|
-
),
|
|
993
|
-
/* @__PURE__ */ jsx(
|
|
994
|
-
"circle",
|
|
995
|
-
{
|
|
996
|
-
cx: "12",
|
|
997
|
-
cy: "12",
|
|
998
|
-
r: "9",
|
|
999
|
-
stroke: "currentColor",
|
|
1000
|
-
strokeWidth: "2"
|
|
1001
|
-
}
|
|
1002
|
-
)
|
|
1003
|
-
] })
|
|
1004
|
-
}
|
|
1005
|
-
),
|
|
1006
|
-
/* @__PURE__ */ jsx(
|
|
1007
|
-
"button",
|
|
1008
|
-
{
|
|
1009
|
-
className: "icon-button danger",
|
|
1010
|
-
onClick: disconnectActiveSession,
|
|
1011
|
-
disabled: !activeSessionId,
|
|
1012
|
-
"aria-label": "End conversation",
|
|
1013
|
-
children: /* @__PURE__ */ jsxs("svg", { width: "18", height: "18", viewBox: "0 0 24 24", fill: "none", children: [
|
|
1014
|
-
/* @__PURE__ */ jsx(
|
|
1015
|
-
"path",
|
|
1016
|
-
{
|
|
1017
|
-
d: "M3 6h18",
|
|
1018
|
-
stroke: "currentColor",
|
|
1019
|
-
strokeWidth: "2",
|
|
1020
|
-
strokeLinecap: "round"
|
|
1021
|
-
}
|
|
1022
|
-
),
|
|
1023
|
-
/* @__PURE__ */ jsx(
|
|
1024
|
-
"path",
|
|
1025
|
-
{
|
|
1026
|
-
d: "M8 6V4h8v2",
|
|
1027
|
-
stroke: "currentColor",
|
|
1028
|
-
strokeWidth: "2",
|
|
1029
|
-
strokeLinejoin: "round"
|
|
1030
|
-
}
|
|
1031
|
-
),
|
|
1032
|
-
/* @__PURE__ */ jsx(
|
|
1033
|
-
"path",
|
|
1034
|
-
{
|
|
1035
|
-
d: "M6 6l1 16h10l1-16",
|
|
1036
|
-
stroke: "currentColor",
|
|
1037
|
-
strokeWidth: "2",
|
|
1038
|
-
strokeLinejoin: "round"
|
|
1039
|
-
}
|
|
1040
|
-
)
|
|
1041
|
-
] })
|
|
1042
|
-
}
|
|
1043
|
-
)
|
|
1044
|
-
] })
|
|
1045
|
-
] }),
|
|
1046
|
-
/* @__PURE__ */ jsxs("div", { className: "messages-container", children: [
|
|
1047
|
-
/* @__PURE__ */ jsx(HeaderAlert, { headerValidation, setShowAlert, showAlert }),
|
|
1048
|
-
messages.length === 0 ? /* @__PURE__ */ jsx("div", { className: "empty-state", children: /* @__PURE__ */ jsx("span", { dangerouslySetInnerHTML: { __html: clean } }) }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1049
|
-
messages.map((message, index) => /* @__PURE__ */ jsx(MessageBubble, { message, onAction: sendMessage }, index)),
|
|
1050
|
-
config?.widget_config.show_typing_indicator && isLoading && /* @__PURE__ */ jsx(TypingDots, {})
|
|
1051
|
-
] }),
|
|
1052
|
-
/* @__PURE__ */ jsx("div", { ref: messagesEndRef })
|
|
1053
|
-
] }),
|
|
1054
|
-
/* @__PURE__ */ jsx(
|
|
1055
|
-
ChatInput,
|
|
1056
|
-
{
|
|
1057
|
-
handleSendMessage: sendMessage,
|
|
1058
|
-
isLoading,
|
|
1059
|
-
placeholder: config?.widget_config.placeholder ?? ""
|
|
1060
|
-
}
|
|
1061
|
-
)
|
|
1062
|
-
] }) }),
|
|
1063
|
-
activeTab === "info" && /* @__PURE__ */ jsxs("div", { className: "info-panel", children: [
|
|
1064
|
-
/* @__PURE__ */ jsx("p", { className: "eyebrow", children: "Info" }),
|
|
1065
|
-
/* @__PURE__ */ jsx("h4", { className: "panel-title", children: "Widget Details" }),
|
|
1066
|
-
/* @__PURE__ */ jsxs("ul", { className: "info-list", children: [
|
|
1067
|
-
/* @__PURE__ */ jsxs("li", { children: [
|
|
1068
|
-
/* @__PURE__ */ jsx("span", { children: "Company" }),
|
|
1069
|
-
/* @__PURE__ */ jsx("strong", { children: config?.widget_config.company_name })
|
|
1070
|
-
] }),
|
|
1071
|
-
/* @__PURE__ */ jsxs("li", { children: [
|
|
1072
|
-
/* @__PURE__ */ jsx("span", { children: "Status" }),
|
|
1073
|
-
/* @__PURE__ */ jsx("strong", { children: isLoading ? "Typing..." : "Online" })
|
|
1074
|
-
] }),
|
|
1075
|
-
/* @__PURE__ */ jsxs("li", { children: [
|
|
1076
|
-
/* @__PURE__ */ jsx("span", { children: "Conversations" }),
|
|
1077
|
-
/* @__PURE__ */ jsx("strong", { children: sessions.length })
|
|
1078
|
-
] })
|
|
1079
|
-
] })
|
|
1080
|
-
] })
|
|
1081
|
-
] }),
|
|
1082
|
-
/* @__PURE__ */ jsxs("div", { className: "bottom-nav", children: [
|
|
1083
|
-
/* @__PURE__ */ jsx(
|
|
1084
|
-
"button",
|
|
1085
|
-
{
|
|
1086
|
-
className: `nav-button ${activeTab === "home" ? "active" : ""}`,
|
|
1087
|
-
onClick: () => {
|
|
1088
|
-
setActiveTab("home");
|
|
1089
|
-
setMessageView("list");
|
|
1090
|
-
},
|
|
1091
|
-
children: "Home"
|
|
1092
|
-
}
|
|
1093
|
-
),
|
|
1094
|
-
/* @__PURE__ */ jsx(
|
|
1095
|
-
"button",
|
|
1096
|
-
{
|
|
1097
|
-
className: `nav-button ${activeTab === "messages" ? "active" : ""}`,
|
|
1098
|
-
onClick: () => {
|
|
1099
|
-
setActiveTab("messages");
|
|
1100
|
-
setMessageView("list");
|
|
1101
|
-
},
|
|
1102
|
-
children: "Messages"
|
|
1103
|
-
}
|
|
1104
|
-
),
|
|
1105
|
-
/* @__PURE__ */ jsx(
|
|
1106
|
-
"button",
|
|
1107
|
-
{
|
|
1108
|
-
className: `nav-button ${activeTab === "info" ? "active" : ""}`,
|
|
1109
|
-
onClick: () => {
|
|
1110
|
-
setActiveTab("info");
|
|
1111
|
-
setMessageView("list");
|
|
1112
|
-
},
|
|
1113
|
-
children: "Info"
|
|
1114
|
-
}
|
|
1115
|
-
)
|
|
1116
|
-
] })
|
|
1117
|
-
] })
|
|
1118
|
-
}
|
|
1119
|
-
),
|
|
1120
|
-
/* @__PURE__ */ jsx(
|
|
1121
|
-
"button",
|
|
1122
|
-
{
|
|
1123
|
-
onClick: toggleChat,
|
|
1124
|
-
className: `floating-button ${config?.widget_config.button_position} button-sizes ${config?.widget_config.button_size} ${isOpen ? "is-open" : ""}`,
|
|
1125
|
-
style: { background: config?.widget_config.button_background },
|
|
1126
|
-
children: isOpen ? /* @__PURE__ */ jsx("svg", { width: "24", height: "24", viewBox: "0 0 24 24", fill: "currentColor", children: /* @__PURE__ */ jsx("path", { d: "M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z" }) }) : /* @__PURE__ */ jsx("svg", { width: "24", height: "24", viewBox: "0 0 24 24", fill: "currentColor", children: /* @__PURE__ */ jsx("path", { d: "M20 2H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h4l4 4 4-4h4c1.1 0-2 .9-2-2V4c0-1.1-.9-2-2-2zm-2 12H6v-2h12v2zm0-3H6V9h12v2zm0-3H6V6h12v2z" }) })
|
|
1127
|
-
}
|
|
1128
|
-
)
|
|
1129
|
-
] }) });
|
|
1130
|
-
};
|
|
1131
|
-
|
|
1132
|
-
export { AizekChatBot };
|
|
1133
|
-
//# sourceMappingURL=index.mjs.map
|
|
1134
|
-
//# sourceMappingURL=index.mjs.map
|