【增加】AI 对话删除,重置 Chat 聊天

This commit is contained in:
cherishsince 2024-05-16 23:21:31 +08:00
parent 934d5e7ca8
commit 222a841226

View File

@ -1,8 +1,11 @@
<template> <template>
<el-container class="ai-layout"> <el-container class="ai-layout">
<!-- 左侧会话列表 --> <!-- 左侧会话列表 -->
<Conversation @onConversationClick="handleConversationClick" <Conversation :active-id="activeConversationId"
@onConversationClear="handlerConversationClear" /> @onConversationClick="handleConversationClick"
@onConversationClear="handlerConversationClear"
@onConversationDelete="handlerConversationDelete"
/>
<!-- 右侧会话详情 --> <!-- 右侧会话详情 -->
<el-container class="detail-container"> <el-container class="detail-container">
<!-- 右顶部 TODO 芋艿右对齐 --> <!-- 右顶部 TODO 芋艿右对齐 -->
@ -30,62 +33,8 @@
<!-- main --> <!-- main -->
<el-main class="main-container"> <el-main class="main-container">
<div class="message-container" ref="messageContainer"> <div class="message-container" >
<div class="chat-list" v-for="(item, index) in list" :key="index"> <Message ref="messageRef" :list="list" />
<!-- 靠左 message -->
<!-- TODO 芋艿类型判断 -->
<div class="left-message message-item" v-if="item.type === 'system'">
<div class="avatar">
<el-avatar
src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
/>
</div>
<div class="message">
<div>
<el-text class="time">{{ formatDate(item.createTime) }}</el-text>
</div>
<div class="left-text-container" ref="markdownViewRef">
<MarkdownView class="left-text" :content="item.content" />
</div>
<div class="left-btns">
<div class="btn-cus" @click="noCopy(item.content)">
<img class="btn-image" src="../../../assets/ai/copy.svg"/>
<el-text class="btn-cus-text">复制</el-text>
</div>
<div class="btn-cus" style="margin-left: 20px" @click="onDelete(item.id)">
<img class="btn-image" src="@/assets/ai/delete.svg" style="height: 17px"/>
<el-text class="btn-cus-text">删除</el-text>
</div>
</div>
</div>
</div>
<!-- 靠右 message -->
<div class="right-message message-item" v-if="item.type === 'user'">
<div class="avatar">
<el-avatar
src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
/>
</div>
<div class="message">
<div>
<el-text class="time">{{ formatDate(item.createTime) }}</el-text>
</div>
<div class="right-text-container">
<div class="right-text">{{ item.content }}</div>
</div>
<div class="right-btns">
<div class="btn-cus" @click="noCopy(item.content)">
<img class="btn-image" src="@/assets/ai/copy.svg"/>
<el-text class="btn-cus-text">复制</el-text>
</div>
<div class="btn-cus" style="margin-left: 20px" @click="onDelete(item.id)">
<img class="btn-image" src="@/assets/ai/delete.svg" style="height: 17px"/>
<el-text class="btn-cus-text">删除</el-text>
</div>
</div>
</div>
</div>
</div>
</div> </div>
</el-main> </el-main>
<el-footer class="footer-container"> <el-footer class="footer-container">
@ -135,8 +84,9 @@
<script setup lang="ts"> <script setup lang="ts">
import MarkdownView from '@/components/MarkdownView/index.vue' import MarkdownView from '@/components/MarkdownView/index.vue'
import Conversation from './Conversation.vue' import Conversation from './Conversation.vue'
import Message from './Message.vue'
import {ChatMessageApi, ChatMessageVO} from '@/api/ai/chat/message' import {ChatMessageApi, ChatMessageVO} from '@/api/ai/chat/message'
import {ChatConversationVO} from '@/api/ai/chat/conversation' import {ChatConversationApi, ChatConversationVO} from '@/api/ai/chat/conversation'
import {formatDate} from '@/utils/formatTime' import {formatDate} from '@/utils/formatTime'
import {useClipboard} from '@vueuse/core' import {useClipboard} from '@vueuse/core'
import ChatConversationUpdateForm from "@/views/ai/chat/components/ChatConversationUpdateForm.vue"; import ChatConversationUpdateForm from "@/views/ai/chat/components/ChatConversationUpdateForm.vue";
@ -146,7 +96,7 @@ const message = useMessage() // 消息弹窗
const {copy} = useClipboard() // copy const {copy} = useClipboard() // copy
// ref // ref
const activeConversationId = ref<number | null>(null) // const activeConversationId = ref<string | null>(null) //
const activeConversation = ref<ChatConversationVO | null>(null) // Conversation const activeConversation = ref<ChatConversationVO | null>(null) // Conversation
const conversationInProgress = ref(false) // const conversationInProgress = ref(false) //
const conversationInAbortController = ref<any>() // abort ( stream ) const conversationInAbortController = ref<any>() // abort ( stream )
@ -155,6 +105,7 @@ const prompt = ref<string>() // prompt
// () // ()
const messageContainer: any = ref(null) const messageContainer: any = ref(null)
const messageRef = ref()
const isScrolling = ref(false) // const isScrolling = ref(false) //
const isComposing = ref(false) // const isComposing = ref(false) //
@ -164,14 +115,14 @@ const list = ref<ChatMessageVO[]>([]) // 列表的数据
// ============ ============== // ============ ==============
function scrollToBottom() { function scrollToBottom() {
nextTick(() => { // nextTick(() => {
//使nexttickdom // //使nexttickdom
console.log('isScrolling.value', isScrolling.value) // console.log('isScrolling.value', isScrolling.value)
if (!isScrolling.value) { // if (!isScrolling.value) {
messageContainer.value.scrollTop = // messageContainer.value.scrollTop =
messageContainer.value.scrollHeight - messageContainer.value.offsetHeight // messageContainer.value.scrollHeight - messageContainer.value.offsetHeight
} // }
}) // })
} }
function handleScroll() { function handleScroll() {
@ -179,6 +130,7 @@ function handleScroll() {
const scrollTop = scrollContainer.scrollTop const scrollTop = scrollContainer.scrollTop
const scrollHeight = scrollContainer.scrollHeight const scrollHeight = scrollContainer.scrollHeight
const offsetHeight = scrollContainer.offsetHeight const offsetHeight = scrollContainer.offsetHeight
console.log('scrollTop', scrollTop)
if ((scrollTop + offsetHeight) < (scrollHeight - 50)) { if ((scrollTop + offsetHeight) < (scrollHeight - 50)) {
// //
isScrolling.value = true isScrolling.value = true
@ -338,7 +290,8 @@ const getMessageList = async () => {
list.value = await ChatMessageApi.messageList(activeConversationId.value) list.value = await ChatMessageApi.messageList(activeConversationId.value)
// //
await nextTick(() => { await nextTick(() => {
scrollToBottom() //
messageRef.value.scrollToBottom(true)
}) })
} finally { } finally {
} }
@ -384,6 +337,8 @@ const handlerTitleSuccess = async () => {
* 对话 - 点击 * 对话 - 点击
*/ */
const handleConversationClick = async (conversation: ChatConversationVO) => { const handleConversationClick = async (conversation: ChatConversationVO) => {
//
isScrolling.value = false
// id // id
activeConversationId.value = conversation.id activeConversationId.value = conversation.id
activeConversation.value = conversation activeConversation.value = conversation
@ -400,12 +355,32 @@ const handlerConversationClear = async ()=> {
list.value = [] list.value = []
} }
/**
* 对话 - 删除
*/
const handlerConversationDelete = async (delConversation: ChatConversationVO) => {
//
if (activeConversationId.value === delConversation.id) {
await handlerConversationClear()
}
}
/**
* 对话 - 获取
*/
const getConversation = async (id: string) => {
const conversation: ChatConversationVO = await ChatConversationApi.getChatConversationMy(id)
return conversation
}
/** 初始化 **/ /** 初始化 **/
onMounted(async () => { onMounted(async () => {
// TODO conversationId // TODO conversationId
// if (route.query.conversationId) { if (route.query.conversationId) {
// conversationId.value = route.query.conversationId as number const id = route.query.conversationId as string
// } activeConversationId.value = id
activeConversation.value = await getConversation(id) as ChatConversationVO
}
// //
// await getChatConversationList() // await getChatConversationList()
// //
@ -415,18 +390,18 @@ onMounted(async () => {
// scrollToBottom(); // scrollToBottom();
// await nextTick // await nextTick
// //
messageContainer.value.addEventListener('scroll', handleScroll) // messageContainer.value.addEventListener('scroll', handleScroll)
// copy // copy
messageContainer.value.addEventListener('click', (e: any) => { // messageContainer.value.addEventListener('click', (e: any) => {
console.log(e) // console.log(e)
if (e.target.id === 'copy') { // if (e.target.id === 'copy') {
copy(e.target?.dataset?.copy) // copy(e.target?.dataset?.copy)
ElMessage({ // ElMessage({
message: '复制成功!', // message: '!',
type: 'success' // type: 'success'
}) // })
} // }
}) // })
}) })
</script> </script>