From c40a931d35f1c205e8db5d6ef4a3781532314aac Mon Sep 17 00:00:00 2001
From: fuliqiang <1348994179@qq.com>
Date: Tue, 3 Dec 2024 19:41:38 +0800
Subject: [PATCH] fix: fix error
---
.../src/components/ai-assistant/chat-history.vue | 16 ++++++++--------
.../src/components/ai-assistant/chat-input.vue | 5 ++++-
.../components/ai-assistant/empty-content.vue | 5 ++++-
.../src/components/ai-assistant/index.vue | 10 ++++++----
.../src/store/ai-assistant/index.ts | 16 +++++++++++-----
bigtop-manager-ui/src/store/llm-config/index.ts | 4 ++--
6 files changed, 35 insertions(+), 21 deletions(-)
diff --git a/bigtop-manager-ui/src/components/ai-assistant/chat-history.vue b/bigtop-manager-ui/src/components/ai-assistant/chat-history.vue
index c50de74b..ae18febc 100644
--- a/bigtop-manager-ui/src/components/ai-assistant/chat-history.vue
+++ b/bigtop-manager-ui/src/components/ai-assistant/chat-history.vue
@@ -61,7 +61,7 @@
})
watch(currThread, (val) => {
- selectKey.value = [val.threadId || '']
+ selectKey.value = [val?.threadId || '']
})
watch(
@@ -69,8 +69,8 @@
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 || '']
}
},
{
@@ -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)
}
}
}
@@ -210,7 +210,7 @@
diff --git a/bigtop-manager-ui/src/components/ai-assistant/chat-input.vue b/bigtop-manager-ui/src/components/ai-assistant/chat-input.vue
index b33d11bd..1b0b085b 100644
--- a/bigtop-manager-ui/src/components/ai-assistant/chat-input.vue
+++ b/bigtop-manager-ui/src/components/ai-assistant/chat-input.vue
@@ -26,7 +26,7 @@
const { t } = useI18n()
const aiChatStore = useAiChatStore()
- const { isSending } = storeToRefs(aiChatStore)
+ const { isSending, threads, chatRecords } = storeToRefs(aiChatStore)
const chatMessage = ref('')
const sendMessage = async () => {
@@ -34,6 +34,9 @@
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 = ''
diff --git a/bigtop-manager-ui/src/components/ai-assistant/empty-content.vue b/bigtop-manager-ui/src/components/ai-assistant/empty-content.vue
index 026a8078..bb63bd66 100644
--- a/bigtop-manager-ui/src/components/ai-assistant/empty-content.vue
+++ b/bigtop-manager-ui/src/components/ai-assistant/empty-content.vue
@@ -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')
@@ -42,6 +42,9 @@
}
const quickAsk = (message: string) => {
+ if (threads.value.length === 0) {
+ chatRecords.value = []
+ }
aiChatStore.setChatRecordForSender('USER', message)
aiChatStore.collectReceiveMessage(message)
}
diff --git a/bigtop-manager-ui/src/components/ai-assistant/index.vue b/bigtop-manager-ui/src/components/ai-assistant/index.vue
index 7c55daa0..f9d7de08 100644
--- a/bigtop-manager-ui/src/components/ai-assistant/index.vue
+++ b/bigtop-manager-ui/src/components/ai-assistant/index.vue
@@ -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')
@@ -53,7 +54,7 @@
const smallChatHistoryRef = ref | 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 }))
@@ -72,7 +73,7 @@
tip: 'new_chat',
icon: addIcon.value,
action: 'ADD',
- clickEvent: aiChatStore.createChatThread,
+ clickEvent: () => aiChatStore.createChatThread(),
disabled: addState.value
},
{
@@ -115,6 +116,7 @@
const controlVisible = (visible: boolean = true) => {
open.value = visible
+ fullScreen.value = false
}
const openRecord = () => {
@@ -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()
}
@@ -187,7 +189,7 @@
- {{ currThread?.platformName }} {{ currThread?.model }}
+ {{ currAuthPlatform?.platformName }} {{ currAuthPlatform?.model }}
diff --git a/bigtop-manager-ui/src/store/ai-assistant/index.ts b/bigtop-manager-ui/src/store/ai-assistant/index.ts
index 1ee6498f..35fb6b81 100644
--- a/bigtop-manager-ui/src/store/ai-assistant/index.ts
+++ b/bigtop-manager-ui/src/store/ai-assistant/index.ts
@@ -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 = {}
@@ -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()
@@ -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) {
@@ -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)
}
@@ -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) {
diff --git a/bigtop-manager-ui/src/store/llm-config/index.ts b/bigtop-manager-ui/src/store/llm-config/index.ts
index 2cfbc906..1249badf 100644
--- a/bigtop-manager-ui/src/store/llm-config/index.ts
+++ b/bigtop-manager-ui/src/store/llm-config/index.ts
@@ -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)