From 4550b41a99427cc054200eec39f838feecaa1d66 Mon Sep 17 00:00:00 2001 From: fit2cloud-chenyw Date: Thu, 17 Jul 2025 18:41:55 +0800 Subject: [PATCH] perf: Assistant message --- frontend/public/assistant.js | 80 ++++++++++++++++++++++- frontend/src/stores/assistant.ts | 16 +++++ frontend/src/views/embedded/assistant.vue | 30 ++++++++- 3 files changed, 123 insertions(+), 3 deletions(-) diff --git a/frontend/public/assistant.js b/frontend/public/assistant.js index eb2d51a6..c7677d65 100644 --- a/frontend/public/assistant.js +++ b/frontend/public/assistant.js @@ -40,7 +40,7 @@ const getChatContainerHtml = (data) => { return `
- +
@@ -311,7 +311,7 @@ #sqlbot-assistant #sqlbot-assistant-chat-container .sqlbot-assistant-viewportnone{ display:none; } - #sqlbot-assistant #sqlbot-assistant-chat-container #sqlbot-assistant-chat{ + #sqlbot-assistant #sqlbot-assistant-chat-container #sqlbot-assistant-chat-iframe-${data.id}{ height:100%; width:100%; border: none; @@ -334,7 +334,79 @@ const url = new URL(src) return url.searchParams.get(key) } + function parsrCertificate(config) { + const certificateList = config.certificate + if (!certificateList?.length) { + return null + } + const list = certificateList.map((item) => formatCertificate(item)).filter((item) => !!item) + return JSON.stringify(list) + } + function isEmpty(obj) { + return obj == null || typeof obj == 'undefined' + } + function formatCertificate(item) { + const { type, source, target, target_key, target_val } = item + let source_val = null + if (type.toLocaleLowerCase() == 'localstorage') { + source_val = localStorage.getItem(source) + } + if (type.toLocaleLowerCase() == 'sessionstorage') { + source_val = sessionStorage.getItem(source) + } + if (type.toLocaleLowerCase() == 'cookie') { + source_val = getCookie(source) + } + if (type.toLocaleLowerCase() == 'custom') { + source_val = source + } + if (isEmpty(source_val)) { + return null + } + return { + target, + key: target_key || source, + value: (target_val && eval(target_val)) || source_val, + } + } + function getCookie(key) { + if (!key || !document.cookie) { + return null + } + const cookies = document.cookie.split(';') + for (let i = 0; i < cookies.length; i++) { + const cookie = cookies[i].trim() + if (cookie.startsWith(key + '=')) { + return decodeURIComponent(cookie.substring(key.length + 1)) + } + } + return null + } + function registerMessageEvent(id, data) { + const iframe = document.getElementById(`sqlbot-assistant-chat-iframe-${id}`) + const url = iframe.src + const eventName = 'sqlbot_assistant_event' + window.addEventListener('message', (event) => { + if (event.data?.eventName === eventName) { + if (event.data?.messageId !== id) { + return + } + if (event.data?.busi == 'ready' && event.data?.ready) { + const certificate = parsrCertificate(data) + console.log(certificate) + params = { + busi: 'certificate', + certificate, + eventName, + messageId: id, + } + const contentWindow = iframe.contentWindow + contentWindow.postMessage(params, url) + } + } + }) + } function loadScript(src, id) { const domain_url = getDomain(src) let url = `${domain_url}/api/v1/system/assistant/info/${id}` @@ -353,6 +425,10 @@ tempData = Object.assign(tempData, config) } initsqlbot_assistant(tempData) + if (data.type == 1) { + registerMessageEvent(id, tempData) + // postMessage the certificate to iframe + } }) } function getDomain(src) { diff --git a/frontend/src/stores/assistant.ts b/frontend/src/stores/assistant.ts index d20a4c6d..ebfe0ffa 100644 --- a/frontend/src/stores/assistant.ts +++ b/frontend/src/stores/assistant.ts @@ -9,6 +9,8 @@ interface AssistantState { token: string assistant: boolean flag: number + type: number + certificate: string } export const AssistantStore = defineStore('assistant', { @@ -17,9 +19,14 @@ export const AssistantStore = defineStore('assistant', { token: '', assistant: false, flag: 0, + type: 0, + certificate: '', } }, getters: { + getCertificate(): string { + return this.certificate + }, getToken(): string { return this.token }, @@ -29,8 +36,17 @@ export const AssistantStore = defineStore('assistant', { getFlag(): number { return this.flag }, + getType(): number { + return this.type + }, }, actions: { + setCertificate(certificate: string) { + this.certificate = certificate + }, + setType(type: number) { + this.type = type + }, setToken(token: string) { this.token = token }, diff --git a/frontend/src/views/embedded/assistant.vue b/frontend/src/views/embedded/assistant.vue index d7b67bbd..f5dfd328 100644 --- a/frontend/src/views/embedded/assistant.vue +++ b/frontend/src/views/embedded/assistant.vue @@ -57,7 +57,7 @@