Skip to content

Commit 2cb9658

Browse files
authored
Merge pull request #5 from DevExpress-Examples/24.2_fix-viewer-assistant-document-loading
24.2 Add error handling, fix viewer assistant creation
2 parents 5cd640f + b0491a8 commit 2cb9658

File tree

4 files changed

+45
-16
lines changed

4 files changed

+45
-16
lines changed

CS/ReportingApp/ReportingApp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<Exec Command="npm install" />
4848
</Target>
4949
<ItemGroup>
50-
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0-beta.2" />
50+
<PackageReference Include="Azure.AI.OpenAI" Version="2.2.0-beta.2" />
5151
<PackageReference Include="Azure.AI.OpenAI.Assistants" Version="1.0.0-beta.4" />
5252
<PackageReference Include="DevExpress.AIIntegration.OpenAI" Version="24.2.*-*" />
5353
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.11" />

CS/ReportingApp/Views/Home/DocumentViewer.cshtml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
async function BeforeRender(sender, args) {
66
const previewModel = args;
77
const reportPreview = previewModel.reportPreview;
8-
const result = await fetch(`/AI/CreateUserAssistant`);
9-
const chatId = await result.text();
10-
aiTab = createAssistantTab(chatId);
8+
aiTab = createAssistantTab();
119
const model = aiTab.model;
1210
previewModel.tabPanel.tabs.push(aiTab);
1311
reportPreview.events.on('documentBuildingChanged', (args) => {

CS/ReportingApp/wwwroot/js/aiIntegration.js

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const createAssistantTab = (function() {
1+
const createAssistantTab = (function() {
22

33
let lastUserQuery;
4-
4+
let errorList = [];
55
const assistant = {
66
id: 'assistant',
77
name: 'Virtual Assistant',
@@ -11,6 +11,28 @@
1111
id: 'user',
1212
};
1313

14+
15+
async function _tryFetch(instance, fetchAction, message) {
16+
try {
17+
return await fetchAction();
18+
} catch(error) {
19+
_handleError(instance, { message: error.message, code: message });
20+
}
21+
}
22+
23+
function _handleError(instance, error) {
24+
const id = "id" + Math.random().toString(16).slice(2)
25+
setTimeout(() => {
26+
errorList = errorList.filter(err => err.id !== id);
27+
instance.option('alerts', errorList);
28+
}, 10000);
29+
errorList.push({
30+
id: id,
31+
message: `${error.code} - ${error.message}`
32+
});
33+
instance.option('alerts', errorList);
34+
}
35+
1436
function normalizeAIResponse(text) {
1537
text = text.replace(/\d+:\d+[^\】]+/g, "");
1638
let html = marked.parse(text);
@@ -23,16 +45,23 @@
2345
navigator.clipboard.writeText(text);
2446
}
2547

26-
async function getAIResponse(text, id) {
48+
async function getAIResponse(instance, text, id) {
2749
const formData = new FormData();
2850
formData.append('text', text);
2951
formData.append('chatId', id);
3052
lastUserQuery = text;
31-
const response = await fetch(`/AI/GetAnswer`, {
32-
method: 'POST',
33-
body: formData
34-
});
35-
return await response.text();
53+
return _tryFetch(instance, async () => {
54+
const response = await fetch('/AI/GetAnswer', {
55+
method: 'POST',
56+
body: formData
57+
});
58+
59+
if(!response.ok) {
60+
_handleError(instance, { code: `${response.status}`, message: `Internal server error` });
61+
return;
62+
}
63+
return await response.text();
64+
}, 'GetAnswer');
3665
}
3766

3867
function RenderAssistantMessage(instance, message) {
@@ -45,14 +74,15 @@
4574
const newItems = items.slice(0, -1);
4675
instance.option({ items: newItems });
4776
instance.option({ typingUsers: [assistant] });
48-
const aiResponse = await getAIResponse(lastUserQuery, assistant.id);
77+
const aiResponse = await getAIResponse(instance, lastUserQuery, assistant.id);
4978
setTimeout(() => {
5079
instance.option({ typingUsers: [] });
5180
RenderAssistantMessage(instance, aiResponse);
5281
}, 200);
5382
}
5483

5584
function createAssistantTab(chatId) {
85+
let lastRefreshButton;
5686
assistant.id = chatId;
5787
const model = {
5888
title: 'AI Assistant',
@@ -72,6 +102,7 @@
72102

73103
const buttonContainer = document.createElement('div');
74104
buttonContainer.classList.add('dx-bubble-button-container');
105+
lastRefreshButton?.remove();
75106
const copyBtnElement = document.createElement('div');
76107
new DevExpress.ui.dxButton(copyBtnElement, {
77108
icon: 'copy',
@@ -86,15 +117,16 @@
86117
onClick: () => refreshAnswer(data.component)
87118
});
88119
buttonContainer.appendChild(refreshBtnElement);
120+
lastRefreshButton = refreshBtnElement;
89121
container.appendChild(buttonContainer);
90122
},
91123
onMessageEntered: async (e) => {
92124
const instance = e.component;
125+
instance.option('alerts', []);
93126
instance.renderMessage(e.message);
94127
instance.option({ typingUsers: [assistant] });
95128
const userInput = e.message.text;
96-
97-
var response = await getAIResponse(userInput, assistant.id);
129+
const response = await getAIResponse(instance, userInput, assistant.id ?? model.chatId);
98130
RenderAssistantMessage(instance, response);
99131
}
100132
};

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<!-- default badges list -->
2-
![](https://img.shields.io/endpoint?url=https://codecentral.devexpress.com/api/v1/VersionRange/853003889/24.2.2%2B)
32
[![](https://img.shields.io/badge/Open_in_DevExpress_Support_Center-FF7200?style=flat-square&logo=DevExpress&logoColor=white)](https://supportcenter.devexpress.com/ticket/details/T1252182)
43
[![](https://img.shields.io/badge/📖_How_to_use_DevExpress_Examples-e9f6fc?style=flat-square)](https://docs.devexpress.com/GeneralInformation/403183)
54
[![](https://img.shields.io/badge/💬_Leave_Feedback-feecdd?style=flat-square)](#does-this-example-address-your-development-requirementsobjectives)

0 commit comments

Comments
 (0)