【增加】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 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 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 列表 */
const list = ref<ChatMessageVO[]>([]) //
@ -195,7 +256,7 @@ const doSend = async (content: string) => {
} as ChatMessageVO
// list.value.push(userMessage)
//
await scrollToBottom()
// await scrollToBottom()
// stream
await doSendStream(userMessage)
}
@ -205,10 +266,13 @@ const doSendStream = async (userMessage: ChatMessageVO) => {
conversationInAbortController.value = new AbortController()
//
conversationInProgress.value = true
//
fullText.value = ''
try {
//
textRoll()
// event stream
let isFirstMessage = true
let content = ''
ChatMessageApi.sendStream(
userMessage.conversationId, // TODO
userMessage.content,
@ -231,10 +295,10 @@ const doSendStream = async (userMessage: ChatMessageVO) => {
list.value.push(data.receive)
} else {
// debugger
content = content + data.receive.content
const lastMessage = list.value[list.value.length - 1]
lastMessage.content = content
list.value[list.value - 1] = lastMessage
fullText.value = fullText.value + data.receive.content
// const lastMessage = list.value[list.value.length - 1]
// lastMessage.content = content
// list.value[list.value - 1] = lastMessage
}
//
await scrollToBottom()