【增加】AI Chat 增加回答字体滚动效果

This commit is contained in:
cherishsince 2024-05-17 18:01:06 +08:00
parent ca2b7202e6
commit 055abf62cd

View File

@ -106,10 +106,71 @@ const conversationInAbortController = ref<any>() // 对话进行中 abort 控制
const inputTimeout = ref<any>() // const inputTimeout = ref<any>() //
const prompt = ref<string>() // prompt const prompt = ref<string>() // prompt
const fullText = ref('');
const displayedText = ref('');
const textSpeed = ref<number>(50); // Typing speed in milliseconds
const textRoleRunning = ref<boolean>(false); // Typing speed in milliseconds
// () // ()
const messageRef = ref() const messageRef = ref()
const isComposing = ref(false) // const isComposing = ref(false) //
// ===========
const textRoll = async () => {
let index = 0;
try {
//
if (textRoleRunning.value) {
return
}
textRoleRunning.value = true
const task = async () => {
//
const diff = (fullText.value.length - displayedText.value.length) / 10
if (diff > 5) {
textSpeed.value = 10
} else if (diff > 2) {
textSpeed.value = 30
} else if (diff > 1.5) {
textSpeed.value = 50
} else {
textSpeed.value = 100
}
console.log(`diff ${diff} 速度 ${textSpeed.value} `)
// console.log('index < fullText.value.length', index < fullText.value.length, conversationInProgress.value)
if (index < fullText.value.length) {
displayedText.value += fullText.value[index];
index++;
// message
const lastMessage = list.value[list.value.length - 1]
lastMessage.content = displayedText.value
list.value[list.value - 1] = lastMessage
//
await scrollToBottom()
//
timer = setTimeout(task, textSpeed.value);
} else {
//
if (!conversationInProgress.value) {
textRoleRunning.value = false
clearTimeout(timer);
console.log("字体滚动退出!")
} else {
//
timer = setTimeout(task, textSpeed.value);
}
}
}
let timer = setTimeout(task, textSpeed.value);
} finally {
}
};
/** chat message 列表 */ /** chat message 列表 */
const list = ref<ChatMessageVO[]>([]) // const list = ref<ChatMessageVO[]>([]) //
@ -195,7 +256,7 @@ const doSend = async (content: string) => {
} as ChatMessageVO } as ChatMessageVO
// list.value.push(userMessage) // list.value.push(userMessage)
// //
await scrollToBottom() // await scrollToBottom()
// stream // stream
await doSendStream(userMessage) await doSendStream(userMessage)
} }
@ -205,10 +266,13 @@ const doSendStream = async (userMessage: ChatMessageVO) => {
conversationInAbortController.value = new AbortController() conversationInAbortController.value = new AbortController()
// //
conversationInProgress.value = true conversationInProgress.value = true
//
fullText.value = ''
try { try {
//
textRoll()
// event stream // event stream
let isFirstMessage = true let isFirstMessage = true
let content = ''
ChatMessageApi.sendStream( ChatMessageApi.sendStream(
userMessage.conversationId, // TODO userMessage.conversationId, // TODO
userMessage.content, userMessage.content,
@ -231,10 +295,10 @@ const doSendStream = async (userMessage: ChatMessageVO) => {
list.value.push(data.receive) list.value.push(data.receive)
} else { } else {
// debugger // debugger
content = content + data.receive.content fullText.value = fullText.value + data.receive.content
const lastMessage = list.value[list.value.length - 1] // const lastMessage = list.value[list.value.length - 1]
lastMessage.content = content // lastMessage.content = content
list.value[list.value - 1] = lastMessage // list.value[list.value - 1] = lastMessage
} }
// //
await scrollToBottom() await scrollToBottom()