share.ts 6.4 KB

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