share.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import type { IOnCompleted, IOnData, IOnError, IOnMessageEnd, IOnMessageReplace } 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, onError, getAbortController, onMessageEnd, onMessageReplace }: {
  23. onData: IOnData
  24. onCompleted: IOnCompleted
  25. onError: IOnError
  26. onMessageEnd?: IOnMessageEnd
  27. onMessageReplace?: IOnMessageReplace
  28. getAbortController?: (abortController: AbortController) => void
  29. }, isInstalledApp: boolean, installedAppId = '') => {
  30. return ssePost(getUrl('chat-messages', isInstalledApp, installedAppId), {
  31. body: {
  32. ...body,
  33. response_mode: 'streaming',
  34. },
  35. }, { onData, onCompleted, isPublicAPI: !isInstalledApp, onError, getAbortController, onMessageEnd, onMessageReplace })
  36. }
  37. export const stopChatMessageResponding = async (appId: string, taskId: string, isInstalledApp: boolean, installedAppId = '') => {
  38. return getAction('post', isInstalledApp)(getUrl(`chat-messages/${taskId}/stop`, isInstalledApp, installedAppId))
  39. }
  40. export const sendCompletionMessage = async (body: Record<string, any>, { onData, onCompleted, onError, onMessageReplace }: {
  41. onData: IOnData
  42. onCompleted: IOnCompleted
  43. onError: IOnError
  44. onMessageReplace: IOnMessageReplace
  45. }, isInstalledApp: boolean, installedAppId = '') => {
  46. return ssePost(getUrl('completion-messages', isInstalledApp, installedAppId), {
  47. body: {
  48. ...body,
  49. response_mode: 'streaming',
  50. },
  51. }, { onData, onCompleted, isPublicAPI: !isInstalledApp, onError, onMessageReplace })
  52. }
  53. export const fetchAppInfo = async () => {
  54. return get('/site')
  55. }
  56. export const fetchConversations = async (isInstalledApp: boolean, installedAppId = '', last_id?: string, pinned?: boolean, limit?: number) => {
  57. return getAction('get', isInstalledApp)(getUrl('conversations', isInstalledApp, installedAppId), { params: { ...{ limit: limit || 20 }, ...(last_id ? { last_id } : {}), ...(pinned !== undefined ? { pinned } : {}) } })
  58. }
  59. export const pinConversation = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  60. return getAction('patch', isInstalledApp)(getUrl(`conversations/${id}/pin`, isInstalledApp, installedAppId))
  61. }
  62. export const unpinConversation = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  63. return getAction('patch', isInstalledApp)(getUrl(`conversations/${id}/unpin`, isInstalledApp, installedAppId))
  64. }
  65. export const delConversation = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  66. return getAction('del', isInstalledApp)(getUrl(`conversations/${id}`, isInstalledApp, installedAppId))
  67. }
  68. export const renameConversation = async (isInstalledApp: boolean, installedAppId = '', id: string, name: string) => {
  69. return getAction('post', isInstalledApp)(getUrl(`conversations/${id}/name`, isInstalledApp, installedAppId), { body: { name } })
  70. }
  71. export const generationConversationName = async (isInstalledApp: boolean, installedAppId = '', id: string) => {
  72. return getAction('post', isInstalledApp)(getUrl(`conversations/${id}/name`, isInstalledApp, installedAppId), { body: { auto_generate: true } })
  73. }
  74. export const fetchChatList = async (conversationId: string, isInstalledApp: boolean, installedAppId = '') => {
  75. return getAction('get', isInstalledApp)(getUrl('messages', isInstalledApp, installedAppId), { params: { conversation_id: conversationId, limit: 20, last_id: '' } })
  76. }
  77. // Abandoned API interface
  78. // export const fetchAppVariables = async () => {
  79. // return get(`variables`)
  80. // }
  81. // init value. wait for server update
  82. export const fetchAppParams = async (isInstalledApp: boolean, installedAppId = '') => {
  83. return (getAction('get', isInstalledApp))(getUrl('parameters', isInstalledApp, installedAppId))
  84. }
  85. export const updateFeedback = async ({ url, body }: { url: string; body: Feedbacktype }, isInstalledApp: boolean, installedAppId = '') => {
  86. return (getAction('post', isInstalledApp))(getUrl(url, isInstalledApp, installedAppId), { body })
  87. }
  88. export const fetchMoreLikeThis = async (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  89. return (getAction('get', isInstalledApp))(getUrl(`/messages/${messageId}/more-like-this`, isInstalledApp, installedAppId), {
  90. params: {
  91. response_mode: 'blocking',
  92. },
  93. })
  94. }
  95. export const saveMessage = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  96. return (getAction('post', isInstalledApp))(getUrl('/saved-messages', isInstalledApp, installedAppId), { body: { message_id: messageId } })
  97. }
  98. export const fetchSavedMessage = async (isInstalledApp: boolean, installedAppId = '') => {
  99. return (getAction('get', isInstalledApp))(getUrl('/saved-messages', isInstalledApp, installedAppId))
  100. }
  101. export const removeMessage = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  102. return (getAction('del', isInstalledApp))(getUrl(`/saved-messages/${messageId}`, isInstalledApp, installedAppId))
  103. }
  104. export const fetchSuggestedQuestions = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  105. return (getAction('get', isInstalledApp))(getUrl(`/messages/${messageId}/suggested-questions`, isInstalledApp, installedAppId))
  106. }
  107. export const audioToText = (url: string, isPublicAPI: boolean, body: FormData) => {
  108. return (getAction('post', !isPublicAPI))(url, { body }, { bodyStringify: false, deleteContentType: true }) as Promise<{ text: string }>
  109. }
  110. export const fetchAccessToken = async (appCode: string) => {
  111. const headers = new Headers()
  112. headers.append('X-App-Code', appCode)
  113. return get('/passport', { headers }) as Promise<{ access_token: string }>
  114. }