Skip to content

Commit 7c0590a

Browse files
author
Jicheng Lu
committed
build basic chat image
1 parent 1cfcef6 commit 7c0590a

File tree

7 files changed

+72
-19
lines changed

7 files changed

+72
-19
lines changed

src/lib/common/FileGallery.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
{#each files as file, idx (idx)}
5454
<GalleryImage title={file.file_name}>
5555
<img src={file.file_data} alt={''} />
56+
{#if !!file.file_name}
5657
<div class="item-text">{file.file_name}</div>
58+
{/if}
5759
</GalleryImage>
5860
{/each}
5961
</LightboxGallery>

src/lib/helpers/http.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,20 @@ axios.interceptors.response.use(
5454

5555
/** @param {import('axios').InternalAxiosRequestConfig<any>} config */
5656
function skipLoader(config) {
57-
let regex = new RegExp('http(s*)://(.*?)/conversation/(.*?)/(.*?)', 'g');
58-
if (config.method === 'post' && !!config.data && regex.test(config.url || '')) {
57+
const postRegexes = [
58+
new RegExp('http(s*)://(.*?)/conversation/(.*?)/(.*?)', 'g')
59+
];
60+
61+
const getRegexes = [
62+
new RegExp('http(s*)://(.*?)/address/options(.*?)', 'g'),
63+
new RegExp('http(s*)://(.*?)/conversation/(.*?)/files/(.*?)', 'g')
64+
];
65+
66+
if (config.method === 'post' && !!config.data && postRegexes.some(regex => regex.test(config.url || ''))) {
5967
return true;
6068
}
6169

62-
regex = new RegExp('http(s*)://(.*?)/address/options(.*?)', 'g');
63-
if (config.method === 'get' && regex.test(config.url || '')) {
70+
if (config.method === 'get' && getRegexes.some(regex => regex.test(config.url || ''))) {
6471
return true;
6572
}
6673

src/lib/scss/custom/components/_file.scss

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ $file-screen-max-width: 500px;
1515

1616
.gallery-item-wrapper {
1717
position: relative;
18+
width: 100%;
19+
height: 100%;
1820

1921
.gallery-item-icon {
2022
position: absolute;
21-
color: white;
23+
color: red;
2224
cursor: pointer;
23-
z-index: 1000;
25+
z-index: 500;
2426
right: 3px;
2527
font-size: 1.5em;
2628
}

src/lib/services/api-endpoints.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const endpoints = {
4141
conversationCountUrl: `${host}/conversations/count`,
4242
conversationDeletionUrl: `${host}/conversation/{conversationId}`,
4343
conversationDetailUrl: `${host}/conversation/{conversationId}`,
44+
conversationAttachmentUrl: `${host}/conversation/{conversationId}/files/{messageId}`,
4445
dialogsUrl: `${host}/conversation/{conversationId}/dialogs`,
4546
conversationMessageDeletionUrl: `${host}/conversation/{conversationId}/message/{messageId}`,
4647

src/lib/services/conversation-service.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ export async function getConversations(filter) {
3737
return response.data;
3838
}
3939

40+
/**
41+
* Get conversation files
42+
* @param {string} conversationId
43+
* @param {string} messageId
44+
*/
45+
export async function getConversationFiles(conversationId, messageId) {
46+
const url = replaceUrl(endpoints.conversationAttachmentUrl, { conversationId: conversationId, messageId: messageId });
47+
const response = await axios.get(url);
48+
return response?.data;
49+
}
50+
4051
/**
4152
* Delete conversation
4253
* @param {string} conversationId

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626
import { replaceNewLine } from '$lib/helpers/http';
2727
import { EditorType, SenderAction, UserRole } from '$lib/helpers/enums';
2828
import RichContent from './richContent/rich-content.svelte';
29+
import RcDisclaimer from './richContent/rc-disclaimer.svelte';
30+
import ChatImage from './chatImage/chat-image.svelte';
2931
import ContentLog from './contentLogs/content-log.svelte';
3032
import _ from "lodash";
3133
import { Pane, Splitpanes } from 'svelte-splitpanes';
3234
import StateLog from './stateLogs/state-log.svelte';
3335
import Swal from 'sweetalert2/dist/sweetalert2.js';
3436
import "sweetalert2/src/sweetalert2.scss";
3537
import moment from 'moment';
36-
import RcDisclaimer from './richContent/rc-disclaimer.svelte';
37-
3838
3939
const options = {
4040
scrollbars: {
@@ -384,14 +384,16 @@
384384
if (!!!data?.truncateMsgId) {
385385
files = getChatFiles();
386386
}
387-
resetStorage();
387+
// resetStorage();
388388
389389
return new Promise((resolve, reject) => {
390390
sendMessageToHub(params.agentId, params.conversationId, msgText, messageData, files).then(res => {
391391
isSendingMsg = false;
392+
resetStorage();
392393
resolve(res);
393394
}).catch(err => {
394395
isSendingMsg = false;
396+
resetStorage();
395397
reject(err);
396398
});
397399
});
@@ -956,6 +958,9 @@
956958
disabled={isSendingMsg || isThinking}
957959
onConfirm={confirmSelectedOption}
958960
/>
961+
{#if message.rich_content?.editor === EditorType.File && message.message_id != lastBotMsg?.message_id}
962+
<ChatImage conversationId={params.conversationId} messageId={message.message_id} />
963+
{/if}
959964
</div>
960965
{/if}
961966
</div>
Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
<script>
2-
import { Lightbox } from 'svelte-lightbox';
2+
import { onMount } from 'svelte';
3+
import FileGallery from '$lib/common/FileGallery.svelte';
4+
import { getUserStore } from '$lib/helpers/store';
5+
import { getConversationFiles } from '$lib/services/conversation-service';
6+
7+
/** @type {string} */
8+
export let conversationId;
9+
/** @type {string} */
10+
export let messageId;
311
4-
/** @type {any} */
5-
export let message;
12+
/** @type {any[]} */
13+
let files = [];
14+
/** @type {string} */
15+
let token = "";
16+
17+
onMount(() => {
18+
const user = getUserStore();
19+
token = user.token;
20+
});
21+
22+
$: {
23+
getConversationFiles(conversationId, messageId).then(data => {
24+
// @ts-ignore
25+
files = data?.filter(item => !!item.file_url)?.map(item => {
26+
return {
27+
...item,
28+
file_data: `${item.file_url}?token=${token}`
29+
};
30+
}) || [];
31+
});
32+
}
633
</script>
734
8-
{#if message?.data && typeof message.data === 'string' && message.data.startsWith('data:image/png;base64,')}
9-
<div class="image-wrap">
10-
<Lightbox transitionDuration={100}>
11-
<img src={message.data} alt={''} class="rounded img-fluid" />
12-
</Lightbox>
13-
</div>
14-
{/if}
35+
<div style="display: block; margin-top: 3px;">
36+
<div style="display: flex; flex-wrap: wrap; gap: 3px;">
37+
<FileGallery files={files} />
38+
</div>
39+
</div>

0 commit comments

Comments
 (0)