share.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import type { IOnCompleted, IOnData, IOnError, IOnFile, IOnMessageEnd, IOnMessageReplace, IOnNodeFinished, IOnNodeStarted, IOnTextChunk, IOnTextReplace, IOnThought, IOnWorkflowFinished, IOnWorkflowStarted } from './base'
  2. import {
  3. del as consoleDel, get as consoleGet, patch as consolePatch, post as consolePost,
  4. delPublic as del, getPublic as get, patchPublic as patch, postPublic as post, ssePost,
  5. } from './base'
  6. import type { Feedbacktype } from '@/app/components/app/chat/type'
  7. import type {
  8. AppConversationData,
  9. AppData,
  10. AppMeta,
  11. ConversationItem,
  12. } from '@/models/share'
  13. import type { ChatConfig } from '@/app/components/base/chat/types'
  14. import type { SystemFeatures } from '@/types/feature'
  15. function getAction(action: 'get' | 'post' | 'del' | 'patch', isInstalledApp: boolean) {
  16. switch (action) {
  17. case 'get':
  18. return isInstalledApp ? consoleGet : get
  19. case 'post':
  20. return isInstalledApp ? consolePost : post
  21. case 'patch':
  22. return isInstalledApp ? consolePatch : patch
  23. case 'del':
  24. return isInstalledApp ? consoleDel : del
  25. }
  26. }
  27. export function getUrl(url: string, isInstalledApp: boolean, installedAppId: string) {
  28. return isInstalledApp ? `installed-apps/${installedAppId}/${url.startsWith('/') ? url.slice(1) : url}` : url
  29. }
  30. export const sendChatMessage = async (body: Record<string, any>, { onData, onCompleted, onThought, onFile, onError, getAbortController, onMessageEnd, onMessageReplace }: {
  31. onData: IOnData
  32. onCompleted: IOnCompleted
  33. onFile: IOnFile
  34. onThought: IOnThought
  35. onError: IOnError
  36. onMessageEnd?: IOnMessageEnd
  37. onMessageReplace?: IOnMessageReplace
  38. getAbortController?: (abortController: AbortController) => void
  39. }, isInstalledApp: boolean, installedAppId = '') => {
  40. return ssePost(getUrl('chat-messages', isInstalledApp, installedAppId), {
  41. body: {
  42. ...body,
  43. response_mode: 'streaming',
  44. },
  45. }, { onData, onCompleted, onThought, onFile, isPublicAPI: !isInstalledApp, onError, getAbortController, onMessageEnd, onMessageReplace })
  46. }
  47. export const stopChatMessageResponding = async (appId: string, taskId: string, isInstalledApp: boolean, installedAppId = '') => {
  48. return getAction('post', isInstalledApp)(getUrl(`chat-messages/${taskId}/stop`, isInstalledApp, installedAppId))
  49. }
  50. export const sendCompletionMessage = async (body: Record<string, any>, { onData, onCompleted, onError, onMessageReplace }: {
  51. onData: IOnData
  52. onCompleted: IOnCompleted
  53. onError: IOnError
  54. onMessageReplace: IOnMessageReplace
  55. }, isInstalledApp: boolean, installedAppId = '') => {
  56. return ssePost(getUrl('completion-messages', isInstalledApp, installedAppId), {
  57. body: {
  58. ...body,
  59. response_mode: 'streaming',
  60. },
  61. }, { onData, onCompleted, isPublicAPI: !isInstalledApp, onError, onMessageReplace })
  62. }
  63. export const sendWorkflowMessage = async (
  64. body: Record<string, any>,
  65. {
  66. onWorkflowStarted,
  67. onNodeStarted,
  68. onNodeFinished,
  69. onWorkflowFinished,
  70. onTextChunk,
  71. onTextReplace,
  72. }: {
  73. onWorkflowStarted: IOnWorkflowStarted
  74. onNodeStarted: IOnNodeStarted
  75. onNodeFinished: IOnNodeFinished
  76. onWorkflowFinished: IOnWorkflowFinished
  77. onTextChunk: IOnTextChunk
  78. onTextReplace: IOnTextReplace
  79. },
  80. isInstalledApp: boolean,
  81. installedAppId = '',
  82. ) => {
  83. return ssePost(getUrl('workflows/run', isInstalledApp, installedAppId), {
  84. body: {
  85. ...body,
  86. response_mode: 'streaming',
  87. },
  88. }, { onNodeStarted, onWorkflowStarted, onWorkflowFinished, isPublicAPI: !isInstalledApp, onNodeFinished, onTextChunk, onTextReplace })
  89. }
  90. export const fetchAppInfo = async () => {
  91. return get('/site') as Promise<AppData>
  92. }
  93. export const fetchConversations = async (isInstalledApp: boolean, installedAppId = '', last_id?: string, pinned?: boolean, limit?: number) => {
  94. return getAction('get', isInstalledApp)(getUrl('conversations', isInstalledApp, installedAppId), { params: { ...{ limit: limit || 20 }, ...(last_id ? { last_id } : {}), ...(pinned !== undefined ? { pinned } : {}) } }) as Promise<AppConversationData>
  95. }
  96. export const pinConversation = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  97. return getAction('patch', isInstalledApp)(getUrl(`conversations/${id}/pin`, isInstalledApp, installedAppId))
  98. }
  99. export const unpinConversation = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  100. return getAction('patch', isInstalledApp)(getUrl(`conversations/${id}/unpin`, isInstalledApp, installedAppId))
  101. }
  102. export const delConversation = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  103. return getAction('del', isInstalledApp)(getUrl(`conversations/${id}`, isInstalledApp, installedAppId))
  104. }
  105. export const renameConversation = async (isInstalledApp: boolean, installedAppId = '', id: string, name: string) => {
  106. return getAction('post', isInstalledApp)(getUrl(`conversations/${id}/name`, isInstalledApp, installedAppId), { body: { name } })
  107. }
  108. export const generationConversationName = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  109. return getAction('post', isInstalledApp)(getUrl(`conversations/${id}/name`, isInstalledApp, installedAppId), { body: { auto_generate: true } }) as Promise<ConversationItem>
  110. }
  111. export const fetchChatList = async (conversationId: string, isInstalledApp: boolean, installedAppId = '') => {
  112. return getAction('get', isInstalledApp)(getUrl('messages', isInstalledApp, installedAppId), { params: { conversation_id: conversationId, limit: 20, last_id: '' } }) as any
  113. }
  114. // Abandoned API interface
  115. // export const fetchAppVariables = async () => {
  116. // return get(`variables`)
  117. // }
  118. // init value. wait for server update
  119. export const fetchAppParams = async (isInstalledApp: boolean, installedAppId = '') => {
  120. return (getAction('get', isInstalledApp))(getUrl('parameters', isInstalledApp, installedAppId)) as Promise<ChatConfig>
  121. }
  122. export const fetchSystemFeatures = async () => {
  123. return (getAction('get', false))(getUrl('system-features', false, '')) as Promise<SystemFeatures>
  124. }
  125. export const fetchWebSAMLSSOUrl = async (appCode: string, redirectUrl: string) => {
  126. return (getAction('get', false))(getUrl('/enterprise/sso/saml/login', false, ''), {
  127. params: {
  128. app_code: appCode,
  129. redirect_url: redirectUrl,
  130. },
  131. }) as Promise<{ url: string }>
  132. }
  133. export const fetchWebOIDCSSOUrl = async (appCode: string, redirectUrl: string) => {
  134. return (getAction('get', false))(getUrl('/enterprise/sso/oidc/login', false, ''), {
  135. params: {
  136. app_code: appCode,
  137. redirect_url: redirectUrl,
  138. },
  139. }) as Promise<{ url: string }>
  140. }
  141. export const fetchWebOAuth2SSOUrl = async (appCode: string, redirectUrl: string) => {
  142. return (getAction('get', false))(getUrl('/enterprise/sso/oauth2/login', false, ''), {
  143. params: {
  144. app_code: appCode,
  145. redirect_url: redirectUrl,
  146. },
  147. }) as Promise<{ url: string }>
  148. }
  149. export const fetchAppMeta = async (isInstalledApp: boolean, installedAppId = '') => {
  150. return (getAction('get', isInstalledApp))(getUrl('meta', isInstalledApp, installedAppId)) as Promise<AppMeta>
  151. }
  152. export const updateFeedback = async ({ url, body }: { url: string; body: Feedbacktype }, isInstalledApp: boolean, installedAppId = '') => {
  153. return (getAction('post', isInstalledApp))(getUrl(url, isInstalledApp, installedAppId), { body })
  154. }
  155. export const fetchMoreLikeThis = async (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  156. return (getAction('get', isInstalledApp))(getUrl(`/messages/${messageId}/more-like-this`, isInstalledApp, installedAppId), {
  157. params: {
  158. response_mode: 'blocking',
  159. },
  160. })
  161. }
  162. export const saveMessage = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  163. return (getAction('post', isInstalledApp))(getUrl('/saved-messages', isInstalledApp, installedAppId), { body: { message_id: messageId } })
  164. }
  165. export const fetchSavedMessage = async (isInstalledApp: boolean, installedAppId = '') => {
  166. return (getAction('get', isInstalledApp))(getUrl('/saved-messages', isInstalledApp, installedAppId))
  167. }
  168. export const removeMessage = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  169. return (getAction('del', isInstalledApp))(getUrl(`/saved-messages/${messageId}`, isInstalledApp, installedAppId))
  170. }
  171. export const fetchSuggestedQuestions = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  172. return (getAction('get', isInstalledApp))(getUrl(`/messages/${messageId}/suggested-questions`, isInstalledApp, installedAppId))
  173. }
  174. export const audioToText = (url: string, isPublicAPI: boolean, body: FormData) => {
  175. return (getAction('post', !isPublicAPI))(url, { body }, { bodyStringify: false, deleteContentType: true }) as Promise<{ text: string }>
  176. }
  177. export const textToAudio = (url: string, isPublicAPI: boolean, body: FormData) => {
  178. return (getAction('post', !isPublicAPI))(url, { body }, { bodyStringify: false, deleteContentType: true }) as Promise<{ data: string }>
  179. }
  180. export const fetchAccessToken = async (appCode: string) => {
  181. const headers = new Headers()
  182. headers.append('X-App-Code', appCode)
  183. return get('/passport', { headers }) as Promise<{ access_token: string }>
  184. }