aizek-chatbot 1.0.25 → 1.0.27
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/dist/index.cjs +675 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.css +1379 -0
- package/dist/index.css.map +1 -0
- package/dist/index.d.mts +50 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.mjs +668 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +1 -1
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,668 @@
|
|
|
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
|
+
|
|
6
|
+
// src/utils/cx.ts
|
|
7
|
+
function validateHeaders(headers, authConfig, opts = {}) {
|
|
8
|
+
if (headers && (!authConfig || Object.keys(authConfig).length === 0)) {
|
|
9
|
+
return {
|
|
10
|
+
isValid: false,
|
|
11
|
+
missingKeys: [],
|
|
12
|
+
extraKeys: [],
|
|
13
|
+
emptyValueKeys: [],
|
|
14
|
+
warning: "Auth config bo\u015F ya da tan\u0131ms\u0131z, header do\u011Frulamas\u0131 yap\u0131lam\u0131yor."
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
const { caseSensitive = false, allowExtra = false } = opts;
|
|
18
|
+
const normalize = (s) => caseSensitive ? s : s.toLowerCase();
|
|
19
|
+
const headerEntries = Object.entries(headers).map(([k, v]) => [normalize(k), v.trim()]);
|
|
20
|
+
const authEntries = Object.entries(authConfig).map(([k, v]) => [normalize(k), v.trim()]);
|
|
21
|
+
const headerKeys = headerEntries.map(([k]) => k);
|
|
22
|
+
const authKeys = authEntries.map(([k]) => k);
|
|
23
|
+
const requiredSet = new Set(authKeys);
|
|
24
|
+
const missingKeys = authKeys.filter((k) => !headerKeys.includes(k));
|
|
25
|
+
const extraKeys = headerKeys.filter((k) => !requiredSet.has(k));
|
|
26
|
+
const hasAllRequired = missingKeys.length === 0;
|
|
27
|
+
const hasExtraKeys = extraKeys.length > 0 && !allowExtra;
|
|
28
|
+
const emptyValueKeys = authKeys.filter((k) => {
|
|
29
|
+
const val = headerEntries.find(([key]) => key === k)?.[1];
|
|
30
|
+
return !val || val.length === 0;
|
|
31
|
+
});
|
|
32
|
+
const isValid = hasAllRequired && !hasExtraKeys && emptyValueKeys.length === 0;
|
|
33
|
+
return { isValid, missingKeys, extraKeys, emptyValueKeys };
|
|
34
|
+
}
|
|
35
|
+
var HeaderAlert = ({ headerValidation, showAlert, setShowAlert }) => {
|
|
36
|
+
if (!headerValidation || !showAlert) return null;
|
|
37
|
+
const { isValid, missingKeys, extraKeys, emptyValueKeys, warning } = headerValidation;
|
|
38
|
+
if (isValid && missingKeys.length === 0 && extraKeys.length === 0 && emptyValueKeys.length === 0 && !warning) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
const hasErrors = missingKeys.length > 0 || emptyValueKeys.length > 0;
|
|
42
|
+
const hasWarnings = extraKeys.length > 0 || !!warning;
|
|
43
|
+
const alertType = hasErrors ? "error" : "warning";
|
|
44
|
+
const getAlertIcon = () => {
|
|
45
|
+
if (hasErrors) {
|
|
46
|
+
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" }) });
|
|
47
|
+
}
|
|
48
|
+
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" }) });
|
|
49
|
+
};
|
|
50
|
+
return /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsxs("div", { className: `alert-container ${alertType}`, children: [
|
|
51
|
+
/* @__PURE__ */ jsx("div", { className: "alert-icon-container", children: getAlertIcon() }),
|
|
52
|
+
/* @__PURE__ */ jsxs("div", { className: "alert-content", children: [
|
|
53
|
+
/* @__PURE__ */ jsx("h4", { className: "alert-title", children: hasErrors ? "Header Do\u011Frulama Hatas\u0131" : "Header Uyar\u0131s\u0131" }),
|
|
54
|
+
/* @__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." }),
|
|
55
|
+
missingKeys.length > 0 && /* @__PURE__ */ jsxs("div", { children: [
|
|
56
|
+
/* @__PURE__ */ jsx("strong", { children: "Eksik Header'lar:" }),
|
|
57
|
+
/* @__PURE__ */ jsx("ul", { className: "alert-list", children: missingKeys.map((key, index) => /* @__PURE__ */ jsxs("li", { className: "alert-list-item", children: [
|
|
58
|
+
/* @__PURE__ */ jsx("span", { children: "\u2022" }),
|
|
59
|
+
/* @__PURE__ */ jsx("code", { children: key })
|
|
60
|
+
] }, index)) })
|
|
61
|
+
] }),
|
|
62
|
+
emptyValueKeys.length > 0 && /* @__PURE__ */ jsxs("div", { children: [
|
|
63
|
+
/* @__PURE__ */ jsx("strong", { children: "Bo\u015F De\u011Ferli Header'lar:" }),
|
|
64
|
+
/* @__PURE__ */ jsx("ul", { className: "alert-list", children: emptyValueKeys.map((key, index) => /* @__PURE__ */ jsxs("li", { className: "alert-list-item", children: [
|
|
65
|
+
/* @__PURE__ */ jsx("span", { children: "\u2022" }),
|
|
66
|
+
/* @__PURE__ */ jsx("code", { children: key }),
|
|
67
|
+
/* @__PURE__ */ jsx("span", { children: "(de\u011Fer bo\u015F olamaz)" })
|
|
68
|
+
] }, index)) })
|
|
69
|
+
] }),
|
|
70
|
+
extraKeys.length > 0 && /* @__PURE__ */ jsxs("div", { children: [
|
|
71
|
+
/* @__PURE__ */ jsx("strong", { children: "Fazla Header'lar:" }),
|
|
72
|
+
/* @__PURE__ */ jsx("ul", { className: "alert-list", children: extraKeys.map((key, index) => /* @__PURE__ */ jsxs("li", { className: "alert-list-item", children: [
|
|
73
|
+
/* @__PURE__ */ jsx("span", { children: "\u2022" }),
|
|
74
|
+
/* @__PURE__ */ jsx("code", { children: key })
|
|
75
|
+
] }, index)) })
|
|
76
|
+
] }),
|
|
77
|
+
warning && /* @__PURE__ */ jsxs("div", { children: [
|
|
78
|
+
/* @__PURE__ */ jsx("strong", { children: "Uyar\u0131:" }),
|
|
79
|
+
/* @__PURE__ */ jsx("p", { className: "alert-message", children: warning })
|
|
80
|
+
] })
|
|
81
|
+
] }),
|
|
82
|
+
/* @__PURE__ */ jsx(
|
|
83
|
+
"button",
|
|
84
|
+
{
|
|
85
|
+
onClick: () => setShowAlert(false),
|
|
86
|
+
className: "alert-close-button",
|
|
87
|
+
"aria-label": "Uyar\u0131y\u0131 kapat",
|
|
88
|
+
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" }) })
|
|
89
|
+
}
|
|
90
|
+
)
|
|
91
|
+
] }) });
|
|
92
|
+
};
|
|
93
|
+
var LoadingSpinner = () => {
|
|
94
|
+
return /* @__PURE__ */ jsx("div", { className: "loading-spinner" });
|
|
95
|
+
};
|
|
96
|
+
var ChatInput = ({ isLoading, placeholder, handleSendMessage }) => {
|
|
97
|
+
const [message, setMessage] = useState("");
|
|
98
|
+
const textareaRef = useRef(null);
|
|
99
|
+
const handleSubmit = (e) => {
|
|
100
|
+
e.preventDefault();
|
|
101
|
+
if (message.trim() && !isLoading) {
|
|
102
|
+
handleSendMessage(message.trim());
|
|
103
|
+
setMessage("");
|
|
104
|
+
if (textareaRef.current) {
|
|
105
|
+
textareaRef.current.style.height = "auto";
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
const handleKeyDown = (e) => {
|
|
110
|
+
if (e.key === "Enter" && !e.shiftKey) {
|
|
111
|
+
e.preventDefault();
|
|
112
|
+
handleSubmit(e);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
const handleInputChange = (e) => {
|
|
116
|
+
setMessage(e.target.value);
|
|
117
|
+
const textarea = e.target;
|
|
118
|
+
textarea.style.height = "auto";
|
|
119
|
+
textarea.style.height = Math.min(textarea.scrollHeight, 120) + "px";
|
|
120
|
+
};
|
|
121
|
+
return /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, className: "input-container", children: [
|
|
122
|
+
/* @__PURE__ */ jsx(
|
|
123
|
+
"textarea",
|
|
124
|
+
{
|
|
125
|
+
ref: textareaRef,
|
|
126
|
+
value: message,
|
|
127
|
+
onChange: handleInputChange,
|
|
128
|
+
onKeyDown: handleKeyDown,
|
|
129
|
+
placeholder,
|
|
130
|
+
disabled: isLoading,
|
|
131
|
+
className: "textarea"
|
|
132
|
+
}
|
|
133
|
+
),
|
|
134
|
+
/* @__PURE__ */ jsx(
|
|
135
|
+
"button",
|
|
136
|
+
{
|
|
137
|
+
type: "submit",
|
|
138
|
+
disabled: isLoading || !message.trim(),
|
|
139
|
+
className: "send-button",
|
|
140
|
+
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" }) })
|
|
141
|
+
}
|
|
142
|
+
)
|
|
143
|
+
] });
|
|
144
|
+
};
|
|
145
|
+
var GenericUIRenderer = ({ uiData, onInteraction }) => {
|
|
146
|
+
const containerRef = useRef(null);
|
|
147
|
+
if (!uiData || !uiData.components) return null;
|
|
148
|
+
const collectFormValues = () => {
|
|
149
|
+
if (!containerRef.current) return {};
|
|
150
|
+
const formData = {};
|
|
151
|
+
const inputs = containerRef.current.querySelectorAll("input, textarea, select");
|
|
152
|
+
inputs.forEach((element) => {
|
|
153
|
+
const name = element.getAttribute("name");
|
|
154
|
+
if (name) {
|
|
155
|
+
if (element instanceof HTMLInputElement && element.type === "checkbox") {
|
|
156
|
+
formData[name] = element.checked;
|
|
157
|
+
} else if (element instanceof HTMLInputElement && element.type === "radio") {
|
|
158
|
+
if (element.checked) {
|
|
159
|
+
formData[name] = element.value;
|
|
160
|
+
}
|
|
161
|
+
} else {
|
|
162
|
+
formData[name] = element.value;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
return formData;
|
|
167
|
+
};
|
|
168
|
+
const renderComponent = (comp) => {
|
|
169
|
+
switch (comp.type) {
|
|
170
|
+
case "text":
|
|
171
|
+
return /* @__PURE__ */ jsx("p", { className: "gen-ui-text", children: comp.value ?? comp.label }, comp.id);
|
|
172
|
+
case "input":
|
|
173
|
+
return /* @__PURE__ */ jsxs("div", { className: "gen-ui-input-wrapper", children: [
|
|
174
|
+
comp.label && /* @__PURE__ */ jsx("label", { className: "gen-ui-input-label", children: comp.label }),
|
|
175
|
+
comp.fieldType === "textarea" ? /* @__PURE__ */ jsx(
|
|
176
|
+
"textarea",
|
|
177
|
+
{
|
|
178
|
+
name: comp.id,
|
|
179
|
+
placeholder: comp.placeholder,
|
|
180
|
+
className: "gen-ui-textarea"
|
|
181
|
+
}
|
|
182
|
+
) : /* @__PURE__ */ jsx(
|
|
183
|
+
"input",
|
|
184
|
+
{
|
|
185
|
+
name: comp.id,
|
|
186
|
+
type: comp.fieldType || "text",
|
|
187
|
+
placeholder: comp.placeholder,
|
|
188
|
+
className: "gen-ui-input"
|
|
189
|
+
}
|
|
190
|
+
)
|
|
191
|
+
] }, comp.id);
|
|
192
|
+
case "select":
|
|
193
|
+
return /* @__PURE__ */ jsxs("div", { className: "gen-ui-select-wrapper", children: [
|
|
194
|
+
comp.label && /* @__PURE__ */ jsx("label", { className: "gen-ui-select-label", children: comp.label }),
|
|
195
|
+
/* @__PURE__ */ jsx("select", { name: comp.id, className: "gen-ui-select", children: comp.options?.map((opt) => /* @__PURE__ */ jsx("option", { value: opt, children: opt }, opt)) })
|
|
196
|
+
] }, comp.id);
|
|
197
|
+
case "button":
|
|
198
|
+
return /* @__PURE__ */ jsx(
|
|
199
|
+
"button",
|
|
200
|
+
{
|
|
201
|
+
onClick: () => {
|
|
202
|
+
const formValues = collectFormValues();
|
|
203
|
+
onInteraction({
|
|
204
|
+
action: comp.label,
|
|
205
|
+
buttonId: comp.buttonType,
|
|
206
|
+
formData: formValues
|
|
207
|
+
});
|
|
208
|
+
},
|
|
209
|
+
className: "gen-ui-button",
|
|
210
|
+
children: comp.label
|
|
211
|
+
},
|
|
212
|
+
comp.id
|
|
213
|
+
);
|
|
214
|
+
case "card":
|
|
215
|
+
return /* @__PURE__ */ jsxs("div", { className: "gen-ui-card", children: [
|
|
216
|
+
comp.label && /* @__PURE__ */ jsx("h2", { className: "gen-ui-card-title", children: comp.label }),
|
|
217
|
+
comp.value && /* @__PURE__ */ jsx("p", { className: "gen-ui-card-content", children: comp.value })
|
|
218
|
+
] }, comp.id);
|
|
219
|
+
case "list":
|
|
220
|
+
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);
|
|
221
|
+
case "image":
|
|
222
|
+
return /* @__PURE__ */ jsx(
|
|
223
|
+
"img",
|
|
224
|
+
{
|
|
225
|
+
src: comp.url,
|
|
226
|
+
alt: comp.label || "",
|
|
227
|
+
className: "gen-ui-image"
|
|
228
|
+
},
|
|
229
|
+
comp.id
|
|
230
|
+
);
|
|
231
|
+
case "link":
|
|
232
|
+
return /* @__PURE__ */ jsx(
|
|
233
|
+
"a",
|
|
234
|
+
{
|
|
235
|
+
href: comp.url,
|
|
236
|
+
className: "gen-ui-link",
|
|
237
|
+
children: comp.label || comp.url
|
|
238
|
+
},
|
|
239
|
+
comp.id
|
|
240
|
+
);
|
|
241
|
+
case "table":
|
|
242
|
+
return /* @__PURE__ */ jsx("div", { className: "gen-ui-table-wrapper", children: /* @__PURE__ */ jsxs("table", { className: "gen-ui-table", children: [
|
|
243
|
+
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)) }) }),
|
|
244
|
+
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)) })
|
|
245
|
+
] }) }, comp.id);
|
|
246
|
+
case "form":
|
|
247
|
+
return /* @__PURE__ */ jsx("form", { className: "gen-ui-form", children: comp.items?.map((field) => renderComponent(field)) }, comp.id);
|
|
248
|
+
default:
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
return /* @__PURE__ */ jsxs("div", { className: "generative-ui-container", ref: containerRef, children: [
|
|
253
|
+
(uiData.title || uiData.description) && /* @__PURE__ */ jsxs("div", { className: "generative-ui-header", children: [
|
|
254
|
+
uiData.title && /* @__PURE__ */ jsx("h1", { className: "generative-ui-title", children: uiData.title }),
|
|
255
|
+
uiData.description && /* @__PURE__ */ jsx("p", { className: "generative-ui-description", children: uiData.description })
|
|
256
|
+
] }),
|
|
257
|
+
uiData.components.map((comp) => renderComponent(comp))
|
|
258
|
+
] });
|
|
259
|
+
};
|
|
260
|
+
var MessageBubble = ({ message, onAction }) => {
|
|
261
|
+
console.log("MESSAGE", message);
|
|
262
|
+
const isUser = message.role === "user";
|
|
263
|
+
const approval = message.role === "approval";
|
|
264
|
+
if (approval) {
|
|
265
|
+
return /* @__PURE__ */ jsx(Fragment, {});
|
|
266
|
+
}
|
|
267
|
+
return /* @__PURE__ */ jsxs("div", { className: `message-container ${isUser ? "user" : "assistant"}`, children: [
|
|
268
|
+
/* @__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: [
|
|
269
|
+
message.text && /* @__PURE__ */ jsx("div", { className: "markdown-content", children: /* @__PURE__ */ jsx(ReactMarkdown, { remarkPlugins: [remarkGfm], children: message.text }) }),
|
|
270
|
+
message.ui && /* @__PURE__ */ jsx(
|
|
271
|
+
GenericUIRenderer,
|
|
272
|
+
{
|
|
273
|
+
uiData: message.ui,
|
|
274
|
+
onInteraction: (event) => {
|
|
275
|
+
console.log("event", event);
|
|
276
|
+
if (event.buttonId === "submit") {
|
|
277
|
+
onAction(JSON.stringify(event.formData), true);
|
|
278
|
+
} else {
|
|
279
|
+
onAction(event.action, true);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
)
|
|
284
|
+
] }) }),
|
|
285
|
+
/* @__PURE__ */ jsx("div", { className: `message-time ${isUser ? "user" : "assistant"}`, children: message.timestamp.toLocaleTimeString("tr-TR", {
|
|
286
|
+
hour: "2-digit",
|
|
287
|
+
minute: "2-digit"
|
|
288
|
+
}) })
|
|
289
|
+
] });
|
|
290
|
+
};
|
|
291
|
+
var TypingDots = () => {
|
|
292
|
+
const [dots, setDots] = useState("");
|
|
293
|
+
useEffect(() => {
|
|
294
|
+
const interval = setInterval(() => {
|
|
295
|
+
setDots((prev) => {
|
|
296
|
+
if (prev === "...") return "";
|
|
297
|
+
return prev + ".";
|
|
298
|
+
});
|
|
299
|
+
}, 500);
|
|
300
|
+
return () => clearInterval(interval);
|
|
301
|
+
}, []);
|
|
302
|
+
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 }) }) }) });
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
// src/utils/chatbot.ts
|
|
306
|
+
var extractUIJsonFromText = (text) => {
|
|
307
|
+
const regex = /```ui-component([\s\S]*?)```/g;
|
|
308
|
+
let cleaned = text;
|
|
309
|
+
let match;
|
|
310
|
+
let uiData = null;
|
|
311
|
+
while ((match = regex.exec(text)) !== null) {
|
|
312
|
+
const block = match[1].trim();
|
|
313
|
+
try {
|
|
314
|
+
const parsed = JSON.parse(block);
|
|
315
|
+
if (parsed && parsed.components && Array.isArray(parsed.components)) {
|
|
316
|
+
parsed.components = parsed.components.map((comp, index) => {
|
|
317
|
+
if (!comp.id || typeof comp.id !== "string" || !comp.id.trim()) {
|
|
318
|
+
comp.id = `ui-comp-${index}-${Date.now()}`;
|
|
319
|
+
}
|
|
320
|
+
if (comp.type === "button") {
|
|
321
|
+
if (!comp.buttonType) {
|
|
322
|
+
comp.buttonType = "click";
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
if (comp.type === "input") {
|
|
326
|
+
if (!comp.fieldType) {
|
|
327
|
+
comp.fieldType = "text";
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (comp.type === "select") {
|
|
331
|
+
if (comp.options && Array.isArray(comp.options)) {
|
|
332
|
+
const firstOption = comp.options[0];
|
|
333
|
+
if (typeof firstOption === "object" && firstOption !== null) {
|
|
334
|
+
if ("label" in firstOption || "value" in firstOption) {
|
|
335
|
+
comp.options = comp.options.map(
|
|
336
|
+
(opt) => opt.label || opt.value || String(opt)
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
if (comp.type === "table") {
|
|
343
|
+
if (comp.columns && Array.isArray(comp.columns)) {
|
|
344
|
+
const firstCol = comp.columns[0];
|
|
345
|
+
if (typeof firstCol === "object" && firstCol !== null) {
|
|
346
|
+
comp.columns = comp.columns.map(
|
|
347
|
+
(col) => col.label || col.id || String(col)
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (comp.rows && Array.isArray(comp.rows)) {
|
|
352
|
+
const firstRow = comp.rows[0];
|
|
353
|
+
if (typeof firstRow === "object" && !Array.isArray(firstRow)) {
|
|
354
|
+
comp.rows = comp.rows.map((row) => {
|
|
355
|
+
if (Array.isArray(row)) return row;
|
|
356
|
+
return Object.values(row).map((val) => {
|
|
357
|
+
if (typeof val === "object" && val !== null) {
|
|
358
|
+
if (val.type === "image" && val.src) return val.src;
|
|
359
|
+
if (val.value !== void 0) return String(val.value);
|
|
360
|
+
}
|
|
361
|
+
return String(val || "");
|
|
362
|
+
});
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
if (comp.type === "form" && comp.items && Array.isArray(comp.items)) {
|
|
368
|
+
comp.items = comp.items.map((field, fieldIndex) => {
|
|
369
|
+
if (!field.id || !field.id.trim()) {
|
|
370
|
+
field.id = `field-${fieldIndex}-${Date.now()}`;
|
|
371
|
+
}
|
|
372
|
+
if (field.optional !== void 0) {
|
|
373
|
+
field.requiredField = !field.optional;
|
|
374
|
+
delete field.optional;
|
|
375
|
+
}
|
|
376
|
+
if (field.required !== void 0) {
|
|
377
|
+
field.requiredField = field.required;
|
|
378
|
+
delete field.required;
|
|
379
|
+
}
|
|
380
|
+
if (field.type === "input" && !field.fieldType) {
|
|
381
|
+
field.fieldType = "text";
|
|
382
|
+
}
|
|
383
|
+
if (field.type === "select" && field.options && Array.isArray(field.options)) {
|
|
384
|
+
const firstOpt = field.options[0];
|
|
385
|
+
if (typeof firstOpt === "object" && firstOpt !== null) {
|
|
386
|
+
field.options = field.options.map(
|
|
387
|
+
(opt) => opt.label || opt.value || String(opt)
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return field;
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
if (comp.type === "list" && comp.items && Array.isArray(comp.items)) {
|
|
395
|
+
comp.items = comp.items.map((item, itemIndex) => {
|
|
396
|
+
if (typeof item === "string") {
|
|
397
|
+
return {
|
|
398
|
+
id: `list-item-${itemIndex}-${Date.now()}`,
|
|
399
|
+
type: "text",
|
|
400
|
+
value: item,
|
|
401
|
+
label: item
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
if (!item.id || !item.id.trim()) {
|
|
405
|
+
item.id = `list-item-${itemIndex}-${Date.now()}`;
|
|
406
|
+
}
|
|
407
|
+
return item;
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
return comp;
|
|
411
|
+
});
|
|
412
|
+
uiData = parsed;
|
|
413
|
+
}
|
|
414
|
+
cleaned = cleaned.replace(match[0], "").trim();
|
|
415
|
+
break;
|
|
416
|
+
} catch (e) {
|
|
417
|
+
console.error("Invalid ui-component JSON:", e, block);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
return {
|
|
421
|
+
cleanedText: cleaned,
|
|
422
|
+
uiData
|
|
423
|
+
};
|
|
424
|
+
};
|
|
425
|
+
var AizekChatBot = ({ clientId, headers, onMounted, onReady, onOpen, onClose, onMessage, onToolCall, onDisconnect }) => {
|
|
426
|
+
const messagesEndRef = useRef(null);
|
|
427
|
+
const [config, setConfig] = useState();
|
|
428
|
+
const [messages, setMessages] = useState([]);
|
|
429
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
430
|
+
const [isConfigLoading, setIsConfigLoading] = useState(true);
|
|
431
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
432
|
+
const [headerValidation, setHeaderValidation] = useState(null);
|
|
433
|
+
const [showAlert, setShowAlert] = useState(true);
|
|
434
|
+
const PROXY_BASE_URL = "https://proxy.aizek.ai/api";
|
|
435
|
+
const loadConfig = async () => {
|
|
436
|
+
try {
|
|
437
|
+
setIsConfigLoading(true);
|
|
438
|
+
const response = await fetch(`${PROXY_BASE_URL}/aizek-connect?clientId=${clientId}`, {
|
|
439
|
+
method: "POST",
|
|
440
|
+
headers: {
|
|
441
|
+
"Content-Type": "application/json",
|
|
442
|
+
"x-alternate": JSON.stringify(headers)
|
|
443
|
+
},
|
|
444
|
+
credentials: "include"
|
|
445
|
+
});
|
|
446
|
+
const data = await response.json();
|
|
447
|
+
if (data.success) {
|
|
448
|
+
setIsOpen(data.data.widget_config.initial_open);
|
|
449
|
+
setConfig(data.data);
|
|
450
|
+
if (headers) {
|
|
451
|
+
const validationResult = validateHeaders(
|
|
452
|
+
headers,
|
|
453
|
+
data.data.auth_config,
|
|
454
|
+
{
|
|
455
|
+
allowExtra: false,
|
|
456
|
+
caseSensitive: true
|
|
457
|
+
}
|
|
458
|
+
);
|
|
459
|
+
setHeaderValidation(validationResult);
|
|
460
|
+
}
|
|
461
|
+
onReady?.({ config: { ...data.data } });
|
|
462
|
+
} else {
|
|
463
|
+
setIsOpen(false);
|
|
464
|
+
}
|
|
465
|
+
} catch (error) {
|
|
466
|
+
console.error("Failed to load chat widget config:", error);
|
|
467
|
+
} finally {
|
|
468
|
+
setIsConfigLoading(false);
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
const disconnect = async () => {
|
|
472
|
+
try {
|
|
473
|
+
setIsConfigLoading(true);
|
|
474
|
+
await fetch(`${PROXY_BASE_URL}/aizek-disconnect?clientId=${clientId}`, {
|
|
475
|
+
method: "POST",
|
|
476
|
+
headers: {
|
|
477
|
+
"Content-Type": "application/json",
|
|
478
|
+
"x-alternate": JSON.stringify(headers)
|
|
479
|
+
},
|
|
480
|
+
credentials: "include"
|
|
481
|
+
});
|
|
482
|
+
onDisconnect?.();
|
|
483
|
+
} catch (error) {
|
|
484
|
+
console.error("Failed to load chat widget config:", error);
|
|
485
|
+
} finally {
|
|
486
|
+
setIsConfigLoading(false);
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
useEffect(() => {
|
|
490
|
+
onMounted?.();
|
|
491
|
+
loadConfig();
|
|
492
|
+
return () => {
|
|
493
|
+
disconnect();
|
|
494
|
+
};
|
|
495
|
+
}, []);
|
|
496
|
+
useEffect(() => {
|
|
497
|
+
const newIsOpen = !isOpen;
|
|
498
|
+
setIsOpen(newIsOpen);
|
|
499
|
+
if (newIsOpen) onOpen?.();
|
|
500
|
+
else onClose?.();
|
|
501
|
+
}, [config?.widget_config.initial_open]);
|
|
502
|
+
useEffect(() => {
|
|
503
|
+
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
|
|
504
|
+
}, [messages]);
|
|
505
|
+
const addMessage = (payload) => {
|
|
506
|
+
const newMessage = {
|
|
507
|
+
text: payload.text,
|
|
508
|
+
ui: payload.ui,
|
|
509
|
+
role: payload.role,
|
|
510
|
+
timestamp: /* @__PURE__ */ new Date()
|
|
511
|
+
};
|
|
512
|
+
onMessage?.(newMessage);
|
|
513
|
+
setMessages((prev) => [...prev, newMessage]);
|
|
514
|
+
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
|
|
515
|
+
return newMessage;
|
|
516
|
+
};
|
|
517
|
+
const sendMessage = async (message, approval) => {
|
|
518
|
+
if (!message.trim() || isLoading) return;
|
|
519
|
+
const newMessage = {
|
|
520
|
+
text: message,
|
|
521
|
+
ui: void 0,
|
|
522
|
+
role: approval ? "approval" : "user",
|
|
523
|
+
timestamp: /* @__PURE__ */ new Date()
|
|
524
|
+
};
|
|
525
|
+
setMessages((prev) => [...prev, newMessage]);
|
|
526
|
+
setIsLoading(true);
|
|
527
|
+
try {
|
|
528
|
+
const response = await fetch(`${PROXY_BASE_URL}/aizek-chat?clientId=${clientId}`, {
|
|
529
|
+
method: "POST",
|
|
530
|
+
headers: {
|
|
531
|
+
"Content-Type": "application/json",
|
|
532
|
+
"x-alternate": JSON.stringify(headers)
|
|
533
|
+
},
|
|
534
|
+
body: JSON.stringify({ message }),
|
|
535
|
+
credentials: "include"
|
|
536
|
+
});
|
|
537
|
+
if (!response.ok) {
|
|
538
|
+
throw new Error(`HTTP error ${response.status}`);
|
|
539
|
+
}
|
|
540
|
+
if (!response.body) {
|
|
541
|
+
throw new Error("Streaming desteklenmiyor (response.body yok)");
|
|
542
|
+
}
|
|
543
|
+
const reader = response.body.getReader();
|
|
544
|
+
const decoder = new TextDecoder();
|
|
545
|
+
let buffer = "";
|
|
546
|
+
while (true) {
|
|
547
|
+
const { value, done } = await reader.read();
|
|
548
|
+
if (done) break;
|
|
549
|
+
buffer += decoder.decode(value, { stream: true });
|
|
550
|
+
const chunks = buffer.split("\n\n");
|
|
551
|
+
buffer = chunks.pop() ?? "";
|
|
552
|
+
for (const rawChunk of chunks) {
|
|
553
|
+
const line = rawChunk.split("\n").find((l) => l.startsWith("data:"));
|
|
554
|
+
if (!line) continue;
|
|
555
|
+
const jsonStr = line.replace(/^data:\s*/, "").trim();
|
|
556
|
+
if (!jsonStr) continue;
|
|
557
|
+
const event = JSON.parse(jsonStr);
|
|
558
|
+
if (event.type === "assistant_text" && event.content) {
|
|
559
|
+
const { cleanedText, uiData } = extractUIJsonFromText(event.content);
|
|
560
|
+
addMessage({
|
|
561
|
+
text: cleanedText,
|
|
562
|
+
ui: uiData,
|
|
563
|
+
role: "assistant"
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
if (event.type === "assistant_tool_result" && event.content) {
|
|
567
|
+
const toolInfoParsed = JSON.parse(event.content);
|
|
568
|
+
onToolCall?.(toolInfoParsed);
|
|
569
|
+
}
|
|
570
|
+
if (event.type === "error" && event.content) {
|
|
571
|
+
const { cleanedText, uiData } = extractUIJsonFromText(event.content);
|
|
572
|
+
addMessage({
|
|
573
|
+
text: cleanedText,
|
|
574
|
+
ui: uiData,
|
|
575
|
+
role: "assistant"
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
} catch (error) {
|
|
581
|
+
console.error("Error sending message:", error);
|
|
582
|
+
addMessage({ text: "\xDCzg\xFCn\xFCm, bir hata olu\u015Ftu. L\xFCtfen tekrar deneyin.", role: "assistant" });
|
|
583
|
+
} finally {
|
|
584
|
+
setIsLoading(false);
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
const toggleChat = () => {
|
|
588
|
+
const newIsOpen = !isOpen;
|
|
589
|
+
setIsOpen(newIsOpen);
|
|
590
|
+
if (newIsOpen) onOpen?.();
|
|
591
|
+
else onClose?.();
|
|
592
|
+
};
|
|
593
|
+
return /* @__PURE__ */ jsx(Fragment, { children: isConfigLoading ? /* @__PURE__ */ jsx(
|
|
594
|
+
"button",
|
|
595
|
+
{
|
|
596
|
+
className: "floating-button bottom-right button-sizes medium loading-state",
|
|
597
|
+
style: { background: "#4f46e5" },
|
|
598
|
+
"aria-label": "Y\xFCkleniyor",
|
|
599
|
+
children: /* @__PURE__ */ jsx("div", { className: "loading-spinner" })
|
|
600
|
+
}
|
|
601
|
+
) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
602
|
+
isOpen && /* @__PURE__ */ jsx("div", { className: `overlay floating-chat-overlay ${isOpen ? "is-open" : ""}`, onClick: toggleChat }),
|
|
603
|
+
/* @__PURE__ */ jsx("div", { className: `chat-container ${config?.widget_config.button_position} ${isOpen ? "is-open" : ""}`, style: { width: config?.widget_config.chat_width, height: config?.widget_config.chat_height }, children: /* @__PURE__ */ jsxs("div", { className: "chatbot-container", children: [
|
|
604
|
+
/* @__PURE__ */ jsxs("div", { className: "header", style: { background: config?.widget_config.header_background }, children: [
|
|
605
|
+
/* @__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(
|
|
606
|
+
"img",
|
|
607
|
+
{
|
|
608
|
+
src: config?.widget_config.company_logo,
|
|
609
|
+
alt: "Company Logo",
|
|
610
|
+
className: "logo-image"
|
|
611
|
+
}
|
|
612
|
+
) : /* @__PURE__ */ jsx("span", { className: "logo-text", children: config?.widget_config.company_logo }) : "\u{1F916}" }),
|
|
613
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
614
|
+
/* @__PURE__ */ jsx("h3", { className: "company-name", children: config?.widget_config.company_name }),
|
|
615
|
+
/* @__PURE__ */ jsx("p", { className: "status-text", children: isLoading ? "Yaz\u0131yor..." : "\xC7evrimi\xE7i" })
|
|
616
|
+
] })
|
|
617
|
+
] }),
|
|
618
|
+
/* @__PURE__ */ jsxs("div", { className: "messages-container", children: [
|
|
619
|
+
/* @__PURE__ */ jsx(
|
|
620
|
+
HeaderAlert,
|
|
621
|
+
{
|
|
622
|
+
headerValidation,
|
|
623
|
+
setShowAlert,
|
|
624
|
+
showAlert
|
|
625
|
+
}
|
|
626
|
+
),
|
|
627
|
+
messages.length === 0 ? /* @__PURE__ */ jsxs("div", { className: "empty-state", children: [
|
|
628
|
+
/* @__PURE__ */ jsx("div", { className: "empty-state-icon", children: "\u{1F4AC}" }),
|
|
629
|
+
/* @__PURE__ */ jsx("h4", { className: "empty-state-title", children: config?.widget_config.welcome_message }),
|
|
630
|
+
/* @__PURE__ */ jsx("p", { className: "empty-state-description", children: "A\u015Fa\u011F\u0131daki alana mesaj\u0131n\u0131z\u0131 yazarak ba\u015Flayabilirsiniz." })
|
|
631
|
+
] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
632
|
+
messages.map((message, index) => /* @__PURE__ */ jsx(
|
|
633
|
+
MessageBubble,
|
|
634
|
+
{
|
|
635
|
+
message,
|
|
636
|
+
onAction: sendMessage
|
|
637
|
+
},
|
|
638
|
+
index
|
|
639
|
+
)),
|
|
640
|
+
config?.widget_config.show_typing_indicator && isLoading && /* @__PURE__ */ jsx(TypingDots, {})
|
|
641
|
+
] }),
|
|
642
|
+
/* @__PURE__ */ jsx("div", { ref: messagesEndRef })
|
|
643
|
+
] }),
|
|
644
|
+
/* @__PURE__ */ jsx(
|
|
645
|
+
ChatInput,
|
|
646
|
+
{
|
|
647
|
+
handleSendMessage: sendMessage,
|
|
648
|
+
isLoading,
|
|
649
|
+
placeholder: config?.widget_config.placeholder ?? ""
|
|
650
|
+
}
|
|
651
|
+
)
|
|
652
|
+
] }) }),
|
|
653
|
+
/* @__PURE__ */ jsx(
|
|
654
|
+
"button",
|
|
655
|
+
{
|
|
656
|
+
onClick: toggleChat,
|
|
657
|
+
className: `floating-button ${config?.widget_config.button_position} button-sizes ${config?.widget_config.button_size} ${isOpen ? "is-open" : ""}`,
|
|
658
|
+
style: { background: config?.widget_config.button_background },
|
|
659
|
+
"aria-label": isOpen ? "Chati kapat" : "Chati a\xE7",
|
|
660
|
+
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" }) })
|
|
661
|
+
}
|
|
662
|
+
)
|
|
663
|
+
] }) });
|
|
664
|
+
};
|
|
665
|
+
|
|
666
|
+
export { AizekChatBot };
|
|
667
|
+
//# sourceMappingURL=index.mjs.map
|
|
668
|
+
//# sourceMappingURL=index.mjs.map
|