feat: 优化多会话内容

This commit is contained in:
ChenZhaoYu 2023-02-14 15:56:44 +08:00
parent 701ef0e6e1
commit cf8e2dd7b6
3 changed files with 37 additions and 14 deletions

View File

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

View File

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

View File

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