share.ts 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import type { IOnCompleted, IOnData, IOnError } from './base'
  2. import {
  3. get as consoleGet, post as consolePost, del as consoleDel,
  4. getPublic as get, postPublic as post, ssePost, delPublic as del
  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 sendCompletionMessage = async (body: Record<string, any>, { onData, onCompleted, onError }: {
  34. onData: IOnData
  35. onCompleted: IOnCompleted
  36. onError: IOnError
  37. }, isInstalledApp: boolean, installedAppId = '') => {
  38. return ssePost(getUrl('completion-messages', isInstalledApp, installedAppId), {
  39. body: {
  40. ...body,
  41. response_mode: 'streaming',
  42. },
  43. }, { onData, onCompleted, isPublicAPI: !isInstalledApp, onError })
  44. }
  45. export const fetchAppInfo = async () => {
  46. return get('/site')
  47. }
  48. export const fetchConversations = async (isInstalledApp: boolean, installedAppId='', last_id?: string) => {
  49. return getAction('get', isInstalledApp)(getUrl('conversations', isInstalledApp, installedAppId), { params: {...{ limit: 20 }, ...(last_id ? { last_id } : {}) } })
  50. }
  51. export const fetchChatList = async (conversationId: string, isInstalledApp: boolean, installedAppId='') => {
  52. return getAction('get', isInstalledApp)(getUrl('messages', isInstalledApp, installedAppId), { params: { conversation_id: conversationId, limit: 20, last_id: '' } })
  53. }
  54. // Abandoned API interface
  55. // export const fetchAppVariables = async () => {
  56. // return get(`variables`)
  57. // }
  58. // init value. wait for server update
  59. export const fetchAppParams = async (isInstalledApp: boolean, installedAppId = '') => {
  60. return (getAction('get', isInstalledApp))(getUrl('parameters', isInstalledApp, installedAppId))
  61. }
  62. export const updateFeedback = async ({ url, body }: { url: string; body: Feedbacktype }, isInstalledApp: boolean, installedAppId = '') => {
  63. return (getAction('post', isInstalledApp))(getUrl(url, isInstalledApp, installedAppId), { body })
  64. }
  65. export const fetchMoreLikeThis = async (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  66. return (getAction('get', isInstalledApp))(getUrl(`/messages/${messageId}/more-like-this`, isInstalledApp, installedAppId), {
  67. params: {
  68. response_mode: 'blocking',
  69. }
  70. })
  71. }
  72. export const saveMessage = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  73. return (getAction('post', isInstalledApp))(getUrl('/saved-messages', isInstalledApp, installedAppId), { body: { message_id: messageId } })
  74. }
  75. export const fetchSavedMessage = async (isInstalledApp: boolean, installedAppId = '') => {
  76. return (getAction('get', isInstalledApp))(getUrl(`/saved-messages`, isInstalledApp, installedAppId))
  77. }
  78. export const removeMessage = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  79. return (getAction('del', isInstalledApp))(getUrl(`/saved-messages/${messageId}`, isInstalledApp, installedAppId))
  80. }
  81. export const fetchSuggestedQuestions = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
  82. return (getAction('get', isInstalledApp))(getUrl(`/messages/${messageId}/suggested-questions`, isInstalledApp, installedAppId))
  83. }