feat: support assistant frontend (#2139)

Co-authored-by: StyleZhang <jasonapring2015@outlook.com>
This commit is contained in:
Joel 2024-01-23 19:31:56 +08:00 • committed by GitHub
commit 7bbe12b2bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
194 changed files with 8726 additions and 1586 deletions

View file

@ -1,7 +1,7 @@
import { API_PREFIX, IS_CE_EDITION, PUBLIC_API_PREFIX } from '@/config'
import Toast from '@/app/components/base/toast'
import type { AnnotationReply, MessageEnd, MessageReplace, ThoughtItem } from '@/app/components/app/chat/type'
import type { VisionFile } from '@/types/app'
const TIME_OUT = 100000
const ContentType = {
@ -32,6 +32,7 @@ export type IOnDataMoreInfo = {
export type IOnData = (message: string, isFirstMessage: boolean, moreInfo: IOnDataMoreInfo) => void
export type IOnThought = (though: ThoughtItem) => void
export type IOnFile = (file: VisionFile) => void
export type IOnMessageEnd = (messageEnd: MessageEnd) => void
export type IOnMessageReplace = (messageReplace: MessageReplace) => void
export type IOnAnnotationReply = (messageReplace: AnnotationReply) => void
@ -45,6 +46,7 @@ type IOtherOptions = {
deleteContentType?: boolean
onData?: IOnData // for stream
onThought?: IOnThought
onFile?: IOnFile
onMessageEnd?: IOnMessageEnd
onMessageReplace?: IOnMessageReplace
onError?: IOnError
@ -80,7 +82,7 @@ export function format(text: string) {
return res.replaceAll('\n', '<br/>').replaceAll('```', '')
}
const handleStream = (response: Response, onData: IOnData, onCompleted?: IOnCompleted, onThought?: IOnThought, onMessageEnd?: IOnMessageEnd, onMessageReplace?: IOnMessageReplace) => {
const handleStream = (response: Response, onData: IOnData, onCompleted?: IOnCompleted, onThought?: IOnThought, onMessageEnd?: IOnMessageEnd, onMessageReplace?: IOnMessageReplace, onFile?: IOnFile) => {
if (!response.ok)
throw new Error('Network response was not ok')
@ -123,7 +125,7 @@ const handleStream = (response: Response, onData: IOnData, onCompleted?: IOnComp
onCompleted?.(true)
return
}
if (bufferObj.event === 'message') {
if (bufferObj.event === 'message' || bufferObj.event === 'agent_message') {
// can not use format here. Because message is splited.
onData(unicodeToChar(bufferObj.answer), isFirstMessage, {
conversationId: bufferObj.conversation_id,
@ -135,6 +137,9 @@ const handleStream = (response: Response, onData: IOnData, onCompleted?: IOnComp
else if (bufferObj.event === 'agent_thought') {
onThought?.(bufferObj as ThoughtItem)
}
else if (bufferObj.event === 'message_file') {
onFile?.(bufferObj as VisionFile)
}
else if (bufferObj.event === 'message_end') {
onMessageEnd?.(bufferObj as MessageEnd)
}
@ -346,7 +351,7 @@ export const upload = (options: any, isPublicAPI?: boolean, url?: string): Promi
})
}
export const ssePost = (url: string, fetchOptions: FetchOptionType, { isPublicAPI = false, onData, onCompleted, onThought, onMessageEnd, onMessageReplace, onError, getAbortController }: IOtherOptions) => {
export const ssePost = (url: string, fetchOptions: FetchOptionType, { isPublicAPI = false, onData, onCompleted, onThought, onFile, onMessageEnd, onMessageReplace, onError, getAbortController }: IOtherOptions) => {
const abortController = new AbortController()
const options = Object.assign({}, baseOptions, {
@ -384,7 +389,7 @@ export const ssePost = (url: string, fetchOptions: FetchOptionType, { isPublicAP
return
}
onData?.(str, isFirstMessage, moreInfo)
}, onCompleted, onThought, onMessageEnd, onMessageReplace)
}, onCompleted, onThought, onMessageEnd, onMessageReplace, onFile)
}).catch((e) => {
if (e.toString() !== 'AbortError: The user aborted a request.')
Toast.notify({ type: 'error', message: e })

View file

@ -1,18 +1,19 @@
import type { IOnCompleted, IOnData, IOnError, IOnMessageEnd, IOnMessageReplace } from './base'
import { get, post, ssePost } from './base'
import type { IOnCompleted, IOnData, IOnError, IOnFile, IOnMessageEnd, IOnMessageReplace, IOnThought } from './base'
import type { ChatPromptConfig, CompletionPromptConfig } from '@/models/debug'
import type { ModelModeType } from '@/types/app'
import type { ModelParameterRule } from '@/app/components/header/account-setting/model-provider-page/declarations'
export type AutomaticRes = {
prompt: string
variables: string[]
opening_statement: string
}
export const sendChatMessage = async (appId: string, body: Record<string, any>, { onData, onCompleted, onError, getAbortController, onMessageEnd, onMessageReplace }: {
export const sendChatMessage = async (appId: string, body: Record<string, any>, { onData, onCompleted, onThought, onFile, onError, getAbortController, onMessageEnd, onMessageReplace }: {
onData: IOnData
onCompleted: IOnCompleted
onFile: IOnFile
onThought: IOnThought
onMessageEnd: IOnMessageEnd
onMessageReplace: IOnMessageReplace
onError: IOnError
@ -23,7 +24,7 @@ export const sendChatMessage = async (appId: string, body: Record<string, any>,
...body,
response_mode: 'streaming',
},
}, { onData, onCompleted, onError, getAbortController, onMessageEnd, onMessageReplace })
}, { onData, onCompleted, onThought, onFile, onError, getAbortController, onMessageEnd, onMessageReplace })
}
export const stopChatMessageResponding = async (appId: string, taskId: string) => {

View file

@ -1,4 +1,4 @@
import type { IOnCompleted, IOnData, IOnError, IOnMessageEnd, IOnMessageReplace } from './base'
import type { IOnCompleted, IOnData, IOnError, IOnFile, IOnMessageEnd, IOnMessageReplace, IOnThought } 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,9 +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, onMessageEnd, onMessageReplace }: {
export const sendChatMessage = async (body: Record<string, any>, { onData, onCompleted, onThought, onFile, onError, getAbortController, onMessageEnd, onMessageReplace }: {
onData: IOnData
onCompleted: IOnCompleted
onFile: IOnFile
onThought: IOnThought
onError: IOnError
onMessageEnd?: IOnMessageEnd
onMessageReplace?: IOnMessageReplace
@ -35,7 +37,7 @@ export const sendChatMessage = async (body: Record<string, any>, { onData, onCom
...body,
response_mode: 'streaming',
},
}, { onData, onCompleted, isPublicAPI: !isInstalledApp, onError, getAbortController, onMessageEnd, onMessageReplace })
}, { onData, onCompleted, onThought, onFile, isPublicAPI: !isInstalledApp, onError, getAbortController, onMessageEnd, onMessageReplace })
}
export const stopChatMessageResponding = async (appId: string, taskId: string, isInstalledApp: boolean, installedAppId = '') => {
@ -98,6 +100,10 @@ export const fetchAppParams = async (isInstalledApp: boolean, installedAppId = '
return (getAction('get', isInstalledApp))(getUrl('parameters', isInstalledApp, installedAppId))
}
export const fetchAppMeta = async (isInstalledApp: boolean, installedAppId = '') => {
return (getAction('get', isInstalledApp))(getUrl('meta', isInstalledApp, installedAppId))
}
export const updateFeedback = async ({ url, body }: { url: string; body: Feedbacktype }, isInstalledApp: boolean, installedAppId = '') => {
return (getAction('post', isInstalledApp))(getUrl(url, isInstalledApp, installedAppId), { body })
}

83
web/service/tools.ts Normal file
View file

@ -0,0 +1,83 @@
import { get, post } from './base'
import type { CustomCollectionBackend } from '@/app/components/tools/types'
export const fetchCollectionList = () => {
return get('/workspaces/current/tool-providers')
}
export const fetchBuiltInToolList = (collectionName: string) => {
return get(`/workspaces/current/tool-provider/builtin/${collectionName}/tools`)
}
export const fetchCustomToolList = (collectionName: string) => {
return get(`/workspaces/current/tool-provider/api/tools?provider=${collectionName}`)
}
export const fetchBuiltInToolCredentialSchema = (collectionName: string) => {
return get(`/workspaces/current/tool-provider/builtin/${collectionName}/credentials_schema`)
}
export const updateBuiltInToolCredential = (collectionName: string, credential: Record<string, any>) => {
return post(`/workspaces/current/tool-provider/builtin/${collectionName}/update`, {
body: {
credentials: credential,
},
})
}
export const removeBuiltInToolCredential = (collectionName: string) => {
return post(`/workspaces/current/tool-provider/builtin/${collectionName}/delete`, {
body: {},
})
}
export const parseParamsSchema = (schema: string) => {
return post('/workspaces/current/tool-provider/api/schema', {
body: {
schema,
},
})
}
export const fetchCustomCollection = (collectionName: string) => {
return get(`/workspaces/current/tool-provider/api/get?provider=${collectionName}`)
}
export const createCustomCollection = (collection: CustomCollectionBackend) => {
return post('/workspaces/current/tool-provider/api/add', {
body: {
...collection,
},
})
}
export const updateCustomCollection = (collection: CustomCollectionBackend) => {
return post('/workspaces/current/tool-provider/api/update', {
body: {
...collection,
},
})
}
export const removeCustomCollection = (collectionName: string) => {
return post('/workspaces/current/tool-provider/api/delete', {
body: {
provider: collectionName,
},
})
}
export const importSchemaFromURL = (url: string) => {
return get('/workspaces/current/tool-provider/api/remote', {
params: {
url,
},
})
}
export const testAPIAvailable = (payload: any) => {
return post('/workspaces/current/tool-provider/api/test/pre', {
body: {
...payload,
},
})
}