【增加】stream 聊天进行中删除,调用 stream 控制器中断

This commit is contained in:
cherishsince 2024-05-12 22:22:34 +08:00
parent 151076a79e
commit c1b9baf8b6

View File

@ -147,11 +147,13 @@
</div> </div>
</el-main> </el-main>
<el-footer class="footer-container"> <el-footer class="footer-container">
<textarea class="prompt-input" v-model="prompt" placeholder="问我任何问题...Shift+Enter 换行,按下 Enter 发送)"></textarea> <form @submit.prevent="onSend" class="prompt-from">
<textarea class="prompt-input" v-model="prompt" @keyup.enter="onSend" placeholder="问我任何问题...Shift+Enter 换行,按下 Enter 发送)"></textarea>
<div class="prompt-btns"> <div class="prompt-btns">
<el-switch/> <el-switch/>
<el-button type="primary" size="default" @click="onSend()">发送</el-button> <el-button type="primary" size="default" @click="onSend()">发送</el-button>
</div> </div>
</form>
</el-footer> </el-footer>
</el-container> </el-container>
</el-container> </el-container>
@ -162,11 +164,23 @@ import {ChatMessageApi, ChatMessageSendVO, ChatMessageVO} from "@/api/ai/chat/me
import {formatDate} from "@/utils/formatTime"; import {formatDate} from "@/utils/formatTime";
import {useClipboard} from '@vueuse/core' import {useClipboard} from '@vueuse/core'
// copy
const { copy } = useClipboard();
const searchName = ref('') // const searchName = ref('') //
const conversationId = ref('1781604279872581648') // id const conversationId = ref('1781604279872581648') // id
const conversationInProgress = ref<Boolean>() // const conversationInProgress = ref<Boolean>() //
const conversationInAbortController = ref<any>() // abort ( stream )
const prompt = ref<string>() // prompt const prompt = ref<string>() // prompt
const promptRes = ref<string>() // prompt res
// ()
const messageContainer: any = ref(null);
const isScrolling = ref(false)//
/** chat message 列表 */
defineOptions({ name: 'chatMessageList' })
const list = ref<ChatMessageVO[]>([]) //
const changeConversation = (conversation) => { const changeConversation = (conversation) => {
console.log(conversation) console.log(conversation)
@ -190,30 +204,40 @@ const searchConversation = () => {
/** send */ /** send */
const onSend = async () => { const onSend = async () => {
const content = prompt.value;
//
prompt.value = ''
const requestParams = { const requestParams = {
conversationId: conversationId.value, conversationId: conversationId.value,
content: prompt.value, content: content,
} as unknown as ChatMessageSendVO } as unknown as ChatMessageSendVO
// message // message
const userMessage = await ChatMessageApi.add(requestParams) as unknown as ChatMessageVO; const userMessage = await ChatMessageApi.add(requestParams) as unknown as ChatMessageVO;
list.value.push(userMessage) list.value.push(userMessage)
// //
scrollToBottom(); scrollToBottom();
// // stream
await doSendStream(userMessage) await doSendStream(userMessage)
//
} }
const doSendStream = async (userMessage: ChatMessageVO) => { const doSendStream = async (userMessage: ChatMessageVO) => {
// AbortController便 // AbortController便
const ctrl = new AbortController() conversationInAbortController.value = new AbortController()
//
conversationInProgress.value = true
try {
// event stream // event stream
let isFirstMessage = true let isFirstMessage = true
ChatMessageApi.sendStream(userMessage.id, ctrl,(message) => { ChatMessageApi.sendStream(userMessage.id, conversationInAbortController.value,(message) => {
console.log('message', message) console.log('message', message)
const data = JSON.parse(message.data) as unknown as ChatMessageVO const data = JSON.parse(message.data) as unknown as ChatMessageVO
// //
if (data.content === '') { if (data.content === '') {
ctrl.abort() //
conversationInProgress.value = false;
// stream
conversationInAbortController.value.abort()
} }
// message // message
if (isFirstMessage) { if (isFirstMessage) {
@ -228,44 +252,23 @@ const doSendStream = async (userMessage: ChatMessageVO) => {
scrollToBottom(); scrollToBottom();
}, (error) => { }, (error) => {
console.log('error', error) console.log('error', error)
//
conversationInProgress.value = false;
// stream
conversationInAbortController.value.abort()
}, () => { }, () => {
console.log('close') console.log('close')
//
conversationInProgress.value = false;
// stream
conversationInAbortController.value.abort()
}) })
} finally {
}
// // message
// const chatMessage = {
// id: null, //
// conversationId: conversationId.value, //
// type: 'system', //
// userId: null, //
// roleId: null, //
// model: null, //
// modelId: null, //
// content: '...', //
// tokens: null, // Token
// createTime: new Date(), //
// } as unknown as ChatMessageVO
// list.value.push(chatMessage)
// //
// scrollToBottom();
} }
/** Prompt */
const onPromptInput = async (e) => {
console.log(e.data)
// prompt.value = e.data
}
// copy
const { copy, isSupported } = useClipboard();
/** chat message 列表 */
defineOptions({ name: 'chatMessageList' })
const list = ref<ChatMessageVO[]>([]) //
// id TODO @
const content = '苹果是什么颜色?'
/** 查询列表 */ /** 查询列表 */
const messageList = async () => { const messageList = async () => {
try { try {
@ -277,9 +280,7 @@ const messageList = async () => {
} finally { } finally {
} }
} }
// ref
const messageContainer: any = ref(null);
const isScrolling = ref(false)//
function scrollToBottom() { function scrollToBottom() {
nextTick(() => { nextTick(() => {
@ -321,6 +322,10 @@ const onDelete = async (id) => {
message: '删除成功!', message: '删除成功!',
type: 'success', type: 'success',
}) })
// tip stream message controller
if (conversationInAbortController.value) {
conversationInAbortController.value.abort()
}
// message // message
await messageList(); await messageList();
} }
@ -525,8 +530,8 @@ onMounted(async () => {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
overflow-wrap: break-word; overflow-wrap: break-word;
background-color: #e4e4e4; background-color: rgba(228, 228, 228, 0.80);
box-shadow: 0 0 0 1px #e4e4e4; box-shadow: 0 0 0 1px rgba(228, 228, 228, 0.80);
border-radius: 10px; border-radius: 10px;
padding: 10px 10px 5px 10px; padding: 10px 10px 5px 10px;
@ -584,6 +589,13 @@ onMounted(async () => {
// //
.footer-container { .footer-container {
display: flex;
flex-direction: column;
height: auto;
margin: 0;
padding: 0;
.prompt-from {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
height: auto; height: auto;
@ -591,6 +603,7 @@ onMounted(async () => {
border-radius: 10px; border-radius: 10px;
margin: 20px 20px; margin: 20px 20px;
padding: 9px 10px; padding: 9px 10px;
}
.prompt-input { .prompt-input {
height: 80px; height: 80px;