share.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. 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 fetchAppInfo = async () => {
  63. return get('/site') as Promise<AppData>
  64. }
  65. export const fetchConversations = async (isInstalledApp: boolean, installedAppId = '', last_id?: string, pinned?: boolean, limit?: number) => {
  66. return getAction('get', isInstalledApp)(getUrl('conversations', isInstalledApp, installedAppId), { params: { ...{ limit: limit || 20 }, ...(last_id ? { last_id } : {}), ...(pinned !== undefined ? { pinned } : {}) } }) as Promise<AppConversationData>
  67. }
  68. export const pinConversation = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  69. return getAction('patch', isInstalledApp)(getUrl(`conversations/${id}/pin`, isInstalledApp, installedAppId))
  70. }
  71. export const unpinConversation = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  72. return getAction('patch', isInstalledApp)(getUrl(`conversations/${id}/unpin`, isInstalledApp, installedAppId))
  73. }
  74. export const delConversation = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  75. return getAction('del', isInstalledApp)(getUrl(`conversations/${id}`, isInstalledApp, installedAppId))
  76. }
  77. export const renameConversation = async (isInstalledApp: boolean, installedAppId = '', id: string, name: string) => {
  78. return getAction('post', isInstalledApp)(getUrl(`conversations/${id}/name`, isInstalledApp, installedAppId), { body: { name } })
  79. }
  80. export const generationConversationName = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  81. return getAction('post', isInstalledApp)(getUrl(`conversations/${id}/name`, isInstalledApp, installedAppId), { body: { auto_generate: true } }) as Promise<ConversationItem>
  82. }
  83. export const fetchChatList = async (conversationId: string, isInstalledApp: boolean, installedAppId = '') => {
  84. return getAction('get', isInstalledApp)(getUrl('messages', isInstalledApp, installedAppId), { params: { conversation_id: conversationId, limit: 20, last_id: '' } }) as any
  85. }
  86. // Abandoned API interface
  87. // export const fetchAppVariables = async () => {
  88. // return get(`variables`)
  89. // }
  90. // init value. wait for server update
  91. export const fetchAppParams = async (isInstalledApp: boolean, installedAppId = '') => {
  92. return (getAction('get', isInstalledApp))(getUrl('parameters', isInstalledApp, installedAppId)) as Promise<ChatConfig>
  93. }
  94. export const fetchAppMeta = async (isInstalledApp: boolean, installedAppId = '') => {
  95. return (getAction('get', isInstalledApp))(getUrl('meta', isInstalledApp, installedAppId)) as Promise<AppMeta>
  96. }
  97. export const updateFeedback = async ({ url, body }: { url: string; body: Feedbacktype }, isInstalledApp: boolean, installedAppId = '') => {
  98. return (getAction('post', isInstalledApp))(getUrl(url, isInstalledApp, installedAppId), { body })
  99. }
  100. export const fetchMoreLikeThis = async (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  101. return (getAction('get', isInstalledApp))(getUrl(`/messages/${messageId}/more-like-this`, isInstalledApp, installedAppId), {
  102. params: {
  103. response_mode: 'blocking',
  104. },
  105. })
  106. }
  107. export const saveMessage = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  108. return (getAction('post', isInstalledApp))(getUrl('/saved-messages', isInstalledApp, installedAppId), { body: { message_id: messageId } })
  109. }
  110. export const fetchSavedMessage = async (isInstalledApp: boolean, installedAppId = '') => {
  111. return (getAction('get', isInstalledApp))(getUrl('/saved-messages', isInstalledApp, installedAppId))
  112. }
  113. export const removeMessage = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  114. return (getAction('del', isInstalledApp))(getUrl(`/saved-messages/${messageId}`, isInstalledApp, installedAppId))
  115. }
  116. export const fetchSuggestedQuestions = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  117. return (getAction('get', isInstalledApp))(getUrl(`/messages/${messageId}/suggested-questions`, isInstalledApp, installedAppId))
  118. }
  119. export const audioToText = (url: string, isPublicAPI: boolean, body: FormData) => {
  120. return (getAction('post', !isPublicAPI))(url, { body }, { bodyStringify: false, deleteContentType: true }) as Promise<{ text: string }>
  121. }
  122. export const textToAudio = (url: string, isPublicAPI: boolean, body: FormData) => {
  123. return (getAction('post', !isPublicAPI))(url, { body }, { bodyStringify: false, deleteContentType: true }) as Promise<{ data: string }>
  124. }
  125. export const fetchAccessToken = async (appCode: string) => {
  126. const headers = new Headers()
  127. headers.append('X-App-Code', appCode)
  128. return get('/passport', { headers }) as Promise<{ access_token: string }>
  129. }