Skip to content

Commit 115016c

Browse files
perf: Assistant switch online
1 parent 5502afc commit 115016c

File tree

6 files changed

+122
-7
lines changed

6 files changed

+122
-7
lines changed

backend/apps/system/crud/assistant.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ def get_assistant_ds(llm_service) -> list[dict]:
3535
config: dict[any] = json.loads(configuration)
3636
oid: str = config['oid']
3737
stmt = select(CoreDatasource.id, CoreDatasource.name, CoreDatasource.description).where(CoreDatasource.oid == oid)
38-
private_list:list[int] = config.get('private_list') or None
39-
if private_list:
40-
stmt.where(~CoreDatasource.id.in_(private_list))
38+
if not assistant.online:
39+
private_list:list[int] = config.get('private_list') or None
40+
if private_list:
41+
stmt = stmt.where(~CoreDatasource.id.in_(private_list))
4142
db_ds_list = session.exec(stmt)
4243

4344
result_list = [

backend/common/core/deps.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ async def get_current_assistant(request: Request) -> AssistantHeader | None:
2525
if request.headers.get("X-SQLBOT-ASSISTANT-CERTIFICATE"):
2626
entry_certificate = request.headers['X-SQLBOT-ASSISTANT-CERTIFICATE']
2727
base_assistant.certificate = unquote(base64.b64decode(entry_certificate).decode('utf-8'))
28+
if request.headers.get("X-SQLBOT-ASSISTANT-ONLINE"):
29+
entry_online = request.headers['X-SQLBOT-ASSISTANT-ONLINE']
30+
base_assistant.online = str(entry_online).lower() == 'true' if entry_online else False
2831
return base_assistant
2932

3033
CurrentAssistant = Annotated[AssistantHeader, Depends(get_current_assistant)]

frontend/public/assistant.js

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
;(function () {
2+
window.sqlbot_assistant_handler = window.sqlbot_assistant_handler || {}
23
const defaultData = {
34
id: '1',
45
show_guide: false,
@@ -67,7 +68,7 @@
6768
const getChatContainerHtml = (data) => {
6869
return `
6970
<div id="sqlbot-assistant-chat-container">
70-
<iframe id="sqlbot-assistant-chat-iframe-${data.id}" allow="microphone" src="${data.domain_url}/#/assistant?id=${data.id}"></iframe>
71+
<iframe id="sqlbot-assistant-chat-iframe-${data.id}" allow="microphone" src="${data.domain_url}/#/assistant?id=${data.id}&online=${!!data.online}"></iframe>
7172
<div class="sqlbot-assistant-operate">
7273
<div class="sqlbot-assistant-closeviewport sqlbot-assistant-viewportnone">
7374
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="none">
@@ -440,6 +441,7 @@
440441
}
441442
function loadScript(src, id) {
442443
const domain_url = getDomain(src)
444+
const online = getParam(src, 'online')
443445
let url = `${domain_url}/api/v1/system/assistant/info/${id}`
444446
if (domain_url.includes('5173')) {
445447
url = url.replace('5173', '8000')
@@ -455,6 +457,7 @@
455457
Object.assign(tempData, config)
456458
tempData = Object.assign(tempData, config)
457459
}
460+
tempData['online'] = online && online.toString().toLowerCase() == 'true'
458461
initsqlbot_assistant(tempData)
459462
if (data.type == 1) {
460463
registerMessageEvent(id, tempData)
@@ -471,13 +474,90 @@
471474
const src_list = scriptsArray.map((script) => script.src)
472475
src_list.forEach((src) => {
473476
const id = getParam(src, 'id')
477+
window.sqlbot_assistant_handler[id] = window.sqlbot_assistant_handler[id] || {}
478+
window.sqlbot_assistant_handler[id]['id'] = id
474479
const propName = script_id_prefix + id + '-state'
475480
if (window[propName]) {
476481
return true
477482
}
478483
window[propName] = true
479484
loadScript(src, id)
485+
expposeGlobalMethods(id)
480486
})
481487
}
482-
window.addEventListener('load', init)
488+
function updateOnlineParam(target_url, newValue) {
489+
try {
490+
const url = new URL(target_url)
491+
const [hashPath, hashQuery] = url.hash.split('?')
492+
let searchParams
493+
if (hashQuery) {
494+
searchParams = new URLSearchParams(hashQuery)
495+
} else {
496+
searchParams = url.searchParams
497+
}
498+
searchParams.set('online', newValue)
499+
if (hashQuery) {
500+
url.hash = `${hashPath}?${searchParams.toString()}`
501+
} else {
502+
url.search = searchParams.toString()
503+
}
504+
return url.toString()
505+
} catch (e) {
506+
console.error('Invalid URL:', target_url)
507+
return target_url
508+
}
509+
}
510+
function expposeGlobalMethods(id) {
511+
window.sqlbot_assistant_handler[id]['setOnline'] = (online) => {
512+
if (online != null && typeof online != 'boolean') {
513+
throw new Error('The parameter can only be of type boolean')
514+
}
515+
const iframe = document.getElementById(`sqlbot-assistant-chat-iframe-${id}`)
516+
if (iframe) {
517+
const url = iframe.src
518+
const eventName = 'sqlbot_assistant_event'
519+
const params = {
520+
busi: 'setOnline',
521+
online,
522+
eventName,
523+
messageId: id,
524+
}
525+
const contentWindow = iframe.contentWindow
526+
contentWindow.postMessage(params, url)
527+
}
528+
}
529+
window.sqlbot_assistant_handler[id]['refresh'] = (online) => {
530+
if (online != null && typeof online != 'boolean') {
531+
throw new Error('The parameter can only be of type boolean')
532+
}
533+
const iframe = document.getElementById(`sqlbot-assistant-chat-iframe-${id}`)
534+
if (iframe) {
535+
const url = iframe.src
536+
let new_url = `${url}&t=${Date.now()}`
537+
if (online != null) {
538+
new_url = updateOnlineParam(new_url, online)
539+
}
540+
iframe.src = new_url
541+
}
542+
}
543+
}
544+
// window.addEventListener('load', init)
545+
const executeWhenReady = (fn) => {
546+
if (
547+
document.readyState === 'complete' ||
548+
(document.readyState !== 'loading' && !document.documentElement.doScroll)
549+
) {
550+
setTimeout(fn, 0)
551+
} else {
552+
const onReady = () => {
553+
document.removeEventListener('DOMContentLoaded', onReady)
554+
window.removeEventListener('load', onReady)
555+
fn()
556+
}
557+
document.addEventListener('DOMContentLoaded', onReady)
558+
window.addEventListener('load', onReady)
559+
}
560+
}
561+
562+
executeWhenReady(init)
483563
})()

frontend/src/stores/assistant.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ interface AssistantState {
1111
flag: number
1212
type: number
1313
certificate: string
14+
online: boolean
1415
}
1516

1617
export const AssistantStore = defineStore('assistant', {
@@ -21,6 +22,7 @@ export const AssistantStore = defineStore('assistant', {
2122
flag: 0,
2223
type: 0,
2324
certificate: '',
25+
online: false,
2426
}
2527
},
2628
getters: {
@@ -39,6 +41,9 @@ export const AssistantStore = defineStore('assistant', {
3941
getType(): number {
4042
return this.type
4143
},
44+
getOnline(): boolean {
45+
return this.online
46+
},
4247
},
4348
actions: {
4449
setCertificate(certificate: string) {
@@ -61,6 +66,9 @@ export const AssistantStore = defineStore('assistant', {
6166
wsCache.set(flagKey, flag)
6267
}
6368
},
69+
setOnline(online: boolean) {
70+
this.online = !!online
71+
},
6472
async setChat() {
6573
if (!this.assistant) {
6674
return null

frontend/src/utils/request.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ class HttpService {
8080
encodeURIComponent(assistantStore.getCertificate)
8181
)
8282
}
83+
if (!assistantStore.getType) {
84+
config.headers['X-SQLBOT-ASSISTANT-ONLINE'] = assistantStore.getOnline
85+
}
8386
}
8487
const locale = getLocale()
8588
if (locale) {
@@ -261,6 +264,9 @@ class HttpService {
261264
encodeURIComponent(assistantStore.getCertificate)
262265
)
263266
}
267+
if (!assistantStore.getType) {
268+
heads['X-SQLBOT-ASSISTANT-ONLINE'] = assistantStore.getOnline
269+
}
264270
}
265271
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
266272
// @ts-ignore

frontend/src/views/embedded/index.vue

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,31 @@ const communicationCb = async (event: any) => {
5858
const certificate = event.data['certificate']
5959
assistantStore.setType(1)
6060
assistantStore.setCertificate(certificate)
61-
// store certificate to pinia
6261
}
62+
if (event.data?.busi == 'setOnline') {
63+
setFormatOnline(event.data.online)
64+
}
65+
}
66+
}
67+
const setFormatOnline = (text?: any) => {
68+
if (text === null || typeof text === 'undefined') {
69+
assistantStore.setOnline(false)
70+
return
71+
}
72+
if (typeof text === 'boolean') {
73+
assistantStore.setOnline(text)
74+
return
75+
}
76+
if (typeof text === 'string') {
77+
assistantStore.setOnline(text.toLowerCase() === 'true')
78+
return
6379
}
80+
assistantStore.setOnline(false)
6481
}
6582
onBeforeMount(async () => {
6683
const assistantId = route.query.id
6784
const online = route.query.online
68-
console.log(online)
85+
setFormatOnline(online)
6986
const now = Date.now()
7087
assistantStore.setFlag(now)
7188
const param = {

0 commit comments

Comments
 (0)