Feat/chat add origin (#1130)
This commit is contained in:
@@ -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 })
|
||||
|
@@ -1,9 +1,10 @@
|
||||
import type { IOnCompleted, IOnData, IOnError } from './base'
|
||||
import type { IOnCompleted, IOnData, IOnError, IOnMessageEnd } from './base'
|
||||
import { get, post, ssePost } from './base'
|
||||
|
||||
export const sendChatMessage = async (appId: string, body: Record<string, any>, { onData, onCompleted, onError, getAbortController }: {
|
||||
export const sendChatMessage = async (appId: string, body: Record<string, any>, { onData, onCompleted, onError, getAbortController, onMessageEnd }: {
|
||||
onData: IOnData
|
||||
onCompleted: IOnCompleted
|
||||
onMessageEnd: IOnMessageEnd
|
||||
onError: IOnError
|
||||
getAbortController?: (abortController: AbortController) => void
|
||||
}) => {
|
||||
@@ -12,7 +13,7 @@ export const sendChatMessage = async (appId: string, body: Record<string, any>,
|
||||
...body,
|
||||
response_mode: 'streaming',
|
||||
},
|
||||
}, { onData, onCompleted, onError, getAbortController })
|
||||
}, { onData, onCompleted, onError, getAbortController, onMessageEnd })
|
||||
}
|
||||
|
||||
export const stopChatMessageResponding = async (appId: string, taskId: string) => {
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import type { IOnCompleted, IOnData, IOnError } from './base'
|
||||
import type { IOnCompleted, IOnData, IOnError, IOnMessageEnd } from './base'
|
||||
import {
|
||||
del as consoleDel, get as consoleGet, patch as consolePatch, post as consolePost,
|
||||
delPublic as del, getPublic as get, patchPublic as patch, postPublic as post, ssePost,
|
||||
@@ -22,10 +22,11 @@ function getUrl(url: string, isInstalledApp: boolean, installedAppId: string) {
|
||||
return isInstalledApp ? `installed-apps/${installedAppId}/${url.startsWith('/') ? url.slice(1) : url}` : url
|
||||
}
|
||||
|
||||
export const sendChatMessage = async (body: Record<string, any>, { onData, onCompleted, onError, getAbortController }: {
|
||||
export const sendChatMessage = async (body: Record<string, any>, { onData, onCompleted, onError, getAbortController, onMessageEnd }: {
|
||||
onData: IOnData
|
||||
onCompleted: IOnCompleted
|
||||
onError: IOnError
|
||||
onMessageEnd?: IOnMessageEnd
|
||||
getAbortController?: (abortController: AbortController) => void
|
||||
}, isInstalledApp: boolean, installedAppId = '') => {
|
||||
return ssePost(getUrl('chat-messages', isInstalledApp, installedAppId), {
|
||||
@@ -33,7 +34,7 @@ export const sendChatMessage = async (body: Record<string, any>, { onData, onCom
|
||||
...body,
|
||||
response_mode: 'streaming',
|
||||
},
|
||||
}, { onData, onCompleted, isPublicAPI: !isInstalledApp, onError, getAbortController })
|
||||
}, { onData, onCompleted, isPublicAPI: !isInstalledApp, onError, getAbortController, onMessageEnd })
|
||||
}
|
||||
|
||||
export const stopChatMessageResponding = async (appId: string, taskId: string, isInstalledApp: boolean, installedAppId = '') => {
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import type { IOnCompleted, IOnData, IOnError, IOnThought } from './base'
|
||||
import type { IOnCompleted, IOnData, IOnError, IOnMessageEnd, IOnThought } from './base'
|
||||
import {
|
||||
del, get, patch, post, ssePost,
|
||||
} from './base'
|
||||
@@ -10,11 +10,12 @@ function getUrl(url: string) {
|
||||
return `${baseUrl}/${url.startsWith('/') ? url.slice(1) : url}`
|
||||
}
|
||||
|
||||
export const sendChatMessage = async (body: Record<string, any>, { onData, onCompleted, onError, onThought, getAbortController }: {
|
||||
export const sendChatMessage = async (body: Record<string, any>, { onData, onCompleted, onError, onThought, onMessageEnd, getAbortController }: {
|
||||
onData: IOnData
|
||||
onCompleted: IOnCompleted
|
||||
onError: IOnError
|
||||
onThought: IOnThought
|
||||
onMessageEnd: IOnMessageEnd
|
||||
getAbortController?: (abortController: AbortController) => void
|
||||
}) => {
|
||||
return ssePost(getUrl('messages'), {
|
||||
@@ -22,7 +23,7 @@ export const sendChatMessage = async (body: Record<string, any>, { onData, onCom
|
||||
...body,
|
||||
response_mode: 'streaming',
|
||||
},
|
||||
}, { onData, onCompleted, onThought, onError, getAbortController })
|
||||
}, { onData, onCompleted, onThought, onError, getAbortController, onMessageEnd })
|
||||
}
|
||||
|
||||
export const stopChatMessageResponding = async (taskId: string) => {
|
||||
|
Reference in New Issue
Block a user