@a.izzuddin/ai-chat 0.2.11 → 0.2.14
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/custom-elements.json +69 -6
- package/dist/index.d.mts +22 -1
- package/dist/index.d.ts +22 -1
- package/dist/index.js +149 -20
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +149 -20
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -2
package/dist/index.mjs
CHANGED
|
@@ -14,7 +14,7 @@ var __decorateClass = (decorators, target, key, kind) => {
|
|
|
14
14
|
if (kind && result) __defProp(target, key, result);
|
|
15
15
|
return result;
|
|
16
16
|
};
|
|
17
|
-
var VERSION = "0.2.
|
|
17
|
+
var VERSION = "0.2.14";
|
|
18
18
|
var AIChat = class extends LitElement {
|
|
19
19
|
constructor() {
|
|
20
20
|
super();
|
|
@@ -36,6 +36,8 @@ var AIChat = class extends LitElement {
|
|
|
36
36
|
this.welcomeMessage = "How can I help you today?";
|
|
37
37
|
this.welcomeSubtitle = "";
|
|
38
38
|
this.initialQuestionsUrl = "";
|
|
39
|
+
this.language = "en";
|
|
40
|
+
this.showRelatedFaqs = false;
|
|
39
41
|
this.messages = [];
|
|
40
42
|
this.input = "";
|
|
41
43
|
this.isLoading = false;
|
|
@@ -205,16 +207,29 @@ ${this.welcomeSubtitle}` : this.welcomeMessage;
|
|
|
205
207
|
let suggestedQuestions = void 0;
|
|
206
208
|
if (this.initialQuestionsUrl) {
|
|
207
209
|
try {
|
|
208
|
-
|
|
210
|
+
let fetchUrl = this.initialQuestionsUrl;
|
|
211
|
+
if (this.language) {
|
|
212
|
+
const separator = fetchUrl.includes("?") ? "&" : "?";
|
|
213
|
+
fetchUrl = `${fetchUrl}${separator}language=${this.language}`;
|
|
214
|
+
}
|
|
215
|
+
console.log("\u{1F4E4} Fetching initial questions from:", fetchUrl);
|
|
216
|
+
const response = await fetch(fetchUrl);
|
|
209
217
|
if (response.ok) {
|
|
210
218
|
const data = await response.json();
|
|
211
219
|
console.log("\u{1F4E5} Fetched initial questions:", data);
|
|
212
220
|
let questionsArray = data.questions || data.suggested_questions || data;
|
|
213
221
|
if (Array.isArray(questionsArray) && questionsArray.length > 0) {
|
|
214
222
|
if (typeof questionsArray[0] === "object" && questionsArray[0].question_text) {
|
|
215
|
-
suggestedQuestions = questionsArray.map((q) =>
|
|
223
|
+
suggestedQuestions = questionsArray.map((q) => ({
|
|
224
|
+
id: q.id,
|
|
225
|
+
question_type: q.question_type,
|
|
226
|
+
question_text: q.question_text,
|
|
227
|
+
category: q.category
|
|
228
|
+
}));
|
|
216
229
|
} else if (typeof questionsArray[0] === "string") {
|
|
217
|
-
suggestedQuestions = questionsArray
|
|
230
|
+
suggestedQuestions = questionsArray.map((q) => ({
|
|
231
|
+
question_text: q
|
|
232
|
+
}));
|
|
218
233
|
}
|
|
219
234
|
}
|
|
220
235
|
console.log("\u2705 Processed suggested questions:", suggestedQuestions);
|
|
@@ -252,14 +267,121 @@ ${this.welcomeSubtitle}` : this.welcomeMessage;
|
|
|
252
267
|
}
|
|
253
268
|
}, 100);
|
|
254
269
|
}
|
|
270
|
+
/**
|
|
271
|
+
* Normalize suggested questions - converts string arrays to SuggestedQuestion objects
|
|
272
|
+
*/
|
|
273
|
+
normalizeSuggestedQuestions(questions) {
|
|
274
|
+
if (!questions || !Array.isArray(questions) || questions.length === 0) {
|
|
275
|
+
return void 0;
|
|
276
|
+
}
|
|
277
|
+
if (typeof questions[0] === "object" && questions[0].question_text) {
|
|
278
|
+
return questions;
|
|
279
|
+
}
|
|
280
|
+
if (typeof questions[0] === "string") {
|
|
281
|
+
return questions.map((q) => ({
|
|
282
|
+
question_text: q
|
|
283
|
+
}));
|
|
284
|
+
}
|
|
285
|
+
return void 0;
|
|
286
|
+
}
|
|
255
287
|
handleInput(e) {
|
|
256
288
|
this.input = e.target.value;
|
|
257
289
|
}
|
|
258
|
-
handleFAQClick(question) {
|
|
290
|
+
async handleFAQClick(question) {
|
|
259
291
|
if (this.isLoading) return;
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
292
|
+
if (question.id && question.question_type) {
|
|
293
|
+
await this.handleSuggestedQuestionClick(question);
|
|
294
|
+
} else {
|
|
295
|
+
this.input = question.question_text;
|
|
296
|
+
const submitEvent = new Event("submit", { cancelable: true });
|
|
297
|
+
this.handleSubmit(submitEvent);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
async handleSuggestedQuestionClick(question) {
|
|
301
|
+
if (!question.id || !question.question_type) return;
|
|
302
|
+
const userMessage = {
|
|
303
|
+
id: Date.now().toString(),
|
|
304
|
+
role: "user",
|
|
305
|
+
content: question.question_text
|
|
306
|
+
};
|
|
307
|
+
this.messages = [...this.messages, userMessage];
|
|
308
|
+
this.isLoading = true;
|
|
309
|
+
this.dispatchEvent(new CustomEvent("message-sent", {
|
|
310
|
+
detail: userMessage,
|
|
311
|
+
bubbles: true,
|
|
312
|
+
composed: true
|
|
313
|
+
}));
|
|
314
|
+
try {
|
|
315
|
+
let baseUrl = "";
|
|
316
|
+
if (this.initialQuestionsUrl) {
|
|
317
|
+
try {
|
|
318
|
+
const urlObj = new URL(this.initialQuestionsUrl);
|
|
319
|
+
baseUrl = `${urlObj.protocol}//${urlObj.host}`;
|
|
320
|
+
} catch {
|
|
321
|
+
baseUrl = "http://43.217.183.120:8080";
|
|
322
|
+
}
|
|
323
|
+
} else {
|
|
324
|
+
baseUrl = "http://43.217.183.120:8080";
|
|
325
|
+
}
|
|
326
|
+
const url = `${baseUrl}/api/questions/${question.id}?question_type=${question.question_type}`;
|
|
327
|
+
console.log("\u{1F4E4} Calling suggested question API:", url);
|
|
328
|
+
const response = await fetch(url, {
|
|
329
|
+
method: "GET",
|
|
330
|
+
headers: { "Content-Type": "application/json" }
|
|
331
|
+
});
|
|
332
|
+
if (!response.ok) {
|
|
333
|
+
const errorText = await response.text();
|
|
334
|
+
throw new Error(`Backend error: ${response.status} ${errorText}`);
|
|
335
|
+
}
|
|
336
|
+
const data = await response.json();
|
|
337
|
+
console.log("\u{1F50D} Suggested question API response:", data);
|
|
338
|
+
let responseText = "No response from agent";
|
|
339
|
+
let suggestedQuestions = void 0;
|
|
340
|
+
if (data && typeof data === "object") {
|
|
341
|
+
if (data.question && data.question.answer_text) {
|
|
342
|
+
responseText = data.question.answer_text;
|
|
343
|
+
}
|
|
344
|
+
if (data.related_questions && Array.isArray(data.related_questions) && data.related_questions.length > 0) {
|
|
345
|
+
if (typeof data.related_questions[0] === "object" && data.related_questions[0].question_text) {
|
|
346
|
+
suggestedQuestions = data.related_questions.map((q) => ({
|
|
347
|
+
id: q.id,
|
|
348
|
+
question_type: q.question_type,
|
|
349
|
+
question_text: q.question_text,
|
|
350
|
+
category: q.category
|
|
351
|
+
}));
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
const assistantMessage = {
|
|
356
|
+
id: (Date.now() + 1).toString(),
|
|
357
|
+
role: "assistant",
|
|
358
|
+
content: responseText,
|
|
359
|
+
suggestedQuestions
|
|
360
|
+
};
|
|
361
|
+
this.messages = [...this.messages, assistantMessage];
|
|
362
|
+
this.dispatchEvent(new CustomEvent("response-received", {
|
|
363
|
+
detail: assistantMessage,
|
|
364
|
+
bubbles: true,
|
|
365
|
+
composed: true
|
|
366
|
+
}));
|
|
367
|
+
} catch (err) {
|
|
368
|
+
console.error("Suggested question API failed:", err);
|
|
369
|
+
const errorMessage = {
|
|
370
|
+
id: (Date.now() + 1).toString(),
|
|
371
|
+
role: "assistant",
|
|
372
|
+
content: `Error: ${err instanceof Error ? err.message : "Unknown error"}
|
|
373
|
+
|
|
374
|
+
Please check your API endpoint configuration.`
|
|
375
|
+
};
|
|
376
|
+
this.messages = [...this.messages, errorMessage];
|
|
377
|
+
this.dispatchEvent(new CustomEvent("error", {
|
|
378
|
+
detail: err,
|
|
379
|
+
bubbles: true,
|
|
380
|
+
composed: true
|
|
381
|
+
}));
|
|
382
|
+
} finally {
|
|
383
|
+
this.isLoading = false;
|
|
384
|
+
}
|
|
263
385
|
}
|
|
264
386
|
async handleSubmit(e) {
|
|
265
387
|
e.preventDefault();
|
|
@@ -308,14 +430,14 @@ ${this.welcomeSubtitle}` : this.welcomeMessage;
|
|
|
308
430
|
if (innerData && innerData.response && typeof innerData.response === "string") {
|
|
309
431
|
responseText = innerData.response;
|
|
310
432
|
faqs = innerData.faq_used || innerData.faqs_used || void 0;
|
|
311
|
-
suggestedQuestions = innerData.suggested_follow_ups || innerData.suggested_questions
|
|
433
|
+
suggestedQuestions = this.normalizeSuggestedQuestions(innerData.suggested_follow_ups || innerData.suggested_questions);
|
|
312
434
|
console.log("\u2705 Extracted text length:", responseText.length);
|
|
313
435
|
console.log("\u2705 Extracted FAQs count:", faqs?.length || 0);
|
|
314
436
|
console.log("\u2705 Extracted suggested questions count:", suggestedQuestions?.length || 0);
|
|
315
437
|
} else {
|
|
316
438
|
responseText = data.response;
|
|
317
439
|
faqs = data.faq_used || data.faqs_used || void 0;
|
|
318
|
-
suggestedQuestions = data.suggested_follow_ups || data.suggested_questions
|
|
440
|
+
suggestedQuestions = this.normalizeSuggestedQuestions(data.suggested_follow_ups || data.suggested_questions);
|
|
319
441
|
}
|
|
320
442
|
} catch (parseError) {
|
|
321
443
|
console.warn("\u26A0\uFE0F JSON.parse failed, using regex extraction...", parseError);
|
|
@@ -352,7 +474,8 @@ ${this.welcomeSubtitle}` : this.welcomeMessage;
|
|
|
352
474
|
const suggestedMatch = data.response.match(suggestedPattern);
|
|
353
475
|
if (suggestedMatch) {
|
|
354
476
|
try {
|
|
355
|
-
|
|
477
|
+
const parsedQuestions = JSON.parse(suggestedMatch[1]);
|
|
478
|
+
suggestedQuestions = this.normalizeSuggestedQuestions(parsedQuestions);
|
|
356
479
|
console.log("\u2705 Extracted suggested questions, count:", suggestedQuestions?.length || 0);
|
|
357
480
|
} catch {
|
|
358
481
|
console.log("\u26A0\uFE0F Could not parse suggested questions, trying multiline...");
|
|
@@ -360,7 +483,8 @@ ${this.welcomeSubtitle}` : this.welcomeMessage;
|
|
|
360
483
|
const suggestedMultiMatch = data.response.match(suggestedMultiPattern);
|
|
361
484
|
if (suggestedMultiMatch) {
|
|
362
485
|
try {
|
|
363
|
-
|
|
486
|
+
const parsedQuestions = JSON.parse(suggestedMultiMatch[1]);
|
|
487
|
+
suggestedQuestions = this.normalizeSuggestedQuestions(parsedQuestions);
|
|
364
488
|
console.log("\u2705 Extracted multi-line suggested questions, count:", suggestedQuestions?.length || 0);
|
|
365
489
|
} catch {
|
|
366
490
|
suggestedQuestions = void 0;
|
|
@@ -373,7 +497,7 @@ ${this.welcomeSubtitle}` : this.welcomeMessage;
|
|
|
373
497
|
console.log("\u{1F4C4} Direct text response (not JSON)");
|
|
374
498
|
responseText = data.response;
|
|
375
499
|
faqs = data.faq_used || data.faqs_used || void 0;
|
|
376
|
-
suggestedQuestions = data.suggested_follow_ups || data.suggested_questions
|
|
500
|
+
suggestedQuestions = this.normalizeSuggestedQuestions(data.suggested_follow_ups || data.suggested_questions);
|
|
377
501
|
}
|
|
378
502
|
} else if (typeof data === "string") {
|
|
379
503
|
console.log("\u{1F4C4} Response is a plain string");
|
|
@@ -382,7 +506,7 @@ ${this.welcomeSubtitle}` : this.welcomeMessage;
|
|
|
382
506
|
console.warn("\u26A0\uFE0F Unexpected format, using fallback");
|
|
383
507
|
responseText = data.message || data.answer || "Error: Unexpected response format";
|
|
384
508
|
faqs = data.faq_used || data.faqs_used || void 0;
|
|
385
|
-
suggestedQuestions = data.suggested_follow_ups || data.suggested_questions
|
|
509
|
+
suggestedQuestions = this.normalizeSuggestedQuestions(data.suggested_follow_ups || data.suggested_questions);
|
|
386
510
|
}
|
|
387
511
|
console.log("\u{1F3AF} Final responseText length:", responseText.length);
|
|
388
512
|
console.log("\u{1F3AF} Final responseText preview:", responseText.substring(0, 100));
|
|
@@ -451,7 +575,7 @@ Please check your API endpoint configuration.`
|
|
|
451
575
|
</div>
|
|
452
576
|
<div class="message-content">
|
|
453
577
|
<div class="message-text">${unsafeHTML(this.formatMessageContent(msg.content))}</div>
|
|
454
|
-
${msg.role === "assistant" && msg.faqs && msg.faqs.length > 0 ? html`
|
|
578
|
+
${msg.role === "assistant" && this.showRelatedFaqs && msg.faqs && msg.faqs.length > 0 ? html`
|
|
455
579
|
<div class="faq-section">
|
|
456
580
|
<p class="faq-title">Related FAQs:</p>
|
|
457
581
|
<ul class="faq-list">
|
|
@@ -469,7 +593,7 @@ Please check your API endpoint configuration.`
|
|
|
469
593
|
<ul class="faq-list">
|
|
470
594
|
${msg.suggestedQuestions.map((question) => html`
|
|
471
595
|
<li class="faq-item" @click=${() => this.handleFAQClick(question)}>
|
|
472
|
-
${question}
|
|
596
|
+
${question.question_text}
|
|
473
597
|
</li>
|
|
474
598
|
`)}
|
|
475
599
|
</ul>
|
|
@@ -635,9 +759,12 @@ AIChat.styles = css`
|
|
|
635
759
|
}
|
|
636
760
|
|
|
637
761
|
.widget-button-icon {
|
|
638
|
-
width:
|
|
639
|
-
height:
|
|
640
|
-
|
|
762
|
+
width: 100%;
|
|
763
|
+
height: 100%;
|
|
764
|
+
max-width: 60px;
|
|
765
|
+
max-height: 60px;
|
|
766
|
+
object-fit: contain;
|
|
767
|
+
border-radius: 50%;
|
|
641
768
|
}
|
|
642
769
|
|
|
643
770
|
.widget-window {
|
|
@@ -1331,7 +1458,9 @@ AIChat.properties = {
|
|
|
1331
1458
|
botMessageBg: { type: String, attribute: "bot-message-bg" },
|
|
1332
1459
|
welcomeMessage: { type: String, attribute: "welcome-message" },
|
|
1333
1460
|
welcomeSubtitle: { type: String, attribute: "welcome-subtitle" },
|
|
1334
|
-
initialQuestionsUrl: { type: String, attribute: "initial-questions-url" }
|
|
1461
|
+
initialQuestionsUrl: { type: String, attribute: "initial-questions-url" },
|
|
1462
|
+
language: { type: String, attribute: "language" },
|
|
1463
|
+
showRelatedFaqs: { type: Boolean, attribute: "show-related-faqs" }
|
|
1335
1464
|
};
|
|
1336
1465
|
__decorateClass([
|
|
1337
1466
|
state()
|