Skip to content

Commit 4550b41

Browse files
perf: Assistant message
1 parent 2d13983 commit 4550b41

File tree

3 files changed

+123
-3
lines changed

3 files changed

+123
-3
lines changed

frontend/public/assistant.js

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
const getChatContainerHtml = (data) => {
4141
return `
4242
<div id="sqlbot-assistant-chat-container">
43-
<iframe id="sqlbot-assistant-chat" allow="microphone" src="${data.domain_url}/#/assistant?id=${data.id}"></iframe>
43+
<iframe id="sqlbot-assistant-chat-iframe-${data.id}" allow="microphone" src="${data.domain_url}/#/assistant?id=${data.id}"></iframe>
4444
<div class="sqlbot-assistant-operate">
4545
<div class="sqlbot-assistant-closeviewport sqlbot-assistant-viewportnone">
4646
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="none">
@@ -311,7 +311,7 @@
311311
#sqlbot-assistant #sqlbot-assistant-chat-container .sqlbot-assistant-viewportnone{
312312
display:none;
313313
}
314-
#sqlbot-assistant #sqlbot-assistant-chat-container #sqlbot-assistant-chat{
314+
#sqlbot-assistant #sqlbot-assistant-chat-container #sqlbot-assistant-chat-iframe-${data.id}{
315315
height:100%;
316316
width:100%;
317317
border: none;
@@ -334,7 +334,79 @@
334334
const url = new URL(src)
335335
return url.searchParams.get(key)
336336
}
337+
function parsrCertificate(config) {
338+
const certificateList = config.certificate
339+
if (!certificateList?.length) {
340+
return null
341+
}
342+
const list = certificateList.map((item) => formatCertificate(item)).filter((item) => !!item)
343+
return JSON.stringify(list)
344+
}
345+
function isEmpty(obj) {
346+
return obj == null || typeof obj == 'undefined'
347+
}
348+
function formatCertificate(item) {
349+
const { type, source, target, target_key, target_val } = item
350+
let source_val = null
351+
if (type.toLocaleLowerCase() == 'localstorage') {
352+
source_val = localStorage.getItem(source)
353+
}
354+
if (type.toLocaleLowerCase() == 'sessionstorage') {
355+
source_val = sessionStorage.getItem(source)
356+
}
357+
if (type.toLocaleLowerCase() == 'cookie') {
358+
source_val = getCookie(source)
359+
}
360+
if (type.toLocaleLowerCase() == 'custom') {
361+
source_val = source
362+
}
363+
if (isEmpty(source_val)) {
364+
return null
365+
}
366+
return {
367+
target,
368+
key: target_key || source,
369+
value: (target_val && eval(target_val)) || source_val,
370+
}
371+
}
372+
function getCookie(key) {
373+
if (!key || !document.cookie) {
374+
return null
375+
}
376+
const cookies = document.cookie.split(';')
377+
for (let i = 0; i < cookies.length; i++) {
378+
const cookie = cookies[i].trim()
337379

380+
if (cookie.startsWith(key + '=')) {
381+
return decodeURIComponent(cookie.substring(key.length + 1))
382+
}
383+
}
384+
return null
385+
}
386+
function registerMessageEvent(id, data) {
387+
const iframe = document.getElementById(`sqlbot-assistant-chat-iframe-${id}`)
388+
const url = iframe.src
389+
const eventName = 'sqlbot_assistant_event'
390+
window.addEventListener('message', (event) => {
391+
if (event.data?.eventName === eventName) {
392+
if (event.data?.messageId !== id) {
393+
return
394+
}
395+
if (event.data?.busi == 'ready' && event.data?.ready) {
396+
const certificate = parsrCertificate(data)
397+
console.log(certificate)
398+
params = {
399+
busi: 'certificate',
400+
certificate,
401+
eventName,
402+
messageId: id,
403+
}
404+
const contentWindow = iframe.contentWindow
405+
contentWindow.postMessage(params, url)
406+
}
407+
}
408+
})
409+
}
338410
function loadScript(src, id) {
339411
const domain_url = getDomain(src)
340412
let url = `${domain_url}/api/v1/system/assistant/info/${id}`
@@ -353,6 +425,10 @@
353425
tempData = Object.assign(tempData, config)
354426
}
355427
initsqlbot_assistant(tempData)
428+
if (data.type == 1) {
429+
registerMessageEvent(id, tempData)
430+
// postMessage the certificate to iframe
431+
}
356432
})
357433
}
358434
function getDomain(src) {

frontend/src/stores/assistant.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ interface AssistantState {
99
token: string
1010
assistant: boolean
1111
flag: number
12+
type: number
13+
certificate: string
1214
}
1315

1416
export const AssistantStore = defineStore('assistant', {
@@ -17,9 +19,14 @@ export const AssistantStore = defineStore('assistant', {
1719
token: '',
1820
assistant: false,
1921
flag: 0,
22+
type: 0,
23+
certificate: '',
2024
}
2125
},
2226
getters: {
27+
getCertificate(): string {
28+
return this.certificate
29+
},
2330
getToken(): string {
2431
return this.token
2532
},
@@ -29,8 +36,17 @@ export const AssistantStore = defineStore('assistant', {
2936
getFlag(): number {
3037
return this.flag
3138
},
39+
getType(): number {
40+
return this.type
41+
},
3242
},
3343
actions: {
44+
setCertificate(certificate: string) {
45+
this.certificate = certificate
46+
},
47+
setType(type: number) {
48+
this.type = type
49+
},
3450
setToken(token: string) {
3551
this.token = token
3652
},

frontend/src/views/embedded/assistant.vue

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
</div>
5858
</template>
5959
<script setup lang="ts">
60-
import { onBeforeMount, ref } from 'vue'
60+
import { onBeforeMount, onBeforeUnmount, ref } from 'vue'
6161
import ChatComponent from '@/views/chat/index.vue'
6262
import AssistantGif from '@/assets/img/assistant.gif'
6363
import history from '@/assets/svg/chart/history.svg'
@@ -101,6 +101,21 @@ const validator = ref({
101101
token: '',
102102
})
103103
const loading = ref(true)
104+
const eventName = 'sqlbot_assistant_event'
105+
const communicationCb = async (event: any) => {
106+
if (event.data?.eventName === eventName) {
107+
if (event.data?.messageId !== route.query.id) {
108+
return
109+
}
110+
if (event.data?.busi == 'certificate') {
111+
const certificate = event.data['certificate']
112+
console.log(certificate)
113+
assistantStore.setType(1)
114+
assistantStore.setCertificate(certificate)
115+
// store certificate to pinia
116+
}
117+
}
118+
}
104119
onBeforeMount(async () => {
105120
const assistantId = route.query.id
106121
const online = route.query.online
@@ -116,6 +131,19 @@ onBeforeMount(async () => {
116131
assistantStore.setToken(validator.value.token)
117132
assistantStore.setAssistant(true)
118133
loading.value = false
134+
135+
window.addEventListener('message', communicationCb)
136+
const readyData = {
137+
eventName: 'sqlbot_assistant_event',
138+
busi: 'ready',
139+
ready: true,
140+
messageId: assistantId,
141+
}
142+
window.parent.postMessage(readyData, '*')
143+
})
144+
145+
onBeforeUnmount(() => {
146+
window.removeEventListener('message', communicationCb)
119147
})
120148
</script>
121149

0 commit comments

Comments
 (0)