share.ts 5.9 KB

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