Skip to content

Commit e2f2a57

Browse files
feat: New API to control the visibility of the assistant's chat history
1 parent 61f06e3 commit e2f2a57

File tree

5 files changed

+99
-12
lines changed

5 files changed

+99
-12
lines changed

frontend/public/assistant.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,16 @@
6767
</div>`
6868

6969
const getChatContainerHtml = (data) => {
70+
let srcUrl = `${data.domain_url}/#/assistant?id=${data.id}&online=${!!data.online}&name=${encodeURIComponent(data.name)}&userFlag=${data.userFlag || ''}`
71+
if (data.userFlag) {
72+
srcUrl += `&userFlag=${data.userFlag || ''}`
73+
}
74+
if (data.history) {
75+
srcUrl += `&userFlag=${data.history}`
76+
}
7077
return `
7178
<div id="sqlbot-assistant-chat-container">
72-
<iframe id="sqlbot-assistant-chat-iframe-${data.id}" allow="microphone;clipboard-read 'src'; clipboard-write 'src'" src="${data.domain_url}/#/assistant?id=${data.id}&online=${!!data.online}&name=${encodeURIComponent(data.name)}&userFlag=${data.userFlag || ''}"></iframe>
79+
<iframe id="sqlbot-assistant-chat-iframe-${data.id}" allow="microphone;clipboard-read 'src'; clipboard-write 'src'" src="${srcUrl}"></iframe>
7380
<div class="sqlbot-assistant-operate">
7481
<div class="sqlbot-assistant-closeviewport sqlbot-assistant-viewportnone">
7582
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="none">
@@ -499,6 +506,7 @@
499506
const domain_url = getDomain(src)
500507
const online = getParam(src, 'online')
501508
const userFlag = getParam(src, 'userFlag')
509+
const history = getParam(src, 'history')
502510
let url = `${domain_url}/api/v1/system/assistant/info/${id}`
503511
if (domain_url.includes('5173')) {
504512
url = url.replace('5173', '8000')
@@ -536,6 +544,7 @@
536544

537545
tempData['online'] = online && online.toString().toLowerCase() == 'true'
538546
tempData['userFlag'] = userFlag
547+
tempData['history'] = history
539548
initsqlbot_assistant(tempData)
540549
if (data.type == 1) {
541550
registerMessageEvent(id, tempData)
@@ -750,6 +759,39 @@
750759
}
751760
delete window.sqlbot_assistant_handler[id]
752761
}
762+
window.sqlbot_assistant_handler[id]['setHistory'] = (show) => {
763+
if (show != null && typeof show != 'boolean') {
764+
throw new Error('The parameter can only be of type boolean')
765+
}
766+
const iframe = document.getElementById(`sqlbot-assistant-chat-iframe-${id}`)
767+
if (iframe) {
768+
const url = iframe.src
769+
const eventName = 'sqlbot_assistant_event'
770+
const params = {
771+
busi: 'setHistory',
772+
show,
773+
eventName,
774+
messageId: id,
775+
}
776+
const contentWindow = iframe.contentWindow
777+
contentWindow.postMessage(params, url)
778+
}
779+
}
780+
window.sqlbot_assistant_handler[id]['createConversation'] = (param) => {
781+
const iframe = document.getElementById(`sqlbot-assistant-chat-iframe-${id}`)
782+
if (iframe) {
783+
const url = iframe.src
784+
const eventName = 'sqlbot_assistant_event'
785+
const params = {
786+
busi: 'createConversation',
787+
param,
788+
eventName,
789+
messageId: id,
790+
}
791+
const contentWindow = iframe.contentWindow
792+
contentWindow.postMessage(params, url)
793+
}
794+
}
753795
}
754796
// window.addEventListener('load', init)
755797
const executeWhenReady = (fn) => {

frontend/src/stores/assistant.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ interface AssistantState {
2020
certificate: string
2121
online: boolean
2222
pageEmbedded?: boolean
23+
history: boolean
2324
requestPromiseMap: Map<string, PendingRequest>
2425
}
2526

@@ -34,6 +35,7 @@ export const AssistantStore = defineStore('assistant', {
3435
certificate: '',
3536
online: false,
3637
pageEmbedded: false,
38+
history: true,
3739
requestPromiseMap: new Map<string, PendingRequest>(),
3840
}
3941
},
@@ -59,6 +61,9 @@ export const AssistantStore = defineStore('assistant', {
5961
getOnline(): boolean {
6062
return this.online
6163
},
64+
getHistory(): boolean {
65+
return this.history
66+
},
6267
getPageEmbedded(): boolean {
6368
return this.pageEmbedded || false
6469
},
@@ -130,6 +135,9 @@ export const AssistantStore = defineStore('assistant', {
130135
setOnline(online: boolean) {
131136
this.online = !!online
132137
},
138+
setHistory(history: boolean) {
139+
this.history = history ?? true
140+
},
133141
async setChat() {
134142
if (!this.assistant) {
135143
return null

frontend/src/views/chat/index.vue

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
popper-class="popover-chat_history popover-chat_history_small"
77
>
88
<template #reference>
9-
<el-icon class="show-history_icon" style="" size="20" @click="showFloatPopover">
9+
<el-icon
10+
class="show-history_icon"
11+
:class="{ 'embedded-history-hidden': embeddedHistoryHidden }"
12+
style=""
13+
size="20"
14+
@click="showFloatPopover"
15+
>
1016
<icon_sidebar_outlined></icon_sidebar_outlined>
1117
</el-icon>
1218
</template>
@@ -17,7 +23,7 @@
1723
v-model:current-chat="currentChat"
1824
v-model:loading="loading"
1925
in-popover
20-
:appName="customName"
26+
:app-name="customName"
2127
@go-empty="goEmpty"
2228
@on-chat-created="onChatCreated"
2329
@on-click-history="onClickHistory"
@@ -30,14 +36,15 @@
3036
<el-aside
3137
v-if="(isCompletePage || pageEmbedded) && chatListSideBarShow"
3238
class="chat-container-left"
39+
:class="{ 'embedded-history-hidden': embeddedHistoryHidden }"
3340
>
3441
<ChatListContainer
3542
v-model:chat-list="chatList"
3643
v-model:current-chat-id="currentChatId"
3744
v-model:current-chat="currentChat"
3845
v-model:loading="loading"
3946
:in-popover="!chatListSideBarShow"
40-
:appName="customName"
47+
:app-name="customName"
4148
@go-empty="goEmpty"
4249
@on-chat-created="onChatCreated"
4350
@on-click-history="onClickHistory"
@@ -50,7 +57,10 @@
5057
<div
5158
v-if="(!isCompletePage && !pageEmbedded) || !chatListSideBarShow"
5259
class="hidden-sidebar-btn"
53-
:class="{ 'assistant-popover-sidebar': !isCompletePage && !pageEmbedded }"
60+
:class="{
61+
'assistant-popover-sidebar': !isCompletePage && !pageEmbedded,
62+
'embedded-history-hidden': embeddedHistoryHidden,
63+
}"
5464
>
5565
<el-popover
5666
:width="280"
@@ -72,7 +82,7 @@
7282
v-model:current-chat="currentChat"
7383
v-model:loading="loading"
7484
:in-popover="!chatListSideBarShow"
75-
:appName="customName"
85+
:app-name="customName"
7686
@go-empty="goEmpty"
7787
@on-chat-created="onChatCreated"
7888
@on-click-history="onClickHistory"
@@ -97,7 +107,7 @@
97107
v-model:current-chat-id="currentChatId"
98108
v-model:current-chat="currentChat"
99109
v-model:loading="loading"
100-
:appName="customName"
110+
:app-name="customName"
101111
:in-popover="false"
102112
@go-empty="goEmpty"
103113
@on-chat-created="onChatCreated"
@@ -199,7 +209,7 @@
199209
:first-chat="message.first_chat"
200210
@click-question="quickAsk"
201211
@stop="onChatStop"
202-
@loadingOver="loadingOver"
212+
@loading-over="loadingOver"
203213
/>
204214
<UserChat v-if="message.role === 'user'" :message="message" />
205215
<template v-if="message.role === 'assistant' && !message.first_chat">
@@ -216,8 +226,8 @@
216226
:current-chat-id="currentChatId"
217227
:loading="isTyping"
218228
:message="message"
219-
@scrollBottom="scrollToBottom"
220229
:reasoning-name="['sql_answer', 'chart_answer']"
230+
@scroll-bottom="scrollToBottom"
221231
@finish="onChartAnswerFinish"
222232
@error="onChartAnswerError"
223233
@stop="onChatStop"
@@ -292,7 +302,7 @@
292302
:first-chat="message.first_chat"
293303
:disabled="isTyping"
294304
@click-question="quickAsk"
295-
@loadingOver="loadingOver"
305+
@loading-over="loadingOver"
296306
@stop="onChatStop"
297307
/>
298308
</template>
@@ -328,7 +338,7 @@
328338
:current-chat-id="currentChatId"
329339
:loading="isTyping"
330340
:message="message"
331-
@scrollBottom="scrollToBottom"
341+
@scroll-bottom="scrollToBottom"
332342
@finish="onPredictAnswerFinish"
333343
@error="onPredictAnswerError"
334344
@stop="onChatStop"
@@ -446,6 +456,9 @@ const defaultFloatPopoverStyle = ref({
446456
})
447457
448458
const isCompletePage = computed(() => !assistantStore.getAssistant || assistantStore.getEmbedded)
459+
const embeddedHistoryHidden = computed(
460+
() => assistantStore.getAssistant && !assistantStore.getHistory
461+
)
449462
const customName = computed(() => {
450463
if (!isCompletePage.value && props.pageEmbedded) return props.appName
451464
return ''
@@ -1321,7 +1334,9 @@ onMounted(() => {
13211334
border: 1px solid rgba(222, 224, 227, 1);
13221335
border-radius: 6px;
13231336
}
1324-
1337+
.embedded-history-hidden {
1338+
display: none !important;
1339+
}
13251340
.show-history_icon {
13261341
cursor: pointer;
13271342
position: absolute;

frontend/src/views/embedded/index.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ const communicationCb = async (event: any) => {
7979
if (event.data?.busi == 'setOnline') {
8080
setFormatOnline(event.data.online)
8181
}
82+
if (event.data?.busi == 'setHistory') {
83+
assistantStore.setHistory(event.data.show ?? true)
84+
}
85+
if (event.data?.busi == 'createConversation') {
86+
createChat()
87+
}
8288
}
8389
}
8490
const setFormatOnline = (text?: any) => {
@@ -141,6 +147,10 @@ onBeforeMount(async () => {
141147
if (userFlag && userFlag === '1') {
142148
userFlag = '100001'
143149
}
150+
151+
const history: boolean = route.query.history !== 'false'
152+
assistantStore.setHistory(history)
153+
144154
const now = Date.now()
145155
assistantStore.setFlag(now)
146156
assistantStore.setId(assistantId?.toString() || '')

frontend/src/views/embedded/page.vue

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,17 @@ const communicationCb = async (event: any) => {
6969
if (event.data?.busi == 'setOnline') {
7070
setFormatOnline(event.data.online)
7171
}
72+
if (event.data?.busi == 'setHistory') {
73+
assistantStore.setHistory(event.data.show ?? true)
74+
}
75+
if (event.data?.busi == 'createConversation') {
76+
createChat()
77+
}
7278
}
7379
}
80+
const createChat = () => {
81+
chatRef.value?.createNewChat()
82+
}
7483
const setFormatOnline = (text?: any) => {
7584
if (text === null || typeof text === 'undefined') {
7685
assistantStore.setOnline(false)
@@ -119,6 +128,9 @@ onBeforeMount(async () => {
119128
const online = route.query.online
120129
setFormatOnline(online)
121130
131+
const history: boolean = route.query.history !== 'false'
132+
assistantStore.setHistory(history)
133+
122134
let name = route.query.name
123135
if (name) {
124136
assistantName.value = decodeURIComponent(name.toString())

0 commit comments

Comments
 (0)