diff --git a/src/components/business/Chat/hooks/useChat.ts b/src/components/business/Chat/hooks/useChat.ts index 7e7ea5e..c8d6f73 100644 --- a/src/components/business/Chat/hooks/useChat.ts +++ b/src/components/business/Chat/hooks/useChat.ts @@ -6,7 +6,6 @@ export function useChat() { function addChat( message: string, args?: { reversal?: boolean; error?: boolean; options?: Chat.ChatOptions }, - uuid?: number | null, ) { historyStore.addChat( { @@ -16,7 +15,6 @@ export function useChat() { error: args?.error ?? false, options: args?.options ?? undefined, }, - uuid, ) } diff --git a/src/components/business/Chat/index.vue b/src/components/business/Chat/index.vue index 29fd73a..8d9156c 100644 --- a/src/components/business/Chat/index.vue +++ b/src/components/business/Chat/index.vue @@ -7,7 +7,6 @@ import { useChat } from './hooks/useChat' import { fetchChatAPI } from '@/api' import { HoverButton, SvgIcon } from '@/components/common' import { useHistoryStore } from '@/store' -import { isNumber } from '@/utils/is' let controller = new AbortController() @@ -23,6 +22,7 @@ const prompt = ref('') const loading = ref(false) const currentActive = computed(() => historyStore.active) +const heartbeat = computed(() => historyStore.heartbeat) const list = computed(() => historyStore.getCurrentChat) const chatList = computed(() => list.value.filter(item => (!item.reversal && !item.error))) @@ -71,9 +71,8 @@ function handleEnter(event: KeyboardEvent) { function addMessage( message: string, args?: { reversal?: boolean; error?: boolean; options?: Chat.ChatOptions }, - uuid?: number | null, ) { - addChat(message, args, uuid) + addChat(message, args) scrollToBottom() } @@ -96,10 +95,18 @@ onMounted(() => { scrollToBottom() }) +watch( + heartbeat, + () => { + handleCancel() + scrollToBottom() + }, +) + watch( currentActive, - (active) => { - if (isNumber(active)) { + (_, oldActive) => { + if (oldActive !== null) { handleCancel() scrollToBottom() } diff --git a/src/components/business/Chat/layout/sider/List.vue b/src/components/business/Chat/layout/sider/List.vue index 000ff96..eacf839 100644 --- a/src/components/business/Chat/layout/sider/List.vue +++ b/src/components/business/Chat/layout/sider/List.vue @@ -12,17 +12,21 @@ function handleSelect(index: number) { historyStore.chooseHistory(index) } -function handleEdit(index: number, isEdit: boolean) { +function handleEdit(index: number, isEdit: boolean, event?: MouseEvent) { historyStore.editHistory(index, isEdit) + event?.stopPropagation() } -function handleRemove(index: number) { +function handleRemove(index: number, event?: MouseEvent) { historyStore.removeHistory(index) + event?.stopPropagation() } function handleEnter(index: number, isEdit: boolean, event: KeyboardEvent) { - if (event.key === 'Enter') + if (event.key === 'Enter') { handleEdit(index, isEdit) + event.stopPropagation() + } } @@ -31,8 +35,8 @@ function handleEnter(index: number, isEdit: boolean, event: KeyboardEvent) {
@@ -45,17 +49,17 @@ function handleEnter(index: number, isEdit: boolean, event: KeyboardEvent) { /> {{ item.title }}
-
+
diff --git a/src/store/modules/history/helper.ts b/src/store/modules/history/helper.ts index 635ba9c..de87985 100644 --- a/src/store/modules/history/helper.ts +++ b/src/store/modules/history/helper.ts @@ -5,10 +5,11 @@ const LOCAL_NAME = 'historyChat' export interface HistoryState { historyChat: Chat.HistoryChat[] active: number | null + heartbeat: boolean } export function defaultSetting() { - return { historyChat: [], active: null } + return { historyChat: [], active: null, heartbeat: false } } export function getLocalHistory() { diff --git a/src/store/modules/history/index.ts b/src/store/modules/history/index.ts index b9ceb5e..2539639 100644 --- a/src/store/modules/history/index.ts +++ b/src/store/modules/history/index.ts @@ -1,6 +1,7 @@ import { defineStore } from 'pinia' import type { HistoryState } from './helper' import { getLocalHistory, setLocalHistory } from './helper' + export const useHistoryStore = defineStore('history-store', { state: (): HistoryState => getLocalHistory(), getters: { @@ -18,16 +19,16 @@ export const useHistoryStore = defineStore('history-store', { }, }, actions: { - addChat(data: Chat.Chat, uuid: number | null = null) { + addChat(data: Chat.Chat) { if (this.active === null) { this.historyChat.push({ title: data.message, isEdit: false, data: [data] }) this.active = this.historyChat.length - 1 } else { - const active = uuid !== null ? uuid : this.active - if (this.historyChat[active].title === 'New Chat') - this.historyChat[active].title = data.message - this.historyChat[active].data.push(data) + if (this.historyChat[this.active].title === 'New Chat') + this.historyChat[this.active].title = data.message + + this.historyChat[this.active].data.push(data) } setLocalHistory(this.$state) }, @@ -52,14 +53,19 @@ export const useHistoryStore = defineStore('history-store', { removeHistory(index: number) { this.historyChat.splice(index, 1) + if (this.active === index) { if (this.historyChat.length === 0) this.active = null else if (this.active === this.historyChat.length) - this.active-- - else - this.active = 0 + this.active = this.historyChat.length - 1 } + + if (this.historyChat.length === 0) + this.active = null + + this.toggleHeartbeat() + setLocalHistory(this.$state) }, @@ -69,5 +75,9 @@ export const useHistoryStore = defineStore('history-store', { this.active = index setLocalHistory(this.$state) }, + + toggleHeartbeat() { + this.heartbeat = !this.heartbeat + }, }, }) diff --git a/vite.config.ts b/vite.config.ts index 81bf35b..affdf9f 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -15,7 +15,7 @@ export default defineConfig((env) => { server: { port: 1002, host: '0.0.0.0', - open: true, + open: false, proxy: { '/api': { target: viteEnv.VITE_APP_API_BASE_URL,