dify/web/service/share.ts

99 lines
4.1 KiB
TypeScript
Raw Normal View History

2023-05-15 08:51:32 +08:00
import type { IOnCompleted, IOnData, IOnError } from './base'
import {
del as consoleDel, get as consoleGet, post as consolePost,
delPublic as del, getPublic as get, postPublic as post, ssePost,
2023-05-25 16:59:47 +08:00
} from './base'
2023-05-15 08:51:32 +08:00
import type { Feedbacktype } from '@/app/components/app/chat'
2023-05-25 16:59:47 +08:00
function getAction(action: 'get' | 'post' | 'del', isInstalledApp: boolean) {
switch (action) {
case 'get':
return isInstalledApp ? consoleGet : get
case 'post':
return isInstalledApp ? consolePost : post
case 'del':
return isInstalledApp ? consoleDel : del
}
}
function getUrl(url: string, isInstalledApp: boolean, installedAppId: string) {
return isInstalledApp ? `installed-apps/${installedAppId}/${url.startsWith('/') ? url.slice(1) : url}` : url
}
2023-05-15 08:51:32 +08:00
export const sendChatMessage = async (body: Record<string, any>, { onData, onCompleted, onError, getAbortController }: {
onData: IOnData
onCompleted: IOnCompleted
onError: IOnError
2023-05-15 08:51:32 +08:00
getAbortController?: (abortController: AbortController) => void
2023-05-25 16:59:47 +08:00
}, isInstalledApp: boolean, installedAppId = '') => {
return ssePost(getUrl('chat-messages', isInstalledApp, installedAppId), {
2023-05-15 08:51:32 +08:00
body: {
...body,
response_mode: 'streaming',
},
2023-05-25 16:59:47 +08:00
}, { onData, onCompleted, isPublicAPI: !isInstalledApp, onError, getAbortController })
2023-05-15 08:51:32 +08:00
}
export const sendCompletionMessage = async (body: Record<string, any>, { onData, onCompleted, onError }: {
onData: IOnData
onCompleted: IOnCompleted
onError: IOnError
2023-05-25 16:59:47 +08:00
}, isInstalledApp: boolean, installedAppId = '') => {
return ssePost(getUrl('completion-messages', isInstalledApp, installedAppId), {
2023-05-15 08:51:32 +08:00
body: {
...body,
response_mode: 'streaming',
},
2023-05-25 16:59:47 +08:00
}, { onData, onCompleted, isPublicAPI: !isInstalledApp, onError })
2023-05-15 08:51:32 +08:00
}
export const fetchAppInfo = async () => {
return get('/site')
}
export const fetchConversations = async (isInstalledApp: boolean, installedAppId = '', last_id?: string) => {
return getAction('get', isInstalledApp)(getUrl('conversations', isInstalledApp, installedAppId), { params: { ...{ limit: 20 }, ...(last_id ? { last_id } : {}) } })
2023-05-15 08:51:32 +08:00
}
export const fetchChatList = async (conversationId: string, isInstalledApp: boolean, installedAppId = '') => {
2023-05-25 16:59:47 +08:00
return getAction('get', isInstalledApp)(getUrl('messages', isInstalledApp, installedAppId), { params: { conversation_id: conversationId, limit: 20, last_id: '' } })
2023-05-15 08:51:32 +08:00
}
// Abandoned API interface
// export const fetchAppVariables = async () => {
// return get(`variables`)
// }
// init value. wait for server update
2023-05-25 16:59:47 +08:00
export const fetchAppParams = async (isInstalledApp: boolean, installedAppId = '') => {
return (getAction('get', isInstalledApp))(getUrl('parameters', isInstalledApp, installedAppId))
2023-05-15 08:51:32 +08:00
}
2023-05-25 16:59:47 +08:00
export const updateFeedback = async ({ url, body }: { url: string; body: Feedbacktype }, isInstalledApp: boolean, installedAppId = '') => {
return (getAction('post', isInstalledApp))(getUrl(url, isInstalledApp, installedAppId), { body })
2023-05-15 08:51:32 +08:00
}
2023-05-25 16:59:47 +08:00
export const fetchMoreLikeThis = async (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
return (getAction('get', isInstalledApp))(getUrl(`/messages/${messageId}/more-like-this`, isInstalledApp, installedAppId), {
2023-05-15 08:51:32 +08:00
params: {
response_mode: 'blocking',
},
2023-05-15 08:51:32 +08:00
})
}
2023-05-25 16:59:47 +08:00
export const saveMessage = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
return (getAction('post', isInstalledApp))(getUrl('/saved-messages', isInstalledApp, installedAppId), { body: { message_id: messageId } })
2023-05-15 08:51:32 +08:00
}
2023-05-25 16:59:47 +08:00
export const fetchSavedMessage = async (isInstalledApp: boolean, installedAppId = '') => {
return (getAction('get', isInstalledApp))(getUrl('/saved-messages', isInstalledApp, installedAppId))
2023-05-15 08:51:32 +08:00
}
2023-05-25 16:59:47 +08:00
export const removeMessage = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
return (getAction('del', isInstalledApp))(getUrl(`/saved-messages/${messageId}`, isInstalledApp, installedAppId))
2023-05-15 08:51:32 +08:00
}
2023-05-25 16:59:47 +08:00
export const fetchSuggestedQuestions = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
return (getAction('get', isInstalledApp))(getUrl(`/messages/${messageId}/suggested-questions`, isInstalledApp, installedAppId))
2023-05-15 08:51:32 +08:00
}