share.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import type { IOnCompleted, IOnData, IOnError, IOnFile, IOnMessageEnd, IOnMessageReplace, IOnThought } 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. function getAction(action: 'get' | 'post' | 'del' | 'patch', isInstalledApp: boolean) {
  8. switch (action) {
  9. case 'get':
  10. return isInstalledApp ? consoleGet : get
  11. case 'post':
  12. return isInstalledApp ? consolePost : post
  13. case 'patch':
  14. return isInstalledApp ? consolePatch : patch
  15. case 'del':
  16. return isInstalledApp ? consoleDel : del
  17. }
  18. }
  19. function getUrl(url: string, isInstalledApp: boolean, installedAppId: string) {
  20. return isInstalledApp ? `installed-apps/${installedAppId}/${url.startsWith('/') ? url.slice(1) : url}` : url
  21. }
  22. export const sendChatMessage = async (body: Record<string, any>, { onData, onCompleted, onThought, onFile, onError, getAbortController, onMessageEnd, onMessageReplace }: {
  23. onData: IOnData
  24. onCompleted: IOnCompleted
  25. onFile: IOnFile
  26. onThought: IOnThought
  27. onError: IOnError
  28. onMessageEnd?: IOnMessageEnd
  29. onMessageReplace?: IOnMessageReplace
  30. getAbortController?: (abortController: AbortController) => void
  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 })
  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: IOnData
  44. onCompleted: IOnCompleted
  45. onError: IOnError
  46. onMessageReplace: IOnMessageReplace
  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 fetchAppInfo = async () => {
  56. return get('/site')
  57. }
  58. export const fetchConversations = async (isInstalledApp: boolean, installedAppId = '', last_id?: string, pinned?: boolean, limit?: number) => {
  59. return getAction('get', isInstalledApp)(getUrl('conversations', isInstalledApp, installedAppId), { params: { ...{ limit: limit || 20 }, ...(last_id ? { last_id } : {}), ...(pinned !== undefined ? { pinned } : {}) } })
  60. }
  61. export const pinConversation = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  62. return getAction('patch', isInstalledApp)(getUrl(`conversations/${id}/pin`, isInstalledApp, installedAppId))
  63. }
  64. export const unpinConversation = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  65. return getAction('patch', isInstalledApp)(getUrl(`conversations/${id}/unpin`, isInstalledApp, installedAppId))
  66. }
  67. export const delConversation = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  68. return getAction('del', isInstalledApp)(getUrl(`conversations/${id}`, isInstalledApp, installedAppId))
  69. }
  70. export const renameConversation = async (isInstalledApp: boolean, installedAppId = '', id: string, name: string) => {
  71. return getAction('post', isInstalledApp)(getUrl(`conversations/${id}/name`, isInstalledApp, installedAppId), { body: { name } })
  72. }
  73. export const generationConversationName = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  74. return getAction('post', isInstalledApp)(getUrl(`conversations/${id}/name`, isInstalledApp, installedAppId), { body: { auto_generate: true } })
  75. }
  76. export const fetchChatList = async (conversationId: string, isInstalledApp: boolean, installedAppId = '') => {
  77. return getAction('get', isInstalledApp)(getUrl('messages', isInstalledApp, installedAppId), { params: { conversation_id: conversationId, limit: 20, last_id: '' } })
  78. }
  79. // Abandoned API interface
  80. // export const fetchAppVariables = async () => {
  81. // return get(`variables`)
  82. // }
  83. // init value. wait for server update
  84. export const fetchAppParams = async (isInstalledApp: boolean, installedAppId = '') => {
  85. return (getAction('get', isInstalledApp))(getUrl('parameters', isInstalledApp, installedAppId))
  86. }
  87. export const fetchAppMeta = async (isInstalledApp: boolean, installedAppId = '') => {
  88. return (getAction('get', isInstalledApp))(getUrl('meta', isInstalledApp, installedAppId))
  89. }
  90. export const updateFeedback = async ({ url, body }: { url: string; body: Feedbacktype }, isInstalledApp: boolean, installedAppId = '') => {
  91. return (getAction('post', isInstalledApp))(getUrl(url, isInstalledApp, installedAppId), { body })
  92. }
  93. export const fetchMoreLikeThis = async (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  94. return (getAction('get', isInstalledApp))(getUrl(`/messages/${messageId}/more-like-this`, isInstalledApp, installedAppId), {
  95. params: {
  96. response_mode: 'blocking',
  97. },
  98. })
  99. }
  100. export const saveMessage = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  101. return (getAction('post', isInstalledApp))(getUrl('/saved-messages', isInstalledApp, installedAppId), { body: { message_id: messageId } })
  102. }
  103. export const fetchSavedMessage = async (isInstalledApp: boolean, installedAppId = '') => {
  104. return (getAction('get', isInstalledApp))(getUrl('/saved-messages', isInstalledApp, installedAppId))
  105. }
  106. export const removeMessage = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  107. return (getAction('del', isInstalledApp))(getUrl(`/saved-messages/${messageId}`, isInstalledApp, installedAppId))
  108. }
  109. export const fetchSuggestedQuestions = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  110. return (getAction('get', isInstalledApp))(getUrl(`/messages/${messageId}/suggested-questions`, isInstalledApp, installedAppId))
  111. }
  112. export const audioToText = (url: string, isPublicAPI: boolean, body: FormData) => {
  113. return (getAction('post', !isPublicAPI))(url, { body }, { bodyStringify: false, deleteContentType: true }) as Promise<{ text: string }>
  114. }
  115. export const textToAudio = (url: string, isPublicAPI: boolean, body: FormData) => {
  116. return (getAction('post', !isPublicAPI))(url, { body }, { bodyStringify: false, deleteContentType: true }) as Promise<{ data: string }>
  117. }
  118. export const fetchAccessToken = async (appCode: string) => {
  119. const headers = new Headers()
  120. headers.append('X-App-Code', appCode)
  121. return get('/passport', { headers }) as Promise<{ access_token: string }>
  122. }