feat: 优化多会话内容

main
ChenZhaoYu 2 years ago
parent 701ef0e6e1
commit cf8e2dd7b6

@ -3,14 +3,21 @@ import { useHistoryStore } from '@/store'
export function useChat() { export function useChat() {
const historyStore = useHistoryStore() const historyStore = useHistoryStore()
function addChat(message: string, args?: { reversal?: boolean; error?: boolean; options?: Chat.ChatOptions }) { function addChat(
historyStore.addChat({ message: string,
dateTime: new Date().toLocaleString(), args?: { reversal?: boolean; error?: boolean; options?: Chat.ChatOptions },
message, uuid?: number | null,
reversal: args?.reversal ?? false, ) {
error: args?.error ?? false, historyStore.addChat(
options: args?.options ?? undefined, {
}) dateTime: new Date().toLocaleString(),
message,
reversal: args?.reversal ?? false,
error: args?.error ?? false,
options: args?.options ?? undefined,
},
uuid,
)
} }
function clearChat() { function clearChat() {

@ -1,5 +1,5 @@
<script setup lang='ts'> <script setup lang='ts'>
import { computed, nextTick, ref } from 'vue' import { computed, nextTick, ref, watch } from 'vue'
import { NButton, NInput, useMessage } from 'naive-ui' import { NButton, NInput, useMessage } from 'naive-ui'
import { Message } from './components' import { Message } from './components'
import { Layout } from './layout' import { Layout } from './layout'
@ -7,6 +7,7 @@ import { useChat } from './hooks/useChat'
import { fetchChatAPI } from '@/api' import { fetchChatAPI } from '@/api'
import { HoverButton, SvgIcon } from '@/components/common' import { HoverButton, SvgIcon } from '@/components/common'
import { useHistoryStore } from '@/store' import { useHistoryStore } from '@/store'
import { isNumber } from '@/utils/is'
const ms = useMessage() const ms = useMessage()
@ -19,6 +20,8 @@ const { addChat, clearChat } = useChat()
const prompt = ref('') const prompt = ref('')
const loading = ref(false) const loading = ref(false)
const currentActive = computed(() => historyStore.active)
const list = computed<Chat.Chat[]>(() => historyStore.getCurrentChat) const list = computed<Chat.Chat[]>(() => historyStore.getCurrentChat)
const chatList = computed<Chat.Chat[]>(() => list.value.filter(item => (!item.reversal && !item.error))) const chatList = computed<Chat.Chat[]>(() => list.value.filter(item => (!item.reversal && !item.error)))
@ -63,14 +66,26 @@ function handleEnter(event: KeyboardEvent) {
function addMessage( function addMessage(
message: string, message: string,
args?: { reversal?: boolean; error?: boolean; options?: Chat.ChatOptions }, args?: { reversal?: boolean; error?: boolean; options?: Chat.ChatOptions },
uuid?: number | null,
) { ) {
addChat(message, args) addChat(message, args, uuid)
nextTick(() => scrollRef.value && (scrollRef.value.scrollTop = scrollRef.value.scrollHeight)) nextTick(() => scrollRef.value && (scrollRef.value.scrollTop = scrollRef.value.scrollHeight))
} }
function handleClear() { function handleClear() {
clearChat() clearChat()
} }
watch(
currentActive,
(active: number | null) => {
if (isNumber(active)) {
loading.value = false
nextTick(() => scrollRef.value && (scrollRef.value.scrollTop = scrollRef.value.scrollHeight))
}
},
{ immediate: true },
)
</script> </script>
<template> <template>

@ -16,15 +16,16 @@ export const useHistoryStore = defineStore('history-store', {
}, },
}, },
actions: { actions: {
addChat(data: Chat.Chat) { addChat(data: Chat.Chat, uuid: number | null = null) {
if (this.active === null) { if (this.active === null) {
this.historyChat.push({ title: data.message, isEdit: false, data: [data] }) this.historyChat.push({ title: data.message, isEdit: false, data: [data] })
this.active = this.historyChat.length - 1 this.active = this.historyChat.length - 1
} }
else { else {
if (this.historyChat[this.active].title === '') const active = uuid !== null ? uuid : this.active
this.historyChat[this.active].title = data.message if (this.historyChat[active].title === '')
this.historyChat[this.active].data.push(data) this.historyChat[active].title = data.message
this.historyChat[active].data.push(data)
} }
setLocalHistory(this.$state) setLocalHistory(this.$state)
}, },

Loading…
Cancel
Save