share.ts 8.7 KB

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