|
@@ -1,7 +1,7 @@
|
|
|
/* eslint-disable no-new, prefer-promise-reject-errors */
|
|
|
import { API_PREFIX, IS_CE_EDITION, PUBLIC_API_PREFIX } from '@/config'
|
|
|
import Toast from '@/app/components/base/toast'
|
|
|
-import type { ThoughtItem } from '@/app/components/app/chat/type'
|
|
|
+import type { MessageEnd, ThoughtItem } from '@/app/components/app/chat/type'
|
|
|
|
|
|
const TIME_OUT = 100000
|
|
|
|
|
@@ -33,6 +33,7 @@ export type IOnDataMoreInfo = {
|
|
|
|
|
|
export type IOnData = (message: string, isFirstMessage: boolean, moreInfo: IOnDataMoreInfo) => void
|
|
|
export type IOnThought = (though: ThoughtItem) => void
|
|
|
+export type IOnMessageEnd = (messageEnd: MessageEnd) => void
|
|
|
export type IOnCompleted = (hasError?: boolean) => void
|
|
|
export type IOnError = (msg: string, code?: string) => void
|
|
|
|
|
@@ -43,6 +44,7 @@ type IOtherOptions = {
|
|
|
deleteContentType?: boolean
|
|
|
onData?: IOnData // for stream
|
|
|
onThought?: IOnThought
|
|
|
+ onMessageEnd?: IOnMessageEnd
|
|
|
onError?: IOnError
|
|
|
onCompleted?: IOnCompleted // for stream
|
|
|
getAbortController?: (abortController: AbortController) => void
|
|
@@ -65,7 +67,7 @@ export function format(text: string) {
|
|
|
return res.replaceAll('\n', '<br/>').replaceAll('```', '')
|
|
|
}
|
|
|
|
|
|
-const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted, onThought?: IOnThought) => {
|
|
|
+const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted, onThought?: IOnThought, onMessageEnd?: IOnMessageEnd) => {
|
|
|
if (!response.ok)
|
|
|
throw new Error('Network response was not ok')
|
|
|
|
|
@@ -86,7 +88,6 @@ const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted
|
|
|
try {
|
|
|
lines.forEach((message) => {
|
|
|
if (message.startsWith('data: ')) { // check if it starts with data:
|
|
|
- // console.log(message);
|
|
|
try {
|
|
|
bufferObj = JSON.parse(message.substring(6)) // remove data: and parse as json
|
|
|
}
|
|
@@ -121,6 +122,9 @@ const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted
|
|
|
else if (bufferObj.event === 'agent_thought') {
|
|
|
onThought?.(bufferObj as any)
|
|
|
}
|
|
|
+ else if (bufferObj.event === 'message_end') {
|
|
|
+ onMessageEnd?.(bufferObj as any)
|
|
|
+ }
|
|
|
}
|
|
|
})
|
|
|
buffer = lines[lines.length - 1]
|
|
@@ -311,7 +315,7 @@ export const upload = (options: any): Promise<any> => {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
-export const ssePost = (url: string, fetchOptions: any, { isPublicAPI = false, onData, onCompleted, onThought, onError, getAbortController }: IOtherOptions) => {
|
|
|
+export const ssePost = (url: string, fetchOptions: any, { isPublicAPI = false, onData, onCompleted, onThought, onMessageEnd, onError, getAbortController }: IOtherOptions) => {
|
|
|
const abortController = new AbortController()
|
|
|
|
|
|
const options = Object.assign({}, baseOptions, {
|
|
@@ -353,7 +357,7 @@ export const ssePost = (url: string, fetchOptions: any, { isPublicAPI = false, o
|
|
|
return
|
|
|
}
|
|
|
onData?.(str, isFirstMessage, moreInfo)
|
|
|
- }, onCompleted, onThought)
|
|
|
+ }, onCompleted, onThought, onMessageEnd)
|
|
|
}).catch((e) => {
|
|
|
if (e.toString() !== 'AbortError: The user aborted a request.')
|
|
|
Toast.notify({ type: 'error', message: e })
|