share.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import type { IOnCompleted, IOnData, IOnError, IOnFile, IOnIterationFinished, IOnIterationNext, IOnIterationStarted, IOnMessageEnd, IOnMessageReplace, IOnNodeFinished, IOnNodeStarted, IOnTTSChunk, IOnTTSEnd, 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/base/chat/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, onTTSChunk, onTTSEnd }: {
  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. onTTSChunk?: IOnTTSChunk
  40. onTTSEnd?: IOnTTSEnd
  41. }, isInstalledApp: boolean, installedAppId = '') => {
  42. return ssePost(getUrl('chat-messages', isInstalledApp, installedAppId), {
  43. body: {
  44. ...body,
  45. response_mode: 'streaming',
  46. },
  47. }, { onData, onCompleted, onThought, onFile, isPublicAPI: !isInstalledApp, onError, getAbortController, onMessageEnd, onMessageReplace, onTTSChunk, onTTSEnd })
  48. }
  49. export const stopChatMessageResponding = async (appId: string, taskId: string, isInstalledApp: boolean, installedAppId = '') => {
  50. return getAction('post', isInstalledApp)(getUrl(`chat-messages/${taskId}/stop`, isInstalledApp, installedAppId))
  51. }
  52. export const sendCompletionMessage = async (body: Record<string, any>, { onData, onCompleted, onError, onMessageReplace }: {
  53. onData: IOnData
  54. onCompleted: IOnCompleted
  55. onError: IOnError
  56. onMessageReplace: IOnMessageReplace
  57. }, isInstalledApp: boolean, installedAppId = '') => {
  58. return ssePost(getUrl('completion-messages', isInstalledApp, installedAppId), {
  59. body: {
  60. ...body,
  61. response_mode: 'streaming',
  62. },
  63. }, { onData, onCompleted, isPublicAPI: !isInstalledApp, onError, onMessageReplace })
  64. }
  65. export const sendWorkflowMessage = async (
  66. body: Record<string, any>,
  67. {
  68. onWorkflowStarted,
  69. onNodeStarted,
  70. onNodeFinished,
  71. onWorkflowFinished,
  72. onIterationStart,
  73. onIterationNext,
  74. onIterationFinish,
  75. onTextChunk,
  76. onTextReplace,
  77. }: {
  78. onWorkflowStarted: IOnWorkflowStarted
  79. onNodeStarted: IOnNodeStarted
  80. onNodeFinished: IOnNodeFinished
  81. onWorkflowFinished: IOnWorkflowFinished
  82. onIterationStart: IOnIterationStarted
  83. onIterationNext: IOnIterationNext
  84. onIterationFinish: IOnIterationFinished
  85. onTextChunk: IOnTextChunk
  86. onTextReplace: IOnTextReplace
  87. },
  88. isInstalledApp: boolean,
  89. installedAppId = '',
  90. ) => {
  91. return ssePost(getUrl('workflows/run', isInstalledApp, installedAppId), {
  92. body: {
  93. ...body,
  94. response_mode: 'streaming',
  95. },
  96. }, { onNodeStarted, onWorkflowStarted, onWorkflowFinished, isPublicAPI: !isInstalledApp, onNodeFinished, onIterationStart, onIterationNext, onIterationFinish, onTextChunk, onTextReplace })
  97. }
  98. export const fetchAppInfo = async () => {
  99. return get('/site') as Promise<AppData>
  100. }
  101. export const fetchConversations = async (isInstalledApp: boolean, installedAppId = '', last_id?: string, pinned?: boolean, limit?: number) => {
  102. return getAction('get', isInstalledApp)(getUrl('conversations', isInstalledApp, installedAppId), { params: { ...{ limit: limit || 20 }, ...(last_id ? { last_id } : {}), ...(pinned !== undefined ? { pinned } : {}) } }) as Promise<AppConversationData>
  103. }
  104. export const pinConversation = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  105. return getAction('patch', isInstalledApp)(getUrl(`conversations/${id}/pin`, isInstalledApp, installedAppId))
  106. }
  107. export const unpinConversation = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  108. return getAction('patch', isInstalledApp)(getUrl(`conversations/${id}/unpin`, isInstalledApp, installedAppId))
  109. }
  110. export const delConversation = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  111. return getAction('del', isInstalledApp)(getUrl(`conversations/${id}`, isInstalledApp, installedAppId))
  112. }
  113. export const renameConversation = async (isInstalledApp: boolean, installedAppId = '', id: string, name: string) => {
  114. return getAction('post', isInstalledApp)(getUrl(`conversations/${id}/name`, isInstalledApp, installedAppId), { body: { name } })
  115. }
  116. export const generationConversationName = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  117. return getAction('post', isInstalledApp)(getUrl(`conversations/${id}/name`, isInstalledApp, installedAppId), { body: { auto_generate: true } }) as Promise<ConversationItem>
  118. }
  119. export const fetchChatList = async (conversationId: string, isInstalledApp: boolean, installedAppId = '') => {
  120. return getAction('get', isInstalledApp)(getUrl('messages', isInstalledApp, installedAppId), { params: { conversation_id: conversationId, limit: 20, last_id: '' } }) as any
  121. }
  122. // Abandoned API interface
  123. // export const fetchAppVariables = async () => {
  124. // return get(`variables`)
  125. // }
  126. // init value. wait for server update
  127. export const fetchAppParams = async (isInstalledApp: boolean, installedAppId = '') => {
  128. return (getAction('get', isInstalledApp))(getUrl('parameters', isInstalledApp, installedAppId)) as Promise<ChatConfig>
  129. }
  130. export const fetchSystemFeatures = async () => {
  131. return (getAction('get', false))(getUrl('system-features', false, '')) as Promise<SystemFeatures>
  132. }
  133. export const fetchWebSAMLSSOUrl = async (appCode: string, redirectUrl: string) => {
  134. return (getAction('get', false))(getUrl('/enterprise/sso/saml/login', false, ''), {
  135. params: {
  136. app_code: appCode,
  137. redirect_url: redirectUrl,
  138. },
  139. }) as Promise<{ url: string }>
  140. }
  141. export const fetchWebOIDCSSOUrl = async (appCode: string, redirectUrl: string) => {
  142. return (getAction('get', false))(getUrl('/enterprise/sso/oidc/login', false, ''), {
  143. params: {
  144. app_code: appCode,
  145. redirect_url: redirectUrl,
  146. },
  147. }) as Promise<{ url: string }>
  148. }
  149. export const fetchWebOAuth2SSOUrl = async (appCode: string, redirectUrl: string) => {
  150. return (getAction('get', false))(getUrl('/enterprise/sso/oauth2/login', false, ''), {
  151. params: {
  152. app_code: appCode,
  153. redirect_url: redirectUrl,
  154. },
  155. }) as Promise<{ url: string }>
  156. }
  157. export const fetchAppMeta = async (isInstalledApp: boolean, installedAppId = '') => {
  158. return (getAction('get', isInstalledApp))(getUrl('meta', isInstalledApp, installedAppId)) as Promise<AppMeta>
  159. }
  160. export const updateFeedback = async ({ url, body }: { url: string; body: FeedbackType }, isInstalledApp: boolean, installedAppId = '') => {
  161. return (getAction('post', isInstalledApp))(getUrl(url, isInstalledApp, installedAppId), { body })
  162. }
  163. export const fetchMoreLikeThis = async (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  164. return (getAction('get', isInstalledApp))(getUrl(`/messages/${messageId}/more-like-this`, isInstalledApp, installedAppId), {
  165. params: {
  166. response_mode: 'blocking',
  167. },
  168. })
  169. }
  170. export const saveMessage = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  171. return (getAction('post', isInstalledApp))(getUrl('/saved-messages', isInstalledApp, installedAppId), { body: { message_id: messageId } })
  172. }
  173. export const fetchSavedMessage = async (isInstalledApp: boolean, installedAppId = '') => {
  174. return (getAction('get', isInstalledApp))(getUrl('/saved-messages', isInstalledApp, installedAppId))
  175. }
  176. export const removeMessage = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  177. return (getAction('del', isInstalledApp))(getUrl(`/saved-messages/${messageId}`, isInstalledApp, installedAppId))
  178. }
  179. export const fetchSuggestedQuestions = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  180. return (getAction('get', isInstalledApp))(getUrl(`/messages/${messageId}/suggested-questions`, isInstalledApp, installedAppId))
  181. }
  182. export const audioToText = (url: string, isPublicAPI: boolean, body: FormData) => {
  183. return (getAction('post', !isPublicAPI))(url, { body }, { bodyStringify: false, deleteContentType: true }) as Promise<{ text: string }>
  184. }
  185. export const textToAudio = (url: string, isPublicAPI: boolean, body: FormData) => {
  186. return (getAction('post', !isPublicAPI))(url, { body }, { bodyStringify: false, deleteContentType: true }) as Promise<{ data: string }>
  187. }
  188. export const textToAudioStream = (url: string, isPublicAPI: boolean, header: { content_type: string }, body: { streaming: boolean; voice?: string; message_id?: string; text?: string | null | undefined }) => {
  189. return (getAction('post', !isPublicAPI))(url, { body, header }, { needAllResponseContent: true })
  190. }
  191. export const fetchAccessToken = async (appCode: string) => {
  192. const headers = new Headers()
  193. headers.append('X-App-Code', appCode)
  194. return get('/passport', { headers }) as Promise<{ access_token: string }>
  195. }