feat: chat in explore support agent (#647)
Co-authored-by: StyleZhang <jasonapring2015@outlook.com>
This commit is contained in:
@@ -1,6 +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'
|
||||
|
||||
const TIME_OUT = 100000
|
||||
|
||||
@@ -30,6 +31,7 @@ export type IOnDataMoreInfo = {
|
||||
}
|
||||
|
||||
export type IOnData = (message: string, isFirstMessage: boolean, moreInfo: IOnDataMoreInfo) => void
|
||||
export type IOnThought = (though: ThoughtItem) => void
|
||||
export type IOnCompleted = (hasError?: boolean) => void
|
||||
export type IOnError = (msg: string) => void
|
||||
|
||||
@@ -39,12 +41,16 @@ type IOtherOptions = {
|
||||
needAllResponseContent?: boolean
|
||||
deleteContentType?: boolean
|
||||
onData?: IOnData // for stream
|
||||
onThought?: IOnThought
|
||||
onError?: IOnError
|
||||
onCompleted?: IOnCompleted // for stream
|
||||
getAbortController?: (abortController: AbortController) => void
|
||||
}
|
||||
|
||||
function unicodeToChar(text: string) {
|
||||
if (!text)
|
||||
return ''
|
||||
|
||||
return text.replace(/\\u[0-9a-f]{4}/g, (_match, p1) => {
|
||||
return String.fromCharCode(parseInt(p1, 16))
|
||||
})
|
||||
@@ -58,7 +64,7 @@ export function format(text: string) {
|
||||
return res.replaceAll('\n', '<br/>').replaceAll('```', '')
|
||||
}
|
||||
|
||||
const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted) => {
|
||||
const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted, onThought?: IOnThought) => {
|
||||
if (!response.ok)
|
||||
throw new Error('Network response was not ok')
|
||||
|
||||
@@ -101,13 +107,18 @@ const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted
|
||||
onCompleted && onCompleted(true)
|
||||
return
|
||||
}
|
||||
// can not use format here. Because message is splited.
|
||||
onData(unicodeToChar(bufferObj.answer), isFirstMessage, {
|
||||
conversationId: bufferObj.conversation_id,
|
||||
taskId: bufferObj.task_id,
|
||||
messageId: bufferObj.id,
|
||||
})
|
||||
isFirstMessage = false
|
||||
if (bufferObj.event === 'message') {
|
||||
// can not use format here. Because message is splited.
|
||||
onData(unicodeToChar(bufferObj.answer), isFirstMessage, {
|
||||
conversationId: bufferObj.conversation_id,
|
||||
taskId: bufferObj.task_id,
|
||||
messageId: bufferObj.id,
|
||||
})
|
||||
isFirstMessage = false
|
||||
}
|
||||
else if (bufferObj.event === 'agent_thought') {
|
||||
onThought?.(bufferObj as any)
|
||||
}
|
||||
}
|
||||
})
|
||||
buffer = lines[lines.length - 1]
|
||||
@@ -298,7 +309,7 @@ export const upload = (options: any): Promise<any> => {
|
||||
})
|
||||
}
|
||||
|
||||
export const ssePost = (url: string, fetchOptions: any, { isPublicAPI = false, onData, onCompleted, onError, getAbortController }: IOtherOptions) => {
|
||||
export const ssePost = (url: string, fetchOptions: any, { isPublicAPI = false, onData, onCompleted, onThought, onError, getAbortController }: IOtherOptions) => {
|
||||
const abortController = new AbortController()
|
||||
|
||||
const options = Object.assign({}, baseOptions, {
|
||||
@@ -333,17 +344,17 @@ export const ssePost = (url: string, fetchOptions: any, { isPublicAPI = false, o
|
||||
}
|
||||
return handleStream(res, (str: string, isFirstMessage: boolean, moreInfo: IOnDataMoreInfo) => {
|
||||
if (moreInfo.errorMessage) {
|
||||
// debugger
|
||||
onError?.(moreInfo.errorMessage)
|
||||
if (moreInfo.errorMessage !== 'AbortError: The user aborted a request.')
|
||||
Toast.notify({ type: 'error', message: moreInfo.errorMessage })
|
||||
return
|
||||
}
|
||||
onData?.(str, isFirstMessage, moreInfo)
|
||||
}, onCompleted)
|
||||
}, onCompleted, onThought)
|
||||
}).catch((e) => {
|
||||
if (e.toString() !== 'AbortError: The user aborted a request.')
|
||||
Toast.notify({ type: 'error', message: e })
|
||||
|
||||
onError?.(e)
|
||||
})
|
||||
}
|
||||
|
@@ -3,7 +3,7 @@ import { del, get, patch, post, put } from './base'
|
||||
import type {
|
||||
AccountIntegrate, CommonResponse, DataSourceNotion,
|
||||
IWorkspace, LangGeniusVersionResponse, Member,
|
||||
OauthResponse, Provider, ProviderAnthropicToken, ProviderAzureToken, TenantInfoResponse,
|
||||
OauthResponse, PluginProvider, Provider, ProviderAnthropicToken, ProviderAzureToken, TenantInfoResponse,
|
||||
UserProfileOriginResponse,
|
||||
} from '@/models/common'
|
||||
import type {
|
||||
@@ -102,6 +102,17 @@ export const updateDataSourceNotionAction: Fetcher<CommonResponse, { url: string
|
||||
return patch(url) as Promise<CommonResponse>
|
||||
}
|
||||
|
||||
export const fetchPluginProviders: Fetcher<PluginProvider[] | null, string> = (url) => {
|
||||
return get(url) as Promise<PluginProvider[] | null>
|
||||
}
|
||||
|
||||
export const validatePluginProviderKey: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: { credentials: any } }> = ({ url, body }) => {
|
||||
return post(url, { body }) as Promise<ValidateOpenAIKeyResponse>
|
||||
}
|
||||
export const updatePluginProviderAIKey: Fetcher<UpdateOpenAIKeyResponse, { url: string; body: { credentials: any } }> = ({ url, body }) => {
|
||||
return post(url, { body }) as Promise<UpdateOpenAIKeyResponse>
|
||||
}
|
||||
|
||||
export const invitationCheck: Fetcher<CommonResponse & { is_valid: boolean; workspace_name: string }, { url: string; params: { workspace_id: string; email: string; token: string } }> = ({ url, params }) => {
|
||||
return get(url, { params }) as Promise<CommonResponse & { is_valid: boolean; workspace_name: string }>
|
||||
}
|
||||
|
@@ -31,3 +31,7 @@ export const updatePinStatus = (id: string, isPinned: boolean) => {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const getToolProviders = () => {
|
||||
return get('/workspaces/current/tool-providers')
|
||||
}
|
||||
|
@@ -3,7 +3,7 @@ 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,
|
||||
} from './base'
|
||||
import type { Feedbacktype } from '@/app/components/app/chat'
|
||||
import type { Feedbacktype } from '@/app/components/app/chat/type'
|
||||
|
||||
function getAction(action: 'get' | 'post' | 'del' | 'patch', isInstalledApp: boolean) {
|
||||
switch (action) {
|
||||
|
75
web/service/universal-chat.ts
Normal file
75
web/service/universal-chat.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import type { IOnCompleted, IOnData, IOnError, IOnThought } from './base'
|
||||
import {
|
||||
del, get, patch, post, ssePost,
|
||||
} from './base'
|
||||
import type { Feedbacktype } from '@/app/components/app/chat/type'
|
||||
|
||||
const baseUrl = 'universal-chat'
|
||||
|
||||
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 }: {
|
||||
onData: IOnData
|
||||
onCompleted: IOnCompleted
|
||||
onError: IOnError
|
||||
onThought: IOnThought
|
||||
getAbortController?: (abortController: AbortController) => void
|
||||
}) => {
|
||||
return ssePost(getUrl('messages'), {
|
||||
body: {
|
||||
...body,
|
||||
response_mode: 'streaming',
|
||||
},
|
||||
}, { onData, onCompleted, onThought, onError, getAbortController })
|
||||
}
|
||||
|
||||
export const stopChatMessageResponding = async (taskId: string) => {
|
||||
return post(getUrl(`messages/${taskId}/stop`))
|
||||
}
|
||||
|
||||
export const fetchConversations = async (last_id?: string, pinned?: boolean, limit?: number) => {
|
||||
return get(getUrl('conversations'), { params: { ...{ limit: limit || 20 }, ...(last_id ? { last_id } : {}), ...(pinned !== undefined ? { pinned } : {}) } })
|
||||
}
|
||||
|
||||
export const pinConversation = async (id: string) => {
|
||||
return patch(getUrl(`conversations/${id}/pin`))
|
||||
}
|
||||
|
||||
export const unpinConversation = async (id: string) => {
|
||||
return patch(getUrl(`conversations/${id}/unpin`))
|
||||
}
|
||||
|
||||
export const delConversation = async (id: string) => {
|
||||
return del(getUrl(`conversations/${id}`))
|
||||
}
|
||||
|
||||
export const fetchChatList = async (conversationId: string) => {
|
||||
return get(getUrl('messages'), { params: { conversation_id: conversationId, limit: 20, last_id: '' } })
|
||||
}
|
||||
|
||||
// init value. wait for server update
|
||||
export const fetchAppParams = async () => {
|
||||
return get(getUrl('parameters'))
|
||||
}
|
||||
|
||||
export const updateFeedback = async ({ url, body }: { url: string; body: Feedbacktype }) => {
|
||||
return post(getUrl(url), { body })
|
||||
}
|
||||
|
||||
export const fetchMoreLikeThis = async (messageId: string) => {
|
||||
return get(getUrl(`/messages/${messageId}/more-like-this`), {
|
||||
params: {
|
||||
response_mode: 'blocking',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const fetchSuggestedQuestions = (messageId: string) => {
|
||||
return get(getUrl(`/messages/${messageId}/suggested-questions`))
|
||||
}
|
||||
|
||||
export const audioToText = (url: string, body: FormData) => {
|
||||
return post(url, { body }, { bodyStringify: false, deleteContentType: true }) as Promise<{ text: string }>
|
||||
}
|
Reference in New Issue
Block a user