From ccb7d97d83a841796c0bf8b1ccc2369906f73917 Mon Sep 17 00:00:00 2001 From: fit2cloud-chenyw Date: Tue, 30 Sep 2025 10:51:41 +0800 Subject: [PATCH] feat: New API to control the visibility of the assistant's chat history --- frontend/public/assistant.js | 44 ++++++++++++++++++++++++++- frontend/src/stores/assistant.ts | 8 +++++ frontend/src/views/chat/index.vue | 37 +++++++++++++++------- frontend/src/views/embedded/index.vue | 10 ++++++ frontend/src/views/embedded/page.vue | 12 ++++++++ 5 files changed, 99 insertions(+), 12 deletions(-) diff --git a/frontend/public/assistant.js b/frontend/public/assistant.js index 38a963b2..b9c4a51d 100644 --- a/frontend/public/assistant.js +++ b/frontend/public/assistant.js @@ -67,9 +67,16 @@ ` const getChatContainerHtml = (data) => { + let srcUrl = `${data.domain_url}/#/assistant?id=${data.id}&online=${!!data.online}&name=${encodeURIComponent(data.name)}&userFlag=${data.userFlag || ''}` + if (data.userFlag) { + srcUrl += `&userFlag=${data.userFlag || ''}` + } + if (data.history) { + srcUrl += `&userFlag=${data.history}` + } return `
- +
@@ -499,6 +506,7 @@ const domain_url = getDomain(src) const online = getParam(src, 'online') const userFlag = getParam(src, 'userFlag') + const history = getParam(src, 'history') let url = `${domain_url}/api/v1/system/assistant/info/${id}` if (domain_url.includes('5173')) { url = url.replace('5173', '8000') @@ -536,6 +544,7 @@ tempData['online'] = online && online.toString().toLowerCase() == 'true' tempData['userFlag'] = userFlag + tempData['history'] = history initsqlbot_assistant(tempData) if (data.type == 1) { registerMessageEvent(id, tempData) @@ -750,6 +759,39 @@ } delete window.sqlbot_assistant_handler[id] } + window.sqlbot_assistant_handler[id]['setHistory'] = (show) => { + if (show != null && typeof show != 'boolean') { + throw new Error('The parameter can only be of type boolean') + } + const iframe = document.getElementById(`sqlbot-assistant-chat-iframe-${id}`) + if (iframe) { + const url = iframe.src + const eventName = 'sqlbot_assistant_event' + const params = { + busi: 'setHistory', + show, + eventName, + messageId: id, + } + const contentWindow = iframe.contentWindow + contentWindow.postMessage(params, url) + } + } + window.sqlbot_assistant_handler[id]['createConversation'] = (param) => { + const iframe = document.getElementById(`sqlbot-assistant-chat-iframe-${id}`) + if (iframe) { + const url = iframe.src + const eventName = 'sqlbot_assistant_event' + const params = { + busi: 'createConversation', + param, + eventName, + messageId: id, + } + const contentWindow = iframe.contentWindow + contentWindow.postMessage(params, url) + } + } } // window.addEventListener('load', init) const executeWhenReady = (fn) => { diff --git a/frontend/src/stores/assistant.ts b/frontend/src/stores/assistant.ts index d6eeb2f1..5b27fec4 100644 --- a/frontend/src/stores/assistant.ts +++ b/frontend/src/stores/assistant.ts @@ -20,6 +20,7 @@ interface AssistantState { certificate: string online: boolean pageEmbedded?: boolean + history: boolean requestPromiseMap: Map } @@ -34,6 +35,7 @@ export const AssistantStore = defineStore('assistant', { certificate: '', online: false, pageEmbedded: false, + history: true, requestPromiseMap: new Map(), } }, @@ -59,6 +61,9 @@ export const AssistantStore = defineStore('assistant', { getOnline(): boolean { return this.online }, + getHistory(): boolean { + return this.history + }, getPageEmbedded(): boolean { return this.pageEmbedded || false }, @@ -130,6 +135,9 @@ export const AssistantStore = defineStore('assistant', { setOnline(online: boolean) { this.online = !!online }, + setHistory(history: boolean) { + this.history = history ?? true + }, async setChat() { if (!this.assistant) { return null diff --git a/frontend/src/views/chat/index.vue b/frontend/src/views/chat/index.vue index f0ce56d7..020a34dd 100644 --- a/frontend/src/views/chat/index.vue +++ b/frontend/src/views/chat/index.vue @@ -6,7 +6,13 @@ popper-class="popover-chat_history popover-chat_history_small" > @@ -17,7 +23,7 @@ v-model:current-chat="currentChat" v-model:loading="loading" in-popover - :appName="customName" + :app-name="customName" @go-empty="goEmpty" @on-chat-created="onChatCreated" @on-click-history="onClickHistory" @@ -30,6 +36,7 @@ @@ -328,7 +338,7 @@ :current-chat-id="currentChatId" :loading="isTyping" :message="message" - @scrollBottom="scrollToBottom" + @scroll-bottom="scrollToBottom" @finish="onPredictAnswerFinish" @error="onPredictAnswerError" @stop="onChatStop" @@ -446,6 +456,9 @@ const defaultFloatPopoverStyle = ref({ }) const isCompletePage = computed(() => !assistantStore.getAssistant || assistantStore.getEmbedded) +const embeddedHistoryHidden = computed( + () => assistantStore.getAssistant && !assistantStore.getHistory +) const customName = computed(() => { if (!isCompletePage.value && props.pageEmbedded) return props.appName return '' @@ -1328,7 +1341,9 @@ onMounted(() => { border: 1px solid rgba(222, 224, 227, 1); border-radius: 6px; } - +.embedded-history-hidden { + display: none !important; +} .show-history_icon { cursor: pointer; position: absolute; diff --git a/frontend/src/views/embedded/index.vue b/frontend/src/views/embedded/index.vue index 928ea3f5..18b6e8d6 100644 --- a/frontend/src/views/embedded/index.vue +++ b/frontend/src/views/embedded/index.vue @@ -79,6 +79,12 @@ const communicationCb = async (event: any) => { if (event.data?.busi == 'setOnline') { setFormatOnline(event.data.online) } + if (event.data?.busi == 'setHistory') { + assistantStore.setHistory(event.data.show ?? true) + } + if (event.data?.busi == 'createConversation') { + createChat() + } } } const setFormatOnline = (text?: any) => { @@ -141,6 +147,10 @@ onBeforeMount(async () => { if (userFlag && userFlag === '1') { userFlag = '100001' } + + const history: boolean = route.query.history !== 'false' + assistantStore.setHistory(history) + const now = Date.now() assistantStore.setFlag(now) assistantStore.setId(assistantId?.toString() || '') diff --git a/frontend/src/views/embedded/page.vue b/frontend/src/views/embedded/page.vue index 58a38a96..aeb3bae2 100644 --- a/frontend/src/views/embedded/page.vue +++ b/frontend/src/views/embedded/page.vue @@ -69,8 +69,17 @@ const communicationCb = async (event: any) => { if (event.data?.busi == 'setOnline') { setFormatOnline(event.data.online) } + if (event.data?.busi == 'setHistory') { + assistantStore.setHistory(event.data.show ?? true) + } + if (event.data?.busi == 'createConversation') { + createChat() + } } } +const createChat = () => { + chatRef.value?.createNewChat() +} const setFormatOnline = (text?: any) => { if (text === null || typeof text === 'undefined') { assistantStore.setOnline(false) @@ -119,6 +128,9 @@ onBeforeMount(async () => { const online = route.query.online setFormatOnline(online) + const history: boolean = route.query.history !== 'false' + assistantStore.setHistory(history) + let name = route.query.name if (name) { assistantName.value = decodeURIComponent(name.toString())