Skip to content

Commit 21f65d7

Browse files
authored
Merge pull request #159 from iceljc/features/refine-chat-window
refine message file loading
2 parents f335003 + 0884808 commit 21f65d7

File tree

7 files changed

+81
-64
lines changed

7 files changed

+81
-64
lines changed

src/lib/helpers/types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ IRichContent.prototype.quick_replies;
318318
* @property {string} post_action_disclaimer - The message disclaimer.
319319
* @property {string} data - The message data.
320320
* @property {Date} created_at - The message sent time.
321+
* @property {boolean} has_message_files
322+
* @property {boolean} is_chat_message
321323
*/
322324

323325
/**

src/lib/services/agent-service.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ export async function getSettings() {
1414
/**
1515
* Get agent list
1616
* @param {import('$types').AgentFilter} filter
17+
* @param {boolean} useHook
1718
* @returns {Promise<import('$types').PagedItems<import('$types').AgentModel>>}
1819
*/
19-
export async function getAgents(filter) {
20+
export async function getAgents(filter, useHook = false) {
2021
let url = endpoints.agentListUrl;
2122
const response = await axios.get(url, {
22-
params: filter,
23+
params: {
24+
...filter,
25+
useHook: useHook || false
26+
},
2327
paramsSerializer: {
2428
dots: true,
2529
indexes: null,

src/routes/chat/[agentId]/[conversationId]/chat-box.svelte

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -312,21 +312,30 @@
312312
313313
/** @param {import('$types').ChatResponseModel} message */
314314
function onMessageReceivedFromClient(message) {
315-
dialogs.push(message);
315+
dialogs.push({
316+
...message,
317+
is_chat_message: true
318+
});
316319
refresh();
317320
text = "";
318321
}
319322
320323
/** @param {import('$types').ChatResponseModel} message */
321324
function onMessageReceivedFromCsr(message) {
322-
dialogs.push(message);
325+
dialogs.push({
326+
...message,
327+
is_chat_message: true
328+
});
323329
refresh();
324330
}
325331
326332
/** @param {import('$types').ChatResponseModel} message */
327333
function onMessageReceivedFromAssistant(message) {
328334
// webSpeech.utter(message.text);
329-
dialogs.push(message);
335+
dialogs.push({
336+
...message,
337+
is_chat_message: true
338+
});
330339
refresh();
331340
}
332341
@@ -997,10 +1006,12 @@
9971006
{#if !!message.post_action_disclaimer}
9981007
<RcDisclaimer content={message.post_action_disclaimer} />
9991008
{/if}
1000-
<MessageImageGallery
1001-
galleryStyles={'justify-content: flex-end;'}
1002-
fetchFiles={() => getConversationFiles(params.conversationId, message.message_id, FileSourceType.User)}
1003-
/>
1009+
{#if !!message.is_chat_message || !!message.has_message_files}
1010+
<MessageImageGallery
1011+
galleryStyles={'justify-content: flex-end;'}
1012+
fetchFiles={() => getConversationFiles(params.conversationId, message.message_id, FileSourceType.User)}
1013+
/>
1014+
{/if}
10041015
</div>
10051016
{#if !isLite}
10061017
<Dropdown>
@@ -1024,10 +1035,12 @@
10241035
</div>
10251036
<div class="msg-container">
10261037
<RcMessage message={message} />
1027-
<MessageImageGallery
1028-
galleryStyles={'justify-content: flex-start;'}
1029-
fetchFiles={() => getConversationFiles(params.conversationId, message.message_id, FileSourceType.Bot)}
1030-
/>
1038+
{#if !!message.is_chat_message || !!message.has_message_files}
1039+
<MessageImageGallery
1040+
galleryStyles={'justify-content: flex-start;'}
1041+
fetchFiles={() => getConversationFiles(params.conversationId, message.message_id, FileSourceType.Bot)}
1042+
/>
1043+
{/if}
10311044
</div>
10321045
{/if}
10331046
</div>

src/routes/page/agent/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
4545
function getPagedAgents() {
4646
isLoading = true;
47-
getAgents(filter).then(data => {
47+
getAgents(filter, true).then(data => {
4848
agents = data;
4949
}).catch(() => {
5050
agents = { items: [], count: 0 };

src/routes/page/agent/router/routing-flow.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
const dispatch = createEventDispatcher();
3131
3232
onMount(async () => {
33-
const response = await getAgents(filter);
33+
const response = await getAgents(filter, true);
3434
agents = response?.items || [];
3535
3636
const container = document.getElementById("drawflow");
@@ -185,7 +185,7 @@
185185
includeTaskAgent = !includeTaskAgent;
186186
filter.type = includeTaskAgent ? "task" : "none";
187187
filter.type += includeStaticAgent ? ",static" : ",none";
188-
const response = await getAgents(filter);
188+
const response = await getAgents(filter, true);
189189
agents = response?.items || [];
190190
renderRoutingFlow();
191191
}
@@ -194,7 +194,7 @@
194194
includeStaticAgent = !includeStaticAgent;
195195
filter.type = includeTaskAgent ? "task" : "none";
196196
filter.type += includeStaticAgent ? ",static" : ",none";
197-
const response = await getAgents(filter);
197+
const response = await getAgents(filter, true);
198198
agents = response?.items || [];
199199
renderRoutingFlow();
200200
}

src/routes/page/conversation/+page.svelte

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,23 @@
7070
};
7171
7272
onMount(async () => {
73-
await loadAgentOptions();
74-
loadSearchOption();
75-
loadConversations();
73+
isLoading = true;
74+
Promise.all([
75+
loadAgentOptions(),
76+
loadSearchOption(),
77+
loadConversations()])
78+
.finally(() => {
79+
isLoading = false
80+
});
7681
});
7782
7883
function loadConversations() {
79-
isLoading = true;
80-
getPagedConversations().then(res => {
81-
isLoading = false;
82-
}).catch(error => {
83-
isLoading = false;
84-
isError = true;
84+
return new Promise((resolve, reject) => {
85+
getPagedConversations().then(res => {
86+
resolve(res);
87+
}).catch((error) => {
88+
reject(error);
89+
});
8590
});
8691
}
8792
@@ -90,14 +95,21 @@
9095
refresh();
9196
}
9297
93-
async function loadAgentOptions() {
94-
const agents = await getAgents({ pager: { page: 1, size: 100, count: 0 } });
95-
agentOptions = agents?.items?.map(x => {
96-
return {
97-
id: x.id,
98-
name: x.name
99-
};
100-
})?.sort((a, b) => a.name.localeCompare(b.name)) || [];
98+
function loadAgentOptions() {
99+
return new Promise((resolve, reject) => {
100+
getAgents({ pager: { page: 1, size: 100, count: 0 } }).then(res => {
101+
agentOptions = res?.items?.map(x => {
102+
return {
103+
id: x.id,
104+
name: x.name
105+
};
106+
})?.sort((a, b) => a.name.localeCompare(b.name)) || [];
107+
resolve(agentOptions);
108+
}).catch((error) => {
109+
agentOptions = [];
110+
reject(error);
111+
});
112+
});
101113
}
102114
103115
function refresh() {
@@ -209,13 +221,16 @@
209221
}
210222
211223
function loadSearchOption() {
212-
const savedOption = conversationSearchOptionStore.get();
213-
searchOption = {
214-
...searchOption,
215-
...savedOption
216-
};
217-
refreshFilter();
218-
handleSearchStates();
224+
return new Promise((resolve, reject) => {
225+
const savedOption = conversationSearchOptionStore.get();
226+
searchOption = {
227+
...searchOption,
228+
...savedOption
229+
};
230+
refreshFilter();
231+
handleSearchStates();
232+
resolve(searchOption);
233+
});
219234
}
220235
221236
function refreshFilter() {

src/routes/page/conversation/[conversationId]/conv-dialogs.svelte

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,8 @@
1919
2020
onMount(async () => {
2121
dialogs = await GetDialogs(conversation.id);
22-
loadMessageImages(dialogs);
2322
});
2423
25-
/** @param {import('$types').ChatResponseModel[]} dialogs */
26-
function loadMessageImages(dialogs) {
27-
if (!!!dialogs) return;
28-
29-
for (let idx = 0; idx < dialogs.length; idx++) {
30-
const curMsg = dialogs[idx];
31-
if (!USER_SENDERS.includes(curMsg?.sender?.role || '')) {
32-
continue;
33-
}
34-
35-
const prevMsg = dialogs[idx-1];
36-
if (!!!prevMsg || BOT_SENDERS.includes(prevMsg?.sender?.role || '')
37-
&& loadFileGallery(prevMsg)) {
38-
curMsg.is_load_images = true;
39-
}
40-
}
41-
}
42-
4324
/**
4425
* @param {import('$types').ChatResponseModel} dialog
4526
* @returns {boolean}
@@ -89,10 +70,12 @@
8970
<p class="fw-bold">
9071
<Markdown text={dialog?.rich_content?.message?.text || dialog?.text} />
9172
</p>
92-
<MessageImageGallery
93-
galleryClasses={'dialog-file-display'}
94-
fetchFiles={() => getConversationFiles(conversation.id, dialog.message_id, showInRight(dialog) ? FileSourceType.User : FileSourceType.Bot)}
95-
/>
73+
{#if !!dialog.has_message_files}
74+
<MessageImageGallery
75+
galleryClasses={'dialog-file-display'}
76+
fetchFiles={() => getConversationFiles(conversation.id, dialog.message_id, showInRight(dialog) ? FileSourceType.User : FileSourceType.Bot)}
77+
/>
78+
{/if}
9679
</div>
9780
{#if dialog.message_id}
9881
<div>

0 commit comments

Comments
 (0)