feat: 流畅对话

This commit is contained in:
shaohuzhang1 2023-11-29 13:00:31 +08:00
parent fcf04ee234
commit 8a91676ccf
2 changed files with 69 additions and 20 deletions

View File

@ -1,3 +1,4 @@
import { type Ref } from 'vue'
interface ApplicationFormType { interface ApplicationFormType {
name?: string name?: string
desc?: string desc?: string
@ -11,5 +12,42 @@ interface chatType {
id: string id: string
problem_text: string problem_text: string
answer_text: string answer_text: string
buffer: Array<String>
}
export class ChatManage {
id?: NodeJS.Timer
ms: number
chat: chatType
is_close?: boolean
loading?: Ref<boolean>
constructor(chat: chatType, ms?: number, loading?: Ref<boolean>) {
this.ms = ms ? ms : 10
this.chat = chat
this.loading = loading
}
write() {
this.id = setInterval(() => {
const s = this.chat.buffer.shift()
if (s !== undefined) {
this.chat.answer_text = this.chat.answer_text + s
} else {
if (this.is_close) {
clearInterval(this.id)
if (this.loading) {
this.loading.value = false
}
}
}
}, this.ms)
}
close() {
this.is_close = true
}
append(answer_text_block: string) {
for (let index = 0; index < answer_text_block.length; index++) {
this.chat.buffer.push(answer_text_block[index])
}
}
} }
export type { ApplicationFormType, chatType } export type { ApplicationFormType, chatType }

View File

@ -100,7 +100,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, nextTick, onUpdated } from 'vue' import { ref, nextTick, onUpdated } from 'vue'
import applicationApi from '@/api/application' import applicationApi from '@/api/application'
import type { chatType } from '@/api/type/application' import { ChatManage, type chatType } from '@/api/type/application'
import { randomId } from '@/utils/utils' import { randomId } from '@/utils/utils'
const props = defineProps({ const props = defineProps({
data: { data: {
@ -158,27 +158,38 @@ function chatMessage() {
if (!chartOpenId.value) { if (!chartOpenId.value) {
getChartOpenId() getChartOpenId()
} else { } else {
applicationApi.postChatMessage(chartOpenId.value, inputValue.value).then(async (response) => { const problem_text = inputValue.value
const randomNum = randomId() inputValue.value = ''
applicationApi.postChatMessage(chartOpenId.value, problem_text).then(async (response) => {
const id = randomId()
chatList.value.push({ chatList.value.push({
id: randomNum, id: id,
problem_text: inputValue.value, problem_text: problem_text,
answer_text: '' answer_text: '',
buffer: []
}) })
inputValue.value = '' const row = chatList.value.find((item) => item.id === id)
const reader = response.body.getReader() if (row) {
while (true) { const chatMange = new ChatManage(row, 50, loading)
const { done, value } = await reader.read() chatMange.write()
if (done) { const reader = response.body.getReader()
loading.value = false while (true) {
break const { done, value } = await reader.read()
} if (done) {
const decoder = new TextDecoder('utf-8') chatMange.close()
const str = decoder.decode(value, { stream: true }) break
// console.log(JSON?.parse(str.replace('data:', ''))) }
const content = JSON?.parse(str.replace('data:', ''))?.content try {
if (content) { const decoder = new TextDecoder('utf-8')
chatList.value[chatList.value.findIndex((v) => v.id === randomNum)].answer_text += content const str = decoder.decode(value, { stream: true })
if (str && str.startsWith('data:')) {
// console.log(JSON?.parse(str.replace('data:', '')))
const content = JSON?.parse(str.replace('data:', ''))?.content
if (content) {
chatMange.append(content)
}
}
} catch (e) {}
} }
} }
}) })