Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
SWLab UI
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Jobs
Commits
Open sidebar
SWD
SWLab UI
Commits
92be2dc3
Commit
92be2dc3
authored
Sep 24, 2024
by
박민석
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update chat.js
parent
270c16c0
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
16 deletions
+36
-16
08-chat.js
src/js/08-chat.js
+36
-16
No files found.
src/js/08-chat.js
View file @
92be2dc3
...
@@ -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'
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment