Skip to content

Commit 0633d55

Browse files
perf: Optimize embedded APIs
1 parent ce8b375 commit 0633d55

5 files changed

Lines changed: 79 additions & 84 deletions

File tree

backend/apps/system/api/assistant.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,13 @@ async def update(request: Request, session: SessionDep, editor: AssistantDTO):
313313
dynamic_upgrade_cors(request=request, session=session)
314314

315315

316-
@router.get("/{id}", response_model=AssistantModel, summary=f"{PLACEHOLDER_PREFIX}assistant_query_api", description=f"{PLACEHOLDER_PREFIX}assistant_query_api")
316+
""" @router.get("/{id}", response_model=AssistantModel, summary=f"{PLACEHOLDER_PREFIX}assistant_query_api", description=f"{PLACEHOLDER_PREFIX}assistant_query_api")
317317
async def get_one(session: SessionDep, id: int = Path(description="ID")):
318318
db_model = await get_assistant_info(session=session, assistant_id=id)
319319
if not db_model:
320320
raise ValueError(f"AssistantModel with id {id} not found")
321321
db_model = AssistantModel.model_validate(db_model)
322-
return db_model
322+
return db_model """
323323

324324

325325
@router.delete("/{id}", summary=f"{PLACEHOLDER_PREFIX}assistant_del_api", description=f"{PLACEHOLDER_PREFIX}assistant_del_api")

frontend/src/api/assistant.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ export const assistantApi = {
66
add: (data: any) => request.post('/system/assistant', data),
77
edit: (data: any) => request.put('/system/assistant', data),
88
delete: (id: number) => request.delete(`/system/assistant/${id}`),
9-
query: (id: number) => request.get(`/system/assistant/${id}`),
9+
// query: (id: number) => request.get(`/system/assistant/${id}`),
1010
validate: (data: any) => request.get('/system/assistant/validator', { params: data }),
1111
}

frontend/src/api/embedded.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const getAdvancedApplicationList = () =>
55
request.get('/system/assistant/advanced_application')
66
export const updateAssistant = (data: any) => request.put('/system/assistant', data)
77
export const saveAssistant = (data: any) => request.post('/system/assistant', data)
8-
export const getOne = (id: any) => request.get(`/system/assistant/${id}`)
8+
// export const getOne = (id: any) => request.get(`/system/assistant/${id}`)
99
export const delOne = (id: any) => request.delete(`/system/assistant/${id}`)
1010
export const dsApi = (id: any) => request.get(`/datasource/ws/${id}`)
1111

frontend/src/views/embedded/index.vue

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
</div>
2121
<div class="sqlbot-chat-container">
2222
<chat-component
23-
v-if="!loading && tokenReady"
23+
v-if="!loading"
2424
ref="chatRef"
2525
:welcome="customSet.welcome"
2626
:welcome-desc="customSet.welcome_desc"
@@ -42,7 +42,6 @@ import {
4242
watch,
4343
} from 'vue'
4444
import ChatComponent from '@/views/chat/index.vue'
45-
import { request } from '@/utils/request'
4645
import LOGO from '@/assets/svg/logo-custom_small.svg'
4746
import icon_new_chat_outlined from '@/assets/svg/icon_new_chat_outlined.svg'
4847
import { useAppearanceStoreWithOut } from '@/stores/appearance'
@@ -80,11 +79,10 @@ const openHistory = () => {
8079
}) */
8180
const appName = ref('')
8281
const loading = ref(true)
83-
const tokenReady = ref(false)
8482
const eventName = 'sqlbot_assistant_event'
8583
86-
let resolveTokenReady: (() => void) | null = null
87-
const tokenReadyPromise = new Promise<void>((resolve) => {
84+
let resolveTokenReady: ((data: any) => any) | null = null
85+
const tokenReadyPromise = new Promise<any>((resolve) => {
8886
resolveTokenReady = resolve
8987
})
9088
const communicationCb = async (event: any) => {
@@ -94,8 +92,8 @@ const communicationCb = async (event: any) => {
9492
}
9593
if (event.data['sqlbot_embedded_token']) {
9694
assistantStore.setToken(event.data['sqlbot_embedded_token'])
97-
resolveTokenReady?.()
98-
tokenReady.value = true
95+
const originData = event.data['sqlbot_origin_data']
96+
resolveTokenReady?.(originData)
9997
}
10098
if (event.data?.busi == 'certificate') {
10199
const certificate = event.data['certificate']
@@ -220,8 +218,6 @@ onBeforeMount(async () => {
220218
validator.value = await assistantApi.validate(param)
221219
assistantStore.setToken(validator.value.token) */
222220
assistantStore.setAssistant(true)
223-
loading.value = false
224-
225221
window.addEventListener('message', communicationCb)
226222
const readyData = {
227223
eventName: 'sqlbot_assistant_event',
@@ -231,46 +227,44 @@ onBeforeMount(async () => {
231227
}
232228
window.parent.postMessage(readyData, '*')
233229
234-
await tokenReadyPromise
235-
236-
request.get(`/system/assistant/${assistantId}`).then((res) => {
237-
if (res.name) {
238-
appName.value = res.name
230+
const res = await tokenReadyPromise
231+
loading.value = false
232+
if (res?.name) {
233+
appName.value = res.name
234+
}
235+
if (res?.configuration) {
236+
const rawData = JSON.parse(res?.configuration)
237+
assistantStore.setAutoDs(rawData?.auto_ds)
238+
if (rawData.logo) {
239+
logo.value = baseUrl + rawData.logo
239240
}
240-
if (res?.configuration) {
241-
const rawData = JSON.parse(res?.configuration)
242-
assistantStore.setAutoDs(rawData?.auto_ds)
243-
if (rawData.logo) {
244-
logo.value = baseUrl + rawData.logo
245-
}
246241
247-
for (const key in customSet) {
248-
if (
249-
Object.prototype.hasOwnProperty.call(customSet, key) &&
250-
![null, undefined].includes(rawData[key])
251-
) {
252-
customSet[key] = rawData[key]
253-
configuredKeys.add(key)
254-
}
255-
}
256-
257-
if (!rawData.theme) {
258-
const { customColor, themeColor } = appearanceStore
259-
const currentColor =
260-
themeColor === 'custom' && customColor
261-
? customColor
262-
: themeColor === 'blue'
263-
? '#3370ff'
264-
: '#1CBA90'
265-
customSet.theme = currentColor || customSet.theme
242+
for (const key in customSet) {
243+
if (
244+
Object.prototype.hasOwnProperty.call(customSet, key) &&
245+
![null, undefined].includes(rawData[key])
246+
) {
247+
customSet[key] = rawData[key]
248+
configuredKeys.add(key)
266249
}
250+
}
267251
268-
nextTick(() => {
269-
setPageCustomColor(customSet.theme)
270-
setPageHeaderFontColor(customSet.header_font_color)
271-
})
252+
if (!rawData.theme) {
253+
const { customColor, themeColor } = appearanceStore
254+
const currentColor =
255+
themeColor === 'custom' && customColor
256+
? customColor
257+
: themeColor === 'blue'
258+
? '#3370ff'
259+
: '#1CBA90'
260+
customSet.theme = currentColor || customSet.theme
272261
}
273-
})
262+
263+
nextTick(() => {
264+
setPageCustomColor(customSet.theme)
265+
setPageHeaderFontColor(customSet.header_font_color)
266+
})
267+
}
274268
})
275269
276270
onBeforeUnmount(() => {

frontend/src/views/embedded/page.vue

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { useAssistantStore } from '@/stores/assistant'
2323
import { useAppearanceStoreWithOut } from '@/stores/appearance'
2424
import { useI18n } from 'vue-i18n'
2525
import { i18n } from '@/i18n'
26-
import { request } from '@/utils/request'
2726
import { setCurrentColor } from '@/utils/utils'
2827
import { useUserStore } from '@/stores/user'
2928
const userStore = useUserStore()
@@ -74,22 +73,25 @@ const divLoading = ref(true)
7473
const tokenReady = ref(false)
7574
const eventName = 'sqlbot_embedded_event'
7675
77-
let resolveTokenReady: (() => void) | null = null
78-
const tokenReadyPromise = new Promise<void>((resolve) => {
76+
let resolveTokenReady: ((data: any) => void) | null = null
77+
const tokenReadyPromise = new Promise<any>((resolve) => {
7978
resolveTokenReady = resolve
8079
})
8180
const communicationCb = async (event: any) => {
8281
if (event.data?.eventName === eventName) {
8382
if (event.data?.messageId !== route.query.id) {
8483
return
8584
}
85+
const assistantTypeObj = event.data['type']
8686
if (
87-
event.data['type'] &&
87+
assistantTypeObj !== null &&
88+
assistantTypeObj !== undefined &&
8889
parseInt(event.data['type']) !== 4 &&
8990
event.data['sqlbot_embedded_token']
9091
) {
9192
assistantStore.setToken(event.data['sqlbot_embedded_token'])
92-
resolveTokenReady?.()
93+
const originData = event.data['sqlbot_origin_data']
94+
resolveTokenReady?.(originData)
9395
tokenReady.value = true
9496
}
9597
if (event.data?.busi == 'certificate') {
@@ -228,42 +230,41 @@ onBeforeMount(async () => {
228230
229231
registerReady(assistantId)
230232
231-
await tokenReadyPromise
233+
const res = await tokenReadyPromise
232234
loading.value = false
233-
request.get(`/system/assistant/${assistantId}`).then((res) => {
234-
if (res?.configuration) {
235-
const rawData = JSON.parse(res?.configuration)
236-
assistantStore.setAutoDs(rawData?.auto_ds)
237-
if (rawData.logo) {
238-
logo.value = baseUrl + rawData.logo
239-
}
240-
rawData['name'] = rawData['name'] || res['name']
241-
for (const key in customSet) {
242-
if (
243-
Object.prototype.hasOwnProperty.call(customSet, key) &&
244-
![null, undefined].includes(rawData[key])
245-
) {
246-
customSet[key] = rawData[key]
247-
configuredKeys.add(key)
248-
}
249-
}
250235
251-
if (!rawData.theme) {
252-
const { customColor, themeColor } = appearanceStore
253-
const currentColor =
254-
themeColor === 'custom' && customColor
255-
? customColor
256-
: themeColor === 'blue'
257-
? '#3370ff'
258-
: '#1CBA90'
259-
customSet.theme = currentColor || customSet.theme
236+
if (res?.configuration) {
237+
const rawData = JSON.parse(res?.configuration)
238+
assistantStore.setAutoDs(rawData?.auto_ds)
239+
if (rawData.logo) {
240+
logo.value = baseUrl + rawData.logo
241+
}
242+
rawData['name'] = rawData['name'] || res['name']
243+
for (const key in customSet) {
244+
if (
245+
Object.prototype.hasOwnProperty.call(customSet, key) &&
246+
![null, undefined].includes(rawData[key])
247+
) {
248+
customSet[key] = rawData[key]
249+
configuredKeys.add(key)
260250
}
251+
}
261252
262-
nextTick(() => {
263-
setPageCustomColor(customSet.theme)
264-
})
253+
if (!rawData.theme) {
254+
const { customColor, themeColor } = appearanceStore
255+
const currentColor =
256+
themeColor === 'custom' && customColor
257+
? customColor
258+
: themeColor === 'blue'
259+
? '#3370ff'
260+
: '#1CBA90'
261+
customSet.theme = currentColor || customSet.theme
265262
}
266-
})
263+
264+
nextTick(() => {
265+
setPageCustomColor(customSet.theme)
266+
})
267+
}
267268
})
268269
269270
onBeforeUnmount(() => {

0 commit comments

Comments
 (0)