Skip to content

Commit 9339385

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 1d48052 + 38f4641 commit 9339385

File tree

5 files changed

+58
-5
lines changed

5 files changed

+58
-5
lines changed

backend/apps/chat/task/llm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,9 @@ def select_datasource(self):
430430
_datasource: int | None = None
431431
_engine_type: str | None = None
432432
try:
433-
data = orjson.loads(json_str)
433+
data: dict = orjson.loads(json_str)
434434

435-
if data['id'] and data['id'] != 0:
435+
if data.get('id') and data.get('id') != 0:
436436
_datasource = data['id']
437437
_chat = self.session.get(Chat, self.record.chat_id)
438438
_chat.datasource = _datasource

frontend/src/stores/assistant.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,43 @@ import { useCache } from '@/utils/useCache'
55

66
const { wsCache } = useCache()
77
const flagKey = 'sqlbit-assistant-flag'
8+
type Resolver<T = any> = (value: T | PromiseLike<T>) => void
9+
type Rejecter = (reason?: any) => void
10+
interface PendingRequest<T = any> {
11+
resolve: Resolver<T>
12+
reject: Rejecter
13+
}
814
interface AssistantState {
15+
id: string
916
token: string
1017
assistant: boolean
1118
flag: number
1219
type: number
1320
certificate: string
1421
online: boolean
22+
requestPromiseMap: Map<string, PendingRequest>
1523
}
1624

1725
export const AssistantStore = defineStore('assistant', {
1826
state: (): AssistantState => {
1927
return {
28+
id: '',
2029
token: '',
2130
assistant: false,
2231
flag: 0,
2332
type: 0,
2433
certificate: '',
2534
online: false,
35+
requestPromiseMap: new Map<string, PendingRequest>(),
2636
}
2737
},
2838
getters: {
2939
getCertificate(): string {
3040
return this.certificate
3141
},
42+
getId(): string {
43+
return this.id
44+
},
3245
getToken(): string {
3346
return this.token
3447
},
@@ -46,6 +59,43 @@ export const AssistantStore = defineStore('assistant', {
4659
},
4760
},
4861
actions: {
62+
refreshCertificate<T>() {
63+
const timeout = 5000
64+
return new Promise((resolve, reject) => {
65+
const timeoutId = setTimeout(() => {
66+
this.requestPromiseMap.delete(this.id)
67+
reject(new Error(`Request ${this.id} timed out after ${timeout}ms`))
68+
}, timeout)
69+
this.requestPromiseMap.set(this.id, {
70+
resolve: (value: T) => {
71+
clearTimeout(timeoutId)
72+
this.requestPromiseMap.delete(this.id)
73+
resolve(value)
74+
},
75+
reject: (reason) => {
76+
clearTimeout(timeoutId)
77+
this.requestPromiseMap.delete(this.id)
78+
reject(reason)
79+
},
80+
})
81+
const readyData = {
82+
eventName: 'sqlbot_assistant_event',
83+
busi: 'ready',
84+
ready: true,
85+
messageId: this.id,
86+
}
87+
window.parent.postMessage(readyData, '*')
88+
})
89+
},
90+
resolveCertificate(data?: any) {
91+
const peddingRequest = this.requestPromiseMap.get(this.id)
92+
if (peddingRequest) {
93+
peddingRequest.resolve(data)
94+
}
95+
},
96+
setId(id: string) {
97+
this.id = id
98+
},
4999
setCertificate(certificate: string) {
50100
this.certificate = certificate
51101
},

frontend/src/utils/request.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class HttpService {
245245
return this.request({ ...config, method: 'POST', url, data })
246246
}
247247

248-
public fetchStream(url: string, data?: any, controller?: AbortController): Promise<any> {
248+
public async fetchStream(url: string, data?: any, controller?: AbortController): Promise<any> {
249249
const token = wsCache.get('user.token')
250250
const heads: any = {
251251
'Content-Type': 'application/json',
@@ -257,6 +257,7 @@ class HttpService {
257257
heads['X-SQLBOT-ASSISTANT-TOKEN'] = `Assistant ${assistantStore.getToken}`
258258
if (heads['X-SQLBOT-TOKEN']) delete heads['X-SQLBOT-TOKEN']
259259
if (assistantStore.getType && assistantStore.getCertificate) {
260+
await assistantStore.refreshCertificate()
260261
heads['X-SQLBOT-ASSISTANT-CERTIFICATE'] = btoa(
261262
encodeURIComponent(assistantStore.getCertificate)
262263
)

frontend/src/views/embedded/index.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const communicationCb = async (event: any) => {
5858
const certificate = event.data['certificate']
5959
assistantStore.setType(1)
6060
assistantStore.setCertificate(certificate)
61+
assistantStore.resolveCertificate(certificate)
6162
}
6263
if (event.data?.busi == 'setOnline') {
6364
setFormatOnline(event.data.online)
@@ -85,6 +86,7 @@ onBeforeMount(async () => {
8586
setFormatOnline(online)
8687
const now = Date.now()
8788
assistantStore.setFlag(now)
89+
assistantStore.setId(assistantId?.toString() || '')
8890
const param = {
8991
id: assistantId,
9092
virtual: assistantStore.getFlag,

frontend/src/views/system/user/User.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ const search = () => {
717717
const addTerm = () => {
718718
const { account, email, name, oid, status } = state.form
719719
userApi.add({ account, email, name, oid, status }).then(() => {
720-
dialogFormVisible.value = false
720+
onFormClose()
721721
search()
722722
ElMessage({
723723
type: 'success',
@@ -731,7 +731,7 @@ const editTerm = () => {
731731
userApi
732732
.edit({ account, id, create_time, email, language, name, oid, oid_list, origin, status })
733733
.then(() => {
734-
dialogFormVisible.value = false
734+
onFormClose()
735735
search()
736736
ElMessage({
737737
type: 'success',

0 commit comments

Comments
 (0)