Commit 92be2dc3 authored by 박민석's avatar 박민석

update chat.js

parent 270c16c0
...@@ -169,15 +169,16 @@ ...@@ -169,15 +169,16 @@
return return
} }
isLoading = true const chatLoader = document.createElement('div');
chatLoader.className = 'chat-loader';
const loader = document.createElement('div');
loader.className = 'loader';
chatLoader.appendChild(loader);
chatBody.appendChild(chatLoader);
chatBody.scrollTop = chatBody.scrollHeight;
const chatLoader = document.createElement('div')
chatLoader.className = 'chat-loader' isLoading = true
const loader = document.createElement('div')
loader.className = 'loader'
chatLoader.appendChild(loader)
chatBody.appendChild(chatLoader)
chatBody.scrollTop = chatBody.scrollHeight
const payload = { const payload = {
question: message, question: message,
...@@ -197,35 +198,54 @@ ...@@ -197,35 +198,54 @@
const messageItem = document.createElement('pre') const messageItem = document.createElement('pre')
messageItem.className = 'message ai' messageItem.className = 'message ai'
chatBody.appendChild(messageItem) // 실제 메시지가 들어갈 자리
function readStream () { function readStream () {
return reader.read().then(({ done, value }) => { return reader.read().then(({ done, value }) => {
console.log(done, value)
if (done) { if (done) {
chatBody.removeChild(chatLoader)
isLoading = false isLoading = false
return return
} }
const chunk = decoder.decode(value, { stream: true }) const chunk = decoder.decode(value, { stream: true })
console.log(chunk) console.log('chunk >>>>> ', chunk)
messageItem.textContent += chunk
chatBody.scrollTop = chatBody?.scrollHeight // 로더 제거 후 메시지 표시
if (chatLoader) chatBody.removeChild(chatLoader);
typeEffect(messageItem, chunk, 30)
chatBody.scrollTop = chatBody.scrollHeight
return readStream() return readStream()
}) })
} }
chatBody.appendChild(messageItem)
chatBody.scrollTop = chatBody.scrollHeight
return readStream() return readStream()
}) })
.catch((error) => { .catch((error) => {
console.error('Chatbot Error', error) console.error('Chatbot Error', error)
chatBody.removeChild(chatLoader)
isLoading = false isLoading = false
}) })
} }
} }
// 타이핑 효과 함수
function typeEffect (element, text, speed = 50) {
let index = 0
function type () {
if (index < text.length) {
element.textContent += text.charAt(index)
chatBody.scrollTop = chatBody.scrollHeight
index++
setTimeout(type, speed) // 한글자씩 추가
}
}
type()
}
function createAiMessage (message) { function createAiMessage (message) {
const messageItem = document.createElement('pre') const messageItem = document.createElement('pre')
messageItem.className = 'message ai' messageItem.className = 'message ai'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment