【增加】AI Chat 抽离 Message

pull/449/MERGE
cherishsince 2024-05-16 23:39:31 +08:00
parent 222a841226
commit c3884c5dfd
2 changed files with 297 additions and 174 deletions

View File

@ -0,0 +1,269 @@
<template>
<div ref="messageContainer" style="height: 100%;overflow-y: auto;">
<div class="chat-list" v-for="(item, index) in list" :key="index" >
<!-- 靠左 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>
</template>
<script setup lang="ts">
import {formatDate} from "@/utils/formatTime";
import MarkdownView from "@/components/MarkdownView/index.vue";
import {ChatMessageApi, ChatMessageVO} from "@/api/ai/chat/message";
import {useClipboard} from "@vueuse/core";
import {PropType} from "vue";
const {copy} = useClipboard() // copy
// ()
const messageContainer: any = ref(null)
const isScrolling = ref(false) //
// props
const props = defineProps({
list: {
type: Array as PropType<ChatMessageVO[]>,
required: true
}
})
// ============ ==============
const scrollToBottom = async (isIgnore?: boolean) =>{
await nextTick(() => {
//使nexttickdom
if (isIgnore || !isScrolling.value) {
messageContainer.value.scrollTop =
messageContainer.value.scrollHeight - messageContainer.value.offsetHeight
}
})
}
function handleScroll() {
const scrollContainer = messageContainer.value
const scrollTop = scrollContainer.scrollTop
const scrollHeight = scrollContainer.scrollHeight
const offsetHeight = scrollContainer.offsetHeight
console.log('scrollTop', scrollTop)
if ((scrollTop + offsetHeight) < (scrollHeight - 100)) {
//
isScrolling.value = true
} else {
//
isScrolling.value = false
}
}
/**
* 复制
*/
const noCopy = async (content) => {
copy(content)
ElMessage({
message: '复制成功!',
type: 'success'
})
}
/**
* 删除
*/
const onDelete = async (id) => {
// message
await ChatMessageApi.delete(id)
ElMessage({
message: '删除成功!',
type: 'success'
})
//
emits('onDeleteSuccess')
}
// list
const { list, conversationId } = toRefs(props)
watch(list, async (newValue, oldValue) => {
console.log('watch list', list)
})
// parent
defineExpose({scrollToBottom})
//
const emits = defineEmits(['onDeleteSuccess'])
//
onMounted(async () => {
messageContainer.value.addEventListener('scroll', handleScroll)
})
</script>
<style scoped lang="scss">
.message-container {
position: relative;
//top: 0;
//bottom: 0;
//left: 0;
//right: 0;
//width: 100%;
//height: 100%;
overflow-y: scroll;
padding: 0 15px;
//z-index: -1;
}
//
.chat-list {
display: flex;
flex-direction: column;
overflow-y: hidden;
.message-item {
margin-top: 50px;
}
.left-message {
display: flex;
flex-direction: row;
}
.right-message {
display: flex;
flex-direction: row-reverse;
justify-content: flex-start;
}
.avatar {
//height: 170px;
//width: 170px;
}
.message {
display: flex;
flex-direction: column;
text-align: left;
margin: 0 15px;
.time {
text-align: left;
line-height: 30px;
}
.left-text-container {
display: flex;
flex-direction: column;
overflow-wrap: break-word;
background-color: rgba(228, 228, 228, 0.8);
box-shadow: 0 0 0 1px rgba(228, 228, 228, 0.8);
border-radius: 10px;
padding: 10px 10px 5px 10px;
.left-text {
color: #393939;
font-size: 0.95rem;
}
}
.right-text-container {
display: flex;
flex-direction: row-reverse;
.right-text {
font-size: 0.95rem;
color: #fff;
display: inline;
background-color: #267fff;
color: #fff;
box-shadow: 0 0 0 1px #267fff;
border-radius: 10px;
padding: 10px;
width: auto;
overflow-wrap: break-word;
}
}
.left-btns,
.right-btns {
display: flex;
flex-direction: row;
margin-top: 8px;
}
}
//
.btn-cus {
display: flex;
background-color: transparent;
align-items: center;
.btn-image {
height: 20px;
margin-right: 5px;
}
.btn-cus-text {
color: #757575;
}
}
.btn-cus:hover {
cursor: pointer;
}
.btn-cus:focus {
background-color: #8c939d;
}
}
</style>

View File

@ -34,7 +34,7 @@
<!-- main -->
<el-main class="main-container">
<div class="message-container" >
<Message ref="messageRef" :list="list" />
<Message ref="messageRef" :list="list" @onDeleteSuccess="handlerMessageDelete" />
</div>
</el-main>
<el-footer class="footer-container">
@ -82,12 +82,10 @@
</template>
<script setup lang="ts">
import MarkdownView from '@/components/MarkdownView/index.vue'
import Conversation from './Conversation.vue'
import Message from './Message.vue'
import {ChatMessageApi, ChatMessageVO} from '@/api/ai/chat/message'
import {ChatConversationApi, ChatConversationVO} from '@/api/ai/chat/conversation'
import {formatDate} from '@/utils/formatTime'
import {useClipboard} from '@vueuse/core'
import ChatConversationUpdateForm from "@/views/ai/chat/components/ChatConversationUpdateForm.vue";
@ -104,9 +102,7 @@ const inputTimeout = ref<any>() // 处理输入中回车的定时器
const prompt = ref<string>() // prompt
// ()
const messageContainer: any = ref(null)
const messageRef = ref()
const isScrolling = ref(false) //
const isComposing = ref(false) //
/** chat message 列表 */
@ -114,30 +110,10 @@ const list = ref<ChatMessageVO[]>([]) // 列表的数据
// ============ ==============
function scrollToBottom() {
// nextTick(() => {
// //使nexttickdom
// console.log('isScrolling.value', isScrolling.value)
// if (!isScrolling.value) {
// messageContainer.value.scrollTop =
// messageContainer.value.scrollHeight - messageContainer.value.offsetHeight
// }
// })
}
function handleScroll() {
const scrollContainer = messageContainer.value
const scrollTop = scrollContainer.scrollTop
const scrollHeight = scrollContainer.scrollHeight
const offsetHeight = scrollContainer.offsetHeight
console.log('scrollTop', scrollTop)
if ((scrollTop + offsetHeight) < (scrollHeight - 50)) {
//
isScrolling.value = true
} else {
//
isScrolling.value = false
}
function scrollToBottom(isIgnore?: boolean) {
nextTick(() => {
messageRef.value.scrollToBottom(isIgnore)
})
}
// ============= =============
@ -203,7 +179,7 @@ const onSend = async () => {
} as ChatMessageVO
// list.value.push(userMessage)
//
scrollToBottom()
await scrollToBottom()
// stream
await doSendStream(userMessage)
}
@ -221,7 +197,7 @@ const doSendStream = async (userMessage: ChatMessageVO) => {
userMessage.conversationId, // TODO
userMessage.content,
conversationInAbortController.value,
(message) => {
async (message) => {
console.log('message', message)
const data = JSON.parse(message.data) // TODO
// debugger
@ -246,7 +222,7 @@ const doSendStream = async (userMessage: ChatMessageVO) => {
list.value[list.value - 1] = lastMessage
}
//
scrollToBottom()
await scrollToBottom()
},
(error) => {
console.log('error', error)
@ -291,34 +267,12 @@ const getMessageList = async () => {
//
await nextTick(() => {
//
messageRef.value.scrollToBottom(true)
scrollToBottom()
})
} finally {
}
}
function noCopy(content) {
copy(content)
ElMessage({
message: '复制成功!',
type: 'success'
})
}
const onDelete = async (id) => {
// message
await ChatMessageApi.delete(id)
ElMessage({
message: '删除成功!',
type: 'success'
})
// tip stream message controller
await stopStream()
// message
await getMessageList()
}
/** 修改聊天会话 */
const chatConversationUpdateFormRef = ref()
const openChatConversationUpdateForm = async () => {
@ -337,13 +291,13 @@ const handlerTitleSuccess = async () => {
* 对话 - 点击
*/
const handleConversationClick = async (conversation: ChatConversationVO) => {
//
isScrolling.value = false
// id
activeConversationId.value = conversation.id
activeConversation.value = conversation
// message
await getMessageList()
//
scrollToBottom(true)
}
/**
@ -373,6 +327,13 @@ const getConversation = async (id: string) => {
return conversation
}
// ============ message ===========
const handlerMessageDelete = async () => {
// message
await getMessageList()
}
/** 初始化 **/
onMounted(async () => {
// TODO conversationId
@ -381,10 +342,6 @@ onMounted(async () => {
activeConversationId.value = id
activeConversation.value = await getConversation(id) as ChatConversationVO
}
//
// await getChatConversationList()
//
// await getConversation(conversationId.value)
//
await getMessageList()
// scrollToBottom();
@ -541,120 +498,17 @@ onMounted(async () => {
margin: 0;
padding: 0;
position: relative;
}
.message-container {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
//width: 100%;
//height: 100%;
overflow-y: scroll;
padding: 0 15px;
}
//
.chat-list {
display: flex;
flex-direction: column;
overflow-y: hidden;
.message-item {
margin-top: 50px;
}
.left-message {
display: flex;
flex-direction: row;
}
.right-message {
display: flex;
flex-direction: row-reverse;
justify-content: flex-start;
}
.avatar {
//height: 170px;
//width: 170px;
}
.message {
display: flex;
flex-direction: column;
text-align: left;
margin: 0 15px;
.time {
text-align: left;
line-height: 30px;
}
.left-text-container {
display: flex;
flex-direction: column;
overflow-wrap: break-word;
background-color: rgba(228, 228, 228, 0.8);
box-shadow: 0 0 0 1px rgba(228, 228, 228, 0.8);
border-radius: 10px;
padding: 10px 10px 5px 10px;
.left-text {
color: #393939;
font-size: 0.95rem;
}
}
.right-text-container {
display: flex;
flex-direction: row-reverse;
.right-text {
font-size: 0.95rem;
color: #fff;
display: inline;
background-color: #267fff;
color: #fff;
box-shadow: 0 0 0 1px #267fff;
border-radius: 10px;
padding: 10px;
width: auto;
overflow-wrap: break-word;
}
}
.left-btns,
.right-btns {
display: flex;
flex-direction: row;
margin-top: 8px;
}
}
//
.btn-cus {
display: flex;
background-color: transparent;
align-items: center;
.btn-image {
height: 20px;
margin-right: 5px;
}
.btn-cus-text {
color: #757575;
}
}
.btn-cus:hover {
cursor: pointer;
}
.btn-cus:focus {
background-color: #8c939d;
.message-container {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
//width: 100%;
//height: 100%;
overflow-y: scroll;
padding: 0 15px;
}
}