Skip to content

Commit bc4a37a

Browse files
Merge branch '24.2.6+' into 24.2_fix-refresh-button-rendering
2 parents 5a57762 + bcac321 commit bc4a37a

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

CS/ReportingApp/ReportingApp.csproj

+1-1
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.2.0-beta.1" />
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.6" />
5353
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.11" />

CS/ReportingApp/Views/Home/DocumentViewer.cshtml

+1-3
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

+41-9
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',
@@ -94,6 +125,7 @@
94125
onMessageEntered: async (e) => {
95126
lastRefreshButton?.remove();
96127
const instance = e.component;
128+
instance.option('alerts', []);
97129
instance.renderMessage(e.message);
98130
instance.option({ typingUsers: [assistant] });
99131
const userInput = e.message.text;

0 commit comments

Comments
 (0)