share.ts 8.0 KB

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