Add typewriter effect to the messages of the model.

This commit is contained in:
Rafi
2023-03-09 15:05:40 +08:00
parent 7bff84638e
commit 8340edbf40
2 changed files with 35 additions and 9 deletions

View File

@@ -13,7 +13,7 @@ hljs.addPlugin({
} }
header = Object.assign(document.createElement("div"), { header = Object.assign(document.createElement("div"), {
className: "hljs-code-header d-flex align-center justify-space-between bg-black pa-1", className: "hljs-code-header d-flex align-center justify-space-between bg-grey-darken-3 pa-1",
innerHTML: `<div class="pl-2 text-caption">${result.language}</div>` innerHTML: `<div class="pl-2 text-caption">${result.language}</div>`
}); });
@@ -56,16 +56,22 @@ const contentHtml = ref('')
const contentElm = ref(null) const contentElm = ref(null)
const highlightCode = () => { const highlightCode = () => {
if (!contentElm.value) {
return
}
contentElm.value.querySelectorAll('pre code').forEach((block) => { contentElm.value.querySelectorAll('pre code').forEach((block) => {
hljs.highlightElement(block) hljs.highlightElement(block)
}) })
} }
watchEffect(() => { watchEffect(() => {
console.log('content changed', props.content)
contentHtml.value = props.content ? marked(props.content) : '' contentHtml.value = props.content ? marked(props.content) : ''
if (props.content && props.content.endsWith('```')) {
nextTick(() => { nextTick(() => {
highlightCode() highlightCode()
}) })
}
}) })
</script> </script>

View File

@@ -10,6 +10,29 @@ const runtimeConfig = useRuntimeConfig()
const currentModel = useCurrentModel() const currentModel = useCurrentModel()
const openaiApiKey = useApiKey() const openaiApiKey = useApiKey()
const fetchingResponse = ref(false) const fetchingResponse = ref(false)
const messageQueue = []
let isProcessingQueue = false
const processMessageQueue = () => {
if (isProcessingQueue || messageQueue.length === 0) {
return
}
if (!currentConversation.value.messages[currentConversation.value.messages.length - 1].is_bot) {
currentConversation.value.messages.push({id: null, is_bot: true, message: ''})
}
isProcessingQueue = true
const nextMessage = messageQueue.shift()
let wordIndex = 0;
const intervalId = setInterval(() => {
currentConversation.value.messages[currentConversation.value.messages.length - 1].message += nextMessage[wordIndex]
wordIndex++
if (wordIndex === nextMessage.length) {
clearInterval(intervalId)
isProcessingQueue = false
processMessageQueue()
}
}, 50)
}
let ctrl let ctrl
const abortFetch = () => { const abortFetch = () => {
@@ -69,11 +92,8 @@ const fetchReply = async (message, parentMessageId) => {
return; return;
} }
if (currentConversation.value.messages[currentConversation.value.messages.length - 1].is_bot) { messageQueue.push(data.content)
currentConversation.value.messages[currentConversation.value.messages.length - 1].message += data.content processMessageQueue()
} else {
currentConversation.value.messages.push({id: null, is_bot: true, message: data.content})
}
scrollChatWindow() scrollChatWindow()
}, },