2
0

🐛 (dify) Fix invalid parsing when text delta is multiline

This commit is contained in:
Baptiste Arnaud
2024-05-27 15:39:36 +02:00
parent 85bcb699f8
commit 176e3a5fcf
4 changed files with 12 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import { defaultBaseUrl } from '../constants'
import { Chunk } from '../types'
import ky, { HTTPError } from 'ky'
import { deprecatedCreateChatMessageOptions } from '../deprecated'
import { formatStreamPart } from 'ai'
export const createChatMessage = createAction({
auth,
@ -120,7 +121,7 @@ export const createChatMessage = createAction({
onMessage: (message) => {
controller.enqueue(
new TextEncoder().encode(
'0:"' + message.replace(/"/g, '\\"') + '"\n'
formatStreamPart('text', message)
)
)
},
@ -299,7 +300,7 @@ const processDifyStream = async (
totalTokens,
conversationId,
}: {
totalTokens: number
totalTokens?: number
conversationId: string
}) => void
}
@ -333,7 +334,7 @@ const processDifyStream = async (
}
if (data.event === 'message_end') {
callbacks.onMessageEnd?.({
totalTokens: data.metadata.usage.total_tokens,
totalTokens: data.metadata.usage?.total_tokens,
conversationId: data.conversation_id,
})
}