|
1 |
| -function normilizeAIResponse(text) { |
| 1 | + |
| 2 | +let lastUserQuery; |
| 3 | + |
| 4 | +const assistant = { |
| 5 | + id: 'assistant', |
| 6 | + name: 'Virtual Assistant', |
| 7 | +}; |
| 8 | + |
| 9 | +const user = { |
| 10 | + id: 'user', |
| 11 | +}; |
| 12 | + |
| 13 | +function normilizeAIResponse(text) { |
2 | 14 | text = text.replace(/【\d+:\d+†[^\】]+】/g, "");
|
3 | 15 | let html = marked.parse(text);
|
4 | 16 | if (/<p>\.\s*<\/p>\s*$/.test(html))
|
5 | 17 | html = html.replace(/<p>\.\s*<\/p>\s*$/, "")
|
6 | 18 | return html;
|
7 | 19 | }
|
8 | 20 |
|
| 21 | +function copyText(text) { |
| 22 | + navigator.clipboard.writeText(text); |
| 23 | +} |
| 24 | + |
| 25 | +async function getAIResponse(text, id) { |
| 26 | + const formData = new FormData(); |
| 27 | + formData.append('text', text); |
| 28 | + formData.append('chatId', id); |
| 29 | + lastUserQuery = text; |
| 30 | + const response = await fetch(`/AI/GetAnswer`, { |
| 31 | + method: 'POST', |
| 32 | + body: formData |
| 33 | + }); |
| 34 | + return await response.text(); |
| 35 | +} |
| 36 | + |
| 37 | +function RenderAssistantMessage(instance, message) { |
| 38 | + instance.option({ typingUsers: [] }); |
| 39 | + instance.renderMessage({ timestamp: new Date(), text: message, author: assistant.name, id: assistant.id }); |
| 40 | +} |
| 41 | + |
| 42 | +async function refreshAnswer(instance) { |
| 43 | + const items = instance.option('items'); |
| 44 | + const newItems = items.slice(0, -1); |
| 45 | + instance.option({ items: newItems }); |
| 46 | + instance.option({ typingUsers: [assistant] }); |
| 47 | + const aiResponse = await getAIResponse(lastUserQuery, assistant.id); |
| 48 | + setTimeout(() => { |
| 49 | + instance.option({ typingUsers: [] }); |
| 50 | + RenderAssistantMessage(instance, aiResponse); |
| 51 | + }, 200); |
| 52 | +} |
| 53 | + |
9 | 54 | function createAssistantTab(chatId) {
|
| 55 | + assistant.id = chatId; |
10 | 56 | const model = {
|
11 | 57 | title: 'AI Assistant',
|
12 |
| - chatId: chatId, |
13 | 58 | showAvatar: false,
|
14 | 59 | showUserName: false,
|
15 | 60 | showMessageTimestamp: false,
|
16 |
| - user: { |
17 |
| - id: 1, |
18 |
| - }, |
| 61 | + user: user, |
19 | 62 | messageTemplate: (data, container) => {
|
20 |
| - if(data.message.author.id === 'Assistant') |
21 |
| - return normilizeAIResponse(data.message.text); |
22 |
| - return data.message.text; |
| 63 | + const { message } = data; |
| 64 | + |
| 65 | + if (message.author.id && message.author.id !== assistant.id) |
| 66 | + return message.text; |
| 67 | + |
| 68 | + const $textElement = $('<div>') |
| 69 | + .html(normilizeAIResponse(message.text)) |
| 70 | + .appendTo(container); |
| 71 | + const $buttonContainer = $('<div>') |
| 72 | + .addClass('dx-bubble-button-containder'); |
| 73 | + $('<div>') |
| 74 | + .dxButton({ |
| 75 | + icon: 'copy', |
| 76 | + stylingMode: 'text', |
| 77 | + onClick: () => { |
| 78 | + const text = $textElement.text(); |
| 79 | + copyText(text); |
| 80 | + } |
| 81 | + }) |
| 82 | + .appendTo($buttonContainer); |
| 83 | + $('<div>') |
| 84 | + .dxButton({ |
| 85 | + icon: 'refresh', |
| 86 | + stylingMode: 'text', |
| 87 | + onClick: () => { |
| 88 | + refreshAnswer(data.component); |
| 89 | + }, |
| 90 | + }) |
| 91 | + .appendTo($buttonContainer); |
| 92 | + $buttonContainer.appendTo(container); |
23 | 93 | },
|
24 |
| - onMessageEntered: (e) => { |
| 94 | + onMessageEntered: async (e) => { |
25 | 95 | const instance = e.component;
|
26 | 96 | instance.renderMessage(e.message);
|
| 97 | + instance.option({ typingUsers: [assistant] }); |
| 98 | + const userInput = e.message.text; |
27 | 99 |
|
28 |
| - const formData = new FormData(); |
29 |
| - formData.append('text', e.message.text); |
30 |
| - formData.append('chatId', model.chatId); |
31 |
| - fetch(`/AI/GetAnswer`, { |
32 |
| - method: 'POST', |
33 |
| - body: formData |
34 |
| - }).then((x) => { |
35 |
| - x.text().then((res) => { |
36 |
| - instance.renderMessage({ |
37 |
| - text: res, |
38 |
| - author: { id: 'Assistant' } |
39 |
| - }, { id: 'Assistant' }); |
40 |
| - }); |
41 |
| - }); |
| 100 | + var response = await getAIResponse(userInput, assistant.id); |
| 101 | + RenderAssistantMessage(instance, response); |
42 | 102 | }
|
43 | 103 | };
|
| 104 | + |
44 | 105 | return new DevExpress.Analytics.Utils.TabInfo({
|
45 | 106 | text: 'AI Assistant',
|
46 | 107 | template: 'dxrd-ai-panel',
|
|
0 commit comments