share.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import type { IOnCompleted, IOnData, IOnError } from './base'
  2. import {
  3. del as consoleDel, get as consoleGet, post as consolePost,
  4. delPublic as del, getPublic as get, postPublic as post, ssePost,
  5. } from './base'
  6. import type { Feedbacktype } from '@/app/components/app/chat'
  7. function getAction(action: 'get' | 'post' | 'del', isInstalledApp: boolean) {
  8. switch (action) {
  9. case 'get':
  10. return isInstalledApp ? consoleGet : get
  11. case 'post':
  12. return isInstalledApp ? consolePost : post
  13. case 'del':
  14. return isInstalledApp ? consoleDel : del
  15. }
  16. }
  17. function getUrl(url: string, isInstalledApp: boolean, installedAppId: string) {
  18. return isInstalledApp ? `installed-apps/${installedAppId}/${url.startsWith('/') ? url.slice(1) : url}` : url
  19. }
  20. export const sendChatMessage = async (body: Record<string, any>, { onData, onCompleted, onError, getAbortController }: {
  21. onData: IOnData
  22. onCompleted: IOnCompleted
  23. onError: IOnError
  24. getAbortController?: (abortController: AbortController) => void
  25. }, isInstalledApp: boolean, installedAppId = '') => {
  26. return ssePost(getUrl('chat-messages', isInstalledApp, installedAppId), {
  27. body: {
  28. ...body,
  29. response_mode: 'streaming',
  30. },
  31. }, { onData, onCompleted, isPublicAPI: !isInstalledApp, onError, getAbortController })
  32. }
  33. export const stopChatMessageResponding = async (appId: string, taskId: string, isInstalledApp: boolean, installedAppId = '') => {
  34. return getAction('post', isInstalledApp)(getUrl(`chat-messages/${taskId}/stop`, isInstalledApp, installedAppId))
  35. }
  36. export const sendCompletionMessage = async (body: Record<string, any>, { onData, onCompleted, onError }: {
  37. onData: IOnData
  38. onCompleted: IOnCompleted
  39. onError: IOnError
  40. }, isInstalledApp: boolean, installedAppId = '') => {
  41. return ssePost(getUrl('completion-messages', isInstalledApp, installedAppId), {
  42. body: {
  43. ...body,
  44. response_mode: 'streaming',
  45. },
  46. }, { onData, onCompleted, isPublicAPI: !isInstalledApp, onError })
  47. }
  48. export const fetchAppInfo = async () => {
  49. return get('/site')
  50. }
  51. export const fetchConversations = async (isInstalledApp: boolean, installedAppId = '', last_id?: string) => {
  52. return getAction('get', isInstalledApp)(getUrl('conversations', isInstalledApp, installedAppId), { params: { ...{ limit: 20 }, ...(last_id ? { last_id } : {}) } })
  53. }
  54. export const fetchChatList = async (conversationId: string, isInstalledApp: boolean, installedAppId = '') => {
  55. return getAction('get', isInstalledApp)(getUrl('messages', isInstalledApp, installedAppId), { params: { conversation_id: conversationId, limit: 20, last_id: '' } })
  56. }
  57. // Abandoned API interface
  58. // export const fetchAppVariables = async () => {
  59. // return get(`variables`)
  60. // }
  61. // init value. wait for server update
  62. export const fetchAppParams = async (isInstalledApp: boolean, installedAppId = '') => {
  63. return (getAction('get', isInstalledApp))(getUrl('parameters', isInstalledApp, installedAppId))
  64. }
  65. export const updateFeedback = async ({ url, body }: { url: string; body: Feedbacktype }, isInstalledApp: boolean, installedAppId = '') => {
  66. return (getAction('post', isInstalledApp))(getUrl(url, isInstalledApp, installedAppId), { body })
  67. }
  68. export const fetchMoreLikeThis = async (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  69. return (getAction('get', isInstalledApp))(getUrl(`/messages/${messageId}/more-like-this`, isInstalledApp, installedAppId), {
  70. params: {
  71. response_mode: 'blocking',
  72. },
  73. })
  74. }
  75. export const saveMessage = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  76. return (getAction('post', isInstalledApp))(getUrl('/saved-messages', isInstalledApp, installedAppId), { body: { message_id: messageId } })
  77. }
  78. export const fetchSavedMessage = async (isInstalledApp: boolean, installedAppId = '') => {
  79. return (getAction('get', isInstalledApp))(getUrl('/saved-messages', isInstalledApp, installedAppId))
  80. }
  81. export const removeMessage = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  82. return (getAction('del', isInstalledApp))(getUrl(`/saved-messages/${messageId}`, isInstalledApp, installedAppId))
  83. }
  84. export const fetchSuggestedQuestions = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  85. return (getAction('get', isInstalledApp))(getUrl(`/messages/${messageId}/suggested-questions`, isInstalledApp, installedAppId))
  86. }