diff --git a/service/src/index.ts b/service/src/index.ts index 8fc5ea6515..449a13212e 100644 --- a/service/src/index.ts +++ b/service/src/index.ts @@ -25,7 +25,7 @@ router.post('/chat-process', [auth, limiter], async (req, res) => { try { const { prompt, options = {}, systemMessage, temperature, topP } = req.body as RequestProps let firstChunk = true - await chatReplyProcess({ + const finalResponse = await chatReplyProcess({ message: prompt, lastContext: options, process: (chat: ChatMessage) => { @@ -36,6 +36,7 @@ router.post('/chat-process', [auth, limiter], async (req, res) => { temperature, topP, }) + res.write(firstChunk ? JSON.stringify(finalResponse) : `\n${JSON.stringify(finalResponse)}`) } catch (error) { res.json(error) diff --git a/src/utils/request/index.ts b/src/utils/request/index.ts index c51c1e70c7..cc0cee3acd 100644 --- a/src/utils/request/index.ts +++ b/src/utils/request/index.ts @@ -25,6 +25,12 @@ function http( const successHandler = (res: AxiosResponse>) => { 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 diff --git a/src/views/chat/components/Message/Text.vue b/src/views/chat/components/Message/Text.vue index c618b8c687..c01638d5a3 100644 --- a/src/views/chat/components/Message/Text.vue +++ b/src/views/chat/components/Message/Text.vue @@ -106,38 +106,36 @@ defineExpose({ inputRef })