Skip to content

Commit

Permalink
fix: fix error
Browse files Browse the repository at this point in the history
  • Loading branch information
FU-design committed Dec 3, 2024
1 parent 2a81ef9 commit c40a931
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
16 changes: 8 additions & 8 deletions bigtop-manager-ui/src/components/ai-assistant/chat-history.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@
})

watch(currThread, (val) => {
selectKey.value = [val.threadId || '']
selectKey.value = [val?.threadId || '']
})

watch(
() => [open.value, visible.value],
async ([open, visible]) => {
if (open || visible) {
await aiChatStore.getThreadsFromAuthPlatform()
Object.keys(currThread.value).length <= 0 && (currThread.value = threads.value[0])
selectKey.value = [currThread.value.threadId || '']
Object.keys(currThread.value || {}).length <= 0 && (currThread.value = threads.value[0])
selectKey.value = [currThread.value?.threadId || '']
}
},
{
Expand Down Expand Up @@ -109,12 +109,12 @@
if (threadCounts === 0) {
currThread.value = {}
} else {
if (idx >= threadCounts) {
currThread.value = threads.value[0]
} else {
const isEqual = thread.threadId === currThread.value.threadId
idx >= threadCounts && (currThread.value = threads.value[0])
if (isEqual) {
currThread.value = threads.value[idx]
aiChatStore.getThread(currThread.value.threadId as ThreadId)
}
aiChatStore.getThread(currThread.value.threadId as ThreadId)
}
}
}
Expand Down Expand Up @@ -210,7 +210,7 @@
</div>
</main>
<footer>
<a-button type="primary" :disabled="threadLimit" @click="aiChatStore.createChatThread">
<a-button type="primary" :disabled="threadLimit" @click="aiChatStore.createChatThread(false)">
{{ $t('aiAssistant.new_chat') }}
</a-button>
</footer>
Expand Down
5 changes: 4 additions & 1 deletion bigtop-manager-ui/src/components/ai-assistant/chat-input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@

const { t } = useI18n()
const aiChatStore = useAiChatStore()
const { isSending } = storeToRefs(aiChatStore)
const { isSending, threads, chatRecords } = storeToRefs(aiChatStore)
const chatMessage = ref('')

const sendMessage = async () => {
if (chatMessage.value === '') {
message.error(t('aiAssistant.message_cannot_be_empty'))
return
}
if (threads.value.length === 0) {
chatRecords.value = []
}
aiChatStore.setChatRecordForSender('USER', chatMessage.value)
aiChatStore.collectReceiveMessage(chatMessage.value)
chatMessage.value = ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
marginBottom: '16px'
})
const aiChatStore = useAiChatStore()
const { hasActivePlatform } = storeToRefs(aiChatStore)
const { hasActivePlatform, threads, chatRecords } = storeToRefs(aiChatStore)
const emptyState = usePngImage('ai_helper')
const disabledState = usePngImage('ai_disabled')

Expand All @@ -42,6 +42,9 @@
}

const quickAsk = (message: string) => {
if (threads.value.length === 0) {
chatRecords.value = []
}
aiChatStore.setChatRecordForSender('USER', message)
aiChatStore.collectReceiveMessage(message)
}
Expand Down
10 changes: 6 additions & 4 deletions bigtop-manager-ui/src/components/ai-assistant/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
const llmConfigStore = useLlmConfigStore()
const { currThread, chatRecords, threads, messageReceiver, hasActivePlatform, loadingChatRecords } =
storeToRefs(aiChatStore)
const { currAuthPlatform } = storeToRefs(llmConfigStore)

const open = ref(false)
const title = ref('aiAssistant.ai_assistant')
Expand All @@ -53,7 +54,7 @@
const smallChatHistoryRef = ref<InstanceType<typeof ChatHistory> | null>(null)

const width = computed(() => (fullScreen.value ? 'calc(100% - 300px)' : 450))
const noChat = computed(() => Object.keys(currThread.value).length === 0 || chatRecords.value.length === 0)
const noChat = computed(() => Object.keys(currThread.value || {}).length === 0 || chatRecords.value.length === 0)
const historyVisible = computed(() => fullScreen.value && open.value)
const historyType = computed(() => (historyVisible.value ? 'large' : 'small'))
const recordReceiver = computed((): ChatMessageItem => ({ sender: 'AI', message: messageReceiver.value }))
Expand All @@ -72,7 +73,7 @@
tip: 'new_chat',
icon: addIcon.value,
action: 'ADD',
clickEvent: aiChatStore.createChatThread,
clickEvent: () => aiChatStore.createChatThread(),
disabled: addState.value
},
{
Expand Down Expand Up @@ -115,6 +116,7 @@

const controlVisible = (visible: boolean = true) => {
open.value = visible
fullScreen.value = false
}

const openRecord = () => {
Expand All @@ -129,7 +131,7 @@
actionGroup.value.forEach(({ action, clickEvent }) => {
action && actionHandlers.value.set(action, clickEvent || (() => {}))
})
!hasActivePlatform.value && (await llmConfigStore.getAuthorizedPlatforms())
!hasActivePlatform.value && (await llmConfigStore.getAuthorizedPlatforms(false))
aiChatStore.initCurrThread()
}

Expand Down Expand Up @@ -187,7 +189,7 @@
<template v-if="hasActivePlatform" #footer>
<chat-input />
<a-typography-text type="secondary" class="ai-assistant-desc">
{{ currThread?.platformName }} {{ currThread?.model }}
{{ currAuthPlatform?.platformName }} {{ currAuthPlatform?.model }}
</a-typography-text>
</template>
</a-drawer>
Expand Down
16 changes: 11 additions & 5 deletions bigtop-manager-ui/src/store/ai-assistant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const useAiChatStore = defineStore(
const threadLimit = computed(() => threads.value.length >= 10)

watch(currAuthPlatform, (val, oldVal) => {
if (val == undefined) {
if (val == undefined || !hasActivePlatform.value) {
currThread.value = {}
} else if (val.llmConfigId !== oldVal?.llmConfigId) {
currThread.value = {}
Expand All @@ -61,6 +61,9 @@ export const useAiChatStore = defineStore(
}

const initCurrThread = () => {
if (!hasActivePlatform.value) {
return
}
if (Object.keys(currThread.value).length == 0) {
if (threads.value.length === 0) {
createChatThread()
Expand All @@ -74,12 +77,12 @@ export const useAiChatStore = defineStore(
}
}

const createChatThread = async () => {
const createChatThread = async (quickCreate = false) => {
try {
const tempName = `thread-${getRandomFromTimestamp()}`
const data = await ai.createChatThread({ id: null, name: tempName })
if (data) {
getThread(data.threadId as ThreadId)
await getThread(data.threadId as ThreadId, quickCreate)
getThreadsFromAuthPlatform()
}
} catch (error) {
Expand Down Expand Up @@ -107,11 +110,11 @@ export const useAiChatStore = defineStore(
}
}

const getThread = async (threadId: ThreadId) => {
const getThread = async (threadId: ThreadId, quickCreate = false) => {
try {
const data = await ai.getThread(threadId)
currThread.value = data
getThreadRecords()
!quickCreate && (await getThreadRecords())
} catch (error) {
console.log('error :>> ', error)
}
Expand Down Expand Up @@ -156,6 +159,9 @@ export const useAiChatStore = defineStore(

const collectReceiveMessage = async (message: string) => {
try {
if (threads.value.length === 0) {
await createChatThread(true)
}
const res = await talkWithChatbot(message)
setChatRecordForSender('AI', res?.message || '')
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions bigtop-manager-ui/src/store/llm-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ export const useLlmConfigStore = defineStore(
} as UpdateAuthorizedPlatformConfig
}

const getAuthorizedPlatforms = async () => {
const getAuthorizedPlatforms = async (hasLoading = true) => {
try {
loading.value = true
hasLoading && (loading.value = true)
authorizedPlatforms.value = await llmServer.getAuthorizedPlatforms()
} catch (error) {
console.log('error :>> ', error)
Expand Down

0 comments on commit c40a931

Please sign in to comment.