Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add typing effect #1017

Merged
merged 5 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
try {
const { prompt, options = {}, systemMessage } = req.body as RequestProps
let firstChunk = true
await chatReplyProcess({
const finalResponse = await chatReplyProcess({
message: prompt,
lastContext: options,
process: (chat: ChatMessage) => {
Expand All @@ -34,6 +34,7 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
},
systemMessage,
})
res.write(firstChunk ? JSON.stringify(finalResponse) : `\n${JSON.stringify(finalResponse)}`)
}
catch (error) {
res.write(JSON.stringify(error))
Expand Down
6 changes: 6 additions & 0 deletions src/utils/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ function http<T = any>(
const successHandler = (res: AxiosResponse<Response<T>>) => {
const authStore = useAuthStore()

if (typeof res.data === 'string') {
const lastIndex = (res.data as string).lastIndexOf('\n')
if (lastIndex !== -1)
res.data = JSON.parse((res.data as string).substring(lastIndex))
}

if (res.data.status === 'Success' || typeof res.data === 'string')
return res.data

Expand Down
18 changes: 8 additions & 10 deletions src/views/chat/components/Message/Text.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,16 @@ defineExpose({ textRef })

<template>
<div class="text-black" :class="wrapClass">
<template v-if="loading">
<span class="dark:text-white w-[4px] h-[20px] block animate-blink" />
</template>
<template v-else>
<div ref="textRef" class="leading-relaxed break-words">
<div v-if="!inversion">
<div v-if="!asRawText" class="markdown-body" v-html="text" />
<div v-else class="whitespace-pre-wrap" v-text="text" />
</div>
<div ref="textRef" class="leading-relaxed break-words">
<div v-if="!inversion">
<div v-if="!asRawText" class="markdown-body" v-html="text" />
<div v-else class="whitespace-pre-wrap" v-text="text" />
</div>
</template>
<div v-else class="whitespace-pre-wrap" v-text="text" />
<template v-if="loading">
<span class="dark:text-white w-[4px] h-[20px] block animate-blink" />
</template>
</div>
</div>
</template>

Expand Down
34 changes: 30 additions & 4 deletions src/views/chat/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async function onConversation() {
try {
let lastText = ''
const fetchChatAPIOnce = async () => {
await fetchChatAPIProcess<Chat.ConversationResponse>({
const { data } = await fetchChatAPIProcess<Chat.ConversationResponse>({
prompt: message,
options,
signal: controller.signal,
Expand All @@ -131,7 +131,7 @@ async function onConversation() {
text: lastText + data.text ?? '',
inversion: false,
error: false,
loading: false,
loading: true,
conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id },
requestOptions: { prompt: message, options: { ...options } },
},
Expand All @@ -151,6 +151,19 @@ async function onConversation() {
}
},
})
updateChat(
+uuid,
dataSources.value.length - 1,
{
dateTime: new Date().toLocaleString(),
text: lastText + data.text ?? '',
inversion: false,
error: false,
loading: false,
conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id },
requestOptions: { prompt: message, options: { ...options } },
},
)
}

await fetchChatAPIOnce()
Expand Down Expand Up @@ -239,7 +252,7 @@ async function onRegenerate(index: number) {
try {
let lastText = ''
const fetchChatAPIOnce = async () => {
await fetchChatAPIProcess<Chat.ConversationResponse>({
const { data } = await fetchChatAPIProcess<Chat.ConversationResponse>({
prompt: message,
options,
signal: controller.signal,
Expand All @@ -261,7 +274,7 @@ async function onRegenerate(index: number) {
text: lastText + data.text ?? '',
inversion: false,
error: false,
loading: false,
loading: true,
conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id },
requestOptions: { prompt: message, ...options },
},
Expand All @@ -279,6 +292,19 @@ async function onRegenerate(index: number) {
}
},
})
updateChat(
+uuid,
index,
{
dateTime: new Date().toLocaleString(),
text: lastText + data.text ?? '',
inversion: false,
error: false,
loading: false,
conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id },
requestOptions: { prompt: message, ...options },
},
)
}
await fetchChatAPIOnce()
}
Expand Down